Skip to content

Commit

Permalink
[Kinetics] Eliminate need for 'finalize' method
Browse files Browse the repository at this point in the history
All reaction-sized arrays are now allocated as reactions are added, which means
that the finalize() method is unnecessary and reactions can be continuously
added, even after the Kinetics object has been used for rate calculations.
  • Loading branch information
speth committed Apr 15, 2016
1 parent 4428a62 commit 1f231d1
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 117 deletions.
2 changes: 0 additions & 2 deletions include/cantera/kinetics/BulkKinetics.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class BulkKinetics : public Kinetics

virtual bool addReaction(shared_ptr<Reaction> r);
virtual void init();
virtual void finalize();
virtual bool ready() const;

virtual void setMultiplier(size_t i, double f);
virtual void invalidateCache();
Expand Down
6 changes: 3 additions & 3 deletions include/cantera/kinetics/EdgeKinetics.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class EdgeKinetics : public InterfaceKinetics
{
public:
//! Constructor
EdgeKinetics() : InterfaceKinetics() {}
EdgeKinetics() : InterfaceKinetics() {
m_nDim = 1;
}

EdgeKinetics(const EdgeKinetics& right) :
InterfaceKinetics(right) {
Expand All @@ -44,8 +46,6 @@ class EdgeKinetics : public InterfaceKinetics
virtual int type() const {
return cEdgeKinetics;
}

virtual void finalize();
};
}

Expand Down
2 changes: 0 additions & 2 deletions include/cantera/kinetics/GasKinetics.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ class GasKinetics : public BulkKinetics
virtual void init();
virtual bool addReaction(shared_ptr<Reaction> r);
virtual void modifyReaction(size_t i, shared_ptr<Reaction> rNew);
virtual void finalize();
virtual bool ready() const;
virtual void invalidateCache();
//@}

Expand Down
6 changes: 4 additions & 2 deletions include/cantera/kinetics/InterfaceKinetics.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ class InterfaceKinetics : public Kinetics
virtual void init();
virtual bool addReaction(shared_ptr<Reaction> r);
virtual void modifyReaction(size_t i, shared_ptr<Reaction> rNew);
virtual void finalize();
virtual bool ready() const;
//! @}

//! Internal routine that updates the Rates of Progress of the reactions
Expand Down Expand Up @@ -674,6 +672,10 @@ class InterfaceKinetics : public Kinetics
void applyStickingCorrection(double* kf);

int m_ioFlag;

//! Number of dimensions of reacting phase (2 for InterfaceKinetics, 1 for
//! EdgeKinetics)
size_t m_nDim;
};
}

Expand Down
6 changes: 5 additions & 1 deletion include/cantera/kinetics/Kinetics.h
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ class Kinetics
* The base class method does nothing, but derived classes may use this to
* perform any initialization (allocating arrays, etc.) that must be done
* after the reactions are entered.
* @deprecated No longer needed. To be removed after Cantera 2.3.
*/
virtual void finalize();

Expand Down Expand Up @@ -810,9 +811,12 @@ class Kinetics
/**
* Returns true if the kinetics manager has been properly initialized and
* finalized.
* @deprecated Object is always ready. To be removed after Cantera 2.3.
*/
virtual bool ready() const {
return false;
warn_deprecated("Kinetics::ready",
"Object is always ready. To be removed after Cantera 2.3.");
return true;
}

//! Check for duplicate reactions.
Expand Down
1 change: 0 additions & 1 deletion interfaces/cython/cantera/_cantera.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ cdef extern from "cantera/kinetics/Kinetics.h" namespace "Cantera":
void init() except +
void skipUndeclaredThirdBodies(cbool)
void addReaction(shared_ptr[CxxReaction]) except +
void finalize() except +
void modifyReaction(int, shared_ptr[CxxReaction]) except +
void invalidateCache() except +

Expand Down
1 change: 0 additions & 1 deletion interfaces/cython/cantera/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ cdef class _SolutionBase:
self.kinetics.skipUndeclaredThirdBodies(True)
for reaction in reactions:
self.kinetics.addReaction(reaction._reaction)
self.kinetics.finalize()


def __getitem__(self, selection):
Expand Down
10 changes: 0 additions & 10 deletions src/kinetics/BulkKinetics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,6 @@ void BulkKinetics::init()
m_grt.resize(m_kk);
}

void BulkKinetics::finalize()
{
m_finalized = true;
}

bool BulkKinetics::ready() const
{
return m_finalized;
}

void BulkKinetics::setMultiplier(size_t i, double f) {
Kinetics::setMultiplier(i, f);
m_ROP_ok = false;
Expand Down
16 changes: 3 additions & 13 deletions src/kinetics/GasKinetics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,11 @@ void GasKinetics::addFalloffReaction(FalloffReaction& r)
}
m_falloff_concm.install(nfall, efficiencies,
r.third_body.default_efficiency);
concm_falloff_values.resize(m_falloff_concm.workSize());

// install the falloff function calculator for this reaction
m_falloffn.install(nfall, r.reaction_type, r.falloff);
falloff_work.resize(m_falloffn.workSize());
}

void GasKinetics::addThreeBodyReaction(ThreeBodyReaction& r)
Expand All @@ -315,6 +317,7 @@ void GasKinetics::addThreeBodyReaction(ThreeBodyReaction& r)
}
m_3b_concm.install(nReactions()-1, efficiencies,
r.third_body.default_efficiency);
concm_3b_values.resize(m_3b_concm.workSize());
}

void GasKinetics::addPlogReaction(PlogReaction& r)
Expand Down Expand Up @@ -389,19 +392,6 @@ void GasKinetics::init()
m_logp_ref = log(thermo().refPressure()) - log(GasConstant);
}

void GasKinetics::finalize()
{
BulkKinetics::finalize();
falloff_work.resize(m_falloffn.workSize());
concm_3b_values.resize(m_3b_concm.workSize());
concm_falloff_values.resize(m_falloff_concm.workSize());
}

bool GasKinetics::ready() const
{
return m_finalized;
}

void GasKinetics::invalidateCache()
{
BulkKinetics::invalidateCache();
Expand Down
76 changes: 10 additions & 66 deletions src/kinetics/InterfaceKinetics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Copyright 2002 California Institute of Technology

#include "cantera/kinetics/InterfaceKinetics.h"
#include "cantera/kinetics/EdgeKinetics.h"
#include "cantera/kinetics/RateCoeffMgr.h"
#include "cantera/kinetics/ImplicitSurfChem.h"
#include "cantera/thermo/SurfPhase.h"
Expand All @@ -31,7 +30,8 @@ InterfaceKinetics::InterfaceKinetics(thermo_t* thermo) :
m_has_electrochem_rxns(false),
m_has_exchange_current_density_formulation(false),
m_phaseExistsCheck(false),
m_ioFlag(0)
m_ioFlag(0),
m_nDim(2)
{
if (thermo != 0) {
addPhase(*thermo);
Expand Down Expand Up @@ -708,6 +708,12 @@ bool InterfaceKinetics::addReaction(shared_ptr<Reaction> r_base)
size_t p = speciesPhaseIndex(k);
m_rxnPhaseIsProduct[i][p] = true;
}

deltaElectricEnergy_.push_back(0.0);
m_deltaG0.push_back(0.0);
m_deltaG.push_back(0.0);
m_ProdStanConcReac.push_back(0.0);

return true;
}

Expand Down Expand Up @@ -828,38 +834,25 @@ void InterfaceKinetics::init()
}
m_actConc.resize(m_kk);
m_conc.resize(m_kk);
m_StandardConc.resize(m_kk, 0.0);
m_mu0.resize(m_kk);
m_mu.resize(m_kk);
m_mu0_Kc.resize(m_kk);
m_grt.resize(m_kk);
m_pot.resize(m_kk, 0.0);
m_phi.resize(nPhases(), 0.0);
}

void InterfaceKinetics::finalize()
{
Kinetics::finalize();
deltaElectricEnergy_.resize(nReactions());
size_t ks = reactionPhaseIndex();
if (ks == npos) throw CanteraError("InterfaceKinetics::finalize",
"no surface phase is present.");

// Check to see that the interface routine has a dimension of 2
m_surf = (SurfPhase*)&thermo(ks);
if (m_surf->nDim() != 2) {
if (m_surf->nDim() != m_nDim) {
throw CanteraError("InterfaceKinetics::finalize",
"expected interface dimension = 2, but got dimension = {}",
m_surf->nDim());
}
m_StandardConc.resize(m_kk, 0.0);
m_deltaG0.resize(nReactions(), 0.0);
m_deltaG.resize(nReactions(), 0.0);
m_ProdStanConcReac.resize(nReactions(), 0.0);

if (m_thermo.size() != m_phaseExists.size()) {
throw CanteraError("InterfaceKinetics::finalize", "internal error");
}
m_finalized = true;
}

doublereal InterfaceKinetics::electrochem_beta(size_t irxn) const
Expand All @@ -872,11 +865,6 @@ doublereal InterfaceKinetics::electrochem_beta(size_t irxn) const
return 0.0;
}

bool InterfaceKinetics::ready() const
{
return m_finalized;
}

void InterfaceKinetics::advanceCoverages(doublereal tstep)
{
if (m_integrator == 0) {
Expand Down Expand Up @@ -1013,48 +1001,4 @@ void InterfaceKinetics::applyStickingCorrection(double* kf)
}
}


void EdgeKinetics::finalize()
{
// Note we can't call the Interface::finalize() routine because we need to
// check for a dimension of 1 below. Therefore, we have to malloc room in
// arrays that would normally be handled by the
// InterfaceKinetics::finalize() call.
Kinetics::finalize();

size_t safe_reaction_size = std::max<size_t>(nReactions(), 1);
deltaElectricEnergy_.resize(safe_reaction_size);
size_t ks = reactionPhaseIndex();
if (ks == npos) throw CanteraError("EdgeKinetics::finalize",
"no surface phase is present.");

// Check to see edge phase has a dimension of 1
m_surf = (SurfPhase*)&thermo(ks);
if (m_surf->nDim() != 1) {
throw CanteraError("EdgeKinetics::finalize",
"expected interface dimension = 1, but got dimension = {}",
m_surf->nDim());
}
m_StandardConc.resize(m_kk, 0.0);
m_deltaG0.resize(safe_reaction_size, 0.0);
m_deltaG.resize(safe_reaction_size, 0.0);

m_ProdStanConcReac.resize(safe_reaction_size, 0.0);

if (m_thermo.size() != m_phaseExists.size()) {
throw CanteraError("InterfaceKinetics::finalize", "internal error");
}

// Guarantee that these arrays can be converted to double* even in the
// special case where there are no reactions defined.
if (!nReactions()) {
m_perturb.resize(1, 1.0);
m_ropf.resize(1, 0.0);
m_ropr.resize(1, 0.0);
m_ropnet.resize(1, 0.0);
m_rkcn.resize(1, 0.0);
}
m_finalized = true;
}

}
8 changes: 3 additions & 5 deletions src/kinetics/Kinetics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,13 @@ void Kinetics::addPhase(thermo_t& thermo)
}
m_thermo.push_back(&thermo);
m_phaseindex[m_thermo.back()->id()] = nPhases();
m_kk += thermo.nSpecies();
}

void Kinetics::finalize()
{
m_kk = 0;
for (size_t n = 0; n < nPhases(); n++) {
size_t nsp = m_thermo[n]->nSpecies();
m_kk += nsp;
}
warn_deprecated("Kinetics::finalize",
"No longer needed. To be removed after Cantera 2.3.");
}

bool Kinetics::addReaction(shared_ptr<Reaction> r)
Expand Down
4 changes: 0 additions & 4 deletions src/kinetics/importKinetics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ bool installReactionArrays(const XML_Node& p, Kinetics& kin,
// purely additive.
vector<XML_Node*> rarrays = p.getChildren("reactionArray");
if (rarrays.empty()) {
kin.finalize();
return false;
}
for (size_t n = 0; n < rarrays.size(); n++) {
Expand Down Expand Up @@ -122,9 +121,6 @@ bool installReactionArrays(const XML_Node& p, Kinetics& kin,
kin.checkDuplicates();
}

// Finalize the installation of the kinetics, now that we know the true
// number of reactions in the mechanism, itot.
kin.finalize();
return true;
}

Expand Down
7 changes: 0 additions & 7 deletions test/kinetics/kineticsFromScratch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ TEST_F(KineticsFromScratch, add_elementary_reaction)
auto R = make_shared<ElementaryReaction>(reac, prod, rate);

kin.addReaction(R);
kin.finalize();
check_rates(0);
}

Expand All @@ -74,7 +73,6 @@ TEST_F(KineticsFromScratch, add_three_body_reaction)
auto R = make_shared<ThreeBodyReaction>(reac, prod, rate, tbody);

kin.addReaction(R);
kin.finalize();
check_rates(1);
}

Expand Down Expand Up @@ -123,7 +121,6 @@ TEST_F(KineticsFromScratch, add_falloff_reaction)
auto R = make_shared<FalloffReaction>(reac, prod, low_rate, high_rate, tbody);
R->falloff = newFalloff(TROE_FALLOFF, falloff_params);
kin.addReaction(R);
kin.finalize();
check_rates(2);
}

Expand All @@ -146,7 +143,6 @@ TEST_F(KineticsFromScratch, add_plog_reaction)

auto R = make_shared<PlogReaction>(reac, prod, Plog(rates));
kin.addReaction(R);
kin.finalize();
check_rates(3);
}

Expand Down Expand Up @@ -194,7 +190,6 @@ TEST_F(KineticsFromScratch, add_chebyshev_reaction)

auto R = make_shared<ChebyshevReaction>(reac, prod, rate);
kin.addReaction(R);
kin.finalize();
check_rates(4);
}

Expand Down Expand Up @@ -369,7 +364,6 @@ TEST_F(InterfaceKineticsFromScratch, add_surface_reaction)

auto R = make_shared<InterfaceReaction>(reac, prod, rate);
kin.addReaction(R);
kin.finalize();
check_rates(3);
}

Expand All @@ -384,6 +378,5 @@ TEST_F(InterfaceKineticsFromScratch, add_sticking_reaction)

auto R = make_shared<InterfaceReaction>(reac, prod, rate, true);
kin.addReaction(R);
kin.finalize();
check_rates(0);
}

0 comments on commit 1f231d1

Please sign in to comment.