From 00934a726f7c26059e76ec7088fd735eb837ecdd Mon Sep 17 00:00:00 2001 From: Knut Morten Okstad Date: Thu, 12 Sep 2019 13:54:42 +0200 Subject: [PATCH] Changed: The old FieldFuncBase class is split into two classes --- src/Utility/FieldFunctions.C | 116 +++++++++++++++-------------------- src/Utility/FieldFunctions.h | 61 +++++++++++------- 2 files changed, 87 insertions(+), 90 deletions(-) diff --git a/src/Utility/FieldFunctions.C b/src/Utility/FieldFunctions.C index fca18a1b..753aa4d6 100644 --- a/src/Utility/FieldFunctions.C +++ b/src/Utility/FieldFunctions.C @@ -29,8 +29,24 @@ class ProcessAdm {}; #endif -FieldFuncBase::FieldFuncBase (const std::string& fName) - : hdf5(nullptr), pAdm(nullptr), pidx(0) +FieldFuncBase::~FieldFuncBase () +{ + for (ASMbase* pch : patch) delete pch; +} + + +bool FieldFuncBase::setPatch (size_t pIdx) +{ + if (pIdx >= npch) + return false; + + pidx = pIdx; + return true; +} + + +FieldFuncHDF5::FieldFuncHDF5 (const std::string& fName) + : hdf5(nullptr), pAdm(nullptr) { lastLevel = 0; lastTime = 0.0; @@ -44,15 +60,14 @@ FieldFuncBase::FieldFuncBase (const std::string& fName) } -FieldFuncBase::~FieldFuncBase () +FieldFuncHDF5::~FieldFuncHDF5 () { - for (ASMbase* pch : patch) delete pch; delete hdf5; delete pAdm; } -int FieldFuncBase::findClosestLevel (double time) const +int FieldFuncHDF5::findClosestLevel (double time) const { if (time == lastTime) return lastLevel; #ifdef HAS_HDF5 @@ -67,7 +82,7 @@ int FieldFuncBase::findClosestLevel (double time) const if (ok && fabs(time-t) >= fabs(time-lastTime)) { #ifdef SP_DEBUG - std::cout <<"FieldFuncBase: New time level "<< lastLevel + std::cout <<"FieldFuncHDF5: New time level "<< lastLevel <<" at t="<< lastTime <<" (dt="<< time-lastTime <<")"<< std::endl; #endif @@ -81,7 +96,7 @@ int FieldFuncBase::findClosestLevel (double time) const } -bool FieldFuncBase::load (const std::vector& fieldNames, +bool FieldFuncHDF5::load (const std::vector& fieldNames, const std::string& basisName, int level, bool isScalar) { @@ -136,31 +151,42 @@ bool FieldFuncBase::load (const std::vector& fieldNames, patch[ip]->read(strg2); } else - std::cerr <<" *** FieldFuncBase::load: Undefined basis "<< sbasis.str() + std::cerr <<" *** FieldFuncHDF5::load: Undefined basis "<< sbasis.str() <<" ("<< g2.substr(0,9) <<")"<< std::endl; } if (patch[ip]) { - RealArrays coefs(nFldCmp); + std::vector coefs(nFldCmp); for (size_t i = 0; i < nFldCmp; i++) { std::stringstream str; str << level << "/" << basisName << "/fields/" << fieldNames[i] << "/" << ip+1; hdf5->readVector(str.str(),coefs[i]); #if SP_DEBUG > 1 - std::cout <<"FieldFuncBase::load: Reading \""<< fieldNames[i] + std::cout <<"FieldFuncHDF5::load: Reading \""<< fieldNames[i] <<"\" ("<< coefs[i].size() <<") for patch "<< ip+1; for (size_t j = 0; j < coefs[i].size(); j++) std::cout << (j%10 ? ' ' : '\n') << coefs[i][j]; std::cout << std::endl; #endif } - this->addPatchField(patch[ip],coefs); + if (nFldCmp > 1) + { + RealArray coef1; + coef1.reserve(nFldCmp*coefs.front().size()); + for (size_t i = 0; i < coefs.front().size(); i++) + for (size_t j = 0; j < nFldCmp; j++) + coef1.push_back(coefs[j][i]); + this->addPatchField(patch[ip],coef1); + } + else + this->addPatchField(patch[ip],coefs.front()); + nOK++; } else - std::cerr <<" *** FieldFuncBase::load: No field function created" + std::cerr <<" *** FieldFuncHDF5::load: No field function created" <<" for patch "<< ip+1 << std::endl; } #endif @@ -173,7 +199,7 @@ FieldFunction::FieldFunction (const std::string& fileName, const std::string& basisName, const std::string& fieldName, int level) - : FieldFuncBase(fileName), currentLevel(level), + : FieldFuncHDF5(fileName), currentLevel(level), fName(fieldName), bName(basisName) { if (level >= 0) @@ -181,26 +207,18 @@ FieldFunction::FieldFunction (const std::string& fileName, } -bool FieldFunction::initPatch (size_t pIdx) -{ - if (pIdx >= field.size()) - return false; - - pidx = pIdx; - return true; -} - - void FieldFunction::clearField () { for (Field* f : field) delete f; field.clear(); + npch = 0; } -void FieldFunction::addPatchField (ASMbase* pch, const RealArrays& coefs) +void FieldFunction::addPatchField (ASMbase* pch, const RealArray& coefs) { - field.push_back(Field::create(pch,coefs.front())); + field.push_back(Field::create(pch,coefs)); + npch = field.size(); } @@ -230,7 +248,7 @@ FieldsFuncBase::FieldsFuncBase (const std::string& fileName, const std::string& basisName, const std::string& fieldName, int level) - : FieldFuncBase(fileName), currentLevel(level), + : FieldFuncHDF5(fileName), currentLevel(level), fName(splitString(fieldName,[](int c){ return c == '|' ? 1 : 0; })), bName(basisName) { @@ -243,22 +261,14 @@ void FieldsFuncBase::clearField () { for (Fields* f : field) delete f; field.clear(); + npch = 0; } -void FieldsFuncBase::addPatchField (ASMbase* pch, const RealArrays& coefs) +void FieldsFuncBase::addPatchField (ASMbase* pch, const RealArray& coefs) { - if (coefs.size() == 1) - field.push_back(Fields::create(pch,coefs.front(),1,pch->getNoFields(1))); - else if (coefs.size() > 1) - { - RealArray coef; - coef.reserve(coefs.size()*coefs.front().size()); - for (size_t i = 0; i < coefs.front().size(); i++) - for (size_t j = 0; j < coefs.size(); j++) - coef.push_back(coefs[j][i]); - field.push_back(Fields::create(pch,coef,1,pch->getNoFields(1))); - } + field.push_back(Fields::create(pch,coefs,1,pch->getNoFields(1))); + npch = field.size(); } @@ -297,16 +307,6 @@ VecFieldFunction::VecFieldFunction (const std::string& fileName, } -bool VecFieldFunction::initPatch (size_t pIdx) -{ - if (pIdx >= field.size()) - return false; - - pidx = pIdx; - return true; -} - - Vec3 VecFieldFunction::evaluate (const Vec3& X) const { if (pidx >= field.size() || !field[pidx]) @@ -327,16 +327,6 @@ TensorFieldFunction::TensorFieldFunction (const std::string& fileName, } -bool TensorFieldFunction::initPatch (size_t pIdx) -{ - if (pIdx >= field.size()) - return false; - - pidx = pIdx; - return true; -} - - Tensor TensorFieldFunction::evaluate (const Vec3& X) const { if (pidx >= field.size() || !field[pidx]) @@ -357,16 +347,6 @@ STensorFieldFunction::STensorFieldFunction (const std::string& fileName, } -bool STensorFieldFunction::initPatch (size_t pIdx) -{ - if (pIdx >= field.size()) - return false; - - pidx = pIdx; - return true; -} - - SymmTensor STensorFieldFunction::evaluate (const Vec3& X) const { if (pidx >= field.size() || !field[pidx]) diff --git a/src/Utility/FieldFunctions.h b/src/Utility/FieldFunctions.h index bd1a35c7..74cc6f88 100644 --- a/src/Utility/FieldFunctions.h +++ b/src/Utility/FieldFunctions.h @@ -31,17 +31,39 @@ class ProcessAdm; class FieldFuncBase { protected: - typedef std::vector RealArray; //!< Convenience type - typedef std::vector RealArrays; //!< Convenience type - - //! \brief The constructor opens the provided HDF5-file. - //! \param[in] fileName Name of the HDF5-file - explicit FieldFuncBase(const std::string& fileName); + //! \brief Default constructor. + FieldFuncBase() : pidx(0), npch(0) {} //! \brief No copying of this class. FieldFuncBase(const FieldFuncBase&) = delete; - //! \brief The destructor deletes the patches and closes the HDF5-file. + //! \brief The destructor deletes the patches. virtual ~FieldFuncBase(); + //! \brief Sets the active patch. + bool setPatch(size_t pIdx); + +protected: + std::vector patch; //!< The patches on which the field is defined + + size_t pidx; //!< Current patch index + size_t npch; //!< Number of patches in the field +}; + + +/*! + \brief Base class for spatial functions, defined from a HDF5-file. +*/ + +class FieldFuncHDF5 : public FieldFuncBase +{ +protected: + //! \brief The constructor opens the provided HDF5-file. + //! \param[in] fileName Name of the HDF5-file + explicit FieldFuncHDF5(const std::string& fileName); + //! \brief No copying of this class. + FieldFuncHDF5(const FieldFuncHDF5&) = delete; + //! \brief The destructor closes the HDF5-file. + virtual ~FieldFuncHDF5(); + //! \brief Loads field values for the specified time level. //! \param[in] fieldNames Name of the field components in the HDF5-file //! \param[in] basisName Name of the basis which the field values refer to @@ -57,7 +79,7 @@ protected: //! \brief Adds a patch-wise field with the given coefficient values. //! \param[in] pch The patch to define the field over //! \param[in] coefs Field values - virtual void addPatchField(ASMbase* pch, const RealArrays& coefs) = 0; + virtual void addPatchField(ASMbase* pch, const std::vector& coefs) = 0; //! \brief Clears the field container. virtual void clearField() = 0; @@ -67,11 +89,6 @@ private: mutable int lastLevel; //!< The last time level read from mutable double lastTime; //!< The time of \a lastLevel - - std::vector patch; //!< The patches on which the field is defined - -protected: - size_t pidx; //!< Current patch index }; @@ -79,7 +96,7 @@ protected: \brief A scalar-valued spatial function, defined through scalar fields. */ -class FieldFunction : public RealFunc, private FieldFuncBase +class FieldFunction : public RealFunc, private FieldFuncHDF5 { public: //! \brief The constructor creates a field from the provided HDF5-file. @@ -95,7 +112,7 @@ public: virtual ~FieldFunction() { this->clearField(); } //! \brief Sets the active patch. - virtual bool initPatch(size_t pIdx); + virtual bool initPatch(size_t pIdx) { return this->setPatch(pIdx); } protected: //! \brief Evaluates the scalar field function. @@ -104,7 +121,7 @@ protected: //! \brief Adds a patch-wise field with the given coefficient values. //! \param[in] pch The patch to define the field over //! \param[in] coefs Field values - virtual void addPatchField(ASMbase* pch, const RealArrays& coefs); + virtual void addPatchField(ASMbase* pch, const std::vector& coefs); //! \brief Clears the field container. virtual void clearField(); @@ -122,7 +139,7 @@ private: \brief Base class for multi-valued spatial functions, defined through fields. */ -class FieldsFuncBase : public FieldFuncBase +class FieldsFuncBase : public FieldFuncHDF5 { protected: //! \brief The constructor creates a field from the provided HDF5-file. @@ -140,12 +157,12 @@ protected: //! \brief Adds a patch-wise field with the given coefficient values. //! \param[in] pch The patch to define the field over //! \param[in] coefs Field values - virtual void addPatchField(ASMbase* pch, const RealArrays& coefs); + virtual void addPatchField(ASMbase* pch, const std::vector& coefs); //! \brief Clears the field container. virtual void clearField(); //! \brief Evaluates the field at the givent point \b X. - RealArray getValues(const Vec3& X); + std::vector getValues(const Vec3& X); private: mutable int currentLevel; //!< Current time level to evaluate at @@ -178,7 +195,7 @@ public: virtual ~VecFieldFunction() {} //! \brief Sets the active patch. - virtual bool initPatch(size_t pIdx); + virtual bool initPatch(size_t pIdx) { return this->setPatch(pIdx); } protected: //! \brief Evaluates the vectorial field function. @@ -206,7 +223,7 @@ public: virtual ~TensorFieldFunction() {} //! \brief Sets the active patch. - virtual bool initPatch(size_t pIdx); + virtual bool initPatch(size_t pIdx) { return this->setPatch(pIdx); } protected: //! \brief Evaluates the tensorial field function. @@ -234,7 +251,7 @@ public: virtual ~STensorFieldFunction() {} //! \brief Sets the active patch. - virtual bool initPatch(size_t pIdx); + virtual bool initPatch(size_t pIdx) { return this->setPatch(pIdx); } protected: //! \brief Evaluates the tensorial field function.