Salome HOME
Some factorization before integration of ParaMEDMEM into medcoupling python module
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingField.cxx
index a4a0534f6dc53b633db384fd09553cf6b45a5c37..94c0a12fad091179c86d4e2c1fc3586e300646dd 100644 (file)
@@ -1,9 +1,9 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2016  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.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 #include <sstream>
 
-using namespace ParaMEDMEM;
+using namespace MEDCoupling;
 
-bool MEDCouplingField::isEqualIfNotWhy(const MEDCouplingField *other, double meshPrec, double valsPrec, std::string& reason) const
+void MEDCouplingField::checkConsistencyLight() const
+{
+  if(!_mesh)
+    throw INTERP_KERNEL::Exception("Field invalid because no mesh specified !");
+  if(_type.isNull())
+    throw INTERP_KERNEL::Exception("MEDCouplingField::checkConsistencyLight : no spatial discretization !");
+}
+
+bool MEDCouplingField::isEqualIfNotWhyProtected(const MEDCouplingField *other, double meshPrec, std::string& reason) const
 {
   if(!other)
     throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualIfNotWhy : other instance is NULL !");
@@ -49,7 +57,7 @@ bool MEDCouplingField::isEqualIfNotWhy(const MEDCouplingField *other, double mes
       reason=oss.str();
       return false;
     }
-  if(!_type->isEqualIfNotWhy(other->_type,valsPrec,reason))
+  if(!_type->isEqualIfNotWhy(other->_type,meshPrec,reason))
     {
       reason.insert(0,"Spatial discretizations differ :");
       return false;
@@ -69,37 +77,22 @@ bool MEDCouplingField::isEqualIfNotWhy(const MEDCouplingField *other, double mes
   return ret;
 }
 
-/*!
- * Checks if \a this and another MEDCouplingField are fully equal.
- *  \param [in] other - the field to compare with \a this one.
- *  \param [in] meshPrec - precision used to compare node coordinates of the underlying mesh.
- *  \param [in] valsPrec - precision used to compare field values.
- *  \return bool - \c true if the two fields are equal, \c false else.
- *  \throw If \a other is NULL.
- */
-bool MEDCouplingField::isEqual(const MEDCouplingField *other, double meshPrec, double valsPrec) const
-{
-  std::string tmp;
-  return isEqualIfNotWhy(other,meshPrec,valsPrec,tmp);
-}
-
 /*!
  * Checks if \a this and another MEDCouplingField are equal. The textual
  * information like names etc. is not considered.
  *  \param [in] other - the field to compare with \a this one.
  *  \param [in] meshPrec - precision used to compare node coordinates of the underlying mesh.
- *  \param [in] valsPrec - precision used to compare field values.
  *  \return bool - \c true if the two fields are equal, \c false else.
  *  \throw If \a other is NULL.
  *  \throw If the spatial discretization of \a this field is NULL.
  */
-bool MEDCouplingField::isEqualWithoutConsideringStr(const MEDCouplingField *other, double meshPrec, double valsPrec) const
+bool MEDCouplingField::isEqualWithoutConsideringStrProtected(const MEDCouplingField *other, double meshPrec) const
 {
   if(!other)
     throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualWithoutConsideringStr : input field is NULL !");
   if(!_type)
     throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualWithoutConsideringStr : spatial discretization of this is NULL !");
-  if(!_type->isEqualWithoutConsideringStr(other->_type,valsPrec))
+  if(!_type->isEqualWithoutConsideringStr(other->_type,meshPrec))
     return false;
   if(_nature!=other->_nature)
     return false;
@@ -145,6 +138,21 @@ bool MEDCouplingField::areStrictlyCompatible(const MEDCouplingField *other) cons
   return _mesh==other->_mesh;
 }
 
+/*!
+ * This method is less strict than MEDCouplingField::areStrictlyCompatible method.
+ * The difference is that the nature is not checked.
+ * This method is used for multiplication and division on fields to operate a first check before attempting operation.
+ */
+bool MEDCouplingField::areStrictlyCompatibleForMulDiv(const MEDCouplingField *other) const
+{
+  if(!other)
+    throw INTERP_KERNEL::Exception("MEDCouplingField::areStrictlyCompatible : input field is NULL !");
+  if(!_type->isEqual(other->_type,1.e-12))
+    return false;
+  return _mesh==other->_mesh;
+}
+
+
 void MEDCouplingField::updateTime() const
 {
   if(_mesh)
@@ -161,20 +169,18 @@ std::size_t MEDCouplingField::getHeapMemorySizeWithoutChildren() const
   return ret;
 }
 
-std::vector<const BigMemoryObject *> MEDCouplingField::getDirectChildren() const
+std::vector<const BigMemoryObject *> MEDCouplingField::getDirectChildrenWithNull() const
 {
   std::vector<const BigMemoryObject *> ret;
-  if(_mesh)
-    ret.push_back(_mesh);
-  if((const MEDCouplingFieldDiscretization *)_type)
-    ret.push_back((const MEDCouplingFieldDiscretization *)_type);
+  ret.push_back(_mesh);
+  ret.push_back((const MEDCouplingFieldDiscretization *)_type);
   return ret;
 }
 
 /*!
  * Returns a type of \ref MEDCouplingSpatialDisc "spatial discretization" of \a this
- * field in terms of enum ParaMEDMEM::TypeOfField. 
- * \return ParaMEDMEM::TypeOfField - the type of \a this field.
+ * field in terms of enum MEDCoupling::TypeOfField. 
+ * \return MEDCoupling::TypeOfField - the type of \a this field.
  * \throw If the geometric type is empty.
  */
 TypeOfField MEDCouplingField::getTypeOfField() const
@@ -186,7 +192,7 @@ TypeOfField MEDCouplingField::getTypeOfField() const
 
 /*!
  * Returns the nature of \a this field. This information is very important during
- * interpolation process using ParaMEDMEM::MEDCouplingRemapper or ParaMEDMEM::InterpKernelDEC.
+ * interpolation process using MEDCoupling::MEDCouplingRemapper or MEDCoupling::InterpKernelDEC.
  * In other context than the two mentioned above, this attribute is unimportant. This
  * attribute is not stored in the MED file.
  * For more information of the semantics and the influence of this attribute to the
@@ -201,7 +207,7 @@ NatureOfField MEDCouplingField::getNature() const
 
 /*!
  * Sets the nature of \a this field. This information is very important during
- * interpolation process using ParaMEDMEM::MEDCouplingRemapper or ParaMEDMEM::InterpKernelDEC.
+ * interpolation process using MEDCoupling::MEDCouplingRemapper or MEDCoupling::InterpKernelDEC.
  * In other context than the two mentioned above, this attribute is unimportant. This
  * attribute is not stored in the MED file.
  * For more information of the semantics and the influence of this attribute to the
@@ -215,6 +221,8 @@ NatureOfField MEDCouplingField::getNature() const
 void MEDCouplingField::setNature(NatureOfField nat)
 {
   MEDCouplingNatureOfField::GetRepr(nat);//generate a throw if nat not recognized
+  if(_type)
+    _type->checkCompatibilityWithNature(nat);
   _nature=nat;
 }
 
@@ -300,7 +308,7 @@ void MEDCouplingField::setMesh(const MEDCouplingMesh *mesh)
  *  \throw If size of any vector do not match the \a type.
  */
 void MEDCouplingField::setGaussLocalizationOnType(INTERP_KERNEL::NormalizedCellType type, const std::vector<double>& refCoo,
-                                                  const std::vector<double>& gsCoo, const std::vector<double>& wg) throw(INTERP_KERNEL::Exception)
+                                                  const std::vector<double>& gsCoo, const std::vector<double>& wg)
 {
   if(!_mesh)
     throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnType method !");
@@ -326,7 +334,7 @@ void MEDCouplingField::setGaussLocalizationOnType(INTERP_KERNEL::NormalizedCellT
  *  \throw If the range [_begin_,_end_) is empty.
  */
 void MEDCouplingField::setGaussLocalizationOnCells(const int *begin, const int *end, const std::vector<double>& refCoo,
-                                                   const std::vector<double>& gsCoo, const std::vector<double>& wg) throw(INTERP_KERNEL::Exception)
+                                                   const std::vector<double>& gsCoo, const std::vector<double>& wg)
 {
   if(!_mesh)
     throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnCells method !");
@@ -353,7 +361,8 @@ void MEDCouplingField::clearGaussLocalizations()
  *          problem.
  *  \param [in] locId - the id of the Gauss localization object of interest.
  *         It must be in range <em> 0 <= locId < getNbOfGaussLocalization() </em>.
- *  \return \ref MEDCouplingGaussLocalization & - the Gauss localization object.
+ *  \return \ref MEDCoupling::MEDCouplingGaussLocalization "MEDCouplingGaussLocalization" & - the
+ *  Gauss localization object.
  *  \throw If \a this field is not on Gauss points.
  *  \throw If \a locId is not within the valid range.
  *  \throw If the spatial discretization of \a this field is NULL.
@@ -447,7 +456,7 @@ void MEDCouplingField::getCellIdsHavingGaussLocalization(int locId, std::vector<
  * \warning This method is const, so the returned object is not apt for modification.
  *  \param [in] locId - the id of the Gauss localization object of interest.
  *         It must be in range <em> 0 <= locId < getNbOfGaussLocalization() </em>.
- *  \return \ref const MEDCouplingGaussLocalization & - the Gauss localization object.
+ *  \return const \ref MEDCouplingGaussLocalization & - the Gauss localization object.
  *  \throw If \a this field is not on Gauss points.
  *  \throw If \a locId is not within the valid range.
  *  \throw If the spatial discretization of \a this field is NULL.
@@ -474,7 +483,7 @@ MEDCouplingField::MEDCouplingField(TypeOfField type):_nature(NoNature),_mesh(0),
 }
 
 MEDCouplingField::MEDCouplingField(const MEDCouplingField& other, bool deepCopy):RefCountObject(other),_name(other._name),_desc(other._desc),_nature(other._nature),
-                                                                                 _mesh(0),_type(0)
+    _mesh(0),_type(0)
 {
   if(other._mesh)
     {
@@ -489,7 +498,7 @@ MEDCouplingField::MEDCouplingField(const MEDCouplingField& other, bool deepCopy)
 
 /*!
  * Returns a new MEDCouplingMesh constituted by some cells of the underlying mesh of \a
- * this filed, and returns ids of entities (nodes, cells, Gauss points) lying on the 
+ * this field, and returns ids of entities (nodes, cells, Gauss points) lying on the
  * specified cells. The cells to include to the result mesh are specified by an array of
  * cell ids. The new mesh shares the coordinates array with the underlying mesh. 
  *  \param [in] start - an array of cell ids to include to the result mesh.
@@ -541,7 +550,7 @@ DataArrayInt *MEDCouplingField::computeTupleIdsToSelectFromCellIds(const int *st
 
 /*!
  * Returns number of tuples expected regarding the spatial discretization of \a this
- * field and number of entities in the underlying mesh.
+ * field and number of entities in the underlying mesh. This method behaves exactly as MEDCouplingFieldDouble::getNumberOfTuples.
  *  \return int - the number of expected tuples.
  *  \throw If the spatial discretization of \a this field is NULL.
  *  \throw If the mesh is not set.
@@ -590,8 +599,8 @@ void MEDCouplingField::copyTinyStringsFrom(const MEDCouplingField *other)
 {
   if(other)
     {
-      setName(other->_name.c_str());
-      setDescription(other->_desc.c_str());    
+      setName(other->_name);
+      setDescription(other->_desc);    
     }
 }
 
@@ -617,5 +626,5 @@ int MEDCouplingField::getNumberOfTuplesExpectedRegardingCode(const std::vector<i
   const MEDCouplingFieldDiscretization *t(_type);
   if(!t)
     throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfTuplesExpectedRegardingCode : no spatial discretization set !");
-  return t->getNumberOfTuplesExpectedRegardingCode(_mesh,code,idsPerType);
+  return t->getNumberOfTuplesExpectedRegardingCode(code,idsPerType);
 }