Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vcs_util.cpp
Go to the documentation of this file.
1 /**
2  * @file vcs_util.cpp
3  * Internal definitions for utility functions for the VCSnonideal package
4  */
5 /*
6  * Copyright (2005) Sandia Corporation. Under the terms of
7  * Contract DE-AC04-94AL85000 with Sandia Corporation, the
8  * U.S. Government retains certain rights in this software.
9  */
10 
12 #include "cantera/equil/vcs_defs.h"
15 #include <cassert>
16 #include <cstring>
17 
18 using namespace std;
19 
20 namespace Cantera
21 {
22 
23 double vcs_l2norm(const std::vector<double> vec)
24 {
25  size_t len = vec.size();
26  if (len == 0) {
27  return 0.0;
28  }
29  double sum = 0.0;
30  std::vector<double>::const_iterator pos;
31  for (pos = vec.begin(); pos != vec.end(); ++pos) {
32  sum += (*pos) * (*pos);
33  }
34  return std::sqrt(sum / len);
35 }
36 
37 size_t vcs_optMax(const double* x, const double* xSize, size_t j, size_t n)
38 {
39  size_t largest = j;
40  double big = x[j];
41  if (xSize) {
42  assert(xSize[j] > 0.0);
43  big *= xSize[j];
44  for (size_t i = j + 1; i < n; ++i) {
45  assert(xSize[i] > 0.0);
46  if ((x[i] * xSize[i]) > big) {
47  largest = i;
48  big = x[i] * xSize[i];
49  }
50  }
51  } else {
52  for (size_t i = j + 1; i < n; ++i) {
53  if (x[i] > big) {
54  largest = i;
55  big = x[i];
56  }
57  }
58  }
59  return largest;
60 }
61 
62 int vcs_max_int(const int* vector, int length)
63 {
64  warn_deprecated("vcs_max_int", "Unused. To be removed after Cantera 2.2.");
65  int retn;
66  if (vector == NULL || length <= 0) {
67  return 0;
68  }
69  retn = vector[0];
70  for (int i = 1; i < length; i++) {
71  retn = std::max(retn, vector[i]);
72  }
73  return retn;
74 }
75 
76 double vcsUtil_gasConstant(int mu_units)
77 {
78  switch (mu_units) {
79  case VCS_UNITS_KCALMOL:
80  return GasConst_cal_mol_K * 1e-3;
81  case VCS_UNITS_UNITLESS:
82  return 1.0;
83  case VCS_UNITS_KJMOL:
84  return GasConstant * 1e-6;
85  case VCS_UNITS_KELVIN:
86  return 1.0;
87  case VCS_UNITS_MKS:
88  /* joules / kg-mol K = kg m2 / s2 kg-mol K */
89  return GasConstant;
90  default:
91  throw CanteraError("vcsUtil_gasConstant",
92  "uknown units: " + int2str(mu_units));
93  }
94 }
95 
96 const char* vcs_speciesType_string(int speciesStatus, int length)
97 {
98  switch (speciesStatus) {
100  return "Component Species";
101  case VCS_SPECIES_MAJOR:
102  return "Major Species";
103  case VCS_SPECIES_MINOR:
104  return "Minor Species";
106  if (length < 48) {
107  return "Set Zeroed-Phase";
108  } else {
109  return "Purposely Zeroed-Phase Species (not in problem)";
110  }
112  if (length < 23) {
113  return "Zeroed-MS Phase";
114  } else {
115  return "Zeroed-MS Phase Species";
116  }
118  if (length < 23) {
119  return "Zeroed-SS Phase";
120  } else {
121  return "Zeroed-SS Phase Species";
122  }
123  case VCS_SPECIES_DELETED:
124  if (length < 22) {
125  return "Deleted Species";
126  } else if (length < 40) {
127  return "Deleted-Small Species";
128  } else {
129  return "Deleted-Small Species in a MS phase";
130  }
132  if (length < 47) {
133  return "Tmp Zeroed in MS";
134  } else {
135  return "Zeroed Species in an active MS phase (tmp)";
136  }
138  if (length < 56) {
139  return "Stoich Zeroed in MS";
140  } else {
141  return "Zeroed Species in an active MS phase (Stoich Constraint)";
142  }
144  if (length < 29) {
145  return "InterfaceVoltage";
146  } else {
147  return "InterfaceVoltage Species";
148  }
149  default:
150  return "unknown species type";
151  }
152 }
153 
154 void vcs_print_stringTrunc(const char* str, size_t space, int alignment)
155 {
156  size_t ls = 0, rs = 0;
157  size_t len = strlen(str);
158  if ((len) >= space) {
159  for (size_t i = 0; i < space; i++) {
160  plogf("%c", str[i]);
161  }
162  } else {
163  if (alignment == 1) {
164  ls = space - len;
165  } else if (alignment == 2) {
166  rs = space - len;
167  } else {
168  ls = (space - len) / 2;
169  rs = space - len - ls;
170  }
171  if (ls != 0) {
172  for (size_t i = 0; i < ls; i++) {
173  plogf(" ");
174  }
175  }
176  plogf("%s", str);
177  if (rs != 0) {
178  for (size_t i = 0; i < rs; i++) {
179  plogf(" ");
180  }
181  }
182  }
183 }
184 
185 bool vcs_doubleEqual(double d1, double d2)
186 {
187  double denom = fabs(d1) + fabs(d2) + 1.0;
188  double fac = fabs(d1 - d2) / denom;
189  if (fac > 1.0E-10) {
190  return false;
191  }
192  return true;
193 }
194 
195 }
bool vcs_doubleEqual(double d1, double d2)
Simple routine to check whether two doubles are equal up to roundoff error.
Definition: vcs_util.cpp:185
std::string int2str(const int n, const std::string &fmt)
Convert an int to a string using a format converter.
Definition: stringUtils.cpp:39
int vcs_max_int(const int *vector, int length)
Returns the maximum integer in a list.
Definition: vcs_util.cpp:62
#define VCS_SPECIES_ZEROEDMS
Species lies in a multicomponent phase with concentration zero.
Definition: vcs_defs.h:150
#define VCS_SPECIES_MINOR
Species is a major species.
Definition: vcs_defs.h:135
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:78
double vcsUtil_gasConstant(int mu_units)
Returns the value of the gas constant in the units specified by parameter.
Definition: vcs_util.cpp:76
#define VCS_SPECIES_INTERFACIALVOLTAGE
Species refers to an electron in the metal.
Definition: vcs_defs.h:173
#define VCS_SPECIES_ZEROEDSS
Species is a SS phase, that is currently zeroed out.
Definition: vcs_defs.h:157
Defines and definitions within the vcs package.
#define VCS_SPECIES_ZEROEDPHASE
Species lies in a multicomponent phase that is zeroed atm.
Definition: vcs_defs.h:182
Internal declarations for the VCSnonideal package.
double vcs_l2norm(const std::vector< double > vec)
determine the l2 norm of a vector of doubles
Definition: vcs_util.cpp:23
void vcs_print_stringTrunc(const char *str, size_t space, int alignment)
Print a string within a given space limit.
Definition: vcs_util.cpp:154
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:99
#define VCS_SPECIES_ACTIVEBUTZERO
Species lies in a multicomponent phase that is active, but species concentration is zero...
Definition: vcs_defs.h:191
#define VCS_SPECIES_MAJOR
Species is a major species.
Definition: vcs_defs.h:128
#define VCS_SPECIES_STOICHZERO
Species lies in a multicomponent phase that is active, but species concentration is zero due to stoic...
Definition: vcs_defs.h:202
size_t vcs_optMax(const double *x, const double *xSize, size_t j, size_t n)
Finds the location of the maximum component in a double vector.
Definition: vcs_util.cpp:37
Contains declarations for string manipulation functions within Cantera.
const doublereal GasConst_cal_mol_K
Universal gas constant in cal/mol/K.
Definition: ct_defs.h:73
const char * vcs_speciesType_string(int speciesStatus, int length)
Returns a const char string representing the type of the species given by the first argument...
Definition: vcs_util.cpp:96
#define plogf
define this Cantera function to replace printf
Definition: vcs_internal.h:24
#define VCS_SPECIES_COMPONENT
Species is a component which can be nonzero.
Definition: vcs_defs.h:121
#define VCS_SPECIES_DELETED
Species has such a small mole fraction it is deleted even though its phase may possibly exist...
Definition: vcs_defs.h:166
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...