Skip to content

Commit

Permalink
[samples] Update cxx samples
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Aug 25, 2024
1 parent 9a9c288 commit 6fcbd68
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 38 deletions.
58 changes: 26 additions & 32 deletions samples/cxx/combustor/combustor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,30 @@ void runexample()

// create a reservoir for the fuel inlet, and set to pure methane.
gas->setState_TPX(300.0, OneAtm, "CH4:1.0");
Reservoir fuel_in(sol);
auto fuel_in = newReservoir(sol);
double fuel_mw = gas->meanMolecularWeight();

auto air = newSolution("air.yaml", "air", "none");
double air_mw = air->thermo()->meanMolecularWeight();

// create a reservoir for the air inlet
Reservoir air_in(air);
auto air_in = newReservoir(air);

// to ignite the fuel/air mixture, we'll introduce a pulse of radicals.
// The steady-state behavior is independent of how we do this, so we'll
// just use a stream of pure atomic hydrogen.
gas->setState_TPX(300.0, OneAtm, "H:1.0");
Reservoir igniter(sol);
auto igniter = newReservoir(sol);


// create the combustor, and fill it in initially with N2
gas->setState_TPX(300.0, OneAtm, "N2:1.0");
Reactor combustor(sol);
combustor.setInitialVolume(1.0);
auto combustor = newReactor(sol);
combustor->setInitialVolume(1.0);


// create a reservoir for the exhaust. The initial composition
// doesn't matter.
Reservoir exhaust(sol);
// create a reservoir for the exhaust. The initial composition does not matter.
auto exhaust = newReservoir(sol);


// lean combustion, phi = 0.5
Expand All @@ -63,42 +62,37 @@ void runexample()
double air_mdot = factor*9.52*air_mw;
double fuel_mdot = factor*equiv_ratio*fuel_mw;

// create and install the mass flow controllers. Controllers
// m1 and m2 provide constant mass flow rates, and m3 provides
// a short Gaussian pulse only to ignite the mixture
MassFlowController m1;
m1.install(fuel_in, combustor);
m1.setMassFlowRate(fuel_mdot);
// create and install the mass flow controllers. Controllers m1 and m2
// provide constant mass flow rates, and m3 provides a short Gaussian
// pulse only to ignite the mixture.
auto m1 = newMassFlowController(fuel_in, combustor);
m1->setMassFlowRate(fuel_mdot);

// Now create the air mass flow controller. Note that this connects
// two reactors with different reaction mechanisms and different
// numbers of species. Downstream and upstream species are matched by
// name.
MassFlowController m2;
m2.install(air_in, combustor);
m2.setMassFlowRate(air_mdot);
// Now create the air mass flow controller. Note that this connects two
// reactors with different reaction mechanisms and different numbers of
// species. Downstreamand upstream species are matched by name.
auto m2 = newMassFlowController(air_in, combustor);
m2->setMassFlowRate(air_mdot);


// The igniter will use a Gaussian 'functor' object to specify the
// time-dependent igniter mass flow rate.
double A = 0.1;
double FWHM = 0.2;
double t0 = 0.5;
Gaussian1 igniter_mdot(A, t0, FWHM);
auto igniter_mdot = make_shared<Gaussian1>(A, t0, FWHM);

MassFlowController m3;
m3.install(igniter, combustor);
m3.setTimeFunction(&igniter_mdot);
auto m3 = newMassFlowController(igniter, combustor);
m3->setTimeFunction(igniter_mdot);

// put a valve on the exhaust line to regulate the pressure
Valve v;
v.install(combustor, exhaust);
auto v = newValve(combustor, exhaust);
double Kv = 1.0;
v.setValveCoeff(Kv);
v->setValveCoeff(Kv);

// the simulation only contains one reactor
ReactorNet sim;
sim.addReactor(combustor);
sim.addReactor(*combustor);

// take single steps to 6 s, writing the results to a CSV file
// for later plotting.
Expand All @@ -121,11 +115,11 @@ void runexample()
while (tnow < tfinal) {
tnow += 0.005;
sim.advance(tnow);
tres = combustor.mass()/v.massFlowRate();
tres = combustor->mass() / v->massFlowRate();
f << tnow << ", "
<< combustor.temperature() << ", "
<< combustor->temperature() << ", "
<< tres << ", ";
ThermoPhase& c = combustor.contents();
ThermoPhase& c = combustor->contents();
for (size_t i = 0; i < k_out.size(); i++) {
f << c.moleFraction(k_out[i]) << ", ";
}
Expand Down
6 changes: 3 additions & 3 deletions samples/cxx/kinetics1/kinetics1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int kinetics1(int np, void* p)
// Note that it is ok to insert the same gas object into multiple reactors
// or reservoirs. All this means is that this object will be used to evaluate
// thermodynamic or kinetic quantities needed.
IdealGasConstPressureReactor r(sol);
auto r = newIdealGasConstPressureReactor(sol);

double dt = 1.e-5; // interval at which output is written
int nsteps = 100; // number of intervals
Expand All @@ -50,7 +50,7 @@ int kinetics1(int np, void* p)
// create a container object to run the simulation
// and add the reactor to it
ReactorNet sim;
sim.addReactor(r);
sim.addReactor(*r);

// main loop
clock_t t0 = clock(); // save start time
Expand All @@ -68,7 +68,7 @@ int kinetics1(int np, void* p)

// print final temperature and timing data
double tmm = 1.0*(t1 - t0)/CLOCKS_PER_SEC;
cout << " Tfinal = " << r.temperature() << endl;
cout << " Tfinal = " << r->temperature() << endl;
cout << " time = " << tmm << endl;
cout << " number of residual function evaluations = "
<< sim.integrator().nEvals() << endl;
Expand Down
6 changes: 3 additions & 3 deletions samples/cxx/openmp_ignition/openmp_ignition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ void run()
// to have its own set of linked Cantera objects. Multiple threads accessing
// the same objects at the same time will cause errors.
vector<shared_ptr<Solution>> sols;
vector<unique_ptr<IdealGasConstPressureReactor>> reactors;
vector<shared_ptr<Reactor>> reactors;
vector<unique_ptr<ReactorNet>> nets;

// Create and link the Cantera objects for each thread. This step should be
// done in serial
for (int i = 0; i < nThreads; i++) {
auto sol = newSolution("gri30.yaml", "gri30", "none");
sols.emplace_back(sol);
reactors.emplace_back(new IdealGasConstPressureReactor(sol));
reactors.emplace_back(newIdealGasConstPressureReactor(sol));
nets.emplace_back(new ReactorNet());
nets.back()->addReactor(*reactors.back());
nets.back()->addReactor(reactors.back());
}

// Points at which to compute ignition delay time
Expand Down

0 comments on commit 6fcbd68

Please sign in to comment.