00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 #include <ctype.h>
00048 #include <stdarg.h>     
00049 #include "cddefines.h"
00050 #include "physconst.h"
00051 #include "cddrive.h"
00052 #include "called.h"
00053 #include "opacity.h"
00054 #include "rfield.h"
00055 #include "hextra.h"
00056 #include "hmi.h"
00057 #include "fudgec.h"
00058 #include "broke.h"
00059 #include "trace.h"
00060 #include "input.h"
00061 #include "elementnames.h"
00062 #include "punch.h"
00063 #include "version.h"
00064 #include "warnings.h"
00065 #include "conv.h"
00066 #include "thirdparty.h"
00067 
00068 
00069 
00070 char *read_whole_line( char *chLine , int nChar , FILE *ioIN )
00071 {
00072         char *chRet ,
00073                 *chEOL;
00074 
00075         DEBUG_ENTRY( "read_whole_line()" );
00076 
00077         if( (chRet = fgets( chLine , nChar, ioIN )) == NULL )
00078         {
00079                 return NULL;
00080         }
00081 
00082         
00083         
00084         chEOL = (char*)memchr( chLine , '\0' , nChar );
00085 
00086         
00087 
00088 
00089         if( (chEOL==NULL) || (chEOL - chLine)>=nChar-1 )
00090         {
00091                 if( called.lgTalk )
00092                         fprintf( ioQQQ, "DISASTER PROBLEM read_whole_line found input"
00093                         " with a line too long to be read.\n" );
00094 
00095                 lgAbort = true;
00096                 return NULL;
00097         }
00098         return chRet;
00099 }
00100 
00102 void Split(const string& str,   
00103            const string& sep,   
00104            vector<string>& lst, 
00105            split_mode mode)     
00106 {
00107         DEBUG_ENTRY( "Split()" );
00108 
00109         bool lgStrict = ( mode == SPM_STRICT );
00110         bool lgKeep = ( mode == SPM_KEEP_EMPTY );
00111         bool lgFail = false;
00112         string::size_type ptr1 = 0;
00113         string::size_type ptr2 = str.find( sep );
00114         string sstr = str.substr( ptr1, ptr2-ptr1 );
00115         if( sstr.length() > 0 )
00116                 lst.push_back( sstr );
00117         else {
00118                 if( lgStrict ) lgFail = true;
00119                 if( lgKeep ) lst.push_back( sstr );
00120         }
00121         while( ptr2 != string::npos ) {
00122                 
00123                 ptr1 = ptr2 + sep.length();
00124                 if( ptr1 < str.length() ) {
00125                         ptr2 = str.find( sep, ptr1 );
00126                         sstr = str.substr( ptr1, ptr2-ptr1 );
00127                         if( sstr.length() > 0 )
00128                                 lst.push_back( sstr );
00129                         else {
00130                                 if( lgStrict ) lgFail = true;
00131                                 if( lgKeep ) lst.push_back( sstr );
00132                         }
00133                 }
00134                 else {
00135                         ptr2 = string::npos;
00136                         if( lgStrict ) lgFail = true;
00137                         if( lgKeep ) lst.push_back( "" );
00138                 }
00139         }
00140         if( lgFail )
00141         {
00142                 fprintf( ioQQQ, " A syntax error occurred while splitting the string: \"%s\"\n", str.c_str() );
00143                 fprintf( ioQQQ, " The separator is \"%s\". Empty substrings are not allowed.\n", sep.c_str() );
00144                 cdEXIT(EXIT_FAILURE);
00145         }
00146 }
00147 
00148 
00149 void MyAssert(const char *file, int line)
00150 {
00151         DEBUG_ENTRY( "MyAssert()" );
00152 
00153         fprintf(ioQQQ," PROBLEM DISASTER An assert has been thrown, this is bad.\n");
00154         fprintf(ioQQQ," It happened in the file %s at line number %i\n", file, line );
00155         fprintf(ioQQQ," This is iteration %li, nzone %li, fzone %.2f, lgSearch=%c.\n", 
00156                 iteration , 
00157                 nzone ,
00158                 fnzone ,
00159                 TorF(conv.lgSearch) );
00160 
00161         ShowMe();
00162 #       if defined ASSERTDEBUG
00163         cdEXIT(EXIT_FAILURE);
00164 #       endif
00165 }
00166 
00167 
00168 double AnuUnit(realnum energy_ryd)
00169 
00170 {
00171         double AnuUnit_v;
00172 
00173         DEBUG_ENTRY( "AnuUnit()" );
00174 
00175         
00176         if( energy_ryd <=0. )
00177         {
00178                 
00179                 AnuUnit_v = 0.;
00180         }
00181         else if( strcmp(punch.chConPunEnr[punch.ipConPun],"ryd ") == 0 )
00182         {
00183                 
00184                 AnuUnit_v = energy_ryd;
00185         }
00186         else if( strcmp(punch.chConPunEnr[punch.ipConPun],"micr") == 0 )
00187         {
00188                 
00189                 AnuUnit_v = RYDLAM/energy_ryd*1e-4;
00190         }
00191         else if( strcmp(punch.chConPunEnr[punch.ipConPun]," kev") == 0 )
00192         {
00193                 
00194                 AnuUnit_v = energy_ryd*EVRYD*1.e-3;
00195         }
00196         else if( strcmp(punch.chConPunEnr[punch.ipConPun]," ev ") == 0 )
00197         {
00198                 
00199                 AnuUnit_v = energy_ryd*EVRYD;
00200         }
00201         else if( strcmp(punch.chConPunEnr[punch.ipConPun],"angs") == 0 )
00202         {
00203                 
00204                 AnuUnit_v = RYDLAM/energy_ryd;
00205         }
00206         else if( strcmp(punch.chConPunEnr[punch.ipConPun],"cent") == 0 )
00207         {
00208                 
00209                 AnuUnit_v = RYDLAM/energy_ryd*1e-8;
00210         }
00211         else if( strcmp(punch.chConPunEnr[punch.ipConPun],"wave") == 0 )
00212         {
00213                 
00214                 AnuUnit_v = RYD_INF*energy_ryd;
00215         }
00216         else if( strcmp(punch.chConPunEnr[punch.ipConPun]," mhz") == 0 )
00217         {
00218                 
00219                 AnuUnit_v = RYD_INF*energy_ryd*SPEEDLIGHT/1e6;
00220         }
00221         else
00222         {
00223                 fprintf( ioQQQ, " insane units in AnuUnit =%4.4s\n", 
00224                   punch.chConPunEnr[punch.ipConPun] );
00225                 cdEXIT(EXIT_FAILURE);
00226         }
00227 
00228         return AnuUnit_v;
00229 }
00230 
00231 
00232 void ShowMe(void)
00233 {
00234 
00235         DEBUG_ENTRY( "ShowMe()" );
00236 
00237         
00238         if( ioQQQ != NULL )
00239         {
00240                 
00241                 if( (hextra.cryden == 0.) && hmi.BiggestH2 > 0.1 )
00242                 {
00243                         fprintf( ioQQQ, " >>> \n >>> \n >>> Cosmic rays are not included and the gas is molecular.  "
00244                                 "THIS IS KNOWN TO BE UNSTABLE.  Add cosmic rays and try again.\n >>> \n >>>\n\n");
00245                 }
00246                 else
00247                 {
00248                         fprintf( ioQQQ, "\n\n\n" );
00249                         fprintf( ioQQQ, "           vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv \n" );
00250                         fprintf( ioQQQ, "          > PROBLEM DISASTER PROBLEM DISASTER.      <\n" );
00251                         fprintf( ioQQQ, "          > Sorry, something bad has happened.      <\n" );
00252                         fprintf( ioQQQ, "          > Please post this on the Cloudy web site <\n" );
00253                         fprintf( ioQQQ, "          > discussion board at www.nublado.org     <\n" );
00254                         fprintf( ioQQQ, "          > Please send all following information:  <\n" );
00255                         fprintf( ioQQQ, "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n" );
00256                         fprintf( ioQQQ, "\n\n" );
00257 
00258 
00259                         fprintf( ioQQQ, " Cloudy version number is %s\n", 
00260                                 t_version::Inst().chVersion );
00261                         fprintf( ioQQQ, " %s\n\n", t_version::Inst().chInfo );
00262 
00263                         fprintf( ioQQQ, "%5ld warnings,%3ld cautions,%3ld temperature failures.  Messages follow.\n", 
00264                           warnings.nwarn, warnings.ncaun, conv.nTeFail );
00265 
00266                         
00267                         cdWarnings(ioQQQ);
00268 
00269                         
00270                         cdCautions(ioQQQ);
00271 
00272                         
00273                         cdPrintCommands(ioQQQ);
00274 
00275                         
00276 
00277                         if( input.nSaveIni )
00278                         {
00279                                 fprintf(ioQQQ," This input stream included an init file.\n");
00280                                 fprintf(ioQQQ," If this init file is not part of the standard Cloudy distribution\n"); 
00281                                 fprintf(ioQQQ," then I will need a copy of it too.\n");
00282                         }
00283                 }
00284         }
00285         return;
00286 }
00287 
00288 
00289 void cap4(
00290                 char *chCAP ,   
00291                                                 
00292                 char *chLab)    
00293 {
00294         long int  
00295           i;
00296 
00297         DEBUG_ENTRY( "cap4()" );
00298 
00299         
00300         for( i=0; i < 4; i++ )
00301         {
00302                 
00303                 chCAP[i] = (char)toupper( chLab[i] );
00304         }
00305 
00306         
00307         chCAP[4] = '\0';
00308         return;
00309 }
00310 
00311 
00312 void uncaps(char *chCard )
00313 {
00314         long int i;
00315 
00316         DEBUG_ENTRY( "caps()" );
00317 
00318         
00319         i = 0;
00320         while( chCard[i]!= '\0' )
00321         {
00322                 chCard[i] = (char)tolower( chCard[i] );
00323                 ++i;
00324         }
00325         return;
00326 }
00327 
00328 
00329 void caps(char *chCard )
00330 {
00331         long int i;
00332 
00333         DEBUG_ENTRY( "caps()" );
00334 
00335         
00336         i = 0;
00337         while( chCard[i]!= '\0' )
00338         {
00339                 chCard[i] = (char)toupper( chCard[i] );
00340                 ++i;
00341         }
00342         return;
00343 }
00344 
00345 
00346 
00347 
00348 double e2(
00349         
00350         double t )
00351 {
00352         
00353         
00354         double hold = sexp(t) - t*ee1(t);
00355         DEBUG_ENTRY( "e2()" );
00356         
00357         return max( hold, 0. );
00358 }
00359 
00360 
00361 double ee1(double x)
00362 {
00363         double ans, 
00364           bot, 
00365           top;
00366         static double a[6]={-.57721566,.99999193,-.24991055,.05519968,-.00976004,
00367           .00107857};
00368         static double b[4]={8.5733287401,18.0590169730,8.6347608925,.2677737343};
00369         static double c[4]={9.5733223454,25.6329561486,21.0996530827,3.9584969228};
00370 
00371         DEBUG_ENTRY( "ee1()" );
00372 
00373         
00374 
00375 
00376 
00377 
00378 
00379         
00380         if( x <= 0 )
00381         {
00382                 fprintf( ioQQQ, " DISASTER negative argument in function ee1, x<0\n" );
00383                 cdEXIT(EXIT_FAILURE);
00384         }
00385 
00386         
00387         else if( x < 1. )
00388         {
00389                 
00390                 ans = ((((a[5]*x + a[4])*x + a[3])*x + a[2])*x + a[1])*x + a[0] - log(x);
00391         }
00392 
00393         
00394         else
00395         {
00396                 
00397                 top = (((x + b[0])*x + b[1])*x + b[2])*x + b[3];
00398                 bot = (((x + c[0])*x + c[1])*x + c[2])*x + c[3];
00399                 ans = top/bot/x*exp(-x);
00400         }
00401         return ans;
00402 }
00403 
00404 
00405 double ee1_safe(double x)
00406 {
00407         double ans, 
00408           bot, 
00409           top;
00410         
00411 
00412         static double b[4]={8.5733287401,18.0590169730,8.6347608925,.2677737343};
00413         static double c[4]={9.5733223454,25.6329561486,21.0996530827,3.9584969228};
00414 
00415         DEBUG_ENTRY( "ee1_safe()" );
00416 
00417         ASSERT( x > 1. );
00418 
00419         
00420         
00421         top = (((x + b[0])*x + b[1])*x + b[2])*x + b[3];
00422         
00423         bot = (((x + c[0])*x + c[1])*x + c[2])*x + c[3];
00424 
00425         ans = top/bot/x;
00426         return ans;
00427 }
00428 
00429 
00430 double FFmtRead(const char *chCard, 
00431                 long int *ipnt, 
00432                 
00433                 long int last, 
00434                 bool *lgEOL)
00435 {
00436         DEBUG_ENTRY( "FFmtRead()" );
00437 
00438         const int MAX_SIZE = 1001;
00439         char chr = '\0', chLocal[MAX_SIZE];
00440         const char *eol_ptr = &chCard[last]; 
00441         const char *ptr = min(&chCard[*ipnt-1],eol_ptr); 
00442 
00443         ASSERT( *ipnt > 0 && last - *ipnt + 2 <= MAX_SIZE );
00444 
00445         while( ptr < eol_ptr && ( chr = *ptr++ ) != '\0' )
00446         {
00447                 const char *lptr = ptr;
00448                 char lchr = chr;
00449                 if( lchr == '-' || lchr == '+' )
00450                         lchr = *lptr++;
00451                 if( lchr == '.' )
00452                         lchr = *lptr;
00453                 if( isdigit(lchr) )
00454                         break;
00455         }
00456 
00457         if( ptr == eol_ptr || chr == '\0' )
00458         {
00459                 *ipnt = last+1;
00460                 *lgEOL = true;
00461                 return 0.;
00462         }
00463 
00464         
00465         char* ptr2 = chLocal;
00466         do
00467         {
00468                 if( chr != ',' )
00469                         *ptr2++ = chr;
00470 #               if 0
00471                 else
00472                 {
00473                         if( ptr != eol_ptr )
00474                         {
00475                                 
00476 
00477                                 char tmpChar = *ptr;
00478                         }
00479                 }
00480 #               endif
00481                 if( ptr == eol_ptr )
00482                         break;
00483                 chr = *ptr++;
00484         }
00485         while( isdigit(chr) || chr == '.' || chr == '-' || chr == '+' || chr == ',' || chr == 'e' || chr == 'E' );
00486         *ptr2 = '\0';
00487 
00488         
00489 
00490 
00491         double value = atof( chLocal );
00492 
00493         *ipnt = (long)(ptr - chCard); 
00494         *lgEOL = false;
00495         return value;
00496 }
00497 
00498 
00499 
00500 long nMatch(const char *chKey, 
00501             const char *chCard)
00502 {
00503         const char *ptr;
00504         long Match_v;
00505 
00506         DEBUG_ENTRY( "nMatch()" );
00507 
00508         ASSERT( strlen(chKey) > 0 );
00509 
00510         if( ( ptr = strstr( chCard, chKey ) ) == NULL )
00511         {
00512                 
00513                 Match_v = 0L;
00514         }
00515         else
00516         {
00517                 
00518                 Match_v = (long)(ptr-chCard+1);
00519         }
00520         return Match_v;
00521 }
00522 
00523 
00524 
00525 
00526 
00527 
00528 
00529 
00530 double fudge(long int ipnt)
00531 {
00532         double fudge_v;
00533 
00534         DEBUG_ENTRY( "fudge()" );
00535 
00536         if( ipnt < 0 )
00537         {
00538                 
00539                 fudge_v = fudgec.nfudge;
00540                 fudgec.lgFudgeUsed = true;
00541         }
00542         else if( ipnt >= fudgec.nfudge )
00543         {
00544                 fprintf( ioQQQ, " FUDGE factor not entered for array number %3ld\n", 
00545                   ipnt );
00546                 cdEXIT(EXIT_FAILURE);
00547         }
00548         else
00549         {
00550                 fudge_v = fudgec.fudgea[ipnt];
00551                 fudgec.lgFudgeUsed = true;
00552         }
00553         return fudge_v;
00554 }
00555 
00556 
00557 
00558 long int GetElem(char *chCARD_CAPS )
00559 {
00560         int i;
00561 
00562         DEBUG_ENTRY( "GetElem()" );
00563 
00564         
00565 
00566         
00567 
00568         
00569         for( i=0; i<(int)LIMELM; ++i )
00570         {
00571                 if( nMatch( elementnames.chElementNameShort[i], chCARD_CAPS ) )
00572                 {
00573                         
00574                         return i;
00575                 }
00576         }
00577         
00578         return (-1 );
00579 }
00580 
00581 
00582 
00583 
00584 int GetQuote(
00585                 
00586                 char *chLabel,  
00587                 
00588                 char *chCard ,
00589                 
00590 
00591                 bool lgABORT )
00592 {
00593         char *i0,       
00594           *i1,          
00595           *iLoc;        
00596         size_t len;
00597 
00598         DEBUG_ENTRY( "GetQuote()" );
00599 
00600         
00601 
00602 
00603         
00604         
00605         i0 = strchr( input.chOrgCard,'\"' );
00606 
00607         if( i0 != NULL ) 
00608         {
00609                 
00610                 
00611                 i1 = strchr( i0+1 ,'\"' );
00612         }
00613         else 
00614         {
00615                 i1 = NULL;
00616         }
00617 
00618         
00619         
00620         if( i0 == NULL || i1 == NULL )
00621         {
00622                 if( lgABORT )
00623                 {
00624                         
00625                         fprintf( ioQQQ, 
00626                                 " A filename or label must be specified within double quotes, but no quotes were encountered on this command.\n" );
00627                         fprintf( ioQQQ, " Name must be surrounded by exactly two double quotes, like \"name.txt\". \n" );
00628                         fprintf( ioQQQ, " The line image follows:\n" );
00629                         fprintf( ioQQQ, " %s\n", input.chOrgCard);
00630                         fprintf( ioQQQ, " Sorry\n" );
00631                         cdEXIT(EXIT_FAILURE);
00632                 }
00633                 else
00634                 {
00635                         
00636                         chLabel[0] = '\0';
00637                         
00638                         return 1;
00639                 }
00640         }
00641 
00642         
00643         len = (size_t)(i1-i0-1);
00644         strncpy(chLabel,i0+1,len);
00645         
00646         chLabel[len] = '\0';
00647 
00648         
00649         iLoc = strchr( chCard ,'\"' );
00650         if( iLoc == NULL )
00651         {
00652                 fprintf( ioQQQ, " Insanity in GetQuote - line image follows:\n" );
00653                 fprintf( ioQQQ, " %s\n", input.chOrgCard);
00654                 cdEXIT(EXIT_FAILURE);
00655         }
00656 
00657         
00658         
00659         while( i0 <= i1 )
00660         {
00661                 *i0 = ' ';
00662                 *iLoc = ' ';
00663                 ++i0;
00664                 ++iLoc;
00665         }
00666         
00667         return 0;
00668 }
00669 
00670 
00671 
00672 
00673 
00674 
00675 
00676 
00677 
00678 
00679 
00680 
00681 #if !HAVE_POWI
00682 
00683 double powi( double x, long int n )     
00684 
00685 
00686 {
00687         double p;       
00688 
00689         DEBUG_ENTRY( "powi()" );
00690 
00691         if( x == 0 )
00692                 return 0.;
00693 
00694         
00695         if( n < 0 )
00696         {       
00697                 n = -n;
00698                 x = 1/x;
00699         }
00700 
00701         p = is_odd(n) ? x : 1;  
00702 
00703         
00704         
00705         while( n >>= 1 )
00706         {       
00707                 x *= x;                 
00708                 if( is_odd(n) ) 
00709                         p *= x;         
00710         }
00711         
00712         
00713         return p;
00714 }
00715 
00716 #endif 
00717 
00718 long ipow( long m, long n )     
00719 
00720 
00721 {
00722         long p; 
00723 
00724         DEBUG_ENTRY( "ipow()" );
00725 
00726         if( m == 0 || (n < 0 && m > 1) )
00727                 return 0L;
00728         
00729 
00730 
00731         if( n < 0 )
00732         {       
00733                 n = -n;
00734                 m = 1/m;
00735         }
00736 
00737         p = is_odd(n) ? m : 1;  
00738 
00739         
00740         
00741         while( n >>= 1 )
00742         {       
00743                 m *= m;                 
00744                 if( is_odd(n) ) 
00745                         p *= m;         
00746         }
00747         
00748         
00749         return p;
00750 }
00751 
00752 
00753 
00754 
00755 
00756 
00757 
00758 
00759 
00760 
00761 
00762 
00763 
00764 
00765 
00766 
00767 
00768 char *PrintEfmt(const char *fmt, double val ) 
00769 {
00770         static char buf[30]; 
00771 
00772         DEBUG_ENTRY( "PrintEfmt()" );
00773 
00774         
00775         sprintf(buf, fmt, val);
00776 
00777         
00778 #       ifdef _MSC_VER
00779         {
00780                 
00781 
00782 
00783                 char *ep , buf2[30];
00784 
00785                 
00786 
00787                 if( val >= 0.)
00788                 {
00789                         strcpy(buf2 , " " );
00790                         strcat(buf2 , buf);
00791                         strcpy( buf , buf2);
00792                 }
00793 
00794                 
00795                 if((ep = strchr(buf, 'e')) == NULL)
00796                 {
00797                         ep = strchr(buf, 'E');
00798                 }
00799 
00800                 
00801                 if(ep != NULL) 
00802                 {
00803                         
00804                         ep += 2;
00805 
00806                         
00807                         *ep = '\0';
00808 
00809                         
00810                         ++ep;
00811 
00812                         
00813                         strcat( buf, ep );
00814                 }
00815         }
00816 #       endif
00817         return buf;
00818 }
00819 
00820 
00821 void PrintE82( FILE* ioOUT, double value )
00822 {
00823         double frac , xlog , xfloor , tvalue;
00824         int iExp;
00825 
00826         DEBUG_ENTRY( "PrintE82()" );
00827 
00828         if( value < 0 )
00829         {
00830                 fprintf(ioOUT,"********");
00831         }
00832         else if( value == 0 )
00833         {
00834                 fprintf(ioOUT,"0.00E+00");
00835         }
00836         else
00837         {
00838                 
00839                 tvalue = value;
00840                 xlog = log10( tvalue );
00841                 xfloor = floor(xlog);
00842                 
00843                 frac = tvalue*pow(10.,-xfloor);
00844                 
00845                 iExp = (int)xfloor;
00846                 if( frac>9.9945 )
00847                 {
00848                         frac /= 10.;
00849                         iExp += 1;
00850                 }
00851                 
00852                 fprintf(ioOUT,"%.2f",frac);
00853                 
00854                 fprintf(ioOUT,"E");
00855                 
00856                 if(iExp>=0 )
00857                 {
00858                         fprintf(ioOUT,"+");
00859                 }
00860                 fprintf(ioOUT,"%.2d",iExp);
00861         }
00862         return;
00863 }
00864 
00865 
00866 
00867 void PrintE71( FILE* ioOUT, double value )
00868 {
00869         double frac , xlog , xfloor , tvalue;
00870         int iExp;
00871 
00872         DEBUG_ENTRY( "PrintE71()" );
00873 
00874         if( value < 0 )
00875         {
00876                 fprintf(ioOUT,"*******");
00877         }
00878         else if( value == 0 )
00879         {
00880                 fprintf(ioOUT,"0.0E+00");
00881         }
00882         else
00883         {
00884                 
00885                 tvalue = value;
00886                 xlog = log10( tvalue );
00887                 xfloor = floor(xlog);
00888                 
00889                 frac = tvalue*pow(10.,-xfloor);
00890                 
00891                 iExp = (int)xfloor;
00892                 if( frac>9.9945 )
00893                 {
00894                         frac /= 10.;
00895                         iExp += 1;
00896                 }
00897                 
00898                 fprintf(ioOUT,"%.1f",frac);
00899                 
00900                 fprintf(ioOUT,"E");
00901                 
00902                 if(iExp>=0 )
00903                 {
00904                         fprintf(ioOUT,"+");
00905                 }
00906                 fprintf(ioOUT,"%.2d",iExp);
00907         }
00908         return;
00909 }
00910 
00911 
00912 
00913 
00914 void PrintE93( FILE* ioOUT, double value )
00915 {
00916         double frac , xlog , xfloor, tvalue;
00917         int iExp;
00918 
00919         DEBUG_ENTRY( "PrintE93()" );
00920 
00921         if( value < 0 )
00922         {
00923                 fprintf(ioOUT,"*********");
00924         }
00925         else if( value == 0 )
00926         {
00927                 fprintf(ioOUT,"0.000E+00");
00928         }
00929         else
00930         {
00931                 
00932                 tvalue = value;
00933                 xlog = log10( tvalue );
00934                 xfloor = floor(xlog);
00935                 
00936                 frac = tvalue*pow(10.,-xfloor);
00937                 
00938                 iExp = (int)xfloor;
00939                 if( frac>9.99949 )
00940                 {
00941                         frac /= 10.;
00942                         iExp += 1;
00943                 }
00944                 
00945                 fprintf(ioOUT,"%5.3f",frac);
00946                 
00947                 fprintf(ioOUT,"E");
00948                 
00949                 if(iExp>=0 )
00950                 {
00951                         fprintf(ioOUT,"+");
00952                 }
00953                 fprintf(ioOUT,"%.2d",iExp);
00954         }
00955         return;
00956 }
00957 
00958 
00959 NORETURN void TotalInsanity(void)
00960 {
00961 
00962         DEBUG_ENTRY( "TotalInsanity()" );
00963 
00964         
00965 
00966 
00967         fprintf( ioQQQ, " Something that cannot happen, has happened.\n" );
00968         fprintf( ioQQQ, " This is TotalInsanity, I live in service.cpp.\n" );
00969         ShowMe();
00970 
00971         cdEXIT(EXIT_FAILURE);
00972 }
00973 
00974 
00975 
00976 NORETURN void BadRead(void)
00977 {
00978 
00979         DEBUG_ENTRY( "BadRead()" );
00980 
00981         
00982         fprintf( ioQQQ, " A read of internal input data has failed.\n" );
00983         fprintf( ioQQQ, " This is BadRead, I live in service.c.\n" );
00984         ShowMe();
00985 
00986         cdEXIT(EXIT_FAILURE);
00987 }
00988 
00989 
00990 NORETURN void BadOpen(void)
00991 {
00992 
00993         DEBUG_ENTRY( "BadOpen()" );
00994 
00995         
00996         fprintf( ioQQQ, " An attempt at opening a files has failed.\n" );
00997         fprintf( ioQQQ, " This is BadOpen, I live in service.c.\n" );
00998         ShowMe();
00999 
01000         cdEXIT(EXIT_FAILURE);
01001 }
01002 
01003 
01004 NORETURN void NoNumb(char *chCard)
01005 {
01006 
01007         DEBUG_ENTRY( "NoNumb()" );
01008 
01009         
01010         fprintf( ioQQQ, " There is a problem on the following command line:\n" );
01011         fprintf( ioQQQ, " %s\n", chCard );
01012         fprintf( ioQQQ, " There should have been a number on this line.   Sorry.\n" );
01013         cdEXIT(EXIT_FAILURE);
01014  }
01015 
01016 
01017 sys_float sexp(sys_float x)
01018 {
01019         sys_float sexp_v;
01020 
01021         DEBUG_ENTRY( "sexp()" );
01022 
01023         
01024         if( x < SEXP_LIMIT )
01025         {
01026                 sexp_v = exp(-x);
01027         }
01028         else
01029         {
01030                 sexp_v = 0.f;
01031         }
01032         return sexp_v;
01033 }
01034 
01035 
01036 double sexp(double x)
01037 {
01038         double sexp_v;
01039 
01040         DEBUG_ENTRY( "sexp()" );
01041 
01042         
01043         if( x < SEXP_LIMIT )
01044         {
01045                 sexp_v = exp(-x);
01046         }
01047         else
01048         {
01049                 sexp_v = 0.;
01050         }
01051         return sexp_v;
01052 }
01053 
01054 
01055 
01056 double dsexp(double x)
01057 {
01058         double dsexp_v;
01059 
01060         DEBUG_ENTRY( "dsexp()" );
01061 
01062         if( x < 680. )
01063         {
01064                 dsexp_v = exp(-x);
01065         }
01066         else
01067         {
01068                 dsexp_v = 0.;
01069         }
01070         return dsexp_v;
01071 }
01072 
01073 
01074 
01075 void TestCode(void)
01076 {
01077 
01078         DEBUG_ENTRY( "TestCode( )" );
01079 
01080         
01081         lgTestCodeCalled = true;
01082         return;
01083 }
01084 
01085 
01086 void broken(void)
01087 {
01088 
01089         DEBUG_ENTRY( "broken( )" );
01090 
01091         broke.lgBroke = true;
01092         return;
01093 }
01094 
01095 
01096 void fixit(void)
01097 {
01098         DEBUG_ENTRY( "fixit( )" );
01099 
01100         broke.lgFixit = true;
01101         return;
01102 }
01103 
01104 
01105 void CodeReview(void)
01106 {
01107         DEBUG_ENTRY( "CodeReview( )" );
01108 
01109         broke.lgCheckit = true;
01110         return;
01111 }
01112 
01114 int dprintf(FILE *fp, const char *format, ...)
01115 {
01116         va_list ap;
01117         int i1, i2;
01118 
01119         DEBUG_ENTRY( "dprintf()" );
01120         va_start(ap,format);
01121         i1 = fprintf(fp,"DEBUG ");
01122         if (i1 >= 0)
01123                 i2 = vfprintf(fp,format,ap);
01124         else
01125                 i2 = 0;
01126         if (i2 < 0)
01127                 i1 = 0;
01128         va_end(ap);
01129 
01130         return i1+i2;
01131 }
01132 
01133 
01134 
01135 
01136 int dbg_printf(int debug, const char *fmt, ...)
01137 {
01138         va_list ap;
01139         int i=0;
01140 
01141         DEBUG_ENTRY( "dbg_printf()" );
01142 
01143         
01144         if(debug <= trace.debug_level) 
01145         {               
01146                 va_start(ap, fmt);      
01147 
01148                 i = vfprintf(ioQQQ, fmt, ap);
01149                 
01150                 fflush(ioQQQ);
01151                 va_end(ap);
01152         }
01153         return i;
01154 }
01155 
01156 
01157 
01158 double qg32(
01159         double xl, 
01160         double xu, 
01161         
01162         double (*fct)(double) )
01163 {
01164         double a, 
01165           b, 
01166           c, 
01167           y;
01168 
01169         DEBUG_ENTRY( "qg32()" );
01170 
01171         
01172 
01173 
01174 
01175 
01176 
01177 
01178 
01179 
01180 
01181 
01182 
01183 
01184         a = .5*(xu + xl);
01185         b = xu - xl;
01186         c = .498631930924740780*b;
01187         y = .35093050047350483e-2*((*fct)(a+c) + (*fct)(a-c));
01188         c = b*.49280575577263417;
01189         y += .8137197365452835e-2*((*fct)(a+c) + (*fct)(a-c));
01190         c = b*.48238112779375322;
01191         y += .1269603265463103e-1*((*fct)(a+c) + (*fct)(a-c));
01192         c = b*.46745303796886984;
01193         y += .17136931456510717e-1*((*fct)(a+c) + (*fct)(a-c));
01194         c = b*.44816057788302606;
01195         y += .21417949011113340e-1*((*fct)(a+c) + (*fct)(a-c));
01196         c = b*.42468380686628499;
01197         y += .25499029631188088e-1*((*fct)(a+c) + (*fct)(a-c));
01198         c = b*.3972418979839712;
01199         y += .29342046739267774e-1*((*fct)(a+c) + (*fct)(a-c));
01200         c = b*.36609105937014484;
01201         y += .32911111388180923e-1*((*fct)(a+c) + (*fct)(a-c));
01202         c = b*.3315221334651076;
01203         y += .36172897054424253e-1*((*fct)(a+c) + (*fct)(a-c));
01204         c = b*.29385787862038116;
01205         y += .39096947893535153e-1*((*fct)(a+c) + (*fct)(a-c));
01206         c = b*.2534499544661147;
01207         y += .41655962113473378e-1*((*fct)(a+c) + (*fct)(a-c));
01208         c = b*.21067563806531767;
01209         y += .43826046502201906e-1*((*fct)(a+c) + (*fct)(a-c));
01210         c = b*.16593430114106382;
01211         y += .45586939347881942e-1*((*fct)(a+c) + (*fct)(a-c));
01212         c = b*.11964368112606854;
01213         y += .46922199540402283e-1*((*fct)(a+c) + (*fct)(a-c));
01214         c = b*.7223598079139825e-1;
01215         y += .47819360039637430e-1*((*fct)(a+c) + (*fct)(a-c));
01216         c = b*.24153832843869158e-1;
01217         y = b*(y + .482700442573639e-1*((*fct)(a+c) + (*fct)(a-c)));
01218         
01219         return y;
01220 }
01221 
01222 
01223 void spsort(
01224           
01225           realnum x[], 
01226           
01227           long int n, 
01228           
01229           long int iperm[], 
01230           
01231 
01232           int kflag, 
01233           
01234           int *ier)
01235 {
01236         
01237 
01238 
01239 
01240 
01241 
01242 
01243 
01244 
01245 
01246 
01247 
01248 
01249 
01250 
01251 
01252 
01253 
01254 
01255 
01256 
01257 
01258 
01259 
01260 
01261 
01262 
01263 
01264 
01265 
01266 
01267 
01268 
01269 
01270 
01271 
01272 
01273 
01274 
01275 
01276 
01277 
01278 
01279 
01280 
01281 
01282 
01283 
01284 
01285 
01286 
01287 
01288 
01289 
01290 
01291 
01292 
01293 
01294 
01295 
01296 
01297 
01298 
01299 
01300 
01301 
01302 
01303 
01304 
01305 
01306 
01307         long int i, 
01308           ij, 
01309           il[21], 
01310           indx, 
01311           indx0, 
01312           istrt, 
01313           istrt_, 
01314           iu[21], 
01315           j, 
01316           k, 
01317           kk, 
01318           l, 
01319           lm, 
01320           lmt, 
01321           m, 
01322           nn;
01323         realnum r, 
01324           ttemp;
01325 
01326         DEBUG_ENTRY( "spsort()" );
01327 
01328         
01329         
01330         
01331         
01332         
01333         
01334         *ier = 0;
01335         nn = n;
01336         if( nn < 1 )
01337         {
01338                 *ier = 1;
01339                 return;
01340         }
01341         else
01342         {
01343                 kk = labs(kflag);
01344                 if( kk != 1 && kk != 2 )
01345                 {
01346                         *ier = 2;
01347                         return;
01348                 }
01349                 else
01350                 {
01351 
01352                         
01353 
01354                         for( i=0; i < nn; i++ )
01355                         {
01356                                 iperm[i] = i+1;
01357                         }
01358 
01359                         
01360                         if( nn == 1 )
01361                         { 
01362                                 --iperm[0];
01363                                 return;
01364                         }
01365 
01366                         
01367                         if( kflag <= -1 )
01368                         {
01369                                 for( i=0; i < nn; i++ )
01370                                 {
01371                                         x[i] = -x[i];
01372                                 }
01373                         }
01374 
01375                         
01376                         m = 1;
01377                         i = 1;
01378                         j = nn;
01379                         r = .375e0;
01380                 }
01381         }
01382 
01383         while( true )
01384         {
01385                 if( i == j )
01386                         goto L_80;
01387                 if( r <= 0.5898437e0 )
01388                 {
01389                         r += 3.90625e-2;
01390                 }
01391                 else
01392                 {
01393                         r -= 0.21875e0;
01394                 }
01395 
01396 L_40:
01397                 k = i;
01398 
01399                 
01400 
01401                 ij = i + (long)((j-i)*r);
01402                 lm = iperm[ij-1];
01403 
01404                 
01405 
01406                 if( x[iperm[i-1]-1] > x[lm-1] )
01407                 {
01408                         iperm[ij-1] = iperm[i-1];
01409                         iperm[i-1] = lm;
01410                         lm = iperm[ij-1];
01411                 }
01412                 l = j;
01413 
01414                 
01415 
01416                 if( x[iperm[j-1]-1] < x[lm-1] )
01417                 {
01418                         iperm[ij-1] = iperm[j-1];
01419                         iperm[j-1] = lm;
01420                         lm = iperm[ij-1];
01421 
01422                         
01423 
01424 
01425                         if( x[iperm[i-1]-1] > x[lm-1] )
01426                         {
01427                                 iperm[ij-1] = iperm[i-1];
01428                                 iperm[i-1] = lm;
01429                                 lm = iperm[ij-1];
01430                         }
01431                 }
01432 
01433                 
01434 
01435                 while( true )
01436                 {
01437                         l -= 1;
01438                         if( x[iperm[l-1]-1] <= x[lm-1] )
01439                         {
01440 
01441                                 
01442 
01443                                 while( true )
01444                                 {
01445                                         k += 1;
01446                                         if( x[iperm[k-1]-1] >= x[lm-1] )
01447                                                 break;
01448                                 }
01449 
01450                                 
01451                                 if( k > l )
01452                                         break;
01453                                 lmt = iperm[l-1];
01454                                 iperm[l-1] = iperm[k-1];
01455                                 iperm[k-1] = lmt;
01456                         }
01457                 }
01458 
01459                 
01460                 if( l - i > j - k )
01461                 {
01462                         il[m-1] = i;
01463                         iu[m-1] = l;
01464                         i = k;
01465                         m += 1;
01466                 }
01467                 else
01468                 {
01469                         il[m-1] = k;
01470                         iu[m-1] = j;
01471                         j = l;
01472                         m += 1;
01473                 }
01474 
01475 L_90:
01476                 if( j - i >= 1 )
01477                         goto L_40;
01478                 if( i == 1 )
01479                         continue;
01480                 i -= 1;
01481 
01482                 while( true )
01483                 {
01484                         i += 1;
01485                         if( i == j )
01486                                 break;
01487                         lm = iperm[i];
01488                         if( x[iperm[i-1]-1] > x[lm-1] )
01489                         {
01490                                 k = i;
01491 
01492                                 while( true )
01493                                 {
01494                                         iperm[k] = iperm[k-1];
01495                                         k -= 1;
01496 
01497                                         if( x[lm-1] >= x[iperm[k-1]-1] )
01498                                                 break;
01499                                 }
01500                                 iperm[k] = lm;
01501                         }
01502                 }
01503 
01504                 
01505 L_80:
01506                 m -= 1;
01507                 if( m == 0 )
01508                         break;
01509                 
01510                 i = il[m-1];
01511                 j = iu[m-1];
01512                 
01513                 goto L_90;
01514         }
01515 
01516         
01517         if( kflag <= -1 )
01518         {
01519                 for( i=0; i < nn; i++ )
01520                 {
01521                         x[i] = -x[i];
01522                 }
01523         }
01524 
01525         
01526         if( kk == 2 )
01527         {
01528 
01529                 
01530 
01531                 for( istrt=1; istrt <= nn; istrt++ )
01532                 {
01533                         istrt_ = istrt - 1;
01534                         if( iperm[istrt_] >= 0 )
01535                         {
01536                                 indx = istrt;
01537                                 indx0 = indx;
01538                                 ttemp = x[istrt_];
01539                                 while( iperm[indx-1] > 0 )
01540                                 {
01541                                         x[indx-1] = x[iperm[indx-1]-1];
01542                                         indx0 = indx;
01543                                         iperm[indx-1] = -iperm[indx-1];
01544                                         indx = labs(iperm[indx-1]);
01545                                 }
01546                                 x[indx0-1] = ttemp;
01547                         }
01548                 }
01549 
01550                 
01551                 for( i=0; i < nn; i++ )
01552                 {
01553                         iperm[i] = -iperm[i];
01554                 }
01555         }
01556 
01557         for( i=0; i < nn; i++ )
01558         {
01559                 --iperm[i];
01560         }
01561         return;
01562 }
01563 
01564 
01565 
01566 
01567 
01568 
01569 
01570 
01571 void *MyMalloc( 
01572          
01573         size_t size ,
01574         const char *chFile, 
01575         int line
01576         )
01577 {
01578         void *ptr;
01579 
01580         DEBUG_ENTRY( "MyMalloc()" );
01581 
01582         ASSERT( size > 0 );
01583 
01584         
01585         {
01586                 
01587                 enum{DEBUG_LOC=false};
01588                 
01589                 if( DEBUG_LOC)
01590                 {
01591                         static long int kount=0, nTot=0;
01592                         nTot += (long)size;
01593                         fprintf(ioQQQ,"%li\t%li\t%li\n", 
01594                                 kount , 
01595                                 (long)size , 
01596                                 nTot );
01597                         ++kount;
01598                 }
01599         }
01600 
01601         if( ( ptr = malloc( size ) ) == NULL )
01602         {
01603                 fprintf(ioQQQ,"DISASTER MyMalloc could not allocate %lu bytes.  Exit in MyMalloc.",
01604                         (unsigned long)size );
01605                 fprintf(ioQQQ,"MyMalloc called from file %s at line %i.\n",
01606                         chFile , line );
01607                 cdEXIT(EXIT_FAILURE);
01608         }
01609 
01610         
01611 #       if !defined(NDEBUG) && !defined(NOINIT)
01612 
01613         size_t nFloat = size/4;
01614         size_t nDouble = size/8;
01615         sys_float *fptr = static_cast<sys_float*>(ptr);
01616         double *dptr = static_cast<double*>(ptr);
01617 
01618         
01619         
01620 
01621         
01622         if( size == nDouble*8 )
01623         {
01624                 
01625 
01626 
01627 
01628 
01629 
01630 
01631                 set_NaN( dptr, (long)nDouble );
01632         }
01633         else if( size == nFloat*4 )
01634         {
01635                 
01636                 set_NaN( fptr, (long)nFloat );
01637         }
01638         else
01639         {
01640                 memset( ptr, 0xff, size );
01641         }
01642 
01643 #       endif 
01644         return ptr;
01645 }
01646 
01647 
01648 
01649 
01650 
01651 
01652 void *MyCalloc( 
01653          
01654         size_t num ,
01655         size_t size )
01656 {
01657         void *ptr;
01658 
01659         DEBUG_ENTRY( "MyCalloc()" );
01660 
01661         ASSERT( size > 0 );
01662 
01663         
01664         {
01665                 
01666                 enum{DEBUG_LOC=false};
01667                 
01668                 if( DEBUG_LOC)
01669                 {
01670                         static long int kount=0;
01671                         fprintf(ioQQQ,"%li\tcall\t%li\tbytes\n", kount, 
01672                                 (long)size );
01673                         ++kount;
01674                 }
01675         }
01676 
01677         if( ( ptr = calloc( num , size ) ) == NULL )
01678         {
01679                 fprintf(ioQQQ,"MyCalloc could not allocate %lu bytes.  Exit in MyCalloc.",
01680                         (unsigned long)size );
01681                 cdEXIT(EXIT_FAILURE);
01682         }
01683         return ptr;
01684 }
01685 
01686 
01687 
01688 
01689 
01690 void *MyRealloc( 
01691          
01692         void *p ,
01693         size_t size )
01694 {
01695         void *ptr;
01696 
01697         DEBUG_ENTRY( "MyRealloc()" );
01698 
01699         ASSERT( size > 0 );
01700 
01701         
01702         {
01703                 
01704                 enum{DEBUG_LOC=false};
01705                 
01706                 if( DEBUG_LOC)
01707                 {
01708                         static long int kount=0;
01709                         fprintf(ioQQQ,"%li\tcall\t%li\tbytes\n", kount, 
01710                                 (long)size );
01711                         ++kount;
01712                 }
01713         }
01714 
01715         if( ( ptr = realloc( p , size ) ) == NULL )
01716         {
01717                 fprintf(ioQQQ,"MyRealloc could not allocate %lu bytes.  Exit in MyRealloc.",
01718                         (unsigned long)size );
01719                 cdEXIT(EXIT_FAILURE);
01720         }
01721         return ptr;
01722 }
01723 
01724 
01725 double csphot(
01726         
01727 
01728         long int inu, 
01729         
01730         long int ithr, 
01731         
01732         long int iofset)
01733 {
01734         double csphot_v;
01735 
01736         DEBUG_ENTRY( "csphot()" );
01737 
01738         csphot_v = opac.OpacStack[inu-ithr+iofset-1];
01739         return csphot_v;
01740 }
01741 
01742 
01743 
01744 
01745 
01746 
01747 
01748 
01749 
01750 
01751 
01752 
01753 
01754 
01755 
01756 
01757 
01758 
01759 
01760 
01761 
01762 
01763 
01764 
01765 
01766 double RandGauss(
01767         
01768         double xMean, 
01769         
01770         double s )
01771 {
01772         double  x1, x2, w, yy1;
01773         static double yy2=-BIGDOUBLE;
01774         static int use_last = false;
01775 
01776         DEBUG_ENTRY( "RandGauss()" );
01777 
01778         if( use_last )
01779         {
01780                 yy1 = yy2;
01781                 use_last = false;
01782         }
01783         else
01784         {
01785                 do {
01786                         x1 = 2.*genrand_real3() - 1.;
01787                         x2 = 2.*genrand_real3() - 1.;
01788                         w = x1 * x1 + x2 * x2;
01789                 } while ( w >= 1.0 );
01790 
01791                 w = sqrt((-2.0*log(w))/w);
01792                 yy1 = x1 * w;
01793                 yy2 = x2 * w;
01794                 use_last = true;
01795         }
01796         return xMean + yy1 * s;
01797 }
01798 
01799 
01800 
01801 
01802 
01803 
01804 
01805 
01806 double MyGaussRand( double PctUncertainty )
01807 {
01808         double StdDev;
01809         double result;
01810 
01811         DEBUG_ENTRY( "MyGaussRand()" );
01812 
01813         ASSERT( PctUncertainty < 0.5 );
01814         
01815         StdDev = 0.5 * PctUncertainty;
01816 
01817         do
01818         {
01819                 
01820                 result = 1.+RandGauss( 0., StdDev );
01821         }
01822         
01823 
01824         while( (result < 1.-PctUncertainty) || (result > 1+PctUncertainty) );
01825 
01826         ASSERT( result>0. && result<2. );
01827         return result;
01828 }
01829 
01830 
01831 double plankf(long int ip)
01832 {
01833         double plankf_v;
01834 
01835         DEBUG_ENTRY( "plankf()" );
01836 
01837         
01838 
01839 
01840         if( rfield.ContBoltz[ip] <= 0. )
01841         {
01842                 plankf_v = 1e-35;
01843         }
01844         else
01845         {
01846                 plankf_v = 6.991e-21*POW2(FR1RYD*rfield.anu[ip])/
01847                         (1./rfield.ContBoltz[ip] - 1.)*FR1RYD*4.;
01848         }
01849         return plankf_v;
01850 }