Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vcs_prep.cpp
Go to the documentation of this file.
1 /**
2  * @file vcs_prep.cpp
3  * This file contains some prepatory functions.
4  */
5 
6 /*
7  * Copyright (2005) Sandia Corporation. Under the terms of
8  * Contract DE-AC04-94AL85000 with Sandia Corporation, the
9  * U.S. Government retains certain rights in this software.
10  */
11 
13 #include "cantera/equil/vcs_prob.h"
15 
16 namespace Cantera
17 {
19 {
20  std::vector<int> numPhSpecies(m_numPhases, 0);
21 
22  for (size_t kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
23  numPhSpecies[m_phaseID[kspec]]++;
24  }
25  /*
26  * Handle the special case of a single species in a phase that
27  * has been earmarked as a multispecies phase.
28  * Treat that species as a single-species phase
29  */
30  for (size_t iph = 0; iph < m_numPhases; iph++) {
31  vcs_VolPhase* Vphase = m_VolPhaseList[iph];
32  Vphase->m_singleSpecies = false;
33  if (TPhInertMoles[iph] > 0.0) {
34  Vphase->setExistence(2);
35  }
36  if (numPhSpecies[iph] <= 1) {
37  if (TPhInertMoles[iph] == 0.0) {
38  Vphase->m_singleSpecies = true;
39  }
40  }
41  }
42 
43  /*
44  * Fill in some useful arrays here that have to do with the
45  * static information concerning the phase ID of species.
46  * SSPhase = Boolean indicating whether a species is in a
47  * single species phase or not.
48  */
49  for (size_t kspec = 0; kspec < m_numSpeciesTot; kspec++) {
50  size_t iph = m_phaseID[kspec];
51  vcs_VolPhase* Vphase = m_VolPhaseList[iph];
52  if (Vphase->m_singleSpecies) {
53  m_SSPhase[kspec] = true;
54  } else {
55  m_SSPhase[kspec] = false;
56  }
57  }
58 }
59 
60 int VCS_SOLVE::vcs_prep_oneTime(int printLvl)
61 {
62  int retn = VCS_SUCCESS;
63  m_debug_print_lvl = printLvl;
64 
65  /*
66  * Calculate the Single Species status of phases
67  * Also calculate the number of species per phase
68  */
69  vcs_SSPhase();
70 
71  /*
72  * Set an initial estimate for the number of noncomponent species
73  * equal to nspecies - nelements. This may be changed below
74  */
76  m_numRxnTot = 0;
77  } else {
79  }
82  for (size_t i = 0; i < m_numRxnRdc; ++i) {
84  }
85 
86  for (size_t kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
87  size_t pID = m_phaseID[kspec];
88  size_t spPhIndex = m_speciesLocalPhaseIndex[kspec];
89  vcs_VolPhase* vPhase = m_VolPhaseList[pID];
90  vcs_SpeciesProperties* spProp = vPhase->speciesProperty(spPhIndex);
91  double sz = 0.0;
92  size_t eSize = spProp->FormulaMatrixCol.size();
93  for (size_t e = 0; e < eSize; e++) {
94  sz += fabs(spProp->FormulaMatrixCol[e]);
95  }
96  if (sz > 0.0) {
97  m_spSize[kspec] = sz;
98  } else {
99  m_spSize[kspec] = 1.0;
100  }
101  }
102 
103  /* ***************************************************** */
104  /* **** DETERMINE THE NUMBER OF COMPONENTS ************* */
105  /* ***************************************************** */
106 
107  /*
108  * Obtain a valid estimate of the mole fraction. This will
109  * be used as an initial ordering vector for prioritizing
110  * which species are defined as components.
111  *
112  * If a mole number estimate was supplied from the
113  * input file, use that mole number estimate.
114  *
115  * If a solution estimate wasn't supplied from the input file,
116  * supply an initial estimate for the mole fractions
117  * based on the relative reverse ordering of the
118  * chemical potentials.
119  *
120  * For voltage unknowns, set these to zero for the moment.
121  */
122  double test = -1.0e-10;
123  bool modifiedSoln = false;
124  if (m_doEstimateEquil < 0) {
125  double sum = 0.0;
126  for (size_t kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
128  sum += fabs(m_molNumSpecies_old[kspec]);
129  }
130  }
131  if (fabs(sum) < 1.0E-6) {
132  modifiedSoln = true;
133  double pres = (m_pressurePA <= 0.0) ? 1.01325E5 : m_pressurePA;
134  retn = vcs_evalSS_TP(0, 0, m_temperature, pres);
135  for (size_t kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
137  m_molNumSpecies_old[kspec] = - m_SSfeSpecies[kspec];
138  } else {
139  m_molNumSpecies_old[kspec] = 0.0;
140  }
141  }
142  }
143  test = -1.0e20;
144  }
145 
146  /*
147  * NC = number of components is in the vcs.h common block
148  * This call to BASOPT doesn't calculate the stoichiometric
149  * reaction matrix.
150  */
151  std::vector<double> awSpace(m_numSpeciesTot + (m_numElemConstraints + 2)*(m_numElemConstraints), 0.0);
152  double* aw = VCS_DATA_PTR(awSpace);
153  if (aw == NULL) {
154  plogf("vcs_prep_oneTime: failed to get memory: global bailout\n");
155  return VCS_NOMEMORY;
156  }
157  double* sa = aw + m_numSpeciesTot;
158  double* sm = sa + m_numElemConstraints;
159  double* ss = sm + (m_numElemConstraints)*(m_numElemConstraints);
160  bool conv;
161  retn = vcs_basopt(true, aw, sa, sm, ss, test, &conv);
162  if (retn != VCS_SUCCESS) {
163  plogf("vcs_prep_oneTime:");
164  plogf(" Determination of number of components failed: %d\n",
165  retn);
166  plogf(" Global Bailout!\n");
167  return retn;
168  }
169 
170  if (m_numSpeciesTot >= m_numComponents) {
171  m_numRxnTot = m_numRxnRdc = m_numSpeciesTot - m_numComponents;
172  for (size_t i = 0; i < m_numRxnRdc; ++i) {
173  m_indexRxnToSpecies[i] = m_numComponents + i;
174  }
175  } else {
176  m_numRxnTot = m_numRxnRdc = 0;
177  }
178 
179  /*
180  * The elements might need to be rearranged.
181  */
182  awSpace.resize(m_numElemConstraints + (m_numElemConstraints + 2)*(m_numElemConstraints), 0.0);
183  aw = VCS_DATA_PTR(awSpace);
184  sa = aw + m_numElemConstraints;
185  sm = sa + m_numElemConstraints;
186  ss = sm + (m_numElemConstraints)*(m_numElemConstraints);
187  retn = vcs_elem_rearrange(aw, sa, sm, ss);
188  if (retn != VCS_SUCCESS) {
189  plogf("vcs_prep_oneTime:");
190  plogf(" Determination of element reordering failed: %d\n",
191  retn);
192  plogf(" Global Bailout!\n");
193  return retn;
194  }
195 
196  // If we mucked up the solution unknowns because they were all
197  // zero to start with, set them back to zero here
198  if (modifiedSoln) {
199  for (size_t kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
200  m_molNumSpecies_old[kspec] = 0.0;
201  }
202  }
203  return VCS_SUCCESS;
204 }
205 
207 {
208  /*
209  * Initialize various arrays in the data to zero
210  */
211  m_feSpecies_old.assign(m_feSpecies_old.size(), 0.0);
212  m_feSpecies_new.assign(m_feSpecies_new.size(), 0.0);
213  m_molNumSpecies_new.assign(m_molNumSpecies_new.size(), 0.0);
216  m_deltaPhaseMoles.assign(m_deltaPhaseMoles.size(), 0.0);
217  m_tPhaseMoles_new.assign(m_tPhaseMoles_new.size(), 0.0);
218  /*
219  * Calculate the total number of moles in all phases.
220  */
221  vcs_tmoles();
222  return VCS_SUCCESS;
223 }
224 
226 {
227  double sum = 0.0;
228  for (size_t e = 0; e < vprob->ne; e++) {
229  sum = sum + vprob->gai[e];
230  }
231  if (sum < 1.0E-20) {
232  plogf("vcs_wellPosed: Element abundance is close to zero\n");
233  return false;
234  }
235  return true;
236 }
237 
238 }
int vcs_elem_rearrange(double *const aw, double *const sa, double *const sm, double *const ss)
Rearrange the constraint equations represented by the Formula Matrix so that the operational ones are...
std::vector< size_t > m_phaseID
Mapping from the species number to the phase number.
Definition: vcs_solve.h:1832
bool m_singleSpecies
If true, this phase consists of a single species.
Definition: vcs_VolPhase.h:584
std::vector< double > m_feSpecies_new
Dimensionless new free energy for all the species in the mechanism at the new tentatite T...
Definition: vcs_solve.h:1593
#define VCS_DATA_PTR(vvv)
Points to the data in a std::vector<> object.
Definition: vcs_internal.h:18
std::vector< vcs_VolPhase * > m_VolPhaseList
Array of Phase Structures. Length = number of phases.
Definition: vcs_solve.h:1869
double vcs_tmoles()
Calculates the total number of moles of species in all phases.
std::vector< double > m_spSize
total size of the species
Definition: vcs_solve.h:1569
bool vcs_wellPosed(VCS_PROB *vprob)
In this routine, we check for things that will cause the algorithm to fail.
Definition: vcs_prep.cpp:225
size_t m_numRxnRdc
Current number of non-component species in the problem.
Definition: vcs_solve.h:1519
size_t m_numSpeciesRdc
Current number of species in the problems.
Definition: vcs_solve.h:1512
void setExistence(const int existence)
Set the existence flag in the object.
int vcs_prep()
Prepare the object for solution.
Definition: vcs_prep.cpp:206
std::vector< double > m_molNumSpecies_old
Total moles of the species.
Definition: vcs_solve.h:1612
Header file for the internal object that holds the vcs equilibrium problem (see Class VCS_SOLVE and E...
Header for the object representing each phase within vcs.
#define VCS_SPECIES_TYPE_MOLNUM
Unknown refers to mole number of a single species.
Definition: vcs_defs.h:343
int m_doEstimateEquil
Setting for whether to do an initial estimate.
Definition: vcs_solve.h:1605
#define VCS_SUCCESS
Definition: vcs_defs.h:21
std::vector< double > m_tPhaseMoles_new
total kmols of species in each phase in the tentative soln vector
Definition: vcs_solve.h:1719
Properties of a single species.
Header for the Interface class for the vcs thermo equilibrium solver package,.
*std::vector< double > FormulaMatrixCol
Column of the formula matrix, comprising the element composition of the species.
size_t m_numElemConstraints
Number of element constraints in the problem.
Definition: vcs_solve.h:1499
size_t m_numSpeciesTot
Total number of species in the problems.
Definition: vcs_solve.h:1493
std::vector< int > m_speciesUnknownType
Specifies the species unknown type.
Definition: vcs_solve.h:1625
std::vector< double > m_deltaPhaseMoles
Change in the total moles in each phase.
Definition: vcs_solve.h:1731
int m_debug_print_lvl
Debug printing lvl.
Definition: vcs_solve.h:1995
int vcs_evalSS_TP(int ipr, int ip1, double Temp, double pres)
Definition: vcs_TP.cpp:58
size_t m_numComponents
Number of components calculated for the problem.
Definition: vcs_solve.h:1502
std::vector< double > m_molNumSpecies_new
Tentative value of the mole number vector.
Definition: vcs_solve.h:1644
std::vector< double > m_SSfeSpecies
Standard state chemical potentials for species K at the current temperature and pressure.
Definition: vcs_solve.h:1577
int vcs_prep_oneTime(int printLvl)
This routine is mostly concerned with changing the private data to be consistent with what's needed f...
Definition: vcs_prep.cpp:60
double m_temperature
Temperature (Kelvin)
Definition: vcs_solve.h:1734
std::vector< char > m_SSPhase
Boolean indicating whether a species belongs to a single-species phase.
Definition: vcs_solve.h:1836
Phase information and Phase calculations for vcs.
Definition: vcs_VolPhase.h:86
size_t ne
Number of element constraints in the equilibrium problem.
Definition: vcs_prob.h:40
std::vector< double > gai
Element abundances for jth element.
Definition: vcs_prob.h:84
Array2D m_deltaMolNumPhase
Change in the number of moles of phase, iphase, due to the noncomponent formation reaction...
Definition: vcs_solve.h:1632
double m_pressurePA
Pressure (units are determined by m_VCS_UnitsFormat.
Definition: vcs_solve.h:1748
vcs_SpeciesProperties * speciesProperty(const size_t kindex)
Retrieve the kth Species structure for the species belonging to this phase.
void vcs_SSPhase(void)
Calculate the status of single species phases.
Definition: vcs_prep.cpp:18
int vcs_basopt(const bool doJustComponents, double aw[], double sa[], double sm[], double ss[], double test, bool *const usedZeroedSpecies)
Choose the optimum species basis for the calculations.
#define plogf
define this Cantera function to replace printf
Definition: vcs_internal.h:24
void zero()
Set all of the entries to zero.
Definition: Array.h:251
Array2D m_phaseParticipation
This is 1 if the phase, iphase, participates in the formation reaction irxn, and zero otherwise...
Definition: vcs_solve.h:1636
std::vector< size_t > m_indexRxnToSpecies
Mapping between the species index for noncomponent species and the full species index.
Definition: vcs_solve.h:1821
size_t m_numPhases
Number of Phases in the problem.
Definition: vcs_solve.h:1526
Interface class for the vcs thermo equilibrium solver package, which generally describes the problem ...
Definition: vcs_prob.h:24
size_t m_numRxnTot
Total number of non-component species in the problem.
Definition: vcs_solve.h:1505
std::vector< double > TPhInertMoles
Total kmoles of inert to add to each phase.
Definition: vcs_solve.h:1755
std::vector< double > m_feSpecies_old
Free energy vector from the start of the current iteration.
Definition: vcs_solve.h:1584
std::vector< size_t > m_speciesLocalPhaseIndex
Index that keeps track of the index of the species within the local phase.
Definition: vcs_solve.h:1795