1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "MEDFileParameter.hxx"
22 #include "MEDFileUtilities.hxx"
23 #include "MEDLoaderBase.hxx"
25 #include "InterpKernelAutoPtr.hxx"
29 using namespace ParaMEDMEM;
31 MEDFileParameter1TS::MEDFileParameter1TS(int iteration, int order, double time):_iteration(iteration),_order(order),_time(time)
35 MEDFileParameter1TS::MEDFileParameter1TS():_iteration(-1),_order(-1),_time(0.)
39 bool MEDFileParameter1TS::isEqual(const MEDFileParameter1TS *other, double eps, std::string& what) const
41 std::ostringstream oss;
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; }
53 MEDFileParameter1TS *MEDFileParameterDouble1TSWTI::deepCpy() const
55 return new MEDFileParameterDouble1TSWTI(*this);
58 bool MEDFileParameterDouble1TSWTI::isEqual(const MEDFileParameter1TS *other, double eps, std::string& what) const
60 if(!MEDFileParameter1TS::isEqual(other,eps,what))
62 const MEDFileParameterDouble1TSWTI *otherC=dynamic_cast<const MEDFileParameterDouble1TSWTI *>(other);
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; }
70 std::size_t MEDFileParameterDouble1TSWTI::getHeapMemorySizeWithoutChildren() const
72 return sizeof(MEDFileParameterDouble1TSWTI);
75 std::vector<const BigMemoryObject *> MEDFileParameterDouble1TSWTI::getDirectChildrenWithNull() const
77 return std::vector<const BigMemoryObject *>();
80 std::string MEDFileParameterDouble1TSWTI::simpleRepr() const
82 std::ostringstream oss;
87 void MEDFileParameterDouble1TSWTI::simpleRepr2(int bkOffset, std::ostream& oss) const
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;
95 MEDFileParameterDouble1TSWTI *MEDFileParameterDouble1TSWTI::New(int iteration, int order, double time)
97 return new MEDFileParameterDouble1TSWTI(iteration,order,time);
100 MEDFileParameterDouble1TSWTI::MEDFileParameterDouble1TSWTI():_arr(std::numeric_limits<double>::max())
104 MEDFileParameterDouble1TSWTI::MEDFileParameterDouble1TSWTI(int iteration, int order, double time):MEDFileParameter1TS(iteration,order,time)
108 void MEDFileParameterDouble1TSWTI::finishLoading(med_idt fid, const std::string& name, int dt, int it, int nbOfSteps)
110 std::ostringstream oss; oss << "MEDFileParameterDouble1TS::finishLoading : no specified time step (" << dt << "," << it << ") ! Time steps available : ";
111 for(int i=0;i<nbOfSteps;i++)
115 MEDparameterComputationStepInfo(fid,name.c_str(),i+1,&locDt,&locIt,&tim);
116 if(dt==locDt && it==locIt)
118 _iteration=locDt; _order=locIt; _time=tim;
119 MEDparameterValueRd(fid,name.c_str(),_iteration,_order,reinterpret_cast<unsigned char *const>(&_arr));
124 oss << "(" << locDt << "," << locIt << ")";
129 throw INTERP_KERNEL::Exception(oss.str().c_str());
132 void MEDFileParameterDouble1TSWTI::readValue(med_idt fid, const std::string& name)
134 MEDparameterValueRd(fid,name.c_str(),_iteration,_order,reinterpret_cast<unsigned char *const>(&_arr));
137 void MEDFileParameterDouble1TSWTI::finishLoading(med_idt fid, const std::string& name, int timeStepId)
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));
146 void MEDFileParameterDouble1TSWTI::writeLL(med_idt fid, const std::string& name, const MEDFileWritable& mw) const
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));
153 std::size_t MEDFileParameterTinyInfo::getHeapMemSizeOfStrings() const
155 return _dt_unit.capacity()+_name.capacity()+_desc_name.capacity();
158 bool MEDFileParameterTinyInfo::isEqualStrings(const MEDFileParameterTinyInfo& other, std::string& what) const
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; }
170 void MEDFileParameterTinyInfo::writeLLHeader(med_idt fid, med_parameter_type typ) const
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);
179 void MEDFileParameterTinyInfo::mainRepr(int bkOffset, std::ostream& oss) const
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;
187 MEDFileParameterDouble1TS *MEDFileParameterDouble1TS::New()
189 return new MEDFileParameterDouble1TS;
192 MEDFileParameterDouble1TS *MEDFileParameterDouble1TS::New(const std::string& fileName)
194 return new MEDFileParameterDouble1TS(fileName);
197 MEDFileParameterDouble1TS *MEDFileParameterDouble1TS::New(const std::string& fileName, const std::string& paramName)
199 return new MEDFileParameterDouble1TS(fileName,paramName);
202 MEDFileParameterDouble1TS *MEDFileParameterDouble1TS::New(const std::string& fileName, const std::string& paramName, int dt, int it)
204 return new MEDFileParameterDouble1TS(fileName,paramName,dt,it);
207 MEDFileParameterDouble1TS::MEDFileParameterDouble1TS()
211 MEDFileParameterDouble1TS::MEDFileParameterDouble1TS(const std::string& fileName, const std::string& paramName, int dt, int it)
213 MEDFileUtilities::CheckFileForRead(fileName);
214 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),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++)
224 MEDparameterInfo(fid,i+1,pName,¶mType,descName,unitName,&nbOfSteps);
225 std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
226 if(paramNameCpp==paramName && paramType==MED_FLOAT64)
228 _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
230 _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
231 finishLoading(fid,_name,dt,it,nbOfSteps);
237 if(i!=nbPar-1) oss << ", ";
240 throw INTERP_KERNEL::Exception(oss.str().c_str());
243 MEDFileParameterDouble1TS::MEDFileParameterDouble1TS(const std::string& fileName, const std::string& paramName)
245 MEDFileUtilities::CheckFileForRead(fileName);
246 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),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++)
256 MEDparameterInfo(fid,i+1,pName,¶mType,descName,unitName,&nbOfSteps);
257 std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
258 if(paramNameCpp==paramName && paramType==MED_FLOAT64)
262 _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
264 _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
265 finishLoading(fid,_name,0);
270 std::ostringstream oss2; oss2 << "Param name \"" << paramName << "\" exists but no time steps on it !";
271 throw INTERP_KERNEL::Exception(oss2.str().c_str());
277 if(i!=nbPar-1) oss << ", ";
280 throw INTERP_KERNEL::Exception(oss.str().c_str());
283 MEDFileParameterDouble1TS::MEDFileParameterDouble1TS(const std::string& fileName)
285 MEDFileUtilities::CheckFileForRead(fileName);
286 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
287 int nbPar=MEDnParameter(fid);
290 std::ostringstream oss2; oss2 << "No parameter in file \"" << fileName << "\" !";
291 throw INTERP_KERNEL::Exception(oss2.str().c_str());
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;
298 MEDparameterInfo(fid,1,pName,¶mType,descName,unitName,&nbOfSteps);
299 std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
300 if(paramType==MED_FLOAT64)
304 _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
306 _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
307 finishLoading(fid,_name,0);
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());
318 std::ostringstream oss2; oss2 << "First parameter in file \"" << fileName << "\" is not double !";
319 throw INTERP_KERNEL::Exception(oss2.str().c_str());
323 bool MEDFileParameterDouble1TS::isEqual(const MEDFileParameter1TS *other, double eps, std::string& what) const
325 if(!MEDFileParameterDouble1TSWTI::isEqual(other,eps,what))
327 const MEDFileParameterDouble1TS *otherC=dynamic_cast<const MEDFileParameterDouble1TS *>(other);
329 { what="Other is not of type MEDFileParameterDouble1TS as this"; return false; }
330 if(!isEqualStrings(*otherC,what))
335 MEDFileParameter1TS *MEDFileParameterDouble1TS::deepCpy() const
337 return new MEDFileParameterDouble1TS(*this);
340 std::string MEDFileParameterDouble1TS::simpleRepr() const
342 std::ostringstream oss;
343 MEDFileParameterTinyInfo::mainRepr(0,oss);
344 MEDFileParameterDouble1TSWTI::simpleRepr2(0,oss);
348 std::size_t MEDFileParameterDouble1TS::getHeapMemorySizeWithoutChildren() const
350 return getHeapMemSizeOfStrings()+sizeof(MEDFileParameterDouble1TS);
353 std::vector<const BigMemoryObject *> MEDFileParameterDouble1TS::getDirectChildrenWithNull() const
355 return std::vector<const BigMemoryObject *>();
358 void MEDFileParameterDouble1TS::write(const std::string& fileName, int mode) const
360 med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode);
361 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),medmod);
362 MEDFileParameterTinyInfo::writeLLHeader(fid,MED_FLOAT64);
363 MEDFileParameterDouble1TSWTI::writeLL(fid,_name,*this);
366 MEDFileParameterMultiTS *MEDFileParameterMultiTS::New()
368 return new MEDFileParameterMultiTS;
371 MEDFileParameterMultiTS *MEDFileParameterMultiTS::New(const std::string& fileName)
373 return new MEDFileParameterMultiTS(fileName);
376 MEDFileParameterMultiTS *MEDFileParameterMultiTS::New(const std::string& fileName, const std::string& paramName)
378 return new MEDFileParameterMultiTS(fileName,paramName);
381 MEDFileParameterMultiTS::MEDFileParameterMultiTS()
385 MEDFileParameterMultiTS::MEDFileParameterMultiTS(const MEDFileParameterMultiTS& other, bool deepCopy):MEDFileParameterTinyInfo(other),_param_per_ts(other._param_per_ts)
388 for(std::size_t i=0;i<_param_per_ts.size();i++)
390 const MEDFileParameter1TS *elt=_param_per_ts[i];
392 _param_per_ts[i]=elt->deepCpy();
396 MEDFileParameterMultiTS::MEDFileParameterMultiTS(const std::string& fileName)
398 MEDFileUtilities::CheckFileForRead(fileName);
399 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
400 int nbPar=MEDnParameter(fid);
403 std::ostringstream oss; oss << "MEDFileParameterMultiTS : no parameter in file \"" << fileName << "\" !" ;
404 throw INTERP_KERNEL::Exception(oss.str().c_str());
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;
411 MEDparameterInfo(fid,1,pName,¶mType,descName,unitName,&nbOfSteps);
412 std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
413 _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
415 _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
416 finishLoading(fid,paramType,nbOfSteps);
419 MEDFileParameterMultiTS::MEDFileParameterMultiTS(const std::string& fileName, const std::string& paramName)
421 MEDFileUtilities::CheckFileForRead(fileName);
422 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),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++)
432 MEDparameterInfo(fid,i+1,pName,¶mType,descName,unitName,&nbOfSteps);
433 std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
434 if(paramNameCpp==paramName)
438 _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
440 _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
441 finishLoading(fid,paramType,nbOfSteps);
446 std::ostringstream oss2; oss2 << "Param name \"" << paramName << "\" exists but no time steps on it !";
447 throw INTERP_KERNEL::Exception(oss2.str().c_str());
453 if(i!=nbPar-1) oss << ", ";
456 throw INTERP_KERNEL::Exception(oss.str().c_str());
459 void MEDFileParameterMultiTS::finishLoading(med_idt fid, med_parameter_type typ, int nbOfSteps)
461 _param_per_ts.resize(nbOfSteps);
462 for(int i=0;i<nbOfSteps;i++)
466 MEDparameterComputationStepInfo(fid,_name.c_str(),i+1,&dt,&it,&tim);
470 _param_per_ts[i]=MEDFileParameterDouble1TSWTI::New(dt,it,tim);
471 _param_per_ts[i]->readValue(fid,_name.c_str());
477 throw INTERP_KERNEL::Exception("MEDFileParameterMultiTS::finishLoading : supporting only FLOAT64 !");
482 std::size_t MEDFileParameterMultiTS::getHeapMemorySizeWithoutChildren() const
484 std::size_t ret(sizeof(MEDFileParameterMultiTS));
485 ret+=sizeof(MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS>)*_param_per_ts.capacity();
489 std::vector<const BigMemoryObject *> MEDFileParameterMultiTS::getDirectChildrenWithNull() const
491 std::vector<const BigMemoryObject *> ret;
492 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
493 ret.push_back((const MEDFileParameter1TS *)*it);
497 MEDFileParameterMultiTS *MEDFileParameterMultiTS::deepCpy() const
499 return new MEDFileParameterMultiTS(*this,true);
502 bool MEDFileParameterMultiTS::isEqual(const MEDFileParameterMultiTS *other, double eps, std::string& what) const
505 { what="other is null !"; return false; }
506 if(_param_per_ts.size()!=other->_param_per_ts.size())
507 { what="number of time steps differs !"; return false; }
508 std::ostringstream oss;
509 for(std::size_t i=0;i<_param_per_ts.size();i++)
511 const MEDFileParameter1TS *a(_param_per_ts[i]),*b(other->_param_per_ts[i]);
512 if((a && !b) || (!a && b))
513 { oss << "At time step id #" << i << " pointer is defined on one side not in the other !"; what=oss.str(); return false; }
515 if(!a->isEqual(b,eps,what))
516 { oss << " At time step id #" << i << " non equality !"; what+=oss.str(); return false; }
521 void MEDFileParameterMultiTS::write(const std::string& fileName, int mode) const
523 med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode);
524 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),medmod);
528 void MEDFileParameterMultiTS::writeLL(med_idt fid, const MEDFileWritable& mw) const
530 std::set<med_parameter_type> diffType;
531 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
533 const MEDFileParameter1TS *elt(*it);
534 if(dynamic_cast<const MEDFileParameterDouble1TSWTI *>(elt))
535 diffType.insert(MED_FLOAT64);
537 if(diffType.size()>1)
538 throw INTERP_KERNEL::Exception("MEDFileParameterMultiTS::writeLL : impossible to mix type of data in parameters in MED file ! Only float64 or only int32 ...");
541 med_parameter_type typ=*diffType.begin();
542 MEDFileParameterTinyInfo::writeLLHeader(fid,typ);
543 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
545 const MEDFileParameter1TS *elt(*it);
547 elt->writeLL(fid,_name,mw);
551 std::string MEDFileParameterMultiTS::simpleRepr() const
553 std::ostringstream oss;
558 void MEDFileParameterMultiTS::simpleRepr2(int bkOffset, std::ostream& oss) const
560 MEDFileParameterTinyInfo::mainRepr(bkOffset,oss);
561 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
563 const MEDFileParameter1TS *elt(*it);
565 elt->simpleRepr2(bkOffset+2,oss);
569 void MEDFileParameterMultiTS::appendValue(int dt, int it, double time, double val)
571 MEDCouplingAutoRefCountObjectPtr<MEDFileParameterDouble1TSWTI> elt=MEDFileParameterDouble1TSWTI::New(dt,it,time);
573 MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> elt2((MEDFileParameterDouble1TSWTI*)elt); elt2->incrRef();
574 _param_per_ts.push_back(elt2);
577 double MEDFileParameterMultiTS::getDoubleValue(int iteration, int order) const
579 int pos=getPosOfTimeStep(iteration,order);
580 const MEDFileParameter1TS *elt=_param_per_ts[pos];
583 std::ostringstream oss; oss << "MEDFileParameterMultiTS::getDoubleValue : time iteration it=" << iteration << " order=" << order;
584 oss << " exists but elt is empty !";
585 throw INTERP_KERNEL::Exception(oss.str().c_str());
587 const MEDFileParameterDouble1TSWTI *eltC=dynamic_cast<const MEDFileParameterDouble1TSWTI *>(elt);
590 std::ostringstream oss; oss << "MEDFileParameterMultiTS::getDoubleValue : time iteration it=" << iteration << " order=" << order;
591 oss << " exists but not double !";
593 return eltC->getValue();
596 int MEDFileParameterMultiTS::getPosOfTimeStep(int iteration, int order) const
599 std::ostringstream oss; oss << "MEDFileParameterMultiTS::getPosOfTimeStep : no such iteration=" << iteration << " order=" << order << " ! Possibilities are :";
600 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++,ret++)
602 const MEDFileParameter1TS *elt(*it);
605 if(elt->getIteration()==iteration && elt->getOrder()==order)
608 oss << "(" << elt->getIteration() << "," << elt->getOrder() << "), ";
611 throw INTERP_KERNEL::Exception(oss.str().c_str());
614 int MEDFileParameterMultiTS::getPosGivenTime(double time, double eps) const
617 std::ostringstream oss; oss << "MEDFileParameterMultiTS::getPosGivenTime : no such time=" << time << " ! Possibilities are :";
618 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++,ret++)
620 const MEDFileParameter1TS *elt(*it);
623 if(fabs(elt->getTimeValue()-time)<=eps)
626 oss << elt->getTimeValue() << ", ";
629 throw INTERP_KERNEL::Exception(oss.str().c_str());
633 * \return an internal pointer that can be null. Warning the caller is \b not responsible of the returned pointer.
635 MEDFileParameter1TS *MEDFileParameterMultiTS::getTimeStepAtPos(int posId) const
637 if(posId<0 || posId>=(int)_param_per_ts.size())
639 std::ostringstream oss; oss << "MEDFileParameterMultiTS::getTimeStepAtPos : invalid pos ! Should be in [0," << _param_per_ts.size() << ") !";
640 throw INTERP_KERNEL::Exception(oss.str().c_str());
642 return const_cast<MEDFileParameter1TS *>(static_cast<const MEDFileParameter1TS *>(_param_per_ts[posId]));
645 void MEDFileParameterMultiTS::eraseTimeStepIds(const int *startIds, const int *endIds)
647 std::vector<bool> b(_param_per_ts.size(),true);
648 int len=(int)_param_per_ts.size();
649 for(const int *w=startIds;w!=endIds;w++)
654 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());
656 std::size_t newNb=std::count(b.begin(),b.end(),true);
657 std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> > paramPerTs(newNb);
659 for(std::size_t i=0;i<_param_per_ts.size();i++)
661 paramPerTs[j++]=_param_per_ts[i];
662 _param_per_ts=paramPerTs;
665 int MEDFileParameterMultiTS::getNumberOfTS() const
667 return (int) getIterations().size();
670 std::vector< std::pair<int,int> > MEDFileParameterMultiTS::getIterations() const
672 std::vector< std::pair<int,int> > ret;
673 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
675 const MEDFileParameter1TS *elt(*it);
677 ret.push_back(std::pair<int,int>(elt->getIteration(),elt->getOrder()));
685 std::vector< std::pair<int,int> > MEDFileParameterMultiTS::getTimeSteps(std::vector<double>& ret1) const
687 std::vector< std::pair<int,int> > ret0;
689 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
691 const MEDFileParameter1TS *elt(*it);
694 ret0.push_back(std::pair<int,int>(elt->getIteration(),elt->getOrder()));
695 ret1.push_back(elt->getTimeValue());
701 MEDFileParameters *MEDFileParameters::New()
703 return new MEDFileParameters;
706 MEDFileParameters *MEDFileParameters::New(const std::string& fileName)
708 return new MEDFileParameters(fileName);
711 MEDFileParameters::MEDFileParameters(const std::string& fileName)
713 MEDFileUtilities::CheckFileForRead(fileName);
714 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
715 int nbPar=MEDnParameter(fid);
716 _params.resize(nbPar);
717 INTERP_KERNEL::AutoPtr<char> pName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
718 INTERP_KERNEL::AutoPtr<char> descName=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
719 INTERP_KERNEL::AutoPtr<char> unitName=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
720 med_parameter_type paramType;
721 for(int i=0;i<nbPar;i++)
724 MEDparameterInfo(fid,i+1,pName,¶mType,descName,unitName,&nbOfSteps);
725 std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
726 _params[i]=MEDFileParameterMultiTS::New(fileName,paramNameCpp);
730 MEDFileParameters::MEDFileParameters()
734 std::size_t MEDFileParameters::getHeapMemorySizeWithoutChildren() const
736 std::size_t ret(sizeof(MEDFileParameters));
737 ret+=sizeof(MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS>)*_params.capacity();
741 std::vector<const BigMemoryObject *> MEDFileParameters::getDirectChildrenWithNull() const
743 std::vector<const BigMemoryObject *> ret;
744 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++)
745 ret.push_back((const MEDFileParameterMultiTS *)*it);
749 MEDFileParameters *MEDFileParameters::deepCpy() const
751 return new MEDFileParameters(*this,true);
754 bool MEDFileParameters::isEqual(const MEDFileParameters *other, double eps, std::string& what) const
757 { what="other is null !"; return false; }
758 if(_params.size()!=other->_params.size())
759 { what="number of parameters differs !"; return false; }
760 std::ostringstream oss;
761 for(std::size_t i=0;i<_params.size();i++)
763 const MEDFileParameterMultiTS *a(_params[i]),*b(other->_params[i]);
764 if((a && !b) || (!a && b))
765 { oss << "At param with id #" << i << " pointer is defined on one side not in the other !"; what=oss.str(); return false; }
767 if(!a->isEqual(b,eps,what))
768 { oss << " At param with id #" << i << " non equality !"; what+=oss.str(); return false; }
773 MEDFileParameters::MEDFileParameters(const MEDFileParameters& other, bool deepCopy):MEDFileWritable(other),_params(other._params)
776 for(std::size_t i=0;i<_params.size();i++)
778 const MEDFileParameterMultiTS *elt=_params[i];
780 _params[i]=elt->deepCpy();
784 void MEDFileParameters::write(const std::string& fileName, int mode) const
786 med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode);
787 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),medmod);
791 void MEDFileParameters::writeLL(med_idt fid) const
793 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++)
795 const MEDFileParameterMultiTS *elt(*it);
797 elt->writeLL(fid,*this);
801 std::vector<std::string> MEDFileParameters::getParamsNames() const
803 std::vector<std::string> ret(_params.size());
805 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++,i++)
807 const MEDFileParameterMultiTS *p=(*it);
814 std::ostringstream oss; oss << "MEDFileParameters::getParamsNames : At rank #" << i << " param is not defined !";
815 throw INTERP_KERNEL::Exception(oss.str().c_str());
821 std::string MEDFileParameters::simpleRepr() const
823 std::ostringstream oss;
824 simpleReprWithoutHeader(oss);
828 void MEDFileParameters::simpleReprWithoutHeader(std::ostream& oss) const
830 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++)
832 const MEDFileParameterMultiTS *elt(*it);
834 elt->simpleRepr2(2,oss);
838 void MEDFileParameters::resize(int newSize)
841 throw INTERP_KERNEL::Exception("MEDFileParameters::resize : should be positive !");
842 _params.resize(newSize);
845 void MEDFileParameters::pushParam(MEDFileParameterMultiTS *param)
849 MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> elt(param);
850 _params.push_back(elt);
853 void MEDFileParameters::setParamAtPos(int i, MEDFileParameterMultiTS *param)
856 throw INTERP_KERNEL::Exception("MEDFileParameters::setParamAtPos : should be positive !");
857 if(i>=(int)_params.size())
861 MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> elt(param);
866 * \return an internal pointer that can be null. Warning the caller is \b not responsible of the returned pointer.
868 MEDFileParameterMultiTS *MEDFileParameters::getParamAtPos(int i) const
870 if(i<0 || i>=(int)_params.size())
872 std::ostringstream oss; oss << "MEDFileParameters::getParamAtPos : should be in [0," << _params.size() << ") !";
873 throw INTERP_KERNEL::Exception(oss.str().c_str());
875 const MEDFileParameterMultiTS *elt=_params[i];
876 return const_cast<MEDFileParameterMultiTS *>(elt);
880 * \return an internal pointer that can be null. Warning the caller is \b not responsible of the returned pointer.
882 MEDFileParameterMultiTS *MEDFileParameters::getParamWithName(const std::string& paramName) const
884 int pos=getPosFromParamName(paramName);
885 return getParamAtPos(pos);
888 void MEDFileParameters::destroyParamAtPos(int i)
890 if(i<0 || i>=(int)_params.size())
892 std::ostringstream oss; oss << "MEDFileParameters::destroyParamAtPos : should be in [0," << _params.size() << ") !";
893 throw INTERP_KERNEL::Exception(oss.str().c_str());
895 _params[i]=MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS>(0);
898 int MEDFileParameters::getPosFromParamName(const std::string& paramName) const
900 std::ostringstream oss; oss << "MEDFileParameters::getPosFromParamName : no such name=" << paramName << " ! Possibilities are :";
902 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++,ret++)
904 const MEDFileParameterMultiTS *elt(*it);
907 if(std::string(elt->getName())==paramName)
910 oss << elt->getName() << ", ";
913 throw INTERP_KERNEL::Exception(oss.str().c_str());
916 int MEDFileParameters::getNumberOfParams() const
918 return (int)_params.size();