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 MEDCouplingRemapper import *
25 class MEDCouplingBasicsTest(unittest.TestCase):
26 def testRemapper1(self):
27 sourceMesh=self.build2DSourceMesh_1();
28 targetMesh=self.build2DTargetMesh_1();
29 remapper=MEDCouplingRemapper()
30 remapper.setPrecision(1e-12);
31 remapper.setIntersectionType(Triangulation);
32 self.failUnless(remapper.prepare(sourceMesh,targetMesh,"P0P0")==1);
33 srcField=MEDCouplingFieldDouble.New(ON_CELLS);
34 srcField.setNature(ConservativeVolumic);
35 srcField.setMesh(sourceMesh);
36 array=DataArrayDouble.New();
37 ptr=sourceMesh.getNumberOfCells()*[None]
38 for i in xrange(sourceMesh.getNumberOfCells()):
41 array.setValues(ptr,sourceMesh.getNumberOfCells(),1);
42 srcField.setArray(array);
43 trgfield=remapper.transferField(srcField,4.57);
44 values=trgfield.getArray().getValues();
45 valuesExpected=[7.5 ,7. ,7.,8.,7.5];
46 for i in xrange(targetMesh.getNumberOfCells()):
47 self.failUnless(abs(values[i]-valuesExpected[i])<1e-12);
49 self.failUnless(1==trgfield.getArray().getNumberOfComponents());
52 def testPrepareEx1(self):
53 sourceMesh=self.build2DSourceMesh_1();
54 targetMesh=self.build2DTargetMesh_3();
56 remapper=MEDCouplingRemapper();
57 remapper.setPrecision(1e-12);
58 remapper.setIntersectionType(Triangulation);
59 srcFt=MEDCouplingFieldTemplate.New(ON_CELLS);
60 trgFt=MEDCouplingFieldTemplate.New(ON_CELLS);
61 srcFt.setMesh(sourceMesh);
62 trgFt.setMesh(targetMesh);
63 self.assertEqual(1,remapper.prepareEx(srcFt,trgFt));
64 srcField=MEDCouplingFieldDouble.New(ON_CELLS);
65 srcField.setNature(ConservativeVolumic);
66 srcField.setMesh(sourceMesh);
67 array=DataArrayDouble.New();
68 ptr=sourceMesh.getNumberOfCells()*[None]
69 for i in xrange(sourceMesh.getNumberOfCells()):
72 array.setValues(ptr,sourceMesh.getNumberOfCells(),1);
73 srcField.setArray(array);
74 trgfield=remapper.transferField(srcField,4.220173);
75 values=trgfield.getArray().getValues();
76 valuesExpected=[7.75, 7.0625, 4.220173,8.0]
77 self.assertEqual(4,trgfield.getArray().getNumberOfTuples());
78 self.assertEqual(1,trgfield.getArray().getNumberOfComponents());
80 self.assertAlmostEqual(valuesExpected[i0],values[i0],12);
84 def testPartialTransfer1(self):
85 sourceMesh=self.build2DSourceMesh_1();
86 targetMesh=self.build2DTargetMesh_3();
88 remapper=MEDCouplingRemapper();
89 remapper.setPrecision(1e-12);
90 remapper.setIntersectionType(Triangulation);
91 srcFt=MEDCouplingFieldTemplate.New(ON_CELLS);
92 trgFt=MEDCouplingFieldTemplate.New(ON_CELLS);
93 srcFt.setMesh(sourceMesh);
94 trgFt.setMesh(targetMesh);
95 self.assertEqual(1,remapper.prepareEx(srcFt,trgFt));
96 srcField=MEDCouplingFieldDouble.New(ON_CELLS);
97 srcField.setNature(ConservativeVolumic);
98 srcField.setMesh(sourceMesh);
99 array=DataArrayDouble.New();
100 ptr=sourceMesh.getNumberOfCells()*[None]
101 for i in xrange(sourceMesh.getNumberOfCells()):
104 array.setValues(ptr,sourceMesh.getNumberOfCells(),1);
105 srcField.setArray(array);
106 trgfield=MEDCouplingFieldDouble.New(ON_CELLS);
107 trgfield.setNature(ConservativeVolumic);
108 trgfield.setMesh(targetMesh);
109 array=DataArrayDouble.New();
110 ptr=targetMesh.getNumberOfCells()*[None]
111 for i in xrange(targetMesh.getNumberOfCells()):
114 array.setValues(ptr,targetMesh.getNumberOfCells(),1);
115 trgfield.setArray(array);
116 remapper.partialTransfer(srcField,trgfield);
117 values=trgfield.getArray().getValues();
118 valuesExpected=[7.75, 7.0625, 4.220173,8.0]
119 self.assertEqual(4,trgfield.getArray().getNumberOfTuples());
120 self.assertEqual(1,trgfield.getArray().getNumberOfComponents());
122 self.assertAlmostEqual(valuesExpected[i0],values[i0],12);
126 def testPrepareUC(self):
128 coords=DataArrayDouble([0.,0.5,0.7])
129 src=MEDCouplingUMesh("",1) ; src.setCoords(coords)
130 src.allocateCells(2) ; src.insertNextCell(NORM_SEG2,[0,1]) ; src.insertNextCell(NORM_SEG2,[1,2]) ; src.finishInsertingCells()
131 trg=MEDCouplingCMesh() ; arr=DataArrayDouble([-0.7,-0.1,0.2,0.7,2.,2.3])
132 trg.setCoordsAt(0,arr)
133 fieldSrc=MEDCouplingFieldDouble(ON_CELLS,NO_TIME) ; fieldSrc.setMesh(src) ; arrSrc=DataArrayDouble([10.,30.])
134 fieldSrc.setNature(Integral) ; fieldSrc.setArray(arrSrc)
135 rem=MEDCouplingRemapper()
136 rem.prepare(src,trg,"P0P0")
137 trgField=rem.transferField(fieldSrc,-7.)
138 expected1=[-7.,4.,36.,-7.,-7.]
139 self.assertEqual(5,trgField.getArray().getNumberOfTuples())
140 self.assertEqual(5,len(expected1))
141 for i,val in enumerate(expected1):
142 self.assertAlmostEqual(expected1[i],trgField.getArray().getIJ(i,0),12);
145 coords=DataArrayDouble([0.,0.,0.,1.,1.,1.,1.,0.,0.5,-0.2],5,2)
146 src=MEDCouplingUMesh("",2) ; src.setCoords(coords)
147 src.allocateCells(2) ; src.insertNextCell(NORM_TRI3,[0,1,2]) ; src.insertNextCell(NORM_TRI3,[3,4,0]) ; src.finishInsertingCells()
148 trg=MEDCouplingCMesh() ; arr=DataArrayDouble([-0.7,-0.1,0.2,0.7,2.,2.3])
149 trg.setCoordsAt(0,arr) ; trg.setCoordsAt(1,arr)
150 fieldSrc=MEDCouplingFieldDouble(ON_CELLS,NO_TIME) ; fieldSrc.setMesh(src) ; arrSrc=DataArrayDouble([10.,30.])
151 fieldSrc.setNature(Integral) ; fieldSrc.setArray(arrSrc)
152 rem=MEDCouplingRemapper()
153 rem.prepare(src,trg,"P0P0")
154 trgField=rem.transferField(fieldSrc,-7.)
155 expected2=[-7.,-7.,7.35,0.15,-7.,-7.,2.8,14.85,5.25,-7.,-7.,2.,2.5,-7.,-7.,-7.,1.2,3.,0.9,-7.,-7.,-7.,-7.,-7.,-7.]
156 self.assertEqual(25,trgField.getArray().getNumberOfTuples())
157 self.assertEqual(25,len(expected2))
158 for i,val in enumerate(expected2):
159 self.assertAlmostEqual(expected2[i],trgField.getArray().getIJ(i,0),12);
162 coords=DataArrayDouble([0.,0.,0.,0.,1.,0.,1.,1.,0.,1.,0.,0.,0.5,-0.2,0.,0.1,0.8,1.,0.5,0.,1.],7,3)
163 src=MEDCouplingUMesh("",3) ; src.setCoords(coords)
164 src.allocateCells(2) ; src.insertNextCell(NORM_TETRA4,[0,1,2,5]) ; src.insertNextCell(NORM_TETRA4,[3,4,0,6]) ; src.finishInsertingCells()
165 trg=MEDCouplingCMesh() ; arr=DataArrayDouble([-0.7,-0.1,0.2,0.7,2.,2.3]) ; arr2=DataArrayDouble([-0.7,0.2,0.6,1.2,2.])
166 trg.setCoordsAt(0,arr) ; trg.setCoordsAt(1,arr) ; trg.setCoordsAt(2,arr2)
167 src.checkCoherency2(1e-10)
169 fieldSrc=MEDCouplingFieldDouble(ON_CELLS,NO_TIME) ; fieldSrc.setMesh(src) ; arrSrc=DataArrayDouble([10.,30.])
170 fieldSrc.setNature(Integral) ; fieldSrc.setArray(arrSrc)
171 rem=MEDCouplingRemapper()
172 rem.prepare(src,trg,"P0P0")
173 trgField=rem.transferField(fieldSrc,-7.)
174 expected3=[-7.,-7.,2.925,0.015,-7.,-7.,0.9392,8.595,2.265,-7.,-7.,1.1008,1.1192,-7.,-7.,-7.,0.6392,1.6408,0.2808,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,0.81,-7.,-7.,-7.,0.1208,11.55,0.96,-7.,-7.,1.1752,0.6592,-7.,-7.,-7.,0.8512,1.7744,0.0192,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,1.92,-7.,-7.,-7.,0.12578571428571422,0.007314285714285673,-7.,-7.,-7.,0.3189253968253971,0.1879746031746033,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.,-7.]
175 self.assertEqual(100,trgField.getArray().getNumberOfTuples())
176 self.assertEqual(100,len(expected3))
177 for i,val in enumerate(expected3):
178 self.assertAlmostEqual(expected3[i],trgField.getArray().getIJ(i,0),12);
182 def testPrepareCU(self):
184 coords=DataArrayDouble([0.,0.5,0.7])
185 trg=MEDCouplingUMesh("",1) ; trg.setCoords(coords)
186 trg.allocateCells(2) ; trg.insertNextCell(NORM_SEG2,[0,1]) ; trg.insertNextCell(NORM_SEG2,[1,2]) ; trg.finishInsertingCells()
187 src=MEDCouplingCMesh() ; arr=DataArrayDouble([-0.7,-0.1,0.2,0.7,2.,2.3])
188 src.setCoordsAt(0,arr)
189 fieldSrc=MEDCouplingFieldDouble(ON_CELLS,NO_TIME) ; fieldSrc.setMesh(src) ; arrTrg=DataArrayDouble([10.,30.,40.,70.,80.])
190 fieldSrc.setNature(Integral) ; fieldSrc.setArray(arrTrg)
191 rem=MEDCouplingRemapper()
192 rem.prepare(src,trg,"P0P0")
193 trgField=rem.transferField(fieldSrc,-7.)
195 self.assertEqual(2.,trgField.getArray().getNumberOfTuples())
196 self.assertEqual(2,len(expected1))
197 for i,val in enumerate(expected1):
198 self.assertAlmostEqual(expected1[i],trgField.getArray().getIJ(i,0),12);
201 coords=DataArrayDouble([0.,0.,0.,1.,1.,1.,1.,0.,0.5,-0.2],5,2)
202 trg=MEDCouplingUMesh("",2) ; trg.setCoords(coords)
203 trg.allocateCells(2) ; trg.insertNextCell(NORM_TRI3,[0,1,2]) ; trg.insertNextCell(NORM_TRI3,[3,4,0]) ; trg.finishInsertingCells()
204 src=MEDCouplingCMesh() ; arr=DataArrayDouble([-0.7,-0.1,0.2,0.7,2.,2.3])
205 src.setCoordsAt(0,arr) ; src.setCoordsAt(1,arr)
206 fieldSrc=MEDCouplingFieldDouble(ON_CELLS,NO_TIME) ; fieldSrc.setMesh(src) ; arrSrc=DataArrayDouble([10.,30.,40.,70.,80.,110.,130.,140.,170.,180.,210.,230.,240.,270.,280.,310.,330.,340.,370.,380.,410.,430.,440.,470.,480.])
207 fieldSrc.setNature(Integral) ; fieldSrc.setArray(arrSrc)
208 rem=MEDCouplingRemapper()
209 rem.prepare(src,trg,"P0P0")
210 trgField=rem.transferField(fieldSrc,-7.)
211 expected2=[441.3050624589086,68.69529914529915]
212 self.assertEqual(2,trgField.getArray().getNumberOfTuples())
213 self.assertEqual(2,len(expected2))
214 for i,val in enumerate(expected2):
215 self.assertAlmostEqual(expected2[i],trgField.getArray().getIJ(i,0),12);
218 coords=DataArrayDouble([0.,0.,0.,0.,1.,0.,1.,1.,0.,1.,0.,0.,0.5,-0.2,0.,0.1,0.8,1.,0.5,0.,1.],7,3)
219 trg=MEDCouplingUMesh("",3) ; trg.setCoords(coords)
220 trg.allocateCells(2) ; trg.insertNextCell(NORM_TETRA4,[0,1,2,5]) ; trg.insertNextCell(NORM_TETRA4,[3,4,0,6]) ; trg.finishInsertingCells()
221 src=MEDCouplingCMesh() ; arr=DataArrayDouble([-0.7,-0.1,0.2,0.7,2.,2.3]) ; arr2=DataArrayDouble([-0.7,0.2,0.6,1.2,2.])
222 src.setCoordsAt(0,arr) ; src.setCoordsAt(1,arr) ; src.setCoordsAt(2,arr2)
223 trg.checkCoherency2(1e-10)
225 fieldSrc=MEDCouplingFieldDouble(ON_CELLS,NO_TIME) ; fieldSrc.setMesh(src) ; arrSrc=DataArrayDouble(100) ; arrSrc.iota(7.7)
226 fieldSrc.setNature(Integral) ; fieldSrc.setArray(arrSrc)
227 rem=MEDCouplingRemapper()
228 rem.prepare(src,trg,"P0P0")
229 trgField=rem.transferField(fieldSrc,-7.)
230 expected3=[39.635196634558845,12.13422356758468]
231 self.assertEqual(2,trgField.getArray().getNumberOfTuples())
232 self.assertEqual(2,len(expected3))
233 for i,val in enumerate(expected3):
234 self.assertAlmostEqual(expected3[i],trgField.getArray().getIJ(i,0),12);
238 def testPrepareCC(self):
240 src=MEDCouplingCMesh() ; arr=DataArrayDouble([-0.7,-0.1,0.2,0.7,2.,2.3])
241 src.setCoordsAt(0,arr)
242 trg=MEDCouplingCMesh() ; arr=DataArrayDouble([-0.9,-0.1,0.15])
243 trg.setCoordsAt(0,arr)
244 fieldSrc=MEDCouplingFieldDouble(ON_CELLS,NO_TIME) ; fieldSrc.setMesh(src) ; arrTrg=DataArrayDouble([10.,30.,40.,70.,80.])
245 fieldSrc.setNature(Integral) ; fieldSrc.setArray(arrTrg)
246 rem=MEDCouplingRemapper()
247 rem.prepare(src,trg,"P0P0")
248 trgField=rem.transferField(fieldSrc,-7.)
250 self.assertEqual(2.,trgField.getArray().getNumberOfTuples())
251 self.assertEqual(2,len(expected1))
252 for i,val in enumerate(expected1):
253 self.assertAlmostEqual(expected1[i],trgField.getArray().getIJ(i,0),12);
256 src=MEDCouplingCMesh() ; arr=DataArrayDouble([-0.7,-0.1,0.2,0.7,2.,2.3])
257 src.setCoordsAt(0,arr) ; src.setCoordsAt(1,arr)
258 trg=MEDCouplingCMesh() ; arr=DataArrayDouble([-0.9,-0.1,0.15])
259 trg.setCoordsAt(0,arr) ; trg.setCoordsAt(1,arr)
260 fieldSrc=MEDCouplingFieldDouble(ON_CELLS,NO_TIME) ; fieldSrc.setMesh(src) ; arrSrc=DataArrayDouble([10.,30.,40.,70.,80.,110.,130.,140.,170.,180.,210.,230.,240.,270.,280.,310.,330.,340.,370.,380.,410.,430.,440.,470.,480.])
261 fieldSrc.setNature(Integral) ; fieldSrc.setArray(arrSrc)
262 rem=MEDCouplingRemapper()
263 rem.prepare(src,trg,"P0P0")
264 trgField=rem.transferField(fieldSrc,-7.)
265 expected2=[10.,25.,91.66666666666666,90.27777777777777]
266 self.assertEqual(4,trgField.getArray().getNumberOfTuples())
267 self.assertEqual(4,len(expected2))
268 for i,val in enumerate(expected2):
269 self.assertAlmostEqual(expected2[i],trgField.getArray().getIJ(i,0),12);
272 src=MEDCouplingCMesh() ; arr=DataArrayDouble([-0.7,-0.1,0.2,0.7,2.,2.3])
273 src.setCoordsAt(0,arr) ; src.setCoordsAt(1,arr) ; src.setCoordsAt(2,arr)
274 trg=MEDCouplingCMesh() ; arr=DataArrayDouble([-0.9,-0.1,0.15])
275 trg.setCoordsAt(0,arr) ; trg.setCoordsAt(1,arr) ; trg.setCoordsAt(2,arr)
276 fieldSrc=MEDCouplingFieldDouble(ON_CELLS,NO_TIME) ; fieldSrc.setMesh(src) ; arrSrc=DataArrayDouble(125) ; arrSrc.iota(7.7)
277 fieldSrc.setNature(Integral) ; fieldSrc.setArray(arrSrc) ; fieldSrc.checkCoherency()
278 rem=MEDCouplingRemapper()
279 rem.prepare(src,trg,"P0P0")
280 trgField=rem.transferField(fieldSrc,-7.)
281 expected3=[7.7, 7.249999999999999, 10.583333333333332, 9.513888888888886, 27.25, 23.40277777777777, 26.180555555555546, 22.39583333333333]
282 self.assertEqual(8,trgField.getArray().getNumberOfTuples())
283 self.assertEqual(8,len(expected3))
284 for i,val in enumerate(expected3):
285 self.assertAlmostEqual(expected3[i],trgField.getArray().getIJ(i,0),12);
289 def build2DSourceMesh_1(self):
290 sourceCoords=[-0.3,-0.3, 0.7,-0.3, -0.3,0.7, 0.7,0.7]
291 sourceConn=[0,3,1,0,2,3]
292 sourceMesh=MEDCouplingUMesh.New("my name of mesh 2D",2)
293 sourceMesh.allocateCells(2);
294 sourceMesh.insertNextCell(NORM_TRI3,3,sourceConn[0:3]);
295 sourceMesh.insertNextCell(NORM_TRI3,3,sourceConn[3:6]);
296 sourceMesh.finishInsertingCells();
297 myCoords=DataArrayDouble.New();
298 myCoords.setValues(sourceCoords,4,2);
299 sourceMesh.setCoords(myCoords);
302 def build2DTargetMesh_1(self):
303 targetCoords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ]
304 targetConn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
305 targetMesh=MEDCouplingUMesh.New();
306 targetMesh.setMeshDimension(2);
307 targetMesh.allocateCells(5);
308 targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[0:4]);
309 targetMesh.insertNextCell(NORM_TRI3,3,targetConn[4:7]);
310 targetMesh.insertNextCell(NORM_TRI3,3,targetConn[7:10]);
311 targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[10:14]);
312 targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[14:18]);
313 targetMesh.finishInsertingCells();
314 myCoords=DataArrayDouble.New();
315 myCoords.setValues(targetCoords,9,2);
316 targetMesh.setCoords(myCoords);
319 def build2DTargetMesh_3(self):
320 targetCoords=[-0.6,-0.4, -0.1,-0.4, 1.1,-0.4, 2.1,-0.4, -0.6,0.1, -0.1,0.1, 1.1,0.1, 2.1,0.1, -0.6,1.1, -0.1,1.1]
321 targetConn=[0,4,5,1, 1,5,6,2, 2,6,7,3, 4,8,9,5]
322 targetMesh=MEDCouplingUMesh.New();
323 targetMesh.setMeshDimension(2);
324 targetMesh.allocateCells(4);
326 targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[4*i:4*(i+1)])
328 targetMesh.finishInsertingCells();
329 myCoords=DataArrayDouble.New();
330 myCoords.setValues(targetCoords,10,2);
331 targetMesh.setCoords(myCoords);