Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / MEDCoupling_Swig / MEDCouplingExamplesTest.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013  CEA/DEN, EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 from MEDCoupling import *
22 import unittest
23 from math import pi
24
25 class MEDCouplingBasicsTest(unittest.TestCase):
26     def testExample_MEDCouplingPointSet_(self):
27         #! [PySnippet_MEDCouplingPointSet__1]
28         pass
29         # mesh.allocateCells(1);
30         # mesh.insertNextCell(NORM_QUAD4,4,range(4));
31         # mesh.finishInsertingCells();
32
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]
43         center = [0.,0.]
44         factor = 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]
52         pass
53
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]
64         vector = [1.,1.]
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]
72         pass
73
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]
83         center = [0.,0.]
84         mesh.rotate(center,-pi/2)
85         #! [PySnippet_MEDCouplingPointSet_rotate_2]
86         #! [PySnippet_MEDCouplingPointSet_rotate_3]
87         mesh.changeSpaceDimension(3)
88         center = [0.,0.,0.]
89         vector = [0.,0.,1.]
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]
98         pass
99
100     def testExample_MEDCouplingPointSet_getBoundingBox(self):
101         #! [PySnippet_MEDCouplingPointSet_getBoundingBox_1]
102         cc=[0.0, 0.1, 0.2, # 3D coordinates of 2 nodes
103             2.0, 2.1, 2.2]
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!
112                         ( cc[1], cc[4] ),
113                         ( cc[2], cc[5] )]
114         #! [PySnippet_MEDCouplingPointSet_getBoundingBox_2]
115
116     def testExample_MEDCouplingPointSet_getNodeIdsNearPoint(self):
117         #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoint_1]
118         # 2D coordinates of 5 nodes
119         coords=[0.3,-0.301, # 0
120                 0.2,-0.3,   # 1
121                 0.3,-0.302, # 2
122                 1.1,0.0,    # 3
123                 0.3,-0.303];# 4
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]
134         pass
135
136     def testExample_MEDCouplingPointSet_getNodeIdsNearPoints(self):
137         #! [PySnippet_MEDCouplingPointSet_getNodeIdsNearPoints_1]
138         # 2D coordinates of 7 nodes
139         coords=[0.3,-0.301, # 0
140                 0.2,-0.3,   # 1
141                 0.3,-0.302, # 2
142                 1.1,0.0,    # 3
143                 1.1,0.0,    # 4
144                 1.1,0.002,  # 5
145                 0.3,-0.303];# 6
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
153                 0.0, 0.0,
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]
159         pass
160
161     def testExample_MEDCouplingPointSet_findCommonNodes(self):
162         #! [PySnippet_MEDCouplingPointSet_findCommonNodes_1]
163         coords=[0.3,-0.301, # 0
164                 0.2,-0.3,   # 1
165                 0.3,-0.302, # 2
166                 1.1,0.0,    # 3
167                 1.1,0.0,    # 4
168                 0.3,-0.303];# 5
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]
180         pass
181
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]
195         pass
196
197     def testExample_DataArrayInt_getTuple(self):
198 #! [Snippet_DataArrayInt_getTuple_1]
199         dv=DataArrayInt.New();
200         dv.alloc( 6, 1 )
201         dv.iota(7)
202         dv.rearrange( 2 )
203         print dv.getTuple( 1 )
204 #! [Snippet_DataArrayInt_getTuple_1]
205 #! [Snippet_DataArrayInt_getTuple_2]
206         for tpl in dv:
207             print tpl
208 #! [Snippet_DataArrayInt_getTuple_2]
209         pass
210
211     def testExample_DataArrayInt_buildPermutationArr(self):
212 #! [PySnippet_DataArrayInt_buildPermutationArr_1]
213         a=DataArrayInt.New()
214         a.setValues([4,5,6,7,8],5,1)
215         b=DataArrayInt.New()
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())
220         pass
221
222     def testExample_DataArrayInt_invertArrayO2N2N2O(self):
223 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
224         arr1=[2,0,4,1,5,3]
225         da=DataArrayInt.New();
226         da.setValues(arr1,6,1);
227         da2=da.invertArrayO2N2N2O(6);
228         expected1=[1,3,0,5,2,4]
229         for i in xrange(6):
230             self.assertEqual(expected1[i],da2.getIJ(i,0));
231             pass
232 #! [PySnippet_DataArrayInt_invertArrayO2N2N2O_1]
233         pass
234
235     def testExample_DataArrayInt_invertArrayN2O2O2N(self):
236 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
237         arr1=[2,0,4,1,5,3]
238         da=DataArrayInt.New();
239         da.setValues(arr1,6,1);
240         da2=da.invertArrayN2O2O2N(7);
241         expected1=[1,3,0,5,2,4,-1]
242         for i in xrange(6):
243             self.assertEqual(expected1[i],da2.getIJ(i,0));
244             pass
245 #! [PySnippet_DataArrayInt_invertArrayN2O2O2N_1]
246         pass
247
248
249     def testExample_DataArrayDouble_getIdsInRange(self):
250 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
251         da=DataArrayDouble.New()
252         da.alloc( 10, 1 )
253         da[ :, :] = range(10)
254         da2 = da.getIdsInRange( 2.5, 6 )
255 #! [PySnippet_DataArrayDouble_getIdsInRange_1]
256         pass
257
258     def testExample_DataArrayDouble_setPartOfValues2(self):
259 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
260         da=DataArrayDouble.New()
261         da.alloc( 4, 7 )
262         #
263         dv=DataArrayDouble.New();
264         dv.alloc( 6, 1 )
265         dv.iota(7)
266         dv.rearrange( 2 )
267 #! [Snippet_DataArrayDouble_setPartOfValues2_1]
268 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
269         da.fillWithZero()
270         da[ [0,1,2], [1,3] ] = dv
271 #! [Snippet_DataArrayDouble_setPartOfValues2_2]
272 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
273         da.fillWithZero()
274         dv.rearrange( 6 )
275         da[ [0,2,3], [0,2,3,4,5,6]] = dv
276 #! [Snippet_DataArrayDouble_setPartOfValues2_3]
277         pass
278
279     def testExample_DataArrayInt_setPartOfValues2(self):
280 #! [Snippet_DataArrayInt_setPartOfValues2_1]
281         da=DataArrayInt.New()
282         da.alloc( 4, 7 )
283         #
284         dv=DataArrayInt.New();
285         dv.alloc( 6, 1 )
286         dv.iota(7)
287         dv.rearrange( 2 )
288 #! [Snippet_DataArrayInt_setPartOfValues2_1]
289 #! [Snippet_DataArrayInt_setPartOfValues2_2]
290         da.fillWithZero()
291         da[ [0,1,2], [1,3] ] = dv
292 #! [Snippet_DataArrayInt_setPartOfValues2_2]
293 #! [Snippet_DataArrayInt_setPartOfValues2_3]
294         da.fillWithZero()
295         dv.rearrange( 6 )
296         da[ [0,2,3], [0,2,3,4,5,6]] = dv
297 #! [Snippet_DataArrayInt_setPartOfValues2_3]
298         pass
299
300     def testExample_DataArrayDouble_setPartOfValues3(self):
301 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
302         da=DataArrayDouble.New()
303         da.alloc( 4, 7 )
304         #
305         dv=DataArrayDouble.New();
306         dv.alloc( 6, 1 )
307         dv.iota(7)
308         dv.rearrange( 2 )
309 #! [Snippet_DataArrayDouble_setPartOfValues3_1]
310 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
311         da.fillWithZero()
312         da[ 0:3, [1,3] ] = dv
313 #! [Snippet_DataArrayDouble_setPartOfValues3_2]
314 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
315         da.fillWithZero()
316         dv.rearrange( 6 )
317         da[ 0:4:2, [0,2,3,4,5,6]] = dv
318 #! [Snippet_DataArrayDouble_setPartOfValues3_3]
319         pass
320
321     def testExample_DataArrayInt_setPartOfValues3(self):
322 #! [Snippet_DataArrayInt_setPartOfValues3_1]
323         da=DataArrayInt.New()
324         da.alloc( 4, 7 )
325         #
326         dv=DataArrayInt.New();
327         dv.alloc( 6, 1 )
328         dv.iota(7)
329         dv.rearrange( 2 )
330 #! [Snippet_DataArrayInt_setPartOfValues3_1]
331 #! [Snippet_DataArrayInt_setPartOfValues3_2]
332         da.fillWithZero()
333         da[ 0:3, [1,3] ] = dv
334 #! [Snippet_DataArrayInt_setPartOfValues3_2]
335 #! [Snippet_DataArrayInt_setPartOfValues3_3]
336         da.fillWithZero()
337         dv.rearrange( 6 )
338         da[ 0:4:2, [0,2,3,4,5,6]] = dv
339 #! [Snippet_DataArrayInt_setPartOfValues3_3]
340         pass
341
342     def testExample_DataArrayDouble_setPartOfValues1(self):
343 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
344         da=DataArrayDouble.New()
345         da.alloc( 4, 4 )
346         da.setInfoOnComponents( ["v1","v2","v3","v4"])
347         #
348         dv=DataArrayDouble.New();
349         dv.alloc( 4, 1 )
350         dv.iota(7)
351         dv.rearrange( 2 )
352         dv.setInfoOnComponents( ["a1","a2"])
353 #! [Snippet_DataArrayDouble_setPartOfValues1_1]
354 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
355         da.fillWithZero()
356         da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
357 #! [Snippet_DataArrayDouble_setPartOfValues1_2]
358 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
359         da.fillWithZero()
360         da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
361 #! [Snippet_DataArrayDouble_setPartOfValues1_3]
362 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
363         da.fillWithZero()
364         da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
365 #! [Snippet_DataArrayDouble_setPartOfValues1_4]
366 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
367         da.fillWithZero()
368         da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
369 #! [Snippet_DataArrayDouble_setPartOfValues1_5]
370 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
371         da2 = da.deepCpy()
372         da2.fillWithZero()
373         da2[ 0:3:2, 1:4:2 ] = dv
374         self.assertTrue( da.isEqual( da2, 1e-20 ))
375 #! [Snippet_DataArrayDouble_setPartOfValues1_6]
376         pass
377
378     def testExample_DataArrayInt_setPartOfValues1(self):
379 #! [Snippet_DataArrayInt_setPartOfValues1_1]
380         da=DataArrayInt.New()
381         da.alloc( 4, 4 )
382         da.setInfoOnComponents( ["v1","v2","v3","v4"])
383         #
384         dv=DataArrayInt.New();
385         dv.alloc( 4, 1 )
386         dv.iota(7)
387         dv.rearrange( 2 )
388         dv.setInfoOnComponents( ["a1","a2"])
389 #! [Snippet_DataArrayInt_setPartOfValues1_1]
390 #! [Snippet_DataArrayInt_setPartOfValues1_2]
391         da.fillWithZero()
392         da.setPartOfValues1( dv, 1,3,1, 1,3,1, True )
393 #! [Snippet_DataArrayInt_setPartOfValues1_2]
394 #! [Snippet_DataArrayInt_setPartOfValues1_3]
395         da.fillWithZero()
396         da.setPartOfValues1( dv, 0,4,1, 1,2,1, False )
397 #! [Snippet_DataArrayInt_setPartOfValues1_3]
398 #! [Snippet_DataArrayInt_setPartOfValues1_4]
399         da.fillWithZero()
400         da.setPartOfValues1( dv, 1,2,1, 0,4,1, False )
401 #! [Snippet_DataArrayInt_setPartOfValues1_4]
402 #! [Snippet_DataArrayInt_setPartOfValues1_5]
403         da.fillWithZero()
404         da.setPartOfValues1( dv, 0,3,2, 1,4,2, True )
405 #! [Snippet_DataArrayInt_setPartOfValues1_5]
406 #! [Snippet_DataArrayInt_setPartOfValues1_6]
407         da2 = da.deepCpy()
408         da2.fillWithZero()
409         da2[ 0:3:2, 1:4:2 ] = dv
410         self.assertTrue( da.isEqual( da2 ))
411 #! [Snippet_DataArrayInt_setPartOfValues1_6]
412         pass
413
414     def testExample_DataArrayDouble_setPartOfValuesSimple1(self):
415 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
416         da=DataArrayDouble.New()
417         da.alloc( 4, 4 )
418         dv = 7
419 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_1]
420 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
421         da.fillWithZero()
422         da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
423 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_2]
424 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
425         da.fillWithZero()
426         da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
427 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_3]
428 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
429         da.fillWithZero()
430         da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
431 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_4]
432 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
433         da.fillWithZero()
434         da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
435 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_5]
436 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
437         da2 = da.deepCpy()
438         da2.fillWithZero()
439         da2[ 0:3:2, 1:4:2 ] = dv
440         self.assertTrue( da.isEqual( da2, 1e-20 ))
441 #! [Snippet_DataArrayDouble_setPartOfValuesSimple1_6]
442         pass
443
444     def testExample_DataArrayInt_setPartOfValuesSimple1(self):
445 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
446         da=DataArrayInt.New()
447         da.alloc( 4, 4 )
448         dv = 7
449 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_1]
450 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
451         da.fillWithZero()
452         da.setPartOfValuesSimple1( dv, 1,3,1, 1,3,1 )
453 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_2]
454 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
455         da.fillWithZero()
456         da.setPartOfValuesSimple1( dv, 0,4,1, 1,2,1 )
457 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_3]
458 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
459         da.fillWithZero()
460         da.setPartOfValuesSimple1( dv, 1,2,1, 0,4,1 )
461 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_4]
462 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
463         da.fillWithZero()
464         da.setPartOfValuesSimple1( dv, 0,3,2, 1,4,2 )
465 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_5]
466 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
467         da2 = da.deepCpy()
468         da2.fillWithZero()
469         da2[ 0:3:2, 1:4:2 ] = dv
470         self.assertTrue( da.isEqual( da2 ))
471 #! [Snippet_DataArrayInt_setPartOfValuesSimple1_6]
472         pass
473
474     def testExample_DataArrayDouble_setPartOfValuesSimple2(self):
475 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
476         da=DataArrayDouble.New()
477         da.alloc( 4, 4 )
478         dv = 7
479 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_1]
480 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
481         da.fillWithZero()
482         da[[1,2], [1,2]] = dv
483 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_2]
484 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
485         da.fillWithZero()
486         da[[0,1,2,3], [1]] = dv
487 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_3]
488 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
489         da.fillWithZero()
490         da[[1], [0,1,2,3]] = dv
491 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_4]
492 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
493         da.fillWithZero()
494         da[[0,2], [1,3]] = dv
495 #! [Snippet_DataArrayDouble_setPartOfValuesSimple2_5]
496         pass
497
498     def testExample_DataArrayInt_setPartOfValuesSimple2(self):
499 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
500         da=DataArrayInt.New()
501         da.alloc( 4, 4 )
502         dv = 7
503 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_1]
504 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
505         da.fillWithZero()
506         da[[1,2], [1,2]] = dv
507 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_2]
508 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
509         da.fillWithZero()
510         da[[0,1,2,3], [1]] = dv
511 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_3]
512 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
513         da.fillWithZero()
514         da[[1], [0,1,2,3]] = dv
515 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_4]
516 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
517         da.fillWithZero()
518         da[[0,2], [1,3]] = dv
519 #! [Snippet_DataArrayInt_setPartOfValuesSimple2_5]
520         pass
521
522     def testExample_DataArrayDouble_setPartOfValuesSimple3(self):
523 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
524         da=DataArrayDouble.New()
525         da.alloc( 4, 4 )
526         dv = 7
527 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_1]
528 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
529         da.fillWithZero()
530         da[[1,2], 1:3] = dv
531 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_2]
532 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
533         da.fillWithZero()
534         da[[0,1,2,3], 1:2] = dv
535 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_3]
536 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
537         da.fillWithZero()
538         da[[1], 0:4] = dv
539 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_4]
540 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
541         da.fillWithZero()
542         da[[0,2], 1:4:2] = dv
543 #! [Snippet_DataArrayDouble_setPartOfValuesSimple3_5]
544         pass
545
546     def testExample_DataArrayInt_setPartOfValuesSimple3(self):
547 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
548         da=DataArrayInt.New()
549         da.alloc( 4, 4 )
550         dv = 7
551 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_1]
552 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
553         da.fillWithZero()
554         da[[1,2], 1:3] = dv
555 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_2]
556 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
557         da.fillWithZero()
558         da[[0,1,2,3], 1:2] = dv
559 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_3]
560 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
561         da.fillWithZero()
562         da[[1], 0:4] = dv
563 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_4]
564 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
565         da.fillWithZero()
566         da[[0,2], 1:4:2] = dv
567 #! [Snippet_DataArrayInt_setPartOfValuesSimple3_5]
568         pass
569
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();
579         dv.alloc( 4, 4 )
580         dv.fillWithZero()
581         dv.setInfoOnComponents( ["v1","v2","v3","v4"])
582         dv2 = dv.deepCpy()
583         dv.setSelectedComponents( da, [1,0] )
584 #! [Snippet_DataArrayDouble_setSelectedComponents2]
585 #! [Snippet_DataArrayDouble_setSelectedComponents3]
586         dv2[:3,[1,0]] = da
587         self.assertTrue( dv.isEqualWithoutConsideringStr( dv2, 1e-20 ))
588 #! [Snippet_DataArrayDouble_setSelectedComponents3]
589         pass
590
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();
600         dv.alloc( 4, 4 )
601         dv.fillWithZero()
602         dv.setInfoOnComponents( ["v1","v2","v3","v4"])
603         dv2 = dv.deepCpy()
604         dv.setSelectedComponents( da, [1,0] )
605 #! [Snippet_DataArrayInt_setSelectedComponents2]
606 #! [Snippet_DataArrayInt_setSelectedComponents3]
607         dv2[:3,[1,0]] = da
608         self.assertTrue( dv.isEqualWithoutConsideringStr( dv2 ))
609 #! [Snippet_DataArrayInt_setSelectedComponents3]
610         pass
611
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)
617         #
618         dv=da.getDifferentValues(2e-1);
619         expected2=[2.301,1.3,0.8]
620         self.assertEqual(3,dv.getNbOfElems());
621         for i in xrange(3):
622             self.assertAlmostEqual(expected2[i],dv.getIJ(i,0),14);
623             pass
624 #! [Snippet_DataArrayDouble_getDifferentValues1]
625         pass
626
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]
636         expected4=[0,3,5]
637         self.assertEqual(expected3,c.getValues())
638         self.assertEqual(expected4,cI.getValues())
639 #! [PySnippet_DataArrayDouble_findCommonTuples2]
640         pass
641
642     def testExampleDataArrayDoubleMeldWith(self):
643 #! [PySnippet_DataArrayDouble_Meld1_1]
644         da1=DataArrayDouble.New();
645         da1.alloc(7,2);
646         da2=DataArrayDouble.New();
647         da2.alloc(7,1);
648         #
649         da1.fillWithValue(7.);
650         da2.iota(0.);
651         da3=da2.applyFunc(3,"10*x*IVec+100*x*JVec+1000*x*KVec");
652         #
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");
658         #
659         da1C=da1.deepCpy();
660         da1.meldWith(da3);
661 #! [PySnippet_DataArrayDouble_Meld1_1]
662
663     def testExampleDataArrayIntMeldWith(self):
664 #! [PySnippet_DataArrayInt_Meld1_1]
665         da1=DataArrayInt.New();
666         da1.alloc(7,2);
667         da2=DataArrayInt.New();
668         da2.alloc(7,1);
669         #
670         da1.fillWithValue(7);
671         da2.iota(0);
672         #
673         da1.setInfoOnComponent(0,"c0da1");
674         da1.setInfoOnComponent(1,"c1da1");
675         da2.setInfoOnComponent(0,"c0da2");
676         #
677         da1.meldWith(da2);
678 #! [PySnippet_DataArrayInt_Meld1_1]
679
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., # ...
685               31.,32.,33.,34.,
686               41.,42.,43.,44.]
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]
695         arr2V=[1,2,1,2,0,0]
696         a2=a1.keepSelectedComponents(arr2V)
697 #! [SnippeDataArrayDoubleKeepSelectedComponents1_2]
698         pass
699
700     def testExampleDataArrayIntKeepSelectedComponents1(self):
701 #! [SnippeDataArrayIntKeepSelectedComponents1_1]
702         arr1=[1,2,3,4,     # tuple 0
703               11,12,13,14, # tuple 1
704               21,22,23,24, # 
705               31,32,33,34,
706               41,42,43,44]
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]
715         arr2V=[1,2,1,2,0,0]
716         a2=a1.keepSelectedComponents(arr2V)
717 #! [SnippeDataArrayIntKeepSelectedComponents1_2]
718 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
719         a3=a1[:,arr2V ]
720 #! [SnippeDataArrayIntKeepSelectedComponents1_3]
721         pass
722
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)
728         f1.setTime(2.3,5,6)
729         f1.setMesh(mesh1)
730         array=DataArrayDouble.New()
731         arr1=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.]
732         array.setValues(arr1,mesh1.getNumberOfCells(),2)
733         f1.setArray(array)
734 # ! [PySnippetFieldDoubleBuildSubPart1_1]
735 # ! [PySnippetFieldDoubleBuildSubPart1_2]
736         part1=[2,1,4]
737         f2=f1.buildSubPart(part1)
738 # ! [PySnippetFieldDoubleBuildSubPart1_2]
739         f2.zipCoords()
740         self.assertEqual(3,f2.getNumberOfTuples())
741         self.assertEqual(2,f2.getNumberOfComponents())
742         expected1=[5.,105.,4.,104.,7.,107.]
743         for i in xrange(6):
744             self.assertAlmostEqual(f2.getIJ(0,i),expected1[i],12)
745             pass
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())
750         m2C=f2.getMesh()
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]
753         for i in xrange(12):
754             self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
755             pass
756         expected3=[3,2,3,1,3,0,2,1,4,4,5,3,2]
757         self.assertEqual(expected3,list(m2C.getNodalConnectivity().getValues()))
758         expected4=[0,4,8,13]
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)
763         f1.setTime(2.3,5,6)
764         f1.setMesh(mesh1)
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)
768         f1.setArray(array)
769 # ! [PySnippetFieldDoubleBuildSubPart1_3]
770 # ! [PySnippetFieldDoubleBuildSubPart1_4]
771         part2=[1,2]
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.]
777         for i in xrange(8):
778             self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
779             pass
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())
784         m2C=f2.getMesh()
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)
788             pass
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
793         part3=[1,2]
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())
799         for i in xrange(8):
800             self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12)
801             pass
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())
806         m2C=f2.getMesh()
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)
810             pass
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()))
814         part4=[1,2,4]
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.]
819         for i in xrange(12):
820             self.assertAlmostEqual(f2.getIJ(0,i),expected6[i],12)
821             pass
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())
826         m2C=f2.getMesh()
827         self.assertEqual(13,m2C.getMeshLength())
828         for i in xrange(12):
829             self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12)
830             pass
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()))
835         pass
836
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()
862         pass
863
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())
892         pass
893
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()
916         pass
917
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]
939         pass
940
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
955         # ...
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
961         f3=f1+f2 # f3 scalar
962         f4=f3/f2 # f4 scalar
963         f2bis.applyFunc(1,"sqrt(x*x+y*y)") # f2bis becomes scalar
964         f5=f2bis*f4 # f5 scalar
965         pos1=[0.48,0.38]
966         res=f4.getValueOn(pos1) # f4 is scalar so the returned value is of size 1.
967         # ...
968 # ! [PySnippetFieldDoubleBuild1_2]
969 # ! [PySnippetFieldDoubleBuild1_3]
970 # ! [PySnippetFieldDoubleBuild1_3]
971         pass
972
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
987         # ...
988 # ! [PySnippetFieldDoubleBuild2_1]
989         pass
990
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
1007         # ...
1008 # ! [PySnippetFieldDoubleBuild3_1]
1009         pass
1010
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
1028         # ...
1029 # ! [PySnippetFieldDoubleBuild4_1]
1030         pass
1031
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]
1069         pass
1070
1071     pass
1072
1073 unittest.main()