From b80afd1cc2c5752e165acb96d4625888f7dc62fc Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Sat, 30 Mar 2024 12:01:37 -0500 Subject: [PATCH] [0D] Move deprecation warning for empty reactors --- interfaces/cython/cantera/reactor.pxd | 1 + interfaces/cython/cantera/reactor.pyx | 29 ++++++++++++++------------- src/zeroD/ReactorFactory.cpp | 4 +++- test/clib/test_ctreactor.cpp | 6 +++--- test/python/test_reactor.py | 13 ++++++------ 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/interfaces/cython/cantera/reactor.pxd b/interfaces/cython/cantera/reactor.pxd index 6c2e479e1..8f24e70b7 100644 --- a/interfaces/cython/cantera/reactor.pxd +++ b/interfaces/cython/cantera/reactor.pxd @@ -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 diff --git a/interfaces/cython/cantera/reactor.pyx b/interfaces/cython/cantera/reactor.pyx index f2ec431e6..d025645de 100644 --- a/interfaces/cython/cantera/reactor.pyx +++ b/interfaces/cython/cantera/reactor.pyx @@ -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 diff --git a/src/zeroD/ReactorFactory.cpp b/src/zeroD/ReactorFactory.cpp index aab8e81e5..360f975bd 100644 --- a/src/zeroD/ReactorFactory.cpp +++ b/src/zeroD/ReactorFactory.cpp @@ -68,7 +68,9 @@ void ReactorFactory::deleteFactory() { shared_ptr 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(ReactorFactory::factory()->create(model)); } diff --git a/test/clib/test_ctreactor.cpp b/test/clib/test_ctreactor.cpp index 914c99210..242342d4d 100644 --- a/test/clib/test_ctreactor.cpp +++ b/test/clib/test_ctreactor.cpp @@ -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(); diff --git a/test/python/test_reactor.py b/test/python/test_reactor.py index 1e4da3e6b..818233b71 100644 --- a/test/python/test_reactor.py +++ b/test/python/test_reactor.py @@ -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()