Skip to content

Commit

Permalink
[Func1] Add Log1
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl authored and speth committed Jun 29, 2023
1 parent b1b9604 commit 8424b80
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
28 changes: 28 additions & 0 deletions include/cantera/numerics/Func1.h
Expand Up @@ -75,6 +75,10 @@ class Func1 : public std::enable_shared_from_this<Func1>

virtual int ID() const;

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

//! Calls method eval to evaluate the function
doublereal operator()(doublereal t) const;

Expand Down Expand Up @@ -296,6 +300,30 @@ class Exp1 : public Func1
};


//! implements the natural logarithm function (log)
class Log1 : public Func1
{
public:
Log1(double A=1.0) {
m_c = A;
}

//! Constructor uses single parameter (factor)
Log1(size_t n, const vector<double>& params);

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

virtual double eval(double t) const {
return log(m_c * t);
}

virtual shared_ptr<Func1> derivative3() const;

virtual std::string write(const string& arg) const;
};

//! implements the power function (pow)
class Pow1 : public Func1
{
Expand Down
26 changes: 26 additions & 0 deletions src/numerics/Func1.cpp
Expand Up @@ -246,6 +246,32 @@ std::string Exp1::write(const std::string& arg) const
}
}

Log1::Log1(size_t n, const vector<double>& params)
{
if (params.size() != 1) {
throw CanteraError("Log1::Log1",
"Constructor needs exactly one parameter (factor).");
}
m_c = params[0];
}

shared_ptr<Func1> Log1::derivative3() const
{
auto f = shared_ptr<Func1>(new Pow1(-1.));
if (m_c != 1.0) {
return newTimesConstFunction(f, m_c);
}
return f;
}

std::string Log1::write(const std::string& arg) const
{
if (m_c == 1.0) {
return fmt::format("\\log({})", arg);
}
return fmt::format("\\log({}{})", m_c, arg);
}

/******************************************************************************/

Pow1::Pow1(size_t n, const vector<double>& params)
Expand Down

0 comments on commit 8424b80

Please sign in to comment.