Changed: The old FieldFuncBase class is split into two classes

This commit is contained in:
Knut Morten Okstad 2019-09-12 13:54:42 +02:00
parent 870df942d8
commit 00934a726f
2 changed files with 87 additions and 90 deletions

View File

@ -29,8 +29,24 @@ class ProcessAdm {};
#endif #endif
FieldFuncBase::FieldFuncBase (const std::string& fName) FieldFuncBase::~FieldFuncBase ()
: hdf5(nullptr), pAdm(nullptr), pidx(0) {
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; lastLevel = 0;
lastTime = 0.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 hdf5;
delete pAdm; delete pAdm;
} }
int FieldFuncBase::findClosestLevel (double time) const int FieldFuncHDF5::findClosestLevel (double time) const
{ {
if (time == lastTime) return lastLevel; if (time == lastTime) return lastLevel;
#ifdef HAS_HDF5 #ifdef HAS_HDF5
@ -67,7 +82,7 @@ int FieldFuncBase::findClosestLevel (double time) const
if (ok && fabs(time-t) >= fabs(time-lastTime)) if (ok && fabs(time-t) >= fabs(time-lastTime))
{ {
#ifdef SP_DEBUG #ifdef SP_DEBUG
std::cout <<"FieldFuncBase: New time level "<< lastLevel std::cout <<"FieldFuncHDF5: New time level "<< lastLevel
<<" at t="<< lastTime <<" (dt="<< time-lastTime <<" at t="<< lastTime <<" (dt="<< time-lastTime
<<")"<< std::endl; <<")"<< std::endl;
#endif #endif
@ -81,7 +96,7 @@ int FieldFuncBase::findClosestLevel (double time) const
} }
bool FieldFuncBase::load (const std::vector<std::string>& fieldNames, bool FieldFuncHDF5::load (const std::vector<std::string>& fieldNames,
const std::string& basisName, int level, const std::string& basisName, int level,
bool isScalar) bool isScalar)
{ {
@ -136,31 +151,42 @@ bool FieldFuncBase::load (const std::vector<std::string>& fieldNames,
patch[ip]->read(strg2); patch[ip]->read(strg2);
} }
else else
std::cerr <<" *** FieldFuncBase::load: Undefined basis "<< sbasis.str() std::cerr <<" *** FieldFuncHDF5::load: Undefined basis "<< sbasis.str()
<<" ("<< g2.substr(0,9) <<")"<< std::endl; <<" ("<< g2.substr(0,9) <<")"<< std::endl;
} }
if (patch[ip]) if (patch[ip])
{ {
RealArrays coefs(nFldCmp); std::vector<RealArray> coefs(nFldCmp);
for (size_t i = 0; i < nFldCmp; i++) for (size_t i = 0; i < nFldCmp; i++)
{ {
std::stringstream str; std::stringstream str;
str << level << "/" << basisName << "/fields/" << fieldNames[i] << "/" << ip+1; str << level << "/" << basisName << "/fields/" << fieldNames[i] << "/" << ip+1;
hdf5->readVector(str.str(),coefs[i]); hdf5->readVector(str.str(),coefs[i]);
#if SP_DEBUG > 1 #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; <<"\" ("<< coefs[i].size() <<") for patch "<< ip+1;
for (size_t j = 0; j < coefs[i].size(); j++) for (size_t j = 0; j < coefs[i].size(); j++)
std::cout << (j%10 ? ' ' : '\n') << coefs[i][j]; std::cout << (j%10 ? ' ' : '\n') << coefs[i][j];
std::cout << std::endl; std::cout << std::endl;
#endif #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++; nOK++;
} }
else else
std::cerr <<" *** FieldFuncBase::load: No field function created" std::cerr <<" *** FieldFuncHDF5::load: No field function created"
<<" for patch "<< ip+1 << std::endl; <<" for patch "<< ip+1 << std::endl;
} }
#endif #endif
@ -173,7 +199,7 @@ FieldFunction::FieldFunction (const std::string& fileName,
const std::string& basisName, const std::string& basisName,
const std::string& fieldName, const std::string& fieldName,
int level) int level)
: FieldFuncBase(fileName), currentLevel(level), : FieldFuncHDF5(fileName), currentLevel(level),
fName(fieldName), bName(basisName) fName(fieldName), bName(basisName)
{ {
if (level >= 0) 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 () void FieldFunction::clearField ()
{ {
for (Field* f : field) delete f; for (Field* f : field) delete f;
field.clear(); 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& basisName,
const std::string& fieldName, const std::string& fieldName,
int level) int level)
: FieldFuncBase(fileName), currentLevel(level), : FieldFuncHDF5(fileName), currentLevel(level),
fName(splitString(fieldName,[](int c){ return c == '|' ? 1 : 0; })), fName(splitString(fieldName,[](int c){ return c == '|' ? 1 : 0; })),
bName(basisName) bName(basisName)
{ {
@ -243,22 +261,14 @@ void FieldsFuncBase::clearField ()
{ {
for (Fields* f : field) delete f; for (Fields* f : field) delete f;
field.clear(); 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,1,pch->getNoFields(1)));
field.push_back(Fields::create(pch,coefs.front(),1,pch->getNoFields(1))); npch = field.size();
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)));
}
} }
@ -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 Vec3 VecFieldFunction::evaluate (const Vec3& X) const
{ {
if (pidx >= field.size() || !field[pidx]) 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 Tensor TensorFieldFunction::evaluate (const Vec3& X) const
{ {
if (pidx >= field.size() || !field[pidx]) 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 SymmTensor STensorFieldFunction::evaluate (const Vec3& X) const
{ {
if (pidx >= field.size() || !field[pidx]) if (pidx >= field.size() || !field[pidx])

View File

@ -31,17 +31,39 @@ class ProcessAdm;
class FieldFuncBase class FieldFuncBase
{ {
protected: protected:
typedef std::vector<Real> RealArray; //!< Convenience type //! \brief Default constructor.
typedef std::vector<RealArray> RealArrays; //!< Convenience type FieldFuncBase() : pidx(0), npch(0) {}
//! \brief The constructor opens the provided HDF5-file.
//! \param[in] fileName Name of the HDF5-file
explicit FieldFuncBase(const std::string& fileName);
//! \brief No copying of this class. //! \brief No copying of this class.
FieldFuncBase(const FieldFuncBase&) = delete; FieldFuncBase(const FieldFuncBase&) = delete;
//! \brief The destructor deletes the patches and closes the HDF5-file. //! \brief The destructor deletes the patches.
virtual ~FieldFuncBase(); virtual ~FieldFuncBase();
//! \brief Sets the active patch.
bool setPatch(size_t pIdx);
protected:
std::vector<ASMbase*> 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. //! \brief Loads field values for the specified time level.
//! \param[in] fieldNames Name of the field components in the HDF5-file //! \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 //! \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. //! \brief Adds a patch-wise field with the given coefficient values.
//! \param[in] pch The patch to define the field over //! \param[in] pch The patch to define the field over
//! \param[in] coefs Field values //! \param[in] coefs Field values
virtual void addPatchField(ASMbase* pch, const RealArrays& coefs) = 0; virtual void addPatchField(ASMbase* pch, const std::vector<Real>& coefs) = 0;
//! \brief Clears the field container. //! \brief Clears the field container.
virtual void clearField() = 0; virtual void clearField() = 0;
@ -67,11 +89,6 @@ private:
mutable int lastLevel; //!< The last time level read from mutable int lastLevel; //!< The last time level read from
mutable double lastTime; //!< The time of \a lastLevel mutable double lastTime; //!< The time of \a lastLevel
std::vector<ASMbase*> 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. \brief A scalar-valued spatial function, defined through scalar fields.
*/ */
class FieldFunction : public RealFunc, private FieldFuncBase class FieldFunction : public RealFunc, private FieldFuncHDF5
{ {
public: public:
//! \brief The constructor creates a field from the provided HDF5-file. //! \brief The constructor creates a field from the provided HDF5-file.
@ -95,7 +112,7 @@ public:
virtual ~FieldFunction() { this->clearField(); } virtual ~FieldFunction() { this->clearField(); }
//! \brief Sets the active patch. //! \brief Sets the active patch.
virtual bool initPatch(size_t pIdx); virtual bool initPatch(size_t pIdx) { return this->setPatch(pIdx); }
protected: protected:
//! \brief Evaluates the scalar field function. //! \brief Evaluates the scalar field function.
@ -104,7 +121,7 @@ protected:
//! \brief Adds a patch-wise field with the given coefficient values. //! \brief Adds a patch-wise field with the given coefficient values.
//! \param[in] pch The patch to define the field over //! \param[in] pch The patch to define the field over
//! \param[in] coefs Field values //! \param[in] coefs Field values
virtual void addPatchField(ASMbase* pch, const RealArrays& coefs); virtual void addPatchField(ASMbase* pch, const std::vector<Real>& coefs);
//! \brief Clears the field container. //! \brief Clears the field container.
virtual void clearField(); virtual void clearField();
@ -122,7 +139,7 @@ private:
\brief Base class for multi-valued spatial functions, defined through fields. \brief Base class for multi-valued spatial functions, defined through fields.
*/ */
class FieldsFuncBase : public FieldFuncBase class FieldsFuncBase : public FieldFuncHDF5
{ {
protected: protected:
//! \brief The constructor creates a field from the provided HDF5-file. //! \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. //! \brief Adds a patch-wise field with the given coefficient values.
//! \param[in] pch The patch to define the field over //! \param[in] pch The patch to define the field over
//! \param[in] coefs Field values //! \param[in] coefs Field values
virtual void addPatchField(ASMbase* pch, const RealArrays& coefs); virtual void addPatchField(ASMbase* pch, const std::vector<Real>& coefs);
//! \brief Clears the field container. //! \brief Clears the field container.
virtual void clearField(); virtual void clearField();
//! \brief Evaluates the field at the givent point \b X. //! \brief Evaluates the field at the givent point \b X.
RealArray getValues(const Vec3& X); std::vector<Real> getValues(const Vec3& X);
private: private:
mutable int currentLevel; //!< Current time level to evaluate at mutable int currentLevel; //!< Current time level to evaluate at
@ -178,7 +195,7 @@ public:
virtual ~VecFieldFunction() {} virtual ~VecFieldFunction() {}
//! \brief Sets the active patch. //! \brief Sets the active patch.
virtual bool initPatch(size_t pIdx); virtual bool initPatch(size_t pIdx) { return this->setPatch(pIdx); }
protected: protected:
//! \brief Evaluates the vectorial field function. //! \brief Evaluates the vectorial field function.
@ -206,7 +223,7 @@ public:
virtual ~TensorFieldFunction() {} virtual ~TensorFieldFunction() {}
//! \brief Sets the active patch. //! \brief Sets the active patch.
virtual bool initPatch(size_t pIdx); virtual bool initPatch(size_t pIdx) { return this->setPatch(pIdx); }
protected: protected:
//! \brief Evaluates the tensorial field function. //! \brief Evaluates the tensorial field function.
@ -234,7 +251,7 @@ public:
virtual ~STensorFieldFunction() {} virtual ~STensorFieldFunction() {}
//! \brief Sets the active patch. //! \brief Sets the active patch.
virtual bool initPatch(size_t pIdx); virtual bool initPatch(size_t pIdx) { return this->setPatch(pIdx); }
protected: protected:
//! \brief Evaluates the tensorial field function. //! \brief Evaluates the tensorial field function.