1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 from MEDCoupling import *
23 from math import pi, sqrt
25 class MEDCouplingBasicsTest(unittest.TestCase):
26 def testExample_MEDCouplingUMesh_(self):
27 #! [PySnippet_MEDCouplingUMesh__1]
30 def testExample_MEDCouplingMesh_fillFromAnalytic3(self):
31 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_1]
32 coords = [0.,2.,4.,6.] # 6. is not used
33 x=DataArrayDouble.New(coords[:3],3,1)
34 y=DataArrayDouble.New(coords[:2],2,1)
35 mesh=MEDCouplingCMesh.New()
37 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_1]
38 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_2]
39 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
40 varNames=["a","b"] # names used to refer to X and Y coord components
41 field=mesh.fillFromAnalytic3(ON_CELLS,3,varNames,func)
42 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_2]
43 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_3]
44 vals1 = field.getArray().getTuple(1) # values of the cell #1
45 assert len( vals1 ) == 3 # 3 components in the field
47 bc = mesh.getBarycenterAndOwner() # func is applied to barycenters of cells
48 bc1 = bc.getTuple(1) # coordinates of the second point
50 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
51 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
52 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
53 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
54 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_3]
57 def testExample_MEDCouplingMesh_fillFromAnalytic2(self):
58 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_1]
59 coords = [0.,2.,4.,6.] # 6. is not used
60 x=DataArrayDouble.New(coords[:3],3,1)
61 y=DataArrayDouble.New(coords[:2],2,1)
62 x.setInfoOnComponent(0,"a") # name used to refer to X coordinate within a function
63 y.setInfoOnComponent(0,"b") # name used to refer to Y coordinate within a function
64 mesh=MEDCouplingCMesh.New()
66 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_1]
67 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_2]
68 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
69 field=mesh.fillFromAnalytic2(ON_CELLS,3,func)
70 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_2]
71 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_3]
72 vals1 = field.getArray().getTuple(1) # values of the cell #1
73 assert len( vals1 ) == 3 # 3 components in the field
75 bc = mesh.getBarycenterAndOwner() # func is applied to barycenters of cells
76 bc1 = bc.getTuple(1) # coordinates of the second point
78 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
79 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
80 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
81 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
82 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_3]
85 def testExample_MEDCouplingMesh_fillFromAnalytic(self):
86 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_1]
87 coords = [0.,2.,4.,6.] # 6. is not used
88 x=DataArrayDouble.New(coords[:3],3,1)
89 y=DataArrayDouble.New(coords[:2],2,1)
90 mesh=MEDCouplingCMesh.New()
92 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_1]
93 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_2]
94 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
95 field=mesh.fillFromAnalytic(ON_CELLS,3,func)
96 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_2]
97 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_3]
98 vals1 = field.getArray().getTuple(1) # values of the cell #1
99 assert len( vals1 ) == 3 # 3 components in the field
101 bc = mesh.getBarycenterAndOwner() # func is applied to barycenters of cells
102 bc1 = bc.getTuple(1) # coordinates of the second point
104 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
105 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
106 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
107 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
108 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_3]
111 def testExample_MEDCouplingCMesh_getCoordsAt(self):
112 #! [PySnippet_MEDCouplingCMesh_getCoordsAt_1]
114 x=DataArrayDouble.New(coords,3,1)
115 mesh=MEDCouplingCMesh.New()
116 mesh.setCoordsAt(0,x)
117 x2=mesh.getCoordsAt(0)
118 assert coords == x2.getValues()
119 #! [PySnippet_MEDCouplingCMesh_getCoordsAt_1]
122 def testExample_MEDCouplingUMesh_areCellsIncludedIn(self):
123 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_1]
124 mesh1=MEDCouplingUMesh.New();
125 mesh1.setMeshDimension(2);
126 mesh1.allocateCells(5);
127 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4];
128 mesh1.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
129 mesh1.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
130 mesh1.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
131 mesh1.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
132 mesh1.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
133 mesh1.finishInsertingCells();
134 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
135 coordsArr=DataArrayDouble.New();
136 coordsArr.setValues(coords,9,2);
137 mesh1.setCoords(coordsArr);
138 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_1]
139 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_2]
141 mesh2 = mesh1.buildPartOfMySelf(cells2, True ) # even cells selected
142 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_2]
143 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_3]
144 compType = 0 # the strongest policy
145 isOk, corr2to1 = mesh1.areCellsIncludedIn( mesh2, compType )
146 assert isOk # a larger mesh1 includes a smaller mesh2
147 assert corr2to1.getValues() == cells2
148 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_3]
149 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_4]
150 isOk, corr1to2 = mesh2.areCellsIncludedIn( mesh1, compType )
151 assert not isOk # the smaller mesh2 does NOT include the larger mesh1
152 assert corr1to2.getValues() == [2, 3, 1, 4, 0]
153 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_4]
155 def testExample_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells(self):
156 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_1]
157 # 2D coordinates of 5 base nodes
158 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2];
159 coordsArr=DataArrayDouble.New();
160 coordsArr.setValues(coords,5,2);
161 # coordinates of 5 top nodes
162 coordsArr2 = coordsArr.deepCpy()
163 # 3D coordinates of base + top nodes
164 coordsArr = coordsArr.changeNbOfComponents( 3, 0 )
165 coordsArr2 = coordsArr2.changeNbOfComponents( 3, 1 )
166 coordsArr = DataArrayDouble.Aggregate([coordsArr,coordsArr2])
168 mesh=MEDCouplingUMesh.New();
169 mesh.setCoords(coordsArr);
170 mesh.setMeshDimension(3);
171 mesh.allocateCells(2);
172 # connectivity of reversed HEXA8 and PENTA6
173 conn=[0,1,4,3, 5,6,9,8, 1,2,4, 6,7,9]
174 mesh.insertNextCell(NORM_HEXA8, 8,conn[0:0+8])
175 mesh.insertNextCell(NORM_PENTA6,6,conn[8:8+6])
176 mesh.finishInsertingCells();
177 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_1]
178 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_2]
179 fixedCells = mesh.findAndCorrectBadOriented3DExtrudedCells()
180 assert len( fixedCells.getValues() ) == 2 # 2 cells fixed
181 fixedCells = mesh.findAndCorrectBadOriented3DExtrudedCells()
182 assert len( fixedCells.getValues() ) == 0 # no bad cells
183 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_2]
186 def testExample_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented(self):
187 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_1]
188 # 2D coordinates of 5 base nodes
189 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2];
190 coordsArr=DataArrayDouble.New();
191 coordsArr.setValues(coords,5,2);
192 # coordinates of 5 top nodes
193 coordsArr2 = coordsArr.deepCpy()
194 # 3D coordinates of base + top nodes
195 coordsArr = coordsArr.changeNbOfComponents( 3, 0 )
196 coordsArr2 = coordsArr2.changeNbOfComponents( 3, 1 )
197 coordsArr = DataArrayDouble.Aggregate([coordsArr,coordsArr2])
199 mesh=MEDCouplingUMesh.New();
200 mesh.setCoords(coordsArr);
201 mesh.setMeshDimension(3);
202 mesh.allocateCells(2);
203 # connectivity of a HEXA8 + a reversed PENTA6
204 conn=[0,3,4,1, 5,8,9,6, 1,2,4, 6,7,9]
205 mesh.insertNextCell(NORM_POLYHED, 8,conn[0:0+8]) # "extruded" polyhedron
206 mesh.insertNextCell(NORM_POLYHED,6,conn[8:8+6])
207 mesh.finishInsertingCells();
208 # fix connectivity of NORM_POLYHED's
209 mesh.convertExtrudedPolyhedra()
210 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_1]
211 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_2]
212 badCells = mesh.arePolyhedronsNotCorrectlyOriented()
213 assert len( badCells.getValues() ) == 1 # one polyhedron is KO
214 # fix invalid rolyherdons
215 mesh.orientCorrectlyPolyhedrons()
216 # re-check the orientation
217 badCells = mesh.arePolyhedronsNotCorrectlyOriented()
218 assert len( badCells.getValues() ) == 0 # connectivity is OK
219 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_2]
222 def testExample_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented(self):
223 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_1]
224 mesh=MEDCouplingUMesh.New();
225 mesh.setMeshDimension(2);
226 mesh.allocateCells(5);
227 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4];
228 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
229 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
230 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
231 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
232 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
233 mesh.finishInsertingCells();
234 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
235 coordsArr=DataArrayDouble.New();
236 coordsArr.setValues(coords,9,2);
237 mesh.setCoords(coordsArr);
238 mesh.changeSpaceDimension(3)
239 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_1]
240 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_2]
242 badCellIds=mesh.are2DCellsNotCorrectlyOriented( vec, False )
243 assert len( badCellIds ) == 1 # one cell is reversed
245 mesh.orientCorrectly2DCells( vec, False )
246 # re-check orientation
247 badCellIds=mesh.are2DCellsNotCorrectlyOriented( vec, False )
248 assert len( badCellIds ) == 0 # the orientation is OK
249 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_2]
252 def testExample_MEDCouplingUMesh_getCellsContainingPoints(self):
253 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_1]
254 mesh=MEDCouplingUMesh.New();
255 mesh.setMeshDimension(2);
256 mesh.allocateCells(5);
257 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4];
258 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
259 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
260 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
261 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
262 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
263 mesh.finishInsertingCells();
264 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
265 coordsArr=DataArrayDouble.New();
266 coordsArr.setValues(coords,9,2);
267 mesh.setCoords(coordsArr);
268 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_1]
269 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_2]
270 pos = [ 10., 10, # point out of the mesh
271 0.3, 0.3, # point located somewhere inside the mesh
272 coords[2], coords[3]] # point at the node #1
273 eps = 1e-4 # ball radius
274 cells,cellsIndex=mesh.getCellsContainingPoints( pos, 3, eps )
275 assert cells.getValues() == [4, 0, 1]
276 assert cellsIndex.getValues() == [0, 0, 1, 3]
277 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_2]
281 def testExample_MEDCouplingUMesh_getCellsContainingPoint(self):
282 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_1]
283 mesh=MEDCouplingUMesh.New();
284 mesh.setMeshDimension(2);
285 mesh.allocateCells(5);
286 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4];
287 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
288 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
289 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
290 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
291 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
292 mesh.finishInsertingCells();
293 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
294 coordsArr=DataArrayDouble.New();
295 coordsArr.setValues(coords,9,2);
296 mesh.setCoords(coordsArr);
297 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_1]
298 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_2]
299 pos4 = coords[ 4*2 : ] # coordinates of the node #4
300 eps = 1e-4 # ball radius
301 pos = [ pos4[0]+eps, pos4[1]-eps ] # ball center
302 cellIds=mesh.getCellsContainingPoint( pos, eps )
303 assert len( cellIds ) == mesh.getNumberOfCells()
304 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_2]
308 def testExample_MEDCouplingUMesh_buildPartOrthogonalField(self):
309 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_1]
310 mesh=MEDCouplingUMesh.New();
311 mesh.setMeshDimension(2);
312 mesh.allocateCells(5);
313 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4];
314 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
315 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
316 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
317 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
318 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
319 mesh.finishInsertingCells();
320 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
321 coordsArr=DataArrayDouble.New();
322 coordsArr.setValues(coords,9,2);
323 mesh.setCoords(coordsArr);
324 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_1]
325 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_2]
326 part = DataArrayInt([1,2,3,4],4,1) # cell #0 is omitted
327 vecField=mesh.buildPartOrthogonalField( part )
328 vecArr = vecField.getArray()
329 assert len( vecArr ) == len( part )
330 assert vecArr.getNumberOfComponents() == 3
331 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_2]
334 def testExample_MEDCouplingUMesh_getPartMeasureField(self):
335 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_1]
336 mesh=MEDCouplingUMesh.New();
337 mesh.setMeshDimension(2);
338 mesh.allocateCells(5);
339 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4];
340 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
341 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
342 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
343 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
344 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
345 mesh.finishInsertingCells();
346 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
347 coordsArr=DataArrayDouble.New();
348 coordsArr.setValues(coords,9,2);
349 mesh.setCoords(coordsArr);
350 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_1]
351 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_2]
353 part = DataArrayInt([1,2,3,4],4,1) # cell #0 is omitted
354 areaArr=mesh.getPartMeasureField( isAbs, part )
355 assert areaArr[0] > 0 # orientation ignored
356 areaArr=mesh.getPartMeasureField( not isAbs, part )
357 assert areaArr[0] < 0 # orientation considered
358 assert len( areaArr ) == len( part )
359 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_2]
360 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_3]
361 part = DataArrayInt([1,2,3,4],4,1) # cell #0 is omitted
362 baryCenters = mesh.getPartBarycenterAndOwner( part )
363 assert len( baryCenters ) == len( part )
364 assert baryCenters.getNumberOfComponents() == mesh.getSpaceDimension()
365 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_3]
368 def testExample_MEDCouplingUMesh_getCellsInBoundingBox(self):
369 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_1]
370 mesh=MEDCouplingUMesh.New();
371 mesh.setMeshDimension(2);
372 coords=[0.,0., 0.,1., 1.,1];
373 coordsArr=DataArrayDouble.New();
374 coordsArr.setValues(coords,3,2);
375 mesh.setCoords(coordsArr);
376 mesh.allocateCells(1);
378 mesh.insertNextCell(NORM_TRI3,3,conn);
379 mesh.finishInsertingCells();
380 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_1]
381 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_2]
382 bbox = [1., 1., 1.001,1.001] # xMin, xMax, yMin, yMax
383 cellsInBox = mesh.getCellsInBoundingBox( bbox, 0.0 )
384 assert cellsInBox.getValues() == []
385 cellsInBox = mesh.getCellsInBoundingBox( bbox, 0.1 )
386 assert cellsInBox.getValues() == [0]
387 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_2]
390 def testExample_MEDCouplingUMesh_renumberNodesInConn(self):
391 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_1]
392 mesh=MEDCouplingUMesh.New();
393 mesh.setMeshDimension(2);
394 mesh.allocateCells(1);
396 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]);
397 mesh.finishInsertingCells();
398 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_1]
399 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_2]
400 old2newIds = [-1,3,2,1,0]
401 mesh.renumberNodesInConn( old2newIds )
402 nodes0 = mesh.getNodeIdsOfCell( 0 )
403 assert nodes0 == [0,1,2,3]
404 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_2]
408 def testExample_MEDCouplingUMesh_renumberNodes(self):
409 #! [PySnippet_MEDCouplingUMesh_renumberNodes_1]
410 mesh=MEDCouplingUMesh.New();
411 mesh.setMeshDimension(2);
412 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.3];
413 coordsArr=DataArrayDouble.New();
414 coordsArr.setValues(coords,4,2);
415 mesh.setCoords(coordsArr);
416 mesh.allocateCells(0);
417 mesh.finishInsertingCells();
418 #! [PySnippet_MEDCouplingUMesh_renumberNodes_1]
419 #! [PySnippet_MEDCouplingUMesh_renumberNodes_2]
420 mesh.renumberNodes([ 2,1,0,-1 ], 3);
421 coordsArr = mesh.getCoords() # get a shorten array
422 assert coordsArr.getValues() == [0.7,-0.3, 0.2,-0.3, -0.3,-0.3]
423 #! [PySnippet_MEDCouplingUMesh_renumberNodes_2]
424 #! [PySnippet_MEDCouplingUMesh_renumberNodes_3]
425 coordsArr.setValues(coords,4,2); # restore old nodes
426 mesh.renumberNodes2([ 2,1,0,2 ], 3);
427 coordsArr = mesh.getCoords() # get a shorten array
428 assert coordsArr.getValues() == [0.7,-0.3, 0.2,-0.3, -0.3,0.0]
429 #! [PySnippet_MEDCouplingUMesh_renumberNodes_3]
432 def testExample_MEDCouplingUMesh_findBoundaryNodes(self):
433 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_1]
434 mesh=MEDCouplingUMesh.New();
435 mesh.setMeshDimension(2);
436 mesh.allocateCells(5);
437 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
438 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
439 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
440 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
441 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
442 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
443 mesh.finishInsertingCells();
444 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
445 coordsArr=DataArrayDouble.New();
446 coordsArr.setValues(coords,9,2);
447 mesh.setCoords(coordsArr);
448 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_1]
449 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_2]
450 nodeIdsArr=mesh.findBoundaryNodes()
451 assert nodeIdsArr.getNumberOfTuples() == mesh.getNumberOfNodes() - 1
452 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_2]
455 def testExample_MEDCouplingUMesh_buildBoundaryMesh(self):
456 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_1]
457 mesh=MEDCouplingUMesh.New();
458 mesh.setMeshDimension(2);
459 mesh.allocateCells(5);
460 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
461 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
462 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
463 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
464 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
465 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
466 mesh.finishInsertingCells();
467 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
468 coordsArr=DataArrayDouble.New();
469 coordsArr.setValues(coords,9,2);
470 mesh.setCoords(coordsArr);
471 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_1]
472 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_2]
473 mesh1=mesh.buildBoundaryMesh(True)
474 mesh2=mesh.buildBoundaryMesh(False)
475 assert coordsArr.isEqual( mesh1.getCoords(), 1e-13 ) # same nodes
476 assert not coordsArr.isEqual( mesh2.getCoords(), 1e-13 ) # different nodes
477 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_2]
480 def testExample_MEDCouplingUMesh_buildFacePartOfMySelfNode(self):
481 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_1]
482 mesh=MEDCouplingUMesh.New();
483 mesh.setMeshDimension(2);
484 mesh.allocateCells(5);
485 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
486 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
487 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
488 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
489 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
490 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
491 mesh.finishInsertingCells();
492 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
493 coordsArr=DataArrayDouble.New();
494 coordsArr.setValues(coords,9,2);
495 mesh.setCoords(coordsArr);
496 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_1]
497 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_2]
498 nodeIds = mesh.getNodeIdsOfCell( 0 )
500 mesh1 = mesh.buildFacePartOfMySelfNode( nodeIds, allNodes )
501 assert mesh1.getNumberOfCells() == 4 # 4 segments bounding QUAD4 #0 only
502 mesh2 = mesh.buildFacePartOfMySelfNode( nodeIds, not allNodes )
503 assert mesh2.getNumberOfCells() > 4 # more segments added
504 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_2]
508 def testExample_MEDCouplingUMesh_buildPartOfMySelfNode(self):
509 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_1]
510 mesh=MEDCouplingUMesh.New();
511 mesh.setMeshDimension(2);
512 mesh.allocateCells(5);
513 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
514 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
515 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
516 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
517 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
518 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
519 mesh.finishInsertingCells();
520 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
521 coordsArr=DataArrayDouble.New();
522 coordsArr.setValues(coords,9,2);
523 mesh.setCoords(coordsArr);
524 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_1]
525 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_2]
526 nodeIds = mesh.getNodeIdsOfCell( 0 )
528 mesh1 = mesh.buildPartOfMySelfNode( nodeIds, allNodes )
529 mesh2 = mesh.buildPartOfMySelfNode( nodeIds, not allNodes )
530 assert mesh1.getNumberOfCells() == 1 # cell #0 is found only
531 assert mesh2.getNumberOfCells() == mesh.getNumberOfCells() # all cells are found
532 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_2]
536 def testExample_MEDCouplingUMesh_getCellIdsLyingOnNodes(self):
537 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_1]
538 mesh=MEDCouplingUMesh.New();
539 mesh.setMeshDimension(2);
540 mesh.allocateCells(5);
541 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
542 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
543 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
544 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
545 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
546 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
547 mesh.finishInsertingCells();
548 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
549 coordsArr=DataArrayDouble.New();
550 coordsArr.setValues(coords,9,2);
551 mesh.setCoords(coordsArr);
552 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_1]
553 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_2]
554 nodeIds = mesh.getNodeIdsOfCell( 0 )
556 cellIdsArr1 = mesh.getCellIdsLyingOnNodes( nodeIds, allNodes )
557 cellIdsArr2 = mesh.getCellIdsLyingOnNodes( nodeIds, not allNodes )
558 assert cellIdsArr1.getNumberOfTuples() == 1 # cell #0 is found only
559 assert cellIdsArr2.getNumberOfTuples() == mesh.getNumberOfCells() # all cells are found
560 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_2]
564 def testExample_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds(self):
565 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_1]
566 mesh=MEDCouplingUMesh.New();
567 mesh.setMeshDimension(2);
568 mesh.allocateCells(5);
569 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
570 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
571 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
572 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
573 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
574 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
575 mesh.finishInsertingCells();
576 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
577 coordsArr=DataArrayDouble.New();
578 coordsArr.setValues(coords,9,2);
579 mesh.setCoords(coordsArr);
580 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_1]
581 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_2]
583 nodeIds = mesh.getNodeIdsOfCell( cellIds[0] )
584 nodeIds += mesh.getNodeIdsOfCell( cellIds[1] )
585 cellIdsArr = mesh.getCellIdsFullyIncludedInNodeIds( nodeIds )
586 assert cellIdsArr.getValues() == cellIds
587 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_2]
591 def testExample_MEDCouplingUMesh_buildPartOfMySelf(self):
592 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_1]
593 mesh=MEDCouplingUMesh.New();
594 mesh.setMeshDimension(2);
595 mesh.allocateCells(5);
596 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
597 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
598 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
599 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
600 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
601 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
602 mesh.finishInsertingCells();
603 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
604 coordsArr=DataArrayDouble.New();
605 coordsArr.setValues(coords,9,2);
606 mesh.setCoords(coordsArr);
607 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_1]
608 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_2]
610 mesh2=mesh.buildPartOfMySelf(cellIds, True)
611 mesh3=mesh.buildPartOfMySelf(cellIds, False)
612 coordsArr2 = mesh2.getCoords()
613 assert coordsArr.isEqual( coordsArr2, 1e-13 ) # same nodes
614 coordsArr3 = mesh3.getCoords()
615 assert not coordsArr.isEqual( coordsArr3, 1e-13 ) # different nodes
616 assert mesh2.getNodeIdsOfCell(0) == mesh.getNodeIdsOfCell( cellIds[0]) # cell #1 was copied
617 assert mesh2.getNodeIdsOfCell(1) == mesh.getNodeIdsOfCell( cellIds[1]) # cell #2 was copied
618 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_2]
621 def testExample_MEDCouplingUMesh_mergeNodes(self):
622 #! [PySnippet_MEDCouplingUMesh_mergeNodes_1]
623 mesh=MEDCouplingUMesh.New();
624 mesh.setMeshDimension(2);
625 mesh.allocateCells(5);
626 conn=[0,3,4,1, 1,4,2, 4,5,2];
627 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]);
628 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]);
629 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]);
630 mesh.finishInsertingCells();
631 coords=[0.3,-0.301, # 0
637 coordsArr=DataArrayDouble.New();
638 coordsArr.setValues(coords,6,2);
639 mesh.setCoords(coordsArr);
640 #! [PySnippet_MEDCouplingUMesh_mergeNodes_1]
641 #! [PySnippet_MEDCouplingUMesh_mergeNodes_2]
642 arr,areNodesMerged,newNbOfNodes=mesh.mergeNodes(0.004)
643 assert arr.getValues() == [0, 1, 0, 2, 2, 0]
644 assert areNodesMerged
645 assert newNbOfNodes == 3
646 #! [PySnippet_MEDCouplingUMesh_mergeNodes_2]
647 #! [PySnippet_MEDCouplingUMesh_mergeNodes_3]
648 baryCoords2 = coords[2*2:] # initial coordinates of node #2
649 coordsArr = mesh.getCoords() # retrieve a new shorten coord array
650 self.assertNotAlmostEqual( baryCoords2[1], coordsArr.getIJ(0,1), 13 ) # Y of node #0 differs from that of baryCoords2
651 # restore coordinates
652 coordsArr = DataArrayDouble(coords,6,2);
653 mesh.setCoords(coordsArr);
655 mesh.mergeNodes2(0.004)
656 coordsArr = mesh.getCoords() # retrieve a new shorten coord array
657 self.assertAlmostEqual( baryCoords2[1], coordsArr.getIJ(0,1), 13 ) # Y of node #0 equals to that of baryCoords2
658 #! [PySnippet_MEDCouplingUMesh_mergeNodes_3]
661 def testExample_MEDCouplingUMesh_zipConnectivityTraducer(self):
662 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_1]
663 mesh=MEDCouplingUMesh.New();
664 mesh.setMeshDimension(2);
665 mesh.allocateCells(5);
666 conn=[0,3,4,1, 1,4,2];
667 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
668 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
669 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 2 == 1
670 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 3 == 0
671 mesh.insertNextCell(NORM_QUAD4,4,conn[2:4]+conn[0:2]); # 4 ~~ 0
672 mesh.finishInsertingCells();
673 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
674 coordsArr=DataArrayDouble.New();
675 coordsArr.setValues(coords,9,2);
676 mesh.setCoords(coordsArr);
677 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_1]
678 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_2]
679 oldNbCells = mesh.getNumberOfCells()
680 arr = mesh.zipConnectivityTraducer(0)
681 assert mesh.getNumberOfCells() == oldNbCells-2
682 assert arr.getValues() == [0, 1, 1, 0, 2]
683 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_2]
686 def testExample_MEDCouplingUMesh_zipCoordsTraducer(self):
687 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_1]
688 mesh=MEDCouplingUMesh.New();
689 mesh.setMeshDimension(2);
690 mesh.allocateCells(5);
691 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
692 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
693 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
694 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
695 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
696 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
697 mesh.finishInsertingCells();
698 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
699 coordsArr=DataArrayDouble.New();
700 coordsArr.setValues(coords,9,2);
701 mesh.setCoords(coordsArr);
702 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_1]
703 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_2]
705 mesh2=mesh.buildPartOfMySelf(cellIds,True);
706 arr=mesh2.zipCoordsTraducer();
707 assert mesh2.getNumberOfNodes() == 4 # nb of nodes decreased
708 assert arr.getValues() == [-1,0,1,-1,2,3,-1,-1,-1] # -1 for unused nodes
709 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_2]
712 def testExample_MEDCouplingUMesh_getNodeIdsInUse(self):
713 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_1]
714 mesh=MEDCouplingUMesh.New();
715 mesh.setMeshDimension(2);
716 mesh.allocateCells(5);
717 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
718 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
719 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
720 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
721 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
722 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
723 mesh.finishInsertingCells();
724 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
725 coordsArr=DataArrayDouble.New();
726 coordsArr.setValues(coords,9,2);
727 mesh.setCoords(coordsArr);
728 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_1]
729 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_2]
731 mesh2=mesh.buildPartOfMySelf(cellIds,True);
732 arr,newNbOfNodes=mesh2.getNodeIdsInUse();
733 assert arr.getValues() == [-1,0,1,-1,2,3,-1,-1,-1]
734 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_2]
735 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_3]
736 arr2=arr.invertArrayO2N2N2O(newNbOfNodes);
737 assert arr2.getValues() == [1,2,4,5]
738 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_3]
741 def testExample_MEDCouplingUMesh_convertToPolyTypes(self):
742 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_1]
743 mesh=MEDCouplingUMesh.New();
744 mesh.setMeshDimension(2);
745 mesh.allocateCells(5);
746 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
747 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
748 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
749 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
750 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
751 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
752 mesh.finishInsertingCells();
753 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
754 coordsArr=DataArrayDouble.New();
755 coordsArr.setValues(coords,9,2);
756 mesh.setCoords(coordsArr);
757 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_1]
758 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_2]
760 mesh.convertToPolyTypes(cells);
761 assert mesh.getTypeOfCell(0) == NORM_QUAD4
762 assert mesh.getTypeOfCell(1) == NORM_POLYGON, mesh.getTypeOfCell(1)
763 assert mesh.getTypeOfCell(2) == NORM_TRI3
764 assert mesh.getTypeOfCell(3) == NORM_POLYGON
765 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_2]
768 def testExample_MEDCouplingUMesh_buildDescendingConnectivity2(self):
769 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_1]
770 mesh=MEDCouplingUMesh.New();
771 mesh.setMeshDimension(2);
772 mesh.allocateCells(5);
773 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
774 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
775 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
776 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
777 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
778 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
779 mesh.finishInsertingCells();
780 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
781 coordsArr=DataArrayDouble.New();
782 coordsArr.setValues(coords,9,2);
783 mesh.setCoords(coordsArr);
784 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_1]
785 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_2]
786 desc=DataArrayInt.New();
787 descIndx=DataArrayInt.New();
788 revDesc=DataArrayInt.New();
789 revDescIndx=DataArrayInt.New();
790 mesh2=mesh.buildDescendingConnectivity2(desc,descIndx,revDesc,revDescIndx);
791 assert desc.getValues() == [1,2,3,4,-3,5,6, 7,8,-5,9,10,-2,11, 12,13,-7,-10]
792 assert descIndx.getValues() == [0,4,7,10,14,18]
793 assert revDesc.getValues() == [0, 0,3, 0,1, 0, 1,2, 1, 2,4, 2, 3, 3,4, 3, 4, 4]
794 assert revDescIndx.getValues() == [0,1,3,5,6,8,9,11,12,13,15,16,17,18]
795 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_2]
796 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_3]
797 assert mesh2.getNodeIdsOfCell( 3-1 ) == [4, 1] # cell #3 in FORTRAN mode
798 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_3]
801 def testExample_MEDCouplingUMesh_buildDescendingConnectivity(self):
802 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_1]
803 mesh=MEDCouplingUMesh.New();
804 mesh.setMeshDimension(2);
805 mesh.allocateCells(5);
806 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
807 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
808 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
809 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
810 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
811 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
812 mesh.finishInsertingCells();
813 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
814 coordsArr=DataArrayDouble.New();
815 coordsArr.setValues(coords,9,2);
816 mesh.setCoords(coordsArr);
817 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_1]
818 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_2]
819 desc=DataArrayInt.New();
820 descIndx=DataArrayInt.New();
821 revDesc=DataArrayInt.New();
822 revDescIndx=DataArrayInt.New();
823 mesh2=mesh.buildDescendingConnectivity(desc,descIndx,revDesc,revDescIndx);
824 assert desc.getValues() == [0,1,2,3, 2,4,5, 6,7,4, 8,9,1,10, 11,12,6,9]
825 assert descIndx.getValues() == [0,4,7,10,14,18]
826 assert revDesc.getValues() == [0, 0,3, 0,1, 0, 1,2, 1, 2,4, 2, 3, 3,4, 3, 4, 4]
827 assert revDescIndx.getValues() == [0,1,3,5,6,8,9,11,12,13,15,16,17,18]
828 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_2]
831 def testExample_MEDCouplingUMesh_getReverseNodalConnectivity(self):
832 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_1]
833 mesh=MEDCouplingUMesh.New();
834 mesh.setMeshDimension(2);
835 mesh.allocateCells(5);
836 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
837 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
838 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
839 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
840 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
841 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
842 mesh.finishInsertingCells();
843 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ];
844 coordsArr=DataArrayDouble.New();
845 coordsArr.setValues(coords,9,2);
846 mesh.setCoords(coordsArr);
847 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_1]
848 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_2]
849 revNodal,revNodalIndx=mesh.getReverseNodalConnectivity();
850 assert revNodal.getValues() == [0,0,1,1,2,0,3,0,1,2,3,4,2,4,3,3,4,4];
851 assert revNodalIndx.getValues() == [0,1,3,5,7,12,14,15,17,18];
852 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_2]
855 def testExample_MEDCouplingUMesh_checkDeepEquivalWith(self):
856 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_1]
858 mesh1=MEDCouplingUMesh.New();
859 mesh1.setMeshDimension(2);
864 coordsArr=DataArrayDouble.New(coords,4,2);
865 mesh1.setCoords(coordsArr);
866 mesh1.allocateCells(2);
867 mesh1.insertNextCell(NORM_TRI3,3,[0,1,2]); #0
868 mesh1.insertNextCell(NORM_TRI3,3,[1,2,3]); #1
869 mesh1.finishInsertingCells();
871 mesh2=MEDCouplingUMesh.New();
872 mesh2.setMeshDimension(2);
873 coords=[0.0,1.0, #0 = #3
877 coordsArr2=DataArrayDouble.New(coords,4,2)
878 mesh2.setCoords(coordsArr2);
879 mesh2.allocateCells(2);
880 mesh2.insertNextCell(NORM_TRI3,3,[2,3,0]); #0 = #1
881 mesh2.insertNextCell(NORM_TRI3,3,[3,1,2]); #1 ~ #0
882 mesh2.finishInsertingCells();
883 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_1]
884 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_2]
885 cellCompPol = 1 # "permuted same orientation" - policy of medium severity
886 cOld2New, nOld2New = mesh1.checkDeepEquivalWith( mesh2, cellCompPol, 0.002 )
887 assert nOld2New.getValues() == [3, 0, 1, 2]
888 assert cOld2New.getValues() == [1, 0]
889 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_2]
890 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_3]
891 self.assertRaises( InterpKernelException, mesh1.checkDeepEquivalOnSameNodesWith, mesh2, cellCompPol, 0.002)
892 mesh2.setCoords(coordsArr) # make meshes share the same coordinates array
893 mesh2.allocateCells(2);
894 mesh2.insertNextCell(NORM_TRI3,3,[1,2,3]); #0 = #1
895 mesh2.insertNextCell(NORM_TRI3,3,[1,0,2]); #1 ~ #0
896 mesh2.finishInsertingCells();
897 cellCompPol = 2 # the weakest policy
898 mesh1.checkDeepEquivalOnSameNodesWith( mesh2, cellCompPol, 0 )
899 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_3]
902 def testExample_MEDCouplingPointSet_scale(self):
903 #! [PySnippet_MEDCouplingPointSet_scale_1]
904 coords=[0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0] # 2D coordinates of 4 nodes
905 coordsArr=DataArrayDouble.New();
906 coordsArr.setValues(coords,4,2);
907 mesh=MEDCouplingUMesh.New();
908 mesh.setCoords(coordsArr);
909 initCoords = coordsArr.deepCpy()
910 #! [PySnippet_MEDCouplingPointSet_scale_1]
911 #! [PySnippet_MEDCouplingPointSet_scale_2]
914 mesh.scale(center,factor)
915 #! [PySnippet_MEDCouplingPointSet_scale_2]
916 #! [PySnippet_MEDCouplingPointSet_scale_3]
917 coords2 = mesh.getCoords()
918 assert coords2.isEqualWithoutConsideringStr( initCoords, 1.0 )
919 assert not coords2.isEqualWithoutConsideringStr( initCoords, 0.9 )
920 #! [PySnippet_MEDCouplingPointSet_scale_3]
923 def testExample_MEDCouplingPointSet_translate(self):
924 #! [PySnippet_MEDCouplingPointSet_translate_1]
925 coords=[0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0] # 2D coordinates of 4 nodes
926 coordsArr=DataArrayDouble.New();
927 coordsArr.setValues(coords,4,2);
928 mesh=MEDCouplingUMesh.New();
929 mesh.setCoords(coordsArr);
930 initCoords = coordsArr.deepCpy()
931 #! [PySnippet_MEDCouplingPointSet_translate_1]
932 #! [PySnippet_MEDCouplingPointSet_translate_2]
934 mesh.translate(vector)
935 #! [PySnippet_MEDCouplingPointSet_translate_2]
936 #! [PySnippet_MEDCouplingPointSet_translate_3]
937 coords2 = mesh.getCoords()
938 assert coords2.isEqualWithoutConsideringStr( initCoords, 1 )
939 assert not coords2.isEqualWithoutConsideringStr( initCoords, 0.9 )
940 #! [PySnippet_MEDCouplingPointSet_translate_3]
943 def testExample_MEDCouplingPointSet_rotate(self):
944 #! [PySnippet_MEDCouplingPointSet_rotate_1]
945 coords=[0.0,0.0, 0.1,0.0, 0.1,0.1, 0.0,0.1] # 2D coordinates of 4 nodes
946 coordsArr=DataArrayDouble.New();
947 coordsArr.setValues(coords,4,2);
948 mesh=MEDCouplingUMesh.New();
949 mesh.setCoords(coordsArr);
950 #! [PySnippet_MEDCouplingPointSet_rotate_1]
951 #! [PySnippet_MEDCouplingPointSet_rotate_2]
953 mesh.rotate(center,-pi/2)
954 #! [PySnippet_MEDCouplingPointSet_rotate_2]
955 #! [PySnippet_MEDCouplingPointSet_rotate_3]
956 mesh.changeSpaceDimension(3)
959 mesh.rotate(center,vector,pi/2)
960 #! [PySnippet_MEDCouplingPointSet_rotate_3]
961 #! [PySnippet_MEDCouplingPointSet_rotate_4]
962 mesh.changeSpaceDimension(2)
963 coords2 = mesh.getCoords().getValues()
964 for i,c in enumerate( coords ):
965 self.assertAlmostEqual( c, coords2[i], 13 )
966 #! [PySnippet_MEDCouplingPointSet_rotate_4]
969 def testExample_MEDCouplingPointSet_getBoundingBox(self):
970 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_1]
971 cc=[0.0, 0.1, 0.2, # 3D coordinates of 2 nodes
973 coordsArr=DataArrayDouble.New();
974 coordsArr.setValues(cc,2,3);
975 mesh=MEDCouplingUMesh.New();
976 mesh.setCoords(coordsArr);
977 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_1]
978 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_2]
979 bbox=mesh.getBoundingBox()
980 assert bbox == [( cc[0], cc[3] ), # NOTE: list of 3 tuples is retirned!
983 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_2]
985 def testExample_MEDCouplingPointSet_getNodeIdsNearPoint(self):
986 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_1]
987 # 2D coordinates of 5 nodes
988 coords=[0.3,-0.301, # 0
992 0.3,-0.30299999999999];# 4
993 coordsArr=DataArrayDouble.New();
994 coordsArr.setValues(coords,5,2);
995 mesh=MEDCouplingUMesh.New();
996 mesh.setCoords(coordsArr);
997 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_1]
998 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_2]
999 point=[0.3, -0.3] # point close to nodes #0, #2 and #4
1000 ids=mesh.getNodeIdsNearPoint(point,0.003);
1001 assert ids.getValues() == [0,2,4]
1002 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_2]
1005 def testExample_MEDCouplingPointSet_getNodeIdsNearPoints(self):
1006 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_1]
1007 # 2D coordinates of 7 nodes
1008 coords=[0.3,-0.301, # 0
1015 coordsArr=DataArrayDouble.New();
1016 coordsArr.setValues(coords,7,2);
1017 mesh=MEDCouplingUMesh.New();
1018 mesh.setCoords(coordsArr);
1019 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_1]
1020 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_2]
1021 points=[0.2,-0.301, # ~ node #1
1023 1.1, 0.002] # ~ nodes #3, #4 and #5
1024 ids,idsIndex=mesh.getNodeIdsNearPoints(points,3,0.003);
1025 assert ids.getValues() == [1, 3, 4, 5]
1026 assert idsIndex.getValues() == [0, 1, 1, 4]
1027 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_2]
1030 def testExample_MEDCouplingPointSet_findCommonNodes(self):
1031 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_1]
1032 coords=[0.3,-0.301, # 0
1038 coordsArr=DataArrayDouble.New();
1039 coordsArr.setValues(coords,6,2);
1040 mesh=MEDCouplingUMesh.New();
1041 mesh.setCoords(coordsArr);
1042 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_1]
1043 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_2]
1044 comm,commI=mesh.findCommonNodes(1e-13)
1045 assert comm.getValues() == [3,4]
1046 comm,commI=mesh.findCommonNodes(0.004)
1047 assert comm.getValues() == [0,2,5,3,4]
1048 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_2]
1051 def testExample_MEDCouplingPointSet_getCoordinatesOfNode(self):
1052 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_1]
1053 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3];
1054 coordsArr=DataArrayDouble.New();
1055 coordsArr.setValues(coords,3,2);
1056 mesh=MEDCouplingUMesh.New();
1057 mesh.setCoords(coordsArr);
1058 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_1]
1059 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_2]
1060 nodeCoords=mesh.getCoordinatesOfNode(1)
1061 self.assertAlmostEqual(0.2, nodeCoords[0],13);
1062 self.assertAlmostEqual(-0.3,nodeCoords[1],13);
1063 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_2]
1066 def testExample_DataArrayInt_getTuple(self):
1067 #! [Snippet_DataArrayInt_getTuple_1]
1068 dv=DataArrayInt.New();
1072 assert dv.getTuple( 1 ) == [9,10]
1073 #! [Snippet_DataArrayInt_getTuple_1]
1074 #! [Snippet_DataArrayInt_getTuple_2]
1077 #! [Snippet_DataArrayInt_getTuple_2]
1080 def testExample_DataArrayInt_buildPermutationArr(self):
1081 #! [PySnippet_DataArrayInt_buildPermutationArr_1]
1082 a=DataArrayInt.New()
1083 a.setValues([4,5,6,7,8],5,1)
1084 b=DataArrayInt.New()
1085 b.setValues([5,4,8,6,7],5,1)
1086 c=a.buildPermutationArr(b)
1087 #! [PySnippet_DataArrayInt_buildPermutationArr_1]
1088 self.assertEqual([1,0,4,2,3],c.getValues())
1091 def testExample_DataArrayInt_invertArrayO2N2N2O(self):
1092 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
1094 da=DataArrayInt.New();
1095 da.setValues(arr1,6,1);
1096 da2=da.invertArrayO2N2N2O(6);
1097 expected1=[1,3,0,5,2,4]
1099 self.assertEqual(expected1[i],da2.getIJ(i,0));
1101 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
1104 def testExample_DataArrayInt_invertArrayN2O2O2N(self):
1105 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
1107 da=DataArrayInt.New();
1108 da.setValues(arr1,6,1);
1109 da2=da.invertArrayN2O2O2N(7);
1110 expected1=[1,3,0,5,2,4,-1]
1112 self.assertEqual(expected1[i],da2.getIJ(i,0));
1114 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
1118 def testExample_DataArrayDouble_getIdsInRange(self):
1119 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
1120 da=DataArrayDouble.New()
1122 da[ :, :] = range(10)
1123 da2 = da.getIdsInRange( 2.5, 6 )
1124 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
1127 def testExample_DataArrayDouble_setPartOfValues2(self):
1128 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
1129 da=DataArrayDouble.New()
1132 dv=DataArrayDouble.New();
1136 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
1137 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
1139 da[ [0,1,2], [1,3] ] = dv
1140 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
1141 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
1144 da[ [0,2,3], [0,2,3,4,5,6]] = dv
1145 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
1148 def testExample_DataArrayInt_setPartOfValues2(self):
1149 #! [Snippet_DataArrayInt_setPartOfValues2_1]
1150 da=DataArrayInt.New()
1153 dv=DataArrayInt.New();
1157 #! [Snippet_DataArrayInt_setPartOfValues2_1]
1158 #! [Snippet_DataArrayInt_setPartOfValues2_2]
1160 da[ [0,1,2], [1,3] ] = dv
1161 #! [Snippet_DataArrayInt_setPartOfValues2_2]
1162 #! [Snippet_DataArrayInt_setPartOfValues2_3]
1165 da[ [0,2,3], [0,2,3,4,5,6]] = dv
1166 #! [Snippet_DataArrayInt_setPartOfValues2_3]
1169 def testExample_DataArrayDouble_setPartOfValues3(self):
1170 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
1171 da=DataArrayDouble.New()
1174 dv=DataArrayDouble.New();
1178 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
1179 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
1181 da[ 0:3, [1,3] ] = dv
1182 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
1183 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
1186 da[ 0:4:2, [0,2,3,4,5,6]] = dv
1187 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
1190 def testExample_DataArrayInt_setPartOfValues3(self):
1191 #! [Snippet_DataArrayInt_setPartOfValues3_1]
1192 da=DataArrayInt.New()
1195 dv=DataArrayInt.New();
1199 #! [Snippet_DataArrayInt_setPartOfValues3_1]
1200 #! [Snippet_DataArrayInt_setPartOfValues3_2]
1202 da[ 0:3, [1,3] ] = dv
1203 #! [Snippet_DataArrayInt_setPartOfValues3_2]
1204 #! [Snippet_DataArrayInt_setPartOfValues3_3]
1207 da[ 0:4:2, [0,2,3,4,5,6]] = dv
1208 #! [Snippet_DataArrayInt_setPartOfValues3_3]
1211 def testExample_DataArrayDouble_setPartOfValues1(self):
1212 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
1213 da=DataArrayDouble.New()
1215 da.setInfoOnComponents( ["v1","v2","v3","v4"])
1217 dv=DataArrayDouble.New();
1221 dv.setInfoOnComponents( ["a1","a2"])
1222 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
1223 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
1225 da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
1226 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
1227 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
1229 da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
1230 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
1231 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
1233 da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
1234 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
1235 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
1237 da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
1238 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
1239 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
1242 da2[ 0:3:2, 1:4:2 ] = dv
1243 self.assertTrue( da.isEqual( da2, 1e-20 ))
1244 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
1247 def testExample_DataArrayInt_setPartOfValues1(self):
1248 #! [Snippet_DataArrayInt_setPartOfValues1_1]
1249 da=DataArrayInt.New()
1251 da.setInfoOnComponents( ["v1","v2","v3","v4"])
1253 dv=DataArrayInt.New();
1257 dv.setInfoOnComponents( ["a1","a2"])
1258 #! [Snippet_DataArrayInt_setPartOfValues1_1]
1259 #! [Snippet_DataArrayInt_setPartOfValues1_2]
1261 da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
1262 #! [Snippet_DataArrayInt_setPartOfValues1_2]
1263 #! [Snippet_DataArrayInt_setPartOfValues1_3]
1265 da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
1266 #! [Snippet_DataArrayInt_setPartOfValues1_3]
1267 #! [Snippet_DataArrayInt_setPartOfValues1_4]
1269 da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
1270 #! [Snippet_DataArrayInt_setPartOfValues1_4]
1271 #! [Snippet_DataArrayInt_setPartOfValues1_5]
1273 da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
1274 #! [Snippet_DataArrayInt_setPartOfValues1_5]
1275 #! [Snippet_DataArrayInt_setPartOfValues1_6]
1278 da2[ 0:3:2, 1:4:2 ] = dv
1279 self.assertTrue( da.isEqual( da2 ))
1280 #! [Snippet_DataArrayInt_setPartOfValues1_6]
1283 def testExample_DataArrayDouble_setPartOfValuesSimple1(self):
1284 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
1285 da=DataArrayDouble.New()
1288 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
1289 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
1291 da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
1292 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
1293 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
1295 da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
1296 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
1297 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
1299 da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
1300 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
1301 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
1303 da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
1304 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
1305 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
1308 da2[ 0:3:2, 1:4:2 ] = dv
1309 self.assertTrue( da.isEqual( da2, 1e-20 ))
1310 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
1313 def testExample_DataArrayInt_setPartOfValuesSimple1(self):
1314 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
1315 da=DataArrayInt.New()
1318 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
1319 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
1321 da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
1322 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
1323 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
1325 da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
1326 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
1327 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
1329 da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
1330 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
1331 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
1333 da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
1334 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
1335 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
1338 da2[ 0:3:2, 1:4:2 ] = dv
1339 self.assertTrue( da.isEqual( da2 ))
1340 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
1343 def testExample_DataArrayDouble_setPartOfValuesSimple2(self):
1344 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
1345 da=DataArrayDouble.New()
1348 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
1349 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
1351 da[[1,2], [1,2]] = dv
1352 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
1353 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
1355 da[[0,1,2,3], [1]] = dv
1356 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
1357 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
1359 da[[1], [0,1,2,3]] = dv
1360 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
1361 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
1363 da[[0,2], [1,3]] = dv
1364 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
1367 def testExample_DataArrayInt_setPartOfValuesSimple2(self):
1368 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
1369 da=DataArrayInt.New()
1372 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
1373 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
1375 da[[1,2], [1,2]] = dv
1376 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
1377 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
1379 da[[0,1,2,3], [1]] = dv
1380 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
1381 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
1383 da[[1], [0,1,2,3]] = dv
1384 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
1385 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
1387 da[[0,2], [1,3]] = dv
1388 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
1391 def testExample_DataArrayDouble_setPartOfValuesSimple3(self):
1392 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
1393 da=DataArrayDouble.New()
1396 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
1397 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
1400 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
1401 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
1403 da[[0,1,2,3], 1:2] = dv
1404 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
1405 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
1408 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
1409 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
1411 da[[0,2], 1:4:2] = dv
1412 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
1415 def testExample_DataArrayInt_setPartOfValuesSimple3(self):
1416 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
1417 da=DataArrayInt.New()
1420 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
1421 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
1424 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
1425 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
1427 da[[0,1,2,3], 1:2] = dv
1428 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
1429 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
1432 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
1433 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
1435 da[[0,2], 1:4:2] = dv
1436 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
1439 def testExample_DataArrayDouble_setSelectedComponents(self):
1440 #! [Snippet_DataArrayDouble_setSelectedComponents1]
1441 da=DataArrayDouble.New();
1442 array1=[1.,2., 3.,4., 5.,6.]
1443 da.setValues(array1,3,2)
1444 da.setInfoOnComponents( ["a1","a2"])
1445 #! [Snippet_DataArrayDouble_setSelectedComponents1]
1446 #! [Snippet_DataArrayDouble_setSelectedComponents2]
1447 dv=DataArrayDouble.New();
1450 dv.setInfoOnComponents( ["v1","v2","v3","v4"])
1452 dv.setSelectedComponents( da, [1,0] )
1453 #! [Snippet_DataArrayDouble_setSelectedComponents2]
1454 #! [Snippet_DataArrayDouble_setSelectedComponents3]
1456 self.assertTrue( dv.isEqualWithoutConsideringStr( dv2, 1e-20 ))
1457 #! [Snippet_DataArrayDouble_setSelectedComponents3]
1460 def testExample_DataArrayInt_setSelectedComponents(self):
1461 #! [Snippet_DataArrayInt_setSelectedComponents1]
1462 da=DataArrayInt.New();
1463 array1=[1,2, 3,4, 5,6]
1464 da.setValues(array1,3,2)
1465 da.setInfoOnComponents( ["a1","a2"])
1466 #! [Snippet_DataArrayInt_setSelectedComponents1]
1467 #! [Snippet_DataArrayInt_setSelectedComponents2]
1468 dv=DataArrayInt.New();
1471 dv.setInfoOnComponents( ["v1","v2","v3","v4"])
1473 dv.setSelectedComponents( da, [1,0] )
1474 #! [Snippet_DataArrayInt_setSelectedComponents2]
1475 #! [Snippet_DataArrayInt_setSelectedComponents3]
1477 self.assertTrue( dv.isEqualWithoutConsideringStr( dv2 ))
1478 #! [Snippet_DataArrayInt_setSelectedComponents3]
1481 def testExample_DataArrayDouble_getDifferentValues(self):
1482 #! [Snippet_DataArrayDouble_getDifferentValues1]
1483 da=DataArrayDouble.New();
1484 array1=[2.3,1.2,1.3,2.3,2.301,0.8]
1485 da.setValues(array1,6,1)
1487 dv=da.getDifferentValues(2e-1);
1488 expected2=[2.301,1.3,0.8]
1489 self.assertEqual(3,dv.getNbOfElems());
1491 self.assertAlmostEqual(expected2[i],dv.getIJ(i,0),14);
1493 #! [Snippet_DataArrayDouble_getDifferentValues1]
1496 def testExample_DataArrayDouble_findCommonTuples1(self):
1497 #! [PySnippet_DataArrayDouble_findCommonTuples1]
1498 da=DataArrayDouble.New();
1499 array2=[2.3,2.3, 1.2,1.2, 1.3,1.3, 2.3,2.3, 2.301,2.301, 0.8,0.8]
1500 da.setValues(array2,6,2)
1501 #! [PySnippet_DataArrayDouble_findCommonTuples1]
1502 #! [PySnippet_DataArrayDouble_findCommonTuples2]
1503 c,cI=da.findCommonTuples(1.01e-1);
1504 expected3=[0,3,4,1,2]
1506 self.assertEqual(expected3,c.getValues())
1507 self.assertEqual(expected4,cI.getValues())
1508 #! [PySnippet_DataArrayDouble_findCommonTuples2]
1511 def testExampleDataArrayDoubleMeldWith(self):
1512 #! [PySnippet_DataArrayDouble_Meld1_1]
1513 da1=DataArrayDouble.New();
1515 da2=DataArrayDouble.New();
1518 da1.fillWithValue(7.);
1520 da3=da2.applyFunc(3,"10*x*IVec+100*x*JVec+1000*x*KVec");
1522 da1.setInfoOnComponent(0,"c0da1");
1523 da1.setInfoOnComponent(1,"c1da1");
1524 da3.setInfoOnComponent(0,"c0da3");
1525 da3.setInfoOnComponent(1,"c1da3");
1526 da3.setInfoOnComponent(2,"c2da3");
1530 #! [PySnippet_DataArrayDouble_Meld1_1]
1532 def testExampleDataArrayIntMeldWith(self):
1533 #! [PySnippet_DataArrayInt_Meld1_1]
1534 da1=DataArrayInt.New();
1536 da2=DataArrayInt.New();
1539 da1.fillWithValue(7);
1542 da1.setInfoOnComponent(0,"c0da1");
1543 da1.setInfoOnComponent(1,"c1da1");
1544 da2.setInfoOnComponent(0,"c0da2");
1547 #! [PySnippet_DataArrayInt_Meld1_1]
1549 def testExampleDataArrayDoubleKeepSelectedComponents1(self):
1550 #! [SnippeDataArrayDoubleKeepSelectedComponents1_1]
1551 arr1=[1.,2.,3.,4., # tuple 0
1552 11.,12.,13.,14., # tuple 1
1553 21.,22.,23.,24., # ...
1556 a1=DataArrayDouble.New()
1557 a1.setValues(arr1,5,4)
1558 a1.setInfoOnComponent(0,"a");
1559 a1.setInfoOnComponent(1,"b");
1560 a1.setInfoOnComponent(2,"c");
1561 a1.setInfoOnComponent(3,"d");
1562 #! [SnippeDataArrayDoubleKeepSelectedComponents1_1]
1563 #! [SnippeDataArrayDoubleKeepSelectedComponents1_2]
1565 a2=a1.keepSelectedComponents(arr2V)
1566 #! [SnippeDataArrayDoubleKeepSelectedComponents1_2]
1569 def testExampleDataArrayIntKeepSelectedComponents1(self):
1570 #! [SnippeDataArrayIntKeepSelectedComponents1_1]
1571 arr1=[1,2,3,4, # tuple 0
1572 11,12,13,14, # tuple 1
1576 a1=DataArrayInt.New()
1577 a1.setValues(arr1,5,4)
1578 a1.setInfoOnComponent(0,"a");
1579 a1.setInfoOnComponent(1,"b");
1580 a1.setInfoOnComponent(2,"c");
1581 a1.setInfoOnComponent(3,"d");
1582 #! [SnippeDataArrayIntKeepSelectedComponents1_1]
1583 #! [SnippeDataArrayIntKeepSelectedComponents1_2]
1585 a2=a1.keepSelectedComponents(arr2V)
1586 #! [SnippeDataArrayIntKeepSelectedComponents1_2]
1587 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
1589 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
1592 def testExampleFieldDoubleBuildSubPart1(self):
1593 from MEDCouplingDataForTest import MEDCouplingDataForTest
1594 #! [PySnippetFieldDoubleBuildSubPart1_1]
1595 mesh1=MEDCouplingDataForTest.build2DTargetMesh_1()
1596 f1=MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME)
1599 array=DataArrayDouble.New()
1600 arr1=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.]
1601 array.setValues(arr1,mesh1.getNumberOfCells(),2)
1603 # ! [PySnippetFieldDoubleBuildSubPart1_1]
1604 # ! [PySnippetFieldDoubleBuildSubPart1_2]
1606 f2=f1.buildSubPart(part1)
1607 # ! [PySnippetFieldDoubleBuildSubPart1_2]
1609 self.assertEqual(3,f2.getNumberOfTuples())
1610 self.assertEqual(2,f2.getNumberOfComponents())
1611 expected1=[5.,105.,4.,104.,7.,107.]
1613 self.assertAlmostEqual(f2.getIJ(0,i),expected1[i],12)
1615 self.assertEqual(3,f2.getMesh().getNumberOfCells())
1616 self.assertEqual(6,f2.getMesh().getNumberOfNodes())
1617 self.assertEqual(2,f2.getMesh().getSpaceDimension())
1618 self.assertEqual(2,f2.getMesh().getMeshDimension())
1620 self.assertEqual(13,m2C.getMeshLength())
1621 expected2=[0.2, -0.3, 0.7, -0.3, 0.2, 0.2, 0.7, 0.2, 0.2, 0.7, 0.7, 0.7]
1622 for i in xrange(12):
1623 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
1625 expected3=[3,2,3,1,3,0,2,1,4,4,5,3,2]
1626 self.assertEqual(expected3,list(m2C.getNodalConnectivity().getValues()))
1627 expected4=[0,4,8,13]
1628 self.assertEqual(expected4,list(m2C.getNodalConnectivityIndex().getValues()))
1629 # Test with field on nodes.
1630 # ! [PySnippetFieldDoubleBuildSubPart1_3]
1631 f1=MEDCouplingFieldDouble.New(ON_NODES,ONE_TIME)
1634 array=DataArrayDouble.New()
1635 arr2=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.,8.,108.,9.,109.,10.,110.,11.,111.]
1636 array.setValues(arr2,mesh1.getNumberOfNodes(),2)
1638 # ! [PySnippetFieldDoubleBuildSubPart1_3]
1639 # ! [PySnippetFieldDoubleBuildSubPart1_4]
1641 f2=f1.buildSubPart(part2)
1642 # ! [PySnippetFieldDoubleBuildSubPart1_4]
1643 self.assertEqual(4,f2.getNumberOfTuples())
1644 self.assertEqual(2,f2.getNumberOfComponents())
1645 expected5=[4.,104.,5.,105.,7.,107.,8.,108.]
1647 self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
1649 self.assertEqual(2,f2.getMesh().getNumberOfCells())
1650 self.assertEqual(4,f2.getMesh().getNumberOfNodes())
1651 self.assertEqual(2,f2.getMesh().getSpaceDimension())
1652 self.assertEqual(2,f2.getMesh().getMeshDimension())
1654 self.assertEqual(8,m2C.getMeshLength())
1655 for i in xrange(8):#8 is not an error
1656 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
1658 self.assertEqual(expected3[:4],list(m2C.getNodalConnectivity().getValues())[4:])
1659 self.assertEqual(expected3[4:8],list(m2C.getNodalConnectivity().getValues())[:4])
1660 self.assertEqual(expected4[:3],list(m2C.getNodalConnectivityIndex().getValues()))
1661 #idem previous because nodes of cell#4 are not fully present in part3
1663 arrr=DataArrayInt.New()
1664 arrr.setValues(part3,2,1)
1665 f2=f1.buildSubPart(arrr)
1666 self.assertEqual(4,f2.getNumberOfTuples())
1667 self.assertEqual(2,f2.getNumberOfComponents())
1669 self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
1671 self.assertEqual(2,f2.getMesh().getNumberOfCells())
1672 self.assertEqual(4,f2.getMesh().getNumberOfNodes())
1673 self.assertEqual(2,f2.getMesh().getSpaceDimension())
1674 self.assertEqual(2,f2.getMesh().getMeshDimension())
1676 self.assertEqual(8,m2C.getMeshLength())
1677 for i in xrange(8):#8 is not an error
1678 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
1680 self.assertEqual(expected3[:4],list(m2C.getNodalConnectivity().getValues())[4:8])
1681 self.assertEqual(expected3[4:8],list(m2C.getNodalConnectivity().getValues())[:4])
1682 self.assertEqual(expected4[:3],list(m2C.getNodalConnectivityIndex().getValues()))
1684 f2=f1.buildSubPart(part4)
1685 self.assertEqual(6,f2.getNumberOfTuples())
1686 self.assertEqual(2,f2.getNumberOfComponents())
1687 expected6=[4.,104.,5.,105.,7.,107.,8.,108.,10.,110.,11.,111.]
1688 for i in xrange(12):
1689 self.assertAlmostEqual(f2.getIJ(0,i),expected6[i],12)
1691 self.assertEqual(3,f2.getMesh().getNumberOfCells())
1692 self.assertEqual(6,f2.getMesh().getNumberOfNodes())
1693 self.assertEqual(2,f2.getMesh().getSpaceDimension())
1694 self.assertEqual(2,f2.getMesh().getMeshDimension())
1696 self.assertEqual(13,m2C.getMeshLength())
1697 for i in xrange(12):
1698 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
1700 self.assertEqual(expected3[0:4],list(m2C.getNodalConnectivity().getValues())[4:8])
1701 self.assertEqual(expected3[4:8],list(m2C.getNodalConnectivity().getValues())[0:4])
1702 self.assertEqual(expected3[8:13],list(m2C.getNodalConnectivity().getValues())[8:13])
1703 self.assertEqual(expected4,list(m2C.getNodalConnectivityIndex().getValues()))
1706 def testExampleUMeshStdBuild1(self):
1707 # ! [PySnippetUMeshStdBuild1_1]
1708 coords=[-0.3,-0.3,0., 0.2,-0.3,0., 0.7,-0.3,0., -0.3,0.2,0., 0.2,0.2,0.,
1709 0.7,0.2,0., -0.3,0.7,0., 0.2,0.7,0., 0.7,0.7,0. ]
1710 nodalConnPerCell=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1711 # ! [PySnippetUMeshStdBuild1_1]
1712 # ! [PySnippetUMeshStdBuild1_2]
1713 mesh=MEDCouplingUMesh.New("My2DMesh",2)
1714 # ! [PySnippetUMeshStdBuild1_2]
1715 # ! [PySnippetUMeshStdBuild1_3]
1716 mesh.allocateCells(5)#You can put more than 5 if you want but not less.
1717 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[:4])
1718 mesh.insertNextCell(NORM_TRI3,nodalConnPerCell[4:7])
1719 mesh.insertNextCell(NORM_TRI3,nodalConnPerCell[7:10])
1720 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[10:14])
1721 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[14:])
1722 mesh.finishInsertingCells()
1723 # ! [PySnippetUMeshStdBuild1_3]
1724 # ! [PySnippetUMeshStdBuild1_4]
1725 coordsArr=DataArrayDouble.New(coords,9,3)#here coordsArr are declared to have 3 components, mesh will deduce that its spaceDim==3.
1726 mesh.setCoords(coordsArr)#coordsArr contains 9 tuples, that is to say mesh contains 9 nodes.
1727 # ! [PySnippetUMeshStdBuild1_4]
1728 # ! [PySnippetUMeshStdBuild1_5]
1729 # ! [PySnippetUMeshStdBuild1_5]
1730 mesh.checkCoherency()
1733 def testExampleCMeshStdBuild1(self):
1734 # ! [PySnippetCMeshStdBuild1_1]
1735 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] # 9 values along X
1736 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] # 7 values along Y
1737 arrX=DataArrayDouble.New(XCoords)
1738 arrX.setInfoOnComponent(0,"X [m]")
1739 arrY=DataArrayDouble.New(YCoords)
1740 arrY.setInfoOnComponent(0,"Y [m]")
1741 # ! [PySnippetCMeshStdBuild1_1]
1742 # ! [PySnippetCMeshStdBuild1_2]
1743 mesh=MEDCouplingCMesh.New("My2D_CMesh")
1744 mesh.setCoords(arrX,arrY)
1745 # ! [PySnippetCMeshStdBuild1_2]
1746 # ! [PySnippetCMeshStdBuild1_3]
1747 self.assertEqual(8*6,mesh.getNumberOfCells())
1748 self.assertEqual(9*7,mesh.getNumberOfNodes())
1749 self.assertEqual(2,mesh.getSpaceDimension())
1750 self.assertEqual(2,mesh.getMeshDimension())
1751 # ! [PySnippetCMeshStdBuild1_3]
1752 mesh=MEDCouplingCMesh.New("My2D_CMesh")
1753 # ! [PySnippetCMeshStdBuild1_2bis]
1754 mesh.setCoordsAt(0,arrX)
1755 mesh.setCoordsAt(1,arrY)
1756 # ! [PySnippetCMeshStdBuild1_2bis]
1757 self.assertEqual(8*6,mesh.getNumberOfCells())
1758 self.assertEqual(9*7,mesh.getNumberOfNodes())
1759 self.assertEqual(2,mesh.getSpaceDimension())
1760 self.assertEqual(2,mesh.getMeshDimension())
1763 def testExampleUMeshAdvBuild1(self):
1764 # ! [PySnippetUMeshAdvBuild1_1]
1765 coords=[-0.3,-0.3,0., 0.2,-0.3,0., 0.7,-0.3,0., -0.3,0.2,0., 0.2,0.2,0.,
1766 0.7,0.2,0., -0.3,0.7,0., 0.2,0.7,0., 0.7,0.7,0. ]
1767 nodalConnPerCell=[4,0,3,4,1, 3,1,4,2, 3,4,5,2, 4,6,7,4,3, 4,7,8,5,4]
1768 nodalConnPerCellIndex=[0,5,9,13,18,23]
1769 # ! [PySnippetUMeshAdvBuild1_1]
1770 # ! [PySnippetUMeshAdvBuild1_2]
1771 mesh=MEDCouplingUMesh.New("My2DMesh",2)
1772 # ! [PySnippetUMeshAdvBuild1_2]
1773 # ! [PySnippetUMeshAdvBuild1_3]
1774 nodalConn=DataArrayInt.New(nodalConnPerCell,23,1)
1775 nodalConnI=DataArrayInt.New(nodalConnPerCellIndex,6,1)
1776 mesh.setConnectivity(nodalConn,nodalConnI,True)
1777 # ! [PySnippetUMeshAdvBuild1_3]
1778 # ! [PySnippetUMeshAdvBuild1_4]
1779 coordsArr=DataArrayDouble.New(coords,9,3)#here coordsArr are declared to have 3 components, mesh will deduce that its spaceDim==3.
1780 mesh.setCoords(coordsArr)#coordsArr contains 9 tuples, that is to say mesh contains 9 nodes.
1781 # ! [PySnippetUMeshAdvBuild1_4]
1782 # ! [PySnippetUMeshAdvBuild1_5]
1783 # ! [PySnippetUMeshAdvBuild1_5]
1784 mesh.checkCoherency()
1787 def testExampleDataArrayBuild1(self):
1788 # ! [PySnippetDataArrayBuild1_0]
1789 dataDouble=[0.,10.,20.,1.,11.,21.,2.,12.,22.,3.,13.,23.,4.,14.,24.]
1790 # ! [PySnippetDataArrayBuild1_0]
1791 # ! [PySnippetDataArrayBuild1_1]
1792 arrayDouble=DataArrayDouble.New()
1793 arrayDouble.setValues(dataDouble,5,3)# 5 tuples containing each 3 components
1794 # ! [PySnippetDataArrayBuild1_1]
1795 # ! [PySnippetDataArrayBuild1_1bis]
1796 arrayDouble=DataArrayDouble.New(dataDouble,5,3)
1797 # ! [PySnippetDataArrayBuild1_1bis]
1798 # ! [PySnippetDataArrayBuild1_2]
1799 dataInt=[0, 10, 20, 1, 11, 21, 2, 12, 22, 3, 13, 23, 4, 14, 24]
1800 # ! [PySnippetDataArrayBuild1_2]
1801 # ! [PySnippetDataArrayBuild1_3]
1802 arrayInt=DataArrayInt.New()
1803 arrayInt.setValues(dataInt,5,3)# 5 tuples containing each 3 components
1804 # ! [PySnippetDataArrayBuild1_3]
1805 # ! [PySnippetDataArrayBuild1_3bis]
1806 arrayInt=DataArrayInt.New(dataInt,5,3)
1807 # ! [PySnippetDataArrayBuild1_3bis]
1810 def testExampleFieldDoubleBuild1(self):
1811 XCoords=[-0.3,0.07,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
1812 YCoords=[0.07,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
1813 mesh=MEDCouplingCMesh.New("My2D_CMesh")
1814 mesh.setCoords(arrX,arrY)
1815 # ! [PySnippetFieldDoubleBuild1_1]
1816 fieldOnCells=MEDCouplingFieldDouble.New(ON_CELLS,NO_TIME)
1817 fieldOnCells.setName("MyTensorFieldOnCellNoTime")
1818 fieldOnCells.setMesh(mesh)
1819 array=DataArrayDouble.New()
1820 array.alloc(fieldOnCells.getMesh().getNumberOfCells(),9) # Implicitely fieldOnCells will be a 9 components field.
1821 array.fillWithValue(7.)
1822 fieldOnCells.setArray(array)
1823 # fieldOnCells is now usable
1825 # ! [PySnippetFieldDoubleBuild1_1]
1826 # ! [PySnippetFieldDoubleBuild1_2]
1827 f1=mesh.fillFromAnalytic(ON_CELLS,1,"x*x+y*y*3+2.*x") # f1 is scalar
1828 f2=mesh.fillFromAnalytic(ON_CELLS,1,"cos(x+y/x)") # f2 is scalar too
1829 f2bis=mesh.fillFromAnalytic(ON_CELLS,2,"x*x*IVec+3*y*JVec") # f2bis is a vectors field
1830 f3=f1+f2 # f3 scalar
1831 f4=f3/f2 # f4 scalar
1832 f2bis.applyFunc(1,"sqrt(x*x+y*y)") # f2bis becomes scalar
1833 f5=f2bis*f4 # f5 scalar
1835 res=f4.getValueOn(pos1) # f4 is scalar so the returned value is of size 1.
1837 # ! [PySnippetFieldDoubleBuild1_2]
1838 # ! [PySnippetFieldDoubleBuild1_3]
1839 # ! [PySnippetFieldDoubleBuild1_3]
1842 def testExampleFieldDoubleBuild2(self):
1843 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
1844 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
1845 mesh=MEDCouplingCMesh.New("My2D_CMesh")
1846 mesh.setCoords(arrX,arrY)
1847 # ! [PySnippetFieldDoubleBuild2_1]
1848 fieldOnNodes=MEDCouplingFieldDouble.New(ON_NODES,NO_TIME)
1849 fieldOnNodes.setName("MyScalarFieldOnNodeNoTime")
1850 fieldOnNodes.setMesh(mesh)
1851 array=DataArrayDouble.New()
1852 array.alloc(fieldOnNodes.getMesh().getNumberOfNodes(),1) # Implicitely fieldOnNodes will be a 1 component field.
1853 array.fillWithValue(7.)
1854 fieldOnNodes.setArray(array)
1855 # fieldOnNodes is now usable
1857 # ! [PySnippetFieldDoubleBuild2_1]
1860 def testExampleFieldDoubleBuild3(self):
1861 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
1862 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
1863 mesh=MEDCouplingCMesh.New("My2D_CMesh")
1864 mesh.setCoords(arrX,arrY)
1865 # ! [PySnippetFieldDoubleBuild3_1]
1866 fieldOnCells=MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME)
1867 fieldOnCells.setName("MyTensorFieldOnCellNoTime")
1868 fieldOnCells.setTimeUnit("ms") # Time unit is ms.
1869 fieldOnCells.setTime(4.22,2,-1) # Time attached is 4.22 ms, iteration id is 2 and order id (or sub iteration id) is -1
1870 fieldOnCells.setMesh(mesh)
1871 array=DataArrayDouble.New()
1872 array.alloc(fieldOnCells.getMesh().getNumberOfCells(),2) # Implicitely fieldOnCells will be a 2 components field.
1873 array.fillWithValue(7.)
1874 fieldOnCells.setArray(array)
1875 # fieldOnCells is now usable
1877 # ! [PySnippetFieldDoubleBuild3_1]
1880 def testExampleFieldDoubleBuild4(self):
1881 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
1882 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
1883 mesh=MEDCouplingCMesh.New("My2D_CMesh")
1884 mesh.setCoords(arrX,arrY)
1885 # ! [PySnippetFieldDoubleBuild4_1]
1886 fieldOnNodes=MEDCouplingFieldDouble.New(ON_NODES,CONST_ON_TIME_INTERVAL)
1887 fieldOnNodes.setName("MyVecFieldOnNodeWithConstTime")
1888 fieldOnNodes.setTimeUnit("ms") # Time unit is ms.
1889 fieldOnNodes.setStartTime(4.22,2,-1)
1890 fieldOnNodes.setEndTime(6.44,4,-1)# fieldOnNodes is defined in interval [4.22 ms,6.44 ms]
1891 fieldOnNodes.setMesh(mesh)
1892 array=DataArrayDouble.New()
1893 array.alloc(fieldOnNodes.getMesh().getNumberOfNodes(),3) # Implicitely fieldOnNodes will be a 3 components field.
1894 array.fillWithValue(7.)
1895 fieldOnNodes.setArray(array)
1896 # fieldOnNodes is now usable
1898 # ! [PySnippetFieldDoubleBuild4_1]
1901 def testExampleDataArrayApplyFunc1(self):
1902 # ! [PySnippetDataArrayApplyFunc1_1]
1903 d=DataArrayDouble.New([1.,2.,11.,12.,21.,22.,31.,41.],4,2)
1904 self.assertRaises(InterpKernelException,d.applyFunc,"x*y")
1905 # ! [PySnippetDataArrayApplyFunc1_1]
1906 # ! [PySnippetDataArrayApplyFunc1_2]
1907 d=DataArrayDouble.New([1.,2.,11.,12.,21.,22.,31.,41.],4,2)
1908 d1=d.applyFunc("smth*smth")
1909 self.assertTrue(d1.isEqual(DataArrayDouble([1.,4.,121.,144.,441.,484.,961.,1681.],4,2),1e-12))
1910 # ! [PySnippetDataArrayApplyFunc1_2]
1911 # ! [PySnippetDataArrayApplyFunc1_3]
1912 d2=d.applyFunc("smth*IVec+2*smth*JVec")
1913 self.assertTrue(d2.isEqual(DataArrayDouble([1.,4.,11.,24.,21.,44.,31.,82.],4,2),1e-12))
1914 # ! [PySnippetDataArrayApplyFunc1_3]
1915 # ! [PySnippetDataArrayApplyFunc1_4]
1916 dd=DataArrayDouble.New([1.,4.,3.,11.,144.,13.,21.,484.,23.,31.,1024.,33.],4,3)
1917 # ! [PySnippetDataArrayApplyFunc1_4]
1918 # ! [PySnippetDataArrayApplyFunc1_5]
1919 dd1=dd.applyFunc(1,"f+sqrt(g)+h")
1920 self.assertTrue(dd1.isEqual(DataArrayDouble([6.,36.,66.,96.],4,1),1e-12))
1921 # ! [PySnippetDataArrayApplyFunc1_5]
1922 # ! [PySnippetDataArrayApplyFunc1_6]
1923 dd2=dd.applyFunc(1,"a+0.*b+c")
1924 self.assertTrue(dd2.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
1925 # ! [PySnippetDataArrayApplyFunc1_6]
1926 # ! [PySnippetDataArrayApplyFunc1_7]
1927 ddd=DataArrayDouble.New([1.,4.,3.,11.,144.,13.,21.,484.,23.,31.,1024.,33.],4,3)
1928 ddd.setInfoOnComponents(["Y [m]","AA [m/s]","GG [MW]"])
1929 # ! [PySnippetDataArrayApplyFunc1_7]
1930 # ! [PySnippetDataArrayApplyFunc1_8]
1931 ddd1=ddd.applyFunc2(1,"Y+GG")
1932 self.assertTrue(ddd1.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
1933 # ! [PySnippetDataArrayApplyFunc1_8]
1934 # ! [PySnippetDataArrayApplyFunc1_9]
1935 ddd1=ddd.applyFunc3(1,["X","Y","Z"],"X+Z")
1936 self.assertTrue(ddd1.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
1937 # ! [PySnippetDataArrayApplyFunc1_9]