Cantera  2.1.2
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 
14 #include "cantera/equil/vcs_prob.h"
17 
18 namespace VCSnonideal
19 {
21 {
22  size_t iph;
23  vcs_VolPhase* Vphase;
24 
25  std::vector<int> numPhSpecies(m_numPhases, 0);
26 
27  for (size_t kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
28  numPhSpecies[m_phaseID[kspec]]++;
29  }
30  /*
31  * Handle the special case of a single species in a phase that
32  * has been earmarked as a multispecies phase.
33  * Treat that species as a single-species phase
34  */
35  for (iph = 0; iph < m_numPhases; iph++) {
36  Vphase = m_VolPhaseList[iph];
37  Vphase->m_singleSpecies = false;
38  if (TPhInertMoles[iph] > 0.0) {
39  Vphase->setExistence(2);
40  }
41  if (numPhSpecies[iph] <= 1) {
42  if (TPhInertMoles[iph] == 0.0) {
43  Vphase->m_singleSpecies = true;
44  }
45  }
46  }
47 
48  /*
49  * Fill in some useful arrays here that have to do with the
50  * static information concerning the phase ID of species.
51  * SSPhase = Boolean indicating whether a species is in a
52  * single species phase or not.
53  */
54  for (size_t kspec = 0; kspec < m_numSpeciesTot; kspec++) {
55  iph = m_phaseID[kspec];
56  Vphase = m_VolPhaseList[iph];
57  if (Vphase->m_singleSpecies) {
58  m_SSPhase[kspec] = true;
59  } else {
60  m_SSPhase[kspec] = false;
61  }
62  }
63 }
64 
65 int VCS_SOLVE::vcs_prep_oneTime(int printLvl)
66 {
67  size_t kspec, i;
68  int retn = VCS_SUCCESS;
69  double pres, test;
70  double* aw, *sa, *sm, *ss;
71  bool modifiedSoln = false;
72  bool conv;
73 
74  m_debug_print_lvl = printLvl;
75 
76  /*
77  * Calculate the Single Species status of phases
78  * Also calculate the number of species per phase
79  */
80  vcs_SSPhase();
81 
82  /*
83  * Set an initial estimate for the number of noncomponent species
84  * equal to nspecies - nelements. This may be changed below
85  */
87  m_numRxnTot = 0;
88  } else {
90  }
93  for (i = 0; i < m_numRxnRdc; ++i) {
95  }
96 
97  for (kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
98  size_t pID = m_phaseID[kspec];
99  size_t spPhIndex = m_speciesLocalPhaseIndex[kspec];
100  vcs_VolPhase* vPhase = m_VolPhaseList[pID];
101  vcs_SpeciesProperties* spProp = vPhase->speciesProperty(spPhIndex);
102  double sz = 0.0;
103  size_t eSize = spProp->FormulaMatrixCol.size();
104  for (size_t e = 0; e < eSize; e++) {
105  sz += fabs(spProp->FormulaMatrixCol[e]);
106  }
107  if (sz > 0.0) {
108  m_spSize[kspec] = sz;
109  } else {
110  m_spSize[kspec] = 1.0;
111  }
112  }
113 
114  /* ***************************************************** */
115  /* **** DETERMINE THE NUMBER OF COMPONENTS ************* */
116  /* ***************************************************** */
117 
118  /*
119  * Obtain a valid estimate of the mole fraction. This will
120  * be used as an initial ordering vector for prioritizing
121  * which species are defined as components.
122  *
123  * If a mole number estimate was supplied from the
124  * input file, use that mole number estimate.
125  *
126  * If a solution estimate wasn't supplied from the input file,
127  * supply an initial estimate for the mole fractions
128  * based on the relative reverse ordering of the
129  * chemical potentials.
130  *
131  * For voltage unknowns, set these to zero for the moment.
132  */
133  test = -1.0e-10;
134  if (m_doEstimateEquil < 0) {
135  double sum = 0.0;
136  for (kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
138  sum += fabs(m_molNumSpecies_old[kspec]);
139  }
140  }
141  if (fabs(sum) < 1.0E-6) {
142  modifiedSoln = true;
143  if (m_pressurePA <= 0.0) {
144  pres = 1.01325E5;
145  } else {
146  pres = m_pressurePA;
147  }
148  retn = vcs_evalSS_TP(0, 0, m_temperature, pres);
149  for (kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
151  m_molNumSpecies_old[kspec] = - m_SSfeSpecies[kspec];
152  } else {
153  m_molNumSpecies_old[kspec] = 0.0;
154  }
155  }
156  }
157  test = -1.0e20;
158  }
159 
160  /*
161  * NC = number of components is in the vcs.h common block
162  * This call to BASOPT doesn't calculate the stoichiometric
163  * reaction matrix.
164  */
165  std::vector<double> awSpace(m_numSpeciesTot + (m_numElemConstraints + 2)*(m_numElemConstraints), 0.0);
166  aw = VCS_DATA_PTR(awSpace);
167  if (aw == NULL) {
168  plogf("vcs_prep_oneTime: failed to get memory: global bailout\n");
169  return VCS_NOMEMORY;
170  }
171  sa = aw + m_numSpeciesTot;
172  sm = sa + m_numElemConstraints;
173  ss = sm + (m_numElemConstraints)*(m_numElemConstraints);
174  retn = vcs_basopt(true, aw, sa, sm, ss, test, &conv);
175  if (retn != VCS_SUCCESS) {
176  plogf("vcs_prep_oneTime:");
177  plogf(" Determination of number of components failed: %d\n",
178  retn);
179  plogf(" Global Bailout!\n");
180  return retn;
181  }
182 
183  if (m_numSpeciesTot >= m_numComponents) {
184  m_numRxnTot = m_numRxnRdc = m_numSpeciesTot - m_numComponents;
185  for (i = 0; i < m_numRxnRdc; ++i) {
186  m_indexRxnToSpecies[i] = m_numComponents + i;
187  }
188  } else {
189  m_numRxnTot = m_numRxnRdc = 0;
190  }
191 
192  /*
193  * The elements might need to be rearranged.
194  */
195  awSpace.resize(m_numElemConstraints + (m_numElemConstraints + 2)*(m_numElemConstraints), 0.0);
196  aw = VCS_DATA_PTR(awSpace);
197  sa = aw + m_numElemConstraints;
198  sm = sa + m_numElemConstraints;
199  ss = sm + (m_numElemConstraints)*(m_numElemConstraints);
200  retn = vcs_elem_rearrange(aw, sa, sm, ss);
201  if (retn != VCS_SUCCESS) {
202  plogf("vcs_prep_oneTime:");
203  plogf(" Determination of element reordering failed: %d\n",
204  retn);
205  plogf(" Global Bailout!\n");
206  return retn;
207  }
208 
209  // If we mucked up the solution unknowns because they were all
210  // zero to start with, set them back to zero here
211  if (modifiedSoln) {
212  for (kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
213  m_molNumSpecies_old[kspec] = 0.0;
214  }
215  }
216  return VCS_SUCCESS;
217 }
218 
220 {
221  /*
222  * Initialize various arrays in the data to zero
223  */
224  vcs_vdzero(m_feSpecies_old, m_numSpeciesTot);
225  vcs_vdzero(m_feSpecies_new, m_numSpeciesTot);
227  vcs_dzero(&(m_deltaMolNumPhase[0][0]), m_numSpeciesTot * m_numPhases);
228  vcs_izero(&(m_phaseParticipation[0][0]), m_numSpeciesTot * m_numPhases);
229  vcs_dzero(VCS_DATA_PTR(m_deltaPhaseMoles), m_numPhases);
230  vcs_dzero(VCS_DATA_PTR(m_tPhaseMoles_new), m_numPhases);
231  /*
232  * Calculate the total number of moles in all phases.
233  */
234  vcs_tmoles();
235  return VCS_SUCCESS;
236 }
237 
239 {
240  double sum = 0.0;
241  for (size_t e = 0; e < vprob->ne; e++) {
242  sum = sum + vprob->gai[e];
243  }
244  if (sum < 1.0E-20) {
245  plogf("vcs_wellPosed: Element abundance is close to zero\n");
246  return false;
247  }
248  return true;
249 }
250 
251 }
std::vector< double > m_SSfeSpecies
Standard state chemical potentials for species K at the current temperature and pressure.
Definition: vcs_solve.h:1581
vcs_SpeciesProperties * speciesProperty(const size_t kindex)
Retrieve the kth Species structure for the species belonging to this phase.
size_t m_numRxnTot
Total number of non-component species in the problem.
Definition: vcs_solve.h:1509
size_t m_numComponents
Number of components calculated for the problem.
Definition: vcs_solve.h:1506
IntStarStar m_phaseParticipation
This is 1 if the phase, iphase, participates in the formation reaction irxn, and zero otherwise...
Definition: vcs_solve.h:1640
*std::vector< double > FormulaMatrixCol
Column of the formula matrix, comprising the element composition of the species.
void vcs_SSPhase(void)
Calculate the status of single species phases.
Definition: vcs_prep.cpp:20
#define VCS_DATA_PTR(vvv)
Points to the data in a std::vector<> object.
Definition: vcs_internal.h:24
std::vector< size_t > m_indexRxnToSpecies
Mapping between the species index for noncomponent species and the full species index.
Definition: vcs_solve.h:1825
std::vector< double > m_tPhaseMoles_new
total kmols of species in each phase in the tentative soln vector
Definition: vcs_solve.h:1723
void setExistence(const int existence)
Set the existence flag in the object.
int m_debug_print_lvl
Debug printing lvl.
Definition: vcs_solve.h:1999
double m_temperature
Temperature (Kelvin)
Definition: vcs_solve.h:1738
std::vector< double > m_feSpecies_old
Free energy vector from the start of the current iteration.
Definition: vcs_solve.h:1588
Properties of a single species.
bool vcs_wellPosed(VCS_PROB *vprob)
In this routine, we check for things that will cause the algorithm to fail.
Definition: vcs_prep.cpp:238
int vcs_prep()
Prepare the object for solution.
Definition: vcs_prep.cpp:219
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.
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:359
Phase information and Phase calculations for vcs.
Definition: vcs_VolPhase.h:97
int m_doEstimateEquil
Setting for whether to do an initial estimate.
Definition: vcs_solve.h:1609
std::vector< double > m_molNumSpecies_new
Tentative value of the mole number vector.
Definition: vcs_solve.h:1648
Internal declarations for the VCSnonideal package.
#define VCS_SUCCESS
Definition: vcs_defs.h:37
std::vector< double > m_molNumSpecies_old
Total moles of the species.
Definition: vcs_solve.h:1616
size_t m_numSpeciesTot
Total number of species in the problems.
Definition: vcs_solve.h:1497
DoubleStarStar m_deltaMolNumPhase
Change in the number of moles of phase, iphase, due to the noncomponent formation reaction...
Definition: vcs_solve.h:1636
Header for the Interface class for the vcs thermo equilibrium solver package,.
size_t m_numElemConstraints
Number of element constraints in the problem.
Definition: vcs_solve.h:1503
std::vector< vcs_VolPhase * > m_VolPhaseList
Array of Phase Structures. Length = number of phases.
Definition: vcs_solve.h:1873
size_t m_numPhases
Number of Phases in the problem.
Definition: vcs_solve.h:1530
std::vector< size_t > m_phaseID
Mapping from the species number to the phase number.
Definition: vcs_solve.h:1836
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:1597
bool m_singleSpecies
If true, this phase consists of a single species.
Definition: vcs_VolPhase.h:595
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...
double vcs_tmoles()
Calculates the total number of moles of species in all phases.
std::vector< double > TPhInertMoles
Total kmoles of inert to add to each phase.
Definition: vcs_solve.h:1759
std::vector< char > m_SSPhase
Boolean indicating whether a species belongs to a single-species phase.
Definition: vcs_solve.h:1840
Interface class for the vcs thermo equilibrium solver package, which generally describes the problem ...
Definition: vcs_prob.h:27
#define plogf
define this Cantera function to replace printf
Definition: vcs_internal.h:30
std::vector< double > gai
Element abundances for jth element.
Definition: vcs_prob.h:87
double m_pressurePA
Pressure (units are determined by m_VCS_UnitsFormat.
Definition: vcs_solve.h:1752
std::vector< int > m_speciesUnknownType
Specifies the species unknown type.
Definition: vcs_solve.h:1629
size_t ne
Number of element constraints in the equilibrium problem.
Definition: vcs_prob.h:43
std::vector< size_t > m_speciesLocalPhaseIndex
Index that keeps track of the index of the species within the local phase.
Definition: vcs_solve.h:1799
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:65
std::vector< double > m_spSize
total size of the species
Definition: vcs_solve.h:1573
size_t m_numRxnRdc
Current number of non-component species in the problem.
Definition: vcs_solve.h:1523
std::vector< double > m_deltaPhaseMoles
Change in the total moles in each phase.
Definition: vcs_solve.h:1735
size_t m_numSpeciesRdc
Current number of species in the problems.
Definition: vcs_solve.h:1516
int vcs_evalSS_TP(int ipr, int ip1, double Temp, double pres)
Definition: vcs_TP.cpp:61