6 import medcoupling as mc
8 print("Loading a triangular mesh of a 2D square")
9 filename = "./meshSquare"
10 M=cdmath.Mesh(filename+".med", "Mesh_1", 0)
12 #Extract groups in the mesh
13 print("Checking boundary group names")
14 boundaryFaceGroupNames=M.getNameOfFaceGroups()
15 boundaryNodeGroupNames=M.getNameOfNodeGroups()
16 print(len(boundaryFaceGroupNames), " Boundary Face Group detected : ", boundaryFaceGroupNames)
17 print(len(boundaryNodeGroupNames), " Boundary Node Group detected : ", boundaryNodeGroupNames)
19 assert(len(boundaryFaceGroupNames)==5)
20 assert(len(boundaryNodeGroupNames)==5)
22 assert boundaryFaceGroupNames[4]=="Top"
23 assert boundaryFaceGroupNames[3]=="Right"
24 assert boundaryFaceGroupNames[2]=="Left"
25 assert boundaryFaceGroupNames[1]=="Bottom"
26 assert boundaryFaceGroupNames[0]=="Boundary"
28 assert boundaryNodeGroupNames[4]=="Top"
29 assert boundaryNodeGroupNames[3]=="Right"
30 assert boundaryNodeGroupNames[2]=="Left"
31 assert boundaryNodeGroupNames[1]=="Bottom"
32 assert boundaryNodeGroupNames[0]=="Boundary"
40 radius = min(xmax-xmin,ymax-ymin)/4
41 xcentre = (xmax+xmin)/2
42 ycentre = (ymax+ymin)/2
44 nbCells = M.getNumberOfCells()
45 nbNodes = M.getNumberOfNodes()
47 # Create solid temperature fields
48 temperature_field_cells = cdmath.Field("Solid temperature", cdmath.CELLS, M, 1)
49 temperature_field_nodes = cdmath.Field("Solid temperature", cdmath.NODES, M, 1)
53 for i in range(nbCells):
56 distance = sqrt( (x - xcentre) * (x - xcentre) + (y - ycentre) * (y - ycentre) )
58 temperature_field_cells[i] = Tin
60 temperature_field_cells[i] = Tout
62 temperature_field_cells.writeMED(filename, False)
64 for i in range(nbNodes):
67 distance = sqrt( (x - xcentre) * (x - xcentre) + (y - ycentre) * (y - ycentre) )
69 temperature_field_nodes[i] = Tin
71 temperature_field_nodes[i] = Tout
73 temperature_field_nodes.writeMED(filename, False)
75 # Create boundary fields
77 Mbottom=M.getBoundaryGroupMesh ( "Bottom" )
78 Mbottom.writeMED(filename, False)
79 Mbottom.writeVTK(filename)
80 temperature_bottom_cells=cdmath.Field("Bottom temperature",cdmath.CELLS,Mbottom)
81 for i in range(temperature_bottom_cells.getNumberOfElements()):
82 temperature_bottom_cells[i]=Tout
83 temperature_bottom_cells.writeMED(filename,False)
84 temperature_bottom_cells.writeVTK(filename,True)
85 temperature_bottom_cells.writeCSV(filename)
86 temperature_bottom_nodes=cdmath.Field("Bottom temperature",cdmath.NODES,Mbottom)
87 for i in range(temperature_bottom_nodes.getNumberOfElements()):
88 temperature_bottom_nodes[i]=Tout
89 temperature_bottom_nodes.writeMED(filename,False)
90 temperature_bottom_nodes.writeVTK(filename,True)
91 temperature_bottom_nodes.writeCSV(filename)
93 Mtop=M.getBoundaryGroupMesh ( "Top" )
94 Mtop.writeMED(filename, False)
95 Mtop.writeVTK(filename)
96 temperature_top_cells=cdmath.Field("Top temperature",cdmath.CELLS,Mtop)
97 for i in range(temperature_top_cells.getNumberOfElements()):
98 temperature_top_cells[i]=Tout
99 temperature_top_cells.writeMED(filename,False)
100 temperature_top_cells.writeVTK(filename,True)
101 temperature_top_cells.writeCSV(filename)
102 temperature_top_nodes=cdmath.Field("Top temperature",cdmath.NODES,Mtop)
103 for i in range(temperature_top_nodes.getNumberOfElements()):
104 temperature_top_nodes[i]=Tout
105 temperature_top_nodes.writeMED(filename,False)
106 temperature_top_nodes.writeVTK(filename,True)
107 temperature_top_nodes.writeCSV(filename)
109 Mleft=M.getBoundaryGroupMesh ( "Left" )
110 Mleft.writeMED(filename, False)
111 Mleft.writeVTK(filename)
112 temperature_left_cells=cdmath.Field("Left temperature",cdmath.CELLS,Mleft)
113 for i in range(temperature_left_cells.getNumberOfElements()):
114 temperature_left_cells[i]=Tout
115 temperature_left_cells.writeMED(filename,False)
116 temperature_left_cells.writeVTK(filename,True)
117 temperature_left_cells.writeCSV(filename)
118 temperature_left_nodes=cdmath.Field("Left temperature",cdmath.NODES,Mleft)
119 for i in range(temperature_left_nodes.getNumberOfElements()):
120 temperature_left_nodes[i]=Tout
121 temperature_left_nodes.writeMED(filename,False)
122 temperature_left_nodes.writeVTK(filename,True)
123 temperature_left_nodes.writeCSV(filename)
125 Mright=M.getBoundaryGroupMesh ( "Right" )
126 Mright.writeMED(filename, False)
127 Mright.writeVTK(filename)
128 temperature_right_cells=cdmath.Field("Right temperature",cdmath.CELLS,Mright)
129 for i in range(temperature_right_cells.getNumberOfElements()):
130 temperature_right_cells[i]=Tout
131 temperature_right_cells.writeMED(filename,False)
132 temperature_right_cells.writeVTK(filename,True)
133 temperature_right_cells.writeCSV(filename)
134 temperature_right_nodes=cdmath.Field("Right temperature",cdmath.NODES,Mright)
135 for i in range(temperature_right_nodes.getNumberOfElements()):
136 temperature_right_nodes[i]=Tout
137 temperature_right_nodes.writeMED(filename,False)
138 temperature_right_nodes.writeVTK(filename,True)
139 temperature_right_nodes.writeCSV(filename)
141 # Create fluid temperature fields
142 temperature_field_cells = cdmath.Field("Fluid temperature", cdmath.CELLS, M, 1)
143 temperature_field_nodes = cdmath.Field("Fluid temperature", cdmath.NODES, M, 1)
146 for i in range(nbCells):
149 temperature_field_cells[i] = Tfluid
151 temperature_field_cells.writeMED(filename, False)
153 for i in range(nbNodes):
156 temperature_field_nodes[i] = Tfluid
158 temperature_field_nodes.writeMED(filename, False)
160 # Create fluid enthalpy fields
161 enthalpy_field_cells = cdmath.Field("Fluid enthalpy", cdmath.CELLS, M, 1)
162 enthalpy_field_nodes = cdmath.Field("Fluid enthalpy", cdmath.NODES, M, 1)
166 for i in range(nbCells):
169 distance = sqrt( (x - xcentre) * (x - xcentre) + (y - ycentre) * (y - ycentre) )
170 if distance < radius:
171 enthalpy_field_cells[i] = Hin
173 enthalpy_field_cells[i] = Hout
175 enthalpy_field_cells.writeMED(filename, False)
177 for i in range(nbNodes):
180 distance = sqrt( (x - xcentre) * (x - xcentre) + (y - ycentre) * (y - ycentre) )
181 if distance < radius:
182 enthalpy_field_nodes[i] = Hin
184 enthalpy_field_nodes[i] = Hout
186 enthalpy_field_nodes.writeMED(filename, False)
188 # Create heat power fields
189 heat_field_cells = cdmath.Field("Heat power", cdmath.CELLS, M, 1)
190 heat_field_nodes = cdmath.Field("Heat power", cdmath.NODES, M, 1)
193 for i in range(nbCells):
196 heat_field_cells[i] = phi
198 heat_field_cells.writeMED(filename, False)
200 for i in range(nbNodes):
203 heat_field_nodes[i] = phi
205 heat_field_nodes.writeMED(filename, False)
207 # Create pressure and velocity fields for the wave equation on CELLS
208 p0=155e5#reference pressure in a pressurised nuclear vessel
210 pressure_field = cdmath.Field("Pressure", cdmath.CELLS, M, 1)
211 velocity_field = cdmath.Field("Velocity", cdmath.CELLS, M, 3)
213 for i in range(nbCells):
216 distance = sqrt( (x - xcentre) * (x - xcentre) + (y - ycentre) * (y - ycentre) )
218 velocity_field[i,0] = 0
219 velocity_field[i,1] = 0
220 velocity_field[i,2] = 0
222 if distance < radius:
223 pressure_field[i] = p0
226 pressure_field[i] = p0/2
230 pressure_field.writeMED(filename, False)
231 velocity_field.writeMED(filename, False)
233 #### Delete mesh and still save field in a med file
234 m = mc.MEDCouplingCMesh()
235 x = mc.DataArrayDouble([0.,1.,2.])
236 y = mc.DataArrayDouble([0.,1.,2.])
238 m = m.buildUnstructured()
241 f = mc.MEDCouplingFieldDouble(mc.ON_CELLS, mc.ONE_TIME)
245 da = mc.DataArrayDouble([1,2,3,4])
251 fName = "/tmp/michael.med"
252 mc.WriteUMesh(fName, m, True)
255 # Maintenant juste les champs:
256 ff = mc.MEDFileField1TS()
257 ff.setFieldNoProfileSBT(f)
264 gc.collect() # Make sure Python interp has called mesh destructor ...
266 # Ecrit encore du champ:
270 print(da2.getValues())
273 ff = mc.MEDFileField1TS()
274 ff.setFieldNoProfileSBT(f) # le maillage n'existe plus, tant pis :-)
275 ff.write(fName, 0) # yes 0