-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathabaqus_inside.py
451 lines (366 loc) · 13 KB
/
abaqus_inside.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"""Functions to manage Abaqus objects and actions internally.
Intended to be used by the Abaqus CAE Python interpreter.
Developed by Rodrigo Rivero.
https://github.com/rodrigo1392
"""
from abaqus import *
from abaqusConstants import *
from driverUtils import *
import odbAccess
# Flexibilize for Abaqus viewer
try:
from caeModules import *
except:
pass
import ast
import os
import sys
from tools_submodule import filesystem_tools as ft
# Abaqus mesh-stats keywords.
ELEMENT_TYPES = ['numLineElems', 'numMeshedRegions', 'numNodes',
'numPointElems', 'numPyramidElems', 'numQuadElems',
'numTetBoundaryElems', 'numTetElems', 'numTriElems',
'numWedgeElems']
def assign_2d_parts_properties(model_name, section_name,
first_letters=None):
"""Assign section properties to parts in a current database model.
Parameters
----------
model_name : str
Name of model of interest.
section_name : str
Name of section properties object.
first_letters : str, optional
If given, filter parts by initial substring.
Returns
-------
None
"""
# Get list of model parts objects.
parts_list = [i for i in mdb.models[model_name].parts.values()]
# Optionally, filter by first letters.
if first_letters:
parts_list = [i for i in parts_list if
i.name.startswith(first_letters)]
# Iterate over parts objects list and assign properties.
for part in parts_list:
faces = part.faces.getSequenceFromMask(mask=('[#1 ]',), )
region = part.Set(faces=faces, name='BODY')
part.SectionAssignment(region=region, sectionName=section_name,
offset=0.0, offsetType=MIDDLE_SURFACE,
offsetField='', thicknessAssignment=FROM_SECTION)
def clean_parts_properties(model_name, first_letters=None):
"""Deletes section properties assignments of parts in a model.
Parameters
----------
model_name : str
Name of model of interest.
first_letters : str, optional
If given, filter parts by initial substring.
Returns
-------
None
"""
# Get list of model parts objects.
parts_list = [i for i in mdb.models[model_name].parts.values()]
# Optionally, filter by first letters.
if first_letters:
parts_list = [i for i in parts_list if i.name.startswith(first_letters)]
# Iterate over parts objects list and delete all properties.
for part in parts_list:
assignments_number = len(part.sectionAssignments)
for i in range(0, assignments_number):
del part.sectionAssignments[0]
def export_parts_iges(model_name, output_path, first_letters=None):
"""Export parts in a model as iges files.
Parameters
----------
model_name : str
Name of model of interest.
output_path : Path-like str
Path to export iges parts to.
first_letters : str, optional
If given, filter parts by initial substring.
Returns
-------
None
"""
# Get list of model parts objects.
parts_list = [i for i in mdb.models[model_name].parts.values()]
# Optionally, filter by first letters.
if first_letters:
parts_list = [i for i in parts_list if i.name.startswith(first_letters)]
# Iterate over parts objects list and export them.
for part in parts_list:
part.writeIgesFile(fileName=output_path + r"\\" + part.name + '.igs',
flavor=STANDARD)
def extract_set_mesh_nodes(odb, set_name):
"""Get mesh nodes labels and coordinates of a set of points.
The method builds a dictionary with a dict {set name: instance name}
as keys and a list of tuples (mesh node labels : nodes coordinates)
as values, for all the points corresponding to `set_name`.
Parameters
----------
odb : Odb object
To read from.
set_name : str
Name of set of points of interest.
Returns
-------
Dict
{set name: instance name} : [(node labels : nodes coordinates)].
"""
# Normalize input to Odb object
print('Extracting nodes...')
odb = normalize_odb_object(odb)
# Get nodes set, instance names and build output dict.
node_set = odb.rootAssembly.nodeSets[set_name]
instances_names_list = [i for i in node_set.instanceNames]
output = {(set_name, instance_name):
[(node.label, node.coordinates) for node in node_set.nodes[num]]
for num, instance_name in enumerate(instances_names_list)}
return output
def get_folder_calc_time(odbs_folder, show=True, recursive=False,
close_odbs=True):
"""Get job calculation time from all Odb objects in folder.
Parameters
----------
odbs_folder : Path
Folder containing Odb objects.
show : bool, optional
If True, print Odb calculation time.
recursive : bool, optional
If True, list Odb files recursively, including subfolders.
close_odbs : bool, optional
If True, close current session Odb objects at the end of script.
Returns
-------
dict
Odb names : Dict of calculation times pairs.
"""
# List odb full paths.
odb_list = ft.list_files_with_extension(root_path=odbs_folder,
extension='.odb',
full_path=True,
recursively=recursive)
print(len(odb_list), 'Odb objects found')
# Iterate trough Odb files, open them and extract calculation time.
output = {}
for job_key in odb_list:
odb = normalize_odb_object(job_key)
output[job_key] = get_odb_calc_time(odb, show)
# Optionally, close all opened Odb objects in current session.
if close_odbs:
from abaqusMacros import close_all_odbs
close_all_odbs()
return output
def get_odb_calc_time(odb, show=True):
"""Get job calculation time from a Odb object.
Parameters
----------
odb : Odb object
To read data from.
show : bool, optional
If True, print Odb calculation time.
Returns
-------
dict
Seconds as values and systemTime, userTime and wallclockTime
objects as keys.
"""
# Normalize input to Odb object.
odb = normalize_odb_object(odb)
calc_time = odb.diagnosticData.jobTime
# Get calculation time and convert time to dict.
output = ast.literal_eval(str(calc_time)[1:-1])
# Print calculation time.
if show:
odb_name = (os.path.splitext(os.path.basename(odb.name))[0])
print(odb_name, ': ', str(calc_time))
return output
def log_message(input_string):
"""Set an output message to pass to Popen subprocess stdout method.
Parameters
----------
input_string : str
Message to be output.
Returns
-------
None
"""
print >>sys.__stdout__, input_string
def normalize_odb_object(odb_ish):
"""Return a odb object from current session.
If input is already a Odb object, return it. If it is a string,
look for corresponding object within opened Odbs, if none is find,
try to open it.
Parameters
----------
odb_ish : Odb object or string-like Path
Odb object identifier.
Returns
-------
Odb object
Opened Odb object from current session.
"""
# Verify is odb is already opened, if not, try to it.
if isinstance(odb_ish, str):
try:
odb = session.odbs[odb_ish]
except KeyError:
odb = session.openOdb(odb_ish, readOnly=False)
# If neither could be done, just return its reference.
else:
odb = odb_ish
return odb
def print_model_mesh_stats(model_name, total_stats=False):
"""Show amount of mesh elements, instance by instance.
Parameters
----------
model_name : str
Name of model of interest.
total_stats : bool, optional
If True, show whole model stats in addition to instances stats.
Returns
-------
None
"""
# Iterate trough model instances and show mesh stats by
# element type.
print('*****', '\nModel:', model_name, '\n')
model = mdb.models[model_name]
for inst_key, instance in model.rootAssembly.instances.items():
print('\nInstance:', inst_key)
try:
for element_type in ELEMENT_TYPES:
number_of_elements = eval('stats.' + element_type)
if number_of_elements:
print(element_type, ':', number_of_elements)
except AttributeError:
pass
# Optionally, show whole model mesh stats.
if total_stats:
print('\n--- TOTAL ---')
try:
for element_type in ELEMENT_TYPES:
number_of_elements = eval('tot_stats.' + element_type)
if number_of_elements:
print(element_type, ':', number_of_elements)
except AttributeError:
pass
def rename_model(model_name, new_name):
"""Assign a new name to a model in current database.
Parameters
----------
model_name : str
Current model name.
new_name : str
New name for model.
Returns
-------
None
"""
mdb.models.changeKey(fromName=model_name, toName=new_name)
def rename_set(set_name, new_set_name):
"""Rename a set of points in all models from current database.
Parameters
----------
set_name : str
Current set name.
new_set_name : str
New name for set.
Returns
-------
None
"""
# Iterate trough models and change set name.
for model_key, model in mdb.models.items():
model.rootAssembly.sets.changeKey(fromName=set_name,
toName=new_set_name)
return
def retrieve_odb_name(number, show_all=False):
"""Get Odb name from session Odbs list, depending on its position.
This function orders session Odbs list alphabetically, and retrieves
the name of an Odb object, depending on its position in that list.
Useful when a Odb name is not known, but its position is.
Parameters
----------
number : int
Position of Odb object in session Odbs list.
show_all : bool, optional
If True, print name of all opened Odbs.
Returns
-------
str
Name of opened Odb object from current session.
"""
# Get list of opened Odbs keys, sort it and select one by position.
keys = session.odbs.keys()
keys = sorted(keys)
selected_key = keys[number]
# Print list of opened Odbs.
if show_all:
print('Currently opened Odbs', keys)
return selected_key
def retrieve_odb_set_name(odb, number, show_all=False):
"""Get set name from a Odb object, depending on its position.
This function orders Odb sets list alphabetically, and retrieves
the name of a set object, depending on its position in that list.
Useful when set name is not known, but its position is.
Parameters
----------
odb : Odb object or string.like Path
Odb object identifier.
number : int
Position of set object in Odb sets list.
show_all : bool, optional
If True, print name of all Odb sets name.
Returns
-------
str
Name of set name in a Odb object from current session.
"""
# Get list of Odb sets keys, sort it and select one by position.
odb = normalize_odb_object(odb)
keys = odb.rootAssembly.nodeSets.keys()
keys = sorted(keys)
selected_key = keys[number]
# Print list of available node sets.
if show_all:
print('Available node sets', keys)
return selected_key
def upgrade_odbs_folder(odbs_folder, recursive=False, print_every=1):
"""Upgrade version of all Odb objects in a folder.
Parameters
----------
odbs_folder : Path
Folder containing Odb objects.
recursive : bool, optional
If True, list Odb files recursively, including subfolders.
print_every : int, optional
If given, reduces printing reports frequency.
Returns
-------
None
"""
# List Odb paths, filter only old versioned and report.
odb_list = ft.list_files_with_extension(odbs_folder, '.odb', 1, recursive)
upgradable_odb_list = [i for i in
odb_list if odbAccess.isUpgradeRequiredForOdb(i)]
print(len(odb_list), 'Odb objects found', len(upgradable_odb_list),
'require upgrade')
# Set temporary names and iterate over old versioned Odbs.
temp_name = os.path.join(odbs_folder, 'temp_odb_name.odb')
for job_number, job_key in enumerate(upgradable_odb_list):
# Optionally, report less times.
if divmod(job_number, print_every)[1] == 0:
print('Processing', job_key,
job_number + 1, 'of', len(upgradable_odb_list))
# Upgrade and rename new and old Odb files.
new_name = job_key
old_name = job_key.replace('.odb', '-old.odb')
session.upgradeOdb(job_key, temp_name)
os.rename(job_key, old_name)
os.rename(temp_name, new_name)
print('DONE')
return