Salome HOME
Check result of Compute() in test
[modules/smesh.git] / test / basic_smesh_output_with_mc_field.py
1 #!/usr/bin/env python
2
3 import sys
4 import salome
5
6 import unittest
7
8 class SMESHExportOfFieldsInMemory(unittest.TestCase):
9
10   def testMEDCouplingFieldOnCells(self):
11     """
12     Test focuses on ExportMEDCoupling method in the context of MED_CELL field output.
13     """
14     salome.standalone()
15     salome.salome_init()
16     ###
17     ### SHAPER component
18     ###
19
20     from salome.shaper import model
21
22     model.begin()
23     partSet = model.moduleDocument()
24
25     ### Create Part
26     Part_1 = model.addPart(partSet)
27     Part_1_doc = Part_1.document()
28
29     ### Create Box
30     Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
31
32     ### Create Plane
33     Plane_4 = model.addPlane(Part_1_doc, model.selection("FACE", "PartSet/XOY"), 5, False)
34
35     ### Create Plane
36     Plane_5 = model.addPlane(Part_1_doc, model.selection("FACE", "PartSet/YOZ"), 5, False)
37
38     ### Create Partition
39     Partition_1_objects = [model.selection("FACE", "Plane_1"),
40                           model.selection("FACE", "Plane_2"),
41                           model.selection("SOLID", "Box_1_1")]
42     Partition_1 = model.addPartition(Part_1_doc, Partition_1_objects, keepSubResults = True)
43
44     ### Create Field
45     Field_1_objects = [model.selection("SOLID", "Partition_1_1_2"),
46                       model.selection("SOLID", "Partition_1_1_4"),
47                       model.selection("SOLID", "Partition_1_1_1"),
48                       model.selection("SOLID", "Partition_1_1_3")]
49     Field_1 = model.addField(Part_1_doc, 1, "DOUBLE", 1, ["Comp 1"], Field_1_objects)
50     Field_1.addStep(0, 0, [[0], [1], [2], [3], [4]])
51
52
53     model.end()
54
55     ###
56     ### SHAPERSTUDY component
57     ###
58
59     model.publishToShaperStudy()
60     import SHAPERSTUDY
61     Partition_1_1, Field_1_1 = SHAPERSTUDY.shape(model.featureStringId(Partition_1))
62     ###
63     ### SMESH component
64     ###
65
66     import  SMESH, SALOMEDS
67     from salome.smesh import smeshBuilder
68
69     smesh = smeshBuilder.New()
70     #smesh.SetEnablePublish( False ) # Set to False to avoid publish in study if not needed or in some particular situations:
71                                     # multiples meshes built in parallel, complex and numerous mesh edition (performance)
72
73     Mesh_1 = smesh.Mesh(Partition_1_1)
74     Regular_1D = Mesh_1.Segment()
75     Local_Length_1 = Regular_1D.LocalLength(5,None,1e-07)
76     Quadrangle_2D = Mesh_1.Quadrangle(algo=smeshBuilder.QUADRANGLE)
77     Hexa_3D = Mesh_1.Hexahedron(algo=smeshBuilder.Hexa)
78     isDone = Mesh_1.Compute()
79     if not isDone:
80       raise Exception("Error when computing Mesh")
81     smesh.SetName(Mesh_1, 'Mesh_1')
82
83     #### Mesh_1.ExportMED( r'Mesh_with_one_field_on_cells.med', 0, 41, 1, Mesh_1.GetMesh(), 1, [ Field_1_1 ], '',-1 )
84     mfd = Mesh_1.ExportMEDCoupling(0, Mesh_1.GetMesh(), 1, [ Field_1_1 ], '',-1 )#### <- important line of test is here !
85
86     self.assertEqual(len(mfd.getMeshes()),1)
87     self.assertEqual(len(mfd.getFields()),1)
88     f = mfd.getFields()[0][0].field(mfd.getMeshes()[0])
89     f.checkConsistencyLight()
90     import medcoupling
91     self.assertEqual(f.getDiscretization().getEnum(),medcoupling.ON_CELLS)
92     self.assertTrue(f.getMesh().getNumberOfCells()>1)
93     pass
94
95
96   def testMEDCouplingFieldOnNodes(self):
97     """
98     Test focuses on ExportMEDCoupling method in the context of MED_NODES field output.
99     """
100     salome.standalone()
101     salome.salome_init()
102
103     ###
104     ### SHAPER component
105     ###
106
107     from salome.shaper import model
108
109     model.begin()
110     partSet = model.moduleDocument()
111
112     ### Create Part
113     Part_1 = model.addPart(partSet)
114     Part_1_doc = Part_1.document()
115
116     ### Create Box
117     Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
118
119     ### Create Plane
120     Plane_4 = model.addPlane(Part_1_doc, model.selection("FACE", "PartSet/XOY"), 5, False)
121
122     ### Create Plane
123     Plane_5 = model.addPlane(Part_1_doc, model.selection("FACE", "PartSet/YOZ"), 5, False)
124
125     ### Create Partition
126     Partition_1_objects = [model.selection("FACE", "Plane_1"),
127                           model.selection("FACE", "Plane_2"),
128                           model.selection("SOLID", "Box_1_1")]
129     Partition_1 = model.addPartition(Part_1_doc, Partition_1_objects, keepSubResults = True)
130
131     ### Create Field
132     Field_2_objects = [model.selection("VERTEX", "[Partition_1_1_2/Modified_Face&Box_1_1/Back][Partition_1_1_2/Modified_Face&Box_1_1/Left][Partition_1_1_2/Modified_Face&Box_1_1/Top]"),
133                       model.selection("VERTEX", "Partition_1_1_2/Generated_Vertex&Plane_2/Plane_2&new_weak_name_1"),
134                       model.selection("VERTEX", "[Partition_1_1_4/Modified_Face&Box_1_1/Front][Partition_1_1_4/Modified_Face&Box_1_1/Left][Partition_1_1_4/Modified_Face&Box_1_1/Top]"),
135                       model.selection("VERTEX", "Partition_1_1_1/Generated_Vertex&Plane_1/Plane_1&new_weak_name_1"),
136                       model.selection("VERTEX", "[Partition_1_1_1/Modified_Face&Box_1_1/Left][Partition_1_1_1/Modified_Face&Plane_1/Plane_1][Partition_1_1_1/Modified_Face&Plane_2/Plane_2]"),
137                       model.selection("VERTEX", "Partition_1_1_3/Generated_Vertex&Plane_1/Plane_1&new_weak_name_1"),
138                       model.selection("VERTEX", "[Partition_1_1_1/Modified_Face&Box_1_1/Back][Partition_1_1_1/Modified_Face&Box_1_1/Left][Partition_1_1_1/Modified_Face&Box_1_1/Bottom]"),
139                       model.selection("VERTEX", "Partition_1_1_1/Generated_Vertex&Plane_2/Plane_2&new_weak_name_1"),
140                       model.selection("VERTEX", "[Partition_1_1_3/Modified_Face&Box_1_1/Left][Partition_1_1_3/Modified_Face&Box_1_1/Bottom][Partition_1_1_3/Modified_Face&Box_1_1/Front]")]
141     Field_2 = model.addField(Part_1_doc, 1, "DOUBLE", 1, ["Comp 1"], Field_2_objects)
142     Field_2.addStep(0, 0, [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]])
143
144     model.end()
145
146     ###
147     ### SHAPERSTUDY component
148     ###
149
150     model.publishToShaperStudy()
151     import SHAPERSTUDY
152     Partition_1_1, Field_2_1 = SHAPERSTUDY.shape(model.featureStringId(Partition_1))
153     ###
154     ### SMESH component
155     ###
156
157     import  SMESH, SALOMEDS
158     from salome.smesh import smeshBuilder
159
160     smesh = smeshBuilder.New()
161     #smesh.SetEnablePublish( False ) # Set to False to avoid publish in study if not needed or in some particular situations:
162                                     # multiples meshes built in parallel, complex and numerous mesh edition (performance)
163
164     Mesh_1 = smesh.Mesh(Partition_1_1)
165     Regular_1D = Mesh_1.Segment()
166     Local_Length_1 = Regular_1D.LocalLength(5,None,1e-07)
167     Quadrangle_2D = Mesh_1.Quadrangle(algo=smeshBuilder.QUADRANGLE)
168     Hexa_3D = Mesh_1.Hexahedron(algo=smeshBuilder.Hexa)
169     isDone = Mesh_1.Compute()
170     if not isDone:
171       raise Exception("Error when computing Mesh")
172     smesh.SetName(Mesh_1, 'Mesh_1')
173
174     # 23th of june 2021 : Bug both in ExportMED and in ExportMEDCoupling
175     #Mesh_1.ExportMED( r'/tmp/Mesh_with_one_field_on_nodes.med', 0, 41, 1, Mesh_1.GetMesh(), 1, [ Field_2_1 ], '',-1 )
176     #mfd = Mesh_1.ExportMEDCoupling(0,Mesh_1.GetMesh(), 1, [ Field_2_1 ], '',-1)
177
178 if __name__ == '__main__':
179     unittest.main()