Updated the C++ samples so that they compile and (mostly) run

This commit is contained in:
Ray Speth 2012-02-15 01:01:30 +00:00
parent 855f2ec77b
commit 46c4baeb8c
17 changed files with 78 additions and 155 deletions

View file

@ -21,7 +21,7 @@ For example, %Cantera classes that represent phases of matter have a method call
\verbatim
ThermoPhase* gas = importPhase("gas.cti");
...
int nsp = gas.nSpecies();
int nsp = gas.nSpecies();
double x[nsp];
gas.getMoleFractions(x); \endverbatim
@ -66,13 +66,13 @@ namespace DropletNamespace {
const double Tboil = 489.5; //
const double molWt = 170.337;
const double hfg = 2.56e5; // J/kg
const double rho_vapor = 0.4267;
const double rho_liquid = 749.0;
const double rho_vapor = 0.4267;
const double rho_liquid = 749.0;
class Droplet {
public:
Droplet(double D=-1.0, double T=300.0) : m_diam(D),
Droplet(double D=-1.0, double T=300.0) : m_diam(D),
m_temp(T),
m_yinf(0.0) {}
virtual ~Droplet() {}
@ -94,11 +94,11 @@ namespace DropletNamespace {
return 4.0*Pi*rho_vapor*Dab()*log(1.0+B());
}
double B() const {
double B() const {
double ys = surfaceMassFraction();
return (ys - m_yinf)/(1.0 - ys);
}
double surfaceMassFraction() const {
double surfaceMassFraction() const {
double ps = Psat();
return ps*molWt/(ps*molWt + (OneAtm - ps)*28.014);
}
@ -109,10 +109,10 @@ namespace DropletNamespace {
double lifetime() const { return m_diam*m_diam/evapConstant();}
protected:
double Dab() { return Dab_0*pow(800.0/399.0, 1.5); }
double m_diam, m_temp, m_yinf;
double m_diam, m_temp, m_yinf;
};
}

View file

@ -11,8 +11,8 @@ header files from the kernel needed to do equilibrium calculations
These headers are designed for use in C++ application programs, and
are not included by the Cantera kernel. The headers and their functions are:
These are:
- equilibrium.h
These are:
- equilibrium.h
- Chemical equilibrium.
- GRI30.h
- Provides class GRI30.

View file

@ -1,100 +0,0 @@
#include <cantera/Cantera.h>
#include <cantera/IdealGasMix.h> // defines class IdealGasMix
#include <cantera/equilibrium.h> // chemical equilibrium
#include <cantera/transport.h> // transport properties
void demoprog()
{
IdealGasMix gas("h2o2.cti","ohmech");
double temp = 1200.0;
double pres = OneAtm;
gas.setState_TPX(temp, pres, "H2:1, O2:1, AR:2");
// Thermodynamic properties
printf("\n\nInitial state:\n\n");
printf(
"Temperature: %14.5g K\n"
"Pressure: %14.5g Pa\n"
"Density: %14.5g kg/m3\n"
"Molar Enthalpy: %14.5g J/kmol\n"
"Molar Entropy: %14.5g J/kmol-K\n"
"Molar cp: %14.5g J/kmol-K\n",
gas.temperature(), gas.pressure(), gas.density(),
gas.enthalpy_mole(), gas.entropy_mole(), gas.cp_mole());
// set the gas to the equilibrium state with the same specific
// enthalpy and pressure
equilibrate(gas,"HP");
printf("\n\nEquilibrium state:\n\n");
printf(
"Temperature: %14.5g K\n"
"Pressure: %14.5g Pa\n"
"Density: %14.5g kg/m3\n"
"Molar Enthalpy: %14.5g J/kmol\n"
"Molar Entropy: %14.5g J/kmol-K\n"
"Molar cp: %14.5g J/kmol-K\n",
gas.temperature(), gas.pressure(), gas.density(),
gas.enthalpy_mole(), gas.entropy_mole(), gas.cp_mole());
// Reaction information
int irxns = gas.nReactions();
double* qf = new double[irxns];
double* qr = new double[irxns];
double* q = new double[irxns];
// since the gas has been set to an equilibrium state, the forward
// and reverse rates of progress should be equal for all
// reversible reactions, and the net rates should be zero.
gas.getFwdRatesOfProgress(qf);
gas.getRevRatesOfProgress(qr);
gas.getNetRatesOfProgress(q);
printf("\n\n");
for (int i = 0; i < irxns; i++) {
printf("%30s %14.5g %14.5g %14.5g kmol/m3/s\n",
gas.reactionString(i).c_str(), qf[i], qr[i], q[i]);
}
// transport properties
Transport* tr = newTransportMgr("Mix", &gas, 1);
printf("\n\nViscosity: %14.5g Pa-s\n", tr->viscosity());
printf("Thermal conductivity: %14.5g W/m/K\n", tr->thermalConductivity());
int nsp = gas.nSpecies();
double* diff = new double[nsp];
tr->getMixDiffCoeffs(diff);
int k;
printf("\n\n%20s %26s\n", "Species","Diffusion Coefficient");
for (k = 0; k < nsp; k++) {
printf("%20s %14.5g m2/s \n", gas.speciesName(k).c_str(), diff[k]);
}
// clean up
delete qf;
delete qr;
delete q;
delete diff;
delete tr;
}
int main()
{
try {
demoprog();
} catch (CanteraError) {
showErrors(cout);
}
}

View file

@ -2,6 +2,8 @@
#include <cantera/equilibrium.h> // chemical equilibrium
#include <cantera/transport.h> // transport properties
using namespace Cantera;
void demoprog()
{
@ -100,7 +102,7 @@ int main()
try {
demoprog();
} catch (CanteraError) {
showErrors(cout);
showErrors(std::cout);
}
}

View file

@ -1,5 +1,6 @@
#include <cantera/thermo.h>
using namespace Cantera;
// The actual code is put into a function that
// can be called from the main program.
@ -16,7 +17,7 @@ void simple_demo()
gas->setState_TPX(500.0, 2.0*OneAtm, "H2O:1.0, H2:8.0, AR:1.0");
// Print a summary report of the state of the gas
cout << report(*gas) << endl;
std::cout << report(*gas) << std::endl;
// Clean up
delete gas;

View file

@ -1,12 +1,14 @@
#include <cantera/equilibrium.h>
#include <cantera/thermo.h>
using namespace Cantera;
void equil_demo()
{
ThermoPhase* gas = newPhase("h2o2.cti","ohmech");
gas->setState_TPX(1500.0, 2.0*OneAtm, "O2:1.0, H2:3.0, AR:1.0");
equilibrate(*gas, "TP");
cout << report(*gas) << endl;
std::cout << report(*gas) << std::endl;
}
int main()

View file

@ -5,12 +5,13 @@ void demoprog()
// Calls Cantera
}
int main()
int main(int argc, char** argv)
{
try {
demoprog();
} catch (CanteraError) {
showErrors(cout);
} catch (Cantera::CanteraError) {
Cantera::showErrors(std::cout);
return 1;
}
return 0;
}

View file

@ -1,9 +1,9 @@
#include "cantera/thermo.h"
#include <iostream>
int main()
int main(int argc, char** argv)
{
ThermoPhase* gas = newPhase("h2o2.cti","ohmech");
cout << gas->temperature() << endl;
Cantera::ThermoPhase* gas = Cantera::newPhase("h2o2.cti","ohmech");
std::cout << gas->temperature() << std::endl;
return 0;
}

View file

@ -2,6 +2,9 @@
//
// artifical example of throwing and catching a CanteraError exception.
//
using namespace Cantera;
void mycode()
{
ThermoPhase* gas = newPhase("h2o2.cti","ohmech");

View file

@ -28,16 +28,16 @@ This program produces the output below:
heat capacity c_p 3919.29 2.904e+04 J/K
heat capacity c_v 2797.09 2.072e+04 J/K
X Y Chem. Pot. / RT
X Y Chem. Pot. / RT
------------- ------------ ------------
H2 0.8 0.217667 -15.6441
H 0 0
O 0 0
O2 0 0
OH 0 0
H 0 0
O 0 0
O2 0 0
OH 0 0
H2O 0.1 0.243153 -82.9531
HO2 0 0
H2O2 0 0
HO2 0 0
H2O2 0 0
AR 0.1 0.53918 -20.5027
\endverbatim

View file

@ -1,41 +1,42 @@
#include "cantera/thermo.h"
using namespace Cantera;
void thermo_demo(string file, string phase)
void thermo_demo(std::string file, std::string phase)
{
ThermoPhase* gas = newPhase(file, phase);
gas->setState_TPX(1500.0, 2.0*OneAtm, "O2:1.0, H2:3.0, AR:1.0");
// temperature, pressure, and density
cout << gas->temperature() << endl;
cout << gas->pressure() << endl;
cout << gas->density() << endl;
std::cout << gas->temperature() << std::endl;
std::cout << gas->pressure() << std::endl;
std::cout << gas->density() << std::endl;
// molar thermodynamic properties
cout << gas->enthalpy_mole() << endl;
cout << gas->entropy_mole() << endl;
std::cout << gas->enthalpy_mole() << std::endl;
std::cout << gas->entropy_mole() << std::endl;
// specific (per unit mass) thermodynamic properties
cout << gas->enthalpy_mass() << endl;
cout << gas->entropy_mass() << endl;
std::cout << gas->enthalpy_mass() << std::endl;
std::cout << gas->entropy_mass() << std::endl;
// chemical potentials of the species
int numSpecies = gas->nSpecies();
vector_fp mu(numSpecies);
gas->getChemPotentials(mu.begin());
gas->getChemPotentials(&mu[0]);
int n;
for (n = 0; n < numSpecies; n++) {
cout << gas->speciesName(n) << " " << mu[n] << endl;
std::cout << gas->speciesName(n) << " " << mu[n] << std::endl;
}
}
int main()
int main(int argc, char** argv)
{
try {
thermo_demo("h2o2.cti","ohmech");
} catch (CanteraError) {
showErrors();
return 1;
}
return 0;
}

View file

@ -10,8 +10,6 @@
using namespace std;
using namespace Cantera;
using namespace Cantera_CXX;
// The program is put into a function so that error handling code can
// be conveniently put around the whole thing. See main() below.

View file

@ -1,6 +1,7 @@
/// @file BoundaryValueProblem.h
/// Simplified interface to the capabilities provided by Cantera to
/// solve boundary value problems.
/// @todo This example cannot currently be compiled
#ifndef BVP_H
#define BVP_H

View file

@ -105,7 +105,7 @@ void runexample()
double tres;
int k;
ofstream f("combustor_cxx.csv");
std::ofstream f("combustor_cxx.csv");
while (tnow < tfinal) {
tnow = sim.step(tfinal);
@ -117,7 +117,7 @@ void runexample()
for (k = 0; k < nsp; k++) {
f << c.moleFraction(k) << ", ";
}
f << endl;
f << std::endl;
}
f.close();
}
@ -131,8 +131,8 @@ int main()
}
// handle exceptions thrown by Cantera
catch (CanteraError) {
showErrors(cout);
cout << " terminating... " << endl;
showErrors(std::cout);
std::cout << " terminating... " << std::endl;
appdelete();
return 1;
}

View file

@ -120,7 +120,7 @@ int main()
try {
demoprog();
} catch (CanteraError) {
showErrors(cout);
showErrors(std::cout);
}
}

View file

@ -1,9 +1,20 @@
#include <cantera/onedim.h>
/*!
* @file flamespeed.cpp
* C++ demo program to compute flame speeds using GRI-Mech.
*
* @todo This demo compiles but does not run correctly.
*/
#include "cantera/oneD/Sim1D.h"
#include "cantera/oneD/Inlet1D.h"
#include "cantera/oneD/StFlow.h"
#include <cantera/IdealGasMix.h>
#include <cantera/equilibrium.h>
#include <cantera/transport.h>
using namespace Cantera;
using std::cout;
using std::endl;
int flamespeed(int np, void* p)
{
@ -27,7 +38,7 @@ int flamespeed(int np, void* p)
}
if (phi == 0.0) {
cout << "Enter phi: ";
cin >> phi;
std::cin >> phi;
}
doublereal C_atoms=1.0;
@ -116,7 +127,7 @@ int flamespeed(int np, void* p)
//=================== create the container and insert the domains =====
vector<Domain1D*> domains;
std::vector<Domain1D*> domains;
domains.push_back(&inlet);
domains.push_back(&flow);
domains.push_back(&outlet);
@ -216,7 +227,7 @@ int flamespeed(int np, void* p)
flame.value(flowdomain,flow.componentIndex("u"),0) << " m/s" << endl;
int np=flow.nPoints();
vector<doublereal> zvec,Tvec,COvec,CO2vec,Uvec;
std::vector<doublereal> zvec,Tvec,COvec,CO2vec,Uvec;
printf("\n%9s\t%8s\t%5s\t%7s\n","z (m)", "T (K)", "U (m/s)", "Y(CO)");
for (int n=0; n<np; n++) {
@ -231,7 +242,7 @@ int flamespeed(int np, void* p)
cout << endl<<"Adiabatic flame temperature from equilibrium is: "<<Tad<<endl;
cout << "Flame speed for phi="<<phi<<" is "<<Uvec[0]<<" m/s."<<endl;
string reportFile = "flamespeed.csv";
std::string reportFile = "flamespeed.csv";
FILE* FP = fopen(reportFile.c_str(), "w");
if (!FP) {
printf("Failure to open file\n");
@ -250,8 +261,8 @@ int flamespeed(int np, void* p)
return 0;
} catch (CanteraError) {
showErrors(cerr);
cerr << "program terminating." << endl;
showErrors(std::cerr);
std::cerr << "program terminating." << endl;
return -1;
}
}

View file

@ -13,6 +13,9 @@
#include <time.h>
#include "example_utils.h"
using namespace Cantera;
using std::cout;
using std::endl;
int kinetics1(int np, void* p)
{
@ -85,7 +88,7 @@ int kinetics1(int np, void* p)
// make a Tecplot data file and an Excel spreadsheet
string plotTitle = "kinetics example 1: constant-pressure ignition";
std::string plotTitle = "kinetics example 1: constant-pressure ignition";
plotSoln("kin1.dat", "TEC", plotTitle, gas, soln);
plotSoln("kin1.csv", "XL", plotTitle, gas, soln);