Skip to content

Commit

Permalink
[Base] consolidate shared_ptr access in Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingmar Schoegl committed Nov 2, 2019
1 parent bcde29d commit 8043e5b
Show file tree
Hide file tree
Showing 28 changed files with 77 additions and 92 deletions.
21 changes: 3 additions & 18 deletions include/cantera/base/Solution.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,18 @@ class Solution : public std::enable_shared_from_this<Solution>
//! Set the Transport object
void setTransport(shared_ptr<Transport> transport);

//! Accessor for the ThermoPhase object
ThermoPhase& thermo() {
return *m_thermo;
}

//! Accessor for the Kinetics object
Kinetics& kinetics() {
return *m_kinetics;
}

//! Accessor for the Transport object
Transport& transport() {
return *m_transport;
}

//! Accessor for the ThermoPhase pointer
shared_ptr<ThermoPhase> thermoPtr() {
shared_ptr<ThermoPhase> thermo() {
return m_thermo;
}

//! Accessor for the Kinetics pointer
shared_ptr<Kinetics> kineticsPtr() {
shared_ptr<Kinetics> kinetics() {
return m_kinetics;
}

//! Accessor for the Transport pointer
shared_ptr<Transport> transportPtr() {
shared_ptr<Transport> transport() {
return m_transport;
}

Expand Down
6 changes: 3 additions & 3 deletions include/cantera/kinetics/GasKinetics.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ class GasKinetics : public BulkKinetics
/**
* Return a pointer to an GasKinetics object contained in Solution.
*/
inline shared_ptr<GasKinetics> getGasKineticsPtr(shared_ptr<Solution> sol) {
auto kin = sol->kineticsPtr();
inline shared_ptr<GasKinetics> getGasKinetics(shared_ptr<Solution> sol) {
auto kin = sol->kinetics();
if (kin->kineticsType()=="Surf") {
return std::dynamic_pointer_cast<GasKinetics>(kin);
} else {
throw CanteraError("getGasKineticsPtr",
throw CanteraError("getGasKinetics",
"Incompatible kinetics");
}
}
Expand Down
6 changes: 3 additions & 3 deletions include/cantera/kinetics/InterfaceKinetics.h
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,12 @@ class InterfaceKinetics : public Kinetics
/**
* Return a pointer to an InterfaceKinetics object contained in Solution.
*/
inline shared_ptr<InterfaceKinetics> getInterfaceKineticsPtr(shared_ptr<Solution> sol) {
auto kin = sol->kineticsPtr();
inline shared_ptr<InterfaceKinetics> getInterfaceKinetics(shared_ptr<Solution> sol) {
auto kin = sol->kinetics();
if (kin->kineticsType()=="Surf") {
return std::dynamic_pointer_cast<InterfaceKinetics>(kin);
} else {
throw CanteraError("getInterfaceKineticsPtr",
throw CanteraError("getInterfaceKinetics",
"Incompatible kinetics");
}
}
Expand Down
6 changes: 3 additions & 3 deletions include/cantera/thermo/IdealGasPhase.h
Original file line number Diff line number Diff line change
Expand Up @@ -635,12 +635,12 @@ class IdealGasPhase: public ThermoPhase
/**
* Return a pointer to an IdealGasPhase object contained in Solution.
*/
inline shared_ptr<IdealGasPhase> getIdealGasPhasePtr(shared_ptr<Solution> sol) {
auto ph = sol->thermoPtr();
inline shared_ptr<IdealGasPhase> getIdealGasPhase(shared_ptr<Solution> sol) {
auto ph = sol->thermo();
if (ph->type()=="IdealGas") {
return std::dynamic_pointer_cast<IdealGasPhase>(ph);
} else {
throw CanteraError("getIdealGasPhasePtr",
throw CanteraError("getIdealGasPhase",
"Incompatible phase");
}
}
Expand Down
6 changes: 3 additions & 3 deletions include/cantera/thermo/SurfPhase.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,12 @@ class SurfPhase : public ThermoPhase
/**
* Return a pointer to an SurfPhase object contained in Solution.
*/
inline shared_ptr<SurfPhase> getSurfPhasePtr(shared_ptr<Solution> sol) {
auto ph = sol->thermoPtr();
inline shared_ptr<SurfPhase> getSurfPhase(shared_ptr<Solution> sol) {
auto ph = sol->thermo();
if (ph->type()=="Surf") {
return std::dynamic_pointer_cast<SurfPhase>(ph);
} else {
throw CanteraError("getSurfPhasePtr",
throw CanteraError("getSurfPhase",
"Incompatible phase");
}
}
Expand Down
4 changes: 2 additions & 2 deletions include/cantera/zeroD/Reactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class Reactor : public ReactorBase
}

void insert(shared_ptr<Solution> sol) {
setThermoMgr(sol->thermo());
setKineticsMgr(sol->kinetics());
setThermoMgr(*(sol->thermo()));
setKineticsMgr(*(sol->kinetics()));
}

virtual void setKineticsMgr(Kinetics& kin);
Expand Down
2 changes: 1 addition & 1 deletion include/cantera/zeroD/Reservoir.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Reservoir : public ReactorBase
}

void insert(shared_ptr<Solution> sol) {
setThermoMgr(sol->thermo());
setThermoMgr(*(sol->thermo()));
}
};

Expand Down
2 changes: 1 addition & 1 deletion samples/cxx/NASA_coeffs/NASA_coeffs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void demoprog()
writelog("\n**** Testing modifying NASA polynomial coefficients ****\n");

auto sol = newSolution("h2o2.yaml", "ohmech");
auto gas = getIdealGasPhasePtr(sol);
auto gas = getIdealGasPhase(sol);
int nsp = gas->nSpecies();

int type;
Expand Down
4 changes: 2 additions & 2 deletions samples/cxx/combustor/combustor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void runexample()
{
// use reaction mechanism GRI-Mech 3.0
auto sol = newSolution("gri30.yaml", "gri30", "None");
auto gas = getIdealGasPhasePtr(sol);
auto gas = getIdealGasPhase(sol);

// create a reservoir for the fuel inlet, and set to pure methane.
Reservoir fuel_in;
Expand All @@ -27,7 +27,7 @@ void runexample()
double fuel_mw = gas->meanMolecularWeight();

auto air = newSolution("air.cti");
double air_mw = air->thermo().meanMolecularWeight();
double air_mw = air->thermo()->meanMolecularWeight();

// create a reservoir for the air inlet
Reservoir air_in;
Expand Down
4 changes: 2 additions & 2 deletions samples/cxx/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void demoprog()
writelog("\n**** C++ Test Program ****\n");

auto sol = newSolution("h2o2.yaml", "ohmech");
auto gas = getIdealGasPhasePtr(sol);
auto gas = getIdealGasPhase(sol);
double temp = 1200.0;
double pres = OneAtm;
gas->setState_TPX(temp, pres, "H2:1, O2:1, AR:2");
Expand Down Expand Up @@ -66,7 +66,7 @@ void demoprog()

// Reaction information

auto kin = getGasKineticsPtr(sol);
auto kin = getGasKinetics(sol);
int irxns = kin->nReactions();
vector_fp qf(irxns);
vector_fp qr(irxns);
Expand Down
8 changes: 4 additions & 4 deletions samples/cxx/flamespeed/flamespeed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int flamespeed(double phi)
{
try {
auto sol = newSolution("gri30.yaml", "gri30", "None");
auto gas = getIdealGasPhasePtr(sol);
auto gas = getIdealGasPhase(sol);

double temp = 300.0; // K
double pressure = 1.0*OneAtm; //atm
Expand Down Expand Up @@ -69,11 +69,11 @@ int flamespeed(double phi)
// specify the objects to use to compute kinetic rates and
// transport properties

std::unique_ptr<Transport> trmix(newTransportMgr("Mix", sol->thermoPtr().get()));
std::unique_ptr<Transport> trmulti(newTransportMgr("Multi", sol->thermoPtr().get()));
std::unique_ptr<Transport> trmix(newTransportMgr("Mix", sol->thermo().get()));
std::unique_ptr<Transport> 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 -----------------------
Expand Down
10 changes: 5 additions & 5 deletions samples/cxx/kinetics1/kinetics1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int kinetics1(int np, void* p)

// create an ideal gas mixture that corresponds to GRI-Mech 3.0
auto sol = newSolution("gri30.yaml", "gri30", "None");
auto gas = getIdealGasPhasePtr(sol);
auto gas = getIdealGasPhase(sol);

// set the state
gas->setState_TPX(1001.0, OneAtm, "H2:2.0, O2:1.0, N2:4.0");
Expand All @@ -45,7 +45,7 @@ int kinetics1(int np, void* p)
// create a 2D array to hold the output variables,
// and store the values for the initial state
Array2D soln(nsp+4, 1);
saveSoln(0, 0.0, sol->thermo(), soln);
saveSoln(0, 0.0, *(sol->thermo()), soln);

// create a container object to run the simulation
// and add the reactor to it
Expand All @@ -58,15 +58,15 @@ int kinetics1(int np, void* p)
double tm = i*dt;
sim.advance(tm);
cout << "time = " << tm << " s" << endl;
saveSoln(tm, sol->thermo(), soln);
saveSoln(tm, *(sol->thermo()), soln);
}
clock_t t1 = clock(); // save end time


// make a Tecplot data file and an Excel spreadsheet
std::string plotTitle = "kinetics example 1: constant-pressure ignition";
plotSoln("kin1.dat", "TEC", plotTitle, sol->thermo(), soln);
plotSoln("kin1.csv", "XL", plotTitle, sol->thermo(), soln);
plotSoln("kin1.dat", "TEC", plotTitle, *(sol->thermo()), soln);
plotSoln("kin1.csv", "XL", plotTitle, *(sol->thermo()), soln);


// print final temperature and timing data
Expand Down
2 changes: 1 addition & 1 deletion samples/cxx/openmp_ignition/openmp_ignition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void run()
for (int i = 0; i < nPoints; i++) {
// Get the Cantera objects that were initialized for this thread
size_t j = omp_get_thread_num();
auto gas = getIdealGasPhasePtr(sols[j]);
auto gas = getIdealGasPhase(sols[j]);
Reactor& reactor = *reactors[j];
ReactorNet& net = *nets[j];

Expand Down
6 changes: 3 additions & 3 deletions samples/f77/demo_ftnlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ extern "C" {
string fth = string(id, lenid);
trmodel = string(transport, lentr);
_sol = newSolution(fin, fth, trmodel);
_gas = getIdealGasPhasePtr(_sol);
_kin = getGasKineticsPtr(_sol);
_trans = _sol->transportPtr();
_gas = getIdealGasPhase(_sol);
_kin = getGasKinetics(_sol);
_trans = _sol->transport();
} catch (CanteraError& err) {
handleError(err);
}
Expand Down
2 changes: 1 addition & 1 deletion src/base/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Solution::Solution() {}
// kinetics
std::vector<ThermoPhase*> phases;
for (auto & adj : adjacent) {
phases.push_back(adj->thermoPtr().get());
phases.push_back(adj->thermo().get());
}
phases.push_back(m_thermo.get());
m_kinetics = std::move(newKinetics(phases, infile, name));
Expand Down
16 changes: 8 additions & 8 deletions test/kinetics/kineticsFromYaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ TEST(Reaction, ElementaryFromYaml)
" rate-constant: [-2.70000E+13 cm^3/mol/s, 0, 355 cal/mol],"
" negative-A: true}");

auto R = newReaction(rxn, sol->kinetics());
auto R = newReaction(rxn, *(sol->kinetics()));
EXPECT_EQ(R->reactants.at("NO"), 1);
EXPECT_EQ(R->products.at("N2"), 1);
EXPECT_EQ(R->reaction_type, ELEMENTARY_RXN);
Expand All @@ -36,7 +36,7 @@ TEST(Reaction, ThreeBodyFromYaml1)
" rate-constant: [1.20000E+17 cm^6/mol^2/s, -1, 0],"
" efficiencies: {AR: 0.83, H2O: 5}}");

auto R = newReaction(rxn, sol->kinetics());
auto R = newReaction(rxn, *(sol->kinetics()));
EXPECT_EQ(R->reactants.count("M"), (size_t) 0);

auto TBR = dynamic_cast<ThreeBodyReaction&>(*R);
Expand All @@ -53,7 +53,7 @@ TEST(Reaction, ThreeBodyFromYaml2)
" type: three-body,"
" rate-constant: [1.20000E+17, -1, 0]}");

EXPECT_THROW(newReaction(rxn, sol->kinetics()), CanteraError);
EXPECT_THROW(newReaction(rxn, *(sol->kinetics())), CanteraError);
}

TEST(Reaction, FalloffFromYaml1)
Expand All @@ -67,7 +67,7 @@ TEST(Reaction, FalloffFromYaml1)
" SRI: {A: 1.1, B: 700.0, C: 1234.0, D: 56.0, E: 0.7},"
" efficiencies: {AR: 0.625}}");

auto R = newReaction(rxn, sol->kinetics());
auto R = newReaction(rxn, *(sol->kinetics()));
auto FR = dynamic_cast<FalloffReaction&>(*R);
EXPECT_DOUBLE_EQ(FR.third_body.efficiency("AR"), 0.625);
EXPECT_DOUBLE_EQ(FR.third_body.efficiency("N2"), 1.0);
Expand All @@ -84,7 +84,7 @@ TEST(Reaction, FalloffFromYaml2)
" Troe: {A: 0.562, T3: 91, T1: 5836},"
" source: somewhere}");

auto R = newReaction(rxn, sol->kinetics());
auto R = newReaction(rxn, *(sol->kinetics()));
auto FR = dynamic_cast<FalloffReaction&>(*R);
EXPECT_DOUBLE_EQ(FR.third_body.efficiency("N2"), 1.0);
EXPECT_DOUBLE_EQ(FR.third_body.efficiency("H2O"), 0.0);
Expand All @@ -109,7 +109,7 @@ TEST(Reaction, ChemicallyActivatedFromYaml)
" high-P-rate-constant: [5.88E-14, 6.721, -3022.227],"
" low-P-rate-constant: [282320.078, 1.46878, -3270.56495]}");

auto R = newReaction(rxn, sol->kinetics());
auto R = newReaction(rxn, *(sol->kinetics()));
auto CAR = dynamic_cast<ChemicallyActivatedReaction&>(*R);
EXPECT_DOUBLE_EQ(CAR.high_rate.preExponentialFactor(), 5.88e-14);
EXPECT_DOUBLE_EQ(CAR.low_rate.preExponentialFactor(), 2.82320078e2);
Expand All @@ -129,7 +129,7 @@ TEST(Reaction, PlogFromYaml)
"- {P: 1.0 atm, A: 1.230000e+04, b: 2.68, Ea: 6335.0}\n"
"- {P: 1.01325 MPa, A: 1.680000e+16, b: -0.6, Ea: 14754.0}");

auto R = newReaction(rxn, sol->kinetics());
auto R = newReaction(rxn, *(sol->kinetics()));
auto PR = dynamic_cast<PlogReaction&>(*R);
const auto& rates = PR.rate.rates();
EXPECT_EQ(rates.size(), (size_t) 4);
Expand All @@ -156,7 +156,7 @@ TEST(Reaction, ChebyshevFromYaml)
" [-2.26210e-01, 1.69190e-01, 4.85810e-03, -2.38030e-03],\n"
" [-1.43220e-01, 7.71110e-02, 1.27080e-02, -6.41540e-04]]\n");

auto R = newReaction(rxn, sol->kinetics());
auto R = newReaction(rxn, *(sol->kinetics()));
EXPECT_EQ(R->reactants.size(), (size_t) 1);
auto CR = dynamic_cast<ChebyshevReaction&>(*R);
double logP = std::log10(2e6);
Expand Down
2 changes: 1 addition & 1 deletion test_problems/ChemEquil_ionizedGas/ionizedGasEquil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main(int argc, char** argv)
#endif
try {
auto sol = newSolution("air_below6000K.cti", "air_below6000K");
auto gas = getIdealGasPhasePtr(sol);
auto gas = getIdealGasPhase(sol);

vector_fp IndVar2(6, 0.0);
IndVar2[0] = 1.5E5;
Expand Down
2 changes: 1 addition & 1 deletion test_problems/CpJump/CpJump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main(int argc, char** argv)
#endif
try {
auto sol = newSolution("bad_air.xml", "air");
auto gas = getIdealGasPhasePtr(sol);
auto gas = getIdealGasPhase(sol);
double pres = 1.0E5;
gas->setState_TPX(1000.1, pres, "O2:0.4, N2:0.6");
gas->equilibrate("TP", "auto");
Expand Down
2 changes: 1 addition & 1 deletion test_problems/cxx_ex/equil_example1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int equil_example1(int job)

// create a gas mixture, and set its state
auto sol = newSolution("silane.xml", "silane");
auto gas = getIdealGasPhasePtr(sol);
auto gas = getIdealGasPhase(sol);
size_t nsp = gas->nSpecies();

int ntemps = 50; // number of temperatures
Expand Down
10 changes: 5 additions & 5 deletions test_problems/cxx_ex/kinetics_example1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int kinetics_example1(int job)
// create an ideal gas mixture that corresponds to GRI-Mech
// 3.0
auto sol = newSolution("gri30.yaml", "gri30", "None");
auto gas = getIdealGasPhasePtr(sol);
auto gas = getIdealGasPhase(sol);

// set the state
gas->setState_TPX(1001.0, OneAtm, "H2:2.0, O2:1.0, N2:4.0");
Expand Down Expand Up @@ -81,19 +81,19 @@ int kinetics_example1(int job)
// create a 2D array to hold the output variables,
// and store the values for the initial state
Array2D soln(kk+4, 1);
saveSoln(0, 0.0, sol->thermo(), soln);
saveSoln(0, 0.0, *(sol->thermo()), soln);

// main loop
for (int i = 1; i <= nsteps; i++) {
tm = i*dt;
sim.advance(tm);
saveSoln(tm, sol->thermo(), soln);
saveSoln(tm, *(sol->thermo()), soln);
}

// make a Tecplot data file and an Excel spreadsheet
string plotTitle = "kinetics example 1: constant-pressure ignition";
plotSoln("kin1.dat", "TEC", plotTitle, sol->thermo(), soln);
plotSoln("kin1.csv", "XL", plotTitle, sol->thermo(), soln);
plotSoln("kin1.dat", "TEC", plotTitle, *(sol->thermo()), soln);
plotSoln("kin1.csv", "XL", plotTitle, *(sol->thermo()), soln);

// print final temperature
cout << " Tfinal = " << r.temperature() << endl;
Expand Down
Loading

0 comments on commit 8043e5b

Please sign in to comment.