mirror of
https://github.com/Cantera/cantera.git
synced 2025-02-25 18:55:29 -06:00
120 lines
3.4 KiB
C++
Executable File
120 lines
3.4 KiB
C++
Executable File
/////////////////////////////////////////////////////////////
|
|
//
|
|
// chemical equilibrium
|
|
//
|
|
// $Author$
|
|
// $Revision$
|
|
// $Date$
|
|
//
|
|
// copyright California Institute of Technology 2002
|
|
//
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
|
|
#include "Cantera.h"
|
|
#include <time.h>
|
|
#include "example_utils.h"
|
|
#include "equilibrium.h"
|
|
|
|
#include "IdealGasMix.h"
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// utility functions for plotting
|
|
|
|
template<class G, class V>
|
|
void makeEquilDataLabels(const G& gas, V& names) {
|
|
int nsp = gas.nSpecies();
|
|
names.resize(nsp + 2);
|
|
names[0] = "Temperature (K)";
|
|
names[1] = "Pressure (Pa)";
|
|
int k;
|
|
for (k = 0; k < nsp; k++) names[2+k] = gas.speciesName(k);
|
|
}
|
|
|
|
template<class G, class A>
|
|
void plotEquilSoln(string fname, string fmt, string title, const G& gas,
|
|
const A& soln) {
|
|
vector<string> names;
|
|
makeEquilDataLabels(gas, names);
|
|
writePlotFile(fname, fmt, title, names, soln);
|
|
}
|
|
//-----------------------------------------------------------------
|
|
|
|
|
|
// Equilibrium example. This is written as a function so that one
|
|
// driver program can run multiple examples.
|
|
// The action taken depends on input parameter job:
|
|
// job = 0: print a one-line description of the example.
|
|
// job = 1: print a longer description
|
|
// job = 2: print description, then run the example.
|
|
|
|
|
|
int equil_example1(int job) {
|
|
|
|
try {
|
|
cout << "Chemical equilibrium." << endl;
|
|
if (job > 0) {
|
|
cout << "Equilibrium composition and pressure for a "
|
|
<< "range of temperatures at constant density." << endl;
|
|
}
|
|
if (job <= 1) return 0;
|
|
|
|
// header
|
|
writeCanteraHeader(cout);
|
|
|
|
// create a gas mixture, and set its state
|
|
|
|
IdealGasMix gas("silane.xml", "silane");
|
|
int nsp = gas.nSpecies();
|
|
|
|
int ntemps = 50; // number of temperatures
|
|
Array2D output(nsp+2, ntemps);
|
|
|
|
// main loop
|
|
doublereal temp;
|
|
doublereal pres = 0.01*OneAtm;
|
|
clock_t t0 = clock();
|
|
for (int i = 0; i < ntemps; i++) {
|
|
temp = 500.0 + 50.0*i;
|
|
gas.setState_TPX(temp, pres, "SIH4:0.01, H2:1.0");
|
|
try {
|
|
equilibrate(gas,TP);
|
|
output(0,i) = temp;
|
|
output(1,i) = gas.pressure();
|
|
gas.getMoleFractions(&output(2,i));
|
|
}
|
|
catch (CanteraError) {
|
|
showErrors(cout);
|
|
}
|
|
catch (...) {
|
|
cout << "exception...." << endl;
|
|
}
|
|
}
|
|
clock_t t1 = clock();
|
|
|
|
// make a Tecplot data file and an Excel spreadsheet
|
|
string plotTitle = "equilibrium example 1: "
|
|
"chemical equilibrium";
|
|
plotEquilSoln("eq1.dat", "TEC", plotTitle, gas, output);
|
|
plotEquilSoln("eq1.csv", "XL", plotTitle, gas, output);
|
|
|
|
// print timing data
|
|
doublereal tmm = 1.0*(t1 - t0)/CLOCKS_PER_SEC;
|
|
cout << " time = " << tmm << endl << endl;
|
|
|
|
cout << "Output files:" << endl
|
|
<< " eq1.csv (Excel CSV file)" << endl
|
|
<< " eq1.dat (Tecplot data file)" << endl;
|
|
|
|
return 0;
|
|
}
|
|
|
|
// handle exceptions thrown by Cantera
|
|
catch (CanteraError) {
|
|
showErrors(cout);
|
|
cout << " terminating... " << endl;
|
|
return -1;
|
|
}
|
|
}
|