Salome HOME
Modification of the getHeapMemorySize computation.
[modules/med.git] / src / MEDLoader / MEDFileParameter.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 "MEDFileParameter.hxx"
22 #include "MEDFileUtilities.hxx"
23 #include "MEDLoaderBase.hxx"
24
25 #include "InterpKernelAutoPtr.hxx"
26
27 #include <set>
28
29 using namespace ParaMEDMEM;
30
31 MEDFileParameter1TS::MEDFileParameter1TS(int iteration, int order, double time):_iteration(iteration),_order(order),_time(time)
32 {
33 }
34
35 MEDFileParameter1TS::MEDFileParameter1TS():_iteration(-1),_order(-1),_time(0.)
36 {
37 }
38
39 bool MEDFileParameter1TS::isEqual(const MEDFileParameter1TS *other, double eps, std::string& what) const
40 {
41   std::ostringstream oss;
42   if(!other)
43     { what="Other is null"; return false; }
44   if(_iteration!=other->_iteration)
45     { oss << "iteration number mismatches " << _iteration << " != " << other->_iteration; what=oss.str(); return false; }
46   if(_order!=other->_order)
47     { oss << "order number mismatches " << _iteration << " != " << other->_iteration; what=oss.str(); return false; }
48   if(fabs(_time-other->_time)>eps)
49     { oss << "time mismatches " << _time << " != " << other->_time << " eps=" << eps; what=oss.str(); return false; }
50   return true;
51 }
52
53 MEDFileParameter1TS *MEDFileParameterDouble1TSWTI::deepCpy() const throw(INTERP_KERNEL::Exception)
54 {
55   return new MEDFileParameterDouble1TSWTI(*this);
56 }
57
58 bool MEDFileParameterDouble1TSWTI::isEqual(const MEDFileParameter1TS *other, double eps, std::string& what) const
59 {
60   if(!MEDFileParameter1TS::isEqual(other,eps,what))
61     return false;
62   const MEDFileParameterDouble1TSWTI *otherC=dynamic_cast<const MEDFileParameterDouble1TSWTI *>(other);
63   if(!otherC)
64     { what="IsEqual fails because this is double parameter other no !"; return false; }
65   if(fabs(_arr-otherC->_arr)>eps)
66     { std::ostringstream oss; oss << "value differ " << _arr << " != " << otherC->_arr << " (eps=" << eps << ")"; return false; }
67   return true;
68 }
69
70 std::size_t MEDFileParameterDouble1TSWTI::getHeapMemorySizeWithoutChildren() const
71 {
72   return sizeof(MEDFileParameterDouble1TSWTI);
73 }
74
75 std::vector<RefCountObject *> MEDFileParameterDouble1TSWTI::getDirectChildren() const
76 {
77   return std::vector<RefCountObject *>();
78 }
79
80 std::string MEDFileParameterDouble1TSWTI::simpleRepr() const
81 {
82   std::ostringstream oss;
83   simpleRepr2(0,oss);
84   return oss.str();
85 }
86
87 void MEDFileParameterDouble1TSWTI::simpleRepr2(int bkOffset, std::ostream& oss) const
88 {
89   std::string startOfLine(bkOffset,' ');
90   oss << startOfLine << "ParameterDoubleItem with (iteration,order) = (" << _iteration << "," << _order << ")" << std::endl;
91   oss << startOfLine << "Time associacited = " << _time << std::endl;
92   oss << startOfLine << "The value is ***** " << _arr <<  " *****" << std::endl;
93 }
94
95 MEDFileParameterDouble1TSWTI *MEDFileParameterDouble1TSWTI::New(int iteration, int order, double time)
96 {
97   return new MEDFileParameterDouble1TSWTI(iteration,order,time);
98 }
99
100 MEDFileParameterDouble1TSWTI::MEDFileParameterDouble1TSWTI():_arr(std::numeric_limits<double>::max())
101 {
102 }
103
104 MEDFileParameterDouble1TSWTI::MEDFileParameterDouble1TSWTI(int iteration, int order, double time):MEDFileParameter1TS(iteration,order,time)
105 {
106 }
107
108 void MEDFileParameterDouble1TSWTI::finishLoading(med_idt fid, const std::string& name, int dt, int it, int nbOfSteps) throw(INTERP_KERNEL::Exception)
109 {
110   std::ostringstream oss; oss << "MEDFileParameterDouble1TS::finishLoading : no specified time step (" << dt << "," << it << ") ! Time steps available : ";
111   for(int i=0;i<nbOfSteps;i++)
112     {
113       int locDt,locIt;
114       double tim;
115       MEDparameterComputationStepInfo(fid,name.c_str(),i+1,&locDt,&locIt,&tim);
116       if(dt==locDt && it==locIt)
117         {
118           _iteration=locDt; _order=locIt; _time=tim;
119           MEDparameterValueRd(fid,name.c_str(),_iteration,_order,reinterpret_cast<unsigned char *const>(&_arr));
120           return ;
121         }
122       else
123         {
124           oss << "(" << locDt << "," << locIt << ")";
125           if(i!=nbOfSteps-1)
126             oss << ", ";
127         }
128     }
129   throw INTERP_KERNEL::Exception(oss.str().c_str());
130 }
131
132 void MEDFileParameterDouble1TSWTI::readValue(med_idt fid, const std::string& name) throw(INTERP_KERNEL::Exception)
133 {
134   MEDparameterValueRd(fid,name.c_str(),_iteration,_order,reinterpret_cast<unsigned char *const>(&_arr));
135 }
136
137 void MEDFileParameterDouble1TSWTI::finishLoading(med_idt fid, const std::string& name, int timeStepId) throw(INTERP_KERNEL::Exception)
138 {
139   int locDt,locIt;
140   double dt;
141   MEDparameterComputationStepInfo(fid,name.c_str(),timeStepId+1,&locDt,&locIt,&dt);
142   _iteration=locDt; _order=locIt; _time=dt;
143   MEDparameterValueRd(fid,name.c_str(),_iteration,_order,reinterpret_cast<unsigned char *const>(&_arr));
144 }
145
146 void MEDFileParameterDouble1TSWTI::writeLL(med_idt fid, const std::string& name, const MEDFileWritable& mw) const throw(INTERP_KERNEL::Exception)
147 {
148   char nameW[MED_NAME_SIZE+1];
149   MEDLoaderBase::safeStrCpy(name.c_str(),MED_NAME_SIZE,nameW,mw.getTooLongStrPolicy());
150   MEDparameterValueWr(fid,nameW,_iteration,_order,_time,reinterpret_cast<const unsigned char *>(&_arr));
151 }
152
153 std::size_t MEDFileParameterTinyInfo::getHeapMemSizeOfStrings() const
154 {
155   return _dt_unit.capacity()+_name.capacity()+_desc_name.capacity();
156 }
157
158 bool MEDFileParameterTinyInfo::isEqualStrings(const MEDFileParameterTinyInfo& other, std::string& what) const
159 {
160   std::ostringstream oss;
161   if(_name!=other._name)
162     { oss << "name differ ! this=" << _name << " and other=" << other._name; what=oss.str(); return false; }
163   if(_desc_name!=other._desc_name)
164     { oss << "name differ ! this=" << _desc_name << " and other=" << other._desc_name; what=oss.str(); return false; }
165   if(_dt_unit!=other._dt_unit)
166     { oss << "unit of time differ ! this=" << _dt_unit << " and other=" << other._dt_unit; what=oss.str(); return false; }
167   return true;
168 }
169
170 void MEDFileParameterTinyInfo::writeLLHeader(med_idt fid, med_parameter_type typ) const throw(INTERP_KERNEL::Exception)
171 {
172   char nameW[MED_NAME_SIZE+1],descW[MED_COMMENT_SIZE+1],dtunitW[MED_SNAME_SIZE+1];
173   MEDLoaderBase::safeStrCpy(_name.c_str(),MED_NAME_SIZE,nameW,getTooLongStrPolicy());
174   MEDLoaderBase::safeStrCpy(_desc_name.c_str(),MED_COMMENT_SIZE,descW,getTooLongStrPolicy());
175   MEDLoaderBase::safeStrCpy(_dt_unit.c_str(),MED_SNAME_SIZE,dtunitW,getTooLongStrPolicy());
176   MEDparameterCr(fid,nameW,typ,descW,dtunitW);
177 }
178
179 void MEDFileParameterTinyInfo::mainRepr(int bkOffset, std::ostream& oss) const
180 {
181   std::string startOfLine(bkOffset,' ');
182   oss << startOfLine << "Parameter with name \"" << _name << "\"" << std::endl;
183   oss << startOfLine << "Parameter with description \"" << _desc_name << "\"" << std::endl;
184   oss << startOfLine << "Parameter with unit name \"" << _dt_unit << "\"" << std::endl;
185 }
186
187 MEDFileParameterDouble1TS *MEDFileParameterDouble1TS::New()
188 {
189   return new MEDFileParameterDouble1TS;
190 }
191
192 MEDFileParameterDouble1TS *MEDFileParameterDouble1TS::New(const char *fileName) throw(INTERP_KERNEL::Exception)
193 {
194   return new MEDFileParameterDouble1TS(fileName);
195 }
196
197 MEDFileParameterDouble1TS *MEDFileParameterDouble1TS::New(const char *fileName, const char *paramName) throw(INTERP_KERNEL::Exception)
198 {
199   return new MEDFileParameterDouble1TS(fileName,paramName);
200 }
201
202 MEDFileParameterDouble1TS *MEDFileParameterDouble1TS::New(const char *fileName, const char *paramName, int dt, int it) throw(INTERP_KERNEL::Exception)
203 {
204   return new MEDFileParameterDouble1TS(fileName,paramName,dt,it);
205 }
206
207 MEDFileParameterDouble1TS::MEDFileParameterDouble1TS()
208 {
209 }
210
211 MEDFileParameterDouble1TS::MEDFileParameterDouble1TS(const char *fileName, const char *paramName, int dt, int it) throw(INTERP_KERNEL::Exception)
212 {
213   MEDFileUtilities::CheckFileForRead(fileName);
214   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
215   int nbPar=MEDnParameter(fid);
216   std::ostringstream oss; oss << "MEDFileParameterDouble1TS : no double param name \"" << paramName << "\" ! Double Parameters available are : ";
217   INTERP_KERNEL::AutoPtr<char> pName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
218   INTERP_KERNEL::AutoPtr<char> descName=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
219   INTERP_KERNEL::AutoPtr<char> unitName=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
220   med_parameter_type paramType;
221   for(int i=0;i<nbPar;i++)
222     {
223       int nbOfSteps;
224       MEDparameterInfo(fid,i+1,pName,&paramType,descName,unitName,&nbOfSteps);
225       std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
226       if(paramNameCpp==paramName && paramType==MED_FLOAT64)
227         {
228           _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
229           _name=paramNameCpp;
230           _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
231           finishLoading(fid,_name,dt,it,nbOfSteps);
232           return ;
233         }
234       else
235         {
236           oss << paramNameCpp;
237           if(i!=nbPar-1) oss << ", ";
238         }
239     }
240   throw INTERP_KERNEL::Exception(oss.str().c_str());
241 }
242
243 MEDFileParameterDouble1TS::MEDFileParameterDouble1TS(const char *fileName, const char *paramName) throw(INTERP_KERNEL::Exception)
244 {
245   MEDFileUtilities::CheckFileForRead(fileName);
246   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
247   int nbPar=MEDnParameter(fid);
248   std::ostringstream oss; oss << "MEDFileParameterDouble1TS : no double param name \"" << paramName << "\" ! Double Parameters available are : ";
249   INTERP_KERNEL::AutoPtr<char> pName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
250   INTERP_KERNEL::AutoPtr<char> descName=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
251   INTERP_KERNEL::AutoPtr<char> unitName=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
252   med_parameter_type paramType;
253   for(int i=0;i<nbPar;i++)
254     {
255       int nbOfSteps;
256       MEDparameterInfo(fid,i+1,pName,&paramType,descName,unitName,&nbOfSteps);
257       std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
258       if(paramNameCpp==paramName && paramType==MED_FLOAT64)
259         {
260           if(nbOfSteps>0)
261             {
262               _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
263               _name=paramNameCpp;
264               _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
265               finishLoading(fid,_name,0);
266               return ;
267             }
268           else
269             {
270               std::ostringstream oss2; oss2 << "Param name \"" << paramName << "\" exists but no time steps on it !";
271               throw INTERP_KERNEL::Exception(oss2.str().c_str());
272             }
273         }
274       else
275         {
276           oss << paramNameCpp;
277           if(i!=nbPar-1) oss << ", ";
278         }
279     }
280   throw INTERP_KERNEL::Exception(oss.str().c_str());
281 }
282
283 MEDFileParameterDouble1TS::MEDFileParameterDouble1TS(const char *fileName) throw(INTERP_KERNEL::Exception)
284 {
285   MEDFileUtilities::CheckFileForRead(fileName);
286   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
287   int nbPar=MEDnParameter(fid);
288   if(nbPar<1)
289     {
290       std::ostringstream oss2; oss2 << "No parameter in file \"" << fileName << "\" !";  
291       throw INTERP_KERNEL::Exception(oss2.str().c_str());
292     }
293   INTERP_KERNEL::AutoPtr<char> pName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
294   INTERP_KERNEL::AutoPtr<char> descName=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
295   INTERP_KERNEL::AutoPtr<char> unitName=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
296   med_parameter_type paramType;
297   int nbOfSteps;
298   MEDparameterInfo(fid,1,pName,&paramType,descName,unitName,&nbOfSteps);
299   std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
300   if(paramType==MED_FLOAT64)
301     {
302       if(nbOfSteps>0)
303         {
304           _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
305           _name=paramNameCpp;
306           _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
307           finishLoading(fid,_name,0);
308           return ;
309         }
310       else
311         {
312           std::ostringstream oss2; oss2 << "Double param name \"" << paramNameCpp << "\" exists in file \""<< fileName << "\"but no time steps on it !";
313           throw INTERP_KERNEL::Exception(oss2.str().c_str());
314         }
315     }
316   else
317     {
318       std::ostringstream oss2; oss2 << "First parameter in file \"" << fileName << "\" is not double !";  
319       throw INTERP_KERNEL::Exception(oss2.str().c_str());
320     }
321 }
322
323 bool MEDFileParameterDouble1TS::isEqual(const MEDFileParameter1TS *other, double eps, std::string& what) const
324 {
325   if(!MEDFileParameterDouble1TSWTI::isEqual(other,eps,what))
326     return false;
327   const MEDFileParameterDouble1TS *otherC=dynamic_cast<const MEDFileParameterDouble1TS *>(other);
328   if(!otherC)
329     { what="Other is not of type MEDFileParameterDouble1TS as this"; return false; }
330   if(!isEqualStrings(*otherC,what))
331     return false;
332   return true;
333 }
334
335 MEDFileParameter1TS *MEDFileParameterDouble1TS::deepCpy() const throw(INTERP_KERNEL::Exception)
336 {
337   return new MEDFileParameterDouble1TS(*this);
338 }
339
340 std::string MEDFileParameterDouble1TS::simpleRepr() const
341 {
342   std::ostringstream oss;
343   MEDFileParameterTinyInfo::mainRepr(0,oss);
344   MEDFileParameterDouble1TSWTI::simpleRepr2(0,oss);
345   return oss.str();
346 }
347
348 std::size_t MEDFileParameterDouble1TS::getHeapMemorySizeWithoutChildren() const
349 {
350   return getHeapMemSizeOfStrings()+sizeof(MEDFileParameterDouble1TS);
351 }
352
353 std::vector<RefCountObject *> MEDFileParameterDouble1TS::getDirectChildren() const
354 {
355   return std::vector<RefCountObject *>();
356 }
357
358 void MEDFileParameterDouble1TS::write(const char *fileName, int mode) const throw(INTERP_KERNEL::Exception)
359 {
360   med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode);
361   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,medmod);
362   MEDFileParameterTinyInfo::writeLLHeader(fid,MED_FLOAT64);
363   MEDFileParameterDouble1TSWTI::writeLL(fid,_name,*this);
364 }
365
366 MEDFileParameterMultiTS *MEDFileParameterMultiTS::New()
367 {
368   return new MEDFileParameterMultiTS;
369 }
370
371 MEDFileParameterMultiTS *MEDFileParameterMultiTS::New(const char *fileName) throw(INTERP_KERNEL::Exception)
372 {
373   return new MEDFileParameterMultiTS(fileName);
374 }
375
376 MEDFileParameterMultiTS *MEDFileParameterMultiTS::New(const char *fileName, const char *paramName) throw(INTERP_KERNEL::Exception)
377 {
378   return new MEDFileParameterMultiTS(fileName,paramName);
379 }
380
381 MEDFileParameterMultiTS::MEDFileParameterMultiTS()
382 {
383 }
384
385 MEDFileParameterMultiTS::MEDFileParameterMultiTS(const MEDFileParameterMultiTS& other, bool deepCopy):MEDFileParameterTinyInfo(other),_param_per_ts(other._param_per_ts)
386 {
387   if(deepCopy)
388     for(std::size_t i=0;i<_param_per_ts.size();i++)
389       {
390         const MEDFileParameter1TS *elt=_param_per_ts[i];
391         if(elt)
392           _param_per_ts[i]=elt->deepCpy();
393       }
394 }
395
396 MEDFileParameterMultiTS::MEDFileParameterMultiTS(const char *fileName) throw(INTERP_KERNEL::Exception)
397 {
398   MEDFileUtilities::CheckFileForRead(fileName);
399   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
400   int nbPar=MEDnParameter(fid);
401   if(nbPar<1)
402     {
403       std::ostringstream oss; oss << "MEDFileParameterMultiTS : no parameter in file \"" << fileName << "\" !" ;
404       throw INTERP_KERNEL::Exception(oss.str().c_str());
405     }
406   INTERP_KERNEL::AutoPtr<char> pName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
407   INTERP_KERNEL::AutoPtr<char> descName=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
408   INTERP_KERNEL::AutoPtr<char> unitName=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
409   med_parameter_type paramType;
410   int nbOfSteps;
411   MEDparameterInfo(fid,1,pName,&paramType,descName,unitName,&nbOfSteps);
412   std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
413   _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
414   _name=paramNameCpp;
415   _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
416   finishLoading(fid,paramType,nbOfSteps);
417 }
418
419 MEDFileParameterMultiTS::MEDFileParameterMultiTS(const char *fileName, const char *paramName) throw(INTERP_KERNEL::Exception)
420 {
421   MEDFileUtilities::CheckFileForRead(fileName);
422   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
423   int nbPar=MEDnParameter(fid);
424   std::ostringstream oss; oss << "MEDFileParameterDouble1TS : no double param name \"" << paramName << "\" ! Double Parameters available are : ";
425   INTERP_KERNEL::AutoPtr<char> pName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
426   INTERP_KERNEL::AutoPtr<char> descName=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
427   INTERP_KERNEL::AutoPtr<char> unitName=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
428   med_parameter_type paramType;
429   for(int i=0;i<nbPar;i++)
430     {
431       int nbOfSteps;
432       MEDparameterInfo(fid,i+1,pName,&paramType,descName,unitName,&nbOfSteps);
433       std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
434       if(paramNameCpp==paramName)
435         {
436           if(nbOfSteps>0)
437             {
438               _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
439               _name=paramNameCpp;
440               _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
441               finishLoading(fid,paramType,nbOfSteps);
442               return ;
443             }
444           else
445             {
446               std::ostringstream oss2; oss2 << "Param name \"" << paramName << "\" exists but no time steps on it !";
447               throw INTERP_KERNEL::Exception(oss2.str().c_str());
448             }
449         }
450       else
451         {
452           oss << paramNameCpp;
453           if(i!=nbPar-1) oss << ", ";
454         }
455     }
456   throw INTERP_KERNEL::Exception(oss.str().c_str());
457 }
458
459 void MEDFileParameterMultiTS::finishLoading(med_idt fid, med_parameter_type typ, int nbOfSteps) throw(INTERP_KERNEL::Exception)
460 {
461   _param_per_ts.resize(nbOfSteps);
462   for(int i=0;i<nbOfSteps;i++)
463     {
464       int dt,it;
465       double tim;
466       MEDparameterComputationStepInfo(fid,_name.c_str(),i+1,&dt,&it,&tim);
467       switch(typ)
468         {
469         case MED_FLOAT64:
470           _param_per_ts[i]=MEDFileParameterDouble1TSWTI::New(dt,it,tim);
471           _param_per_ts[i]->readValue(fid,_name.c_str());
472           break;
473           /*case MED_INT32;
474          _param_per_ts[i]=;
475          break;*/
476         default:
477           throw INTERP_KERNEL::Exception("MEDFileParameterMultiTS::finishLoading : supporting only FLOAT64 !");
478         }
479     }
480 }
481
482 std::size_t MEDFileParameterMultiTS::getHeapMemorySizeWithoutChildren() const
483 {
484   std::size_t ret(sizeof(MEDFileParameterMultiTS));
485   ret+=sizeof(MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS>)*_param_per_ts.capacity();
486   return ret;
487 }
488
489 std::vector<RefCountObject *> MEDFileParameterMultiTS::getDirectChildren() const
490 {
491   std::vector<RefCountObject *> ret;
492   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
493     {
494       const MEDFileParameter1TS *elt(*it);
495       if(elt)
496         ret.push_back(const_cast<MEDFileParameter1TS *>(elt));
497     }
498   return ret;
499 }
500
501 MEDFileParameterMultiTS *MEDFileParameterMultiTS::deepCpy() const throw(INTERP_KERNEL::Exception)
502 {
503   return new MEDFileParameterMultiTS(*this,true);
504 }
505
506 bool MEDFileParameterMultiTS::isEqual(const MEDFileParameterMultiTS *other, double eps, std::string& what) const
507 {
508   if(!other)
509     { what="other is null !"; return false; }
510   if(_param_per_ts.size()!=other->_param_per_ts.size())
511     { what="number of time steps differs !"; return false; }
512   std::ostringstream oss;
513   for(std::size_t i=0;i<_param_per_ts.size();i++)
514     {
515       const MEDFileParameter1TS *a(_param_per_ts[i]),*b(other->_param_per_ts[i]);
516       if((a && !b) || (!a && b))
517         { oss << "At time step id #" << i << " pointer is defined on one side not in the other !"; what=oss.str(); return false; }
518       if(a)
519         if(!a->isEqual(b,eps,what))
520           { oss << " At time step id #" << i << " non equality !"; what+=oss.str(); return false; }
521     }
522   return true;
523 }
524
525 void MEDFileParameterMultiTS::write(const char *fileName, int mode) const throw(INTERP_KERNEL::Exception)
526 {
527   med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode);
528   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,medmod);
529   writeLL(fid,*this);
530 }
531
532 void MEDFileParameterMultiTS::writeLL(med_idt fid, const MEDFileWritable& mw) const throw(INTERP_KERNEL::Exception)
533 {
534   std::set<med_parameter_type> diffType;
535   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
536     {
537       const MEDFileParameter1TS *elt(*it);
538       if(dynamic_cast<const MEDFileParameterDouble1TSWTI *>(elt))
539         diffType.insert(MED_FLOAT64);
540     }
541   if(diffType.size()>1)
542     throw INTERP_KERNEL::Exception("MEDFileParameterMultiTS::writeLL : impossible to mix type of data in parameters in MED file ! Only float64 or only int32 ...");
543   if(diffType.empty())
544     return;
545   med_parameter_type typ=*diffType.begin();
546   MEDFileParameterTinyInfo::writeLLHeader(fid,typ);
547   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
548     {
549       const MEDFileParameter1TS *elt(*it);
550       if(elt)
551         elt->writeLL(fid,_name,mw);
552     }
553 }
554
555 std::string MEDFileParameterMultiTS::simpleRepr() const
556 {
557   std::ostringstream oss;
558   simpleRepr2(0,oss);
559   return oss.str();
560 }
561
562 void MEDFileParameterMultiTS::simpleRepr2(int bkOffset, std::ostream& oss) const
563 {
564   MEDFileParameterTinyInfo::mainRepr(bkOffset,oss);
565   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
566     {
567       const MEDFileParameter1TS *elt(*it);
568       if(elt)
569         elt->simpleRepr2(bkOffset+2,oss);
570     }
571 }
572
573 void MEDFileParameterMultiTS::appendValue(int dt, int it, double time, double val) throw(INTERP_KERNEL::Exception)
574 {
575   MEDCouplingAutoRefCountObjectPtr<MEDFileParameterDouble1TSWTI> elt=MEDFileParameterDouble1TSWTI::New(dt,it,time);
576   elt->setValue(val);
577   MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> elt2((MEDFileParameterDouble1TSWTI*)elt); elt2->incrRef();
578   _param_per_ts.push_back(elt2);
579 }
580
581 double MEDFileParameterMultiTS::getDoubleValue(int iteration, int order) const throw(INTERP_KERNEL::Exception)
582 {
583   int pos=getPosOfTimeStep(iteration,order);
584   const MEDFileParameter1TS *elt=_param_per_ts[pos];
585   if(!elt)
586     {
587       std::ostringstream oss; oss << "MEDFileParameterMultiTS::getDoubleValue : time iteration it=" << iteration << " order=" << order;
588       oss << " exists but elt is empty !"; 
589       throw INTERP_KERNEL::Exception(oss.str().c_str());
590     }
591   const MEDFileParameterDouble1TSWTI *eltC=dynamic_cast<const MEDFileParameterDouble1TSWTI *>(elt);
592   if(!eltC)
593     {
594       std::ostringstream oss; oss << "MEDFileParameterMultiTS::getDoubleValue : time iteration it=" << iteration << " order=" << order;
595       oss << " exists but not double !"; 
596     }
597   return eltC->getValue();
598 }
599
600 int MEDFileParameterMultiTS::getPosOfTimeStep(int iteration, int order) const throw(INTERP_KERNEL::Exception)
601 {
602   int ret=0;
603   std::ostringstream oss; oss << "MEDFileParameterMultiTS::getPosOfTimeStep : no such iteration=" << iteration << " order=" << order << " ! Possibilities are :";
604   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++,ret++)
605     {
606       const MEDFileParameter1TS *elt(*it);
607       if(elt)
608         {
609           if(elt->getIteration()==iteration && elt->getOrder())
610             return ret;
611           else
612             oss << "(" << elt->getIteration() << "," << elt->getOrder() << "), ";
613         }
614     }
615   throw INTERP_KERNEL::Exception(oss.str().c_str());
616 }
617
618 int MEDFileParameterMultiTS::getPosGivenTime(double time, double eps) const throw(INTERP_KERNEL::Exception)
619 {
620   int ret=0;
621   std::ostringstream oss; oss << "MEDFileParameterMultiTS::getPosGivenTime : no such time=" << time << " ! Possibilities are :";
622   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++,ret++)
623     {
624       const MEDFileParameter1TS *elt(*it);
625       if(elt)
626         {
627           if(fabs(elt->getTimeValue()-time)<=eps)
628             return ret;
629           else
630             oss << elt->getTimeValue() << ", ";
631         }
632     }
633   throw INTERP_KERNEL::Exception(oss.str().c_str());
634 }
635
636 /*!
637  * \return an internal pointer that can be null. Warning the caller is \b not responsible of the returned pointer.
638  */
639 MEDFileParameter1TS *MEDFileParameterMultiTS::getTimeStepAtPos(int posId) const throw(INTERP_KERNEL::Exception)
640 {
641   if(posId<0 || posId>=(int)_param_per_ts.size())
642     {
643       std::ostringstream oss; oss << "MEDFileParameterMultiTS::getTimeStepAtPos : invalid pos ! Should be in [0," << _param_per_ts.size() << ") !";
644       throw INTERP_KERNEL::Exception(oss.str().c_str());
645     }
646   return const_cast<MEDFileParameter1TS *>(static_cast<const MEDFileParameter1TS *>(_param_per_ts[posId]));
647 }
648
649 void MEDFileParameterMultiTS::eraseTimeStepIds(const int *startIds, const int *endIds) throw(INTERP_KERNEL::Exception)
650 {
651   std::vector<bool> b(_param_per_ts.size(),true);
652   int len=(int)_param_per_ts.size();
653   for(const int *w=startIds;w!=endIds;w++)
654     if(*w>=0 && *w<len)
655       b[*w]=false;
656     else
657       {
658         std::ostringstream oss; oss << "MEDFileParameterMultiTS::eraseTimeStepIds : At pos #" << std::distance(startIds,w) << " value is " << *w << " should be in [0," << len << ") !"; throw INTERP_KERNEL::Exception(oss.str().c_str());
659       }
660   std::size_t newNb=std::count(b.begin(),b.end(),true);
661   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> > paramPerTs(newNb);
662   std::size_t j=0;
663   for(std::size_t i=0;i<_param_per_ts.size();i++)
664     if(b[i])
665       paramPerTs[j++]=_param_per_ts[i];
666   _param_per_ts=paramPerTs;
667 }
668
669 std::vector< std::pair<int,int> > MEDFileParameterMultiTS::getIterations() const throw(INTERP_KERNEL::Exception)
670 {
671   std::vector< std::pair<int,int> > ret;
672   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
673     {
674       const MEDFileParameter1TS *elt(*it);
675       if(elt)
676         ret.push_back(std::pair<int,int>(elt->getIteration(),elt->getOrder()));
677     }
678   return ret;
679 }
680
681 /*!
682  * \param [out] ret1
683  */
684 std::vector< std::pair<int,int> > MEDFileParameterMultiTS::getTimeSteps(std::vector<double>& ret1) const throw(INTERP_KERNEL::Exception)
685 {
686   std::vector< std::pair<int,int> > ret0;
687   ret1.clear();
688   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
689     {
690       const MEDFileParameter1TS *elt(*it);
691       if(elt)
692         {
693           ret0.push_back(std::pair<int,int>(elt->getIteration(),elt->getOrder()));
694           ret1.push_back(elt->getTimeValue());
695         }
696     }
697   return ret0;
698 }
699
700 MEDFileParameters *MEDFileParameters::New()
701 {
702   return new MEDFileParameters;
703 }
704
705 MEDFileParameters *MEDFileParameters::New(const char *fileName) throw(INTERP_KERNEL::Exception)
706 {
707   return new MEDFileParameters(fileName);
708 }
709
710 MEDFileParameters::MEDFileParameters(const char *fileName) throw(INTERP_KERNEL::Exception)
711 {
712   MEDFileUtilities::CheckFileForRead(fileName);
713   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
714   int nbPar=MEDnParameter(fid);
715   _params.resize(nbPar);
716   INTERP_KERNEL::AutoPtr<char> pName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
717   INTERP_KERNEL::AutoPtr<char> descName=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
718   INTERP_KERNEL::AutoPtr<char> unitName=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
719   med_parameter_type paramType;
720   for(int i=0;i<nbPar;i++)
721     {
722       int nbOfSteps;
723       MEDparameterInfo(fid,i+1,pName,&paramType,descName,unitName,&nbOfSteps);
724       std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
725       _params[i]=MEDFileParameterMultiTS::New(fileName,paramNameCpp.c_str());
726     }
727 }
728
729 MEDFileParameters::MEDFileParameters()
730 {
731 }
732
733 std::size_t MEDFileParameters::getHeapMemorySizeWithoutChildren() const
734 {
735   std::size_t ret(sizeof(MEDFileParameters));
736   ret+=sizeof(MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS>)*_params.capacity();
737   return ret;
738 }
739
740 std::vector<RefCountObject *> MEDFileParameters::getDirectChildren() const
741 {
742   std::vector<RefCountObject *> ret;
743   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++)
744     {
745       const MEDFileParameterMultiTS *elt(*it);
746       if(elt)
747         ret.push_back(const_cast<MEDFileParameterMultiTS *>(elt));
748     }
749   return ret;
750 }
751
752 MEDFileParameters *MEDFileParameters::deepCpy() const throw(INTERP_KERNEL::Exception)
753 {
754   return new MEDFileParameters(*this,true);
755 }
756
757 bool MEDFileParameters::isEqual(const MEDFileParameters *other, double eps, std::string& what) const
758 {
759   if(!other)
760     { what="other is null !"; return false; }
761   if(_params.size()!=other->_params.size())
762     { what="number of parameters differs !"; return false; }
763   std::ostringstream oss;
764   for(std::size_t i=0;i<_params.size();i++)
765     {
766       const MEDFileParameterMultiTS *a(_params[i]),*b(other->_params[i]);
767       if((a && !b) || (!a && b))
768         { oss << "At param with id #" << i << " pointer is defined on one side not in the other !"; what=oss.str(); return false; }
769       if(a)
770         if(!a->isEqual(b,eps,what))
771           { oss << " At param with id #" << i << " non equality !"; what+=oss.str(); return false; }
772     }
773   return true;
774 }
775
776 MEDFileParameters::MEDFileParameters(const MEDFileParameters& other, bool deepCopy):MEDFileWritable(other),_params(other._params)
777 {
778   if(deepCopy)
779     for(std::size_t i=0;i<_params.size();i++)
780       {
781         const MEDFileParameterMultiTS *elt=_params[i];
782         if(elt)
783           _params[i]=elt->deepCpy();
784       }
785 }
786
787 void MEDFileParameters::write(const char *fileName, int mode) const throw(INTERP_KERNEL::Exception)
788 {
789   med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode);
790   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,medmod);
791   writeLL(fid);
792 }
793
794 void MEDFileParameters::writeLL(med_idt fid) const throw(INTERP_KERNEL::Exception)
795 {
796   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++)
797     {
798       const MEDFileParameterMultiTS *elt(*it);
799       if(elt)
800         elt->writeLL(fid,*this);
801     }
802 }
803
804 std::vector<std::string> MEDFileParameters::getParamsNames() const throw(INTERP_KERNEL::Exception)
805 {
806   std::vector<std::string> ret(_params.size());
807   int i=0;
808   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++,i++)
809     {
810       const MEDFileParameterMultiTS *p=(*it);
811       if(p)
812         {
813           ret[i]=p->getName();
814         }
815       else
816         {
817           std::ostringstream oss; oss << "MEDFileParameters::getParamsNames : At rank #" << i << " param is not defined !";
818           throw INTERP_KERNEL::Exception(oss.str().c_str());
819         }
820     }
821   return ret;
822 }
823
824 std::string MEDFileParameters::simpleRepr() const
825 {
826   std::ostringstream oss;
827   simpleReprWithoutHeader(oss);
828   return oss.str();
829 }
830
831 void MEDFileParameters::simpleReprWithoutHeader(std::ostream& oss) const
832 {
833   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++)
834     {
835       const MEDFileParameterMultiTS *elt(*it);
836       if(elt)
837         elt->simpleRepr2(2,oss);
838     }
839 }
840
841 void MEDFileParameters::resize(int newSize) throw(INTERP_KERNEL::Exception)
842 {
843   if(newSize<0)
844     throw INTERP_KERNEL::Exception("MEDFileParameters::resize : should be positive !");
845   _params.resize(newSize);
846 }
847
848 void MEDFileParameters::pushParam(MEDFileParameterMultiTS *param) throw(INTERP_KERNEL::Exception)
849 {
850   if(param)
851     param->incrRef();
852   MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> elt(param);
853   _params.push_back(elt);
854 }
855
856 void MEDFileParameters::setParamAtPos(int i, MEDFileParameterMultiTS *param) throw(INTERP_KERNEL::Exception)
857 {
858   if(i<0)
859     throw INTERP_KERNEL::Exception("MEDFileParameters::setParamAtPos : should be positive !");
860   if(i>=(int)_params.size())
861     _params.resize(i+1);
862   if(param)
863     param->incrRef();
864   MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> elt(param);
865   _params[i]=elt;
866 }
867
868 /*!
869  * \return an internal pointer that can be null. Warning the caller is \b not responsible of the returned pointer.
870  */
871 MEDFileParameterMultiTS *MEDFileParameters::getParamAtPos(int i) const throw(INTERP_KERNEL::Exception)
872 {
873   if(i<0 || i>=(int)_params.size())
874     {
875       std::ostringstream oss; oss << "MEDFileParameters::getParamAtPos : should be in [0," << _params.size() << ") !";
876       throw INTERP_KERNEL::Exception(oss.str().c_str());
877     }
878   const MEDFileParameterMultiTS *elt=_params[i];
879   return const_cast<MEDFileParameterMultiTS *>(elt);
880 }
881
882 /*!
883  * \return an internal pointer that can be null. Warning the caller is \b not responsible of the returned pointer.
884  */
885 MEDFileParameterMultiTS *MEDFileParameters::getParamWithName(const char *paramName) const throw(INTERP_KERNEL::Exception)
886 {
887   int pos=getPosFromParamName(paramName);
888   return getParamAtPos(pos);
889 }
890
891 void MEDFileParameters::destroyParamAtPos(int i) throw(INTERP_KERNEL::Exception)
892 {
893   if(i<0 || i>=(int)_params.size())
894     {
895       std::ostringstream oss; oss << "MEDFileParameters::destroyParamAtPos : should be in [0," << _params.size() << ") !";
896       throw INTERP_KERNEL::Exception(oss.str().c_str());
897     }
898   _params[i]=MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS>(0);
899 }
900
901 int MEDFileParameters::getPosFromParamName(const char *paramName) const throw(INTERP_KERNEL::Exception)
902 {
903   std::ostringstream oss; oss << "MEDFileParameters::getPosFromParamName : no such name=" << paramName << " ! Possibilities are :";
904   int ret=0;
905   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++,ret++)
906     {
907       const MEDFileParameterMultiTS *elt(*it);
908       if(elt)
909         {
910           if(std::string(elt->getName())==paramName)
911             return ret;
912           else
913             oss << elt->getName() << ", ";
914         }
915     }
916   throw INTERP_KERNEL::Exception(oss.str().c_str());
917 }
918
919 int MEDFileParameters::getNumberOfParams() const throw(INTERP_KERNEL::Exception)
920 {
921   return (int)_params.size();
922 }