Salome HOME
Merge 'agy/br810_1' branch.
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingFieldT.txx
1 // Copyright (C) 2016  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, or (at your option) any later version.
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 (EDF R&D)
20
21 #ifndef __MEDCOUPLINGFIELDT_TXX__
22 #define __MEDCOUPLINGFIELDT_TXX__
23
24 #include "MEDCouplingTimeDiscretization.hxx"
25 #include "MEDCouplingMesh.hxx"
26
27 namespace MEDCoupling
28 {
29   template<class T>
30   MEDCouplingFieldT<T>::MEDCouplingFieldT(const MEDCouplingFieldT<T>& other, bool deepCopy):MEDCouplingField(other,deepCopy),_time_discr(other._time_discr->performCopyOrIncrRef(deepCopy))
31   {
32   }
33   
34   /*!
35    * Checks if \a this field is correctly defined, else an exception is thrown.
36    *  \throw If the mesh is not set.
37    *  \throw If the data array is not set.
38    *  \throw If the spatial discretization of \a this field is NULL.
39    *  \throw If \a this->getTimeTolerance() < 0.
40    *  \throw If the temporal discretization data is incorrect.
41    *  \throw If mesh data does not correspond to field data.
42    */
43   template<class T>
44   void MEDCouplingFieldT<T>::checkConsistencyLight() const
45   {
46     MEDCouplingField::checkConsistencyLight();
47     _time_discr->checkConsistencyLight();
48     _type->checkCoherencyBetween(_mesh,getArray());
49   }
50
51   template<class T>
52   MEDCouplingFieldT<T>::MEDCouplingFieldT(const MEDCouplingField& other, MEDCouplingTimeDiscretizationTemplate<T> *timeDiscr, bool deepCopy):MEDCouplingField(other,deepCopy),_time_discr(timeDiscr)
53   {
54   }
55   
56   template<class T>
57   MEDCouplingFieldT<T>::MEDCouplingFieldT(TypeOfField type, MEDCouplingTimeDiscretizationTemplate<T> *timeDiscr):MEDCouplingField(type),_time_discr(timeDiscr)
58   {
59   }
60
61   template<class T>
62   MEDCouplingFieldT<T>::MEDCouplingFieldT(MEDCouplingFieldDiscretization *type, NatureOfField n, MEDCouplingTimeDiscretizationTemplate<T> *timeDiscr):MEDCouplingField(type,n),_time_discr(timeDiscr)
63   {
64   }
65
66   template<class T>
67   MEDCouplingFieldT<T>::~MEDCouplingFieldT()
68   {
69     delete _time_discr;
70   }
71
72   /*!
73    * This method synchronizes time information (time, iteration, order, time unit) regarding the information in \c this->_mesh.
74    * \throw If no mesh is set in this. Or if \a this is not compatible with time setting (typically NO_TIME)
75    */
76   template<class T>
77   void MEDCouplingFieldT<T>::synchronizeTimeWithMesh()
78   {
79     if(!_mesh)
80       throw INTERP_KERNEL::Exception("MEDCouplingFieldT::synchronizeTimeWithMesh : no mesh set in this !");
81     int it(-1),ordr(-1);
82     double val(_mesh->getTime(it,ordr));
83     std::string timeUnit(_mesh->getTimeUnit());
84     setTime(val,it,ordr);
85     setTimeUnit(timeUnit);
86   }
87
88   /*!
89    * Returns a new MEDCouplingFieldDouble which is a copy of \a this one. The data
90    * of \a this field is copied either deep or shallow depending on \a recDeepCpy
91    * parameter. But the underlying mesh is always deep copied.
92    * Data that can be copied either deeply or shallow are:
93    * - \ref MEDCouplingTemporalDisc "temporal discretization" data that holds array(s)
94    * of field values,
95    * - \ref MEDCouplingSpatialDisc "a spatial discretization".
96    * 
97    * This method behaves exactly like clone() except that here the underlying **mesh is
98    * always deeply duplicated**, whatever the value \a recDeepCpy parameter.
99    * The result of \c cloneWithMesh(true) is exactly the same as that of deepCopy().
100    * So the resulting field can not be used together with \a this one in the methods
101    * like operator+(), operator*() etc. To avoid deep copying the underlying mesh,
102    * the user can call clone().
103    *  \param [in] recDeepCpy - if \c true, the copy of the underlying data arrays is
104    *         deep, else all data arrays of \a this field are shared by the new field.
105    *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble. The
106    *         caller is to delete this field using decrRef() as it is no more needed.
107    * \sa clone()
108    */
109   template<class T>
110   typename Traits<T>::FieldType *MEDCouplingFieldT<T>::cloneWithMesh(bool recDeepCpy) const
111   {
112     MCAuto<  typename Traits<T>::FieldType > ret(clone(recDeepCpy));
113     if(_mesh)
114       {
115         MCAuto<MEDCouplingMesh> mCpy(_mesh->deepCopy());
116         ret->setMesh(mCpy);
117       }
118     return ret.retn();
119   }
120   
121   template<class T>
122   bool MEDCouplingFieldT<T>::isEqualIfNotWhy(const MEDCouplingField *other, double meshPrec, double valsPrec, std::string& reason) const
123   {
124     if(!other)
125       throw INTERP_KERNEL::Exception("MEDCouplingFieldT::isEqualIfNotWhy : other instance is NULL !");
126     const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
127     if(!otherC)
128       {
129         reason="field given in input is not castable in MEDCouplingFieldT !";
130         return false;
131       }
132     if(!MEDCouplingField::isEqualIfNotWhy(other,meshPrec,valsPrec,reason))
133       return false;
134     if(!_time_discr->isEqualIfNotWhy(otherC->_time_discr,T(valsPrec),reason))
135       {
136         reason.insert(0,"In FieldT time discretizations differ :");
137         return false;
138       }
139     return true;
140   }
141   
142   /*!
143    * Checks equality of \a this and \a other field. Only numeric data is considered,
144    * i.e. names, description etc are not compared.
145    *  \param [in] other - the field to compare with.
146    *  \param [in] meshPrec - a precision used to compare node coordinates of meshes.
147    *  \param [in] valsPrec - a precision used to compare data arrays of the two fields.
148    *  \return bool - \c true if the two fields are equal, \c false else.
149    *  \throw If \a other == NULL.
150    *  \throw If the spatial discretization of \a this field is NULL.
151    */
152   template<class T>
153   bool MEDCouplingFieldT<T>::isEqualWithoutConsideringStr(const MEDCouplingField *other, double meshPrec, double valsPrec) const
154   {
155     const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
156     if(!otherC)
157       return false;
158     if(!MEDCouplingField::isEqualWithoutConsideringStr(other,meshPrec,valsPrec))
159       return false;
160     if(!_time_discr->isEqualWithoutConsideringStr(otherC->_time_discr,T(valsPrec)))
161       return false;
162     return true;
163   }
164   
165   /*!
166    * Copies tiny info (component names, name and description) from an \a other field to
167    * \a this one.
168    * \warning The underlying mesh is not renamed (for safety reason).
169    *  \param [in] other - the field to copy the tiny info from.
170    *  \throw If \a this->getNumberOfComponents() != \a other->getNumberOfComponents()
171    */
172   template<class T>
173   void MEDCouplingFieldT<T>::copyTinyStringsFrom(const MEDCouplingField *other)
174   {
175     MEDCouplingField::copyTinyStringsFrom(other);
176     const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
177     if(otherC)
178       {
179         _time_discr->copyTinyStringsFrom(*otherC->_time_discr);
180       }
181   }
182
183   /*!
184    * Copies only times, order and iteration from an \a other field to
185    * \a this one. The underlying mesh is not impacted by this method.
186    * Arrays are not impacted neither.
187    *  \param [in] other - the field to tiny attributes from.
188    *  \throw If \a this->getNumberOfComponents() != \a other->getNumberOfComponents()
189    */
190   template<class T>
191   void MEDCouplingFieldT<T>::copyTinyAttrFrom(const MEDCouplingFieldT<T> *other)
192   {
193     if(other)
194       {
195         _time_discr->copyTinyAttrFrom(*other->_time_discr);
196       }
197   }
198   
199   template<class T>
200   void MEDCouplingFieldT<T>::copyAllTinyAttrFrom(const MEDCouplingFieldT<T> *other)
201   {
202     copyTinyStringsFrom(other);
203     copyTinyAttrFrom(other);
204   }
205   
206   /*!
207    * This method is more strict than MEDCouplingField::areCompatibleForMerge method.
208    * This method is used for operation on fields to operate a first check before attempting operation.
209    */
210   template<class T>
211   bool MEDCouplingFieldT<T>::areStrictlyCompatible(const MEDCouplingField *other) const
212   {
213     std::string tmp;
214     if(!MEDCouplingField::areStrictlyCompatible(other))
215       return false;
216     const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
217     if(!otherC)
218       return false;
219     if(!_time_discr->areStrictlyCompatible(otherC->_time_discr,tmp))
220       return false;
221     return true;
222   }
223   
224   template<class T>
225   bool MEDCouplingFieldT<T>::areStrictlyCompatibleForMulDiv(const MEDCouplingField *other) const
226   {
227     if(!MEDCouplingField::areStrictlyCompatibleForMulDiv(other))
228       return false;
229     const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
230     if(!otherC)
231       return false;
232     if(!_time_discr->areStrictlyCompatibleForDiv(otherC->_time_discr))
233       return false;
234     return true;
235   }
236
237   /*!
238    * Method with same principle than MEDCouplingFieldDouble::areStrictlyCompatibleForMulDiv method except that
239    * number of components between \a this and 'other' can be different here (for operator/).
240    */
241   template<class T>
242   bool MEDCouplingFieldT<T>::areCompatibleForDiv(const MEDCouplingField *other) const
243   {
244     if(!MEDCouplingField::areStrictlyCompatibleForMulDiv(other))
245       return false;
246     const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
247     if(!otherC)
248       return false;
249     if(!_time_discr->areStrictlyCompatibleForDiv(otherC->_time_discr))
250       return false;
251     return true;
252   }
253   
254   template<class T>
255   bool MEDCouplingFieldT<T>::areCompatibleForMul(const MEDCouplingField *other) const
256   {
257     if(!MEDCouplingField::areStrictlyCompatibleForMulDiv(other))
258       return false;
259     const MEDCouplingFieldT<T> *otherC(dynamic_cast<const MEDCouplingFieldT<T> *>(other));
260     if(!otherC)
261       return false;
262     if(!_time_discr->areStrictlyCompatibleForMul(otherC->_time_discr))
263       return false;
264     return true;
265   }
266
267   /*!
268    * Returns a string describing \a this field. This string is outputted by \c print
269    * Python command. The string includes info on
270    * - name,
271    * - description,
272    * - \ref MEDCouplingSpatialDisc "spatial discretization",
273    * - \ref MEDCouplingTemporalDisc "time discretization",
274    * - \ref NatureOfField,
275    * - components,
276    * - mesh.
277    *
278    *  \return std::string - the string describing \a this field.
279    */
280   template<class T>
281   std::string MEDCouplingFieldT<T>::simpleRepr() const
282   {
283     std::ostringstream ret;
284     ret << Traits<T>::FieldTypeName << " with name : \"" << getName() << "\"\n";
285     ret << "Description of field is : \"" << getDescription() << "\"\n";
286     if(_type)
287       { ret << Traits<T>::FieldTypeName << " space discretization is : " << _type->getStringRepr() << "\n"; }
288     else
289       { ret << Traits<T>::FieldTypeName << " has no spatial discretization !\n"; }
290     if(_time_discr)
291       { ret << Traits<T>::FieldTypeName << " time discretization is : " << _time_discr->getStringRepr() << "\n"; }
292     else
293       { ret << Traits<T>::FieldTypeName << " has no time discretization !\n"; }
294     ret << Traits<T>::FieldTypeName << " nature of field is : \"" << MEDCouplingNatureOfField::GetReprNoThrow(_nature) << "\"\n";
295     if(getArray())
296       {
297         if(getArray()->isAllocated())
298           {
299             int nbOfCompo=getArray()->getNumberOfComponents();
300             ret << Traits<T>::FieldTypeName << " default array has " << nbOfCompo << " components and " << getArray()->getNumberOfTuples() << " tuples.\n";
301             ret << Traits<T>::FieldTypeName << " default array has following info on components : ";
302             for(int i=0;i<nbOfCompo;i++)
303               ret << "\"" << getArray()->getInfoOnComponent(i) << "\" ";
304             ret << "\n";
305           }
306         else
307           {
308             ret << "Array set but not allocated !\n";
309           }
310       }
311     if(_mesh)
312       ret << "Mesh support information :\n__________________________\n" << _mesh->simpleRepr();
313     else
314       ret << "Mesh support information : No mesh set !\n";
315     return ret.str();
316   }
317   
318   template<class T>
319   void MEDCouplingFieldT<T>::reprQuickOverview(std::ostream& stream) const
320   {
321     stream << Traits<T>::FieldTypeName << " C++ instance at " << this << ". Name : \"" << _name << "\"." << std::endl;
322     const char *nat(0);
323     try
324       {
325         nat=MEDCouplingNatureOfField::GetRepr(_nature);
326         stream << "Nature of field : " << nat << ".\n";
327       }
328     catch(INTERP_KERNEL::Exception& e)
329       {  }
330     const MEDCouplingFieldDiscretization *fd(_type);
331     if(!fd)
332       stream << "No spatial discretization set !";
333     else
334       fd->reprQuickOverview(stream);
335     stream << std::endl;
336     if(!_mesh)
337       stream << "\nNo mesh support defined !";
338     else
339       {
340         std::ostringstream oss;
341         _mesh->reprQuickOverview(oss);
342         std::string tmp(oss.str());
343         stream << "\nMesh info : " << tmp.substr(0,tmp.find('\n'));
344       }
345     if(_time_discr)
346       {
347         const typename Traits<T>::ArrayType *arr(_time_discr->getArray());
348         if(arr)
349           {
350             stream << "\n\nArray info : ";
351             arr->reprQuickOverview(stream);
352           }
353         else
354           {
355             stream << "\n\nNo data array set !";
356           }
357       }
358   }
359
360   /*!
361    * Returns a type of \ref MEDCouplingTemporalDisc "time discretization" of \a this field.
362    *  \return MEDCoupling::TypeOfTimeDiscretization - an enum item describing the time
363    *          discretization type.
364    */
365   template<class T>
366   TypeOfTimeDiscretization MEDCouplingFieldT<T>::getTimeDiscretization() const
367   {
368     return _time_discr->getEnum();
369   }
370 }
371
372 #endif