Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce cantera/core.h for C++ Examples #1238

Merged
merged 4 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion include/cantera/base/Solution.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ class Solution
//! Set the Kinetics object
virtual void setKinetics(shared_ptr<Kinetics> kinetics);

//! Set the Transport object
//! Set the Transport object directly
virtual void setTransport(shared_ptr<Transport> 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<ThermoPhase> thermo() {
return m_thermo;
Expand Down
17 changes: 17 additions & 0 deletions include/cantera/core.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @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/Solution.h"
#include "cantera/thermo/ThermoPhase.h"
#include "cantera/kinetics/Kinetics.h"
#include "cantera/transport/TransportBase.h"

#endif
3 changes: 3 additions & 0 deletions include/cantera/onedim.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions include/cantera/zerodim.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 3 additions & 3 deletions samples/cxx/LiC6_electrode/LiC6_electrode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
// 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 <iostream>
#include <fstream>

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<ThermoPhase> 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();
Expand Down
2 changes: 0 additions & 2 deletions samples/cxx/combustor/combustor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <fstream>

using namespace Cantera;
Expand Down
16 changes: 7 additions & 9 deletions samples/cxx/custom/custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
ischoegl marked this conversation as resolved.
Show resolved Hide resolved
#include "cantera/base/Solution.h"
#include "cantera/core.h"
#include "cantera/numerics/Integrator.h"
#include <fstream>

Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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)";
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions samples/cxx/demo/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>

// All Cantera kernel names are in namespace Cantera. You can either
Expand Down Expand Up @@ -98,7 +96,8 @@ void demoprog()

// create a transport manager for the gas that computes
// mixture-averaged properties
std::unique_ptr<Transport> tr(newTransportMgr("Mix", sol->thermo().get()));
sol->setTransport("mixture-averaged");
auto tr = sol->transport();

// print the viscosity, thermal conductivity, and diffusion
// coefficients
Expand Down
16 changes: 5 additions & 11 deletions samples/cxx/flamespeed/flamespeed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <fstream>

Expand Down Expand Up @@ -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<Transport> trmix(newTransportMgr("Mix", sol->thermo().get()));
std::unique_ptr<Transport> trmulti(newTransportMgr("Multi", sol->thermo().get()));

flow.setTransport(*trmix);
sol->setTransport("mixture-averaged");
flow.setTransport(*sol->transport());
flow.setKinetics(*sol->kinetics());
flow.setPressure(pressure);

Expand Down Expand Up @@ -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);
Expand Down
15 changes: 6 additions & 9 deletions samples/cxx/gas_transport/gas_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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/transport.h"
#include "cantera/base/Solution.h"
#include "cantera/core.h"
#include "cantera/base/Array.h"

#include <iostream>
Expand Down Expand Up @@ -85,18 +83,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<Transport> 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
Expand Down
5 changes: 1 addition & 4 deletions samples/cxx/jacobian/derivative_speed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
#include <iostream>
#include <iomanip>
#include <numeric>
#include "cantera/base/Solution.h"
#include "cantera/thermo/IdealGasPhase.h"
#include "cantera/kinetics.h"
#include "cantera/numerics/eigen_sparse.h"
#include "cantera/core.h"
ischoegl marked this conversation as resolved.
Show resolved Hide resolved

using namespace Cantera;

Expand Down
1 change: 0 additions & 1 deletion samples/cxx/kinetics1/kinetics1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 0 additions & 1 deletion samples/cxx/openmp_ignition/openmp_ignition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <omp.h>

Expand Down
22 changes: 12 additions & 10 deletions src/base/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ void Solution::setTransport(shared_ptr<Transport> transport) {
m_transport = transport;
}

void Solution::setTransport(const std::string& model) {
if (model == "") {
setTransport(shared_ptr<Transport>(
newDefaultTransportMgr(m_thermo.get())));
} else {
setTransport(shared_ptr<Transport>(
newTransportMgr(model, m_thermo.get())));
}
}

void Solution::addAdjacent(shared_ptr<Solution> adjacent) {
if (m_adjacentByName.count(adjacent->name())) {
throw CanteraError("Solution::addAdjacent",
Expand Down Expand Up @@ -257,16 +267,8 @@ shared_ptr<Solution> newSolution(const AnyMap& phaseNode,
}
sol->setKinetics(newKinetics(phases, phaseNode, rootNode));

// transport
if (transport == "") {
sol->setTransport(shared_ptr<Transport>(
newDefaultTransportMgr(sol->thermo().get())));
} else if (transport == "None") {
sol->setTransport(shared_ptr<Transport>(newTransportMgr("None")));
} else {
sol->setTransport(shared_ptr<Transport>(
newTransportMgr(transport, sol->thermo().get())));
}
// set transport model by name
sol->setTransport(transport);

// save root-level information (YAML header)
AnyMap header;
Expand Down