Skip to content

Commit

Permalink
Change return type of newReactor with contents
Browse files Browse the repository at this point in the history
The newReactor function with contents was only introduced in 3.1 and
thus can be modified without deprecations.
  • Loading branch information
ischoegl committed Aug 23, 2024
1 parent 8cca5e4 commit 2334ad8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 25 deletions.
21 changes: 14 additions & 7 deletions include/cantera/zeroD/ReactorFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,30 @@ class ReactorFactory : public Factory<ReactorNode, shared_ptr<Solution>, const s
//! @ingroup zerodGroup
//! @{

//! Create a ReactorNode object of the specified type
//! Create a %ReactorNode object of the specified type.
//! @param model String representing type of reactor node.
//! @param contents Solution object holding thermo/kinetics.
//! @param name Name of reactor.
//! @since New in %Cantera 3.1.
shared_ptr<ReactorNode> newReactorNode(
const string& model, shared_ptr<Solution> contents=nullptr,
shared_ptr<ReactorNode> newReactor(
const string& model, shared_ptr<Solution> contents,
const string& name="(none)");

//! Create a Reactor object of the specified type
//! Create an empty ReactorNode object of the specified type.
//! @since Transitional method; new in %Cantera 3.1.
//! @deprecated Empty reactors will no longer be supported after %Cantera 3.1.
//! Use newReactor() with contents instead.
shared_ptr<ReactorNode> newReactor4(const string& model);

//! Create an empty ReactorBase object of the specified type.
//! @since Starting in %Cantera 3.1, this method returns a `shared_ptr<ReactorBase>`
//! @deprecated Transitional method. Use newReactor() with contents instead.
//! @deprecated Empty reactors will no longer be supported after %Cantera 3.1.
//! Use newReactor() with contents instead.
shared_ptr<ReactorBase> newReactor(const string& model);

//! Create a Reactor object of the specified type
//! Create a Reactor object of the specified type.
//! @since New in %Cantera 3.0.
//! @deprecated Transitional method. Use newReactor() instead.
//! @deprecated Transitional method. Use non-empty newReactor() instead.
shared_ptr<ReactorBase> newReactor3(const string& model);

//! @}
Expand Down
4 changes: 2 additions & 2 deletions interfaces/cython/cantera/reactor.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
cdef cppclass CxxFlowDevice "Cantera::FlowDevice"

# factories
cdef shared_ptr[CxxReactorNode] newReactorNode(string) except +translate_exception
cdef shared_ptr[CxxReactorNode] newReactorNode(string, shared_ptr[CxxSolution], string) except +translate_exception
cdef shared_ptr[CxxReactorNode] newReactor4(string) except +translate_exception
cdef shared_ptr[CxxReactorNode] newReactor(string, shared_ptr[CxxSolution], string) except +translate_exception
cdef shared_ptr[CxxConnector] newConnector(string, shared_ptr[CxxReactorNode], shared_ptr[CxxReactorNode], string) except +translate_exception

# reactors
Expand Down
6 changes: 3 additions & 3 deletions interfaces/cython/cantera/reactor.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ cdef class ReactorNode:

def __cinit__(self, _SolutionBase contents=None, *args, name="(none)", **kwargs):
if isinstance(contents, _SolutionBase):
self._node = newReactorNode(stringify(self.reactor_type),
contents._base, stringify(name))
self._node = newReactor(stringify(self.reactor_type),
contents._base, stringify(name))
else:
# deprecated: will raise warnings in C++ layer
self._node = newReactorNode(stringify(self.reactor_type))
self._node = newReactor4(stringify(self.reactor_type))
self._node.get().setName(stringify(name))
self.node = self._node.get()

Expand Down
2 changes: 1 addition & 1 deletion src/clib/ctreactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" {
{
try {
return ReactorCabinet::add(
newReactorNode(type, SolutionCabinet::at(n), name));
newReactor(type, SolutionCabinet::at(n), name));
} catch (...) {
return handleAllExceptions(-1, ERR);
}
Expand Down
12 changes: 10 additions & 2 deletions src/zeroD/ReactorFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,27 @@ void ReactorFactory::deleteFactory() {
s_factory = 0;
}

shared_ptr<ReactorNode> newReactorNode(
shared_ptr<ReactorNode> newReactor(
const string& model, shared_ptr<Solution> contents, const string& name)
{
return shared_ptr<ReactorNode>(
ReactorFactory::factory()->create(model, contents, name));
}

shared_ptr<ReactorNode> newReactor4(const string& model)
{
warn_deprecated("newReactor4",
"Replaced by newReactor with contents; to be removed after Cantera 3.1.");
return shared_ptr<ReactorNode>(
ReactorFactory::factory()->create(model, nullptr, ""));
}

shared_ptr<ReactorBase> newReactor(const string& model)
{
warn_deprecated("newReactor",
"Replaced by newReactor with contents; to be removed after Cantera 3.1.");
auto reactor = std::dynamic_pointer_cast<ReactorBase>(
newReactorNode(model, nullptr, ""));
newReactor(model, nullptr, ""));
if (!reactor) {
throw CanteraError("newReactor",
"Detected incompatible ReactorBase type '{}'", model);
Expand Down
20 changes: 10 additions & 10 deletions test/zeroD/test_zeroD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TEST(zerodim, simple)

auto sol = newSolution("gri30.yaml", "gri30", "none");
sol->thermo()->setState_TPX(T, P, X);
auto cppNode = newReactorNode("IdealGasReactor", sol, "simple");
auto cppNode = newReactor("IdealGasReactor", sol, "simple");
ASSERT_EQ(cppNode->name(), "simple");
auto reactor = std::dynamic_pointer_cast<ReactorBase>(cppNode);
reactor->initialize();
Expand Down Expand Up @@ -62,11 +62,11 @@ TEST(zerodim, surface)
auto gas = newSolution("ptcombust.yaml", "gas");
auto surf = newInterface("ptcombust.yaml", "Pt_surf", {gas});

auto node0 = newReactorNode("IdealGasReactor", gas, "bulk");
auto node0 = newReactor("IdealGasReactor", gas, "bulk");
auto reactor = std::dynamic_pointer_cast<ReactorBase>(node0);
ASSERT_EQ(reactor->name(), "bulk");

auto node1 = newReactorNode("ReactorSurface", surf, "surface");
auto node1 = newReactor("ReactorSurface", surf, "surface");
auto cppSurface = std::dynamic_pointer_cast<ReactorSurface>(node1);
ASSERT_EQ(cppSurface->name(), "surface");
}
Expand All @@ -75,8 +75,8 @@ TEST(zerodim, flowdevice)
{
auto gas = newSolution("gri30.yaml", "gri30", "none");

auto node0 = newReactorNode("IdealGasReactor", gas, "upstream");
auto node1 = newReactorNode("IdealGasReactor", gas, "downstream");
auto node0 = newReactor("IdealGasReactor", gas, "upstream");
auto node1 = newReactor("IdealGasReactor", gas, "downstream");

auto edge = newConnector("Valve", node0, node1, "valve");
ASSERT_EQ(edge->name(), "valve");
Expand All @@ -95,8 +95,8 @@ TEST(zerodim, wall)
{
auto gas = newSolution("gri30.yaml", "gri30", "none");

auto node0 = newReactorNode("IdealGasReactor", gas, "left");
auto node1 = newReactorNode("IdealGasReactor", gas, "right");
auto node0 = newReactor("IdealGasReactor", gas, "left");
auto node1 = newReactor("IdealGasReactor", gas, "right");

auto edge = newConnector("Wall", node0, node1, "wall");
ASSERT_EQ(edge->name(), "wall");
Expand All @@ -112,12 +112,12 @@ TEST(zerodim, wall)
TEST(zerodim, empty)
{
// Deprecated/transitional modes. Remove after Cantera 3.1.
EXPECT_THROW(newReactorNode("IdealGasReactor", nullptr, "igr"), CanteraError);
EXPECT_THROW(newReactor("IdealGasReactor", nullptr, "igr"), CanteraError);
EXPECT_THROW(newConnector("Valve", nullptr, nullptr, "valve"), CanteraError);

suppress_deprecation_warnings();
auto node0 = newReactorNode("IdealGasReactor", nullptr, "r0");
auto node1 = newReactorNode("IdealGasReactor", nullptr, "r1");
auto node0 = newReactor("IdealGasReactor", nullptr, "r0");
auto node1 = newReactor("IdealGasReactor", nullptr, "r1");
make_deprecation_warnings_fatal();

auto gas = newSolution("gri30.yaml", "gri30", "none");
Expand Down

0 comments on commit 2334ad8

Please sign in to comment.