27 void vcs_dzero(
double* vector,
int length)
37 for (i = 0; i < length; i++) {
46 void vcs_izero(
int* vector,
int length)
56 for (i = 0; i < length; i++) {
66 void vcs_dcopy(
double*
const vec_to,
const double*
const vec_from,
int length)
76 for (i = 0; i < length; i++) {
77 vec_to[i] = vec_from[i];
86 void vcs_icopy(
int* vec_to,
int* vec_from,
int length)
96 for (i = 0; i < length; i++) {
97 vec_to[i] = vec_from[i];
112 void vcs_vdzero(std::vector<double> &vvv,
int len)
115 std::fill(vvv.begin(), vvv.end(), 0.0);
117 std::fill_n(vvv.begin(), len, 0.0);
122 double vcs_l2norm(
const std::vector<double> vec)
124 size_t len = vec.size();
129 std::vector<double>::const_iterator pos;
130 for (pos = vec.begin(); pos != vec.end(); ++pos) {
131 sum += (*pos) * (*pos);
133 return std::sqrt(sum/len);
146 void vcs_vizero(std::vector<int> &vvv,
int len)
149 std::fill(vvv.begin(), vvv.end(), 0.0);
151 std::fill_n(vvv.begin(), len, 0.0);
168 void vcs_vdcopy(std::vector<double> &vec_to,
169 const std::vector<double> & vec_from,
int length)
171 std::copy(vec_from.begin(), vec_from.begin() + length, vec_to.begin());
186 void vcs_vicopy(std::vector<int> &vec_to,
187 const std::vector<int> & vec_from,
int length)
189 std::copy(vec_from.begin(), vec_from.begin() + length, vec_to.begin());
205 size_t vcs_optMax(
const double* x,
const double* xSize,
size_t j,
size_t n)
211 assert(xSize[j] > 0.0);
213 for (i = j + 1; i < n; ++i) {
214 assert(xSize[i] > 0.0);
215 if ((x[i]*xSize[i]) > big) {
221 for (i = j + 1; i < n; ++i) {
231 int vcs_max_int(
const int* vector,
int length)
241 if (vector == NULL || length <= 0) {
245 for (i = 1; i < length; i++) {
254 static void mlequ_matrixDump(
double* c,
int idem,
int n)
257 printf(
"vcsUtil_mlequ() MATRIX DUMP --------------------------------------------------\n");
259 for (j = 0; j < n; ++j) {
263 for (j = 0; j < n; ++j) {
264 printf(
"-----------");
267 for (i = 0; i < n; ++i) {
268 printf(
" %3d | ", i);
269 for (j = 0; j < n; ++j) {
270 printf(
"% 10.3e ", c[i + j * idem]);
274 for (j = 0; j < n; ++j) {
275 printf(
"-----------");
278 printf(
"vcsUtil_mlequ() END MATRIX DUMP --------------------------------------------------\n");
293 static void vcsUtil_swapRows(
double* c,
size_t idem,
size_t n,
double* b,
size_t m,
size_t irowa,
size_t irowb)
295 if (irowa == irowb) {
298 for (
size_t j = 0; j < n; j++) {
299 std::swap(c[irowa + j * idem], c[irowb + j * idem]);
301 for (
size_t j = 0; j < m; j++) {
302 std::swap(b[irowa + j * idem], b[irowb + j * idem]);
314 static void vcsUtil_mlequ_preprocess(
double* c,
size_t idem,
size_t n,
double* b,
size_t m)
317 std::vector<int> irowUsed(n, 0);
319 for (j = 0; j < n; j++) {
321 size_t inonzero =
npos;
322 for (
size_t i = 0; i < n; i++) {
323 if (c[i + j * idem] != 0.0) {
328 if (numNonzero == 1) {
330 if (irowUsed[inonzero] == 0) {
331 vcsUtil_swapRows(c, idem, n, b, m, j, inonzero);
341 for (j = 0; j < n; j++) {
342 if (c[j + j * idem] == 0.0) {
344 size_t inonzero =
npos;
345 for (
size_t i = 0; i < n; i++) {
347 if (c[i + j * idem] != 0.0) {
348 if ((c[i + i * idem] == 0.0) || (c[j + i * idem] != 0.0)) {
355 if (numNonzero == 1) {
357 if (irowUsed[inonzero] == 0) {
358 vcsUtil_swapRows(c, idem, n, b, m, j, inonzero);
369 for (j = 0; j < n; j++) {
370 if (c[j + j * idem] == 0.0) {
372 size_t inonzero =
npos;
373 for (
size_t i = 0; i < n; i++) {
375 if (c[i + j * idem] != 0.0) {
376 if ((c[i + i * idem] == 0.0) || (c[j + i * idem] != 0.0)) {
383 if (inonzero !=
npos) {
385 if (irowUsed[inonzero] == 0) {
386 vcsUtil_swapRows(c, idem, n, b, m, j, inonzero);
427 int vcsUtil_mlequ(
double* c,
size_t idem,
size_t n,
double* b,
size_t m)
433 vcsUtil_mlequ_preprocess(c, idem, n, b, m);
436 static int s_numCalls = 0;
441 if (n > idem || n <= 0) {
442 plogf(
"vcsUtil_mlequ ERROR: badly dimensioned matrix: %d %d\n", n, idem);
448 for (
size_t i = 0; i < n; ++i) {
449 bool notFound =
true;
450 for (
size_t j = 0; j < n; ++j) {
451 if (c[i + j * idem] != 0.0) {
456 printf(
" vcsUtil_mlequ ERROR(): row %d is identically zero\n", i);
459 for (
size_t j = 0; j < n; ++j) {
460 bool notFound =
true;
461 for (
size_t i = 0; i < n; ++i) {
462 if (c[i + j * idem] != 0.0) {
467 printf(
" vcsUtil_mlequ ERROR(): column %d is identically zero\n", j);
476 mlequ_matrixDump(c, idem, n);
485 for (
size_t i = 0; i < n; ++i) {
486 if (c[i + i * idem] == 0.0) {
490 for (k = i + 1; k < n; ++k) {
491 if (c[k + i * idem] != 0.0) {
495 plogf(
"vcsUtil_mlequ ERROR: Encountered a zero column: %d\n", i);
497 plogf(
" call # %d\n", s_numCalls);
500 mlequ_matrixDump(c, idem, n);
505 for (
size_t j = 0; j < n; ++j) {
506 c[i + j * idem] += c[k + j * idem];
508 for (
size_t j = 0; j < m; ++j) {
509 b[i + j * idem] += b[k + j * idem];
513 for (
size_t l = 0; l < n; ++l) {
514 if (l != i && c[l + i * idem] != 0.0) {
515 R = c[l + i * idem] / c[i + i * idem];
516 c[l + i * idem] = 0.0;
517 for (
size_t j = i+1; j < n; ++j) {
518 c[l + j * idem] -= c[i + j * idem] * R;
520 for (
size_t j = 0; j < m; ++j) {
521 b[l + j * idem] -= b[i + j * idem] * R;
530 for (
size_t i = 0; i < n; ++i) {
531 for (
size_t j = 0; j < m; ++j) {
532 b[i + j * idem] = -b[i + j * idem] / c[i + i*idem];
567 int vcsUtil_gaussj(
double* c,
size_t idem,
size_t n,
double* b,
size_t m)
570 size_t i, j, k, l, ll;
573 bool needInverse =
false;
576 static int s_numCalls = 0;
585 vcsUtil_mlequ_preprocess(c, idem, n, b, m);
591 std::vector<size_t> indxc(n);
592 std::vector<size_t> indxr(n);
593 std::vector<int> ipiv(n, 0);
594 doublereal big = 0.0;
598 for (i = 0; i < n; i++) {
600 for (j = 0; j < n; j++) {
602 for (k = 0; k < n; k++) {
604 if (fabs(c[j + idem * k]) >= big) {
605 big = fabs(c[j + idem * k]);
615 vcsUtil_swapRows(c, idem, n, b, m, irow, icol);
619 if (c[icol + idem * icol] == 0.0) {
620 plogf(
"vcsUtil_gaussj ERROR: Encountered a zero column: %d\n", i);
623 pivinv = 1.0 / c[icol + idem * icol];
624 c[icol + idem * icol] = 1.0;
625 for (l = 0; l < n; l++) {
626 c[icol + idem * l] *= pivinv;
628 for (l = 0; l < m; l++) {
629 b[icol + idem * l] *= pivinv;
631 for (ll = 0; ll < n; ll++) {
633 double dum = c[ll + idem * icol];
634 c[ll + idem * icol] = 0;
635 for (l = 0; l < n; l++) {
636 c[ll + idem * l] -= c[icol + idem * l] * dum;
638 for (l = 0; l < m; l++) {
639 b[ll + idem * l] -= b[icol + idem * l] * dum;
645 for (l = n-1; l !=
npos; l--) {
646 if (indxr[l] != indxc[l]) {
647 for (k = 0; k < n; k++) {
648 std::swap(c[k + idem * indxr[l]], c[k + idem * indxr[l]]);
659 for (i = 0; i < n; ++i) {
660 for (j = 0; j < m; ++j) {
661 b[i + j * idem] = -b[i + j * idem];
677 double vcsUtil_gasConstant(
int mu_units)
681 case VCS_UNITS_KCALMOL:
684 case VCS_UNITS_UNITLESS:
687 case VCS_UNITS_KJMOL:
690 case VCS_UNITS_KELVIN:
698 plogf(
"vcs_gasConstant error: uknown units: %d\n",
707 void vcs_print_line(
const char*
string,
int num)
718 for (
int j = 0; j < num; j++) {
726 const char* vcs_speciesType_string(
int speciesStatus,
int length)
729 switch (speciesStatus) {
731 sss =
"Component Species";
734 sss =
"Major Species";
737 sss =
"Minor Species";
741 sss =
"Set Zeroed-Phase";
743 sss =
"Purposely Zeroed-Phase Species (not in problem)";
748 sss =
"Zeroed-MS Phase";
750 sss =
"Zeroed-MS Phase Species";
755 sss =
"Zeroed-SS Phase";
757 sss =
"Zeroed-SS Phase Species";
762 sss =
"Deleted Species";
763 }
else if (length < 40) {
764 sss =
"Deleted-Small Species";
766 sss =
"Deleted-Small Species in a MS phase";
771 sss =
"Tmp Zeroed in MS";
773 sss =
"Zeroed Species in an active MS phase (tmp)";
778 sss =
"Stoich Zeroed in MS";
780 sss =
"Zeroed Species in an active MS phase (Stoich Constraint)";
785 sss =
"InterfaceVoltage";
787 sss =
"InterfaceVoltage Species";
791 sss =
"unknown species type";
799 void vcs_print_stringTrunc(
const char* str,
size_t space,
int alignment)
815 size_t i, ls=0, rs=0;
816 size_t len = strlen(str);
817 if ((len) >= space) {
818 for (i = 0; i < space; i++) {
822 if (alignment == 1) {
824 }
else if (alignment == 2) {
827 ls = (space - len) / 2;
828 rs = space - len - ls;
831 for (i = 0; i < ls; i++) {
837 for (i = 0; i < rs; i++) {
848 bool vcs_doubleEqual(
double d1,
double d2)
858 double denom = fabs(d1) + fabs(d2) + 1.0;
859 double fac = fabs(d1 - d2) / denom;