13 #include "vcs_species_thermo.h"
22 #define dbocls_ dbocls
28 #define MAX(x,y) (( (x) > (y) ) ? (x) : (y))
32 extern "C" void dbocls_(
double* W,
int* MDW,
int* MCON,
int* MROWS,
34 double* BL,
double* BU,
int* IND,
int* IOPT,
35 double* X,
double* RNORMC,
double* RNORM,
36 int* MODE,
double* RW,
int* IW);
44 static void printProgress(
const vector<string> &spName,
45 const vector<double> &soln,
46 const vector<double> &ff)
48 int nsp = soln.size();
50 plogf(
" --- Summary of current progress:\n");
51 plogf(
" --- Name Moles - SSGibbs \n");
52 plogf(
" -------------------------------------------------------------------------------------\n");
53 for (
int k = 0; k < nsp; k++) {
54 plogf(
" --- %20s %12.4g - %12.4g\n", spName[k].c_str(), soln[k], ff[k]);
55 sum += soln[k] * ff[k];
57 plogf(
" --- Total sum to be minimized = %g\n", sum);
74 int VCS_SOLVE::vcs_setMolesLinProg()
77 double test = -1.0E-10;
80 std::string pprefix(
" --- seMolesLinProg ");
81 if (m_debug_print_lvl >= 2) {
82 plogf(
" --- call setInitialMoles\n");
95 double delta_xi, dxi_min = 1.0e10;
99 bool abundancesOK =
true;
100 bool usedZeroedSpecies;
102 std::vector<double> sm(m_numElemConstraints*m_numElemConstraints, 0.0);
103 std::vector<double> ss(m_numElemConstraints, 0.0);
104 std::vector<double> sa(m_numElemConstraints, 0.0);
105 std::vector<double> wx(m_numElemConstraints, 0.0);
106 std::vector<double> aw(m_numSpeciesTot, 0.0);
108 for (ik = 0; ik < m_numSpeciesTot; ik++) {
110 m_molNumSpecies_old[ik] = MAX(0.0, m_molNumSpecies_old[ik]);
115 if (m_debug_print_lvl >= 2) {
116 printProgress(m_speciesName, m_molNumSpecies_old, m_SSfeSpecies);
122 if (!vcs_elabcheck(0)) {
124 if (m_debug_print_lvl >= 2) {
125 plogf(
"%s Mole numbers failing element abundances\n", pprefix.c_str());
126 plogf(
"%sCall vcs_elcorr to attempt fix\n", pprefix.c_str());
131 abundancesOK =
false;
145 test, &usedZeroedSpecies);
151 if (m_debug_print_lvl >= 2) {
152 plogf(
"iteration %d\n", iter);
162 for (irxn = 0; irxn < m_numRxnTot; irxn++) {
165 ik = m_numComponents + irxn;
166 dg_rt = m_SSfeSpecies[ik];
168 const double* sc_irxn = m_stoichCoeffRxnMatrix[irxn];
169 for (
size_t jcomp = 0; jcomp < m_numElemConstraints; jcomp++) {
170 dg_rt += m_SSfeSpecies[jcomp] * sc_irxn[jcomp];
175 idir = (dg_rt < 0.0 ? 1 : -1);
177 dxi_min = m_molNumSpecies_old[ik];
180 for (
size_t jcomp = 0; jcomp < m_numComponents; jcomp++) {
186 delta_xi = fabs(m_molNumSpecies_old[jcomp]/nu);
190 if (delta_xi < 1.0e-10 && (m_molNumSpecies_old[ik] >= 1.0E-10)) {
192 if (m_debug_print_lvl >= 2) {
193 plogf(
" --- Component too small: %s\n", m_speciesName[jcomp].c_str());
199 if (delta_xi < dxi_min) {
208 double dsLocal = idir*dxi_min;
209 m_molNumSpecies_old[ik] += dsLocal;
210 m_molNumSpecies_old[ik] = MAX(0.0, m_molNumSpecies_old[ik]);
211 for (
size_t jcomp = 0; jcomp < m_numComponents; jcomp++) {
213 if (m_molNumSpecies_old[jcomp] > 1.0E-15) {
216 m_molNumSpecies_old[jcomp] += sc_irxn[jcomp] * dsLocal;
217 m_molNumSpecies_old[jcomp] = MAX(0.0, m_molNumSpecies_old[jcomp]);
219 if (m_molNumSpecies_old[jcomp] < 1.0E-60) {
231 if (m_debug_print_lvl >= 2) {
232 printProgress(m_speciesName, m_molNumSpecies_old, m_SSfeSpecies);
238 if (m_debug_print_lvl == 1) {
239 printProgress(m_speciesName, m_molNumSpecies_old, m_SSfeSpecies);
240 plogf(
" --- setInitialMoles end\n");
246 }
else if (iter > 15) {
254 int linprogmax(
double* XMOLES,
double* CC,
double* AX,
double* BB,
255 size_t NE,
size_t M,
size_t NE0)
275 int MROWS, MCON, NCOLS, NX, NI, MDW, i, j, MODE;
276 double sum, F[1], RNORMC, RNORM, *W, *BL, *BU, *RW, *X;
277 int* IND, *IW, *IOPT;
287 for (i = 0; i < NCOLS; i++) {
295 BL = (
double*) malloc(2*(NCOLS+MCON) *
sizeof(double));
296 BU = BL + (NCOLS+MCON);
297 IND = (
int*) malloc((NCOLS+MCON) *
sizeof(int));
298 RW = (
double*) malloc((6*NCOLS + 5*MCON) *
sizeof(double));
299 IW = (
int*) malloc((2*NCOLS + 2*MCON) *
sizeof(int));
300 IOPT = (
int*) malloc((17 + NI) *
sizeof(int));
301 X = (
double*) malloc((2*(NCOLS+MCON) + 2 + NX) *
sizeof(
double));
302 W = (
double*) malloc((MDW*(NCOLS+MCON+1)) *
sizeof(
double));
304 plogf(
"linproxmax ERROR: can not malloc memory of size %d bytes\n",
305 (
int)((MDW*(NCOLS+MCON+1)) *
sizeof(
double)));
326 for (j = 0; j < MCON; j++) {
327 for (i = 0; i < NCOLS; i++) {
328 W[j + i*MDW] = AX[j + i*NE0];
331 for (i = 0; i < NCOLS; i++) {
332 W[MCON + i*MDW] = CC[i];
334 W[MCON + (NCOLS)*MDW] = F[0];
337 for (j = 0; j < NCOLS; j++) {
342 for (j = 0; j < MCON; j++) {
344 BL[j + NCOLS] = BB[j];
345 BU[j + NCOLS] = BL[j + NCOLS];
349 dbocls_(W, &MDW, &MCON, &MROWS, &NCOLS, BL, BU, IND, IOPT,
350 X, &RNORMC, &RNORM, &MODE, RW, IW);
352 plogf(
"Return from DBOCLS was not normal, MODE = %d\n", MODE);
353 plogf(
" refer to subroutine DBOCLS for resolution\n");
354 plogf(
" RNORMC = %g\n", RNORMC);
357 for (j = 0; j < NCOLS; j++) {