Changeset 2028 for branches/newmole/source/newton_step.cpp
- Timestamp:
- 05/06/08 16:35:36 (8 months ago)
- Files:
-
- 1 modified
-
branches/newmole/source/newton_step.cpp (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/newmole/source/newton_step.cpp
r1851 r2028 11 11 #define ABSLIM 1e-12 12 12 #define ERRLIM 1e-12 13 #define SMALLABUND 1e-2414 13 # ifdef MAT 15 14 # undef MAT … … 21 20 enum {PRINTSOL = false}; 22 21 23 extern void funjac(valarray<double> &b2vec, double *ervals, double *amat, bool lgJac);24 25 22 /* mole_newton_step -- improve balance in chemical network along 26 23 * descent direction, step limited to ensure improvement */ 27 void newton_step(valarray<double> &b2vec, int *nBad, realnum *error, long n) 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)) 28 26 { 29 27 long int i, … … 31 29 iworst; 32 30 33 int printsol = PRINTSOL;34 35 31 double 36 32 etmp, 37 33 emax, 38 fracneg,39 34 fstep, 40 35 flast, … … 45 40 valarray<double> 46 41 amat(n*n), 47 b0vec(n),48 42 b1vec(n), 49 43 escale(n), … … 51 45 int32 merror; 52 46 53 DEBUG_ENTRY( " mole_newton_step()" );47 DEBUG_ENTRY( "newton_step()" ); 54 48 55 49 for( i=0; i < n; i++ ) 56 50 { 57 b 0vec[i] = b2vec[i];51 b2vec[i] = b0vec[i]; 58 52 } 59 53 … … 61 55 error1 = grad = 0.; 62 56 fstep = flast = 1.; 63 *nBad = 0;64 57 65 58 for (loop=0;loop<20;loop++) … … 67 60 flast = fstep; 68 61 69 funjac(b2vec,&ervals[0],&amat[0],loop==0);62 jacobn(b2vec,&ervals[0],&amat[0],loop==0); 70 63 71 64 if (loop == 0) … … 89 82 iworst = i; 90 83 } 91 92 if(printsol)93 fprintf(ioQQQ,"%15.7g\t",etmp);94 84 error0 += etmp; 95 85 } 96 97 // fprintf(stdout,"Loop %ld error %15.8g fstep %15.8g; worst species %s abund %g error %g\n",98 // loop,error0,fstep,groupspecies[iworst]->label,groupspecies[iworst]->den,,emax);99 86 100 87 if (loop == 0) … … 107 94 error1 = error0; 108 95 grad = -2*error1; 109 /* b1vec is now descent direction */110 111 //fprintf(ioQQQ,"Step factor %g bad species %s abund %g step %g\n",112 // fstep,iworst!=-1?groupspecies[iworst]->label:"None",113 // iworst!=-1?b0vec[iworst]:0.,114 // iworst!=-1?-b1vec[iworst]:0.);115 96 } else { 116 //fprintf(ioQQQ,"Step factor %15.8g bad species %d %s abund %15.8g step %15.8g\n",117 // fstep,iworst,iworst!=-1?groupspecies[iworst]->label:"None",118 // iworst!=-1?b0vec[iworst]:0.,119 // iworst!=-1?-b1vec[iworst]:0.);120 //fprintf(stdout,"Grads %15.8g %15.8g (%15.8g %15.8g)\n",grad,(error0-error1)/fstep,error0,error1);121 97 if (error0 < (1-2e-4*fstep)*error1 || error0 < 1e-20) 122 98 { … … 124 100 } 125 101 fstep = -0.5*grad*fstep*fstep/SDIV(error0-error1-grad*fstep); 126 //fprintf(stdout,"Errs1 loop %ld %g now %g fac %g was %g pred %g\n",loop,127 // error1,error0,fstep,flast,128 // error1+grad*fstep+(fstep*fstep/(flast*flast))*(error0-(error1+grad*flast)));129 102 if (fstep > 0.9*flast || fstep < 0.) 130 103 fstep = 0.5*flast; … … 133 106 } 134 107 135 //if (iworst != -1)136 // fprintf(stdout,"Loop %ld, worst error for %s: %g of %g, step %g of %g %g\n",137 // loop, groupspecies[iworst]->label,emax,error0,-b1vec[iworst]*fstep,b0vec[iworst],fstep);138 139 //fprintf(ioQQQ,"Loops %ld %g %g\n",loop,error0,(1-2e-4*fstep)*error1);140 141 108 /* 142 109 * b1vec[] is descent direction 143 * new densit yin b2vec[]110 * new densities in b2vec[] 144 111 */ 145 112 146 113 for( i=0; i < n; i++ ) 147 114 { 148 115 b2vec[i] = b0vec[i]-b1vec[i]*fstep; 149 116 } 150 151 /* check for negative populations */152 *nBad = 0;153 fracneg = 0.;154 iworst = -1;155 for( i=0; i < n; i++ )156 {157 if( b2vec[i] < 0. )158 {159 // fprintf(stdout,"Species %s negative at %g from %g\n",groupspecies[i]->label,b2vec[i],b0vec[i]);160 /* largest relative change in solution for neg soln */161 /* this can only occur for negative solutions since fracneg starts162 * as zero */163 if(-b2vec[i] > fracneg*SDIV(b0vec[i]) && b0vec[i] > SMALLABUND)164 {165 fracneg = -b2vec[i]/SDIV(b0vec[i]);166 iworst = i;167 }168 if (b0vec[i] > SMALLABUND || b2vec[i] < -SMALLABUND)169 ++*nBad;170 }171 }172 173 /* Expand species groups into mole.list[]->den -- either for174 * re-evaluation of network error, or for communication with175 * outside code */176 177 117 } 178 //fprintf(ioQQQ,"Final fstep %g\n",fstep);179 118 180 119 *error = (realnum) MIN2(error0,1e30);
