Salome HOME
[EDF26877] : management of computation of measure field on field on Gauss Point in...
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingMesh.cxx
index 6de0b97a7523f71c40c3aaac3d71d7d4b165b1ee..f7ab4ccddf7179655a9efeea75057f387ac820da 100755 (executable)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2021  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2022  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
@@ -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<DataArrayDouble> MEDCouplingMesh::computeMeshCenterOfMass() const
+{
+  MCAuto<DataArrayDouble> cellCenters( this->computeCellCenterOfMass() );
+  MCAuto<MEDCouplingFieldDouble> vol( this->getMeasureField(true) );
+  MCAuto<DataArrayDouble> volXCenter( DataArrayDouble::Multiply(cellCenters,vol->getArray()) );
+  MCAuto<DataArrayDouble> 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.