1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2016 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, or (at your option) any later version.
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):
27 def testExample_MEDCouplingFieldDouble_WriteVTK(self):
28 #! [PySnippet_MEDCouplingFieldDouble_WriteVTK_1]
31 coordsArr=DataArrayDouble(coords,3,1)
32 mesh=MEDCouplingCMesh()
33 mesh.setCoords(coordsArr,coordsArr) # mesh becomes a 2D one
35 # 3 fields (lying on the same mesh!)
36 field1 = mesh.getMeasureField( True )
37 field2 = mesh.buildOrthogonalField()
38 field3 = mesh.fillFromAnalytic( ON_CELLS, 2, "IVec * x + JVec * y" )
39 field2.setName( "Normal" ) # name is necessary!
40 field3.setName( "Barycenter" ) # name is necessary!
43 fileName = "testExample_MEDCouplingFieldDouble_WriteVTK"
44 fs = [ field1, field2, field3 ] # field series
45 writtenFileName=MEDCouplingFieldDouble.WriteVTK( fileName, fs )
46 print("The file name with correct extension is : %s"%(writtenFileName))
47 #! [PySnippet_MEDCouplingFieldDouble_WriteVTK_1]
49 os.remove( writtenFileName )
53 def testExample_MEDCouplingFieldDouble_MaxFields(self):
54 #! [PySnippet_MEDCouplingFieldDouble_MaxFields_1]
55 vals1 = [0.,2., 4.,6.] # for field 1
56 vals2 = [2.,0., 6.,4.] # for field 2
57 valsMax = [2.,2., 6.,6.] # expected max field
58 valsMin = [0.,0., 4.,4.] # expected min field
61 valsArr1=DataArrayDouble(vals1,2,2) # 2 tuples per 2 components
62 field1 = MEDCouplingFieldDouble( ON_NODES )
63 field1.setArray( valsArr1 )
66 valsArr2=DataArrayDouble(vals2,2,2) # 2 tuples per 2 components
67 field2 = MEDCouplingFieldDouble( ON_NODES )
68 field2.setArray( valsArr2 )
71 fieldMax = MEDCouplingFieldDouble.MaxFields( field1, field2 )
72 self.assertTrue( fieldMax.getArray().getValues() == valsMax )
75 fieldMin = MEDCouplingFieldDouble.MinFields( field1, field2 )
76 self.assertTrue( fieldMin.getArray().getValues() == valsMin )
77 #! [PySnippet_MEDCouplingFieldDouble_MaxFields_1]
79 def testExample_MEDCouplingFieldDouble_MergeFields(self):
80 #! [PySnippet_MEDCouplingFieldDouble_MergeFields_1]
83 coordsArr=DataArrayDouble(coords,3,1)
84 mesh1=MEDCouplingCMesh()
85 mesh1.setCoords(coordsArr)
87 field1 = mesh1.fillFromAnalytic( ON_CELLS, 1, "x")
90 field2 = field1.cloneWithMesh( True )
92 field2.getMesh().translate(vec) # translate mesh2
93 field2.applyFunc("x + 5") # "translate" field2
95 # concatenate field1 and field2
96 field3 = MEDCouplingFieldDouble.MergeFields( field1, field2 )
97 field4 = MEDCouplingFieldDouble.MergeFields( [ field1, field2] )
98 #! [PySnippet_MEDCouplingFieldDouble_MergeFields_1]
101 def testExample_MEDCouplingFieldDouble_substractInPlaceDM(self):
102 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_1]
103 coords1=[0.,1.,2.,3.]
104 coords2=[2.,1.,0.,3.] #0 <==> #2
106 mesh1=MEDCouplingUMesh()
107 coordsArr=DataArrayDouble(coords1, 4, 1)
108 mesh1.setCoords(coordsArr)
109 mesh1.setMeshDimension(0)
110 mesh1.allocateCells(0)
111 mesh1.finishInsertingCells()
113 mesh2=mesh1.deepCopy()
114 mesh2.getCoords().setValues(coords2, 4, 1)
115 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_1]
116 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_2]
117 field1 = mesh1.fillFromAnalytic(ON_NODES,1,"x") # field1 values == coords1
118 field2 = mesh2.fillFromAnalytic(ON_NODES,1,"x") # field2 values == coords2
119 levOfCheck = 10 # nodes can be permuted
120 field1.substractInPlaceDM( field2, levOfCheck, 1e-13, 0 ) # values #0 and #2 must swap
121 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_2]
122 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_3]
123 field2.applyFunc( 1, 0.0 ) # all field2 values == 0.0
124 self.assertTrue( field1.isEqual( field2, 1e-13, 1e-13 )) # field1 == field2 == 0.0
125 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_3]
128 def testExample_MEDCouplingFieldDouble_changeUnderlyingMesh(self):
129 #! [PySnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_1]
130 coords1=[0.,1.,2.,3.]
131 coords2=[2.,1.,0.,3.] #0 <==> #2
133 mesh1=MEDCouplingUMesh()
134 coordsArr=DataArrayDouble(coords1, 4, 1)
135 mesh1.setCoords(coordsArr)
136 mesh1.setMeshDimension(0)
137 mesh1.allocateCells(0)
138 mesh1.finishInsertingCells()
140 mesh2=mesh1.deepCopy()
141 mesh2.getCoords().setValues(coords2, 4, 1)
142 #! [PySnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_1]
143 #! [PySnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_2]
144 field = mesh1.fillFromAnalytic(ON_NODES,1,"x") # field values == coords1
145 levOfCheck = 10 # nodes can be permuted
146 field.changeUnderlyingMesh( mesh2, levOfCheck, 1e-13, 0 ) # values #0 and #2 must swap
147 self.assertTrue( field.getArray().getValues() == coords2 )
148 #! [PySnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_2]
151 def testExample_MEDCouplingFieldDouble_applyFunc_same_nb_comp(self):
152 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_1]
154 array = DataArrayDouble( v, 2, 2 ) # 2 tuples per 2 components
155 field = MEDCouplingFieldDouble( ON_CELLS )
156 field.setArray( array )
157 func = "IVec * v + JVec * w*w + 10"
158 field.applyFunc( 2, func )
159 self.assertTrue( field.getNumberOfComponents() == 2 ) # 2 components remains
160 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_1]
161 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_2]
162 v2 = field.getArray().getValues()
163 self.assertAlmostEqual( v2[0], 10 + v[0], 13 ) # "10 + IVec * v"
164 self.assertAlmostEqual( v2[1], 10 + v[1]*v[1], 13 ) # "10 + JVec * v*v"
165 self.assertAlmostEqual( v2[2], 10 + v[2], 13 ) # "10 + IVec * v"
166 self.assertAlmostEqual( v2[3], 10 + v[3]*v[3], 13 ) # "10 + JVec * v*v"
167 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_2]
170 def testExample_MEDCouplingFieldDouble_applyFunc3(self):
171 #! [PySnippet_MEDCouplingFieldDouble_applyFunc3_1]
172 # create a 2D vector field
173 values = [1.,1., 2.,1.]
174 array = DataArrayDouble( values, 2, 2 ) # 2 tuples per 2 components
175 field = MEDCouplingFieldDouble( ON_CELLS )
176 field.setArray( array )
177 # transform the field to a 3D vector field
178 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
179 varNames=["a","b"] # names used to refer to X and Y components
180 field.applyFuncNamedCompo( 3, varNames, func ) # require 3 components
181 self.assertTrue( field.getNumberOfComponents() == 3 ) # 3 components as required
182 #! [PySnippet_MEDCouplingFieldDouble_applyFunc3_1]
183 #! [PySnippet_MEDCouplingFieldDouble_applyFunc3_2]
184 vec1 = field.getArray().getTuple(1) # vector #1
185 a,b = values[2], values[3] # initial components of the vector #1
186 self.assertAlmostEqual( vec1[0], 10 + b, 13 ) # "10 + IVec * b"
187 self.assertAlmostEqual( vec1[1], 10 + a, 13 ) # "10 + JVec * a"
188 self.assertAlmostEqual( vec1[2], 10 + sqrt(a*a+b*b), 13 ) # "10 + KVec * sqrt( a*a + b*b )"
189 #! [PySnippet_MEDCouplingFieldDouble_applyFunc3_2]
192 def testExample_MEDCouplingFieldDouble_applyFunc2(self):
193 #! [PySnippet_MEDCouplingFieldDouble_applyFunc2_1]
194 # create a 2D vector field
195 values = [1.,1., 2.,1.]
196 array = DataArrayDouble( values, 2, 2 ) # 2 tuples per 2 components
197 array.setInfoOnComponent(0,"a") # name used to refer to X component within a function
198 array.setInfoOnComponent(1,"b") # name used to refer to Y component within a function
199 field = MEDCouplingFieldDouble( ON_CELLS )
200 field.setArray( array )
201 # transform the field to a 3D vector field
202 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
203 field.applyFuncCompo( 3, func ) # require 3 components
204 self.assertTrue( field.getNumberOfComponents() == 3 ) # 3 components as required
205 #! [PySnippet_MEDCouplingFieldDouble_applyFunc2_1]
206 #! [PySnippet_MEDCouplingFieldDouble_applyFunc2_2]
207 vec1 = field.getArray().getTuple(1) # vector #1
208 a,b = values[2], values[3] # initial components of the vector #1
209 self.assertAlmostEqual( vec1[0], 10 + b, 13 ) # "10 + IVec * b"
210 self.assertAlmostEqual( vec1[1], 10 + a, 13 ) # "10 + JVec * a"
211 self.assertAlmostEqual( vec1[2], 10 + sqrt(a*a+b*b), 13 ) # "10 + KVec * sqrt( a*a + b*b )"
212 #! [PySnippet_MEDCouplingFieldDouble_applyFunc2_2]
215 def testExample_MEDCouplingFieldDouble_applyFunc(self):
216 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_1]
217 # create a 2D vector field
218 values = [1.,1., 2.,1.]
219 array = DataArrayDouble( values, 2, 2 ) # 2 tuples per 2 components
220 field = MEDCouplingFieldDouble( ON_CELLS )
221 field.setArray( array )
222 # transform the field to a 3D vector field
223 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
224 field.applyFunc( 3, func ) # require 3 components
225 self.assertTrue( field.getNumberOfComponents() == 3 ) # 3 components as required
226 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_1]
227 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_2]
228 vec1 = field.getArray().getTuple(1) # vector #1
229 a,b = values[2], values[3] # initial components of the vector #1
230 self.assertAlmostEqual( vec1[0], 10 + b, 13 ) # "10 + IVec * b"
231 self.assertAlmostEqual( vec1[1], 10 + a, 13 ) # "10 + JVec * a"
232 self.assertAlmostEqual( vec1[2], 10 + sqrt(a*a+b*b), 13 ) # "10 + KVec * sqrt( a*a + b*b )"
233 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_2]
236 def testExample_MEDCouplingFieldDouble_applyFunc_val(self):
237 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_val_1]
239 coordsArr=DataArrayDouble(coords,3,1)
240 mesh=MEDCouplingCMesh()
241 mesh.setCoords(coordsArr,coordsArr)
242 field = MEDCouplingFieldDouble( ON_CELLS )
243 field.setMesh( mesh )
244 field.fillFromAnalytic(2,"IVec * x + JVec * y") # 2 components
245 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_val_1]
246 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_val_2]
248 field.applyFunc( 3, newValue ) # 3 components are required
249 self.assertTrue( field.getIJ(1,0) == newValue ) # a value is as expected
250 self.assertTrue( field.getNumberOfComponents() == 3 )
251 self.assertTrue( field.getNumberOfTuples() == mesh.getNumberOfCells() )
252 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_val_2]
255 def testExample_MEDCouplingFieldDouble_fillFromAnalytic3(self):
256 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_1]
257 coords = [0.,2.,4.,6.] # 6. is not used
258 x=DataArrayDouble(coords[:3],3,1)
259 y=DataArrayDouble(coords[:2],2,1)
260 mesh=MEDCouplingCMesh()
262 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_1]
263 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_2]
264 field = MEDCouplingFieldDouble( ON_CELLS )
265 field.setMesh( mesh )
266 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
267 varNames=["a","b"] # names used to refer to X and Y coord components
268 field.fillFromAnalyticNamedCompo(3,varNames,func)
269 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_2]
270 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_3]
271 vals1 = field.getArray().getTuple(1) # values of the cell #1
272 assert len( vals1 ) == 3 # 3 components in the field
274 bc = mesh.computeCellCenterOfMass() # func is applied to barycenters of cells
275 bc1 = bc.getTuple(1) # coordinates of the second point
277 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
278 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
279 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
280 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
281 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_3]
284 def testExample_MEDCouplingFieldDouble_fillFromAnalytic2(self):
285 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_1]
287 x=DataArrayDouble(coords[:3],3,1)
288 y=DataArrayDouble(coords[:2],2,1)
289 x.setInfoOnComponent(0,"a") # name used to refer to X coordinate within a function
290 y.setInfoOnComponent(0,"b") # name used to refer to Y coordinate within a function
291 mesh=MEDCouplingCMesh()
293 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_1]
294 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_2]
295 field = MEDCouplingFieldDouble( ON_CELLS )
296 field.setMesh( mesh )
297 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
298 field.fillFromAnalyticCompo(3,func)
299 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_2]
300 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_3]
301 vals1 = field.getArray().getTuple(1) # values of the cell #1
302 assert len( vals1 ) == 3 # 3 components in the field
304 bc = mesh.computeCellCenterOfMass() # func is applied to barycenters of cells
305 bc1 = bc.getTuple(1) # coordinates of the second point
307 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
308 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
309 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
310 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
311 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_3]
314 def testExample_MEDCouplingFieldDouble_fillFromAnalytic(self):
315 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_1]
317 x=DataArrayDouble(coords[:3],3,1)
318 y=DataArrayDouble(coords[:2],2,1)
319 mesh=MEDCouplingCMesh()
321 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_1]
322 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_2]
323 field = MEDCouplingFieldDouble( ON_CELLS )
324 field.setMesh( mesh )
325 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
326 field.fillFromAnalytic(3,func)
327 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_2]
328 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_3]
329 vals1 = field.getArray().getTuple(1) # values of the cell #1
330 assert len( vals1 ) == 3 # 3 components in the field
332 bc = mesh.computeCellCenterOfMass() # func is applied to barycenters of cells
333 bc1 = bc.getTuple(1) # coordinates of the second point
335 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
336 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
337 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
338 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
339 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_3]
342 def testExample_MEDCouplingFieldDouble_getValueOn_time(self):
343 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_1]
345 coordsArr=DataArrayDouble(coords,3,1)
346 mesh=MEDCouplingCMesh()
347 mesh.setCoords(coordsArr,coordsArr)
348 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_1]
349 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_2]
350 field = MEDCouplingFieldDouble( ON_CELLS, LINEAR_TIME )
351 field.setMesh( mesh )
352 field.fillFromAnalytic(1,"10") # all values == 10.
353 field.setEndArray( field.getArray() + field.getArray() ) # all values == 20.
354 time1, time2 = 1.1, 22.
355 field.setStartTime( time1, 0, 0 )
356 field.setEndTime ( time2, 0, 0 )
357 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_2]
358 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_3]
359 pos = [ 1., 1. ] # we are in 2D space
360 value = field.getValueOn( pos, 0.5*( time1 + time2 ))
361 self.assertTrue( value[0] == 0.5*( 10. + 20.))
362 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_3]
365 def testExample_MEDCouplingFieldDouble_getValueOnMulti(self):
366 #! [PySnippet_MEDCouplingFieldDouble_getValueOnMulti_1]
368 coordsArr=DataArrayDouble(coords,3,1)
369 mesh=MEDCouplingCMesh()
370 mesh.setCoords(coordsArr,coordsArr)
371 field = mesh.fillFromAnalytic(ON_CELLS,1,"x+y")
372 #! [PySnippet_MEDCouplingFieldDouble_getValueOnMulti_1]
373 #! [PySnippet_MEDCouplingFieldDouble_getValueOnMulti_2]
374 bc = mesh.computeCellCenterOfMass() # field values are located at cell barycenters
375 valArray = field.getValueOnMulti( bc )
376 self.assertTrue( valArray.isEqual( field.getArray(), 1e-13 ))
377 #! [PySnippet_MEDCouplingFieldDouble_getValueOnMulti_2]
380 def testExample_MEDCouplingFieldDouble_getValueOn(self):
381 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_1]
383 coordsArr=DataArrayDouble(coords,3,1)
384 mesh=MEDCouplingCMesh()
385 mesh.setCoords(coordsArr,coordsArr)
386 field = mesh.fillFromAnalytic(ON_CELLS,1,"x+y")
387 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_1]
388 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_2]
389 bc = mesh.computeCellCenterOfMass() # field values are located at cell barycenters
390 vals = [] # array to collect values returned by getValueOn()
391 for i,tupl in enumerate( bc ):
392 vals.extend( field.getValueOn( tupl ) )
393 self.assertTrue( vals == field.getArray().getValues() )
394 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_2]
397 def testExample_MEDCouplingFieldDouble_getValueOnPos(self):
398 #! [PySnippet_MEDCouplingFieldDouble_getValueOnPos_1]
400 coordsArr=DataArrayDouble(coords,3,1)
401 mesh=MEDCouplingCMesh()
402 mesh.setCoords(coordsArr,coordsArr)
403 field = mesh.fillFromAnalytic(ON_CELLS,1,"x+y")
404 #! [PySnippet_MEDCouplingFieldDouble_getValueOnPos_1]
405 #! [PySnippet_MEDCouplingFieldDouble_getValueOnPos_2]
406 val11 = field.getValueOnPos( 1,1,-1)
407 bc = mesh.computeCellCenterOfMass() # field values are located at cell barycenters
408 self.assertTrue( val11[0] == bc[3,0] + bc[3,1] )
409 #! [PySnippet_MEDCouplingFieldDouble_getValueOnPos_2]
412 def testExample_MEDCouplingFieldDouble_renumberNodes(self):
413 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_1]
415 coordsArr=DataArrayDouble(coords,3,1)
416 mesh=MEDCouplingCMesh()
417 mesh.setCoords(coordsArr,coordsArr)
418 mesh=mesh.buildUnstructured()
419 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_1]
420 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_2]
421 field = mesh.fillFromAnalytic(ON_NODES,2,"IVec*x+JVec*y")
422 values = field.getArray()
423 nodeCoords = mesh.getCoords()
424 self.assertTrue( values.isEqualWithoutConsideringStr( nodeCoords, 1e-13 ))
425 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_2]
426 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_3]
427 renumber = [8, 7, 6, 5, 4, 3, 2, 1, 0]
428 field.renumberNodes(renumber,False)
429 mesh2 = field.getMesh() # field now refers to another mesh
430 values = field.getArray()
431 nodeCoords = mesh2.getCoords()
432 self.assertTrue( values.isEqualWithoutConsideringStr( nodeCoords, 1e-13 ))
433 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_3]
437 def testExample_MEDCouplingFieldDouble_renumberCells(self):
438 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_1]
440 coordsArr=DataArrayDouble(coords,3,1)
441 mesh=MEDCouplingCMesh()
442 mesh.setCoords(coordsArr,coordsArr)
443 mesh=mesh.buildUnstructured()
444 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_1]
445 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_2]
446 field = mesh.fillFromAnalytic(ON_CELLS,2,"IVec*x+JVec*y")
447 values = field.getArray()
448 bc = mesh.computeCellCenterOfMass()
449 self.assertTrue( values.isEqualWithoutConsideringStr( bc, 1e-13 ))
450 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_2]
451 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_3]
452 renumber = [ 3, 2, 1, 0 ]
453 field.renumberCells(renumber,False)
454 mesh2 = field.getMesh() # field now refers to another mesh
455 values = field.getArray()
456 bc = mesh2.computeCellCenterOfMass()
457 self.assertTrue( values.isEqualWithoutConsideringStr( bc, 1e-13 ))
458 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_3]
461 def testExample_MEDCouplingFieldDouble_buildNewTimeReprFromThis(self):
462 #! [PySnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_1]
464 coordsArr=DataArrayDouble(coords,3,1)
465 mesh=MEDCouplingCMesh()
466 mesh.setCoords(coordsArr,coordsArr)
467 field1 = mesh.fillFromAnalytic(ON_NODES,1,"x+y")
468 self.assertTrue( field1.getTimeDiscretization() == ONE_TIME )
469 #! [PySnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_1]
470 #! [PySnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_2]
471 field2 = field1.buildNewTimeReprFromThis(NO_TIME,False)
472 self.assertTrue( field2.getTimeDiscretization() == NO_TIME )
473 #! [PySnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_2]
476 def testExample_MEDCouplingMesh_fillFromAnalytic3(self):
477 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_1]
478 coords = [0.,2.,4.,6.] # 6. is not used
479 x=DataArrayDouble(coords[:3],3,1)
480 y=DataArrayDouble(coords[:2],2,1)
481 mesh=MEDCouplingCMesh()
483 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_1]
484 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_2]
485 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
486 varNames=["a","b"] # names used to refer to X and Y coord components
487 field=mesh.fillFromAnalyticNamedCompo(ON_CELLS,3,varNames,func)
488 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_2]
489 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_3]
490 vals1 = field.getArray().getTuple(1) # values of the cell #1
491 assert len( vals1 ) == 3 # 3 components in the field
493 bc = mesh.computeCellCenterOfMass() # func is applied to barycenters of cells
494 bc1 = bc.getTuple(1) # coordinates of the second point
496 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
497 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
498 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
499 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
500 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_3]
503 def testExample_MEDCouplingMesh_fillFromAnalytic2(self):
504 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_1]
505 coords = [0.,2.,4.,6.] # 6. is not used
506 x=DataArrayDouble(coords[:3],3,1)
507 y=DataArrayDouble(coords[:2],2,1)
508 x.setInfoOnComponent(0,"a") # name used to refer to X coordinate within a function
509 y.setInfoOnComponent(0,"b") # name used to refer to Y coordinate within a function
510 mesh=MEDCouplingCMesh()
512 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_1]
513 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_2]
514 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
515 field=mesh.fillFromAnalyticCompo(ON_CELLS,3,func)
516 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_2]
517 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_3]
518 vals1 = field.getArray().getTuple(1) # values of the cell #1
519 assert len( vals1 ) == 3 # 3 components in the field
521 bc = mesh.computeCellCenterOfMass() # func is applied to barycenters of cells
522 bc1 = bc.getTuple(1) # coordinates of the second point
524 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
525 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
526 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
527 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
528 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_3]
531 def testExample_MEDCouplingMesh_fillFromAnalytic(self):
532 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_1]
533 coords = [0.,2.,4.,6.] # 6. is not used
534 x=DataArrayDouble(coords[:3],3,1)
535 y=DataArrayDouble(coords[:2],2,1)
536 mesh=MEDCouplingCMesh()
538 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_1]
539 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_2]
540 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
541 field=mesh.fillFromAnalytic(ON_CELLS,3,func)
542 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_2]
543 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_3]
544 vals1 = field.getArray().getTuple(1) # values of the cell #1
545 assert len( vals1 ) == 3 # 3 components in the field
547 bc = mesh.computeCellCenterOfMass() # func is applied to barycenters of cells
548 bc1 = bc.getTuple(1) # coordinates of the second point
550 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
551 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
552 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
553 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
554 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_3]
557 def testExample_MEDCouplingCMesh_getCoordsAt(self):
558 #! [PySnippet_MEDCouplingCMesh_getCoordsAt_1]
560 x=DataArrayDouble(coords,3,1)
561 mesh=MEDCouplingCMesh()
562 mesh.setCoordsAt(0,x)
563 x2=mesh.getCoordsAt(0)
564 assert coords == x2.getValues()
565 #! [PySnippet_MEDCouplingCMesh_getCoordsAt_1]
568 def testExample_MEDCouplingUMesh_areCellsIncludedIn(self):
569 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_1]
570 mesh1=MEDCouplingUMesh()
571 mesh1.setMeshDimension(2)
572 mesh1.allocateCells(5)
573 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4]
574 mesh1.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
575 mesh1.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
576 mesh1.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
577 mesh1.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
578 mesh1.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
579 mesh1.finishInsertingCells()
580 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 ]
581 coordsArr=DataArrayDouble(coords,9,2)
582 mesh1.setCoords(coordsArr)
583 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_1]
584 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_2]
586 mesh2 = mesh1.buildPartOfMySelf(cells2, True ) # even cells selected
587 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_2]
588 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_3]
589 compType = 0 # the strongest policy
590 isOk, corr2to1 = mesh1.areCellsIncludedIn( mesh2, compType )
591 assert isOk # a larger mesh1 includes a smaller mesh2
592 assert corr2to1.getValues() == cells2
593 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_3]
594 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_4]
595 isOk, corr1to2 = mesh2.areCellsIncludedIn( mesh1, compType )
596 assert not isOk # the smaller mesh2 does NOT include the larger mesh1
597 assert corr1to2.getValues() == [2, 3, 1, 4, 0]
598 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_4]
600 def testExample_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells(self):
601 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_1]
602 # 2D coordinates of 5 base nodes
603 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2]
604 coordsArr=DataArrayDouble(coords,5,2)
605 # coordinates of 5 top nodes
606 coordsArr2 = coordsArr.deepCopy()
607 # 3D coordinates of base + top nodes
608 coordsArr = coordsArr.changeNbOfComponents( 3, 0 )
609 coordsArr2 = coordsArr2.changeNbOfComponents( 3, 1 )
610 coordsArr = DataArrayDouble.Aggregate([coordsArr,coordsArr2])
612 mesh=MEDCouplingUMesh()
613 mesh.setCoords(coordsArr)
614 mesh.setMeshDimension(3)
615 mesh.allocateCells(2)
616 # connectivity of reversed HEXA8 and PENTA6
617 conn=[0,1,4,3, 5,6,9,8, 1,2,4, 6,7,9]
618 mesh.insertNextCell(NORM_HEXA8, 8,conn[0:0+8])
619 mesh.insertNextCell(NORM_PENTA6,6,conn[8:8+6])
620 mesh.finishInsertingCells()
621 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_1]
622 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_2]
623 fixedCells = mesh.findAndCorrectBadOriented3DExtrudedCells()
624 assert len( fixedCells ) == 2 # 2 cells fixed
625 fixedCells = mesh.findAndCorrectBadOriented3DExtrudedCells()
626 assert len( fixedCells ) == 0 # no bad cells
627 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_2]
630 def testExample_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented(self):
631 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_1]
632 # 2D coordinates of 5 base nodes
633 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2]
634 coordsArr=DataArrayDouble(coords,5,2)
635 # coordinates of 5 top nodes
636 coordsArr2 = coordsArr.deepCopy()
637 # 3D coordinates of base + top nodes
638 coordsArr = coordsArr.changeNbOfComponents( 3, 0 )
639 coordsArr2 = coordsArr2.changeNbOfComponents( 3, 1 )
640 coordsArr = DataArrayDouble.Aggregate([coordsArr,coordsArr2])
642 mesh=MEDCouplingUMesh()
643 mesh.setCoords(coordsArr)
644 mesh.setMeshDimension(3)
645 mesh.allocateCells(2)
646 # connectivity of a HEXA8 + a reversed PENTA6
647 conn=[0,3,4,1, 5,8,9,6, 1,2,4, 6,7,9]
648 mesh.insertNextCell(NORM_POLYHED, 8,conn[0:0+8]) # "extruded" polyhedron
649 mesh.insertNextCell(NORM_POLYHED,6,conn[8:8+6])
650 mesh.finishInsertingCells()
651 # fix connectivity of NORM_POLYHED's
652 mesh.convertExtrudedPolyhedra()
653 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_1]
654 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_2]
655 badCells = mesh.arePolyhedronsNotCorrectlyOriented()
656 assert len( badCells ) == 1 # one polyhedron is KO
657 # fix invalid rolyherdons
658 mesh.orientCorrectlyPolyhedrons()
659 # re-check the orientation
660 badCells = mesh.arePolyhedronsNotCorrectlyOriented()
661 assert len( badCells ) == 0 # connectivity is OK
662 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_2]
665 def testExample_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented(self):
666 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_1]
667 mesh=MEDCouplingUMesh()
668 mesh.setMeshDimension(2)
669 mesh.allocateCells(5)
670 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4]
671 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
672 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
673 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
674 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
675 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
676 mesh.finishInsertingCells()
677 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 ]
678 coordsArr=DataArrayDouble(coords,9,2)
679 mesh.setCoords(coordsArr)
680 mesh.changeSpaceDimension(3)
681 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_1]
682 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_2]
684 badCellIds=mesh.are2DCellsNotCorrectlyOriented( vec, False )
685 assert len( badCellIds ) == 1 # one cell is reversed
687 mesh.orientCorrectly2DCells( vec, False )
688 # re-check orientation
689 badCellIds=mesh.are2DCellsNotCorrectlyOriented( vec, False )
690 assert len( badCellIds ) == 0 # the orientation is OK
691 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_2]
694 def testExample_MEDCouplingUMesh_getCellsContainingPoints(self):
695 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_1]
696 mesh=MEDCouplingUMesh()
697 mesh.setMeshDimension(2)
698 mesh.allocateCells(5)
699 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4]
700 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
701 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
702 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
703 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
704 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
705 mesh.finishInsertingCells()
706 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 ]
707 coordsArr=DataArrayDouble(coords,9,2)
708 mesh.setCoords(coordsArr)
709 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_1]
710 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_2]
711 pos = [ 10., 10, # point out of the mesh
712 0.3, 0.3, # point located somewhere inside the mesh
713 coords[2], coords[3]] # point at the node #1
714 eps = 1e-4 # ball radius
715 cells,cellsIndex=mesh.getCellsContainingPoints( pos, 3, eps )
716 assert cells.getValues() == [4, 0, 1]
717 assert cellsIndex.getValues() == [0, 0, 1, 3]
718 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_2]
722 def testExample_MEDCouplingUMesh_getCellsContainingPoint(self):
723 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_1]
724 mesh=MEDCouplingUMesh()
725 mesh.setMeshDimension(2)
726 mesh.allocateCells(5)
727 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4]
728 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
729 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
730 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
731 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14])
732 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18])
733 mesh.finishInsertingCells()
734 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 ]
735 coordsArr=DataArrayDouble(coords,9,2)
736 mesh.setCoords(coordsArr)
737 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_1]
738 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_2]
739 pos4 = coords[ 4*2 : ] # coordinates of the node #4
740 eps = 1e-4 # ball radius
741 pos = [ pos4[0]+eps, pos4[1]-eps ] # ball center
742 cellIds=mesh.getCellsContainingPoint( pos, eps )
743 assert len( cellIds ) == mesh.getNumberOfCells()
744 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_2]
748 def testExample_MEDCouplingUMesh_buildPartOrthogonalField(self):
749 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_1]
750 mesh=MEDCouplingUMesh()
751 mesh.setMeshDimension(2)
752 mesh.allocateCells(5)
753 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4]
754 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
755 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
756 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
757 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
758 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
759 mesh.finishInsertingCells()
760 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 ]
761 coordsArr=DataArrayDouble(coords,9,2)
762 mesh.setCoords(coordsArr)
763 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_1]
764 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_2]
765 part = DataArrayInt([1,2,3,4],4,1) # cell #0 is omitted
766 vecField=mesh.buildPartOrthogonalField( part )
767 vecArr = vecField.getArray()
768 assert len( vecArr ) == len( part )
769 assert vecArr.getNumberOfComponents() == 3
770 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_2]
773 def testExample_MEDCouplingUMesh_getPartMeasureField(self):
774 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_1]
775 mesh=MEDCouplingUMesh()
776 mesh.setMeshDimension(2)
777 mesh.allocateCells(5)
778 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4]
779 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
780 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
781 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
782 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
783 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
784 mesh.finishInsertingCells()
785 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 ]
786 coordsArr=DataArrayDouble(coords,9,2)
787 mesh.setCoords(coordsArr)
788 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_1]
789 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_2]
791 part = DataArrayInt([1,2,3,4],4,1) # cell #0 is omitted
792 areaArr=mesh.getPartMeasureField( isAbs, part )
793 assert areaArr[0] > 0 # orientation ignored
794 areaArr=mesh.getPartMeasureField( not isAbs, part )
795 assert areaArr[0] < 0 # orientation considered
796 assert len( areaArr ) == len( part )
797 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_2]
798 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_3]
799 part = DataArrayInt([1,2,3,4],4,1) # cell #0 is omitted
800 baryCenters = mesh.getPartBarycenterAndOwner( part )
801 assert len( baryCenters ) == len( part )
802 assert baryCenters.getNumberOfComponents() == mesh.getSpaceDimension()
803 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_3]
806 def testExample_MEDCouplingUMesh_getCellsInBoundingBox(self):
807 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_1]
808 mesh=MEDCouplingUMesh()
809 mesh.setMeshDimension(2)
810 coords=[0.,0., 0.,1., 1.,1]
811 coordsArr=DataArrayDouble(coords,3,2)
812 mesh.setCoords(coordsArr)
813 mesh.allocateCells(1)
815 mesh.insertNextCell(NORM_TRI3,3,conn)
816 mesh.finishInsertingCells()
817 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_1]
818 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_2]
819 bbox = [1., 1., 1.001,1.001] # xMin, xMax, yMin, yMax
820 cellsInBox = mesh.getCellsInBoundingBox( bbox, 0.0 )
821 assert cellsInBox.getValues() == []
822 cellsInBox = mesh.getCellsInBoundingBox( bbox, 0.1 )
823 assert cellsInBox.getValues() == [0]
824 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_2]
827 def testExample_MEDCouplingUMesh_renumberNodesInConn(self):
828 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_1]
829 mesh=MEDCouplingUMesh()
830 mesh.setMeshDimension(2)
831 mesh.allocateCells(1)
833 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
834 mesh.finishInsertingCells()
835 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_1]
836 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_2]
837 old2newIds = [-1,3,2,1,0]
838 mesh.renumberNodesInConn( old2newIds )
839 nodes0 = mesh.getNodeIdsOfCell( 0 )
840 assert nodes0 == [0,1,2,3]
841 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_2]
845 def testExample_MEDCouplingUMesh_renumberNodes(self):
846 #! [PySnippet_MEDCouplingUMesh_renumberNodes_1]
847 mesh=MEDCouplingUMesh()
848 mesh.setMeshDimension(2)
849 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.3]
850 coordsArr=DataArrayDouble(coords,4,2)
851 mesh.setCoords(coordsArr)
852 mesh.allocateCells(0)
853 mesh.finishInsertingCells()
854 #! [PySnippet_MEDCouplingUMesh_renumberNodes_1]
855 #! [PySnippet_MEDCouplingUMesh_renumberNodes_2]
856 mesh.renumberNodes([ 2,1,0,-1 ], 3)
857 coordsArr = mesh.getCoords() # get a shorten array
858 assert coordsArr.getValues() == [0.7,-0.3, 0.2,-0.3, -0.3,-0.3]
859 #! [PySnippet_MEDCouplingUMesh_renumberNodes_2]
860 #! [PySnippet_MEDCouplingUMesh_renumberNodes_3]
861 coordsArr.setValues(coords,4,2) # restore old nodes
862 mesh.renumberNodesCenter([ 2,1,0,2 ], 3)
863 coordsArr = mesh.getCoords() # get a shorten array
864 assert coordsArr.getValues() == [0.7,-0.3, 0.2,-0.3, -0.3,0.0]
865 #! [PySnippet_MEDCouplingUMesh_renumberNodes_3]
868 def testExample_MEDCouplingUMesh_findBoundaryNodes(self):
869 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_1]
870 mesh=MEDCouplingUMesh()
871 mesh.setMeshDimension(2)
872 mesh.allocateCells(5)
873 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
874 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
875 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
876 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
877 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14])
878 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18])
879 mesh.finishInsertingCells()
880 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 ]
881 coordsArr=DataArrayDouble(coords,9,2)
882 mesh.setCoords(coordsArr)
883 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_1]
884 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_2]
885 nodeIdsArr=mesh.findBoundaryNodes()
886 assert nodeIdsArr.getNumberOfTuples() == mesh.getNumberOfNodes() - 1
887 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_2]
890 def testExample_MEDCouplingUMesh_buildBoundaryMesh(self):
891 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_1]
892 mesh=MEDCouplingUMesh()
893 mesh.setMeshDimension(2)
894 mesh.allocateCells(5)
895 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
896 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
897 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
898 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
899 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14])
900 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18])
901 mesh.finishInsertingCells()
902 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 ]
903 coordsArr=DataArrayDouble(coords,9,2)
904 mesh.setCoords(coordsArr)
905 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_1]
906 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_2]
907 mesh1=mesh.buildBoundaryMesh(True)
908 mesh2=mesh.buildBoundaryMesh(False)
909 assert coordsArr.isEqual( mesh1.getCoords(), 1e-13 ) # same nodes
910 assert not coordsArr.isEqual( mesh2.getCoords(), 1e-13 ) # different nodes
911 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_2]
914 def testExample_MEDCouplingUMesh_buildFacePartOfMySelfNode(self):
915 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_1]
916 mesh=MEDCouplingUMesh()
917 mesh.setMeshDimension(2)
918 mesh.allocateCells(5)
919 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
920 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
921 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
922 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
923 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
924 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
925 mesh.finishInsertingCells()
926 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 ]
927 coordsArr=DataArrayDouble(coords,9,2)
928 mesh.setCoords(coordsArr)
929 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_1]
930 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_2]
931 nodeIds = mesh.getNodeIdsOfCell( 0 )
933 mesh1 = mesh.buildFacePartOfMySelfNode( nodeIds, allNodes )
934 assert mesh1.getNumberOfCells() == 4 # 4 segments bounding QUAD4 #0 only
935 mesh2 = mesh.buildFacePartOfMySelfNode( nodeIds, not allNodes )
936 assert mesh2.getNumberOfCells() > 4 # more segments added
937 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_2]
941 def testExample_MEDCouplingUMesh_buildPartOfMySelfNode(self):
942 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_1]
943 mesh=MEDCouplingUMesh()
944 mesh.setMeshDimension(2)
945 mesh.allocateCells(5)
946 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
947 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
948 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
949 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
950 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
951 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
952 mesh.finishInsertingCells()
953 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 ]
954 coordsArr=DataArrayDouble(coords,9,2)
955 mesh.setCoords(coordsArr)
956 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_1]
957 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_2]
958 nodeIds = mesh.getNodeIdsOfCell( 0 )
960 mesh1 = mesh.buildPartOfMySelfNode( nodeIds, allNodes )
961 mesh2 = mesh.buildPartOfMySelfNode( nodeIds, not allNodes )
962 assert mesh1.getNumberOfCells() == 1 # cell #0 is found only
963 assert mesh2.getNumberOfCells() == mesh.getNumberOfCells() # all cells are found
964 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_2]
968 def testExample_MEDCouplingUMesh_getCellIdsLyingOnNodes(self):
969 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_1]
970 mesh=MEDCouplingUMesh()
971 mesh.setMeshDimension(2)
972 mesh.allocateCells(5)
973 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
974 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
975 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
976 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
977 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
978 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
979 mesh.finishInsertingCells()
980 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 ]
981 coordsArr=DataArrayDouble(coords,9,2)
982 mesh.setCoords(coordsArr)
983 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_1]
984 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_2]
985 nodeIds = mesh.getNodeIdsOfCell( 0 )
987 cellIdsArr1 = mesh.getCellIdsLyingOnNodes( nodeIds, allNodes )
988 cellIdsArr2 = mesh.getCellIdsLyingOnNodes( nodeIds, not allNodes )
989 assert cellIdsArr1.getNumberOfTuples() == 1 # cell #0 is found only
990 assert cellIdsArr2.getNumberOfTuples() == mesh.getNumberOfCells() # all cells are found
991 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_2]
995 def testExample_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds(self):
996 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_1]
997 mesh=MEDCouplingUMesh()
998 mesh.setMeshDimension(2)
999 mesh.allocateCells(5)
1000 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1001 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
1002 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
1003 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
1004 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14])
1005 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18])
1006 mesh.finishInsertingCells()
1007 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 ]
1008 coordsArr=DataArrayDouble(coords,9,2)
1009 mesh.setCoords(coordsArr)
1010 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_1]
1011 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_2]
1013 nodeIds = mesh.getNodeIdsOfCell( cellIds[0] )
1014 nodeIds += mesh.getNodeIdsOfCell( cellIds[1] )
1015 cellIdsArr = mesh.getCellIdsFullyIncludedInNodeIds( nodeIds )
1016 assert cellIdsArr.getValues() == cellIds
1017 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_2]
1021 def testExample_MEDCouplingUMesh_buildPartOfMySelf(self):
1022 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_1]
1023 mesh=MEDCouplingUMesh()
1024 mesh.setMeshDimension(2)
1025 mesh.allocateCells(5)
1026 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1027 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
1028 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
1029 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
1030 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
1031 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
1032 mesh.finishInsertingCells()
1033 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 ]
1034 coordsArr=DataArrayDouble(coords,9,2)
1035 mesh.setCoords(coordsArr)
1036 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_1]
1037 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_2]
1039 mesh2=mesh.buildPartOfMySelf(cellIds, True)
1040 mesh3=mesh.buildPartOfMySelf(cellIds, False)
1041 coordsArr2 = mesh2.getCoords()
1042 assert coordsArr.isEqual( coordsArr2, 1e-13 ) # same nodes
1043 coordsArr3 = mesh3.getCoords()
1044 assert not coordsArr.isEqual( coordsArr3, 1e-13 ) # different nodes
1045 assert mesh2.getNodeIdsOfCell(0) == mesh.getNodeIdsOfCell( cellIds[0]) # cell #1 was copied
1046 assert mesh2.getNodeIdsOfCell(1) == mesh.getNodeIdsOfCell( cellIds[1]) # cell #2 was copied
1047 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_2]
1050 def testExample_MEDCouplingUMesh_mergeNodes(self):
1051 #! [PySnippet_MEDCouplingUMesh_mergeNodes_1]
1052 mesh=MEDCouplingUMesh()
1053 mesh.setMeshDimension(2)
1054 mesh.allocateCells(5)
1055 conn=[0,3,4,1, 1,4,2, 4,5,2]
1056 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
1057 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
1058 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
1059 mesh.finishInsertingCells()
1060 coords=[0.3,-0.301, # 0
1062 0.3,-0.302, # 2 ~~ 0
1066 coordsArr=DataArrayDouble(coords,6,2)
1067 mesh.setCoords(coordsArr)
1068 #! [PySnippet_MEDCouplingUMesh_mergeNodes_1]
1069 #! [PySnippet_MEDCouplingUMesh_mergeNodes_2]
1070 arr,areNodesMerged,newNbOfNodes=mesh.mergeNodes(0.004)
1071 assert arr.getValues() == [0, 1, 0, 2, 2, 0]
1072 assert areNodesMerged
1073 assert newNbOfNodes == 3
1074 #! [PySnippet_MEDCouplingUMesh_mergeNodes_2]
1075 #! [PySnippet_MEDCouplingUMesh_mergeNodes_3]
1076 baryCoords2 = coords[2*2:] # initial coordinates of node #2
1077 coordsArr = mesh.getCoords() # retrieve a new shorten coord array
1078 self.assertNotAlmostEqual( baryCoords2[1], coordsArr.getIJ(0,1), 13 ) # Y of node #0 differs from that of baryCoords2
1079 # restore coordinates
1080 coordsArr = DataArrayDouble(coords,6,2)
1081 mesh.setCoords(coordsArr)
1082 # call mergeNodesCenter()
1083 mesh.mergeNodesCenter(0.004)
1084 coordsArr = mesh.getCoords() # retrieve a new shorten coord array
1085 self.assertAlmostEqual( baryCoords2[1], coordsArr.getIJ(0,1), 13 ) # Y of node #0 equals to that of baryCoords2
1086 #! [PySnippet_MEDCouplingUMesh_mergeNodes_3]
1089 def testExample_MEDCouplingUMesh_zipConnectivityTraducer(self):
1090 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_1]
1091 mesh=MEDCouplingUMesh()
1092 mesh.setMeshDimension(2)
1093 mesh.allocateCells(5)
1094 conn=[0,3,4,1, 1,4,2]
1095 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
1096 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
1097 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 2 == 1
1098 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 3 == 0
1099 mesh.insertNextCell(NORM_QUAD4,4,conn[2:4]+conn[0:2]) # 4 ~~ 0
1100 mesh.finishInsertingCells()
1101 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 ]
1102 coordsArr=DataArrayDouble(coords,9,2)
1103 mesh.setCoords(coordsArr)
1104 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_1]
1105 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_2]
1106 oldNbCells = mesh.getNumberOfCells()
1107 arr = mesh.zipConnectivityTraducer(0)
1108 assert mesh.getNumberOfCells() == oldNbCells-2
1109 assert arr.getValues() == [0, 1, 1, 0, 2]
1110 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_2]
1113 def testExample_MEDCouplingUMesh_zipCoordsTraducer(self):
1114 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_1]
1115 mesh=MEDCouplingUMesh()
1116 mesh.setMeshDimension(2)
1117 mesh.allocateCells(5)
1118 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1119 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
1120 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
1121 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
1122 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14])
1123 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18])
1124 mesh.finishInsertingCells()
1125 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 ]
1126 coordsArr=DataArrayDouble(coords,9,2)
1127 mesh.setCoords(coordsArr)
1128 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_1]
1129 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_2]
1131 mesh2=mesh.buildPartOfMySelf(cellIds,True)
1132 arr=mesh2.zipCoordsTraducer()
1133 assert mesh2.getNumberOfNodes() == 4 # nb of nodes decreased
1134 assert arr.getValues() == [-1,0,1,-1,2,3,-1,-1,-1] # -1 for unused nodes
1135 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_2]
1138 def testExample_MEDCouplingUMesh_getNodeIdsInUse(self):
1139 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_1]
1140 mesh=MEDCouplingUMesh()
1141 mesh.setMeshDimension(2)
1142 mesh.allocateCells(5)
1143 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1144 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
1145 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
1146 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
1147 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14])
1148 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18])
1149 mesh.finishInsertingCells()
1150 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 ]
1151 coordsArr=DataArrayDouble(coords,9,2)
1152 mesh.setCoords(coordsArr)
1153 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_1]
1154 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_2]
1156 mesh2=mesh.buildPartOfMySelf(cellIds,True)
1157 arr,newNbOfNodes=mesh2.getNodeIdsInUse()
1158 assert arr.getValues() == [-1,0,1,-1,2,3,-1,-1,-1]
1159 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_2]
1160 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_3]
1161 arr2=arr.invertArrayO2N2N2O(newNbOfNodes)
1162 assert arr2.getValues() == [1,2,4,5]
1163 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_3]
1166 def testExample_MEDCouplingUMesh_convertToPolyTypes(self):
1167 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_1]
1168 mesh=MEDCouplingUMesh()
1169 mesh.setMeshDimension(2)
1170 mesh.allocateCells(5)
1171 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1172 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
1173 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
1174 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
1175 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
1176 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
1177 mesh.finishInsertingCells()
1178 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 ]
1179 coordsArr=DataArrayDouble(coords,9,2)
1180 mesh.setCoords(coordsArr)
1181 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_1]
1182 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_2]
1184 mesh.convertToPolyTypes(cells)
1185 assert mesh.getTypeOfCell(0) == NORM_QUAD4
1186 assert mesh.getTypeOfCell(1) == NORM_POLYGON, mesh.getTypeOfCell(1)
1187 assert mesh.getTypeOfCell(2) == NORM_TRI3
1188 assert mesh.getTypeOfCell(3) == NORM_POLYGON
1189 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_2]
1192 def testExample_MEDCouplingUMesh_buildDescendingConnectivity2(self):
1193 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_1]
1194 mesh=MEDCouplingUMesh()
1195 mesh.setMeshDimension(2)
1196 mesh.allocateCells(5)
1197 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1198 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
1199 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
1200 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
1201 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
1202 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
1203 mesh.finishInsertingCells()
1204 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 ]
1205 coordsArr=DataArrayDouble(coords,9,2)
1206 mesh.setCoords(coordsArr)
1207 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_1]
1208 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_2]
1209 mesh2,desc,descIndx,revDesc,revDescIndx=mesh.buildDescendingConnectivity2()
1210 assert desc.getValues() == [1,2,3,4,-3,5,6, 7,8,-5,9,10,-2,11, 12,13,-7,-10]
1211 assert descIndx.getValues() == [0,4,7,10,14,18]
1212 assert revDesc.getValues() == [0, 0,3, 0,1, 0, 1,2, 1, 2,4, 2, 3, 3,4, 3, 4, 4]
1213 assert revDescIndx.getValues() == [0,1,3,5,6,8,9,11,12,13,15,16,17,18]
1214 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_2]
1215 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_3]
1216 assert mesh2.getNodeIdsOfCell( 3-1 ) == [4, 1] # cell #3 in FORTRAN mode
1217 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_3]
1220 def testExample_MEDCouplingUMesh_buildDescendingConnectivity(self):
1221 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_1]
1222 mesh=MEDCouplingUMesh()
1223 mesh.setMeshDimension(2)
1224 mesh.allocateCells(5)
1225 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1226 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
1227 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
1228 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
1229 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
1230 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
1231 mesh.finishInsertingCells()
1232 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 ]
1233 coordsArr=DataArrayDouble(coords,9,2)
1234 mesh.setCoords(coordsArr)
1235 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_1]
1236 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_2]
1237 mesh2,desc,descIndx,revDesc,revDescIndx=mesh.buildDescendingConnectivity()
1238 assert desc.getValues() == [0,1,2,3, 2,4,5, 6,7,4, 8,9,1,10, 11,12,6,9]
1239 assert descIndx.getValues() == [0,4,7,10,14,18]
1240 assert revDesc.getValues() == [0, 0,3, 0,1, 0, 1,2, 1, 2,4, 2, 3, 3,4, 3, 4, 4]
1241 assert revDescIndx.getValues() == [0,1,3,5,6,8,9,11,12,13,15,16,17,18]
1242 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_2]
1245 def testExample_MEDCouplingUMesh_getReverseNodalConnectivity(self):
1246 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_1]
1247 mesh=MEDCouplingUMesh()
1248 mesh.setMeshDimension(2)
1249 mesh.allocateCells(5)
1250 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1251 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
1252 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
1253 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
1254 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
1255 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
1256 mesh.finishInsertingCells()
1257 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 ]
1258 coordsArr=DataArrayDouble(coords,9,2)
1259 mesh.setCoords(coordsArr)
1260 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_1]
1261 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_2]
1262 revNodal,revNodalIndx=mesh.getReverseNodalConnectivity()
1263 assert revNodal.getValues() == [0,0,1,1,2,0,3,0,1,2,3,4,2,4,3,3,4,4]
1264 assert revNodalIndx.getValues() == [0,1,3,5,7,12,14,15,17,18]
1265 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_2]
1268 def testExample_MEDCouplingUMesh_checkDeepEquivalWith(self):
1269 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_1]
1271 mesh1=MEDCouplingUMesh()
1272 mesh1.setMeshDimension(2)
1277 coordsArr=DataArrayDouble(coords,4,2)
1278 mesh1.setCoords(coordsArr)
1279 mesh1.allocateCells(2)
1280 mesh1.insertNextCell(NORM_TRI3,3,[0,1,2]) #0
1281 mesh1.insertNextCell(NORM_TRI3,3,[1,2,3]) #1
1282 mesh1.finishInsertingCells()
1284 mesh2=MEDCouplingUMesh()
1285 mesh2.setMeshDimension(2)
1286 coords=[0.0,1.0, #0 = #3
1290 coordsArr2=DataArrayDouble(coords,4,2)
1291 mesh2.setCoords(coordsArr2)
1292 mesh2.allocateCells(2)
1293 mesh2.insertNextCell(NORM_TRI3,3,[2,3,0]) #0 = #1
1294 mesh2.insertNextCell(NORM_TRI3,3,[3,1,2]) #1 ~ #0
1295 mesh2.finishInsertingCells()
1296 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_1]
1297 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_2]
1298 cellCompPol = 1 # "permuted same orientation" - policy of medium severity
1299 cOld2New, nOld2New = mesh1.checkDeepEquivalWith( mesh2, cellCompPol, 0.002 )
1300 assert nOld2New.getValues() == [3, 0, 1, 2]
1301 assert cOld2New.getValues() == [1, 0]
1302 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_2]
1303 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_3]
1304 self.assertRaises( InterpKernelException, mesh1.checkDeepEquivalOnSameNodesWith, mesh2, cellCompPol, 0.002)
1305 mesh2.setCoords(coordsArr) # make meshes share the same coordinates array
1306 mesh2.allocateCells(2)
1307 mesh2.insertNextCell(NORM_TRI3,3,[1,2,3]) #0 = #1
1308 mesh2.insertNextCell(NORM_TRI3,3,[1,0,2]) #1 ~ #0
1309 mesh2.finishInsertingCells()
1310 cellCompPol = 2 # the weakest policy
1311 mesh1.checkDeepEquivalOnSameNodesWith( mesh2, cellCompPol, 0 )
1312 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_3]
1315 def testExample_MEDCouplingPointSet_scale(self):
1316 #! [PySnippet_MEDCouplingPointSet_scale_1]
1317 coords=[0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0] # 2D coordinates of 4 nodes
1318 coordsArr=DataArrayDouble(coords,4,2)
1319 mesh=MEDCouplingUMesh()
1320 mesh.setCoords(coordsArr)
1321 initCoords = coordsArr.deepCopy()
1322 #! [PySnippet_MEDCouplingPointSet_scale_1]
1323 #! [PySnippet_MEDCouplingPointSet_scale_2]
1326 mesh.scale(center,factor)
1327 #! [PySnippet_MEDCouplingPointSet_scale_2]
1328 #! [PySnippet_MEDCouplingPointSet_scale_3]
1329 coords2 = mesh.getCoords()
1330 assert coords2.isEqualWithoutConsideringStr( initCoords, 1.0 )
1331 assert not coords2.isEqualWithoutConsideringStr( initCoords, 0.9 )
1332 #! [PySnippet_MEDCouplingPointSet_scale_3]
1335 def testExample_MEDCouplingPointSet_translate(self):
1336 #! [PySnippet_MEDCouplingPointSet_translate_1]
1337 coords=[0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0] # 2D coordinates of 4 nodes
1338 coordsArr=DataArrayDouble(coords,4,2)
1339 mesh=MEDCouplingUMesh()
1340 mesh.setCoords(coordsArr)
1341 initCoords = coordsArr.deepCopy()
1342 #! [PySnippet_MEDCouplingPointSet_translate_1]
1343 #! [PySnippet_MEDCouplingPointSet_translate_2]
1345 mesh.translate(vector)
1346 #! [PySnippet_MEDCouplingPointSet_translate_2]
1347 #! [PySnippet_MEDCouplingPointSet_translate_3]
1348 coords2 = mesh.getCoords()
1349 assert coords2.isEqualWithoutConsideringStr( initCoords, 1 )
1350 assert not coords2.isEqualWithoutConsideringStr( initCoords, 0.9 )
1351 #! [PySnippet_MEDCouplingPointSet_translate_3]
1354 def testExample_MEDCouplingPointSet_rotate(self):
1355 #! [PySnippet_MEDCouplingPointSet_rotate_1]
1356 coords=[0.0,0.0, 0.1,0.0, 0.1,0.1, 0.0,0.1] # 2D coordinates of 4 nodes
1357 coordsArr=DataArrayDouble(coords,4,2)
1358 mesh=MEDCouplingUMesh()
1359 mesh.setCoords(coordsArr)
1360 #! [PySnippet_MEDCouplingPointSet_rotate_1]
1361 #! [PySnippet_MEDCouplingPointSet_rotate_2]
1363 mesh.rotate(center,-pi/2)
1364 #! [PySnippet_MEDCouplingPointSet_rotate_2]
1365 #! [PySnippet_MEDCouplingPointSet_rotate_3]
1366 mesh.changeSpaceDimension(3)
1369 mesh.rotate(center,vector,pi/2)
1370 #! [PySnippet_MEDCouplingPointSet_rotate_3]
1371 #! [PySnippet_MEDCouplingPointSet_rotate_4]
1372 mesh.changeSpaceDimension(2)
1373 coords2 = mesh.getCoords()
1374 for i,c in enumerate( coords ):
1375 self.assertAlmostEqual( c, coords2.getIJ(0,i), 13 )
1376 #! [PySnippet_MEDCouplingPointSet_rotate_4]
1379 def testExample_MEDCouplingPointSet_getBoundingBox(self):
1380 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_1]
1381 cc=[0.0, 0.1, 0.2, # 3D coordinates of 2 nodes
1383 coordsArr=DataArrayDouble(cc,2,3)
1384 mesh=MEDCouplingUMesh()
1385 mesh.setCoords(coordsArr)
1386 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_1]
1387 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_2]
1388 bbox=mesh.getBoundingBox()
1389 assert bbox == [( cc[0], cc[3] ), # NOTE: list of 3 tuples is retirned!
1392 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_2]
1394 def testExample_MEDCouplingPointSet_getNodeIdsNearPoint(self):
1395 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_1]
1396 # 2D coordinates of 5 nodes
1397 coords=[0.3,-0.301, # 0
1401 0.3,-0.30299999999999]# 4
1402 coordsArr=DataArrayDouble(coords,5,2)
1403 mesh=MEDCouplingUMesh()
1404 mesh.setCoords(coordsArr)
1405 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_1]
1406 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_2]
1407 point=[0.3, -0.3] # point close to nodes #0, #2 and #4
1408 ids=mesh.getNodeIdsNearPoint(point,0.003)
1409 assert ids.getValues() == [0,2,4]
1410 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_2]
1413 def testExample_MEDCouplingPointSet_getNodeIdsNearPoints(self):
1414 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_1]
1415 # 2D coordinates of 7 nodes
1416 coords=[0.3,-0.301, # 0
1423 coordsArr=DataArrayDouble(coords,7,2)
1424 mesh=MEDCouplingUMesh()
1425 mesh.setCoords(coordsArr)
1426 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_1]
1427 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_2]
1428 points=[0.2,-0.301, # ~ node #1
1430 1.1, 0.002] # ~ nodes #3, #4 and #5
1431 ids,idsIndex=mesh.getNodeIdsNearPoints(points,3,0.003)
1432 assert ids.getValues() == [1, 3, 4, 5]
1433 assert idsIndex.getValues() == [0, 1, 1, 4]
1434 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_2]
1437 def testExample_MEDCouplingPointSet_findCommonNodes(self):
1438 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_1]
1439 coords=[0.3,-0.301, # 0
1445 coordsArr=DataArrayDouble(coords,6,2)
1446 mesh=MEDCouplingUMesh()
1447 mesh.setCoords(coordsArr)
1448 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_1]
1449 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_2]
1450 comm,commI=mesh.findCommonNodes(1e-13)
1451 assert comm.getValues() == [3,4]
1452 comm,commI=mesh.findCommonNodes(0.004)
1453 assert comm.getValues() == [0,2,5,3,4]
1454 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_2]
1457 def testExample_MEDCouplingPointSet_getCoordinatesOfNode(self):
1458 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_1]
1459 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3]
1460 coordsArr=DataArrayDouble(coords,3,2)
1461 mesh=MEDCouplingUMesh()
1462 mesh.setCoords(coordsArr)
1463 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_1]
1464 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_2]
1465 nodeCoords=mesh.getCoordinatesOfNode(1)
1466 self.assertAlmostEqual(0.2, nodeCoords[0],13)
1467 self.assertAlmostEqual(-0.3,nodeCoords[1],13)
1468 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_2]
1471 def testExample_DataArrayInt_getTuple(self):
1472 #! [Snippet_DataArrayInt_getTuple_1]
1477 assert dv.getTuple( 1 ) == [9,10]
1478 #! [Snippet_DataArrayInt_getTuple_1]
1479 #! [Snippet_DataArrayInt_getTuple_2]
1482 #! [Snippet_DataArrayInt_getTuple_2]
1485 def testExample_DataArrayInt_buildPermutationArr(self):
1486 #! [PySnippet_DataArrayInt_buildPermutationArr_1]
1488 a.setValues([4,5,6,7,8],5,1)
1490 b.setValues([5,4,8,6,7],5,1)
1491 c=a.buildPermutationArr(b)
1492 #! [PySnippet_DataArrayInt_buildPermutationArr_1]
1493 self.assertEqual([1,0,4,2,3],c.getValues())
1496 def testExample_DataArrayInt_invertArrayO2N2N2O(self):
1497 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
1500 da.setValues(arr1,6,1)
1501 da2=da.invertArrayO2N2N2O(6)
1502 expected1=[1,3,0,5,2,4]
1504 self.assertEqual(expected1[i],da2.getIJ(i,0))
1506 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
1509 def testExample_DataArrayInt_invertArrayN2O2O2N(self):
1510 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
1513 da.setValues(arr1,6,1)
1514 da2=da.invertArrayN2O2O2N(7)
1515 expected1=[1,3,0,5,2,4,-1]
1517 self.assertEqual(expected1[i],da2.getIJ(i,0))
1519 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
1523 def testExample_DataArrayDouble_getIdsInRange(self):
1524 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
1525 da=DataArrayDouble()
1527 da[ :, :] = list(range(10))
1528 da2 = da.findIdsInRange( 2.5, 6 )
1529 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
1532 def testExample_DataArrayDouble_setPartOfValues2(self):
1533 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
1534 da=DataArrayDouble()
1537 dv=DataArrayDouble()
1541 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
1542 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
1544 da[ [0,1,2], [1,3] ] = dv
1545 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
1546 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
1549 da[ [0,2,3], [0,2,3,4,5,6]] = dv
1550 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
1553 def testExample_DataArrayInt_setPartOfValues2(self):
1554 #! [Snippet_DataArrayInt_setPartOfValues2_1]
1562 #! [Snippet_DataArrayInt_setPartOfValues2_1]
1563 #! [Snippet_DataArrayInt_setPartOfValues2_2]
1565 da[ [0,1,2], [1,3] ] = dv
1566 #! [Snippet_DataArrayInt_setPartOfValues2_2]
1567 #! [Snippet_DataArrayInt_setPartOfValues2_3]
1570 da[ [0,2,3], [0,2,3,4,5,6]] = dv
1571 #! [Snippet_DataArrayInt_setPartOfValues2_3]
1574 def testExample_DataArrayDouble_setPartOfValues3(self):
1575 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
1576 da=DataArrayDouble()
1579 dv=DataArrayDouble()
1583 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
1584 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
1586 da[ 0:3, [1,3] ] = dv
1587 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
1588 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
1591 da[ 0:4:2, [0,2,3,4,5,6]] = dv
1592 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
1595 def testExample_DataArrayInt_setPartOfValues3(self):
1596 #! [Snippet_DataArrayInt_setPartOfValues3_1]
1604 #! [Snippet_DataArrayInt_setPartOfValues3_1]
1605 #! [Snippet_DataArrayInt_setPartOfValues3_2]
1607 da[ 0:3, [1,3] ] = dv
1608 #! [Snippet_DataArrayInt_setPartOfValues3_2]
1609 #! [Snippet_DataArrayInt_setPartOfValues3_3]
1612 da[ 0:4:2, [0,2,3,4,5,6]] = dv
1613 #! [Snippet_DataArrayInt_setPartOfValues3_3]
1616 def testExample_DataArrayDouble_setPartOfValues1(self):
1617 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
1618 da=DataArrayDouble()
1620 da.setInfoOnComponents( ["v1","v2","v3","v4"])
1622 dv=DataArrayDouble()
1626 dv.setInfoOnComponents( ["a1","a2"])
1627 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
1628 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
1630 da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
1631 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
1632 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
1634 da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
1635 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
1636 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
1638 da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
1639 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
1640 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
1642 da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
1643 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
1644 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
1647 da2[ 0:3:2, 1:4:2 ] = dv
1648 self.assertTrue( da.isEqual( da2, 1e-20 ))
1649 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
1652 def testExample_DataArrayInt_setPartOfValues1(self):
1653 #! [Snippet_DataArrayInt_setPartOfValues1_1]
1656 da.setInfoOnComponents( ["v1","v2","v3","v4"])
1662 dv.setInfoOnComponents( ["a1","a2"])
1663 #! [Snippet_DataArrayInt_setPartOfValues1_1]
1664 #! [Snippet_DataArrayInt_setPartOfValues1_2]
1666 da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
1667 #! [Snippet_DataArrayInt_setPartOfValues1_2]
1668 #! [Snippet_DataArrayInt_setPartOfValues1_3]
1670 da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
1671 #! [Snippet_DataArrayInt_setPartOfValues1_3]
1672 #! [Snippet_DataArrayInt_setPartOfValues1_4]
1674 da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
1675 #! [Snippet_DataArrayInt_setPartOfValues1_4]
1676 #! [Snippet_DataArrayInt_setPartOfValues1_5]
1678 da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
1679 #! [Snippet_DataArrayInt_setPartOfValues1_5]
1680 #! [Snippet_DataArrayInt_setPartOfValues1_6]
1683 da2[ 0:3:2, 1:4:2 ] = dv
1684 self.assertTrue( da.isEqual( da2 ))
1685 #! [Snippet_DataArrayInt_setPartOfValues1_6]
1688 def testExample_DataArrayDouble_setPartOfValuesSimple1(self):
1689 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
1690 da=DataArrayDouble()
1693 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
1694 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
1696 da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
1697 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
1698 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
1700 da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
1701 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
1702 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
1704 da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
1705 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
1706 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
1708 da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
1709 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
1710 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
1713 da2[ 0:3:2, 1:4:2 ] = dv
1714 self.assertTrue( da.isEqual( da2, 1e-20 ))
1715 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
1718 def testExample_DataArrayInt_setPartOfValuesSimple1(self):
1719 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
1723 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
1724 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
1726 da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
1727 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
1728 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
1730 da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
1731 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
1732 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
1734 da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
1735 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
1736 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
1738 da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
1739 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
1740 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
1743 da2[ 0:3:2, 1:4:2 ] = dv
1744 self.assertTrue( da.isEqual( da2 ))
1745 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
1748 def testExample_DataArrayDouble_setPartOfValuesSimple2(self):
1749 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
1750 da=DataArrayDouble()
1753 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
1754 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
1756 da[[1,2], [1,2]] = dv
1757 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
1758 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
1760 da[[0,1,2,3], [1]] = dv
1761 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
1762 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
1764 da[[1], [0,1,2,3]] = dv
1765 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
1766 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
1768 da[[0,2], [1,3]] = dv
1769 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
1772 def testExample_DataArrayInt_setPartOfValuesSimple2(self):
1773 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
1777 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
1778 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
1780 da[[1,2], [1,2]] = dv
1781 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
1782 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
1784 da[[0,1,2,3], [1]] = dv
1785 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
1786 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
1788 da[[1], [0,1,2,3]] = dv
1789 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
1790 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
1792 da[[0,2], [1,3]] = dv
1793 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
1796 def testExample_DataArrayDouble_setPartOfValuesSimple3(self):
1797 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
1798 da=DataArrayDouble()
1801 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
1802 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
1805 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
1806 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
1808 da[[0,1,2,3], 1:2] = dv
1809 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
1810 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
1813 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
1814 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
1816 da[[0,2], 1:4:2] = dv
1817 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
1820 def testExample_DataArrayInt_setPartOfValuesSimple3(self):
1821 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
1825 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
1826 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
1829 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
1830 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
1832 da[[0,1,2,3], 1:2] = dv
1833 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
1834 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
1837 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
1838 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
1840 da[[0,2], 1:4:2] = dv
1841 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
1844 def testExample_DataArrayDouble_setSelectedComponents(self):
1845 #! [Snippet_DataArrayDouble_setSelectedComponents1]
1846 array1=[1.,2., 3.,4., 5.,6.]
1847 da=DataArrayDouble(array1,3,2)
1848 da.setInfoOnComponents( ["a1","a2"])
1849 #! [Snippet_DataArrayDouble_setSelectedComponents1]
1850 #! [Snippet_DataArrayDouble_setSelectedComponents2]
1851 dv=DataArrayDouble()
1854 dv.setInfoOnComponents( ["v1","v2","v3","v4"])
1856 dv.setSelectedComponents( da, [1,0] )
1857 #! [Snippet_DataArrayDouble_setSelectedComponents2]
1858 #! [Snippet_DataArrayDouble_setSelectedComponents3]
1860 self.assertTrue( dv.isEqualWithoutConsideringStr( dv2, 1e-20 ))
1861 #! [Snippet_DataArrayDouble_setSelectedComponents3]
1864 def testExample_DataArrayInt_setSelectedComponents(self):
1865 #! [Snippet_DataArrayInt_setSelectedComponents1]
1867 array1=[1,2, 3,4, 5,6]
1868 da.setValues(array1,3,2)
1869 da.setInfoOnComponents( ["a1","a2"])
1870 #! [Snippet_DataArrayInt_setSelectedComponents1]
1871 #! [Snippet_DataArrayInt_setSelectedComponents2]
1875 dv.setInfoOnComponents( ["v1","v2","v3","v4"])
1877 dv.setSelectedComponents( da, [1,0] )
1878 #! [Snippet_DataArrayInt_setSelectedComponents2]
1879 #! [Snippet_DataArrayInt_setSelectedComponents3]
1881 self.assertTrue( dv.isEqualWithoutConsideringStr( dv2 ))
1882 #! [Snippet_DataArrayInt_setSelectedComponents3]
1885 def testExample_DataArrayDouble_getDifferentValues(self):
1886 #! [Snippet_DataArrayDouble_getDifferentValues1]
1887 array1=[2.3,1.2,1.3,2.3,2.301,0.8]
1888 da=DataArrayDouble(array1,6,1)
1890 dv=da.getDifferentValues(2e-1)
1891 expected2=[2.301,1.3,0.8]
1892 self.assertEqual(3,dv.getNbOfElems())
1894 self.assertAlmostEqual(expected2[i],dv.getIJ(i,0),14)
1896 #! [Snippet_DataArrayDouble_getDifferentValues1]
1899 def testExample_DataArrayDouble_findCommonTuples1(self):
1900 #! [PySnippet_DataArrayDouble_findCommonTuples1]
1901 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]
1902 da=DataArrayDouble(array2,6,2)
1903 #! [PySnippet_DataArrayDouble_findCommonTuples1]
1904 #! [PySnippet_DataArrayDouble_findCommonTuples2]
1905 c,cI=da.findCommonTuples(1.01e-1)
1906 expected3=[0,3,4,1,2]
1908 self.assertEqual(expected3,c.getValues())
1909 self.assertEqual(expected4,cI.getValues())
1910 #! [PySnippet_DataArrayDouble_findCommonTuples2]
1913 def testExampleDataArrayDoubleMeldWith(self):
1914 #! [PySnippet_DataArrayDouble_Meld1_1]
1915 da1=DataArrayDouble()
1917 da2=DataArrayDouble()
1920 da1.fillWithValue(7.)
1922 da3=da2.applyFunc(3,"10*x*IVec+100*x*JVec+1000*x*KVec")
1924 da1.setInfoOnComponent(0,"c0da1")
1925 da1.setInfoOnComponent(1,"c1da1")
1926 da3.setInfoOnComponent(0,"c0da3")
1927 da3.setInfoOnComponent(1,"c1da3")
1928 da3.setInfoOnComponent(2,"c2da3")
1932 #! [PySnippet_DataArrayDouble_Meld1_1]
1934 def testExampleDataArrayIntMeldWith(self):
1935 #! [PySnippet_DataArrayInt_Meld1_1]
1941 da1.fillWithValue(7)
1944 da1.setInfoOnComponent(0,"c0da1")
1945 da1.setInfoOnComponent(1,"c1da1")
1946 da2.setInfoOnComponent(0,"c0da2")
1949 #! [PySnippet_DataArrayInt_Meld1_1]
1951 def testExampleDataArrayDoubleKeepSelectedComponents1(self):
1952 #! [SnippeDataArrayDoubleKeepSelectedComponents1_1]
1953 arr1=[1.,2.,3.,4., # tuple 0
1954 11.,12.,13.,14., # tuple 1
1955 21.,22.,23.,24., # ...
1958 a1=DataArrayDouble(arr1,5,4)
1959 a1.setInfoOnComponent(0,"a")
1960 a1.setInfoOnComponent(1,"b")
1961 a1.setInfoOnComponent(2,"c")
1962 a1.setInfoOnComponent(3,"d")
1963 #! [SnippeDataArrayDoubleKeepSelectedComponents1_1]
1964 #! [SnippeDataArrayDoubleKeepSelectedComponents1_2]
1966 a2=a1.keepSelectedComponents(arr2V)
1967 #! [SnippeDataArrayDoubleKeepSelectedComponents1_2]
1970 def testExampleDataArrayIntKeepSelectedComponents1(self):
1971 #! [SnippeDataArrayIntKeepSelectedComponents1_1]
1972 arr1=[1,2,3,4, # tuple 0
1973 11,12,13,14, # tuple 1
1978 a1.setValues(arr1,5,4)
1979 a1.setInfoOnComponent(0,"a")
1980 a1.setInfoOnComponent(1,"b")
1981 a1.setInfoOnComponent(2,"c")
1982 a1.setInfoOnComponent(3,"d")
1983 #! [SnippeDataArrayIntKeepSelectedComponents1_1]
1984 #! [SnippeDataArrayIntKeepSelectedComponents1_2]
1986 a2=a1.keepSelectedComponents(arr2V)
1987 #! [SnippeDataArrayIntKeepSelectedComponents1_2]
1988 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
1990 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
1993 def testExampleFieldDoubleBuildSubPart1(self):
1994 from MEDCouplingDataForTest import MEDCouplingDataForTest
1995 #! [PySnippetFieldDoubleBuildSubPart1_1]
1996 mesh1=MEDCouplingDataForTest.build2DTargetMesh_1()
1997 f1=MEDCouplingFieldDouble(ON_CELLS,ONE_TIME)
2000 arr1=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.]
2001 array=DataArrayDouble(arr1,mesh1.getNumberOfCells(),2)
2003 # ! [PySnippetFieldDoubleBuildSubPart1_1]
2004 # ! [PySnippetFieldDoubleBuildSubPart1_2]
2006 f2=f1.buildSubPart(part1)
2007 # ! [PySnippetFieldDoubleBuildSubPart1_2]
2009 self.assertEqual(3,f2.getNumberOfTuples())
2010 self.assertEqual(2,f2.getNumberOfComponents())
2011 expected1=[5.,105.,4.,104.,7.,107.]
2013 self.assertAlmostEqual(f2.getIJ(0,i),expected1[i],12)
2015 self.assertEqual(3,f2.getMesh().getNumberOfCells())
2016 self.assertEqual(6,f2.getMesh().getNumberOfNodes())
2017 self.assertEqual(2,f2.getMesh().getSpaceDimension())
2018 self.assertEqual(2,f2.getMesh().getMeshDimension())
2020 self.assertEqual(13,m2C.getNodalConnectivityArrayLen())
2021 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]
2023 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
2025 expected3=[3,2,3,1,3,0,2,1,4,4,5,3,2]
2026 self.assertEqual(expected3,list(m2C.getNodalConnectivity().getValues()))
2027 expected4=[0,4,8,13]
2028 self.assertEqual(expected4,list(m2C.getNodalConnectivityIndex().getValues()))
2029 # Test with field on nodes.
2030 # ! [PySnippetFieldDoubleBuildSubPart1_3]
2031 f1=MEDCouplingFieldDouble(ON_NODES,ONE_TIME)
2034 arr2=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.,8.,108.,9.,109.,10.,110.,11.,111.]
2035 array=DataArrayDouble(arr2,mesh1.getNumberOfNodes(),2)
2037 # ! [PySnippetFieldDoubleBuildSubPart1_3]
2038 # ! [PySnippetFieldDoubleBuildSubPart1_4]
2040 f2=f1.buildSubPart(part2)
2041 # ! [PySnippetFieldDoubleBuildSubPart1_4]
2042 self.assertEqual(4,f2.getNumberOfTuples())
2043 self.assertEqual(2,f2.getNumberOfComponents())
2044 expected5=[4.,104.,5.,105.,7.,107.,8.,108.]
2046 self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
2048 self.assertEqual(2,f2.getMesh().getNumberOfCells())
2049 self.assertEqual(4,f2.getMesh().getNumberOfNodes())
2050 self.assertEqual(2,f2.getMesh().getSpaceDimension())
2051 self.assertEqual(2,f2.getMesh().getMeshDimension())
2053 self.assertEqual(8,m2C.getNodalConnectivityArrayLen())
2054 for i in range(8): # 8 is not an error
2055 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
2057 self.assertEqual(expected3[:4],[int(i) for i in m2C.getNodalConnectivity()][4:])
2058 self.assertEqual(expected3[4:8],[int(i) for i in m2C.getNodalConnectivity()][:4])
2059 self.assertEqual(expected4[:3],[int(i) for i in m2C.getNodalConnectivityIndex()])
2060 #idem previous because nodes of cell#4 are not fully present in part3
2063 arrr.setValues(part3,2,1)
2064 f2=f1.buildSubPart(arrr)
2065 self.assertEqual(4,f2.getNumberOfTuples())
2066 self.assertEqual(2,f2.getNumberOfComponents())
2068 self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
2070 self.assertEqual(2,f2.getMesh().getNumberOfCells())
2071 self.assertEqual(4,f2.getMesh().getNumberOfNodes())
2072 self.assertEqual(2,f2.getMesh().getSpaceDimension())
2073 self.assertEqual(2,f2.getMesh().getMeshDimension())
2075 self.assertEqual(8,m2C.getNodalConnectivityArrayLen())
2076 for i in range(8): # 8 is not an error
2077 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
2079 self.assertEqual(expected3[:4],[int(i) for i in m2C.getNodalConnectivity()][4:8])
2080 self.assertEqual(expected3[4:8],[int(i) for i in m2C.getNodalConnectivity()][:4])
2081 self.assertEqual(expected4[:3],m2C.getNodalConnectivityIndex().getValues())
2083 f2=f1.buildSubPart(part4)
2084 self.assertEqual(6,f2.getNumberOfTuples())
2085 self.assertEqual(2,f2.getNumberOfComponents())
2086 expected6=[4.,104.,5.,105.,7.,107.,8.,108.,10.,110.,11.,111.]
2088 self.assertAlmostEqual(f2.getIJ(0,i),expected6[i],12)
2090 self.assertEqual(3,f2.getMesh().getNumberOfCells())
2091 self.assertEqual(6,f2.getMesh().getNumberOfNodes())
2092 self.assertEqual(2,f2.getMesh().getSpaceDimension())
2093 self.assertEqual(2,f2.getMesh().getMeshDimension())
2095 self.assertEqual(13,m2C.getNodalConnectivityArrayLen())
2097 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
2099 self.assertEqual(expected3[0:4],m2C.getNodalConnectivity().getValues()[4:8])
2100 self.assertEqual(expected3[4:8],m2C.getNodalConnectivity().getValues()[0:4])
2101 self.assertEqual(expected3[8:13],m2C.getNodalConnectivity().getValues()[8:13])
2102 self.assertEqual(expected4,m2C.getNodalConnectivityIndex().getValues())
2103 # previous line equivalent to
2104 self.assertEqual(expected4,[int(i) for i in m2C.getNodalConnectivityIndex()])
2107 def testExampleUMeshStdBuild1(self):
2108 # ! [PySnippetUMeshStdBuild1_1]
2109 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.,
2110 0.7,0.2,0., -0.3,0.7,0., 0.2,0.7,0., 0.7,0.7,0. ]
2111 nodalConnPerCell=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
2112 # ! [PySnippetUMeshStdBuild1_1]
2113 # ! [PySnippetUMeshStdBuild1_2]
2114 mesh=MEDCouplingUMesh("My2DMesh",2)
2115 # ! [PySnippetUMeshStdBuild1_2]
2116 # ! [PySnippetUMeshStdBuild1_3]
2117 mesh.allocateCells(5)#You can put more than 5 if you want but not less.
2119 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[:4])
2120 mesh.insertNextCell(NORM_TRI3,nodalConnPerCell[4:7])
2121 mesh.insertNextCell(NORM_TRI3,nodalConnPerCell[7:10])
2122 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[10:14])
2123 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[14:])
2125 mesh.finishInsertingCells()
2126 # ! [PySnippetUMeshStdBuild1_3]
2127 # ! [PySnippetUMeshStdBuild1_4]
2128 coordsArr=DataArrayDouble(coords,9,3)#here coordsArr are declared to have 3 components, mesh will deduce that its spaceDim==3.
2129 mesh.setCoords(coordsArr)#coordsArr contains 9 tuples, that is to say mesh contains 9 nodes.
2130 # ! [PySnippetUMeshStdBuild1_4]
2131 # ! [PySnippetUMeshStdBuild1_5]
2132 mesh.checkConsistencyLight()
2133 # ! [PySnippetUMeshStdBuild1_5]
2136 def testExampleCMeshStdBuild1(self):
2137 # ! [PySnippetCMeshStdBuild1_1]
2138 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] # 9 values along X
2139 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] # 7 values along Y
2140 arrX=DataArrayDouble(XCoords)
2141 arrX.setInfoOnComponent(0,"X [m]")
2142 arrY=DataArrayDouble(YCoords)
2143 arrY.setInfoOnComponent(0,"Y [m]")
2144 # ! [PySnippetCMeshStdBuild1_1]
2145 # ! [PySnippetCMeshStdBuild1_2]
2146 mesh=MEDCouplingCMesh("My2D_CMesh")
2147 mesh.setCoords(arrX,arrY)
2148 # ! [PySnippetCMeshStdBuild1_2]
2149 # ! [PySnippetCMeshStdBuild1_3]
2150 self.assertEqual(8*6,mesh.getNumberOfCells())
2151 self.assertEqual(9*7,mesh.getNumberOfNodes())
2152 self.assertEqual(2,mesh.getSpaceDimension())
2153 self.assertEqual(2,mesh.getMeshDimension())
2154 # ! [PySnippetCMeshStdBuild1_3]
2155 mesh=MEDCouplingCMesh("My2D_CMesh")
2156 # ! [PySnippetCMeshStdBuild1_2bis]
2157 mesh.setCoordsAt(0,arrX)
2158 mesh.setCoordsAt(1,arrY)
2159 # ! [PySnippetCMeshStdBuild1_2bis]
2160 self.assertEqual(8*6,mesh.getNumberOfCells())
2161 self.assertEqual(9*7,mesh.getNumberOfNodes())
2162 self.assertEqual(2,mesh.getSpaceDimension())
2163 self.assertEqual(2,mesh.getMeshDimension())
2164 # ! [PySnippetCMeshStdBuild1_4]
2165 # ! [PySnippetCMeshStdBuild1_4]
2168 def testExampleUMeshAdvBuild1(self):
2169 # ! [PySnippetUMeshAdvBuild1_1]
2170 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.,
2171 0.7,0.2,0., -0.3,0.7,0., 0.2,0.7,0., 0.7,0.7,0. ]
2172 nodalConnPerCell=[4,0,3,4,1, 3,1,4,2, 3,4,5,2, 4,6,7,4,3, 4,7,8,5,4]
2173 nodalConnPerCellIndex=[0,5,9,13,18,23]
2174 # ! [PySnippetUMeshAdvBuild1_1]
2175 # ! [PySnippetUMeshAdvBuild1_2]
2176 mesh=MEDCouplingUMesh("My2DMesh",2)
2177 # ! [PySnippetUMeshAdvBuild1_2]
2178 # ! [PySnippetUMeshAdvBuild1_3]
2179 nodalConn=DataArrayInt(nodalConnPerCell,23,1)
2180 nodalConnI=DataArrayInt(nodalConnPerCellIndex,6,1)
2181 mesh.setConnectivity(nodalConn,nodalConnI,True)
2182 # ! [PySnippetUMeshAdvBuild1_3]
2183 # ! [PySnippetUMeshAdvBuild1_4]
2184 coordsArr=DataArrayDouble(coords,9,3)#here coordsArr are declared to have 3 components, mesh will deduce that its spaceDim==3.
2185 mesh.setCoords(coordsArr)#coordsArr contains 9 tuples, that is to say mesh contains 9 nodes.
2186 # ! [PySnippetUMeshAdvBuild1_4]
2187 # ! [PySnippetUMeshAdvBuild1_5]
2188 # ! [PySnippetUMeshAdvBuild1_5]
2189 mesh.checkConsistencyLight()
2192 def testExampleDataArrayBuild1(self):
2193 # ! [PySnippetDataArrayBuild1_0]
2194 dataDouble=[0.,10.,20.,1.,11.,21.,2.,12.,22.,3.,13.,23.,4.,14.,24.]
2195 # ! [PySnippetDataArrayBuild1_0]
2196 # ! [PySnippetDataArrayBuild1_1]
2197 arrayDouble=DataArrayDouble()
2198 arrayDouble.setValues(dataDouble,5,3)# 5 tuples containing each 3 components
2199 # ! [PySnippetDataArrayBuild1_1]
2200 # ! [PySnippetDataArrayBuild1_1bis]
2201 arrayDouble=DataArrayDouble(dataDouble,5,3)
2202 # ! [PySnippetDataArrayBuild1_1bis]
2203 # ! [PySnippetDataArrayBuild1_2]
2204 dataInt=[0, 10, 20, 1, 11, 21, 2, 12, 22, 3, 13, 23, 4, 14, 24]
2205 # ! [PySnippetDataArrayBuild1_2]
2206 # ! [PySnippetDataArrayBuild1_3]
2207 arrayInt=DataArrayInt()
2208 arrayInt.setValues(dataInt,5,3)# 5 tuples containing each 3 components
2209 # ! [PySnippetDataArrayBuild1_3]
2210 # ! [PySnippetDataArrayBuild1_3bis]
2211 arrayInt=DataArrayInt(dataInt,5,3)
2212 # ! [PySnippetDataArrayBuild1_3bis]
2215 def testExampleFieldDoubleBuild1(self):
2216 XCoords=[-0.3,0.07,0.1,0.3,0.45,0.47,0.49,1.,1.22]; arrX=DataArrayDouble(XCoords)
2217 YCoords=[0.07,0.1,0.37,0.45,0.47,0.49,1.007]; arrY=DataArrayDouble(YCoords)
2218 mesh=MEDCouplingCMesh("My2D_CMesh")
2219 mesh.setCoords(arrX,arrY)
2220 # ! [PySnippetFieldDoubleBuild1_1]
2221 fieldOnCells=MEDCouplingFieldDouble(ON_CELLS,NO_TIME)
2222 fieldOnCells.setName("MyTensorFieldOnCellNoTime")
2223 fieldOnCells.setMesh(mesh)
2224 array=DataArrayDouble()
2225 array.alloc(fieldOnCells.getMesh().getNumberOfCells(),9) # Implicitly fieldOnCells will be a 9 components field.
2226 array.fillWithValue(7.)
2227 fieldOnCells.setArray(array)
2228 # fieldOnCells is now usable
2230 # ! [PySnippetFieldDoubleBuild1_1]
2231 # ! [PySnippetFieldDoubleBuild1_2]
2232 f1=mesh.fillFromAnalytic(ON_CELLS,1,"x*x+y*y*3+2.*x") # f1 is scalar
2233 f2=mesh.fillFromAnalytic(ON_CELLS,1,"cos(x+y/x)") # f2 is scalar too
2234 f2bis=mesh.fillFromAnalytic(ON_CELLS,2,"x*x*IVec+3*y*JVec") # f2bis is a vectors field
2235 f3=f1+f2 # f3 scalar
2236 f4=f3/f2 # f4 scalar
2237 f2bis.applyFunc(1,"sqrt(x*x+y*y)") # f2bis becomes scalar
2238 f5=f2bis*f4 # f5 scalar
2240 res=f4.getValueOn(pos1) # f4 is scalar so the returned value is of size 1.
2242 # ! [PySnippetFieldDoubleBuild1_2]
2243 # ! [PySnippetFieldDoubleBuild1_3]
2244 # ! [PySnippetFieldDoubleBuild1_3]
2247 def testExampleFieldDoubleBuild2(self):
2248 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22]; arrX=DataArrayDouble(XCoords)
2249 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007]; arrY=DataArrayDouble(YCoords)
2250 mesh=MEDCouplingCMesh("My2D_CMesh")
2251 mesh.setCoords(arrX,arrY)
2252 # ! [PySnippetFieldDoubleBuild2_1]
2253 fieldOnNodes=MEDCouplingFieldDouble(ON_NODES,NO_TIME)
2254 fieldOnNodes.setName("MyScalarFieldOnNodeNoTime")
2255 fieldOnNodes.setMesh(mesh)
2256 array=DataArrayDouble()
2257 array.alloc(fieldOnNodes.getMesh().getNumberOfNodes(),1) # Implicitly fieldOnNodes will be a 1 component field.
2258 array.fillWithValue(7.)
2259 fieldOnNodes.setArray(array)
2260 # fieldOnNodes is now usable
2262 # ! [PySnippetFieldDoubleBuild2_1]
2265 def testExampleFieldDoubleBuild3(self):
2266 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22]; arrX=DataArrayDouble(XCoords)
2267 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007]; arrY=DataArrayDouble(YCoords)
2268 mesh=MEDCouplingCMesh("My2D_CMesh")
2269 mesh.setCoords(arrX,arrY)
2270 # ! [PySnippetFieldDoubleBuild3_1]
2271 fieldOnCells=MEDCouplingFieldDouble(ON_CELLS,ONE_TIME)
2272 fieldOnCells.setName("MyTensorFieldOnCellNoTime")
2273 fieldOnCells.setTimeUnit("ms") # Time unit is ms.
2274 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
2275 fieldOnCells.setMesh(mesh)
2276 array=DataArrayDouble()
2277 array.alloc(fieldOnCells.getMesh().getNumberOfCells(),2) # Implicitly fieldOnCells will be a 2 components field.
2278 array.fillWithValue(7.)
2279 fieldOnCells.setArray(array)
2280 # fieldOnCells is now usable
2282 # ! [PySnippetFieldDoubleBuild3_1]
2285 def testExampleFieldDoubleBuild4(self):
2286 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22]; arrX=DataArrayDouble(XCoords)
2287 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007]; arrY=DataArrayDouble(YCoords)
2288 mesh=MEDCouplingCMesh("My2D_CMesh")
2289 mesh.setCoords(arrX,arrY)
2290 # ! [PySnippetFieldDoubleBuild4_1]
2291 fieldOnNodes=MEDCouplingFieldDouble(ON_NODES,CONST_ON_TIME_INTERVAL)
2292 fieldOnNodes.setName("MyVecFieldOnNodeWithConstTime")
2293 fieldOnNodes.setTimeUnit("ms") # Time unit is ms.
2294 fieldOnNodes.setStartTime(4.22,2,-1)
2295 fieldOnNodes.setEndTime(6.44,4,-1)# fieldOnNodes is defined in interval [4.22 ms,6.44 ms]
2296 fieldOnNodes.setMesh(mesh)
2297 array=DataArrayDouble()
2298 array.alloc(fieldOnNodes.getMesh().getNumberOfNodes(),3) # Implicitly fieldOnNodes will be a 3 components field.
2299 array.fillWithValue(7.)
2300 fieldOnNodes.setArray(array)
2301 # fieldOnNodes is now usable
2303 # ! [PySnippetFieldDoubleBuild4_1]
2306 def testExampleDataArrayApplyFunc1(self):
2307 # ! [PySnippetDataArrayApplyFunc1_1]
2308 d=DataArrayDouble([1.,2.,11.,12.,21.,22.,31.,41.],4,2)
2309 self.assertRaises(InterpKernelException,d.applyFunc,"x*y")
2310 # ! [PySnippetDataArrayApplyFunc1_1]
2311 # ! [PySnippetDataArrayApplyFunc1_2]
2312 d=DataArrayDouble([1.,2.,11.,12.,21.,22.,31.,41.],4,2)
2313 d1=d.applyFunc("smth*smth")
2314 self.assertTrue(d1.isEqual(DataArrayDouble([1.,4.,121.,144.,441.,484.,961.,1681.],4,2),1e-12))
2315 # ! [PySnippetDataArrayApplyFunc1_2]
2316 # ! [PySnippetDataArrayApplyFunc1_3]
2317 d2=d.applyFunc(2,"smth1*IVec+2*smth2*JVec")
2318 self.assertTrue(d2.isEqual(DataArrayDouble([1.,4.,11.,24.,21.,44.,31.,82.],4,2),1e-12))
2319 # ! [PySnippetDataArrayApplyFunc1_3]
2320 # ! [PySnippetDataArrayApplyFunc1_4]
2321 dd=DataArrayDouble([1.,4.,3.,11.,144.,13.,21.,484.,23.,31.,1024.,33.],4,3)
2322 # ! [PySnippetDataArrayApplyFunc1_4]
2323 # ! [PySnippetDataArrayApplyFunc1_5]
2324 dd1=dd.applyFunc(1,"f+sqrt(g)+h")
2325 self.assertTrue(dd1.isEqual(DataArrayDouble([6.,36.,66.,96.],4,1),1e-12))
2326 # ! [PySnippetDataArrayApplyFunc1_5]
2327 # ! [PySnippetDataArrayApplyFunc1_6]
2328 dd2=dd.applyFunc(1,"a+0.*b+c")
2329 self.assertTrue(dd2.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
2330 # ! [PySnippetDataArrayApplyFunc1_6]
2331 # ! [PySnippetDataArrayApplyFunc1_7]
2332 ddd=DataArrayDouble([1.,4.,3.,11.,144.,13.,21.,484.,23.,31.,1024.,33.],4,3)
2333 ddd.setInfoOnComponents(["Y [m]","AA [m/s]","GG [MW]"])
2334 # ! [PySnippetDataArrayApplyFunc1_7]
2335 # ! [PySnippetDataArrayApplyFunc1_8]
2336 ddd1=ddd.applyFuncCompo(1,"Y+GG")
2337 self.assertTrue(ddd1.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
2338 # ! [PySnippetDataArrayApplyFunc1_8]
2339 # ! [PySnippetDataArrayApplyFunc1_9]
2340 ddd1=ddd.applyFuncNamedCompo(1,["X","Y","Z"],"X+Z")
2341 self.assertTrue(ddd1.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
2342 # ! [PySnippetDataArrayApplyFunc1_9]