Show
Ignore:
Timestamp:
05/06/08 16:35:36 (8 months ago)
Author:
rjrw
Message:

Further refactor newton_step to more purely numerical function.

Remove more dead or leftover code and data.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/newmole/source/newton_step.cpp

    r1851 r2028  
    1111#define ABSLIM  1e-12 
    1212#define ERRLIM  1e-12 
    13 #define SMALLABUND 1e-24 
    1413#       ifdef MAT 
    1514#               undef MAT 
     
    2120enum {PRINTSOL = false}; 
    2221 
    23 extern void funjac(valarray<double> &b2vec, double *ervals, double *amat, bool lgJac); 
    24  
    2522/* mole_newton_step -- improve balance in chemical network along 
    2623 * descent direction, step limited to ensure improvement */ 
    27 void newton_step(valarray<double> &b2vec, int *nBad, realnum *error, long n) 
     24void 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)) 
    2826{ 
    2927        long int i,  
     
    3129                iworst; 
    3230 
    33         int printsol = PRINTSOL; 
    34  
    3531        double  
    3632                etmp, 
    3733                emax, 
    38                 fracneg, 
    3934                fstep, 
    4035                flast, 
     
    4540        valarray<double>  
    4641                amat(n*n), 
    47                 b0vec(n), 
    4842                b1vec(n), 
    4943                escale(n), 
     
    5145        int32 merror; 
    5246 
    53         DEBUG_ENTRY( "mole_newton_step()" ); 
     47        DEBUG_ENTRY( "newton_step()" ); 
    5448 
    5549        for( i=0; i < n; i++ ) 
    5650        { 
    57                 b0vec[i] = b2vec[i]; 
     51                b2vec[i] = b0vec[i]; 
    5852        } 
    5953 
     
    6155        error1 = grad = 0.; 
    6256        fstep = flast = 1.;                      
    63         *nBad = 0;  
    6457 
    6558        for (loop=0;loop<20;loop++)  
     
    6760                flast = fstep; 
    6861                 
    69                 funjac(b2vec,&ervals[0],&amat[0],loop==0); 
     62                jacobn(b2vec,&ervals[0],&amat[0],loop==0); 
    7063 
    7164                if (loop == 0)  
     
    8982                                iworst = i; 
    9083                        } 
    91                          
    92                         if(printsol) 
    93                                 fprintf(ioQQQ,"%15.7g\t",etmp); 
    9484                        error0 += etmp; 
    9585                } 
    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); 
    9986 
    10087                if (loop == 0)  
     
    10794                        error1 = error0; 
    10895                        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.);  
    11596                } 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); 
    12197                        if (error0 < (1-2e-4*fstep)*error1 || error0 < 1e-20) 
    12298                        { 
     
    124100                        } 
    125101                        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))); 
    129102                        if (fstep > 0.9*flast || fstep < 0.) 
    130103                                fstep = 0.5*flast; 
     
    133106                } 
    134107 
    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  
    141108                /* 
    142109                 * b1vec[] is descent direction 
    143                  * new density in b2vec[] 
     110                 * new densities in b2vec[] 
    144111                 */ 
    145  
     112                 
    146113                for( i=0; i < n; i++ ) 
    147114                { 
    148115                        b2vec[i] = b0vec[i]-b1vec[i]*fstep;                              
    149116                } 
    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 starts 
    162                                  * 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 for 
    174                  * re-evaluation of network error, or for communication with 
    175                  * outside code */ 
    176                  
    177117        } 
    178         //fprintf(ioQQQ,"Final fstep %g\n",fstep); 
    179118 
    180119        *error = (realnum) MIN2(error0,1e30);