-
Notifications
You must be signed in to change notification settings - Fork 14
/
abaq.py
272 lines (180 loc) · 6.04 KB
/
abaq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from compas_fea.fea import Writer
from compas_fea.fea.abaq import launch_job
from compas_fea.fea.abaq import odb_extract
from subprocess import Popen
from subprocess import PIPE
from time import time
import json
import os
# Author(s): Andrew Liew (github.com/andrewliew)
__all__ = [
'input_generate',
'extract_data',
'launch_process',
]
node_fields = ['rf', 'rm', 'u', 'ur', 'cf', 'cm']
element_fields = ['sf', 'sm', 'sk', 'se', 's', 'e', 'pe', 'rbfor', 'ctf']
def input_generate(structure, fields, output):
""" Creates the Abaqus .inp file from the Structure object.
Parameters
----------
structure : obj
The Structure object to read from.
fields : list
Data field requests.
output : bool
Print terminal output.
Returns
-------
None
"""
filename = '{0}{1}.inp'.format(structure.path, structure.name)
if isinstance(fields, str):
fields = [fields]
if 'u' not in fields:
fields.append('u')
with Writer(structure=structure, software='abaqus', filename=filename, fields=fields) as writer:
writer.write_heading()
writer.write_nodes()
writer.write_node_sets()
writer.write_boundary_conditions()
writer.write_materials()
writer.write_elements()
writer.write_element_sets()
writer.write_steps()
if output:
print('***** Abaqus input file generated: {0} *****\n'.format(filename))
def launch_process(structure, exe, cpus, output):
""" Runs the analysis through Abaqus.
Parameters
----------
structure : obj
Structure object.
exe : str
Abaqus exe path to bypass defaults.
cpus : int
Number of CPU cores to use.
output : bool
Print terminal output.
Returns
-------
None
"""
name = structure.name
path = structure.path
temp = '{0}{1}/'.format(path, name)
# Analyse
tic = time()
subprocess = 'noGUI={0}'.format(launch_job.__file__.replace('\\', '/'))
success = False
if not exe:
args = ['abaqus', 'cae', subprocess, '--', str(cpus), path, name]
p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=temp, shell=True)
while True:
line = p.stdout.readline()
if not line:
break
line = str(line.strip())
if output:
print(line)
if 'COMPLETED' in line:
success = True
stdout, stderr = p.communicate()
if output:
print(stdout)
print(stderr)
else:
os.chdir(temp)
os.system('{0} {1} -- {2} {3} {4}'.format(exe, subprocess, cpus, path, name))
success = True
toc = time() - tic
if not success:
try:
with open(temp + name + '.sta', 'r') as f:
if 'COMPLETED SUCCESSFULLY' in f.readlines()[-1]:
success = True
except Exception:
pass
if success:
if output:
print('***** Analysis successful - analysis time : {0} s *****'.format(toc))
else:
print('***** Analysis failed *****')
def extract_data(structure, fields, exe, output, return_data, components):
""" Extract data from the Abaqus .odb file.
Parameters
----------
structure : obj
Structure object.
fields : list
Data field requests.
exe : str
Abaqus exe path to bypass defaults.
output : bool
Print terminal output.
return_data : bool
Return data back into structure.results.
components : list
Specific components to extract from the fields data.
Returns
-------
None
"""
# Extract
name = structure.name
path = structure.path
temp = '{0}{1}/'.format(path, name)
if isinstance(fields, str):
fields = [fields]
fields = ','.join(fields)
components = ','.join(components) if components else 'None'
tic1 = time()
subprocess = 'noGUI={0}'.format(odb_extract.__file__.replace('\\', '/'))
if not exe:
args = ['abaqus', 'cae', subprocess, '--', components, fields, name, temp]
p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=temp, shell=True)
while True:
line = p.stdout.readline()
if not line:
break
line = str(line.strip())
if output:
print(line)
stdout, stderr = p.communicate()
if output:
print(stdout)
print(stderr)
else:
os.chdir(temp)
os.system('{0}{1} -- {2} {3} {4} {5}'.format(exe, subprocess, components, fields, name, temp))
toc1 = time() - tic1
if output:
print('\n***** Data extracted from Abaqus .odb file : {0:.3f} s *****\n'.format(toc1))
# Save results to Structure
if return_data:
try:
tic2 = time()
with open('{0}{1}-results.json'.format(temp, name), 'r') as f:
results = json.load(f)
with open('{0}{1}-info.json'.format(temp, name), 'r') as f:
info = json.load(f)
for step in results:
for dtype in results[step]:
if dtype in ['nodal', 'element']:
for field in results[step][dtype]:
data = {}
for key in results[step][dtype][field]:
data[int(key)] = results[step][dtype][field][key]
results[step][dtype][field] = data
structure.results = results
for step in info:
structure.results[step]['info'] = info[step]
toc2 = time() - tic2
if output:
print('***** Saving data to structure.results successful : {0:.3f} s *****\n'.format(toc2))
except Exception:
if output:
print('***** Saving data to structure.results unsuccessful *****')