From 0fc6b521afbe97912131c81615674cf33512dbf8 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Mon, 4 Sep 2017 15:03:11 +0200 Subject: [PATCH] WriteFieldUsingAlreadyWrittenMesh for everybody --- src/MEDCoupling/MEDCouplingFieldDouble.cxx | 70 --------------------- src/MEDCoupling/MEDCouplingFieldDouble.hxx | 2 - src/MEDCoupling/MEDCouplingFieldT.hxx | 2 + src/MEDCoupling/MEDCouplingFieldT.txx | 72 ++++++++++++++++++++++ src/MEDLoader/MEDLoader.cxx | 37 +++++++++-- src/MEDLoader/MEDLoader.hxx | 3 +- src/MEDLoader/Swig/MEDLoaderCommon.i | 2 +- 7 files changed, 110 insertions(+), 78 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingFieldDouble.cxx b/src/MEDCoupling/MEDCouplingFieldDouble.cxx index 20ded05dd..5d5f50ae7 100644 --- a/src/MEDCoupling/MEDCouplingFieldDouble.cxx +++ b/src/MEDCoupling/MEDCouplingFieldDouble.cxx @@ -368,76 +368,6 @@ bool MEDCouplingFieldDouble::areCompatibleForMeld(const MEDCouplingFieldDouble * return true; } -/*! - * Permutes values of \a this field according to a given permutation array for cells - * renumbering. The underlying mesh is deeply copied and its cells are also permuted. - * The number of cells remains the same; for that the permutation array \a old2NewBg - * should not contain equal ids. - * ** Warning, this method modifies the mesh aggreagated by \a this (by performing a deep copy ) **. - * - * \param [in] old2NewBg - the permutation array in "Old to New" mode. Its length is - * to be equal to \a this->getMesh()->getNumberOfCells(). - * \param [in] check - if \c true, \a old2NewBg is transformed to a new permutation - * array, so that its maximal cell id to correspond to (be less than) the number - * of cells in mesh. This new array is then used for the renumbering. If \a - * check == \c false, \a old2NewBg is used as is, that is less secure as validity - * of ids in \a old2NewBg is not checked. - * \throw If the mesh is not set. - * \throw If the spatial discretization of \a this field is NULL. - * \throw If \a check == \c true and \a old2NewBg contains equal ids. - * \throw If mesh nature does not allow renumbering (e.g. structured mesh). - * - * \if ENABLE_EXAMPLES - * \ref cpp_mcfielddouble_renumberCells "Here is a C++ example".
- * \ref py_mcfielddouble_renumberCells "Here is a Python example". - * \endif - */ -void MEDCouplingFieldDouble::renumberCells(const int *old2NewBg, bool check) -{ - renumberCellsWithoutMesh(old2NewBg,check); - MCAuto m=_mesh->deepCopy(); - m->renumberCells(old2NewBg,check); - setMesh(m); - updateTime(); -} - -/*! - * Permutes values of \a this field according to a given permutation array for cells - * renumbering. The underlying mesh is \b not permuted. - * The number of cells remains the same; for that the permutation array \a old2NewBg - * should not contain equal ids. - * This method performs a part of job of renumberCells(). The reasonable use of this - * method is only for multi-field instances lying on the same mesh to avoid a - * systematic duplication and renumbering of _mesh attribute. - * \warning Use this method with a lot of care! - * \param [in] old2NewBg - the permutation array in "Old to New" mode. Its length is - * to be equal to \a this->getMesh()->getNumberOfCells(). - * \param [in] check - if \c true, \a old2NewBg is transformed to a new permutation - * array, so that its maximal cell id to correspond to (be less than) the number - * of cells in mesh. This new array is then used for the renumbering. If \a - * check == \c false, \a old2NewBg is used as is, that is less secure as validity - * of ids in \a old2NewBg is not checked. - * \throw If the mesh is not set. - * \throw If the spatial discretization of \a this field is NULL. - * \throw If \a check == \c true and \a old2NewBg contains equal ids. - * \throw If mesh nature does not allow renumbering (e.g. structured mesh). - */ -void MEDCouplingFieldDouble::renumberCellsWithoutMesh(const int *old2NewBg, bool check) -{ - if(!_mesh) - throw INTERP_KERNEL::Exception("Expecting a defined mesh to be able to operate a renumbering !"); - if(_type.isNull()) - throw INTERP_KERNEL::Exception("Expecting a spatial discretization to be able to operate a renumbering !"); - // - _type->renumberCells(old2NewBg,check); - std::vector arrays; - timeDiscr()->getArrays(arrays); - std::vector arrays2(arrays.size()); std::copy(arrays.begin(),arrays.end(),arrays2.begin()); - _type->renumberArraysForCell(_mesh,arrays2,old2NewBg,check); - // - updateTime(); -} - /*! * Permutes values of \a this field according to a given permutation array for node * renumbering. The underlying mesh is deeply copied and its nodes are also permuted. diff --git a/src/MEDCoupling/MEDCouplingFieldDouble.hxx b/src/MEDCoupling/MEDCouplingFieldDouble.hxx index 7ae4248dd..322d66dd7 100644 --- a/src/MEDCoupling/MEDCouplingFieldDouble.hxx +++ b/src/MEDCoupling/MEDCouplingFieldDouble.hxx @@ -41,8 +41,6 @@ namespace MEDCoupling MEDCOUPLING_EXPORT std::string writeVTK(const std::string& fileName, bool isBinary=true) const; MEDCOUPLING_EXPORT bool areCompatibleForMerge(const MEDCouplingField *other) const; MEDCOUPLING_EXPORT bool areCompatibleForMeld(const MEDCouplingFieldDouble *other) const; - MEDCOUPLING_EXPORT void renumberCells(const int *old2NewBg, bool check=true); - MEDCOUPLING_EXPORT void renumberCellsWithoutMesh(const int *old2NewBg, bool check=true); MEDCOUPLING_EXPORT void renumberNodes(const int *old2NewBg, double eps=1e-15); MEDCOUPLING_EXPORT void renumberNodesWithoutMesh(const int *old2NewBg, int newNbOfNodes, double eps=1e-15); MEDCOUPLING_EXPORT DataArrayInt *findIdsInRange(double vmin, double vmax) const; diff --git a/src/MEDCoupling/MEDCouplingFieldT.hxx b/src/MEDCoupling/MEDCouplingFieldT.hxx index 3e832ef56..fa4681783 100644 --- a/src/MEDCoupling/MEDCouplingFieldT.hxx +++ b/src/MEDCoupling/MEDCouplingFieldT.hxx @@ -83,6 +83,8 @@ namespace MEDCoupling MEDCOUPLING_EXPORT bool areStrictlyCompatibleForMulDiv(const MEDCouplingField *other) const; MEDCOUPLING_EXPORT std::string simpleRepr() const; MEDCOUPLING_EXPORT void reprQuickOverview(std::ostream& stream) const; + MEDCOUPLING_EXPORT void renumberCells(const int *old2NewBg, bool check=true); + MEDCOUPLING_EXPORT void renumberCellsWithoutMesh(const int *old2NewBg, bool check=true); // specific MEDCOUPLING_EXPORT bool areCompatibleForMul(const MEDCouplingField *other) const; MEDCOUPLING_EXPORT bool areCompatibleForDiv(const MEDCouplingField *other) const; diff --git a/src/MEDCoupling/MEDCouplingFieldT.txx b/src/MEDCoupling/MEDCouplingFieldT.txx index d274bdce6..2e0766c8e 100644 --- a/src/MEDCoupling/MEDCouplingFieldT.txx +++ b/src/MEDCoupling/MEDCouplingFieldT.txx @@ -657,6 +657,78 @@ namespace MEDCoupling timeDiscrSafe()->getArrays(arrays); _type->getSerializationIntArray(dataInt); } + + /*! + * Permutes values of \a this field according to a given permutation array for cells + * renumbering. The underlying mesh is deeply copied and its cells are also permuted. + * The number of cells remains the same; for that the permutation array \a old2NewBg + * should not contain equal ids. + * ** Warning, this method modifies the mesh aggreagated by \a this (by performing a deep copy ) **. + * + * \param [in] old2NewBg - the permutation array in "Old to New" mode. Its length is + * to be equal to \a this->getMesh()->getNumberOfCells(). + * \param [in] check - if \c true, \a old2NewBg is transformed to a new permutation + * array, so that its maximal cell id to correspond to (be less than) the number + * of cells in mesh. This new array is then used for the renumbering. If \a + * check == \c false, \a old2NewBg is used as is, that is less secure as validity + * of ids in \a old2NewBg is not checked. + * \throw If the mesh is not set. + * \throw If the spatial discretization of \a this field is NULL. + * \throw If \a check == \c true and \a old2NewBg contains equal ids. + * \throw If mesh nature does not allow renumbering (e.g. structured mesh). + * + * \if ENABLE_EXAMPLES + * \ref cpp_mcfielddouble_renumberCells "Here is a C++ example".
+ * \ref py_mcfielddouble_renumberCells "Here is a Python example". + * \endif + */ + template + void MEDCouplingFieldT::renumberCells(const int *old2NewBg, bool check) + { + renumberCellsWithoutMesh(old2NewBg,check); + MCAuto m(_mesh->deepCopy()); + m->renumberCells(old2NewBg,check); + setMesh(m); + updateTime(); + } + + /*! + * Permutes values of \a this field according to a given permutation array for cells + * renumbering. The underlying mesh is \b not permuted. + * The number of cells remains the same; for that the permutation array \a old2NewBg + * should not contain equal ids. + * This method performs a part of job of renumberCells(). The reasonable use of this + * method is only for multi-field instances lying on the same mesh to avoid a + * systematic duplication and renumbering of _mesh attribute. + * \warning Use this method with a lot of care! + * \param [in] old2NewBg - the permutation array in "Old to New" mode. Its length is + * to be equal to \a this->getMesh()->getNumberOfCells(). + * \param [in] check - if \c true, \a old2NewBg is transformed to a new permutation + * array, so that its maximal cell id to correspond to (be less than) the number + * of cells in mesh. This new array is then used for the renumbering. If \a + * check == \c false, \a old2NewBg is used as is, that is less secure as validity + * of ids in \a old2NewBg is not checked. + * \throw If the mesh is not set. + * \throw If the spatial discretization of \a this field is NULL. + * \throw If \a check == \c true and \a old2NewBg contains equal ids. + * \throw If mesh nature does not allow renumbering (e.g. structured mesh). + */ + template + void MEDCouplingFieldT::renumberCellsWithoutMesh(const int *old2NewBg, bool check) + { + if(!_mesh) + throw INTERP_KERNEL::Exception("Expecting a defined mesh to be able to operate a renumbering !"); + if(_type.isNull()) + throw INTERP_KERNEL::Exception("Expecting a spatial discretization to be able to operate a renumbering !"); + // + _type->renumberCells(old2NewBg,check); + std::vector::ArrayType *> arrays; + _time_discr->getArrays(arrays); + std::vector arrays2(arrays.size()); std::copy(arrays.begin(),arrays.end(),arrays2.begin()); + _type->renumberArraysForCell(_mesh,arrays2,old2NewBg,check); + // + updateTime(); + } } #endif diff --git a/src/MEDLoader/MEDLoader.cxx b/src/MEDLoader/MEDLoader.cxx index 6f3ae83f2..cd159472a 100644 --- a/src/MEDLoader/MEDLoader.cxx +++ b/src/MEDLoader/MEDLoader.cxx @@ -25,11 +25,15 @@ #include "MEDFileSafeCaller.txx" #include "MEDFileMesh.hxx" #include "MEDFileField.hxx" +#include "MEDLoaderTraits.hxx" #include "CellModel.hxx" #include "MEDCouplingUMesh.hxx" #include "MEDCouplingMemArray.hxx" #include "MEDCouplingFieldDouble.hxx" +#include "MEDCouplingFieldFloat.hxx" +#include "MEDCouplingFieldInt.hxx" #include "MEDCouplingGaussLocalization.hxx" +#include "MEDCouplingTraits.hxx" #include "MCAuto.hxx" #include "InterpKernelAutoPtr.hxx" @@ -1615,24 +1619,25 @@ void MEDCoupling::WriteFieldDep(const std::string& fileName, const MEDCoupling:: WriteField(fileName,f,writeFromScratch); } -void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f) +template +void WriteFieldUsingAlreadyWrittenMeshImpl(const std::string& fileName, const typename MEDCoupling::Traits::FieldType *f) { if(!f) throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is null !"); f->checkConsistencyLight(); - int status=MEDLoaderBase::getStatusOfFile(fileName); + int status(MEDLoaderBase::getStatusOfFile(fileName)); if(status!=MEDLoaderBase::EXIST_RW) { std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions or not exists !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - MCAuto f1ts(MEDFileField1TS::New()); + MCAuto< typename MEDCoupling::MLFieldTraits::F1TSType > f1ts(MEDCoupling::MLFieldTraits::F1TSType::New()); AssignStaticWritePropertiesTo(*f1ts); MEDCouplingUMesh *m(dynamic_cast(const_cast(f->getMesh()))); if(m) { MCAuto o2n(m->getRenumArrForMEDFileFrmt()); - MCAuto f2(f->deepCopy()); + MCAuto< typename MEDCoupling::Traits::FieldType > f2(f->deepCopy()); f2->renumberCells(o2n->begin(),false); f1ts->setFieldNoProfileSBT(f2); } @@ -1640,3 +1645,27 @@ void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, f1ts->setFieldNoProfileSBT(f); f1ts->write(fileName,0); } + +void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingField *f) +{ + const MEDCoupling::MEDCouplingFieldDouble *f1(dynamic_cast(f)); + if(f1) + { + WriteFieldUsingAlreadyWrittenMeshImpl(fileName,f1); + return ; + } + const MEDCoupling::MEDCouplingFieldInt *f2(dynamic_cast(f)); + if(f2) + { + WriteFieldUsingAlreadyWrittenMeshImpl(fileName,f2); + return ; + } + const MEDCoupling::MEDCouplingFieldFloat *f3(dynamic_cast(f)); + if(f3) + { + WriteFieldUsingAlreadyWrittenMeshImpl(fileName,f3); + return ; + } + WriteFieldUsingAlreadyWrittenMeshImpl(fileName,NULL); +} + diff --git a/src/MEDLoader/MEDLoader.hxx b/src/MEDLoader/MEDLoader.hxx index df6f61ca5..aa17e825f 100644 --- a/src/MEDLoader/MEDLoader.hxx +++ b/src/MEDLoader/MEDLoader.hxx @@ -35,6 +35,7 @@ namespace MEDCoupling class DataArrayInt; class MEDCouplingMesh; class MEDCouplingUMesh; + class MEDCouplingField; class MEDCouplingFieldDouble; class MEDFileWritable; @@ -98,7 +99,7 @@ namespace MEDCoupling MEDLOADER_EXPORT void WriteUMeshes(const std::string& fileName, const std::vector& meshes, bool writeFromScratch); MEDLOADER_EXPORT void WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch); MEDLOADER_EXPORT void WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch); - MEDLOADER_EXPORT void WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f); + MEDLOADER_EXPORT void WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingField *f); MEDLOADER_EXPORT void AssignStaticWritePropertiesTo(MEDCoupling::MEDFileWritable& obj); } diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index 7219f54e1..69c5f68b5 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -374,7 +374,7 @@ namespace MEDCoupling void WriteUMeshDep(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch) throw(INTERP_KERNEL::Exception); void WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch) throw(INTERP_KERNEL::Exception); void WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch) throw(INTERP_KERNEL::Exception); - void WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f) throw(INTERP_KERNEL::Exception); + void WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingField *f) throw(INTERP_KERNEL::Exception); } %rename (MEDFileVersion) MEDFileVersionSwig; -- 2.39.2