Salome HOME
eed9db85b54872ce5ae1db1a358c63b98a57681c
[modules/med.git] / src / MEDCoupling / MEDCouplingFieldOverTime.cxx
1 // Copyright (C) 2007-2012  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
20 #include "MEDCouplingFieldOverTime.hxx"
21 #include "MEDCouplingMesh.hxx"
22
23 #include <cmath>
24
25 using namespace ParaMEDMEM;
26
27 MEDCouplingFieldOverTime *MEDCouplingFieldOverTime::New(const std::vector<MEDCouplingFieldDouble *>& fs) throw(INTERP_KERNEL::Exception)
28 {
29   return new MEDCouplingFieldOverTime(fs);
30 }
31
32 double MEDCouplingFieldOverTime::getTimeTolerance() const throw(INTERP_KERNEL::Exception)
33 {
34   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> >::const_iterator it=_fs.begin();
35   if(_fs.empty())
36     throw INTERP_KERNEL::Exception("MEDCouplingFieldOverTime::getTimeTolerance : empty set !");
37   for(;it!=_fs.end();it++)
38     if((const MEDCouplingFieldDouble *)(*it)!=0)
39       return (*it)->getTimeTolerance();
40   throw INTERP_KERNEL::Exception("MEDCouplingFieldOverTime::getTimeTolerance : only empty fields in this !");
41 }
42
43 void MEDCouplingFieldOverTime::checkCoherency() const throw(INTERP_KERNEL::Exception)
44 {
45   MEDCouplingMultiFields::checkCoherency();
46   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> >::const_iterator it=_fs.begin();
47   for(;it!=_fs.end();it++)
48     if((*it)->getTimeDiscretization()==NO_TIME)
49       {
50         std::ostringstream oss; oss << "MEDCouplingFieldOverTime::checkCoherency : At rank #" << std::distance(_fs.begin(),it) << " the field has no time !";
51         throw INTERP_KERNEL::Exception(oss.str().c_str());
52       }
53   if(_fs.empty())
54     return ;
55   it=_fs.begin();
56   const MEDCouplingFieldDouble& ref=*(*(it++));
57   int tt1,tt2;
58   double reft=ref.getEndTime(tt1,tt2);
59   double eps=getTimeTolerance();
60   int id=1;
61   for(;it!=_fs.end();it++,id++)
62     {
63       if(!ref.getMesh()->areCompatibleForMerge((*it)->getMesh()))
64         {
65           std::ostringstream oss; oss << "Field slice at rank #" << id << " is not compatible with the first !";
66           throw INTERP_KERNEL::Exception(oss.str().c_str());
67         }
68       double curt=(*it)->getStartTime(tt1,tt2);
69       if(curt<reft-eps)
70         throw INTERP_KERNEL::Exception("MEDCouplingFieldOverTime::checkCoherency : fields are NOT sorted properly in ascending time !");
71       reft=(*it)->getEndTime(tt1,tt2);
72     }
73 }
74
75 std::string MEDCouplingFieldOverTime::simpleRepr() const
76 {
77   std::ostringstream ret;
78   ret << "MEDCouplingFieldOverTime with name : \"" << getName() << "\"\n";
79   ret << "Description of MEDCouplingFieldOverTime is : \"" << getDescription() << "\"\n";
80   ret << "Number of discretization : " << _fs.size() << "\n";
81   ret << "Number of different meshes : ";
82   std::vector<MEDCouplingMesh *> ms;
83   std::vector<int> refms;
84   try
85     {
86       ms=getDifferentMeshes(refms);
87       ret << ms.size() << "\n";
88     }
89   catch(INTERP_KERNEL::Exception& e)
90     { ret << "Current instance is INVALID !\n"; }
91   try
92     {
93       MEDCouplingDefinitionTime dt=getDefinitionTimeZone();
94       dt.appendRepr(ret);
95     }
96   catch(INTERP_KERNEL::Exception& e)
97     { ret << "Definition zone is INVALID !\n"; }
98   return ret.str();
99 }
100
101 bool MEDCouplingFieldOverTime::isEqual(const MEDCouplingMultiFields *other, double meshPrec, double valsPrec) const
102 {
103   if(!MEDCouplingMultiFields::isEqual(other,meshPrec,valsPrec))
104     return false;
105   const MEDCouplingFieldOverTime *otherC=dynamic_cast<const MEDCouplingFieldOverTime *>(other);
106   if(!otherC)
107     return false;
108   // to implement
109   return true;
110 }
111
112 bool MEDCouplingFieldOverTime::isEqualWithoutConsideringStr(const MEDCouplingMultiFields *other, double meshPrec, double valsPrec) const
113 {
114   if(!MEDCouplingMultiFields::isEqualWithoutConsideringStr(other,meshPrec,valsPrec))
115     return false;
116   const MEDCouplingFieldOverTime *otherC=dynamic_cast<const MEDCouplingFieldOverTime *>(other);
117   if(!otherC)
118     return false;
119   // to implement
120   return true;
121 }
122
123 std::vector<MEDCouplingMesh *> MEDCouplingFieldOverTime::getMeshes() const throw(INTERP_KERNEL::Exception)
124 {
125   checkCoherency();
126   return MEDCouplingMultiFields::getMeshes();
127 }
128
129 std::vector<MEDCouplingMesh *> MEDCouplingFieldOverTime::getDifferentMeshes(std::vector<int>& refs) const throw(INTERP_KERNEL::Exception)
130 {
131   checkCoherency();
132   return MEDCouplingMultiFields::getDifferentMeshes(refs);
133 }
134
135 std::vector<DataArrayDouble *> MEDCouplingFieldOverTime::getArrays() const throw(INTERP_KERNEL::Exception)
136 {
137   checkCoherency();
138   return MEDCouplingMultiFields::getArrays();
139 }
140
141 std::vector<DataArrayDouble *> MEDCouplingFieldOverTime::getDifferentArrays(std::vector< std::vector<int> >& refs) const throw(INTERP_KERNEL::Exception)
142 {
143   checkCoherency();
144   return MEDCouplingMultiFields::getDifferentArrays(refs);
145 }
146
147 MEDCouplingDefinitionTime MEDCouplingFieldOverTime::getDefinitionTimeZone() const
148 {
149   std::vector< std::vector<int> > tmp;
150   getDifferentArrays(tmp);
151   std::vector<const MEDCouplingFieldDouble *> tmp2(_fs.begin(),_fs.end());
152   std::vector<int> tmp3;
153   getDifferentMeshes(tmp3);
154   return MEDCouplingDefinitionTime(tmp2,tmp3,tmp);
155 }
156
157 MEDCouplingFieldOverTime::MEDCouplingFieldOverTime(const std::vector<MEDCouplingFieldDouble *>& fs) throw(INTERP_KERNEL::Exception):MEDCouplingMultiFields(fs)
158 {
159   checkCoherency();
160 }
161
162 MEDCouplingFieldOverTime::MEDCouplingFieldOverTime()
163 {
164 }