[0D] Move deprecation warning for empty reactors

This commit is contained in:
Ingmar Schoegl
2024-03-30 12:01:37 -05:00
committed by Ray Speth
parent 8a789676cf
commit b80afd1cc2
5 changed files with 29 additions and 24 deletions

View File

@@ -31,6 +31,7 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera":
# factories
cdef shared_ptr[CxxReactorBase] newReactor(string) except +translate_exception
cdef shared_ptr[CxxReactorBase] newReactor(string, shared_ptr[CxxSolution], string) except +translate_exception
cdef shared_ptr[CxxFlowDevice] newFlowDevice(string) except +translate_exception
cdef shared_ptr[CxxWallBase] newWall(string) except +translate_exception

View File

@@ -23,8 +23,20 @@ cdef class ReactorBase:
Common base class for reactors and reservoirs.
"""
reactor_type = "none"
def __cinit__(self, *args, **kwargs):
self._reactor = newReactor(stringify(self.reactor_type))
def __cinit__(self, _SolutionBase contents=None, name=None, *, **kwargs):
def reactor_name(name):
if name is not None:
return name
_reactor_counts[self.reactor_type] += 1
return f"{self.reactor_type}_{_reactor_counts[self.reactor_type]}"
if isinstance(contents, _SolutionBase):
self._reactor = newReactor(stringify(self.reactor_type),
contents._base, stringify(reactor_name(name)))
else:
# deprecated: will raise warnings in C++ layer
self._reactor = newReactor(stringify(self.reactor_type))
self._reactor.get().setName(stringify(reactor_name(name)))
self.rbase = self._reactor.get()
def __init__(self, _SolutionBase contents=None, name=None, *, volume=None,
@@ -36,18 +48,7 @@ cdef class ReactorBase:
self._walls = []
self._surfaces = []
if isinstance(contents, _SolutionBase):
self.insert(contents)
else:
warnings.warn(
"Starting in Cantera 3.1, the reactor contents must not be empty.",
DeprecationWarning)
if name is not None:
self.name = name
else:
_reactor_counts[self.reactor_type] += 1
n = _reactor_counts[self.reactor_type]
self.name = '{0}_{1}'.format(self.reactor_type, n)
self.insert(contents) # leave insert for the time being
if volume is not None:
self.volume = volume

View File

@@ -68,7 +68,9 @@ void ReactorFactory::deleteFactory() {
shared_ptr<ReactorBase> newReactor(const string& model)
{
// deprecation warning is handled in Python API
warn_deprecated("newReactor",
"Creation of empty reactor objects is deprecated in Cantera 3.1 and will be \n"
"removed thereafter; reactor contents should be provided in the constructor.");
return shared_ptr<ReactorBase>(ReactorFactory::factory()->create(model));
}

View File

@@ -20,9 +20,9 @@ TEST(ctreactor, reactor_objects)
int thermo = thermo_newFromFile("gri30.yaml", "gri30");
int kin = kin_newFromFile("gri30.yaml", "", thermo, -1, -1, -1, -1);
suppress_deprecation_warnings();
int reactor = reactor_new("IdealGasReactor");
ASSERT_GE(reactor, 0);
suppress_deprecation_warnings();
int ret = reactor_setThermoMgr(reactor, thermo);
ASSERT_EQ(ret, 0);
ret = reactor_setKineticsMgr(reactor, kin);
@@ -76,8 +76,8 @@ TEST(ctreactor, reactor_insert)
thermo_setTemperature(thermo, T);
thermo_setPressure(thermo, P);
int reactor = reactor_new("IdealGasReactor");
suppress_deprecation_warnings();
int reactor = reactor_new("IdealGasReactor");
int ret = reactor_insert(reactor, sol);
make_deprecation_warnings_fatal();
ASSERT_EQ(ret, 0);
@@ -109,8 +109,8 @@ TEST(ctreactor, reactor_from_parts)
thermo_setTemperature(thermo, T);
thermo_setPressure(thermo, P);
int reactor = reactor_new("IdealGasReactor");
suppress_deprecation_warnings();
int reactor = reactor_new("IdealGasReactor");
reactor_setThermoMgr(reactor, thermo);
reactor_setKineticsMgr(reactor, kin);
make_deprecation_warnings_fatal();

View File

@@ -4,7 +4,7 @@ import re
import numpy as np
import pytest
from pytest import approx
from .utilities import unittest
from .utilities import unittest, allow_deprecated
import cantera as ct
@@ -52,9 +52,9 @@ class TestReactor(utilities.CanteraTest):
self.net.verbose = True
self.assertTrue(self.net.verbose)
@pytest.mark.usefixtures("allow_deprecated")
def test_insert(self):
with pytest.warns(DeprecationWarning, match="must not be empty"):
R = self.reactorClass()
R = self.reactorClass() # warning raised from C++ code
with self.assertRaisesRegex(ct.CanteraError, 'No phase'):
R.T
with self.assertRaisesRegex(ct.CanteraError, 'No phase'):
@@ -692,9 +692,10 @@ class TestReactor(utilities.CanteraTest):
assert valve.pressure_function == approx(delta_p())
assert valve.mass_flow_rate == approx(mdot())
@pytest.mark.usefixtures("allow_deprecated")
def test_valve_errors(self):
self.make_reactors()
res = ct.Reservoir()
res = ct.Reservoir() # warning raised from C++ code
with self.assertRaisesRegex(ct.CanteraError, 'contents not defined'):
# Must assign contents of both reactors before creating Valve
@@ -857,8 +858,8 @@ class TestReactor(utilities.CanteraTest):
def test_bad_kwarg(self):
g = ct.Solution('h2o2.yaml', transport_model=None)
self.reactorClass(g, name='ok')
with self.assertRaises(TypeError):
r1 = self.reactorClass(foobar=3.14)
with self.assertRaises(ct.CanteraError):
self.reactorClass(foobar=3.14)
def test_preconditioner_unsupported(self):
self.make_reactors()