19 void vcs_dzero(
double* vector,
int length)
22 for (i = 0; i < length; i++) {
29 void vcs_izero(
int* vector,
int length)
32 for (i = 0; i < length; i++) {
39 void vcs_dcopy(
double*
const vec_to,
const double*
const vec_from,
int length)
42 for (i = 0; i < length; i++) {
43 vec_to[i] = vec_from[i];
49 void vcs_icopy(
int* vec_to,
int* vec_from,
int length)
52 for (i = 0; i < length; i++) {
53 vec_to[i] = vec_from[i];
59 void vcs_vdzero(std::vector<double> &vvv,
int len)
62 std::fill(vvv.begin(), vvv.end(), 0.0);
64 std::fill_n(vvv.begin(), len, 0.0);
71 size_t len = vec.size();
76 std::vector<double>::const_iterator pos;
77 for (pos = vec.begin(); pos != vec.end(); ++pos) {
78 sum += (*pos) * (*pos);
80 return std::sqrt(sum / len);
84 void vcs_vizero(std::vector<int> &vvv,
int len)
87 std::fill(vvv.begin(), vvv.end(), 0.0);
89 std::fill_n(vvv.begin(), len, 0.0);
95 void vcs_vdcopy(std::vector<double> &vec_to,
96 const std::vector<double> & vec_from,
int length)
98 std::copy(vec_from.begin(), vec_from.begin() + length, vec_to.begin());
104 const std::vector<int> & vec_from,
int length)
106 std::copy(vec_from.begin(), vec_from.begin() + length, vec_to.begin());
110 size_t vcs_optMax(
const double* x,
const double* xSize,
size_t j,
size_t n)
116 assert(xSize[j] > 0.0);
118 for (i = j + 1; i < n; ++i) {
119 assert(xSize[i] > 0.0);
120 if ((x[i] * xSize[i]) > big) {
122 big = x[i] * xSize[i];
126 for (i = j + 1; i < n; ++i) {
139 if (vector == NULL || length <= 0) {
143 for (i = 1; i < length; i++) {
144 retn = std::max(retn, vector[i]);
150 static void mlequ_matrixDump(
double* c,
int idem,
int n)
153 printf(
"vcsUtil_mlequ() MATRIX DUMP --------------------------------------------------\n");
155 for (j = 0; j < n; ++j) {
159 for (j = 0; j < n; ++j) {
160 printf(
"-----------");
163 for (i = 0; i < n; ++i) {
164 printf(
" %3d | ", i);
165 for (j = 0; j < n; ++j) {
166 printf(
"% 10.3e ", c[i + j * idem]);
170 for (j = 0; j < n; ++j) {
171 printf(
"-----------");
174 printf(
"vcsUtil_mlequ() END MATRIX DUMP --------------------------------------------------\n");
190 size_t m,
size_t irowa,
size_t irowb)
192 if (irowa == irowb) {
195 for (
size_t j = 0; j < n; j++) {
196 std::swap(c[irowa + j * idem], c[irowb + j * idem]);
198 for (
size_t j = 0; j < m; j++) {
199 std::swap(b[irowa + j * idem], b[irowb + j * idem]);
215 std::vector<int> irowUsed(n, 0);
217 for (j = 0; j < n; j++) {
219 size_t inonzero =
npos;
220 for (
size_t i = 0; i < n; i++) {
221 if (c[i + j * idem] != 0.0) {
226 if (numNonzero == 1) {
228 if (irowUsed[inonzero] == 0) {
239 for (j = 0; j < n; j++) {
240 if (c[j + j * idem] == 0.0) {
242 size_t inonzero =
npos;
243 for (
size_t i = 0; i < n; i++) {
245 if (c[i + j * idem] != 0.0) {
246 if ((c[i + i * idem] == 0.0)
247 || (c[j + i * idem] != 0.0)) {
254 if (numNonzero == 1) {
256 if (irowUsed[inonzero] == 0) {
268 for (j = 0; j < n; j++) {
269 if (c[j + j * idem] == 0.0) {
271 size_t inonzero =
npos;
272 for (
size_t i = 0; i < n; i++) {
274 if (c[i + j * idem] != 0.0) {
275 if ((c[i + i * idem] == 0.0)
276 || (c[j + i * idem] != 0.0)) {
283 if (inonzero !=
npos) {
285 if (irowUsed[inonzero] == 0) {
306 static int s_numCalls = 0;
311 if (n > idem || n <= 0) {
312 plogf(
"vcsUtil_mlequ ERROR: badly dimensioned matrix: %d %d\n", n, idem);
318 for (
size_t i = 0; i < n; ++i) {
319 bool notFound =
true;
320 for (
size_t j = 0; j < n; ++j) {
321 if (c[i + j * idem] != 0.0) {
326 printf(
" vcsUtil_mlequ ERROR(): row %d is identically zero\n", i);
329 for (
size_t j = 0; j < n; ++j) {
330 bool notFound =
true;
331 for (
size_t i = 0; i < n; ++i) {
332 if (c[i + j * idem] != 0.0) {
337 printf(
" vcsUtil_mlequ ERROR(): column %d is identically zero\n", j);
346 mlequ_matrixDump(c, idem, n);
355 for (
size_t i = 0; i < n; ++i) {
356 if (c[i + i * idem] == 0.0) {
360 for (k = i + 1; k < n; ++k) {
361 if (c[k + i * idem] != 0.0) {
365 plogf(
"vcsUtil_mlequ ERROR: Encountered a zero column: %d\n", i);
367 plogf(
" call # %d\n", s_numCalls);
370 mlequ_matrixDump(c, idem, n);
375 for (
size_t j = 0; j < n; ++j) {
376 c[i + j * idem] += c[k + j * idem];
378 for (
size_t j = 0; j < m; ++j) {
379 b[i + j * idem] += b[k + j * idem];
383 for (
size_t l = 0; l < n; ++l) {
384 if (l != i && c[l + i * idem] != 0.0) {
385 R = c[l + i * idem] / c[i + i * idem];
386 c[l + i * idem] = 0.0;
387 for (
size_t j = i + 1; j < n; ++j) {
388 c[l + j * idem] -= c[i + j * idem] * R;
390 for (
size_t j = 0; j < m; ++j) {
391 b[l + j * idem] -= b[i + j * idem] * R;
400 for (
size_t i = 0; i < n; ++i) {
401 for (
size_t j = 0; j < m; ++j) {
402 b[i + j * idem] = -b[i + j * idem] / c[i + i * idem];
410 size_t i, j, k, l, ll;
413 bool needInverse =
false;
416 static int s_numCalls = 0;
431 std::vector<size_t> indxc(n);
432 std::vector<size_t> indxr(n);
433 std::vector<int> ipiv(n, 0);
434 doublereal big = 0.0;
438 for (i = 0; i < n; i++) {
440 for (j = 0; j < n; j++) {
442 for (k = 0; k < n; k++) {
444 if (fabs(c[j + idem * k]) >= big) {
445 big = fabs(c[j + idem * k]);
459 if (c[icol + idem * icol] == 0.0) {
460 plogf(
"vcsUtil_gaussj ERROR: Encountered a zero column: %d\n", i);
463 pivinv = 1.0 / c[icol + idem * icol];
464 c[icol + idem * icol] = 1.0;
465 for (l = 0; l < n; l++) {
466 c[icol + idem * l] *= pivinv;
468 for (l = 0; l < m; l++) {
469 b[icol + idem * l] *= pivinv;
471 for (ll = 0; ll < n; ll++) {
473 double dum = c[ll + idem * icol];
474 c[ll + idem * icol] = 0;
475 for (l = 0; l < n; l++) {
476 c[ll + idem * l] -= c[icol + idem * l] * dum;
478 for (l = 0; l < m; l++) {
479 b[ll + idem * l] -= b[icol + idem * l] * dum;
485 for (l = n - 1; l !=
npos; l--) {
486 if (indxr[l] != indxc[l]) {
487 for (k = 0; k < n; k++) {
488 std::swap(c[k + idem * indxr[l]], c[k + idem * indxr[l]]);
498 for (i = 0; i < n; ++i) {
499 for (j = 0; j < m; ++j) {
500 b[i + j * idem] = -b[i + j * idem];
510 case VCS_UNITS_KCALMOL:
513 case VCS_UNITS_UNITLESS:
516 case VCS_UNITS_KJMOL:
519 case VCS_UNITS_KELVIN:
527 plogf(
"vcs_gasConstant error: uknown units: %d\n",
537 for (
int j = 0; j < num; j++) {
547 switch (speciesStatus) {
549 sss =
"Component Species";
552 sss =
"Major Species";
555 sss =
"Minor Species";
559 sss =
"Set Zeroed-Phase";
561 sss =
"Purposely Zeroed-Phase Species (not in problem)";
566 sss =
"Zeroed-MS Phase";
568 sss =
"Zeroed-MS Phase Species";
573 sss =
"Zeroed-SS Phase";
575 sss =
"Zeroed-SS Phase Species";
580 sss =
"Deleted Species";
581 }
else if (length < 40) {
582 sss =
"Deleted-Small Species";
584 sss =
"Deleted-Small Species in a MS phase";
589 sss =
"Tmp Zeroed in MS";
591 sss =
"Zeroed Species in an active MS phase (tmp)";
596 sss =
"Stoich Zeroed in MS";
598 sss =
"Zeroed Species in an active MS phase (Stoich Constraint)";
603 sss =
"InterfaceVoltage";
605 sss =
"InterfaceVoltage Species";
609 sss =
"unknown species type";
616 size_t i, ls = 0, rs = 0;
617 size_t len = strlen(str);
618 if ((len) >= space) {
619 for (i = 0; i < space; i++) {
623 if (alignment == 1) {
625 }
else if (alignment == 2) {
628 ls = (space - len) / 2;
629 rs = space - len - ls;
632 for (i = 0; i < ls; i++) {
638 for (i = 0; i < rs; i++) {
647 double denom = fabs(d1) + fabs(d2) + 1.0;
648 double fac = fabs(d1 - d2) / denom;
684 if (x[j] < x[j + 1]) {
702 std::vector<int> xordered(x);
704 int lastV = x[0] - 1;
705 xOrderedUnique.clear();
706 for (
int i = 0; i < (int) xordered.size(); i++) {
707 if (lastV != xordered[i]) {
708 xOrderedUnique.push_back(xordered[i]);
void vcs_print_stringTrunc(const char *str, size_t space, int alignment)
Print a string within a given space limit.
double vcsUtil_gasConstant(int mu_units)
Returns the value of the gas constant in the units specified by parameter.
#define VCS_SPECIES_ZEROEDMS
Species lies in a multicomponent phase with concentration zero.
#define VCS_SPECIES_MINOR
Species is a major species.
const size_t npos
index returned by functions to indicate "no position"
static void vcsUtil_swapRows(double *c, size_t idem, size_t n, double *b, size_t m, size_t irowa, size_t irowb)
Swap rows in the c matrix and the b rhs matrix.
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...
#define VCS_SPECIES_INTERFACIALVOLTAGE
Species refers to an electron in the metal.
#define VCS_SPECIES_ZEROEDSS
Species is a SS phase, that is currently zeroed out.
#define VCS_SPECIES_ZEROEDPHASE
Species lies in a multicomponent phase that is zeroed atm.
Internal declarations for the VCSnonideal package.
void vcs_orderedUnique(std::vector< int > &xOrderedUnique, const std::vector< int > &x)
Sorts a vector of ints and eliminates duplicates from the resulting list.
void vcs_print_line(const char *string, int num)
Prints a line consisting of multiple occurrences of the same string.
int vcsUtil_mlequ(double *c, size_t idem, size_t n, double *b, size_t m)
Invert an n x n matrix and solve m rhs's.
static void vcsUtil_mlequ_preprocess(double *c, size_t idem, size_t n, double *b, size_t m)
Swap rows in the c matrix and the b rhs matrix to lower the condition number of the matrix...
void vcs_heapsort(std::vector< int > &x)
Sorts a vector of ints in place from lowest to the highest values.
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.
#define VCS_SPECIES_ACTIVEBUTZERO
Species lies in a multicomponent phase that is active, but species concentration is zero...
#define VCS_SPECIES_MAJOR
Species is a major species.
void vcs_vicopy(std::vector< int > &vec_to, const std::vector< int > &vec_from, int length)
Copy one std integer vector into another.
#define VCS_SPECIES_STOICHZERO
Species lies in a multicomponent phase that is active, but species concentration is zero due to stoic...
double vcs_l2norm(const std::vector< double > vec)
determine the l2 norm of a vector of doubles
bool vcs_doubleEqual(double d1, double d2)
Simple routine to check whether two doubles are equal up to roundoff error.
#define plogendl()
define this Cantera function to replace cout << endl;
const doublereal GasConstant
Universal Gas Constant. [J/kmol/K].
const doublereal GasConst_cal_mol_K
Universal gas constant in cal/mol/K.
int vcsUtil_gaussj(double *c, size_t idem, size_t n, double *b, size_t m)
Invert an n x n matrix and solve m rhs's.
int vcs_max_int(const int *vector, int length)
Returns the maximum integer in a list.
#define plogf
define this Cantera function to replace printf
#define VCS_SPECIES_COMPONENT
Species is a component which can be nonzero.
#define VCS_SPECIES_DELETED
Species has such a small mole fraction it is deleted even though its phase may possibly exist...