[Base] finalize introduction of Solution object in C++ layer

This commit is contained in:
Ingmar Schoegl 2019-11-05 21:28:15 -06:00 committed by Ray Speth
parent cf315f7c5e
commit 91c7bf14d7
12 changed files with 79 additions and 87 deletions

View file

@ -20,8 +20,6 @@ class Solution : public std::enable_shared_from_this<Solution>
{
private:
Solution();
Solution(const std::string& infile, std::string name, std::string transport,
std::vector<shared_ptr<Solution> > adjacent);
public:
~Solution() {}
@ -33,25 +31,6 @@ public:
return shared_ptr<Solution>( 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<Solution> create(const std::string& infile,
std::string name="",
std::string transport="",
std::vector<shared_ptr<Solution> > adjacent={}) {
return shared_ptr<Solution>( 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<ThermoPhase> thermo);
void setThermo(shared_ptr<ThermoPhase> thermo);
//! Set the Kinetics object
void setKinetics(shared_ptr<Kinetics> kinetics);
@ -88,14 +67,22 @@ protected:
shared_ptr<Transport> 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<Solution> newSolution(const std::string& infile, std::string name="",
std::string transport="",
std::vector<shared_ptr<Solution> > adjacent={}) {
return Solution::create(infile, name, transport, adjacent);
}
shared_ptr<Solution> newSolution(const std::string& infile,
const std::string& name="",
const std::string& transport="",
const std::vector<shared_ptr<Solution>>& adjacent={});
}
#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

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

View file

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

View file

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

View file

@ -72,7 +72,7 @@ int flamespeed(double phi)
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

@ -41,15 +41,15 @@ using std::string;
static shared_ptr<Solution> _sol = NULL;
// store a pointer to the thermophase object
static shared_ptr<IdealGasPhase> _gas = NULL;
shared_ptr<IdealGasPhase> _gasptr()
static shared_ptr<ThermoPhase> _gas = NULL;
shared_ptr<ThermoPhase> _gasptr()
{
return _gas;
}
// store a pointer to the kinetics object
static shared_ptr<GasKinetics> _kin = NULL;
shared_ptr<GasKinetics> _kinptr()
static shared_ptr<Kinetics> _kin = NULL;
shared_ptr<Kinetics> _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<IdealGasPhase>(_sol->thermo());
_kin = std::dynamic_pointer_cast<GasKinetics>(_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, ' ');

View file

@ -19,28 +19,6 @@ namespace Cantera
Solution::Solution() {}
Solution::Solution(const std::string& infile, std::string name, std::string transport,
std::vector<shared_ptr<Solution> > adjacent) {
// thermo phase
m_thermo = shared_ptr<ThermoPhase>(newPhase(infile, name));
// kinetics
std::vector<ThermoPhase*> 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<Transport>(newDefaultTransportMgr(m_thermo.get()));
} else if (transport!="None") {
m_transport = shared_ptr<Transport>(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<ThermoPhase> thermo) {
void Solution::setThermo(shared_ptr<ThermoPhase> thermo) {
m_thermo = thermo;
if (m_thermo) {
m_thermo->setRoot(shared_from_this());
@ -80,4 +58,33 @@ void Solution::setTransport(shared_ptr<Transport> transport) {
}
}
shared_ptr<Solution> newSolution(const std::string& infile,
const std::string& name,
const std::string& transport,
const std::vector<shared_ptr<Solution>>& adjacent) {
// instantiate Solution object
auto sol = Solution::create();
// thermo phase
sol->setThermo(shared_ptr<ThermoPhase>(newPhase(infile, name)));
// kinetics
std::vector<ThermoPhase*> 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<Transport>(newDefaultTransportMgr(sol->thermo().get())));
} else if (transport != "None") {
sol->setTransport(shared_ptr<Transport>(newTransportMgr(transport, sol->thermo().get())));
}
return sol;
}
} // namespace Cantera

View file

@ -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<IdealGasPhase*>(ph);
} else {
throw CanteraError("StFlow::StFlow",
"Unsupported phase type: need 'IdealGasPhase'");

View file

@ -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<MixTransport>(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,7 +194,7 @@ int main(int argc, char** argv)
Array2D fluxes(nsp, 2, 0.0);
tranMix->getSpeciesFluxes(2, grad_T.data(), nsp,
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;

View file

@ -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<MultiTransport>(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,7 +198,7 @@ int main(int argc, char** argv)
}
Array2D fluxes(nsp, 2, 0.0);
tranMix->getSpeciesFluxes(2, grad_T.data(), nsp,
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;
@ -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));