From 7a8f178c82eb2ffcffbb11ab524b938f67d84feb Mon Sep 17 00:00:00 2001 From: ageay Date: Thu, 11 Apr 2013 13:36:34 +0000 Subject: [PATCH] MEDCouplingFieldDouble::buildSubPartRange --- .../MEDCouplingFieldDiscretization.cxx | 9 +- src/MEDCoupling/MEDCouplingPointSet.cxx | 5 + src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 133 ++++++++++++++++++ src/MEDCoupling_Swig/MEDCouplingCommon.i | 69 ++++++++- 4 files changed, 208 insertions(+), 8 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingFieldDiscretization.cxx b/src/MEDCoupling/MEDCouplingFieldDiscretization.cxx index 4cedfe1a0..1c99da1c2 100644 --- a/src/MEDCoupling/MEDCouplingFieldDiscretization.cxx +++ b/src/MEDCoupling/MEDCouplingFieldDiscretization.cxx @@ -778,9 +778,12 @@ MEDCouplingMesh *MEDCouplingFieldDiscretizationOnNodes::buildSubMeshDataRange(co throw INTERP_KERNEL::Exception("MEDCouplingFieldDiscretizationOnNodes::buildSubMeshDataRange : NULL input mesh !"); DataArrayInt *diTmp=0; MEDCouplingAutoRefCountObjectPtr ret=mesh->buildPartRangeAndReduceNodes(beginCellIds,endCellIds,stepCellIds,beginOut,endOut,stepOut,diTmp); - MEDCouplingAutoRefCountObjectPtr diTmpSafe(diTmp); - MEDCouplingAutoRefCountObjectPtr di2=diTmpSafe->invertArrayO2N2N2O(ret->getNumberOfNodes()); - di=di2.retn(); + if(diTmp) + { + MEDCouplingAutoRefCountObjectPtr diTmpSafe(diTmp); + MEDCouplingAutoRefCountObjectPtr di2=diTmpSafe->invertArrayO2N2N2O(ret->getNumberOfNodes()); + di=di2.retn(); + } return ret.retn(); } diff --git a/src/MEDCoupling/MEDCouplingPointSet.cxx b/src/MEDCoupling/MEDCouplingPointSet.cxx index 38e9f1f0b..7cad8e079 100644 --- a/src/MEDCoupling/MEDCouplingPointSet.cxx +++ b/src/MEDCoupling/MEDCouplingPointSet.cxx @@ -1062,6 +1062,11 @@ MEDCouplingMesh *MEDCouplingPointSet::buildPartRange(int beginCellIds, int endCe /*! * This method specialized the MEDCouplingMesh::buildPartRangeAndReduceNodes * + * \param [out] beginOut valid only if \a arr not NULL ! + * \param [out] endOut valid only if \a arr not NULL ! + * \param [out] stepOut valid only if \a arr not NULL ! + * \param [out] arr correspondance old to new in node ids. + * * \sa MEDCouplingUMesh::buildPartOfMySelf2 */ MEDCouplingMesh *MEDCouplingPointSet::buildPartRangeAndReduceNodes(int beginCellIds, int endCellIds, int stepCellIds, int& beginOut, int& endOut, int& stepOut, DataArrayInt*& arr) const throw(INTERP_KERNEL::Exception) diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index e4f9c8d56..c758d43ba 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -12382,6 +12382,139 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertTrue(ff.getArray().isEqual(DataArrayDouble([(1,0.25),(1,0.44444444444444442),(1,0.59999999999999998),(1,0.72727272727272729),(1,0.83333333333333337)]),1e-12)) self.assertRaises(InterpKernelException,f.__rdiv__,f2) pass + + def testSwig2FieldDoubleBuildSubPartRange1(self): + #ON_CELLS + m=MEDCouplingDataForTest.build2DTargetMesh_1() + f=MEDCouplingFieldDouble(ON_CELLS) + f.setMesh(m) + arr=DataArrayDouble(5,2) ; arr[:,0]=range(7,12) ; arr[:,1]=100+arr[:,0] + f.setArray(arr) + f.checkCoherency() + ff=f[1:-1:2] + ff.checkCoherency() + self.assertTrue((m.buildPartOfMySelf([1,3],True)).isEqual(ff.getMesh(),1e-12)) + self.assertTrue(9,ff.getMesh().getNumberOfNodes()) + self.assertTrue(2,ff.getMesh().getNumberOfCells()) + self.assertTrue(ff.getArray().isEqual(arr[[1,3]],1e-12)) + # + a,b=f.buildSubMeshDataRange(2,5,1) + self.assertTrue(m.buildPartOfMySelf([2,3,4],True).isEqual(a,1e-12)) + self.assertEqual(b,slice(2,5,1)) + ff=f[2:] + ff.checkCoherency() + self.assertTrue((m.buildPartOfMySelf([2,3,4],True)).isEqual(ff.getMesh(),1e-12)) + self.assertTrue(9,ff.getMesh().getNumberOfNodes()) + self.assertTrue(3,ff.getMesh().getNumberOfCells()) + self.assertTrue(ff.getArray().isEqual(arr[[2,3,4]],1e-12)) + # + ff=f[-2:0:-1] + ff.checkCoherency() + self.assertTrue((m.buildPartOfMySelf([3,2,1],True)).isEqual(ff.getMesh(),1e-12)) + self.assertTrue(9,ff.getMesh().getNumberOfNodes()) + self.assertTrue(3,ff.getMesh().getNumberOfCells()) + self.assertTrue(ff.getArray().isEqual(arr[[3,2,1]],1e-12)) + self.assertTrue(f[-2:0:-1,1].getArray().isEqual(arr[[3,2,1],1],1e-12)) + #ON_NODES + f=MEDCouplingFieldDouble(ON_NODES) + f.setMesh(m) + arr=DataArrayDouble(9,2) ; arr[:,0]=range(7,16) ; arr[:,1]=100+arr[:,0] + f.setArray(arr) + f.checkCoherency() + ff=f[1:-1:2] + ff.checkCoherency() + self.assertTrue((m.buildPartOfMySelf([1,3],False)).isEqual(ff.getMesh(),1e-12)) + self.assertTrue(6,ff.getMesh().getNumberOfNodes()) + self.assertTrue(2,ff.getMesh().getNumberOfCells()) + self.assertTrue(ff.getArray().isEqual(arr[[1,2,3,4,6,7]],1e-12)) + # + m2=m.buildPartRange(2,5,1) + self.assertTrue(m.buildPartOfMySelf([2,3,4],True).isEqual(m2,1e-12)) + m2,b=m.buildPartRangeAndReduceNodes(2,5,1) + self.assertTrue(m.buildPartOfMySelf([2,3,4],False).isEqual(m2,1e-12)) + self.assertTrue(b.isEqual(DataArrayInt([-1,-1,0,1,2,3,4,5,6]))) + a,b=f.buildSubMeshDataRange(2,5,1) + self.assertTrue(m.buildPartOfMySelf([2,3,4],False).isEqual(a,1e-12)) + self.assertTrue(b.isEqual(DataArrayInt([2,3,4,5,6,7,8]))) + ff=f[2:] + ff.checkCoherency() + self.assertTrue((m.buildPartOfMySelf([2,3,4],False)).isEqual(ff.getMesh(),1e-12)) + self.assertTrue(7,ff.getMesh().getNumberOfNodes()) + self.assertTrue(3,ff.getMesh().getNumberOfCells()) + self.assertTrue(ff.getArray().isEqual(arr[[2,3,4,5,6,7,8]],1e-12)) + # + ff=f[-2:0:-1] + ff.checkCoherency() + self.assertTrue((m.buildPartOfMySelf([3,2,1],False)).isEqual(ff.getMesh(),1e-12)) + self.assertTrue(7,ff.getMesh().getNumberOfNodes()) + self.assertTrue(3,ff.getMesh().getNumberOfCells()) + self.assertTrue(ff.getArray().isEqual(arr[[1,2,3,4,5,6,7]],1e-12)) + self.assertTrue(f[-2:0:-1,1].getArray().isEqual(arr[[1,2,3,4,5,6,7],1],1e-12)) + #ON_GAUSS_NE + f=MEDCouplingFieldDouble(ON_GAUSS_NE) + f.setMesh(m) + arr=DataArrayDouble(18,2) ; arr[:,0]=range(7,25) ; arr[:,1]=100+arr[:,0] + f.setArray(arr) + f.checkCoherency() + ff=f[1:-1:2] + ff.checkCoherency() + self.assertTrue((m.buildPartOfMySelf([1,3],True)).isEqual(ff.getMesh(),1e-12)) + self.assertTrue(9,ff.getMesh().getNumberOfNodes()) + self.assertTrue(2,ff.getMesh().getNumberOfCells()) + self.assertTrue(ff.getArray().isEqual(arr[[4,5,6,10,11,12,13]],1e-12)) + # + a,b=f.buildSubMeshDataRange(2,5,1) + self.assertTrue(m.buildPartOfMySelf([2,3,4],True).isEqual(a,1e-12)) + self.assertEqual(b,slice(7,18,1)) + ff=f[2:] + ff.checkCoherency() + self.assertTrue((m.buildPartOfMySelf([2,3,4],True)).isEqual(ff.getMesh(),1e-12)) + self.assertTrue(9,ff.getMesh().getNumberOfNodes()) + self.assertTrue(3,ff.getMesh().getNumberOfCells()) + self.assertTrue(ff.getArray().isEqual(arr[[7,8,9,10,11,12,13,14,15,16,17]],1e-12)) + # + ff=f[-2:0:-1] + ff.checkCoherency() + self.assertTrue((m.buildPartOfMySelf([3,2,1],True)).isEqual(ff.getMesh(),1e-12)) + self.assertTrue(9,ff.getMesh().getNumberOfNodes()) + self.assertTrue(3,ff.getMesh().getNumberOfCells()) + self.assertTrue(ff.getArray().isEqual(arr[[10,11,12,13,7,8,9,4,5,6]],1e-12)) + self.assertTrue(f[-2:0:-1,1].getArray().isEqual(arr[[10,11,12,13,7,8,9,4,5,6],1],1e-12)) + #ON_GAUSS_PT + f=MEDCouplingFieldDouble(ON_GAUSS_PT) + f.setMesh(m) + f.setGaussLocalizationOnCells([0,4],[0,0,1,0,1,1,1,0],[1.1,1.1,2.2,2.2],[0.2,0.8]); + f.setGaussLocalizationOnCells([3],[0,0,1,0,1,1,1,0],[1.1,1.1,2.2,2.2,3.,3.],[0.2,0.4,0.4]); + f.setGaussLocalizationOnCells([1],[0,0,1,0,1,0],[1.1,1.1,2.2,2.2,3.,3.,4.,4.],[0.1,0.1,0.4,0.4]); + f.setGaussLocalizationOnCells([2],[0,0,1,0,1,0],[1.1,1.1,2.2,2.2,3.,3.,4.,4.,5.,5.],[0.1,0.1,0.4,0.3,0.1]); + arr=DataArrayDouble(16,2) ; arr[:,0]=range(7,23) ; arr[:,1]=100+arr[:,0] + f.setArray(arr) + f.checkCoherency() + ff=f[1:-1:2] + ff.checkCoherency() + self.assertTrue((m.buildPartOfMySelf([1,3],True)).isEqual(ff.getMesh(),1e-12)) + self.assertTrue(9,ff.getMesh().getNumberOfNodes()) + self.assertTrue(2,ff.getMesh().getNumberOfCells()) + self.assertTrue(ff.getArray().isEqual(arr[[2,3,4,5,11,12,13]],1e-12)) + # + a,b=f.buildSubMeshDataRange(2,5,1) + self.assertTrue(m.buildPartOfMySelf([2,3,4],True).isEqual(a,1e-12)) + self.assertEqual(b,slice(6,16,1)) + ff=f[2:] + ff.checkCoherency() + self.assertTrue((m.buildPartOfMySelf([2,3,4],True)).isEqual(ff.getMesh(),1e-12)) + self.assertTrue(9,ff.getMesh().getNumberOfNodes()) + self.assertTrue(3,ff.getMesh().getNumberOfCells()) + self.assertTrue(ff.getArray().isEqual(arr[[6,7,8,9,10,11,12,13,14,15]],1e-12)) + # + ff=f[-2:0:-1] + ff.checkCoherency() + self.assertTrue((m.buildPartOfMySelf([3,2,1],True)).isEqual(ff.getMesh(),1e-12)) + self.assertTrue(9,ff.getMesh().getNumberOfNodes()) + self.assertTrue(3,ff.getMesh().getNumberOfCells()) + self.assertTrue(ff.getArray().isEqual(arr[[11,12,13,6,7,8,9,10,2,3,4,5]],1e-12)) + self.assertTrue(f[-2:0:-1,0].getArray().isEqual(arr[[11,12,13,6,7,8,9,10,2,3,4,5],0],1e-12)) + pass def setUp(self): pass diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index 5874ed8ab..309b8a84e 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -93,6 +93,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::MEDCouplingFieldDiscretization::deepCpy; %newobject ParaMEDMEM::MEDCouplingFieldDiscretization::clone; %newobject ParaMEDMEM::MEDCouplingFieldDiscretization::clonePart; +%newobject ParaMEDMEM::MEDCouplingFieldDiscretization::clonePartRange; %newobject ParaMEDMEM::MEDCouplingFieldDiscretization::getMeasureField; %newobject ParaMEDMEM::MEDCouplingFieldDiscretization::getOffsetArr; %newobject ParaMEDMEM::MEDCouplingFieldDiscretization::getLocalizationOfDiscValues; @@ -133,6 +134,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::MEDCouplingFieldDouble::negate; %newobject ParaMEDMEM::MEDCouplingFieldDouble::getIdsInRange; %newobject ParaMEDMEM::MEDCouplingFieldDouble::buildSubPart; +%newobject ParaMEDMEM::MEDCouplingFieldDouble::buildSubPartRange; %newobject ParaMEDMEM::MEDCouplingFieldDouble::__getitem__; %newobject ParaMEDMEM::MEDCouplingFieldDouble::__neg__; %newobject ParaMEDMEM::MEDCouplingFieldDouble::__add__; @@ -300,6 +302,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::MEDCouplingMesh::checkDeepEquivalOnSameNodesWith; %newobject ParaMEDMEM::MEDCouplingMesh::checkTypeConsistencyAndContig; %newobject ParaMEDMEM::MEDCouplingMesh::computeNbOfNodesPerCell; +%newobject ParaMEDMEM::MEDCouplingMesh::buildPartRange; %newobject ParaMEDMEM::MEDCouplingMesh::giveCellsWithType; %newobject ParaMEDMEM::MEDCouplingMesh::getCoordinatesAndOwner; %newobject ParaMEDMEM::MEDCouplingMesh::getBarycenterAndOwner; @@ -554,6 +557,7 @@ namespace ParaMEDMEM virtual DataArrayDouble *computeIsoBarycenterOfNodesPerCell() const throw(INTERP_KERNEL::Exception); virtual DataArrayInt *giveCellsWithType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception); virtual DataArrayInt *computeNbOfNodesPerCell() const throw(INTERP_KERNEL::Exception); + virtual MEDCouplingMesh *buildPartRange(int beginCellIds, int endCellIds, int stepCellIds) const throw(INTERP_KERNEL::Exception); virtual int getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception); virtual INTERP_KERNEL::NormalizedCellType getTypeOfCell(int cellId) const throw(INTERP_KERNEL::Exception); virtual std::string simpleRepr() const throw(INTERP_KERNEL::Exception); @@ -844,6 +848,23 @@ namespace ParaMEDMEM return res; } + PyObject *buildPartRangeAndReduceNodes(int beginCellIds, int endCellIds, int stepCellIds) const throw(INTERP_KERNEL::Exception) + { + int a,b,c; + DataArrayInt *arr=0; + MEDCouplingMesh *ret=self->buildPartRangeAndReduceNodes(beginCellIds,endCellIds,stepCellIds,a,b,c,arr); + PyObject *res = PyTuple_New(2); + PyObject *obj0=convertMesh(ret, SWIG_POINTER_OWN | 0 ); + PyObject *obj1=0; + if(arr) + obj1=SWIG_NewPointerObj(SWIG_as_voidptr(arr),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ); + else + obj1=PySlice_New(PyInt_FromLong(a),PyInt_FromLong(b),PyInt_FromLong(b)); + PyTuple_SetItem(res,0,obj0); + PyTuple_SetItem(res,1,obj1); + return res; + } + PyObject *getDistributionOfTypes() const throw(INTERP_KERNEL::Exception) { std::vector vals=self->getDistributionOfTypes(); @@ -955,6 +976,7 @@ namespace ParaMEDMEM %include "MEDCouplingFieldDiscretization.hxx" %ignore ParaMEDMEM::MEDCouplingFieldDiscretization::clonePart; +%ignore ParaMEDMEM::MEDCouplingFieldDiscretization::buildSubMeshDataRange; %ignore ParaMEDMEM::MEDCouplingFieldDiscretizationPerCell::getArrayOfDiscIds; namespace ParaMEDMEM @@ -2586,6 +2608,23 @@ namespace ParaMEDMEM const int *inp=convertObjToPossibleCpp1_Safe(li,sw,sz,val1,val2); return self->clonePart(inp,inp+sz); } + + PyObject *buildSubMeshDataRange(const MEDCouplingMesh *mesh, int beginCellIds, int endCellIds, int stepCellIds, int& beginOut, int& endOut, int& stepOut, DataArrayInt *&di) const throw(INTERP_KERNEL::Exception) + { + DataArrayInt *ret1=0; + int bb,ee,ss; + MEDCouplingMesh *ret0=self->buildSubMeshDataRange(mesh,begin,end,step,bb,ee,ss,ret1); + PyObject *res=PyTuple_New(2); + PyTuple_SetItem(res,0,convertMesh(ret0, SWIG_POINTER_OWN | 0 )); + if(ret1) + PyTuple_SetItem(res,1,SWIG_NewPointerObj((void*)ret1,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,SWIG_POINTER_OWN | 0)); + else + { + PyObject *res1=PySlice_New(PyInt_FromLong(bb),PyInt_FromLong(ee),PyInt_FromLong(ss)); + PyTuple_SetItem(res,1,res1); + } + return res; + } PyObject *computeMeshRestrictionFromTupleIds(const MEDCouplingMesh *mesh, PyObject *tupleIds) const throw(INTERP_KERNEL::Exception) { @@ -2761,6 +2800,23 @@ namespace ParaMEDMEM return res; } + PyObject *buildSubMeshDataRange(int begin, int end, int step) const throw(INTERP_KERNEL::Exception) + { + DataArrayInt *ret1=0; + int bb,ee,ss; + MEDCouplingMesh *ret0=self->buildSubMeshDataRange(begin,end,step,bb,ee,ss,ret1); + PyObject *res=PyTuple_New(2); + PyTuple_SetItem(res,0,convertMesh(ret0, SWIG_POINTER_OWN | 0 )); + if(ret1) + PyTuple_SetItem(res,1,SWIG_NewPointerObj((void*)ret1,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,SWIG_POINTER_OWN | 0)); + else + { + PyObject *res1=PySlice_New(PyInt_FromLong(bb),PyInt_FromLong(ee),PyInt_FromLong(ss)); + PyTuple_SetItem(res,1,res1); + } + return res; + } + DataArrayInt *computeTupleIdsToSelectFromCellIds(PyObject *li) const { int sw; @@ -2935,6 +2991,7 @@ namespace ParaMEDMEM double normL1(int compId) const throw(INTERP_KERNEL::Exception); double normL2(int compId) const throw(INTERP_KERNEL::Exception); DataArrayInt *getIdsInRange(double vmin, double vmax) const throw(INTERP_KERNEL::Exception); + MEDCouplingFieldDouble *buildSubPartRange(int begin, int end, int step) const throw(INTERP_KERNEL::Exception); static MEDCouplingFieldDouble *MergeFields(const MEDCouplingFieldDouble *f1, const MEDCouplingFieldDouble *f2) throw(INTERP_KERNEL::Exception); static MEDCouplingFieldDouble *MeldFields(const MEDCouplingFieldDouble *f1, const MEDCouplingFieldDouble *f2) throw(INTERP_KERNEL::Exception); static MEDCouplingFieldDouble *DotFields(const MEDCouplingFieldDouble *f1, const MEDCouplingFieldDouble *f2) throw(INTERP_KERNEL::Exception); @@ -3270,8 +3327,7 @@ namespace ParaMEDMEM } case 3: { - MEDCouplingAutoRefCountObjectPtr rg=DataArrayInt::Range(slic.first,slic.second.first,slic.second.second); - return self->buildSubPart(rg->begin(),rg->end()); + return self->buildSubPartRange(slic.first,slic.second.first,slic.second.second); } case 4: { @@ -3314,12 +3370,14 @@ namespace ParaMEDMEM case 1: { std::vector v2(1,singleVal); - ret0->setArray(ret0Arr->keepSelectedComponents(v2)); + MEDCouplingAutoRefCountObjectPtr aarr=ret0Arr->keepSelectedComponents(v2); + ret0->setArray(aarr); return ret0.retn(); } case 2: { - ret0->setArray(ret0Arr->keepSelectedComponents(multiVal)); + MEDCouplingAutoRefCountObjectPtr aarr=ret0Arr->keepSelectedComponents(multiVal); + ret0->setArray(aarr); return ret0.retn(); } case 3: @@ -3328,7 +3386,8 @@ namespace ParaMEDMEM std::vector v2(nbOfComp); for(int i=0;isetArray(ret0Arr->keepSelectedComponents(v2)); + MEDCouplingAutoRefCountObjectPtr aarr=ret0Arr->keepSelectedComponents(v2); + ret0->setArray(aarr); return ret0.retn(); } default: -- 2.39.2