From bcfc7cd9e27bb556cbd6999eab8cca70823be2e2 Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Fri, 23 Jun 2023 17:54:17 -0600 Subject: [PATCH] [Func1] Implement Func1::type --- include/cantera/numerics/Func1.h | 84 ++++++++++++++++++++++++++++++-- src/numerics/Func1.cpp | 32 ++++++------ 2 files changed, 96 insertions(+), 20 deletions(-) diff --git a/include/cantera/numerics/Func1.h b/include/cantera/numerics/Func1.h index 5427f6ee0b..a3466b17c7 100644 --- a/include/cantera/numerics/Func1.h +++ b/include/cantera/numerics/Func1.h @@ -77,6 +77,12 @@ class Func1 //! @deprecated To be removed after Cantera 3.0. Replaced by type. virtual int ID() const; + //! Returns a string describing the type of the function + //! @since New in Cantera 3.0. + virtual string type() const { + return "functor"; + } + //! Calls method eval to evaluate the function doublereal operator()(doublereal t) const; @@ -209,17 +215,21 @@ class Sin1 : public Func1 return *this; } - virtual Func1& duplicate() const; virtual std::string write(const std::string& arg) const; virtual int ID() const { return SinFuncType; } + virtual string type() const { + return "sin"; + } + virtual doublereal eval(doublereal t) const { return sin(m_c*t); } + virtual Func1& duplicate() const; virtual Func1& derivative() const; virtual shared_ptr derivative3() const; }; @@ -256,6 +266,10 @@ class Cos1 : public Func1 virtual int ID() const { return CosFuncType; } + virtual string type() const { + return "cos"; + } + virtual doublereal eval(doublereal t) const { return cos(m_c * t); } @@ -289,11 +303,15 @@ class Exp1 : public Func1 virtual int ID() const { return ExpFuncType; } - virtual Func1& duplicate() const; + virtual string type() const { + return "exp"; + } + virtual doublereal eval(doublereal t) const { return exp(m_c*t); } + virtual Func1& duplicate() const; virtual Func1& derivative() const; virtual shared_ptr derivative3() const; }; @@ -348,10 +366,14 @@ class Pow1 : public Func1 virtual int ID() const { return PowFuncType; } - virtual Func1& duplicate() const; + virtual string type() const { + return "pow"; + } + virtual doublereal eval(doublereal t) const { return pow(t, m_c); } + virtual Func1& duplicate() const; virtual Func1& derivative() const; virtual shared_ptr derivative3() const; }; @@ -382,6 +404,13 @@ class Tabulated1 : public Func1 virtual int ID() const { return TabulatedFuncType; } + virtual string type() const { + if (m_isLinear) { + return "tabulated-linear"; + } + return "tabulated-previous"; + } + virtual double eval(double t) const; virtual Func1& duplicate() const; virtual Func1& derivative() const; @@ -424,6 +453,10 @@ class Const1 : public Func1 virtual int ID() const { return ConstFuncType; } + virtual string type() const { + return "constant"; + } + virtual doublereal eval(doublereal t) const { return m_c; } @@ -480,6 +513,9 @@ class Sum1 : public Func1 virtual int ID() const { return SumFuncType; } + virtual string type() const { + return "sum"; + } virtual doublereal eval(doublereal t) const { return m_f1->eval(t) + m_f2->eval(t); @@ -546,6 +582,10 @@ class Diff1 : public Func1 return DiffFuncType; } + virtual string type() const { + return "diff"; + } + virtual doublereal eval(doublereal t) const { return m_f1->eval(t) - m_f2->eval(t); } @@ -611,6 +651,10 @@ class Product1 : public Func1 return ProdFuncType; } + virtual string type() const { + return "product"; + } + virtual std::string write(const std::string& arg) const; virtual doublereal eval(doublereal t) const { @@ -632,7 +676,7 @@ class Product1 : public Func1 }; /** - * Product of two functions. + * Product of function and constant. */ class TimesConstant1 : public Func1 { @@ -669,6 +713,9 @@ class TimesConstant1 : public Func1 virtual int ID() const { return TimesConstantFuncType; } + virtual string type() const { + return "times-constant"; + } virtual doublereal isProportional(TimesConstant1& other) { if (func1().isIdentical(other.func1())) { @@ -743,6 +790,9 @@ class PlusConstant1 : public Func1 virtual int ID() const { return PlusConstantFuncType; } + virtual string type() const { + return "plus-constant"; + } virtual doublereal eval(doublereal t) const { return m_f1->eval(t) + m_c; @@ -808,6 +858,9 @@ class Ratio1 : public Func1 virtual int ID() const { return RatioFuncType; } + virtual string type() const { + return "ratio"; + } virtual doublereal eval(doublereal t) const { return m_f1->eval(t) / m_f2->eval(t); @@ -876,6 +929,9 @@ class Composite1 : public Func1 virtual int ID() const { return CompositeFuncType; } + virtual string type() const { + return "composite"; + } virtual doublereal eval(doublereal t) const { return m_f1->eval(m_f2->eval(t)); @@ -942,6 +998,10 @@ class Gaussian1 : public Func1 return *this; } + virtual string type() const { + return "gaussian"; + } + virtual doublereal eval(doublereal t) const { doublereal x = (t - m_t0)/m_tau; return m_A * std::exp(-x*x); @@ -1003,6 +1063,10 @@ class Poly1 : public Func1 return *this; } + virtual string type() const { + return "polynomial"; + } + virtual Func1& duplicate() const; virtual doublereal eval(doublereal t) const { @@ -1061,6 +1125,10 @@ class Fourier1 : public Func1 return *this; } + virtual string type() const { + return "fourier"; + } + virtual Func1& duplicate() const; virtual doublereal eval(doublereal t) const { @@ -1122,6 +1190,10 @@ class Arrhenius1 : public Func1 return *this; } + virtual string type() const { + return "arrhenius"; + } + virtual Func1& duplicate() const; virtual doublereal eval(doublereal t) const { @@ -1162,6 +1234,10 @@ class Periodic1 : public Func1 return *this; } + virtual string type() const { + return "periodic"; + } + virtual Func1& duplicate() const; virtual ~Periodic1() { diff --git a/src/numerics/Func1.cpp b/src/numerics/Func1.cpp index b52972a847..a951b988fd 100644 --- a/src/numerics/Func1.cpp +++ b/src/numerics/Func1.cpp @@ -860,7 +860,7 @@ doublereal Func1::isProportional(Func1& other) static bool isConstant(Func1& f) { - if (f.ID() == ConstFuncType) { + if (f.type() == "constant") { return true; } else { return false; @@ -869,12 +869,12 @@ static bool isConstant(Func1& f) static bool isConstant(shared_ptr f) { - return f->ID() == ConstFuncType; + return f->type() == "constant"; } static bool isZero(Func1& f) { - if (f.ID() == ConstFuncType && f.c() == 0.0) { + if (f.type() == "constant" && f.c() == 0.0) { return true; } else { return false; @@ -883,12 +883,12 @@ static bool isZero(Func1& f) static bool isZero(shared_ptr f) { - return f->ID() == ConstFuncType && f->c() == 0.0; + return f->type() == "constnat" && f->c() == 0.0; } static bool isOne(Func1& f) { - if (f.ID() == ConstFuncType && f.c() == 1.0) { + if (f.type() == "constant" && f.c() == 1.0) { return true; } else { return false; @@ -897,12 +897,12 @@ static bool isOne(Func1& f) static bool isOne(shared_ptr f) { - return f->ID() == ConstFuncType && f->c() == 1.0; + return f->type() == "constant" && f->c() == 1.0; } static bool isTimesConst(Func1& f) { - if (f.ID() == TimesConstantFuncType) { + if (f.type() == "times-constant") { return true; } else { return false; @@ -911,12 +911,12 @@ static bool isTimesConst(Func1& f) static bool isTimesConst(shared_ptr f) { - return f->ID() == TimesConstantFuncType; + return f->type() == "times-constant"; } static bool isExp(Func1& f) { - if (f.ID() == ExpFuncType) { + if (f.type() == "exp") { return true; } else { return false; @@ -925,12 +925,12 @@ static bool isExp(Func1& f) static bool isExp(shared_ptr f) { - return f->ID() == ExpFuncType; + return f->type() == "exp"; } static bool isPow(Func1& f) { - if (f.ID() == PowFuncType) { + if (f.type() == "pow") { return true; } else { return false; @@ -939,7 +939,7 @@ static bool isPow(Func1& f) static bool isPow(shared_ptr f) { - return f->ID() == PowFuncType; + return f->type() == "pow"; } Func1& newSumFunction(Func1& f1, Func1& f2) @@ -1281,7 +1281,7 @@ Func1& newTimesConstFunction(Func1& f, doublereal c) if (c == 1.0) { return f; } - if (f.ID() == TimesConstantFuncType) { + if (f.type() == "times-constant") { f.setC(f.c() * c); return f; } @@ -1296,7 +1296,7 @@ shared_ptr newTimesConstFunction(shared_ptr f, double c) if (c == 1.0) { return f; } - if (f->ID() == TimesConstantFuncType) { + if (f->type() == "times-constant") { return shared_ptr(new TimesConstant1(f->func1_shared(), f->c() * c)); } return shared_ptr(new TimesConstant1(f, c)); @@ -1315,7 +1315,7 @@ Func1& newPlusConstFunction(Func1& f, doublereal c) delete &f; return *(new Const1(cc)); } - if (f.ID() == PlusConstantFuncType) { + if (f.type() == "plus-constant") { f.setC(f.c() + c); return f; } @@ -1330,7 +1330,7 @@ shared_ptr newPlusConstFunction(shared_ptr f, double c) if (isConstant(f)) { return shared_ptr(new Const1(f->c() + c)); } - if (f->ID() == PlusConstantFuncType) { + if (f->type() == "plus-constant") { return shared_ptr(new PlusConstant1(f->func1_shared(), f->c() + c)); } return shared_ptr(new PlusConstant1(f, c));