From: Anthony Geay Date: Tue, 30 Mar 2021 14:05:37 +0000 (+0200) Subject: Add MEDCouplingMesh.computeMeshCenterOfMass X-Git-Tag: V9_7_0a1~6 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=d582668bb1a7780f07bef756d113cad9f4b52bab;p=tools%2Fmedcoupling.git Add MEDCouplingMesh.computeMeshCenterOfMass --- diff --git a/src/MEDCoupling/MEDCouplingMesh.cxx b/src/MEDCoupling/MEDCouplingMesh.cxx index 6de0b97a7..58b557348 100755 --- a/src/MEDCoupling/MEDCouplingMesh.cxx +++ b/src/MEDCoupling/MEDCouplingMesh.cxx @@ -674,6 +674,22 @@ void MEDCouplingMesh::getCellsContainingPointsLinearPartOnlyOnNonDynType(const d this->getCellsContainingPoints(pos,nbOfPoints,eps,elts,eltsIndex); } +/*! + * Method computing center of mass of whole mesh (\a this) + * \return DataArrayDouble * - a new instance of DataArrayDouble with one tuple of n components where n is space dimension + */ +MCAuto MEDCouplingMesh::computeMeshCenterOfMass() const +{ + MCAuto cellCenters( this->computeCellCenterOfMass() ); + MCAuto vol( this->getMeasureField(true) ); + MCAuto volXCenter( DataArrayDouble::Multiply(cellCenters,vol->getArray()) ); + MCAuto ret(DataArrayDouble::New()); ret->alloc(1, this->getSpaceDimension()); + volXCenter->accumulate( ret->getPointer() ); + double volOfMesh(vol->accumulate(0)); + ret->applyLin(1.0/volOfMesh,0.0); + return ret; +} + /*! * Writes \a this mesh into a VTK format file named as specified. * \param [in] fileName - the name of the file to write in. If the extension is OK the fileName will be used directly. diff --git a/src/MEDCoupling/MEDCouplingMesh.hxx b/src/MEDCoupling/MEDCouplingMesh.hxx index 4a3f63133..f75ca7f4e 100644 --- a/src/MEDCoupling/MEDCouplingMesh.hxx +++ b/src/MEDCoupling/MEDCouplingMesh.hxx @@ -106,6 +106,7 @@ namespace MEDCoupling MEDCOUPLING_EXPORT virtual std::string simpleRepr() const = 0; MEDCOUPLING_EXPORT virtual std::string advancedRepr() const = 0; // tools + MEDCOUPLING_EXPORT virtual MCAuto computeMeshCenterOfMass() const; MEDCOUPLING_EXPORT virtual const DataArrayDouble *getDirectAccessOfCoordsArrIfInStructure() const = 0; MEDCOUPLING_EXPORT virtual std::vector getDistributionOfTypes() const = 0; MEDCOUPLING_EXPORT virtual DataArrayIdType *checkTypeConsistencyAndContig(const std::vector& code, const std::vector& idsPerType) const = 0; diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest7.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest7.py index 6b3955a86..70dfa7bf1 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest7.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest7.py @@ -956,6 +956,16 @@ class MEDCouplingBasicsTest7(unittest.TestCase): self.assertEqual(arr3.getInfoOnComponents(),comps) self.assertTrue(arr3.isEqual(arr)) + def testComputeMeshCenterOfMass0(self): + #2D + arr = DataArrayDouble(5) ; arr.iota() + m = MEDCouplingCMesh() ; m.setCoords(arr,arr) ; m=m.buildUnstructured() + self.assertTrue( m.computeMeshCenterOfMass().isEqual(DataArrayDouble([2,2],1,2),1e-12) ) + #3D + m = MEDCouplingCMesh() ; m.setCoords(arr,arr,arr) ; m=m.buildUnstructured() + self.assertTrue( m.computeMeshCenterOfMass().isEqual(DataArrayDouble([2,2,2],1,3),1e-12) ) + + pass if __name__ == '__main__': diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index ab90fc0f9..1639e0751 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -309,6 +309,7 @@ typedef long int mcIdType; %newobject MEDCoupling::MEDCouplingMesh::buildPartRange; %newobject MEDCoupling::MEDCouplingMesh::giveCellsWithType; %newobject MEDCoupling::MEDCouplingMesh::getCoordinatesAndOwner; +%newobject MEDCoupling::MEDCouplingMesh::computeMeshCenterOfMass; %newobject MEDCoupling::MEDCouplingMesh::computeCellCenterOfMass; %newobject MEDCoupling::MEDCouplingMesh::computeIsoBarycenterOfNodesPerCell; %newobject MEDCoupling::MEDCouplingMesh::buildOrthogonalField; @@ -750,6 +751,12 @@ namespace MEDCoupling { return self->simpleRepr(); } + + DataArrayDouble *computeMeshCenterOfMass() const + { + MCAuto ret(self->computeMeshCenterOfMass()); + return ret.retn(); + } PyObject *getTime() {