From: ageay Date: Tue, 26 Feb 2013 09:41:20 +0000 (+0000) Subject: Bug corrections and tiny devs in Parameters X-Git-Tag: V6_main_FINAL~338 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=1a6de1ef4f24dfbd5d79f3c395cc0d19fb3a0460;p=tools%2Fmedcoupling.git Bug corrections and tiny devs in Parameters --- diff --git a/src/MEDLoader/MEDFileData.cxx b/src/MEDLoader/MEDFileData.cxx index f2d09da1c..e5493423a 100644 --- a/src/MEDLoader/MEDFileData.cxx +++ b/src/MEDLoader/MEDFileData.cxx @@ -91,10 +91,9 @@ void MEDFileData::setMeshes(MEDFileMeshes *meshes) throw(INTERP_KERNEL::Exceptio void MEDFileData::setParams(MEDFileParameters *params) throw(INTERP_KERNEL::Exception) { - if(!params) + if(params) params->incrRef(); _params=params; - } int MEDFileData::getNumberOfFields() const throw(INTERP_KERNEL::Exception) @@ -140,7 +139,7 @@ std::string MEDFileData::simpleRepr() const tmp2->simpleReprWithoutHeader(oss); } else - oss << "No meshes set !!!\n"; + oss << "No meshes set !!!\n\n"; oss << "Params part :\n*************\n\n"; const MEDFileParameters *tmp3=_params; if(tmp3) @@ -223,6 +222,7 @@ try { _fields=MEDFileFields::New(fileName); _meshes=MEDFileMeshes::New(fileName); + _params=MEDFileParameters::New(fileName); } catch(INTERP_KERNEL::Exception& e) { diff --git a/src/MEDLoader/MEDFileParameter.cxx b/src/MEDLoader/MEDFileParameter.cxx index 61a6d55fa..e9c3c9abf 100644 --- a/src/MEDLoader/MEDFileParameter.cxx +++ b/src/MEDLoader/MEDFileParameter.cxx @@ -70,6 +70,13 @@ std::size_t MEDFileParameterDouble1TSWTI::getHeapMemorySize() const return sizeof(MEDFileParameterDouble1TSWTI); } +std::string MEDFileParameterDouble1TSWTI::simpleRepr() const +{ + std::ostringstream oss; + simpleRepr2(0,oss); + return oss.str(); +} + void MEDFileParameterDouble1TSWTI::simpleRepr2(int bkOffset, std::ostream& oss) const { std::string startOfLine(bkOffset,' '); @@ -478,6 +485,25 @@ MEDFileParameterMultiTS *MEDFileParameterMultiTS::deepCpy() const throw(INTERP_K return new MEDFileParameterMultiTS(*this,true); } +bool MEDFileParameterMultiTS::isEqual(const MEDFileParameterMultiTS *other, double eps, std::string& what) const +{ + if(!other) + { what="other is null !"; return false; } + if(_param_per_ts.size()!=other->_param_per_ts.size()) + { what="number of time steps differs !"; return false; } + std::ostringstream oss; + for(std::size_t i=0;i<_param_per_ts.size();i++) + { + const MEDFileParameter1TS *a(_param_per_ts[i]),*b(other->_param_per_ts[i]); + if((a && !b) || (!a && b)) + { oss << "At time step id #" << i << " pointer is defined on one side not in the other !"; what=oss.str(); return false; } + if(a) + if(!a->isEqual(b,eps,what)) + { oss << " At time step id #" << i << " non equality !"; what+=oss.str(); return false; } + } + return true; +} + void MEDFileParameterMultiTS::write(const char *fileName, int mode) const throw(INTERP_KERNEL::Exception) { med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode); @@ -491,7 +517,7 @@ void MEDFileParameterMultiTS::writeLL(med_idt fid, const MEDFileWritable& mw) co for(std::vector< MEDCouplingAutoRefCountObjectPtr >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++) { const MEDFileParameter1TS *elt(*it); - if(dynamic_cast(elt)) + if(dynamic_cast(elt)) diffType.insert(MED_FLOAT64); } if(diffType.size()>1) @@ -589,6 +615,19 @@ int MEDFileParameterMultiTS::getPosGivenTime(double time, double eps) const thro throw INTERP_KERNEL::Exception(oss.str().c_str()); } +/*! + * \return an internal pointer that can be null. Warning the caller is \b not responsible of the returned pointer. + */ +MEDFileParameter1TS *MEDFileParameterMultiTS::getTimeStepAtPos(int posId) const throw(INTERP_KERNEL::Exception) +{ + if(posId<0 || posId>=(int)_param_per_ts.size()) + { + std::ostringstream oss; oss << "MEDFileParameterMultiTS::getTimeStepAtPos : invalid pos ! Should be in [0," << _param_per_ts.size() << ") !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + return const_cast(static_cast(_param_per_ts[posId])); +} + void MEDFileParameterMultiTS::eraseTimeStepIds(const int *startIds, const int *endIds) throw(INTERP_KERNEL::Exception) { std::vector b(_param_per_ts.size(),true); @@ -691,6 +730,25 @@ MEDFileParameters *MEDFileParameters::deepCpy() const throw(INTERP_KERNEL::Excep return new MEDFileParameters(*this,true); } +bool MEDFileParameters::isEqual(const MEDFileParameters *other, double eps, std::string& what) const +{ + if(!other) + { what="other is null !"; return false; } + if(_params.size()!=other->_params.size()) + { what="number of parameters differs !"; return false; } + std::ostringstream oss; + for(std::size_t i=0;i<_params.size();i++) + { + const MEDFileParameterMultiTS *a(_params[i]),*b(other->_params[i]); + if((a && !b) || (!a && b)) + { oss << "At param with id #" << i << " pointer is defined on one side not in the other !"; what=oss.str(); return false; } + if(a) + if(!a->isEqual(b,eps,what)) + { oss << " At param with id #" << i << " non equality !"; what+=oss.str(); return false; } + } + return true; +} + MEDFileParameters::MEDFileParameters(const MEDFileParameters& other, bool deepCopy):MEDFileWritable(other),_params(other._params) { if(deepCopy) @@ -825,7 +883,7 @@ int MEDFileParameters::getPosFromParamName(const char *paramName) const throw(IN const MEDFileParameterMultiTS *elt(*it); if(elt) { - if(elt->getName()==paramName) + if(std::string(elt->getName())==paramName) return ret; else oss << elt->getName() << ", "; diff --git a/src/MEDLoader/MEDFileParameter.hxx b/src/MEDLoader/MEDFileParameter.hxx index 5386a02b8..c3e61962c 100644 --- a/src/MEDLoader/MEDFileParameter.hxx +++ b/src/MEDLoader/MEDFileParameter.hxx @@ -64,6 +64,7 @@ namespace ParaMEDMEM bool isEqual(const MEDFileParameter1TS *other, double eps, std::string& what) const; std::size_t getHeapMemorySize() const; void readValue(med_idt fid, const std::string& name) throw(INTERP_KERNEL::Exception); + std::string simpleRepr() const; protected: MEDFileParameterDouble1TSWTI(); MEDFileParameterDouble1TSWTI(int iteration, int order, double time); @@ -124,6 +125,7 @@ namespace ParaMEDMEM void setName(const char *name) { _name=name; } std::size_t getHeapMemorySize() const; MEDFileParameterMultiTS *deepCpy() const throw(INTERP_KERNEL::Exception); + bool isEqual(const MEDFileParameterMultiTS *other, double eps, std::string& what) const; void write(const char *fileName, int mode) const throw(INTERP_KERNEL::Exception); void writeLL(med_idt fid, const MEDFileWritable& mw) const throw(INTERP_KERNEL::Exception); std::string simpleRepr() const; @@ -131,6 +133,7 @@ namespace ParaMEDMEM double getDoubleValue(int iteration, int order) const throw(INTERP_KERNEL::Exception); int getPosOfTimeStep(int iteration, int order) const throw(INTERP_KERNEL::Exception); int getPosGivenTime(double time, double eps=1e-8) const throw(INTERP_KERNEL::Exception); + MEDFileParameter1TS *getTimeStepAtPos(int posId) const throw(INTERP_KERNEL::Exception); void eraseTimeStepIds(const int *startIds, const int *endIds) throw(INTERP_KERNEL::Exception); std::vector< std::pair > getIterations() const throw(INTERP_KERNEL::Exception); std::vector< std::pair > getTimeSteps(std::vector& ret1) const throw(INTERP_KERNEL::Exception); @@ -152,6 +155,7 @@ namespace ParaMEDMEM static MEDFileParameters *New(const char *fileName) throw(INTERP_KERNEL::Exception); std::size_t getHeapMemorySize() const; MEDFileParameters *deepCpy() const throw(INTERP_KERNEL::Exception); + bool isEqual(const MEDFileParameters *other, double eps, std::string& what) const; void write(const char *fileName, int mode) const throw(INTERP_KERNEL::Exception); void writeLL(med_idt fid) const throw(INTERP_KERNEL::Exception); std::vector getParamsNames() const throw(INTERP_KERNEL::Exception); diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index 4452d34da..6082423e5 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -133,10 +133,13 @@ using namespace ParaMEDMEM; %newobject ParaMEDMEM::MEDFileParameterDouble1TS::deepCpy; %newobject ParaMEDMEM::MEDFileParameterMultiTS::New; %newobject ParaMEDMEM::MEDFileParameterMultiTS::deepCpy; +%newobject ParaMEDMEM::MEDFileParameterMultiTS::getTimeStepAtPos; +%newobject ParaMEDMEM::MEDFileParameterMultiTS::__getitem__; %newobject ParaMEDMEM::MEDFileParameters::New; %newobject ParaMEDMEM::MEDFileParameters::deepCpy; %newobject ParaMEDMEM::MEDFileParameters::getParamAtPos; %newobject ParaMEDMEM::MEDFileParameters::getParamWithName; +%newobject ParaMEDMEM::MEDFileParameters::__getitem__; %newobject ParaMEDMEM::SauvWriter::New; %newobject ParaMEDMEM::SauvReader::New; @@ -152,6 +155,7 @@ using namespace ParaMEDMEM; %feature("unref") MEDFileFieldMultiTS "$this->decrRef();" %feature("unref") MEDFileFields "$this->decrRef();" %feature("unref") MEDFileParameter1TS "$this->decrRef();" +%feature("unref") MEDFileParameterDouble1TSWTI "$this->decrRef();" %feature("unref") MEDFileParameterDouble1TS "$this->decrRef();" %feature("unref") MEDFileParameterMultiTS "$this->decrRef();" %feature("unref") MEDFileParameters "$this->decrRef();" @@ -1712,6 +1716,14 @@ namespace ParaMEDMEM public: void setValue(double val) throw(INTERP_KERNEL::Exception); double getValue() const throw(INTERP_KERNEL::Exception); + std::string simpleRepr() const; + %extend + { + std::string __str__() const throw(INTERP_KERNEL::Exception) + { + return self->simpleRepr(); + } + } }; class MEDFileParameterTinyInfo : public MEDFileWritable @@ -1813,6 +1825,18 @@ namespace ParaMEDMEM return self->simpleRepr(); } + PyObject *isEqual(const MEDFileParameterMultiTS *other, double eps) const + { + std::string what; + bool ret0=self->isEqual(other,eps,what); + PyObject *res=PyList_New(2); + PyObject *ret0Py=ret0?Py_True:Py_False; + Py_XINCREF(ret0Py); + PyList_SetItem(res,0,ret0Py); + PyList_SetItem(res,1,PyString_FromString(what.c_str())); + return res; + } + void eraseTimeStepIds(PyObject *ids) throw(INTERP_KERNEL::Exception) { int sw; @@ -1845,6 +1869,84 @@ namespace ParaMEDMEM } } + int getTimeStepId(PyObject *elt0) const throw(INTERP_KERNEL::Exception) + { + if(elt0 && PyInt_Check(elt0)) + {//fmts[3] + int pos=PyInt_AS_LONG(elt0); + return pos; + } + else if(elt0 && PyTuple_Check(elt0)) + { + if(PyTuple_Size(elt0)==2) + { + PyObject *o0=PyTuple_GetItem(elt0,0); + PyObject *o1=PyTuple_GetItem(elt0,1); + if(PyInt_Check(o0) && PyInt_Check(o1)) + {//fmts(1,-1) + int iter=PyInt_AS_LONG(o0); + int order=PyInt_AS_LONG(o1); + return self->getPosOfTimeStep(iter,order); + } + else + throw INTERP_KERNEL::Exception("MEDFileParameterMultiTS::getTimeStepId : invalid input param ! input is a tuple of size 2 but two integers are expected in this tuple to request a time steps !"); + } + else + throw INTERP_KERNEL::Exception("MEDFileParameterMultiTS::getTimeStepId : invalid input param ! input is a tuple of size != 2 ! two integers are expected in this tuple to request a time steps !"); + } + else if(elt0 && PyFloat_Check(elt0)) + { + double val=PyFloat_AS_DOUBLE(elt0); + return self->getPosGivenTime(val); + } + else + throw INTERP_KERNEL::Exception("MEDFileParameterMultiTS::getTimeStepId : invalid input params ! expected fmts[int], fmts[int,int] or fmts[double] to request time step !"); + } + + MEDFileParameter1TS *__getitem__(PyObject *elt0) const throw(INTERP_KERNEL::Exception) + { + MEDFileParameter1TS *ret=self->getTimeStepAtPos(ParaMEDMEM_MEDFileParameterMultiTS_getTimeStepId(self,elt0)); + if(ret) + ret->incrRef(); + return ret; + } + + std::vector getTimeStepIds(PyObject *elts) const throw(INTERP_KERNEL::Exception) + { + if(PyList_Check(elts)) + { + int sz=PyList_Size(elts); + std::vector ret(sz); + for(int i=0;i ret(1); + ret[0]=ParaMEDMEM_MEDFileParameterMultiTS_getTimeStepId(self,elts); + return ret; + } + } + + void __delitem__(PyObject *elts) throw(INTERP_KERNEL::Exception) + { + std::vector idsToRemove=ParaMEDMEM_MEDFileParameterMultiTS_getTimeStepIds(self,elts); + if(!idsToRemove.empty()) + self->eraseTimeStepIds(&idsToRemove[0],&idsToRemove[0]+idsToRemove.size()); + } + + MEDFileParameter1TS *getTimeStepAtPos(int posId) const throw(INTERP_KERNEL::Exception) + { + MEDFileParameter1TS *ret=self->getTimeStepAtPos(posId); + if(ret) + ret->incrRef(); + return ret; + } + PyObject *getIterations() const throw(INTERP_KERNEL::Exception) { std::vector< std::pair > res=self->getIterations(); @@ -1911,6 +2013,26 @@ namespace ParaMEDMEM return self->simpleRepr(); } + MEDFileParameterMultiTS *__getitem__(PyObject *obj) throw(INTERP_KERNEL::Exception) + { + if(PyInt_Check(obj)) + { + MEDFileParameterMultiTS *ret=self->getParamAtPos((int)PyInt_AS_LONG(obj)); + if(ret) + ret->incrRef(); + return ret; + } + else if(PyString_Check(obj)) + { + MEDFileParameterMultiTS *ret=self->getParamWithName(PyString_AsString(obj)); + if(ret) + ret->incrRef(); + return ret; + } + else + throw INTERP_KERNEL::Exception("MEDFileParameters::__getitem__ : only integer or string with meshname supported !"); + } + MEDFileParameterMultiTS *getParamAtPos(int i) const throw(INTERP_KERNEL::Exception) { MEDFileParameterMultiTS *ret=self->getParamAtPos(i); @@ -1926,6 +2048,18 @@ namespace ParaMEDMEM ret->incrRef(); return ret; } + + PyObject *isEqual(const MEDFileParameters *other, double eps) const + { + std::string what; + bool ret0=self->isEqual(other,eps,what); + PyObject *res=PyList_New(2); + PyObject *ret0Py=ret0?Py_True:Py_False; + Py_XINCREF(ret0Py); + PyList_SetItem(res,0,ret0Py); + PyList_SetItem(res,1,PyString_FromString(what.c_str())); + return res; + } } }; diff --git a/src/MEDLoader/Swig/MEDLoaderTypemaps.i b/src/MEDLoader/Swig/MEDLoaderTypemaps.i index 8ce6f26f3..07ae0b12e 100644 --- a/src/MEDLoader/Swig/MEDLoaderTypemaps.i +++ b/src/MEDLoader/Swig/MEDLoaderTypemaps.i @@ -39,6 +39,8 @@ static PyObject* convertMEDFileParameter1TS(ParaMEDMEM::MEDFileParameter1TS* p1t PyObject *ret=0; if(dynamic_cast(p1ts)) ret=SWIG_NewPointerObj((void*)p1ts,SWIGTYPE_p_ParaMEDMEM__MEDFileParameterDouble1TS,owner); + if(dynamic_cast(p1ts)) + ret=SWIG_NewPointerObj((void*)p1ts,SWIGTYPE_p_ParaMEDMEM__MEDFileParameterDouble1TSWTI,owner); if(!ret) throw INTERP_KERNEL::Exception("Not recognized type of MEDFileParameter1TS on downcast !"); return ret;