Changeset 2054 for branches/newmole

Show
Ignore:
Timestamp:
05/13/08 16:02:39 (8 months ago)
Author:
rjrw
Message:

Wrap comment in mole_solve

Cubic backtrack in newton_step

Location:
branches/newmole/source
Files:
2 modified

Legend:

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

    r2050 r2054  
    1919#include "newton_step.h" 
    2020#include "householder.h" 
    21 /* Nick Abel between July and October of 2003 assisted Dr. Ferland in improving the heavy element  
    22  * molecular network in Cloudy. Before this routine would predict negative abundances if  
    23  * the fraction of carbon in the form of molecules came close to 100%. A reorganizing of  
    24  * the reaction network detected several bugs.  Treatment of "coupled reactions", 
    25  * in which both densities in the reaction rate were being predicted by Cloudy, were also  
    26  * added.  Due to these improvements, Cloudy can now perform calculations 
    27  * where 100% of the carbon is in the form of CO without predicting negative abundances 
     21/* Nick Abel between July and October of 2003 assisted Dr. Ferland in 
     22 * improving the heavy element molecular network in Cloudy. Before 
     23 * this routine would predict negative abundances if the fraction of 
     24 * carbon in the form of molecules came close to 100%. A reorganizing 
     25 * of the reaction network detected several bugs.  Treatment of 
     26 * "coupled reactions", in which both densities in the reaction rate 
     27 * were being predicted by Cloudy, were also added.  Due to these 
     28 * improvements, Cloudy can now perform calculations where 100% of the 
     29 * carbon is in the form of CO without predicting negative abundances 
    2830 * 
    2931 * Additional changes were made in November of 2003 so that our reaction  
  • branches/newmole/source/newton_step.cpp

    r2031 r2054  
    3232                etmp, 
    3333                emax, 
    34                 fstep, 
    35                 flast, 
     34                f1, 
     35                f2, 
     36                error2, 
    3637                error1, 
    3738                error0, 
     
    5253        } 
    5354 
    54         error0 = 1; 
    55         error1 = grad = 0.; 
    56         fstep = 1.;                      
     55        error0 = error2 = grad = f1 = f2 = 0.; 
    5756 
    5857        for (loop=0;loop<20;loop++)  
    5958        { 
    60                 flast = fstep; 
    61                  
    6259                jacobn(b2vec,&ervals[0],&amat[0],loop==0); 
    6360 
     
    7067                } 
    7168 
    72                 error0 = emax = 0.; 
     69                error1 = emax = 0.; 
    7370                iworst = -1; 
    7471                for( i=0; i < n; i++ ) 
     
    8279                                iworst = i; 
    8380                        } 
    84                         error0 += etmp; 
     81                        error1 += etmp; 
    8582                } 
    8683 
     
    9289                        } 
    9390                        merror = solve_system(amat,b1vec,n); 
    94                         error1 = error0; 
    95                         grad = -2*error1; 
     91                        error0 = error1; 
     92                        grad = -2*error0; 
     93                        f1 = 1.0; 
    9694                } else { 
    97                         if (error0 < (1-2e-4*fstep)*error1 || error0 < 1e-20) 
     95                        if (error1 < (1-2e-4*f1)*error0 || error1 < 1e-20) 
    9896                        { 
    9997                                break; 
     98                        }  
     99                        if (loop == 1)  
     100                        { 
     101                                f2 = 1.; 
     102                                f1 = -0.5*grad/SDIV(error1-error0-grad); 
    100103                        } 
    101                         fstep = -0.5*grad*fstep*fstep/SDIV(error0-error1-grad*fstep); 
    102                         if (fstep > 0.9*flast || fstep < 0.) 
    103                                 fstep = 0.5*flast; 
    104                         if (fstep < 0.1*flast) 
    105                                 fstep = 0.1*flast; 
     104                        else 
     105                        { 
     106                                // Update using cubic fit, ref Dennis & Schnabel 1996 
     107                                double a1 = error1-error0-grad*f1; 
     108                                double b1 = error2-error0-grad*f2; 
     109                                double a = (a1/(f1*f1)-b1/(f2*f2)); 
     110                                double b = (-f2*a1/(f1*f1)+f1*b1/(f2*f2)); 
     111                                double ft = f1; 
     112                                f1 = (-b+sqrt(b*b-3*a*grad*(f1-f2)))/(3*a); 
     113                                f2 = ft; 
     114                        } 
     115                        error2 = error1; 
     116                        if (f1 > 0.5*f2 || f1 < 0.) 
     117                                f1 = 0.5*f2; 
     118                        else if (f1 < 0.1*f2) 
     119                                f1 = 0.1*f2; 
    106120                } 
    107121 
     
    113127                for( i=0; i < n; i++ ) 
    114128                { 
    115                         b2vec[i] = b0vec[i]-b1vec[i]*fstep;                              
     129                        b2vec[i] = b0vec[i]-b1vec[i]*f1;                                 
    116130                } 
    117131        } 
    118132 
    119         *error = (realnum) MIN2(error0,1e30); 
     133        *error = (realnum) MIN2(error1,1e30); 
    120134 
    121135        return;