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