1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2014 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 from MEDCoupling import *
23 from math import pi, sqrt
25 class MEDCouplingBasicsTest(unittest.TestCase):
27 def testExample_MEDCouplingFieldDouble_WriteVTK(self):
28 #! [PySnippet_MEDCouplingFieldDouble_WriteVTK_1]
31 coordsArr=DataArrayDouble.New(coords,3,1)
32 mesh=MEDCouplingCMesh.New()
33 mesh.setCoords(coordsArr,coordsArr) # mesh becomes a 2D one
35 # 3 fields (lying on the same mesh!)
36 field1 = mesh.getMeasureField( True )
37 field2 = mesh.buildOrthogonalField()
38 field3 = mesh.fillFromAnalytic( ON_CELLS, 2, "IVec * x + JVec * y" )
39 field2.setName( "Normal" ) # name is necessary!
40 field3.setName( "Barycenter" ) # name is necessary!
43 fileName = "testExample_MEDCouplingFieldDouble_WriteVTK.vtk"
44 fs = [ field1, field2, field3 ] # field series
45 MEDCouplingFieldDouble.WriteVTK( fileName, fs )
46 #! [PySnippet_MEDCouplingFieldDouble_WriteVTK_1]
52 def testExample_MEDCouplingFieldDouble_MaxFields(self):
53 #! [PySnippet_MEDCouplingFieldDouble_MaxFields_1]
54 vals1 = [0.,2., 4.,6.] # for field 1
55 vals2 = [2.,0., 6.,4.] # for field 2
56 valsMax = [2.,2., 6.,6.] # expected max field
57 valsMin = [0.,0., 4.,4.] # expected min field
60 valsArr1=DataArrayDouble.New(vals1,2,2) # 2 tuples per 2 components
61 field1 = MEDCouplingFieldDouble.New( ON_NODES )
62 field1.setArray( valsArr1 )
65 valsArr2=DataArrayDouble.New(vals2,2,2) # 2 tuples per 2 components
66 field2 = MEDCouplingFieldDouble.New( ON_NODES )
67 field2.setArray( valsArr2 )
70 fieldMax = MEDCouplingFieldDouble.MaxFields( field1, field2 )
71 self.assertTrue( fieldMax.getArray().getValues() == valsMax )
74 fieldMin = MEDCouplingFieldDouble.MinFields( field1, field2 )
75 self.assertTrue( fieldMin.getArray().getValues() == valsMin )
76 #! [PySnippet_MEDCouplingFieldDouble_MaxFields_1]
78 def testExample_MEDCouplingFieldDouble_MergeFields(self):
79 #! [PySnippet_MEDCouplingFieldDouble_MergeFields_1]
82 coordsArr=DataArrayDouble.New(coords,3,1)
83 mesh1=MEDCouplingCMesh.New()
84 mesh1.setCoords(coordsArr)
86 field1 = mesh1.fillFromAnalytic( ON_CELLS, 1, "x")
89 field2 = field1.cloneWithMesh( True )
91 field2.getMesh().translate(vec) # translate mesh2
92 field2.applyFunc("x + 5") # "translate" field2
94 # concatenate field1 and field2
95 field3 = MEDCouplingFieldDouble.MergeFields( field1, field2 )
96 field4 = MEDCouplingFieldDouble.MergeFields( [ field1, field2] )
97 #! [PySnippet_MEDCouplingFieldDouble_MergeFields_1]
100 def testExample_MEDCouplingFieldDouble_substractInPlaceDM(self):
101 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_1]
102 coords1=[0.,1.,2.,3.]
103 coords2=[2.,1.,0.,3.] #0 <==> #2
105 mesh1=MEDCouplingUMesh.New();
106 coordsArr=DataArrayDouble.New(coords1, 4, 1);
107 mesh1.setCoords(coordsArr);
108 mesh1.setMeshDimension(0);
109 mesh1.allocateCells(0);
110 mesh1.finishInsertingCells();
112 mesh2=mesh1.deepCpy();
113 mesh2.getCoords().setValues(coords2, 4, 1);
114 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_1]
115 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_2]
116 field1 = mesh1.fillFromAnalytic(ON_NODES,1,"x") # field1 values == coords1
117 field2 = mesh2.fillFromAnalytic(ON_NODES,1,"x") # field2 values == coords2
118 levOfCheck = 10 # nodes can be permuted
119 field1.substractInPlaceDM( field2, levOfCheck, 1e-13, 0 ) # values #0 and #2 must swap
120 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_2]
121 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_3]
122 field2.applyFunc( 1, 0.0 ) # all field2 values == 0.0
123 self.assertTrue( field1.isEqual( field2, 1e-13, 1e-13 )) # field1 == field2 == 0.0
124 #! [PySnippet_MEDCouplingFieldDouble_substractInPlaceDM_3]
127 def testExample_MEDCouplingFieldDouble_changeUnderlyingMesh(self):
128 #! [PySnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_1]
129 coords1=[0.,1.,2.,3.]
130 coords2=[2.,1.,0.,3.] #0 <==> #2
132 mesh1=MEDCouplingUMesh.New();
133 coordsArr=DataArrayDouble.New(coords1, 4, 1);
134 mesh1.setCoords(coordsArr);
135 mesh1.setMeshDimension(0);
136 mesh1.allocateCells(0);
137 mesh1.finishInsertingCells();
139 mesh2=mesh1.deepCpy();
140 mesh2.getCoords().setValues(coords2, 4, 1);
141 #! [PySnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_1]
142 #! [PySnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_2]
143 field = mesh1.fillFromAnalytic(ON_NODES,1,"x") # field values == coords1
144 levOfCheck = 10 # nodes can be permuted
145 field.changeUnderlyingMesh( mesh2, levOfCheck, 1e-13, 0 ) # values #0 and #2 must swap
146 self.assertTrue( field.getArray().getValues() == coords2 )
147 #! [PySnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_2]
150 def testExample_MEDCouplingFieldDouble_applyFunc_same_nb_comp(self):
151 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_1]
153 array = DataArrayDouble.New( v, 2, 2 ) # 2 tuples per 2 components
154 field = MEDCouplingFieldDouble.New( ON_CELLS )
155 field.setArray( array )
156 func = "IVec * v + JVec * v*v + 10"
157 field.applyFunc( func )
158 self.assertTrue( field.getNumberOfComponents() == 2 ) # 2 components remains
159 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_1]
160 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_2]
161 v2 = field.getArray().getValues()
162 self.assertAlmostEqual( v2[0], 10 + v[0], 13 ) # "10 + IVec * v"
163 self.assertAlmostEqual( v2[1], 10 + v[1]*v[1], 13 ) # "10 + JVec * v*v"
164 self.assertAlmostEqual( v2[2], 10 + v[2], 13 ) # "10 + IVec * v"
165 self.assertAlmostEqual( v2[3], 10 + v[3]*v[3], 13 ) # "10 + JVec * v*v"
166 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_2]
169 def testExample_MEDCouplingFieldDouble_applyFunc3(self):
170 #! [PySnippet_MEDCouplingFieldDouble_applyFunc3_1]
171 # create a 2D vector field
172 values = [1.,1., 2.,1.]
173 array = DataArrayDouble.New( values, 2, 2 ) # 2 tuples per 2 components
174 field = MEDCouplingFieldDouble.New( ON_CELLS )
175 field.setArray( array )
176 # transform the field to a 3D vector field
177 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
178 varNames=["a","b"] # names used to refer to X and Y components
179 field.applyFunc3( 3, varNames, func ) # require 3 components
180 self.assertTrue( field.getNumberOfComponents() == 3 ) # 3 components as required
181 #! [PySnippet_MEDCouplingFieldDouble_applyFunc3_1]
182 #! [PySnippet_MEDCouplingFieldDouble_applyFunc3_2]
183 vec1 = field.getArray().getTuple(1) # vector #1
184 a,b = values[2], values[3] # initial components of the vector #1
185 self.assertAlmostEqual( vec1[0], 10 + b, 13 ) # "10 + IVec * b"
186 self.assertAlmostEqual( vec1[1], 10 + a, 13 ) # "10 + JVec * a"
187 self.assertAlmostEqual( vec1[2], 10 + sqrt(a*a+b*b), 13 ) # "10 + KVec * sqrt( a*a + b*b )"
188 #! [PySnippet_MEDCouplingFieldDouble_applyFunc3_2]
191 def testExample_MEDCouplingFieldDouble_applyFunc2(self):
192 #! [PySnippet_MEDCouplingFieldDouble_applyFunc2_1]
193 # create a 2D vector field
194 values = [1.,1., 2.,1.]
195 array = DataArrayDouble.New( values, 2, 2 ) # 2 tuples per 2 components
196 array.setInfoOnComponent(0,"a") # name used to refer to X component within a function
197 array.setInfoOnComponent(1,"b") # name used to refer to Y component within a function
198 field = MEDCouplingFieldDouble.New( ON_CELLS )
199 field.setArray( array )
200 # transform the field to a 3D vector field
201 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
202 field.applyFunc2( 3, func ) # require 3 components
203 self.assertTrue( field.getNumberOfComponents() == 3 ) # 3 components as required
204 #! [PySnippet_MEDCouplingFieldDouble_applyFunc2_1]
205 #! [PySnippet_MEDCouplingFieldDouble_applyFunc2_2]
206 vec1 = field.getArray().getTuple(1) # vector #1
207 a,b = values[2], values[3] # initial components of the vector #1
208 self.assertAlmostEqual( vec1[0], 10 + b, 13 ) # "10 + IVec * b"
209 self.assertAlmostEqual( vec1[1], 10 + a, 13 ) # "10 + JVec * a"
210 self.assertAlmostEqual( vec1[2], 10 + sqrt(a*a+b*b), 13 ) # "10 + KVec * sqrt( a*a + b*b )"
211 #! [PySnippet_MEDCouplingFieldDouble_applyFunc2_2]
214 def testExample_MEDCouplingFieldDouble_applyFunc(self):
215 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_1]
216 # create a 2D vector field
217 values = [1.,1., 2.,1.]
218 array = DataArrayDouble.New( values, 2, 2 ) # 2 tuples per 2 components
219 field = MEDCouplingFieldDouble.New( ON_CELLS )
220 field.setArray( array )
221 # transform the field to a 3D vector field
222 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
223 field.applyFunc( 3, func ) # require 3 components
224 self.assertTrue( field.getNumberOfComponents() == 3 ) # 3 components as required
225 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_1]
226 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_2]
227 vec1 = field.getArray().getTuple(1) # vector #1
228 a,b = values[2], values[3] # initial components of the vector #1
229 self.assertAlmostEqual( vec1[0], 10 + b, 13 ) # "10 + IVec * b"
230 self.assertAlmostEqual( vec1[1], 10 + a, 13 ) # "10 + JVec * a"
231 self.assertAlmostEqual( vec1[2], 10 + sqrt(a*a+b*b), 13 ) # "10 + KVec * sqrt( a*a + b*b )"
232 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_2]
235 def testExample_MEDCouplingFieldDouble_applyFunc_val(self):
236 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_val_1]
238 coordsArr=DataArrayDouble.New(coords,3,1)
239 mesh=MEDCouplingCMesh.New()
240 mesh.setCoords(coordsArr,coordsArr)
241 field = MEDCouplingFieldDouble.New( ON_CELLS )
242 field.setMesh( mesh )
243 field.fillFromAnalytic(2,"IVec * x + JVec * y") # 2 components
244 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_val_1]
245 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_val_2]
247 field.applyFunc( 3, newValue ) # 3 components are required
248 self.assertTrue( field.getIJ(1,0) == newValue ) # a value is as expected
249 self.assertTrue( field.getNumberOfComponents() == 3 )
250 self.assertTrue( field.getNumberOfTuples() == mesh.getNumberOfCells() )
251 #! [PySnippet_MEDCouplingFieldDouble_applyFunc_val_2]
254 def testExample_MEDCouplingFieldDouble_fillFromAnalytic3(self):
255 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_1]
256 coords = [0.,2.,4.,6.] # 6. is not used
257 x=DataArrayDouble.New(coords[:3],3,1)
258 y=DataArrayDouble.New(coords[:2],2,1)
259 mesh=MEDCouplingCMesh.New()
261 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_1]
262 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_2]
263 field = MEDCouplingFieldDouble.New( ON_CELLS )
264 field.setMesh( mesh )
265 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
266 varNames=["a","b"] # names used to refer to X and Y coord components
267 field.fillFromAnalytic3(3,varNames,func)
268 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_2]
269 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_3]
270 vals1 = field.getArray().getTuple(1) # values of the cell #1
271 assert len( vals1 ) == 3 # 3 components in the field
273 bc = mesh.getBarycenterAndOwner() # func is applied to barycenters of cells
274 bc1 = bc.getTuple(1) # coordinates of the second point
276 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
277 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
278 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
279 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
280 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic3_3]
283 def testExample_MEDCouplingFieldDouble_fillFromAnalytic2(self):
284 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_1]
286 x=DataArrayDouble.New(coords[:3],3,1)
287 y=DataArrayDouble.New(coords[:2],2,1)
288 x.setInfoOnComponent(0,"a") # name used to refer to X coordinate within a function
289 y.setInfoOnComponent(0,"b") # name used to refer to Y coordinate within a function
290 mesh=MEDCouplingCMesh.New()
292 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_1]
293 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_2]
294 field = MEDCouplingFieldDouble.New( ON_CELLS )
295 field.setMesh( mesh )
296 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
297 field.fillFromAnalytic2(3,func)
298 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_2]
299 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_3]
300 vals1 = field.getArray().getTuple(1) # values of the cell #1
301 assert len( vals1 ) == 3 # 3 components in the field
303 bc = mesh.getBarycenterAndOwner() # func is applied to barycenters of cells
304 bc1 = bc.getTuple(1) # coordinates of the second point
306 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
307 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
308 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
309 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
310 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic2_3]
313 def testExample_MEDCouplingFieldDouble_fillFromAnalytic(self):
314 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_1]
316 x=DataArrayDouble.New(coords[:3],3,1)
317 y=DataArrayDouble.New(coords[:2],2,1)
318 mesh=MEDCouplingCMesh.New()
320 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_1]
321 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_2]
322 field = MEDCouplingFieldDouble.New( ON_CELLS )
323 field.setMesh( mesh )
324 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
325 field.fillFromAnalytic(3,func)
326 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_2]
327 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_3]
328 vals1 = field.getArray().getTuple(1) # values of the cell #1
329 assert len( vals1 ) == 3 # 3 components in the field
331 bc = mesh.getBarycenterAndOwner() # func is applied to barycenters of cells
332 bc1 = bc.getTuple(1) # coordinates of the second point
334 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
335 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
336 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
337 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
338 #! [PySnippet_MEDCouplingFieldDouble_fillFromAnalytic_3]
341 def testExample_MEDCouplingFieldDouble_getValueOn_time(self):
342 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_1]
344 coordsArr=DataArrayDouble.New(coords,3,1)
345 mesh=MEDCouplingCMesh.New()
346 mesh.setCoords(coordsArr,coordsArr)
347 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_1]
348 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_2]
349 field = MEDCouplingFieldDouble.New( ON_CELLS, LINEAR_TIME )
350 field.setMesh( mesh )
351 field.fillFromAnalytic(1,"10") # all values == 10.
352 field.setEndArray( field.getArray() + field.getArray() ) # all values == 20.
353 time1, time2 = 1.1, 22.
354 field.setStartTime( time1, 0, 0 )
355 field.setEndTime ( time2, 0, 0 )
356 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_2]
357 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_3]
358 pos = [ 1., 1. ] # we are in 2D space
359 value = field.getValueOn( pos, 0.5*( time1 + time2 ))
360 self.assertTrue( value[0] == 0.5*( 10. + 20.))
361 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_time_3]
364 def testExample_MEDCouplingFieldDouble_getValueOnMulti(self):
365 #! [PySnippet_MEDCouplingFieldDouble_getValueOnMulti_1]
367 coordsArr=DataArrayDouble.New(coords,3,1)
368 mesh=MEDCouplingCMesh.New()
369 mesh.setCoords(coordsArr,coordsArr)
370 field = mesh.fillFromAnalytic(ON_CELLS,1,"x+y")
371 #! [PySnippet_MEDCouplingFieldDouble_getValueOnMulti_1]
372 #! [PySnippet_MEDCouplingFieldDouble_getValueOnMulti_2]
373 bc = mesh.getBarycenterAndOwner() # field values are located at cell barycenters
374 valArray = field.getValueOnMulti( bc )
375 self.assertTrue( valArray.isEqual( field.getArray(), 1e-13 ))
376 #! [PySnippet_MEDCouplingFieldDouble_getValueOnMulti_2]
379 def testExample_MEDCouplingFieldDouble_getValueOn(self):
380 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_1]
382 coordsArr=DataArrayDouble.New(coords,3,1)
383 mesh=MEDCouplingCMesh.New()
384 mesh.setCoords(coordsArr,coordsArr)
385 field = mesh.fillFromAnalytic(ON_CELLS,1,"x+y")
386 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_1]
387 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_2]
388 bc = mesh.getBarycenterAndOwner() # field values are located at cell barycenters
389 vals = [] # array to collect values returned by getValueOn()
390 for i,tupl in enumerate( bc ):
391 vals.extend( field.getValueOn( tupl ) )
392 self.assertTrue( vals == field.getArray().getValues() )
393 #! [PySnippet_MEDCouplingFieldDouble_getValueOn_2]
396 def testExample_MEDCouplingFieldDouble_getValueOnPos(self):
397 #! [PySnippet_MEDCouplingFieldDouble_getValueOnPos_1]
399 coordsArr=DataArrayDouble.New(coords,3,1)
400 mesh=MEDCouplingCMesh.New()
401 mesh.setCoords(coordsArr,coordsArr)
402 field = mesh.fillFromAnalytic(ON_CELLS,1,"x+y")
403 #! [PySnippet_MEDCouplingFieldDouble_getValueOnPos_1]
404 #! [PySnippet_MEDCouplingFieldDouble_getValueOnPos_2]
405 val11 = field.getValueOnPos( 1,1,-1)
406 bc = mesh.getBarycenterAndOwner() # field values are located at cell barycenters
407 self.assertTrue( val11[0] == bc[3,0] + bc[3,1] )
408 #! [PySnippet_MEDCouplingFieldDouble_getValueOnPos_2]
411 def testExample_MEDCouplingFieldDouble_renumberNodes(self):
412 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_1]
414 coordsArr=DataArrayDouble.New(coords,3,1)
415 mesh=MEDCouplingCMesh.New()
416 mesh.setCoords(coordsArr,coordsArr)
417 mesh=mesh.buildUnstructured()
418 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_1]
419 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_2]
420 field = mesh.fillFromAnalytic(ON_NODES,2,"IVec*x+JVec*y")
421 values = field.getArray()
422 nodeCoords = mesh.getCoords()
423 self.assertTrue( values.isEqualWithoutConsideringStr( nodeCoords, 1e-13 ))
424 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_2]
425 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_3]
426 renumber = [8, 7, 6, 5, 4, 3, 2, 1, 0]
427 field.renumberNodes(renumber,False)
428 mesh2 = field.getMesh() # field now refers to another mesh
429 values = field.getArray()
430 nodeCoords = mesh2.getCoords()
431 self.assertTrue( values.isEqualWithoutConsideringStr( nodeCoords, 1e-13 ))
432 #! [PySnippet_MEDCouplingFieldDouble_renumberNodes_3]
436 def testExample_MEDCouplingFieldDouble_renumberCells(self):
437 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_1]
439 coordsArr=DataArrayDouble.New(coords,3,1)
440 mesh=MEDCouplingCMesh.New()
441 mesh.setCoords(coordsArr,coordsArr)
442 mesh=mesh.buildUnstructured()
443 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_1]
444 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_2]
445 field = mesh.fillFromAnalytic(ON_CELLS,2,"IVec*x+JVec*y")
446 values = field.getArray()
447 bc = mesh.getBarycenterAndOwner()
448 self.assertTrue( values.isEqualWithoutConsideringStr( bc, 1e-13 ))
449 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_2]
450 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_3]
451 renumber = [ 3, 2, 1, 0 ]
452 field.renumberCells(renumber,False)
453 mesh2 = field.getMesh() # field now refers to another mesh
454 values = field.getArray()
455 bc = mesh2.getBarycenterAndOwner()
456 self.assertTrue( values.isEqualWithoutConsideringStr( bc, 1e-13 ))
457 #! [PySnippet_MEDCouplingFieldDouble_renumberCells_3]
460 def testExample_MEDCouplingFieldDouble_buildNewTimeReprFromThis(self):
461 #! [PySnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_1]
463 coordsArr=DataArrayDouble.New(coords,3,1)
464 mesh=MEDCouplingCMesh.New()
465 mesh.setCoords(coordsArr,coordsArr)
466 field1 = mesh.fillFromAnalytic(ON_NODES,1,"x+y")
467 self.assertTrue( field1.getTimeDiscretization() == ONE_TIME )
468 #! [PySnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_1]
469 #! [PySnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_2]
470 field2 = field1.buildNewTimeReprFromThis(NO_TIME,False)
471 self.assertTrue( field2.getTimeDiscretization() == NO_TIME )
472 #! [PySnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_2]
475 def testExample_MEDCouplingMesh_fillFromAnalytic3(self):
476 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_1]
477 coords = [0.,2.,4.,6.] # 6. is not used
478 x=DataArrayDouble.New(coords[:3],3,1)
479 y=DataArrayDouble.New(coords[:2],2,1)
480 mesh=MEDCouplingCMesh.New()
482 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_1]
483 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_2]
484 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
485 varNames=["a","b"] # names used to refer to X and Y coord components
486 field=mesh.fillFromAnalytic3(ON_CELLS,3,varNames,func)
487 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_2]
488 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_3]
489 vals1 = field.getArray().getTuple(1) # values of the cell #1
490 assert len( vals1 ) == 3 # 3 components in the field
492 bc = mesh.getBarycenterAndOwner() # func is applied to barycenters of cells
493 bc1 = bc.getTuple(1) # coordinates of the second point
495 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
496 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
497 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
498 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
499 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic3_3]
502 def testExample_MEDCouplingMesh_fillFromAnalytic2(self):
503 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_1]
504 coords = [0.,2.,4.,6.] # 6. is not used
505 x=DataArrayDouble.New(coords[:3],3,1)
506 y=DataArrayDouble.New(coords[:2],2,1)
507 x.setInfoOnComponent(0,"a") # name used to refer to X coordinate within a function
508 y.setInfoOnComponent(0,"b") # name used to refer to Y coordinate within a function
509 mesh=MEDCouplingCMesh.New()
511 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_1]
512 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_2]
513 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
514 field=mesh.fillFromAnalytic2(ON_CELLS,3,func)
515 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_2]
516 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_3]
517 vals1 = field.getArray().getTuple(1) # values of the cell #1
518 assert len( vals1 ) == 3 # 3 components in the field
520 bc = mesh.getBarycenterAndOwner() # func is applied to barycenters of cells
521 bc1 = bc.getTuple(1) # coordinates of the second point
523 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
524 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
525 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
526 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
527 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic2_3]
530 def testExample_MEDCouplingMesh_fillFromAnalytic(self):
531 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_1]
532 coords = [0.,2.,4.,6.] # 6. is not used
533 x=DataArrayDouble.New(coords[:3],3,1)
534 y=DataArrayDouble.New(coords[:2],2,1)
535 mesh=MEDCouplingCMesh.New()
537 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_1]
538 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_2]
539 func = "IVec * b + JVec * a + KVec * sqrt( a*a + b*b ) + 10"
540 field=mesh.fillFromAnalytic(ON_CELLS,3,func)
541 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_2]
542 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_3]
543 vals1 = field.getArray().getTuple(1) # values of the cell #1
544 assert len( vals1 ) == 3 # 3 components in the field
546 bc = mesh.getBarycenterAndOwner() # func is applied to barycenters of cells
547 bc1 = bc.getTuple(1) # coordinates of the second point
549 dist = sqrt( bc1[0]*bc1[0] + bc1[1]*bc1[1] ) # "sqrt( a*a + b*b )"
550 self.assertAlmostEqual( vals1[0], 10 + bc1[1], 13 ) # "10 + IVec * b"
551 self.assertAlmostEqual( vals1[1], 10 + bc1[0], 13 ) # "10 + JVec * a"
552 self.assertAlmostEqual( vals1[2], 10 + dist , 13 ) # "10 + KVec * sqrt( a*a + b*b )"
553 #! [PySnippet_MEDCouplingMesh_fillFromAnalytic_3]
556 def testExample_MEDCouplingCMesh_getCoordsAt(self):
557 #! [PySnippet_MEDCouplingCMesh_getCoordsAt_1]
559 x=DataArrayDouble.New(coords,3,1)
560 mesh=MEDCouplingCMesh.New()
561 mesh.setCoordsAt(0,x)
562 x2=mesh.getCoordsAt(0)
563 assert coords == x2.getValues()
564 #! [PySnippet_MEDCouplingCMesh_getCoordsAt_1]
567 def testExample_MEDCouplingUMesh_areCellsIncludedIn(self):
568 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_1]
569 mesh1=MEDCouplingUMesh.New();
570 mesh1.setMeshDimension(2);
571 mesh1.allocateCells(5);
572 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4];
573 mesh1.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
574 mesh1.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
575 mesh1.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
576 mesh1.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
577 mesh1.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
578 mesh1.finishInsertingCells();
579 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 ];
580 coordsArr=DataArrayDouble.New();
581 coordsArr.setValues(coords,9,2);
582 mesh1.setCoords(coordsArr);
583 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_1]
584 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_2]
586 mesh2 = mesh1.buildPartOfMySelf(cells2, True ) # even cells selected
587 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_2]
588 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_3]
589 compType = 0 # the strongest policy
590 isOk, corr2to1 = mesh1.areCellsIncludedIn( mesh2, compType )
591 assert isOk # a larger mesh1 includes a smaller mesh2
592 assert corr2to1.getValues() == cells2
593 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_3]
594 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_4]
595 isOk, corr1to2 = mesh2.areCellsIncludedIn( mesh1, compType )
596 assert not isOk # the smaller mesh2 does NOT include the larger mesh1
597 assert corr1to2.getValues() == [2, 3, 1, 4, 0]
598 #! [PySnippet_MEDCouplingUMesh_areCellsIncludedIn_4]
600 def testExample_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells(self):
601 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_1]
602 # 2D coordinates of 5 base nodes
603 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2];
604 coordsArr=DataArrayDouble.New();
605 coordsArr.setValues(coords,5,2);
606 # coordinates of 5 top nodes
607 coordsArr2 = coordsArr.deepCpy()
608 # 3D coordinates of base + top nodes
609 coordsArr = coordsArr.changeNbOfComponents( 3, 0 )
610 coordsArr2 = coordsArr2.changeNbOfComponents( 3, 1 )
611 coordsArr = DataArrayDouble.Aggregate([coordsArr,coordsArr2])
613 mesh=MEDCouplingUMesh.New();
614 mesh.setCoords(coordsArr);
615 mesh.setMeshDimension(3);
616 mesh.allocateCells(2);
617 # connectivity of reversed HEXA8 and PENTA6
618 conn=[0,1,4,3, 5,6,9,8, 1,2,4, 6,7,9]
619 mesh.insertNextCell(NORM_HEXA8, 8,conn[0:0+8])
620 mesh.insertNextCell(NORM_PENTA6,6,conn[8:8+6])
621 mesh.finishInsertingCells();
622 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_1]
623 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_2]
624 fixedCells = mesh.findAndCorrectBadOriented3DExtrudedCells()
625 assert len( fixedCells ) == 2 # 2 cells fixed
626 fixedCells = mesh.findAndCorrectBadOriented3DExtrudedCells()
627 assert len( fixedCells ) == 0 # no bad cells
628 #! [PySnippet_MEDCouplingUMesh_findAndCorrectBadOriented3DExtrudedCells_2]
631 def testExample_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented(self):
632 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_1]
633 # 2D coordinates of 5 base nodes
634 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2];
635 coordsArr=DataArrayDouble.New();
636 coordsArr.setValues(coords,5,2);
637 # coordinates of 5 top nodes
638 coordsArr2 = coordsArr.deepCpy()
639 # 3D coordinates of base + top nodes
640 coordsArr = coordsArr.changeNbOfComponents( 3, 0 )
641 coordsArr2 = coordsArr2.changeNbOfComponents( 3, 1 )
642 coordsArr = DataArrayDouble.Aggregate([coordsArr,coordsArr2])
644 mesh=MEDCouplingUMesh.New();
645 mesh.setCoords(coordsArr);
646 mesh.setMeshDimension(3);
647 mesh.allocateCells(2);
648 # connectivity of a HEXA8 + a reversed PENTA6
649 conn=[0,3,4,1, 5,8,9,6, 1,2,4, 6,7,9]
650 mesh.insertNextCell(NORM_POLYHED, 8,conn[0:0+8]) # "extruded" polyhedron
651 mesh.insertNextCell(NORM_POLYHED,6,conn[8:8+6])
652 mesh.finishInsertingCells();
653 # fix connectivity of NORM_POLYHED's
654 mesh.convertExtrudedPolyhedra()
655 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_1]
656 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_2]
657 badCells = mesh.arePolyhedronsNotCorrectlyOriented()
658 assert len( badCells ) == 1 # one polyhedron is KO
659 # fix invalid rolyherdons
660 mesh.orientCorrectlyPolyhedrons()
661 # re-check the orientation
662 badCells = mesh.arePolyhedronsNotCorrectlyOriented()
663 assert len( badCells ) == 0 # connectivity is OK
664 #! [PySnippet_MEDCouplingUMesh_arePolyhedronsNotCorrectlyOriented_2]
667 def testExample_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented(self):
668 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_1]
669 mesh=MEDCouplingUMesh.New();
670 mesh.setMeshDimension(2);
671 mesh.allocateCells(5);
672 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4];
673 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
674 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
675 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
676 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
677 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
678 mesh.finishInsertingCells();
679 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 ];
680 coordsArr=DataArrayDouble.New();
681 coordsArr.setValues(coords,9,2);
682 mesh.setCoords(coordsArr);
683 mesh.changeSpaceDimension(3)
684 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_1]
685 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_2]
687 badCellIds=mesh.are2DCellsNotCorrectlyOriented( vec, False )
688 assert len( badCellIds ) == 1 # one cell is reversed
690 mesh.orientCorrectly2DCells( vec, False )
691 # re-check orientation
692 badCellIds=mesh.are2DCellsNotCorrectlyOriented( vec, False )
693 assert len( badCellIds ) == 0 # the orientation is OK
694 #! [PySnippet_MEDCouplingUMesh_are2DCellsNotCorrectlyOriented_2]
697 def testExample_MEDCouplingUMesh_getCellsContainingPoints(self):
698 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_1]
699 mesh=MEDCouplingUMesh.New();
700 mesh.setMeshDimension(2);
701 mesh.allocateCells(5);
702 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4];
703 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
704 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
705 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
706 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
707 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
708 mesh.finishInsertingCells();
709 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 ];
710 coordsArr=DataArrayDouble.New();
711 coordsArr.setValues(coords,9,2);
712 mesh.setCoords(coordsArr);
713 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_1]
714 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_2]
715 pos = [ 10., 10, # point out of the mesh
716 0.3, 0.3, # point located somewhere inside the mesh
717 coords[2], coords[3]] # point at the node #1
718 eps = 1e-4 # ball radius
719 cells,cellsIndex=mesh.getCellsContainingPoints( pos, 3, eps )
720 assert cells.getValues() == [4, 0, 1]
721 assert cellsIndex.getValues() == [0, 0, 1, 3]
722 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoints_2]
726 def testExample_MEDCouplingUMesh_getCellsContainingPoint(self):
727 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_1]
728 mesh=MEDCouplingUMesh.New();
729 mesh.setMeshDimension(2);
730 mesh.allocateCells(5);
731 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4];
732 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]);
733 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]);
734 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]);
735 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]);
736 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]);
737 mesh.finishInsertingCells();
738 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 ];
739 coordsArr=DataArrayDouble.New();
740 coordsArr.setValues(coords,9,2);
741 mesh.setCoords(coordsArr);
742 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_1]
743 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_2]
744 pos4 = coords[ 4*2 : ] # coordinates of the node #4
745 eps = 1e-4 # ball radius
746 pos = [ pos4[0]+eps, pos4[1]-eps ] # ball center
747 cellIds=mesh.getCellsContainingPoint( pos, eps )
748 assert len( cellIds ) == mesh.getNumberOfCells()
749 #! [PySnippet_MEDCouplingUMesh_getCellsContainingPoint_2]
753 def testExample_MEDCouplingUMesh_buildPartOrthogonalField(self):
754 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_1]
755 mesh=MEDCouplingUMesh.New();
756 mesh.setMeshDimension(2);
757 mesh.allocateCells(5);
758 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4];
759 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
760 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
761 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
762 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
763 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
764 mesh.finishInsertingCells();
765 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 ];
766 coordsArr=DataArrayDouble.New();
767 coordsArr.setValues(coords,9,2);
768 mesh.setCoords(coordsArr);
769 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_1]
770 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_2]
771 part = DataArrayInt([1,2,3,4],4,1) # cell #0 is omitted
772 vecField=mesh.buildPartOrthogonalField( part )
773 vecArr = vecField.getArray()
774 assert len( vecArr ) == len( part )
775 assert vecArr.getNumberOfComponents() == 3
776 #! [PySnippet_MEDCouplingUMesh_buildPartOrthogonalField_2]
779 def testExample_MEDCouplingUMesh_getPartMeasureField(self):
780 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_1]
781 mesh=MEDCouplingUMesh.New();
782 mesh.setMeshDimension(2);
783 mesh.allocateCells(5);
784 conn=[0,3,4,1, 1,2,4, 4,5,2, 6,7,4,3, 7,8,5,4];
785 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
786 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
787 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
788 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
789 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
790 mesh.finishInsertingCells();
791 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 ];
792 coordsArr=DataArrayDouble.New();
793 coordsArr.setValues(coords,9,2);
794 mesh.setCoords(coordsArr);
795 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_1]
796 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_2]
798 part = DataArrayInt([1,2,3,4],4,1) # cell #0 is omitted
799 areaArr=mesh.getPartMeasureField( isAbs, part )
800 assert areaArr[0] > 0 # orientation ignored
801 areaArr=mesh.getPartMeasureField( not isAbs, part )
802 assert areaArr[0] < 0 # orientation considered
803 assert len( areaArr ) == len( part )
804 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_2]
805 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_3]
806 part = DataArrayInt([1,2,3,4],4,1) # cell #0 is omitted
807 baryCenters = mesh.getPartBarycenterAndOwner( part )
808 assert len( baryCenters ) == len( part )
809 assert baryCenters.getNumberOfComponents() == mesh.getSpaceDimension()
810 #! [PySnippet_MEDCouplingUMesh_getPartMeasureField_3]
813 def testExample_MEDCouplingUMesh_getCellsInBoundingBox(self):
814 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_1]
815 mesh=MEDCouplingUMesh.New();
816 mesh.setMeshDimension(2);
817 coords=[0.,0., 0.,1., 1.,1];
818 coordsArr=DataArrayDouble.New();
819 coordsArr.setValues(coords,3,2);
820 mesh.setCoords(coordsArr);
821 mesh.allocateCells(1);
823 mesh.insertNextCell(NORM_TRI3,3,conn);
824 mesh.finishInsertingCells();
825 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_1]
826 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_2]
827 bbox = [1., 1., 1.001,1.001] # xMin, xMax, yMin, yMax
828 cellsInBox = mesh.getCellsInBoundingBox( bbox, 0.0 )
829 assert cellsInBox.getValues() == []
830 cellsInBox = mesh.getCellsInBoundingBox( bbox, 0.1 )
831 assert cellsInBox.getValues() == [0]
832 #! [PySnippet_MEDCouplingUMesh_getCellsInBoundingBox_2]
835 def testExample_MEDCouplingUMesh_renumberNodesInConn(self):
836 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_1]
837 mesh=MEDCouplingUMesh.New();
838 mesh.setMeshDimension(2);
839 mesh.allocateCells(1);
841 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]);
842 mesh.finishInsertingCells();
843 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_1]
844 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_2]
845 old2newIds = [-1,3,2,1,0]
846 mesh.renumberNodesInConn( old2newIds )
847 nodes0 = mesh.getNodeIdsOfCell( 0 )
848 assert nodes0 == [0,1,2,3]
849 #! [PySnippet_MEDCouplingUMesh_renumberNodesInConn_2]
853 def testExample_MEDCouplingUMesh_renumberNodes(self):
854 #! [PySnippet_MEDCouplingUMesh_renumberNodes_1]
855 mesh=MEDCouplingUMesh.New();
856 mesh.setMeshDimension(2);
857 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.3];
858 coordsArr=DataArrayDouble.New();
859 coordsArr.setValues(coords,4,2);
860 mesh.setCoords(coordsArr);
861 mesh.allocateCells(0);
862 mesh.finishInsertingCells();
863 #! [PySnippet_MEDCouplingUMesh_renumberNodes_1]
864 #! [PySnippet_MEDCouplingUMesh_renumberNodes_2]
865 mesh.renumberNodes([ 2,1,0,-1 ], 3);
866 coordsArr = mesh.getCoords() # get a shorten array
867 assert coordsArr.getValues() == [0.7,-0.3, 0.2,-0.3, -0.3,-0.3]
868 #! [PySnippet_MEDCouplingUMesh_renumberNodes_2]
869 #! [PySnippet_MEDCouplingUMesh_renumberNodes_3]
870 coordsArr.setValues(coords,4,2); # restore old nodes
871 mesh.renumberNodes2([ 2,1,0,2 ], 3);
872 coordsArr = mesh.getCoords() # get a shorten array
873 assert coordsArr.getValues() == [0.7,-0.3, 0.2,-0.3, -0.3,0.0]
874 #! [PySnippet_MEDCouplingUMesh_renumberNodes_3]
877 def testExample_MEDCouplingUMesh_findBoundaryNodes(self):
878 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_1]
879 mesh=MEDCouplingUMesh.New();
880 mesh.setMeshDimension(2);
881 mesh.allocateCells(5);
882 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
883 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]);
884 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]);
885 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]);
886 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]);
887 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]);
888 mesh.finishInsertingCells();
889 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 ];
890 coordsArr=DataArrayDouble.New();
891 coordsArr.setValues(coords,9,2);
892 mesh.setCoords(coordsArr);
893 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_1]
894 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_2]
895 nodeIdsArr=mesh.findBoundaryNodes()
896 assert nodeIdsArr.getNumberOfTuples() == mesh.getNumberOfNodes() - 1
897 #! [PySnippet_MEDCouplingUMesh_findBoundaryNodes_2]
900 def testExample_MEDCouplingUMesh_buildBoundaryMesh(self):
901 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_1]
902 mesh=MEDCouplingUMesh.New();
903 mesh.setMeshDimension(2);
904 mesh.allocateCells(5);
905 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
906 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]);
907 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]);
908 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]);
909 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]);
910 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]);
911 mesh.finishInsertingCells();
912 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 ];
913 coordsArr=DataArrayDouble.New();
914 coordsArr.setValues(coords,9,2);
915 mesh.setCoords(coordsArr);
916 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_1]
917 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_2]
918 mesh1=mesh.buildBoundaryMesh(True)
919 mesh2=mesh.buildBoundaryMesh(False)
920 assert coordsArr.isEqual( mesh1.getCoords(), 1e-13 ) # same nodes
921 assert not coordsArr.isEqual( mesh2.getCoords(), 1e-13 ) # different nodes
922 #! [PySnippet_MEDCouplingUMesh_buildBoundaryMesh_2]
925 def testExample_MEDCouplingUMesh_buildFacePartOfMySelfNode(self):
926 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_1]
927 mesh=MEDCouplingUMesh.New();
928 mesh.setMeshDimension(2);
929 mesh.allocateCells(5);
930 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
931 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
932 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
933 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
934 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
935 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
936 mesh.finishInsertingCells();
937 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 ];
938 coordsArr=DataArrayDouble.New();
939 coordsArr.setValues(coords,9,2);
940 mesh.setCoords(coordsArr);
941 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_1]
942 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_2]
943 nodeIds = mesh.getNodeIdsOfCell( 0 )
945 mesh1 = mesh.buildFacePartOfMySelfNode( nodeIds, allNodes )
946 assert mesh1.getNumberOfCells() == 4 # 4 segments bounding QUAD4 #0 only
947 mesh2 = mesh.buildFacePartOfMySelfNode( nodeIds, not allNodes )
948 assert mesh2.getNumberOfCells() > 4 # more segments added
949 #! [PySnippet_MEDCouplingUMesh_buildFacePartOfMySelfNode_2]
953 def testExample_MEDCouplingUMesh_buildPartOfMySelfNode(self):
954 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_1]
955 mesh=MEDCouplingUMesh.New();
956 mesh.setMeshDimension(2);
957 mesh.allocateCells(5);
958 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
959 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
960 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
961 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
962 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
963 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
964 mesh.finishInsertingCells();
965 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 ];
966 coordsArr=DataArrayDouble.New();
967 coordsArr.setValues(coords,9,2);
968 mesh.setCoords(coordsArr);
969 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_1]
970 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_2]
971 nodeIds = mesh.getNodeIdsOfCell( 0 )
973 mesh1 = mesh.buildPartOfMySelfNode( nodeIds, allNodes )
974 mesh2 = mesh.buildPartOfMySelfNode( nodeIds, not allNodes )
975 assert mesh1.getNumberOfCells() == 1 # cell #0 is found only
976 assert mesh2.getNumberOfCells() == mesh.getNumberOfCells() # all cells are found
977 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelfNode_2]
981 def testExample_MEDCouplingUMesh_getCellIdsLyingOnNodes(self):
982 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_1]
983 mesh=MEDCouplingUMesh.New();
984 mesh.setMeshDimension(2);
985 mesh.allocateCells(5);
986 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
987 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
988 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
989 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
990 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
991 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
992 mesh.finishInsertingCells();
993 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 ];
994 coordsArr=DataArrayDouble.New();
995 coordsArr.setValues(coords,9,2);
996 mesh.setCoords(coordsArr);
997 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_1]
998 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_2]
999 nodeIds = mesh.getNodeIdsOfCell( 0 )
1001 cellIdsArr1 = mesh.getCellIdsLyingOnNodes( nodeIds, allNodes )
1002 cellIdsArr2 = mesh.getCellIdsLyingOnNodes( nodeIds, not allNodes )
1003 assert cellIdsArr1.getNumberOfTuples() == 1 # cell #0 is found only
1004 assert cellIdsArr2.getNumberOfTuples() == mesh.getNumberOfCells() # all cells are found
1005 #! [PySnippet_MEDCouplingUMesh_getCellIdsLyingOnNodes_2]
1009 def testExample_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds(self):
1010 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_1]
1011 mesh=MEDCouplingUMesh.New();
1012 mesh.setMeshDimension(2);
1013 mesh.allocateCells(5);
1014 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
1015 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]);
1016 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]);
1017 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]);
1018 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]);
1019 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]);
1020 mesh.finishInsertingCells();
1021 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 ];
1022 coordsArr=DataArrayDouble.New();
1023 coordsArr.setValues(coords,9,2);
1024 mesh.setCoords(coordsArr);
1025 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_1]
1026 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_2]
1028 nodeIds = mesh.getNodeIdsOfCell( cellIds[0] )
1029 nodeIds += mesh.getNodeIdsOfCell( cellIds[1] )
1030 cellIdsArr = mesh.getCellIdsFullyIncludedInNodeIds( nodeIds )
1031 assert cellIdsArr.getValues() == cellIds
1032 #! [PySnippet_MEDCouplingUMesh_getCellIdsFullyIncludedInNodeIds_2]
1036 def testExample_MEDCouplingUMesh_buildPartOfMySelf(self):
1037 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_1]
1038 mesh=MEDCouplingUMesh.New();
1039 mesh.setMeshDimension(2);
1040 mesh.allocateCells(5);
1041 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
1042 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
1043 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
1044 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
1045 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
1046 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
1047 mesh.finishInsertingCells();
1048 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 ];
1049 coordsArr=DataArrayDouble.New();
1050 coordsArr.setValues(coords,9,2);
1051 mesh.setCoords(coordsArr);
1052 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_1]
1053 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_2]
1055 mesh2=mesh.buildPartOfMySelf(cellIds, True)
1056 mesh3=mesh.buildPartOfMySelf(cellIds, False)
1057 coordsArr2 = mesh2.getCoords()
1058 assert coordsArr.isEqual( coordsArr2, 1e-13 ) # same nodes
1059 coordsArr3 = mesh3.getCoords()
1060 assert not coordsArr.isEqual( coordsArr3, 1e-13 ) # different nodes
1061 assert mesh2.getNodeIdsOfCell(0) == mesh.getNodeIdsOfCell( cellIds[0]) # cell #1 was copied
1062 assert mesh2.getNodeIdsOfCell(1) == mesh.getNodeIdsOfCell( cellIds[1]) # cell #2 was copied
1063 #! [PySnippet_MEDCouplingUMesh_buildPartOfMySelf_2]
1066 def testExample_MEDCouplingUMesh_mergeNodes(self):
1067 #! [PySnippet_MEDCouplingUMesh_mergeNodes_1]
1068 mesh=MEDCouplingUMesh.New();
1069 mesh.setMeshDimension(2);
1070 mesh.allocateCells(5);
1071 conn=[0,3,4,1, 1,4,2, 4,5,2];
1072 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]);
1073 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]);
1074 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]);
1075 mesh.finishInsertingCells();
1076 coords=[0.3,-0.301, # 0
1078 0.3,-0.302, # 2 ~~ 0
1081 0.3,-0.303];# 5 ~~ 0
1082 coordsArr=DataArrayDouble.New();
1083 coordsArr.setValues(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 mergeNodes2()
1100 mesh.mergeNodes2(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.New();
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.New();
1120 coordsArr.setValues(coords,9,2);
1121 mesh.setCoords(coordsArr);
1122 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_1]
1123 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_2]
1124 oldNbCells = mesh.getNumberOfCells()
1125 arr = mesh.zipConnectivityTraducer(0)
1126 assert mesh.getNumberOfCells() == oldNbCells-2
1127 assert arr.getValues() == [0, 1, 1, 0, 2]
1128 #! [PySnippet_MEDCouplingUMesh_zipConnectivityTraducer_2]
1131 def testExample_MEDCouplingUMesh_zipCoordsTraducer(self):
1132 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_1]
1133 mesh=MEDCouplingUMesh.New();
1134 mesh.setMeshDimension(2);
1135 mesh.allocateCells(5);
1136 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
1137 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]);
1138 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]);
1139 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]);
1140 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]);
1141 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]);
1142 mesh.finishInsertingCells();
1143 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 ];
1144 coordsArr=DataArrayDouble.New();
1145 coordsArr.setValues(coords,9,2);
1146 mesh.setCoords(coordsArr);
1147 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_1]
1148 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_2]
1150 mesh2=mesh.buildPartOfMySelf(cellIds,True);
1151 arr=mesh2.zipCoordsTraducer();
1152 assert mesh2.getNumberOfNodes() == 4 # nb of nodes decreased
1153 assert arr.getValues() == [-1,0,1,-1,2,3,-1,-1,-1] # -1 for unused nodes
1154 #! [PySnippet_MEDCouplingUMesh_zipCoordsTraducer_2]
1157 def testExample_MEDCouplingUMesh_getNodeIdsInUse(self):
1158 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_1]
1159 mesh=MEDCouplingUMesh.New();
1160 mesh.setMeshDimension(2);
1161 mesh.allocateCells(5);
1162 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
1163 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]);
1164 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]);
1165 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]);
1166 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]);
1167 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]);
1168 mesh.finishInsertingCells();
1169 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 ];
1170 coordsArr=DataArrayDouble.New();
1171 coordsArr.setValues(coords,9,2);
1172 mesh.setCoords(coordsArr);
1173 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_1]
1174 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_2]
1176 mesh2=mesh.buildPartOfMySelf(cellIds,True);
1177 arr,newNbOfNodes=mesh2.getNodeIdsInUse();
1178 assert arr.getValues() == [-1,0,1,-1,2,3,-1,-1,-1]
1179 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_2]
1180 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_3]
1181 arr2=arr.invertArrayO2N2N2O(newNbOfNodes);
1182 assert arr2.getValues() == [1,2,4,5]
1183 #! [PySnippet_MEDCouplingUMesh_getNodeIdsInUse_3]
1186 def testExample_MEDCouplingUMesh_convertToPolyTypes(self):
1187 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_1]
1188 mesh=MEDCouplingUMesh.New();
1189 mesh.setMeshDimension(2);
1190 mesh.allocateCells(5);
1191 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
1192 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
1193 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
1194 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
1195 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
1196 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
1197 mesh.finishInsertingCells();
1198 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 ];
1199 coordsArr=DataArrayDouble.New();
1200 coordsArr.setValues(coords,9,2);
1201 mesh.setCoords(coordsArr);
1202 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_1]
1203 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_2]
1205 mesh.convertToPolyTypes(cells);
1206 assert mesh.getTypeOfCell(0) == NORM_QUAD4
1207 assert mesh.getTypeOfCell(1) == NORM_POLYGON, mesh.getTypeOfCell(1)
1208 assert mesh.getTypeOfCell(2) == NORM_TRI3
1209 assert mesh.getTypeOfCell(3) == NORM_POLYGON
1210 #! [PySnippet_MEDCouplingUMesh_convertToPolyTypes_2]
1213 def testExample_MEDCouplingUMesh_buildDescendingConnectivity2(self):
1214 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_1]
1215 mesh=MEDCouplingUMesh.New();
1216 mesh.setMeshDimension(2);
1217 mesh.allocateCells(5);
1218 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
1219 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
1220 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
1221 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
1222 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
1223 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
1224 mesh.finishInsertingCells();
1225 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 ];
1226 coordsArr=DataArrayDouble.New();
1227 coordsArr.setValues(coords,9,2);
1228 mesh.setCoords(coordsArr);
1229 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_1]
1230 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_2]
1231 desc=DataArrayInt.New();
1232 descIndx=DataArrayInt.New();
1233 revDesc=DataArrayInt.New();
1234 revDescIndx=DataArrayInt.New();
1235 mesh2=mesh.buildDescendingConnectivity2(desc,descIndx,revDesc,revDescIndx);
1236 assert desc.getValues() == [1,2,3,4,-3,5,6, 7,8,-5,9,10,-2,11, 12,13,-7,-10]
1237 assert descIndx.getValues() == [0,4,7,10,14,18]
1238 assert revDesc.getValues() == [0, 0,3, 0,1, 0, 1,2, 1, 2,4, 2, 3, 3,4, 3, 4, 4]
1239 assert revDescIndx.getValues() == [0,1,3,5,6,8,9,11,12,13,15,16,17,18]
1240 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_2]
1241 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_3]
1242 assert mesh2.getNodeIdsOfCell( 3-1 ) == [4, 1] # cell #3 in FORTRAN mode
1243 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity2_3]
1246 def testExample_MEDCouplingUMesh_buildDescendingConnectivity(self):
1247 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_1]
1248 mesh=MEDCouplingUMesh.New();
1249 mesh.setMeshDimension(2);
1250 mesh.allocateCells(5);
1251 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
1252 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
1253 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
1254 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
1255 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
1256 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
1257 mesh.finishInsertingCells();
1258 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 ];
1259 coordsArr=DataArrayDouble.New();
1260 coordsArr.setValues(coords,9,2);
1261 mesh.setCoords(coordsArr);
1262 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_1]
1263 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_2]
1264 desc=DataArrayInt.New();
1265 descIndx=DataArrayInt.New();
1266 revDesc=DataArrayInt.New();
1267 revDescIndx=DataArrayInt.New();
1268 mesh2=mesh.buildDescendingConnectivity(desc,descIndx,revDesc,revDescIndx);
1269 assert desc.getValues() == [0,1,2,3, 2,4,5, 6,7,4, 8,9,1,10, 11,12,6,9]
1270 assert descIndx.getValues() == [0,4,7,10,14,18]
1271 assert revDesc.getValues() == [0, 0,3, 0,1, 0, 1,2, 1, 2,4, 2, 3, 3,4, 3, 4, 4]
1272 assert revDescIndx.getValues() == [0,1,3,5,6,8,9,11,12,13,15,16,17,18]
1273 #! [PySnippet_MEDCouplingUMesh_buildDescendingConnectivity_2]
1276 def testExample_MEDCouplingUMesh_getReverseNodalConnectivity(self):
1277 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_1]
1278 mesh=MEDCouplingUMesh.New();
1279 mesh.setMeshDimension(2);
1280 mesh.allocateCells(5);
1281 conn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4];
1282 mesh.insertNextCell(NORM_QUAD4,4,conn[0:4]); # 0
1283 mesh.insertNextCell(NORM_TRI3,3, conn[4:7]); # 1
1284 mesh.insertNextCell(NORM_TRI3,3, conn[7:10]); # 2
1285 mesh.insertNextCell(NORM_QUAD4,4,conn[10:14]); # 3
1286 mesh.insertNextCell(NORM_QUAD4,4,conn[14:18]); # 4
1287 mesh.finishInsertingCells();
1288 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 ];
1289 coordsArr=DataArrayDouble.New();
1290 coordsArr.setValues(coords,9,2);
1291 mesh.setCoords(coordsArr);
1292 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_1]
1293 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_2]
1294 revNodal,revNodalIndx=mesh.getReverseNodalConnectivity();
1295 assert revNodal.getValues() == [0,0,1,1,2,0,3,0,1,2,3,4,2,4,3,3,4,4];
1296 assert revNodalIndx.getValues() == [0,1,3,5,7,12,14,15,17,18];
1297 #! [PySnippet_MEDCouplingUMesh_getReverseNodalConnectivity_2]
1300 def testExample_MEDCouplingUMesh_checkDeepEquivalWith(self):
1301 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_1]
1303 mesh1=MEDCouplingUMesh.New();
1304 mesh1.setMeshDimension(2);
1309 coordsArr=DataArrayDouble.New(coords,4,2);
1310 mesh1.setCoords(coordsArr);
1311 mesh1.allocateCells(2);
1312 mesh1.insertNextCell(NORM_TRI3,3,[0,1,2]); #0
1313 mesh1.insertNextCell(NORM_TRI3,3,[1,2,3]); #1
1314 mesh1.finishInsertingCells();
1316 mesh2=MEDCouplingUMesh.New();
1317 mesh2.setMeshDimension(2);
1318 coords=[0.0,1.0, #0 = #3
1322 coordsArr2=DataArrayDouble.New(coords,4,2)
1323 mesh2.setCoords(coordsArr2);
1324 mesh2.allocateCells(2);
1325 mesh2.insertNextCell(NORM_TRI3,3,[2,3,0]); #0 = #1
1326 mesh2.insertNextCell(NORM_TRI3,3,[3,1,2]); #1 ~ #0
1327 mesh2.finishInsertingCells();
1328 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_1]
1329 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_2]
1330 cellCompPol = 1 # "permuted same orientation" - policy of medium severity
1331 cOld2New, nOld2New = mesh1.checkDeepEquivalWith( mesh2, cellCompPol, 0.002 )
1332 assert nOld2New.getValues() == [3, 0, 1, 2]
1333 assert cOld2New.getValues() == [1, 0]
1334 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_2]
1335 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_3]
1336 self.assertRaises( InterpKernelException, mesh1.checkDeepEquivalOnSameNodesWith, mesh2, cellCompPol, 0.002)
1337 mesh2.setCoords(coordsArr) # make meshes share the same coordinates array
1338 mesh2.allocateCells(2);
1339 mesh2.insertNextCell(NORM_TRI3,3,[1,2,3]); #0 = #1
1340 mesh2.insertNextCell(NORM_TRI3,3,[1,0,2]); #1 ~ #0
1341 mesh2.finishInsertingCells();
1342 cellCompPol = 2 # the weakest policy
1343 mesh1.checkDeepEquivalOnSameNodesWith( mesh2, cellCompPol, 0 )
1344 #! [PySnippet_MEDCouplingUMesh_checkDeepEquivalWith_3]
1347 def testExample_MEDCouplingPointSet_scale(self):
1348 #! [PySnippet_MEDCouplingPointSet_scale_1]
1349 coords=[0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0] # 2D coordinates of 4 nodes
1350 coordsArr=DataArrayDouble.New();
1351 coordsArr.setValues(coords,4,2);
1352 mesh=MEDCouplingUMesh.New();
1353 mesh.setCoords(coordsArr);
1354 initCoords = coordsArr.deepCpy()
1355 #! [PySnippet_MEDCouplingPointSet_scale_1]
1356 #! [PySnippet_MEDCouplingPointSet_scale_2]
1359 mesh.scale(center,factor)
1360 #! [PySnippet_MEDCouplingPointSet_scale_2]
1361 #! [PySnippet_MEDCouplingPointSet_scale_3]
1362 coords2 = mesh.getCoords()
1363 assert coords2.isEqualWithoutConsideringStr( initCoords, 1.0 )
1364 assert not coords2.isEqualWithoutConsideringStr( initCoords, 0.9 )
1365 #! [PySnippet_MEDCouplingPointSet_scale_3]
1368 def testExample_MEDCouplingPointSet_translate(self):
1369 #! [PySnippet_MEDCouplingPointSet_translate_1]
1370 coords=[0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0] # 2D coordinates of 4 nodes
1371 coordsArr=DataArrayDouble.New();
1372 coordsArr.setValues(coords,4,2);
1373 mesh=MEDCouplingUMesh.New();
1374 mesh.setCoords(coordsArr);
1375 initCoords = coordsArr.deepCpy()
1376 #! [PySnippet_MEDCouplingPointSet_translate_1]
1377 #! [PySnippet_MEDCouplingPointSet_translate_2]
1379 mesh.translate(vector)
1380 #! [PySnippet_MEDCouplingPointSet_translate_2]
1381 #! [PySnippet_MEDCouplingPointSet_translate_3]
1382 coords2 = mesh.getCoords()
1383 assert coords2.isEqualWithoutConsideringStr( initCoords, 1 )
1384 assert not coords2.isEqualWithoutConsideringStr( initCoords, 0.9 )
1385 #! [PySnippet_MEDCouplingPointSet_translate_3]
1388 def testExample_MEDCouplingPointSet_rotate(self):
1389 #! [PySnippet_MEDCouplingPointSet_rotate_1]
1390 coords=[0.0,0.0, 0.1,0.0, 0.1,0.1, 0.0,0.1] # 2D coordinates of 4 nodes
1391 coordsArr=DataArrayDouble.New();
1392 coordsArr.setValues(coords,4,2);
1393 mesh=MEDCouplingUMesh.New();
1394 mesh.setCoords(coordsArr);
1395 #! [PySnippet_MEDCouplingPointSet_rotate_1]
1396 #! [PySnippet_MEDCouplingPointSet_rotate_2]
1398 mesh.rotate(center,-pi/2)
1399 #! [PySnippet_MEDCouplingPointSet_rotate_2]
1400 #! [PySnippet_MEDCouplingPointSet_rotate_3]
1401 mesh.changeSpaceDimension(3)
1404 mesh.rotate(center,vector,pi/2)
1405 #! [PySnippet_MEDCouplingPointSet_rotate_3]
1406 #! [PySnippet_MEDCouplingPointSet_rotate_4]
1407 mesh.changeSpaceDimension(2)
1408 coords2 = mesh.getCoords()
1409 for i,c in enumerate( coords ):
1410 self.assertAlmostEqual( c, coords2.getIJ(0,i), 13 )
1411 #! [PySnippet_MEDCouplingPointSet_rotate_4]
1414 def testExample_MEDCouplingPointSet_getBoundingBox(self):
1415 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_1]
1416 cc=[0.0, 0.1, 0.2, # 3D coordinates of 2 nodes
1418 coordsArr=DataArrayDouble.New();
1419 coordsArr.setValues(cc,2,3);
1420 mesh=MEDCouplingUMesh.New();
1421 mesh.setCoords(coordsArr);
1422 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_1]
1423 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_2]
1424 bbox=mesh.getBoundingBox()
1425 assert bbox == [( cc[0], cc[3] ), # NOTE: list of 3 tuples is retirned!
1428 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_2]
1430 def testExample_MEDCouplingPointSet_getNodeIdsNearPoint(self):
1431 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_1]
1432 # 2D coordinates of 5 nodes
1433 coords=[0.3,-0.301, # 0
1437 0.3,-0.30299999999999];# 4
1438 coordsArr=DataArrayDouble.New();
1439 coordsArr.setValues(coords,5,2);
1440 mesh=MEDCouplingUMesh.New();
1441 mesh.setCoords(coordsArr);
1442 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_1]
1443 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_2]
1444 point=[0.3, -0.3] # point close to nodes #0, #2 and #4
1445 ids=mesh.getNodeIdsNearPoint(point,0.003);
1446 assert ids.getValues() == [0,2,4]
1447 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_2]
1450 def testExample_MEDCouplingPointSet_getNodeIdsNearPoints(self):
1451 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_1]
1452 # 2D coordinates of 7 nodes
1453 coords=[0.3,-0.301, # 0
1460 coordsArr=DataArrayDouble.New();
1461 coordsArr.setValues(coords,7,2);
1462 mesh=MEDCouplingUMesh.New();
1463 mesh.setCoords(coordsArr);
1464 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_1]
1465 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_2]
1466 points=[0.2,-0.301, # ~ node #1
1468 1.1, 0.002] # ~ nodes #3, #4 and #5
1469 ids,idsIndex=mesh.getNodeIdsNearPoints(points,3,0.003);
1470 assert ids.getValues() == [1, 3, 4, 5]
1471 assert idsIndex.getValues() == [0, 1, 1, 4]
1472 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_2]
1475 def testExample_MEDCouplingPointSet_findCommonNodes(self):
1476 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_1]
1477 coords=[0.3,-0.301, # 0
1483 coordsArr=DataArrayDouble.New();
1484 coordsArr.setValues(coords,6,2);
1485 mesh=MEDCouplingUMesh.New();
1486 mesh.setCoords(coordsArr);
1487 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_1]
1488 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_2]
1489 comm,commI=mesh.findCommonNodes(1e-13)
1490 assert comm.getValues() == [3,4]
1491 comm,commI=mesh.findCommonNodes(0.004)
1492 assert comm.getValues() == [0,2,5,3,4]
1493 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_2]
1496 def testExample_MEDCouplingPointSet_getCoordinatesOfNode(self):
1497 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_1]
1498 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3];
1499 coordsArr=DataArrayDouble.New();
1500 coordsArr.setValues(coords,3,2);
1501 mesh=MEDCouplingUMesh.New();
1502 mesh.setCoords(coordsArr);
1503 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_1]
1504 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_2]
1505 nodeCoords=mesh.getCoordinatesOfNode(1)
1506 self.assertAlmostEqual(0.2, nodeCoords[0],13);
1507 self.assertAlmostEqual(-0.3,nodeCoords[1],13);
1508 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_2]
1511 def testExample_DataArrayInt_getTuple(self):
1512 #! [Snippet_DataArrayInt_getTuple_1]
1513 dv=DataArrayInt.New();
1517 assert dv.getTuple( 1 ) == [9,10]
1518 #! [Snippet_DataArrayInt_getTuple_1]
1519 #! [Snippet_DataArrayInt_getTuple_2]
1522 #! [Snippet_DataArrayInt_getTuple_2]
1525 def testExample_DataArrayInt_buildPermutationArr(self):
1526 #! [PySnippet_DataArrayInt_buildPermutationArr_1]
1527 a=DataArrayInt.New()
1528 a.setValues([4,5,6,7,8],5,1)
1529 b=DataArrayInt.New()
1530 b.setValues([5,4,8,6,7],5,1)
1531 c=a.buildPermutationArr(b)
1532 #! [PySnippet_DataArrayInt_buildPermutationArr_1]
1533 self.assertEqual([1,0,4,2,3],c.getValues())
1536 def testExample_DataArrayInt_invertArrayO2N2N2O(self):
1537 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
1539 da=DataArrayInt.New();
1540 da.setValues(arr1,6,1);
1541 da2=da.invertArrayO2N2N2O(6);
1542 expected1=[1,3,0,5,2,4]
1544 self.assertEqual(expected1[i],da2.getIJ(i,0));
1546 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
1549 def testExample_DataArrayInt_invertArrayN2O2O2N(self):
1550 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
1552 da=DataArrayInt.New();
1553 da.setValues(arr1,6,1);
1554 da2=da.invertArrayN2O2O2N(7);
1555 expected1=[1,3,0,5,2,4,-1]
1557 self.assertEqual(expected1[i],da2.getIJ(i,0));
1559 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
1563 def testExample_DataArrayDouble_getIdsInRange(self):
1564 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
1565 da=DataArrayDouble.New()
1567 da[ :, :] = range(10)
1568 da2 = da.getIdsInRange( 2.5, 6 )
1569 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
1572 def testExample_DataArrayDouble_setPartOfValues2(self):
1573 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
1574 da=DataArrayDouble.New()
1577 dv=DataArrayDouble.New();
1581 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
1582 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
1584 da[ [0,1,2], [1,3] ] = dv
1585 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
1586 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
1589 da[ [0,2,3], [0,2,3,4,5,6]] = dv
1590 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
1593 def testExample_DataArrayInt_setPartOfValues2(self):
1594 #! [Snippet_DataArrayInt_setPartOfValues2_1]
1595 da=DataArrayInt.New()
1598 dv=DataArrayInt.New();
1602 #! [Snippet_DataArrayInt_setPartOfValues2_1]
1603 #! [Snippet_DataArrayInt_setPartOfValues2_2]
1605 da[ [0,1,2], [1,3] ] = dv
1606 #! [Snippet_DataArrayInt_setPartOfValues2_2]
1607 #! [Snippet_DataArrayInt_setPartOfValues2_3]
1610 da[ [0,2,3], [0,2,3,4,5,6]] = dv
1611 #! [Snippet_DataArrayInt_setPartOfValues2_3]
1614 def testExample_DataArrayDouble_setPartOfValues3(self):
1615 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
1616 da=DataArrayDouble.New()
1619 dv=DataArrayDouble.New();
1623 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
1624 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
1626 da[ 0:3, [1,3] ] = dv
1627 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
1628 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
1631 da[ 0:4:2, [0,2,3,4,5,6]] = dv
1632 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
1635 def testExample_DataArrayInt_setPartOfValues3(self):
1636 #! [Snippet_DataArrayInt_setPartOfValues3_1]
1637 da=DataArrayInt.New()
1640 dv=DataArrayInt.New();
1644 #! [Snippet_DataArrayInt_setPartOfValues3_1]
1645 #! [Snippet_DataArrayInt_setPartOfValues3_2]
1647 da[ 0:3, [1,3] ] = dv
1648 #! [Snippet_DataArrayInt_setPartOfValues3_2]
1649 #! [Snippet_DataArrayInt_setPartOfValues3_3]
1652 da[ 0:4:2, [0,2,3,4,5,6]] = dv
1653 #! [Snippet_DataArrayInt_setPartOfValues3_3]
1656 def testExample_DataArrayDouble_setPartOfValues1(self):
1657 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
1658 da=DataArrayDouble.New()
1660 da.setInfoOnComponents( ["v1","v2","v3","v4"])
1662 dv=DataArrayDouble.New();
1666 dv.setInfoOnComponents( ["a1","a2"])
1667 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
1668 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
1670 da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
1671 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
1672 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
1674 da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
1675 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
1676 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
1678 da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
1679 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
1680 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
1682 da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
1683 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
1684 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
1687 da2[ 0:3:2, 1:4:2 ] = dv
1688 self.assertTrue( da.isEqual( da2, 1e-20 ))
1689 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
1692 def testExample_DataArrayInt_setPartOfValues1(self):
1693 #! [Snippet_DataArrayInt_setPartOfValues1_1]
1694 da=DataArrayInt.New()
1696 da.setInfoOnComponents( ["v1","v2","v3","v4"])
1698 dv=DataArrayInt.New();
1702 dv.setInfoOnComponents( ["a1","a2"])
1703 #! [Snippet_DataArrayInt_setPartOfValues1_1]
1704 #! [Snippet_DataArrayInt_setPartOfValues1_2]
1706 da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
1707 #! [Snippet_DataArrayInt_setPartOfValues1_2]
1708 #! [Snippet_DataArrayInt_setPartOfValues1_3]
1710 da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
1711 #! [Snippet_DataArrayInt_setPartOfValues1_3]
1712 #! [Snippet_DataArrayInt_setPartOfValues1_4]
1714 da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
1715 #! [Snippet_DataArrayInt_setPartOfValues1_4]
1716 #! [Snippet_DataArrayInt_setPartOfValues1_5]
1718 da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
1719 #! [Snippet_DataArrayInt_setPartOfValues1_5]
1720 #! [Snippet_DataArrayInt_setPartOfValues1_6]
1723 da2[ 0:3:2, 1:4:2 ] = dv
1724 self.assertTrue( da.isEqual( da2 ))
1725 #! [Snippet_DataArrayInt_setPartOfValues1_6]
1728 def testExample_DataArrayDouble_setPartOfValuesSimple1(self):
1729 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
1730 da=DataArrayDouble.New()
1733 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
1734 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
1736 da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
1737 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
1738 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
1740 da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
1741 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
1742 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
1744 da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
1745 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
1746 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
1748 da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
1749 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
1750 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
1753 da2[ 0:3:2, 1:4:2 ] = dv
1754 self.assertTrue( da.isEqual( da2, 1e-20 ))
1755 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
1758 def testExample_DataArrayInt_setPartOfValuesSimple1(self):
1759 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
1760 da=DataArrayInt.New()
1763 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
1764 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
1766 da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
1767 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
1768 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
1770 da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
1771 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
1772 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
1774 da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
1775 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
1776 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
1778 da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
1779 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
1780 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
1783 da2[ 0:3:2, 1:4:2 ] = dv
1784 self.assertTrue( da.isEqual( da2 ))
1785 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
1788 def testExample_DataArrayDouble_setPartOfValuesSimple2(self):
1789 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
1790 da=DataArrayDouble.New()
1793 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
1794 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
1796 da[[1,2], [1,2]] = dv
1797 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
1798 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
1800 da[[0,1,2,3], [1]] = dv
1801 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
1802 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
1804 da[[1], [0,1,2,3]] = dv
1805 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
1806 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
1808 da[[0,2], [1,3]] = dv
1809 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
1812 def testExample_DataArrayInt_setPartOfValuesSimple2(self):
1813 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
1814 da=DataArrayInt.New()
1817 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
1818 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
1820 da[[1,2], [1,2]] = dv
1821 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
1822 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
1824 da[[0,1,2,3], [1]] = dv
1825 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
1826 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
1828 da[[1], [0,1,2,3]] = dv
1829 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
1830 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
1832 da[[0,2], [1,3]] = dv
1833 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
1836 def testExample_DataArrayDouble_setPartOfValuesSimple3(self):
1837 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
1838 da=DataArrayDouble.New()
1841 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
1842 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
1845 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
1846 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
1848 da[[0,1,2,3], 1:2] = dv
1849 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
1850 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
1853 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
1854 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
1856 da[[0,2], 1:4:2] = dv
1857 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
1860 def testExample_DataArrayInt_setPartOfValuesSimple3(self):
1861 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
1862 da=DataArrayInt.New()
1865 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
1866 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
1869 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
1870 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
1872 da[[0,1,2,3], 1:2] = dv
1873 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
1874 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
1877 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
1878 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
1880 da[[0,2], 1:4:2] = dv
1881 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
1884 def testExample_DataArrayDouble_setSelectedComponents(self):
1885 #! [Snippet_DataArrayDouble_setSelectedComponents1]
1886 da=DataArrayDouble.New();
1887 array1=[1.,2., 3.,4., 5.,6.]
1888 da.setValues(array1,3,2)
1889 da.setInfoOnComponents( ["a1","a2"])
1890 #! [Snippet_DataArrayDouble_setSelectedComponents1]
1891 #! [Snippet_DataArrayDouble_setSelectedComponents2]
1892 dv=DataArrayDouble.New();
1895 dv.setInfoOnComponents( ["v1","v2","v3","v4"])
1897 dv.setSelectedComponents( da, [1,0] )
1898 #! [Snippet_DataArrayDouble_setSelectedComponents2]
1899 #! [Snippet_DataArrayDouble_setSelectedComponents3]
1901 self.assertTrue( dv.isEqualWithoutConsideringStr( dv2, 1e-20 ))
1902 #! [Snippet_DataArrayDouble_setSelectedComponents3]
1905 def testExample_DataArrayInt_setSelectedComponents(self):
1906 #! [Snippet_DataArrayInt_setSelectedComponents1]
1907 da=DataArrayInt.New();
1908 array1=[1,2, 3,4, 5,6]
1909 da.setValues(array1,3,2)
1910 da.setInfoOnComponents( ["a1","a2"])
1911 #! [Snippet_DataArrayInt_setSelectedComponents1]
1912 #! [Snippet_DataArrayInt_setSelectedComponents2]
1913 dv=DataArrayInt.New();
1916 dv.setInfoOnComponents( ["v1","v2","v3","v4"])
1918 dv.setSelectedComponents( da, [1,0] )
1919 #! [Snippet_DataArrayInt_setSelectedComponents2]
1920 #! [Snippet_DataArrayInt_setSelectedComponents3]
1922 self.assertTrue( dv.isEqualWithoutConsideringStr( dv2 ))
1923 #! [Snippet_DataArrayInt_setSelectedComponents3]
1926 def testExample_DataArrayDouble_getDifferentValues(self):
1927 #! [Snippet_DataArrayDouble_getDifferentValues1]
1928 da=DataArrayDouble.New();
1929 array1=[2.3,1.2,1.3,2.3,2.301,0.8]
1930 da.setValues(array1,6,1)
1932 dv=da.getDifferentValues(2e-1);
1933 expected2=[2.301,1.3,0.8]
1934 self.assertEqual(3,dv.getNbOfElems());
1936 self.assertAlmostEqual(expected2[i],dv.getIJ(i,0),14);
1938 #! [Snippet_DataArrayDouble_getDifferentValues1]
1941 def testExample_DataArrayDouble_findCommonTuples1(self):
1942 #! [PySnippet_DataArrayDouble_findCommonTuples1]
1943 da=DataArrayDouble.New();
1944 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]
1945 da.setValues(array2,6,2)
1946 #! [PySnippet_DataArrayDouble_findCommonTuples1]
1947 #! [PySnippet_DataArrayDouble_findCommonTuples2]
1948 c,cI=da.findCommonTuples(1.01e-1);
1949 expected3=[0,3,4,1,2]
1951 self.assertEqual(expected3,c.getValues())
1952 self.assertEqual(expected4,cI.getValues())
1953 #! [PySnippet_DataArrayDouble_findCommonTuples2]
1956 def testExampleDataArrayDoubleMeldWith(self):
1957 #! [PySnippet_DataArrayDouble_Meld1_1]
1958 da1=DataArrayDouble.New();
1960 da2=DataArrayDouble.New();
1963 da1.fillWithValue(7.);
1965 da3=da2.applyFunc(3,"10*x*IVec+100*x*JVec+1000*x*KVec");
1967 da1.setInfoOnComponent(0,"c0da1");
1968 da1.setInfoOnComponent(1,"c1da1");
1969 da3.setInfoOnComponent(0,"c0da3");
1970 da3.setInfoOnComponent(1,"c1da3");
1971 da3.setInfoOnComponent(2,"c2da3");
1975 #! [PySnippet_DataArrayDouble_Meld1_1]
1977 def testExampleDataArrayIntMeldWith(self):
1978 #! [PySnippet_DataArrayInt_Meld1_1]
1979 da1=DataArrayInt.New();
1981 da2=DataArrayInt.New();
1984 da1.fillWithValue(7);
1987 da1.setInfoOnComponent(0,"c0da1");
1988 da1.setInfoOnComponent(1,"c1da1");
1989 da2.setInfoOnComponent(0,"c0da2");
1992 #! [PySnippet_DataArrayInt_Meld1_1]
1994 def testExampleDataArrayDoubleKeepSelectedComponents1(self):
1995 #! [SnippeDataArrayDoubleKeepSelectedComponents1_1]
1996 arr1=[1.,2.,3.,4., # tuple 0
1997 11.,12.,13.,14., # tuple 1
1998 21.,22.,23.,24., # ...
2001 a1=DataArrayDouble.New()
2002 a1.setValues(arr1,5,4)
2003 a1.setInfoOnComponent(0,"a");
2004 a1.setInfoOnComponent(1,"b");
2005 a1.setInfoOnComponent(2,"c");
2006 a1.setInfoOnComponent(3,"d");
2007 #! [SnippeDataArrayDoubleKeepSelectedComponents1_1]
2008 #! [SnippeDataArrayDoubleKeepSelectedComponents1_2]
2010 a2=a1.keepSelectedComponents(arr2V)
2011 #! [SnippeDataArrayDoubleKeepSelectedComponents1_2]
2014 def testExampleDataArrayIntKeepSelectedComponents1(self):
2015 #! [SnippeDataArrayIntKeepSelectedComponents1_1]
2016 arr1=[1,2,3,4, # tuple 0
2017 11,12,13,14, # tuple 1
2021 a1=DataArrayInt.New()
2022 a1.setValues(arr1,5,4)
2023 a1.setInfoOnComponent(0,"a");
2024 a1.setInfoOnComponent(1,"b");
2025 a1.setInfoOnComponent(2,"c");
2026 a1.setInfoOnComponent(3,"d");
2027 #! [SnippeDataArrayIntKeepSelectedComponents1_1]
2028 #! [SnippeDataArrayIntKeepSelectedComponents1_2]
2030 a2=a1.keepSelectedComponents(arr2V)
2031 #! [SnippeDataArrayIntKeepSelectedComponents1_2]
2032 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
2034 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
2037 def testExampleFieldDoubleBuildSubPart1(self):
2038 from MEDCouplingDataForTest import MEDCouplingDataForTest
2039 #! [PySnippetFieldDoubleBuildSubPart1_1]
2040 mesh1=MEDCouplingDataForTest.build2DTargetMesh_1()
2041 f1=MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME)
2044 array=DataArrayDouble.New()
2045 arr1=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.]
2046 array.setValues(arr1,mesh1.getNumberOfCells(),2)
2048 # ! [PySnippetFieldDoubleBuildSubPart1_1]
2049 # ! [PySnippetFieldDoubleBuildSubPart1_2]
2051 f2=f1.buildSubPart(part1)
2052 # ! [PySnippetFieldDoubleBuildSubPart1_2]
2054 self.assertEqual(3,f2.getNumberOfTuples())
2055 self.assertEqual(2,f2.getNumberOfComponents())
2056 expected1=[5.,105.,4.,104.,7.,107.]
2058 self.assertAlmostEqual(f2.getIJ(0,i),expected1[i],12)
2060 self.assertEqual(3,f2.getMesh().getNumberOfCells())
2061 self.assertEqual(6,f2.getMesh().getNumberOfNodes())
2062 self.assertEqual(2,f2.getMesh().getSpaceDimension())
2063 self.assertEqual(2,f2.getMesh().getMeshDimension())
2065 self.assertEqual(13,m2C.getMeshLength())
2066 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]
2067 for i in xrange(12):
2068 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
2070 expected3=[3,2,3,1,3,0,2,1,4,4,5,3,2]
2071 self.assertEqual(expected3,list(m2C.getNodalConnectivity().getValues()))
2072 expected4=[0,4,8,13]
2073 self.assertEqual(expected4,list(m2C.getNodalConnectivityIndex().getValues()))
2074 # Test with field on nodes.
2075 # ! [PySnippetFieldDoubleBuildSubPart1_3]
2076 f1=MEDCouplingFieldDouble.New(ON_NODES,ONE_TIME)
2079 array=DataArrayDouble.New()
2080 arr2=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.,8.,108.,9.,109.,10.,110.,11.,111.]
2081 array.setValues(arr2,mesh1.getNumberOfNodes(),2)
2083 # ! [PySnippetFieldDoubleBuildSubPart1_3]
2084 # ! [PySnippetFieldDoubleBuildSubPart1_4]
2086 f2=f1.buildSubPart(part2)
2087 # ! [PySnippetFieldDoubleBuildSubPart1_4]
2088 self.assertEqual(4,f2.getNumberOfTuples())
2089 self.assertEqual(2,f2.getNumberOfComponents())
2090 expected5=[4.,104.,5.,105.,7.,107.,8.,108.]
2092 self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
2094 self.assertEqual(2,f2.getMesh().getNumberOfCells())
2095 self.assertEqual(4,f2.getMesh().getNumberOfNodes())
2096 self.assertEqual(2,f2.getMesh().getSpaceDimension())
2097 self.assertEqual(2,f2.getMesh().getMeshDimension())
2099 self.assertEqual(8,m2C.getMeshLength())
2100 for i in xrange(8):#8 is not an error
2101 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
2103 self.assertEqual(expected3[:4],[int(i) for i in m2C.getNodalConnectivity()][4:])
2104 self.assertEqual(expected3[4:8],[int(i) for i in m2C.getNodalConnectivity()][:4])
2105 self.assertEqual(expected4[:3],[int(i) for i in m2C.getNodalConnectivityIndex()])
2106 #idem previous because nodes of cell#4 are not fully present in part3
2108 arrr=DataArrayInt.New()
2109 arrr.setValues(part3,2,1)
2110 f2=f1.buildSubPart(arrr)
2111 self.assertEqual(4,f2.getNumberOfTuples())
2112 self.assertEqual(2,f2.getNumberOfComponents())
2114 self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
2116 self.assertEqual(2,f2.getMesh().getNumberOfCells())
2117 self.assertEqual(4,f2.getMesh().getNumberOfNodes())
2118 self.assertEqual(2,f2.getMesh().getSpaceDimension())
2119 self.assertEqual(2,f2.getMesh().getMeshDimension())
2121 self.assertEqual(8,m2C.getMeshLength())
2122 for i in xrange(8):#8 is not an error
2123 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
2125 self.assertEqual(expected3[:4],[int(i) for i in m2C.getNodalConnectivity()][4:8])
2126 self.assertEqual(expected3[4:8],[int(i) for i in m2C.getNodalConnectivity()][:4])
2127 self.assertEqual(expected4[:3],m2C.getNodalConnectivityIndex().getValues())
2129 f2=f1.buildSubPart(part4)
2130 self.assertEqual(6,f2.getNumberOfTuples())
2131 self.assertEqual(2,f2.getNumberOfComponents())
2132 expected6=[4.,104.,5.,105.,7.,107.,8.,108.,10.,110.,11.,111.]
2133 for i in xrange(12):
2134 self.assertAlmostEqual(f2.getIJ(0,i),expected6[i],12)
2136 self.assertEqual(3,f2.getMesh().getNumberOfCells())
2137 self.assertEqual(6,f2.getMesh().getNumberOfNodes())
2138 self.assertEqual(2,f2.getMesh().getSpaceDimension())
2139 self.assertEqual(2,f2.getMesh().getMeshDimension())
2141 self.assertEqual(13,m2C.getMeshLength())
2142 for i in xrange(12):
2143 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
2145 self.assertEqual(expected3[0:4],m2C.getNodalConnectivity().getValues()[4:8])
2146 self.assertEqual(expected3[4:8],m2C.getNodalConnectivity().getValues()[0:4])
2147 self.assertEqual(expected3[8:13],m2C.getNodalConnectivity().getValues()[8:13])
2148 self.assertEqual(expected4,m2C.getNodalConnectivityIndex().getValues())
2149 # previous line equivalent to
2150 self.assertEqual(expected4,[int(i) for i in m2C.getNodalConnectivityIndex()])
2153 def testExampleUMeshStdBuild1(self):
2154 # ! [PySnippetUMeshStdBuild1_1]
2155 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.,
2156 0.7,0.2,0., -0.3,0.7,0., 0.2,0.7,0., 0.7,0.7,0. ]
2157 nodalConnPerCell=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
2158 # ! [PySnippetUMeshStdBuild1_1]
2159 # ! [PySnippetUMeshStdBuild1_2]
2160 mesh=MEDCouplingUMesh.New("My2DMesh",2)
2161 # ! [PySnippetUMeshStdBuild1_2]
2162 # ! [PySnippetUMeshStdBuild1_3]
2163 mesh.allocateCells(5)#You can put more than 5 if you want but not less.
2164 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[:4])
2165 mesh.insertNextCell(NORM_TRI3,nodalConnPerCell[4:7])
2166 mesh.insertNextCell(NORM_TRI3,nodalConnPerCell[7:10])
2167 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[10:14])
2168 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[14:])
2169 mesh.finishInsertingCells()
2170 # ! [PySnippetUMeshStdBuild1_3]
2171 # ! [PySnippetUMeshStdBuild1_4]
2172 coordsArr=DataArrayDouble.New(coords,9,3)#here coordsArr are declared to have 3 components, mesh will deduce that its spaceDim==3.
2173 mesh.setCoords(coordsArr)#coordsArr contains 9 tuples, that is to say mesh contains 9 nodes.
2174 # ! [PySnippetUMeshStdBuild1_4]
2175 # ! [PySnippetUMeshStdBuild1_5]
2176 # ! [PySnippetUMeshStdBuild1_5]
2177 mesh.checkCoherency()
2180 def testExampleCMeshStdBuild1(self):
2181 # ! [PySnippetCMeshStdBuild1_1]
2182 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] # 9 values along X
2183 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] # 7 values along Y
2184 arrX=DataArrayDouble.New(XCoords)
2185 arrX.setInfoOnComponent(0,"X [m]")
2186 arrY=DataArrayDouble.New(YCoords)
2187 arrY.setInfoOnComponent(0,"Y [m]")
2188 # ! [PySnippetCMeshStdBuild1_1]
2189 # ! [PySnippetCMeshStdBuild1_2]
2190 mesh=MEDCouplingCMesh.New("My2D_CMesh")
2191 mesh.setCoords(arrX,arrY)
2192 # ! [PySnippetCMeshStdBuild1_2]
2193 # ! [PySnippetCMeshStdBuild1_3]
2194 self.assertEqual(8*6,mesh.getNumberOfCells())
2195 self.assertEqual(9*7,mesh.getNumberOfNodes())
2196 self.assertEqual(2,mesh.getSpaceDimension())
2197 self.assertEqual(2,mesh.getMeshDimension())
2198 # ! [PySnippetCMeshStdBuild1_3]
2199 mesh=MEDCouplingCMesh.New("My2D_CMesh")
2200 # ! [PySnippetCMeshStdBuild1_2bis]
2201 mesh.setCoordsAt(0,arrX)
2202 mesh.setCoordsAt(1,arrY)
2203 # ! [PySnippetCMeshStdBuild1_2bis]
2204 self.assertEqual(8*6,mesh.getNumberOfCells())
2205 self.assertEqual(9*7,mesh.getNumberOfNodes())
2206 self.assertEqual(2,mesh.getSpaceDimension())
2207 self.assertEqual(2,mesh.getMeshDimension())
2210 def testExampleUMeshAdvBuild1(self):
2211 # ! [PySnippetUMeshAdvBuild1_1]
2212 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.,
2213 0.7,0.2,0., -0.3,0.7,0., 0.2,0.7,0., 0.7,0.7,0. ]
2214 nodalConnPerCell=[4,0,3,4,1, 3,1,4,2, 3,4,5,2, 4,6,7,4,3, 4,7,8,5,4]
2215 nodalConnPerCellIndex=[0,5,9,13,18,23]
2216 # ! [PySnippetUMeshAdvBuild1_1]
2217 # ! [PySnippetUMeshAdvBuild1_2]
2218 mesh=MEDCouplingUMesh.New("My2DMesh",2)
2219 # ! [PySnippetUMeshAdvBuild1_2]
2220 # ! [PySnippetUMeshAdvBuild1_3]
2221 nodalConn=DataArrayInt.New(nodalConnPerCell,23,1)
2222 nodalConnI=DataArrayInt.New(nodalConnPerCellIndex,6,1)
2223 mesh.setConnectivity(nodalConn,nodalConnI,True)
2224 # ! [PySnippetUMeshAdvBuild1_3]
2225 # ! [PySnippetUMeshAdvBuild1_4]
2226 coordsArr=DataArrayDouble.New(coords,9,3)#here coordsArr are declared to have 3 components, mesh will deduce that its spaceDim==3.
2227 mesh.setCoords(coordsArr)#coordsArr contains 9 tuples, that is to say mesh contains 9 nodes.
2228 # ! [PySnippetUMeshAdvBuild1_4]
2229 # ! [PySnippetUMeshAdvBuild1_5]
2230 # ! [PySnippetUMeshAdvBuild1_5]
2231 mesh.checkCoherency()
2234 def testExampleDataArrayBuild1(self):
2235 # ! [PySnippetDataArrayBuild1_0]
2236 dataDouble=[0.,10.,20.,1.,11.,21.,2.,12.,22.,3.,13.,23.,4.,14.,24.]
2237 # ! [PySnippetDataArrayBuild1_0]
2238 # ! [PySnippetDataArrayBuild1_1]
2239 arrayDouble=DataArrayDouble.New()
2240 arrayDouble.setValues(dataDouble,5,3)# 5 tuples containing each 3 components
2241 # ! [PySnippetDataArrayBuild1_1]
2242 # ! [PySnippetDataArrayBuild1_1bis]
2243 arrayDouble=DataArrayDouble.New(dataDouble,5,3)
2244 # ! [PySnippetDataArrayBuild1_1bis]
2245 # ! [PySnippetDataArrayBuild1_2]
2246 dataInt=[0, 10, 20, 1, 11, 21, 2, 12, 22, 3, 13, 23, 4, 14, 24]
2247 # ! [PySnippetDataArrayBuild1_2]
2248 # ! [PySnippetDataArrayBuild1_3]
2249 arrayInt=DataArrayInt.New()
2250 arrayInt.setValues(dataInt,5,3)# 5 tuples containing each 3 components
2251 # ! [PySnippetDataArrayBuild1_3]
2252 # ! [PySnippetDataArrayBuild1_3bis]
2253 arrayInt=DataArrayInt.New(dataInt,5,3)
2254 # ! [PySnippetDataArrayBuild1_3bis]
2257 def testExampleFieldDoubleBuild1(self):
2258 XCoords=[-0.3,0.07,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
2259 YCoords=[0.07,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
2260 mesh=MEDCouplingCMesh.New("My2D_CMesh")
2261 mesh.setCoords(arrX,arrY)
2262 # ! [PySnippetFieldDoubleBuild1_1]
2263 fieldOnCells=MEDCouplingFieldDouble.New(ON_CELLS,NO_TIME)
2264 fieldOnCells.setName("MyTensorFieldOnCellNoTime")
2265 fieldOnCells.setMesh(mesh)
2266 array=DataArrayDouble.New()
2267 array.alloc(fieldOnCells.getMesh().getNumberOfCells(),9) # Implicitely fieldOnCells will be a 9 components field.
2268 array.fillWithValue(7.)
2269 fieldOnCells.setArray(array)
2270 # fieldOnCells is now usable
2272 # ! [PySnippetFieldDoubleBuild1_1]
2273 # ! [PySnippetFieldDoubleBuild1_2]
2274 f1=mesh.fillFromAnalytic(ON_CELLS,1,"x*x+y*y*3+2.*x") # f1 is scalar
2275 f2=mesh.fillFromAnalytic(ON_CELLS,1,"cos(x+y/x)") # f2 is scalar too
2276 f2bis=mesh.fillFromAnalytic(ON_CELLS,2,"x*x*IVec+3*y*JVec") # f2bis is a vectors field
2277 f3=f1+f2 # f3 scalar
2278 f4=f3/f2 # f4 scalar
2279 f2bis.applyFunc(1,"sqrt(x*x+y*y)") # f2bis becomes scalar
2280 f5=f2bis*f4 # f5 scalar
2282 res=f4.getValueOn(pos1) # f4 is scalar so the returned value is of size 1.
2284 # ! [PySnippetFieldDoubleBuild1_2]
2285 # ! [PySnippetFieldDoubleBuild1_3]
2286 # ! [PySnippetFieldDoubleBuild1_3]
2289 def testExampleFieldDoubleBuild2(self):
2290 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
2291 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
2292 mesh=MEDCouplingCMesh.New("My2D_CMesh")
2293 mesh.setCoords(arrX,arrY)
2294 # ! [PySnippetFieldDoubleBuild2_1]
2295 fieldOnNodes=MEDCouplingFieldDouble.New(ON_NODES,NO_TIME)
2296 fieldOnNodes.setName("MyScalarFieldOnNodeNoTime")
2297 fieldOnNodes.setMesh(mesh)
2298 array=DataArrayDouble.New()
2299 array.alloc(fieldOnNodes.getMesh().getNumberOfNodes(),1) # Implicitely fieldOnNodes will be a 1 component field.
2300 array.fillWithValue(7.)
2301 fieldOnNodes.setArray(array)
2302 # fieldOnNodes is now usable
2304 # ! [PySnippetFieldDoubleBuild2_1]
2307 def testExampleFieldDoubleBuild3(self):
2308 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
2309 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
2310 mesh=MEDCouplingCMesh.New("My2D_CMesh")
2311 mesh.setCoords(arrX,arrY)
2312 # ! [PySnippetFieldDoubleBuild3_1]
2313 fieldOnCells=MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME)
2314 fieldOnCells.setName("MyTensorFieldOnCellNoTime")
2315 fieldOnCells.setTimeUnit("ms") # Time unit is ms.
2316 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
2317 fieldOnCells.setMesh(mesh)
2318 array=DataArrayDouble.New()
2319 array.alloc(fieldOnCells.getMesh().getNumberOfCells(),2) # Implicitely fieldOnCells will be a 2 components field.
2320 array.fillWithValue(7.)
2321 fieldOnCells.setArray(array)
2322 # fieldOnCells is now usable
2324 # ! [PySnippetFieldDoubleBuild3_1]
2327 def testExampleFieldDoubleBuild4(self):
2328 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
2329 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
2330 mesh=MEDCouplingCMesh.New("My2D_CMesh")
2331 mesh.setCoords(arrX,arrY)
2332 # ! [PySnippetFieldDoubleBuild4_1]
2333 fieldOnNodes=MEDCouplingFieldDouble.New(ON_NODES,CONST_ON_TIME_INTERVAL)
2334 fieldOnNodes.setName("MyVecFieldOnNodeWithConstTime")
2335 fieldOnNodes.setTimeUnit("ms") # Time unit is ms.
2336 fieldOnNodes.setStartTime(4.22,2,-1)
2337 fieldOnNodes.setEndTime(6.44,4,-1)# fieldOnNodes is defined in interval [4.22 ms,6.44 ms]
2338 fieldOnNodes.setMesh(mesh)
2339 array=DataArrayDouble.New()
2340 array.alloc(fieldOnNodes.getMesh().getNumberOfNodes(),3) # Implicitely fieldOnNodes will be a 3 components field.
2341 array.fillWithValue(7.)
2342 fieldOnNodes.setArray(array)
2343 # fieldOnNodes is now usable
2345 # ! [PySnippetFieldDoubleBuild4_1]
2348 def testExampleDataArrayApplyFunc1(self):
2349 # ! [PySnippetDataArrayApplyFunc1_1]
2350 d=DataArrayDouble.New([1.,2.,11.,12.,21.,22.,31.,41.],4,2)
2351 self.assertRaises(InterpKernelException,d.applyFunc,"x*y")
2352 # ! [PySnippetDataArrayApplyFunc1_1]
2353 # ! [PySnippetDataArrayApplyFunc1_2]
2354 d=DataArrayDouble.New([1.,2.,11.,12.,21.,22.,31.,41.],4,2)
2355 d1=d.applyFunc("smth*smth")
2356 self.assertTrue(d1.isEqual(DataArrayDouble([1.,4.,121.,144.,441.,484.,961.,1681.],4,2),1e-12))
2357 # ! [PySnippetDataArrayApplyFunc1_2]
2358 # ! [PySnippetDataArrayApplyFunc1_3]
2359 d2=d.applyFunc("smth*IVec+2*smth*JVec")
2360 self.assertTrue(d2.isEqual(DataArrayDouble([1.,4.,11.,24.,21.,44.,31.,82.],4,2),1e-12))
2361 # ! [PySnippetDataArrayApplyFunc1_3]
2362 # ! [PySnippetDataArrayApplyFunc1_4]
2363 dd=DataArrayDouble.New([1.,4.,3.,11.,144.,13.,21.,484.,23.,31.,1024.,33.],4,3)
2364 # ! [PySnippetDataArrayApplyFunc1_4]
2365 # ! [PySnippetDataArrayApplyFunc1_5]
2366 dd1=dd.applyFunc(1,"f+sqrt(g)+h")
2367 self.assertTrue(dd1.isEqual(DataArrayDouble([6.,36.,66.,96.],4,1),1e-12))
2368 # ! [PySnippetDataArrayApplyFunc1_5]
2369 # ! [PySnippetDataArrayApplyFunc1_6]
2370 dd2=dd.applyFunc(1,"a+0.*b+c")
2371 self.assertTrue(dd2.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
2372 # ! [PySnippetDataArrayApplyFunc1_6]
2373 # ! [PySnippetDataArrayApplyFunc1_7]
2374 ddd=DataArrayDouble.New([1.,4.,3.,11.,144.,13.,21.,484.,23.,31.,1024.,33.],4,3)
2375 ddd.setInfoOnComponents(["Y [m]","AA [m/s]","GG [MW]"])
2376 # ! [PySnippetDataArrayApplyFunc1_7]
2377 # ! [PySnippetDataArrayApplyFunc1_8]
2378 ddd1=ddd.applyFunc2(1,"Y+GG")
2379 self.assertTrue(ddd1.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
2380 # ! [PySnippetDataArrayApplyFunc1_8]
2381 # ! [PySnippetDataArrayApplyFunc1_9]
2382 ddd1=ddd.applyFunc3(1,["X","Y","Z"],"X+Z")
2383 self.assertTrue(ddd1.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
2384 # ! [PySnippetDataArrayApplyFunc1_9]