1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "MEDCouplingField.hxx"
22 #include "MEDCouplingMesh.hxx"
23 #include "MEDCouplingFieldDiscretization.hxx"
27 using namespace ParaMEDMEM;
29 bool MEDCouplingField::isEqualIfNotWhy(const MEDCouplingField *other, double meshPrec, double valsPrec, std::string& reason) const throw(INTERP_KERNEL::Exception)
32 throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualIfNotWhy : other instance is NULL !");
33 std::ostringstream oss; oss.precision(15);
34 if(_name!=other->_name)
36 oss << "Field names differ : this name = \"" << _name << "\" and other name = \"" << other->_name << "\" !";
40 if(_desc!=other->_desc)
42 oss << "Field descriptions differ : this description = \"" << _desc << "\" and other description = \"" << other->_desc << "\" !";
46 if(_nature!=other->_nature)
48 oss << "Field nature differ : this nature = \"" << MEDCouplingNatureOfField::getRepr(_nature) << "\" and other nature = \"" << MEDCouplingNatureOfField::getRepr(other->_nature) << "\" !";
52 if(!_type->isEqualIfNotWhy(other->_type,valsPrec,reason))
54 reason.insert(0,"Spatial discretizations differ :");
57 if(_mesh==0 && other->_mesh==0)
59 if(_mesh==0 || other->_mesh==0)
61 reason="Only one field between the two this and other has its underlying mesh defined !";
64 if(_mesh==other->_mesh)
66 bool ret=_mesh->isEqualIfNotWhy(other->_mesh,meshPrec,reason);
68 reason.insert(0,"Underlying meshes of fields differ for the following reason : ");
72 bool MEDCouplingField::isEqual(const MEDCouplingField *other, double meshPrec, double valsPrec) const
75 return isEqualIfNotWhy(other,meshPrec,valsPrec,tmp);
78 bool MEDCouplingField::isEqualWithoutConsideringStr(const MEDCouplingField *other, double meshPrec, double valsPrec) const
80 if(!_type->isEqualWithoutConsideringStr(other->_type,valsPrec))
82 if(_nature!=other->_nature)
84 if(_mesh==0 && other->_mesh==0)
86 if(_mesh==0 || other->_mesh==0)
88 if(_mesh==other->_mesh)
90 return _mesh->isEqualWithoutConsideringStr(other->_mesh,meshPrec);
94 * This method states if 'this' and 'other' are compatibles each other before performing any treatment.
95 * This method is good for methods like : mergeFields.
96 * This method is not very demanding compared to areStrictlyCompatible that is better for operation on fields.
98 bool MEDCouplingField::areCompatibleForMerge(const MEDCouplingField *other) const
100 if(!_type->isEqual(other->_type,1.))
102 if(_nature!=other->_nature)
104 if(_mesh==other->_mesh)
106 return _mesh->areCompatibleForMerge(other->_mesh);
110 * This method is more strict than MEDCouplingField::areCompatibleForMerge method.
111 * This method is used for operation on fields to operate a first check before attempting operation.
113 bool MEDCouplingField::areStrictlyCompatible(const MEDCouplingField *other) const
115 if(!_type->isEqual(other->_type,1.e-12))
117 if(_nature!=other->_nature)
119 return _mesh==other->_mesh;
122 void MEDCouplingField::updateTime() const
125 updateTimeWith(*_mesh);
127 updateTimeWith(*_type);
130 TypeOfField MEDCouplingField::getTypeOfField() const
132 return _type->getEnum();
136 * This method returns the nature of field. This information is very important during interpolation process using ParaMEDMEM::MEDCouplingRemapper or ParaMEDMEM::InterpKernelDEC.
137 * In other context than the two mentioned before this attribute of the field is not sensitive. This attribute is not store in MED file in MEDLoader.
138 * More information of the semantic, and the consequence of this attribute in the result of the interpolation, is available \ref NatureOfField "here".
140 NatureOfField MEDCouplingField::getNature() const
146 * This method set the nature of field in \b this.This information is very important during interpolation process using ParaMEDMEM::MEDCouplingRemapper or ParaMEDMEM::InterpKernelDEC.
147 * In other context than the two mentioned before this attribute of the field is not sensitive. This attribute is not store in MED file in MEDLoader.
148 * More information of the semantic, and the consequence of this attribute in the result of the interpolation, is available \ref TableNatureOfField "here".
150 void MEDCouplingField::setNature(NatureOfField nat) throw(INTERP_KERNEL::Exception)
156 * This method returns is case of success an instance of DataArrayDouble the user is in reponsability to deal with.
157 * If 'this->_mesh' is not set an exception will be thrown.
158 * For a field on node the array of coords will be returned. For a field on cell a ParaMEDMEM::DataArrayDouble instance
159 * containing the barycenter of cells will be returned. And for a field on gauss point the explicit position of gauss points.
161 DataArrayDouble *MEDCouplingField::getLocalizationOfDiscr() const throw(INTERP_KERNEL::Exception)
164 throw INTERP_KERNEL::Exception("MEDCouplingField::getLocalizationOfDiscr : No mesh set !");
165 return _type->getLocalizationOfDiscValues(_mesh);
169 * This method retrieves the measure field of 'this'. If no '_mesh' is defined an exception will be thrown.
170 * Warning the retrieved field life cycle is the responsability of caller.
172 MEDCouplingFieldDouble *MEDCouplingField::buildMeasureField(bool isAbs) const throw(INTERP_KERNEL::Exception)
175 throw INTERP_KERNEL::Exception("MEDCouplingField::getMeasureField : no mesh defined !!!");
176 return _type->getMeasureField(_mesh,isAbs);
179 void MEDCouplingField::setMesh(const MEDCouplingMesh *mesh)
189 updateTimeWith(*_mesh);
195 * This method sets gauss localization by geometric type.
196 * @param type geometric type on which the gauss localization will be set.
197 * @param refCoo is the reference coordinates of the specified element. Its size has to be equal to nbOfNodesPerCell*dimOfType
198 * @param gsCoo are the coordinates of Gauss points in reference element specified by 'refCoo'. Its size must be equal to wg.size()*dimOfType
199 * @param wg are the weights on Gauss points. The size of this array is used to determine the number of Gauss point in the element.
200 * @throw when size of 'RefCoo' is not valid regarding 'type' parameter, it throws too when the mesh is not set before or if it is not a field on Gauss points.
202 void MEDCouplingField::setGaussLocalizationOnType(INTERP_KERNEL::NormalizedCellType type, const std::vector<double>& refCoo,
203 const std::vector<double>& gsCoo, const std::vector<double>& wg) throw(INTERP_KERNEL::Exception)
206 throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnType method !");
207 _type->setGaussLocalizationOnType(_mesh,type,refCoo,gsCoo,wg);
211 * This method sets on ids defined by [begin;end) their gauss localization. This method checks the coherency of cells ids in [begin;end) and 'refCoo' size.
212 * If an incoherence appears an exception will be thrown and no seting will be performed.
213 * An exception is thrown too if [begin,end) has a size lesser than 1.
215 * @param refCoo is the reference coordinates of the specified element. Its size has to be equal to nbOfNodesPerCell*dimOfType
216 * @param gsCoo are the coordinates of Gauss points in reference element specified by 'refCoo'. Its size must be equal to wg.size()*dimOfType
217 * @param wg are the weights on Gauss points. The size of this array is used to determine the number of Gauss point in the element.
218 * @throw when size of 'RefCoo' is not valid regarding cells in [begin,end) parameters, it throws too when the mesh is not set before or if it is not a field on Gauss points.
220 void MEDCouplingField::setGaussLocalizationOnCells(const int *begin, const int *end, const std::vector<double>& refCoo,
221 const std::vector<double>& gsCoo, const std::vector<double>& wg) throw(INTERP_KERNEL::Exception)
224 throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnCells method !");
225 _type->setGaussLocalizationOnCells(_mesh,begin,end,refCoo,gsCoo,wg);
229 * This method resets all Gauss loalizations if any.
231 void MEDCouplingField::clearGaussLocalizations()
234 throw INTERP_KERNEL::Exception("Mesh has to be set before calling clearGaussLocalizations method !");
235 _type->clearGaussLocalizations();
239 * This method returns reference to the Gauss localization object corresponding to 'locId' id.
240 * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss) and if 'locId' is invalid because out of range given by
241 * MEDCouplingField::getNbOfGaussLocalization method.
242 * Warning this method is not const, so the returned object could be modified without any problem.
244 MEDCouplingGaussLocalization& MEDCouplingField::getGaussLocalization(int locId) throw(INTERP_KERNEL::Exception)
247 throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalization method !");
248 return _type->getGaussLocalization(locId);
252 * This method returns reference to the Gauss localization object corresponding to 'locId' id.
253 * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss) and if several localization ids have been found
256 int MEDCouplingField::getGaussLocalizationIdOfOneType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
259 throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneType method !");
260 return _type->getGaussLocalizationIdOfOneType(type);
264 * This method returns number of Gauss localization available. Implicitely all ids in [0,getNbOfGaussLocalization()) is a valid Gauss localisation id.
265 * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss)
267 int MEDCouplingField::getNbOfGaussLocalization() const throw(INTERP_KERNEL::Exception)
270 throw INTERP_KERNEL::Exception("Mesh has to be set before calling getNbOfGaussLocalization method !");
271 return _type->getNbOfGaussLocalization();
275 * This method returns an id of Gauss localization in [0,getNbOfGaussLocalization()) that corresponds to the localization of the cell specified by its cellId.
276 * This methods throws an exception if there is no mesh, invalid FieldDescription (different from Gauss) or if at the cell with id 'cellId' in this->_mesh no
277 * Gauss localization has been set.
279 int MEDCouplingField::getGaussLocalizationIdOfOneCell(int cellId) const throw(INTERP_KERNEL::Exception)
282 throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneCell method !");
283 return _type->getGaussLocalizationIdOfOneCell(cellId);
287 * This method returns all cellIds that share the same Gauss localization given by 'locId' parameter (in range [0,getNbOfGaussLocalization()) ).
288 * If no cells fit the Gauss localization given by 'locId' cellIds will be returned empty.
289 * @param locId input that specifies the id of Gauss localization.
290 * @param cellIds output parameter, that will contain the result if this method succeds. This parameter is systematically cleared when called.
291 * @throw if there is no mesh, invalid FieldDescription (different from Gauss) or if locId not in [0,getNbOfGaussLocalization())
293 void MEDCouplingField::getCellIdsHavingGaussLocalization(int locId, std::vector<int>& cellIds) const throw(INTERP_KERNEL::Exception)
297 throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneCell method !");
298 _type->getCellIdsHavingGaussLocalization(locId,cellIds);
302 * This method returns reference to the Gauss localization object corresponding to 'locId' id.
303 * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss) and if 'locId' is invalid because out of range given by
304 * MEDCouplingField::getNbOfGaussLocalization method.
305 * Warning this method is const.
307 const MEDCouplingGaussLocalization& MEDCouplingField::getGaussLocalization(int locId) const throw(INTERP_KERNEL::Exception)
310 throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalization method !");
311 return _type->getGaussLocalization(locId);
314 MEDCouplingField::~MEDCouplingField()
320 MEDCouplingField::MEDCouplingField(MEDCouplingFieldDiscretization *type, NatureOfField nature):_nature(nature),_mesh(0),_type(type)
324 MEDCouplingField::MEDCouplingField(TypeOfField type):_nature(NoNature),_mesh(0),_type(MEDCouplingFieldDiscretization::New(type))
328 MEDCouplingField::MEDCouplingField(const MEDCouplingField& other):RefCountObject(other),_name(other._name),_desc(other._desc),_nature(other._nature),
329 _mesh(0),_type(other._type->clone())
339 * This method returns a submesh of 'mesh' instance constituting cell ids contained in array defined as an interval [start;end).
340 * @param di is an array returned that specifies entity ids (nodes, cells ids...) in mesh 'mesh' of entity in returned submesh.
342 MEDCouplingMesh *MEDCouplingField::buildSubMeshData(const int *start, const int *end, DataArrayInt *&di) const
344 return _type->buildSubMeshData(_mesh,start,end,di);
348 * This method returns tuples ids implied by the mesh selection of the cell ids contained in array defined as an interval [start;end).
349 * \return a newly allocated DataArrayInt instance containing tuples ids.
351 DataArrayInt *MEDCouplingField::computeTupleIdsToSelectFromCellIds(const int *startCellIds, const int *endCellIds) const
353 return _type->computeTupleIdsToSelectFromCellIds(_mesh,startCellIds,endCellIds);
357 * This method returns number of tuples expected regarding its discretization and its _mesh attribute.
358 * This method expected a not null _mesh instance. If null, an exception will be thrown.
360 int MEDCouplingField::getNumberOfTuplesExpected() const throw(INTERP_KERNEL::Exception)
363 return _type->getNumberOfTuples(_mesh);
365 throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfTuplesExpected : Empty mesh !");
369 * This method returns number of mesh placed expected regarding its discretization and its _mesh attribute.
370 * This method expected a not null _mesh instance. If null, an exception will be thrown.
372 int MEDCouplingField::getNumberOfMeshPlacesExpected() const throw(INTERP_KERNEL::Exception)
375 return _type->getNumberOfMeshPlaces(_mesh);
377 throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfMeshPlacesExpected : Empty mesh !");