This repository has been archived by the owner on Oct 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
455 lines (357 loc) · 10.8 KB
/
Main.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
452
453
454
455
from py_mentat import *
from TowMentat import *
from Material import *
import pickle
import os
import winsound
def main():
# For geenrating multiple files, specify file names in this list
files = [
# "test_flat",
# "test_flat_090_6",
"test_flat_8",
# "test_flat_quasi_16",
'test_cylinder',
# 'test_cylinder_tst',
#'test_cylinder_45',
#'test_cone',
# 'test_weave_12',
#'test_clutch'
#'test_dome',
# 'test_nozzle',
# 'filament_winding'
]
for file_name in files:
p("*new_model yes")
save_file(file_name)
generate_model(file_name)
winsound.PlaySound("SystemQuestion", winsound.SND_ALIAS)
def generate_model(file):
"""Main function for generating model in Marc.
Steps through following process
1 - Generate tow shell elements
2 - Specify geometry, material properties and orientation
3 - Specify contact relations
Parameters
----------
file : String
file name to open based on array above
"""
plys = load_tows(file)
print("geom = ", file, "plies = ", len(plys))
for i in range(len(plys)):
p("*store_elements")
p("ply"+str(i+1))
p("#")
# General variables
thick = plys[0].tows[0][0].t
width = plys[0].tows[0][0].w
for ply in plys:
ply_name = "ply" + str(ply.id)
for t in ply.tows:
create_tow_shell(t)
p("*store_elements")
p(ply_name)
p(t[0].name())
p("*visible_elements")
p("all_existing")
assign_geometry(thick)
standard_material(T300, "all_existing")
assign_orientation()
cb_index = cbody_index(plys)
create_contact_bodies(plys)
contact_table(0.01)
create_table(0,0.5)
p("*save_model")
return
def create_tow_shell(tow_list):
"""Primary geometry creation function
Genrates a series of points for each sub-tow in tow_list.
Genreates curves -> surfaces -> elements
Parameters
----------
tow_list : List(TowMentat)
Batched list of tows
"""
total_divisions = sum([len(t.pts) for t in tow_list])
start = True
for tow in tow_list:
# Uncomment this if you want to skip tow trimming all together
# if tow.trimmed== True:
# continue
el_size = tow.w
curve_div = tow.w
# Generate curves from points on tow path
curves = []
curve_pts = []
for row in tow.pts:
if len(row) == 0:
print("empty_row")
continue
curve = generate_curve(row)
curves += curve
if curves == "":
print("no points")
return
# Get point ids associated for new curves
for curve in curves:
npts = py_get_int("ncurve_points(%s)" % curve)
s = [py_get_int("curve_point_id(%s, %s)" % (curve, pt)) for pt in range(1, npts+1)]
s = sorted(list(set(s))) #remove duplicates
curve_pts += [s]
# If the tow section (start/end) has been trimmed, then run the element edge algorithm
# Comment this if you don't want to trinagulate the edge meshes
if tow.trimmed == True:
element_algo(curve_pts, start=start)
if start == True: start=False
p("*clear_geometry")
p("*renumber_all")
# create surface using 5 guide curves
print("surface")
p("*set_surface_type skin")
p("*set_trim_new_surfs y")
p("*add_surfaces")
p("%s %s" % (" ".join(curves), '#'))
n_divisions = max([len(t) for t in tow.pts])
# Instead of trinagulating the boundary, this creates an automesh outer elements
# Did not produce as good results
if tow.trimmed == True:
p("@set($convert_surfaces_method,surface_faceted)")
p("*set_convert_remove_original off")
p("*set_curve_div_type_fix")
p("*set_curve_div_type_fix_avgl")
p("*set_curve_div_avgl %s" % str(curve_div))
p("*apply_curve_divisions")
p("all_existing")
p("*surface_faceted")
print("n surfaces ", py_get_int("nsurfaces()"))
p("%s #" % py_get_int("nsurfaces()"))
p("all_existing")
p("@set($automesh_surface_desc,facets)")
p("*pt_set_element_size %s" % str(el_size))
if n_divisions < 3:
p("@set($automesh_surface_family,tria)")
p("*pt_trimesh_surf")
else:
p("@set($automesh_surface_family,mixed)")
p("*pt_quadmesh_surf")
p("all_visible")
p("*clear_geometry")
else:
if total_divisions > 1000:
n_divisions *= 4
p("@set($convert_entities,surfaces)")
p("@set($convert_surfaces_method,convert_surface")
p("*set_convert_uvdiv u %s" % str((n_divisions-1)))
p("*set_convert_uvdiv v %s" % str(((len(tow.pts)-1))))
p("*convert_surfaces")
p("all_visible")
p("*clear_geometry")
# p("*invisible_surface all_existing")
# Sweep nodes to remove duplicates at batch boundary
p("*set_sweep_tolerance 0.2")
p("*sweep_nodes")
p("*all_visible")
# Store elements in set
p("*store_elements")
p(tow.name())
p("*all_visible")
# Clear geometry to simplify indexing for future tows
p("*clear_geometry")
p("*invisible_elements")
p("all_existing")
p("select_clear")
def generate_points(point):
p(point.send_coord())
def generate_curve(pts):
"""Given a set of points, generate a polyline curve
Parameters
----------
pts : list(PointMentat)
Column of points in a tow
Returns
-------
int
curve id
"""
ni = int(py_get_float("npoints()"))
p("*add_points")
if len(pts) < 200:
pts_arr = [i.send_coord() for i in pts]
pts_arr = " ".join(pts_arr)
p(pts_arr)
else:
k = 0
while(k < len(pts)):
pts_arr = [i.send_coord() for i in pts[k:k+100]]
pts_arr = " ".join(pts_arr)
p(pts_arr)
k += 100
pts_arr = [i.send_coord() for i in pts[k:]]
pts_arr = " ".join(pts_arr)
p(pts_arr)
nf = int(py_get_float("npoints()"))
pt_to_add = [str(i) for i in list(range(ni+1,nf+1))]
pt_to_add = " ".join(pt_to_add)
# Form curve
p("*set_curve_type polyline")
p("*add_curves")
p(pt_to_add)
p("#")
id = int(py_get_float("ncurves()"))
return str(id)
def contact_table(tolerance):
"""Generate and populate contact table
Parameters
----------
tolerance : float
contact distance tolerance
"""
ctable_name = "ct_edge_face"
p("*new_contact_table")
p("*contact_table_name %s" % ctable_name)
p("*prog_option ctable:criterion:contact_distance")
p("*prog_param ctable:contact_distance %f" % tolerance)
p("*prog_option ctable:add_replace_mode:both")
p("*prog_option ctable:contact_type:glue")
p("@set($cta_crit_dist_action,all_pairs)")
p("*ctable_add_replace_entries_all")
p("*interact_option retain_gaps:on")
def assign_geometry(t):
"""Specify geometry for elements
Parameters
----------
t : float
tow thickness
"""
# Specify geometry for elements
p("*new_geometry")
p("*geometry_type mech_three_shell")
p("*geometry_param thick %f" %t)
p("*add_geometry_elements")
p("all_existing")
def standard_material(m, elements):
p("*new_mater standard")
p("*mater_option general:state:solid")
p("*mater_option general:skip_structural:off")
p('*mater_name ' + m.name)
p("*mater_option structural:type:elast_plast_ortho")
p("*mater_param structural:youngs_modulus1 %f" % m.E1)
p("*mater_param structural:youngs_modulus2 %f" % m.E2)
p("*mater_param structural:youngs_modulus3 %f" % m.E3)
p("*mater_param structural:poissons_ratio12 %f" % m.Nu12)
p("*mater_param structural:poissons_ratio23 %f" % m.Nu23)
p("*mater_param structural:poissons_ratio31 %f" % m.Nu31)
p("*mater_param structural:shear_modulus12 %f" % m.G12)
p("*mater_param structural:shear_modulus23 %f" % m.G23)
p("*mater_param structural:shear_modulus31 %f" % m.G31)
p("*add_mater_elements")
p(elements)
return
def create_contact_bodies(plys):
p("*remove_empty_sets")
nsets = py_get_int("nsets()")
for i in range(1, nsets+1):
# iterate through tow sets
si = py_get_int("set_id(%d)" % i)
sn = py_get_string("set_name(%d)" % si)
if "ply" in sn:
continue
cn = sn.replace("tow","cb")
# Create contact body for tow
p("*new_cbody mesh *contact_option state:solid *contact_option skip_structural:off")
p("*contact_body_name %s" % cn)
p("*add_contact_body_elements")
p(sn)
return py_get_int("ncbodys()")
def cbody_index(plys):
cb_index = {}
for p in plys:
cb_index[p.id] = []
for s in p.list_tows():
cb_index[p.id].append(s.replace("tow","cb"))
return cb_index
def assign_orientation():
p("*new_orient *orent_type edge23")
p("*add_orient_elements")
p("all_existing")
def load_tows(file):
cwd = os.getcwd()
f = "batched\\"+file+".dat"
file_name = "\\".join([cwd,'dat_files',f])
# file_name = "/".join([cwd,'tows.dat']) #for linux FS
with open(file_name,'rb') as f:
ply = pickle.load(f)
return ply
def save_file(file):
print('*set_save_formatted off *save_as_model "%s.mud" yes' % file)
p('*set_save_formatted off *save_as_model "%s.mud" yes' % file)
# # Assuming an array of pts id
def element_algo(a, start=False):
"""algorithm to convert trimmed sections of a tow
to quad mesh, if 4 points are available, otherwise triangulates
the edge elements.
Parameters
----------
a : [5,n] int array
Array of point ids for points in trimmed section
start : bool, optional
specifies whether the points are at the start or end, in order
to determine orientation, by default False
"""
p("*renumber_nodes")
# Create nodes first
n_init = py_get_int("nnodes()")
n_pts = py_get_int("npoints()")
nodes = [str(i+1) for i in range(n_pts)]
p("@set($convert_entities,points")
p("@set($convert_points_method,convert_points)")
p("*convert_points")
p("%s #" % (" ".join(nodes)))
quads = []
tris = []
if start == True:
for i in a:
i.reverse()
print(a)
for i in range(len(a)-1):
for j in range(len(a[i])-1):
# 1: point i,j+1 and i+1,j+1 --> quad mesh
if len(a[i+1]) > j+1:
q = [a[i][j], a[i][j+1], a[i+1][j+1], a[i+1][j]]
if start == True: q = q[2:4] + q[0:2]
quads += [q]
#2: point at i,j+1, and i+1,j
else:
tris += [[a[i][j], a[i][-1], a[i+1][-1]]]
if len(a[i+1]) > len(a[i]):
tris += [[a[i][j+1], a[i+1][j+2], a[i+1][j+1]]]
p("*set_element_class tria3")
for t in tris:
p("*add_elements")
p(" ".join([str(i+n_init) for i in t]))
print(" ".join([str(i+n_init) for i in t]))
for q in quads:
pts = [str(i) for i in q]
print(pts)
p("*set_surface_type quad")
p("*add_surfaces")
p("%s #" % (" ".join(pts)))
p("@set($convert_entities,surfaces")
p("@set($convert_points_method,convert_surfaces)")
p("*set_convert_uvdiv u 1")
p("*set_convert_uvdiv v 1")
p("*convert_surfaces")
p("all_existing")
p("*renumber_nodes")
def create_table(initial, final):
p("*new_md_table 1 1")
p("*set_md_table_type 1")
p("time")
p("*table_add")
p("0 %s" % initial)
p("1 %s" % final)
p("*table_fit")
return
def p(s):
py_send(s)