-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_geometries_io.py
1340 lines (1048 loc) · 68.1 KB
/
test_geometries_io.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
# _ __ _ ___ _ ___ _ _
# | |/ /_ _ __ _| |_ ___ __/ __| __ _| |___ _ __ ___| _ \ |_ _ __ _(_)_ _
# | ' <| '_/ _` | _/ _ (_-<__ \/ _` | / _ \ ' \/ -_) _/ | || / _` | | ' \
# |_|\_\_| \__,_|\__\___/__/___/\__,_|_\___/_|_|_\___|_| |_|\_,_\__, |_|_||_|
# |___/
# License: BSD License ; see LICENSE
#
# Main authors: Philipp Bucher (https://github.com/philbucher)
#
# set up testing environment (before anything else)
import initialize_testing_environment
# python imports
import unittest
from unittest.mock import MagicMock
from abc import ABCMeta, abstractmethod
# plugin imports
import kratos_salome_plugin.model_part as py_model_part
from kratos_salome_plugin import geometries_io
from kratos_salome_plugin.mesh_interface import MeshInterface
from kratos_salome_plugin import salome_utilities
# tests imports
from testing_utilities import SalomeTestCaseWithBox, CheckIfKratosAvailable
# Kratos imports
kratos_available = CheckIfKratosAvailable()
if kratos_available:
import KratosMultiphysics as KM
class TestGeometriesIOWithMockMeshInterfaces:
"""This TestCase contains basic tests for the GeometriesIO where the MeshInterface is substituted by a Mock object
"""
class BaseTests(unittest.TestCase, metaclass=ABCMeta):
@abstractmethod
def _CreateModelPart(self, name): pass
def test_not_same_main_mesh(self):
model_part = self._CreateModelPart()
# apparently if not configuring this, it returns True
attrs = { 'DoMeshesBelongToSameMainMesh.return_value': False }
mesh_interface_mock = MagicMock(spec=MeshInterface)
mesh_interface_mock.configure_mock(**attrs)
meshes = [geometries_io.Mesh(mesh_interface_mock, {})]
with self.assertRaisesRegex(Exception, 'The meshes to be added to ModelPart "for_test" don\'t belong to the same main mesh!\nThis is necessary to ensure a consistent numbering.'):
geometries_io.GeometriesIO.AddMeshes(model_part, meshes)
def test_add_nodes_from_one_mesh_to_main_model_part(self):
self.__ExecuteTestAddNodesFromOneMeshToModelPart("")
def test_add_nodes_from_one_mesh_to_sub_model_part(self):
self.__ExecuteTestAddNodesFromOneMeshToModelPart("sub_mp")
def test_add_nodes_from_one_mesh_to_sub_sub_model_part(self):
self.__ExecuteTestAddNodesFromOneMeshToModelPart("subda3_mp.the_sub_sub_model_part")
def __ExecuteTestAddNodesFromOneMeshToModelPart(self, model_part_name):
model_part = self._CreateModelPart()
the_nodes = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
attrs = { 'GetNodesAndGeometricalEntities.return_value': (the_nodes, {}) }
mesh_interface_mock = MagicMock(spec=MeshInterface)
mesh_interface_mock.configure_mock(**attrs)
meshes = [geometries_io.Mesh(mesh_interface_mock, {}, model_part_name)]
geometries_io.GeometriesIO.AddMeshes(model_part, meshes)
def CheckModelPart(model_part_to_check):
self.assertEqual(len(the_nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in the_nodes)
orig_node_coords = the_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
self.__RecursiveCheckModelParts(model_part, model_part_name, CheckModelPart)
def test_add_nodes_from_multiple_meshes_to_main_model_part(self):
self.__ExecuteTestAddNodesFromMultipleMeshesToModelPart("")
def test_add_nodes_from_multiple_meshes_to_sub_model_part(self):
self.__ExecuteTestAddNodesFromMultipleMeshesToModelPart("my_sub")
def test_add_nodes_from_multiple_meshes_to_sub_sub_model_part(self):
self.__ExecuteTestAddNodesFromMultipleMeshesToModelPart("sub_mp_mul.sub_sub_mult_meshes")
def __ExecuteTestAddNodesFromMultipleMeshesToModelPart(self, model_part_name):
model_part = self._CreateModelPart()
nodes_mesh_1 = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
attrs_mesh_1 = { 'GetNodesAndGeometricalEntities.return_value': (nodes_mesh_1, {}) }
mesh_interface_mock_mesh_1 = MagicMock(spec=MeshInterface)
mesh_interface_mock_mesh_1.configure_mock(**attrs_mesh_1)
nodes_mesh_2 = {i+25 : [i+3.5,i-13.22,i*2] for i in range(5)}
attrs_mesh_2 = { 'GetNodesAndGeometricalEntities.return_value': (nodes_mesh_2, {}) }
mesh_interface_mock_mesh_2 = MagicMock(spec=MeshInterface)
mesh_interface_mock_mesh_2.configure_mock(**attrs_mesh_2)
nodes_mesh_3 = {i+1 : [i+1,i*2,i+3.5] for i in range(3)} # those nodes have the same coord, hence no new nodes will be created!
attrs_mesh_3 = { 'GetNodesAndGeometricalEntities.return_value': (nodes_mesh_3, {}) }
mesh_interface_mock_mesh_3 = MagicMock(spec=MeshInterface)
mesh_interface_mock_mesh_3.configure_mock(**attrs_mesh_3)
meshes = [
geometries_io.Mesh(mesh_interface_mock_mesh_1, {}, model_part_name),
geometries_io.Mesh(mesh_interface_mock_mesh_2, {}, model_part_name),
geometries_io.Mesh(mesh_interface_mock_mesh_3, {}, model_part_name)
]
geometries_io.GeometriesIO.AddMeshes(model_part, meshes)
def CheckModelPart(model_part_to_check):
self.assertEqual(len(nodes_mesh_1) + len(nodes_mesh_2), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
if node.Id > 16:
ref_nodes = nodes_mesh_2
else:
ref_nodes = nodes_mesh_1
self.assertTrue(node.Id in ref_nodes)
orig_node_coords = ref_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
self.__RecursiveCheckModelParts(model_part, model_part_name, CheckModelPart)
def test_add_elements_from_one_mesh_to_main_model_part(self):
self.__ExecuteTestAddElementsFromOneMeshToModelPart("")
def test_add_elements_from_one_mesh_to_sub_model_part(self):
self.__ExecuteTestAddElementsFromOneMeshToModelPart("sub_mp_el")
def test_add_elements_from_one_mesh_to_sub_sub_model_part(self):
self.__ExecuteTestAddElementsFromOneMeshToModelPart("subda3_mp.the_sub_sub_element_model_part")
def __ExecuteTestAddElementsFromOneMeshToModelPart(self, model_part_name):
model_part = self._CreateModelPart()
geometry_name = "Line"
element_name = "Element2D2N"
props_id = 12
mesh_description = { "elements" : {geometry_name : {element_name : props_id} } }
the_nodes = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
the_geom_entities = {geometry_name : {i+1 : [i+1, i+2] for i in range(14)}}
attrs = { 'GetNodesAndGeometricalEntities.return_value': (the_nodes, the_geom_entities) }
mesh_interface_mock = MagicMock(spec=MeshInterface)
mesh_interface_mock.configure_mock(**attrs)
meshes = [geometries_io.Mesh(mesh_interface_mock, mesh_description, model_part_name)]
geometries_io.GeometriesIO.AddMeshes(model_part, meshes)
def CheckModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id))
self.assertEqual(len(the_nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in the_nodes)
orig_node_coords = the_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
self.assertEqual(len(the_geom_entities[geometry_name]), model_part_to_check.NumberOfElements())
for elem in model_part_to_check.Elements:
# checking the connectivities
self.assertTrue(elem.Id in the_geom_entities[geometry_name])
nodes_id_list = sorted([node.Id for node in elem.GetNodes()])
self.assertListEqual(sorted(the_geom_entities[geometry_name][elem.Id]), nodes_id_list)
self.__RecursiveCheckModelParts(model_part, model_part_name, CheckModelPart)
def test_add_elements_from_multiple_meshes_to_main_model_part(self):
self.__ExecuteTestAddElementsFromMultipleMeshesToModelPart("")
def test_add_elements_from_multiple_meshes_to_sub_model_part(self):
self.__ExecuteTestAddElementsFromMultipleMeshesToModelPart("sub_mpss_el")
def test_add_elements_from_multiple_meshes_to_sub_sub_model_part(self):
self.__ExecuteTestAddElementsFromMultipleMeshesToModelPart("subdafq23_mp.the_sub_sub_elemaent_model_part")
def __ExecuteTestAddElementsFromMultipleMeshesToModelPart(self, model_part_name):
model_part = self._CreateModelPart()
geometry_name = "Line"
element_name = "Element2D2N"
props_id = 12
mesh_description = { "elements" : {geometry_name : {element_name : props_id} } }
nodes_mesh_1 = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
geometries_mesh_1 = {geometry_name : {i+1 : [i+1, i+2] for i in range(14)}}
attrs_mesh_1 = { 'GetNodesAndGeometricalEntities.return_value': (nodes_mesh_1, geometries_mesh_1) }
mesh_interface_mock_mesh_1 = MagicMock(spec=MeshInterface)
mesh_interface_mock_mesh_1.configure_mock(**attrs_mesh_1)
nodes_mesh_2 = {i+1 : [i*2,i+88.5, i-1] for i in range(33,39)}
geometries_mesh_2 = {geometry_name : {i+1 : [i+1, i+2] for i in range(33,38)}}
attrs_mesh_2 = { 'GetNodesAndGeometricalEntities.return_value': (nodes_mesh_2, geometries_mesh_2) }
mesh_interface_mock_mesh_2 = MagicMock(spec=MeshInterface)
mesh_interface_mock_mesh_2.configure_mock(**attrs_mesh_2)
# this mesh is a subset of mesh 1, hence NO new entities should be created from this mesh!
nodes_mesh_3 = {i+1 : [i+1,i*2,i+3.5] for i in range(5)}
geometries_mesh_3 = {geometry_name : {i+1 : [i+1, i+2] for i in range(3)}}
attrs_mesh_3 = { 'GetNodesAndGeometricalEntities.return_value': (nodes_mesh_3, geometries_mesh_3) }
mesh_interface_mock_mesh_3 = MagicMock(spec=MeshInterface)
mesh_interface_mock_mesh_3.configure_mock(**attrs_mesh_3)
meshes = [
geometries_io.Mesh(mesh_interface_mock_mesh_1, mesh_description, model_part_name),
geometries_io.Mesh(mesh_interface_mock_mesh_2, mesh_description, model_part_name),
geometries_io.Mesh(mesh_interface_mock_mesh_3, mesh_description, model_part_name)
]
geometries_io.GeometriesIO.AddMeshes(model_part, meshes)
def CheckModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id))
self.assertEqual(len(nodes_mesh_1)+len(nodes_mesh_2), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
if node.Id > 16:
ref_nodes = nodes_mesh_2
else:
ref_nodes = nodes_mesh_1
self.assertTrue(node.Id in ref_nodes)
orig_node_coords = ref_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
self.assertEqual(len(geometries_mesh_1[geometry_name])+len(geometries_mesh_2[geometry_name]), model_part_to_check.NumberOfElements())
for elem in model_part_to_check.Elements:
# checking the connectivities
# Id offset is required because Elements are renumbered!
if elem.Id > 14:
ref_geometries = geometries_mesh_2[geometry_name]
id_offset = 19
else:
ref_geometries = geometries_mesh_1[geometry_name]
id_offset = 0
self.assertTrue(elem.Id+id_offset in ref_geometries)
nodes_id_list = sorted([node.Id for node in elem.GetNodes()])
self.assertListEqual(sorted(ref_geometries[elem.Id+id_offset]), nodes_id_list)
self.assertEqual(elem.Properties.Id, props_id)
self.__RecursiveCheckModelParts(model_part, model_part_name, CheckModelPart)
def test_add_different_elements_to_one_modelpart(self):
# note that usually different elements/conditions are added to different SubModelParts
# and hence also to the MainModelPart
# in this test however we intentionally check the creation of multiple elements form
# the same mesh
model_part = self._CreateModelPart()
geometry_name_1D = "Line"
element_name_1D = "Element2D2N"
props_id_1D = 12
geometry_name_2D = "Triangle"
element_name_2D = "Element2D3N"
props_id_2D = 77
geometry_name_3D = "Tetra"
element_name_3D = "Element3D4N"
props_id_3D = 6
mesh_description = { "elements" : {
geometry_name_1D : {element_name_1D : props_id_1D},
geometry_name_2D : {element_name_2D : props_id_2D},
geometry_name_3D : {element_name_3D : props_id_3D}
}}
the_nodes = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
the_geom_entities = {
geometry_name_1D : {i+1 : [i+1, i+2] for i in range(12)},
geometry_name_2D : {i+1 : [(i+1)%15+1, (i+3)%15+1, (i+4)%15+1] for i in range(25)},
geometry_name_3D : {i+1 : [(i+2)%15+1, (i+6)%15+1, (i+4)%15+1, (i+8)%15+1] for i in range(31)}
}
attrs = { 'GetNodesAndGeometricalEntities.return_value': (the_nodes, the_geom_entities) }
mesh_interface_mock = MagicMock(spec=MeshInterface)
mesh_interface_mock.configure_mock(**attrs)
meshes = [geometries_io.Mesh(mesh_interface_mock, mesh_description, model_part.Name)]
geometries_io.GeometriesIO.AddMeshes(model_part, meshes)
def CheckModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id_1D))
self.assertTrue(model_part_to_check.HasProperties(props_id_2D))
self.assertTrue(model_part_to_check.HasProperties(props_id_3D))
self.assertEqual(len(the_nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in the_nodes)
orig_node_coords = the_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
self.assertEqual(sum([len(v) for v in the_geom_entities.values()]), model_part_to_check.NumberOfElements())
num_elements_1D = 0
num_elements_2D = 0
num_elements_3D = 0
for elem in model_part_to_check.Elements:
# Note: due to "mesh_description" and "the_geom_entities" being unordered
# and because everything is in one ModelPart we cannot check the connectivities
# it is probably possible with some smart sorting to get the Id-offsets
if len(elem.GetNodes()) == 2:
num_elements_1D += 1
ref_props_id = props_id_1D
elif len(elem.GetNodes()) == 3:
num_elements_2D += 1
ref_props_id = props_id_2D
elif len(elem.GetNodes()) == 4:
num_elements_3D += 1
ref_props_id = props_id_3D
else:
raise Exception("Unknown number of nodes!")
self.assertEqual(elem.Properties.Id, ref_props_id)
self.assertEqual(num_elements_1D, len(the_geom_entities[geometry_name_1D]))
self.assertEqual(num_elements_2D, len(the_geom_entities[geometry_name_2D]))
self.assertEqual(num_elements_3D, len(the_geom_entities[geometry_name_3D]))
CheckModelPart(model_part)
def test_add_different_elements_to_different_modelparts(self):
model_part = self._CreateModelPart()
geometry_name_1D = "Line"
element_name_1D = "Element2D2N"
props_id_1D = 12
smp_name_1D = "smp_1D_lines"
geometry_name_2D = "Triangle"
element_name_2D = "Element2D3N"
props_id_2D = 77
smp_name_2D = "surfaces"
geometry_name_3D = "Tetra"
element_name_3D = "Element3D4N"
props_id_3D = 6
smp_name_3D = "part_domain"
mesh_description_1D = { "elements" : {
geometry_name_1D : {element_name_1D : props_id_1D}
}}
mesh_description_2D = { "elements" : {
geometry_name_2D : {element_name_2D : props_id_2D}
}}
mesh_description_3D = { "elements" : {
geometry_name_3D : {element_name_3D : props_id_3D}
}}
the_nodes = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
the_geom_entities_1D = {
geometry_name_1D : {i+1 : [i+1, i+2] for i in range(12)}
}
the_geom_entities_2D = {
geometry_name_2D : {i+1 : [(i+1)%15+1, (i+3)%15+1, (i+4)%15+1] for i in range(25)}
}
the_geom_entities_3D = {
geometry_name_3D : {i+1 : [(i+2)%15+1, (i+6)%15+1, (i+4)%15+1, (i+8)%15+1] for i in range(31)}
}
attrs_1D = { 'GetNodesAndGeometricalEntities.return_value': (the_nodes, the_geom_entities_1D) }
attrs_2D = { 'GetNodesAndGeometricalEntities.return_value': (the_nodes, the_geom_entities_2D) }
attrs_3D = { 'GetNodesAndGeometricalEntities.return_value': (the_nodes, the_geom_entities_3D) }
mesh_interface_mock_1D = MagicMock(spec=MeshInterface)
mesh_interface_mock_1D.configure_mock(**attrs_1D)
mesh_interface_mock_2D = MagicMock(spec=MeshInterface)
mesh_interface_mock_2D.configure_mock(**attrs_2D)
mesh_interface_mock_3D = MagicMock(spec=MeshInterface)
mesh_interface_mock_3D.configure_mock(**attrs_3D)
meshes = [
geometries_io.Mesh(mesh_interface_mock_1D, mesh_description_1D, smp_name_1D),
geometries_io.Mesh(mesh_interface_mock_2D, mesh_description_2D, smp_name_2D),
geometries_io.Mesh(mesh_interface_mock_3D, mesh_description_3D, smp_name_3D)
]
geometries_io.GeometriesIO.AddMeshes(model_part, meshes)
def CheckMainModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id_1D))
self.assertTrue(model_part_to_check.HasProperties(props_id_2D))
self.assertTrue(model_part_to_check.HasProperties(props_id_3D))
self.assertEqual(len(the_nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in the_nodes)
orig_node_coords = the_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
self.assertEqual(len(the_geom_entities_1D[geometry_name_1D])+len(the_geom_entities_2D[geometry_name_2D])+len(the_geom_entities_3D[geometry_name_3D]), model_part_to_check.NumberOfElements())
for elem in model_part_to_check.Elements:
# TODO check everyhthing, now the order in which the entities are being created is deterministic
if len(elem.GetNodes()) == 2:
ref_props_id = props_id_1D
elif len(elem.GetNodes()) == 3:
ref_props_id = props_id_2D
elif len(elem.GetNodes()) == 4:
ref_props_id = props_id_3D
else:
raise Exception("Unknown number of nodes!")
self.assertEqual(elem.Properties.Id, ref_props_id)
def CheckSubModelPart(model_part_to_check, geometries, props_id, id_offset=0):
self.assertTrue(model_part_to_check.HasProperties(props_id))
self.assertEqual(len(the_nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in the_nodes)
orig_node_coords = the_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
self.assertEqual(len(geometries), model_part_to_check.NumberOfElements())
for elem in model_part_to_check.Elements:
# checking the connectivities
geom_id = elem.Id - id_offset
self.assertTrue(geom_id in geometries)
nodes_id_list = sorted([node.Id for node in elem.GetNodes()])
self.assertListEqual(sorted(geometries[geom_id]), nodes_id_list)
self.assertEqual(elem.Properties.Id, props_id)
CheckMainModelPart(model_part)
CheckSubModelPart(model_part.GetSubModelPart(smp_name_1D), the_geom_entities_1D[geometry_name_1D], props_id_1D)
id_offset_2D = len(the_geom_entities_1D[geometry_name_1D])
CheckSubModelPart(model_part.GetSubModelPart(smp_name_2D), the_geom_entities_2D[geometry_name_2D], props_id_2D, id_offset_2D)
id_offset_3D = id_offset_2D + len(the_geom_entities_2D[geometry_name_2D])
CheckSubModelPart(model_part.GetSubModelPart(smp_name_3D), the_geom_entities_3D[geometry_name_3D], props_id_3D, id_offset_3D)
def test_add_conditions_from_one_mesh_to_main_model_part(self):
self.__ExecuteTestAddConditionsFromOneMeshToModelPart("")
def test_add_conditions_from_one_mesh_to_sub_model_part(self):
self.__ExecuteTestAddConditionsFromOneMeshToModelPart("sub_mp_cond")
def test_add_conditions_from_one_mesh_to_sub_sub_model_part(self):
self.__ExecuteTestAddConditionsFromOneMeshToModelPart("subda3_mp.the_sub_sub_condition_model_part")
def __ExecuteTestAddConditionsFromOneMeshToModelPart(self, model_part_name):
model_part = self._CreateModelPart()
geometry_name = "Line"
condition_name = "LineCondition2D2N"
props_id = 12
mesh_description = { "conditions" : {geometry_name : {condition_name : props_id} } }
the_nodes = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
the_geom_entities = {geometry_name : {i+1 : [i+1, i+2] for i in range(14)}}
attrs = { 'GetNodesAndGeometricalEntities.return_value': (the_nodes, the_geom_entities) }
mesh_interface_mock = MagicMock(spec=MeshInterface)
mesh_interface_mock.configure_mock(**attrs)
meshes = [geometries_io.Mesh(mesh_interface_mock, mesh_description, model_part_name)]
geometries_io.GeometriesIO.AddMeshes(model_part, meshes)
def CheckModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id))
self.assertEqual(len(the_nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in the_nodes)
orig_node_coords = the_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
self.assertEqual(len(the_geom_entities[geometry_name]), model_part_to_check.NumberOfConditions())
for elem in model_part_to_check.Conditions:
# checking the connectivities
self.assertTrue(elem.Id in the_geom_entities[geometry_name])
nodes_id_list = sorted([node.Id for node in elem.GetNodes()])
self.assertListEqual(sorted(the_geom_entities[geometry_name][elem.Id]), nodes_id_list)
self.__RecursiveCheckModelParts(model_part, model_part_name, CheckModelPart)
def test_add_conditions_from_multiple_meshes_to_main_model_part(self):
self.__ExecuteTestAddConditionsFromMultipleMeshesToModelPart("")
def test_add_conditions_from_multiple_meshes_to_sub_model_part(self):
self.__ExecuteTestAddConditionsFromMultipleMeshesToModelPart("sub_mpss_conditions")
def test_add_conditions_from_multiple_meshes_to_sub_sub_model_part(self):
self.__ExecuteTestAddConditionsFromMultipleMeshesToModelPart("subdafq23_mp.the_sub_sub_elemaent_model_part")
def __ExecuteTestAddConditionsFromMultipleMeshesToModelPart(self, model_part_name):
model_part = self._CreateModelPart()
geometry_name = "Line"
condition_name = "LineCondition2D2N"
props_id = 12
mesh_description = { "conditions" : {geometry_name : {condition_name : props_id} } }
nodes_mesh_1 = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
geometries_mesh_1 = {geometry_name : {i+1 : [i+1, i+2] for i in range(14)}}
attrs_mesh_1 = { 'GetNodesAndGeometricalEntities.return_value': (nodes_mesh_1, geometries_mesh_1) }
mesh_interface_mock_mesh_1 = MagicMock(spec=MeshInterface)
mesh_interface_mock_mesh_1.configure_mock(**attrs_mesh_1)
nodes_mesh_2 = {i+1 : [i*2,i+88.5, i-1] for i in range(33,39)}
geometries_mesh_2 = {geometry_name : {i+1 : [i+1, i+2] for i in range(33,38)}}
attrs_mesh_2 = { 'GetNodesAndGeometricalEntities.return_value': (nodes_mesh_2, geometries_mesh_2) }
mesh_interface_mock_mesh_2 = MagicMock(spec=MeshInterface)
mesh_interface_mock_mesh_2.configure_mock(**attrs_mesh_2)
# this mesh is a subset of mesh 1, hence NO new entities should be created from this mesh!
nodes_mesh_3 = {i+1 : [i+1,i*2,i+3.5] for i in range(5)}
geometries_mesh_3 = {geometry_name : {i+1 : [i+1, i+2] for i in range(3)}}
attrs_mesh_3 = { 'GetNodesAndGeometricalEntities.return_value': (nodes_mesh_3, geometries_mesh_3) }
mesh_interface_mock_mesh_3 = MagicMock(spec=MeshInterface)
mesh_interface_mock_mesh_3.configure_mock(**attrs_mesh_3)
meshes = [
geometries_io.Mesh(mesh_interface_mock_mesh_1, mesh_description, model_part_name),
geometries_io.Mesh(mesh_interface_mock_mesh_2, mesh_description, model_part_name),
geometries_io.Mesh(mesh_interface_mock_mesh_3, mesh_description, model_part_name)
]
geometries_io.GeometriesIO.AddMeshes(model_part, meshes)
def CheckModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id))
self.assertEqual(len(nodes_mesh_1)+len(nodes_mesh_2), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
if node.Id > 16:
ref_nodes = nodes_mesh_2
else:
ref_nodes = nodes_mesh_1
self.assertTrue(node.Id in ref_nodes)
orig_node_coords = ref_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
self.assertEqual(len(geometries_mesh_1[geometry_name])+len(geometries_mesh_2[geometry_name]), model_part_to_check.NumberOfConditions())
for elem in model_part_to_check.Conditions:
# checking the connectivities
# Id offset is required because Conditions are renumbered!
if elem.Id > 14:
ref_geometries = geometries_mesh_2[geometry_name]
id_offset = 19
else:
ref_geometries = geometries_mesh_1[geometry_name]
id_offset = 0
self.assertTrue(elem.Id+id_offset in ref_geometries)
nodes_id_list = sorted([node.Id for node in elem.GetNodes()])
self.assertListEqual(sorted(ref_geometries[elem.Id+id_offset]), nodes_id_list)
self.__RecursiveCheckModelParts(model_part, model_part_name, CheckModelPart)
def test_add_to_existing_modelpart(self):
# in the other tests the (sub-)modelparts to which the entities are being don't exist yet
# in this test we make sure that it is possible to add entities to existing ModelParts
main_model_part = self._CreateModelPart()
sub_model_part = main_model_part.CreateSubModelPart("abc_sub")
the_nodes = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
attrs = { 'GetNodesAndGeometricalEntities.return_value': (the_nodes, {}) }
mesh_interface_mock = MagicMock(spec=MeshInterface)
mesh_interface_mock.configure_mock(**attrs)
meshes = [geometries_io.Mesh(mesh_interface_mock, {}, sub_model_part.Name)]
geometries_io.GeometriesIO.AddMeshes(main_model_part, meshes)
def CheckModelPart(model_part_to_check):
self.assertEqual(len(the_nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in the_nodes)
orig_node_coords = the_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
self.__RecursiveCheckModelParts(main_model_part, sub_model_part.Name, CheckModelPart)
def test_use_exsting_properties(self):
# in the other tests the properties that are used for the creation of element/conditions
# exist already. In this test the properties of the parent ModelPart are used
main_model_part = self._CreateModelPart()
sub_model_part = main_model_part.CreateSubModelPart("abc_sub")
geometry_name = "Line"
element_name = "Element2D2N"
props_id = 12
main_model_part.CreateNewProperties(props_id) # only exist in the main, but not yet in the submodelpart
mesh_description = { "elements" : {geometry_name : {element_name : props_id} } }
the_nodes = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
the_geom_entities = {geometry_name : {i+1 : [i+1, i+2] for i in range(14)}}
attrs = { 'GetNodesAndGeometricalEntities.return_value': (the_nodes, the_geom_entities) }
mesh_interface_mock = MagicMock(spec=MeshInterface)
mesh_interface_mock.configure_mock(**attrs)
meshes = [geometries_io.Mesh(mesh_interface_mock, mesh_description, sub_model_part.Name)]
geometries_io.GeometriesIO.AddMeshes(main_model_part, meshes)
def CheckModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id))
self.assertEqual(len(the_nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in the_nodes)
orig_node_coords = the_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
self.assertEqual(len(the_geom_entities[geometry_name]), model_part_to_check.NumberOfElements())
for elem in model_part_to_check.Elements:
# checking the connectivities
self.assertTrue(elem.Id in the_geom_entities[geometry_name])
nodes_id_list = sorted([node.Id for node in elem.GetNodes()])
self.assertListEqual(sorted(the_geom_entities[geometry_name][elem.Id]), nodes_id_list)
self.__RecursiveCheckModelParts(main_model_part, sub_model_part.Name, CheckModelPart)
def test_add_entities_to_Root_and_SubModelPart(self):
# in this test we check if adding the same entities to a main and submodelpart works
# first we add the entities to the submodelpart, then to the parent modelpart
# the created entities must be the same!
main_model_part = self._CreateModelPart()
sub_model_part = main_model_part.CreateSubModelPart("abc_sub")
geometry_name = "Line"
element_name = "Element2D2N"
props_id = 3
mesh_description = { "elements" : {geometry_name : {element_name : props_id} } }
nodes = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
geometries = {geometry_name : {i+1 : [i+1, i+2] for i in range(14)}}
attrs = { 'GetNodesAndGeometricalEntities.return_value': (nodes, geometries) }
mesh_interface_mock_sub_mp = MagicMock(spec=MeshInterface)
mesh_interface_mock_sub_mp.configure_mock(**attrs)
mesh_interface_mock_main_mp = MagicMock(spec=MeshInterface)
mesh_interface_mock_main_mp.configure_mock(**attrs)
meshes = [
geometries_io.Mesh(mesh_interface_mock_sub_mp, mesh_description, sub_model_part.Name),
geometries_io.Mesh(mesh_interface_mock_main_mp, mesh_description, main_model_part.Name)
]
geometries_io.GeometriesIO.AddMeshes(main_model_part, meshes)
def CheckModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id))
self.assertEqual(len(nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in nodes)
orig_node_coords = nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
self.assertEqual(len(geometries[geometry_name]), model_part_to_check.NumberOfElements())
for elem in model_part_to_check.Elements:
# checking the connectivities
ref_geometries = geometries[geometry_name]
self.assertTrue(elem.Id in ref_geometries)
nodes_id_list = sorted([node.Id for node in elem.GetNodes()])
self.assertListEqual(sorted(ref_geometries[elem.Id]), nodes_id_list)
self.__RecursiveCheckModelParts(main_model_part, sub_model_part.Name, CheckModelPart)
def test_add_entities_to_different_SubModelPart(self):
# in this test we check if adding the same entities to two sibling-submodelpart works
# the entities are added one after the other, and the created entities must be the same!
main_model_part = self._CreateModelPart()
sub_model_part_1 = main_model_part.CreateSubModelPart("abc_sub")
sub_model_part_2 = main_model_part.CreateSubModelPart("def_sub")
geometry_name = "Line"
element_name = "Element2D2N"
props_id = 3
mesh_description = { "elements" : {geometry_name : {element_name : props_id} } }
nodes = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
geometries = {geometry_name : {i+1 : [i+1, i+2] for i in range(14)}}
attrs = { 'GetNodesAndGeometricalEntities.return_value': (nodes, geometries) }
mesh_interface_mock_sub_mp_1 = MagicMock(spec=MeshInterface)
mesh_interface_mock_sub_mp_1.configure_mock(**attrs)
mesh_interface_mock_sub_mp_2 = MagicMock(spec=MeshInterface)
mesh_interface_mock_sub_mp_2.configure_mock(**attrs)
meshes = [
geometries_io.Mesh(mesh_interface_mock_sub_mp_1, mesh_description, sub_model_part_1.Name),
geometries_io.Mesh(mesh_interface_mock_sub_mp_2, mesh_description, sub_model_part_2.Name)
]
geometries_io.GeometriesIO.AddMeshes(main_model_part, meshes)
def CheckModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id))
self.assertEqual(len(nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in nodes)
orig_node_coords = nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
self.assertEqual(len(geometries[geometry_name]), model_part_to_check.NumberOfElements())
for elem in model_part_to_check.Elements:
# checking the connectivities
ref_geometries = geometries[geometry_name]
self.assertTrue(elem.Id in ref_geometries)
nodes_id_list = sorted([node.Id for node in elem.GetNodes()])
self.assertListEqual(sorted(ref_geometries[elem.Id]), nodes_id_list)
self.__RecursiveCheckModelParts(main_model_part, sub_model_part_1.Name, CheckModelPart)
self.__RecursiveCheckModelParts(main_model_part, sub_model_part_2.Name, CheckModelPart)
def test_add_entities_different_properties_ids(self):
# checking if an error is thrown if the same entities are added with different properties Ids
main_model_part = self._CreateModelPart()
sub_model_part_1 = main_model_part.CreateSubModelPart("abc_sub")
sub_model_part_2 = main_model_part.CreateSubModelPart("def_sub")
geometry_name = "Line"
element_name = "Element2D2N"
props_id = 3
mesh_description = { "elements" : {geometry_name : {element_name : props_id} } }
mesh_description_2 = { "elements" : {geometry_name : {element_name : props_id+2} } }
nodes = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
geometries = {geometry_name : {i+1 : [i+1, i+2] for i in range(14)}}
attrs = { 'GetNodesAndGeometricalEntities.return_value': (nodes, geometries) }
mesh_interface_mock_sub_mp_1 = MagicMock(spec=MeshInterface)
mesh_interface_mock_sub_mp_1.configure_mock(**attrs)
mesh_interface_mock_sub_mp_2 = MagicMock(spec=MeshInterface)
mesh_interface_mock_sub_mp_2.configure_mock(**attrs)
meshes = [
geometries_io.Mesh(mesh_interface_mock_sub_mp_1, mesh_description, sub_model_part_1.Name),
geometries_io.Mesh(mesh_interface_mock_sub_mp_2, mesh_description_2, sub_model_part_2.Name)
]
# this should throw because the entities that are supposed to be added to smp_2 have a different properties-Id, which is not possible!
with self.assertRaisesRegex(Exception, "Mismatch in properties Ids!\nTrying to use properties with Id 5 with an existing entity that has the properties with Id 3"):
geometries_io.GeometriesIO.AddMeshes(main_model_part, meshes)
def test_add_elements_and_conditions_to_same_model_part(self):
model_part = self._CreateModelPart()
smp_name = "smp_elemes_conds"
geometry_name_1D = "Line"
condition_name = "LineCondition2D2N"
props_id_conditions = 12
geometry_name_2D = "Triangle"
element_name = "Element2D3N"
props_id_elements = 77
mesh_description = { "elements" : {geometry_name_2D : {element_name : props_id_elements}},
"conditions" : {geometry_name_1D : {condition_name : props_id_conditions}}}
the_nodes = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
geometries = {
geometry_name_1D : {i+1 : [i+1, i+2] for i in range(12)},
geometry_name_2D : {i+1 : [(i+1)%15+1, (i+3)%15+1, (i+4)%15+1] for i in range(25)}}
attrs = { 'GetNodesAndGeometricalEntities.return_value': (the_nodes, geometries) }
mesh_interface_mock = MagicMock(spec=MeshInterface)
mesh_interface_mock.configure_mock(**attrs)
meshes = [geometries_io.Mesh( mesh_interface_mock, mesh_description, smp_name)]
geometries_io.GeometriesIO.AddMeshes(model_part, meshes)
def CheckModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id_elements))
self.assertTrue(model_part_to_check.HasProperties(props_id_conditions))
self.assertEqual(len(the_nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in the_nodes)
orig_node_coords = the_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
elem_geoms = geometries[geometry_name_2D]
self.assertEqual(len(elem_geoms), model_part_to_check.NumberOfElements())
for elem in model_part_to_check.Elements:
# checking the connectivities
self.assertTrue(elem.Id in elem_geoms)
nodes_id_list = sorted([node.Id for node in elem.GetNodes()])
self.assertListEqual(sorted(elem_geoms[elem.Id]), nodes_id_list)
self.assertEqual(elem.Properties.Id, props_id_elements)
cond_geoms = geometries[geometry_name_1D]
self.assertEqual(len(cond_geoms), model_part_to_check.NumberOfConditions())
for cond in model_part_to_check.Conditions:
# checking the connectivities
self.assertTrue(cond.Id in cond_geoms)
nodes_id_list = sorted([node.Id for node in cond.GetNodes()])
self.assertListEqual(sorted(cond_geoms[cond.Id]), nodes_id_list)
self.assertEqual(cond.Properties.Id, props_id_conditions)
self.__RecursiveCheckModelParts(model_part, smp_name, CheckModelPart)
def test_add_elements_and_conditions_to_different_model_parts(self):
model_part = self._CreateModelPart()
smp_name_conds = "smp_line_conds"
geometry_name_1D = "Line"
condition_name = "LineCondition2D2N"
props_id_conditions = 12
smp_name_elems = "smp_with_elements"
geometry_name_2D = "Triangle"
element_name = "Element2D3N"
props_id_elements = 77
mesh_description_elems = {"elements" : {geometry_name_2D : {element_name : props_id_elements}}}
mesh_description_conds = {"conditions" : {geometry_name_1D : {condition_name : props_id_conditions}}}
the_nodes = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
geometries_1D = {geometry_name_1D : {i+1 : [i+1, i+2] for i in range(12)}}
geometries_2D = {geometry_name_2D : {i+1 : [(i+1)%15+1, (i+3)%15+1, (i+4)%15+1] for i in range(25)}}
attrs_1D = { 'GetNodesAndGeometricalEntities.return_value': (the_nodes, geometries_1D) }
mesh_interface_mock_1D = MagicMock(spec=MeshInterface)
mesh_interface_mock_1D.configure_mock(**attrs_1D)
attrs_2D = { 'GetNodesAndGeometricalEntities.return_value': (the_nodes, geometries_2D) }
mesh_interface_mock_2D = MagicMock(spec=MeshInterface)
mesh_interface_mock_2D.configure_mock(**attrs_2D)
meshes = [
geometries_io.Mesh(mesh_interface_mock_1D, mesh_description_conds, smp_name_conds),
geometries_io.Mesh(mesh_interface_mock_2D, mesh_description_elems, smp_name_elems)
]
geometries_io.GeometriesIO.AddMeshes(model_part, meshes)
def CheckElementsModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id_elements))
self.assertEqual(len(the_nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in the_nodes)
orig_node_coords = the_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
elem_geoms = geometries_2D[geometry_name_2D]
self.assertEqual(len(elem_geoms), model_part_to_check.NumberOfElements())
for elem in model_part_to_check.Elements:
# checking the connectivities
self.assertTrue(elem.Id in elem_geoms)
nodes_id_list = sorted([node.Id for node in elem.GetNodes()])
self.assertListEqual(sorted(elem_geoms[elem.Id]), nodes_id_list)
self.assertEqual(elem.Properties.Id, props_id_elements)
def CheckConditionsModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id_conditions))
self.assertEqual(len(the_nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in the_nodes)
orig_node_coords = the_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
cond_geoms = geometries_1D[geometry_name_1D]
self.assertEqual(len(cond_geoms), model_part_to_check.NumberOfConditions())
for cond in model_part_to_check.Conditions:
# checking the connectivities
self.assertTrue(cond.Id in cond_geoms)
nodes_id_list = sorted([node.Id for node in cond.GetNodes()])
self.assertListEqual(sorted(cond_geoms[cond.Id]), nodes_id_list)
self.assertEqual(cond.Properties.Id, props_id_conditions)
def CheckMainModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id_elements))
self.assertTrue(model_part_to_check.HasProperties(props_id_conditions))
self.assertEqual(len(the_nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in the_nodes)
orig_node_coords = the_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
CheckElementsModelPart(model_part_to_check.GetSubModelPart(smp_name_elems))
CheckConditionsModelPart(model_part_to_check.GetSubModelPart(smp_name_conds))
CheckMainModelPart(model_part)
CheckElementsModelPart(model_part.GetSubModelPart(smp_name_elems))
CheckConditionsModelPart(model_part.GetSubModelPart(smp_name_conds))
def test_add_elements_and_conditions_on_same_geometry(self):
model_part = self._CreateModelPart()
smp_name = "smp_elemes_conds"
condition_name = "SurfaceCondition3D3N"
props_id_conditions = 12
geometry_name_2D = "Triangle"
element_name = "Element2D3N"
props_id_elements = 77
mesh_description = { "elements" : {geometry_name_2D : {element_name : props_id_elements}},
"conditions" : {geometry_name_2D : {condition_name : props_id_conditions}}}
the_nodes = {i+1 : [i+1,i*2,i+3.5] for i in range(15)}
geometries = {geometry_name_2D : {i+1 : [(i+1)%15+1, (i+3)%15+1, (i+4)%15+1] for i in range(25)}}
attrs = { 'GetNodesAndGeometricalEntities.return_value': (the_nodes, geometries) }
mesh_interface_mock = MagicMock(spec=MeshInterface)
mesh_interface_mock.configure_mock(**attrs)
meshes = [geometries_io.Mesh(mesh_interface_mock, mesh_description, smp_name)]
geometries_io.GeometriesIO.AddMeshes(model_part, meshes)
def CheckModelPart(model_part_to_check):
self.assertTrue(model_part_to_check.HasProperties(props_id_elements))
self.assertTrue(model_part_to_check.HasProperties(props_id_conditions))
self.assertEqual(len(the_nodes), model_part_to_check.NumberOfNodes())
for node in model_part_to_check.Nodes:
self.assertTrue(node.Id in the_nodes)
orig_node_coords = the_nodes[node.Id]
self.assertAlmostEqual(orig_node_coords[0], node.X)
self.assertAlmostEqual(orig_node_coords[1], node.Y)
self.assertAlmostEqual(orig_node_coords[2], node.Z)
elem_geoms = geometries[geometry_name_2D]
self.assertEqual(len(elem_geoms), model_part_to_check.NumberOfElements())
for elem in model_part_to_check.Elements:
# checking the connectivities
self.assertTrue(elem.Id in elem_geoms)
nodes_id_list = sorted([node.Id for node in elem.GetNodes()])
self.assertListEqual(sorted(elem_geoms[elem.Id]), nodes_id_list)
self.assertEqual(elem.Properties.Id, props_id_elements)
cond_geoms = geometries[geometry_name_2D]
self.assertEqual(len(cond_geoms), model_part_to_check.NumberOfConditions())
for cond in model_part_to_check.Conditions:
# checking the connectivities
self.assertTrue(cond.Id in cond_geoms)
nodes_id_list = sorted([node.Id for node in cond.GetNodes()])
self.assertListEqual(sorted(cond_geoms[cond.Id]), nodes_id_list)
self.assertEqual(cond.Properties.Id, props_id_conditions)
self.__RecursiveCheckModelParts(model_part, smp_name, CheckModelPart)