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 build2DSourceMesh_1(self):
127 sourceCoords=[-0.3,-0.3, 0.7,-0.3, -0.3,0.7, 0.7,0.7]
128 sourceConn=[0,3,1,0,2,3]
129 sourceMesh=MEDCouplingUMesh.New("my name of mesh 2D",2)
130 sourceMesh.allocateCells(2);
131 sourceMesh.insertNextCell(NORM_TRI3,3,sourceConn[0:3]);
132 sourceMesh.insertNextCell(NORM_TRI3,3,sourceConn[3:6]);
133 sourceMesh.finishInsertingCells();
134 myCoords=DataArrayDouble.New();
135 myCoords.setValues(sourceCoords,4,2);
136 sourceMesh.setCoords(myCoords);
139 def build2DTargetMesh_1(self):
140 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 ]
141 targetConn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
142 targetMesh=MEDCouplingUMesh.New();
143 targetMesh.setMeshDimension(2);
144 targetMesh.allocateCells(5);
145 targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[0:4]);
146 targetMesh.insertNextCell(NORM_TRI3,3,targetConn[4:7]);
147 targetMesh.insertNextCell(NORM_TRI3,3,targetConn[7:10]);
148 targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[10:14]);
149 targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[14:18]);
150 targetMesh.finishInsertingCells();
151 myCoords=DataArrayDouble.New();
152 myCoords.setValues(targetCoords,9,2);
153 targetMesh.setCoords(myCoords);
156 def build2DTargetMesh_3(self):
157 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]
158 targetConn=[0,4,5,1, 1,5,6,2, 2,6,7,3, 4,8,9,5]
159 targetMesh=MEDCouplingUMesh.New();
160 targetMesh.setMeshDimension(2);
161 targetMesh.allocateCells(4);
163 targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[4*i:4*(i+1)])
165 targetMesh.finishInsertingCells();
166 myCoords=DataArrayDouble.New();
167 myCoords.setValues(targetCoords,10,2);
168 targetMesh.setCoords(myCoords);