From a20fd580000bf2e05724e93a6157c0264b232021 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 18 Jul 2013 00:04:13 +0000 Subject: [PATCH] [1D] Fix save/restore when mechanism size differs The failures were caused when attempting to restore the tolerance vectors, which have a value for each species. Since these tolerances are usually the same for all species, the last value in the array can be used to extend the array to the required length. Also add some tests for this feature. --- interfaces/cython/cantera/test/test_onedim.py | 61 +- src/oneD/Domain1D.cpp | 20 +- src/oneD/StFlow.cpp | 11 +- test/data/h2o2-plus.xml | 687 ++++++++++++++++++ 4 files changed, 771 insertions(+), 8 deletions(-) create mode 100644 test/data/h2o2-plus.xml diff --git a/interfaces/cython/cantera/test/test_onedim.py b/interfaces/cython/cantera/test/test_onedim.py index e5a9db4c9..f42eb8076 100644 --- a/interfaces/cython/cantera/test/test_onedim.py +++ b/interfaces/cython/cantera/test/test_onedim.py @@ -52,12 +52,12 @@ class TestFreeFlame(utilities.CanteraTest): tol_ss = [1.0e-5, 1.0e-14] # [rtol atol] for steady-state problem tol_ts = [1.0e-4, 1.0e-11] # [rtol atol] for time stepping - def create_sim(self, p, Tin, reactants): + def create_sim(self, p, Tin, reactants, mech='h2o2.xml'): initial_grid = [0.0, 0.001, 0.01, 0.02, 0.029, 0.03] # m # IdealGasMix object used to compute mixture properties - self.gas = ct.Solution('h2o2.xml') + self.gas = ct.Solution(mech) self.gas.TPX = Tin, p, reactants # Flame object @@ -255,6 +255,63 @@ class TestFreeFlame(utilities.CanteraTest): if isinstance(ct.FlameBase.__dict__[attr], property): getattr(self.sim, attr) + def test_save_restore_add_species(self): + reactants= 'H2:1.1, O2:1, AR:5' + p = 2 * ct.one_atm + Tin = 400 + + filename = 'onedim-add-species.xml' + if os.path.exists(filename): + os.remove(filename) + + self.create_sim(p, Tin, reactants, mech='h2o2.xml') + gas1 = self.gas + self.solve_fixed_T() + self.solve_mix(ratio=5, slope=0.5, curve=0.3) + self.sim.save(filename, 'test', loglevel=0) + T1 = self.sim.T + Y1 = self.sim.Y + + gas2 = ct.Solution('h2o2-plus.xml') + self.sim = ct.FreeFlame(gas2) + self.sim.restore(filename, 'test', loglevel=0) + T2 = self.sim.T + Y2 = self.sim.Y + + self.assertArrayNear(T1, T2) + for k1, species in enumerate(gas1.species_names): + k2 = gas2.species_index(species) + self.assertArrayNear(Y1[k1], Y2[k2]) + + + def test_save_restore_remove_species(self): + reactants= 'H2:1.1, O2:1, AR:5' + p = 2 * ct.one_atm + Tin = 400 + + filename = 'onedim-add-species.xml' + if os.path.exists(filename): + os.remove(filename) + + self.create_sim(p, Tin, reactants, mech='h2o2-plus.xml') + gas1 = self.gas + self.solve_fixed_T() + self.solve_mix(ratio=5, slope=0.5, curve=0.3) + self.sim.save(filename, 'test', loglevel=0) + T1 = self.sim.T + Y1 = self.sim.Y + + gas2 = ct.Solution('h2o2.xml') + self.sim = ct.FreeFlame(gas2) + self.sim.restore(filename, 'test', loglevel=0) + T2 = self.sim.T + Y2 = self.sim.Y + + self.assertArrayNear(T1, T2) + for k2, species in enumerate(gas2.species_names): + k1 = gas1.species_index(species) + self.assertArrayNear(Y1[k1], Y2[k2]) + class TestDiffusionFlame(utilities.CanteraTest): referenceFile = '../data/DiffusionFlameTest-h2-mix.csv' diff --git a/src/oneD/Domain1D.cpp b/src/oneD/Domain1D.cpp index a5e12967c..f68757555 100644 --- a/src/oneD/Domain1D.cpp +++ b/src/oneD/Domain1D.cpp @@ -173,9 +173,23 @@ void Domain1D::restore(const XML_Node& dom, doublereal* soln, int loglevel) string title = nodes[i]->attrib("title"); getFloatArray(*nodes[i], values, false); if (values.size() != nComponents()) { - throw CanteraError("Domain1D::restore", "Got an array of length " + - int2str(values.size()) + " when one of length " + - int2str(nComponents()) + "was expected."); + if (loglevel > 0) { + writelog("Warning: Domain1D::restore: Got an array of length " + + int2str(values.size()) + " when one of length " + + int2str(nComponents()) + " was expected. " + + "Tolerances for individual species may not be preserved.\n"); + } + // The number of components will differ when restoring from a + // mechanism with a different number of species. Assuming that + // tolerances are the same for all species, we can just copy the + // tolerance from the last species. + if (!values.empty()) { + values.resize(nComponents(), values[values.size()-1]); + } else { + // If the tolerance vector is empty, just leave the defaults + // in place. + continue; + } } if (title == "abstol_transient") { m_atol_ts = values; diff --git a/src/oneD/StFlow.cpp b/src/oneD/StFlow.cpp index 2a25c2b4b..1a5aadea1 100644 --- a/src/oneD/StFlow.cpp +++ b/src/oneD/StFlow.cpp @@ -823,9 +823,14 @@ void StFlow::restore(const XML_Node& dom, doublereal* soln, int loglevel) m_do_species[i] = x[i]; } } else if (!x.empty()) { - throw CanteraError("StFlow::restore", "species_enabled is length" + - int2str(x.size()) + "but should be length" + - int2str(m_nsp)); + // This may occur when restoring from a mechanism with a different + // number of species. + if (loglevel > 0) { + writelog("\nWarning: StFlow::restore: species_enabled is length " + + int2str(x.size()) + " but should be length " + + int2str(m_nsp) + ". Enabling all species equations by default."); + } + m_do_species.assign(m_nsp, true); } } diff --git a/test/data/h2o2-plus.xml b/test/data/h2o2-plus.xml new file mode 100644 index 000000000..c44ef3753 --- /dev/null +++ b/test/data/h2o2-plus.xml @@ -0,0 +1,687 @@ + + + + + + + O H Ar N + H2 H O O2 N2 OH H2O HO2 H2O2 AR + + + 300.0 + 101325.0 + + + + + + + + + O H Ar + H2 H O O2 N2 OH H2O HO2 H2O2 AR + + + 300.0 + 101325.0 + + + + + + + + + + + + H:2 + TPIS78 + + + + 2.344331120E+00, 7.980520750E-03, -1.947815100E-05, 2.015720940E-08, + -7.376117610E-12, -9.179351730E+02, 6.830102380E-01 + + + + 3.337279200E+00, -4.940247310E-05, 4.994567780E-07, -1.795663940E-10, + 2.002553760E-14, -9.501589220E+02, -3.205023310E+00 + + + + linear + 38.000 + 2.920 + 0.000 + 0.790 + 280.000 + + + + + + H:1 + L 7/88 + + + + 2.500000000E+00, 7.053328190E-13, -1.995919640E-15, 2.300816320E-18, + -9.277323320E-22, 2.547365990E+04, -4.466828530E-01 + + + + 2.500000010E+00, -2.308429730E-11, 1.615619480E-14, -4.735152350E-18, + 4.981973570E-22, 2.547365990E+04, -4.466829140E-01 + + + + atom + 145.000 + 2.050 + 0.000 + 0.000 + 0.000 + + + + + + O:1 + L 1/90 + + + + 3.168267100E+00, -3.279318840E-03, 6.643063960E-06, -6.128066240E-09, + 2.112659710E-12, 2.912225920E+04, 2.051933460E+00 + + + + 2.569420780E+00, -8.597411370E-05, 4.194845890E-08, -1.001777990E-11, + 1.228336910E-15, 2.921757910E+04, 4.784338640E+00 + + + + atom + 80.000 + 2.750 + 0.000 + 0.000 + 0.000 + + + + + + O:2 + TPIS89 + + + + 3.782456360E+00, -2.996734160E-03, 9.847302010E-06, -9.681295090E-09, + 3.243728370E-12, -1.063943560E+03, 3.657675730E+00 + + + + 3.282537840E+00, 1.483087540E-03, -7.579666690E-07, 2.094705550E-10, + -2.167177940E-14, -1.088457720E+03, 5.453231290E+00 + + + + linear + 107.400 + 3.460 + 0.000 + 1.600 + 3.800 + + + + + + H:1 O:1 + RUS 78 + + + + 3.992015430E+00, -2.401317520E-03, 4.617938410E-06, -3.881133330E-09, + 1.364114700E-12, 3.615080560E+03, -1.039254580E-01 + + + + 3.092887670E+00, 5.484297160E-04, 1.265052280E-07, -8.794615560E-11, + 1.174123760E-14, 3.858657000E+03, 4.476696100E+00 + + + + linear + 80.000 + 2.750 + 0.000 + 0.000 + 0.000 + + + + + + H:2 O:1 + L 8/89 + + + + 4.198640560E+00, -2.036434100E-03, 6.520402110E-06, -5.487970620E-09, + 1.771978170E-12, -3.029372670E+04, -8.490322080E-01 + + + + 3.033992490E+00, 2.176918040E-03, -1.640725180E-07, -9.704198700E-11, + 1.682009920E-14, -3.000429710E+04, 4.966770100E+00 + + + + nonlinear + 572.400 + 2.600 + 1.840 + 0.000 + 4.000 + + + + + + H:1 O:2 + L 5/89 + + + + 4.301798010E+00, -4.749120510E-03, 2.115828910E-05, -2.427638940E-08, + 9.292251240E-12, 2.948080400E+02, 3.716662450E+00 + + + + 4.017210900E+00, 2.239820130E-03, -6.336581500E-07, 1.142463700E-10, + -1.079085350E-14, 1.118567130E+02, 3.785102150E+00 + + + + nonlinear + 107.400 + 3.460 + 0.000 + 0.000 + 1.000 + + + + + + H:2 O:2 + L 7/88 + + + + 4.276112690E+00, -5.428224170E-04, 1.673357010E-05, -2.157708130E-08, + 8.624543630E-12, -1.770258210E+04, 3.435050740E+00 + + + + 4.165002850E+00, 4.908316940E-03, -1.901392250E-06, 3.711859860E-10, + -2.879083050E-14, -1.786178770E+04, 2.916156620E+00 + + + + nonlinear + 107.400 + 3.460 + 0.000 + 0.000 + 3.800 + + + + + + Ar:1 + 120186 + + + + 2.500000000E+00, 0.000000000E+00, 0.000000000E+00, 0.000000000E+00, + 0.000000000E+00, -7.453750000E+02, 4.366000000E+00 + + + + 2.500000000E+00, 0.000000000E+00, 0.000000000E+00, 0.000000000E+00, + 0.000000000E+00, -7.453750000E+02, 4.366000000E+00 + + + + atom + 136.500 + 3.330 + 0.000 + 0.000 + 0.000 + + + + + + N:2 + 121286 + + + + 3.298677000E+00, 1.408240400E-03, -3.963222000E-06, 5.641515000E-09, + -2.444854000E-12, -1.020899900E+03, 3.950372000E+00 + + + + 2.926640000E+00, 1.487976800E-03, -5.684760000E-07, 1.009703800E-10, + -6.753351000E-15, -9.227977000E+02, 5.980528000E+00 + + + + linear + 97.530 + 3.620 + 0.000 + 1.760 + 4.000 + + + + + + + + 2 O + M [=] O2 + M + + + 1.200000E+11 + -1 + 0.000000 + + AR:0.83 H2:2.4 H2O:15.4 + + O:2.0 + O2:1.0 + + + + + O + H + M [=] OH + M + + + 5.000000E+11 + -1 + 0.000000 + + AR:0.7 H2:2 H2O:6 + + H:1 O:1.0 + OH:1.0 + + + + + O + H2 [=] H + OH + + + 3.870000E+01 + 2.7000000000000002 + 6260.000000 + + + H2:1 O:1.0 + H:1.0 OH:1 + + + + + O + HO2 [=] OH + O2 + + + 2.000000E+10 + 0 + 0.000000 + + + HO2:1 O:1.0 + O2:1 OH:1.0 + + + + + O + H2O2 [=] OH + HO2 + + + 9.630000E+03 + 2 + 4000.000000 + + + H2O2:1 O:1.0 + HO2:1 OH:1.0 + + + + + H + 2 O2 [=] HO2 + O2 + + + 2.080000E+13 + -1.24 + 0.000000 + + + H:1.0 O2:2.0 + HO2:1.0 O2:1 + + + + + H + O2 + H2O [=] HO2 + H2O + + + 1.126000E+13 + -0.76000000000000001 + 0.000000 + + + H:1.0 H2O:1 O2:1 + H2O:1 HO2:1.0 + + + + + H + O2 + AR [=] HO2 + AR + + + 7.000000E+11 + -0.80000000000000004 + 0.000000 + + + H:1.0 AR:1 O2:1 + AR:1 HO2:1.0 + + + + + H + O2 [=] O + OH + + + 2.650000E+13 + -0.67069999999999996 + 17041.000000 + + + H:1.0 O2:1 + O:1.0 OH:1 + + + + + 2 H + M [=] H2 + M + + + 1.000000E+12 + -1 + 0.000000 + + AR:0.63 H2:0 H2O:0 + + H:2.0 + H2:1.0 + + + + + 2 H + H2 [=] 2 H2 + + + 9.000000E+10 + -0.59999999999999998 + 0.000000 + + + H2:1 H:2.0 + H2:2.0 + + + + + 2 H + H2O [=] H2 + H2O + + + 6.000000E+13 + -1.25 + 0.000000 + + + H:2.0 H2O:1 + H2:1.0 H2O:1 + + + + + H + OH + M [=] H2O + M + + + 2.200000E+16 + -2 + 0.000000 + + AR:0.38 H2:0.73 H2O:3.65 + + H:1.0 OH:1 + H2O:1.0 + + + + + H + HO2 [=] O + H2O + + + 3.970000E+09 + 0 + 671.000000 + + + H:1.0 HO2:1 + H2O:1 O:1.0 + + + + + H + HO2 [=] O2 + H2 + + + 4.480000E+10 + 0 + 1068.000000 + + + H:1.0 HO2:1 + H2:1 O2:1.0 + + + + + H + HO2 [=] 2 OH + + + 8.400000E+10 + 0 + 635.000000 + + + H:1.0 HO2:1 + OH:2.0 + + + + + H + H2O2 [=] HO2 + H2 + + + 1.210000E+04 + 2 + 5200.000000 + + + H:1.0 H2O2:1 + H2:1 HO2:1.0 + + + + + H + H2O2 [=] OH + H2O + + + 1.000000E+10 + 0 + 3600.000000 + + + H:1.0 H2O2:1 + H2O:1 OH:1.0 + + + + + OH + H2 [=] H + H2O + + + 2.160000E+05 + 1.51 + 3430.000000 + + + H2:1 OH:1.0 + H:1.0 H2O:1 + + + + + 2 OH (+ M) [=] H2O2 (+ M) + + + 7.400000E+10 + -0.37 + 0.000000 + + + 2.300000E+12 + -0.90000000000000002 + -1700.000000 + + AR:0.7 H2:2 H2O:6 + 0.7346 94 1756 5182 + + OH:2.0 + H2O2:1.0 + + + + + 2 OH [=] O + H2O + + + 3.570000E+01 + 2.3999999999999999 + -2110.000000 + + + OH:2.0 + H2O:1 O:1.0 + + + + + OH + HO2 [=] O2 + H2O + + + 1.450000E+10 + 0 + -500.000000 + + + HO2:1 OH:1.0 + H2O:1 O2:1.0 + + + + + OH + H2O2 [=] HO2 + H2O + + + 2.000000E+09 + 0 + 427.000000 + + + H2O2:1 OH:1.0 + H2O:1 HO2:1.0 + + + + + OH + H2O2 [=] HO2 + H2O + + + 1.700000E+15 + 0 + 29410.000000 + + + H2O2:1 OH:1.0 + H2O:1 HO2:1.0 + + + + + 2 HO2 [=] O2 + H2O2 + + + 1.300000E+08 + 0 + -1630.000000 + + + HO2:2.0 + O2:1.0 H2O2:1 + + + + + 2 HO2 [=] O2 + H2O2 + + + 4.200000E+11 + 0 + 12000.000000 + + + HO2:2.0 + O2:1.0 H2O2:1 + + + + + OH + HO2 [=] O2 + H2O + + + 5.000000E+12 + 0 + 17330.000000 + + + HO2:1 OH:1.0 + H2O:1 O2:1.0 + + +