Skip to content

Update ReactorSurface / generalize ReactorBase #1864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions include/cantera/clib/ctreactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,9 @@ extern "C" {
CANTERA_CAPI int wall_setVelocity(int i, int n);
CANTERA_CAPI int wall_setEmissivity(int i, double epsilon);

CANTERA_CAPI int reactorsurface_new(const char* name);
CANTERA_CAPI int reactorsurface_del(int i);
CANTERA_CAPI int reactorsurface_name(int i, int len, char* nbuf);
CANTERA_CAPI int reactorsurface_setName(int i, const char* name);
CANTERA_CAPI int reactorsurface_install(int i, int n);
CANTERA_CAPI int reactorsurface_setkinetics(int i, int n);
CANTERA_CAPI double reactorsurface_area(int i);
CANTERA_CAPI int reactorsurface_setArea(int i, double v);
CANTERA_CAPI int reactorsurface_addSensitivityReaction(int i, int rxn);

CANTERA_CAPI int ct_clearReactors();

Expand Down
18 changes: 9 additions & 9 deletions include/cantera/zeroD/Reactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class Reactor : public ReactorBase
return true;
}

void setInitialVolume(double vol) override {
m_vol = vol;
}

void setChemistry(bool cflag=true) override {
m_chem = cflag;
}
Expand Down Expand Up @@ -145,13 +149,11 @@ class Reactor : public ReactorBase
//! Set the state of the reactor to correspond to the state vector *y*.
virtual void updateState(double* y);

//! Number of sensitivity parameters associated with this reactor
//! Number of sensitivity parameters associated with this reactor.
//! (including walls)
virtual size_t nSensParams() const;
size_t nSensParams() const override;

//! Add a sensitivity parameter associated with the reaction number *rxn*
//! (in the homogeneous phase).
virtual void addSensitivityReaction(size_t rxn);
void addSensitivityReaction(size_t rxn) override;

//! Add a sensitivity parameter associated with the enthalpy formation of
//! species *k* (in the homogeneous phase)
Expand Down Expand Up @@ -228,6 +230,8 @@ class Reactor : public ReactorBase
virtual bool preconditionerSupported() const {return false;};

protected:
//! @deprecated To be removed after %Cantera 3.2. Use constructor with
//! Solution object instead.
void setKinetics(Kinetics& kin) override;

//! Return the index in the solution vector for this reactor of the species
Expand Down Expand Up @@ -272,7 +276,6 @@ class Reactor : public ReactorBase

double m_Qdot = 0.0; //!< net heat transfer into the reactor, through walls [W]

double m_mass = 0.0; //!< total mass
vector<double> m_work;

//! Production rates of gas phase species on surfaces [kmol/s]
Expand All @@ -287,9 +290,6 @@ class Reactor : public ReactorBase

vector<double> m_advancelimits; //!< Advance step limit

// Data associated each sensitivity parameter
vector<SensitivityParameter> m_sensParams;

//! Vector of triplets representing the jacobian
vector<Eigen::Triplet<double>> m_jac_trips;
};
Expand Down
62 changes: 39 additions & 23 deletions include/cantera/zeroD/ReactorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
};

/**
* Base class for stirred reactors. Allows using any substance model, with
* arbitrary inflow, outflow, heat loss/gain, surface chemistry, and volume
* change.
* Base class for reactor objects. Allows using any substance model, with arbitrary
* inflow, outflow, heat loss/gain, surface chemistry, and volume change, whenever
* defined.
* @ingroup reactorGroup
*/
class ReactorBase
Expand Down Expand Up @@ -87,9 +87,10 @@
//! @name Methods to set up a simulation
//! @{

//! Set the initial reactor volume. By default, the volume is 1.0 m^3.
void setInitialVolume(double vol) {
m_vol = vol;
//! Set the initial reactor volume.
virtual void setInitialVolume(double vol) {

Check warning on line 91 in include/cantera/zeroD/ReactorBase.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorBase.h#L91

Added line #L91 was not covered by tests
throw NotImplementedError("ReactorBase::setInitialVolume",
"Volume is undefined for reactors of type '{}'.", type());

Check warning on line 93 in include/cantera/zeroD/ReactorBase.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorBase.h#L93

Added line #L93 was not covered by tests
}

//! Enable or disable changes in reactor composition due to chemical reactions.
Expand All @@ -103,26 +104,23 @@
}

//! Connect an inlet FlowDevice to this reactor
void addInlet(FlowDevice& inlet);
virtual void addInlet(FlowDevice& inlet);

//! Connect an outlet FlowDevice to this reactor
void addOutlet(FlowDevice& outlet);
virtual void addOutlet(FlowDevice& outlet);

//! Return a reference to the *n*-th inlet FlowDevice connected to this
//! reactor.
//! Return a reference to the *n*-th inlet FlowDevice connected to this reactor.
FlowDevice& inlet(size_t n = 0);

//! Return a reference to the *n*-th outlet FlowDevice connected to this
//! reactor.
//! Return a reference to the *n*-th outlet FlowDevice connected to this reactor.
FlowDevice& outlet(size_t n = 0);

//! Return the number of inlet FlowDevice objects connected to this reactor.
size_t nInlets() {
return m_inlet.size();
}

//! Return the number of outlet FlowDevice objects connected to this
//! reactor.
//! Return the number of outlet FlowDevice objects connected to this reactor.
size_t nOutlets() {
return m_outlet.size();
}
Expand All @@ -138,15 +136,14 @@
* this reactor is to the right of the wall. This method is called
* automatically for both the left and right reactors by WallBase::install.
*/
void addWall(WallBase& w, int lr);
virtual void addWall(WallBase& w, int lr);

//! Return a reference to the *n*-th Wall connected to this reactor.
WallBase& wall(size_t n);

virtual void addSurface(ReactorSurface* surf);

//! Return a reference to the *n*-th ReactorSurface connected to this
//! reactor
//! Return a reference to the *n*-th ReactorSurface connected to this reactor.
ReactorSurface* surface(size_t n);

//! Return the number of surfaces in a reactor
Expand All @@ -167,10 +164,13 @@
//! reactor's current state.
void restoreState();

//! Set the state of the reactor to correspond to the state of the
//! associated ThermoPhase object. This is the inverse of restoreState().
//! Calling this will trigger integrator reinitialization.
virtual void syncState();
//! Set the state of the reactor to the associated ThermoPhase object.
//! This method is the inverse of restoreState() and will trigger integrator
//! reinitialization.
//! The method needs to be implemented by a ReactorBase specialization.
virtual void syncState() {
throw NotImplementedError("ReactorBase::syncState");

Check warning on line 172 in include/cantera/zeroD/ReactorBase.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorBase.h#L171-L172

Added lines #L171 - L172 were not covered by tests
}

//! return a reference to the contents.
ThermoPhase& contents() {
Expand Down Expand Up @@ -239,7 +239,7 @@

//! Returns the mass (kg) of the reactor's contents.
double mass() const {
return m_vol * density();
return m_mass;
}

//! Return the vector of species mass fractions.
Expand Down Expand Up @@ -268,6 +268,16 @@
//! Set the ReactorNet that this reactor belongs to.
void setNetwork(ReactorNet* net);

//! Add a sensitivity parameter associated with the reaction number *rxn*
virtual void addSensitivityReaction(size_t rxn) {
throw NotImplementedError("ReactorBase::addSensitivityReaction");

Check warning on line 273 in include/cantera/zeroD/ReactorBase.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorBase.h#L272-L273

Added lines #L272 - L273 were not covered by tests
}

//! Number of sensitivity parameters associated with this reactor.
virtual size_t nSensParams() const {
return m_sensParams.size();

Check warning on line 278 in include/cantera/zeroD/ReactorBase.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorBase.h#L277-L278

Added lines #L277 - L278 were not covered by tests
}

protected:
//! Specify the mixture contained in the reactor. Note that a pointer to
//! this substance is stored, and as the integration proceeds, the state of
Expand All @@ -277,6 +287,8 @@

//! Specify the kinetics manager for the reactor. Called by setSolution().
//! @since New in %Cantera 3.1.
//! @deprecated To be removed after %Cantera 3.2. Superseded by instantiation of
//! ReactorBase with Solution object.
virtual void setKinetics(Kinetics& kin) {
throw NotImplementedError("ReactorBase::setKinetics");
}
Expand All @@ -285,7 +297,8 @@
size_t m_nsp = 0;

ThermoPhase* m_thermo = nullptr;
double m_vol = 1.0; //!< Current volume of the reactor [m^3]
double m_vol = 0.0; //!< Current volume of the reactor [m^3]
double m_mass = 0.0; //!< Current mass of the reactor [kg]
double m_enthalpy = 0.0; //!< Current specific enthalpy of the reactor [J/kg]
double m_intEnergy = 0.0; //!< Current internal energy of the reactor [J/kg]
double m_pressure = 0.0; //!< Current pressure in the reactor [Pa]
Expand All @@ -306,6 +319,9 @@

//! Composite thermo/kinetics/transport handler
shared_ptr<Solution> m_solution;

// Data associated each sensitivity parameter
vector<SensitivityParameter> m_sensParams;
};
}

Expand Down
73 changes: 38 additions & 35 deletions include/cantera/zeroD/ReactorSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,22 @@
namespace Cantera
{

class Kinetics;
class SurfPhase;

//! A surface where reactions can occur that is in contact with the bulk fluid of a
//! Reactor.
class ReactorSurface
//! @ingroup reactorGroup
class ReactorSurface : public ReactorBase
{
public:
ReactorSurface(const string& name="(none)") : m_name(name) {}
virtual ~ReactorSurface() = default;
ReactorSurface(const ReactorSurface&) = delete;
ReactorSurface& operator=(const ReactorSurface&) = delete;
ReactorSurface(shared_ptr<Solution> sol, const string& name="(none)");
using ReactorBase::ReactorBase; // inherit constructors

//! String indicating the wall model implemented.
virtual string type() const {
string type() const override {
return "ReactorSurface";
}

//! Retrieve reactor surface name.
string name() const {
return m_name;
}

//! Set reactor surface name.
void setName(const string& name) {
m_name = name;
}

//! Set the default name of a wall. Returns `false` if it was previously set.
bool setDefaultName(map<string, int>& counts);

//! Returns the surface area [m^2]
double area() const;

Expand All @@ -50,7 +35,7 @@

//! Accessor for the SurfPhase object
SurfPhase* thermo() {
return m_thermo;
return m_surf;
}

//! Accessor for the InterfaceKinetics object
Expand All @@ -59,17 +44,31 @@
}

//! Set the InterfaceKinetics object for this surface
//! @deprecated To be removed after %Cantera 3.2. Use constructor with
//! Solution object instead.
void setKinetics(Kinetics* kin);

//! Set the reactor that this Surface interacts with
void setReactor(ReactorBase* reactor);
void addInlet(FlowDevice& inlet) override {

Check warning on line 51 in include/cantera/zeroD/ReactorSurface.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorSurface.h#L51

Added line #L51 was not covered by tests
throw NotImplementedError("ReactorSurface::addInlet",
"Inlets are undefined for reactors of type '{}'.", type());

Check warning on line 53 in include/cantera/zeroD/ReactorSurface.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorSurface.h#L53

Added line #L53 was not covered by tests
}

//! Number of sensitivity parameters associated with reactions on this
//! surface
size_t nSensParams() const {
return m_params.size();
void addOutlet(FlowDevice& outlet) override {

Check warning on line 56 in include/cantera/zeroD/ReactorSurface.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorSurface.h#L56

Added line #L56 was not covered by tests
throw NotImplementedError("ReactorSurface::addOutlet",
"Outlets are undefined for reactors of type '{}'.", type());

Check warning on line 58 in include/cantera/zeroD/ReactorSurface.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorSurface.h#L58

Added line #L58 was not covered by tests
}

void addWall(WallBase& w, int lr) override {
throw NotImplementedError("ReactorSurface::addWall");

Check warning on line 62 in include/cantera/zeroD/ReactorSurface.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorSurface.h#L61-L62

Added lines #L61 - L62 were not covered by tests
}

void addSurface(ReactorSurface* surf) override {
throw NotImplementedError("ReactorSurface::addSurface");

Check warning on line 66 in include/cantera/zeroD/ReactorSurface.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorSurface.h#L65-L66

Added lines #L65 - L66 were not covered by tests
}

//! Set the reactor that this Surface interacts with
void setReactor(ReactorBase* reactor);

//! Set the surface coverages. Array `cov` has length equal to the number of
//! surface species.
void setCoverages(const double* cov);
Expand All @@ -87,11 +86,9 @@
//! Set the coverages and temperature in the surface phase object to the
//! values for this surface. The temperature is set to match the bulk phase
//! of the attached Reactor.
void syncState();
void syncState() override;

//! Enable calculation of sensitivities with respect to the rate constant
//! for reaction `i`.
void addSensitivityReaction(size_t i);
void addSensitivityReaction(size_t rxn) override;

//! Set reaction rate multipliers. `params` is the global vector of
//! sensitivity parameters. This function is called within
Expand All @@ -104,16 +101,22 @@
void resetSensitivityParameters();

protected:
string m_name; //!< Reactor surface name.
bool m_defaultNameSet = false; //!< `true` if default name has been previously set.
void setThermo(ThermoPhase& thermo) override {}

Check warning on line 104 in include/cantera/zeroD/ReactorSurface.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/ReactorSurface.h#L104

Added line #L104 was not covered by tests

//! Set the InterfaceKinetics object for this surface.
//! Method is needed to prevent compiler warnings by disambiguating from the
//! non-protected variant.
//! @since New in %Cantera 3.2.
//! @deprecated To be removed after %Cantera 3.2. Use constructor with
//! Solution object instead.
void setKinetics(Kinetics& kin) override;

double m_area = 1.0;

SurfPhase* m_thermo = nullptr;
SurfPhase* m_surf = nullptr;
Kinetics* m_kinetics = nullptr;
ReactorBase* m_reactor = nullptr;
vector<double> m_cov;
vector<SensitivityParameter> m_params;
};

}
Expand Down
2 changes: 2 additions & 0 deletions include/cantera/zeroD/Reservoir.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
}

void initialize(double t0=0.0) override {}

void syncState() override {}

Check warning on line 29 in include/cantera/zeroD/Reservoir.h

View check run for this annotation

Codecov / codecov/patch

include/cantera/zeroD/Reservoir.h#L29

Added line #L29 was not covered by tests
};

}
Expand Down
Loading
Loading