| Cantera 3.0.0

Loading...
Searching...
No Matches
ReactionRateDelegator.cpp
Go to the documentation of this file.
1//! @file ReactionRateDelegator.cpp
2
3// This file is part of Cantera. See License.txt in the top-level directory or
4// at https://cantera.org/license.txt for license and copyright information.
5
6#include "cantera/kinetics/ReactionRateDelegator.h"
7#include "cantera/kinetics/Kinetics.h"
8#include "cantera/base/Solution.h"
9
10 namespace Cantera
11{
12
13ReactionDataDelegator::ReactionDataDelegator()
14{
15 install("update", m_update,
16 [](void*) {
17 throw NotImplementedError("ReactionDataDelegator::update");
18 return 0.0; // necessary to set lambda's function signature
19 }
20 );
21}
22
23 bool ReactionDataDelegator::update(const ThermoPhase& phase, const Kinetics& kin)
24{
25 if (!m_wrappedSolution) {
26 auto wrapperType = ExtensionManager::getSolutionWrapperType(m_rateType);
27 auto soln = kin.root();
28 if (!soln) {
29 throw CanteraError("ReactionDataDelegator::update",
30 "Phase must be instantiated as a Solution to use extensible "
31 "reactions of type '{}'", m_rateType);
32 }
33 if (soln->getExternalHandle(wrapperType)) {
34 m_wrappedSolution = soln->getExternalHandle(wrapperType);
35 } else {
36 m_wrappedSolution = ExtensionManager::wrapSolution(wrapperType, soln);
37 }
38 }
39 double needsUpdate = m_update(m_wrappedSolution->get());
40 return needsUpdate != 0.0;
41}
42
43ReactionRateDelegator::ReactionRateDelegator()
44{
45 install("evalFromStruct", m_evalFromStruct,
46 [](void*) {
47 throw NotImplementedError("ReactionRateDelegator::evalFromStruct");
48 return 0.0; // necessary to set lambda's function signature
49 }
50 );
51 install("setParameters", m_setParameters,
52 [this](const AnyMap& node, const UnitStack& units) {
53 ReactionRate::setParameters(node, units); });
54 install("getParameters", m_getParameters,
55 [this](AnyMap& node) { ReactionRate::getParameters(node); });
56 install("validate", m_validate,
57 [](const string& equation, void* soln) {
58 throw NotImplementedError("ReactionRateDelegator::validate"); });
59}
60
61unique_ptr<MultiRateBase> ReactionRateDelegator::newMultiRate() const
62{
63 auto multirate = make_unique<MultiRate<ReactionRateDelegator,
64 ReactionDataDelegator>>();
65 multirate->sharedData().setType(m_rateType);
66 ExtensionManager::wrapReactionData(m_rateType, multirate->sharedData());
67 return multirate;
68}
69
70 void ReactionRateDelegator::validate(const string& equation, const Kinetics& kin)
71{
72 auto soln = kin.root();
73 if (!soln) {
74 throw CanteraError("ReactionRateDelegator::validate",
75 "Phase must be instantiated as a Solution to use extensible "
76 "reactions of type '{}'", m_rateType);
77 }
78 auto wrapperType = ExtensionManager::getSolutionWrapperType(m_rateType);
79 auto wrappedSoln = soln->getExternalHandle(wrapperType);
80 if (!wrappedSoln) {
81 wrappedSoln = ExtensionManager::wrapSolution(wrapperType, soln);
82 }
83
84 try {
85 m_validate(equation, wrappedSoln->get());
86 } catch (CanteraError& err) {
87 throw InputFileError("'" + m_rateType + "' validate", m_input,
88 err.getMessage());
89 }
90}
91
92}
Kinetics.h
Base class for kinetics managers and also contains the kineticsmgr module documentation (see Kinetics...
ReactionRateDelegator.h
Solution.h
Cantera::CanteraError
Base class for exceptions thrown by Cantera classes.
Definition ctexceptions.h:66
Cantera::CanteraError::getMessage
virtual string getMessage() const
Method overridden by derived classes to format the error message.
Definition ctexceptions.cpp:45
Cantera::Delegator::install
void install(const string &name, function< void()> &target, const function< void()> &func)
Install a function with the signature void() as being delegatable.
Definition Delegator.h:301
Cantera::ExtensionManager::wrapReactionData
static void wrapReactionData(const string &rateName, ReactionDataDelegator &data)
Create an object in an external language that wraps the specified ReactionData object.
Definition ExtensionManager.cpp:16
Cantera::ExtensionManager::getSolutionWrapperType
static string getSolutionWrapperType(const string &userType)
Get the Solution wrapper type corresponding to the specified user-defined reaction rate type.
Definition ExtensionManager.cpp:53
Cantera::ExtensionManager::wrapSolution
static shared_ptr< ExternalHandle > wrapSolution(const string &wrapperType, shared_ptr< Solution > soln)
Create an object in an external language that wraps the specified Solution object.
Definition ExtensionManager.cpp:35
Cantera::InputFileError
Error thrown for problems processing information contained in an AnyMap or AnyValue.
Definition AnyMap.h:738
Cantera::Kinetics
Public interface for kinetics managers.
Definition Kinetics.h:126
Cantera::Kinetics::root
shared_ptr< Solution > root() const
Get the Solution object containing this Kinetics object and associated ThermoPhase objects.
Definition Kinetics.h:1485
Cantera::MultiRate
A class template handling ReactionRate specializations.
Definition MultiRate.h:22
Cantera::NotImplementedError
An error indicating that an unimplemented function has been called.
Definition ctexceptions.h:195
Cantera::ReactionDataDelegator
Delegate methods of the ReactionData class to external functions.
Definition ReactionRateDelegator.h:20
Cantera::ReactionDataDelegator::m_wrappedSolution
shared_ptr< ExternalHandle > m_wrappedSolution
An external language's wrapper for the Solution object where this ReactionData object is being used.
Definition ReactionRateDelegator.h:55
Cantera::ReactionDataDelegator::update
bool update(const ThermoPhase &phase, const Kinetics &kin) override
Update data container based on thermodynamic phase state.
Definition ReactionRateDelegator.cpp:23
Cantera::ReactionDataDelegator::m_rateType
string m_rateType
The reaction rate type.
Definition ReactionRateDelegator.h:51
Cantera::ReactionDataDelegator::m_update
function< double(void *)> m_update
Delegated update method taking the Solution wrapper as its argument.
Definition ReactionRateDelegator.h:61
Cantera::ReactionDataDelegator::setType
void setType(const string &name)
Set the type of the ReactionData class.
Definition ReactionRateDelegator.h:35
Cantera::ReactionRateDelegator
Delegate methods of the ReactionRate class to external functions.
Definition ReactionRateDelegator.h:69
Cantera::ReactionRateDelegator::newMultiRate
unique_ptr< MultiRateBase > newMultiRate() const override
Create a rate evaluator for reactions of a particular derived type.
Definition ReactionRateDelegator.cpp:61
Cantera::ReactionRateDelegator::validate
void validate(const string &equation, const Kinetics &kin) override
Validate the reaction rate expression.
Definition ReactionRateDelegator.cpp:70
Cantera::ReactionRateDelegator::m_rateType
string m_rateType
The name of the reaction rate type.
Definition ReactionRateDelegator.h:105
Cantera::ReactionRateDelegator::m_evalFromStruct
function< double(void *)> m_evalFromStruct
Delegated evalFromStruct method taking a pointer to the corresponding ReactionData wrapper object.
Definition ReactionRateDelegator.h:109
Cantera::ReactionRate::setParameters
virtual void setParameters(const AnyMap &node, const UnitStack &units)
Set parameters.
Definition ReactionRate.h:101
Cantera::ReactionRate::m_input
AnyMap m_input
Input data used for specific models.
Definition ReactionRate.h:247
Cantera::ReactionRate::getParameters
virtual void getParameters(AnyMap &node) const
Get parameters.
Definition ReactionRate.h:241
Cantera::ThermoPhase
Base class for a phase with thermodynamic properties.
Definition ThermoPhase.h:390
Cantera
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564