1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2012 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 *
24 class MEDCouplingBasicsTest(unittest.TestCase):
25 def testExample_DataArrayInt_(self):
26 #! [PySnippet_DataArrayInt__1]
29 def testExample_DataArrayInt_getTuple(self):
30 #! [Snippet_DataArrayInt_getTuple_1]
31 dv=DataArrayInt.New();
35 print dv.getTuple( 1 )
36 #! [Snippet_DataArrayInt_getTuple_1]
37 #! [Snippet_DataArrayInt_getTuple_2]
40 #! [Snippet_DataArrayInt_getTuple_2]
43 def testExample_DataArrayInt_buildPermutationArr(self):
44 #! [PySnippet_DataArrayInt_buildPermutationArr_1]
46 a.setValues([4,5,6,7,8],5,1)
48 b.setValues([5,4,8,6,7],5,1)
49 c=a.buildPermutationArr(b)
50 #! [PySnippet_DataArrayInt_buildPermutationArr_1]
51 self.assertEqual([1,0,4,2,3],c.getValues())
54 def testExample_DataArrayInt_invertArrayO2N2N2O(self):
55 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
57 da=DataArrayInt.New();
58 da.setValues(arr1,6,1);
59 da2=da.invertArrayO2N2N2O(6);
60 expected1=[1,3,0,5,2,4]
62 self.assertEqual(expected1[i],da2.getIJ(i,0));
64 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
67 def testExample_DataArrayInt_invertArrayN2O2O2N(self):
68 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
70 da=DataArrayInt.New();
71 da.setValues(arr1,6,1);
72 da2=da.invertArrayN2O2O2N(7);
73 expected1=[1,3,0,5,2,4,-1]
75 self.assertEqual(expected1[i],da2.getIJ(i,0));
77 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
81 def testExample_DataArrayDouble_getIdsInRange(self):
82 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
83 da=DataArrayDouble.New()
86 da2 = da.getIdsInRange( 2.5, 6 )
87 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
90 def testExample_DataArrayDouble_setPartOfValues2(self):
91 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
92 da=DataArrayDouble.New()
95 dv=DataArrayDouble.New();
99 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
100 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
102 da[ [0,1,2], [1,3] ] = dv
103 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
104 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
107 da[ [0,2,3], [0,2,3,4,5,6]] = dv
108 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
111 def testExample_DataArrayInt_setPartOfValues2(self):
112 #! [Snippet_DataArrayInt_setPartOfValues2_1]
113 da=DataArrayInt.New()
116 dv=DataArrayInt.New();
120 #! [Snippet_DataArrayInt_setPartOfValues2_1]
121 #! [Snippet_DataArrayInt_setPartOfValues2_2]
123 da[ [0,1,2], [1,3] ] = dv
124 #! [Snippet_DataArrayInt_setPartOfValues2_2]
125 #! [Snippet_DataArrayInt_setPartOfValues2_3]
128 da[ [0,2,3], [0,2,3,4,5,6]] = dv
129 #! [Snippet_DataArrayInt_setPartOfValues2_3]
132 def testExample_DataArrayDouble_setPartOfValues3(self):
133 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
134 da=DataArrayDouble.New()
137 dv=DataArrayDouble.New();
141 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
142 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
144 da[ 0:3, [1,3] ] = dv
145 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
146 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
149 da[ 0:4:2, [0,2,3,4,5,6]] = dv
150 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
153 def testExample_DataArrayInt_setPartOfValues3(self):
154 #! [Snippet_DataArrayInt_setPartOfValues3_1]
155 da=DataArrayInt.New()
158 dv=DataArrayInt.New();
162 #! [Snippet_DataArrayInt_setPartOfValues3_1]
163 #! [Snippet_DataArrayInt_setPartOfValues3_2]
165 da[ 0:3, [1,3] ] = dv
166 #! [Snippet_DataArrayInt_setPartOfValues3_2]
167 #! [Snippet_DataArrayInt_setPartOfValues3_3]
170 da[ 0:4:2, [0,2,3,4,5,6]] = dv
171 #! [Snippet_DataArrayInt_setPartOfValues3_3]
174 def testExample_DataArrayDouble_setPartOfValues1(self):
175 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
176 da=DataArrayDouble.New()
178 da.setInfoOnComponents( ["v1","v2","v3","v4"])
180 dv=DataArrayDouble.New();
184 dv.setInfoOnComponents( ["a1","a2"])
185 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
186 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
188 da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
189 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
190 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
192 da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
193 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
194 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
196 da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
197 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
198 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
200 da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
201 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
202 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
205 da2[ 0:3:2, 1:4:2 ] = dv
206 self.assertTrue( da.isEqual( da2, 1e-20 ))
207 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
210 def testExample_DataArrayInt_setPartOfValues1(self):
211 #! [Snippet_DataArrayInt_setPartOfValues1_1]
212 da=DataArrayInt.New()
214 da.setInfoOnComponents( ["v1","v2","v3","v4"])
216 dv=DataArrayInt.New();
220 dv.setInfoOnComponents( ["a1","a2"])
221 #! [Snippet_DataArrayInt_setPartOfValues1_1]
222 #! [Snippet_DataArrayInt_setPartOfValues1_2]
224 da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
225 #! [Snippet_DataArrayInt_setPartOfValues1_2]
226 #! [Snippet_DataArrayInt_setPartOfValues1_3]
228 da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
229 #! [Snippet_DataArrayInt_setPartOfValues1_3]
230 #! [Snippet_DataArrayInt_setPartOfValues1_4]
232 da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
233 #! [Snippet_DataArrayInt_setPartOfValues1_4]
234 #! [Snippet_DataArrayInt_setPartOfValues1_5]
236 da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
237 #! [Snippet_DataArrayInt_setPartOfValues1_5]
238 #! [Snippet_DataArrayInt_setPartOfValues1_6]
241 da2[ 0:3:2, 1:4:2 ] = dv
242 self.assertTrue( da.isEqual( da2 ))
243 #! [Snippet_DataArrayInt_setPartOfValues1_6]
246 def testExample_DataArrayDouble_setPartOfValuesSimple1(self):
247 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
248 da=DataArrayDouble.New()
251 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
252 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
254 da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
255 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
256 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
258 da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
259 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
260 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
262 da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
263 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
264 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
266 da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
267 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
268 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
271 da2[ 0:3:2, 1:4:2 ] = dv
272 self.assertTrue( da.isEqual( da2, 1e-20 ))
273 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
276 def testExample_DataArrayInt_setPartOfValuesSimple1(self):
277 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
278 da=DataArrayInt.New()
281 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
282 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
284 da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
285 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
286 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
288 da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
289 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
290 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
292 da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
293 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
294 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
296 da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
297 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
298 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
301 da2[ 0:3:2, 1:4:2 ] = dv
302 self.assertTrue( da.isEqual( da2 ))
303 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
306 def testExample_DataArrayDouble_setPartOfValuesSimple2(self):
307 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
308 da=DataArrayDouble.New()
311 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
312 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
314 da[[1,2], [1,2]] = dv
315 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
316 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
318 da[[0,1,2,3], [1]] = dv
319 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
320 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
322 da[[1], [0,1,2,3]] = dv
323 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
324 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
326 da[[0,2], [1,3]] = dv
327 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
330 def testExample_DataArrayInt_setPartOfValuesSimple2(self):
331 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
332 da=DataArrayInt.New()
335 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
336 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
338 da[[1,2], [1,2]] = dv
339 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
340 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
342 da[[0,1,2,3], [1]] = dv
343 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
344 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
346 da[[1], [0,1,2,3]] = dv
347 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
348 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
350 da[[0,2], [1,3]] = dv
351 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
354 def testExample_DataArrayDouble_setPartOfValuesSimple3(self):
355 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
356 da=DataArrayDouble.New()
359 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
360 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
363 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
364 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
366 da[[0,1,2,3], 1:2] = dv
367 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
368 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
371 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
372 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
374 da[[0,2], 1:4:2] = dv
375 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
378 def testExample_DataArrayInt_setPartOfValuesSimple3(self):
379 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
380 da=DataArrayInt.New()
383 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
384 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
387 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
388 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
390 da[[0,1,2,3], 1:2] = dv
391 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
392 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
395 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
396 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
398 da[[0,2], 1:4:2] = dv
399 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
402 def testExample_DataArrayDouble_setSelectedComponents(self):
403 #! [Snippet_DataArrayDouble_setSelectedComponents1]
404 da=DataArrayDouble.New();
405 array1=[1.,2., 3.,4., 5.,6.]
406 da.setValues(array1,3,2)
407 da.setInfoOnComponents( ["a1","a2"])
408 #! [Snippet_DataArrayDouble_setSelectedComponents1]
409 #! [Snippet_DataArrayDouble_setSelectedComponents2]
410 dv=DataArrayDouble.New();
413 dv.setInfoOnComponents( ["v1","v2","v3","v4"])
415 dv.setSelectedComponents( da, [1,0] )
416 #! [Snippet_DataArrayDouble_setSelectedComponents2]
417 #! [Snippet_DataArrayDouble_setSelectedComponents3]
419 self.assertTrue( dv.isEqualWithoutConsideringStr( dv2, 1e-20 ))
420 #! [Snippet_DataArrayDouble_setSelectedComponents3]
423 def testExample_DataArrayInt_setSelectedComponents(self):
424 #! [Snippet_DataArrayInt_setSelectedComponents1]
425 da=DataArrayInt.New();
426 array1=[1,2, 3,4, 5,6]
427 da.setValues(array1,3,2)
428 da.setInfoOnComponents( ["a1","a2"])
429 #! [Snippet_DataArrayInt_setSelectedComponents1]
430 #! [Snippet_DataArrayInt_setSelectedComponents2]
431 dv=DataArrayInt.New();
434 dv.setInfoOnComponents( ["v1","v2","v3","v4"])
436 dv.setSelectedComponents( da, [1,0] )
437 #! [Snippet_DataArrayInt_setSelectedComponents2]
438 #! [Snippet_DataArrayInt_setSelectedComponents3]
440 self.assertTrue( dv.isEqualWithoutConsideringStr( dv2 ))
441 #! [Snippet_DataArrayInt_setSelectedComponents3]
444 def testExample_DataArrayDouble_getDifferentValues(self):
445 #! [Snippet_DataArrayDouble_getDifferentValues1]
446 da=DataArrayDouble.New();
447 array1=[2.3,1.2,1.3,2.3,2.301,0.8]
448 da.setValues(array1,6,1)
450 dv=da.getDifferentValues(2e-1);
451 expected2=[2.301,1.3,0.8]
452 self.assertEqual(3,dv.getNbOfElems());
454 self.assertAlmostEqual(expected2[i],dv.getIJ(i,0),14);
456 #! [Snippet_DataArrayDouble_getDifferentValues1]
459 def testExample_DataArrayDouble_findCommonTuples1(self):
460 #! [PySnippet_DataArrayDouble_findCommonTuples1]
461 da=DataArrayDouble.New();
462 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]
463 da.setValues(array2,6,2)
464 #! [PySnippet_DataArrayDouble_findCommonTuples1]
465 #! [PySnippet_DataArrayDouble_findCommonTuples2]
466 c,cI=da.findCommonTuples(1e-1);
467 expected3=[0,3,4,1,2]
469 self.assertEqual(expected3,c.getValues())
470 self.assertEqual(expected4,cI.getValues())
471 #! [PySnippet_DataArrayDouble_findCommonTuples2]
474 def testExampleDataArrayDoubleMeldWith(self):
475 #! [PySnippet_DataArrayDouble_Meld1_1]
476 da1=DataArrayDouble.New();
478 da2=DataArrayDouble.New();
481 da1.fillWithValue(7.);
483 da3=da2.applyFunc(3,"10*x*IVec+100*x*JVec+1000*x*KVec");
485 da1.setInfoOnComponent(0,"c0da1");
486 da1.setInfoOnComponent(1,"c1da1");
487 da3.setInfoOnComponent(0,"c0da3");
488 da3.setInfoOnComponent(1,"c1da3");
489 da3.setInfoOnComponent(2,"c2da3");
493 #! [PySnippet_DataArrayDouble_Meld1_1]
495 def testExampleDataArrayIntMeldWith(self):
496 #! [PySnippet_DataArrayInt_Meld1_1]
497 da1=DataArrayInt.New();
499 da2=DataArrayInt.New();
502 da1.fillWithValue(7);
505 da1.setInfoOnComponent(0,"c0da1");
506 da1.setInfoOnComponent(1,"c1da1");
507 da2.setInfoOnComponent(0,"c0da2");
510 #! [PySnippet_DataArrayInt_Meld1_1]
512 def testExampleDataArrayDoubleKeepSelectedComponents1(self):
513 #! [SnippeDataArrayDoubleKeepSelectedComponents1_1]
514 arr1=[1.,2.,3.,4., # tuple 0
515 11.,12.,13.,14., # tuple 1
516 21.,22.,23.,24., # ...
519 a1=DataArrayDouble.New()
520 a1.setValues(arr1,5,4)
521 a1.setInfoOnComponent(0,"a");
522 a1.setInfoOnComponent(1,"b");
523 a1.setInfoOnComponent(2,"c");
524 a1.setInfoOnComponent(3,"d");
525 #! [SnippeDataArrayDoubleKeepSelectedComponents1_1]
526 #! [SnippeDataArrayDoubleKeepSelectedComponents1_2]
528 a2=a1.keepSelectedComponents(arr2V)
529 #! [SnippeDataArrayDoubleKeepSelectedComponents1_2]
532 def testExampleDataArrayIntKeepSelectedComponents1(self):
533 #! [SnippeDataArrayIntKeepSelectedComponents1_1]
534 arr1=[1,2,3,4, # tuple 0
535 11,12,13,14, # tuple 1
539 a1=DataArrayInt.New()
540 a1.setValues(arr1,5,4)
541 a1.setInfoOnComponent(0,"a");
542 a1.setInfoOnComponent(1,"b");
543 a1.setInfoOnComponent(2,"c");
544 a1.setInfoOnComponent(3,"d");
545 #! [SnippeDataArrayIntKeepSelectedComponents1_1]
546 #! [SnippeDataArrayIntKeepSelectedComponents1_2]
548 a2=a1.keepSelectedComponents(arr2V)
549 #! [SnippeDataArrayIntKeepSelectedComponents1_2]
550 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
552 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
555 def testExampleFieldDoubleBuildSubPart1(self):
556 from MEDCouplingDataForTest import MEDCouplingDataForTest
557 #! [PySnippetFieldDoubleBuildSubPart1_1]
558 mesh1=MEDCouplingDataForTest.build2DTargetMesh_1()
559 f1=MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME)
562 array=DataArrayDouble.New()
563 arr1=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.]
564 array.setValues(arr1,mesh1.getNumberOfCells(),2)
566 # ! [PySnippetFieldDoubleBuildSubPart1_1]
567 # ! [PySnippetFieldDoubleBuildSubPart1_2]
569 f2=f1.buildSubPart(part1)
570 # ! [PySnippetFieldDoubleBuildSubPart1_2]
572 self.assertEqual(3,f2.getNumberOfTuples())
573 self.assertEqual(2,f2.getNumberOfComponents())
574 expected1=[5.,105.,4.,104.,7.,107.]
576 self.assertAlmostEqual(f2.getIJ(0,i),expected1[i],12)
578 self.assertEqual(3,f2.getMesh().getNumberOfCells())
579 self.assertEqual(6,f2.getMesh().getNumberOfNodes())
580 self.assertEqual(2,f2.getMesh().getSpaceDimension())
581 self.assertEqual(2,f2.getMesh().getMeshDimension())
583 self.assertEqual(13,m2C.getMeshLength())
584 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]
586 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
588 expected3=[3,2,3,1,3,0,2,1,4,4,5,3,2]
589 self.assertEqual(expected3,list(m2C.getNodalConnectivity().getValues()))
591 self.assertEqual(expected4,list(m2C.getNodalConnectivityIndex().getValues()))
592 # Test with field on nodes.
593 # ! [PySnippetFieldDoubleBuildSubPart1_3]
594 f1=MEDCouplingFieldDouble.New(ON_NODES,ONE_TIME)
597 array=DataArrayDouble.New()
598 arr2=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.,8.,108.,9.,109.,10.,110.,11.,111.]
599 array.setValues(arr2,mesh1.getNumberOfNodes(),2)
601 # ! [PySnippetFieldDoubleBuildSubPart1_3]
602 # ! [PySnippetFieldDoubleBuildSubPart1_4]
604 f2=f1.buildSubPart(part2)
605 # ! [PySnippetFieldDoubleBuildSubPart1_4]
606 self.assertEqual(4,f2.getNumberOfTuples())
607 self.assertEqual(2,f2.getNumberOfComponents())
608 expected5=[4.,104.,5.,105.,7.,107.,8.,108.]
610 self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
612 self.assertEqual(2,f2.getMesh().getNumberOfCells())
613 self.assertEqual(4,f2.getMesh().getNumberOfNodes())
614 self.assertEqual(2,f2.getMesh().getSpaceDimension())
615 self.assertEqual(2,f2.getMesh().getMeshDimension())
617 self.assertEqual(8,m2C.getMeshLength())
618 for i in xrange(8):#8 is not an error
619 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
621 self.assertEqual(expected3[:4],list(m2C.getNodalConnectivity().getValues())[4:])
622 self.assertEqual(expected3[4:8],list(m2C.getNodalConnectivity().getValues())[:4])
623 self.assertEqual(expected4[:3],list(m2C.getNodalConnectivityIndex().getValues()))
624 #idem previous because nodes of cell#4 are not fully present in part3
626 arrr=DataArrayInt.New()
627 arrr.setValues(part3,2,1)
628 f2=f1.buildSubPart(arrr)
629 self.assertEqual(4,f2.getNumberOfTuples())
630 self.assertEqual(2,f2.getNumberOfComponents())
632 self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
634 self.assertEqual(2,f2.getMesh().getNumberOfCells())
635 self.assertEqual(4,f2.getMesh().getNumberOfNodes())
636 self.assertEqual(2,f2.getMesh().getSpaceDimension())
637 self.assertEqual(2,f2.getMesh().getMeshDimension())
639 self.assertEqual(8,m2C.getMeshLength())
640 for i in xrange(8):#8 is not an error
641 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
643 self.assertEqual(expected3[:4],list(m2C.getNodalConnectivity().getValues())[4:8])
644 self.assertEqual(expected3[4:8],list(m2C.getNodalConnectivity().getValues())[:4])
645 self.assertEqual(expected4[:3],list(m2C.getNodalConnectivityIndex().getValues()))
647 f2=f1.buildSubPart(part4)
648 self.assertEqual(6,f2.getNumberOfTuples())
649 self.assertEqual(2,f2.getNumberOfComponents())
650 expected6=[4.,104.,5.,105.,7.,107.,8.,108.,10.,110.,11.,111.]
652 self.assertAlmostEqual(f2.getIJ(0,i),expected6[i],12)
654 self.assertEqual(3,f2.getMesh().getNumberOfCells())
655 self.assertEqual(6,f2.getMesh().getNumberOfNodes())
656 self.assertEqual(2,f2.getMesh().getSpaceDimension())
657 self.assertEqual(2,f2.getMesh().getMeshDimension())
659 self.assertEqual(13,m2C.getMeshLength())
661 self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
663 self.assertEqual(expected3[0:4],list(m2C.getNodalConnectivity().getValues())[4:8])
664 self.assertEqual(expected3[4:8],list(m2C.getNodalConnectivity().getValues())[0:4])
665 self.assertEqual(expected3[8:13],list(m2C.getNodalConnectivity().getValues())[8:13])
666 self.assertEqual(expected4,list(m2C.getNodalConnectivityIndex().getValues()))
669 def testExampleUMeshStdBuild1(self):
670 # ! [PySnippetUMeshStdBuild1_1]
671 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.,
672 0.7,0.2,0., -0.3,0.7,0., 0.2,0.7,0., 0.7,0.7,0. ]
673 nodalConnPerCell=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
674 # ! [PySnippetUMeshStdBuild1_1]
675 # ! [PySnippetUMeshStdBuild1_2]
676 mesh=MEDCouplingUMesh.New("My2DMesh",2)
677 # ! [PySnippetUMeshStdBuild1_2]
678 # ! [PySnippetUMeshStdBuild1_3]
679 mesh.allocateCells(5)#You can put more than 5 if you want but not less.
680 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[:4])
681 mesh.insertNextCell(NORM_TRI3,nodalConnPerCell[4:7])
682 mesh.insertNextCell(NORM_TRI3,nodalConnPerCell[7:10])
683 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[10:14])
684 mesh.insertNextCell(NORM_QUAD4,nodalConnPerCell[14:])
685 mesh.finishInsertingCells()
686 # ! [PySnippetUMeshStdBuild1_3]
687 # ! [PySnippetUMeshStdBuild1_4]
688 myCoords=DataArrayDouble.New(coords,9,3)#here myCoords are declared to have 3 components, mesh will deduce that its spaceDim==3.
689 mesh.setCoords(myCoords)#myCorrds contains 9 tuples, that is to say mesh contains 9 nodes.
690 # ! [PySnippetUMeshStdBuild1_4]
691 # ! [PySnippetUMeshStdBuild1_5]
692 # ! [PySnippetUMeshStdBuild1_5]
693 mesh.checkCoherency()
696 def testExampleCMeshStdBuild1(self):
697 # ! [PySnippetCMeshStdBuild1_1]
698 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] # 9 values along X
699 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] # 7 values along Y
700 arrX=DataArrayDouble.New(XCoords)
701 arrX.setInfoOnComponent(0,"X [m]")
702 arrY=DataArrayDouble.New(YCoords)
703 arrY.setInfoOnComponent(0,"Y [m]")
704 # ! [PySnippetCMeshStdBuild1_1]
705 # ! [PySnippetCMeshStdBuild1_2]
706 mesh=MEDCouplingCMesh.New("My2D_CMesh")
707 mesh.setCoords(arrX,arrY)
708 # ! [PySnippetCMeshStdBuild1_2]
709 # ! [PySnippetCMeshStdBuild1_3]
710 self.assertEqual(8*6,mesh.getNumberOfCells())
711 self.assertEqual(9*7,mesh.getNumberOfNodes())
712 self.assertEqual(2,mesh.getSpaceDimension())
713 self.assertEqual(2,mesh.getMeshDimension())
714 # ! [PySnippetCMeshStdBuild1_3]
715 mesh=MEDCouplingCMesh.New("My2D_CMesh")
716 # ! [PySnippetCMeshStdBuild1_2bis]
717 mesh.setCoordsAt(0,arrX)
718 mesh.setCoordsAt(1,arrY)
719 # ! [PySnippetCMeshStdBuild1_2bis]
720 self.assertEqual(8*6,mesh.getNumberOfCells())
721 self.assertEqual(9*7,mesh.getNumberOfNodes())
722 self.assertEqual(2,mesh.getSpaceDimension())
723 self.assertEqual(2,mesh.getMeshDimension())
726 def testExampleUMeshAdvBuild1(self):
727 # ! [PySnippetUMeshAdvBuild1_1]
728 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.,
729 0.7,0.2,0., -0.3,0.7,0., 0.2,0.7,0., 0.7,0.7,0. ]
730 nodalConnPerCell=[4,0,3,4,1, 3,1,4,2, 3,4,5,2, 4,6,7,4,3, 4,7,8,5,4]
731 nodalConnPerCellIndex=[0,5,9,13,18,23]
732 # ! [PySnippetUMeshAdvBuild1_1]
733 # ! [PySnippetUMeshAdvBuild1_2]
734 mesh=MEDCouplingUMesh.New("My2DMesh",2)
735 # ! [PySnippetUMeshAdvBuild1_2]
736 # ! [PySnippetUMeshAdvBuild1_3]
737 nodalConn=DataArrayInt.New(nodalConnPerCell,23,1)
738 nodalConnI=DataArrayInt.New(nodalConnPerCellIndex,6,1)
739 mesh.setConnectivity(nodalConn,nodalConnI,True)
740 # ! [PySnippetUMeshAdvBuild1_3]
741 # ! [PySnippetUMeshAdvBuild1_4]
742 myCoords=DataArrayDouble.New(coords,9,3)#here myCoords are declared to have 3 components, mesh will deduce that its spaceDim==3.
743 mesh.setCoords(myCoords)#myCorrds contains 9 tuples, that is to say mesh contains 9 nodes.
744 # ! [PySnippetUMeshAdvBuild1_4]
745 # ! [PySnippetUMeshAdvBuild1_5]
746 # ! [PySnippetUMeshAdvBuild1_5]
747 mesh.checkCoherency()
750 def testExampleDataArrayBuild1(self):
751 # ! [PySnippetDataArrayBuild1_0]
752 dataDouble=[0.,10.,20.,1.,11.,21.,2.,12.,22.,3.,13.,23.,4.,14.,24.]
753 # ! [PySnippetDataArrayBuild1_0]
754 # ! [PySnippetDataArrayBuild1_1]
755 arrayDouble=DataArrayDouble.New()
756 arrayDouble.setValues(dataDouble,5,3)# 5 tuples containing each 3 components
757 # ! [PySnippetDataArrayBuild1_1]
758 # ! [PySnippetDataArrayBuild1_1bis]
759 arrayDouble=DataArrayDouble.New(dataDouble,5,3)
760 # ! [PySnippetDataArrayBuild1_1bis]
761 # ! [PySnippetDataArrayBuild1_2]
762 dataInt=[0, 10, 20, 1, 11, 21, 2, 12, 22, 3, 13, 23, 4, 14, 24]
763 # ! [PySnippetDataArrayBuild1_2]
764 # ! [PySnippetDataArrayBuild1_3]
765 arrayInt=DataArrayInt.New()
766 arrayInt.setValues(dataInt,5,3)# 5 tuples containing each 3 components
767 # ! [PySnippetDataArrayBuild1_3]
768 # ! [PySnippetDataArrayBuild1_3bis]
769 arrayInt=DataArrayInt.New(dataInt,5,3)
770 # ! [PySnippetDataArrayBuild1_3bis]
773 def testExampleFieldDoubleBuild1(self):
774 XCoords=[-0.3,0.07,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
775 YCoords=[0.07,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
776 mesh=MEDCouplingCMesh.New("My2D_CMesh")
777 mesh.setCoords(arrX,arrY)
778 # ! [PySnippetFieldDoubleBuild1_1]
779 fieldOnCells=MEDCouplingFieldDouble.New(ON_CELLS,NO_TIME)
780 fieldOnCells.setName("MyTensorFieldOnCellNoTime")
781 fieldOnCells.setMesh(mesh)
782 array=DataArrayDouble.New()
783 array.alloc(fieldOnCells.getMesh().getNumberOfCells(),9) # Implicitely fieldOnCells will be a 9 components field.
784 array.fillWithValue(7.)
785 fieldOnCells.setArray(array)
786 # fieldOnCells is now usable
788 # ! [PySnippetFieldDoubleBuild1_1]
789 # ! [PySnippetFieldDoubleBuild1_2]
790 f1=mesh.fillFromAnalytic(ON_CELLS,1,"x*x+y*y*3+2.*x") # f1 is scalar
791 f2=mesh.fillFromAnalytic(ON_CELLS,1,"cos(x+y/x)") # f2 is scalar too
792 f2bis=mesh.fillFromAnalytic(ON_CELLS,2,"x*x*IVec+3*y*JVec") # f2bis is a vectors field
795 f2bis.applyFunc(1,"sqrt(x*x+y*y)") # f2bis becomes scalar
796 f5=f2bis*f4 # f5 scalar
798 res=f4.getValueOn(pos1) # f4 is scalar so the returned value is of size 1.
800 # ! [PySnippetFieldDoubleBuild1_2]
801 # ! [PySnippetFieldDoubleBuild1_3]
802 # ! [PySnippetFieldDoubleBuild1_3]
805 def testExampleFieldDoubleBuild2(self):
806 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
807 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
808 mesh=MEDCouplingCMesh.New("My2D_CMesh")
809 mesh.setCoords(arrX,arrY)
810 # ! [PySnippetFieldDoubleBuild2_1]
811 fieldOnNodes=MEDCouplingFieldDouble.New(ON_NODES,NO_TIME)
812 fieldOnNodes.setName("MyScalarFieldOnNodeNoTime")
813 fieldOnNodes.setMesh(mesh)
814 array=DataArrayDouble.New()
815 array.alloc(fieldOnNodes.getMesh().getNumberOfNodes(),1) # Implicitely fieldOnNodes will be a 1 component field.
816 array.fillWithValue(7.)
817 fieldOnNodes.setArray(array)
818 # fieldOnNodes is now usable
820 # ! [PySnippetFieldDoubleBuild2_1]
823 def testExampleFieldDoubleBuild3(self):
824 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
825 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
826 mesh=MEDCouplingCMesh.New("My2D_CMesh")
827 mesh.setCoords(arrX,arrY)
828 # ! [PySnippetFieldDoubleBuild3_1]
829 fieldOnCells=MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME)
830 fieldOnCells.setName("MyTensorFieldOnCellNoTime")
831 fieldOnCells.setTimeUnit("ms") # Time unit is ms.
832 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
833 fieldOnCells.setMesh(mesh)
834 array=DataArrayDouble.New()
835 array.alloc(fieldOnCells.getMesh().getNumberOfCells(),2) # Implicitely fieldOnCells will be a 2 components field.
836 array.fillWithValue(7.)
837 fieldOnCells.setArray(array)
838 # fieldOnCells is now usable
840 # ! [PySnippetFieldDoubleBuild3_1]
843 def testExampleFieldDoubleBuild4(self):
844 XCoords=[-0.3,0.,0.1,0.3,0.45,0.47,0.49,1.,1.22] ; arrX=DataArrayDouble.New(XCoords)
845 YCoords=[0.,0.1,0.37,0.45,0.47,0.49,1.007] ; arrY=DataArrayDouble.New(YCoords)
846 mesh=MEDCouplingCMesh.New("My2D_CMesh")
847 mesh.setCoords(arrX,arrY)
848 # ! [PySnippetFieldDoubleBuild4_1]
849 fieldOnNodes=MEDCouplingFieldDouble.New(ON_NODES,CONST_ON_TIME_INTERVAL)
850 fieldOnNodes.setName("MyVecFieldOnNodeWithConstTime")
851 fieldOnNodes.setTimeUnit("ms") # Time unit is ms.
852 fieldOnNodes.setStartTime(4.22,2,-1)
853 fieldOnNodes.setEndTime(6.44,4,-1)# fieldOnNodes is defined in interval [4.22 ms,6.44 ms]
854 fieldOnNodes.setMesh(mesh)
855 array=DataArrayDouble.New()
856 array.alloc(fieldOnNodes.getMesh().getNumberOfNodes(),3) # Implicitely fieldOnNodes will be a 3 components field.
857 array.fillWithValue(7.)
858 fieldOnNodes.setArray(array)
859 # fieldOnNodes is now usable
861 # ! [PySnippetFieldDoubleBuild4_1]
864 def testExampleDataArrayApplyFunc1(self):
865 # ! [PySnippetDataArrayApplyFunc1_1]
866 d=DataArrayDouble.New([1.,2.,11.,12.,21.,22.,31.,41.],4,2)
867 self.assertRaises(InterpKernelException,d.applyFunc,"x*y")
868 # ! [PySnippetDataArrayApplyFunc1_1]
869 # ! [PySnippetDataArrayApplyFunc1_2]
870 d=DataArrayDouble.New([1.,2.,11.,12.,21.,22.,31.,41.],4,2)
871 d1=d.applyFunc("smth*smth")
872 self.assertTrue(d1.isEqual(DataArrayDouble([1.,4.,121.,144.,441.,484.,961.,1681.],4,2),1e-12))
873 # ! [PySnippetDataArrayApplyFunc1_2]
874 # ! [PySnippetDataArrayApplyFunc1_3]
875 d2=d.applyFunc("smth*IVec+2*smth*JVec")
876 self.assertTrue(d2.isEqual(DataArrayDouble([1.,4.,11.,24.,21.,44.,31.,82.],4,2),1e-12))
877 # ! [PySnippetDataArrayApplyFunc1_3]
878 # ! [PySnippetDataArrayApplyFunc1_4]
879 dd=DataArrayDouble.New([1.,4.,3.,11.,144.,13.,21.,484.,23.,31.,1024.,33.],4,3)
880 # ! [PySnippetDataArrayApplyFunc1_4]
881 # ! [PySnippetDataArrayApplyFunc1_5]
882 dd1=dd.applyFunc(1,"f+sqrt(g)+h")
883 self.assertTrue(dd1.isEqual(DataArrayDouble([6.,36.,66.,96.],4,1),1e-12))
884 # ! [PySnippetDataArrayApplyFunc1_5]
885 # ! [PySnippetDataArrayApplyFunc1_6]
886 dd2=dd.applyFunc(1,"a+0.*b+c")
887 self.assertTrue(dd2.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
888 # ! [PySnippetDataArrayApplyFunc1_6]
889 # ! [PySnippetDataArrayApplyFunc1_7]
890 ddd=DataArrayDouble.New([1.,4.,3.,11.,144.,13.,21.,484.,23.,31.,1024.,33.],4,3)
891 ddd.setInfoOnComponents(["Y [m]","AA [m/s]","GG [MW]"])
892 # ! [PySnippetDataArrayApplyFunc1_7]
893 # ! [PySnippetDataArrayApplyFunc1_8]
894 ddd1=ddd.applyFunc2(1,"Y+GG")
895 self.assertTrue(ddd1.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
896 # ! [PySnippetDataArrayApplyFunc1_8]
897 # ! [PySnippetDataArrayApplyFunc1_9]
898 ddd1=ddd.applyFunc3(1,["X","Y","Z"],"X+Z")
899 self.assertTrue(ddd1.isEqual(DataArrayDouble([4.,24.,44.,64.],4,1),1e-12))
900 # ! [PySnippetDataArrayApplyFunc1_9]