Salome HOME
Add missing icon name
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingField.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDCouplingField.hxx"
22 #include "MEDCouplingMesh.hxx"
23 #include "MEDCouplingFieldDiscretization.hxx"
24
25 #include <sstream>
26
27 using namespace ParaMEDMEM;
28
29 bool MEDCouplingField::isEqualIfNotWhy(const MEDCouplingField *other, double meshPrec, double valsPrec, std::string& reason) const throw(INTERP_KERNEL::Exception)
30 {
31   if(!other)
32     throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualIfNotWhy : other instance is NULL !");
33   std::ostringstream oss; oss.precision(15);
34   if(_name!=other->_name)
35     {
36       oss << "Field names differ : this name = \"" << _name << "\" and other name = \"" << other->_name << "\" !";
37       reason=oss.str();
38       return false;
39     }
40   if(_desc!=other->_desc)
41     {
42       oss << "Field descriptions differ : this description = \"" << _desc << "\" and other description = \"" << other->_desc << "\" !";
43       reason=oss.str();
44       return false;
45     }
46   if(_nature!=other->_nature)
47     {
48       oss << "Field nature differ : this nature = \"" << MEDCouplingNatureOfField::GetRepr(_nature) << "\" and other nature = \"" << MEDCouplingNatureOfField::GetRepr(other->_nature) << "\" !";
49       reason=oss.str();
50       return false;
51     }
52   if(!_type->isEqualIfNotWhy(other->_type,valsPrec,reason))
53     {
54       reason.insert(0,"Spatial discretizations differ :");
55       return false;
56     }
57   if(_mesh==0 && other->_mesh==0)
58     return true;
59   if(_mesh==0 || other->_mesh==0)
60     {
61       reason="Only one field between the two this and other has its underlying mesh defined !";
62       return false;
63     }
64   if(_mesh==other->_mesh)
65     return true;
66   bool ret=_mesh->isEqualIfNotWhy(other->_mesh,meshPrec,reason);
67   if(!ret)
68     reason.insert(0,"Underlying meshes of fields differ for the following reason : ");
69   return ret;
70 }
71
72 bool MEDCouplingField::isEqual(const MEDCouplingField *other, double meshPrec, double valsPrec) const
73 {
74   std::string tmp;
75   return isEqualIfNotWhy(other,meshPrec,valsPrec,tmp);
76 }
77
78 bool MEDCouplingField::isEqualWithoutConsideringStr(const MEDCouplingField *other, double meshPrec, double valsPrec) const
79 {
80   if(!other)
81     throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualWithoutConsideringStr : input field is NULL !");
82   if(!_type)
83     throw INTERP_KERNEL::Exception("MEDCouplingField::isEqualWithoutConsideringStr : spatial discretization of this is NULL !");
84   if(!_type->isEqualWithoutConsideringStr(other->_type,valsPrec))
85     return false;
86   if(_nature!=other->_nature)
87     return false;
88   if(_mesh==0 && other->_mesh==0)
89     return true;
90   if(_mesh==0 || other->_mesh==0)
91     return false;
92   if(_mesh==other->_mesh)
93     return true;
94   return _mesh->isEqualWithoutConsideringStr(other->_mesh,meshPrec);
95 }
96
97 /*!
98  * This method states if 'this' and 'other' are compatibles each other before performing any treatment.
99  * This method is good for methods like : mergeFields.
100  * This method is not very demanding compared to areStrictlyCompatible that is better for operation on fields.
101  */
102 bool MEDCouplingField::areCompatibleForMerge(const MEDCouplingField *other) const
103 {
104   if(!other)
105     throw INTERP_KERNEL::Exception("MEDCouplingField::areCompatibleForMerge : input field is NULL !");
106   if(!_type->isEqual(other->_type,1.))
107     return false;
108   if(_nature!=other->_nature)
109     return false;
110   if(_mesh==other->_mesh)
111     return true;
112   return _mesh->areCompatibleForMerge(other->_mesh);
113 }
114
115 /*!
116  * This method is more strict than MEDCouplingField::areCompatibleForMerge method.
117  * This method is used for operation on fields to operate a first check before attempting operation.
118  */
119 bool MEDCouplingField::areStrictlyCompatible(const MEDCouplingField *other) const
120 {
121   if(!other)
122     throw INTERP_KERNEL::Exception("MEDCouplingField::areStrictlyCompatible : input field is NULL !");
123   if(!_type->isEqual(other->_type,1.e-12))
124     return false;
125   if(_nature!=other->_nature)
126     return false;
127   return _mesh==other->_mesh;
128 }
129
130 void MEDCouplingField::updateTime() const
131 {
132   if(_mesh)
133     updateTimeWith(*_mesh);
134   if(_type)
135     updateTimeWith(*_type);
136 }
137
138 std::size_t MEDCouplingField::getHeapMemorySize() const
139 {
140   std::size_t ret=0;
141   ret+=_name.capacity();
142   ret+=_desc.capacity();
143   if(_mesh)
144     ret+=_mesh->getHeapMemorySize();
145   if((const MEDCouplingFieldDiscretization *)_type)
146     ret+=_type->getHeapMemorySize();
147   return ret;
148 }
149
150 TypeOfField MEDCouplingField::getTypeOfField() const
151 {
152   return _type->getEnum();
153 }
154
155 /*!
156  * This method returns the nature of field. This information is very important during interpolation process using ParaMEDMEM::MEDCouplingRemapper or ParaMEDMEM::InterpKernelDEC.
157  * 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.
158  * More information of the semantic, and the consequence of this attribute in the result of the interpolation, is available \ref NatureOfField "here".
159  */
160 NatureOfField MEDCouplingField::getNature() const
161 {
162   return _nature;
163 }
164
165 /*!
166  * This method set the nature of field in \b this.This  information is very important during interpolation process using ParaMEDMEM::MEDCouplingRemapper or ParaMEDMEM::InterpKernelDEC.
167  * 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.
168  * More information of the semantic, and the consequence of this attribute in the result of the interpolation, is available \ref TableNatureOfField "here".
169  */
170 void MEDCouplingField::setNature(NatureOfField nat) throw(INTERP_KERNEL::Exception)
171 {
172   MEDCouplingNatureOfField::GetRepr(nat);//generate a throw if nat not recognized
173   _nature=nat;
174 }
175
176 /*!
177  * This method returns is case of success an instance of DataArrayDouble the user is in reponsability to deal with.
178  * If 'this->_mesh' is not set an exception will be thrown.
179  * For a field on node the array of coords will be returned. For a field on cell a ParaMEDMEM::DataArrayDouble instance
180  * containing the barycenter of cells will be returned. And for a field on gauss point the explicit position of gauss points.
181  */
182 DataArrayDouble *MEDCouplingField::getLocalizationOfDiscr() const throw(INTERP_KERNEL::Exception)
183 {
184   if(!_mesh)
185     throw INTERP_KERNEL::Exception("MEDCouplingField::getLocalizationOfDiscr : No mesh set !");
186   if(!((const MEDCouplingFieldDiscretization *)_type))
187     throw INTERP_KERNEL::Exception("MEDCouplingField::getLocalizationOfDiscr : No spatial discretization set !");
188   return _type->getLocalizationOfDiscValues(_mesh);
189 }
190
191 /*!
192  * This method retrieves the measure field of 'this'. If no '_mesh' is defined an exception will be thrown.
193  * Warning the retrieved field life cycle is the responsability of caller.
194  */
195 MEDCouplingFieldDouble *MEDCouplingField::buildMeasureField(bool isAbs) const throw(INTERP_KERNEL::Exception)
196 {
197   if(!_mesh)
198     throw INTERP_KERNEL::Exception("MEDCouplingField::buildMeasureField : no mesh defined !");
199   if(!((const MEDCouplingFieldDiscretization *)_type))
200     throw INTERP_KERNEL::Exception("MEDCouplingField::buildMeasureField : No spatial discretization set !");
201   return _type->getMeasureField(_mesh,isAbs);
202 }
203
204 void MEDCouplingField::setMesh(const MEDCouplingMesh *mesh)
205 {
206   if(mesh!=_mesh)
207     {
208       if(_mesh)
209         _mesh->decrRef();
210       _mesh=mesh;
211       declareAsNew();
212       if(_mesh)
213         {
214           _mesh->incrRef();
215           updateTimeWith(*_mesh);
216         }
217     }
218 }
219
220 /*!
221  * This method sets gauss localization by geometric type.
222  * @param type geometric type on which the gauss localization will be set.
223  * @param refCoo is the reference coordinates of the specified element. Its size has to be equal to nbOfNodesPerCell*dimOfType
224  * @param gsCoo are the coordinates of Gauss points in reference element specified by 'refCoo'. Its size must be equal to wg.size()*dimOfType
225  * @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.
226  * @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.
227  */
228 void MEDCouplingField::setGaussLocalizationOnType(INTERP_KERNEL::NormalizedCellType type, const std::vector<double>& refCoo,
229                                                   const std::vector<double>& gsCoo, const std::vector<double>& wg) throw(INTERP_KERNEL::Exception)
230 {
231   if(!_mesh)
232     throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnType method !");
233   if(!((const MEDCouplingFieldDiscretization *)_type))
234     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call setGaussLocalizationOnType method !");
235   _type->setGaussLocalizationOnType(_mesh,type,refCoo,gsCoo,wg);
236 }
237
238 /*!
239  * 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.
240  * If an incoherence appears an exception will be thrown and no seting will be performed.
241  * An exception is thrown too if [begin,end) has a size lesser than 1.
242  * 
243  * @param refCoo is the reference coordinates of the specified element. Its size has to be equal to nbOfNodesPerCell*dimOfType
244  * @param gsCoo are the coordinates of Gauss points in reference element specified by 'refCoo'. Its size must be equal to wg.size()*dimOfType
245  * @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.
246  * @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.
247  */
248 void MEDCouplingField::setGaussLocalizationOnCells(const int *begin, const int *end, const std::vector<double>& refCoo,
249                                                    const std::vector<double>& gsCoo, const std::vector<double>& wg) throw(INTERP_KERNEL::Exception)
250 {
251   if(!_mesh)
252     throw INTERP_KERNEL::Exception("Mesh has to be set before calling setGaussLocalizationOnCells method !");
253   if(!((const MEDCouplingFieldDiscretization *)_type))
254     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call setGaussLocalizationOnCells method !");
255   _type->setGaussLocalizationOnCells(_mesh,begin,end,refCoo,gsCoo,wg);
256 }
257
258 /*!
259  * This method resets all Gauss loalizations if any.
260  */
261 void MEDCouplingField::clearGaussLocalizations()
262 {
263   if(!_mesh)
264     throw INTERP_KERNEL::Exception("Mesh has to be set before calling clearGaussLocalizations method !");
265   if(!((const MEDCouplingFieldDiscretization *)_type))
266     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call clearGaussLocalizations method !");
267   _type->clearGaussLocalizations();
268 }
269
270 /*!
271  * This method returns reference to the Gauss localization object corresponding to 'locId' id.
272  * 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
273  * MEDCouplingField::getNbOfGaussLocalization method.
274  * Warning this method is not const, so the returned object could be modified without any problem.
275  */
276 MEDCouplingGaussLocalization& MEDCouplingField::getGaussLocalization(int locId) throw(INTERP_KERNEL::Exception)
277 {
278   if(!_mesh)
279     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalization method !");
280   if(!((const MEDCouplingFieldDiscretization *)_type))
281     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalization method !");
282   return _type->getGaussLocalization(locId);
283 }
284
285 /*!
286  * This method returns reference to the Gauss localization object corresponding to 'locId' id.
287  * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss) and if several localization ids have been found
288  * for a type.
289  */
290 int MEDCouplingField::getGaussLocalizationIdOfOneType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
291 {
292   if(!_mesh)
293     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneType method !");
294   if(!((const MEDCouplingFieldDiscretization *)_type))
295     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalizationIdOfOneType method !");
296   return _type->getGaussLocalizationIdOfOneType(type);
297 }
298
299 std::set<int> MEDCouplingField::getGaussLocalizationIdsOfOneType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
300 {
301   if(!_mesh)
302     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdsOfOneType method !");
303   if(!((const MEDCouplingFieldDiscretization *)_type))
304     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalizationIdsOfOneType method !");
305   return _type->getGaussLocalizationIdsOfOneType(type);
306 }
307
308 /*!
309  * This method returns number of Gauss localization available. Implicitely all ids in [0,getNbOfGaussLocalization()) is a valid Gauss localisation id.
310  * This method throws an exception if there is no mesh, invalid FieldDescription (different from Gauss)
311  */
312 int MEDCouplingField::getNbOfGaussLocalization() const throw(INTERP_KERNEL::Exception)
313 {
314   if(!_mesh)
315     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getNbOfGaussLocalization method !");
316   if(!((const MEDCouplingFieldDiscretization *)_type))
317     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getNbOfGaussLocalization method !");
318   return _type->getNbOfGaussLocalization();
319 }
320
321 /*!
322  * This method returns an id of Gauss localization in [0,getNbOfGaussLocalization()) that corresponds to the localization of the cell specified by its cellId.
323  * 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
324  * Gauss localization has been set.
325  */
326 int MEDCouplingField::getGaussLocalizationIdOfOneCell(int cellId) const throw(INTERP_KERNEL::Exception)
327 {
328   if(!_mesh)
329     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneCell method !");
330   if(!((const MEDCouplingFieldDiscretization *)_type))
331     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalizationIdOfOneCell method !");
332   return _type->getGaussLocalizationIdOfOneCell(cellId);
333 }
334
335 /*!
336  * This method returns all cellIds that share the same Gauss localization given by 'locId' parameter (in range [0,getNbOfGaussLocalization()) ).
337  * If no cells fit the Gauss localization given by 'locId' cellIds will be returned empty.
338  * @param locId input that specifies the id of Gauss localization.
339  * @param cellIds output parameter, that will contain the result if this method succeds. This parameter is systematically cleared when called.
340  * @throw  if there is no mesh, invalid FieldDescription (different from Gauss) or if locId not in [0,getNbOfGaussLocalization())
341  */
342 void MEDCouplingField::getCellIdsHavingGaussLocalization(int locId, std::vector<int>& cellIds) const throw(INTERP_KERNEL::Exception)
343 {
344   cellIds.clear();
345   if(!_mesh)
346     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalizationIdOfOneCell method !");
347   if(!((const MEDCouplingFieldDiscretization *)_type))
348     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getCellIdsHavingGaussLocalization method !");
349   _type->getCellIdsHavingGaussLocalization(locId,cellIds);
350 }
351
352 /*!
353  * This method returns reference to the Gauss localization object corresponding to 'locId' id.
354  * 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
355  * MEDCouplingField::getNbOfGaussLocalization method.
356  * Warning this method is const.
357  */
358 const MEDCouplingGaussLocalization& MEDCouplingField::getGaussLocalization(int locId) const throw(INTERP_KERNEL::Exception)
359 {
360   if(!_mesh)
361     throw INTERP_KERNEL::Exception("Mesh has to be set before calling getGaussLocalization method !");
362   if(!((const MEDCouplingFieldDiscretization *)_type))
363     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getGaussLocalization method !");
364   return _type->getGaussLocalization(locId);
365 }
366
367 MEDCouplingField::~MEDCouplingField()
368 {
369   if(_mesh)
370     _mesh->decrRef();
371 }
372
373 MEDCouplingField::MEDCouplingField(MEDCouplingFieldDiscretization *type, NatureOfField nature):_nature(nature),_mesh(0),_type(type)
374 {
375 }
376
377 MEDCouplingField::MEDCouplingField(TypeOfField type):_nature(NoNature),_mesh(0),_type(MEDCouplingFieldDiscretization::New(type))
378 {
379 }
380
381 MEDCouplingField::MEDCouplingField(const MEDCouplingField& other, bool deepCopy):RefCountObject(other),_name(other._name),_desc(other._desc),_nature(other._nature),
382                                                                                  _mesh(0),_type(0)
383 {
384   if(other._mesh)
385     {
386       _mesh=other._mesh;
387       _mesh->incrRef();
388     }
389   if(deepCopy)
390     _type=other._type->clone();
391   else
392     _type=other._type;
393 }
394
395 /*!
396  * This method returns a submesh of 'mesh' instance constituting cell ids contained in array defined as an interval [start;end).
397  * @param di is an array returned that specifies entity ids (nodes, cells, Gauss points... ) in array.
398  * 
399  * \sa MEDCouplingField::buildSubMeshDataRange
400  */
401 MEDCouplingMesh *MEDCouplingField::buildSubMeshData(const int *start, const int *end, DataArrayInt *&di) const
402 {
403   if(!((const MEDCouplingFieldDiscretization *)_type))
404     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call buildSubMeshData method !");
405   return _type->buildSubMeshData(_mesh,start,end,di);
406 }
407
408 /*!
409  * This method returns a submesh of 'mesh' instance constituting cell ids defined by a range given by the 3 following inputs \a begin, \a end and \a step.
410  * 
411  * \param [out] beginOut Valid only if \a di is NULL
412  * \param [out] endOut Valid only if \a di is NULL
413  * \param [out] stepOut Valid only if \a di is NULL
414  * \param [out] di is an array returned that specifies entity ids (nodes, cells, Gauss points... ) in array if no output range is foundable.
415  * 
416  * \sa MEDCouplingField::buildSubMeshData
417  */
418 MEDCouplingMesh *MEDCouplingField::buildSubMeshDataRange(int begin, int end, int step, int& beginOut, int& endOut, int& stepOut, DataArrayInt *&di) const
419 {
420   if(!((const MEDCouplingFieldDiscretization *)_type))
421     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call buildSubMeshDataRange method !");
422   return _type->buildSubMeshDataRange(_mesh,begin,end,step,beginOut,endOut,stepOut,di);
423 }
424
425 /*!
426  * This method returns tuples ids implied by the mesh selection of the  cell ids contained in array defined as an interval [start;end).
427  * \return a newly allocated DataArrayInt instance containing tuples ids.
428  */
429 DataArrayInt *MEDCouplingField::computeTupleIdsToSelectFromCellIds(const int *startCellIds, const int *endCellIds) const
430 {
431   if(!((const MEDCouplingFieldDiscretization *)_type))
432     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call computeTupleIdsToSelectFromCellIds method !");
433   return _type->computeTupleIdsToSelectFromCellIds(_mesh,startCellIds,endCellIds);
434 }
435
436 /*!
437  * This method returns number of tuples expected regarding its discretization and its _mesh attribute.
438  * This method expected a not null _mesh instance. If null, an exception will be thrown.
439  */
440 int MEDCouplingField::getNumberOfTuplesExpected() const throw(INTERP_KERNEL::Exception)
441 {
442   if(!((const MEDCouplingFieldDiscretization *)_type))
443     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getNumberOfTuplesExpected method !");
444   if(_mesh)
445     return _type->getNumberOfTuples(_mesh);
446   else
447     throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfTuplesExpected : Empty mesh !");
448 }
449
450 void MEDCouplingField::setDiscretization(MEDCouplingFieldDiscretization *newDisc)
451 {
452   bool needUpdate=(const MEDCouplingFieldDiscretization *)_type!=newDisc;
453   _type=newDisc;
454   if(newDisc)
455     newDisc->incrRef();
456   if(needUpdate)
457     declareAsNew();
458 }
459
460 /*!
461  * This method returns number of mesh placed expected regarding its discretization and its _mesh attribute.
462  * This method expected a not null _mesh instance. If null, an exception will be thrown.
463  */
464 int MEDCouplingField::getNumberOfMeshPlacesExpected() const throw(INTERP_KERNEL::Exception)
465 {
466   if(!((const MEDCouplingFieldDiscretization *)_type))
467     throw INTERP_KERNEL::Exception("Spatial discretization not set ! Impossible to call getNumberOfMeshPlacesExpected method !");
468   if(_mesh)
469     return _type->getNumberOfMeshPlaces(_mesh);
470   else
471     throw INTERP_KERNEL::Exception("MEDCouplingField::getNumberOfMeshPlacesExpected : Empty mesh !");
472 }
473
474 /*!
475  * Copy tiny info (component names, name, description) but warning the underlying mesh is not renamed (for safety reason).
476  */
477 void MEDCouplingField::copyTinyStringsFrom(const MEDCouplingField *other) throw(INTERP_KERNEL::Exception)
478 {
479   if(other)
480     {
481       setName(other->_name.c_str());
482       setDescription(other->_desc.c_str());    
483     }
484 }