1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2021 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
23 if sys.platform == "win32":
24 from MEDCouplingCompat import *
26 from medcoupling import *
28 from math import pi, sqrt
30 def WriteInTmpDir(func):
31 def decoratedFunc(*args,**kwargs):
34 with tempfile.TemporaryDirectory() as tmpdirname:
36 ret = func(*args,**kwargs)
41 class MEDCouplingBasicsTest(unittest.TestCase):
44 def testExample_MEDCouplingFieldDouble_WriteVTK(self):
45 #! [PySnippet_MEDCouplingFieldDouble_WriteVTK_1]
48 coordsArr=DataArrayDouble(coords,3,1)
49 mesh=MEDCouplingCMesh()
50 mesh.setCoords(coordsArr,coordsArr) # mesh becomes a 2D one
52 # 3 fields (lying on the same mesh!)
53 field1 = mesh.getMeasureField( True )
54 field2 = mesh.buildOrthogonalField()
55 field3 = mesh.fillFromAnalytic( ON_CELLS, 2, "IVec * x + JVec * y" )
56 field2.setName( "Normal" ) # name is necessary!
57 field3.setName( "Barycenter" ) # name is necessary!
60 fileName = "testExample_MEDCouplingFieldDouble_WriteVTK"
61 fs = [ field1, field2, field3 ] # field series
62 writtenFileName=MEDCouplingFieldDouble.WriteVTK( fileName, fs )
63 print("The file name with correct extension is : %s"%(writtenFileName))
64 #! [PySnippet_MEDCouplingFieldDouble_WriteVTK_1]
66 os.remove( writtenFileName )
70 def testExample_MEDCouplingFieldDouble_MaxFields(self):
71 #! [PySnippet_MEDCouplingFieldDouble_MaxFields_1]
72 vals1 = [0.,2., 4.,6.] # for field 1
73 vals2 = [2.,0., 6.,4.] # for field 2
74 valsMax = [2.,2., 6.,6.] # expected max field
75 valsMin = [0.,0., 4.,4.] # expected min field
78 valsArr1=DataArrayDouble(vals1,2,2) # 2 tuples per 2 components
79 field1 = MEDCouplingFieldDouble( ON_NODES )
80 field1.setArray( valsArr1 )
83 valsArr2=DataArrayDouble(vals2,2,2) # 2 tuples per 2 components
84 field2 = MEDCouplingFieldDouble( ON_NODES )
85 field2.setArray( valsArr2 )
88 fieldMax = MEDCouplingFieldDouble.MaxFields( field1, field2 )
89 self.assertTrue( fieldMax.getArray().getValues() == valsMax )
92 fieldMin = MEDCouplingFieldDouble.MinFields( field1, field2 )
93 self.assertTrue( fieldMin.getArray().getValues() == valsMin )
94 #! [PySnippet_MEDCouplingFieldDouble_MaxFields_1]
96 def testExample_MEDCouplingFieldDouble_MergeFields(self):
97 #! [PySnippet_MEDCouplingFieldDouble_MergeFields_1]
100 coordsArr=DataArrayDouble(coords,3,1)
101 mesh1=MEDCouplingCMesh()
102 mesh1.setCoords(coordsArr)
104 field1 = mesh1.fillFromAnalytic( ON_CELLS, 1, "x")
107 field2 = field1.cloneWithMesh( True )
109 field2.getMesh().translate(vec) # translate mesh2
110 field2.applyFunc("x + 5") # "translate" field2
112 # concatenate field1 and field2
113 field3 = MEDCouplingFieldDouble.MergeFields( field1, field2 )
114 field4 = MEDCouplingFieldDouble.MergeFields( [ field1, field2] )
115 #! [PySnippet_MEDCouplingFieldDouble_MergeFields_1]
118 def testExample_MEDCouplingFieldDouble_substractInPlaceDM(self):
119 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_1]
120 coords1=[0.,1.,2.,3.]
121 coords2=[2.,1.,0.,3.] #0 <==> #2
123 mesh1=MEDCouplingUMesh()
124 coordsArr=DataArrayDouble(coords1, 4, 1)
125 mesh1.setCoords(coordsArr)
126 mesh1.setMeshDimension(0)
127 mesh1.allocateCells(0)
128 mesh1.finishInsertingCells()
130 mesh2=mesh1.deepCopy()
131 mesh2.getCoords().setValues(coords2, 4, 1)
132 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_1]
133 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_2]
134 field1 = mesh1.fillFromAnalytic(ON_NODES,1,"x") # field1 values == coords1
135 field2 = mesh2.fillFromAnalytic(ON_NODES,1,"x") # field2 values == coords2
136 levOfCheck = 10 # nodes can be permuted
137 field1.substractInPlaceDM( field2, levOfCheck, 1e-13, 0 ) # values #0 and #2 must swap
138 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_2]
139 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_3]
140 field2.applyFunc( 1, 0.0 ) # all field2 values == 0.0
141 self.assertTrue( field1.isEqual( field2, 1e-13, 1e-13 )) # field1 == field2 == 0.0
142 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_3]
145 def testExample_MEDCouplingFieldDouble_changeUnderlyingMesh(self):
146 #! [PySnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_1]
147 coords1=[0.,1.,2.,3.]
148 coords2=[2.,1.,0.,3.] #0 <==> #2
150 mesh1=MEDCouplingUMesh()
151 coordsArr=DataArrayDouble(coords1, 4, 1)
152 mesh1.setCoords(coordsArr)
153 mesh1.setMeshDimension(0)
154 mesh1.allocateCells(0)
155 mesh1.finishInsertingCells()
157 mesh2=mesh1.deepCopy()
158 mesh2.getCoords().setValues(coords2, 4, 1)
159 #! [PySnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_1]
160 #! [PySnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_2]
161 field = mesh1.fillFromAnalytic(ON_NODES,1,"x") # field values == coords1
162 levOfCheck = 10 # nodes can be permuted
163 field.changeUnderlyingMesh( mesh2, levOfCheck, 1e-13, 0 ) # values #0 and #2 must swap
164 self.assertTrue( field.getArray().getValues() == coords2 )
165 #! [PySnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_2]
168 def testExample_MEDCouplingFieldDouble_applyFunc_same_nb_comp(self):
169 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_1]
171 array = DataArrayDouble( v, 2, 2 ) # 2 tuples per 2 components
172 field = MEDCouplingFieldDouble( ON_CELLS )
173 field.setArray( array )
174 func = "IVec * v + JVec * w*w + 10"
175 field.applyFunc( 2, func )
176 self.assertTrue( field.getNumberOfComponents() == 2 ) # 2 components remains
177 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_1]
178 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_2]
179 v2 = field.getArray().getValues()
180 self.assertAlmostEqual( v2[0], 10 + v[0], 13 ) # "10 + IVec * v"
181 self.assertAlmostEqual( v2[1], 10 + v[1]*v[1], 13 ) # "10 + JVec * v*v"
182 self.assertAlmostEqual( v2[2], 10 + v[2], 13 ) # "10 + IVec * v"
183 self.assertAlmostEqual( v2[3], 10 + v[3]*v[3], 13 ) # "10 + JVec * v*v"
184 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_2]
187 def testExample_MEDCouplingFieldDouble_applyFunc3(self):
188 #! [PySnippet_MEDCouplingFieldDouble_applyFunc3_1]
189 # create a 2D vector field
190 values = [1.,1., 2.,1.]
191 array = DataArrayDouble( values, 2, 2 ) # 2 tuples per 2 components
192 field = MEDCouplingFieldDouble( ON_CELLS )
193 field.setArray( array )
194 # transform the field to a 3D vector field
195 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
196 varNames=["a","b"] # names used to refer to X and Y components
197 field.applyFuncNamedCompo( 3, varNames, func ) # require 3 components
198 self.assertTrue( field.getNumberOfComponents() == 3 ) # 3 components as required
199 #! [PySnippet_MEDCouplingFieldDouble_applyFunc3_1]
200 #! [PySnippet_MEDCouplingFieldDouble_applyFunc3_2]
201 vec1 = field.getArray().getTuple(1) # vector #1
202 a,b = values[2], values[3] # initial components of the vector #1
203 self.assertAlmostEqual( vec1[0], 10 + b, 13 ) # "10 + IVec * b"
204 self.assertAlmostEqual( vec1[1], 10 + a, 13 ) # "10 + JVec * a"
205 self.assertAlmostEqual( vec1[2], 10 + sqrt(a*a+b*b), 13 ) # "10 + KVec * sqrt( a*a + b*b )"
206 #! [PySnippet_MEDCouplingFieldDouble_applyFunc3_2]
209 def testExample_MEDCouplingFieldDouble_applyFunc2(self):
210 #! [PySnippet_MEDCouplingFieldDouble_applyFunc2_1]
211 # create a 2D vector field
212 values = [1.,1., 2.,1.]
213 array = DataArrayDouble( values, 2, 2 ) # 2 tuples per 2 components
214 array.setInfoOnComponent(0,"a") # name used to refer to X component within a function
215 array.setInfoOnComponent(1,"b") # name used to refer to Y component within a function
216 field = MEDCouplingFieldDouble( ON_CELLS )
217 field.setArray( array )
218 # transform the field to a 3D vector field
219 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
220 field.applyFuncCompo( 3, func ) # require 3 components
221 self.assertTrue( field.getNumberOfComponents() == 3 ) # 3 components as required
222 #! [PySnippet_MEDCouplingFieldDouble_applyFunc2_1]
223 #! [PySnippet_MEDCouplingFieldDouble_applyFunc2_2]
224 vec1 = field.getArray().getTuple(1) # vector #1
225 a,b = values[2], values[3] # initial components of the vector #1
226 self.assertAlmostEqual( vec1[0], 10 + b, 13 ) # "10 + IVec * b"
227 self.assertAlmostEqual( vec1[1], 10 + a, 13 ) # "10 + JVec * a"
228 self.assertAlmostEqual( vec1[2], 10 + sqrt(a*a+b*b), 13 ) # "10 + KVec * sqrt( a*a + b*b )"
229 #! [PySnippet_MEDCouplingFieldDouble_applyFunc2_2]
232 def testExample_MEDCouplingFieldDouble_applyFunc(self):
233 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_1]
234 # create a 2D vector field
235 values = [1.,1., 2.,1.]
236 array = DataArrayDouble( values, 2, 2 ) # 2 tuples per 2 components
237 field = MEDCouplingFieldDouble( ON_CELLS )
238 field.setArray( array )
239 # transform the field to a 3D vector field
240 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
241 field.applyFunc( 3, func ) # require 3 components
242 self.assertTrue( field.getNumberOfComponents() == 3 ) # 3 components as required
243 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_1]
244 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_2]
245 vec1 = field.getArray().getTuple(1) # vector #1
246 a,b = values[2], values[3] # initial components of the vector #1
247 self.assertAlmostEqual( vec1[0], 10 + b, 13 ) # "10 + IVec * b"
248 self.assertAlmostEqual( vec1[1], 10 + a, 13 ) # "10 + JVec * a"
249 self.assertAlmostEqual( vec1[2], 10 + sqrt(a*a+b*b), 13 ) # "10 + KVec * sqrt( a*a + b*b )"
250 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_2]
253 def testExample_MEDCouplingFieldDouble_applyFunc_val(self):
254 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_val_1]
256 coordsArr=DataArrayDouble(coords,3,1)
257 mesh=MEDCouplingCMesh()
258 mesh.setCoords(coordsArr,coordsArr)
259 field = MEDCouplingFieldDouble( ON_CELLS )
260 field.setMesh( mesh )
261 field.fillFromAnalytic(2,"IVec * x + JVec * y") # 2 components
262 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_val_1]
263 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_val_2]
265 field.applyFunc( 3, newValue ) # 3 components are required
266 self.assertTrue( field.getIJ(1,0) == newValue ) # a value is as expected
267 self.assertTrue( field.getNumberOfComponents() == 3 )
268 self.assertTrue( field.getNumberOfTuples() == mesh.getNumberOfCells() )
269 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_val_2]
272 def testExample_MEDCouplingFieldDouble_fillFromAnalytic3(self):
273 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_1]
274 coords = [0.,2.,4.,6.] # 6. is not used
275 x=DataArrayDouble(coords[:3],3,1)
276 y=DataArrayDouble(coords[:2],2,1)
277 mesh=MEDCouplingCMesh()
279 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_1]
280 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_2]
281 field = MEDCouplingFieldDouble( ON_CELLS )
282 field.setMesh( mesh )
283 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
284 varNames=["a","b"] # names used to refer to X and Y coord components
285 field.fillFromAnalyticNamedCompo(3,varNames,func)
286 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_2]
287 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_3]
288 vals1 = field.getArray().getTuple(1) # values of the cell #1
289 assert len( vals1 ) == 3 # 3 components in the field
291 bc = mesh.computeCellCenterOfMass() # func is applied to barycenters of cells
292 bc1 = bc.getTuple(1) # coordinates of the second point
294 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
295 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
296 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
297 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
298 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_3]
301 def testExample_MEDCouplingFieldDouble_fillFromAnalytic2(self):
302 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_1]
304 x=DataArrayDouble(coords[:3],3,1)
305 y=DataArrayDouble(coords[:2],2,1)
306 x.setInfoOnComponent(0,"a") # name used to refer to X coordinate within a function
307 y.setInfoOnComponent(0,"b") # name used to refer to Y coordinate within a function
308 mesh=MEDCouplingCMesh()
310 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_1]
311 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_2]
312 field = MEDCouplingFieldDouble( ON_CELLS )
313 field.setMesh( mesh )
314 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
315 field.fillFromAnalyticCompo(3,func)
316 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_2]
317 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_3]
318 vals1 = field.getArray().getTuple(1) # values of the cell #1
319 assert len( vals1 ) == 3 # 3 components in the field
321 bc = mesh.computeCellCenterOfMass() # func is applied to barycenters of cells
322 bc1 = bc.getTuple(1) # coordinates of the second point
324 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
325 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
326 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
327 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
328 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_3]
331 def testExample_MEDCouplingFieldDouble_fillFromAnalytic(self):
332 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_1]
334 x=DataArrayDouble(coords[:3],3,1)
335 y=DataArrayDouble(coords[:2],2,1)
336 mesh=MEDCouplingCMesh()
338 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_1]
339 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_2]
340 field = MEDCouplingFieldDouble( ON_CELLS )
341 field.setMesh( mesh )
342 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
343 field.fillFromAnalytic(3,func)
344 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_2]
345 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_3]
346 vals1 = field.getArray().getTuple(1) # values of the cell #1
347 assert len( vals1 ) == 3 # 3 components in the field
349 bc = mesh.computeCellCenterOfMass() # func is applied to barycenters of cells
350 bc1 = bc.getTuple(1) # coordinates of the second point
352 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
353 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
354 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
355 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
356 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_3]
359 def testExample_MEDCouplingFieldDouble_getValueOn_time(self):
360 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_1]
362 coordsArr=DataArrayDouble(coords,3,1)
363 mesh=MEDCouplingCMesh()
364 mesh.setCoords(coordsArr,coordsArr)
365 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_1]
366 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_2]
367 field = MEDCouplingFieldDouble( ON_CELLS, LINEAR_TIME )
368 field.setMesh( mesh )
369 field.fillFromAnalytic(1,"10") # all values == 10.
370 field.setEndArray( field.getArray() + field.getArray() ) # all values == 20.
371 time1, time2 = 1.1, 22.
372 field.setStartTime( time1, 0, 0 )
373 field.setEndTime ( time2, 0, 0 )
374 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_2]
375 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_3]
376 pos = [ 1., 1. ] # we are in 2D space
377 value = field.getValueOn( pos, 0.5*( time1 + time2 ))
378 self.assertTrue( value[0] == 0.5*( 10. + 20.))
379 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_3]
382 def testExample_MEDCouplingFieldDouble_getValueOnMulti(self):
383 #! [PySnippet_MEDCouplingFieldDouble_getValueOnMulti_1]
385 coordsArr=DataArrayDouble(coords,3,1)
386 mesh=MEDCouplingCMesh()
387 mesh.setCoords(coordsArr,coordsArr)
388 field = mesh.fillFromAnalytic(ON_CELLS,1,"x+y")
389 #! [PySnippet_MEDCouplingFieldDouble_getValueOnMulti_1]
390 #! [PySnippet_MEDCouplingFieldDouble_getValueOnMulti_2]
391 bc = mesh.computeCellCenterOfMass() # field values are located at cell barycenters
392 valArray = field.getValueOnMulti( bc )
393 self.assertTrue( valArray.isEqual( field.getArray(), 1e-13 ))
394 #! [PySnippet_MEDCouplingFieldDouble_getValueOnMulti_2]
397 def testExample_MEDCouplingFieldDouble_getValueOn(self):
398 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_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_getValueOn_1]
405 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_2]
406 bc = mesh.computeCellCenterOfMass() # field values are located at cell barycenters
407 vals = [] # array to collect values returned by getValueOn()
408 for i,tupl in enumerate( bc ):
409 vals.extend( field.getValueOn( tupl ) )
410 self.assertTrue( vals == field.getArray().getValues() )
411 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_2]
414 def testExample_MEDCouplingFieldDouble_getValueOnPos(self):
415 #! [PySnippet_MEDCouplingFieldDouble_getValueOnPos_1]
417 coordsArr=DataArrayDouble(coords,3,1)
418 mesh=MEDCouplingCMesh()
419 mesh.setCoords(coordsArr,coordsArr)
420 field = mesh.fillFromAnalytic(ON_CELLS,1,"x+y")
421 #! [PySnippet_MEDCouplingFieldDouble_getValueOnPos_1]
422 #! [PySnippet_MEDCouplingFieldDouble_getValueOnPos_2]
423 val11 = field.getValueOnPos( 1,1,-1)
424 bc = mesh.computeCellCenterOfMass() # field values are located at cell barycenters
425 self.assertTrue( val11[0] == bc[3,0] + bc[3,1] )
426 #! [PySnippet_MEDCouplingFieldDouble_getValueOnPos_2]
429 def testExample_MEDCouplingFieldDouble_renumberNodes(self):
430 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_1]
432 coordsArr=DataArrayDouble(coords,3,1)
433 mesh=MEDCouplingCMesh()
434 mesh.setCoords(coordsArr,coordsArr)
435 mesh=mesh.buildUnstructured()
436 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_1]
437 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_2]
438 field = mesh.fillFromAnalytic(ON_NODES,2,"IVec*x+JVec*y")
439 values = field.getArray()
440 nodeCoords = mesh.getCoords()
441 self.assertTrue( values.isEqualWithoutConsideringStr( nodeCoords, 1e-13 ))
442 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_2]
443 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_3]
444 renumber = [8, 7, 6, 5, 4, 3, 2, 1, 0]
445 field.renumberNodes(renumber,False)
446 mesh2 = field.getMesh() # field now refers to another mesh
447 values = field.getArray()
448 nodeCoords = mesh2.getCoords()
449 self.assertTrue( values.isEqualWithoutConsideringStr( nodeCoords, 1e-13 ))
450 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_3]
454 def testExample_MEDCouplingFieldDouble_renumberCells(self):
455 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_1]
457 coordsArr=DataArrayDouble(coords,3,1)
458 mesh=MEDCouplingCMesh()
459 mesh.setCoords(coordsArr,coordsArr)
460 mesh=mesh.buildUnstructured()
461 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_1]
462 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_2]
463 field = mesh.fillFromAnalytic(ON_CELLS,2,"IVec*x+JVec*y")
464 values = field.getArray()
465 bc = mesh.computeCellCenterOfMass()
466 self.assertTrue( values.isEqualWithoutConsideringStr( bc, 1e-13 ))
467 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_2]
468 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_3]
469 renumber = [ 3, 2, 1, 0 ]
470 field.renumberCells(renumber,False)
471 mesh2 = field.getMesh() # field now refers to another mesh
472 values = field.getArray()
473 bc = mesh2.computeCellCenterOfMass()
474 self.assertTrue( values.isEqualWithoutConsideringStr( bc, 1e-13 ))
475 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_3]
478 def testExample_MEDCouplingFieldDouble_buildNewTimeReprFromThis(self):
479 #! [PySnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_1]
481 coordsArr=DataArrayDouble(coords,3,1)
482 mesh=MEDCouplingCMesh()
483 mesh.setCoords(coordsArr,coordsArr)
484 field1 = mesh.fillFromAnalytic(ON_NODES,1,"x+y")
485 self.assertTrue( field1.getTimeDiscretization() == ONE_TIME )
486 #! [PySnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_1]
487 #! [PySnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_2]
488 field2 = field1.buildNewTimeReprFromThis(NO_TIME,False)
489 self.assertTrue( field2.getTimeDiscretization() == NO_TIME )
490 #! [PySnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_2]
493 def testExample_MEDCouplingMesh_fillFromAnalytic3(self):
494 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_1]
495 coords = [0.,2.,4.,6.] # 6. is not used
496 x=DataArrayDouble(coords[:3],3,1)
497 y=DataArrayDouble(coords[:2],2,1)
498 mesh=MEDCouplingCMesh()
500 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_1]
501 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_2]
502 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
503 varNames=["a","b"] # names used to refer to X and Y coord components
504 field=mesh.fillFromAnalyticNamedCompo(ON_CELLS,3,varNames,func)
505 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_2]
506 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_3]
507 vals1 = field.getArray().getTuple(1) # values of the cell #1
508 assert len( vals1 ) == 3 # 3 components in the field
510 bc = mesh.computeCellCenterOfMass() # func is applied to barycenters of cells
511 bc1 = bc.getTuple(1) # coordinates of the second point
513 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
514 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
515 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
516 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
517 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_3]
520 def testExample_MEDCouplingMesh_fillFromAnalytic2(self):
521 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_1]
522 coords = [0.,2.,4.,6.] # 6. is not used
523 x=DataArrayDouble(coords[:3],3,1)
524 y=DataArrayDouble(coords[:2],2,1)
525 x.setInfoOnComponent(0,"a") # name used to refer to X coordinate within a function
526 y.setInfoOnComponent(0,"b") # name used to refer to Y coordinate within a function
527 mesh=MEDCouplingCMesh()
529 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_1]
530 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_2]
531 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
532 field=mesh.fillFromAnalyticCompo(ON_CELLS,3,func)
533 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_2]
534 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_3]
535 vals1 = field.getArray().getTuple(1) # values of the cell #1
536 assert len( vals1 ) == 3 # 3 components in the field
538 bc = mesh.computeCellCenterOfMass() # func is applied to barycenters of cells
539 bc1 = bc.getTuple(1) # coordinates of the second point
541 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
542 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
543 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
544 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
545 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_3]
548 def testExample_MEDCouplingMesh_fillFromAnalytic(self):
549 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_1]
550 coords = [0.,2.,4.,6.] # 6. is not used
551 x=DataArrayDouble(coords[:3],3,1)
552 y=DataArrayDouble(coords[:2],2,1)
553 mesh=MEDCouplingCMesh()
555 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_1]
556 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_2]
557 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
558 field=mesh.fillFromAnalytic(ON_CELLS,3,func)
559 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_2]
560 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_3]
561 vals1 = field.getArray().getTuple(1) # values of the cell #1
562 assert len( vals1 ) == 3 # 3 components in the field
564 bc = mesh.computeCellCenterOfMass() # func is applied to barycenters of cells
565 bc1 = bc.getTuple(1) # coordinates of the second point
567 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
568 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
569 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
570 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
571 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_3]
574 def testExample_MEDCouplingCMesh_getCoordsAt(self):
575 #! [PySnippet_MEDCouplingCMesh_getCoordsAt_1]
577 x=DataArrayDouble(coords,3,1)
578 mesh=MEDCouplingCMesh()
579 mesh.setCoordsAt(0,x)
580 x2=mesh.getCoordsAt(0)
581 assert coords == x2.getValues()
582 #! [PySnippet_MEDCouplingCMesh_getCoordsAt_1]
585 def testExample_MEDCouplingUMesh_areCellsIncludedIn(self):
586 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_1]
587 mesh1=MEDCouplingUMesh()
588 mesh1.setMeshDimension(2)
589 mesh1.allocateCells(5)
590 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4]
591 mesh1.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
592 mesh1.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
593 mesh1.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
594 mesh1.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
595 mesh1.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
596 mesh1.finishInsertingCells()
597 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 ]
598 coordsArr=DataArrayDouble(coords,9,2)
599 mesh1.setCoords(coordsArr)
600 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_1]
601 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_2]
603 mesh2 = mesh1.buildPartOfMySelf(cells2, True ) # even cells selected
604 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_2]
605 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_3]
606 compType = 0 # the strongest policy
607 isOk, corr2to1 = mesh1.areCellsIncludedIn( mesh2, compType )
608 assert isOk # a larger mesh1 includes a smaller mesh2
609 assert corr2to1.getValues() == cells2
610 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_3]
611 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_4]
612 isOk, corr1to2 = mesh2.areCellsIncludedIn( mesh1, compType )
613 assert not isOk # the smaller mesh2 does NOT include the larger mesh1
614 assert corr1to2.getValues() == [2, 3, 1, 4, 0]
615 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_4]
617 def testExample_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells(self):
618 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_1]
619 # 2D coordinates of 5 base nodes
620 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2]
621 coordsArr=DataArrayDouble(coords,5,2)
622 # coordinates of 5 top nodes
623 coordsArr2 = coordsArr.deepCopy()
624 # 3D coordinates of base + top nodes
625 coordsArr = coordsArr.changeNbOfComponents( 3, 0 )
626 coordsArr2 = coordsArr2.changeNbOfComponents( 3, 1 )
627 coordsArr = DataArrayDouble.Aggregate([coordsArr,coordsArr2])
629 mesh=MEDCouplingUMesh()
630 mesh.setCoords(coordsArr)
631 mesh.setMeshDimension(3)
632 mesh.allocateCells(2)
633 # connectivity of reversed HEXA8 and PENTA6
634 conn=[0,1,4,3, 5,6,9,8, 1,2,4, 6,7,9]
635 mesh.insertNextCell(NORM_HEXA8, 8,conn[0:0+8])
636 mesh.insertNextCell(NORM_PENTA6,6,conn[8:8+6])
637 mesh.finishInsertingCells()
638 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_1]
639 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_2]
640 fixedCells = mesh.findAndCorrectBadOriented3DExtrudedCells()
641 assert len( fixedCells ) == 2 # 2 cells fixed
642 fixedCells = mesh.findAndCorrectBadOriented3DExtrudedCells()
643 assert len( fixedCells ) == 0 # no bad cells
644 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_2]
647 def testExample_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented(self):
648 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_1]
649 # 2D coordinates of 5 base nodes
650 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2]
651 coordsArr=DataArrayDouble(coords,5,2)
652 # coordinates of 5 top nodes
653 coordsArr2 = coordsArr.deepCopy()
654 # 3D coordinates of base + top nodes
655 coordsArr = coordsArr.changeNbOfComponents( 3, 0 )
656 coordsArr2 = coordsArr2.changeNbOfComponents( 3, 1 )
657 coordsArr = DataArrayDouble.Aggregate([coordsArr,coordsArr2])
659 mesh=MEDCouplingUMesh()
660 mesh.setCoords(coordsArr)
661 mesh.setMeshDimension(3)
662 mesh.allocateCells(2)
663 # connectivity of a HEXA8 + a reversed PENTA6
664 conn=[0,3,4,1, 5,8,9,6, 1,2,4, 6,7,9]
665 mesh.insertNextCell(NORM_POLYHED, 8,conn[0:0+8]) # "extruded" polyhedron
666 mesh.insertNextCell(NORM_POLYHED,6,conn[8:8+6])
667 mesh.finishInsertingCells()
668 # fix connectivity of NORM_POLYHED's
669 mesh.convertExtrudedPolyhedra()
670 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_1]
671 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_2]
672 badCells = mesh.arePolyhedronsNotCorrectlyOriented()
673 assert len( badCells ) == 1 # one polyhedron is KO
674 # fix invalid rolyherdons
675 mesh.orientCorrectlyPolyhedrons()
676 # re-check the orientation
677 badCells = mesh.arePolyhedronsNotCorrectlyOriented()
678 assert len( badCells ) == 0 # connectivity is OK
679 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_2]
682 def testExample_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented(self):
683 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_1]
684 mesh=MEDCouplingUMesh()
685 mesh.setMeshDimension(2)
686 mesh.allocateCells(5)
687 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4]
688 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
689 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
690 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
691 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
692 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
693 mesh.finishInsertingCells()
694 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 ]
695 coordsArr=DataArrayDouble(coords,9,2)
696 mesh.setCoords(coordsArr)
697 mesh.changeSpaceDimension(3)
698 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_1]
699 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_2]
701 badCellIds=mesh.are2DCellsNotCorrectlyOriented( vec, False )
702 assert len( badCellIds ) == 1 # one cell is reversed
704 mesh.orientCorrectly2DCells( vec, False )
705 # re-check orientation
706 badCellIds=mesh.are2DCellsNotCorrectlyOriented( vec, False )
707 assert len( badCellIds ) == 0 # the orientation is OK
708 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_2]
711 def testExample_MEDCouplingUMesh_getCellsContainingPoints(self):
712 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_1]
713 mesh=MEDCouplingUMesh()
714 mesh.setMeshDimension(2)
715 mesh.allocateCells(5)
716 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4]
717 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
718 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
719 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
720 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
721 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
722 mesh.finishInsertingCells()
723 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 ]
724 coordsArr=DataArrayDouble(coords,9,2)
725 mesh.setCoords(coordsArr)
726 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_1]
727 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_2]
728 pos = [ 10., 10, # point out of the mesh
729 0.3, 0.3, # point located somewhere inside the mesh
730 coords[2], coords[3]] # point at the node #1
731 eps = 1e-4 # ball radius
732 cells,cellsIndex=mesh.getCellsContainingPoints( pos, 3, eps )
733 assert cells.getValues() == [4, 0, 1]
734 assert cellsIndex.getValues() == [0, 0, 1, 3]
735 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_2]
739 def testExample_MEDCouplingUMesh_getCellsContainingPoint(self):
740 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_1]
741 mesh=MEDCouplingUMesh()
742 mesh.setMeshDimension(2)
743 mesh.allocateCells(5)
744 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4]
745 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
746 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
747 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
748 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14])
749 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18])
750 mesh.finishInsertingCells()
751 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 ]
752 coordsArr=DataArrayDouble(coords,9,2)
753 mesh.setCoords(coordsArr)
754 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_1]
755 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_2]
756 pos4 = coords[ 4*2 : ] # coordinates of the node #4
757 eps = 1e-4 # ball radius
758 pos = [ pos4[0]+eps, pos4[1]-eps ] # ball center
759 cellIds=mesh.getCellsContainingPoint( pos, eps )
760 assert len( cellIds ) == mesh.getNumberOfCells()
761 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_2]
765 def testExample_MEDCouplingUMesh_buildPartOrthogonalField(self):
766 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_1]
767 mesh=MEDCouplingUMesh()
768 mesh.setMeshDimension(2)
769 mesh.allocateCells(5)
770 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4]
771 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
772 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
773 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
774 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
775 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
776 mesh.finishInsertingCells()
777 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 ]
778 coordsArr=DataArrayDouble(coords,9,2)
779 mesh.setCoords(coordsArr)
780 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_1]
781 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_2]
782 part = DataArrayInt([1,2,3,4],4,1) # cell #0 is omitted
783 vecField=mesh.buildPartOrthogonalField( part )
784 vecArr = vecField.getArray()
785 assert len( vecArr ) == len( part )
786 assert vecArr.getNumberOfComponents() == 3
787 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_2]
790 def testExample_MEDCouplingUMesh_getPartMeasureField(self):
791 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_1]
792 mesh=MEDCouplingUMesh()
793 mesh.setMeshDimension(2)
794 mesh.allocateCells(5)
795 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4]
796 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
797 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
798 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
799 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
800 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
801 mesh.finishInsertingCells()
802 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 ]
803 coordsArr=DataArrayDouble(coords,9,2)
804 mesh.setCoords(coordsArr)
805 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_1]
806 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_2]
808 part = DataArrayInt([1,2,3,4],4,1) # cell #0 is omitted
809 areaArr=mesh.getPartMeasureField( isAbs, part )
810 assert areaArr[0] > 0 # orientation ignored
811 areaArr=mesh.getPartMeasureField( not isAbs, part )
812 assert areaArr[0] < 0 # orientation considered
813 assert len( areaArr ) == len( part )
814 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_2]
815 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_3]
816 part = DataArrayInt([1,2,3,4],4,1) # cell #0 is omitted
817 baryCenters = mesh.getPartBarycenterAndOwner( part )
818 assert len( baryCenters ) == len( part )
819 assert baryCenters.getNumberOfComponents() == mesh.getSpaceDimension()
820 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_3]
823 def testExample_MEDCouplingUMesh_getCellsInBoundingBox(self):
824 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_1]
825 mesh=MEDCouplingUMesh()
826 mesh.setMeshDimension(2)
827 coords=[0.,0., 0.,1., 1.,1]
828 coordsArr=DataArrayDouble(coords,3,2)
829 mesh.setCoords(coordsArr)
830 mesh.allocateCells(1)
832 mesh.insertNextCell(NORM_TRI3,3,conn)
833 mesh.finishInsertingCells()
834 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_1]
835 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_2]
836 bbox = [1., 1., 1.001,1.001] # xMin, xMax, yMin, yMax
837 cellsInBox = mesh.getCellsInBoundingBox( bbox, 0.0 )
838 assert cellsInBox.getValues() == []
839 cellsInBox = mesh.getCellsInBoundingBox( bbox, 0.1 )
840 assert cellsInBox.getValues() == [0]
841 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_2]
844 def testExample_MEDCouplingUMesh_renumberNodesInConn(self):
845 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_1]
846 mesh=MEDCouplingUMesh()
847 mesh.setMeshDimension(2)
848 mesh.allocateCells(1)
850 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
851 mesh.finishInsertingCells()
852 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_1]
853 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_2]
854 old2newIds = [-1,3,2,1,0]
855 mesh.renumberNodesInConn( old2newIds )
856 nodes0 = mesh.getNodeIdsOfCell( 0 )
857 assert nodes0 == [0,1,2,3]
858 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_2]
862 def testExample_MEDCouplingUMesh_renumberNodes(self):
863 #! [PySnippet_MEDCouplingUMesh_renumberNodes_1]
864 mesh=MEDCouplingUMesh()
865 mesh.setMeshDimension(2)
866 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.3]
867 coordsArr=DataArrayDouble(coords,4,2)
868 mesh.setCoords(coordsArr)
869 mesh.allocateCells(0)
870 mesh.finishInsertingCells()
871 #! [PySnippet_MEDCouplingUMesh_renumberNodes_1]
872 #! [PySnippet_MEDCouplingUMesh_renumberNodes_2]
873 mesh.renumberNodes([ 2,1,0,-1 ], 3)
874 coordsArr = mesh.getCoords() # get a shorten array
875 assert coordsArr.getValues() == [0.7,-0.3, 0.2,-0.3, -0.3,-0.3]
876 #! [PySnippet_MEDCouplingUMesh_renumberNodes_2]
877 #! [PySnippet_MEDCouplingUMesh_renumberNodes_3]
878 coordsArr.setValues(coords,4,2) # restore old nodes
879 mesh.renumberNodesCenter([ 2,1,0,2 ], 3)
880 coordsArr = mesh.getCoords() # get a shorten array
881 assert coordsArr.getValues() == [0.7,-0.3, 0.2,-0.3, -0.3,0.0]
882 #! [PySnippet_MEDCouplingUMesh_renumberNodes_3]
885 def testExample_MEDCouplingUMesh_findBoundaryNodes(self):
886 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_1]
887 mesh=MEDCouplingUMesh()
888 mesh.setMeshDimension(2)
889 mesh.allocateCells(5)
890 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
891 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
892 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
893 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
894 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14])
895 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18])
896 mesh.finishInsertingCells()
897 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 ]
898 coordsArr=DataArrayDouble(coords,9,2)
899 mesh.setCoords(coordsArr)
900 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_1]
901 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_2]
902 nodeIdsArr=mesh.findBoundaryNodes()
903 assert nodeIdsArr.getNumberOfTuples() == mesh.getNumberOfNodes() - 1
904 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_2]
907 def testExample_MEDCouplingUMesh_buildBoundaryMesh(self):
908 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_1]
909 mesh=MEDCouplingUMesh()
910 mesh.setMeshDimension(2)
911 mesh.allocateCells(5)
912 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
913 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
914 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
915 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
916 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14])
917 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18])
918 mesh.finishInsertingCells()
919 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 ]
920 coordsArr=DataArrayDouble(coords,9,2)
921 mesh.setCoords(coordsArr)
922 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_1]
923 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_2]
924 mesh1=mesh.buildBoundaryMesh(True)
925 mesh2=mesh.buildBoundaryMesh(False)
926 assert coordsArr.isEqual( mesh1.getCoords(), 1e-13 ) # same nodes
927 assert not coordsArr.isEqual( mesh2.getCoords(), 1e-13 ) # different nodes
928 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_2]
931 def testExample_MEDCouplingUMesh_buildFacePartOfMySelfNode(self):
932 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_1]
933 mesh=MEDCouplingUMesh()
934 mesh.setMeshDimension(2)
935 mesh.allocateCells(5)
936 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
937 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
938 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
939 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
940 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
941 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
942 mesh.finishInsertingCells()
943 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 ]
944 coordsArr=DataArrayDouble(coords,9,2)
945 mesh.setCoords(coordsArr)
946 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_1]
947 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_2]
948 nodeIds = mesh.getNodeIdsOfCell( 0 )
950 mesh1 = mesh.buildFacePartOfMySelfNode( nodeIds, allNodes )
951 assert mesh1.getNumberOfCells() == 4 # 4 segments bounding QUAD4 #0 only
952 mesh2 = mesh.buildFacePartOfMySelfNode( nodeIds, not allNodes )
953 assert mesh2.getNumberOfCells() > 4 # more segments added
954 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_2]
958 def testExample_MEDCouplingUMesh_buildPartOfMySelfNode(self):
959 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_1]
960 mesh=MEDCouplingUMesh()
961 mesh.setMeshDimension(2)
962 mesh.allocateCells(5)
963 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
964 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
965 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
966 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
967 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
968 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
969 mesh.finishInsertingCells()
970 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 ]
971 coordsArr=DataArrayDouble(coords,9,2)
972 mesh.setCoords(coordsArr)
973 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_1]
974 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_2]
975 nodeIds = mesh.getNodeIdsOfCell( 0 )
977 mesh1 = mesh.buildPartOfMySelfNode( nodeIds, allNodes )
978 mesh2 = mesh.buildPartOfMySelfNode( nodeIds, not allNodes )
979 assert mesh1.getNumberOfCells() == 1 # cell #0 is found only
980 assert mesh2.getNumberOfCells() == mesh.getNumberOfCells() # all cells are found
981 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_2]
985 def testExample_MEDCouplingUMesh_getCellIdsLyingOnNodes(self):
986 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_1]
987 mesh=MEDCouplingUMesh()
988 mesh.setMeshDimension(2)
989 mesh.allocateCells(5)
990 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
991 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
992 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
993 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
994 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
995 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
996 mesh.finishInsertingCells()
997 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 ]
998 coordsArr=DataArrayDouble(coords,9,2)
999 mesh.setCoords(coordsArr)
1000 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_1]
1001 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_2]
1002 nodeIds = mesh.getNodeIdsOfCell( 0 )
1004 cellIdsArr1 = mesh.getCellIdsLyingOnNodes( nodeIds, allNodes )
1005 cellIdsArr2 = mesh.getCellIdsLyingOnNodes( nodeIds, not allNodes )
1006 assert cellIdsArr1.getNumberOfTuples() == 1 # cell #0 is found only
1007 assert cellIdsArr2.getNumberOfTuples() == mesh.getNumberOfCells() # all cells are found
1008 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_2]
1012 def testExample_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds(self):
1013 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_1]
1014 mesh=MEDCouplingUMesh()
1015 mesh.setMeshDimension(2)
1016 mesh.allocateCells(5)
1017 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1018 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
1019 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
1020 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
1021 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14])
1022 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18])
1023 mesh.finishInsertingCells()
1024 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 ]
1025 coordsArr=DataArrayDouble(coords,9,2)
1026 mesh.setCoords(coordsArr)
1027 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_1]
1028 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_2]
1030 nodeIds = mesh.getNodeIdsOfCell( cellIds[0] )
1031 nodeIds += mesh.getNodeIdsOfCell( cellIds[1] )
1032 cellIdsArr = mesh.getCellIdsFullyIncludedInNodeIds( nodeIds )
1033 assert cellIdsArr.getValues() == cellIds
1034 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_2]
1038 def testExample_MEDCouplingUMesh_buildPartOfMySelf(self):
1039 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_1]
1040 mesh=MEDCouplingUMesh()
1041 mesh.setMeshDimension(2)
1042 mesh.allocateCells(5)
1043 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1044 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
1045 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
1046 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
1047 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
1048 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
1049 mesh.finishInsertingCells()
1050 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 ]
1051 coordsArr=DataArrayDouble(coords,9,2)
1052 mesh.setCoords(coordsArr)
1053 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_1]
1054 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_2]
1056 mesh2=mesh.buildPartOfMySelf(cellIds, True)
1057 mesh3=mesh.buildPartOfMySelf(cellIds, False)
1058 coordsArr2 = mesh2.getCoords()
1059 assert coordsArr.isEqual( coordsArr2, 1e-13 ) # same nodes
1060 coordsArr3 = mesh3.getCoords()
1061 assert not coordsArr.isEqual( coordsArr3, 1e-13 ) # different nodes
1062 assert mesh2.getNodeIdsOfCell(0) == mesh.getNodeIdsOfCell( cellIds[0]) # cell #1 was copied
1063 assert mesh2.getNodeIdsOfCell(1) == mesh.getNodeIdsOfCell( cellIds[1]) # cell #2 was copied
1064 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_2]
1067 def testExample_MEDCouplingUMesh_mergeNodes(self):
1068 #! [PySnippet_MEDCouplingUMesh_mergeNodes_1]
1069 mesh=MEDCouplingUMesh()
1070 mesh.setMeshDimension(2)
1071 mesh.allocateCells(5)
1072 conn=[0,3,4,1, 1,4,2, 4,5,2]
1073 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
1074 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
1075 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
1076 mesh.finishInsertingCells()
1077 coords=[0.3,-0.301, # 0
1079 0.3,-0.302, # 2 ~~ 0
1083 coordsArr=DataArrayDouble(coords,6,2)
1084 mesh.setCoords(coordsArr)
1085 #! [PySnippet_MEDCouplingUMesh_mergeNodes_1]
1086 #! [PySnippet_MEDCouplingUMesh_mergeNodes_2]
1087 arr,areNodesMerged,newNbOfNodes=mesh.mergeNodes(0.004)
1088 assert arr.getValues() == [0, 1, 0, 2, 2, 0]
1089 assert areNodesMerged
1090 assert newNbOfNodes == 3
1091 #! [PySnippet_MEDCouplingUMesh_mergeNodes_2]
1092 #! [PySnippet_MEDCouplingUMesh_mergeNodes_3]
1093 baryCoords2 = coords[2*2:] # initial coordinates of node #2
1094 coordsArr = mesh.getCoords() # retrieve a new shorten coord array
1095 self.assertNotAlmostEqual( baryCoords2[1], coordsArr.getIJ(0,1), 13 ) # Y of node #0 differs from that of baryCoords2
1096 # restore coordinates
1097 coordsArr = DataArrayDouble(coords,6,2)
1098 mesh.setCoords(coordsArr)
1099 # call mergeNodesCenter()
1100 mesh.mergeNodesCenter(0.004)
1101 coordsArr = mesh.getCoords() # retrieve a new shorten coord array
1102 self.assertAlmostEqual( baryCoords2[1], coordsArr.getIJ(0,1), 13 ) # Y of node #0 equals to that of baryCoords2
1103 #! [PySnippet_MEDCouplingUMesh_mergeNodes_3]
1106 def testExample_MEDCouplingUMesh_zipConnectivityTraducer(self):
1107 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_1]
1108 mesh=MEDCouplingUMesh()
1109 mesh.setMeshDimension(2)
1110 mesh.allocateCells(5)
1111 conn=[0,3,4,1, 1,4,2]
1112 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
1113 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
1114 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 2 == 1
1115 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 3 == 0
1116 mesh.insertNextCell(NORM_QUAD4,4,conn[2:4]+conn[0:2]) # 4 ~~ 0
1117 mesh.finishInsertingCells()
1118 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 ]
1119 coordsArr=DataArrayDouble(coords,9,2)
1120 mesh.setCoords(coordsArr)
1121 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_1]
1122 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_2]
1123 oldNbCells = mesh.getNumberOfCells()
1124 arr = mesh.zipConnectivityTraducer(0)
1125 assert mesh.getNumberOfCells() == oldNbCells-2
1126 assert arr.getValues() == [0, 1, 1, 0, 2]
1127 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_2]
1130 def testExample_MEDCouplingUMesh_zipCoordsTraducer(self):
1131 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_1]
1132 mesh=MEDCouplingUMesh()
1133 mesh.setMeshDimension(2)
1134 mesh.allocateCells(5)
1135 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1136 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
1137 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
1138 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
1139 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14])
1140 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18])
1141 mesh.finishInsertingCells()
1142 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 ]
1143 coordsArr=DataArrayDouble(coords,9,2)
1144 mesh.setCoords(coordsArr)
1145 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_1]
1146 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_2]
1148 mesh2=mesh.buildPartOfMySelf(cellIds,True)
1149 arr=mesh2.zipCoordsTraducer()
1150 assert mesh2.getNumberOfNodes() == 4 # nb of nodes decreased
1151 assert arr.getValues() == [-1,0,1,-1,2,3,-1,-1,-1] # -1 for unused nodes
1152 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_2]
1155 def testExample_MEDCouplingUMesh_getNodeIdsInUse(self):
1156 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_1]
1157 mesh=MEDCouplingUMesh()
1158 mesh.setMeshDimension(2)
1159 mesh.allocateCells(5)
1160 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1161 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4])
1162 mesh.insertNextCell(NORM_TRI3,3, conn[4:7])
1163 mesh.insertNextCell(NORM_TRI3,3, conn[7:10])
1164 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14])
1165 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18])
1166 mesh.finishInsertingCells()
1167 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 ]
1168 coordsArr=DataArrayDouble(coords,9,2)
1169 mesh.setCoords(coordsArr)
1170 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_1]
1171 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_2]
1173 mesh2=mesh.buildPartOfMySelf(cellIds,True)
1174 arr,newNbOfNodes=mesh2.getNodeIdsInUse()
1175 assert arr.getValues() == [-1,0,1,-1,2,3,-1,-1,-1]
1176 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_2]
1177 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_3]
1178 arr2=arr.invertArrayO2N2N2O(newNbOfNodes)
1179 assert arr2.getValues() == [1,2,4,5]
1180 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_3]
1183 def testExample_MEDCouplingUMesh_convertToPolyTypes(self):
1184 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_1]
1185 mesh=MEDCouplingUMesh()
1186 mesh.setMeshDimension(2)
1187 mesh.allocateCells(5)
1188 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1189 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
1190 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
1191 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
1192 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
1193 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
1194 mesh.finishInsertingCells()
1195 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 ]
1196 coordsArr=DataArrayDouble(coords,9,2)
1197 mesh.setCoords(coordsArr)
1198 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_1]
1199 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_2]
1201 mesh.convertToPolyTypes(cells)
1202 assert mesh.getTypeOfCell(0) == NORM_QUAD4
1203 assert mesh.getTypeOfCell(1) == NORM_POLYGON, mesh.getTypeOfCell(1)
1204 assert mesh.getTypeOfCell(2) == NORM_TRI3
1205 assert mesh.getTypeOfCell(3) == NORM_POLYGON
1206 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_2]
1209 def testExample_MEDCouplingUMesh_buildDescendingConnectivity2(self):
1210 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_1]
1211 mesh=MEDCouplingUMesh()
1212 mesh.setMeshDimension(2)
1213 mesh.allocateCells(5)
1214 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1215 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
1216 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
1217 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
1218 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
1219 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
1220 mesh.finishInsertingCells()
1221 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 ]
1222 coordsArr=DataArrayDouble(coords,9,2)
1223 mesh.setCoords(coordsArr)
1224 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_1]
1225 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_2]
1226 mesh2,desc,descIndx,revDesc,revDescIndx=mesh.buildDescendingConnectivity2()
1227 assert desc.getValues() == [1,2,3,4,-3,5,6, 7,8,-5,9,10,-2,11, 12,13,-7,-10]
1228 assert descIndx.getValues() == [0,4,7,10,14,18]
1229 assert revDesc.getValues() == [0, 0,3, 0,1, 0, 1,2, 1, 2,4, 2, 3, 3,4, 3, 4, 4]
1230 assert revDescIndx.getValues() == [0,1,3,5,6,8,9,11,12,13,15,16,17,18]
1231 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_2]
1232 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_3]
1233 assert mesh2.getNodeIdsOfCell( 3-1 ) == [4, 1] # cell #3 in FORTRAN mode
1234 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_3]
1237 def testExample_MEDCouplingUMesh_buildDescendingConnectivity(self):
1238 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_1]
1239 mesh=MEDCouplingUMesh()
1240 mesh.setMeshDimension(2)
1241 mesh.allocateCells(5)
1242 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1243 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
1244 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
1245 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
1246 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
1247 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
1248 mesh.finishInsertingCells()
1249 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 ]
1250 coordsArr=DataArrayDouble(coords,9,2)
1251 mesh.setCoords(coordsArr)
1252 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_1]
1253 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_2]
1254 mesh2,desc,descIndx,revDesc,revDescIndx=mesh.buildDescendingConnectivity()
1255 assert desc.getValues() == [0,1,2,3, 2,4,5, 6,7,4, 8,9,1,10, 11,12,6,9]
1256 assert descIndx.getValues() == [0,4,7,10,14,18]
1257 assert revDesc.getValues() == [0, 0,3, 0,1, 0, 1,2, 1, 2,4, 2, 3, 3,4, 3, 4, 4]
1258 assert revDescIndx.getValues() == [0,1,3,5,6,8,9,11,12,13,15,16,17,18]
1259 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_2]
1262 def testExample_MEDCouplingUMesh_getReverseNodalConnectivity(self):
1263 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_1]
1264 mesh=MEDCouplingUMesh()
1265 mesh.setMeshDimension(2)
1266 mesh.allocateCells(5)
1267 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
1268 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]) # 0
1269 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]) # 1
1270 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]) # 2
1271 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]) # 3
1272 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]) # 4
1273 mesh.finishInsertingCells()
1274 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 ]
1275 coordsArr=DataArrayDouble(coords,9,2)
1276 mesh.setCoords(coordsArr)
1277 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_1]
1278 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_2]
1279 revNodal,revNodalIndx=mesh.getReverseNodalConnectivity()
1280 assert revNodal.getValues() == [0,0,1,1,2,0,3,0,1,2,3,4,2,4,3,3,4,4]
1281 assert revNodalIndx.getValues() == [0,1,3,5,7,12,14,15,17,18]
1282 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_2]
1285 def testExample_MEDCouplingUMesh_checkDeepEquivalWith(self):
1286 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_1]
1288 mesh1=MEDCouplingUMesh()
1289 mesh1.setMeshDimension(2)
1294 coordsArr=DataArrayDouble(coords,4,2)
1295 mesh1.setCoords(coordsArr)
1296 mesh1.allocateCells(2)
1297 mesh1.insertNextCell(NORM_TRI3,3,[0,1,2]) #0
1298 mesh1.insertNextCell(NORM_TRI3,3,[1,2,3]) #1
1299 mesh1.finishInsertingCells()
1301 mesh2=MEDCouplingUMesh()
1302 mesh2.setMeshDimension(2)
1303 coords=[0.0,1.0, #0 = #3
1307 coordsArr2=DataArrayDouble(coords,4,2)
1308 mesh2.setCoords(coordsArr2)
1309 mesh2.allocateCells(2)
1310 mesh2.insertNextCell(NORM_TRI3,3,[2,3,0]) #0 = #1
1311 mesh2.insertNextCell(NORM_TRI3,3,[3,1,2]) #1 ~ #0
1312 mesh2.finishInsertingCells()
1313 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_1]
1314 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_2]
1315 cellCompPol = 1 # "permuted same orientation" - policy of medium severity
1316 cOld2New, nOld2New = mesh1.checkDeepEquivalWith( mesh2, cellCompPol, 0.002 )
1317 assert nOld2New.getValues() == [3, 0, 1, 2]
1318 assert cOld2New.getValues() == [1, 0]
1319 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_2]
1320 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_3]
1321 self.assertRaises( InterpKernelException, mesh1.checkDeepEquivalOnSameNodesWith, mesh2, cellCompPol, 0.002)
1322 mesh2.setCoords(coordsArr) # make meshes share the same coordinates array
1323 mesh2.allocateCells(2)
1324 mesh2.insertNextCell(NORM_TRI3,3,[1,2,3]) #0 = #1
1325 mesh2.insertNextCell(NORM_TRI3,3,[1,0,2]) #1 ~ #0
1326 mesh2.finishInsertingCells()
1327 cellCompPol = 2 # the weakest policy
1328 mesh1.checkDeepEquivalOnSameNodesWith( mesh2, cellCompPol, 0 )
1329 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_3]
1332 def testExample_MEDCouplingPointSet_scale(self):
1333 #! [PySnippet_MEDCouplingPointSet_scale_1]
1334 coords=[0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0] # 2D coordinates of 4 nodes
1335 coordsArr=DataArrayDouble(coords,4,2)
1336 mesh=MEDCouplingUMesh()
1337 mesh.setCoords(coordsArr)
1338 initCoords = coordsArr.deepCopy()
1339 #! [PySnippet_MEDCouplingPointSet_scale_1]
1340 #! [PySnippet_MEDCouplingPointSet_scale_2]
1343 mesh.scale(center,factor)
1344 #! [PySnippet_MEDCouplingPointSet_scale_2]
1345 #! [PySnippet_MEDCouplingPointSet_scale_3]
1346 coords2 = mesh.getCoords()
1347 assert coords2.isEqualWithoutConsideringStr( initCoords, 1.0 )
1348 assert not coords2.isEqualWithoutConsideringStr( initCoords, 0.9 )
1349 #! [PySnippet_MEDCouplingPointSet_scale_3]
1352 def testExample_MEDCouplingPointSet_translate(self):
1353 #! [PySnippet_MEDCouplingPointSet_translate_1]
1354 coords=[0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0] # 2D coordinates of 4 nodes
1355 coordsArr=DataArrayDouble(coords,4,2)
1356 mesh=MEDCouplingUMesh()
1357 mesh.setCoords(coordsArr)
1358 initCoords = coordsArr.deepCopy()
1359 #! [PySnippet_MEDCouplingPointSet_translate_1]
1360 #! [PySnippet_MEDCouplingPointSet_translate_2]
1362 mesh.translate(vector)
1363 #! [PySnippet_MEDCouplingPointSet_translate_2]
1364 #! [PySnippet_MEDCouplingPointSet_translate_3]
1365 coords2 = mesh.getCoords()
1366 assert coords2.isEqualWithoutConsideringStr( initCoords, 1 )
1367 assert not coords2.isEqualWithoutConsideringStr( initCoords, 0.9 )
1368 #! [PySnippet_MEDCouplingPointSet_translate_3]
1371 def testExample_MEDCouplingPointSet_rotate(self):
1372 #! [PySnippet_MEDCouplingPointSet_rotate_1]
1373 coords=[0.0,0.0, 0.1,0.0, 0.1,0.1, 0.0,0.1] # 2D coordinates of 4 nodes
1374 coordsArr=DataArrayDouble(coords,4,2)
1375 mesh=MEDCouplingUMesh()
1376 mesh.setCoords(coordsArr)
1377 #! [PySnippet_MEDCouplingPointSet_rotate_1]
1378 #! [PySnippet_MEDCouplingPointSet_rotate_2]
1380 mesh.rotate(center,-pi/2)
1381 #! [PySnippet_MEDCouplingPointSet_rotate_2]
1382 #! [PySnippet_MEDCouplingPointSet_rotate_3]
1383 mesh.changeSpaceDimension(3)
1386 mesh.rotate(center,vector,pi/2)
1387 #! [PySnippet_MEDCouplingPointSet_rotate_3]
1388 #! [PySnippet_MEDCouplingPointSet_rotate_4]
1389 mesh.changeSpaceDimension(2)
1390 coords2 = mesh.getCoords()
1391 for i,c in enumerate( coords ):
1392 self.assertAlmostEqual( c, coords2.getIJ(0,i), 13 )
1393 #! [PySnippet_MEDCouplingPointSet_rotate_4]
1396 def testExample_MEDCouplingPointSet_getBoundingBox(self):
1397 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_1]
1398 cc=[0.0, 0.1, 0.2, # 3D coordinates of 2 nodes
1400 coordsArr=DataArrayDouble(cc,2,3)
1401 mesh=MEDCouplingUMesh()
1402 mesh.setCoords(coordsArr)
1403 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_1]
1404 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_2]
1405 bbox=mesh.getBoundingBox()
1406 assert bbox == [( cc[0], cc[3] ), # NOTE: list of 3 tuples is retirned!
1409 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_2]
1411 def testExample_MEDCouplingPointSet_getNodeIdsNearPoint(self):
1412 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_1]
1413 # 2D coordinates of 5 nodes
1414 coords=[0.3,-0.301, # 0
1418 0.3,-0.30299999999999]# 4
1419 coordsArr=DataArrayDouble(coords,5,2)
1420 mesh=MEDCouplingUMesh()
1421 mesh.setCoords(coordsArr)
1422 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_1]
1423 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_2]
1424 point=[0.3, -0.3] # point close to nodes #0, #2 and #4
1425 ids=mesh.getNodeIdsNearPoint(point,0.003)
1426 assert ids.getValues() == [0,2,4]
1427 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_2]
1430 def testExample_MEDCouplingPointSet_getNodeIdsNearPoints(self):
1431 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_1]
1432 # 2D coordinates of 7 nodes
1433 coords=[0.3,-0.301, # 0
1440 coordsArr=DataArrayDouble(coords,7,2)
1441 mesh=MEDCouplingUMesh()
1442 mesh.setCoords(coordsArr)
1443 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_1]
1444 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_2]
1445 points=[0.2,-0.301, # ~ node #1
1447 1.1, 0.002] # ~ nodes #3, #4 and #5
1448 ids,idsIndex=mesh.getNodeIdsNearPoints(points,3,0.003)
1449 assert ids.getValues() == [1, 3, 4, 5]
1450 assert idsIndex.getValues() == [0, 1, 1, 4]
1451 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_2]
1454 def testExample_MEDCouplingPointSet_findCommonNodes(self):
1455 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_1]
1456 coords=[0.3,-0.301, # 0
1462 coordsArr=DataArrayDouble(coords,6,2)
1463 mesh=MEDCouplingUMesh()
1464 mesh.setCoords(coordsArr)
1465 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_1]
1466 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_2]
1467 comm,commI=mesh.findCommonNodes(1e-13)
1468 assert comm.getValues() == [3,4]
1469 comm,commI=mesh.findCommonNodes(0.004)
1470 assert comm.getValues() == [0,2,5,3,4]
1471 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_2]
1474 def testExample_MEDCouplingPointSet_getCoordinatesOfNode(self):
1475 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_1]
1476 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3]
1477 coordsArr=DataArrayDouble(coords,3,2)
1478 mesh=MEDCouplingUMesh()
1479 mesh.setCoords(coordsArr)
1480 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_1]
1481 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_2]
1482 nodeCoords=mesh.getCoordinatesOfNode(1)
1483 self.assertAlmostEqual(0.2, nodeCoords[0],13)
1484 self.assertAlmostEqual(-0.3,nodeCoords[1],13)
1485 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_2]
1488 def testExample_DataArrayInt_getTuple(self):
1489 #! [Snippet_DataArrayInt_getTuple_1]
1494 assert dv.getTuple( 1 ) == [9,10]
1495 #! [Snippet_DataArrayInt_getTuple_1]
1496 #! [Snippet_DataArrayInt_getTuple_2]
1499 #! [Snippet_DataArrayInt_getTuple_2]
1502 def testExample_DataArrayInt_buildPermutationArr(self):
1503 #! [PySnippet_DataArrayInt_buildPermutationArr_1]
1505 a.setValues([4,5,6,7,8],5,1)
1507 b.setValues([5,4,8,6,7],5,1)
1508 c=a.buildPermutationArr(b)
1509 #! [PySnippet_DataArrayInt_buildPermutationArr_1]
1510 self.assertEqual([1,0,4,2,3],c.getValues())
1513 def testExample_DataArrayInt_invertArrayO2N2N2O(self):
1514 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
1517 da.setValues(arr1,6,1)
1518 da2=da.invertArrayO2N2N2O(6)
1519 expected1=[1,3,0,5,2,4]
1521 self.assertEqual(expected1[i],da2.getIJ(i,0))
1523 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
1526 def testExample_DataArrayInt_invertArrayN2O2O2N(self):
1527 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
1530 da.setValues(arr1,6,1)
1531 da2=da.invertArrayN2O2O2N(7)
1532 expected1=[1,3,0,5,2,4,-1]
1534 self.assertEqual(expected1[i],da2.getIJ(i,0))
1536 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
1540 def testExample_DataArrayDouble_getIdsInRange(self):
1541 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
1542 da=DataArrayDouble()
1544 da[ :, :] = list(range(10))
1545 da2 = da.findIdsInRange( 2.5, 6 )
1546 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
1549 def testExample_DataArrayDouble_setPartOfValues2(self):
1550 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
1551 da=DataArrayDouble()
1554 dv=DataArrayDouble()
1558 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
1559 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
1561 da[ [0,1,2], [1,3] ] = dv
1562 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
1563 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
1566 da[ [0,2,3], [0,2,3,4,5,6]] = dv
1567 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
1570 def testExample_DataArrayInt_setPartOfValues2(self):
1571 #! [Snippet_DataArrayInt_setPartOfValues2_1]
1579 #! [Snippet_DataArrayInt_setPartOfValues2_1]
1580 #! [Snippet_DataArrayInt_setPartOfValues2_2]
1582 da[ [0,1,2], [1,3] ] = dv
1583 #! [Snippet_DataArrayInt_setPartOfValues2_2]
1584 #! [Snippet_DataArrayInt_setPartOfValues2_3]
1587 da[ [0,2,3], [0,2,3,4,5,6]] = dv
1588 #! [Snippet_DataArrayInt_setPartOfValues2_3]
1591 def testExample_DataArrayDouble_setPartOfValues3(self):
1592 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
1593 da=DataArrayDouble()
1596 dv=DataArrayDouble()
1600 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
1601 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
1603 da[ 0:3, [1,3] ] = dv
1604 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
1605 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
1608 da[ 0:4:2, [0,2,3,4,5,6]] = dv
1609 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
1612 def testExample_DataArrayInt_setPartOfValues3(self):
1613 #! [Snippet_DataArrayInt_setPartOfValues3_1]
1621 #! [Snippet_DataArrayInt_setPartOfValues3_1]
1622 #! [Snippet_DataArrayInt_setPartOfValues3_2]
1624 da[ 0:3, [1,3] ] = dv
1625 #! [Snippet_DataArrayInt_setPartOfValues3_2]
1626 #! [Snippet_DataArrayInt_setPartOfValues3_3]
1629 da[ 0:4:2, [0,2,3,4,5,6]] = dv
1630 #! [Snippet_DataArrayInt_setPartOfValues3_3]
1633 def testExample_DataArrayDouble_setPartOfValues1(self):
1634 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
1635 da=DataArrayDouble()
1637 da.setInfoOnComponents( ["v1","v2","v3","v4"])
1639 dv=DataArrayDouble()
1643 dv.setInfoOnComponents( ["a1","a2"])
1644 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
1645 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
1647 da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
1648 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
1649 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
1651 da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
1652 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
1653 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
1655 da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
1656 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
1657 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
1659 da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
1660 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
1661 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
1664 da2[ 0:3:2, 1:4:2 ] = dv
1665 self.assertTrue( da.isEqual( da2, 1e-20 ))
1666 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
1669 def testExample_DataArrayInt_setPartOfValues1(self):
1670 #! [Snippet_DataArrayInt_setPartOfValues1_1]
1673 da.setInfoOnComponents( ["v1","v2","v3","v4"])
1679 dv.setInfoOnComponents( ["a1","a2"])
1680 #! [Snippet_DataArrayInt_setPartOfValues1_1]
1681 #! [Snippet_DataArrayInt_setPartOfValues1_2]
1683 da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
1684 #! [Snippet_DataArrayInt_setPartOfValues1_2]
1685 #! [Snippet_DataArrayInt_setPartOfValues1_3]
1687 da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
1688 #! [Snippet_DataArrayInt_setPartOfValues1_3]
1689 #! [Snippet_DataArrayInt_setPartOfValues1_4]
1691 da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
1692 #! [Snippet_DataArrayInt_setPartOfValues1_4]
1693 #! [Snippet_DataArrayInt_setPartOfValues1_5]
1695 da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
1696 #! [Snippet_DataArrayInt_setPartOfValues1_5]
1697 #! [Snippet_DataArrayInt_setPartOfValues1_6]
1700 da2[ 0:3:2, 1:4:2 ] = dv
1701 self.assertTrue( da.isEqual( da2 ))
1702 #! [Snippet_DataArrayInt_setPartOfValues1_6]
1705 def testExample_DataArrayDouble_setPartOfValuesSimple1(self):
1706 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
1707 da=DataArrayDouble()
1710 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
1711 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
1713 da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
1714 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
1715 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
1717 da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
1718 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
1719 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
1721 da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
1722 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
1723 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
1725 da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
1726 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
1727 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
1730 da2[ 0:3:2, 1:4:2 ] = dv
1731 self.assertTrue( da.isEqual( da2, 1e-20 ))
1732 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
1735 def testExample_DataArrayInt_setPartOfValuesSimple1(self):
1736 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
1740 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
1741 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
1743 da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
1744 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
1745 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
1747 da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
1748 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
1749 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
1751 da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
1752 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
1753 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
1755 da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
1756 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
1757 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
1760 da2[ 0:3:2, 1:4:2 ] = dv
1761 self.assertTrue( da.isEqual( da2 ))
1762 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
1765 def testExample_DataArrayDouble_setPartOfValuesSimple2(self):
1766 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
1767 da=DataArrayDouble()
1770 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
1771 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
1773 da[[1,2], [1,2]] = dv
1774 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
1775 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
1777 da[[0,1,2,3], [1]] = dv
1778 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
1779 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
1781 da[[1], [0,1,2,3]] = dv
1782 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
1783 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
1785 da[[0,2], [1,3]] = dv
1786 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
1789 def testExample_DataArrayInt_setPartOfValuesSimple2(self):
1790 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
1794 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
1795 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
1797 da[[1,2], [1,2]] = dv
1798 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
1799 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
1801 da[[0,1,2,3], [1]] = dv
1802 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
1803 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
1805 da[[1], [0,1,2,3]] = dv
1806 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
1807 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
1809 da[[0,2], [1,3]] = dv
1810 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
1813 def testExample_DataArrayDouble_setPartOfValuesSimple3(self):
1814 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
1815 da=DataArrayDouble()
1818 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
1819 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
1822 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
1823 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
1825 da[[0,1,2,3], 1:2] = dv
1826 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
1827 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
1830 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
1831 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
1833 da[[0,2], 1:4:2] = dv
1834 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
1837 def testExample_DataArrayInt_setPartOfValuesSimple3(self):
1838 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
1842 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
1843 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
1846 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
1847 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
1849 da[[0,1,2,3], 1:2] = dv
1850 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
1851 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
1854 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
1855 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
1857 da[[0,2], 1:4:2] = dv
1858 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
1861 def testExample_DataArrayDouble_setSelectedComponents(self):
1862 #! [Snippet_DataArrayDouble_setSelectedComponents1]
1863 array1=[1.,2., 3.,4., 5.,6.]
1864 da=DataArrayDouble(array1,3,2)
1865 da.setInfoOnComponents( ["a1","a2"])
1866 #! [Snippet_DataArrayDouble_setSelectedComponents1]
1867 #! [Snippet_DataArrayDouble_setSelectedComponents2]
1868 dv=DataArrayDouble()
1871 dv.setInfoOnComponents( ["v1","v2","v3","v4"])
1873 dv.setSelectedComponents( da, [1,0] )
1874 #! [Snippet_DataArrayDouble_setSelectedComponents2]
1875 #! [Snippet_DataArrayDouble_setSelectedComponents3]
1877 self.assertTrue( dv.isEqualWithoutConsideringStr( dv2, 1e-20 ))
1878 #! [Snippet_DataArrayDouble_setSelectedComponents3]
1881 def testExample_DataArrayInt_setSelectedComponents(self):
1882 #! [Snippet_DataArrayInt_setSelectedComponents1]
1884 array1=[1,2, 3,4, 5,6]
1885 da.setValues(array1,3,2)
1886 da.setInfoOnComponents( ["a1","a2"])
1887 #! [Snippet_DataArrayInt_setSelectedComponents1]
1888 #! [Snippet_DataArrayInt_setSelectedComponents2]
1892 dv.setInfoOnComponents( ["v1","v2","v3","v4"])
1894 dv.setSelectedComponents( da, [1,0] )
1895 #! [Snippet_DataArrayInt_setSelectedComponents2]
1896 #! [Snippet_DataArrayInt_setSelectedComponents3]
1898 self.assertTrue( dv.isEqualWithoutConsideringStr( dv2 ))
1899 #! [Snippet_DataArrayInt_setSelectedComponents3]
1902 def testExample_DataArrayDouble_getDifferentValues(self):
1903 #! [Snippet_DataArrayDouble_getDifferentValues1]
1904 array1=[2.3,1.2,1.3,2.3,2.301,0.8]
1905 da=DataArrayDouble(array1,6,1)
1907 dv=da.getDifferentValues(2e-1)
1908 expected2=[2.301,1.3,0.8]
1909 self.assertEqual(3,dv.getNbOfElems())
1911 self.assertAlmostEqual(expected2[i],dv.getIJ(i,0),14)
1913 #! [Snippet_DataArrayDouble_getDifferentValues1]
1916 def testExample_DataArrayDouble_findCommonTuples1(self):
1917 #! [PySnippet_DataArrayDouble_findCommonTuples1]
1918 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]
1919 da=DataArrayDouble(array2,6,2)
1920 #! [PySnippet_DataArrayDouble_findCommonTuples1]
1921 #! [PySnippet_DataArrayDouble_findCommonTuples2]
1922 c,cI=da.findCommonTuples(1.01e-1)
1923 expected3=[0,3,4,1,2]
1925 self.assertEqual(expected3,c.getValues())
1926 self.assertEqual(expected4,cI.getValues())
1927 #! [PySnippet_DataArrayDouble_findCommonTuples2]
1930 def testExampleDataArrayDoubleMeldWith(self):
1931 #! [PySnippet_DataArrayDouble_Meld1_1]
1932 da1=DataArrayDouble()
1934 da2=DataArrayDouble()
1937 da1.fillWithValue(7.)
1939 da3=da2.applyFunc(3,"10*x*IVec+100*x*JVec+1000*x*KVec")
1941 da1.setInfoOnComponent(0,"c0da1")
1942 da1.setInfoOnComponent(1,"c1da1")
1943 da3.setInfoOnComponent(0,"c0da3")
1944 da3.setInfoOnComponent(1,"c1da3")
1945 da3.setInfoOnComponent(2,"c2da3")
1949 #! [PySnippet_DataArrayDouble_Meld1_1]
1951 def testExampleDataArrayIntMeldWith(self):
1952 #! [PySnippet_DataArrayInt_Meld1_1]
1958 da1.fillWithValue(7)
1961 da1.setInfoOnComponent(0,"c0da1")
1962 da1.setInfoOnComponent(1,"c1da1")
1963 da2.setInfoOnComponent(0,"c0da2")
1966 #! [PySnippet_DataArrayInt_Meld1_1]
1968 def testExampleDataArrayDoubleKeepSelectedComponents1(self):
1969 #! [SnippeDataArrayDoubleKeepSelectedComponents1_1]
1970 arr1=[1.,2.,3.,4., # tuple 0
1971 11.,12.,13.,14., # tuple 1
1972 21.,22.,23.,24., # ...
1975 a1=DataArrayDouble(arr1,5,4)
1976 a1.setInfoOnComponent(0,"a")
1977 a1.setInfoOnComponent(1,"b")
1978 a1.setInfoOnComponent(2,"c")
1979 a1.setInfoOnComponent(3,"d")
1980 #! [SnippeDataArrayDoubleKeepSelectedComponents1_1]
1981 #! [SnippeDataArrayDoubleKeepSelectedComponents1_2]
1983 a2=a1.keepSelectedComponents(arr2V)
1984 #! [SnippeDataArrayDoubleKeepSelectedComponents1_2]
1987 def testExampleDataArrayIntKeepSelectedComponents1(self):
1988 #! [SnippeDataArrayIntKeepSelectedComponents1_1]
1989 arr1=[1,2,3,4, # tuple 0
1990 11,12,13,14, # tuple 1
1995 a1.setValues(arr1,5,4)
1996 a1.setInfoOnComponent(0,"a")
1997 a1.setInfoOnComponent(1,"b")
1998 a1.setInfoOnComponent(2,"c")
1999 a1.setInfoOnComponent(3,"d")
2000 #! [SnippeDataArrayIntKeepSelectedComponents1_1]
2001 #! [SnippeDataArrayIntKeepSelectedComponents1_2]
2003 a2=a1.keepSelectedComponents(arr2V)
2004 #! [SnippeDataArrayIntKeepSelectedComponents1_2]
2005 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
2007 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
2010 def testExampleFieldDoubleBuildSubPart1(self):
2011 from MEDCouplingDataForTest import MEDCouplingDataForTest
2012 #! [PySnippetFieldDoubleBuildSubPart1_1]
2013 mesh1=MEDCouplingDataForTest.build2DTargetMesh_1()
2014 f1=MEDCouplingFieldDouble(ON_CELLS,ONE_TIME)
2017 arr1=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.]
2018 array=DataArrayDouble(arr1,mesh1.getNumberOfCells(),2)
2020 # ! [PySnippetFieldDoubleBuildSubPart1_1]
2021 # ! [PySnippetFieldDoubleBuildSubPart1_2]
2023 f2=f1.buildSubPart(part1)
2024 # ! [PySnippetFieldDoubleBuildSubPart1_2]
2026 self.assertEqual(3,f2.getNumberOfTuples())
2027 self.assertEqual(2,f2.getNumberOfComponents())
2028 expected1=[5.,105.,4.,104.,7.,107.]
2030 self.assertAlmostEqual(f2.getIJ(0,i),expected1[i],12)
2032 self.assertEqual(3,f2.getMesh().getNumberOfCells())
2033 self.assertEqual(6,f2.getMesh().getNumberOfNodes())
2034 self.assertEqual(2,f2.getMesh().getSpaceDimension())
2035 self.assertEqual(2,f2.getMesh().getMeshDimension())
2037 self.assertEqual(13,m2C.getNodalConnectivityArrayLen())
2038 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]
2040 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
2042 expected3=[3,2,3,1,3,0,2,1,4,4,5,3,2]
2043 self.assertEqual(expected3,list(m2C.getNodalConnectivity().getValues()))
2044 expected4=[0,4,8,13]
2045 self.assertEqual(expected4,list(m2C.getNodalConnectivityIndex().getValues()))
2046 # Test with field on nodes.
2047 # ! [PySnippetFieldDoubleBuildSubPart1_3]
2048 f1=MEDCouplingFieldDouble(ON_NODES,ONE_TIME)
2051 arr2=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.,8.,108.,9.,109.,10.,110.,11.,111.]
2052 array=DataArrayDouble(arr2,mesh1.getNumberOfNodes(),2)
2054 # ! [PySnippetFieldDoubleBuildSubPart1_3]
2055 # ! [PySnippetFieldDoubleBuildSubPart1_4]
2057 f2=f1.buildSubPart(part2)
2058 # ! [PySnippetFieldDoubleBuildSubPart1_4]
2059 self.assertEqual(4,f2.getNumberOfTuples())
2060 self.assertEqual(2,f2.getNumberOfComponents())
2061 expected5=[4.,104.,5.,105.,7.,107.,8.,108.]
2063 self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
2065 self.assertEqual(2,f2.getMesh().getNumberOfCells())
2066 self.assertEqual(4,f2.getMesh().getNumberOfNodes())
2067 self.assertEqual(2,f2.getMesh().getSpaceDimension())
2068 self.assertEqual(2,f2.getMesh().getMeshDimension())
2070 self.assertEqual(8,m2C.getNodalConnectivityArrayLen())
2071 for i in range(8): # 8 is not an error
2072 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
2074 self.assertEqual(expected3[:4],[int(i) for i in m2C.getNodalConnectivity()][4:])
2075 self.assertEqual(expected3[4:8],[int(i) for i in m2C.getNodalConnectivity()][:4])
2076 self.assertEqual(expected4[:3],[int(i) for i in m2C.getNodalConnectivityIndex()])
2077 #idem previous because nodes of cell#4 are not fully present in part3
2080 arrr.setValues(part3,2,1)
2081 f2=f1.buildSubPart(arrr)
2082 self.assertEqual(4,f2.getNumberOfTuples())
2083 self.assertEqual(2,f2.getNumberOfComponents())
2085 self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
2087 self.assertEqual(2,f2.getMesh().getNumberOfCells())
2088 self.assertEqual(4,f2.getMesh().getNumberOfNodes())
2089 self.assertEqual(2,f2.getMesh().getSpaceDimension())
2090 self.assertEqual(2,f2.getMesh().getMeshDimension())
2092 self.assertEqual(8,m2C.getNodalConnectivityArrayLen())
2093 for i in range(8): # 8 is not an error
2094 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
2096 self.assertEqual(expected3[:4],[int(i) for i in m2C.getNodalConnectivity()][4:8])
2097 self.assertEqual(expected3[4:8],[int(i) for i in m2C.getNodalConnectivity()][:4])
2098 self.assertEqual(expected4[:3],m2C.getNodalConnectivityIndex().getValues())
2100 f2=f1.buildSubPart(part4)
2101 self.assertEqual(6,f2.getNumberOfTuples())
2102 self.assertEqual(2,f2.getNumberOfComponents())
2103 expected6=[4.,104.,5.,105.,7.,107.,8.,108.,10.,110.,11.,111.]
2105 self.assertAlmostEqual(f2.getIJ(0,i),expected6[i],12)
2107 self.assertEqual(3,f2.getMesh().getNumberOfCells())
2108 self.assertEqual(6,f2.getMesh().getNumberOfNodes())
2109 self.assertEqual(2,f2.getMesh().getSpaceDimension())
2110 self.assertEqual(2,f2.getMesh().getMeshDimension())
2112 self.assertEqual(13,m2C.getNodalConnectivityArrayLen())
2114 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
2116 self.assertEqual(expected3[0:4],m2C.getNodalConnectivity().getValues()[4:8])
2117 self.assertEqual(expected3[4:8],m2C.getNodalConnectivity().getValues()[0:4])
2118 self.assertEqual(expected3[8:13],m2C.getNodalConnectivity().getValues()[8:13])
2119 self.assertEqual(expected4,m2C.getNodalConnectivityIndex().getValues())
2120 # previous line equivalent to
2121 self.assertEqual(expected4,[int(i) for i in m2C.getNodalConnectivityIndex()])
2124 def testExampleUMeshStdBuild1(self):
2125 # ! [PySnippetUMeshStdBuild1_1]
2126 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.,
2127 0.7,0.2,0., -0.3,0.7,0., 0.2,0.7,0., 0.7,0.7,0. ]
2128 nodalConnPerCell=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
2129 # ! [PySnippetUMeshStdBuild1_1]
2130 # ! [PySnippetUMeshStdBuild1_2]
2131 mesh=MEDCouplingUMesh("My2DMesh",2)
2132 # ! [PySnippetUMeshStdBuild1_2]
2133 # ! [PySnippetUMeshStdBuild1_3]
2134 mesh.allocateCells(5)#You can put more than 5 if you want but not less.
2136 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[:4])
2137 mesh.insertNextCell(NORM_TRI3,nodalConnPerCell[4:7])
2138 mesh.insertNextCell(NORM_TRI3,nodalConnPerCell[7:10])
2139 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[10:14])
2140 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[14:])
2142 mesh.finishInsertingCells()
2143 # ! [PySnippetUMeshStdBuild1_3]
2144 # ! [PySnippetUMeshStdBuild1_4]
2145 coordsArr=DataArrayDouble(coords,9,3)#here coordsArr are declared to have 3 components, mesh will deduce that its spaceDim==3.
2146 mesh.setCoords(coordsArr)#coordsArr contains 9 tuples, that is to say mesh contains 9 nodes.
2147 # ! [PySnippetUMeshStdBuild1_4]
2148 # ! [PySnippetUMeshStdBuild1_5]
2149 mesh.checkConsistencyLight()
2150 # ! [PySnippetUMeshStdBuild1_5]
2153 def testExampleCMeshStdBuild1(self):
2154 # ! [PySnippetCMeshStdBuild1_1]
2155 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] # 9 values along X
2156 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] # 7 values along Y
2157 arrX=DataArrayDouble(XCoords)
2158 arrX.setInfoOnComponent(0,"X [m]")
2159 arrY=DataArrayDouble(YCoords)
2160 arrY.setInfoOnComponent(0,"Y [m]")
2161 # ! [PySnippetCMeshStdBuild1_1]
2162 # ! [PySnippetCMeshStdBuild1_2]
2163 mesh=MEDCouplingCMesh("My2D_CMesh")
2164 mesh.setCoords(arrX,arrY)
2165 # ! [PySnippetCMeshStdBuild1_2]
2166 # ! [PySnippetCMeshStdBuild1_3]
2167 self.assertEqual(8*6,mesh.getNumberOfCells())
2168 self.assertEqual(9*7,mesh.getNumberOfNodes())
2169 self.assertEqual(2,mesh.getSpaceDimension())
2170 self.assertEqual(2,mesh.getMeshDimension())
2171 # ! [PySnippetCMeshStdBuild1_3]
2172 mesh=MEDCouplingCMesh("My2D_CMesh")
2173 # ! [PySnippetCMeshStdBuild1_2bis]
2174 mesh.setCoordsAt(0,arrX)
2175 mesh.setCoordsAt(1,arrY)
2176 # ! [PySnippetCMeshStdBuild1_2bis]
2177 self.assertEqual(8*6,mesh.getNumberOfCells())
2178 self.assertEqual(9*7,mesh.getNumberOfNodes())
2179 self.assertEqual(2,mesh.getSpaceDimension())
2180 self.assertEqual(2,mesh.getMeshDimension())
2181 # ! [PySnippetCMeshStdBuild1_4]
2182 # ! [PySnippetCMeshStdBuild1_4]
2185 def testExampleUMeshAdvBuild1(self):
2186 # ! [PySnippetUMeshAdvBuild1_1]
2187 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.,
2188 0.7,0.2,0., -0.3,0.7,0., 0.2,0.7,0., 0.7,0.7,0. ]
2189 nodalConnPerCell=[4,0,3,4,1, 3,1,4,2, 3,4,5,2, 4,6,7,4,3, 4,7,8,5,4]
2190 nodalConnPerCellIndex=[0,5,9,13,18,23]
2191 # ! [PySnippetUMeshAdvBuild1_1]
2192 # ! [PySnippetUMeshAdvBuild1_2]
2193 mesh=MEDCouplingUMesh("My2DMesh",2)
2194 # ! [PySnippetUMeshAdvBuild1_2]
2195 # ! [PySnippetUMeshAdvBuild1_3]
2196 nodalConn=DataArrayInt(nodalConnPerCell,23,1)
2197 nodalConnI=DataArrayInt(nodalConnPerCellIndex,6,1)
2198 mesh.setConnectivity(nodalConn,nodalConnI,True)
2199 # ! [PySnippetUMeshAdvBuild1_3]
2200 # ! [PySnippetUMeshAdvBuild1_4]
2201 coordsArr=DataArrayDouble(coords,9,3)#here coordsArr are declared to have 3 components, mesh will deduce that its spaceDim==3.
2202 mesh.setCoords(coordsArr)#coordsArr contains 9 tuples, that is to say mesh contains 9 nodes.
2203 # ! [PySnippetUMeshAdvBuild1_4]
2204 # ! [PySnippetUMeshAdvBuild1_5]
2205 # ! [PySnippetUMeshAdvBuild1_5]
2206 mesh.checkConsistencyLight()
2209 def testExampleDataArrayBuild1(self):
2210 # ! [PySnippetDataArrayBuild1_0]
2211 dataDouble=[0.,10.,20.,1.,11.,21.,2.,12.,22.,3.,13.,23.,4.,14.,24.]
2212 # ! [PySnippetDataArrayBuild1_0]
2213 # ! [PySnippetDataArrayBuild1_1]
2214 arrayDouble=DataArrayDouble()
2215 arrayDouble.setValues(dataDouble,5,3)# 5 tuples containing each 3 components
2216 # ! [PySnippetDataArrayBuild1_1]
2217 # ! [PySnippetDataArrayBuild1_1bis]
2218 arrayDouble=DataArrayDouble(dataDouble,5,3)
2219 # ! [PySnippetDataArrayBuild1_1bis]
2220 # ! [PySnippetDataArrayBuild1_2]
2221 dataInt=[0, 10, 20, 1, 11, 21, 2, 12, 22, 3, 13, 23, 4, 14, 24]
2222 # ! [PySnippetDataArrayBuild1_2]
2223 # ! [PySnippetDataArrayBuild1_3]
2224 arrayInt=DataArrayInt()
2225 arrayInt.setValues(dataInt,5,3)# 5 tuples containing each 3 components
2226 # ! [PySnippetDataArrayBuild1_3]
2227 # ! [PySnippetDataArrayBuild1_3bis]
2228 arrayInt=DataArrayInt(dataInt,5,3)
2229 # ! [PySnippetDataArrayBuild1_3bis]
2232 def testExampleFieldDoubleBuild1(self):
2233 XCoords=[-0.3,0.07,0.1,0.3,0.45,0.47,0.49,1.,1.22]; arrX=DataArrayDouble(XCoords)
2234 YCoords=[0.07,0.1,0.37,0.45,0.47,0.49,1.007]; arrY=DataArrayDouble(YCoords)
2235 mesh=MEDCouplingCMesh("My2D_CMesh")
2236 mesh.setCoords(arrX,arrY)
2237 # ! [PySnippetFieldDoubleBuild1_1]
2238 fieldOnCells=MEDCouplingFieldDouble(ON_CELLS,NO_TIME)
2239 fieldOnCells.setName("MyTensorFieldOnCellNoTime")
2240 fieldOnCells.setMesh(mesh)
2241 array=DataArrayDouble()
2242 array.alloc(fieldOnCells.getMesh().getNumberOfCells(),9) # Implicitly fieldOnCells will be a 9 components field.
2243 array.fillWithValue(7.)
2244 fieldOnCells.setArray(array)
2245 # fieldOnCells is now usable
2247 # ! [PySnippetFieldDoubleBuild1_1]
2248 # ! [PySnippetFieldDoubleBuild1_2]
2249 f1=mesh.fillFromAnalytic(ON_CELLS,1,"x*x+y*y*3+2.*x") # f1 is scalar
2250 f2=mesh.fillFromAnalytic(ON_CELLS,1,"cos(x+y/x)") # f2 is scalar too
2251 f2bis=mesh.fillFromAnalytic(ON_CELLS,2,"x*x*IVec+3*y*JVec") # f2bis is a vectors field
2252 f3=f1+f2 # f3 scalar
2253 f4=f3/f2 # f4 scalar
2254 f2bis.applyFunc(1,"sqrt(x*x+y*y)") # f2bis becomes scalar
2255 f5=f2bis*f4 # f5 scalar
2257 res=f4.getValueOn(pos1) # f4 is scalar so the returned value is of size 1.
2259 # ! [PySnippetFieldDoubleBuild1_2]
2260 # ! [PySnippetFieldDoubleBuild1_3]
2261 # ! [PySnippetFieldDoubleBuild1_3]
2264 def testExampleFieldDoubleBuild2(self):
2265 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22]; arrX=DataArrayDouble(XCoords)
2266 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007]; arrY=DataArrayDouble(YCoords)
2267 mesh=MEDCouplingCMesh("My2D_CMesh")
2268 mesh.setCoords(arrX,arrY)
2269 # ! [PySnippetFieldDoubleBuild2_1]
2270 fieldOnNodes=MEDCouplingFieldDouble(ON_NODES,NO_TIME)
2271 fieldOnNodes.setName("MyScalarFieldOnNodeNoTime")
2272 fieldOnNodes.setMesh(mesh)
2273 array=DataArrayDouble()
2274 array.alloc(fieldOnNodes.getMesh().getNumberOfNodes(),1) # Implicitly fieldOnNodes will be a 1 component field.
2275 array.fillWithValue(7.)
2276 fieldOnNodes.setArray(array)
2277 # fieldOnNodes is now usable
2279 # ! [PySnippetFieldDoubleBuild2_1]
2282 def testExampleFieldDoubleBuild3(self):
2283 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22]; arrX=DataArrayDouble(XCoords)
2284 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007]; arrY=DataArrayDouble(YCoords)
2285 mesh=MEDCouplingCMesh("My2D_CMesh")
2286 mesh.setCoords(arrX,arrY)
2287 # ! [PySnippetFieldDoubleBuild3_1]
2288 fieldOnCells=MEDCouplingFieldDouble(ON_CELLS,ONE_TIME)
2289 fieldOnCells.setName("MyTensorFieldOnCellNoTime")
2290 fieldOnCells.setTimeUnit("ms") # Time unit is ms.
2291 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
2292 fieldOnCells.setMesh(mesh)
2293 array=DataArrayDouble()
2294 array.alloc(fieldOnCells.getMesh().getNumberOfCells(),2) # Implicitly fieldOnCells will be a 2 components field.
2295 array.fillWithValue(7.)
2296 fieldOnCells.setArray(array)
2297 # fieldOnCells is now usable
2299 # ! [PySnippetFieldDoubleBuild3_1]
2302 def testExampleFieldDoubleBuild4(self):
2303 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22]; arrX=DataArrayDouble(XCoords)
2304 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007]; arrY=DataArrayDouble(YCoords)
2305 mesh=MEDCouplingCMesh("My2D_CMesh")
2306 mesh.setCoords(arrX,arrY)
2307 # ! [PySnippetFieldDoubleBuild4_1]
2308 fieldOnNodes=MEDCouplingFieldDouble(ON_NODES,CONST_ON_TIME_INTERVAL)
2309 fieldOnNodes.setName("MyVecFieldOnNodeWithConstTime")
2310 fieldOnNodes.setTimeUnit("ms") # Time unit is ms.
2311 fieldOnNodes.setStartTime(4.22,2,-1)
2312 fieldOnNodes.setEndTime(6.44,4,-1)# fieldOnNodes is defined in interval [4.22 ms,6.44 ms]
2313 fieldOnNodes.setMesh(mesh)
2314 array=DataArrayDouble()
2315 array.alloc(fieldOnNodes.getMesh().getNumberOfNodes(),3) # Implicitly fieldOnNodes will be a 3 components field.
2316 array.fillWithValue(7.)
2317 fieldOnNodes.setArray(array)
2318 # fieldOnNodes is now usable
2320 # ! [PySnippetFieldDoubleBuild4_1]
2323 def testExampleDataArrayApplyFunc1(self):
2324 # ! [PySnippetDataArrayApplyFunc1_1]
2325 d=DataArrayDouble([1.,2.,11.,12.,21.,22.,31.,41.],4,2)
2326 self.assertRaises(InterpKernelException,d.applyFunc,"x*y")
2327 # ! [PySnippetDataArrayApplyFunc1_1]
2328 # ! [PySnippetDataArrayApplyFunc1_2]
2329 d=DataArrayDouble([1.,2.,11.,12.,21.,22.,31.,41.],4,2)
2330 d1=d.applyFunc("smth*smth")
2331 self.assertTrue(d1.isEqual(DataArrayDouble([1.,4.,121.,144.,441.,484.,961.,1681.],4,2),1e-12))
2332 # ! [PySnippetDataArrayApplyFunc1_2]
2333 # ! [PySnippetDataArrayApplyFunc1_3]
2334 d2=d.applyFunc(2,"smth1*IVec+2*smth2*JVec")
2335 self.assertTrue(d2.isEqual(DataArrayDouble([1.,4.,11.,24.,21.,44.,31.,82.],4,2),1e-12))
2336 # ! [PySnippetDataArrayApplyFunc1_3]
2337 # ! [PySnippetDataArrayApplyFunc1_4]
2338 dd=DataArrayDouble([1.,4.,3.,11.,144.,13.,21.,484.,23.,31.,1024.,33.],4,3)
2339 # ! [PySnippetDataArrayApplyFunc1_4]
2340 # ! [PySnippetDataArrayApplyFunc1_5]
2341 dd1=dd.applyFunc(1,"f+sqrt(g)+h")
2342 self.assertTrue(dd1.isEqual(DataArrayDouble([6.,36.,66.,96.],4,1),1e-12))
2343 # ! [PySnippetDataArrayApplyFunc1_5]
2344 # ! [PySnippetDataArrayApplyFunc1_6]
2345 dd2=dd.applyFunc(1,"a+0.*b+c")
2346 self.assertTrue(dd2.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
2347 # ! [PySnippetDataArrayApplyFunc1_6]
2348 # ! [PySnippetDataArrayApplyFunc1_7]
2349 ddd=DataArrayDouble([1.,4.,3.,11.,144.,13.,21.,484.,23.,31.,1024.,33.],4,3)
2350 ddd.setInfoOnComponents(["Y [m]","AA [m/s]","GG [MW]"])
2351 # ! [PySnippetDataArrayApplyFunc1_7]
2352 # ! [PySnippetDataArrayApplyFunc1_8]
2353 ddd1=ddd.applyFuncCompo(1,"Y+GG")
2354 self.assertTrue(ddd1.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
2355 # ! [PySnippetDataArrayApplyFunc1_8]
2356 # ! [PySnippetDataArrayApplyFunc1_9]
2357 ddd1=ddd.applyFuncNamedCompo(1,["X","Y","Z"],"X+Z")
2358 self.assertTrue(ddd1.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
2359 # ! [PySnippetDataArrayApplyFunc1_9]