Back to index

salome-med  6.5.0
MEDCouplingRemapperTest.py
Go to the documentation of this file.
00001 #  -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00003 #
00004 # This library is free software; you can redistribute it and/or
00005 # modify it under the terms of the GNU Lesser General Public
00006 # License as published by the Free Software Foundation; either
00007 # version 2.1 of the License.
00008 #
00009 # This library is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 # Lesser General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU Lesser General Public
00015 # License along with this library; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 #
00018 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00019 #
00020 
00021 from MEDCouplingRemapper import *
00022 from math import *
00023 import unittest
00024 
00025 class MEDCouplingBasicsTest(unittest.TestCase):
00026     def testRemapper1(self):
00027         sourceMesh=self.build2DSourceMesh_1();
00028         targetMesh=self.build2DTargetMesh_1();
00029         remapper=MEDCouplingRemapper()
00030         remapper.setPrecision(1e-12);
00031         remapper.setIntersectionType(Triangulation);
00032         self.failUnless(remapper.prepare(sourceMesh,targetMesh,"P0P0")==1);
00033         srcField=MEDCouplingFieldDouble.New(ON_CELLS);
00034         srcField.setNature(ConservativeVolumic);
00035         srcField.setMesh(sourceMesh);
00036         array=DataArrayDouble.New();
00037         ptr=sourceMesh.getNumberOfCells()*[None]
00038         for i in xrange(sourceMesh.getNumberOfCells()):
00039             ptr[i]=float(i+7)
00040             pass
00041         array.setValues(ptr,sourceMesh.getNumberOfCells(),1);
00042         srcField.setArray(array);
00043         trgfield=remapper.transferField(srcField,4.57);
00044         values=trgfield.getArray().getValues();
00045         valuesExpected=[7.5 ,7. ,7.,8.,7.5];
00046         for i in xrange(targetMesh.getNumberOfCells()):
00047             self.failUnless(abs(values[i]-valuesExpected[i])<1e-12);
00048             pass
00049         self.failUnless(1==trgfield.getArray().getNumberOfComponents());
00050         pass
00051 
00052     def testPrepareEx1(self):
00053         sourceMesh=self.build2DSourceMesh_1();
00054         targetMesh=self.build2DTargetMesh_3();
00055         #
00056         remapper=MEDCouplingRemapper();
00057         remapper.setPrecision(1e-12);
00058         remapper.setIntersectionType(Triangulation);
00059         srcFt=MEDCouplingFieldTemplate.New(ON_CELLS);
00060         trgFt=MEDCouplingFieldTemplate.New(ON_CELLS);
00061         srcFt.setMesh(sourceMesh);
00062         trgFt.setMesh(targetMesh);
00063         self.assertEqual(1,remapper.prepareEx(srcFt,trgFt));
00064         srcField=MEDCouplingFieldDouble.New(ON_CELLS);
00065         srcField.setNature(ConservativeVolumic);
00066         srcField.setMesh(sourceMesh);
00067         array=DataArrayDouble.New();
00068         ptr=sourceMesh.getNumberOfCells()*[None]
00069         for i in xrange(sourceMesh.getNumberOfCells()):
00070             ptr[i]=float(i+7);
00071             pass
00072         array.setValues(ptr,sourceMesh.getNumberOfCells(),1);
00073         srcField.setArray(array);
00074         trgfield=remapper.transferField(srcField,4.220173);
00075         values=trgfield.getArray().getValues();
00076         valuesExpected=[7.75, 7.0625, 4.220173,8.0]
00077         self.assertEqual(4,trgfield.getArray().getNumberOfTuples());
00078         self.assertEqual(1,trgfield.getArray().getNumberOfComponents());
00079         for i0 in xrange(4):
00080             self.assertAlmostEqual(valuesExpected[i0],values[i0],12);
00081             pass
00082         pass
00083 
00084     def testPartialTransfer1(self):
00085         sourceMesh=self.build2DSourceMesh_1();
00086         targetMesh=self.build2DTargetMesh_3();
00087         #
00088         remapper=MEDCouplingRemapper();
00089         remapper.setPrecision(1e-12);
00090         remapper.setIntersectionType(Triangulation);
00091         srcFt=MEDCouplingFieldTemplate.New(ON_CELLS);
00092         trgFt=MEDCouplingFieldTemplate.New(ON_CELLS);
00093         srcFt.setMesh(sourceMesh);
00094         trgFt.setMesh(targetMesh);
00095         self.assertEqual(1,remapper.prepareEx(srcFt,trgFt));
00096         srcField=MEDCouplingFieldDouble.New(ON_CELLS);
00097         srcField.setNature(ConservativeVolumic);
00098         srcField.setMesh(sourceMesh);
00099         array=DataArrayDouble.New();
00100         ptr=sourceMesh.getNumberOfCells()*[None]
00101         for i in xrange(sourceMesh.getNumberOfCells()):
00102             ptr[i]=float(i+7);
00103             pass
00104         array.setValues(ptr,sourceMesh.getNumberOfCells(),1);
00105         srcField.setArray(array);
00106         trgfield=MEDCouplingFieldDouble.New(ON_CELLS);
00107         trgfield.setNature(ConservativeVolumic);
00108         trgfield.setMesh(targetMesh);
00109         array=DataArrayDouble.New();
00110         ptr=targetMesh.getNumberOfCells()*[None]
00111         for i in xrange(targetMesh.getNumberOfCells()):
00112             ptr[i]=4.220173;
00113             pass
00114         array.setValues(ptr,targetMesh.getNumberOfCells(),1);
00115         trgfield.setArray(array);
00116         remapper.partialTransfer(srcField,trgfield);
00117         values=trgfield.getArray().getValues();
00118         valuesExpected=[7.75, 7.0625, 4.220173,8.0]
00119         self.assertEqual(4,trgfield.getArray().getNumberOfTuples());
00120         self.assertEqual(1,trgfield.getArray().getNumberOfComponents());
00121         for i0 in xrange(4):
00122             self.assertAlmostEqual(valuesExpected[i0],values[i0],12);
00123             pass
00124         pass
00125     
00126     def build2DSourceMesh_1(self):
00127         sourceCoords=[-0.3,-0.3, 0.7,-0.3, -0.3,0.7, 0.7,0.7]
00128         sourceConn=[0,3,1,0,2,3]
00129         sourceMesh=MEDCouplingUMesh.New("my name of mesh 2D",2)
00130         sourceMesh.allocateCells(2);
00131         sourceMesh.insertNextCell(NORM_TRI3,3,sourceConn[0:3]);
00132         sourceMesh.insertNextCell(NORM_TRI3,3,sourceConn[3:6]);
00133         sourceMesh.finishInsertingCells();
00134         myCoords=DataArrayDouble.New();
00135         myCoords.setValues(sourceCoords,4,2);
00136         sourceMesh.setCoords(myCoords);
00137         return sourceMesh;
00138     
00139     def build2DTargetMesh_1(self):
00140         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 ]
00141         targetConn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
00142         targetMesh=MEDCouplingUMesh.New();
00143         targetMesh.setMeshDimension(2);
00144         targetMesh.allocateCells(5);
00145         targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[0:4]);
00146         targetMesh.insertNextCell(NORM_TRI3,3,targetConn[4:7]);
00147         targetMesh.insertNextCell(NORM_TRI3,3,targetConn[7:10]);
00148         targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[10:14]);
00149         targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[14:18]);
00150         targetMesh.finishInsertingCells();
00151         myCoords=DataArrayDouble.New();
00152         myCoords.setValues(targetCoords,9,2);
00153         targetMesh.setCoords(myCoords);
00154         return targetMesh;
00155 
00156     def build2DTargetMesh_3(self):
00157         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]
00158         targetConn=[0,4,5,1, 1,5,6,2, 2,6,7,3, 4,8,9,5]
00159         targetMesh=MEDCouplingUMesh.New();
00160         targetMesh.setMeshDimension(2);
00161         targetMesh.allocateCells(4);
00162         for i in xrange(4):
00163             targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[4*i:4*(i+1)])
00164             pass
00165         targetMesh.finishInsertingCells();
00166         myCoords=DataArrayDouble.New();
00167         myCoords.setValues(targetCoords,10,2);
00168         targetMesh.setCoords(myCoords);
00169         return targetMesh;
00170         pass
00171     
00172     def setUp(self):
00173         pass
00174     pass
00175 
00176 unittest.main()