-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculation.py
1976 lines (1567 loc) · 60.8 KB
/
calculation.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import bpy
import bmesh
import sys
import os
path_addons = os.path.dirname(__file__) # path to the folder of addons
path_phaenotyp = path_addons + "/phaenotyp"
sys.path.append(path_addons)
from PyNite import FEModel3D
from numpy import array, empty, append, poly1d, polyfit, linalg, zeros, intersect1d, arctan, sin, cos
from phaenotyp import basics, material, geometry
from math import sqrt, tanh, pi, degrees, radians
from subprocess import Popen, PIPE
import pickle
import gc
gc.disable()
def check_scipy():
"""
Checking if scipy is available and is setting the value to data.
"""
scene = bpy.context.scene
data = scene["<Phaenotyp>"]
try:
import scipy
data["scipy_available"] = True
except:
data["scipy_available"] = False
def prepare_fea_pn(frame):
'''
Is preparing the calculaton of the current frame for for PyNite.
:return model: FEModel3D function of PyNite
'''
scene = bpy.context.scene
phaenotyp = scene.phaenotyp
calculation_type = phaenotyp.calculation_type
data = scene["<Phaenotyp>"]
supports = data["supports"]
members = data["members"]
quads = data["quads"]
loads_v = data["loads_v"]
loads_e = data["loads_e"]
loads_f = data["loads_f"]
bpy.context.scene.frame_current = frame
bpy.context.view_layer.update()
geometry.update_geometry_pre()
model = FEModel3D()
basics.timer.start()
psf_members = phaenotyp.psf_members
psf_quads = phaenotyp.psf_quads
psf_loads = phaenotyp.psf_loads
for mat in material.library:
name = mat[0]
E = mat[2]
G = mat[3]
nu = None # replace later
rho = None # replace later
model.add_material(name, E, G, nu, rho)
# apply chromosome if available
individuals = data.get("individuals")
if individuals:
shape_keys = data["structure"].data.shape_keys.key_blocks
chromosome = individuals[str(frame)]["chromosome"]
geometry.set_shape_keys(shape_keys, chromosome)
# get absolute position of vertex (when using shape-keys, animation et cetera)
dg = bpy.context.evaluated_depsgraph_get()
obj = data["structure"].evaluated_get(dg)
mesh = obj.to_mesh(preserve_all_data_layers=True, depsgraph=dg)
vertices = mesh.vertices
edges = mesh.edges
faces = mesh.polygons
# to be collected:
data["frames"][str(frame)] = {}
frame_volume = 0
frame_area = 0
frame_length = 0
frame_weight = 0
frame_rise = 0
frame_span = 0
frame_cantilever = 0
# like suggested here by Gorgious and CodeManX:
# https://blender.stackexchange.com/questions/6155/how-to-convert-coordinates-from-vertex-to-world-space
mat = obj.matrix_world
# add nodes from vertices
for vertex in vertices:
vertex_id = vertex.index
name = str(vertex_id)
# like suggested here by Gorgious and CodeManX:
# https://blender.stackexchange.com/questions/6155/how-to-convert-coordinates-from-vertex-to-world-space
v = mat @ vertex.co
# apply to all vertices to work for edges and faces also
vertex.co = v
x = v[0] * 100 # convert to cm for calculation
y = v[1] * 100 # convert to cm for calculation
z = v[2] * 100 # convert to cm for calculation
# only create Node if needed for the model
create = False
for member_id, member in members.items():
if vertex_id in [member["vertex_0_id"], member["vertex_1_id"]]:
create = True
break
for quad_id, quad in quads.items():
if vertex_id in quad["vertices_ids_structure"]:
create = True
break
if create:
model.add_node(name, x,y,z)
# define support
for id, support in supports.items():
model.def_support(id, support[0], support[1], support[2], support[3], support[4], support[5])
# create members
for id, member in members.items():
vertex_0_id = member["vertex_0_id"]
vertex_1_id = member["vertex_1_id"]
v_0 = vertices[vertex_0_id].co
v_1 = vertices[vertex_1_id].co
# save initial_positions to mix with deflection
initial_positions = []
for i in range(11):
position = (v_0*(i) + v_1*(10-i))*0.1
x = position[0]
y = position[1]
z = position[2]
initial_positions.append([x,y,z])
member["initial_positions"][str(frame)] = initial_positions
node_0 = str(vertex_0_id)
node_1 = str(vertex_1_id)
material_name = member["material_name"]
if member["type"] == "full":
tension_only = False
comp_only = False
if member["type"] == "tension_only":
tension_only = True
comp_only = False
if member["type"] == "comp_only":
tension_only = False
comp_only = True
model.add_member(
id, node_0, node_1, material_name,
member["Iy"][str(frame)], member["Iz"][str(frame)],
member["J"][str(frame)], member["A"][str(frame)],
tension_only=tension_only, comp_only=comp_only,
)
# release Moments
if phaenotyp.type_of_joints == "release_moments":
model.def_releases(id,
False, False, False, False, True, True,
False, False, False, False, True, True)
# add self weight
weight_A = member["weight_A"][str(frame)]
kN = weight_A * -0.0000981
# add self weight as distributed load
model.add_member_dist_load(id, "FZ", kN*psf_members, kN*psf_members)
# calculate lenght of parts (maybe usefull later ...)
length = (v_0 - v_1).length
frame_length += length
# calculate and add weight to overall weight of structure
weight = length * weight_A
frame_weight += weight
# store in member
member["weight"][str(frame)] = weight
member["length"][str(frame)] = length
# create quads
for id, quad in quads.items():
E = quad["E"]
G = quad["G"]
nu = quad["nu"]
rho = quad["rho"]
# unique name of the material trough parameters
material_name = (
"material_" +
"E" + "_" +
"G" + "_" +
"nu" + "_" +
"rho")
if material_name not in model.Materials:
model.add_material(material_name, E, G, nu, rho)
vertex_ids = quad["vertices_ids_structure"]
# get thickness of frame or first
t = quad["thickness"].get(str(frame))
v_0 = str(vertex_ids[0])
v_1 = str(vertex_ids[1])
v_2 = str(vertex_ids[2])
v_3 = str(vertex_ids[3])
model.add_quad(id, v_0, v_1, v_2, v_3, t, material_name, kx_mod=1.0, ky_mod=1.0)
# save position before to morph with deflection afterwards
initial_positions = [
vertices[vertex_ids[0]].co,
vertices[vertex_ids[1]].co,
vertices[vertex_ids[2]].co,
vertices[vertex_ids[3]].co,
]
quad["initial_positions"][str(frame)] = initial_positions
# self weight
face = data["structure"].data.polygons[int(id)]
area = face.area
weight_A = t * rho
weight = weight_A * area * 10000 # in cm
for vertex_id in quads[id]["vertices_ids_structure"]:
vertex_id = str(vertex_id)
# area * thickness * density * 0.25 (to distribute to all four faces) - for gravity
z = weight * (-0.25)
model.add_node_load(vertex_id, 'FZ', z * 0.00000981 * psf_quads) # to cm and force
quad["area"][str(frame)] = area # in m²
quad["weight_A"][str(frame)] = t * weight_A
quad["weight"][str(frame)] = weight_A * area # in kg
frame_weight += weight_A * area # in kg
# add loads
for id, load in loads_v.items():
model.add_node_load(id, 'FX', load[0] * psf_loads)
model.add_node_load(id, 'FY', load[1] * psf_loads)
model.add_node_load(id, 'FZ', load[2] * psf_loads)
model.add_node_load(id, 'MX', load[3] * psf_loads)
model.add_node_load(id, 'MY', load[4] * psf_loads)
model.add_node_load(id, 'MZ', load[5] * psf_loads)
for id, load in loads_e.items():
model.add_member_dist_load(id, 'FX', load[0]*0.01 * psf_loads, load[0]*0.01 * psf_loads) # m to cm
model.add_member_dist_load(id, 'FY', load[1]*0.01 * psf_loads, load[1]*0.01 * psf_loads) # m to cm
model.add_member_dist_load(id, 'FZ', load[2]*0.01 * psf_loads, load[2]*0.01 * psf_loads) # m to cm
model.add_member_dist_load(id, 'Fx', load[3]*0.01 * psf_loads, load[3]*0.01 * psf_loads) # m to cm
model.add_member_dist_load(id, 'Fy', load[4]*0.01 * psf_loads, load[4]*0.01 * psf_loads) # m to cm
model.add_member_dist_load(id, 'Fz', load[5]*0.01 * psf_loads, load[5]*0.01 * psf_loads) # m to cm
for id, load in loads_f.items():
# apply force to quad if a quad is available
quad = quads.get(str(id))
if quad:
# int(id), otherwise crashing Speicherzugriffsfehler
face = data["structure"].data.polygons[int(id)]
normal = face.normal
edge_keys = face.edge_keys
area = face.area # in m²
load_normal = load[0]
load_projected = load[1]
load_area_z = load[2]
area_projected = geometry.area_projected(face, vertices)
# a quad is available to apply forces to
for vertex_id in quads[id]["vertices_ids_structure"]:
vertex_id = str(vertex_id)
x,y,z = 0,0,0
# load normal
area_load = load_normal * area
x += area_load * normal[0]
y += area_load * normal[1]
z += area_load * normal[2]
# load projected
area_load = load_projected * area_projected
z += area_load * 0.25 # divided by four points of each quad
# load z
area_load = load_area_z * area
z += area_load * 0.25 # divided by four points of each quad
model.add_node_load(vertex_id, 'FX', x * psf_loads) # to cm
model.add_node_load(vertex_id, 'FY', y * psf_loads) # to cm
model.add_node_load(vertex_id, 'FZ', z * psf_loads) # to cm
# apply force to members
else:
# int(id), otherwise crashing Speicherzugriffsfehler
face = data["structure"].data.polygons[int(id)]
normal = face.normal
edge_keys = face.edge_keys
area = face.area # in m²
load_normal = load[0]
load_projected = load[1]
load_area_z = load[2]
area_projected = geometry.area_projected(face, vertices)
distances, perimeter = geometry.perimeter(edge_keys, vertices)
# define loads for each edge
edge_load_normal = []
edge_load_projected = []
edge_load_area_z = []
ratio = 1 / len(edge_keys)
for edge_id, dist in enumerate(distances):
# load_normal
area_load = load_normal * area
edge_load = area_load * ratio / dist * 0.01 # m to cm
edge_load_normal.append(edge_load)
# load projected
area_load = load_projected * area_projected
edge_load = area_load * ratio / dist * 0.01 # m to cm
edge_load_projected.append(edge_load)
# load in z
area_load = load_area_z * area
edge_load = area_load * ratio / dist * 0.01 # m to cm
edge_load_area_z.append(edge_load)
# i is the id within the class (0, 1, 3 and maybe more)
# edge_id is the id of the edge in the mesh -> the member
for i, edge_key in enumerate(edge_keys):
# get name <---------------------------------------- maybe better method?
for edge in edges:
if edge.vertices[0] in edge_key:
if edge.vertices[1] in edge_key:
name = str(edge.index)
# edge_load_normal <--------------------------------- to be tested / checked
x = edge_load_normal[i] * normal[0]
y = edge_load_normal[i] * normal[1]
z = edge_load_normal[i] * normal[2]
model.add_member_dist_load(name, 'FX', x, x)
model.add_member_dist_load(name, 'FY', y, y)
model.add_member_dist_load(name, 'FZ', z, z)
# edge_load_projected
z = edge_load_projected[i]
model.add_member_dist_load(name, 'FZ', z, z)
# edge_load_area_z
z = edge_load_area_z[i]
model.add_member_dist_load(name, 'FZ', z, z)
# store frame based data
data["frames"][str(frame)]["volume"] = geometry.volume(mesh)
data["frames"][str(frame)]["area"] = geometry.area(faces)
data["frames"][str(frame)]["length"] = frame_length
data["frames"][str(frame)]["weight"] = frame_weight
data["frames"][str(frame)]["rise"] = geometry.rise(vertices)
data["frames"][str(frame)]["span"] = geometry.span(vertices, supports)
data["frames"][str(frame)]["cantilever"] = geometry.cantilever(vertices, supports)
# get duration
text = calculation_type + " preparation for frame " + str(frame) + " done"
text += basics.timer.stop()
basics.print_data(text)
# created a model object of PyNite and add to dict
basics.models[frame] = model
def prepare_fea_fd(frame):
'''
Is preparing the calculaton of the current frame for for force disbribution.
:return model: List of [points_array, supports_ids, edges_array, forces_array].
'''
scene = bpy.context.scene
phaenotyp = scene.phaenotyp
calculation_type = phaenotyp.calculation_type
data = scene["<Phaenotyp>"]
bpy.context.scene.frame_current = frame
bpy.context.view_layer.update()
geometry.update_geometry_pre()
basics.timer.start()
psf_members = phaenotyp.psf_members
psf_loads = phaenotyp.psf_loads
# apply chromosome if available
individuals = data.get("individuals")
if individuals:
shape_keys = data["structure"].data.shape_keys.key_blocks
chromosome = individuals[str(frame)]["chromosome"]
geometry.set_shape_keys(shape_keys, chromosome)
# get absolute position of vertex (when using shape-keys, animation et cetera)
dg = bpy.context.evaluated_depsgraph_get()
obj = data["structure"].evaluated_get(dg)
mesh = obj.to_mesh(preserve_all_data_layers=True, depsgraph=dg)
vertices = mesh.vertices
edges = mesh.edges
faces = mesh.polygons
# to be collected:
data["frames"][str(frame)] = {}
frame_volume = 0
frame_area = 0
frame_length = 0
frame_weight = 0
frame_rise = 0
frame_span = 0
frame_cantilever = 0
# to sum up loads
forces = []
for i in range(len(vertices)):
forces.append(array([0.0, 0.0, 0.0]))
# like suggested here by Gorgious and CodeManX:
# https://blender.stackexchange.com/questions/6155/how-to-convert-coordinates-from-vertex-to-world-space
mat = obj.matrix_world
# add nodes from vertices
points = []
for vertex in vertices:
# like suggested here by Gorgious and CodeManX:
# https://blender.stackexchange.com/questions/6155/how-to-convert-coordinates-from-vertex-to-world-space
v = mat @ vertex.co
# apply to all vertices to work for edges and faces also
vertex.co = v
x = v[0] * 100 # convert to cm for calculation
y = v[1] * 100 # convert to cm for calculation
z = v[2] * 100 # convert to cm for calculation
pos = [x,y,z]
points.append(pos)
points_array = array(points)
# define support
fixed = []
supports = data["supports"]
for id, support in supports.items():
id = int(id)
fixed.append(id)
supports_ids = fixed
# create members
members = data["members"]
keys = []
lenghtes = []
for id, member in members.items():
vertex_0_id = member["vertex_0_id"]
vertex_1_id = member["vertex_1_id"]
key = [vertex_0_id, vertex_1_id]
keys.append(key)
v_0 = vertices[vertex_0_id].co
v_1 = vertices[vertex_1_id].co
# save initial_positions to mix with deflection
initial_positions = []
for position in [v_0, v_1]:
x = position[0]
y = position[1]
z = position[2]
initial_positions.append([x,y,z])
member["initial_positions"][str(frame)] = initial_positions
# add self weight
weight_A = member["weight_A"][str(frame)]
kN = weight_A * -0.0000981
# calculate lenght of parts (maybe usefull later ...)
length = (v_0 - v_1).length
frame_length += length
# calculate and add weight to overall weight of structure
weight = length * weight_A
frame_weight += weight
# for calculation
lenghtes.append(length)
# add self weight
self_weight = kN * length * 100 * psf_members
forces[vertex_0_id] += array([0.0, 0.0, self_weight*0.5])
forces[vertex_1_id] += array([0.0, 0.0, self_weight*0.5])
# store in member
member["weight"][str(frame)] = weight
member["length"][str(frame)] = length
edges_array = array(keys)
# add loads
loads_v = data["loads_v"]
for id, load in loads_v.items():
forces[int(id)] += array([load[0]*100*psf_loads, load[1]*100*psf_loads, load[2]*100*psf_loads])
loads_e = data["loads_e"]
for id, load in loads_e.items():
member = members[id]
vertex_0_id = member["vertex_0_id"]
vertex_1_id = member["vertex_1_id"]
length = lenghtes[int(id)]
f = length * 0.5 * 100 * psf_loads # half of the member, m to cm + psf
forces[int(vertex_0_id)] += array([load[0]*f, load[1]*f, load[2]*f])
forces[int(vertex_1_id)] += array([load[0]*f, load[1]*f, load[2]*f])
loads_f = data["loads_f"]
for id, load in loads_f.items():
# int(id), otherwise crashing Speicherzugriffsfehler
face = data["structure"].data.polygons[int(id)]
normal = face.normal
edge_keys = face.edge_keys
area = face.area
load_normal = load[0]
load_projected = load[1]
load_area_z = load[2]
area_projected = geometry.area_projected(face, vertices)
distances, perimeter = geometry.perimeter(edge_keys, vertices)
# define loads for each edge
edge_load_normal = []
edge_load_projected = []
edge_load_area_z = []
ratio = 1 / len(edge_keys)
for edge_id, dist in enumerate(distances):
# load normal
area_load = load_normal * area
edge_load = area_load * ratio / dist * 0.01 *psf_loads # m to cm + psf
edge_load_normal.append(edge_load)
# load projected
area_load = load_projected * area_projected
edge_load = area_load * ratio / dist * 0.01 *psf_loads # m to cm + psf
edge_load_projected.append(edge_load)
# load in z
area_load = load_area_z * area
edge_load = area_load * ratio / dist * 0.01 *psf_loads # m to cm + psf
edge_load_area_z.append(edge_load)
# i is the id within the class (0, 1, 3 and maybe more)
# edge_id is the id of the edge in the mesh -> the member
for i, edge_key in enumerate(edge_keys):
for edge in edges:
if edge.vertices[0] in edge_key:
if edge.vertices[1] in edge_key:
id = edge.index
x = edge_load_normal[i] * normal[0]
y = edge_load_normal[i] * normal[1]
z = edge_load_normal[i] * normal[2]
member = members[str(id)]
vertex_0_id = member["vertex_0_id"]
vertex_1_id = member["vertex_1_id"]
length = lenghtes[int(id)]
f = length * 0.5 * 100 # half of the member, m to cm
forces[int(vertex_0_id)] += array([x*f, y*f, z*f])
forces[int(vertex_1_id)] += array([x*f, y*f, z*f])
# edge_load_projected
z = edge_load_projected[i]
forces[int(vertex_0_id)] += array([0.0, 0.0, z*f])
forces[int(vertex_1_id)] += array([0.0, 0.0, z*f])
# edge_load_area_z
z = edge_load_area_z[i]
forces[int(vertex_0_id)] += array([0.0, 0.0, z*f])
forces[int(vertex_1_id)] += array([0.0, 0.0, z*f])
# move all forces from the supports to the next load
# (is ignored if the both vertices are supports)
for id, member in members.items():
vertex_0_id = member["vertex_0_id"]
vertex_1_id = member["vertex_1_id"]
if vertex_0_id in supports_ids:
forces[int(vertex_1_id)] += forces[int(vertex_0_id)]
if vertex_1_id in supports_ids:
forces[int(vertex_0_id)] += forces[int(vertex_1_id)]
forces_array = array(forces)
# store frame based data
data["frames"][str(frame)]["volume"] = geometry.volume(mesh)
data["frames"][str(frame)]["area"] = geometry.area(faces)
data["frames"][str(frame)]["length"] = frame_length
data["frames"][str(frame)]["weight"] = frame_weight
data["frames"][str(frame)]["rise"] = geometry.rise(vertices)
data["frames"][str(frame)]["span"] = geometry.span(vertices, supports)
data["frames"][str(frame)]["cantilever"] = geometry.cantilever(vertices, supports)
# get duration
text = calculation_type + " preparation for frame " + str(frame) + " done"
text += basics.timer.stop()
basics.print_data(text)
# created a model object of PyNite and add to dict
model = [points_array, supports_ids, edges_array, forces_array]
basics.models[str(frame)] = model
def run_mp(models):
'''
Is calculating the given models, pickles them for mp.
:param models: Needs a list of models from any prepare_fea as dict with frame as key.
:return models: Returns the calculated models as dict with the frame as key.
'''
# get pathes
path_addons = os.path.dirname(__file__) # path to the folder of addons
path_script = path_addons + "/mp.py"
path_python = sys.executable # path to bundled python
path_blend = bpy.data.filepath # path to stored blender file
directory_blend = os.path.dirname(path_blend) # directory of blender file
name_blend = bpy.path.basename(path_blend) # name of file
# pickle models to file
path_export = directory_blend + "/Phaenotyp-export_mp.p"
export_models = open(path_export, 'wb')
pickle.dump(models, export_models)
export_models.close()
# scipy_available to pass forward
if bpy.context.scene["<Phaenotyp>"]["scipy_available"]:
scipy_available = "True" # as string
else:
scipy_available = "False" # as string
# calculation_type
scene = bpy.context.scene
phaenotyp = scene.phaenotyp
calculation_type = phaenotyp.calculation_type
task = [path_python, path_script, directory_blend, scipy_available, calculation_type]
# feedback from python like suggested from Markus Amalthea Magnuson and user3759376 here
# https://stackoverflow.com/questions/4417546/constantly-print-subprocess-output-while-process-is-running
p = Popen(task, stdout=PIPE, bufsize=1)
lines_iterator = iter(p.stdout.readline, b"")
while p.poll() is None:
for line in lines_iterator:
nline = line.rstrip()
#print(nline.decode("utf8"), end = "\r\n",flush =True) # yield line
# get models back from mp
path_import = directory_blend + "/Phaenotyp-return_mp.p"
file = open(path_import, 'rb')
imported_models = pickle.load(file)
file.close()
basics.feas = imported_models
def interweave_results_pn(frame):
'''
Function to integrate the results of PyNite.
:param feas: Feas as dict with frame as key.
:param members: Pass the members from <Phaenotyp>
'''
scene = bpy.context.scene
data = scene["<Phaenotyp>"]
phaenotyp = scene.phaenotyp
calculation_type = phaenotyp.calculation_type
members = data["members"]
quads = data["quads"]
frame = str(frame)
model = basics.feas[frame]
basics.timer.start()
for id in members:
member = members[id]
model_member = model.Members[id]
L = model_member.L() # Member length
T = model_member.T() # Member local transformation matrix
axial = []
moment_y = []
moment_z = []
shear_y = []
shear_z = []
torque = []
for i in range(11): # get the forces at 11 positions and
x = L/10*i
axial_pos = model_member.axial(x) * (-1) # Druckkraft minus
axial.append(axial_pos)
moment_y_pos = model_member.moment("My", x)
moment_y.append(moment_y_pos)
moment_z_pos = model_member.moment("Mz", x)
moment_z.append(moment_z_pos)
shear_y_pos = model_member.shear("Fy", x)
shear_y.append(shear_y_pos)
shear_z_pos = model_member.shear("Fz", x)
shear_z.append(shear_z_pos)
torque_pos = model_member.torque(x)
torque.append(torque_pos)
member["axial"][frame] = axial
member["moment_y"][frame] = moment_y
member["moment_z"][frame] = moment_z
member["shear_y"][frame] = shear_y
member["shear_z"][frame] = shear_z
member["torque"][frame] = torque
# shorten and accessing once
A = member["A"][frame]
J = member["J"][frame]
Do = member["Do"][frame]
# buckling
member["ir"][frame] = sqrt(J/A) # für runde Querschnitte in cm
# bucklng resolution
buckling_resolution = member["buckling_resolution"]
# modulus from the moments of area
#(Wy and Wz are the same within a pipe)
member["Wy"][frame] = member["Iy"][frame]/(Do/2)
# polar modulus of torsion
member["WJ"][frame] = J/(Do/2)
# calculation of the longitudinal stresses
long_stress = []
for i in range(11): # get the stresses at 11 positions and
moment_h = sqrt(moment_y[i]**2+moment_z[i]**2)
if axial[i] > 0:
s = axial[i]/A + moment_h/member["Wy"][frame]
else:
s = axial[i]/A - moment_h/member["Wy"][frame]
long_stress.append(s)
# get max stress of the beam
# (can be positive or negative)
member["long_stress"][frame] = long_stress
member["max_long_stress"][frame] = basics.return_max_diff_to_zero(long_stress) # -> is working as fitness
# calculation of the shear stresses from shear force
# (always positive)
tau_shear = []
shear_h = []
for i in range(11): # get the stresses at 11 positions and
# shear_h
s_h = sqrt(shear_y[i]**2+shear_z[i]**2)
shear_h.append(s_h)
tau = 1.333 * s_h/A # for pipes
tau_shear.append(tau)
member["shear_h"][frame] = shear_h
# get max shear stress of shear force of the beam
# shear stress is mostly small compared to longitudinal
# in common architectural usage and only importand with short beam lenght
member["tau_shear"][frame] = tau_shear
member["max_tau_shear"][frame] = max(tau_shear)
# Calculation of the torsion stresses
# (always positiv)
tau_torsion = []
for i in range(11): # get the stresses at 11 positions and
tau = abs(torque[i]/member["WJ"][frame])
tau_torsion.append(tau)
# get max torsion stress of the beam
member["tau_torsion"][frame] = tau_torsion
member["max_tau_torsion"][frame] = max(tau_torsion)
# torsion stress is mostly small compared to longitudinal
# in common architectural usage
# calculation of the shear stresses form shear force and torsion
# (always positiv)
sum_tau = []
for i in range(11): # get the stresses at 11 positions and
tau = tau_shear[i] + tau_torsion[i]
sum_tau.append(tau)
member["sum_tau"][frame] = sum_tau
member["max_sum_tau"][frame] = max(sum_tau)
# combine shear and torque
sigmav = []
for i in range(11): # get the stresses at 11 positions and
sv = sqrt(long_stress[i]**2 + 3*sum_tau[i]**2)
sigmav.append(sv)
member["sigmav"][frame] = sigmav
member["max_sigmav"][frame] = max(sigmav)
# check out: http://www.bs-wiki.de/mediawiki/index.php?title=Festigkeitsberechnung
member["sigma"][frame] = member["long_stress"][frame]
member["max_sigma"][frame] = member["max_long_stress"][frame]
# overstress
member["overstress"][frame] = False
# check overstress and add 1.05 savety factor
safety_factor = 1.05
if abs(member["max_tau_shear"][frame]) > safety_factor*member["acceptable_shear"]:
member["overstress"][frame] = True
if abs(member["max_tau_torsion"][frame]) > safety_factor*member["acceptable_torsion"]:
member["overstress"][frame] = True
if abs(member["max_sigmav"][frame]) > safety_factor*member["acceptable_sigmav"]:
member["overstress"][frame] = True
# buckling
if member["axial"][frame][0] < 0: # nur für Druckstäbe, axial kann nicht flippen?
member["lamda"][frame] = L*buckling_resolution*0.5/member["ir"][frame] # für eingespannte Stäbe ist die Knicklänge 0.5 der Stablänge L, Stablänge muss in cm sein !
if member["lamda"][frame] > 20: # für lamda < 20 (kurze Träger) gelten die default-Werte)
kn = member["knick_model"]
function_to_run = poly1d(polyfit(material.kn_lamda, kn, 6))
member["acceptable_sigma_buckling"][frame] = function_to_run(member["lamda"][frame])
if member["lamda"][frame] > 250: # Schlankheit zu schlank
member["acceptable_sigma_buckling"][frame] = function_to_run(250)
member["overstress"][frame] = True
if safety_factor*abs(member["acceptable_sigma_buckling"][frame]) > abs(member["max_sigma"][frame]): # Sigma
member["overstress"][frame] = True
else:
member["acceptable_sigma_buckling"][frame] = member["acceptable_sigma"]
# without buckling
else:
member["acceptable_sigma_buckling"][frame] = member["acceptable_sigma"]
member["lamda"][frame] = None # to avoid missing KeyError
if abs(member["max_sigma"][frame]) > safety_factor*member["acceptable_sigma"]:
member["overstress"][frame] = True
# lever_arm
lever_arm = []
moment_h = []
for i in range(11):
# moment_h
m_h = sqrt(moment_y[i]**2+moment_z[i]**2)
moment_h.append(m_h)
# to avoid division by zero
if member["axial"][frame][i] < 0.1:
lv = m_h / 0.1
else:
lv = m_h / member["axial"][frame][i]
lv = abs(lv) # absolute highest value within member
lever_arm.append(lv)
member["moment_h"][frame] = moment_h
member["lever_arm"][frame] = lever_arm
member["max_lever_arm"][frame] = max(lever_arm)
# Ausnutzungsgrad
member["utilization"][frame] = abs(member["max_long_stress"][frame] / member["acceptable_sigma_buckling"][frame])
# Einführung in die Technische Mechanik - Festigkeitslehre, H.Balke, Springer 2010
normalkraft_energie=[]
moment_energie=[]
strain_energy = []
for i in range(10): # get the energie at 10 positions for 10 section
# Berechnung der strain_energy für Normalkraft
ne = (axial[i]**2)*(L/10)/(2*member["E"]*A)
normalkraft_energie.append(ne)
# Berechnung der strain_energy für Moment
moment_hq = moment_y[i]**2+moment_z[i]**2
me = (moment_hq * L/10) / (member["E"] * member["Wy"][frame] * Do)
moment_energie.append(me)
# Summe von Normalkraft und Moment-Verzerrunsenergie
value = ne + me
strain_energy.append(value)
member["strain_energy"][frame] = strain_energy
member["normal_energy"][frame] = normalkraft_energie
member["moment_energy"][frame] = moment_energie
# deflection
deflection = []
# --> taken from pyNite VisDeformedMember: https://github.com/JWock82/PyNite
scale_factor = 10.0
cos_x = array([T[0,0:3]]) # Direction cosines of local x-axis
cos_y = array([T[1,0:3]]) # Direction cosines of local y-axis
cos_z = array([T[2,0:3]]) # Direction cosines of local z-axis
DY_plot = empty((0, 3))
DZ_plot = empty((0, 3))
for i in range(11):
# Calculate the local y-direction displacement
dy_tot = model_member.deflection('dy', L/10*i)
# Calculate the scaled displacement in global coordinates
DY_plot = append(DY_plot, dy_tot*cos_y*scale_factor, axis=0)
# Calculate the local z-direction displacement
dz_tot = model_member.deflection('dz', L/10*i)
# Calculate the scaled displacement in global coordinates
DZ_plot = append(DZ_plot, dz_tot*cos_z*scale_factor, axis=0)
# Calculate the local x-axis displacements at 20 points along the member's length
DX_plot = empty((0, 3))
Xi = model_member.i_node.X
Yi = model_member.i_node.Y
Zi = model_member.i_node.Z
for i in range(11):
# Displacements in local coordinates
dx_tot = [[Xi, Yi, Zi]] + (L/10*i + model_member.deflection('dx', L/10*i)*scale_factor)*cos_x
# Magnified displacements in global coordinates
DX_plot = append(DX_plot, dx_tot, axis=0)
# Sum the component displacements to obtain overall displacement
D_plot = DY_plot + DZ_plot + DX_plot
# <-- taken from pyNite VisDeformedMember: https://github.com/JWock82/PyNite
# add to results
for i in range(11):
x = D_plot[i, 0] * 0.01
y = D_plot[i, 1] * 0.01
z = D_plot[i, 2] * 0.01
deflection.append([x,y,z])
member["deflection"][frame] = deflection
nodes = model.Nodes
for id in quads:
quad = quads[id]
# read results from PyNite
result = model.Quads[id]
# only take highest value to zero
shear = result.shear()
moment = result.moment()
membrane = result.membrane()
# from PyNite