From 91c7bf14d75b9589c05876a768c8ec0e49b93ad5 Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Tue, 5 Nov 2019 21:28:15 -0600 Subject: [PATCH] [Base] finalize introduction of Solution object in C++ layer --- include/cantera/base/Solution.h | 45 ++++++---------- include/cantera/zeroD/Reactor.h | 4 +- include/cantera/zeroD/Reservoir.h | 2 +- interfaces/cython/cantera/_cantera.pxd | 2 +- interfaces/cython/cantera/base.pyx | 2 +- samples/cxx/demo.cpp | 2 +- samples/cxx/flamespeed/flamespeed.cpp | 2 +- samples/f77/demo_ftnlib.cpp | 22 ++++---- src/base/Solution.cpp | 53 +++++++++++-------- src/oneD/StFlow.cpp | 2 +- .../mixGasTransport/mixGasTransport.cpp | 15 +++--- .../multiGasTransport/multiGasTransport.cpp | 15 +++--- 12 files changed, 79 insertions(+), 87 deletions(-) diff --git a/include/cantera/base/Solution.h b/include/cantera/base/Solution.h index f50200835..155c2d674 100644 --- a/include/cantera/base/Solution.h +++ b/include/cantera/base/Solution.h @@ -20,8 +20,6 @@ class Solution : public std::enable_shared_from_this { private: Solution(); - Solution(const std::string& infile, std::string name, std::string transport, - std::vector > adjacent); public: ~Solution() {} @@ -33,25 +31,6 @@ public: return shared_ptr( new Solution ); } - //! Create and initialize a Solution object from an input file - /*! - * This constructor wraps newPhase(), newKinetics() and - * newTransportMgr() routines for initialization. - * - * @param infile name of the input file - * @param name name of the phase in the file. - * If this is blank, the first phase in the file is used. - * @param transport name of the transport model. - * @param adjacent vector containing adjacent solution objects. - * @returns an initialized Solution object. - */ - static shared_ptr create(const std::string& infile, - std::string name="", - std::string transport="", - std::vector > adjacent={}) { - return shared_ptr( new Solution(infile, name, transport, adjacent) ); - } - //! Return the name of this Solution object std::string name() const; @@ -59,7 +38,7 @@ public: void setName(const std::string& name); //! Set the ThermoPhase object - void setThermoPhase(shared_ptr thermo); + void setThermo(shared_ptr thermo); //! Set the Kinetics object void setKinetics(shared_ptr kinetics); @@ -88,14 +67,22 @@ protected: shared_ptr m_transport; //!< Transport manager }; -/** - * Create a new solution manager. +//! Create and initialize a new Solution manager from an input file +/*! + * This constructor wraps newPhase(), newKinetics() and + * newTransportMgr() routines for initialization. + * + * @param infile name of the input file + * @param name name of the phase in the file. + * If this is blank, the first phase in the file is used. + * @param transport name of the transport model. + * @param adjacent vector containing adjacent solution objects. + * @returns an initialized Solution object. */ -inline shared_ptr newSolution(const std::string& infile, std::string name="", - std::string transport="", - std::vector > adjacent={}) { - return Solution::create(infile, name, transport, adjacent); -} +shared_ptr newSolution(const std::string& infile, + const std::string& name="", + const std::string& transport="", + const std::vector>& adjacent={}); } #endif diff --git a/include/cantera/zeroD/Reactor.h b/include/cantera/zeroD/Reactor.h index 920f87c12..d9e156ba4 100644 --- a/include/cantera/zeroD/Reactor.h +++ b/include/cantera/zeroD/Reactor.h @@ -65,8 +65,8 @@ public: } void insert(shared_ptr sol) { - setThermoMgr(*(sol->thermo())); - setKineticsMgr(*(sol->kinetics())); + setThermoMgr(*sol->thermo()); + setKineticsMgr(*sol->kinetics()); } virtual void setKineticsMgr(Kinetics& kin); diff --git a/include/cantera/zeroD/Reservoir.h b/include/cantera/zeroD/Reservoir.h index 65728881f..0de897859 100644 --- a/include/cantera/zeroD/Reservoir.h +++ b/include/cantera/zeroD/Reservoir.h @@ -37,7 +37,7 @@ public: } void insert(shared_ptr sol) { - setThermoMgr(*(sol->thermo())); + setThermoMgr(*sol->thermo()); } }; diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 58d8b2650..cfb7b6943 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -127,7 +127,7 @@ cdef extern from "cantera/base/Solution.h" namespace "Cantera": CxxSolution() string name() void setName(string) - void setThermoPhase(shared_ptr[CxxThermoPhase]) + void setThermo(shared_ptr[CxxThermoPhase]) void setKinetics(shared_ptr[CxxKinetics]) void setTransport(shared_ptr[CxxTransport]) diff --git a/interfaces/cython/cantera/base.pyx b/interfaces/cython/cantera/base.pyx index 6444a4c56..7ed83554b 100644 --- a/interfaces/cython/cantera/base.pyx +++ b/interfaces/cython/cantera/base.pyx @@ -61,7 +61,7 @@ cdef class _SolutionBase: raise ValueError("Arguments are insufficient to define a phase") # Initialization of transport is deferred to Transport.__init__ - self.base.setThermoPhase(self._thermo) + self.base.setThermo(self._thermo) self.base.setKinetics(self._kinetics) self._selected_species = np.ndarray(0, dtype=np.integer) diff --git a/samples/cxx/demo.cpp b/samples/cxx/demo.cpp index fe678c86f..6af584509 100644 --- a/samples/cxx/demo.cpp +++ b/samples/cxx/demo.cpp @@ -66,7 +66,7 @@ void demoprog() // Reaction information - auto kin = getGasKinetics(sol); + auto kin = sol->kinetics(); int irxns = kin->nReactions(); vector_fp qf(irxns); vector_fp qr(irxns); diff --git a/samples/cxx/flamespeed/flamespeed.cpp b/samples/cxx/flamespeed/flamespeed.cpp index bc62bf054..d687671d7 100644 --- a/samples/cxx/flamespeed/flamespeed.cpp +++ b/samples/cxx/flamespeed/flamespeed.cpp @@ -72,7 +72,7 @@ int flamespeed(double phi) std::unique_ptr trmulti(newTransportMgr("Multi", sol->thermo().get())); flow.setTransport(*trmix); - flow.setKinetics(*(sol->kinetics())); + flow.setKinetics(*sol->kinetics()); flow.setPressure(pressure); //------- step 2: create the inlet ----------------------- diff --git a/samples/f77/demo_ftnlib.cpp b/samples/f77/demo_ftnlib.cpp index 9e5b19bc0..8e050d012 100644 --- a/samples/f77/demo_ftnlib.cpp +++ b/samples/f77/demo_ftnlib.cpp @@ -41,15 +41,15 @@ using std::string; static shared_ptr _sol = NULL; // store a pointer to the thermophase object -static shared_ptr _gas = NULL; -shared_ptr _gasptr() +static shared_ptr _gas = NULL; +shared_ptr _gasptr() { return _gas; } // store a pointer to the kinetics object -static shared_ptr _kin = NULL; -shared_ptr _kinptr() +static shared_ptr _kin = NULL; +shared_ptr _kinptr() { return _kin; } @@ -94,8 +94,8 @@ extern "C" { string fth = string(id, lenid); trmodel = string(transport, lentr); _sol = newSolution(fin, fth, trmodel); - _gas = std::dynamic_pointer_cast(_sol->thermo()); - _kin = std::dynamic_pointer_cast(_sol->kinetics()); + _gas = _sol->thermo(); + _kin = _sol->kinetics(); _trans = _sol->transport(); } catch (CanteraError& err) { handleError(err); @@ -103,24 +103,24 @@ extern "C" { } /// integer function nElements() - int nelements_() + integer nelements_() { return _gas->nElements(); } /// integer function nSpecies() - int nspecies_() + integer nspecies_() { return _gas->nSpecies(); } /// integer function nReactions() - int nreactions_() + integer nreactions_() { return _kin->nReactions(); } - void getspeciesname_(int* k, char* name, ftnlen n) + void getspeciesname_(integer* k, char* name, ftnlen n) { int ik = *k - 1; std::fill(name, name + n, ' '); @@ -292,7 +292,7 @@ extern "C" { //---------------- kinetics ------------------------- - void getreactioneqn_(int* i, char* eqn, ftnlen n) + void getreactioneqn_(integer* i, char* eqn, ftnlen n) { int irxn = *i - 1; std::fill(eqn, eqn + n, ' '); diff --git a/src/base/Solution.cpp b/src/base/Solution.cpp index 2f621e489..1e25e3708 100644 --- a/src/base/Solution.cpp +++ b/src/base/Solution.cpp @@ -19,28 +19,6 @@ namespace Cantera Solution::Solution() {} - Solution::Solution(const std::string& infile, std::string name, std::string transport, - std::vector > adjacent) { - - // thermo phase - m_thermo = shared_ptr(newPhase(infile, name)); - - // kinetics - std::vector phases; - for (auto & adj : adjacent) { - phases.push_back(adj->thermo().get()); - } - phases.push_back(m_thermo.get()); - m_kinetics = std::move(newKinetics(phases, infile, name)); - - // transport - if (transport=="") { - m_transport = shared_ptr(newDefaultTransportMgr(m_thermo.get())); - } else if (transport!="None") { - m_transport = shared_ptr(newTransportMgr(transport, m_thermo.get())); - } -} - std::string Solution::name() const { if (m_thermo) { return m_thermo->name(); @@ -59,7 +37,7 @@ void Solution::setName(const std::string& name) { } } -void Solution::setThermoPhase(shared_ptr thermo) { +void Solution::setThermo(shared_ptr thermo) { m_thermo = thermo; if (m_thermo) { m_thermo->setRoot(shared_from_this()); @@ -80,4 +58,33 @@ void Solution::setTransport(shared_ptr transport) { } } +shared_ptr newSolution(const std::string& infile, + const std::string& name, + const std::string& transport, + const std::vector>& adjacent) { + + // instantiate Solution object + auto sol = Solution::create(); + + // thermo phase + sol->setThermo(shared_ptr(newPhase(infile, name))); + + // kinetics + std::vector phases; + phases.push_back(sol->thermo().get()); + for (auto& adj : adjacent) { + phases.push_back(adj->thermo().get()); + } + sol->setKinetics(newKinetics(phases, infile, name)); + + // transport + if (transport == "") { + sol->setTransport(shared_ptr(newDefaultTransportMgr(sol->thermo().get()))); + } else if (transport != "None") { + sol->setTransport(shared_ptr(newTransportMgr(transport, sol->thermo().get()))); + } + + return sol; +} + } // namespace Cantera diff --git a/src/oneD/StFlow.cpp b/src/oneD/StFlow.cpp index 15e65da8f..48e70bdbf 100644 --- a/src/oneD/StFlow.cpp +++ b/src/oneD/StFlow.cpp @@ -31,7 +31,7 @@ StFlow::StFlow(ThermoPhase* ph, size_t nsp, size_t points) : m_tfixed(Undef) { if (ph->type() == "IdealGas") { - m_thermo = (IdealGasPhase*)ph; + m_thermo = static_cast(ph); } else { throw CanteraError("StFlow::StFlow", "Unsupported phase type: need 'IdealGasPhase'"); diff --git a/test_problems/mixGasTransport/mixGasTransport.cpp b/test_problems/mixGasTransport/mixGasTransport.cpp index 1a6fc47c1..ea8a66398 100644 --- a/test_problems/mixGasTransport/mixGasTransport.cpp +++ b/test_problems/mixGasTransport/mixGasTransport.cpp @@ -134,12 +134,11 @@ int main(int argc, char** argv) grad_T[1] = (T3 - T1) / dist; auto tran = sol->transport(); - auto tranMix = dynamic_pointer_cast(tran); gas->setState_TPX(1500.0, pres, Xset.data()); vector_fp mixDiffs(nsp, 0.0); - tranMix->getMixDiffCoeffs(mixDiffs.data()); + tran->getMixDiffCoeffs(mixDiffs.data()); printf(" Dump of the mixture Diffusivities:\n"); for (size_t k = 0; k < nsp; k++) { string sss = gas->speciesName(k); @@ -148,7 +147,7 @@ int main(int argc, char** argv) vector_fp specVisc(nsp, 0.0); - tranMix->getSpeciesViscosities(specVisc.data()); + tran->getSpeciesViscosities(specVisc.data()); printf(" Dump of the species viscosities:\n"); for (size_t k = 0; k < nsp; k++) { string sss = gas->speciesName(k); @@ -156,7 +155,7 @@ int main(int argc, char** argv) } vector_fp thermDiff(nsp, 0.0); - tranMix->getThermalDiffCoeffs(thermDiff.data()); + tran->getThermalDiffCoeffs(thermDiff.data()); printf(" Dump of the Thermal Diffusivities :\n"); for (size_t k = 0; k < nsp; k++) { string sss = gas->speciesName(k); @@ -177,7 +176,7 @@ int main(int argc, char** argv) Array2D Bdiff(nsp, nsp, 0.0); printf("Binary Diffusion Coefficients H2 vs species\n"); - tranMix->getBinaryDiffCoeffs(nsp, Bdiff.ptrColumn(0)); + tran->getBinaryDiffCoeffs(nsp, Bdiff.ptrColumn(0)); for (size_t k = 0; k < nsp; k++) { string sss = gas->speciesName(k); printf(" H2 - %15s %13.4g %13.4g\n", sss.c_str(), Bdiff(0,k), Bdiff(k,0)); @@ -186,7 +185,7 @@ int main(int argc, char** argv) vector_fp specMob(nsp, 0.0); - tranMix->getMobilities(specMob.data()); + tran->getMobilities(specMob.data()); printf(" Dump of the species mobilities:\n"); for (size_t k = 0; k < nsp; k++) { string sss = gas->speciesName(k); @@ -195,8 +194,8 @@ int main(int argc, char** argv) Array2D fluxes(nsp, 2, 0.0); - tranMix->getSpeciesFluxes(2, grad_T.data(), nsp, - grad_X.ptrColumn(0), nsp, fluxes.ptrColumn(0)); + tran->getSpeciesFluxes(2, grad_T.data(), nsp, + grad_X.ptrColumn(0), nsp, fluxes.ptrColumn(0)); printf(" Dump of the species fluxes:\n"); double sum1 = 0.0; double sum2 = 0.0; diff --git a/test_problems/multiGasTransport/multiGasTransport.cpp b/test_problems/multiGasTransport/multiGasTransport.cpp index 9b3882fd9..1c19cecba 100644 --- a/test_problems/multiGasTransport/multiGasTransport.cpp +++ b/test_problems/multiGasTransport/multiGasTransport.cpp @@ -142,11 +142,10 @@ int main(int argc, char** argv) grad_T[1] = (T3 - T1) / dist; auto tran = sol->transport(); - auto tranMix = dynamic_pointer_cast(tran); gas->setState_TPX(1500.0, pres, Xset.data()); vector_fp mixDiffs(nsp, 0.0); - tranMix->getMixDiffCoeffs(mixDiffs.data()); + tran->getMixDiffCoeffs(mixDiffs.data()); printf(" Dump of the mixture Diffusivities:\n"); for (size_t k = 0; k < nsp; k++) { string sss = gas->speciesName(k); @@ -154,7 +153,7 @@ int main(int argc, char** argv) } vector_fp specVisc(nsp, 0.0); - tranMix->getSpeciesViscosities(specVisc.data()); + tran->getSpeciesViscosities(specVisc.data()); printf(" Dump of the species viscosities:\n"); for (size_t k = 0; k < nsp; k++) { string sss = gas->speciesName(k); @@ -162,7 +161,7 @@ int main(int argc, char** argv) } vector_fp thermDiff(nsp, 0.0); - tranMix->getThermalDiffCoeffs(thermDiff.data()); + tran->getThermalDiffCoeffs(thermDiff.data()); printf(" Dump of the Thermal Diffusivities :\n"); for (size_t k = 0; k < nsp; k++) { string sss = gas->speciesName(k); @@ -184,7 +183,7 @@ int main(int argc, char** argv) Array2D Bdiff(nsp, nsp, 0.0); printf("Binary Diffusion Coefficients H2 vs species\n"); - tranMix->getBinaryDiffCoeffs(nsp, Bdiff.ptrColumn(0)); + tran->getBinaryDiffCoeffs(nsp, Bdiff.ptrColumn(0)); for (size_t k = 0; k < nsp; k++) { string sss = gas->speciesName(k); printf(" H2 - %15s %13.4g %13.4g\n", sss.c_str(), Bdiff(0,k), Bdiff(k,0)); @@ -199,8 +198,8 @@ int main(int argc, char** argv) } Array2D fluxes(nsp, 2, 0.0); - tranMix->getSpeciesFluxes(2, grad_T.data(), nsp, - grad_X.ptrColumn(0), nsp, fluxes.ptrColumn(0)); + tran->getSpeciesFluxes(2, grad_T.data(), nsp, + grad_X.ptrColumn(0), nsp, fluxes.ptrColumn(0)); printf(" Dump of the species fluxes:\n"); double sum1 = 0.0; double sum2 = 0.0; @@ -237,7 +236,7 @@ int main(int argc, char** argv) Array2D MDdiff(nsp, nsp, 0.0); printf("Multicomponent Diffusion Coefficients H2 vs species\n"); - tranMix->getMultiDiffCoeffs(nsp, MDdiff.ptrColumn(0)); + tran->getMultiDiffCoeffs(nsp, MDdiff.ptrColumn(0)); for (size_t k = 0; k < nsp; k++) { string sss = gas->speciesName(k); printf(" H2 - %15s %13.4g %13.4g\n", sss.c_str(), MDdiff(0,k), MDdiff(k,0));