From 49e48a4757aca21b20982294dfb5e87d4a079b0e Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Wed, 4 May 2022 21:34:19 -0500 Subject: [PATCH 1/4] [Solution] Implement Solution::setTransport by model name --- include/cantera/base/Solution.h | 6 +++++- src/base/Solution.cpp | 24 ++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/include/cantera/base/Solution.h b/include/cantera/base/Solution.h index 3e39777de6..d392a0ea01 100644 --- a/include/cantera/base/Solution.h +++ b/include/cantera/base/Solution.h @@ -44,9 +44,13 @@ class Solution //! Set the Kinetics object virtual void setKinetics(shared_ptr kinetics); - //! Set the Transport object + //! Set the Transport object directly virtual void setTransport(shared_ptr transport); + //! Set the Transport object by name + //! @param model name of transport model + virtual void setTransport(const std::string& model); + //! Accessor for the ThermoPhase pointer shared_ptr thermo() { return m_thermo; diff --git a/src/base/Solution.cpp b/src/base/Solution.cpp index f5fef21ee9..6352ce257b 100644 --- a/src/base/Solution.cpp +++ b/src/base/Solution.cpp @@ -51,6 +51,18 @@ void Solution::setTransport(shared_ptr transport) { m_transport = transport; } +void Solution::setTransport(const std::string& model) { + if (model == "") { + setTransport(shared_ptr( + newDefaultTransportMgr(m_thermo.get()))); + } else if (model == "None") { + setTransport(shared_ptr(newTransportMgr("None"))); + } else { + setTransport(shared_ptr( + newTransportMgr(model, m_thermo.get()))); + } +} + void Solution::addAdjacent(shared_ptr adjacent) { if (m_adjacentByName.count(adjacent->name())) { throw CanteraError("Solution::addAdjacent", @@ -257,16 +269,8 @@ shared_ptr newSolution(const AnyMap& phaseNode, } sol->setKinetics(newKinetics(phases, phaseNode, rootNode)); - // transport - if (transport == "") { - sol->setTransport(shared_ptr( - newDefaultTransportMgr(sol->thermo().get()))); - } else if (transport == "None") { - sol->setTransport(shared_ptr(newTransportMgr("None"))); - } else { - sol->setTransport(shared_ptr( - newTransportMgr(transport, sol->thermo().get()))); - } + // set transport model by name + sol->setTransport(transport); // save root-level information (YAML header) AnyMap header; From 2266deb1630a4eec7dcb353becd375b5c067356d Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Tue, 5 Apr 2022 10:43:31 -0500 Subject: [PATCH 2/4] [samples] Introduce cantera/core.h header --- include/cantera/core.h | 18 ++++++++++++++++++ include/cantera/onedim.h | 3 +++ include/cantera/zerodim.h | 3 +++ 3 files changed, 24 insertions(+) create mode 100644 include/cantera/core.h diff --git a/include/cantera/core.h b/include/cantera/core.h new file mode 100644 index 0000000000..787f2ece54 --- /dev/null +++ b/include/cantera/core.h @@ -0,0 +1,18 @@ +/** + * @file core.h + * + * Support for Cantera core calculations from C++ application programs. This + * header file includes a minimal set of headers needed to create and use objects + * that evaluate thermo properties, chemical kinetics and transport properties. + */ + +#ifndef CT_INCL_CORE_H +#define CT_INCL_CORE_H + +// #include "cantera/base/global.h" +#include "cantera/base/Solution.h" +#include "cantera/thermo/ThermoPhase.h" +#include "cantera/kinetics/Kinetics.h" +#include "cantera/transport/TransportBase.h" + +#endif diff --git a/include/cantera/onedim.h b/include/cantera/onedim.h index 55116ae182..83217049dc 100644 --- a/include/cantera/onedim.h +++ b/include/cantera/onedim.h @@ -11,6 +11,9 @@ #ifndef CT_INCL_ONEDIM_H #define CT_INCL_ONEDIM_H +// Cantera core +#include "cantera/core.h" + #include "oneD/Sim1D.h" #include "oneD/Domain1D.h" #include "oneD/Boundary1D.h" diff --git a/include/cantera/zerodim.h b/include/cantera/zerodim.h index 2265f19e9a..42ea4b6580 100644 --- a/include/cantera/zerodim.h +++ b/include/cantera/zerodim.h @@ -6,6 +6,9 @@ #ifndef CT_INCL_ZERODIM_H #define CT_INCL_ZERODIM_H +// Cantera core +#include "cantera/core.h" + // reactor network #include "cantera/zeroD/ReactorNet.h" From 2acc046c81bbbee92425701ce76dc39a24c9aae7 Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Mon, 4 Apr 2022 22:30:00 -0500 Subject: [PATCH 3/4] [samples] Remove redundant headers --- include/cantera/core.h | 1 - samples/cxx/LiC6_electrode/LiC6_electrode.cpp | 6 +++--- samples/cxx/combustor/combustor.cpp | 2 -- samples/cxx/custom/custom.cpp | 16 +++++++--------- samples/cxx/demo/demo.cpp | 9 ++++----- samples/cxx/flamespeed/flamespeed.cpp | 16 +++++----------- samples/cxx/gas_transport/gas_transport.cpp | 16 +++++++--------- samples/cxx/jacobian/derivative_speed.cpp | 4 +--- samples/cxx/kinetics1/kinetics1.cpp | 1 - samples/cxx/openmp_ignition/openmp_ignition.cpp | 1 - 10 files changed, 27 insertions(+), 45 deletions(-) diff --git a/include/cantera/core.h b/include/cantera/core.h index 787f2ece54..f0b7bf9b8b 100644 --- a/include/cantera/core.h +++ b/include/cantera/core.h @@ -9,7 +9,6 @@ #ifndef CT_INCL_CORE_H #define CT_INCL_CORE_H -// #include "cantera/base/global.h" #include "cantera/base/Solution.h" #include "cantera/thermo/ThermoPhase.h" #include "cantera/kinetics/Kinetics.h" diff --git a/samples/cxx/LiC6_electrode/LiC6_electrode.cpp b/samples/cxx/LiC6_electrode/LiC6_electrode.cpp index 4fb3c2f01e..1496530350 100644 --- a/samples/cxx/LiC6_electrode/LiC6_electrode.cpp +++ b/samples/cxx/LiC6_electrode/LiC6_electrode.cpp @@ -13,7 +13,7 @@ // This file is part of Cantera. See License.txt in the top-level directory or // at https://cantera.org/license.txt for license and copyright information. -#include "cantera/thermo.h" +#include "cantera/core.h" #include #include @@ -21,12 +21,12 @@ using namespace Cantera; void calc_potentials() { - suppress_deprecation_warnings(); double Tk = 273.15 + 25.0; std::string filename = "LiC6_electrodebulk.yaml"; std::string phasename = "LiC6_and_Vacancies"; - std::unique_ptr electrodebulk(newPhase(filename,phasename)); + auto sol = newSolution(filename, phasename); + auto electrodebulk = sol->thermo(); std::string intercalatingSpeciesName("Li(C6)"); size_t intercalatingSpeciesIdx = electrodebulk->speciesIndex(intercalatingSpeciesName); size_t nsp_tot = electrodebulk->nSpecies(); diff --git a/samples/cxx/combustor/combustor.cpp b/samples/cxx/combustor/combustor.cpp index 2aed391c74..66c2610108 100644 --- a/samples/cxx/combustor/combustor.cpp +++ b/samples/cxx/combustor/combustor.cpp @@ -17,8 +17,6 @@ // at https://cantera.org/license.txt for license and copyright information. #include "cantera/zerodim.h" -#include "cantera/thermo/IdealGasPhase.h" - #include using namespace Cantera; diff --git a/samples/cxx/custom/custom.cpp b/samples/cxx/custom/custom.cpp index 4bc80cb913..4d36aa4b49 100644 --- a/samples/cxx/custom/custom.cpp +++ b/samples/cxx/custom/custom.cpp @@ -13,9 +13,7 @@ // This file is part of Cantera. See License.txt in the top-level directory or // at https://cantera.org/license.txt for license and copyright information. -#include "cantera/thermo.h" -#include "cantera/kinetics.h" -#include "cantera/base/Solution.h" +#include "cantera/core.h" #include "cantera/numerics/Integrator.h" #include @@ -75,7 +73,7 @@ class ReactorODEs : public FuncEval { double *massFracs = &y[1]; double *dTdt = &ydot[0]; double *dYdt = &ydot[1]; - + /* ------------------------- UPDATE GAS STATE ------------------------- */ // the state of the ThermoPhase is updated to reflect the current solution // vector, which was calculated by the integrator. @@ -87,7 +85,7 @@ class ReactorODEs : public FuncEval { double cp = m_gas->cp_mass(); m_gas->getPartialMolarEnthalpies(&m_hbar[0]); m_kinetics->getNetProductionRates(&m_wdot[0]); - + /* -------------------------- ENERGY EQUATION ------------------------- */ // the rate of change of the system temperature is found using the energy // equation for a closed-system constant pressure ideal gas: @@ -110,7 +108,7 @@ class ReactorODEs : public FuncEval { dYdt[k] = m_wdot[k] * m_gas->molecularWeight(k) / rho; } } - + /** * Number of equations in the ODE system. * - overridden from FuncEval, called by the integrator during initialization. @@ -153,7 +151,7 @@ int main() { // for each simulation time point. // create the csv file, overwriting any existing ones with the same name. std::ofstream outputFile("custom_cxx.csv"); - + // for convenience, a title for each of the state vector's components is written to // the first line of the csv file. outputFile << "time (s), temp (K)"; @@ -171,7 +169,7 @@ int main() { // the new absolute time of the system. double tnow = 0.0; double tfinal = 1e-3; - + /* ------------------- CREATE & INIT ODE INTEGRATOR ------------------- */ // create an ODE integrator object, which will be used to solve the system of ODES defined // in the ReactorODEs class. a C++ interface to the C-implemented SUNDIALS CVODES integrator @@ -188,7 +186,7 @@ int main() { // internally, the integrator will apply settings, allocate needed memory, and populate // this memory with the appropriate initial values for the system. integrator->initialize(tnow, odes); - + /* ----------------------- SIMULATION TIME LOOP ----------------------- */ while (tnow < tfinal) { // advance the simulation to the current absolute time, tnow, using the integrator's diff --git a/samples/cxx/demo/demo.cpp b/samples/cxx/demo/demo.cpp index 9ee4e14a66..143f58c291 100644 --- a/samples/cxx/demo/demo.cpp +++ b/samples/cxx/demo/demo.cpp @@ -18,10 +18,8 @@ // provide a simplified interface to the Cantera header files. If you need // to include core headers directly, use the format "cantera/module/*.h". -#include "cantera/thermo/IdealGasPhase.h" // defines class IdealGasPhase -#include "cantera/base/Solution.h" -#include "cantera/kinetics/GasKinetics.h" -#include "cantera/transport.h" // transport properties +#include "cantera/core.h" +#include "cantera/base/global.h" // provides Cantera::writelog #include // All Cantera kernel names are in namespace Cantera. You can either @@ -98,7 +96,8 @@ void demoprog() // create a transport manager for the gas that computes // mixture-averaged properties - std::unique_ptr tr(newTransportMgr("Mix", sol->thermo().get())); + sol->setTransport("mixture-averaged"); + auto tr = sol->transport(); // print the viscosity, thermal conductivity, and diffusion // coefficients diff --git a/samples/cxx/flamespeed/flamespeed.cpp b/samples/cxx/flamespeed/flamespeed.cpp index 9f1be1c0b0..38965b1963 100644 --- a/samples/cxx/flamespeed/flamespeed.cpp +++ b/samples/cxx/flamespeed/flamespeed.cpp @@ -13,12 +13,7 @@ // This file is part of Cantera. See License.txt in the top-level directory or // at https://cantera.org/license.txt for license and copyright information. -#include "cantera/oneD/Sim1D.h" -#include "cantera/oneD/Boundary1D.h" -#include "cantera/oneD/StFlow.h" -#include "cantera/thermo/IdealGasPhase.h" -#include "cantera/transport.h" -#include "cantera/base/Solution.h" +#include "cantera/onedim.h" #include "cantera/base/stringUtils.h" #include @@ -75,10 +70,8 @@ int flamespeed(double phi, bool refine_grid, int loglevel) // specify the objects to use to compute kinetic rates and // transport properties - std::unique_ptr trmix(newTransportMgr("Mix", sol->thermo().get())); - std::unique_ptr trmulti(newTransportMgr("Multi", sol->thermo().get())); - - flow.setTransport(*trmix); + sol->setTransport("mixture-averaged"); + flow.setTransport(*sol->transport()); flow.setKinetics(*sol->kinetics()); flow.setPressure(pressure); @@ -145,7 +138,8 @@ int flamespeed(double phi, bool refine_grid, int loglevel) flameSpeed_mix); // now switch to multicomponent transport - flow.setTransport(*trmulti); + sol->setTransport("multicomponent"); + flow.setTransport(*sol->transport()); flame.solve(loglevel, refine_grid); double flameSpeed_multi = flame.value(flowdomain, flow.componentIndex("velocity"),0); diff --git a/samples/cxx/gas_transport/gas_transport.cpp b/samples/cxx/gas_transport/gas_transport.cpp index 1d76016a72..fa023957d4 100644 --- a/samples/cxx/gas_transport/gas_transport.cpp +++ b/samples/cxx/gas_transport/gas_transport.cpp @@ -13,11 +13,10 @@ // This file is part of Cantera. See License.txt in the top-level directory or // at https://cantera.org/license.txt for license and copyright information. -#include "cantera/thermo.h" -#include "cantera/transport.h" -#include "cantera/base/Solution.h" +#include "cantera/core.h" #include "cantera/base/Array.h" +#include #include #include @@ -85,18 +84,17 @@ void transport_example() // Save transport properties to a file write_csv("transport_mix.csv", labels, output); - // Create a new transport manager for multicomponent properties - unique_ptr multi( - newTransportMgr("multicomponent", sol->thermo().get())); + // Switch transport manager to multicomponent properties + sol->setTransport("multicomponent"); // Get multicomponent properties at several temperatures for (int i = 0; i < ntemps; i++) { temp = 500.0 + 100.0*i; gas->setState_TP(temp, pres); output(0,i) = temp; - output(1,i) = multi->viscosity(); - output(2,i) = multi->thermalConductivity(); - multi->getThermalDiffCoeffs(&output(3,i)); + output(1,i) = sol->transport()->viscosity(); + output(2,i) = sol->transport()->thermalConductivity(); + sol->transport()->getThermalDiffCoeffs(&output(3,i)); } // Save transport properties to a file diff --git a/samples/cxx/jacobian/derivative_speed.cpp b/samples/cxx/jacobian/derivative_speed.cpp index f3b13871af..1c2589c8ff 100644 --- a/samples/cxx/jacobian/derivative_speed.cpp +++ b/samples/cxx/jacobian/derivative_speed.cpp @@ -17,9 +17,7 @@ #include #include #include -#include "cantera/base/Solution.h" -#include "cantera/thermo/IdealGasPhase.h" -#include "cantera/kinetics.h" +#include "cantera/core.h" #include "cantera/numerics/eigen_sparse.h" using namespace Cantera; diff --git a/samples/cxx/kinetics1/kinetics1.cpp b/samples/cxx/kinetics1/kinetics1.cpp index 8e445783f6..0675eee949 100644 --- a/samples/cxx/kinetics1/kinetics1.cpp +++ b/samples/cxx/kinetics1/kinetics1.cpp @@ -13,7 +13,6 @@ // at https://cantera.org/license.txt for license and copyright information. #include "cantera/zerodim.h" -#include "cantera/thermo/IdealGasPhase.h" #include "cantera/numerics/Integrator.h" #include "example_utils.h" diff --git a/samples/cxx/openmp_ignition/openmp_ignition.cpp b/samples/cxx/openmp_ignition/openmp_ignition.cpp index 9ceb7c28a1..7241d7cae4 100644 --- a/samples/cxx/openmp_ignition/openmp_ignition.cpp +++ b/samples/cxx/openmp_ignition/openmp_ignition.cpp @@ -13,7 +13,6 @@ // at https://cantera.org/license.txt for license and copyright information. #include "cantera/zerodim.h" -#include "cantera/thermo/IdealGasPhase.h" #include From 31ab3d7e6e5e90a2b614d15dad45e6f5433d81de Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Tue, 7 Jun 2022 14:45:00 -0500 Subject: [PATCH 4/4] Adopt review suggestions Co-authored-by: Ray Speth --- samples/cxx/gas_transport/gas_transport.cpp | 1 - samples/cxx/jacobian/derivative_speed.cpp | 1 - src/base/Solution.cpp | 2 -- 3 files changed, 4 deletions(-) diff --git a/samples/cxx/gas_transport/gas_transport.cpp b/samples/cxx/gas_transport/gas_transport.cpp index fa023957d4..67170bfb62 100644 --- a/samples/cxx/gas_transport/gas_transport.cpp +++ b/samples/cxx/gas_transport/gas_transport.cpp @@ -16,7 +16,6 @@ #include "cantera/core.h" #include "cantera/base/Array.h" -#include #include #include diff --git a/samples/cxx/jacobian/derivative_speed.cpp b/samples/cxx/jacobian/derivative_speed.cpp index 1c2589c8ff..e57b245868 100644 --- a/samples/cxx/jacobian/derivative_speed.cpp +++ b/samples/cxx/jacobian/derivative_speed.cpp @@ -18,7 +18,6 @@ #include #include #include "cantera/core.h" -#include "cantera/numerics/eigen_sparse.h" using namespace Cantera; diff --git a/src/base/Solution.cpp b/src/base/Solution.cpp index 6352ce257b..ec423c0bc3 100644 --- a/src/base/Solution.cpp +++ b/src/base/Solution.cpp @@ -55,8 +55,6 @@ void Solution::setTransport(const std::string& model) { if (model == "") { setTransport(shared_ptr( newDefaultTransportMgr(m_thermo.get()))); - } else if (model == "None") { - setTransport(shared_ptr(newTransportMgr("None"))); } else { setTransport(shared_ptr( newTransportMgr(model, m_thermo.get())));