[Base] consolidate shared_ptr access in Solution

This commit is contained in:
Ingmar Schoegl 2019-11-02 16:42:25 -05:00 committed by Ray Speth
parent 58fc8f770c
commit 2a9554c134
28 changed files with 65 additions and 133 deletions

View file

@ -67,33 +67,18 @@ public:
//! 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;
}

View file

@ -119,19 +119,6 @@ protected:
void updateKc();
};
/**
* Return a pointer to an GasKinetics object contained in Solution.
*/
inline shared_ptr<GasKinetics> getGasKineticsPtr(shared_ptr<Solution> sol) {
auto kin = sol->kineticsPtr();
if (kin->kineticsType()=="Surf") {
return std::dynamic_pointer_cast<GasKinetics>(kin);
} else {
throw CanteraError("getGasKineticsPtr",
"Incompatible kinetics");
}
}
}
#endif

View file

@ -679,19 +679,6 @@ protected:
size_t m_nDim;
};
/**
* Return a pointer to an InterfaceKinetics object contained in Solution.
*/
inline shared_ptr<InterfaceKinetics> getInterfaceKineticsPtr(shared_ptr<Solution> sol) {
auto kin = sol->kineticsPtr();
if (kin->kineticsType()=="Surf") {
return std::dynamic_pointer_cast<InterfaceKinetics>(kin);
} else {
throw CanteraError("getInterfaceKineticsPtr",
"Incompatible kinetics");
}
}
}
#endif

View file

@ -632,19 +632,6 @@ private:
void _updateThermo() const;
};
/**
* Return a pointer to an IdealGasPhase object contained in Solution.
*/
inline shared_ptr<IdealGasPhase> getIdealGasPhasePtr(shared_ptr<Solution> sol) {
auto ph = sol->thermoPtr();
if (ph->type()=="IdealGas") {
return std::dynamic_pointer_cast<IdealGasPhase>(ph);
} else {
throw CanteraError("getIdealGasPhasePtr",
"Incompatible phase");
}
}
}
#endif

View file

@ -448,19 +448,6 @@ private:
void _updateThermo(bool force=false) const;
};
/**
* Return a pointer to an SurfPhase object contained in Solution.
*/
inline shared_ptr<SurfPhase> getSurfPhasePtr(shared_ptr<Solution> sol) {
auto ph = sol->thermoPtr();
if (ph->type()=="Surf") {
return std::dynamic_pointer_cast<SurfPhase>(ph);
} else {
throw CanteraError("getSurfPhasePtr",
"Incompatible phase");
}
}
}
#endif

View file

@ -65,8 +65,8 @@ public:
}
void insert(shared_ptr<Solution> sol) {
setThermoMgr(sol->thermo());
setKineticsMgr(sol->kinetics());
setThermoMgr(*(sol->thermo()));
setKineticsMgr(*(sol->kinetics()));
}
virtual void setKineticsMgr(Kinetics& kin);

View file

@ -37,7 +37,7 @@ public:
}
void insert(shared_ptr<Solution> sol) {
setThermoMgr(sol->thermo());
setThermoMgr(*(sol->thermo()));
}
};

View file

@ -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 = sol->thermo();
int nsp = gas->nSpecies();
int type;

View file

@ -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 = sol->thermo();
// create a reservoir for the fuel inlet, and set to pure methane.
Reservoir fuel_in;
@ -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;

View file

@ -29,7 +29,7 @@ void demoprog()
writelog("\n**** C++ Test Program ****\n");
auto sol = newSolution("h2o2.yaml", "ohmech");
auto gas = getIdealGasPhasePtr(sol);
auto gas = sol->thermo();
double temp = 1200.0;
double pres = OneAtm;
gas->setState_TPX(temp, pres, "H2:1, O2:1, AR:2");
@ -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);

View file

@ -17,8 +17,7 @@ int flamespeed(double phi)
{
try {
auto sol = newSolution("gri30.yaml", "gri30", "None");
auto gas = getIdealGasPhasePtr(sol);
auto gas = std::dynamic_pointer_cast<IdealGasPhase>(sol->thermo());
double temp = 300.0; // K
double pressure = 1.0*OneAtm; //atm
double uin = 0.3; //m/sec
@ -69,11 +68,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 -----------------------

View file

@ -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 = sol->thermo();
// set the state
gas->setState_TPX(1001.0, OneAtm, "H2:2.0, O2:1.0, N2:4.0");
@ -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
@ -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

View file

@ -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 = sols[j]->thermo();
Reactor& reactor = *reactors[j];
ReactorNet& net = *nets[j];

View file

@ -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 = std::dynamic_pointer_cast<IdealGasPhase>(_sol->thermo());
_kin = std::dynamic_pointer_cast<GasKinetics>(_sol->kinetics());
_trans = _sol->transport();
} catch (CanteraError& err) {
handleError(err);
}

View file

@ -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));

View file

@ -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);
@ -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);
@ -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)
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);

View file

@ -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 = sol->thermo();
vector_fp IndVar2(6, 0.0);
IndVar2[0] = 1.5E5;

View file

@ -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 = sol->thermo();
double pres = 1.0E5;
gas->setState_TPX(1000.1, pres, "O2:0.4, N2:0.6");
gas->equilibrate("TP", "auto");

View file

@ -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 = sol->thermo();
size_t nsp = gas->nSpecies();
int ntemps = 50; // number of temperatures

View file

@ -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 = sol->thermo();
// set the state
gas->setState_TPX(1001.0, OneAtm, "H2:2.0, O2:1.0, N2:4.0");
@ -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;

View file

@ -45,7 +45,7 @@ int kinetics_example3(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 = sol->thermo();
// set the state
gas->setState_TPX(1001.0, OneAtm, "H2:2.0, O2:1.0, N2:4.0");
@ -79,19 +79,19 @@ int kinetics_example3(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
std::string plotTitle = "kinetics example 3: constant-pressure ignition";
plotSoln("kin3.dat", "TEC", plotTitle, sol->thermo(), soln);
plotSoln("kin3.csv", "XL", plotTitle, sol->thermo(), soln);
plotSoln("kin3.dat", "TEC", plotTitle, *(sol->thermo()), soln);
plotSoln("kin3.csv", "XL", plotTitle, *(sol->thermo()), soln);
// print final temperature

View file

@ -89,7 +89,7 @@ int rxnpath_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 = sol->thermo();
gas->setState_TPX(1001.0, OneAtm, "H2:2.0, O2:1.0, N2:4.0");
// create a reactor
@ -124,13 +124,13 @@ int rxnpath_example1(int job)
ReactionPathBuilder b;
std::ofstream rplog("rp1.log"); // log file
std::ofstream rplot("rp1.dot"); // output file
b.init(rplog, sol->kinetics()); // initialize
b.init(rplog, *(sol->kinetics())); // initialize
// main loop
for (int i = 1; i <= nsteps; i++) {
tm = i*dt;
sim.advance(tm);
writeRxnPathDiagram(tm, b, sol->kinetics(), rplog, rplot);
writeRxnPathDiagram(tm, b, *(sol->kinetics()), rplog, rplot);
}
// print final temperature

View file

@ -31,7 +31,7 @@ int transport_example1(int job)
// create a gas mixture, and set its state
auto sol = newSolution("gri30.yaml", "gri30", "Mix");
auto gas = getIdealGasPhasePtr(sol);
auto gas = sol->thermo();
double temp = 500.0;
double pres = 2.0*OneAtm;
gas->setState_TPX(temp, pres, "H2:1.0, CH4:0.1");
@ -39,7 +39,7 @@ int transport_example1(int job)
// create a transport manager that implements
// mixture-averaged transport properties
auto tr = sol->transportPtr();
auto tr = sol->transport();
size_t nsp = gas->nSpecies();
@ -61,8 +61,8 @@ int transport_example1(int job)
// make a Tecplot data file and an Excel spreadsheet
std::string plotTitle = "transport example 1: "
"mixture-averaged transport properties";
plotTransportSoln("tr1.dat", "TEC", plotTitle, sol->thermo(), output);
plotTransportSoln("tr1.csv", "XL", plotTitle, sol->thermo(), output);
plotTransportSoln("tr1.dat", "TEC", plotTitle, *(sol->thermo()), output);
plotTransportSoln("tr1.csv", "XL", plotTitle, *(sol->thermo()), output);
// print final temperature
cout << "Output files:" << endl

View file

@ -31,7 +31,7 @@ int transport_example2(int job)
// create a gas mixture, and set its state
auto sol = newSolution("gri30.yaml", "gri30", "Multi");
auto gas = getIdealGasPhasePtr(sol);
auto gas = sol->thermo();
double temp = 2000.0;
double pres = 2.0*OneAtm;
gas->setState_TPX(temp, pres, "H2:1.0, O2:0.5, CH4:0.1, N2:0.2");
@ -40,7 +40,7 @@ int transport_example2(int job)
// create a transport manager that implements
// multicomponent transport properties
auto tr = sol->transportPtr();
auto tr = sol->transport();
size_t nsp = gas->nSpecies();
// create a 2D array to hold the outputs
@ -60,8 +60,8 @@ int transport_example2(int job)
// make a Tecplot data file and an Excel spreadsheet
std::string plotTitle = "transport example 2: "
"multicomponent transport properties";
plotTransportSoln("tr2.dat", "TEC", plotTitle, sol->thermo(), output);
plotTransportSoln("tr2.csv", "XL", plotTitle, sol->thermo(), output);
plotTransportSoln("tr2.dat", "TEC", plotTitle, *(sol->thermo()), output);
plotTransportSoln("tr2.csv", "XL", plotTitle, *(sol->thermo()), output);
// print final temperature
cout << "Output files:" << endl

View file

@ -37,7 +37,7 @@ int main(int argc, char** argv)
try {
auto sol = newSolution("gri30.yaml", "gri30", "Mix");
auto gas = getIdealGasPhasePtr(sol);
auto gas = sol->thermo();
size_t nsp = gas->nSpecies();
double pres = 1.0E5;
vector_fp Xset(nsp, 0.0);
@ -133,7 +133,7 @@ int main(int argc, char** argv)
grad_T[0] = (T2 - T1) / dist;
grad_T[1] = (T3 - T1) / dist;
auto tran = sol->transportPtr();
auto tran = sol->transport();
auto tranMix = dynamic_pointer_cast<MixTransport>(tran);
gas->setState_TPX(1500.0, pres, Xset.data());

View file

@ -45,7 +45,7 @@ int main(int argc, char** argv)
try {
auto sol = newSolution("gri30.yaml", "gri30", "Multi");
auto gas = getIdealGasPhasePtr(sol);
auto gas = sol->thermo();
size_t nsp = gas->nSpecies();
double pres = 1.0E5;
vector_fp Xset(nsp, 0.0);
@ -141,7 +141,7 @@ int main(int argc, char** argv)
grad_T[0] = (T2 - T1) / dist;
grad_T[1] = (T3 - T1) / dist;
auto tran = sol->transportPtr();
auto tran = sol->transport();
auto tranMix = dynamic_pointer_cast<MultiTransport>(tran);
gas->setState_TPX(1500.0, pres, Xset.data());
vector_fp mixDiffs(nsp, 0.0);

View file

@ -15,7 +15,7 @@ int main(int argc, char** argv)
#endif
try {
auto sol = newSolution("silane.xml", "silane");
auto gas = getIdealGasPhasePtr(sol);
auto gas = sol->thermo();
gas->setState_TPX(1500.0, 100.0, "SIH4:0.01, H2:0.99");
gas->equilibrate("TP");
return 0;

View file

@ -19,13 +19,13 @@ int main()
{
try {
auto sol = newSolution("gri30.yaml", "gri30");
auto gas = getIdealGasPhasePtr(sol);
auto gas = sol->thermo();
gas->setState_TPX(1200.0, OneAtm,
"H2:2, O2:1, OH:0.01, H:0.01, O:0.01");
auto surf = newSolution("surface.xml", "surface", "None", {sol});
auto surf_ph = getSurfPhasePtr(surf);
auto surf_kin = getInterfaceKineticsPtr(surf);
auto surf_ph = std::dynamic_pointer_cast<SurfPhase>(surf->thermo());
auto surf_kin = std::dynamic_pointer_cast<InterfaceKinetics>(surf->kinetics());
vector_fp cov { 0.8, 0.2 };
cout.precision(4);