From 4acb3dc1a14af3843d3abb171ca2a1643da195a5 Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Thu, 12 Dec 2019 08:44:53 -0600 Subject: [PATCH] [Input] finalize transitional handling of gri30 phases --- interfaces/cython/cantera/base.pyx | 41 ++++++++++++------- .../examples/onedim/diffusion_flame.py | 2 +- .../cantera/examples/onedim/flame_fixed_T.py | 2 +- .../examples/onedim/flamespeed_sensitivity.py | 2 +- .../cython/cantera/test/test_convert.py | 33 ++++----------- .../matlab/toolbox/@Solution/Solution.m | 3 +- src/thermo/ThermoFactory.cpp | 41 +++++++++++++------ test/equil/equil_gas.cpp | 2 +- test/matlab/TestImport.m | 8 +++- 9 files changed, 75 insertions(+), 59 deletions(-) diff --git a/interfaces/cython/cantera/base.pyx b/interfaces/cython/cantera/base.pyx index faa3626f7bf..71e7ec79328 100644 --- a/interfaces/cython/cantera/base.pyx +++ b/interfaces/cython/cantera/base.pyx @@ -109,21 +109,32 @@ cdef class _SolutionBase: elif source: root = AnyMapFromYamlString(stringify(source)) - if name == 'gri30_mix' or name == 'gri30_multi': - # catch transitional behavior - warnings.warn( - "The phases 'gri30_mix' and 'gri30_multi' are no longer " - "defined in the input file 'gri30.yaml'. The YAML version uses " - "the 'Mix' transport manager by default instead, which can be " - "overridden by specifying a transport manager via the " - "`transport_model' keyword argument during initialization. " - "Support for transitional behavior will be removed after " - "Cantera 2.5.", DeprecationWarning) - phaseNode = root[stringify("phases")].getMapWhere(stringify("name"), - stringify("gri30")) - else: - phaseNode = root[stringify("phases")].getMapWhere(stringify("name"), - stringify(name)) + try: + phaseNode = root[stringify("phases")].getMapWhere( + stringify("name"), stringify(name)) + except CanteraError: + if name in ['gri30_mix', 'gri30_multi']: + # catch transitional behavior + warnings.warn( + "The phases named 'gri30_mix' and 'gri30_multi' are no " + "longer defined in the input file 'gri30.yaml'. By " + "default, 'gri30.yaml' uses the mixture-averaged transport " + "model. The following options are available:\n" + " * ct.Solution('gri30.yaml') " + " # Mixture-averaged transport\n" + " * ct.Solution('gri30.yaml', transport_model='mix') " + " # Mixture-averaged transport\n" + " * ct.Solution('gri30.yaml', transport_model=None) " + " # No transport model, useful for 0-D simulations\n" + " * ct.Solution('gri30.yaml', " + "transport_model='multicomponent') " + " # Multicomponent transport\n" + "Support for transitional behavior will be removed after " + "Cantera 2.5.", DeprecationWarning) + phaseNode = root[stringify("phases")].getMapWhere( + stringify("name"), stringify("gri30")) + else: + raise # Thermo if isinstance(self, ThermoPhase): diff --git a/interfaces/cython/cantera/examples/onedim/diffusion_flame.py b/interfaces/cython/cantera/examples/onedim/diffusion_flame.py index dcacee62e95..c368d9c69b1 100644 --- a/interfaces/cython/cantera/examples/onedim/diffusion_flame.py +++ b/interfaces/cython/cantera/examples/onedim/diffusion_flame.py @@ -22,7 +22,7 @@ # Create the gas object used to evaluate all thermodynamic, kinetic, and # transport properties. -gas = ct.Solution('gri30.yaml', 'gri30') +gas = ct.Solution('gri30.yaml') gas.TP = gas.T, p # Create an object representing the counterflow flame configuration, diff --git a/interfaces/cython/cantera/examples/onedim/flame_fixed_T.py b/interfaces/cython/cantera/examples/onedim/flame_fixed_T.py index 9cc3b235ddb..178dbd2ac60 100644 --- a/interfaces/cython/cantera/examples/onedim/flame_fixed_T.py +++ b/interfaces/cython/cantera/examples/onedim/flame_fixed_T.py @@ -25,7 +25,7 @@ # transport properties. It is created with two transport managers, to enable # switching from mixture-averaged to multicomponent transport on the last # solution. -gas = ct.Solution('gri30.yaml', 'gri30') +gas = ct.Solution('gri30.yaml') # set its state to that of the unburned gas at the burner gas.TPX = tburner, p, comp diff --git a/interfaces/cython/cantera/examples/onedim/flamespeed_sensitivity.py b/interfaces/cython/cantera/examples/onedim/flamespeed_sensitivity.py index 191190bb717..08fe151deba 100644 --- a/interfaces/cython/cantera/examples/onedim/flamespeed_sensitivity.py +++ b/interfaces/cython/cantera/examples/onedim/flamespeed_sensitivity.py @@ -15,7 +15,7 @@ width = 0.03 # m # Solution object used to compute mixture properties -gas = ct.Solution('gri30.yaml', 'gri30') +gas = ct.Solution('gri30.yaml') gas.TPX = Tin, p, reactants # Flame object diff --git a/interfaces/cython/cantera/test/test_convert.py b/interfaces/cython/cantera/test/test_convert.py index 07967935c4b..b65cbc3a071 100644 --- a/interfaces/cython/cantera/test/test_convert.py +++ b/interfaces/cython/cantera/test/test_convert.py @@ -8,8 +8,6 @@ from cantera import ck2cti, ck2yaml, cti2yaml, ctml2yaml -import warnings - class converterTestCommon: def convert(self, inputFile, thermo=None, transport=None, surface=None, output=None, quiet=True, permissive=None): @@ -85,29 +83,14 @@ def test_gri30(self): self.checkKinetics(ref, gas, [300, 1500], [5e3, 1e5, 2e6]) def test_gri30_phases(self): - - # cause all warnings to always be triggered. - warnings.simplefilter("always") - - with warnings.catch_warnings(record=True) as w: - - gas = ct.Solution('gri30.yaml', 'gri30_mix') - - self.assertEqual(gas.transport_model, 'Mix') - self.assertEqual(len(w), 1) - self.assertTrue(issubclass(w[-1].category, DeprecationWarning)) - self.assertIn("be removed after Cantera 2.5.", - str(w[-1].message)) - - with warnings.catch_warnings(record=True) as w: - - gas = ct.Solution('gri30.yaml', 'gri30_multi') - - self.assertEqual(gas.transport_model, 'Multi') - self.assertEqual(len(w), 1) - self.assertTrue(issubclass(w[-1].category, DeprecationWarning)) - self.assertIn("be removed after Cantera 2.5.", - str(w[-1].message)) + yml_file = pjoin(self.cantera_data, "gri30.yaml") + with self.assertWarnsRegex(DeprecationWarning, + 'be removed after Cantera 2.5.'): + gas = ct.Solution(yml_file, 'gri30_mix') + + with self.assertWarnsRegex(DeprecationWarning, + 'be removed after Cantera 2.5.'): + gas = ct.Solution(yml_file, 'gri30_multi') def test_soot(self): self.convert('soot.inp', thermo='soot-therm.dat', output='soot_test') diff --git a/interfaces/matlab/toolbox/@Solution/Solution.m b/interfaces/matlab/toolbox/@Solution/Solution.m index 29af2ae5994..e285c790ad4 100644 --- a/interfaces/matlab/toolbox/@Solution/Solution.m +++ b/interfaces/matlab/toolbox/@Solution/Solution.m @@ -52,7 +52,8 @@ s.kin = k; s.th = t; if nargin == 3 - if strcmp(trans, 'default') || strcmp(trans, 'Mix') || strcmp(trans, 'Multi') + if strcmp(trans, 'default') || strcmp(trans, 'Mix') || ... + strcmp(trans, 'Multi') || strcmp(trans, 'None') tr = Transport(t, trans, 0); else error('Unknown transport modeling specified.') diff --git a/src/thermo/ThermoFactory.cpp b/src/thermo/ThermoFactory.cpp index ed72b53695d..b88e8b18620 100644 --- a/src/thermo/ThermoFactory.cpp +++ b/src/thermo/ThermoFactory.cpp @@ -130,20 +130,35 @@ ThermoPhase* newPhase(const std::string& infile, std::string id) if (extension == "yml" || extension == "yaml") { AnyMap root = AnyMap::fromYamlFile(infile); std::string name = id; - if (name=="gri30_mix" || name=="gri30_multi") { - name = "gri30"; - warn_deprecated("newPhase", - "The phases 'gri30_mix' and 'gri30_multi' are no longer " - "defined in the input file 'gri30.yaml'. The YAML version uses " - "the 'Mix' transport manager by default instead, which can be " - "overridden by specifying a transport manager during " - "initialization. Support for transitional behavior will be " - "removed after Cantera 2.5."); + try { + AnyMap& phase = root["phases"].getMapWhere("name", name); + unique_ptr t(newThermoPhase(phase["thermo"].asString())); + setupPhase(*t, phase, root); + return t.release(); + } catch (CanteraError& err) { + if (name=="gri30_mix" || name=="gri30_multi") { + warn_deprecated("newPhase", + "\nThe phases named 'gri30_mix' and 'gri30_multi' are no " + "longer defined in the input file 'gri30.yaml'. By default, " + "'gri30.yaml' uses the mixture-averaged transport model. The " + "following options are available:\n" + " * 'Mix': Mixture-averaged transport\n" + " * 'Multi': Multicomponent transport\n" + " * 'None': No transport model, useful for 0-D simulations\n" + "Examples for setting of transport model:\n" + " - MATLAB: Solution('gri30.yaml', 'gri30', 'None')\n" + " - C++: newSolution(\"gri30.yaml\", \"gri30\", \"Mix\")\n" + "Support for transitional behavior will be removed after " + "Cantera 2.5."); + AnyMap& phase = root["phases"].getMapWhere("name", "gri30"); + unique_ptr t(newThermoPhase(phase["thermo"].asString())); + setupPhase(*t, phase, root); + t->setName(name); + return t.release(); + } else { + throw; + } } - AnyMap& phase = root["phases"].getMapWhere("name", name); - unique_ptr t(newThermoPhase(phase["thermo"].asString())); - setupPhase(*t, phase, root); - return t.release(); } else { XML_Node* root = get_XML_File(infile); XML_Node* xphase = get_XML_NameID("phase", "#"+id, root); diff --git a/test/equil/equil_gas.cpp b/test/equil/equil_gas.cpp index 9fb9c61ddac..96b8d859c57 100644 --- a/test/equil/equil_gas.cpp +++ b/test/equil/equil_gas.cpp @@ -103,7 +103,7 @@ TEST_F(OverconstrainedEquil, DISABLED_BasisOptimize2) class GriEquilibriumTest : public testing::Test { public: - GriEquilibriumTest() : gas("gri30.yaml", "gri30") { + GriEquilibriumTest() : gas("gri30.yaml") { X.resize(gas.nSpecies()); Yelem.resize(gas.nElements()); }; diff --git a/test/matlab/TestImport.m b/test/matlab/TestImport.m index 351bccf2ff6..491667d0c56 100644 --- a/test/matlab/TestImport.m +++ b/test/matlab/TestImport.m @@ -5,7 +5,13 @@ end function testImportYAML(self) - gas = Solution('gri30.yaml', 'gri30', 'Mix'); + gas = Solution('gri30.yaml'); + dkm = mixDiffCoeffs(gas); + assertEqual(length(dkm), nSpecies(gas)) + end + + function testImportXML(self) + gas = Solution('gri30.xml', 'gri30_mix'); dkm = mixDiffCoeffs(gas); assertEqual(length(dkm), nSpecies(gas)) end