Skip to content

Commit

Permalink
[Func1] Remove raw pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Jul 31, 2024
1 parent 90f1cfa commit 32b81b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 139 deletions.
142 changes: 14 additions & 128 deletions include/cantera/numerics/Func1.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,9 @@ class Func1
public:
Func1() = default;

Func1(shared_ptr<Func1> f1, shared_ptr<Func1> f2)
: m_f1_shared(f1), m_f2_shared(f2)
{
m_f1 = f1.get();
m_f2 = f2.get();
}
Func1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : m_f1(f1), m_f2(f2) {}

Func1(shared_ptr<Func1> f1, double A) : m_c(A), m_f1_shared(f1) {
m_f1 = f1.get();
}
Func1(shared_ptr<Func1> f1, double A) : m_c(A), m_f1(f1) {}

virtual ~Func1() = default;

Expand Down Expand Up @@ -132,28 +125,25 @@ class Func1
//! Accessor function for the stored constant
double c() const;

//! Accessor function for m_f1_shared
//! Accessor function for m_f1
//! @since New in %Cantera 3.0.
shared_ptr<Func1> func1_shared() const {
return m_f1_shared;
return m_f1;
}

//! Accessor function for m_f2_shared
//! Accessor function for m_f2
//! @since New in %Cantera 3.0.
shared_ptr<Func1> func2_shared() const {
return m_f2_shared;
return m_f2;
}

//! Return the order of the function, if it makes sense
virtual int order() const;

protected:
double m_c = 0.0;
Func1* m_f1 = nullptr;
Func1* m_f2 = nullptr;

shared_ptr<Func1> m_f1_shared;
shared_ptr<Func1> m_f2_shared;
shared_ptr<Func1> m_f1;
shared_ptr<Func1> m_f2;
};

//! Sum of two functions.
Expand Down Expand Up @@ -186,7 +176,7 @@ shared_ptr<Func1> newPlusConstFunction(shared_ptr<Func1> f1, double c);

//! Implements the @c sin() function.
/*!
* The functor class with type @c "sin" returns @f$ f(x) = \cos(\omega x) @f$,
* The functor class with type @c "sin" returns @f$ f(x) = \sin(\omega x) @f$,
* where the argument @f$ x @f$ is in radians.
* @param omega Frequency @f$ \omega @f$ (default=1.0)
* @ingroup func1basic
Expand Down Expand Up @@ -426,22 +416,8 @@ class Const1 : public Func1
class Sum1 : public Func1
{
public:
Sum1(Func1& f1, Func1& f2) {
m_f1 = &f1;
m_f2 = &f2;
}

Sum1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}

~Sum1() override {
if (!m_f1_shared) {
delete m_f1;
}
if (!m_f2_shared) {
delete m_f2;
}
}

string type() const override {
return "sum";
}
Expand All @@ -451,7 +427,7 @@ class Sum1 : public Func1
}

shared_ptr<Func1> derivative() const override {
return newSumFunction(m_f1_shared->derivative(), m_f2_shared->derivative());
return newSumFunction(m_f1->derivative(), m_f2->derivative());
}

int order() const override {
Expand All @@ -471,22 +447,8 @@ class Sum1 : public Func1
class Diff1 : public Func1
{
public:
Diff1(Func1& f1, Func1& f2) {
m_f1 = &f1;
m_f2 = &f2;
}

Diff1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}

~Diff1() override {
if (!m_f1_shared) {
delete m_f1;
}
if (!m_f2_shared) {
delete m_f2;
}
}

string type() const override {
return "diff";
}
Expand All @@ -496,7 +458,7 @@ class Diff1 : public Func1
}

shared_ptr<Func1> derivative() const override {
return newDiffFunction(m_f1_shared->derivative(), m_f2_shared->derivative());
return newDiffFunction(m_f1->derivative(), m_f2->derivative());
}

int order() const override {
Expand All @@ -517,22 +479,8 @@ class Diff1 : public Func1
class Product1 : public Func1
{
public:
Product1(Func1& f1, Func1& f2) {
m_f1 = &f1;
m_f2 = &f2;
}

Product1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}

~Product1() override {
if (!m_f1_shared) {
delete m_f1;
}
if (!m_f2_shared) {
delete m_f2;
}
}

string type() const override {
return "product";
}
Expand Down Expand Up @@ -560,19 +508,8 @@ class Product1 : public Func1
class TimesConstant1 : public Func1
{
public:
TimesConstant1(Func1& f1, double a) {
m_f1 = &f1;
m_c = a;
}

TimesConstant1(shared_ptr<Func1> f1, double a) : Func1(f1, a) {}

~TimesConstant1() override {
if (!m_f1_shared) {
delete m_f1;
}
}

string type() const override {
return "times-constant";
}
Expand All @@ -598,7 +535,7 @@ class TimesConstant1 : public Func1
}

shared_ptr<Func1> derivative() const override {
return newTimesConstFunction(m_f1_shared->derivative(), m_c);
return newTimesConstFunction(m_f1->derivative(), m_c);
}

string write(const string& arg) const override;
Expand All @@ -618,19 +555,8 @@ class TimesConstant1 : public Func1
class PlusConstant1 : public Func1
{
public:
PlusConstant1(Func1& f1, double a) {
m_f1 = &f1;
m_c = a;
}

PlusConstant1(shared_ptr<Func1> f1, double a) : Func1(f1, a) {}

~PlusConstant1() override {
if (!m_f1_shared) {
delete m_f1;
}
}

string type() const override {
return "plus-constant";
}
Expand All @@ -640,7 +566,7 @@ class PlusConstant1 : public Func1
}

shared_ptr<Func1> derivative() const override {
return m_f1_shared->derivative();
return m_f1->derivative();
}

string write(const string& arg) const override;
Expand All @@ -661,22 +587,8 @@ class PlusConstant1 : public Func1
class Ratio1 : public Func1
{
public:
Ratio1(Func1& f1, Func1& f2) {
m_f1 = &f1;
m_f2 = &f2;
}

Ratio1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}

~Ratio1() override {
if (!m_f1_shared) {
delete m_f1;
}
if (!m_f2_shared) {
delete m_f2;
}
}

string type() const override {
return "ratio";
}
Expand Down Expand Up @@ -704,22 +616,8 @@ class Ratio1 : public Func1
class Composite1 : public Func1
{
public:
Composite1(Func1& f1, Func1& f2) {
m_f1 = &f1;
m_f2 = &f2;
}

Composite1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}

~Composite1() override {
if (!m_f1_shared) {
delete m_f1;
}
if (!m_f2_shared) {
delete m_f2;
}
}

string type() const override {
return "composite";
}
Expand Down Expand Up @@ -832,8 +730,7 @@ class Poly13 : public Func1
* Implements a Fourier cosine/sine series.
* The functor class with type @c "Fourier" returns
* @f[
* f(t) = \frac{A_0}{2} +
* \sum_{n=1}^N A_n \cos (n \omega t) + B_n \sin (n \omega t)
* f(t) = \frac{a_0}{2} + \sum_{n=1}^N a_n \cos (n \omega t) + b_n \sin (n \omega t)
* @f]
* @ingroup func1advanced
*/
Expand Down Expand Up @@ -927,23 +824,12 @@ class Arrhenius1 : public Func1
class Periodic1 : public Func1
{
public:
Periodic1(Func1& f, double T) {
m_f1 = &f;
m_c = T;
}

Periodic1(shared_ptr<Func1> f, double A) : Func1(f, A) {}

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

~Periodic1() override {
if (!m_f1_shared) {
delete m_f1;
}
}

double eval(double t) const override {
int np = int(t/m_c);
double time = t - np*m_c;
Expand Down
21 changes: 10 additions & 11 deletions src/numerics/Func1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,15 +444,14 @@ string Const1::write(const string& arg) const

string Ratio1::write(const string& arg) const
{
return "\\frac{" + m_f1->write(arg) + "}{"
+ m_f2->write(arg) + "}";
return "\\frac{" + m_f1->write(arg) + "}{" + m_f2->write(arg) + "}";
}

shared_ptr<Func1> Ratio1::derivative() const {
auto a1 = newProdFunction(m_f1_shared->derivative(), m_f2_shared);
auto a2 = newProdFunction(m_f1_shared, m_f2_shared->derivative());
auto a1 = newProdFunction(m_f1->derivative(), m_f2);
auto a2 = newProdFunction(m_f1, m_f2->derivative());
auto s = newDiffFunction(a1, a2);
auto p = newProdFunction(m_f2_shared, m_f2_shared);
auto p = newProdFunction(m_f2, m_f2);
return newRatioFunction(s, p);
}

Expand All @@ -470,8 +469,8 @@ string Product1::write(const string& arg) const
}

shared_ptr<Func1> Product1::derivative() const {
auto a1 = newProdFunction(m_f1_shared, m_f2_shared->derivative());
auto a2 = newProdFunction(m_f2_shared, m_f1_shared->derivative());
auto a1 = newProdFunction(m_f1, m_f2->derivative());
auto a2 = newProdFunction(m_f2, m_f1->derivative());
return newSumFunction(a1, a2);
}

Expand Down Expand Up @@ -504,9 +503,9 @@ string Composite1::write(const string& arg) const
}

shared_ptr<Func1> Composite1::derivative() const {
auto d1 = m_f1_shared->derivative();
auto d2 = m_f2_shared->derivative();
auto d3 = newCompositeFunction(d1, m_f2_shared);
auto d1 = m_f1->derivative();
auto d2 = m_f2->derivative();
auto d3 = newCompositeFunction(d1, m_f2);
return newProdFunction(d3, d2);
}

Expand Down Expand Up @@ -537,14 +536,14 @@ string PlusConstant1::write(const string& arg) const
return fmt::format("{} + {}", m_f1->write(arg), m_c);
}


double Func1::isProportional(TimesConstant1& other)
{
if (isIdentical(*other.func1_shared())) {
return other.c();
}
return 0.0;
}

double Func1::isProportional(Func1& other)
{
if (isIdentical(other)) {
Expand Down

0 comments on commit 32b81b8

Please sign in to comment.