Salome HOME
Merge 'agy/br810_1' branch.
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingTimeDiscretization.txx
1 // Copyright (C) 2007-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 __MEDCOUPLINGTIMEDISCRETIZATION_TXX__
22 #define __MEDCOUPLINGTIMEDISCRETIZATION_TXX__
23
24 #include "MEDCouplingTimeDiscretization.hxx"
25
26 #include <cmath>
27 #include <sstream>
28
29 namespace MEDCoupling
30 {
31   template<class T>
32   const double MEDCouplingTimeDiscretizationTemplate<T>::TIME_TOLERANCE_DFT=1.e-12;
33   
34   template<class T>
35   void MEDCouplingTimeDiscretizationTemplate<T>::updateTime() const
36   {
37     if(_array)
38       updateTimeWith(*_array);
39   }
40   
41   template<class T>
42   void MEDCouplingTimeDiscretizationTemplate<T>::setArray(typename Traits<T>::ArrayType *array, TimeLabel *owner)
43   {
44     if(array!=_array)
45       {
46         if(_array)
47         _array->decrRef();
48         _array=array;
49         if(_array)
50           _array->incrRef();
51         if(owner)
52           owner->declareAsNew();
53       }
54   }
55   
56   template<class T>
57   void MEDCouplingTimeDiscretizationTemplate<T>::copyTinyAttrFrom(const MEDCouplingTimeDiscretizationTemplate<T>& other)
58   {
59     _time_tolerance=other._time_tolerance;
60     _time_unit=other._time_unit;
61   }
62   
63   template<class T>
64   void MEDCouplingTimeDiscretizationTemplate<T>::copyTinyStringsFrom(const MEDCouplingTimeDiscretizationTemplate<T>& other)
65   {
66     _time_unit=other._time_unit;
67     if(_array && other._array)
68       _array->copyStringInfoFrom(*other._array);
69   }
70
71   template<class T>
72   std::size_t MEDCouplingTimeDiscretizationTemplate<T>::getHeapMemorySizeWithoutChildren() const
73   {
74     return _time_unit.capacity();
75   }
76   
77   template<class T>
78   void MEDCouplingTimeDiscretizationTemplate<T>::checkConsistencyLight() const
79   {
80     if(!_array)
81       throw INTERP_KERNEL::Exception("Field invalid because no values set !");
82     if(_time_tolerance<0.)
83       throw INTERP_KERNEL::Exception("time tolerance is expected to be greater than 0. !");
84   }
85   
86   template<class T>
87   std::vector<const BigMemoryObject *> MEDCouplingTimeDiscretizationTemplate<T>::getDirectChildrenWithNull() const
88   {
89     std::vector<const BigMemoryObject *> ret;
90     ret.push_back(_array);
91     return ret;
92   }
93   
94   template<class T>
95   bool MEDCouplingTimeDiscretizationTemplate<T>::areStrictlyCompatible(const MEDCouplingTimeDiscretizationTemplate<T> *other, std::string& reason) const
96   {
97     std::ostringstream oss; oss.precision(15);
98     if(_time_unit!=other->_time_unit)
99       {
100         oss << "Field discretizations differ : this time unit = \"" << _time_unit << "\" and other time unit = \"" << other->_time_unit << "\" !";
101         reason=oss.str();
102         return false;
103       }
104     if(std::fabs(_time_tolerance-other->_time_tolerance)>1.e-16)
105       {
106         oss << "Field discretizations differ : this time tolerance = \"" << _time_tolerance << "\" and other time tolerance = \"" << other->_time_tolerance << "\" !";
107         reason=oss.str();
108         return false;
109       }
110     if(_array==0 && other->_array==0)
111       return true;
112     if(_array==0 || other->_array==0)
113       {
114         reason="Field discretizations differ : Only one timediscretization between the two this and other has a DataArrayDouble for values defined";
115         return false;
116       }
117     if(_array->getNumberOfComponents()!=other->_array->getNumberOfComponents())
118       return false;
119     if(_array->getNumberOfTuples()!=other->_array->getNumberOfTuples())
120       return false;
121     return true;
122   }
123   
124   template<class T>
125   bool MEDCouplingTimeDiscretizationTemplate<T>::areCompatible(const MEDCouplingTimeDiscretizationTemplate<T> *other) const
126   {
127     if(std::fabs(_time_tolerance-other->_time_tolerance)>1.e-16)
128       return false;
129     if(_array==0 && other->_array==0)
130       return true;
131     if(_array==0 || other->_array==0)
132       return false;
133     if(_array->getNumberOfComponents()!=other->_array->getNumberOfComponents())
134       return false;
135     return true;
136   }
137   
138   template<class T>
139   bool MEDCouplingTimeDiscretizationTemplate<T>::areStrictlyCompatibleForMul(const MEDCouplingTimeDiscretizationTemplate<T> *other) const
140   {
141     if(std::fabs(_time_tolerance-other->_time_tolerance)>1.e-16)
142       return false;
143     if(_array==0 && other->_array==0)
144       return true;
145     if(_array==0 || other->_array==0)
146       return false;
147     int nbC1(_array->getNumberOfComponents()),nbC2(other->_array->getNumberOfComponents());
148     int nbMin(std::min(nbC1,nbC2));
149     if(nbC1!=nbC2 && nbMin!=1)
150       return false;
151     return true;
152   }
153   
154   template<class T>
155   bool MEDCouplingTimeDiscretizationTemplate<T>::areStrictlyCompatibleForDiv(const MEDCouplingTimeDiscretizationTemplate<T> *other) const
156   {
157     if(std::fabs(_time_tolerance-other->_time_tolerance)>1.e-16)
158       return false;
159     if(_array==0 && other->_array==0)
160       return true;
161     if(_array==0 || other->_array==0)
162       return false;
163     int nbC1(_array->getNumberOfComponents()),nbC2(other->_array->getNumberOfComponents());
164     if(nbC1!=nbC2 && nbC2!=1)
165       return false;
166     return true;
167   }
168
169   template<class T>
170   MEDCouplingTimeDiscretizationTemplate<T>::MEDCouplingTimeDiscretizationTemplate():_time_tolerance(TIME_TOLERANCE_DFT),_array(0)
171   {
172   }
173
174   template<class T>
175   MEDCouplingTimeDiscretizationTemplate<T>::MEDCouplingTimeDiscretizationTemplate(const MEDCouplingTimeDiscretizationTemplate<T>& other, bool deepCopy):_time_unit(other._time_unit),_time_tolerance(other._time_tolerance)
176   {
177     if(other._array)
178       _array=other._array->performCopyOrIncrRef(deepCopy);
179     else
180       _array=0;
181   }
182   
183   template<class T>
184   MEDCouplingTimeDiscretizationTemplate<T>::~MEDCouplingTimeDiscretizationTemplate()
185   {
186     if(_array)
187       _array->decrRef();
188   }
189   
190   template<class T>
191   void MEDCouplingTimeDiscretizationTemplate<T>::setEndArray(typename Traits<T>::ArrayType *array, TimeLabel *owner)
192   {
193     throw INTERP_KERNEL::Exception("setEndArray not available for this type of time discretization !");
194   }
195   
196   template<class T>
197   void MEDCouplingTimeDiscretizationTemplate<T>::setArrays(const std::vector<typename Traits<T>::ArrayType *>& arrays, TimeLabel *owner)
198   {
199     if(arrays.size()!=1)
200       throw INTERP_KERNEL::Exception("MEDCouplingTimeDiscretization::setArrays : number of arrays must be one.");
201     setArray(arrays.back(),owner);
202   }
203   
204   template<class T>
205   const typename Traits<T>::ArrayType *MEDCouplingTimeDiscretizationTemplate<T>::getEndArray() const
206   {
207     throw INTERP_KERNEL::Exception("getEndArray not available for this type of time discretization !");
208   }
209   
210   template<class T>
211   typename Traits<T>::ArrayType *MEDCouplingTimeDiscretizationTemplate<T>::getEndArray()
212   {
213     throw INTERP_KERNEL::Exception("getEndArray not available for this type of time discretization !");
214   }
215   
216   template<class T>
217   void MEDCouplingTimeDiscretizationTemplate<T>::getArrays(std::vector<typename Traits<T>::ArrayType *>& arrays) const
218   {
219     arrays.resize(1);
220     arrays[0]=_array;
221   }
222 }
223
224 #endif