Skip to content

Commit

Permalink
[Func1] Change argument order in newFunc1 variant
Browse files Browse the repository at this point in the history
List order/number last as it is not used by all variants; also update
order for func_new_advanced in clib.
  • Loading branch information
ischoegl authored and speth committed Jun 29, 2023
1 parent 5c9f8a9 commit 010d550
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 63 deletions.
3 changes: 2 additions & 1 deletion include/cantera/clib/ctfunc.h
Expand Up @@ -16,7 +16,8 @@ extern "C" {

CANTERA_CAPI int func_new(int type, size_t n, size_t lenp, const double* p);
CANTERA_CAPI int func_new_basic(const char* type, double c);
CANTERA_CAPI int func_new_advanced(const char* type, size_t n, size_t lenp, const double* p);
CANTERA_CAPI int func_new_advanced(
const char* type, size_t lenp, const double* p, size_t n);
CANTERA_CAPI int func_new_math(const char* type, size_t a, size_t b);
CANTERA_CAPI int func_new_misc(const char* type, size_t a, double c);
CANTERA_CAPI int func_del(int i);
Expand Down
22 changes: 11 additions & 11 deletions include/cantera/numerics/Func1.h
Expand Up @@ -201,7 +201,7 @@ class Sin1 : public Func1
}

//! Constructor uses single parameter (frequency)
Sin1(size_t n, const vector<double>& params);
Sin1(const vector<double>& paramss, size_t n=npos);

Sin1(const Sin1& b) :
Func1(b) {
Expand Down Expand Up @@ -247,7 +247,7 @@ class Cos1 : public Func1
}

//! Constructor uses single parameter (frequency)
Cos1(size_t n, const vector<double>& params);
Cos1(const vector<double>& params, size_t n=npos);

Cos1(const Cos1& b) :
Func1(b) {
Expand Down Expand Up @@ -287,7 +287,7 @@ class Exp1 : public Func1
}

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

Exp1(const Exp1& b) :
Func1(b) {
Expand Down Expand Up @@ -326,7 +326,7 @@ class Log1 : public Func1
}

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

virtual string type() const {
return "log";
Expand All @@ -350,7 +350,7 @@ class Pow1 : public Func1
}

//! Constructor uses single parameter (exponent)
Pow1(size_t n, const vector<double>& params);
Pow1(const vector<double>& params, size_t n=npos);

Pow1(const Pow1& b) :
Func1(b) {
Expand Down Expand Up @@ -395,7 +395,7 @@ class Tabulated1 : public Func1

//! Constructor uses 2*n parameters
//! [t0, t1, .. tn-1, f0, f1, .. fn-1]
Tabulated1(size_t n, const vector<double>& params);
Tabulated1(const vector<double>& params, size_t n);

//! Set the interpolation method
void setMethod(const string& mode);
Expand Down Expand Up @@ -435,7 +435,7 @@ class Const1 : public Func1
}

//! Constructor uses single parameter (constant)
Const1(size_t n, const vector<double>& params);
Const1(const vector<double>& params, size_t n=npos);

Const1(const Const1& b) :
Func1(b) {
Expand Down Expand Up @@ -979,7 +979,7 @@ class Gaussian1 : public Func1

//! Constructor uses 3 parameters in the following order:
//! [A, t0, fwhm]
Gaussian1(size_t n, const vector<double>& params);
Gaussian1(const vector<double>& params, size_t n=npos);

Gaussian1(const Gaussian1& b) :
Func1(b) {
Expand Down Expand Up @@ -1046,7 +1046,7 @@ class Poly1 : public Func1

//! Constructor uses n + 1 parameters in the following order:
//! [an, ..., a1, a0]
Poly1(size_t n, const vector<double>& params);
Poly1(const vector<double>& params, size_t n);

Poly1(const Poly1& b) :
Func1(b) {
Expand Down Expand Up @@ -1105,7 +1105,7 @@ class Fourier1 : public Func1

//! Constructor uses 2 * n + 2 parameters in the following order:
//! [a0, a1, ... an, omega, b1, ... bn]
Fourier1(size_t n, const vector<double>& params);
Fourier1(const vector<double>& params, size_t n);

Fourier1(const Fourier1& b) :
Func1(b) {
Expand Down Expand Up @@ -1171,7 +1171,7 @@ class Arrhenius1 : public Func1

//! Constructor uses 3 * n parameters in the following order:
//! [A1, b1, E1, A2, b2, E2, ... An, bn, En]
Arrhenius1(size_t n, const vector<double>& params);
Arrhenius1(const vector<double>& params, size_t n=1);

Arrhenius1(const Arrhenius1& b) :
Func1() {
Expand Down
10 changes: 5 additions & 5 deletions include/cantera/numerics/Func1Factory.h
Expand Up @@ -17,9 +17,9 @@ namespace Cantera
//! This class is mainly used via the newFunc1() function, for example:
//!
//! ```cpp
//! shared_ptr<Func1> d1 = newFunc1("sin", 0, {1.0});
//! shared_ptr<Func1> d1 = newFunc1("sin", {1.0});
//! ```
class Func1Factory : public Factory<Func1, size_t, const vector<double>&>
class Func1Factory : public Factory<Func1, const vector<double>&, size_t>
{
public:
/**
Expand Down Expand Up @@ -115,10 +115,10 @@ shared_ptr<Func1> newFunc1(const string& func1Type, double coeff=1.);

//! Create a new Func1 functor object.
//! @param func1Type string identifying function type.
//! @param n Integer; definition depends on the function type.
//! @param params Parameter vector; definition depends on the function type.
shared_ptr<Func1> newFunc1(const string& func1Type, size_t n=0,
const vector<double>& params={});
//! @param n Integer; definition depends on function type and may or may not be used.
shared_ptr<Func1> newFunc1(const string& func1Type,
const vector<double>& params, size_t n=1);

//! Create a new Func1 functor object based on mathematical operations.
//! @param mathType String identifying operation.
Expand Down
12 changes: 6 additions & 6 deletions src/clib/ctfunc.cpp
Expand Up @@ -41,19 +41,19 @@ extern "C" {
} else if (type == FourierFuncType) {
vector<double> par(lenp);
std::copy(params, params + lenp, par.data());
r = newFunc1("fourier", n, par);
r = newFunc1("fourier", par, n);
} else if (type == GaussianFuncType) {
vector<double> par(lenp);
std::copy(params, params + lenp, par.data());
r = newFunc1("gaussian", n, par);
r = newFunc1("gaussian", par, n);
} else if (type == PolyFuncType) {
vector<double> par(lenp);
std::copy(params, params + lenp, par.data());
r = newFunc1("polynomial", n, par);
r = newFunc1("polynomial", par, n);
} else if (type == ArrheniusFuncType) {
vector<double> par(lenp);
std::copy(params, params + lenp, par.data());
r = newFunc1("arrhenius", n, par);
r = newFunc1("arrhenius", par, n);
} else if (type == PeriodicFuncType) {
r = newMath1("periodic", FuncCabinet::at(n), params[0]);
} else if (type == SumFuncType) {
Expand Down Expand Up @@ -88,12 +88,12 @@ extern "C" {
}
}

int func_new_advanced(const char* type, size_t n, size_t lenp, const double* params)
int func_new_advanced(const char* type, size_t lenp, const double* params, size_t n)
{
try {
vector<double> par(lenp);
std::copy(params, params + lenp, par.data());
return FuncCabinet::add(newFunc1(type, n, par));
return FuncCabinet::add(newFunc1(type, par, n));
} catch (...) {
return handleAllExceptions(-1, ERR);
}
Expand Down
22 changes: 11 additions & 11 deletions src/numerics/Func1.cpp
Expand Up @@ -147,7 +147,7 @@ void Func1::setParent(Func1* p)

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

Sin1::Sin1(size_t n, const vector<double>& params)
Sin1::Sin1(const vector<double>& params, size_t n)
{
if (params.size() != 1) {
throw CanteraError("Sin1::Sin1",
Expand Down Expand Up @@ -189,7 +189,7 @@ shared_ptr<Func1> Sin1::derivative3() const

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

Cos1::Cos1(size_t n, const vector<double>& params)
Cos1::Cos1(const vector<double>& params, size_t n)
{
if (params.size() != 1) {
throw CanteraError("Cos1::Cos1",
Expand Down Expand Up @@ -231,7 +231,7 @@ std::string Cos1::write(const std::string& arg) const

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

Exp1::Exp1(size_t n, const vector<double>& params)
Exp1::Exp1(const vector<double>& params, size_t n)
{
if (params.size() != 1) {
throw CanteraError("Exp1::Exp1",
Expand Down Expand Up @@ -276,7 +276,7 @@ std::string Exp1::write(const std::string& arg) const
}
}

Log1::Log1(size_t n, const vector<double>& params)
Log1::Log1(const vector<double>& params, size_t n)
{
if (params.size() != 1) {
throw CanteraError("Log1::Log1",
Expand Down Expand Up @@ -304,7 +304,7 @@ std::string Log1::write(const std::string& arg) const

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

Pow1::Pow1(size_t n, const vector<double>& params)
Pow1::Pow1(const vector<double>& params, size_t n)
{
if (params.size() != 1) {
throw CanteraError("Pow1::Pow1",
Expand Down Expand Up @@ -349,7 +349,7 @@ shared_ptr<Func1> Pow1::derivative3() const

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

Const1::Const1(size_t n, const vector<double>& params)
Const1::Const1(const vector<double>& params, size_t n)
{
if (params.size() != 1) {
throw CanteraError("Const1::Const1",
Expand All @@ -358,7 +358,7 @@ Const1::Const1(size_t n, const vector<double>& params)
m_c = params[0];
}

Poly1::Poly1(size_t n, const vector<double>& params)
Poly1::Poly1(const vector<double>& params, size_t n)
{
if (n < 0) {
throw CanteraError("Poly1::Poly1",
Expand All @@ -372,7 +372,7 @@ Poly1::Poly1(size_t n, const vector<double>& params)
copy(params.data(), params.data() + m_cpoly.size(), m_cpoly.begin());
}

Fourier1::Fourier1(size_t n, const vector<double>& params)
Fourier1::Fourier1(const vector<double>& params, size_t n)
{
if (n < 1) {
throw CanteraError("Fourier1::Fourier1",
Expand All @@ -391,7 +391,7 @@ Fourier1::Fourier1(size_t n, const vector<double>& params)
copy(params.data() + n + 2, params.data() + 2 * n + 2, m_csin.begin());
}

Gaussian1::Gaussian1(size_t n, const vector<double>& params)
Gaussian1::Gaussian1(const vector<double>& params, size_t n)
{
if (params.size() != 3) {
throw CanteraError("Gaussian1::Gaussian1",
Expand All @@ -402,7 +402,7 @@ Gaussian1::Gaussian1(size_t n, const vector<double>& params)
m_tau = params[2] / (2. * sqrt(log(2.)));
}

Arrhenius1::Arrhenius1(size_t n, const vector<double>& params)
Arrhenius1::Arrhenius1(const vector<double>& params, size_t n)
{
if (n < 1) {
throw CanteraError("Arrhenius1::Arrhenius1",
Expand Down Expand Up @@ -440,7 +440,7 @@ Tabulated1::Tabulated1(size_t n, const double* tvals, const double* fvals,
setMethod(method);
}

Tabulated1::Tabulated1(size_t n, const vector<double>& params) : m_isLinear(true)
Tabulated1::Tabulated1(const vector<double>& params, size_t n) : m_isLinear(true)
{
if (n < 1) {
throw CanteraError("Tabulated1::Tabulated1",
Expand Down
58 changes: 29 additions & 29 deletions src/numerics/Func1Factory.cpp
Expand Up @@ -13,44 +13,44 @@ std::mutex Func1Factory::domain_mutex;

Func1Factory::Func1Factory()
{
reg("functor", [](size_t n, const vector<double>& params) {
reg("functor", [](const vector<double>& params, size_t n) {
return new Func1();
});
reg("sin", [](size_t n, const vector<double>& params) {
return new Sin1(n, params);
reg("sin", [](const vector<double>& params, size_t n) {
return new Sin1(params);
});
reg("cos", [](size_t n, const vector<double>& params) {
return new Cos1(n, params);
reg("cos", [](const vector<double>& params, size_t n) {
return new Cos1(params);
});
reg("exp", [](size_t n, const vector<double>& params) {
return new Exp1(n, params);
reg("exp", [](const vector<double>& params, size_t n) {
return new Exp1(params);
});
reg("log", [](size_t n, const vector<double>& params) {
return new Log1(n, params);
reg("log", [](const vector<double>& params, size_t n) {
return new Log1(params);
});
reg("pow", [](size_t n, const vector<double>& params) {
return new Pow1(n, params);
reg("pow", [](const vector<double>& params, size_t n) {
return new Pow1(params);
});
reg("constant", [](size_t n, const vector<double>& params) {
return new Const1(n, params);
reg("constant", [](const vector<double>& params, size_t n) {
return new Const1(params);
});
reg("polynomial", [](size_t n, const vector<double>& params) {
return new Poly1(n, params);
reg("polynomial", [](const vector<double>& params, size_t n) {
return new Poly1(params, n);
});
reg("fourier", [](size_t n, const vector<double>& params) {
return new Fourier1(n, params);
reg("fourier", [](const vector<double>& params, size_t n) {
return new Fourier1(params, n);
});
reg("gaussian", [](size_t n, const vector<double>& params) {
return new Gaussian1(n, params);
reg("gaussian", [](const vector<double>& params, size_t n) {
return new Gaussian1(params, n);
});
reg("arrhenius", [](size_t n, const vector<double>& params) {
return new Arrhenius1(n, params);
reg("arrhenius", [](const vector<double>& params, size_t n) {
return new Arrhenius1(params, n);
});
reg("tabulated-linear", [](size_t n, const vector<double>& params) {
return new Tabulated1(n, params);
reg("tabulated-linear", [](const vector<double>& params, size_t n) {
return new Tabulated1(params, n);
});
reg("tabulated-previous", [](size_t n, const vector<double>& params) {
auto fcn = new Tabulated1(n, params);
reg("tabulated-previous", [](const vector<double>& params, size_t n) {
auto fcn = new Tabulated1(params, n);
fcn->setMethod("previous");
return fcn;
});
Expand Down Expand Up @@ -145,14 +145,14 @@ void Math1FactoryB::deleteFactory()
shared_ptr<Func1> newFunc1(const string& func1Type, double c)
{
return shared_ptr<Func1>(
Func1Factory::factory()->create(func1Type, 0, {c}));
Func1Factory::factory()->create(func1Type, {c}, npos));
}

shared_ptr<Func1> newFunc1(const string& func1Type, size_t n,
const vector<double>& params)
shared_ptr<Func1> newFunc1(const string& func1Type,
const vector<double>& params, size_t n)
{
return shared_ptr<Func1>(
Func1Factory::factory()->create(func1Type, n, params));
Func1Factory::factory()->create(func1Type, params, n));
}

shared_ptr<Func1> newMath1(const string& mathType, const shared_ptr<Func1> f1,
Expand Down

0 comments on commit 010d550

Please sign in to comment.