-
Notifications
You must be signed in to change notification settings - Fork 0
/
kratos_io_utilities.py
1000 lines (737 loc) · 36 KB
/
kratos_io_utilities.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
'''
___ _ _ ___ __ __ ___ _ _____ _ _____ ___ ___
/ __| /_\ | | / _ \| \/ | __|__| |/ / _ \ /_\_ _/ _ \/ __|
\__ \/ _ \| |_| (_) | |\/| | _|___| ' <| / / _ \| || (_) \__ \
|___/_/ \_\____\___/|_| |_|___| |_|\_\_|_\/_/ \_\_| \___/|___/
/ __|___ _ ___ _____ _ _| |_ ___ _ _
| (__/ _ \ ' \ V / -_) '_| _/ -_) '_|
\___\___/_||_\_/\___|_| \__\___|_|
Salome to Kratos Converter
Converts *.dat files that contain mesh information to *.mdpa file to be used as input for Kratos Multiphysics.
Author: Philipp Bucher
Chair of Structural Analysis
June 2017
Intended for non-commercial use in research
'''
# Python imports
import time
# Project imports
import global_utilities as global_utils
READABLE_MDPA = False
def CreateNode(node_id, coords, nodal_data={}):
'''Wrapper function for once the Node-Class will be used'''
if node_id <= 0:
raise Exception("The NodeID has to be larger than 0!")
if type(coords) != list:
raise Exception("The NodeCoords have to be provided as a list!")
if len(coords) != 3:
raise Exception("The NodeCoords have to consist of three doubles!")
return [coords, nodal_data]
class Node(object):
def __init__(self, Id, coordinates, nodal_data=None):
self.Id = Id
self.coordinates = coordinates
self.nodal_data = nodal_data
if self.nodal_data == None: # this is done bcs default args are shared!
self.nodal_data = {}
if not type(coordinates) == list:
raise TypeError('type of "coordinates" should be "list", received: ' + str(type(coordinates)))
if not len(coordinates) == 3:
raise Exception('Coordinates have to be three doubles!')
if not type(nodal_data) == dict:
raise TypeError('type of "nodal_data" should be "dict", received: ' + str(type(nodal_data)))
def GetId(self):
return self.Id
def GetCoordinates(self):
return self.coordinates
def HasNodalData(self):
return len(self.nodal_data) > 0
def GetNodalData(self):
return self.nodal_data
def SetNodalData(self, data_name, data_value):
self.entity_data[data_name] = data_value
def Serialize(self):
"""This function returns the serialized node
"""
serialized_node = [self.Id,
self.coordinates,
self.nodal_data]
return serialized_node
@staticmethod
def Deserialize(serialized_node):
"""This function takes a serialized node and creates a new
node out of it
"""
Id = serialized_node[0]
coordinates = serialized_node[1]
nodal_data = serialized_node[2]
node = Node(Id, coordinates, nodal_data)
return node
class KratosEntity(object):
def __init__(self, origin_entity, name, property_ID):
self.is_node = False
if isinstance(origin_entity, int):
self.is_node = True
self.origin_entity = origin_entity # int for Node, GeometricEntitySalome for Element/Condition
self.name = name
self.property_ID = property_ID
self.new_ID = -1
self.is_added_already = False
def __str__(self):
stringbuf = "Name: " + self.name
stringbuf += "; PropID: " + str(self.property_ID)
stringbuf += "; NewId: " + str(self.new_ID)
'''
if self.is_node:
stringbuf += "; OriginEntity: " + str(self.origin_entity)
else:
stringbuf += "; OriginEntity: " + str(self.origin_entity)
'''
stringbuf += "; OriginEntity: " + str(self.origin_entity)
return stringbuf
__repr__ = __str__
def __eq__(self, Other):
if self.origin_entity != Other.origin_entity:
return False
if self.name != Other.name:
return False
if self.property_ID != Other.property_ID:
return False
return True
def GetWriteLine(self, format_str, space):
# "0" is the Property Placeholder
line = format_str.format(str(self.new_ID), str(self.property_ID))
if self.is_node:
line += space + str(self.origin_entity)
else:
for node in self.origin_entity.GetNodeList():
line += space + str(node)
if global_utils.DEBUG:
line += " // " + str(self.origin_entity.GetID()) # add the origin ID
return line
def SetIsAdded(self):
self.is_added_already = True
def IsAddedAlready(self):
return self.is_added_already
def ResetWritingInfo(self):
self.new_ID = -1
self.is_added_already = False
def SetID(self, new_id):
self.new_ID = new_id
def GetID(self):
if self.new_ID == -1:
raise RuntimeError("No new ID has been assiged")
return self.new_ID
def GetNodeList(self):
return self.origin_entity.GetNodeList()
def HasEntityData(self):
return self.origin_entity.HasEntityData()
def GetEntityData(self):
return self.origin_entity.GetEntityData()
def SetEntityData(self, data_name, data_value):
self.origin_entity.SetEntityData(data_name, data_value)
class Element(KratosEntity):
def __init__(self, origin_entity, name, property_ID):
super(Element, self).__init__(origin_entity, name, property_ID)
def __str__(self):
return "Element | " + super(Element, self).__str__()
__repr__ = __str__
class Condition(KratosEntity):
def __init__(self, origin_entity, name, property_ID):
super(Condition, self).__init__(origin_entity, name, property_ID)
def __str__(self):
return "Condition | " + super(Condition, self).__str__()
__repr__ = __str__
class MainModelPart:
def __init__(self):
self.__Initialize()
def __Initialize(self):
self.sub_model_parts = {}
self.__InitializeMesh()
self.mesh_read = False
self.precision = 12 # Same as in Kratos ("/kratos/includes/gid_io.h")
self.num_spaces = 3 # number of spaces in mdpa btw numbers
def __InitializeMesh(self):
self.nodes = {} # ID : [Coord_X, Coord_Y, Coord_Z]
self.elements = {} # Name : [List]
self.conditions = {} # Name : [List]
self.node_counter = 1
self.element_counter = 1
self.condition_counter = 1
# Variables for optimizing the size of the mdpa file
self.max_node_coord_x = 0
self.max_node_coord_y = 0
self.max_node_coord_z = 0
def GetMeshRead(self):
return self.mesh_read
def GetSubModelPart(self, smp_name):
if smp_name in self.sub_model_parts:
return self.sub_model_parts[smp_name]
def Reset(self):
self.__Initialize()
def SubModelPartNameExists(self, smp_name):
smp_exists = False
if smp_name in self.sub_model_parts.keys():
smp_exists = True
return smp_exists
def FileNameExists(self, file_name):
for smp in self.sub_model_parts.values():
smp_file_name = smp.GetFileName()
if smp_file_name != "":
if file_name == smp_file_name:
return True
return False
def FilePathExists(self, file_path):
for smp in self.sub_model_parts.values():
smp_file_path = smp.GetFilePath()
if smp_file_path != "":
if file_path == smp_file_path:
return True
return False
def AssembleMeshInfoDict(self):
mp_dict = {}
for smp_name in self.sub_model_parts.keys():
mp_dict[smp_name] = self.sub_model_parts[smp_name].GetMeshInfoDict()
return mp_dict
def AddMesh(self, smp_info_dict, mesh_dict, nodes_read, geom_entities_read):
smp_name = smp_info_dict["smp_name"]
if smp_name in self.sub_model_parts.keys():
raise NameError("SubModelPart \"" + smp_name + "\" exists already!")
self.sub_model_parts[smp_name] = MeshSubmodelPart()
self.sub_model_parts[smp_name].FillWithEntities(smp_info_dict, mesh_dict, nodes_read, geom_entities_read)
self.mesh_read = True
def UpdateMesh(self, old_smp_name, smp_info_dict, mesh_dict):
new_smp_name = smp_info_dict["smp_name"]
if old_smp_name not in self.sub_model_parts.keys():
raise NameError("SubModelPart \"" + old_smp_name + "\" does not exist!")
self.sub_model_parts[new_smp_name] = self.sub_model_parts.pop(old_smp_name) # Update the key
self.sub_model_parts[new_smp_name].Update(smp_info_dict, mesh_dict)
def RemoveSubmodelPart(self, name_smp):
self.sub_model_parts.pop(name_smp, None)
def Serialize(self):
# This function serializes the ModelPart such that it can be saved in a json file
global_utils.LogDebug("Serializing ModelPart")
serialized_dict = {}
for smp_name in sorted(self.sub_model_parts.keys()):
smp = self.sub_model_parts[smp_name]
serialized_dict.update(smp.Serialize())
return serialized_dict
def Deserialize(self, serialized_dict):
# This function constructs a modelpart from a serialized dictionary
self.Reset()
for smp_name in sorted(serialized_dict.keys()):
if smp_name != "general":
self.sub_model_parts[smp_name] = MeshSubmodelPart()
self.sub_model_parts[smp_name].Deserialize(smp_name, serialized_dict[smp_name])
self.mesh_read = True
global_utils.LogDebug("Deserialized ModelPart")
def GetTreeItems(self):
return sorted(self.sub_model_parts.keys()) # return sorted bcs it is also sorted in the json format!
def WriteMesh(self, mdpa_file_path, info_text="", readable_mdpa=False): # TODO use this
self.__Assemble(readable_mdpa) # TODO only do this if sth has changed
start_time = time.time()
global_utils.LogInfo("Writing Mesh")
# Write Header
if not mdpa_file_path.endswith('.mdpa'):
mdpa_file_path += ".mdpa"
with open(mdpa_file_path,"w") as mdpa_file:
self.__WriteMeshInfo(mdpa_file, info_text)
mdpa_file.write("\nBegin ModelPartData\n// VARIABLE_NAME value\nEnd ModelPartData\n\n")
mdpa_file.write("Begin Properties 0\nEnd Properties\n\n")
# Write Nodes
self.__WriteNodes(mdpa_file, readable_mdpa)
# Write Elements
self.__WriteElements(mdpa_file, readable_mdpa)
# Write Conditions
self.__WriteConditions(mdpa_file, readable_mdpa)
# Write Nodal Data
self.__WriteNodalData(mdpa_file, readable_mdpa)
# Write Elemental Data
self.__WriteGeometricalEntityData(mdpa_file, readable_mdpa, self.elements, "Element")
# Write Conditional Data
self.__WriteGeometricalEntityData(mdpa_file, readable_mdpa, self.conditions, "Condition")
# Write SubModelParts
for smp_name in sorted(self.sub_model_parts.keys()):
smp = self.sub_model_parts[smp_name]
smp.WriteMesh(mdpa_file, readable_mdpa)
global_utils.LogTiming("Mesh writing time", start_time)
self.__ClearAfterWriting()
return True
def __WriteMeshInfo(self, open_file, info_text=""):
'''
Writing some information about the ModelPart to the mdpa file
'''
import time
localtime = time.asctime( time.localtime(time.time()) )
# open_file.write("// File created on " + localtime + "\n")
if info_text != "":
open_file.write("// " + info_text + "\n")
open_file.write("// Mesh Information:\n")
open_file.write("// Number of Nodes: " + str(self.NumberOfNodes()) + "\n")
open_file.write("// Number of Elements: " + str(self.NumberOfElements()) + "\n")
open_file.write("// Number of Conditions: " + str(self.NumberOfConditions()) + "\n")
num_smps_to_write = sum([smp.WriteSubModelPart() for smp in self.sub_model_parts.values()])
open_file.write("// Number of SubModelParts: " + str(num_smps_to_write) + "\n")
for smp_name in sorted(self.sub_model_parts.keys()):
smp = self.sub_model_parts[smp_name]
smp.WriteMeshInfo(open_file)
open_file.write("\n// Listing the SubModelParts: \"" + '", "'.join(sorted(self.sub_model_parts.keys())) + "\"\n")
open_file.write("\n")
def __WriteNodes(self, open_file, readable_mdpa):
open_file.write("Begin Nodes\n")
if readable_mdpa:
max_ID = max(self.nodes.keys())
global_utils.LogDebug("Max Node ID: " + str(max_ID))
spaces_coords_x = '{:>' + str(len(str(int(self.max_node_coord_x))) + self.precision + self.num_spaces) + '} '
spaces_coords_y = '{:>' + str(len(str(int(self.max_node_coord_y))) + self.precision + self.num_spaces) + '} '
spaces_coords_z = '{:>' + str(len(str(int(self.max_node_coord_z))) + self.precision + self.num_spaces) + '} '
format_str = '{:>' + str(len(str(max_ID))) + '} ' + spaces_coords_x + spaces_coords_y + spaces_coords_z
else:
format_str = '{} {} {} {}'
global_utils.LogDebug("Node Format String: " + str(format_str))
for ID in sorted(list(self.nodes.keys())):
coords = self.nodes[ID][0]
coords = [round(coords[0], self.precision),
round(coords[1], self.precision),
round(coords[2], self.precision)]
line = format_str.format(str(ID), coords[0], str(coords[1]), str(coords[2])) + "\n"
open_file.write(line)
open_file.write("End Nodes\n\n")
def __WriteElements(self, open_file, readable_mdpa):
if readable_mdpa:
num_elements = self.NumberOfElements()
format_str = '{:>' + str(len(str(num_elements))) + '} {:>' + str(self.num_spaces) + '}'
space = "\t"
else:
format_str = '{} {}'
space = " "
global_utils.LogDebug("Element Format String: " + str(format_str))
for element_name in sorted(list(self.elements.keys())):
open_file.write("Begin Elements " + element_name + "\n")
elements_by_name = self.elements[element_name]
for elem in elements_by_name:
open_file.write(elem.GetWriteLine(format_str, space) + "\n")
open_file.write("End Elements // " + element_name + "\n\n")
def __WriteNodalData(self, open_file, readable_mdpa):
all_geom_entity_data = {}
# Extracting the Data from the Nodes
for node_id in sorted(list(self.nodes.keys())):
node = self.nodes[node_id]
nodal_data = node[1]
if len(nodal_data) > 0:
for var_name, var_data in nodal_data.items():
if not var_name in all_geom_entity_data:
all_geom_entity_data[var_name] = {}
all_geom_entity_data[var_name][node_id] = var_data
self.__WriteEntityData(open_file, readable_mdpa, all_geom_entity_data, "Nod")
def __WriteGeometricalEntityData(self, open_file, readable_mdpa, geom_entities, entity_name):
all_geom_entity_data = {}
# Extracting the Data from the geom_entities
for geom_entity_name in sorted(list(geom_entities.keys())):
geom_entities_by_name = geom_entities[geom_entity_name]
for geom_entity in geom_entities_by_name:
if geom_entity.HasEntityData():
geom_entity_data = geom_entity.GetEntityData()
geom_entity_id = geom_entity.GetID()
for var_name, var_data in geom_entity_data.items():
if not var_name in all_geom_entity_data:
all_geom_entity_data[var_name] = {}
all_geom_entity_data[var_name][geom_entity_id] = var_data
self.__WriteEntityData(open_file, readable_mdpa, all_geom_entity_data, entity_name)
def __WriteEntityData(self, open_file, readable_mdpa, all_geom_entity_data, entity_name):
entity_name += "alData" # Convert e.g. "Element" to "ElementalData"
if readable_mdpa:
space = " "
else:
space = ""
for var_name in sorted(list(all_geom_entity_data.keys())):
geom_entity_data_by_var = all_geom_entity_data[var_name]
rand_var = next(iter(geom_entity_data_by_var.values())) # gettign a random value to check the type
var_type = self.__GetVariableType(rand_var)
open_file.write("Begin " + entity_name + " " + var_name + "\n")
if var_type == "scalar":
for geom_entity_data_id in sorted(list(geom_entity_data_by_var.keys())):
data = geom_entity_data_by_var[geom_entity_data_id]
open_file.write(space + str(geom_entity_data_id) + " " + str(data) + "\n")
elif var_type == "vector":
for geom_entity_data_id in sorted(list(geom_entity_data_by_var.keys())):
data = geom_entity_data_by_var[geom_entity_data_id]
open_file.write(space + str(geom_entity_data_id) + " [" + str(len(data)) + "] ( ")
open_file.write(str(data[0]))
for entry in data[1:]:
open_file.write(" , " + str(entry))
open_file.write(" )\n")
elif var_type == "matrix":
raise NotImplementedError("writting Matrices as Data is not yet implemented!")
open_file.write("End " + entity_name + " // " + var_name + "\n\n")
def __GetVariableType(self, variable):
if type(variable) == list:
if len(variable) == 0:
raise Exception("Entity data vector cannot have size 0!")
if type(variable[0]) == list:
return "matrix"
elif isinstance(variable[0], (float, int)):
return "vector"
else:
raise Exception("Wrong data type!", type(variable[0]))
elif isinstance(variable, (float, int)):
return "scalar"
else:
raise Exception("Wrong data type!", type(variable))
def __WriteConditions(self, open_file, readable_mdpa):
if readable_mdpa:
num_conditions = self.NumberOfConditions()
format_str = '{:>' + str(len(str(num_conditions))) + '} {:>' + str(self.num_spaces) + '}'
space = "\t"
else:
format_str = '{} {}'
space = " "
global_utils.LogDebug("Condition Format String: " + str(format_str))
for condition_name in sorted(list(self.conditions.keys())):
open_file.write("Begin Conditions " + condition_name + "\n")
conditions_by_name = self.conditions[condition_name]
for cond in conditions_by_name:
open_file.write(cond.GetWriteLine(format_str, space) + "\n")
open_file.write("End Conditions // " + condition_name + "\n\n")
def __Assemble(self, readable_mdpa):
# TODO Check if this was done before! (same for the submodelparts)
start_time = time.time()
global_utils.LogInfo("Assembling Mesh")
self.__InitializeMesh()
for smp_name in sorted(self.sub_model_parts.keys()):
smp = self.sub_model_parts[smp_name]
smp.Assemble()
smp_nodes, smp_elements, smp_conditions = smp.GetMesh()
self.__AddNodes(smp_nodes, readable_mdpa)
self.__AddElements(smp_elements)
self.__AddConditions(smp_conditions)
global_utils.LogTiming("Mesh assembling time", start_time)
def __AddNodes(self, smp_nodes, readable_mdpa):
for node_ID in smp_nodes.keys():
if node_ID in self.nodes.keys():
existing_node_coords = self.nodes[node_ID][0]
new_coords = smp_nodes[node_ID][0]
if existing_node_coords != new_coords:
err_msg = "Node with ID " + str(node_ID) + " already exists with different coordinates!\n"
err_msg += "\tExisting Coords: [" + str(existing_node_coords[0]) + " , "
err_msg += str(existing_node_coords[1]) + " , "
err_msg += str(existing_node_coords[2]) + "]\n"
err_msg += "\tNew Coords: [" + str(new_coords[0]) + " , "
err_msg += str(new_coords[1]) + " , "
err_msg += str(new_coords[2]) + "] "
raise Exception(err_msg)
else:
self.nodes[node_ID] = smp_nodes[node_ID]
if readable_mdpa:
if smp_nodes[node_ID][0][0] > self.max_node_coord_x: self.max_node_coord_x = smp_nodes[node_ID][0][0]
if smp_nodes[node_ID][0][1] > self.max_node_coord_y: self.max_node_coord_y = smp_nodes[node_ID][0][1]
if smp_nodes[node_ID][0][2] > self.max_node_coord_z: self.max_node_coord_z = smp_nodes[node_ID][0][2]
def __AddElements(self, smp_elements):
self.__AddGeometricEntities(smp_elements, self.elements)
def __AddConditions(self, smp_conditions):
self.__AddGeometricEntities(smp_conditions, self.conditions)
def __AddGeometricEntities(self, smp_entities, all_entities):
id_index = sum([len(val) for val in all_entities.values()]) + 1
for entity_name in sorted(smp_entities.keys()):
entities = smp_entities[entity_name]
if entity_name not in all_entities.keys():
all_entities[entity_name] = []
for entity in entities:
if not entity.IsAddedAlready():
all_entities[entity_name].append(entity)
entity.SetID(id_index)
entity.SetIsAdded()
id_index += 1
def __ClearAfterWriting(self):
"""Clearing some old entries
Esp since the geometric-entities store the information abt child-elements!
"""
for smp in self.sub_model_parts.values():
smp.ClearAfterWriting()
def NumberOfNodes(self):
return len(self.nodes)
def NumberOfElements(self):
return sum([len(val) for val in self.elements.values()])
def NumberOfConditions(self):
return sum([len(val) for val in self.conditions.values()])
class MeshSubmodelPart:
def __init__(self):
"""Constructor of the MeshSubModelPart
Sets some internal variables
"""
self.is_properly_initialized = False
self.is_assembled = False
def __InitializeMesh(self):
"""This function initializes the member variables for the mesh
"""
self.nodes = {}
self.elements = {}
self.conditions = {}
def FillWithEntities(self, smp_info_dict, mesh_dict, nodes_read, geom_entities_read):
"""This function fills the MeshSubModelPart with entities
This function has to be called before other operations such
as assembling can be performed
"""
self.__ValidateSMPInfoDict(smp_info_dict)
self.smp_info_dict = smp_info_dict
self.__ValidateMeshDict(mesh_dict)
self.mesh_dict = mesh_dict
self.nodes_read = nodes_read
self.geom_entities_read = geom_entities_read
self.is_properly_initialized = True
def Update(self, smp_info_dict, mesh_dict):
"""This function updates the MeshSubModelPart
E.g. if different Elements/Conditions should
be created from the Geometric Entities
"""
self.__CheckIsProperlyInitialized()
self.__ValidateSMPInfoDict(smp_info_dict)
self.smp_info_dict = smp_info_dict
self.__ValidateMeshDict(mesh_dict)
self.mesh_dict = mesh_dict
self.is_assembled = False
def __ValidateSMPInfoDict(self, smp_info_dict):
"""This function adds missing information to the
dictionary based on default values.
It mimics the "ValidateAndAssignDefaults" function in Kratos
"""
default_smp_info_dict = {
"smp_name" : "PLEASE_SPECIFY_SUBMODELPART_NAME",
"smp_file_name" : "",
"smp_file_path" : ""
}
for k,v in default_smp_info_dict.items():
if k not in smp_info_dict:
smp_info_dict[k] = v # Assigning the default value
def __ValidateMeshDict(self, mesh_dict):
"""This function adds missing information to the
dictionary based on default values.
It mimics the "ValidateAndAssignDefaults" function in Kratos
"""
default_mesh_dict = {
"entity_creation" : {},
"write_smp" : True
}
for k,v in default_mesh_dict.items():
if k not in mesh_dict:
mesh_dict[k] = v # Assigning the default value
def Assemble(self):
"""This function assembles the entities in the MeshSubModelPart
After this operation the Object is ready to write it's
entities to an mdpa file
"""
self.__CheckIsProperlyInitialized()
self.__InitializeMesh()
# This function creates the Kratos entities from the read entities
# that are defined in it's dictionary
self.__AddNodes()
self.__AddElements()
self.__AddConditions()
self.is_assembled = True
def __AddNodes(self):
self.nodes = self.nodes_read
def __AddElements(self):
self.__AddGeometricEntities("Element", Element, self.elements)
def __AddConditions(self):
self.__AddGeometricEntities("Condition", Condition, self.conditions)
def __AddGeometricEntities(self, entity_name, entity_class, entity_container):
entity_container.clear()
for geometry_identifier in self.mesh_dict["entity_creation"].keys():
if geometry_identifier == global_utils.NODE_IDENTIFIER:
if geometry_identifier in self.geom_entities_read:
entities = self.geom_entities_read[geometry_identifier] # in case the 101-entities were created outside
else:
entities = self.__CreateGeometricEntitiesFromNodes(self.nodes)
else:
entities = self.geom_entities_read[geometry_identifier]
if entity_name in self.mesh_dict["entity_creation"][geometry_identifier]:
entity_dict = self.mesh_dict["entity_creation"][geometry_identifier][entity_name]
entity_name_list = entity_dict.keys()
for ent_name in sorted(entity_name_list):
if ent_name not in entity_container:
entity_container[ent_name] = []
property_ID = entity_dict[ent_name]
for entity in entities:
new_entity = entity.GetChildObject(ent_name, entity_class, property_ID)
entity_container[ent_name].append(new_entity)
def __CreateGeometricEntitiesFromNodes(self, nodes):
"""This function creates objects of type "GeometricEntity"
from Nodes. This is needed for point-based entities in Kratos,
e.g. PointLoadCondition, NodalConcentratedElement
"""
geom_entities = []
for node_id, node_data in nodes.items():
nodal_data = node_data[1]
if type(nodal_data) != dict:
raise Exception("wrong datatype for nodal_data, type received: "+ str(type(nodal_data)))
geom_entities.append(global_utils.GeometricEntity(-1,
global_utils.NODE_IDENTIFIER,
[node_id],
nodal_data))
return geom_entities
def GetMesh(self):
self.__CheckIsAssembled()
return self.nodes, self.elements, self.conditions
def GetGeomEntites(self): # TODO still needed?
return self.geom_entities_read
def GetInfoDict(self):
self.__CheckIsProperlyInitialized()
return self.smp_info_dict
def GetMeshInfoDict(self):
self.__CheckIsProperlyInitialized()
return self.mesh_dict
def GetFileName(self):
if self.is_properly_initialized:
return self.smp_info_dict["smp_file_name"]
else:
return ""
def GetFilePath(self):
if self.is_properly_initialized:
return self.smp_info_dict["smp_file_path"]
else:
return ""
def NumberOfNodes(self):
"""Returns the number of Nodes in this SubModelPart
"""
self.__CheckIsAssembled()
return len(self.nodes)
def NumberOfElements(self):
"""Returns the number of Elements in this SubModelPart
"""
self.__CheckIsAssembled()
return sum([len(val) for val in self.elements.values()])
def NumberOfConditions(self):
"""Returns the number of Conditions in this SubModelPart
"""
self.__CheckIsAssembled()
return sum([len(val) for val in self.conditions.values()])
def WriteMesh(self, open_file, readable_mdpa=False):
"""This function writes the SubModelPart to the
mdpa-file (If wanted).
"""
self.__CheckIsAssembled()
# Write Header
if self.mesh_dict["write_smp"]:
smp_name = self.smp_info_dict["smp_name"]
open_file.write("Begin SubModelPart " + smp_name + "\n")
space = ""
if readable_mdpa:
space = "\t"
self.__WriteNodes(open_file, space)
self.__WriteElements(open_file, space)
self.__WriteConditions(open_file, space)
open_file.write("End SubModelPart // " + smp_name + "\n\n")
def __WriteNodes(self, open_file, space):
"""This function write the SubModelPartNodes to the
mdpa-file.
Note that these are only the Ids of the Nodes
"""
self.__CheckIsAssembled()
open_file.write(space + "Begin SubModelPartNodes\n")
for ID in sorted(self.nodes.keys()):
open_file.write(space + space + str(ID) + "\n")
open_file.write(space + "End SubModelPartNodes\n")
def __WriteElements(self, open_file, space):
"""This function write the SubModelPartElements to the
mdpa-file.
"""
self.__WriteEntityIds(open_file, space, "Elements", self.elements)
def __WriteConditions(self, open_file, space):
"""This functin write the SubModelPartConditions to the
mdpa-file.
"""
self.__WriteEntityIds(open_file, space, "Conditions", self.conditions)
def __WriteEntityIds(self, open_file, space, entity_type_name, entities):
"""This function writes the Ids of the entities to the
mdpa file.
Note that these are only the Ids of the entities
"""
self.__CheckIsAssembled()
smp_entities_name = "SubModelPart" + entity_type_name
open_file.write(space + "Begin " + smp_entities_name + "\n")
for entity_name in sorted(entities.keys()):
for entity in entities[entity_name]:
open_file.write(space + space + str(entity.GetID()) + "\n")
open_file.write(space + "End " + smp_entities_name + "\n")
def WriteSubModelPart(self):
return self.mesh_dict["write_smp"]
def WriteMeshInfo(self, open_file):
"""This function is called by the parent class to write info about this
SubModelPart to the header of the mdpa-file
"""
self.__CheckIsAssembled()
if self.mesh_dict["write_smp"]:
open_file.write("// SubModelPart " + self.smp_info_dict["smp_name"] + "\n")
open_file.write("// Number of Nodes: " + str(self.NumberOfNodes()) + "\n")
open_file.write("// Number of Elements: " + str(self.NumberOfElements()) + "\n")
open_file.write("// Number of Conditions: " + str(self.NumberOfConditions()) + "\n")
def __CheckIsAssembled(self):
"""This function checks if the MeshSubModelPart has been assembled
It is used internally to ensure that the MeshSubModelPart is assembled
"""
self.__CheckIsProperlyInitialized()
if not self.is_assembled:
raise RuntimeError("MeshSubmodelPart was not assembled!")
def __CheckIsProperlyInitialized(self):
"""This function checks if the MeshSubModelPart has been properly initialized
It is used internally to ensure that the MeshSubModelPart is properly initialized
"""
if not self.is_properly_initialized:
raise RuntimeError("MeshSubmodelPart is not properly initialized!")
def ClearAfterWriting(self):
"""Clearing some old entries
Esp since the geometric-entities store the information abt child-elements!
"""
for geom_entities_by_key in self.geom_entities_read.values():
for geom_entity in geom_entities_by_key:
geom_entity.ClearChildObjects()
##############################################
##### Functions related to Serialization #####
##############################################
def Serialize(self):
self.__CheckIsProperlyInitialized()
global_utils.LogDebug("Serializing " + self.smp_info_dict["smp_name"])
serialized_smp = {}
serialized_smp["submodelpart_information"] = self.smp_info_dict
serialized_smp["mesh_information"] = self.mesh_dict
serialized_smp["nodes_read"] = self.__SerializeNodesRead()
serialized_smp["geom_entities_read"] = self.__SerializeGeomEntitiesRead()
return {self.smp_info_dict["smp_name"] : serialized_smp}
def __SerializeNodesRead(self):
self.__AddNodes() # Update the internal information
return self.nodes
def __SerializeGeomEntitiesRead(self):
serialized_geom_entities = []
for salome_ID in self.geom_entities_read:
for entity in self.geom_entities_read[salome_ID]:
serialized_geom_entities.append(entity.Serialize())
return serialized_geom_entities
def Deserialize(self, smp_name, serialized_smp):
smp_info_dict, mesh_dict = self.__DeserializeDictionary(serialized_smp)
nodes_read = {}
geom_entities_read = {}
if "nodes_read" in serialized_smp:
nodes_read = self.__DeserializeNodesRead(serialized_smp["nodes_read"])
if "geom_entities_read" in serialized_smp: # Geometric Entities can only exist if there are nodes!
geom_entities_read = self.__DeserializeGeomEntitiesRead(serialized_smp["geom_entities_read"])
self.FillWithEntities(smp_info_dict, mesh_dict, nodes_read, geom_entities_read)
global_utils.LogDebug("Deserialized " + smp_name)
def __DeserializeDictionary(self, serialized_smp):
# concert the keys to strings (has to be done bcs json converts ints to string)
if not "submodelpart_information" in serialized_smp:
raise RuntimeError("\"submodelpart_information\" is not in serialized SubModelPart!")
if not "mesh_information" in serialized_smp:
raise RuntimeError("\"mesh_information\" is not in serialized SubModelPart!")
mesh_dict = global_utils.CorrectMeshDict(serialized_smp["mesh_information"])
return serialized_smp["submodelpart_information"], mesh_dict
def __DeserializeNodesRead(self, serialized_nodes_read):
return global_utils.DictKeyToInt(serialized_nodes_read) # Nodes don't need deserialization
def __DeserializeGeomEntitiesRead(self, serialized_geom_entities_read):
deserialized_geom_entities_read = {}
for serialized_entity in serialized_geom_entities_read:
geom_entity = global_utils.GeometricEntity.Deserialize(serialized_entity)
geometry_identifier = geom_entity.GetGeometryIdentifier()
if geometry_identifier not in deserialized_geom_entities_read.keys(): # geom entities with this identifier are already existing # TODO don't I have to use .key() here?
deserialized_geom_entities_read[geometry_identifier] = []
deserialized_geom_entities_read[geometry_identifier].append(geom_entity)
return deserialized_geom_entities_read