19 #include "vcs_species_thermo.h"
28 #define MAX(x,y) (( (x) > (y) ) ? (x) : (y))
39 static void print_space(
size_t num);
45 static void prneav(
void);
46 static int prnfm(
void);
53 void VCS_SOLVE::checkDelta1(
double*
const dsLocal,
54 double*
const delTPhMoles,
int kspec)
56 std::vector<double> dchange(m_numPhases, 0.0);
57 for (
int k = 0; k < kspec; k++) {
59 int iph = m_phaseID[k];
60 dchange[iph] += dsLocal[k];
63 for (
size_t iphase = 0; iphase < m_numPhases; iphase++) {
64 double denom = MAX(m_totalMolNum, 1.0E-4);
65 if (!vcs_doubleEqual(dchange[iphase]/denom, delTPhMoles[iphase]/denom)) {
66 plogf(
"checkDelta1: we have found a problem\n");
102 int VCS_SOLVE::vcs_solve_TP(
int print_lvl,
int printDetails,
int maxit)
106 size_t j, k, l, l1, kspec, irxn, i;
107 bool conv =
false, allMinorZeroedSpecies =
false, forced, lec;
111 size_t npb, iti, lnospec;
113 int rangeErrorFound = 0;
114 bool giveUpOnElemAbund =
false;
115 int finalElemAbundAttempts = 0;
116 bool uptodate_minors =
true;
117 bool justDeletedMultiPhase =
false;
118 bool usedZeroedSpecies;
121 double* sc_irxn = NULL;
122 double* dnPhase_irxn;
125 int forceComponentCalc = 1;
127 std::vector<size_t> phasePopPhaseIDs(0);
128 size_t doPhaseDeleteIph =
npos;
129 size_t doPhaseDeleteKspec =
npos;
136 m_debug_print_lvl = printDetails;
138 if (printDetails > 0 && print_lvl == 0) {
144 vcs_counters_init(0);
159 std::vector<double> sm(m_numElemConstraints*m_numElemConstraints, 0.0);
160 std::vector<double> ss(m_numElemConstraints, 0.0);
161 std::vector<double> sa(m_numElemConstraints, 0.0);
163 std::vector<double> aw(m_numSpeciesTot, 0.0);
164 std::vector<double> wx(m_numElemConstraints, 0.0);
180 if (print_lvl != 0) {
181 plogf(
"VCS CALCULATION METHOD\n\n ");
182 plogf(
"%s\n", m_title.c_str());
183 plogf(
"\n\n%5d SPECIES\n%5d ELEMENTS\n", m_numSpeciesTot, m_numElemConstraints);
184 plogf(
"%5d COMPONENTS\n", m_numComponents);
185 plogf(
"%5d PHASES\n", m_numPhases);
187 plogf(
" PRESSURE%22.8g %3s\n", m_pressurePA,
"Pa ");
188 plogf(
" TEMPERATURE%19.3f K\n", m_temperature);
189 Vphase = m_VolPhaseList[0];
191 plogf(
" PHASE1 INERTS%17.3f\n", TPhInertMoles[0]);
193 if (m_numPhases > 1) {
194 plogf(
" PHASE2 INERTS%17.3f\n", TPhInertMoles[1]);
196 plogf(
"\n ELEMENTAL ABUNDANCES CORRECT");
197 plogf(
" FROM ESTIMATE Type\n\n");
198 for (i = 0; i < m_numElemConstraints; ++i) {
200 plogf(
"%-2.2s", (m_elementName[i]).c_str());
201 plogf(
"%20.12E%20.12E %3d\n", m_elemAbundancesGoal[i], m_elemAbundances[i],
204 if (m_doEstimateEquil < 0) {
205 plogf(
"\n MODIFIED LINEAR PROGRAMMING ESTIMATE OF EQUILIBRIUM - forced\n");
206 }
else if (m_doEstimateEquil > 0) {
207 plogf(
"\n MODIFIED LINEAR PROGRAMMING ESTIMATE OF EQUILIBRIUM - where necessary\n");
209 if (m_doEstimateEquil == 0) {
210 plogf(
"\n USER ESTIMATE OF EQUILIBRIUM\n");
212 if (m_VCS_UnitsFormat == VCS_UNITS_KCALMOL) {
213 plogf(
" Stan. Chem. Pot. in kcal/mole\n");
215 if (m_VCS_UnitsFormat == VCS_UNITS_UNITLESS) {
216 plogf(
" Stan. Chem. Pot. is MU/RT\n");
218 if (m_VCS_UnitsFormat == VCS_UNITS_KJMOL) {
219 plogf(
" Stan. Chem. Pot. in KJ/mole\n");
221 if (m_VCS_UnitsFormat == VCS_UNITS_KELVIN) {
222 plogf(
" Stan. Chem. Pot. in Kelvin\n");
224 if (m_VCS_UnitsFormat == VCS_UNITS_MKS) {
225 plogf(
" Stan. Chem. Pot. in J/kmol\n");
227 plogf(
"\n SPECIES FORMULA VECTOR ");
229 plogf(
" STAN_CHEM_POT EQUILIBRIUM_EST. Species_Type\n\n");
231 for (i = 0; i < m_numElemConstraints; ++i) {
232 plogf(
"%-4.4s ", m_elementName[i].c_str());
235 RT = vcs_nondimMult_TP(m_VCS_UnitsFormat, m_temperature);
236 for (i = 0; i < m_numSpeciesTot; ++i) {
237 plogf(
" %-18.18s", m_speciesName[i].c_str());
238 for (j = 0; j < m_numElemConstraints; ++j) {
239 plogf(
"% -7.3g ", m_formulaMatrix[j][i]);
241 plogf(
" %3d ", m_phaseID[i]);
242 print_space(
std::max(55-
int(m_numElemConstraints)*8, 0));
243 plogf(
"%12.5E %12.5E", RT * m_SSfeSpecies[i], m_molNumSpecies_old[i]);
255 for (i = 0; i < m_numSpeciesTot; ++i) {
256 if (m_molNumSpecies_old[i] < 0.0) {
257 plogf(
"On Input species %-12s has a "
258 "negative MF, setting it small",
259 m_speciesName[i].c_str());
266 m_molNumSpecies_old[i] = tmp;
293 test, &usedZeroedSpecies);
302 forceComponentCalc = 0;
312 allMinorZeroedSpecies = vcs_evaluate_speciesType();
318 if (! vcs_elabcheck(0)) {
320 if (m_debug_print_lvl >= 2) {
321 plogf(
" --- Element Abundance check failed");
334 if (m_debug_print_lvl >= 2) {
335 plogf(
" --- Element Abundance check passed");
342 goto L_MAINLOOP_ALL_SPECIES;
352 L_MAINLOOP_MM4_SPECIES:
354 iti = ((it1/4) *4) - it1;
358 L_MAINLOOP_ALL_SPECIES:
366 if (!uptodate_minors) {
371 uptodate_minors =
true;
373 uptodate_minors =
false;
378 vcs_print_line(
"=", 110);
379 plogf(
" Iteration = %3d, Iterations since last evaluation of "
380 "optimal basis = %3d",
381 m_VCount->Its, it1 - 1);
383 plogf(
" (all species)\n");
385 plogf(
" (only major species)\n");
414 vcs_dzero(
VCS_DATA_PTR(m_deltaMolNumSpecies), m_numSpeciesTot);
423 phasePopPhaseIDs.clear();
424 iphasePop = vcs_popPhaseID(phasePopPhaseIDs);
429 if (iphasePop !=
npos) {
430 soldel = vcs_popPhaseRxnStepSizes(iphasePop);
434 if (m_debug_print_lvl >= 2) {
435 plogf(
" --- vcs_popPhaseRxnStepSizes() was called but stoich "
436 "prevented phase %d popping\n");
449 if (iphasePop ==
npos) {
455 iphaseDelete = vcs_RxnStepSizes(forceComponentCalc, kspec);
459 if (m_debug_print_lvl >= 2) {
460 plogf(
" --- vcs_RxnStepSizes not called because alternative"
461 "phase creation delta was used instead\n");
466 doPhaseDeleteIph =
npos;
467 doPhaseDeleteKspec =
npos;
471 vcs_dzero(
VCS_DATA_PTR(m_deltaPhaseMoles), m_numPhases);
478 if (m_VCount->Its > maxit) {
505 if (iphaseDelete !=
npos) {
507 if (m_debug_print_lvl >= 2) {
508 plogf(
" --- Main Loop Treatment -> Circumvented due to Phase Deletion ");
513 for (k = 0; k < m_numSpeciesTot; k++) {
514 m_molNumSpecies_new[k] = m_molNumSpecies_old[k] + m_deltaMolNumSpecies[k];
516 m_tPhaseMoles_new[iph] += m_deltaMolNumSpecies[k];
518 if (kspec >= m_numComponents) {
519 if (m_molNumSpecies_new[k] != 0.0) {
520 printf(
"vcs_solve_tp:: we shouldn't be here!\n");
523 if (m_SSPhase[kspec] == 1) {
526 printf(
"vcs_solve_tp:: we shouldn't be here!\n");
529 ++m_numRxnMinorZeroed;
530 allMinorZeroedSpecies = (m_numRxnMinorZeroed == m_numRxnRdc);
552 if (m_debug_print_lvl >= 2) {
553 plogf(
" --- Main Loop Treatment of each non-component species ");
555 plogf(
"- Full Calculation:\n");
557 plogf(
"- Major Components Calculation:\n");
559 plogf(
" --- Species IC ");
560 plogf(
" KMoles Tent_KMoles Rxn_Adj | Comment \n");
563 for (irxn = 0; irxn < m_numRxnRdc; irxn++) {
564 kspec = m_indexRxnToSpecies[irxn];
565 sc_irxn = m_stoichCoeffRxnMatrix[irxn];
566 iph = m_phaseID[kspec];
567 Vphase = m_VolPhaseList[iph];
571 if (iphasePop !=
npos) {
572 if (iph == iphasePop) {
573 dx = m_deltaMolNumSpecies[kspec];
574 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec] + m_deltaMolNumSpecies[kspec];
576 sprintf(ANOTE,
"Phase pop");
580 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec];
591 dx = vcs_minor_alt_calc(kspec, irxn, &soldel_ret, ANOTE);
593 dx = vcs_minor_alt_calc(kspec, irxn, &soldel_ret);
596 m_deltaMolNumSpecies[kspec] = dx;
601 bool resurrect = (m_deltaMolNumSpecies[kspec] > 0.0);
603 if (m_debug_print_lvl >= 3) {
604 plogf(
" --- %s currently zeroed (SpStatus=%-2d):",
605 m_speciesName[kspec].c_str(), m_speciesStatus[kspec]);
606 plogf(
"%3d DG = %11.4E WT = %11.4E W = %11.4E DS = %11.4E\n",
607 irxn, m_deltaGRxn_new[irxn], m_molNumSpecies_new[kspec],
608 m_molNumSpecies_old[kspec], m_deltaMolNumSpecies[kspec]);
612 if (m_deltaGRxn_new[irxn] >= 0.0 || !resurrect) {
613 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec];
614 m_deltaMolNumSpecies[kspec] = 0.0;
617 sprintf(ANOTE,
"Species stays zeroed: DG = %11.4E", m_deltaGRxn_new[irxn]);
618 if (m_deltaGRxn_new[irxn] < 0.0) {
620 sprintf(ANOTE,
"Species stays zeroed even though dg neg due to "
621 "STOICH/PHASEPOP constraint: DG = %11.4E",
622 m_deltaGRxn_new[irxn]);
624 sprintf(ANOTE,
"Species stays zeroed even though dg neg: DG = %11.4E, ds zeroed",
625 m_deltaGRxn_new[irxn]);
630 for (
size_t j = 0; j < m_numElemConstraints; ++j) {
631 int elType = m_elType[j];
633 atomComp = m_formulaMatrix[j][kspec];
634 if (atomComp > 0.0) {
635 double maxPermissible = m_elemAbundancesGoal[j] / atomComp;
638 sprintf(ANOTE,
"Species stays zeroed even though dG "
639 "neg, because of %s elemAbund",
640 m_elementName[j].c_str());
653 bool phaseResurrected =
false;
656 phaseResurrected =
true;
659 if (phaseResurrected) {
661 if (m_debug_print_lvl >= 2) {
662 plogf(
" --- Zeroed species changed to major: ");
663 plogf(
"%-12s\n", m_speciesName[kspec].c_str());
667 allMinorZeroedSpecies =
false;
670 if (m_debug_print_lvl >= 2) {
671 plogf(
" --- Zeroed species changed to minor: ");
672 plogf(
"%-12s\n", m_speciesName[kspec].c_str());
677 if (m_deltaMolNumSpecies[kspec] > 0.0) {
678 dx = m_deltaMolNumSpecies[kspec] * 0.01;
679 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec] + dx;
682 dx = m_molNumSpecies_new[kspec] - m_molNumSpecies_old[kspec];
684 m_deltaMolNumSpecies[kspec] = dx;
686 sprintf(ANOTE,
"Born:IC=-1 to IC=1:DG=%11.4E", m_deltaGRxn_new[irxn]);
689 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec];
690 m_deltaMolNumSpecies[kspec] = 0.0;
702 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec];
703 m_deltaMolNumSpecies[kspec] = 0.0;
706 sprintf(ANOTE,
"minor species not considered");
707 if (m_debug_print_lvl >= 2) {
709 plogf(
"%-12s", m_speciesName[kspec].c_str());
710 plogf(
"%3d%11.4E%11.4E%11.4E | %s",
711 m_speciesStatus[kspec], m_molNumSpecies_old[kspec], m_molNumSpecies_new[kspec],
712 m_deltaMolNumSpecies[kspec], ANOTE);
734 dx = vcs_minor_alt_calc(kspec, irxn, &soldel_ret, ANOTE);
736 dx = vcs_minor_alt_calc(kspec, irxn, &soldel_ret);
739 m_deltaMolNumSpecies[kspec] = dx;
740 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec] + dx;
748 if (m_debug_print_lvl >= 2) {
749 plogf(
" --- Delete minor species in multispec phase: %-12s",
750 m_speciesName[kspec].c_str());
754 m_deltaMolNumSpecies[kspec] = 0.0;
761 lnospec = vcs_delete_species(kspec);
763 goto L_RECHECK_DELETED;
775 goto L_MAIN_LOOP_END_NO_PRINT;
777 goto L_MAIN_LOOP_END;
785 sprintf(ANOTE,
"Normal Major Calc");
792 if (fabs(m_deltaGRxn_new[irxn]) <= m_tolmaj2) {
793 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec];
794 m_deltaMolNumSpecies[kspec] = 0.0;
797 sprintf(ANOTE,
"major species is converged");
798 if (m_debug_print_lvl >= 2) {
800 plogf(
"%-12s", m_speciesName[kspec].c_str());
801 plogf(
"%3d%11.4E%11.4E%11.4E | %s",
802 m_speciesStatus[kspec], m_molNumSpecies_old[kspec], m_molNumSpecies_new[kspec],
803 m_deltaMolNumSpecies[kspec], ANOTE);
818 if ((m_deltaGRxn_new[irxn] * m_deltaMolNumSpecies[kspec]) <= 0.0) {
819 dx = m_deltaMolNumSpecies[kspec];
822 m_deltaMolNumSpecies[kspec] = 0.0;
824 sprintf(ANOTE,
"dx set to 0, DG flipped sign due to "
825 "changed initial point");
831 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec] + dx;
839 if (m_molNumSpecies_new[kspec] <= 0.0) {
841 sprintf(ANOTE,
"initial nonpos kmoles= %11.3E",
842 m_molNumSpecies_new[kspec]);
854 if (!(m_SSPhase[kspec])) {
861 dx = -0.9 * m_molNumSpecies_old[kspec];
862 m_deltaMolNumSpecies[kspec] = dx;
863 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec] + dx;
870 dx = -m_molNumSpecies_old[kspec];
877 for (j = 0; j < m_numComponents; ++j) {
878 if (sc_irxn[j] != 0.0) {
879 wx[j] = m_molNumSpecies_old[j] + sc_irxn[j] * dx;
880 if (wx[j] <= m_molNumSpecies_old[j] * 0.01 - 1.0E-150) {
881 dx =
std::max(dx, m_molNumSpecies_old[j] * -0.99 / sc_irxn[j]);
884 wx[j] = m_molNumSpecies_old[j];
887 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec] + dx;
888 if (m_molNumSpecies_new[kspec] > 0.0) {
889 m_deltaMolNumSpecies[kspec] = dx;
892 "zeroing SS phase created a neg component species "
893 "-> reducing step size instead");
900 iph = m_phaseID[kspec];
901 Vphase = m_VolPhaseList[iph];
904 sprintf(ANOTE,
"zeroing out SS phase: ");
912 m_molNumSpecies_new[kspec] = 0.0;
913 doPhaseDeleteIph = iph;
914 doPhaseDeleteKspec = kspec;
917 if (m_debug_print_lvl >= 2) {
918 if (m_speciesStatus[kspec] >= 0) {
919 plogf(
" --- SS species changed to zeroedss: ");
920 plogf(
"%-12s", m_speciesName[kspec].c_str());
926 ++m_numRxnMinorZeroed;
927 allMinorZeroedSpecies = (m_numRxnMinorZeroed == m_numRxnRdc);
929 for (
size_t kk = 0; kk < m_numSpeciesTot; kk++) {
930 m_deltaMolNumSpecies[kk] = 0.0;
931 m_molNumSpecies_new[kk] = m_molNumSpecies_old[kk];
933 m_deltaMolNumSpecies[kspec] = dx;
934 m_molNumSpecies_new[kspec] = 0.0;
936 for (k = 0; k < m_numComponents; ++k) {
937 m_deltaMolNumSpecies[k] = 0.0;
939 for (iph = 0; iph < m_numPhases; iph++) {
940 m_deltaPhaseMoles[iph] = 0.0;
948 #ifdef VCS_LINE_SEARCH
956 (m_molNumSpecies_old[kspec] > 0.0) &&
957 (doPhaseDeleteIph == -1) &&
962 dx = vcs_line_search(irxn, dx_old, ANOTE);
964 dx = vcs_line_search(irxn, dx_old);
968 m_deltaMolNumSpecies[kspec] = dx;
975 if (dx != 0.0 && (m_speciesUnknownType[kspec] !=
983 if (fabs(m_deltaMolNumSpecies[kspec] -dx) >
984 1.0E-14*(fabs(m_deltaMolNumSpecies[kspec]) + fabs(dx) + 1.0E-32)) {
985 plogf(
" ds[kspec] = %20.16g dx = %20.16g , kspec = %d\n",
986 m_deltaMolNumSpecies[kspec], dx, kspec);
987 plogf(
"we have a problem!");
992 for (k = 0; k < m_numComponents; ++k) {
993 m_deltaMolNumSpecies[k] += sc_irxn[k] * dx;
999 dnPhase_irxn = m_deltaMolNumPhase[irxn];
1000 for (iph = 0; iph < m_numPhases; iph++) {
1001 m_deltaPhaseMoles[iph] += dx * dnPhase_irxn[iph];
1017 if (m_debug_print_lvl >= 2) {
1018 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec] + m_deltaMolNumSpecies[kspec];
1020 plogf(
"%-12.12s", m_speciesName[kspec].c_str());
1021 plogf(
"%3d%11.4E%11.4E%11.4E | %s",
1022 m_speciesStatus[kspec], m_molNumSpecies_old[kspec],
1023 m_molNumSpecies_new[kspec],
1024 m_deltaMolNumSpecies[kspec], ANOTE);
1027 L_MAIN_LOOP_END_NO_PRINT:
1030 if (doPhaseDeleteIph !=
npos) {
1032 if (m_debug_print_lvl >= 2) {
1034 plogf(
"%-12.12s Main Loop Special Case deleting phase with species: ",
1035 m_speciesName[doPhaseDeleteKspec].c_str());
1044 if (m_debug_print_lvl >= 2) {
1045 for (k = 0; k < m_numComponents; k++) {
1047 plogf(
"%-12.12s", m_speciesName[k].c_str());
1048 plogf(
" c%11.4E%11.4E%11.4E |\n",
1049 m_molNumSpecies_old[k],
1050 m_molNumSpecies_old[k]+m_deltaMolNumSpecies[k], m_deltaMolNumSpecies[k]);
1053 vcs_print_line(
"-", 80);
1054 plogf(
" --- Finished Main Loop");
1069 for (k = 0; k < m_numComponents; ++k) {
1070 if (m_molNumSpecies_old[k] > 0.0) {
1071 xx = -m_deltaMolNumSpecies[k] / m_molNumSpecies_old[k];
1079 if (m_deltaMolNumSpecies[k] < 0.0) {
1085 m_deltaPhaseMoles[iph] -= m_deltaMolNumSpecies[k];
1086 m_deltaMolNumSpecies[k] = 0.0;
1091 if (par <= 1.01 && par > 0.0) {
1095 if (m_debug_print_lvl >= 2) {
1096 plogf(
" --- Reduction in step size due to component ");
1097 plogf(
"%s", m_speciesName[ll].c_str());
1098 plogf(
" going negative = %11.3E", par);
1102 for (i = 0; i < m_numSpeciesTot; ++i) {
1103 m_deltaMolNumSpecies[i] *= par;
1105 for (iph = 0; iph < m_numPhases; iph++) {
1106 m_deltaPhaseMoles[iph] *= par;
1122 for (kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
1123 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec] + m_deltaMolNumSpecies[kspec];
1124 if (m_molNumSpecies_new[kspec] < 0.0 && (m_speciesUnknownType[kspec]
1126 plogf(
"vcs_solve_TP: ERROR on step change wt[%d:%s]: %g < 0.0",
1127 kspec, m_speciesName[kspec].c_str(), m_molNumSpecies_new[kspec]);
1136 for (iph = 0; iph < m_numPhases; iph++) {
1137 m_tPhaseMoles_new[iph] = m_tPhaseMoles_old[iph] + m_deltaPhaseMoles[iph];
1163 plogf(
" --- Total Old Dimensionless Gibbs Free Energy = %20.13E\n",
1166 plogf(
" --- Total tentative Dimensionless Gibbs Free Energy = %20.13E",
1172 forced = vcs_globStepDamp();
1177 if (printDetails && forced) {
1179 plogf(
" -----------------------------------------------------\n");
1180 plogf(
" --- FORCER SUBROUTINE changed the solution:\n");
1181 plogf(
" --- SPECIES Status INIT MOLES TENT_MOLES");
1182 plogf(
" FINAL KMOLES INIT_DEL_G/RT TENT_DEL_G/RT FINAL_DELTA_G/RT\n");
1183 for (i = 0; i < m_numComponents; ++i) {
1184 plogf(
" --- %-12.12s", m_speciesName[i].c_str());
1185 plogf(
" %14.6E %14.6E %14.6E\n", m_molNumSpecies_old[i],
1186 m_molNumSpecies_old[i] + m_deltaMolNumSpecies[i], m_molNumSpecies_new[i]);
1188 for (kspec = m_numComponents; kspec < m_numSpeciesRdc; ++kspec) {
1189 irxn = kspec - m_numComponents;
1190 plogf(
" --- %-12.12s", m_speciesName[kspec].c_str());
1191 plogf(
" %2d %14.6E%14.6E%14.6E%14.6E%14.6E%14.6E\n", m_speciesStatus[kspec],
1192 m_molNumSpecies_old[kspec],
1193 m_molNumSpecies_old[kspec]+m_deltaMolNumSpecies[kspec],
1194 m_molNumSpecies_new[kspec], m_deltaGRxn_old[irxn],
1195 m_deltaGRxn_tmp[irxn], m_deltaGRxn_new[irxn]);
1198 plogf(
"Norms of Delta G():%14.6E%14.6E\n",
1201 plogf(
" Total kmoles of gas = %15.7E\n", m_tPhaseMoles_old[0]);
1202 if ((m_numPhases > 1) && (!(m_VolPhaseList[1])->m_singleSpecies)) {
1203 plogf(
" Total kmoles of liquid = %15.7E\n", m_tPhaseMoles_old[1]);
1205 plogf(
" Total kmoles of liquid = %15.7E\n", 0.0);
1207 plogf(
" Total New Dimensionless Gibbs Free Energy = %20.13E\n",
1210 plogf(
" -----------------------------------------------------");
1220 vcs_print_line(
"-", 103);
1221 plogf(
" --- Summary of the Update ");
1223 plogf(
" (all species):");
1225 plogf(
" (only major species):");
1227 if (m_totalMoleScale != 1.0) {
1228 plogf(
" (Total Mole Scale = %g)", m_totalMoleScale);
1231 plogf(
" --- Species Status Initial_KMoles Final_KMoles Initial_Mu/RT");
1232 plogf(
" Mu/RT Init_Del_G/RT Delta_G/RT\n");
1233 for (i = 0; i < m_numComponents; ++i) {
1234 plogf(
" --- %-12.12s", m_speciesName[i].c_str());
1236 plogf(
"%14.6E%14.6E%14.6E%14.6E\n", m_molNumSpecies_old[i],
1237 m_molNumSpecies_new[i], m_feSpecies_old[i], m_feSpecies_new[i]);
1239 for (i = m_numComponents; i < m_numSpeciesRdc; ++i) {
1240 l1 = i - m_numComponents;
1241 plogf(
" --- %-12.12s", m_speciesName[i].c_str());
1242 plogf(
" %2d %14.6E%14.6E%14.6E%14.6E%14.6E%14.6E\n",
1243 m_speciesStatus[i], m_molNumSpecies_old[i],
1244 m_molNumSpecies_new[i], m_feSpecies_old[i], m_feSpecies_new[i],
1245 m_deltaGRxn_old[l1], m_deltaGRxn_new[l1]);
1247 for (kspec = m_numSpeciesRdc; kspec < m_numSpeciesTot; ++kspec) {
1248 l1 = kspec - m_numComponents;
1249 plogf(
" --- %-12.12s", m_speciesName[kspec].c_str());
1250 plogf(
" %2d %14.6E%14.6E%14.6E%14.6E%14.6E%14.6E\n",
1251 m_speciesStatus[kspec], m_molNumSpecies_old[kspec],
1252 m_molNumSpecies_new[kspec], m_feSpecies_old[kspec], m_feSpecies_new[kspec],
1253 m_deltaGRxn_old[l1], m_deltaGRxn_new[l1]);
1257 plogf(
"Norms of Delta G():%14.6E%14.6E",
1262 plogf(
" --- Phase_Name KMoles(after update)\n");
1264 vcs_print_line(
"-", 50);
1265 for (iph = 0; iph < m_numPhases; iph++) {
1266 Vphase = m_VolPhaseList[iph];
1267 plogf(
" --- %18s = %15.7E\n", Vphase->
PhaseName.c_str(), m_tPhaseMoles_new[iph]);
1270 vcs_print_line(
"-", 103);
1271 plogf(
" --- Total Old Dimensionless Gibbs Free Energy = %20.13E\n",
1274 plogf(
" --- Total New Dimensionless Gibbs Free Energy = %20.13E",
1279 if (m_VCount->Its > 550) {
1280 plogf(
" --- Troublesome solve");
1317 if (m_debug_print_lvl >= 2) {
1318 plogf(
" --- Increment counter increased, step is accepted: %4d",
1333 justDeletedMultiPhase =
false;
1334 for (iph = 0; iph < m_numPhases; iph++) {
1335 Vphase = m_VolPhaseList[iph];
1337 if (m_tPhaseMoles_old[iph] != 0.0 &&
1341 for (kspec = 0; kspec < m_numSpeciesRdc; kspec++) {
1342 if (m_phaseID[kspec] == iph && m_molNumSpecies_old[kspec] > 0.0) {
1343 irxn = kspec - m_numComponents;
1348 if (m_debug_print_lvl >= 1) {
1349 plogf(
" --- Setting microscopic phase %d to zero", iph);
1353 justDeletedMultiPhase =
true;
1354 vcs_delete_multiphase(iph);
1365 if (justDeletedMultiPhase) {
1366 justDeletedMultiPhase =
false;
1369 &usedZeroedSpecies);
1372 plogf(
" --- BASOPT returned with an error condition\n");
1381 goto L_MAINLOOP_ALL_SPECIES ;
1388 if (m_debug_print_lvl >= 2) {
1389 plogf(
" --- Normal element abundance check");
1393 if (! vcs_elabcheck(0)) {
1395 if (m_debug_print_lvl >= 2) {
1396 plogf(
" - failed -> redoing element abundances.");
1404 uptodate_minors =
true;
1408 if (m_debug_print_lvl >= 2) {
1428 dofast = (m_numComponents != 1);
1429 for (i = 1; i < m_numComponents; ++i) {
1430 if ((m_molNumSpecies_old[i - 1] * m_spSize[i-1]) < (m_molNumSpecies_old[i] * m_spSize[i])) {
1437 for (i = 0; i < m_numRxnRdc; ++i) {
1438 l = m_indexRxnToSpecies[i];
1440 for (j = m_numComponents - 1; j !=
npos; j--) {
1441 bool doSwap =
false;
1443 doSwap = (m_molNumSpecies_old[l] * m_spSize[l]) >
1444 (m_molNumSpecies_old[j] * m_spSize[j] * 1.01);
1445 if (!m_SSPhase[i]) {
1447 doSwap = (m_molNumSpecies_old[l]) > (m_molNumSpecies_old[j] * 1.01);
1452 doSwap = (m_molNumSpecies_old[l] * m_spSize[l]) >
1453 (m_molNumSpecies_old[j] * m_spSize[j] * 1.01);
1455 doSwap = (m_molNumSpecies_old[l]) > (m_molNumSpecies_old[j] * 1.01);
1458 doSwap = (m_molNumSpecies_old[l] * m_spSize[l]) >
1459 (m_molNumSpecies_old[j] * m_spSize[j] * 1.01);
1463 if (m_stoichCoeffRxnMatrix[i][j] != 0.0) {
1465 if (m_debug_print_lvl >= 2) {
1466 plogf(
" --- Get a new basis because %s", m_speciesName[l].c_str());
1467 plogf(
" is better than comp %s", m_speciesName[j].c_str());
1468 plogf(
" and share nonzero stoic: %-9.1f",
1469 m_stoichCoeffRxnMatrix[i][j]);
1473 goto L_COMPONENT_CALC;
1480 if (m_molNumSpecies_old[j] == 0.0) {
1481 if (m_stoichCoeffRxnMatrix[i][j] != 0.0) {
1484 if (m_debug_print_lvl >= 2) {
1485 plogf(
" --- Get a new basis because %s", m_speciesName[l].c_str());
1486 plogf(
" has dg < 0.0 and comp %s has zero mole num", m_speciesName[j].c_str());
1487 plogf(
" and share nonzero stoic: %-9.1f",
1488 m_stoichCoeffRxnMatrix[i][j]);
1492 goto L_COMPONENT_CALC;
1502 for (i = 0; i < m_numRxnRdc; ++i) {
1503 l = m_indexRxnToSpecies[i];
1505 for (j = 0; j < m_numComponents; ++j) {
1506 bool doSwap =
false;
1508 doSwap = (m_molNumSpecies_old[l] * m_spSize[l]) >
1509 (m_molNumSpecies_old[j] * m_spSize[j] * 1.01);
1510 if (!m_SSPhase[l]) {
1512 doSwap = (m_molNumSpecies_old[l]) > (m_molNumSpecies_old[j] * 1.01);
1517 doSwap = (m_molNumSpecies_old[l] * m_spSize[l]) >
1518 (m_molNumSpecies_old[j] * m_spSize[j] * 1.01);
1520 doSwap = (m_molNumSpecies_old[l]) > (m_molNumSpecies_old[j] * 1.01);
1523 doSwap = (m_molNumSpecies_old[l] * m_spSize[l]) >
1524 (m_molNumSpecies_old[j] * m_spSize[j] * 1.01);
1528 if (m_stoichCoeffRxnMatrix[i][j] != 0.0) {
1530 if (m_debug_print_lvl >= 2) {
1531 plogf(
" --- Get a new basis because ");
1532 plogf(
"%s", m_speciesName[l].c_str());
1533 plogf(
" is better than comp ");
1534 plogf(
"%s", m_speciesName[j].c_str());
1535 plogf(
" and share nonzero stoic: %-9.1f",
1536 m_stoichCoeffRxnMatrix[i][j]);
1540 goto L_COMPONENT_CALC;
1545 if (m_molNumSpecies_old[j] == 0.0) {
1546 if (m_stoichCoeffRxnMatrix[i][j] != 0.0) {
1549 if (m_debug_print_lvl >= 2) {
1550 plogf(
" --- Get a new basis because %s", m_speciesName[l].c_str());
1551 plogf(
" has dg < 0.0 and comp %s has zero mole num",
1552 m_speciesName[j].c_str());
1553 plogf(
" and share nonzero stoic: %-9.1f",
1554 m_stoichCoeffRxnMatrix[i][j]);
1558 goto L_COMPONENT_CALC;
1569 if (m_debug_print_lvl >= 2) {
1570 plogf(
" --- Check for an optimum basis passed");
1583 if (m_debug_print_lvl >= 2) {
1584 plogf(
" --- Reevaluate major-minor status of noncomponents:\n");
1587 m_numRxnMinorZeroed = 0;
1588 for (irxn = 0; irxn < m_numRxnRdc; irxn++) {
1589 kspec = m_indexRxnToSpecies[irxn];
1591 int speciesType = vcs_species_type(kspec);
1594 if (m_debug_print_lvl >= 2) {
1596 plogf(
" --- major/minor species is now zeroed out: %s\n",
1597 m_speciesName[kspec].c_str());
1601 ++m_numRxnMinorZeroed;
1604 if (m_debug_print_lvl >= 2) {
1607 plogf(
" --- Noncomponent turned from major to minor: ");
1608 }
else if (kspec < m_numComponents) {
1609 plogf(
" --- Component turned into a minor species: ");
1611 plogf(
" --- Zeroed Species turned into a "
1614 plogf(
"%s\n", m_speciesName[kspec].c_str());
1618 ++m_numRxnMinorZeroed;
1622 if (m_debug_print_lvl >= 2) {
1624 plogf(
" --- Noncomponent turned from minor to major: ");
1625 }
else if (kspec < m_numComponents) {
1626 plogf(
" --- Component turned into a major: ");
1628 plogf(
" --- Noncomponent turned from zeroed to major: ");
1630 plogf(
"%s\n", m_speciesName[kspec].c_str());
1644 m_speciesStatus[kspec] = speciesType;
1650 allMinorZeroedSpecies = (m_numRxnMinorZeroed == m_numRxnRdc);
1657 if (! allMinorZeroedSpecies) {
1659 if (m_debug_print_lvl >= 2) {
1660 plogf(
" --- Equilibrium check for major species: ");
1663 for (irxn = 0; irxn < m_numRxnRdc; ++irxn) {
1664 kspec = irxn + m_numComponents;
1665 if (m_speciesStatus[kspec] ==
VCS_SPECIES_MAJOR && (fabs(m_deltaGRxn_new[irxn]) > m_tolmaj)) {
1666 if (m_VCount->Its >= maxit) {
1672 goto L_RETURN_BLOCK;
1675 if (m_debug_print_lvl >= 2) {
1676 plogf(
"%s failed\n", m_speciesName[m_indexRxnToSpecies[irxn]].c_str());
1683 if (forceComponentCalc) {
1684 goto L_COMPONENT_CALC;
1686 goto L_MAINLOOP_MM4_SPECIES;
1691 if (m_debug_print_lvl >= 2) {
1692 plogf(
" MAJOR SPECIES CONVERGENCE achieved");
1699 if (m_debug_print_lvl >= 2) {
1700 plogf(
" MAJOR SPECIES CONVERGENCE achieved "
1701 "(because there are no major species)");
1711 if (m_numRxnMinorZeroed != 0) {
1720 uptodate_minors =
true;
1723 if (m_debug_print_lvl >= 2) {
1724 plogf(
" --- Equilibrium check for minor species: ");
1727 for (irxn = 0; irxn < m_numRxnRdc; ++irxn) {
1728 kspec = irxn + m_numComponents;
1729 if (m_speciesStatus[kspec] ==
VCS_SPECIES_MINOR && (fabs(m_deltaGRxn_new[irxn]) > m_tolmin)) {
1730 if (m_VCount->Its >= maxit) {
1736 goto L_RETURN_BLOCK;
1739 if (m_debug_print_lvl >= 2) {
1740 plogf(
"%s failed\n", m_speciesName[m_indexRxnToSpecies[irxn]].c_str());
1748 if (forceComponentCalc) {
1749 goto L_COMPONENT_CALC;
1751 goto L_MAINLOOP_ALL_SPECIES;
1755 if (m_debug_print_lvl >= 2) {
1756 plogf(
" CONVERGENCE achieved\n");
1771 if (!giveUpOnElemAbund) {
1773 if (m_debug_print_lvl >= 2) {
1774 plogf(
" --- Check the Full Element Abundances: ");
1782 if (! vcs_elabcheck(1)) {
1784 if (m_debug_print_lvl >= 2) {
1785 if (! vcs_elabcheck(0)) {
1788 plogf(
" passed for NC but failed for NE: RANGE ERROR\n");
1793 goto L_ELEM_ABUND_CHECK;
1796 if (m_debug_print_lvl >= 2) {
1805 if (m_numSpeciesRdc != m_numSpeciesTot) {
1806 goto L_RECHECK_DELETED;
1809 goto L_RETURN_BLOCK;
1824 rangeErrorFound = 0;
1825 if (! vcs_elabcheck(1)) {
1826 bool ncBefore = vcs_elabcheck(0);
1828 bool ncAfter = vcs_elabcheck(0);
1829 bool neAfter = vcs_elabcheck(1);
1846 goto L_MAINLOOP_ALL_SPECIES;
1851 if (finalElemAbundAttempts >= 3) {
1852 giveUpOnElemAbund =
true;
1853 goto L_EQUILIB_CHECK;
1855 finalElemAbundAttempts++;
1858 goto L_MAINLOOP_ALL_SPECIES;
1869 goto L_EQUILIB_CHECK;
1875 if (m_debug_print_lvl >= 2) {
1876 plogf(
" --- vcs_solve_tp: RANGE SPACE ERROR ENCOUNTERED\n");
1877 plogf(
" --- vcs_solve_tp: - Giving up on NE Element Abundance satisfaction \n");
1878 plogf(
" --- vcs_solve_tp: - However, NC Element Abundance criteria is satisfied \n");
1879 plogf(
" --- vcs_solve_tp: - Returning the calculated equilibrium condition ");
1883 rangeErrorFound = 1;
1884 giveUpOnElemAbund =
true;
1885 goto L_EQUILIB_CHECK;
1893 goto L_EQUILIB_CHECK;
1910 npb = vcs_recheck_deleted();
1915 goto L_RETURN_BLOCK_B;
1925 goto L_MAINLOOP_ALL_SPECIES;
1932 npb = vcs_recheck_deleted();
1945 goto L_MAINLOOP_ALL_SPECIES;
1955 npb = vcs_add_all_deleted();
1959 if (m_debug_print_lvl >= 1) {
1960 plogf(
" --- add_all_deleted(): some rxns not converged. RETURNING TO LOOP!");
1964 goto L_MAINLOOP_ALL_SPECIES;
1989 vcs_vdzero(m_molNumSpecies_new, m_numSpeciesTot);
1990 for (kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
1991 if (m_SSPhase[kspec]) {
1992 m_molNumSpecies_new[kspec] = 1.0;
1994 iph = m_phaseID[kspec];
1995 if (m_tPhaseMoles_old[iph] != 0.0) {
1996 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec] / m_tPhaseMoles_old[iph];
2004 i = m_speciesLocalPhaseIndex[kspec];
2005 Vphase = m_VolPhaseList[iph];
2011 if (rangeErrorFound) {
2022 m_VCount->Time_vcs_TP = tsecond;
2023 m_VCount->T_Time_vcs_TP += m_VCount->Time_vcs_TP;
2024 (m_VCount->T_Calls_vcs_TP)++;
2025 m_VCount->T_Its += m_VCount->Its;
2026 m_VCount->T_Basis_Opts += m_VCount->Basis_Opts;
2027 m_VCount->T_Time_basopt += m_VCount->Time_basopt;
2035 double VCS_SOLVE::vcs_minor_alt_calc(
size_t kspec,
size_t irxn,
bool* do_delete
2042 double w_kspec = m_molNumSpecies_old[kspec];
2043 double molNum_kspec_new;
2045 double dg_irxn = m_deltaGRxn_old[irxn];
2047 size_t iph = m_phaseID[kspec];
2051 if (w_kspec <= 0.0) {
2054 if (dg_irxn < -200.) {
2058 sprintf(ANOTE,
"minor species alternative calc");
2060 if (dg_irxn >= 23.0) {
2061 molNum_kspec_new = w_kspec * 1.0e-10;
2063 goto L_ZERO_SPECIES;
2065 dx = molNum_kspec_new - w_kspec;
2068 if (fabs(dg_irxn) <= m_tolmin2) {
2069 molNum_kspec_new = w_kspec;
2077 s = m_dLnActCoeffdMolNum[kspec][kspec];
2094 if (a < (-1.0 + 1.0E-8)) {
2096 }
else if (a > 100.0) {
2099 tmp = -dg_irxn / (1.0 + a);
2102 }
else if (tmp > 200.) {
2105 wTrial = w_kspec * exp(tmp);
2108 molNum_kspec_new = wTrial;
2110 if (wTrial > 100. * w_kspec) {
2111 double molNumMax = 0.0001 * m_tPhaseMoles_old[iph];
2112 if (molNumMax < 100. * w_kspec) {
2113 molNumMax = 100. * w_kspec;
2115 if (wTrial > molNumMax) {
2116 molNum_kspec_new = molNumMax;
2118 molNum_kspec_new = wTrial;
2121 }
else if (1.0E10 * wTrial < w_kspec) {
2122 molNum_kspec_new= 1.0E-10 * w_kspec;
2124 molNum_kspec_new = wTrial;
2129 goto L_ZERO_SPECIES;
2131 dx = molNum_kspec_new - w_kspec;
2149 dx = m_deltaGRxn_old[irxn]/ m_Faraday_dim;
2151 sprintf(ANOTE,
"voltage species alternative calc");
2171 int VCS_SOLVE::delta_species(
const size_t kspec,
double*
const delta_ptr)
2173 size_t irxn = kspec - m_numComponents;
2176 double delta = *delta_ptr;
2178 if (kspec < m_numComponents) {
2179 plogf(
" --- delete_species() ERROR: called for a component %d", kspec);
2190 double* sc_irxn = m_stoichCoeffRxnMatrix[irxn];
2191 for (
size_t j = 0; j < m_numComponents; ++j) {
2192 if (m_molNumSpecies_old[j] > 0.0) {
2193 tmp = sc_irxn[j] * dx;
2194 if (-tmp > m_molNumSpecies_old[j]) {
2196 dx =
std::min(dx, - m_molNumSpecies_old[j] / sc_irxn[j]);
2203 if (m_molNumSpecies_old[j] <= 0.0) {
2204 if (sc_irxn[j] < 0.0) {
2214 m_molNumSpecies_old[kspec] += dx;
2215 size_t iph = m_phaseID[kspec];
2216 m_tPhaseMoles_old[iph] += dx;
2219 for (
size_t j = 0; j < m_numComponents; ++j) {
2220 tmp = sc_irxn[j] * dx;
2223 m_molNumSpecies_old[j] += tmp;
2224 m_tPhaseMoles_old[iph] += tmp;
2226 if (m_molNumSpecies_old[j] < 0.0) {
2227 m_molNumSpecies_old[j] = 0.0;
2249 int VCS_SOLVE::vcs_zero_species(
const size_t kspec)
2256 double dx = -(m_molNumSpecies_old[kspec]);
2258 retn = delta_species(kspec, &dx);
2261 if (m_debug_print_lvl >= 1) {
2262 plogf(
"vcs_zero_species: Couldn't zero the species %d, "
2263 "did delta of %g. orig conc of %g",
2264 kspec, dx, m_molNumSpecies_old[kspec] + dx);
2289 int VCS_SOLVE::vcs_delete_species(
const size_t kspec)
2291 const size_t klast = m_numSpeciesRdc - 1;
2292 const size_t iph = m_phaseID[kspec];
2294 const size_t irxn = kspec - m_numComponents;
2299 const int retn = vcs_zero_species(kspec);
2300 if (DEBUG_MODE_ENABLED && !retn) {
2301 plogf(
"Failed to delete a species!");
2310 --(m_numRxnMinorZeroed);
2313 m_deltaGRxn_new[irxn] = 0.0;
2314 m_deltaGRxn_old[irxn] = 0.0;
2315 m_feSpecies_new[kspec] = 0.0;
2316 m_feSpecies_old[kspec] = 0.0;
2317 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec];
2322 if (kspec != klast) {
2323 vcs_switch_pos(
true, klast, kspec);
2336 --(m_numSpeciesRdc);
2342 if (! m_SSPhase[klast]) {
2344 bool stillExists =
false;
2345 for (
size_t k = 0; k < m_numSpeciesRdc; k++) {
2347 if (m_phaseID[k] == iph) {
2348 if (m_molNumSpecies_old[k] > 0.0) {
2356 vcs_delete_multiphase(iph);
2364 return (m_numRxnRdc == 0);
2385 void VCS_SOLVE::vcs_reinsert_deleted(
size_t kspec)
2389 size_t iph = m_phaseID[kspec];
2392 if (m_debug_print_lvl >= 2) {
2393 plogf(
" --- Add back a deleted species: %-12s\n", m_speciesName[kspec].c_str());
2402 delta_species(kspec, &dx);
2405 if (m_SSPhase[kspec]) {
2407 --(m_numRxnMinorZeroed);
2410 vcs_VolPhase* Vphase = m_VolPhaseList[iph];
2423 if (! m_SSPhase[kspec]) {
2426 for (k = 0; k < m_numSpeciesTot; k++) {
2427 if (m_phaseID[k] == iph) {
2439 ++(m_numSpeciesRdc);
2440 ++(m_numRxnMinorZeroed);
2442 if (kspec != (m_numSpeciesRdc - 1)) {
2446 vcs_switch_pos(
true, (m_numSpeciesRdc - 1), kspec);
2465 bool VCS_SOLVE::vcs_delete_multiphase(
const size_t iph)
2470 bool successful =
true;
2476 if (m_debug_print_lvl >= 2) {
2477 plogf(
" --- delete_multiphase %d, %s\n", iph, Vphase->
PhaseName.c_str());
2484 for (kspec = m_numComponents; kspec < m_numSpeciesRdc; ++kspec) {
2485 if (m_phaseID[kspec] == iph) {
2490 dx = - (m_molNumSpecies_old[kspec]);
2493 int retn = delta_species(kspec, &dxTent);
2497 if (m_debug_print_lvl >= 2) {
2498 plogf(
" --- delete_multiphase %d, %s ERROR problems deleting species %s\n",
2499 iph, Vphase->
PhaseName.c_str(), m_speciesName[kspec].c_str());
2500 plogf(
" --- delta attempted: %g achieved: %g "
2501 " Zeroing it manually\n", dx, dxTent);
2504 m_molNumSpecies_old[kspec] = 0.0;
2505 m_molNumSpecies_new[kspec] = 0.0;
2506 m_deltaMolNumSpecies[kspec] = 0.0;
2513 m_molNumSpecies_old[kspec] = 0.0;
2514 m_molNumSpecies_new[kspec] = 0.0;
2515 m_deltaMolNumSpecies[kspec] = 0.0;
2526 double dj, dxWant, dxPerm = 0.0, dxPerm2 = 0.0;
2527 for (
size_t kcomp = 0; kcomp < m_numComponents; ++kcomp) {
2528 if (m_phaseID[kcomp] == iph) {
2530 if (m_debug_print_lvl >= 2) {
2531 plogf(
" --- delete_multiphase One of the species is a component %d - %s with mole number %g\n",
2532 kcomp, m_speciesName[kcomp].c_str(), m_molNumSpecies_old[kcomp]);
2535 if (m_molNumSpecies_old[kcomp] != 0.0) {
2536 for (kspec = m_numComponents; kspec < m_numSpeciesRdc; ++kspec) {
2537 irxn = kspec - m_numComponents;
2538 if (m_phaseID[kspec] != iph) {
2539 if (m_stoichCoeffRxnMatrix[irxn][kcomp] != 0.0) {
2540 dxWant = -m_molNumSpecies_old[kcomp] / m_stoichCoeffRxnMatrix[irxn][kcomp];
2541 if (dxWant + m_molNumSpecies_old[kspec] < 0.0) {
2542 dxPerm = -m_molNumSpecies_old[kspec];
2544 for (
size_t jcomp = 0; kcomp < m_numComponents; ++kcomp) {
2545 if (jcomp != kcomp) {
2546 if (m_phaseID[jcomp] == iph) {
2549 dj = dxWant * m_stoichCoeffRxnMatrix[irxn][jcomp];
2550 if (dj + m_molNumSpecies_old[kcomp] < 0.0) {
2551 dxPerm2 = -m_molNumSpecies_old[kcomp] / m_stoichCoeffRxnMatrix[irxn][jcomp];
2553 if (fabs(dxPerm2) < fabs(dxPerm)) {
2560 if (dxPerm != 0.0) {
2561 delta_species(kspec, &dxPerm);
2567 if (m_molNumSpecies_old[kcomp] != 0.0) {
2569 if (m_debug_print_lvl >= 2) {
2570 plogf(
" --- delete_multiphase One of the species is a component %d - %s still with mole number %g\n",
2571 kcomp, m_speciesName[kcomp].c_str(), m_molNumSpecies_old[kcomp]);
2572 plogf(
" --- zeroing it \n");
2575 m_molNumSpecies_old[kcomp] = 0.0;
2591 for (kspec = m_numSpeciesRdc; kspec < m_numSpeciesTot; ++kspec) {
2592 if (m_phaseID[kspec] == iph) {
2593 irxn = kspec - m_numComponents;
2594 m_molNumSpecies_old[kspec] = 0.0;
2595 m_molNumSpecies_new[kspec] = 0.0;
2596 m_deltaMolNumSpecies[kspec] = 0.0;
2600 ++(m_numSpeciesRdc);
2602 if (m_debug_print_lvl >= 2) {
2603 plogf(
" --- Make %s", m_speciesName[kspec].c_str());
2604 plogf(
" an active but zeroed species because its phase "
2608 if (kspec != (m_numSpeciesRdc - 1)) {
2612 vcs_switch_pos(
true, (m_numSpeciesRdc - 1), kspec);
2620 m_tPhaseMoles_old[iph] = 0.0;
2621 m_tPhaseMoles_new[iph] = 0.0;
2622 m_deltaPhaseMoles[iph] = 0.0;
2649 int VCS_SOLVE::vcs_recheck_deleted()
2652 size_t iph, kspec, irxn;
2655 if (m_debug_print_lvl >= 2) {
2656 plogf(
" --- Start rechecking deleted species in multispec phases\n");
2659 if (m_numSpeciesRdc == m_numSpeciesTot) {
2668 for (kspec = m_numSpeciesRdc; kspec < m_numSpeciesTot; ++kspec) {
2669 iph = m_phaseID[kspec];
2670 m_feSpecies_new[kspec] = (m_SSfeSpecies[kspec] + log(m_actCoeffSpecies_old[kspec])
2671 - m_lnMnaughtSpecies[kspec]
2672 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iph]);
2681 for (iph = 0; iph < m_numPhases; iph++) {
2682 if (m_tPhaseMoles_old[iph] > 0.0) {
2685 xtcutoff[iph] = 0.0;
2716 for (irxn = m_numRxnRdc; irxn < m_numRxnTot; ++irxn) {
2717 kspec = m_indexRxnToSpecies[irxn];
2718 iph = m_phaseID[kspec];
2719 if (m_tPhaseMoles_old[iph] == 0.0) {
2720 if (m_deltaGRxn_new[irxn] < 0.0) {
2721 vcs_reinsert_deleted(kspec);
2724 m_molNumSpecies_old[kspec] = 0.0;
2726 }
else if (m_tPhaseMoles_old[iph] > 0.0) {
2727 if (m_deltaGRxn_new[irxn] < xtcutoff[iph]) {
2728 vcs_reinsert_deleted(kspec);
2737 bool VCS_SOLVE::recheck_deleted_phase(
const int iphase)
2751 irxn = kspec + m_numComponents;
2752 if (m_deltaGRxn_old[irxn] < 0.0) {
2758 double phaseDG = 1.0;
2759 for (
size_t kk = 0; kk < Vphase->
nSpecies(); kk++) {
2761 irxn = kspec + m_numComponents;
2762 if (m_deltaGRxn_old[irxn] > 50.0) {
2763 m_deltaGRxn_old[irxn] = 50.0;
2765 if (m_deltaGRxn_old[irxn] < -50.0) {
2766 m_deltaGRxn_old[irxn] = -50.0;
2768 phaseDG -= exp(-m_deltaGRxn_old[irxn]);
2771 if (phaseDG < 0.0) {
2783 size_t VCS_SOLVE::vcs_add_all_deleted()
2785 size_t iph, kspec, retn;
2786 if (m_numSpeciesRdc == m_numSpeciesTot) {
2797 for (
int cits = 0; cits < 3; cits++) {
2798 for (kspec = m_numSpeciesRdc; kspec < m_numSpeciesTot; ++kspec) {
2799 iph = m_phaseID[kspec];
2801 if (m_molNumSpecies_new[kspec] == 0.0) {
2807 m_feSpecies_new[kspec] = (m_SSfeSpecies[kspec] + log(m_actCoeffSpecies_new[kspec]) - m_lnMnaughtSpecies[kspec]
2808 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iph]);
2814 for (
size_t irxn = m_numRxnRdc; irxn < m_numRxnTot; ++irxn) {
2815 kspec = m_indexRxnToSpecies[irxn];
2816 iph = m_phaseID[kspec];
2817 if (m_tPhaseMoles_old[iph] > 0.0) {
2818 double maxDG =
std::min(m_deltaGRxn_new[irxn], 690.0);
2819 double dx = m_tPhaseMoles_old[iph] * exp(- maxDG);
2820 m_molNumSpecies_new[kspec] = dx;
2827 for (
size_t irxn = m_numRxnRdc; irxn < m_numRxnTot; ++irxn) {
2828 kspec = m_indexRxnToSpecies[irxn];
2829 iph = m_phaseID[kspec];
2830 if (m_tPhaseMoles_old[iph] > 0.0) {
2831 double dx = m_molNumSpecies_new[kspec];
2832 retn = delta_species(kspec, &dx);
2835 if (m_debug_print_lvl) {
2836 plogf(
" --- add_deleted(): delta_species() failed for species %s (%d) with mol number %g\n",
2837 m_speciesName[kspec].c_str(), kspec, dx);
2842 retn = delta_species(kspec, &dx);
2845 if (m_debug_print_lvl) {
2846 plogf(
" --- add_deleted(): delta_species() failed for species %s (%d) with mol number %g\n",
2847 m_speciesName[kspec].c_str(), kspec, dx);
2854 if (m_debug_print_lvl >= 2) {
2856 plogf(
" --- add_deleted(): species %s added back in with mol number %g",
2857 m_speciesName[kspec].c_str(), dx);
2860 plogf(
" --- add_deleted(): species %s failed to be added back in");
2872 for (
size_t irxn = m_numRxnRdc; irxn < m_numRxnTot; ++irxn) {
2873 kspec = m_indexRxnToSpecies[irxn];
2874 iph = m_phaseID[kspec];
2875 if (m_tPhaseMoles_old[iph] > 0.0) {
2876 if (fabs(m_deltaGRxn_old[irxn]) > m_tolmin) {
2877 if (((m_molNumSpecies_old[kspec] * exp(-m_deltaGRxn_old[irxn])) >
2882 if (m_debug_print_lvl >= 2) {
2883 plogf(
" --- add_deleted(): species %s with mol number %g not converged: DG = %g",
2884 m_speciesName[kspec].c_str(), m_molNumSpecies_old[kspec],
2885 m_deltaGRxn_old[irxn]);
2917 bool VCS_SOLVE::vcs_globStepDamp()
2920 size_t irxn, kspec, iph;
2927 for (irxn = 0; irxn < m_numRxnRdc; ++irxn) {
2928 kspec = irxn + m_numComponents;
2930 s2 += dptr[irxn] * m_deltaMolNumSpecies[kspec];
2940 for (irxn = 0; irxn < m_numRxnRdc; ++irxn) {
2941 kspec = irxn + m_numComponents;
2943 s1 += dptr[irxn] * m_deltaMolNumSpecies[kspec];
2948 if (m_debug_print_lvl >= 2) {
2949 plogf(
" --- subroutine FORCE: Beginning Slope = %g\n", s1);
2950 plogf(
" --- subroutine FORCE: End Slope = %g\n", s2);
2956 if (m_debug_print_lvl >= 2) {
2957 plogf(
" --- subroutine FORCE produced no adjustments,");
2959 plogf(
" s1 positive but really small");
2961 plogf(
" failed s1 test");
2971 if (m_debug_print_lvl >= 2) {
2972 plogf(
" --- subroutine FORCE produced no adjustments, s2 < 0");
2983 if (fabs(s1 -s2) > 1.0E-200) {
2984 al = s1 / (s1 - s2);
2986 if (al >= 0.95 || al < 0.0) {
2988 if (m_debug_print_lvl >= 2) {
2989 plogf(
" --- subroutine FORCE produced no adjustments (al = %g)\n", al);
2995 if (m_debug_print_lvl >= 2) {
2996 plogf(
" --- subroutine FORCE produced a damping factor = %g\n", al);
3004 if (m_debug_print_lvl >= 2) {
3011 for (kspec = 0; kspec < m_numSpeciesRdc; ++kspec) {
3012 m_molNumSpecies_new[kspec] = m_molNumSpecies_old[kspec] +
3013 al * m_deltaMolNumSpecies[kspec];
3015 for (iph = 0; iph < m_numPhases; iph++) {
3016 m_tPhaseMoles_new[iph] = m_tPhaseMoles_old[iph] + al * m_deltaPhaseMoles[iph];
3021 if (m_debug_print_lvl >= 2) {
3022 plogf(
" --- subroutine FORCE adjusted the mole "
3023 "numbers, AL = %10.3f\n", al);
3043 for (irxn = 0; irxn < m_numRxnRdc; ++irxn) {
3044 kspec = irxn + m_numComponents;
3046 s2 += dptr[irxn] * m_deltaMolNumSpecies[kspec];
3052 if (m_debug_print_lvl >= 2) {
3053 plogf(
" --- subroutine FORCE: Adj End Slope = %g", s2);
3061 int VCS_SOLVE::vcs_basopt(
const bool doJustComponents,
double aw[],
double sa[],
double sm[],
3062 double ss[],
double test,
bool*
const usedZeroedSpecies)
3064 size_t j, k, l, i, jl, ml, jr, irxn, kspec;
3068 size_t jlose =
npos;
3069 double* dptr, *scrxn_ptr;
3072 if (m_debug_print_lvl >= 2) {
3074 for (i=0; i<77; i++) {
3078 plogf(
" --- Subroutine BASOPT called to ");
3079 if (doJustComponents) {
3080 plogf(
"calculate the number of components\n");
3082 plogf(
"reevaluate the components\n");
3084 if (m_debug_print_lvl >= 2) {
3086 plogf(
" --- Formula Matrix used in BASOPT calculation\n");
3087 plogf(
" --- Active | ");
3088 for (j = 0; j < m_numElemConstraints; j++) {
3089 plogf(
" %1d ", m_elementActive[j]);
3092 plogf(
" --- Species | ");
3093 for (j = 0; j < m_numElemConstraints; j++) {
3095 vcs_print_stringTrunc(m_elementName[j].c_str(), 8, 1);
3098 for (k = 0; k < m_numSpeciesTot; k++) {
3100 vcs_print_stringTrunc(m_speciesName[k].c_str(), 11, 1);
3102 for (j = 0; j < m_numElemConstraints; j++) {
3103 plogf(
" %8.2g", m_formulaMatrix[j][k]);
3117 ncTrial =
std::min(m_numElemConstraints, m_numSpeciesTot);
3118 m_numComponents = ncTrial;
3119 *usedZeroedSpecies =
false;
3124 vcs_dcopy(aw,
VCS_DATA_PTR(m_molNumSpecies_old), m_numSpeciesTot);
3128 for (k = 0; k < m_numSpeciesTot; k++) {
3150 k = vcs_basisOptMax(aw, jr, m_numSpeciesTot);
3192 *usedZeroedSpecies =
true;
3194 double maxConcPossKspec = 0.0;
3195 double maxConcPoss = 0.0;
3196 size_t kfound =
npos;
3197 int minNonZeroes = 100000;
3198 int nonZeroesKspec = 0;
3199 for (kspec = ncTrial; kspec < m_numSpeciesTot; kspec++) {
3200 if (aw[kspec] >= 0.0) {
3202 maxConcPossKspec = 1.0E10;
3204 for (
size_t j = 0; j < m_numElemConstraints; ++j) {
3205 if (m_elementActive[j]) {
3207 double nu = m_formulaMatrix[j][kspec];
3210 maxConcPossKspec =
std::min(m_elemAbundancesGoal[j] / nu, maxConcPossKspec);
3215 if ((maxConcPossKspec >= maxConcPoss) || (maxConcPossKspec > 1.0E-5)) {
3216 if (nonZeroesKspec <= minNonZeroes) {
3217 if (kfound ==
npos || nonZeroesKspec < minNonZeroes) {
3221 if (m_SSfeSpecies[kspec] <= m_SSfeSpecies[kfound]) {
3226 if (nonZeroesKspec < minNonZeroes) {
3227 minNonZeroes = nonZeroesKspec;
3229 if (maxConcPossKspec > maxConcPoss) {
3230 maxConcPoss = maxConcPossKspec;
3236 if (kfound ==
npos) {
3239 for (kspec = ncTrial; kspec < m_numSpeciesTot; kspec++) {
3240 if (aw[kspec] >= 0.0) {
3241 irxn = kspec - ncTrial;
3242 if (m_deltaGRxn_new[irxn] < gmin) {
3243 gmin = m_deltaGRxn_new[irxn];
3253 if (aw[k] == test) {
3254 m_numComponents = jr;
3255 ncTrial = m_numComponents;
3256 size_t numPreDeleted = m_numRxnTot - m_numRxnRdc;
3257 if (numPreDeleted != (m_numSpeciesTot - m_numSpeciesRdc)) {
3258 plogf(
"vcs_basopt:: we shouldn't be here\n");
3261 m_numRxnTot = m_numSpeciesTot - ncTrial;
3262 m_numRxnRdc = m_numRxnTot - numPreDeleted;
3263 m_numSpeciesRdc = m_numSpeciesTot - numPreDeleted;
3264 for (i = 0; i < m_numSpeciesTot; ++i) {
3265 m_indexRxnToSpecies[i] = ncTrial + i;
3268 if (m_debug_print_lvl >= 2) {
3269 plogf(
" --- Total number of components found = %3d (ne = %d)\n ",
3270 ncTrial, m_numElemConstraints);
3288 for (j = 0; j < m_numElemConstraints; ++j) {
3289 sm[j + jr*m_numElemConstraints] = m_formulaMatrix[j][k];
3298 for (j = 0; j < jl; ++j) {
3300 for (i = 0; i < m_numElemConstraints; ++i) {
3301 ss[j] += sm[i + jr*m_numElemConstraints] * sm[i + j*m_numElemConstraints];
3309 for (j = 0; j < jl; ++j) {
3310 for (l = 0; l < m_numElemConstraints; ++l) {
3311 sm[l + jr*m_numElemConstraints] -= ss[j] * sm[l + j*m_numElemConstraints];
3320 for (ml = 0; ml < m_numElemConstraints; ++ml) {
3321 sa[jr] += SQUARE(sm[ml + jr*m_numElemConstraints]);
3326 lindep = (sa[jr] < 1.0e-6);
3333 if (m_debug_print_lvl >= 2) {
3334 plogf(
" --- %-12.12s", (m_speciesName[k]).c_str());
3335 plogf(
"(%9.2g) replaces %-12.12s", m_molNumSpecies_old[k],
3336 m_speciesName[jr].c_str());
3337 plogf(
"(%9.2g) as component %3d\n", m_molNumSpecies_old[jr], jr);
3340 vcs_switch_pos(
false, jr, k);
3341 std::swap(aw[jr], aw[k]);
3345 if (m_debug_print_lvl >= 2) {
3346 plogf(
" --- %-12.12s", m_speciesName[k].c_str());
3347 plogf(
"(%9.2g) remains ", m_molNumSpecies_old[k]);
3348 plogf(
" as component %3d\n", jr);
3360 }
while (jr < (ncTrial-1));
3362 if (doJustComponents) {
3399 for (j = 0; j < ncTrial; ++j) {
3400 for (i = 0; i < ncTrial; ++i) {
3401 sm[i + j*m_numElemConstraints] = m_formulaMatrix[i][j];
3404 for (i = 0; i < m_numRxnTot; ++i) {
3405 k = m_indexRxnToSpecies[i];
3406 for (j = 0; j < ncTrial; ++j) {
3407 m_stoichCoeffRxnMatrix[i][j] = m_formulaMatrix[j][k];
3415 j = vcsUtil_gaussj(sm, m_numElemConstraints, ncTrial, m_stoichCoeffRxnMatrix[0], m_numRxnTot);
3420 plogf(
"vcs_solve_TP ERROR: mlequ returned an error condition\n");
3421 return VCS_FAILED_CONVERGENCE;
3431 for (j = 0; j < m_numElemConstraints; j++) {
3432 if (!(m_elementActive[j])) {
3433 if (!strcmp((m_elementName[j]).c_str(),
"E")) {
3438 for (j = 0; j < m_numElemConstraints; j++) {
3439 if (m_elementActive[j]) {
3440 if (!strncmp((m_elementName[j]).c_str(),
"cn_", 3)) {
3445 for (k = 0; k < m_numSpeciesTot; k++) {
3448 for (j = 0; j < ncTrial; ++j) {
3449 for (i = 0; i < ncTrial; ++i) {
3451 sm[i + j*m_numElemConstraints] = m_formulaMatrix[juse][j];
3453 sm[i + j*m_numElemConstraints] = m_formulaMatrix[i][j];
3457 for (i = 0; i < m_numRxnTot; ++i) {
3458 k = m_indexRxnToSpecies[i];
3459 for (j = 0; j < ncTrial; ++j) {
3461 aw[j] = m_formulaMatrix[juse][k];
3463 aw[j] = m_formulaMatrix[j][k];
3468 j = vcsUtil_gaussj(sm, m_numElemConstraints, ncTrial, aw, 1);
3471 plogf(
"vcs_solve_TP ERROR: mlequ returned an error condition\n");
3472 return VCS_FAILED_CONVERGENCE;
3475 for (j = 0; j < ncTrial; j++) {
3476 m_stoichCoeffRxnMatrix[i][j] = aw[j];
3485 for (i = 0; i < m_numRxnTot; i++) {
3487 for (j = 0; j < ncTrial; j++) {
3488 szTmp += fabs(m_stoichCoeffRxnMatrix[i][j]);
3490 m_scSize[i] = szTmp;
3495 if (m_debug_print_lvl >= 2) {
3496 plogf(
" --- Components:");
3497 for (j = 0; j < ncTrial; j++) {
3500 plogf(
"\n --- Components Moles:");
3501 for (j = 0; j < ncTrial; j++) {
3502 plogf(
" % -10.3E", m_molNumSpecies_old[j]);
3504 plogf(
"\n --- NonComponent| Moles |");
3505 for (j = 0; j < ncTrial; j++) {
3506 plogf(
" %10.10s", m_speciesName[j].c_str());
3510 for (i = 0; i < m_numRxnTot; i++) {
3511 plogf(
" --- %3d ", m_indexRxnToSpecies[i]);
3512 plogf(
"%-10.10s", m_speciesName[m_indexRxnToSpecies[i]].c_str());
3513 plogf(
"|% -10.3E|", m_molNumSpecies_old[m_indexRxnToSpecies[i]]);
3514 for (j = 0; j < ncTrial; j++) {
3515 plogf(
" %+7.3f", m_stoichCoeffRxnMatrix[i][j]);
3527 double sumMax = -1.0;
3531 for (i = 0; i < m_numRxnTot; ++i) {
3532 k = m_indexRxnToSpecies[i];
3533 for (j = 0; j < ncTrial; ++j) {
3535 sum = m_formulaMatrix[juse][k];
3536 for (n = 0; n < ncTrial; n++) {
3537 double numElements = m_formulaMatrix[juse][n];
3538 double coeff = m_stoichCoeffRxnMatrix[i][n];
3539 sum += coeff * numElements;
3542 sum = m_formulaMatrix[j][k];
3543 for (n = 0; n < ncTrial; n++) {
3544 double numElements = m_formulaMatrix[j][n];
3545 double coeff = m_stoichCoeffRxnMatrix[i][n];
3546 sum += coeff * numElements;
3549 if (fabs(sum) > sumMax) {
3557 if (fabs(sum) > 1.0E-6) {
3558 printf(
"we have a prob\n");
3563 plogf(
" --- largest error in Stoich coeff = %g at rxn = %d ", sumMax, iMax);
3564 plogf(
"%-10.10s", m_speciesName[m_indexRxnToSpecies[iMax]].c_str());
3565 plogf(
" element = %d ", jMax);
3566 plogf(
"%-5.5s", m_elementName[jMax].c_str());
3569 for (i=0; i<77; i++) {
3586 vcs_dzero(m_deltaMolNumPhase[0], (NSPECIES0)*(NPHASE0));
3587 vcs_izero(m_phaseParticipation[0], (NSPECIES0)*(NPHASE0));
3593 for (irxn = 0; irxn < m_numRxnTot; ++irxn) {
3594 scrxn_ptr = m_stoichCoeffRxnMatrix[irxn];
3595 dptr = m_deltaMolNumPhase[irxn];
3596 kspec = m_indexRxnToSpecies[irxn];
3597 size_t iph = m_phaseID[kspec];
3598 int* pp_ptr = m_phaseParticipation[irxn];
3601 for (j = 0; j < ncTrial; ++j) {
3603 if (fabs(scrxn_ptr[j]) <= 1.0e-6) {
3606 dptr[iph] += scrxn_ptr[j];
3615 m_VCount->Time_basopt += tsecond;
3616 (m_VCount->Basis_Opts)++;
3646 size_t VCS_SOLVE::vcs_basisOptMax(
const double*
const molNum,
const size_t j,
3650 double big = molNum[j] * m_spSize[j] * 1.01;
3652 for (
size_t i = j + 1; i < n; ++i) {
3654 bool doSwap =
false;
3656 doSwap = (molNum[i] * m_spSize[i]) > (big);
3657 if (!m_SSPhase[i]) {
3659 doSwap = (molNum[i]) > (molNum[largest] * 1.001);
3664 doSwap = (molNum[i] * m_spSize[i]) > (big);
3666 doSwap = (molNum[i]) > (molNum[largest] * 1.001);
3669 doSwap = (molNum[i] * m_spSize[i]) > (big);
3674 big = molNum[i] * m_spSize[i] * 1.01;
3689 int VCS_SOLVE::vcs_species_type(
const size_t kspec)
const
3699 size_t iph = m_phaseID[kspec];
3700 int irxn = int(kspec) - int(m_numComponents);
3702 int phaseExist = VPhase->
exists();
3706 if (m_molNumSpecies_old[kspec] <= 0.0) {
3708 if (m_tPhaseMoles_old[iph] <= 0.0) {
3709 if (!m_SSPhase[kspec]) {
3719 for (
size_t j = 0; j < m_numElemConstraints; ++j) {
3720 int elType = m_elType[j];
3722 double atomComp = m_formulaMatrix[j][kspec];
3723 if (atomComp > 0.0) {
3724 double maxPermissible = m_elemAbundancesGoal[j] / atomComp;
3727 if (m_debug_print_lvl >= 2) {
3728 plogf(
" --- %s can not be nonzero because"
3729 " needed element %s is zero\n",
3730 m_speciesName[kspec].c_str(), (m_elementName[j]).c_str());
3733 if (m_SSPhase[kspec]) {
3753 for (
size_t j = 0; j < m_numComponents; ++j) {
3754 double stoicC = m_stoichCoeffRxnMatrix[irxn][j];
3755 if (stoicC != 0.0) {
3756 double negChangeComp = - stoicC;
3757 if (negChangeComp > 0.0) {
3758 if (m_molNumSpecies_old[j] < 1.0E-60) {
3760 if (m_debug_print_lvl >= 2) {
3761 plogf(
" --- %s is prevented from popping into existence because"
3762 " a needed component to be consumed, %s, has a zero mole number\n",
3763 m_speciesName[kspec].c_str(), m_speciesName[j].c_str());
3766 if (m_SSPhase[kspec]) {
3772 }
else if (negChangeComp < 0.0) {
3773 size_t jph = m_phaseID[j];
3775 if (jVPhase->
exists() <= 0) {
3777 if (m_debug_print_lvl >= 2) {
3778 plogf(
" --- %s is prevented from popping into existence because"
3779 " a needed component %s is in a zeroed-phase that would be "
3780 "popped into existence at the same time\n",
3781 m_speciesName[kspec].c_str(), m_speciesName[j].c_str());
3784 if (m_SSPhase[kspec]) {
3796 if (m_deltaGRxn_old[irxn] >= 0.0) {
3800 if (m_SSPhase[kspec]) {
3816 if (m_tPhaseMoles_old[iph] > 0.0) {
3817 if (m_SSPhase[kspec]) {
3833 if (m_tPhaseMoles_old[iph] <= 0.0) {
3834 if (m_SSPhase[kspec]) {
3848 if (m_SSPhase[kspec]) {
3859 if (m_molNumSpecies_old[kspec] > (m_tPhaseMoles_old[iph] * 0.001)) {
3874 double szAdj = m_scSize[irxn] * std::sqrt((
double)m_numRxnTot);
3875 for (
size_t k = 0; k < m_numComponents; ++k) {
3876 if (!(m_SSPhase[k])) {
3877 if (m_stoichCoeffRxnMatrix[irxn][k] != 0.0) {
3878 if (m_molNumSpecies_old[kspec] * szAdj >= m_molNumSpecies_old[k] * 0.01) {
3984 void VCS_SOLVE::vcs_chemPotPhase(
const int stateCalc,
3985 const size_t iph,
const double*
const molNum,
3986 double*
const ac,
double*
const mu_i,
3987 const bool do_deleted)
4000 double tMoles = TPhInertMoles[iph];
4001 for (k = 0; k < nkk; k++) {
4003 tMoles += molNum[kspec];
4005 double tlogMoles = 0.0;
4007 tlogMoles = log(tMoles);
4014 double Faraday_phi = m_Faraday_dim * phi;
4016 for (k = 0; k < nkk; k++) {
4018 if (kspec >= m_numComponents) {
4026 if (molNum[kspec] != phi) {
4027 plogf(
"We have an inconsistency!\n");
4030 if (m_chargeSpecies[kspec] != -1.0) {
4031 plogf(
"We have an unexpected situation!\n");
4035 mu_i[kspec] = m_SSfeSpecies[kspec] + m_chargeSpecies[kspec] * Faraday_phi;
4037 if (m_SSPhase[kspec]) {
4038 mu_i[kspec] = m_SSfeSpecies[kspec] + m_chargeSpecies[kspec] * Faraday_phi;
4041 - tlogMoles - m_lnMnaughtSpecies[kspec] + m_chargeSpecies[kspec] * Faraday_phi;
4043 mu_i[kspec] = m_SSfeSpecies[kspec] + log(ac[kspec] * molNum[kspec])
4044 - tlogMoles - m_lnMnaughtSpecies[kspec] + m_chargeSpecies[kspec] * Faraday_phi;
4179 void VCS_SOLVE::vcs_dfe(
const int stateCalc,
4180 const int ll,
const size_t lbot,
const size_t ltop)
4182 size_t l1, l2, iph, kspec, irxn;
4184 double* tPhMoles_ptr;
4185 double* actCoeff_ptr;
4204 plogf(
"vcs_dfe: wrong stateCalc value");
4205 plogf(
" --- Subroutine vcs_dfe called with bad stateCalc value: %d", stateCalc);
4213 printf(
"vcs_dfe: called with wrong units state\n");
4219 if (m_debug_print_lvl >= 2) {
4222 plogf(
" --- Subroutine vcs_dfe called for one species: ");
4223 plogf(
"%-12.12s", m_speciesName[lbot].c_str());
4225 plogf(
" --- Subroutine vcs_dfe called for all species");
4227 }
else if (ll > 0) {
4228 plogf(
" --- Subroutine vcs_dfe called for components and minors");
4230 plogf(
" --- Subroutine vcs_dfe called for components and majors");
4233 plogf(
" using tentative solution");
4245 for (iph = 0; iph < m_numPhases; iph++) {
4246 tlogMoles[iph] = tPhInertMoles[iph];
4249 for (kspec = 0; kspec < m_numSpeciesTot; kspec++) {
4251 iph = m_phaseID[kspec];
4252 tlogMoles[iph] += molNum[kspec];
4256 for (iph = 0; iph < m_numPhases; iph++) {
4257 if (! vcs_doubleEqual(tlogMoles[iph], tPhMoles_ptr[iph])) {
4258 plogf(
"phase Moles may be off, iph = %d, %20.14g %20.14g \n",
4259 iph, tlogMoles[iph], tPhMoles_ptr[iph]);
4264 vcs_dzero(tlogMoles, m_numPhases);
4265 for (iph = 0; iph < m_numPhases; iph++) {
4266 if (tPhMoles_ptr[iph] > 0.0) {
4267 tlogMoles[iph] = log(tPhMoles_ptr[iph]);
4274 l2 = m_numComponents;
4285 for (iphase = 0; iphase < m_numPhases; iphase++) {
4286 Vphase = m_VolPhaseList[iphase];
4302 for (kspec = l1; kspec < l2; ++kspec) {
4303 iphase = m_phaseID[kspec];
4306 if (molNum[kspec] != m_phasePhi[iphase]) {
4307 plogf(
"We have an inconsistency!\n");
4310 if (m_chargeSpecies[kspec] != -1.0) {
4311 plogf(
"We have an unexpected situation!\n");
4315 feSpecies[kspec] = m_SSfeSpecies[kspec]
4316 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4318 if (m_SSPhase[kspec]) {
4319 feSpecies[kspec] = m_SSfeSpecies[kspec]
4320 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4323 feSpecies[kspec] = m_SSfeSpecies[kspec] - m_lnMnaughtSpecies[kspec]
4324 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4327 iph = m_phaseID[kspec];
4328 if (tPhMoles_ptr[iph] > 0.0) {
4329 feSpecies[kspec] = m_SSfeSpecies[kspec]
4331 - tlogMoles[m_phaseID[kspec]] - m_lnMnaughtSpecies[kspec]
4332 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4334 feSpecies[kspec] = m_SSfeSpecies[kspec] - m_lnMnaughtSpecies[kspec]
4335 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4338 feSpecies[kspec] = m_SSfeSpecies[kspec]
4339 + log(actCoeff_ptr[kspec] * molNum[kspec])
4340 - tlogMoles[m_phaseID[kspec]] - m_lnMnaughtSpecies[kspec]
4341 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4350 for (irxn = 0; irxn < m_numRxnRdc; ++irxn) {
4351 kspec = m_indexRxnToSpecies[irxn];
4353 iphase = m_phaseID[kspec];
4356 if (molNum[kspec] != m_phasePhi[iphase]) {
4357 plogf(
"We have an inconsistency!\n");
4360 if (m_chargeSpecies[kspec] != -1.0) {
4361 plogf(
"We have an unexpected situation!\n");
4365 feSpecies[kspec] = m_SSfeSpecies[kspec]
4366 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4368 if (m_SSPhase[kspec]) {
4369 feSpecies[kspec] = m_SSfeSpecies[kspec]
4370 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4373 feSpecies[kspec] = m_SSfeSpecies[kspec] - m_lnMnaughtSpecies[kspec]
4374 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4377 iph = m_phaseID[kspec];
4378 if (tPhMoles_ptr[iph] > 0.0) {
4379 feSpecies[kspec] = m_SSfeSpecies[kspec]
4381 - tlogMoles[m_phaseID[kspec]] - m_lnMnaughtSpecies[kspec]
4382 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4384 feSpecies[kspec] = m_SSfeSpecies[kspec] - m_lnMnaughtSpecies[kspec]
4385 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4388 feSpecies[kspec] = m_SSfeSpecies[kspec]
4389 + log(actCoeff_ptr[kspec] * molNum[kspec])
4390 - tlogMoles[m_phaseID[kspec]] - m_lnMnaughtSpecies[kspec]
4391 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4400 }
else if (ll > 0) {
4401 for (irxn = 0; irxn < m_numRxnRdc; ++irxn) {
4402 kspec = m_indexRxnToSpecies[irxn];
4404 iphase = m_phaseID[kspec];
4407 if (molNum[kspec] != m_phasePhi[iphase]) {
4408 plogf(
"We have an inconsistency!\n");
4411 if (m_chargeSpecies[kspec] != -1.0) {
4412 plogf(
"We have an unexpected situation!\n");
4416 feSpecies[kspec] = m_SSfeSpecies[kspec]
4417 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase]; ;
4419 if (m_SSPhase[kspec]) {
4420 feSpecies[kspec] = m_SSfeSpecies[kspec]
4421 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4424 feSpecies[kspec] = m_SSfeSpecies[kspec] - m_lnMnaughtSpecies[kspec]
4425 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4428 iph = m_phaseID[kspec];
4429 if (tPhMoles_ptr[iph] > 0.0) {
4430 feSpecies[kspec] = m_SSfeSpecies[kspec]
4432 - tlogMoles[m_phaseID[kspec]] - m_lnMnaughtSpecies[kspec]
4433 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4435 feSpecies[kspec] = m_SSfeSpecies[kspec] - m_lnMnaughtSpecies[kspec]
4436 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4439 feSpecies[kspec] = m_SSfeSpecies[kspec]
4440 + log(actCoeff_ptr[kspec] * molNum[kspec])
4441 - tlogMoles[m_phaseID[kspec]] - m_lnMnaughtSpecies[kspec]
4442 + m_chargeSpecies[kspec] * m_Faraday_dim * m_phasePhi[iphase];
4454 void VCS_SOLVE::vcs_printSpeciesChemPot(
const int stateCalc)
const
4456 double mfValue = 1.0;
4457 bool zeroedPhase =
false;
4460 const double* molNum =
VCS_DATA_PTR(m_molNumSpecies_old);
4461 const double* actCoeff_ptr =
VCS_DATA_PTR(m_actCoeffSpecies_old);
4468 const double* tPhInertMoles =
VCS_DATA_PTR(TPhInertMoles);
4469 for (
size_t iph = 0; iph < m_numPhases; iph++) {
4470 tMoles[iph] = tPhInertMoles[iph];
4472 for (kspec = 0; kspec < m_numSpeciesTot; kspec++) {
4474 size_t iph = m_phaseID[kspec];
4475 tMoles[iph] += molNum[kspec];
4480 printf(
" --- CHEMICAL POT TABLE (J/kmol) Name PhID MolFR ChemoSS "
4481 " logMF Gamma Elect extra ElectrChem\n");
4483 vcs_print_line(
"-", 132);
4485 for (kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
4487 size_t iphase = m_phaseID[kspec];
4494 zeroedPhase =
false;
4496 if (tMoles[iphase] > 0.0) {
4500 mfValue = molNum[kspec]/tMoles[iphase];
4503 size_t klocal = m_speciesLocalPhaseIndex[kspec];
4504 mfValue = Vphase->moleFraction(klocal);
4507 double elect = m_chargeSpecies[kspec] * m_Faraday_dim * volts;
4508 double comb = - m_lnMnaughtSpecies[kspec];
4509 double total = (m_SSfeSpecies[kspec] + log(mfValue) + elect + log(actCoeff_ptr[kspec]) + comb);
4512 printf(
" --- ** zp *** ");
4516 printf(
"%-24.24s", m_speciesName[kspec].c_str());
4518 printf(
" % -12.4e", mfValue);
4519 printf(
" % -12.4e", m_SSfeSpecies[kspec] * RT);
4520 printf(
" % -12.4e", log(mfValue) * RT);
4521 printf(
" % -12.4e", log(actCoeff_ptr[kspec]) * RT);
4522 printf(
" % -12.4e", elect * RT);
4523 printf(
" % -12.4e", comb * RT);
4524 printf(
" % -12.4e\n", total *RT);
4527 vcs_print_line(
"-", 132);
4533 void VCS_SOLVE::prneav()
const
4537 std::vector<double> eav(m_numElemConstraints, 0.0);
4539 for (j = 0; j < m_numElemConstraints; ++j) {
4540 for (
size_t i = 0; i < m_numSpeciesTot; ++i) {
4542 eav[j] += m_formulaMatrix[j][i] * m_molNumSpecies_old[i];
4547 plogf(
"--------------------------------------------------");
4548 plogf(
"ELEMENT ABUNDANCE VECTOR:\n");
4549 plogf(
" Element Now Orignal Deviation Type\n");
4550 for (j = 0; j < m_numElemConstraints; ++j) {
4552 plogf(
"%-2.2s", (m_elementName[j]).c_str());
4553 plogf(
" = %15.6E %15.6E %15.6E %3d\n",
4554 eav[j], m_elemAbundancesGoal[j], eav[j] - m_elemAbundancesGoal[j], m_elType[j]);
4555 if (m_elemAbundancesGoal[j] != 0.) {
4556 if (fabs(eav[j] - m_elemAbundancesGoal[j]) > m_elemAbundancesGoal[j] * 5.0e-9) {
4560 if (fabs(eav[j]) > 1.0e-10) {
4566 plogf(
"Element abundance check failure\n");
4568 plogf(
"--------------------------------------------------");
4574 double VCS_SOLVE::l2normdg(
double dgLocal[])
const
4578 if (m_numRxnRdc <= 0) {
4581 for (irxn = 0, tmp = 0.0; irxn < m_numRxnRdc; ++irxn) {
4582 size_t kspec = irxn + m_numComponents;
4584 dgLocal[irxn] < 0.0) {
4586 tmp += dgLocal[irxn] * dgLocal[irxn];
4590 return (std::sqrt(tmp / m_numRxnRdc));
4599 double VCS_SOLVE::vcs_tmoles()
4603 for (
size_t i = 0; i < m_numPhases; i++) {
4604 m_tPhaseMoles_old[i] = TPhInertMoles[i];
4606 for (
size_t i = 0; i < m_numSpeciesTot; i++) {
4608 m_tPhaseMoles_old[m_phaseID[i]] += m_molNumSpecies_old[i];
4612 for (
size_t i = 0; i < m_numPhases; i++) {
4613 sum += m_tPhaseMoles_old[i];
4614 Vphase = m_VolPhaseList[i];
4617 if (m_tPhaseMoles_old[i] == 0.0) {
4623 m_totalMolNum = sum;
4624 return m_totalMolNum;
4628 void VCS_SOLVE::check_tmoles()
const
4632 for (i = 0; i < m_numPhases; i++) {
4633 double m_tPhaseMoles_old_a = TPhInertMoles[i];
4635 for (
size_t k = 0; k < m_numSpeciesTot; k++) {
4637 if (m_phaseID[k] == i) {
4638 m_tPhaseMoles_old_a += m_molNumSpecies_old[k];
4642 sum += m_tPhaseMoles_old_a;
4644 double denom = m_tPhaseMoles_old[i]+ m_tPhaseMoles_old_a + 1.0E-19;
4645 if (!vcs_doubleEqual(m_tPhaseMoles_old[i]/denom, m_tPhaseMoles_old_a/denom)) {
4646 plogf(
"check_tmoles: we have found a problem with phase %d: %20.15g, %20.15g\n",
4647 i, m_tPhaseMoles_old[i], m_tPhaseMoles_old_a);
4654 void VCS_SOLVE::vcs_updateVP(
const int vcsState)
4657 for (
size_t i = 0; i < m_numPhases; i++) {
4658 Vphase = m_VolPhaseList[i];
4670 plogf(
"vcs_updateVP ERROR: wrong stateCalc value: %d", vcsState);
4727 bool VCS_SOLVE::vcs_evaluate_speciesType()
4729 bool allMinorZeroedSpecies;
4731 m_numRxnMinorZeroed = 0;
4733 if (m_debug_print_lvl >= 2) {
4734 plogf(
" --- Species Status decision is reevaluated: All species are minor except for:\n");
4735 }
else if (m_debug_print_lvl >= 5) {
4736 plogf(
" --- Species Status decision is reevaluated");
4740 for (
size_t kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
4741 m_speciesStatus[kspec] = vcs_species_type(kspec);
4743 if (m_debug_print_lvl >= 5) {
4744 plogf(
" --- %-16s: ", m_speciesName[kspec].c_str());
4745 if (kspec < m_numComponents) {
4750 plogf(
" %10.3g ", m_molNumSpecies_old[kspec]);
4751 const char* sString = vcs_speciesType_string(m_speciesStatus[kspec], 100);
4752 plogf(
"%s\n", sString);
4754 }
else if (m_debug_print_lvl >= 2) {
4756 switch (m_speciesStatus[kspec]) {
4760 plogf(
" --- Major Species : %-s\n", m_speciesName[kspec].c_str());
4763 plogf(
" --- Purposely Zeroed-Phase Species (not in problem): %-s\n",
4764 m_speciesName[kspec].c_str());
4767 plogf(
" --- Zeroed-MS Phase Species: %-s\n", m_speciesName[kspec].c_str());
4770 plogf(
" --- Zeroed-SS Phase Species: %-s\n", m_speciesName[kspec].c_str());
4773 plogf(
" --- Deleted-Small Species : %-s\n", m_speciesName[kspec].c_str());
4776 plogf(
" --- Zeroed Species in an active MS phase (tmp): %-s\n",
4777 m_speciesName[kspec].c_str());
4780 plogf(
" --- Zeroed Species in an active MS phase (Stoich Constraint): %-s\n",
4781 m_speciesName[kspec].c_str());
4784 plogf(
" --- InterfaceVoltage Species: %-s\n", m_speciesName[kspec].c_str());
4787 plogf(
" --- Unknown type - ERROR %d\n", m_speciesStatus[kspec]);
4794 if (kspec >= m_numComponents) {
4796 ++m_numRxnMinorZeroed;
4801 if (m_debug_print_lvl >= 2) {
4807 allMinorZeroedSpecies = (m_numRxnMinorZeroed >= m_numRxnRdc);
4809 return allMinorZeroedSpecies;
4825 void VCS_SOLVE::vcs_switch2D(
double*
const*
const Jac,
4826 const size_t k1,
const size_t k2)
const
4832 for (i = 0; i < m_numSpeciesTot; i++) {
4833 std::swap(Jac[k1][i], Jac[k2][i]);
4835 for (i = 0; i < m_numSpeciesTot; i++) {
4836 std::swap(Jac[i][k1], Jac[i][k2]);
4841 static void print_space(
size_t num)
4844 for (j = 0; j < num; j++) {
4879 void VCS_SOLVE::vcs_deltag(
const int l,
const bool doDeleted,
4880 const int vcsState,
const bool alterZeroedPhases)
4887 size_t irxnl = m_numRxnRdc;
4889 irxnl = m_numRxnTot;
4894 double* molNumSpecies;
4895 double* actCoeffSpecies;
4912 if (m_debug_print_lvl >= 2) {
4913 plogf(
" --- Subroutine vcs_deltag called for ");
4915 plogf(
"major noncomponents\n");
4916 }
else if (l == 0) {
4917 plogf(
"all noncomponents\n");
4919 plogf(
"minor noncomponents\n");
4927 for (irxn = 0; irxn < m_numRxnRdc; ++irxn) {
4928 kspec = irxn + m_numComponents;
4931 deltaGRxn[irxn] = feSpecies[m_indexRxnToSpecies[irxn]];
4932 dtmp_ptr = m_stoichCoeffRxnMatrix[irxn];
4933 for (kspec = 0; kspec < m_numComponents; ++kspec) {
4934 deltaGRxn[irxn] += dtmp_ptr[kspec] * feSpecies[kspec];
4940 deltaGRxn[irxn] =
std::max(0.0, deltaGRxn[irxn]);
4944 }
else if (l == 0) {
4948 for (irxn = 0; irxn < irxnl; ++irxn) {
4950 deltaGRxn[irxn] = feSpecies[m_indexRxnToSpecies[irxn]];
4951 dtmp_ptr = m_stoichCoeffRxnMatrix[irxn];
4952 for (kspec = 0; kspec < m_numComponents; ++kspec) {
4953 deltaGRxn[irxn] += dtmp_ptr[kspec] * feSpecies[kspec];
4955 dtmp_ptr[kspec] < 0.0) {
4960 deltaGRxn[irxn] =
std::max(0.0, deltaGRxn[irxn]);
4967 for (irxn = 0; irxn < m_numRxnRdc; ++irxn) {
4968 kspec = irxn + m_numComponents;
4971 deltaGRxn[irxn] = feSpecies[m_indexRxnToSpecies[irxn]];
4972 dtmp_ptr = m_stoichCoeffRxnMatrix[irxn];
4973 for (kspec = 0; kspec < m_numComponents; ++kspec) {
4974 deltaGRxn[irxn] += dtmp_ptr[kspec] * feSpecies[kspec];
4976 dtmp_ptr[kspec] < 0.0) {
4981 deltaGRxn[irxn] =
std::max(0.0, deltaGRxn[irxn]);
5029 if (alterZeroedPhases &&
false) {
5030 for (iph = 0; iph < m_numPhases; iph++) {
5035 for (
size_t k = 0; k < Vphase->
nSpecies(); k++) {
5038 sum += molNumSpecies[kspec];
5051 for (
size_t k = 0; k < Vphase->
nSpecies(); k++) {
5054 if (kspec >= m_numComponents) {
5055 irxn = kspec - m_numComponents;
5056 if (deltaGRxn[irxn] > 50.0) {
5057 deltaGRxn[irxn] = 50.0;
5059 if (deltaGRxn[irxn] < -50.0) {
5060 deltaGRxn[irxn] = -50.0;
5062 poly += exp(-deltaGRxn[irxn])/actCoeffSpecies[kspec];
5070 for (
size_t k = 0; k < Vphase->
nSpecies(); k++) {
5072 if (kspec >= m_numComponents) {
5073 irxn = kspec - m_numComponents;
5074 deltaGRxn[irxn] = 1.0 - poly;
5084 for (irxn = 0; irxn < m_numRxnRdc; ++irxn) {
5090 void VCS_SOLVE::vcs_printDeltaG(
const int stateCalc)
5095 double* molNumSpecies =
VCS_DATA_PTR(m_molNumSpecies_old);
5096 const double* tPhMoles_ptr =
VCS_DATA_PTR(m_tPhaseMoles_old);
5097 const double* actCoeff_ptr =
VCS_DATA_PTR(m_actCoeffSpecies_old);
5106 bool zeroedPhase =
false;
5107 if (m_debug_print_lvl >= 2) {
5108 plogf(
" --- DELTA_G TABLE Components:");
5109 for (j = 0; j < m_numComponents; j++) {
5112 plogf(
"\n --- Components Moles:");
5113 for (j = 0; j < m_numComponents; j++) {
5114 plogf(
"%10.3g", m_molNumSpecies_old[j]);
5116 plogf(
"\n --- NonComponent| Moles | ");
5117 for (j = 0; j < m_numComponents; j++) {
5118 plogf(
"%-10.10s", m_speciesName[j].c_str());
5122 for (
size_t i = 0; i < m_numRxnTot; i++) {
5123 plogf(
" --- %3d ", m_indexRxnToSpecies[i]);
5124 plogf(
"%-10.10s", m_speciesName[m_indexRxnToSpecies[i]].c_str());
5125 plogf(
"|%10.3g|", m_molNumSpecies_old[m_indexRxnToSpecies[i]]);
5126 for (j = 0; j < m_numComponents; j++) {
5127 plogf(
" %6.2f", m_stoichCoeffRxnMatrix[i][j]);
5133 for (
int i=0; i<77; i++) {
5139 printf(
" --- DeltaG Table (J/kmol) Name PhID MoleNum MolFR "
5140 " ElectrChemStar ElectrChem DeltaGStar DeltaG(Pred) Stability\n");
5142 vcs_print_line(
"-", 132);
5144 for (
size_t kspec = 0; kspec < m_numSpeciesTot; kspec++) {
5146 size_t irxn = kspec - m_numComponents;
5148 double mfValue = 1.0;
5149 size_t iphase = m_phaseID[kspec];
5150 const vcs_VolPhase* Vphase = m_VolPhaseList[iphase];
5156 zeroedPhase =
false;
5158 if (tPhMoles_ptr[iphase] > 0.0) {
5162 mfValue = molNumSpecies[kspec] / tPhMoles_ptr[iphase];
5165 size_t klocal = m_speciesLocalPhaseIndex[kspec];
5166 mfValue = Vphase->moleFraction(klocal);
5169 printf(
" --- ** zp *** ");
5173 double feFull = feSpecies[kspec];
5176 feFull += log(actCoeff_ptr[kspec]) + log(mfValue);
5178 printf(
"%-24.24s", m_speciesName[kspec].c_str());
5180 printf(
" % -12.4e", molNumSpecies[kspec]);
5181 printf(
" % -12.4e", mfValue);
5182 printf(
" % -12.4e", feSpecies[kspec] * RT);
5183 printf(
" % -12.4e", feFull * RT);
5185 printf(
" % -12.4e", deltaGRxn[irxn] * RT);
5186 printf(
" % -12.4e", (deltaGRxn[irxn] + feFull - feSpecies[kspec]) * RT);
5188 if (deltaGRxn[irxn] < 0.0) {
5189 if (molNumSpecies[kspec] > 0.0) {
5194 }
else if (deltaGRxn[irxn] > 0.0) {
5195 if (molNumSpecies[kspec] > 0.0) {
5196 printf(
" shrinking");
5198 printf(
" unstable");
5201 printf(
" balanced");
5209 vcs_print_line(
"-", 132);
5228 void VCS_SOLVE::vcs_deltag_Phase(
const size_t iphase,
const bool doDeleted,
5229 const int stateCalc,
const bool alterZeroedPhases)
5232 size_t irxn, kspec, kcomp;
5237 double* actCoeffSpecies;
5249 plogf(
"vcs_deltag_Phase: we shouldn't be here\n");
5255 size_t irxnl = m_numRxnRdc;
5257 irxnl = m_numRxnTot;
5262 if (m_debug_print_lvl >= 2) {
5263 plogf(
" --- Subroutine vcs_deltag_Phase called for phase %d\n",
5274 if (iphase != m_phaseID[kspec]) {
5275 plogf(
"vcs_deltag_Phase index error\n");
5279 if (kspec >= m_numComponents) {
5280 irxn = kspec - m_numComponents;
5281 deltaGRxn[irxn] = feSpecies[kspec];
5282 dtmp_ptr = m_stoichCoeffRxnMatrix[irxn];
5283 for (kcomp = 0; kcomp < m_numComponents; ++kcomp) {
5284 deltaGRxn[irxn] += dtmp_ptr[kcomp] * feSpecies[kcomp];
5292 bool zeroedPhase =
true;
5294 for (irxn = 0; irxn < irxnl; ++irxn) {
5295 kspec = m_indexRxnToSpecies[irxn];
5297 iph = m_phaseID[kspec];
5298 if (iph == iphase) {
5299 if (m_molNumSpecies_old[kspec] > 0.0) {
5300 zeroedPhase =
false;
5302 deltaGRxn[irxn] = feSpecies[kspec];
5303 dtmp_ptr = m_stoichCoeffRxnMatrix[irxn];
5304 for (kcomp = 0; kcomp < m_numComponents; ++kcomp) {
5305 deltaGRxn[irxn] += dtmp_ptr[kcomp] * feSpecies[kcomp];
5351 if (alterZeroedPhases) {
5353 double phaseDG = 1.0;
5354 for (irxn = 0; irxn < irxnl; ++irxn) {
5355 kspec = m_indexRxnToSpecies[irxn];
5356 iph = m_phaseID[kspec];
5357 if (iph == iphase) {
5358 if (deltaGRxn[irxn] > 50.0) {
5359 deltaGRxn[irxn] = 50.0;
5361 if (deltaGRxn[irxn] < -50.0) {
5362 deltaGRxn[irxn] = -50.0;
5364 phaseDG -= exp(-deltaGRxn[irxn])/actCoeffSpecies[kspec];
5370 for (irxn = 0; irxn < irxnl; ++irxn) {
5371 kspec = m_indexRxnToSpecies[irxn];
5372 iph = m_phaseID[kspec];
5373 if (iph == iphase) {
5374 deltaGRxn[irxn] = 1.0 - phaseDG;
5397 void VCS_SOLVE::vcs_switch_pos(
const bool ifunc,
const size_t k1,
const size_t k2)
5399 size_t i1, i2, iph, kp1, kp2;
5405 if (k1 > (m_numSpeciesTot - 1) ||
5406 k2 > (m_numSpeciesTot - 1)) {
5407 plogf(
"vcs_switch_pos: ifunc = 0: inappropriate args: %d %d\n",
5414 pv1 = m_VolPhaseList[m_phaseID[k1]];
5415 pv2 = m_VolPhaseList[m_phaseID[k2]];
5417 kp1 = m_speciesLocalPhaseIndex[k1];
5418 kp2 = m_speciesLocalPhaseIndex[k2];
5421 plogf(
"Indexing error in program\n");
5425 plogf(
"Indexing error in program\n");
5433 std::swap(m_speciesName[k1], m_speciesName[k2]);
5434 std::swap(m_molNumSpecies_old[k1], m_molNumSpecies_old[k2]);
5435 std::swap(m_speciesUnknownType[k1], m_speciesUnknownType[k2]);
5436 std::swap(m_molNumSpecies_new[k1], m_molNumSpecies_new[k2]);
5437 std::swap(m_SSfeSpecies[k1], m_SSfeSpecies[k2]);
5438 std::swap(m_spSize[k1], m_spSize[k2]);
5439 std::swap(m_deltaMolNumSpecies[k1], m_deltaMolNumSpecies[k2]);
5440 std::swap(m_feSpecies_old[k1], m_feSpecies_old[k2]);
5441 std::swap(m_feSpecies_new[k1], m_feSpecies_new[k2]);
5442 std::swap(m_SSPhase[k1], m_SSPhase[k2]);
5443 std::swap(m_phaseID[k1], m_phaseID[k2]);
5444 std::swap(m_speciesMapIndex[k1], m_speciesMapIndex[k2]);
5445 std::swap(m_speciesLocalPhaseIndex[k1], m_speciesLocalPhaseIndex[k2]);
5446 std::swap(m_actConventionSpecies[k1], m_actConventionSpecies[k2]);
5447 std::swap(m_lnMnaughtSpecies[k1], m_lnMnaughtSpecies[k2]);
5448 std::swap(m_actCoeffSpecies_new[k1], m_actCoeffSpecies_new[k2]);
5449 std::swap(m_actCoeffSpecies_old[k1], m_actCoeffSpecies_old[k2]);
5450 std::swap(m_wtSpecies[k1], m_wtSpecies[k2]);
5451 std::swap(m_chargeSpecies[k1], m_chargeSpecies[k2]);
5452 std::swap(m_speciesThermoList[k1], m_speciesThermoList[k2]);
5453 std::swap(m_PMVolumeSpecies[k1], m_PMVolumeSpecies[k2]);
5455 for (
size_t j = 0; j < m_numElemConstraints; ++j) {
5456 std::swap(m_formulaMatrix[j][k1], m_formulaMatrix[j][k2]);
5458 if (m_useActCoeffJac) {
5459 vcs_switch2D(m_dLnActCoeffdMolNum.baseDataAddr(), k1, k2);
5461 std::swap(m_speciesStatus[k1], m_speciesStatus[k2]);
5471 i1 = k1 - m_numComponents;
5472 i2 = k2 - m_numComponents;
5474 if (i1 > (m_numRxnTot - 1) ||
5475 i2 > (m_numRxnTot - 1)) {
5476 plogf(
"switch_pos: ifunc = 1: inappropriate noncomp values: %d %d\n",
5480 for (
size_t j = 0; j < m_numComponents; ++j) {
5481 std::swap(m_stoichCoeffRxnMatrix[i1][j], m_stoichCoeffRxnMatrix[i2][j]);
5483 std::swap(m_scSize[i1], m_scSize[i2]);
5484 for (iph = 0; iph < m_numPhases; iph++) {
5485 std::swap(m_deltaMolNumPhase[i1][iph], m_deltaMolNumPhase[i2][iph]);
5486 std::swap(m_phaseParticipation[i1][iph],
5487 m_phaseParticipation[i2][iph]);
5489 std::swap(m_deltaGRxn_new[i1], m_deltaGRxn_new[i2]);
5490 std::swap(m_deltaGRxn_old[i1], m_deltaGRxn_old[i2]);
5491 std::swap(m_deltaGRxn_tmp[i1], m_deltaGRxn_tmp[i2]);
5527 double VCS_SOLVE::vcs_birthGuess(
const int kspec)
5529 size_t irxn = kspec - m_numComponents;
5538 if (m_molNumSpecies_old[kspec] != 0.0) {
5540 plogf(
"vcs_birthGuess:: we shouldn't be here\n");
5544 int ss = m_SSPhase[kspec];
5553 double dxm = vcs_minor_alt_calc(kspec, irxn, &soldel_ret, ANOTE);
5555 double dxm = vcs_minor_alt_calc(kspec, irxn, &soldel_ret);
5579 double* sc_irxn = m_stoichCoeffRxnMatrix[irxn];
5580 for (
size_t j = 0; j < m_numComponents; ++j) {
5583 if (m_molNumSpecies_old[j] > 0.0) {
5584 double tmp = sc_irxn[j] * dx;
5585 if (3.0*(-tmp) > m_molNumSpecies_old[j]) {
5586 dx =
std::min(dx, - 0.3333* m_molNumSpecies_old[j] / sc_irxn[j]);
5589 if (m_molNumSpecies_old[j] <= 0.0) {
5590 if (sc_irxn[j] < 0.0) {
5600 void VCS_SOLVE::vcs_setFlagsVolPhases(
const bool upToDate,
const int stateCalc)
5604 for (
size_t iph = 0; iph < m_numPhases; iph++) {
5605 Vphase = m_VolPhaseList[iph];
5609 for (
size_t iph = 0; iph < m_numPhases; iph++) {
5610 Vphase = m_VolPhaseList[iph];
5617 void VCS_SOLVE::vcs_setFlagsVolPhase(
const size_t iph,
const bool upToDate,
5618 const int stateCalc)
5620 vcs_VolPhase* Vphase = m_VolPhaseList[iph];
5624 Vphase->setMolesCurrent(stateCalc);
5637 void VCS_SOLVE::vcs_updateMolNumVolPhases(
const int stateCalc)
5640 for (
size_t iph = 0; iph < m_numPhases; iph++) {
5641 Vphase = m_VolPhaseList[iph];