Changeset 2069 for branches/newmole
- Timestamp:
- 05/15/08 13:33:22 (8 months ago)
- Location:
- branches/newmole/source
- Files:
-
- 2 modified
-
mole_solve.cpp (modified) (9 diffs)
-
newton_step.cpp (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/newmole/source/mole_solve.cpp
r2068 r2069 49 49 #endif 50 50 51 static double molnum[LIMELM]; 52 51 53 double mole_solve() 52 54 { … … 114 116 fjac(mole.num_compacted*mole.num_compacted), 115 117 wa(lwa); 116 double tol = 1e- 7;118 double tol = 1e-9; 117 119 for ( long i=0; i < mole.num_compacted; i++ ) 118 120 { … … 121 123 hybrj1_(fcn, &mole.num_compacted, &newmols[0], &fvec[0], &fjac[0], 122 124 &mole.num_compacted, &tol, &info, &wa[0], &lwa); 123 fprintf(ioQQQ,"hybrj1_ returns info %d\n",info);125 //fprintf(ioQQQ,"hybrj1_ returns info %d\n",info); 124 126 #else 125 127 ; … … 325 327 } 326 328 if (*iflag == 1) 329 { 327 330 funjac(b2vec, fvec, &tmpvec[0], false); 331 } 328 332 else if (*iflag == 2) 329 funjac(b2vec, &tmpvec[0], fjac, true); 333 { 334 funjac(b2vec, &tmpvec[0], &fjac[0], true); 335 } 330 336 } 331 337 #endif … … 495 501 // constraints, now look for common species 496 502 valarray<int> iamax(mole.num_compacted), ncons(LIMELM); 503 double molnow[LIMELM]; 504 505 for ( nelem=ipHYDROGEN; nelem<LIMELM; nelem++) 506 { 507 molnow[nelem] = 0.; 508 } 509 for( i=0; i < mole.num_compacted; i++ ) 510 { 511 for ( nelem=ipHYDROGEN; nelem<LIMELM; nelem++) 512 { 513 molnow[nelem] += b2vec[i]*groupspecies[i]->nElem[nelem]; 514 } 515 } 497 516 iamax = 0; 498 517 for (nelem=ipHYDROGEN;nelem<LIMELM;nelem++) … … 500 519 if (element_list[nelem]->ipMl[0] != -1) 501 520 { 502 int imax = -1; 503 double amax = 0.; 504 for (i=0;i<mole.num_compacted;i++) 505 { 506 if (0 == iamax[i] && groupspecies[i]->nElem[nelem]*groupspecies[i]->den > amax) 521 if (0) 522 { 523 // determine common species 524 int imax = -1; 525 double amax = 0.; 526 for (i=0;i<mole.num_compacted;i++) 507 527 { 508 amax = groupspecies[i]->nElem[nelem]*groupspecies[i]->den; 509 imax = i; 528 if (0 == iamax[i] && groupspecies[i]->nElem[nelem]*groupspecies[i]->den > amax) 529 { 530 amax = groupspecies[i]->nElem[nelem]*groupspecies[i]->den; 531 imax = i; 532 } 510 533 } 511 } 512 iamax[imax] = 1; 513 ncons[nelem] = groupspecies[imax]->index; // was element_list[nelem]->ipMl[0]; 534 iamax[imax] = 1; 535 536 ncons[nelem] = groupspecies[imax]->index; 537 } 538 else 539 { 540 ncons[nelem] = element_list[nelem]->ipMl[0]; 541 } 514 542 } 515 543 } … … 519 547 if (element_list[nelem]->ipMl[0] != -1) 520 548 { 549 double scale=1.; 521 550 if (lgJac) 522 551 { 523 doublescale = -(ABSLIM+fabs(c[ncons[nelem]][ncons[nelem]]));552 //scale = -(ABSLIM+fabs(c[ncons[nelem]][ncons[nelem]])); 524 553 for(i=0;i<mole.num_compacted;i++) 525 554 { … … 527 556 } 528 557 } 529 530 b[ncons[nelem]] = 0.;558 b[ncons[nelem]] = (molnow[nelem]-molnum[nelem]); 559 //fprintf(ioQQQ,"Elem %ld err %g\n",nelem,b[ncons[nelem]]); 531 560 } 532 561 } … … 773 802 } 774 803 804 for ( nelem=ipHYDROGEN; nelem<LIMELM; nelem++) 805 { 806 molnum[nelem] = 0.; 807 } 775 808 for( i=0; i < mole.num_compacted; i++ ) 776 809 { 777 810 b0vec[i] = calcv[groupspecies[i]->index]; 811 for ( nelem=ipHYDROGEN; nelem<LIMELM; nelem++) 812 { 813 molnum[nelem] += b0vec[i]*groupspecies[i]->nElem[nelem]; 814 } 778 815 } 779 816 } -
branches/newmole/source/newton_step.cpp
r2055 r2069 8 8 #include "cddefines.h" 9 9 #include "thirdparty.h" 10 #include "mole.h" 11 #include "mole_priv.h" 10 12 11 13 #define ABSLIM 1e-12 … … 22 24 /* mole_newton_step -- improve balance in chemical network along 23 25 * descent direction, step limited to ensure improvement */ 24 void newton_step(const valarray<double> &b0vec, valarray<double> &b2vec, realnum *error, const long n, 25 void (*jacobn)(const valarray<double> &b2vec, double * const ervals, double * const amat, const bool lgJac)) 26 void newton_step(const valarray<double> &b0vec, valarray<double> &b2vec, 27 realnum *error, const long n, 28 void (*jacobn)(const valarray<double> &b2vec, 29 double * const ervals, double * const amat, 30 const bool lgJac)) 26 31 { 27 32 long int i, … … 55 60 error0 = error2 = grad = f1 = f2 = 0.; 56 61 57 for (loop=0;loop< 20;loop++)62 for (loop=0;loop<40;loop++) 58 63 { 59 64 jacobn(b2vec,&ervals[0],&amat[0],loop==0); … … 92 97 grad = -2*error0; 93 98 f1 = 1.0; 99 100 if (0) 101 { 102 for (i=0; i<LIMELM; ++i) 103 { 104 double felem = 0., fproj = 0.; 105 for (long j=0; j<n; ++j) 106 { 107 felem += b1vec[j]*groupspecies[j]->nElem[i]; 108 fproj += groupspecies[j]->nElem[i]; 109 } 110 //fprintf(ioQQQ,"Elem %ld cons %g\n",i,felem); 111 if (felem != 0.) 112 { 113 felem /= fproj; 114 for (long j=0; j<n; ++j) 115 { 116 b1vec[j] -= groupspecies[j]->nElem[i]*felem; 117 } 118 } 119 } 120 } 121 if (0) 122 { 123 double rmax = 0., rval; 124 int imax=0; 125 for( i=0; i < n; ++i ) 126 { 127 if (b0vec[i] != 0.) 128 rval = b1vec[i]/b0vec[i]; 129 else 130 rval = 0.; 131 if (fabs(rval) > rmax) 132 { 133 rmax = fabs(rval); 134 imax = i; 135 } 136 } 137 fprintf(ioQQQ,"Biggest is %s %g\n",groupspecies[imax]->label, 138 b1vec[imax]/b0vec[imax]); 139 /* if (rmax > 0.5) 140 f1 = 0.5/rmax; 141 if (f1 < 1e-3) 142 f1 = 1e-3; */ 143 } 94 144 } else { 145 //fprintf(ioQQQ,"Newt %ld stp %11.4g err %11.4g grd %11.4g fdg %11.4g e0 %11.4g de %11.4g\n", 146 // loop,f1,error1,grad,(error1-error0)/f1,error0,error1-error0); 95 147 if (error1 < (1-2e-4*f1)*error0 || error1 < 1e-20) 96 148 { … … 100 152 if (loop == 1) 101 153 { 102 f2 = 1.;103 f1 = -0.5* grad/SDIV(error1-error0-grad);154 f2 = f1; 155 f1 = -0.5*f1*grad/SDIV(error1-error0-f1*grad); 104 156 } 105 157 else … … 117 169 f1 = 0.5*f2; 118 170 else if (f1 < 0.1*f2) 119 f1 = 0.1*f2; 120 } 121 171 f1 = 0.1*f2; 172 } 173 122 174 /* 123 175 * b1vec[] is descent direction … … 129 181 b2vec[i] = b0vec[i]-b1vec[i]*f1; 130 182 } 183 } 184 if (0 && 40 == loop) 185 { 186 double rmax = 0., rval; 187 int imax=0; 188 for( i=0; i < n; ++i ) 189 { 190 if (b0vec[i] != 0.) 191 rval = b1vec[i]/b0vec[i]; 192 else 193 rval = 0.; 194 fprintf(ioQQQ,"%7s %11.4g %11.4g\n", 195 groupspecies[i]->label,rval,b0vec[i]); 196 if (fabs(rval) > rmax) 197 { 198 rmax = fabs(rval); 199 imax = i; 200 } 201 } 202 fprintf(ioQQQ,"Biggest is %s\n",groupspecies[imax]->label); 203 if (0) 204 { // Verify Jacobian 205 long j; 206 for( j=0; j < n; j++ ) 207 { 208 b1vec[j] = b0vec[j]; 209 } 210 jacobn(b1vec,&ervals[0],&amat[0],false); 211 for( i=0; i < n; i++ ) 212 { 213 for( j=0; j < n; j++ ) 214 { 215 b1vec[j] = b0vec[j]; 216 } 217 double db = 1e-3*fabs(b0vec[i])+1e-9; 218 b1vec[i] += db; 219 db = b1vec[i]-b0vec[i]; 220 jacobn(b1vec,&escale[0],&amat[0],false); 221 for( j=0; j < n; j++ ) 222 { 223 double e1 = MAT(amat,i,j); 224 double e2 = (escale[j]-ervals[j])/db; 225 if (fabs(e1-e2) > 0.01*fabs(e1+e2)) 226 fprintf(ioQQQ,"%7s %7s %11.4g %11.4g %11.4g\n", 227 groupspecies[i]->label,groupspecies[j]->label,e1,e2, 228 ervals[j]/db); 229 } 230 } 231 } 232 exit(-1); 131 233 } 132 234
