Changeset 2069 for branches/newmole

Show
Ignore:
Timestamp:
05/15/08 13:33:22 (8 months ago)
Author:
rjrw
Message:

More optional debugging code,

Ensure we're using the exact Jacobian for constraints,

Remove exit(-1) from default behaviour when > 40 loops (probably still
fails elsewhere, though).

Location:
branches/newmole/source
Files:
2 modified

Legend:

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

    r2068 r2069  
    4949#endif 
    5050 
     51static double molnum[LIMELM]; 
     52 
    5153double mole_solve() 
    5254{ 
     
    114116                                fjac(mole.num_compacted*mole.num_compacted), 
    115117                                wa(lwa); 
    116                         double tol = 1e-7; 
     118                        double tol = 1e-9; 
    117119                        for ( long i=0; i < mole.num_compacted; i++ ) 
    118120                        { 
     
    121123                        hybrj1_(fcn, &mole.num_compacted, &newmols[0], &fvec[0], &fjac[0], 
    122124                                                        &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); 
    124126#else 
    125127                        ; 
     
    325327        } 
    326328        if (*iflag == 1) 
     329        { 
    327330                funjac(b2vec, fvec, &tmpvec[0], false); 
     331        } 
    328332        else if (*iflag == 2) 
    329                 funjac(b2vec, &tmpvec[0], fjac, true); 
     333        { 
     334                funjac(b2vec, &tmpvec[0], &fjac[0], true); 
     335        } 
    330336} 
    331337#endif 
     
    495501                        // constraints, now look for common species 
    496502                        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                        } 
    497516                        iamax = 0; 
    498517                        for (nelem=ipHYDROGEN;nelem<LIMELM;nelem++) 
     
    500519                                if (element_list[nelem]->ipMl[0] != -1) 
    501520                                { 
    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++) 
    507527                                                { 
    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                                                        } 
    510533                                                } 
    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                                        } 
    514542                                } 
    515543                        } 
     
    519547                                if (element_list[nelem]->ipMl[0] != -1) 
    520548                                { 
     549                                        double scale=1.; 
    521550                                        if (lgJac) 
    522551                                        { 
    523                                                 double scale = -(ABSLIM+fabs(c[ncons[nelem]][ncons[nelem]])); 
     552                                                //scale = -(ABSLIM+fabs(c[ncons[nelem]][ncons[nelem]])); 
    524553                                                for(i=0;i<mole.num_compacted;i++) 
    525554                                                { 
     
    527556                                                } 
    528557                                        } 
    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]]); 
    531560                                } 
    532561                        } 
     
    773802        }        
    774803 
     804        for ( nelem=ipHYDROGEN; nelem<LIMELM; nelem++) 
     805        { 
     806                molnum[nelem] = 0.; 
     807        } 
    775808        for( i=0; i < mole.num_compacted; i++ ) 
    776809        { 
    777810                b0vec[i] = calcv[groupspecies[i]->index]; 
     811                for ( nelem=ipHYDROGEN; nelem<LIMELM; nelem++) 
     812                { 
     813                        molnum[nelem] += b0vec[i]*groupspecies[i]->nElem[nelem]; 
     814                } 
    778815        } 
    779816} 
  • branches/newmole/source/newton_step.cpp

    r2055 r2069  
    88#include "cddefines.h" 
    99#include "thirdparty.h" 
     10#include "mole.h" 
     11#include "mole_priv.h" 
    1012 
    1113#define ABSLIM  1e-12 
     
    2224/* mole_newton_step -- improve balance in chemical network along 
    2325 * 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)) 
     26void 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)) 
    2631{ 
    2732        long int i,  
     
    5560        error0 = error2 = grad = f1 = f2 = 0.; 
    5661 
    57         for (loop=0;loop<20;loop++)  
     62        for (loop=0;loop<40;loop++)  
    5863        { 
    5964                jacobn(b2vec,&ervals[0],&amat[0],loop==0); 
     
    9297                        grad = -2*error0; 
    9398                        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                        } 
    94144                } 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); 
    95147                        if (error1 < (1-2e-4*f1)*error0 || error1 < 1e-20) 
    96148                        { 
     
    100152                        if (loop == 1)  
    101153                        { 
    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); 
    104156                        } 
    105157                        else 
     
    117169                                f1 = 0.5*f2; 
    118170                        else if (f1 < 0.1*f2) 
    119                                 f1 = 0.1*f2; 
    120                 } 
    121  
     171                                f1 = 0.1*f2;                     
     172                } 
     173                 
    122174                /* 
    123175                 * b1vec[] is descent direction 
     
    129181                        b2vec[i] = b0vec[i]-b1vec[i]*f1;                                 
    130182                } 
     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); 
    131233        } 
    132234