From 5b77bbb719ce7103db40cff788aec15a8839f801 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Mon, 24 Mar 2014 21:38:22 +0000 Subject: [PATCH] [1D] Add bounds checks to setRefineCriteria --- include/cantera/oneD/refine.h | 8 ++---- interfaces/cython/cantera/_cantera.pxd | 2 +- interfaces/cython/cantera/test/test_onedim.py | 13 +++++++++- src/oneD/refine.cpp | 26 +++++++++++++++++++ 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/include/cantera/oneD/refine.h b/include/cantera/oneD/refine.h index 6dd7d2d73..2eadf84dc 100644 --- a/include/cantera/oneD/refine.h +++ b/include/cantera/oneD/refine.h @@ -31,12 +31,8 @@ public: void setCriteria(doublereal ratio = 10.0, doublereal slope = 0.8, doublereal curve = 0.8, - doublereal prune = -0.1) { - m_ratio = ratio; - m_slope = slope; - m_curve = curve; - m_prune = prune; - } + doublereal prune = -0.1); + void setActive(int comp, bool state = true) { m_active[comp] = state; } diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index a9ed8c177..08a656698 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -449,7 +449,7 @@ cdef extern from "cantera/oneD/Sim1D.h": void getInitialSoln() except + void solve(int, cbool) except +translate_exception void refine(int) except + - void setRefineCriteria(size_t, double, double, double, double) + void setRefineCriteria(size_t, double, double, double, double) except + void save(string, string, string, int) except + void restore(string, string, int) except + void writeStats(int) except + diff --git a/interfaces/cython/cantera/test/test_onedim.py b/interfaces/cython/cantera/test/test_onedim.py index a6b0ec82a..e3febd7e8 100644 --- a/interfaces/cython/cantera/test/test_onedim.py +++ b/interfaces/cython/cantera/test/test_onedim.py @@ -297,7 +297,6 @@ class TestFreeFlame(utilities.CanteraTest): 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 @@ -326,6 +325,18 @@ class TestFreeFlame(utilities.CanteraTest): k1 = gas1.species_index(species) self.assertArrayNear(Y1[k1], Y2[k2]) + def test_refine_criteria_boundscheck(self): + self.create_sim(ct.one_atm, 300.0, 'H2:1.1, O2:1, AR:5') + good = [3.0, 0.1, 0.2, 0.05] + bad = [1.2, 1.1, -2, 0.3] + + self.sim.set_refine_criteria(*good) + for i in range(4): + with self.assertRaises(Exception): + vals = list(good) + vals[i] = bad[i] + self.sim.set_refine_criteria(*vals) + class TestDiffusionFlame(utilities.CanteraTest): referenceFile = '../data/DiffusionFlameTest-h2-mix.csv' diff --git a/src/oneD/refine.cpp b/src/oneD/refine.cpp index bed9828bf..bb9ab5ec6 100644 --- a/src/oneD/refine.cpp +++ b/src/oneD/refine.cpp @@ -27,6 +27,32 @@ Refiner::Refiner(Domain1D& domain) : m_thresh = std::sqrt(std::numeric_limits::epsilon()); } +void Refiner::setCriteria(doublereal ratio, doublereal slope, + doublereal curve, doublereal prune) +{ + if (ratio < 2.0) { + throw CanteraError("Refiner::setCriteria", + "'ratio' must be greater than 2.0 (" + fp2str(ratio) + + " was specified)."); + } else if (slope < 0.0 || slope > 1.0) { + throw CanteraError("Refiner::setCriteria", + "'slope' must be between 0.0 and 1.0 (" + fp2str(slope) + + " was specified)."); + } else if (curve < 0.0 || curve > 1.0) { + throw CanteraError("Refiner::setCriteria", + "'curve' must be between 0.0 and 1.0 (" + fp2str(curve) + + " was specified)."); + } else if (prune > curve || prune > slope) { + throw CanteraError("Refiner::setCriteria", + "'prune' must be less than 'curve' and 'slope' (" + fp2str(prune) + + " was specified)."); + } + m_ratio = ratio; + m_slope = slope; + m_curve = curve; + m_prune = prune; +} + int Refiner::analyze(size_t n, const doublereal* z, const doublereal* x) {