Show
Ignore:
Timestamp:
12/15/07 08:24:13 (11 months ago)
Author:
rjrw
Message:

Back out changes to mole_newton_step from r1645:1652 which seemed to
cause more problems than they solved.

Some finesse of the handling of output buffering.

Location:
branches/newmole/source
Files:
5 modified

Legend:

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

    r1610 r1676  
    21022102                        fflush( ioQQQ ); 
    21032103                        /* stderr is always unbuffered */ 
    2104                         ioQQQ = stderr; 
     2104                        //ioQQQ = stderr; 
     2105                        setvbuf( ioQQQ, NULL, _IONBF, 0);  
    21052106                        /* will be used to generate comment at end */ 
    2106                         input.lgSetNoBuffering = false; 
     2107                        input.lgSetNoBuffering = true; 
    21072108                } 
    21082109        } 
  • branches/newmole/source/mole.h

    r1658 r1676  
    4242 
    4343/** Take one Newton step of the chemical network  
    44 \param *nFixup 
     44\param *nBad 
    4545\param *error 
    4646*/ 
    47 void mole_newton_step(int *nFixup, realnum *error, realnum numelem[]); 
     47void mole_newton_step(int *nBad, realnum *error, realnum numelem[]); 
    4848 
    4949/** \verbatim >>chng 03 feb 09, rm ipH3P_hev, since not used, and decrement NUM_HEAVY_MOLEC to 17  
  • branches/newmole/source/mole_co_solve.cpp

    r1658 r1676  
    3636static void mole_h_fixup(void); 
    3737/**hmole determine populations of hydrogen molecules */ 
    38 STATIC double hmole(bool *lgNegPop, bool *lgZerPop); 
     38STATIC double hmole( void ); 
    3939 
    4040 
     
    6262        ASSERT(lgElemsConserved()); 
    6363 
    64         error = hmole(lgNegPop, lgZerPop);               
     64        error = hmole();                 
    6565 
    6666        ASSERT(lgElemsConserved()); 
     
    196196 * For conditions of distribution and use see copyright notice in license.txt */ 
    197197/*hmole determine populations of hydrogen molecules */ 
    198 STATIC double hmole( bool *lgNegPop, bool *lgZerPop ) 
     198STATIC double hmole( void ) 
    199199{ 
    200         int nFixup, i, nelem; 
     200        int nBad, i, nelem; 
    201201        realnum error, 
    202202                numelem[LIMELM], 
     
    227227        /* loop until run to limit, or no fix ups needed and error is small */ 
    228228        /* will be used to keep track of neg solns */ 
    229         nFixup = 0; 
     229        nBad = 0; 
    230230        error = -1.; 
    231231        for(i=0; i < LIM_LOOP;i++)  
     
    247247                        } 
    248248                } 
    249                 /* nFixup is number of times negative abundances were fixed, should be zero  
    250                  * at return for valid soln */ 
    251                 nFixup = 0; 
    252  
    253                 mole_newton_step(&nFixup, &error, numelem); 
     249                /* nBad returns number of times negative abundances were fixed 
     250                 * for latest step, should be zero at return for valid soln */ 
     251 
     252                mole_newton_step(&nBad, &error, numelem); 
    254253                 
    255                 //fprintf(stdout,"Hmole zone %ld -- %7.2f loop %d error %15.8g fixup %d\n",nzone,fnzone,i,error,nFixup); 
     254                //fprintf(stdout,"Hmole zone %ld -- %7.2f loop %d error %15.8g fixup %d\n",nzone,fnzone,i,error,nBad); 
    256255 
    257256                { 
     
    284283                        } 
    285284                } 
    286                 if (error < BIGERROR && !nFixup) /* Stop early if good enough */ 
     285 
     286                if (error < BIGERROR && nBad == 0) /* Stop early if good enough */ 
    287287                        break; 
    288288        } 
    289289 
    290         if( (i == LIM_LOOP && error > BIGERROR)  || nFixup != 0 ) 
     290        if( (i == LIM_LOOP && error > BIGERROR)  || nBad != 0 ) 
    291291        { 
    292292                /* most of these failures occur just one time during search phase - 
     
    294294                conv.lgConvPops = false; 
    295295 
    296                 if( !conv.lgSearch && called.lgTalk ) 
     296                if( 1||!conv.lgSearch && called.lgTalk ) 
    297297                { 
    298298                        fprintf(ioQQQ," PROBLEM  hmole, zone %li: %d iters, %d bad; final error: %g lgSearch %i\n", 
    299299                        nzone, 
    300300                        i, 
    301                         nFixup, 
     301                        nBad, 
    302302                        error, 
    303303                        conv.lgSearch); 
  • branches/newmole/source/mole_newton_step.cpp

    r1658 r1676  
    4040/* mole_newton_step -- improve balance in chemical network along 
    4141 * descent direction, step limited to ensure improvement */ 
    42 void mole_newton_step(int *nFixup, realnum *error, realnum numelem[]) 
     42void mole_newton_step(int *nBad, realnum *error, realnum numelem[]) 
    4343{ 
    4444        enum {PRINTSOL = false}; 
     
    7676                *calcv=NULL, 
    7777                *b=NULL, 
    78                 **c=NULL, 
    79                 *scalev; 
     78                **c=NULL; 
    8079        int32 merror; 
    8180        double maxrate = 0., 
     
    9695                b1vec = ((double*)MALLOC( (size_t)(mole.num_compacted)*sizeof(double) )); 
    9796                b2vec = ((double*)MALLOC( (size_t)(mole.num_compacted)*sizeof(double) )); 
    98                 scalev = ((double*)MALLOC( (size_t)(mole.num_compacted)*sizeof(double) )); 
    9997                if (mole.num_calc != 0) 
    10098                { 
     
    180178        error1 = grad = 0.; 
    181179        fstep = flast = 1.;                      
    182         *nFixup = 0;  
     180        *nBad = 0;  
    183181 
    184182        lgNegPop = false; 
     
    196194                 
    197195                /* add positive ions and neutral atoms: ratios are set by ion_solver, 
    198                  * we determine abundance of the group as a whole here */ 
     196                 * we determine sum of two here */ 
    199197                 
    200198                if (loop == 0) { 
     
    237235                                } 
    238236                        } 
    239                         maxrate = 0.0; 
    240                         for(i=0;i<mole.num_compacted;i++) 
    241                         { 
    242                                 if (-c[groupspecies[i]->index][groupspecies[i]->index] > maxrate) 
    243                                         maxrate = -c[groupspecies[i]->index][groupspecies[i]->index]; 
    244                         } 
    245                         for(i=0;i<mole.num_compacted;i++)  
    246                         { 
    247                                 //ASSERT (c[COmole[i]->index][COmole[i]->index] < 0); 
    248                                 scalev[groupspecies[i]->index] = 1./(1e-6*maxrate-c[groupspecies[i]->index][groupspecies[i]->index]); 
    249                                 for (j=0;j<mole.num_compacted;j++)  
    250                                 { 
    251                                         c[groupspecies[j]->index][groupspecies[i]->index] *= scalev[groupspecies[i]->index]; 
    252                                 } 
    253                         } 
    254237                } 
    255238 
     
    271254                } 
    272255 
    273                 for (i=0;i<mole.num_compacted;++i)  
    274                 { 
    275                         b[groupspecies[i]->index] *= scalev[groupspecies[i]->index]; 
    276                 } 
    277  
    278256                /* For Newton-Raphson method, want the change in populations to be zero, 
    279257                 * so the conserved component must also be zero */ 
     
    308286                for( i=0; i < mole.num_compacted; i++ ) 
    309287                { 
    310                         etmp = (b[groupspecies[i]->index]-scalerate*(b2vec[i]-b0vec[i])); 
     288                        /* Smooth the error mode tailoff */ 
     289                        etmp = (b[groupspecies[i]->index]-scalerate*(b2vec[i]-b0vec[i]))/ 
     290                                ((ABSLIM+b0vec[i])*(-c[groupspecies[i]->index][groupspecies[i]->index])); 
    311291                        etmp *= etmp; 
    312292                        if (etmp > emax) 
     
    321301                } 
    322302 
    323                 //fprintf(stdout,"Loop %ld error %15.8g fstep %15.8g; worst species %s abund %g error %g\n", 
    324                 //                              loop,error0,fstep,groupspecies[iworst]->label,groupspecies[iworst]->hevmol,emax); 
     303                // fprintf(stdout,"Loop %ld error %15.8g fstep %15.8g; worst species %s abund %g error %g\n", 
     304                //                              loop,error0,fstep,groupspecies[iworst]->label,groupspecies[iworst]->hevmol,,emax); 
    325305 
    326306                /*------------------------------------------------------------------ */ 
     
    397377                                                 groupspecies[i]->hevmol, 
    398378                                                 -b1vec[i]); */ 
     379                                        if(b0vec[i] > 1e-12) 
     380                                        { 
     381                                                fixit(); /* Nadger low populations -- magic number */ 
    399382                                                 
    400                                         if (b0vec[i]-b1vec[i] < 0)  
    401                                                 fprintf(stdout,"Bad NR step for %s wants %g step %g orig %g\n",groupspecies[i]->label,b0vec[i]-b1vec[i],b1vec[i],b0vec[i]); 
    402                                         if (fstep*(1e-24+b1vec[i]) > 0.9*(1e-24+b0vec[i]))  
    403                                         { 
    404                                                 fstep = 0.9*(1e-24+b0vec[i])/(1e-24+b1vec[i]); 
    405                                                 iworst = i; 
    406                                                 fprintf(stdout,"Limiting %s %g %g %g\n",groupspecies[i]->label,b0vec[i],b1vec[i],fstep); 
    407                                         }                                
    408                                 } 
    409                                 // if (fstep < 0.1*flast) 
    410                                 //      fstep = 0.1*flast;                       
     383                                                if (b0vec[i]-b1vec[i] < 0)  
     384                                                        fprintf(stdout,"Bad NR step for %s %g %g\n",groupspecies[i]->label,b0vec[i],b1vec[i]); 
     385                                                if (fstep*b1vec[i] > 0.9*(ABSLIM+b0vec[i]))  
     386                                                { 
     387                                                        fstep = 0.9*(ABSLIM+b0vec[i])/(b1vec[i]); 
     388                                                        iworst = i; 
     389                                                } 
     390                                        } 
     391                                 
     392                                } 
     393                                if (fstep < 0.1*flast) 
     394                                        fstep = 0.1*flast;                       
    411395                        } 
    412396                        //fprintf(ioQQQ,"Step factor %g bad species %s abund %g step %g\n", 
     
    420404                        //                              iworst!=-1?-b1vec[iworst]:0.);  
    421405                        //fprintf(stdout,"Grads %g %g (%g %g)\n",grad,(error0-error1)/fstep,error0,error1); 
    422                         if ((error0 < (1-2e-4*fstep)*error1 || error0 < 1e-20)) 
     406                        if (!lgNegPop && (error0 < (1-2e-4*fstep)*error1 || error0 < 1e-20)) 
    423407                        { 
    424408                                // fprintf(stdout,"OK %g %g\n",error0,error1); 
    425409                                break; 
    426                         } 
    427                         else 
    428                         { 
    429                                 // fprintf(ioQQQ,"Continuing: tests %c %g %g\n",TorF(lgNegPop),error0,(1-2e-4*fstep)*error1-error0); 
    430410                        } 
    431411                        fstep = -0.5*grad*fstep*fstep/SDIV(error0-error1-grad*fstep); 
     
    518498                /* check for negative populations */ 
    519499                lgNegPop = false; 
    520                 *nFixup = 0;  
     500                *nBad = 0;  
    521501                fracneg = 0.; 
    522502                iworst = -1; 
     
    525505                        if( b2vec[i] < 0. )  
    526506                        { 
    527                                 //fprintf(stdout,"Species %s negative at %g from %g\n",groupspecies[i]->label,b2vec[i],b0vec[i]); 
     507                                // fprintf(stdout,"Species %s negative at %g from %g\n",groupspecies[i]->label,b2vec[i],b0vec[i]); 
    528508                                /* largest relative change in solution for neg soln */ 
    529509                                /* this can only occur for negative solutions since fracneg starts 
     
    534514                                        iworst = i; 
    535515                                } 
    536                                 if (b2vec[i] < -SMALLABUND) 
    537                                         lgNegPop = true; 
    538                                 b2vec[i] = 0.; 
    539                         } 
    540                 } 
     516                                //if (b2vec[i] < -SMALLABUND) 
     517                                //      lgNegPop = true; 
     518                                //b2vec[i] = 0.; 
     519                        } 
     520                } 
     521                 
    541522                 
    542523#       if 0 
     
    566547                        { 
    567548                                if (b0vec[i] > SMALLABUND) 
    568                                         ++*nFixup; 
     549                                        ++*nBad; 
    569550                        } 
    570551                } 
     
    578559                                { 
    579560                                        if (b0vec[i] > SMALLABUND) 
    580                                                 ++*nFixup; 
     561                                                ++*nBad; 
    581562                                        if (0) 
    582563                                                b2vec[i] = 0.; 
     
    586567                        /* very common to obtain negative solution on very first try -  
    587568                         * don't print in this case */ 
    588                         if(0 && *nFixup > 0) 
     569                        if(*nBad > 0) 
    589570                        { 
    590571                                fprintf( ioQQQ, " PROBLEM  hmole_step finds negative H molecule, in zone %.2f, loop %ld step %g.\n",fnzone,loop,fstep ); 
     
    882863        if ( merror != 0 ) 
    883864        { 
    884                 fprintf( ioQQQ, " CO_solve getrf_wrapper error %ld\n",merror ); 
     865                fprintf( ioQQQ, " CO_solve getrf_wrapper error %ld\n",(long int) merror ); 
    885866                puts( "[Stop in CO_solve]" ); 
    886867                cdEXIT(EXIT_FAILURE); 
  • branches/newmole/source/prt_final.cpp

    r1610 r1676  
    19401940        /* print input title for model */ 
    19411941        fprintf( ioQQQ, "%s\n\n", input.chTitle ); 
     1942        fflush(ioQQQ); 
    19421943        return; 
    19431944}