diff --git a/Cantera/src/Func1.cpp b/Cantera/src/Func1.cpp index d4d39a453..6fcc8d53f 100644 --- a/Cantera/src/Func1.cpp +++ b/Cantera/src/Func1.cpp @@ -1,5 +1,7 @@ #include "Func1.h" #include "stringUtils.h" +#include "global.h" + using namespace std; namespace Cantera { @@ -13,12 +15,24 @@ namespace Cantera { Func1& Sin1::derivative() const { Func1* c = new Cos1(m_c); - return newTimesConstFunction(*c, m_c); + Func1* r = &newTimesConstFunction(*c, m_c); +#ifdef DEBUG_FUNC + cout << "Sin1::derivative: \n"; + cout << "function = \'" + write("x") + "\'\n"; + cout << "derivative = \'" + r->write("x") + "\'\n"; +#endif + return *r; } Func1& Cos1::derivative() const { Func1* s = new Sin1(m_c); - return newTimesConstFunction(*s, -m_c); + Func1* r = &newTimesConstFunction(*s, -m_c); +#ifdef DEBUG_FUNC + cout << "Cos1::derivative: \n"; + cout << "function = \'" + write("x") + "\'\n"; + cout << "derivative = \'" + r->write("x") + "\'\n"; +#endif + return *r; } Func1& Exp1::derivative() const { @@ -30,11 +44,23 @@ namespace Cantera { } Func1& Pow1::derivative() const { - Func1* f = new Pow1(m_c - 1.0); - if (m_c != 1.0) - return newTimesConstFunction(*f, m_c); - else - return *(new Const1(1.0)); + Func1* r; + if (m_c == 0.0) { + r = new Const1(0.0); + } + else if (m_c == 1.0) { + r = new Const1(1.0); + } + else { + Func1* f = new Pow1(m_c - 1.0); + r = &newTimesConstFunction(*f, m_c); + } +#ifdef DEBUG_FUNC + cout << "Pow1::derivative: \n"; + cout << "function = \'" + write("x") + "\'\n"; + cout << "derivative = \'" + r->write("x") + "\'\n"; +#endif + return *r; } string Func1::write(std::string arg) const { @@ -314,6 +340,11 @@ namespace Cantera { } Func1& newCompositeFunction(Func1& f1, Func1& f2) { + //#ifdef DEBUG_FUNC + //cout << "creating new composite function." << endl; + //cout << "f1 = " << f1.write("x") << " " << f1.ID() << endl; + //cout << "f2 = " << f2.write("x") << " " << f2.ID() << endl; + //#endif if (isZero(f1)) { delete &f1; delete &f2; return *(new Const1(0.0)); @@ -337,11 +368,6 @@ namespace Cantera { delete &f2; return *(new Pow1(c1c2)); } - if (isTimesConst(f1)) { - Func1& p = newTimesConstFunction(f2, f1.c()); - delete &f1; - return p; - } return *(new Composite1(f1, f2)); } @@ -364,6 +390,15 @@ namespace Cantera { if (c == 0.0) { return f; } + if (isConstant(f)) { + doublereal cc = f.c() + c; + delete &f; + return *(new Const1(cc)); + } + if (f.ID() == PlusConstantFuncType) { + f.setC(f.c() + c); + return f; + } return *(new PlusConstant1(f, c)); } diff --git a/Cantera/src/Func1.h b/Cantera/src/Func1.h index 4e5b47775..246845d3e 100644 --- a/Cantera/src/Func1.h +++ b/Cantera/src/Func1.h @@ -11,6 +11,9 @@ #ifndef CT_FUNC1_H #define CT_FUNC1_H +#undef DEBUG_FUNC + + #ifdef WIN32 #pragma warning(disable:4786) #pragma warning(disable:4503) @@ -224,8 +227,6 @@ namespace Cantera { Sum1(Func1& f1, Func1& f2) { m_f1 = &f1; m_f2 = &f2; - if (m_f1 == m_f2) - cout << "Same functions!" << endl; m_f1->setParent(this); m_f2->setParent(this); } @@ -300,7 +301,6 @@ namespace Cantera { } virtual ~Product1() { - //cout << "In Product1 destructor, deleting" << m_f1 << " " << m_f2 << endl; delete m_f1; delete m_f2; } @@ -379,7 +379,6 @@ namespace Cantera { } virtual ~PlusConstant1() { - //cout << "PlusConstant1: deleting " << m_f1 << endl; delete m_f1; } virtual int ID() const { return PlusConstantFuncType; } @@ -413,7 +412,6 @@ namespace Cantera { m_f2 = &f2; } virtual ~Ratio1() { - //cout << "Ratio1: deleting " << m_f1 << " " << m_f2 << endl; delete m_f1; delete m_f2; } @@ -465,11 +463,22 @@ namespace Cantera { return dup; } virtual Func1& derivative() const { - Func1& d1 = m_f1->derivative(); - Func1& d3 = newCompositeFunction(d1, m_f2->duplicate()); - Func1& d2 = m_f2->derivative(); - Func1& p = newProdFunction(d3, d2); - return p; + Func1* d1 = &m_f1->derivative(); + + Func1* d3 = &newCompositeFunction(*d1, m_f2->duplicate()); + Func1* d2 = &m_f2->derivative(); + Func1* p = &newProdFunction(*d3, *d2); +#ifdef DEBUG_FUNC + cout << "Composite1::derivative: \n"; + cout << "f1 = " << m_f1->write("x") << endl; + cout << "f2 = " << m_f2->write("x") << endl; + cout << "d1 = " << d1 << " " << d1->write("x") << endl; + cout << "d3 = " << d3->write("x") << endl; + cout << "d2 = " << d2->write("x") << endl; + cout << "function = \'" + write("x") + "\'\n"; + cout << "derivative = \'" + p->write("x") + "\'\n"; +#endif + return *p; } virtual std::string write(std::string arg) const; virtual int order() const { return 2; }