Skip to content

Commit

Permalink
[Func1] Implement Func1::type
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl authored and speth committed Jun 29, 2023
1 parent 15955c7 commit bcfc7cd
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 20 deletions.
84 changes: 80 additions & 4 deletions include/cantera/numerics/Func1.h
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Func1> derivative3() const;
};
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<Func1> derivative3() const;
};
Expand Down Expand Up @@ -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<Func1> derivative3() const;
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand All @@ -632,7 +676,7 @@ class Product1 : public Func1
};

/**
* Product of two functions.
* Product of function and constant.
*/
class TimesConstant1 : public Func1
{
Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1162,6 +1234,10 @@ class Periodic1 : public Func1
return *this;
}

virtual string type() const {
return "periodic";
}

virtual Func1& duplicate() const;

virtual ~Periodic1() {
Expand Down
32 changes: 16 additions & 16 deletions src/numerics/Func1.cpp
Expand Up @@ -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;
Expand All @@ -869,12 +869,12 @@ static bool isConstant(Func1& f)

static bool isConstant(shared_ptr<Func1> 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;
Expand All @@ -883,12 +883,12 @@ static bool isZero(Func1& f)

static bool isZero(shared_ptr<Func1> 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;
Expand All @@ -897,12 +897,12 @@ static bool isOne(Func1& f)

static bool isOne(shared_ptr<Func1> 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;
Expand All @@ -911,12 +911,12 @@ static bool isTimesConst(Func1& f)

static bool isTimesConst(shared_ptr<Func1> 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;
Expand All @@ -925,12 +925,12 @@ static bool isExp(Func1& f)

static bool isExp(shared_ptr<Func1> 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;
Expand All @@ -939,7 +939,7 @@ static bool isPow(Func1& f)

static bool isPow(shared_ptr<Func1> f)
{
return f->ID() == PowFuncType;
return f->type() == "pow";
}

Func1& newSumFunction(Func1& f1, Func1& f2)
Expand Down Expand Up @@ -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;
}
Expand All @@ -1296,7 +1296,7 @@ shared_ptr<Func1> newTimesConstFunction(shared_ptr<Func1> f, double c)
if (c == 1.0) {
return f;
}
if (f->ID() == TimesConstantFuncType) {
if (f->type() == "times-constant") {
return shared_ptr<Func1>(new TimesConstant1(f->func1_shared(), f->c() * c));
}
return shared_ptr<Func1>(new TimesConstant1(f, c));
Expand All @@ -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;
}
Expand All @@ -1330,7 +1330,7 @@ shared_ptr<Func1> newPlusConstFunction(shared_ptr<Func1> f, double c)
if (isConstant(f)) {
return shared_ptr<Func1>(new Const1(f->c() + c));
}
if (f->ID() == PlusConstantFuncType) {
if (f->type() == "plus-constant") {
return shared_ptr<Func1>(new PlusConstant1(f->func1_shared(), f->c() + c));
}
return shared_ptr<Func1>(new PlusConstant1(f, c));
Expand Down

0 comments on commit bcfc7cd

Please sign in to comment.