Cantera  2.0
FlowReactor.cpp
Go to the documentation of this file.
1 /**
2 * @file FlowReactor.cpp
3 *
4 * A zero-dimensional reactor
5 */
6 
7 // Copyright 2001 California Institute of Technology
8 
10 
11 using namespace std;
12 
13 namespace Cantera
14 {
15 
16 FlowReactor::FlowReactor() : Reactor(), m_fctr(1.0e10),
17  m_speed0(0.0) {}
18 
19 // overloaded method of FuncEval. Called by the integrator to
20 // get the initial conditions.
21 void FlowReactor::getInitialConditions(double t0, size_t leny, double* y)
22 {
23  m_init = true;
24  if (m_thermo == 0) {
25  writelog("Error: reactor is empty.\n");
26  return;
27  }
28  m_time = t0;
29  m_thermo->restoreState(m_state);
30 
31  m_thermo->getMassFractions(y+2);
32 
33  y[0] = 0.0; // distance
34 
35  // set the second component to the initial speed
36  y[1] = m_speed0;
37 }
38 
39 /*
40  * Must be called before calling method 'advance'
41  */
42 void FlowReactor::initialize(doublereal t0)
43 {
44  m_thermo->restoreState(m_state);
45  m_nv = m_nsp + 2;
46  m_init = true;
47 }
48 
49 void FlowReactor::updateState(doublereal* y)
50 {
51 
52  // Set the mass fractions and density of the mixture.
53  m_dist = y[0];
54  m_speed = y[1];
55  doublereal* mss = y + 2;
56  // doublereal mass = accumulate(y+2, y+2+m_nsp, 0.0);
57  m_thermo->setMassFractions(mss);
58 
59  doublereal rho = m_rho0 * m_speed0/m_speed;
60 
61  // assumes frictionless
62  doublereal pmom = m_P0 - rho*m_speed*m_speed;
63 
64  doublereal hmom;
65  // assumes adiabatic
66  if (m_energy) {
67  hmom = m_h0 - 0.5*m_speed*m_speed;
68  m_thermo->setState_HP(hmom, pmom);
69  } else {
70  m_thermo->setState_TP(m_T, pmom);
71  }
72  m_thermo->saveState(m_state);
73 }
74 
75 
76 /*
77  * Called by the integrator to evaluate ydot given y at time 'time'.
78  */
79 void FlowReactor::evalEqs(doublereal time, doublereal* y,
80  doublereal* ydot, doublereal* params)
81 {
82  m_time = time;
83  m_thermo->restoreState(m_state);
84 
85  double mult;
86  size_t n, npar;
87 
88  // process sensitivity parameters
89  if (params) {
90  npar = nSensParams();
91  for (n = 0; n < npar; n++) {
92  mult = m_kin->multiplier(m_pnum[n]);
93  m_kin->setMultiplier(m_pnum[n], mult*params[n]);
94  }
95  }
96 
97  // distance equation
98  ydot[0] = m_speed;
99 
100  // speed equation. Set m_fctr to a large value, so that rho*u is
101  // held fixed
102  ydot[1] = m_fctr*(m_speed0 - m_thermo->density()*m_speed/m_rho0);
103 
104  /* species equations */
105  const doublereal* mw = DATA_PTR(m_thermo->molecularWeights());
106 
107  if (m_chem) {
108  m_kin->getNetProductionRates(ydot+2); // "omega dot"
109  } else {
110  fill(ydot + 2, ydot + 2 + m_nsp, 0.0);
111  }
112  doublereal rrho = 1.0/m_thermo->density();
113  for (n = 0; n < m_nsp; n++) {
114  ydot[n+2] *= mw[n]*rrho;
115  }
116 
117  // reset sensitivity parameters
118  if (params) {
119  npar = nSensParams();
120  for (n = 0; n < npar; n++) {
121  mult = m_kin->multiplier(m_pnum[n]);
122  m_kin->setMultiplier(m_pnum[n], mult/params[n]);
123  }
124  }
125 
126 
127 }
128 
129 
130 size_t FlowReactor::componentIndex(string nm) const
131 {
132  if (nm == "X") {
133  return 0;
134  }
135  if (nm == "U") {
136  return 1;
137  }
138  // check for a gas species name
139  size_t k = m_thermo->speciesIndex(nm);
140  if (k != npos) {
141  return k + 2;
142  } else {
143  return npos;
144  }
145 }
146 
147 }