1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 from MEDCoupling import *
25 class MEDCouplingBasicsTest(unittest.TestCase):
26 def testExample_MEDCouplingPointSet_(self):
27 #! [PySnippet_MEDCouplingPointSet__1]
29 # mesh.allocateCells(1);
30 # mesh.insertNextCell(NORM_QUAD4,4,range(4));
31 # mesh.finishInsertingCells();
33 def testExample_MEDCouplingPointSet_scale(self):
34 #! [PySnippet_MEDCouplingPointSet_scale_1]
35 coords=[0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0] # 2D coordinates of 4 nodes
36 coordsArr=DataArrayDouble.New();
37 coordsArr.setValues(coords,4,2);
38 mesh=MEDCouplingUMesh.New();
39 mesh.setCoords(coordsArr);
40 initCoords = coordsArr.deepCpy()
41 #! [PySnippet_MEDCouplingPointSet_scale_1]
42 #! [PySnippet_MEDCouplingPointSet_scale_2]
45 mesh.scale(center,factor)
46 #! [PySnippet_MEDCouplingPointSet_scale_2]
47 #! [PySnippet_MEDCouplingPointSet_scale_3]
48 coords2 = mesh.getCoords()
49 assert coords2.isEqualWithoutConsideringStr( initCoords, 1.0 )
50 assert not coords2.isEqualWithoutConsideringStr( initCoords, 0.9 )
51 #! [PySnippet_MEDCouplingPointSet_scale_3]
54 def testExample_MEDCouplingPointSet_translate(self):
55 #! [PySnippet_MEDCouplingPointSet_translate_1]
56 coords=[0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0] # 2D coordinates of 4 nodes
57 coordsArr=DataArrayDouble.New();
58 coordsArr.setValues(coords,4,2);
59 mesh=MEDCouplingUMesh.New();
60 mesh.setCoords(coordsArr);
61 initCoords = coordsArr.deepCpy()
62 #! [PySnippet_MEDCouplingPointSet_translate_1]
63 #! [PySnippet_MEDCouplingPointSet_translate_2]
65 mesh.translate(vector)
66 #! [PySnippet_MEDCouplingPointSet_translate_2]
67 #! [PySnippet_MEDCouplingPointSet_translate_3]
68 coords2 = mesh.getCoords()
69 assert coords2.isEqualWithoutConsideringStr( initCoords, 1 )
70 assert not coords2.isEqualWithoutConsideringStr( initCoords, 0.9 )
71 #! [PySnippet_MEDCouplingPointSet_translate_3]
74 def testExample_MEDCouplingPointSet_rotate(self):
75 #! [PySnippet_MEDCouplingPointSet_rotate_1]
76 coords=[0.0,0.0, 0.1,0.0, 0.1,0.1, 0.0,0.1] # 2D coordinates of 4 nodes
77 coordsArr=DataArrayDouble.New();
78 coordsArr.setValues(coords,4,2);
79 mesh=MEDCouplingUMesh.New();
80 mesh.setCoords(coordsArr);
81 #! [PySnippet_MEDCouplingPointSet_rotate_1]
82 #! [PySnippet_MEDCouplingPointSet_rotate_2]
84 mesh.rotate(center,-pi/2)
85 #! [PySnippet_MEDCouplingPointSet_rotate_2]
86 #! [PySnippet_MEDCouplingPointSet_rotate_3]
87 mesh.changeSpaceDimension(3)
90 mesh.rotate(center,vector,pi/2)
91 #! [PySnippet_MEDCouplingPointSet_rotate_3]
92 #! [PySnippet_MEDCouplingPointSet_rotate_4]
93 mesh.changeSpaceDimension(2)
94 coords2 = mesh.getCoords().getValues()
95 for i,c in enumerate( coords ):
96 self.assertAlmostEqual( c, coords2[i], 13 )
97 #! [PySnippet_MEDCouplingPointSet_rotate_4]
100 def testExample_MEDCouplingPointSet_getBoundingBox(self):
101 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_1]
102 cc=[0.0, 0.1, 0.2, # 3D coordinates of 2 nodes
104 coordsArr=DataArrayDouble.New();
105 coordsArr.setValues(cc,2,3);
106 mesh=MEDCouplingUMesh.New();
107 mesh.setCoords(coordsArr);
108 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_1]
109 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_2]
110 bbox=mesh.getBoundingBox()
111 assert bbox == [( cc[0], cc[3] ), # NOTE: list of 3 tuples is retirned!
114 #! [PySnippet_MEDCouplingPointSet_getBoundingBox_2]
116 def testExample_MEDCouplingPointSet_getNodeIdsNearPoint(self):
117 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_1]
118 # 2D coordinates of 5 nodes
119 coords=[0.3,-0.301, # 0
124 coordsArr=DataArrayDouble.New();
125 coordsArr.setValues(coords,5,2);
126 mesh=MEDCouplingUMesh.New();
127 mesh.setCoords(coordsArr);
128 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_1]
129 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_2]
130 point=[0.3, -0.3] # point close to nodes #0, #2 and #4
131 ids=mesh.getNodeIdsNearPoint(point,0.003);
132 assert ids.getValues() == [0,2,4]
133 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_2]
136 def testExample_MEDCouplingPointSet_getNodeIdsNearPoints(self):
137 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_1]
138 # 2D coordinates of 7 nodes
139 coords=[0.3,-0.301, # 0
146 coordsArr=DataArrayDouble.New();
147 coordsArr.setValues(coords,7,2);
148 mesh=MEDCouplingUMesh.New();
149 mesh.setCoords(coordsArr);
150 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_1]
151 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_2]
152 points=[0.2,-0.301, # ~ node #1
154 1.1, 0.002] # ~ nodes #3, #4 and #5
155 ids,idsIndex=mesh.getNodeIdsNearPoints(points,3,0.003);
156 assert ids.getValues() == [1, 3, 4, 5]
157 print idsIndex.getValues()
158 #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_2]
161 def testExample_MEDCouplingPointSet_findCommonNodes(self):
162 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_1]
163 coords=[0.3,-0.301, # 0
169 coordsArr=DataArrayDouble.New();
170 coordsArr.setValues(coords,6,2);
171 mesh=MEDCouplingUMesh.New();
172 mesh.setCoords(coordsArr);
173 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_1]
174 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_2]
175 comm,commI=mesh.findCommonNodes(1e-13)
176 assert comm.getValues() == [3,4]
177 comm,commI=mesh.findCommonNodes(0.004)
178 assert comm.getValues() == [0,2,5,3,4]
179 #! [PySnippet_MEDCouplingPointSet_findCommonNodes_2]
182 def testExample_MEDCouplingPointSet_getCoordinatesOfNode(self):
183 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_1]
184 coords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3];
185 coordsArr=DataArrayDouble.New();
186 coordsArr.setValues(coords,3,2);
187 mesh=MEDCouplingUMesh.New();
188 mesh.setCoords(coordsArr);
189 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_1]
190 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_2]
191 nodeCoords=mesh.getCoordinatesOfNode(1)
192 self.assertAlmostEqual(0.2, nodeCoords[0],13);
193 self.assertAlmostEqual(-0.3,nodeCoords[1],13);
194 #! [PySnippet_MEDCouplingPointSet_getCoordinatesOfNode_2]
197 def testExample_DataArrayInt_getTuple(self):
198 #! [Snippet_DataArrayInt_getTuple_1]
199 dv=DataArrayInt.New();
203 print dv.getTuple( 1 )
204 #! [Snippet_DataArrayInt_getTuple_1]
205 #! [Snippet_DataArrayInt_getTuple_2]
208 #! [Snippet_DataArrayInt_getTuple_2]
211 def testExample_DataArrayInt_buildPermutationArr(self):
212 #! [PySnippet_DataArrayInt_buildPermutationArr_1]
214 a.setValues([4,5,6,7,8],5,1)
216 b.setValues([5,4,8,6,7],5,1)
217 c=a.buildPermutationArr(b)
218 #! [PySnippet_DataArrayInt_buildPermutationArr_1]
219 self.assertEqual([1,0,4,2,3],c.getValues())
222 def testExample_DataArrayInt_invertArrayO2N2N2O(self):
223 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
225 da=DataArrayInt.New();
226 da.setValues(arr1,6,1);
227 da2=da.invertArrayO2N2N2O(6);
228 expected1=[1,3,0,5,2,4]
230 self.assertEqual(expected1[i],da2.getIJ(i,0));
232 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
235 def testExample_DataArrayInt_invertArrayN2O2O2N(self):
236 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
238 da=DataArrayInt.New();
239 da.setValues(arr1,6,1);
240 da2=da.invertArrayN2O2O2N(7);
241 expected1=[1,3,0,5,2,4,-1]
243 self.assertEqual(expected1[i],da2.getIJ(i,0));
245 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
249 def testExample_DataArrayDouble_getIdsInRange(self):
250 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
251 da=DataArrayDouble.New()
253 da[ :, :] = range(10)
254 da2 = da.getIdsInRange( 2.5, 6 )
255 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
258 def testExample_DataArrayDouble_setPartOfValues2(self):
259 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
260 da=DataArrayDouble.New()
263 dv=DataArrayDouble.New();
267 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
268 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
270 da[ [0,1,2], [1,3] ] = dv
271 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
272 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
275 da[ [0,2,3], [0,2,3,4,5,6]] = dv
276 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
279 def testExample_DataArrayInt_setPartOfValues2(self):
280 #! [Snippet_DataArrayInt_setPartOfValues2_1]
281 da=DataArrayInt.New()
284 dv=DataArrayInt.New();
288 #! [Snippet_DataArrayInt_setPartOfValues2_1]
289 #! [Snippet_DataArrayInt_setPartOfValues2_2]
291 da[ [0,1,2], [1,3] ] = dv
292 #! [Snippet_DataArrayInt_setPartOfValues2_2]
293 #! [Snippet_DataArrayInt_setPartOfValues2_3]
296 da[ [0,2,3], [0,2,3,4,5,6]] = dv
297 #! [Snippet_DataArrayInt_setPartOfValues2_3]
300 def testExample_DataArrayDouble_setPartOfValues3(self):
301 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
302 da=DataArrayDouble.New()
305 dv=DataArrayDouble.New();
309 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
310 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
312 da[ 0:3, [1,3] ] = dv
313 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
314 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
317 da[ 0:4:2, [0,2,3,4,5,6]] = dv
318 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
321 def testExample_DataArrayInt_setPartOfValues3(self):
322 #! [Snippet_DataArrayInt_setPartOfValues3_1]
323 da=DataArrayInt.New()
326 dv=DataArrayInt.New();
330 #! [Snippet_DataArrayInt_setPartOfValues3_1]
331 #! [Snippet_DataArrayInt_setPartOfValues3_2]
333 da[ 0:3, [1,3] ] = dv
334 #! [Snippet_DataArrayInt_setPartOfValues3_2]
335 #! [Snippet_DataArrayInt_setPartOfValues3_3]
338 da[ 0:4:2, [0,2,3,4,5,6]] = dv
339 #! [Snippet_DataArrayInt_setPartOfValues3_3]
342 def testExample_DataArrayDouble_setPartOfValues1(self):
343 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
344 da=DataArrayDouble.New()
346 da.setInfoOnComponents( ["v1","v2","v3","v4"])
348 dv=DataArrayDouble.New();
352 dv.setInfoOnComponents( ["a1","a2"])
353 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
354 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
356 da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
357 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
358 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
360 da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
361 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
362 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
364 da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
365 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
366 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
368 da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
369 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
370 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
373 da2[ 0:3:2, 1:4:2 ] = dv
374 self.assertTrue( da.isEqual( da2, 1e-20 ))
375 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
378 def testExample_DataArrayInt_setPartOfValues1(self):
379 #! [Snippet_DataArrayInt_setPartOfValues1_1]
380 da=DataArrayInt.New()
382 da.setInfoOnComponents( ["v1","v2","v3","v4"])
384 dv=DataArrayInt.New();
388 dv.setInfoOnComponents( ["a1","a2"])
389 #! [Snippet_DataArrayInt_setPartOfValues1_1]
390 #! [Snippet_DataArrayInt_setPartOfValues1_2]
392 da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
393 #! [Snippet_DataArrayInt_setPartOfValues1_2]
394 #! [Snippet_DataArrayInt_setPartOfValues1_3]
396 da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
397 #! [Snippet_DataArrayInt_setPartOfValues1_3]
398 #! [Snippet_DataArrayInt_setPartOfValues1_4]
400 da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
401 #! [Snippet_DataArrayInt_setPartOfValues1_4]
402 #! [Snippet_DataArrayInt_setPartOfValues1_5]
404 da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
405 #! [Snippet_DataArrayInt_setPartOfValues1_5]
406 #! [Snippet_DataArrayInt_setPartOfValues1_6]
409 da2[ 0:3:2, 1:4:2 ] = dv
410 self.assertTrue( da.isEqual( da2 ))
411 #! [Snippet_DataArrayInt_setPartOfValues1_6]
414 def testExample_DataArrayDouble_setPartOfValuesSimple1(self):
415 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
416 da=DataArrayDouble.New()
419 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
420 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
422 da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
423 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
424 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
426 da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
427 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
428 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
430 da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
431 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
432 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
434 da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
435 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
436 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
439 da2[ 0:3:2, 1:4:2 ] = dv
440 self.assertTrue( da.isEqual( da2, 1e-20 ))
441 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
444 def testExample_DataArrayInt_setPartOfValuesSimple1(self):
445 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
446 da=DataArrayInt.New()
449 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
450 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
452 da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
453 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
454 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
456 da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
457 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
458 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
460 da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
461 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
462 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
464 da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
465 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
466 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
469 da2[ 0:3:2, 1:4:2 ] = dv
470 self.assertTrue( da.isEqual( da2 ))
471 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
474 def testExample_DataArrayDouble_setPartOfValuesSimple2(self):
475 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
476 da=DataArrayDouble.New()
479 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
480 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
482 da[[1,2], [1,2]] = dv
483 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
484 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
486 da[[0,1,2,3], [1]] = dv
487 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
488 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
490 da[[1], [0,1,2,3]] = dv
491 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
492 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
494 da[[0,2], [1,3]] = dv
495 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
498 def testExample_DataArrayInt_setPartOfValuesSimple2(self):
499 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
500 da=DataArrayInt.New()
503 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
504 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
506 da[[1,2], [1,2]] = dv
507 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
508 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
510 da[[0,1,2,3], [1]] = dv
511 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
512 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
514 da[[1], [0,1,2,3]] = dv
515 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
516 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
518 da[[0,2], [1,3]] = dv
519 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
522 def testExample_DataArrayDouble_setPartOfValuesSimple3(self):
523 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
524 da=DataArrayDouble.New()
527 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
528 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
531 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
532 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
534 da[[0,1,2,3], 1:2] = dv
535 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
536 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
539 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
540 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
542 da[[0,2], 1:4:2] = dv
543 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
546 def testExample_DataArrayInt_setPartOfValuesSimple3(self):
547 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
548 da=DataArrayInt.New()
551 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
552 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
555 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
556 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
558 da[[0,1,2,3], 1:2] = dv
559 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
560 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
563 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
564 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
566 da[[0,2], 1:4:2] = dv
567 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
570 def testExample_DataArrayDouble_setSelectedComponents(self):
571 #! [Snippet_DataArrayDouble_setSelectedComponents1]
572 da=DataArrayDouble.New();
573 array1=[1.,2., 3.,4., 5.,6.]
574 da.setValues(array1,3,2)
575 da.setInfoOnComponents( ["a1","a2"])
576 #! [Snippet_DataArrayDouble_setSelectedComponents1]
577 #! [Snippet_DataArrayDouble_setSelectedComponents2]
578 dv=DataArrayDouble.New();
581 dv.setInfoOnComponents( ["v1","v2","v3","v4"])
583 dv.setSelectedComponents( da, [1,0] )
584 #! [Snippet_DataArrayDouble_setSelectedComponents2]
585 #! [Snippet_DataArrayDouble_setSelectedComponents3]
587 self.assertTrue( dv.isEqualWithoutConsideringStr( dv2, 1e-20 ))
588 #! [Snippet_DataArrayDouble_setSelectedComponents3]
591 def testExample_DataArrayInt_setSelectedComponents(self):
592 #! [Snippet_DataArrayInt_setSelectedComponents1]
593 da=DataArrayInt.New();
594 array1=[1,2, 3,4, 5,6]
595 da.setValues(array1,3,2)
596 da.setInfoOnComponents( ["a1","a2"])
597 #! [Snippet_DataArrayInt_setSelectedComponents1]
598 #! [Snippet_DataArrayInt_setSelectedComponents2]
599 dv=DataArrayInt.New();
602 dv.setInfoOnComponents( ["v1","v2","v3","v4"])
604 dv.setSelectedComponents( da, [1,0] )
605 #! [Snippet_DataArrayInt_setSelectedComponents2]
606 #! [Snippet_DataArrayInt_setSelectedComponents3]
608 self.assertTrue( dv.isEqualWithoutConsideringStr( dv2 ))
609 #! [Snippet_DataArrayInt_setSelectedComponents3]
612 def testExample_DataArrayDouble_getDifferentValues(self):
613 #! [Snippet_DataArrayDouble_getDifferentValues1]
614 da=DataArrayDouble.New();
615 array1=[2.3,1.2,1.3,2.3,2.301,0.8]
616 da.setValues(array1,6,1)
618 dv=da.getDifferentValues(2e-1);
619 expected2=[2.301,1.3,0.8]
620 self.assertEqual(3,dv.getNbOfElems());
622 self.assertAlmostEqual(expected2[i],dv.getIJ(i,0),14);
624 #! [Snippet_DataArrayDouble_getDifferentValues1]
627 def testExample_DataArrayDouble_findCommonTuples1(self):
628 #! [PySnippet_DataArrayDouble_findCommonTuples1]
629 da=DataArrayDouble.New();
630 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]
631 da.setValues(array2,6,2)
632 #! [PySnippet_DataArrayDouble_findCommonTuples1]
633 #! [PySnippet_DataArrayDouble_findCommonTuples2]
634 c,cI=da.findCommonTuples(1e-1);
635 expected3=[0,3,4,1,2]
637 self.assertEqual(expected3,c.getValues())
638 self.assertEqual(expected4,cI.getValues())
639 #! [PySnippet_DataArrayDouble_findCommonTuples2]
642 def testExampleDataArrayDoubleMeldWith(self):
643 #! [PySnippet_DataArrayDouble_Meld1_1]
644 da1=DataArrayDouble.New();
646 da2=DataArrayDouble.New();
649 da1.fillWithValue(7.);
651 da3=da2.applyFunc(3,"10*x*IVec+100*x*JVec+1000*x*KVec");
653 da1.setInfoOnComponent(0,"c0da1");
654 da1.setInfoOnComponent(1,"c1da1");
655 da3.setInfoOnComponent(0,"c0da3");
656 da3.setInfoOnComponent(1,"c1da3");
657 da3.setInfoOnComponent(2,"c2da3");
661 #! [PySnippet_DataArrayDouble_Meld1_1]
663 def testExampleDataArrayIntMeldWith(self):
664 #! [PySnippet_DataArrayInt_Meld1_1]
665 da1=DataArrayInt.New();
667 da2=DataArrayInt.New();
670 da1.fillWithValue(7);
673 da1.setInfoOnComponent(0,"c0da1");
674 da1.setInfoOnComponent(1,"c1da1");
675 da2.setInfoOnComponent(0,"c0da2");
678 #! [PySnippet_DataArrayInt_Meld1_1]
680 def testExampleDataArrayDoubleKeepSelectedComponents1(self):
681 #! [SnippeDataArrayDoubleKeepSelectedComponents1_1]
682 arr1=[1.,2.,3.,4., # tuple 0
683 11.,12.,13.,14., # tuple 1
684 21.,22.,23.,24., # ...
687 a1=DataArrayDouble.New()
688 a1.setValues(arr1,5,4)
689 a1.setInfoOnComponent(0,"a");
690 a1.setInfoOnComponent(1,"b");
691 a1.setInfoOnComponent(2,"c");
692 a1.setInfoOnComponent(3,"d");
693 #! [SnippeDataArrayDoubleKeepSelectedComponents1_1]
694 #! [SnippeDataArrayDoubleKeepSelectedComponents1_2]
696 a2=a1.keepSelectedComponents(arr2V)
697 #! [SnippeDataArrayDoubleKeepSelectedComponents1_2]
700 def testExampleDataArrayIntKeepSelectedComponents1(self):
701 #! [SnippeDataArrayIntKeepSelectedComponents1_1]
702 arr1=[1,2,3,4, # tuple 0
703 11,12,13,14, # tuple 1
707 a1=DataArrayInt.New()
708 a1.setValues(arr1,5,4)
709 a1.setInfoOnComponent(0,"a");
710 a1.setInfoOnComponent(1,"b");
711 a1.setInfoOnComponent(2,"c");
712 a1.setInfoOnComponent(3,"d");
713 #! [SnippeDataArrayIntKeepSelectedComponents1_1]
714 #! [SnippeDataArrayIntKeepSelectedComponents1_2]
716 a2=a1.keepSelectedComponents(arr2V)
717 #! [SnippeDataArrayIntKeepSelectedComponents1_2]
718 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
720 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
723 def testExampleFieldDoubleBuildSubPart1(self):
724 from MEDCouplingDataForTest import MEDCouplingDataForTest
725 #! [PySnippetFieldDoubleBuildSubPart1_1]
726 mesh1=MEDCouplingDataForTest.build2DTargetMesh_1()
727 f1=MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME)
730 array=DataArrayDouble.New()
731 arr1=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.]
732 array.setValues(arr1,mesh1.getNumberOfCells(),2)
734 # ! [PySnippetFieldDoubleBuildSubPart1_1]
735 # ! [PySnippetFieldDoubleBuildSubPart1_2]
737 f2=f1.buildSubPart(part1)
738 # ! [PySnippetFieldDoubleBuildSubPart1_2]
740 self.assertEqual(3,f2.getNumberOfTuples())
741 self.assertEqual(2,f2.getNumberOfComponents())
742 expected1=[5.,105.,4.,104.,7.,107.]
744 self.assertAlmostEqual(f2.getIJ(0,i),expected1[i],12)
746 self.assertEqual(3,f2.getMesh().getNumberOfCells())
747 self.assertEqual(6,f2.getMesh().getNumberOfNodes())
748 self.assertEqual(2,f2.getMesh().getSpaceDimension())
749 self.assertEqual(2,f2.getMesh().getMeshDimension())
751 self.assertEqual(13,m2C.getMeshLength())
752 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]
754 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
756 expected3=[3,2,3,1,3,0,2,1,4,4,5,3,2]
757 self.assertEqual(expected3,list(m2C.getNodalConnectivity().getValues()))
759 self.assertEqual(expected4,list(m2C.getNodalConnectivityIndex().getValues()))
760 # Test with field on nodes.
761 # ! [PySnippetFieldDoubleBuildSubPart1_3]
762 f1=MEDCouplingFieldDouble.New(ON_NODES,ONE_TIME)
765 array=DataArrayDouble.New()
766 arr2=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.,8.,108.,9.,109.,10.,110.,11.,111.]
767 array.setValues(arr2,mesh1.getNumberOfNodes(),2)
769 # ! [PySnippetFieldDoubleBuildSubPart1_3]
770 # ! [PySnippetFieldDoubleBuildSubPart1_4]
772 f2=f1.buildSubPart(part2)
773 # ! [PySnippetFieldDoubleBuildSubPart1_4]
774 self.assertEqual(4,f2.getNumberOfTuples())
775 self.assertEqual(2,f2.getNumberOfComponents())
776 expected5=[4.,104.,5.,105.,7.,107.,8.,108.]
778 self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
780 self.assertEqual(2,f2.getMesh().getNumberOfCells())
781 self.assertEqual(4,f2.getMesh().getNumberOfNodes())
782 self.assertEqual(2,f2.getMesh().getSpaceDimension())
783 self.assertEqual(2,f2.getMesh().getMeshDimension())
785 self.assertEqual(8,m2C.getMeshLength())
786 for i in xrange(8):#8 is not an error
787 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
789 self.assertEqual(expected3[:4],list(m2C.getNodalConnectivity().getValues())[4:])
790 self.assertEqual(expected3[4:8],list(m2C.getNodalConnectivity().getValues())[:4])
791 self.assertEqual(expected4[:3],list(m2C.getNodalConnectivityIndex().getValues()))
792 #idem previous because nodes of cell#4 are not fully present in part3
794 arrr=DataArrayInt.New()
795 arrr.setValues(part3,2,1)
796 f2=f1.buildSubPart(arrr)
797 self.assertEqual(4,f2.getNumberOfTuples())
798 self.assertEqual(2,f2.getNumberOfComponents())
800 self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
802 self.assertEqual(2,f2.getMesh().getNumberOfCells())
803 self.assertEqual(4,f2.getMesh().getNumberOfNodes())
804 self.assertEqual(2,f2.getMesh().getSpaceDimension())
805 self.assertEqual(2,f2.getMesh().getMeshDimension())
807 self.assertEqual(8,m2C.getMeshLength())
808 for i in xrange(8):#8 is not an error
809 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
811 self.assertEqual(expected3[:4],list(m2C.getNodalConnectivity().getValues())[4:8])
812 self.assertEqual(expected3[4:8],list(m2C.getNodalConnectivity().getValues())[:4])
813 self.assertEqual(expected4[:3],list(m2C.getNodalConnectivityIndex().getValues()))
815 f2=f1.buildSubPart(part4)
816 self.assertEqual(6,f2.getNumberOfTuples())
817 self.assertEqual(2,f2.getNumberOfComponents())
818 expected6=[4.,104.,5.,105.,7.,107.,8.,108.,10.,110.,11.,111.]
820 self.assertAlmostEqual(f2.getIJ(0,i),expected6[i],12)
822 self.assertEqual(3,f2.getMesh().getNumberOfCells())
823 self.assertEqual(6,f2.getMesh().getNumberOfNodes())
824 self.assertEqual(2,f2.getMesh().getSpaceDimension())
825 self.assertEqual(2,f2.getMesh().getMeshDimension())
827 self.assertEqual(13,m2C.getMeshLength())
829 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
831 self.assertEqual(expected3[0:4],list(m2C.getNodalConnectivity().getValues())[4:8])
832 self.assertEqual(expected3[4:8],list(m2C.getNodalConnectivity().getValues())[0:4])
833 self.assertEqual(expected3[8:13],list(m2C.getNodalConnectivity().getValues())[8:13])
834 self.assertEqual(expected4,list(m2C.getNodalConnectivityIndex().getValues()))
837 def testExampleUMeshStdBuild1(self):
838 # ! [PySnippetUMeshStdBuild1_1]
839 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.,
840 0.7,0.2,0., -0.3,0.7,0., 0.2,0.7,0., 0.7,0.7,0. ]
841 nodalConnPerCell=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
842 # ! [PySnippetUMeshStdBuild1_1]
843 # ! [PySnippetUMeshStdBuild1_2]
844 mesh=MEDCouplingUMesh.New("My2DMesh",2)
845 # ! [PySnippetUMeshStdBuild1_2]
846 # ! [PySnippetUMeshStdBuild1_3]
847 mesh.allocateCells(5)#You can put more than 5 if you want but not less.
848 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[:4])
849 mesh.insertNextCell(NORM_TRI3,nodalConnPerCell[4:7])
850 mesh.insertNextCell(NORM_TRI3,nodalConnPerCell[7:10])
851 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[10:14])
852 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[14:])
853 mesh.finishInsertingCells()
854 # ! [PySnippetUMeshStdBuild1_3]
855 # ! [PySnippetUMeshStdBuild1_4]
856 myCoords=DataArrayDouble.New(coords,9,3)#here myCoords are declared to have 3 components, mesh will deduce that its spaceDim==3.
857 mesh.setCoords(myCoords)#myCorrds contains 9 tuples, that is to say mesh contains 9 nodes.
858 # ! [PySnippetUMeshStdBuild1_4]
859 # ! [PySnippetUMeshStdBuild1_5]
860 # ! [PySnippetUMeshStdBuild1_5]
861 mesh.checkCoherency()
864 def testExampleCMeshStdBuild1(self):
865 # ! [PySnippetCMeshStdBuild1_1]
866 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] # 9 values along X
867 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] # 7 values along Y
868 arrX=DataArrayDouble.New(XCoords)
869 arrX.setInfoOnComponent(0,"X [m]")
870 arrY=DataArrayDouble.New(YCoords)
871 arrY.setInfoOnComponent(0,"Y [m]")
872 # ! [PySnippetCMeshStdBuild1_1]
873 # ! [PySnippetCMeshStdBuild1_2]
874 mesh=MEDCouplingCMesh.New("My2D_CMesh")
875 mesh.setCoords(arrX,arrY)
876 # ! [PySnippetCMeshStdBuild1_2]
877 # ! [PySnippetCMeshStdBuild1_3]
878 self.assertEqual(8*6,mesh.getNumberOfCells())
879 self.assertEqual(9*7,mesh.getNumberOfNodes())
880 self.assertEqual(2,mesh.getSpaceDimension())
881 self.assertEqual(2,mesh.getMeshDimension())
882 # ! [PySnippetCMeshStdBuild1_3]
883 mesh=MEDCouplingCMesh.New("My2D_CMesh")
884 # ! [PySnippetCMeshStdBuild1_2bis]
885 mesh.setCoordsAt(0,arrX)
886 mesh.setCoordsAt(1,arrY)
887 # ! [PySnippetCMeshStdBuild1_2bis]
888 self.assertEqual(8*6,mesh.getNumberOfCells())
889 self.assertEqual(9*7,mesh.getNumberOfNodes())
890 self.assertEqual(2,mesh.getSpaceDimension())
891 self.assertEqual(2,mesh.getMeshDimension())
894 def testExampleUMeshAdvBuild1(self):
895 # ! [PySnippetUMeshAdvBuild1_1]
896 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.,
897 0.7,0.2,0., -0.3,0.7,0., 0.2,0.7,0., 0.7,0.7,0. ]
898 nodalConnPerCell=[4,0,3,4,1, 3,1,4,2, 3,4,5,2, 4,6,7,4,3, 4,7,8,5,4]
899 nodalConnPerCellIndex=[0,5,9,13,18,23]
900 # ! [PySnippetUMeshAdvBuild1_1]
901 # ! [PySnippetUMeshAdvBuild1_2]
902 mesh=MEDCouplingUMesh.New("My2DMesh",2)
903 # ! [PySnippetUMeshAdvBuild1_2]
904 # ! [PySnippetUMeshAdvBuild1_3]
905 nodalConn=DataArrayInt.New(nodalConnPerCell,23,1)
906 nodalConnI=DataArrayInt.New(nodalConnPerCellIndex,6,1)
907 mesh.setConnectivity(nodalConn,nodalConnI,True)
908 # ! [PySnippetUMeshAdvBuild1_3]
909 # ! [PySnippetUMeshAdvBuild1_4]
910 myCoords=DataArrayDouble.New(coords,9,3)#here myCoords are declared to have 3 components, mesh will deduce that its spaceDim==3.
911 mesh.setCoords(myCoords)#myCorrds contains 9 tuples, that is to say mesh contains 9 nodes.
912 # ! [PySnippetUMeshAdvBuild1_4]
913 # ! [PySnippetUMeshAdvBuild1_5]
914 # ! [PySnippetUMeshAdvBuild1_5]
915 mesh.checkCoherency()
918 def testExampleDataArrayBuild1(self):
919 # ! [PySnippetDataArrayBuild1_0]
920 dataDouble=[0.,10.,20.,1.,11.,21.,2.,12.,22.,3.,13.,23.,4.,14.,24.]
921 # ! [PySnippetDataArrayBuild1_0]
922 # ! [PySnippetDataArrayBuild1_1]
923 arrayDouble=DataArrayDouble.New()
924 arrayDouble.setValues(dataDouble,5,3)# 5 tuples containing each 3 components
925 # ! [PySnippetDataArrayBuild1_1]
926 # ! [PySnippetDataArrayBuild1_1bis]
927 arrayDouble=DataArrayDouble.New(dataDouble,5,3)
928 # ! [PySnippetDataArrayBuild1_1bis]
929 # ! [PySnippetDataArrayBuild1_2]
930 dataInt=[0, 10, 20, 1, 11, 21, 2, 12, 22, 3, 13, 23, 4, 14, 24]
931 # ! [PySnippetDataArrayBuild1_2]
932 # ! [PySnippetDataArrayBuild1_3]
933 arrayInt=DataArrayInt.New()
934 arrayInt.setValues(dataInt,5,3)# 5 tuples containing each 3 components
935 # ! [PySnippetDataArrayBuild1_3]
936 # ! [PySnippetDataArrayBuild1_3bis]
937 arrayInt=DataArrayInt.New(dataInt,5,3)
938 # ! [PySnippetDataArrayBuild1_3bis]
941 def testExampleFieldDoubleBuild1(self):
942 XCoords=[-0.3,0.07,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
943 YCoords=[0.07,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
944 mesh=MEDCouplingCMesh.New("My2D_CMesh")
945 mesh.setCoords(arrX,arrY)
946 # ! [PySnippetFieldDoubleBuild1_1]
947 fieldOnCells=MEDCouplingFieldDouble.New(ON_CELLS,NO_TIME)
948 fieldOnCells.setName("MyTensorFieldOnCellNoTime")
949 fieldOnCells.setMesh(mesh)
950 array=DataArrayDouble.New()
951 array.alloc(fieldOnCells.getMesh().getNumberOfCells(),9) # Implicitely fieldOnCells will be a 9 components field.
952 array.fillWithValue(7.)
953 fieldOnCells.setArray(array)
954 # fieldOnCells is now usable
956 # ! [PySnippetFieldDoubleBuild1_1]
957 # ! [PySnippetFieldDoubleBuild1_2]
958 f1=mesh.fillFromAnalytic(ON_CELLS,1,"x*x+y*y*3+2.*x") # f1 is scalar
959 f2=mesh.fillFromAnalytic(ON_CELLS,1,"cos(x+y/x)") # f2 is scalar too
960 f2bis=mesh.fillFromAnalytic(ON_CELLS,2,"x*x*IVec+3*y*JVec") # f2bis is a vectors field
963 f2bis.applyFunc(1,"sqrt(x*x+y*y)") # f2bis becomes scalar
964 f5=f2bis*f4 # f5 scalar
966 res=f4.getValueOn(pos1) # f4 is scalar so the returned value is of size 1.
968 # ! [PySnippetFieldDoubleBuild1_2]
969 # ! [PySnippetFieldDoubleBuild1_3]
970 # ! [PySnippetFieldDoubleBuild1_3]
973 def testExampleFieldDoubleBuild2(self):
974 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
975 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
976 mesh=MEDCouplingCMesh.New("My2D_CMesh")
977 mesh.setCoords(arrX,arrY)
978 # ! [PySnippetFieldDoubleBuild2_1]
979 fieldOnNodes=MEDCouplingFieldDouble.New(ON_NODES,NO_TIME)
980 fieldOnNodes.setName("MyScalarFieldOnNodeNoTime")
981 fieldOnNodes.setMesh(mesh)
982 array=DataArrayDouble.New()
983 array.alloc(fieldOnNodes.getMesh().getNumberOfNodes(),1) # Implicitely fieldOnNodes will be a 1 component field.
984 array.fillWithValue(7.)
985 fieldOnNodes.setArray(array)
986 # fieldOnNodes is now usable
988 # ! [PySnippetFieldDoubleBuild2_1]
991 def testExampleFieldDoubleBuild3(self):
992 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
993 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
994 mesh=MEDCouplingCMesh.New("My2D_CMesh")
995 mesh.setCoords(arrX,arrY)
996 # ! [PySnippetFieldDoubleBuild3_1]
997 fieldOnCells=MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME)
998 fieldOnCells.setName("MyTensorFieldOnCellNoTime")
999 fieldOnCells.setTimeUnit("ms") # Time unit is ms.
1000 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
1001 fieldOnCells.setMesh(mesh)
1002 array=DataArrayDouble.New()
1003 array.alloc(fieldOnCells.getMesh().getNumberOfCells(),2) # Implicitely fieldOnCells will be a 2 components field.
1004 array.fillWithValue(7.)
1005 fieldOnCells.setArray(array)
1006 # fieldOnCells is now usable
1008 # ! [PySnippetFieldDoubleBuild3_1]
1011 def testExampleFieldDoubleBuild4(self):
1012 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
1013 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
1014 mesh=MEDCouplingCMesh.New("My2D_CMesh")
1015 mesh.setCoords(arrX,arrY)
1016 # ! [PySnippetFieldDoubleBuild4_1]
1017 fieldOnNodes=MEDCouplingFieldDouble.New(ON_NODES,CONST_ON_TIME_INTERVAL)
1018 fieldOnNodes.setName("MyVecFieldOnNodeWithConstTime")
1019 fieldOnNodes.setTimeUnit("ms") # Time unit is ms.
1020 fieldOnNodes.setStartTime(4.22,2,-1)
1021 fieldOnNodes.setEndTime(6.44,4,-1)# fieldOnNodes is defined in interval [4.22 ms,6.44 ms]
1022 fieldOnNodes.setMesh(mesh)
1023 array=DataArrayDouble.New()
1024 array.alloc(fieldOnNodes.getMesh().getNumberOfNodes(),3) # Implicitely fieldOnNodes will be a 3 components field.
1025 array.fillWithValue(7.)
1026 fieldOnNodes.setArray(array)
1027 # fieldOnNodes is now usable
1029 # ! [PySnippetFieldDoubleBuild4_1]
1032 def testExampleDataArrayApplyFunc1(self):
1033 # ! [PySnippetDataArrayApplyFunc1_1]
1034 d=DataArrayDouble.New([1.,2.,11.,12.,21.,22.,31.,41.],4,2)
1035 self.assertRaises(InterpKernelException,d.applyFunc,"x*y")
1036 # ! [PySnippetDataArrayApplyFunc1_1]
1037 # ! [PySnippetDataArrayApplyFunc1_2]
1038 d=DataArrayDouble.New([1.,2.,11.,12.,21.,22.,31.,41.],4,2)
1039 d1=d.applyFunc("smth*smth")
1040 self.assertTrue(d1.isEqual(DataArrayDouble([1.,4.,121.,144.,441.,484.,961.,1681.],4,2),1e-12))
1041 # ! [PySnippetDataArrayApplyFunc1_2]
1042 # ! [PySnippetDataArrayApplyFunc1_3]
1043 d2=d.applyFunc("smth*IVec+2*smth*JVec")
1044 self.assertTrue(d2.isEqual(DataArrayDouble([1.,4.,11.,24.,21.,44.,31.,82.],4,2),1e-12))
1045 # ! [PySnippetDataArrayApplyFunc1_3]
1046 # ! [PySnippetDataArrayApplyFunc1_4]
1047 dd=DataArrayDouble.New([1.,4.,3.,11.,144.,13.,21.,484.,23.,31.,1024.,33.],4,3)
1048 # ! [PySnippetDataArrayApplyFunc1_4]
1049 # ! [PySnippetDataArrayApplyFunc1_5]
1050 dd1=dd.applyFunc(1,"f+sqrt(g)+h")
1051 self.assertTrue(dd1.isEqual(DataArrayDouble([6.,36.,66.,96.],4,1),1e-12))
1052 # ! [PySnippetDataArrayApplyFunc1_5]
1053 # ! [PySnippetDataArrayApplyFunc1_6]
1054 dd2=dd.applyFunc(1,"a+0.*b+c")
1055 self.assertTrue(dd2.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
1056 # ! [PySnippetDataArrayApplyFunc1_6]
1057 # ! [PySnippetDataArrayApplyFunc1_7]
1058 ddd=DataArrayDouble.New([1.,4.,3.,11.,144.,13.,21.,484.,23.,31.,1024.,33.],4,3)
1059 ddd.setInfoOnComponents(["Y [m]","AA [m/s]","GG [MW]"])
1060 # ! [PySnippetDataArrayApplyFunc1_7]
1061 # ! [PySnippetDataArrayApplyFunc1_8]
1062 ddd1=ddd.applyFunc2(1,"Y+GG")
1063 self.assertTrue(ddd1.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
1064 # ! [PySnippetDataArrayApplyFunc1_8]
1065 # ! [PySnippetDataArrayApplyFunc1_9]
1066 ddd1=ddd.applyFunc3(1,["X","Y","Z"],"X+Z")
1067 self.assertTrue(ddd1.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
1068 # ! [PySnippetDataArrayApplyFunc1_9]