From: ageay Date: Wed, 11 Mar 2009 17:24:40 +0000 (+0000) Subject: Swig addition. X-Git-Tag: V5_1_main_FINAL~422 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=986a05f0b36d92c2d663b07ec839f9ad22d54ecd;p=tools%2Fmedcoupling.git Swig addition. --- diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py new file mode 100644 index 000000000..66c91674a --- /dev/null +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -0,0 +1,61 @@ +from libMEDCoupling_Swig import * +import unittest + +class MEDCouplingBasicsTest(unittest.TestCase): + def testMesh(self): + tab4=[1, 2, 8, 7, 2, 3, 9, 8, 3, + 4, 10, 9, 4, 5, 11, 10, 5, + 0, 6, 11, 0, 1, 7, 6 ] + nbOfNodes=12 + nbOfCells=6 + coords=[ 0.024155, 0.04183768725682622, -0.305, 0.04831000000000001, -1.015761910347357e-17, + -0.305, 0.09662000000000001, -1.832979297858306e-18, -0.305, 0.120775, 0.04183768725682623, + -0.305, 0.09662000000000001, 0.08367537451365245, -0.305, 0.04831000000000001, 0.08367537451365246, + -0.305, 0.024155, 0.04183768725682622, -0.2863, 0.04831000000000001, -1.015761910347357e-17, -0.2863, + 0.09662000000000001, -1.832979297858306e-18, -0.2863, 0.120775, 0.04183768725682623, -0.2863, 0.09662000000000001, + 0.08367537451365245, -0.2863, 0.04831000000000001, 0.08367537451365246, -0.2863 ] + mesh=MEDCouplingUMesh.New() + mesh.setMeshDimension(2) + mesh.allocateCells(8); + mesh.setName("mesh1") + self.failUnless(mesh.getName()=="mesh1") + for i in range(nbOfCells): + mesh.insertNextCell(NORM_QUAD4,4,tab4[4*i:4*(i+1)]); + pass + mesh.finishInsertingCells() + self.failUnless(mesh.getNumberOfCells()==nbOfCells) + self.failUnless(mesh.getNodalConnectivity().getNbOfElems()==30) + self.failUnless(mesh.getNodalConnectivityIndex().getNbOfElems()==nbOfCells+1) + myCoords=DataArrayDouble.New() + myCoords.setValues(coords,nbOfNodes,3); + self.failUnless(myCoords.getIJ(3,2)==-0.305) + mesh.setCoords(myCoords); + mesh.checkCoherency(); + self.failUnless(mesh.getAllTypes()==[4]) + myFalseConn=DataArrayInt.New() + myFalseConn.setValues(tab4,6,4) + self.failUnless(myFalseConn.getIJ(1,1)==3) + # + field=MEDCouplingFieldDouble.New(ON_CELLS) + field.setMesh(mesh) + myCoords=DataArrayDouble.New() + sampleTab=[] + for i in range(nbOfCells*9): + sampleTab.append(float(i)) + myCoords.setValues(sampleTab,nbOfCells,9); + field.setArray(myCoords) + self.failUnless(3==mesh.getSpaceDimension()) + field.checkCoherency() + mesh2=mesh.clone(False) + mesh3=mesh.clone(True) + mesh3=0 + mesh2=0 + ## deep full recursively copy of field -> both field and mesh underneath copied + field2=field.clone(True) + field2.setMesh(field.getMesh().clone(True)) + pass + def setUp(self): + pass + pass + +unittest.main() diff --git a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i new file mode 100644 index 000000000..c3a33b1be --- /dev/null +++ b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i @@ -0,0 +1,128 @@ +// Copyright (C) 2007-2008 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +static PyObject* convertMesh(ParaMEDMEM::MEDCouplingMesh* mesh, int owner) +{ + PyObject *ret; + if(dynamic_cast(mesh)) + ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDCouplingUMesh,owner); + return ret; +} + +static PyObject *convertIntArrToPyList(const int *ptr, int size) +{ + PyObject *ret=PyList_New(size); + for(int i=0;idecrRef();" +%feature("unref") MEDCouplingUMesh "$this->decrRef();" +%feature("unref") DataArrayInt "$this->decrRef();" +%feature("unref") MEDCouplingFieldDouble "$this->decrRef();" + +%ignore ParaMEDMEM::TimeLabel::operator=; +%ignore ParaMEDMEM::MemArray::operator=; +%ignore ParaMEDMEM::MemArray::operator[]; + +%nodefaultctor; +%include "TimeLabel.hxx" +%include "RefCountObject.hxx" +%include "MEDCouplingMesh.hxx" +%include "MemArray.hxx" +%include "MEDCouplingMesh.hxx" +%include "NormalizedUnstructuredMesh.hxx" +%include "MEDCouplingField.hxx" + +namespace ParaMEDMEM +{ + class MEDCouplingUMesh : public ParaMEDMEM::MEDCouplingMesh + { + public: + static MEDCouplingUMesh *New(); + MEDCouplingUMesh *clone(bool recDeepCpy) const; + void updateTime(); + void checkCoherency() const throw(INTERP_KERNEL::Exception); + void setMeshDimension(unsigned meshDim); + void allocateCells(int nbOfCells); + void setCoords(DataArrayDouble *coords); + DataArrayDouble *getCoords() const; + void finishInsertingCells(); + void setConnectivity(DataArrayInt *conn, DataArrayInt *connIndex, bool isComputingTypes=true); + DataArrayInt *getNodalConnectivity() const; + DataArrayInt *getNodalConnectivityIndex() const; + INTERP_KERNEL::NormalizedCellType getTypeOfCell(int cellId) const; + int getNumberOfNodesInCell(int cellId) const; + bool isStructured() const; + int getNumberOfCells() const; + int getNumberOfNodes() const; + int getSpaceDimension() const; + int getMeshDimension() const; + int getMeshLength() const; + //tools + void zipCoords(); + DataArrayInt *zipCoordsTraducer(); + void getReverseNodalConnectivity(DataArrayInt *revNodal, DataArrayInt *revNodalIndx) const; + MEDCouplingUMesh *buildPartOfMySelf(const int *start, const int *end, bool keepCoords) const; + %extend { + void insertNextCell(INTERP_KERNEL::NormalizedCellType type, int size, PyObject *li) + { + int *tmp=convertPyToNewIntArr(li,size); + self->insertNextCell(type,size,tmp); + delete [] tmp; + } + PyObject *getAllTypes() const + { + std::set result=self->getAllTypes(); + std::set::const_iterator iL=result.begin(); + PyObject *res = PyList_New(result.size()); + for (int i=0;iL!=result.end(); i++, iL++) + PyList_SetItem(res,i,PyInt_FromLong(*iL)); + return res; + } + } + }; +} + +%extend ParaMEDMEM::DataArrayDouble + { + void setValues(PyObject *li, int nbOfTuples, int nbOfElsPerTuple) + { + double *tmp=convertPyToNewDblArr2(li); + self->useArray(tmp,true,CPP_DEALLOC,nbOfTuples,nbOfElsPerTuple); + } + + PyObject *getValues() + { + const double *vals=self->getPointer(); + return convertDblArrToPyList(vals,self->getNbOfElems()); + } + }; + +%extend ParaMEDMEM::DataArrayInt + { + void setValues(PyObject *li, int nbOfTuples, int nbOfElsPerTuple) + { + int *tmp=convertPyToNewIntArr2(li); + self->useArray(tmp,true,CPP_DEALLOC,nbOfTuples,nbOfElsPerTuple); + } + + PyObject *getValues() + { + const int *vals=self->getPointer(); + return convertIntArrToPyList(vals,self->getNbOfElems()); + } + }; + +%include "MEDCouplingField.hxx" + +namespace ParaMEDMEM +{ + class MEDCouplingFieldDouble : public ParaMEDMEM::MEDCouplingField + { + public: + static MEDCouplingFieldDouble *New(TypeOfField type); + MEDCouplingFieldDouble *clone(bool recDeepCpy) const; + void checkCoherency() const throw(INTERP_KERNEL::Exception); + double getIJ(int tupleId, int compoId) const; + void setArray(DataArrayDouble *array); + DataArrayDouble *getArray() const { return _array; } + void applyLin(double a, double b, int compoId); + int getNumberOfComponents() const; + int getNumberOfTuples() const throw(INTERP_KERNEL::Exception); + void updateTime(); + %extend { + void setValues(PyObject *li) + { + if(self->getArray()!=0) + { + double *tmp=convertPyToNewDblArr2(li); + int nbTuples=self->getArray()->getNumberOfTuples(); + int nbOfCompo=self->getArray()->getNumberOfComponents(); + self->getArray()->useArray(tmp,true,CPP_DEALLOC,nbTuples,nbOfCompo); + } + else + PyErr_SetString(PyExc_TypeError,"setValuesCpy : field must contain an array behind"); + } + } + }; +} diff --git a/src/ParaMEDMEM_Swig/Makefile.am b/src/ParaMEDMEM_Swig/Makefile.am index 7c0d356d7..e41cf7672 100644 --- a/src/ParaMEDMEM_Swig/Makefile.am +++ b/src/ParaMEDMEM_Swig/Makefile.am @@ -29,7 +29,8 @@ salomeinclude_HEADERS = \ SWIG_DEF = libParaMEDMEM_Swig.i -SWIG_FLAGS = @SWIG_FLAGS@ -I$(srcdir) $(MPI_INCLUDES) -I$(srcdir)/../MEDMEM_SWIG +SWIG_FLAGS = @SWIG_FLAGS@ -I$(srcdir) $(MPI_INCLUDES) -I$(srcdir)/../ParaMEDMEM -I$(srcdir)/../MEDCoupling -I$(srcdir)/../MEDCoupling_Swig \ + -I$(srcdir)/../INTERP_KERNEL -I$(srcdir)/../INTERP_KERNEL/Bases -I$(srcdir)/../ParaMEDMEM/MEDLoader dist__libParaMEDMEM_Swig_la_SOURCES = libParaMEDMEM_Swig.i nodist__libParaMEDMEM_Swig_la_SOURCES = libParaMEDMEM_Swig_wrap.cxx @@ -40,13 +41,13 @@ libParaMEDMEM_Swig_wrap.cxx: $(SWIG_DEF) _libParaMEDMEM_Swig_la_CPPFLAGS = $(CORBA_CXXFLAGS) $(CORBA_INCLUDES) $(PYTHON_INCLUDES) \ $(MED2_INCLUDES) $(HDF5_INCLUDES) @CXXTMPDPTHFLAGS@ \ - -I$(srcdir)/../MEDMEM -I$(srcdir)/../MEDWrapper/V2_1/Core -I$(srcdir)/../INTERP_KERNEL \ - $(MPI_INCLUDES) -I$(srcdir)/../ParaMEDMEM -I$(srcdir)/../MEDMEM_SWIG -I$(srcdir)/../INTERP_KERNEL/Bases \ - -I$(srcdir)/../MEDCoupling + -I$(srcdir)/../INTERP_KERNEL \ + $(MPI_INCLUDES) -I$(srcdir)/../ParaMEDMEM -I$(srcdir)/../MEDCoupling_Swig -I$(srcdir)/../INTERP_KERNEL/Bases \ + -I$(srcdir)/../MEDCoupling -I$(srcdir)/../ParaMEDMEM/MEDLoader _libParaMEDMEM_Swig_la_LDFLAGS = -module $(MED2_LIBS) $(HDF5_LIBS) $(PYTHON_LIBS) $(MPI_LIBS) \ - ../MEDMEM/libmedmem.la ../INTERP_KERNEL/libinterpkernel.la \ - ../ParaMEDMEM/libparamedmem.la + ../MEDCoupling/libmedcoupling.la ../INTERP_KERNEL/libinterpkernel.la \ + ../ParaMEDMEM/libparamedmem.la ../ParaMEDMEM/MEDLoader/libparamedmemmedloader.la if MED_ENABLE_KERNEL _libParaMEDMEM_Swig_la_CPPFLAGS += ${KERNEL_CXXFLAGS} diff --git a/src/ParaMEDMEM_Swig/libParaMEDMEM_Swig.i b/src/ParaMEDMEM_Swig/libParaMEDMEM_Swig.i index 435b7ead4..8bd9bca8e 100644 --- a/src/ParaMEDMEM_Swig/libParaMEDMEM_Swig.i +++ b/src/ParaMEDMEM_Swig/libParaMEDMEM_Swig.i @@ -20,207 +20,95 @@ %module libParaMEDMEM_Swig %include "libParaMEDMEM_Swig.typemap" -%include "../MEDMEM_SWIG/libMEDMEM_Swig.i" +%include "libMEDCoupling_Swig.i" %{ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "CommInterface.hxx" +#include "ProcessorGroup.hxx" +#include "Topology.hxx" +#include "MPIProcessorGroup.hxx" +#include "DEC.hxx" +#include "IntersectionDEC.hxx" +#include "NonCoincidentDEC.hxx" +#include "StructuredCoincidentDEC.hxx" +#include "ParaMESH.hxx" +#include "ParaFIELD.hxx" +#include "ICoCoMEDField.hxx" + +#include "MEDLoader.hxx" #include using namespace ParaMEDMEM; using namespace ICoCo; - - enum mpi_constants { mpi_comm_world, mpi_comm_self, mpi_double, mpi_int }; - + +enum mpi_constants { mpi_comm_world, mpi_comm_self, mpi_double, mpi_int }; %} +%newobject MEDLoader::ReadUMeshFromFile; -class CommInterface { -public: - CommInterface(); -}; - -class ProcessorGroup { -public: - %extend { - // avoid error when SWIG creates a default constructor of an abstract class - ProcessorGroup() { return NULL; } - } -}; - -class MPIProcessorGroup: public ProcessorGroup { -public: - MPIProcessorGroup(const CommInterface& interface); - MPIProcessorGroup(const CommInterface& interface, set proc_ids); - MPIProcessorGroup(const CommInterface& interface,int pstart, int pend); - - int translateRank(const ProcessorGroup* group, int rank) const; - ProcessorGroup* createComplementProcGroup() const; - ProcessorGroup* fuse (const ProcessorGroup&) const; +%include "ComponentTopology.hxx" +%include "CommInterface.hxx" +%include "ProcessorGroup.hxx" +%include "DECOptions.hxx" +%include "ParaMESH.hxx" +%include "ParaFIELD.hxx" +%include "MPIProcessorGroup.hxx" +%include "DEC.hxx" +%include "IntersectionDEC.hxx" +%include "StructuredCoincidentDEC.hxx" - bool containsMyRank() const; - int myRank() const; -}; +%rename(ICoCoMEDField) ICoCo::MEDField; +%include "ICoCoMEDField.hxx" -%rename(ICoCo_MEDField) ICoCo::MEDField; -namespace ICoCo { -class Field { -}; +%nodefaultctor; -class MEDField: public Field { -public: - MEDField(ParaMESH* mesh, ParaFIELD* field); -}; +namespace MEDLoader +{ + ParaMEDMEM::MEDCouplingUMesh *ReadUMeshFromFile(const char *fileName, const char *meshName=0, int meshDimRelToMax=0) throw(INTERP_KERNEL::Exception); } -class DEC { -public: - %extend { - // avoid error when SWIG creates a default constructor of an abstract class - DEC() { return NULL; }; - } - void recvData(); - void sendData(); - void synchronize(); - - void attachLocalField(FIELD* field, const char* method="P0"); - void attachLocalField(const ParaFIELD* field, const char* method="P0"); - void attachLocalField(const ICoCo::Field* field, const char* method="P0"); - - void renormalizeTargetField(); -}; - - -typedef enum{WithoutTimeInterp,LinearTimeInterp} TimeInterpolationMethod; - -typedef enum{Native,PointToPoint} AllToAllMethod; - -class IntersectionDEC: public DEC { -public: - - IntersectionDEC(ProcessorGroup& local_group, ProcessorGroup& distant_group); - - void recvData(); - void recvData( double time ); - - void sendData(); - void sendData( double time, double deltatime ); - - void setTimeInterpolationMethod(TimeInterpolationMethod it); - TimeInterpolationMethod getTimeInterpolationMethod(); - - void setAsynchronous( bool dr); - bool getAsynchronous(); - - AllToAllMethod getAllToAllMethod(); - void setAllToAllMethod(AllToAllMethod sp); - - bool getForcedRenormalization(); - void setForcedRenormalization( bool dr); -}; - - - /* This object can be used only if MED_ENABLE_FVM is defined*/ #ifdef MED_ENABLE_FVM -class NonCoincidentDEC: public DEC { +class NonCoincidentDEC : public DEC +{ public: NonCoincidentDEC(ProcessorGroup& source, ProcessorGroup& target); }; #endif - -class StructuredCoincidentDEC: public DEC { -public: - StructuredCoincidentDEC(ProcessorGroup& source, ProcessorGroup& target); - void prepareSourceDE(); - void prepareTargetDE(); -}; - - -class ParaMESH { -public: - ParaMESH(driverTypes driver_type, const char* file_name, const ProcessorGroup& group); - ParaMESH(MESH& subdomain_mesh, const ProcessorGroup& proc_group, const char* name); - - void write(driverTypes driverType, const char* fileName=""); - MESH* getMesh() const; -}; - - - -class ParaSUPPORT { -public: - %extend { - // avoid error when SWIG creates a default constructor of an abstract class - ParaSUPPORT() { return NULL; } - - PyObject* getGlobalNumbering() { - const int * numIndex = self->getGlobalNumbering(); - int aSize = self->getSupport()->getNumberOfElements(999); - TYPEMAP_OUTPUT_ARRAY(numIndex, aSize, PyInt_FromLong, ParaSUPPORT::getGlobalNumbering); - } +%extend ParaMEDMEM::ParaMESH +{ + PyObject *getGlobalNumberingCell2() const + { + const int *tmp=self->getGlobalNumberingCell(); + int size=self->getCellMesh()->getNumberOfCells(); + PyObject *ret=PyList_New(size); + for(int i=0;igetGlobalNumberingFace(); + int size=self->getFaceMesh()->getNumberOfCells(); + PyObject *ret=PyList_New(size); + for(int i=0;igetGlobalNumberingNode(); + int size=self->getCellMesh()->getNumberOfNodes(); + PyObject *ret=PyList_New(size); + for(int i=0;i MPI_COMM_WORLD and MPI_COMM_SELF %typemap(in) MPI_Comm { - switch (PyInt_AsLong($input)) { - case mpi_comm_world: $1 = MPI_COMM_WORLD; break; - case mpi_comm_self: $1 = MPI_COMM_SELF; break; - default: - PyErr_SetString(PyExc_TypeError,"unexpected value of MPI_Comm"); - return NULL; - } + switch (PyInt_AsLong($input)) + { + case mpi_comm_world: $1 = MPI_COMM_WORLD; break; + case mpi_comm_self: $1 = MPI_COMM_SELF; break; + default: + PyErr_SetString(PyExc_TypeError,"unexpected value of MPI_Comm"); + return NULL; + } } // Map mpi_double and mpi_int -> MPI_DOUBLE and MPI_INT %typemap(in) MPI_Datatype { - switch (PyInt_AsLong($input)) { - case mpi_double: $1 = MPI_DOUBLE; break; - case mpi_int: $1 = MPI_INT; break; - default: - PyErr_SetString(PyExc_TypeError,"unexpected value of MPI_Datatype"); - return NULL; - } + switch (PyInt_AsLong($input)) + { + case mpi_double: $1 = MPI_DOUBLE; break; + case mpi_int: $1 = MPI_INT; break; + default: + PyErr_SetString(PyExc_TypeError,"unexpected value of MPI_Datatype"); + return NULL; + } } // The following code gets inserted into the result python file: // create needed python symbols %pythoncode %{ - MPI_COMM_WORLD = mpi_comm_world - MPI_COMM_SELF = mpi_comm_self - MPI_DOUBLE = mpi_double - MPI_INT = mpi_int +MPI_COMM_WORLD = mpi_comm_world +MPI_COMM_SELF = mpi_comm_self +MPI_DOUBLE = mpi_double +MPI_INT = mpi_int %} //============================================================================================= // ============== // MPI_Comm_size // ============== -%inline %{ PyObject* MPI_Comm_size(MPI_Comm comm) { - int res = 0; - int err = MPI_Comm_size(comm, &res); - if ( err != MPI_SUCCESS ) { - PyErr_SetString(PyExc_RuntimeError,"Erorr in MPI_Comm_size()"); - return NULL; - } - return PyInt_FromLong( res ); -} %} +%inline %{ PyObject* MPI_Comm_size(MPI_Comm comm) + { + int res = 0; + int err = MPI_Comm_size(comm, &res); + if ( err != MPI_SUCCESS ) + { + PyErr_SetString(PyExc_RuntimeError,"Erorr in MPI_Comm_size()"); + return NULL; + } + return PyInt_FromLong( res ); + } %} // ============== // MPI_Comm_rank // ============== -%inline %{ PyObject* MPI_Comm_rank(MPI_Comm comm) { - int res = 0; - int err = MPI_Comm_rank(comm, &res); - if ( err != MPI_SUCCESS ) { - PyErr_SetString(PyExc_RuntimeError,"Erorr in MPI_Comm_rank()"); - return NULL; - } - return PyInt_FromLong( res ); -} %} +%inline %{ PyObject* MPI_Comm_rank(MPI_Comm comm) + { + int res = 0; + int err = MPI_Comm_rank(comm, &res); + if ( err != MPI_SUCCESS ) + { + PyErr_SetString(PyExc_RuntimeError,"Erorr in MPI_Comm_rank()"); + return NULL; + } + return PyInt_FromLong( res ); + } + %} int MPI_Init(int *argc, char ***argv ); int MPI_Barrier(MPI_Comm comm); @@ -301,61 +196,73 @@ int MPI_Finalize(); // MPI_Bcast // ========== -%inline %{ PyObject* MPI_Bcast(PyObject* buffer, int nb, MPI_Datatype type, int root, MPI_Comm c) { - // buffer must be a list - if (!PyList_Check(buffer)) { - PyErr_SetString(PyExc_TypeError, "buffer is expected to be a list"); - return NULL; - } - // check list size - int aSize = PyList_Size(buffer); - if ( aSize != nb ) { - PyErr_SetString(PyExc_ValueError, MEDMEM::STRING("buffer is expected to be of size ")<; // Creates "int *argc, char ***argv" parameters from input list %typemap(in) (int *argc, char ***argv) { @@ -34,7 +38,7 @@ if (PyList_Check($input)) { int size = PyList_Size($input); - set< TYPE > tmpSet; + std::set< TYPE > tmpSet; for (int i=0; i < size; i++) { @@ -52,10 +56,10 @@ } %enddef -%typemap(in) set +%typemap(in) std::set { TYPEMAP_INPUT_SET_BY_VALUE( int ) } -%typecheck(SWIG_TYPECHECK_POINTER) set { +%typecheck(SWIG_TYPECHECK_POINTER) std::set { $1 = PyList_Check($input) ? 1 : 0; } diff --git a/src/ParaMEDMEM_Swig/test_IntersectionDEC.py b/src/ParaMEDMEM_Swig/test_IntersectionDEC.py index c36cb6cf0..0b0c070d8 100755 --- a/src/ParaMEDMEM_Swig/test_IntersectionDEC.py +++ b/src/ParaMEDMEM_Swig/test_IntersectionDEC.py @@ -18,131 +18,101 @@ # # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com # -from ParaMEDMEM import * +from libParaMEDMEM_Swig import * import sys, os - -# testIntersectionDEC_2D - -MPI_Init(sys.argv) - -size = MPI_Comm_size(MPI_COMM_WORLD) -rank = MPI_Comm_rank(MPI_COMM_WORLD) -if size != 5: - raise RuntimeError, "Expect MPI_COMM_WORLD size == 5" - -nproc_source = 3 -procs_source = range( nproc_source ) -procs_target = range( size - nproc_source + 1, size) - -interface = CommInterface() - -target_group = MPIProcessorGroup(interface, procs_target) -source_group = MPIProcessorGroup(interface, procs_source) - -dec = IntersectionDEC(source_group, target_group) - -mesh =0 -support =0 -paramesh =0 -parafield =0 -parasupport=0 -icocofield =0 - -data_dir = os.environ['MED_ROOT_DIR'] -tmp_dir = os.environ['TMP'] - -if not tmp_dir or len(tmp_dir)==0: - tmp_dir = "/tmp" +import unittest +import math + +class ParaMEDMEMBasicsTest(unittest.TestCase): + def testIntersectionDEC_2D(self): + MPI_Init(sys.argv) + size = MPI_Comm_size(MPI_COMM_WORLD) + rank = MPI_Comm_rank(MPI_COMM_WORLD) + if size != 5: + raise RuntimeError, "Expect MPI_COMM_WORLD size == 5" + print rank + nproc_source = 3 + procs_source = range( nproc_source ) + procs_target = range( size - nproc_source + 1, size) + + interface = CommInterface() + target_group = MPIProcessorGroup(interface, procs_target) + source_group = MPIProcessorGroup(interface, procs_source) + dec = IntersectionDEC(source_group, target_group) + + mesh =0 + support =0 + paramesh =0 + parafield =0 + icocofield =0 + data_dir = os.environ['MED_ROOT_DIR'] + tmp_dir = os.environ['TMP'] + + if not tmp_dir or len(tmp_dir)==0: + tmp_dir = "/tmp" + pass + + filename_xml1 = os.path.join(data_dir, "share/salome/resources/med/square1_split") + filename_xml2 = os.path.join(data_dir, "share/salome/resources/med/square2_split") + + MPI_Barrier(MPI_COMM_WORLD) + if source_group.containsMyRank(): + filename = filename_xml1 + str(rank+1) + ".med" + meshname = "Mesh_2_" + str(rank+1) + mesh=ReadUMeshFromFile(filename,meshname,0) + paramesh=ParaMESH(mesh,source_group,"source mesh") + comptopo = ComponentTopology() + parafield = ParaFIELD(ON_CELLS,paramesh, comptopo) + nb_local=mesh.getNumberOfCells() + value = [1.0]*nb_local + parafield.getField().setValues(value) + icocofield = ICoCoMEDField(paramesh,parafield) + dec.setMethod("P0") + dec.attachLocalField(icocofield) + pass + else: + filename = filename_xml2 + str(rank - nproc_source + 1) + ".med" + meshname = "Mesh_3_" + str(rank - nproc_source + 1) + mesh=ReadUMeshFromFile(filename,meshname,0) + paramesh=ParaMESH(mesh,target_group,"target mesh") + comptopo = ComponentTopology() + parafield = ParaFIELD(ON_CELLS,paramesh, comptopo) + nb_local=mesh.getNumberOfCells() + value = [0.0]*nb_local + parafield.getField().setValues(value) + icocofield = ICoCoMEDField(paramesh,parafield) + dec.setMethod("P0") + dec.attachLocalField(icocofield) + pass + + if source_group.containsMyRank(): + field_before_int = parafield.getVolumeIntegral(0) + dec.synchronize() + dec.setForcedRenormalization(False) + dec.sendData() + dec.recvData() + field_after_int=parafield.getVolumeIntegral(0); + self.failUnless(math.fabs(field_after_int-field_before_int)<1e-8) + pass + else: + dec.synchronize() + dec.setForcedRenormalization(False) + dec.recvData() + dec.sendData() + pass + ## end + interface = 0 + target_group = 0 + source_group = 0 + dec = 0 + mesh =0 + support =0 + paramesh =0 + parafield =0 + icocofield =0 + MPI_Barrier(MPI_COMM_WORLD) + MPI_Finalize() + pass pass -filename_xml1 = os.path.join(data_dir, "share/salome/resources/med/square1_split") -filename_xml2 = os.path.join(data_dir, "share/salome/resources/med/square2_split") - -MPI_Barrier(MPI_COMM_WORLD) - -if source_group.containsMyRank(): - - filename = filename_xml1 + str(rank+1) + ".med" - meshname = "Mesh_2_" + str(rank+1) - - mesh = MESH(MED_DRIVER, filename, meshname) - support = SUPPORT(mesh, "all elements", MED_CELL) - paramesh = ParaMESH(mesh, source_group, "source mesh") - - parasupport = UnstructuredParaSUPPORT( support, source_group) - comptopo = ComponentTopology() - - parafield = ParaFIELD(parasupport, comptopo) - - nb_local = support.getNumberOfElements(MED_ALL_ELEMENTS); - - value = [1.0]*nb_local - - parafield.getField().setValue(value) - icocofield = ICoCo_MEDField(paramesh,parafield) - dec.attachLocalField(icocofield,'P0') - pass - -if target_group.containsMyRank(): - - filename = filename_xml2 + str(rank - nproc_source + 1) + ".med" - meshname = "Mesh_3_" + str(rank - nproc_source + 1) - - mesh = MESH(MED_DRIVER, filename, meshname) - support = SUPPORT(mesh, "all elements", MED_CELL) - paramesh = ParaMESH(mesh, target_group, "target mesh") - - parasupport = UnstructuredParaSUPPORT( support, target_group) - comptopo = ComponentTopology() - parafield = ParaFIELD(parasupport, comptopo) - - nb_local = support.getNumberOfElements(MED_ALL_ELEMENTS) - value = [0.0]*nb_local - - parafield.getField().setValue(value) - icocofield = ICoCo_MEDField(paramesh,parafield) - - dec.attachLocalField(icocofield, 'P0') - pass - -if source_group.containsMyRank(): - field_before_int = parafield.getVolumeIntegral(1) - dec.synchronize() - print "DEC usage" - dec.setForcedRenormalization(False) - - dec.sendData() - print "writing 1" - paramesh.write(MED_DRIVER,"./sourcesquareb"); - filename = "./sourcesquareb_" + str(source_group.myRank()+1) - parafield.write(MED_DRIVER, "./sourcesquareb", "boundary"); - - print "b dec.recvData()" - dec.recvData() - print "writing 2" - paramesh.write(MED_DRIVER, "./sourcesquare") - parafield.write(MED_DRIVER, "./sourcesquare", "boundary") - - filename = "./sourcesquare_" + str(source_group.myRank()+1) - field_after_int = parafield.getVolumeIntegral(1) - print "field_before_int", field_before_int,"field_after_int", field_after_int - - pass - -if target_group.containsMyRank(): - dec.synchronize() - print "TARGET: after dec.synchronize()" - dec.setForcedRenormalization(False) - - dec.recvData() - paramesh.write(0, "./targetsquareb") - parafield.write(0, "./targetsquareb", "boundary") - dec.sendData(); - paramesh.write(0, "./targetsquare") - parafield.write(0, "./targetsquare", "boundary") - pass - - -MPI_Barrier(MPI_COMM_WORLD) -MPI_Finalize() -print "### End of testIntersectionDEC_2D" +unittest.main() diff --git a/src/ParaMEDMEM_Swig/test_StructuredCoincodentDEC.py b/src/ParaMEDMEM_Swig/test_StructuredCoincodentDEC.py index c60831c1f..fea118e61 100755 --- a/src/ParaMEDMEM_Swig/test_StructuredCoincodentDEC.py +++ b/src/ParaMEDMEM_Swig/test_StructuredCoincodentDEC.py @@ -18,95 +18,108 @@ # # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com # -from ParaMEDMEM import * +from libParaMEDMEM_Swig import * import sys, os +import unittest +import math + +class ParaMEDMEMBasicsTest2(unittest.TestCase): + def testStructuredCoincidentDEC(self): + MPI_Init(sys.argv) + # + size = MPI_Comm_size(MPI_COMM_WORLD) + rank = MPI_Comm_rank(MPI_COMM_WORLD) + # + if size < 4: + raise RuntimeError, "Expect MPI_COMM_WORLD size >= 4" + # + interface = CommInterface() + # + self_group = MPIProcessorGroup(interface, rank, rank) + target_group = MPIProcessorGroup(interface, 3, size-1) + source_group = MPIProcessorGroup(interface, 0, 2) + # + mesh = 0 + support = 0 + paramesh = 0 + parafield = 0 + comptopo = 0 + icocofield= 0 + # + data_dir = os.environ['MED_ROOT_DIR'] + tmp_dir = os.environ['TMP'] + if tmp_dir == '': + tmp_dir = "/tmp" + pass + + filename_xml1 = data_dir + "/share/salome/resources/med/square1_split" + filename_2 = data_dir + "/share/salome/resources/med/square1.med" + filename_seq_wr = tmp_dir + "/" + filename_seq_med = tmp_dir + "/myWrField_seq_pointe221.med" + + dec = StructuredCoincidentDEC(source_group, target_group) + MPI_Barrier(MPI_COMM_WORLD) + if source_group.containsMyRank(): + filename = filename_xml1 + str(rank+1) + ".med" + meshname = "Mesh_2_" + str(rank+1) + mesh=ReadUMeshFromFile(filename,meshname,0) + paramesh=ParaMESH(mesh,source_group,"source mesh") + comptopo=ComponentTopology(6) + parafield=ParaFIELD(ON_CELLS,paramesh,comptopo) + nb_local=mesh.getNumberOfCells() + global_numbering=paramesh.getGlobalNumberingCell2() + value = [] + for ielem in range(nb_local): + for icomp in range(6): + value.append(global_numbering[ielem]*6.0+icomp); + pass + pass + parafield.getField().setValues(value) + icocofield = ICoCoMEDField(paramesh,parafield) + dec.setMethod("P0") + dec.attachLocalField(icocofield) + dec.synchronize() + dec.sendData() + pass -MPI_Init(sys.argv) - -size = MPI_Comm_size(MPI_COMM_WORLD) -rank = MPI_Comm_rank(MPI_COMM_WORLD) - -if size < 4: - raise RuntimeError, "Expect MPI_COMM_WORLD size >= 4" - -interface = CommInterface() - -self_group = MPIProcessorGroup(interface, rank, rank) -target_group = MPIProcessorGroup(interface, 3, size-1) -source_group = MPIProcessorGroup(interface, 0, 2) - -mesh = 0 -support = 0 -paramesh = 0 -parafield = 0 - -data_dir = os.environ['MED_ROOT_DIR'] -tmp_dir = os.environ['TMP'] -if tmp_dir == '': - tmp_dir = "/tmp" - pass - -filename_xml1 = data_dir + "/share/salome/resources/med/square1_split" -filename_2 = data_dir + "/share/salome/resources/med/square1.med" -filename_seq_wr = tmp_dir + "/" -filename_seq_med = tmp_dir + "/myWrField_seq_pointe221.med" - -dec = StructuredCoincidentDEC(source_group, target_group) -MPI_Barrier(MPI_COMM_WORLD) - -if source_group.containsMyRank(): - filename = filename_xml1 + str(rank+1) + ".med" - meshname = "Mesh_2_" + str(rank+1) - - mesh = MESH(MED_DRIVER, filename, meshname) - support = SUPPORT(mesh, "all elements", MED_CELL) - paramesh = ParaMESH(mesh, source_group, "source mesh") - - parasupport = UnstructuredParaSUPPORT( support, source_group); - comptopo = ComponentTopology(6) - parafield = ParaFIELD(parasupport, comptopo) - - nb_local = support.getNumberOfElements(MED_ALL_ELEMENTS); - global_numbering = parasupport.getGlobalNumbering() - - value = [] - for ielem in range(nb_local): - for icomp in range(6): - value.append(global_numbering[ielem]*6.0+icomp); + if target_group.containsMyRank(): + meshname2 = "Mesh_2" + mesh=ReadUMeshFromFile(filename_2, meshname2,0) + paramesh=ParaMESH(mesh, self_group, "target mesh") + comptopo=ComponentTopology(6,target_group) + parafield=ParaFIELD(ON_CELLS,paramesh, comptopo) + nb_local=mesh.getNumberOfCells() + value = [0.0]*(nb_local*comptopo.nbLocalComponents()) + parafield.getField().setValues(value) + icocofield = ICoCoMEDField(paramesh,parafield) + dec.setMethod("P0") + dec.attachLocalField(icocofield) + dec.synchronize() + dec.recvData() + recv_value = parafield.getField().getArray().getValues() + for i in range(nb_local): + first=comptopo.firstLocalComponent() + for icomp in range(comptopo.nbLocalComponents()): + self.failUnless(math.fabs(recv_value[i*comptopo.nbLocalComponents()+icomp]- + (float)(i*6+icomp+first))<1e-12) + pass + pass pass + comptopo=0 + interface = 0 + mesh =0 + support =0 + paramesh =0 + parafield =0 + icocofield =0 + dec=0 + self_group =0 + target_group = 0 + source_group = 0 + MPI_Barrier(MPI_COMM_WORLD) + MPI_Finalize() + print "End of test StructuredCoincidentDEC" pass - parafield.getField().setValue(value) - icocofield = ICoCo_MEDField(paramesh,parafield) - dec.attachLocalField(icocofield) - dec.synchronize() - dec.sendData() - pass - -if target_group.containsMyRank(): - - meshname2 = "Mesh_2" - mesh = MESH(MED_DRIVER, filename_2, meshname2) - support = SUPPORT(mesh, "all elements", MED_CELL) - - paramesh = ParaMESH(mesh, self_group, "target mesh") - parasupport = UnstructuredParaSUPPORT( support, self_group) - comptopo = ComponentTopology(6, target_group) - parafield = ParaFIELD(parasupport, comptopo) - - nb_local = support.getNumberOfElements(MED_ALL_ELEMENTS) - value = [0.0]*(nb_local*comptopo.nbLocalComponents()) - - parafield.getField().setValue(value) - icocofield = ICoCo_MEDField(paramesh,parafield) - - dec.attachLocalField(icocofield) - dec.synchronize() - dec.recvData() - - recv_value = parafield.getField().getValue() - pass - -MPI_Finalize() - -print "End of test StructuredCoincidentDEC" + +unittest.main()