From 2aec189ddcfef2b227603d421cb6a723df260295 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 18 Jun 2015 14:37:54 -0400 Subject: [PATCH] [Reactor] Check for Reactor contents before installing FlowDevice Fixes #279. --- interfaces/cython/cantera/_cantera.pxd | 2 +- interfaces/cython/cantera/test/test_reactor.py | 13 +++++++++++++ src/zeroD/FlowDevice.cpp | 5 +++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index ac28ddbed..78d83b15e 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -513,7 +513,7 @@ cdef extern from "cantera/zeroD/flowControllers.h": cdef cppclass CxxFlowDevice "Cantera::FlowDevice": CxxFlowDevice() double massFlowRate(double) except + - cbool install(CxxReactorBase&, CxxReactorBase&) + cbool install(CxxReactorBase&, CxxReactorBase&) except + void setFunction(CxxFunc1*) void setParameters(int, double*) diff --git a/interfaces/cython/cantera/test/test_reactor.py b/interfaces/cython/cantera/test/test_reactor.py index a54aa6b98..6c2e6e2b4 100644 --- a/interfaces/cython/cantera/test/test_reactor.py +++ b/interfaces/cython/cantera/test/test_reactor.py @@ -433,6 +433,19 @@ class TestReactor(utilities.CanteraTest): self.assertNear(speciesMass(kAr), mAr) self.assertNear(speciesMass(kO2), mO2) + def test_valve_errors(self): + self.make_reactors() + res = ct.Reservoir() + + with self.assertRaises(RuntimeError): + # Must assign contents of both reactors before creating Valve + v = ct.Valve(self.r1, res) + + v = ct.Valve(self.r1, self.r2) + with self.assertRaises(RuntimeError): + # inlet and outlet cannot be reassigned + v._install(self.r2, self.r1) + def test_pressure_controller(self): self.make_reactors(n_reactors=1) g = ct.Solution('h2o2.xml') diff --git a/src/zeroD/FlowDevice.cpp b/src/zeroD/FlowDevice.cpp index 24bee4085..00c3bac63 100644 --- a/src/zeroD/FlowDevice.cpp +++ b/src/zeroD/FlowDevice.cpp @@ -9,7 +9,7 @@ namespace Cantera bool FlowDevice::install(ReactorBase& in, ReactorBase& out) { if (m_in || m_out) { - return false; + throw CanteraError("FlowDevice::install", "Already installed"); } m_in = ∈ m_out = &out; @@ -20,7 +20,8 @@ bool FlowDevice::install(ReactorBase& in, ReactorBase& out) ThermoPhase* mixin = &m_in->contents(); ThermoPhase* mixout = &m_out->contents(); if (mixin == 0 || mixout == 0) { - return false; + throw CanteraError("FlowDevice::install", "Can't install flow device " + "until reactor contents have been assigned."); } m_nspin = mixin->nSpecies();