| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | #include "cddefines.h" |
|---|
| 7 | #include "physconst.h" |
|---|
| 8 | #include "rfield.h" |
|---|
| 9 | #include "ipoint.h" |
|---|
| 10 | #include "opacity.h" |
|---|
| 11 | #include "continuum.h" |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | STATIC void ReadTable(const string& fnam); |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | double ffun( |
|---|
| 19 | |
|---|
| 20 | double anu ) |
|---|
| 21 | { |
|---|
| 22 | double frac_beam_time; |
|---|
| 23 | |
|---|
| 24 | double frac_beam_const; |
|---|
| 25 | |
|---|
| 26 | double frac_isotropic; |
|---|
| 27 | double a; |
|---|
| 28 | |
|---|
| 29 | DEBUG_ENTRY( "ffun()" ); |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | a = ffun( anu , &frac_beam_time , &frac_beam_const , &frac_isotropic ); |
|---|
| 33 | return a; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | double ffun( |
|---|
| 39 | |
|---|
| 40 | double anu , |
|---|
| 41 | |
|---|
| 42 | double *frac_beam_time, |
|---|
| 43 | |
|---|
| 44 | double *frac_beam_const, |
|---|
| 45 | |
|---|
| 46 | double *frac_isotropic ) |
|---|
| 47 | { |
|---|
| 48 | double ffun_v; |
|---|
| 49 | static bool lgWarn = false; |
|---|
| 50 | double flx_beam_time , flx_beam_const , flx_isotropic; |
|---|
| 51 | |
|---|
| 52 | DEBUG_ENTRY( "ffun()" ); |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | ffun_v = 0.; |
|---|
| 59 | flx_beam_time = 0.; |
|---|
| 60 | flx_beam_const = 0.; |
|---|
| 61 | flx_isotropic = 0.; |
|---|
| 62 | for( rfield.ipspec=0; rfield.ipspec < rfield.nspec; rfield.ipspec++ ) |
|---|
| 63 | { |
|---|
| 64 | double one = ffun1(anu)*rfield.spfac[rfield.ipspec]; |
|---|
| 65 | ffun_v += one; |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | if( rfield.lgBeamed[rfield.ipspec] ) |
|---|
| 70 | { |
|---|
| 71 | if( rfield.lgTimeVary[rfield.ipspec] ) |
|---|
| 72 | flx_beam_time += one; |
|---|
| 73 | else |
|---|
| 74 | flx_beam_const += one; |
|---|
| 75 | } |
|---|
| 76 | else |
|---|
| 77 | flx_isotropic += one; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | if( ffun_v < SMALLFLOAT ) |
|---|
| 83 | { |
|---|
| 84 | *frac_beam_time = 0.; |
|---|
| 85 | *frac_beam_const = 1.; |
|---|
| 86 | *frac_isotropic = 0.; |
|---|
| 87 | } |
|---|
| 88 | else |
|---|
| 89 | { |
|---|
| 90 | |
|---|
| 91 | *frac_beam_time = flx_beam_time / ffun_v; |
|---|
| 92 | |
|---|
| 93 | *frac_beam_const = flx_beam_const / ffun_v; |
|---|
| 94 | |
|---|
| 95 | *frac_isotropic = flx_isotropic / ffun_v; |
|---|
| 96 | } |
|---|
| 97 | ASSERT( *frac_beam_time >=0. && *frac_beam_time<=1.+3.*DBL_EPSILON ); |
|---|
| 98 | ASSERT( *frac_beam_const >=0.&& *frac_beam_const<=1.+3.*DBL_EPSILON ); |
|---|
| 99 | ASSERT( *frac_isotropic >=0. && *frac_isotropic<=1.+3.*DBL_EPSILON ); |
|---|
| 100 | ASSERT( fabs( 1.-*frac_beam_time-*frac_beam_const-*frac_isotropic)< |
|---|
| 101 | 10.*DBL_EPSILON); |
|---|
| 102 | |
|---|
| 103 | if( ffun_v > BIGFLOAT && !lgWarn ) |
|---|
| 104 | { |
|---|
| 105 | lgWarn = true; |
|---|
| 106 | fprintf( ioQQQ, " FFUN: The net continuum is very intense.\n" ); |
|---|
| 107 | fprintf( ioQQQ, " I will try to press on, but may have problems.\n" ); |
|---|
| 108 | } |
|---|
| 109 | return( ffun_v ); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | double ffun1(double xnu) |
|---|
| 114 | { |
|---|
| 115 | char chKey[6]; |
|---|
| 116 | long int i; |
|---|
| 117 | double fac, |
|---|
| 118 | ffun1_v, |
|---|
| 119 | y; |
|---|
| 120 | static bool lgWarn = false; |
|---|
| 121 | |
|---|
| 122 | DEBUG_ENTRY( "ffun1()" ); |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | ASSERT( rfield.ipspec >= 0); |
|---|
| 127 | ASSERT( rfield.ipspec < rfield.nspec ); |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | ASSERT( xnu >= rfield.emm*0.99 ); |
|---|
| 135 | ASSERT( xnu <= rfield.egamry*1.01 ); |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | strcpy( chKey, rfield.chSpType[rfield.ipspec] ); |
|---|
| 139 | |
|---|
| 140 | if( strcmp(chKey,"AGN ") == 0 ) |
|---|
| 141 | { |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | ffun1_v = pow(xnu,-1. + rfield.cutoff[rfield.ipspec][1])* |
|---|
| 147 | sexp(xnu/rfield.slope[rfield.ipspec])*sexp(0.01/ |
|---|
| 148 | xnu); |
|---|
| 149 | |
|---|
| 150 | if( xnu > 0.1 ) |
|---|
| 151 | { |
|---|
| 152 | if( xnu < 7350. ) |
|---|
| 153 | { |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | ffun1_v += pow(xnu,rfield.cutoff[rfield.ipspec][2] - |
|---|
| 157 | 1.)*rfield.cutoff[rfield.ipspec][0]*sexp(1./ |
|---|
| 158 | xnu); |
|---|
| 159 | } |
|---|
| 160 | else |
|---|
| 161 | { |
|---|
| 162 | ffun1_v += pow(7350.,rfield.cutoff[rfield.ipspec][2] - |
|---|
| 163 | 1.)*rfield.cutoff[rfield.ipspec][0]/ |
|---|
| 164 | POW3(xnu/7350.); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | } |
|---|
| 169 | else if( strcmp(chKey,"POWER") == 0 ) |
|---|
| 170 | { |
|---|
| 171 | |
|---|
| 172 | ffun1_v = pow(xnu,-1. + rfield.slope[rfield.ipspec])* |
|---|
| 173 | sexp(xnu/rfield.cutoff[rfield.ipspec][0]+rfield.cutoff[rfield.ipspec][1]/ |
|---|
| 174 | xnu); |
|---|
| 175 | |
|---|
| 176 | } |
|---|
| 177 | else if( strcmp(chKey,"BLACK") == 0 ) |
|---|
| 178 | { |
|---|
| 179 | const double db_log = log(DBL_MAX); |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | fac = TE1RYD*xnu/rfield.slope[rfield.ipspec]; |
|---|
| 183 | |
|---|
| 184 | if( fac > db_log ) |
|---|
| 185 | { |
|---|
| 186 | ffun1_v = 0.; |
|---|
| 187 | } |
|---|
| 188 | else if( fac > 1.e-5 ) |
|---|
| 189 | { |
|---|
| 190 | ffun1_v = xnu*xnu/(exp(fac) - 1.); |
|---|
| 191 | } |
|---|
| 192 | else |
|---|
| 193 | { |
|---|
| 194 | ffun1_v = xnu*xnu/(fac*(1. + fac/2.)); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | } |
|---|
| 198 | else if( strcmp(chKey,"INTER") == 0 ) |
|---|
| 199 | { |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | if( xnu >= rfield.tNuRyd[rfield.ipspec][0]*1.000001 ) |
|---|
| 203 | { |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | i = 1; |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | while( i< NCELL-1 && rfield.tNuRyd[rfield.ipspec][i]>0. ) |
|---|
| 211 | { |
|---|
| 212 | if( xnu < rfield.tNuRyd[rfield.ipspec][i] ) |
|---|
| 213 | { |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | y = rfield.tFluxLog[rfield.ipspec][i-1] + |
|---|
| 218 | rfield.tslop[rfield.ipspec][i-1]* |
|---|
| 219 | log10(xnu/rfield.tNuRyd[rfield.ipspec][i-1]); |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | ffun1_v = pow(10.,y); |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | # ifndef NDEBUG |
|---|
| 227 | double ys1 = MIN2( rfield.tFluxLog[rfield.ipspec][i-1],rfield.tFluxLog[rfield.ipspec][i]); |
|---|
| 228 | double ys2 = MAX2( rfield.tFluxLog[rfield.ipspec][i-1],rfield.tFluxLog[rfield.ipspec][i]); |
|---|
| 229 | ys1 = pow( 10. , ys1 ); |
|---|
| 230 | ys2 = pow( 10. , ys2 ); |
|---|
| 231 | ASSERT( ffun1_v >= ys1/(1.+100.*FLT_EPSILON) ); |
|---|
| 232 | ASSERT( ffun1_v <= ys2*(1.+100.*FLT_EPSILON) ); |
|---|
| 233 | # endif |
|---|
| 234 | |
|---|
| 235 | return( ffun1_v/xnu ); |
|---|
| 236 | } |
|---|
| 237 | ++i; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | ffun1_v = 0.; |
|---|
| 241 | } |
|---|
| 242 | else |
|---|
| 243 | { |
|---|
| 244 | |
|---|
| 245 | ffun1_v = 0.; |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | else if( strcmp(chKey,"BREMS") == 0 ) |
|---|
| 250 | { |
|---|
| 251 | |
|---|
| 252 | fac = TE1RYD*xnu/rfield.slope[rfield.ipspec]; |
|---|
| 253 | ffun1_v = sexp(fac)/pow(xnu,1.2); |
|---|
| 254 | |
|---|
| 255 | } |
|---|
| 256 | else if( strcmp(chKey,"LASER") == 0 ) |
|---|
| 257 | { |
|---|
| 258 | const double BIG = 1.e10; |
|---|
| 259 | const double SMALL = 1.e-25; |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | if( xnu > (1.-rfield.cutoff[rfield.ipspec][0])*rfield.slope[rfield.ipspec] && |
|---|
| 266 | xnu < (1.+rfield.cutoff[rfield.ipspec][0])*rfield.slope[rfield.ipspec] ) |
|---|
| 267 | { |
|---|
| 268 | ffun1_v = BIG; |
|---|
| 269 | } |
|---|
| 270 | else |
|---|
| 271 | { |
|---|
| 272 | ffun1_v = SMALL; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | } |
|---|
| 276 | else if( strcmp(chKey,"READ ") == 0 ) |
|---|
| 277 | { |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | if( rfield.tslop[rfield.ipspec][0] == 0. ) |
|---|
| 281 | { |
|---|
| 282 | |
|---|
| 283 | ReadTable(rfield.ioTableRead[rfield.ipspec]); |
|---|
| 284 | rfield.tslop[rfield.ipspec][0] = 1.; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | if( xnu >= rfield.egamry ) |
|---|
| 288 | { |
|---|
| 289 | ffun1_v = 0.; |
|---|
| 290 | } |
|---|
| 291 | else |
|---|
| 292 | { |
|---|
| 293 | i = ipoint(xnu); |
|---|
| 294 | if( i > rfield.nupper || i < 1 ) |
|---|
| 295 | { |
|---|
| 296 | ffun1_v = 0.; |
|---|
| 297 | } |
|---|
| 298 | else |
|---|
| 299 | { |
|---|
| 300 | ffun1_v = rfield.ConTabRead[i-1]/rfield.anu[i-1]/rfield.anu[i-1]; |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | else if( strcmp(chKey,"VOLK ") == 0 ) |
|---|
| 309 | { |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | if( xnu >= rfield.egamry ) |
|---|
| 313 | { |
|---|
| 314 | ffun1_v = 0.; |
|---|
| 315 | } |
|---|
| 316 | else |
|---|
| 317 | { |
|---|
| 318 | i = ipoint(xnu); |
|---|
| 319 | if( i > rfield.nupper ) |
|---|
| 320 | { |
|---|
| 321 | fprintf( ioQQQ, " ffun1: Too many points - increase ncell\n" ); |
|---|
| 322 | fprintf( ioQQQ, " cell needed=%4ld ncell=%4ld\n", |
|---|
| 323 | i, rfield.nupper ); |
|---|
| 324 | cdEXIT(EXIT_FAILURE); |
|---|
| 325 | } |
|---|
| 326 | if( i > rfield.nupper || i < 1 ) |
|---|
| 327 | { |
|---|
| 328 | ffun1_v = 0.; |
|---|
| 329 | } |
|---|
| 330 | else |
|---|
| 331 | { |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | ffun1_v = rfield.tslop[rfield.ipspec][i-1]/ rfield.anu[i-1]; |
|---|
| 335 | } |
|---|
| 336 | } |
|---|
| 337 | } |
|---|
| 338 | else |
|---|
| 339 | { |
|---|
| 340 | fprintf( ioQQQ, " ffun1: I do not understand continuum label \"%s\" for continuum %li.\n", |
|---|
| 341 | chKey , rfield.ipspec); |
|---|
| 342 | cdEXIT(EXIT_FAILURE); |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | if( ffun1_v > 1e35 && !lgWarn ) |
|---|
| 346 | { |
|---|
| 347 | lgWarn = true; |
|---|
| 348 | fprintf( ioQQQ, " FFUN1: Continuum %ld is very intense.\n", |
|---|
| 349 | rfield.ipspec ); |
|---|
| 350 | fprintf( ioQQQ, " I will try to press on, but may have problems.\n" ); |
|---|
| 351 | } |
|---|
| 352 | return( ffun1_v ); |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | STATIC void ReadTable(const string& fnam) |
|---|
| 357 | { |
|---|
| 358 | char chLine[INPUT_LINE_LENGTH]; |
|---|
| 359 | long int i, |
|---|
| 360 | nPoints; |
|---|
| 361 | double Differ, |
|---|
| 362 | EnerLast; |
|---|
| 363 | FILE *io; |
|---|
| 364 | |
|---|
| 365 | DEBUG_ENTRY( "ReadTable()" ); |
|---|
| 366 | |
|---|
| 367 | io = open_data( fnam.c_str(), "r", AS_LOCAL_ONLY ); |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | if( read_whole_line( chLine , (int)sizeof(chLine) , io )==NULL ) |
|---|
| 371 | { |
|---|
| 372 | fprintf( ioQQQ, " error 1 reading input continuum file.\n" ); |
|---|
| 373 | cdEXIT(EXIT_FAILURE); |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | i = 0; |
|---|
| 378 | |
|---|
| 379 | while( (read_whole_line(chLine, (int)sizeof(chLine),io)!=NULL) && (i<rfield.nupper) ) |
|---|
| 380 | { |
|---|
| 381 | double help[2]; |
|---|
| 382 | sscanf( chLine, "%lf%lf ", &help[0], &help[1] ); |
|---|
| 383 | opac.tmn[i] = (realnum)help[0]; |
|---|
| 384 | rfield.ConTabRead[i] = (realnum)help[1]; |
|---|
| 385 | ++i; |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | nPoints = i - 1; |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | if( nPoints < 1 ) |
|---|
| 392 | { |
|---|
| 393 | fprintf( ioQQQ, " ReadTable, file for TABLE READ has too few points, =%5ld\n", |
|---|
| 394 | nPoints ); |
|---|
| 395 | cdEXIT(EXIT_FAILURE); |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | EnerLast = opac.tmn[nPoints]; |
|---|
| 402 | if( fabs(opac.tmn[0]-rfield.anu[0])/rfield.anu[0] > 1e-3 ) |
|---|
| 403 | { |
|---|
| 404 | |
|---|
| 405 | if( opac.tmn[0] <= 0. ) |
|---|
| 406 | { |
|---|
| 407 | fprintf( ioQQQ, |
|---|
| 408 | " DISASTER ReadTable: the first energy in table read file is not positive. Something is wrong.\n" ); |
|---|
| 409 | cdEXIT(EXIT_FAILURE); |
|---|
| 410 | } |
|---|
| 411 | else if( fabs(opac.tmn[0]-RYDLAM/rfield.anu[0]*1e-4)/opac.tmn[0] < |
|---|
| 412 | 1e-3 ) |
|---|
| 413 | { |
|---|
| 414 | |
|---|
| 415 | EnerLast = RYDLAM/opac.tmn[nPoints]/1e4; |
|---|
| 416 | } |
|---|
| 417 | else if( fabs(opac.tmn[0]-RYDLAM/rfield.anu[0])/opac.tmn[0] < |
|---|
| 418 | 1e-3 ) |
|---|
| 419 | { |
|---|
| 420 | |
|---|
| 421 | EnerLast = RYDLAM/opac.tmn[nPoints]; |
|---|
| 422 | } |
|---|
| 423 | else if( fabs(opac.tmn[0]-rfield.anu[0]*EVRYD*1e-3)/opac.tmn[0] < |
|---|
| 424 | 1e-3 ) |
|---|
| 425 | { |
|---|
| 426 | |
|---|
| 427 | EnerLast = opac.tmn[nPoints]/EVRYD/1e-3; |
|---|
| 428 | } |
|---|
| 429 | else if( fabs(opac.tmn[0]-rfield.anu[0]*EVRYD)/opac.tmn[0] < |
|---|
| 430 | 1e-3 ) |
|---|
| 431 | { |
|---|
| 432 | |
|---|
| 433 | EnerLast = opac.tmn[nPoints]/EVRYD; |
|---|
| 434 | } |
|---|
| 435 | else |
|---|
| 436 | { |
|---|
| 437 | fprintf( ioQQQ, " DISASTER ReadTable: the energy scale in the table read file makes no sense to me.\n" ); |
|---|
| 438 | cdEXIT(EXIT_FAILURE); |
|---|
| 439 | } |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | Differ = fabs(EnerLast-rfield.anu[nPoints])/rfield.anu[nPoints]; |
|---|
| 444 | if( Differ > 0.001 ) |
|---|
| 445 | { |
|---|
| 446 | fprintf( ioQQQ, " DISASTER ReadTable: The energy mesh of the file read in by the TABLE READ command does not agree with this version of Cloudy.\n" ); |
|---|
| 447 | fprintf( ioQQQ, " DISASTER ReadTable: Was the file generated by an older version of the code?\n" ); |
|---|
| 448 | fprintf( ioQQQ, " DISASTER ReadTable: Did the first model do more than one iteration, but the LAST option was missing on PUNCH LAST TRANSMITTED CONTINUUM?\n"); |
|---|
| 449 | fprintf( ioQQQ, " DISASTER ReadTable: Number of points read in=%5ld\n", |
|---|
| 450 | nPoints ); |
|---|
| 451 | fprintf( ioQQQ, " ReadTable: input, internal energies=%12.4e%12.4e\n", |
|---|
| 452 | opac.tmn[nPoints], rfield.anu[nPoints] ); |
|---|
| 453 | cdEXIT(EXIT_FAILURE); |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | for( i=nPoints + 1; i < rfield.nupper; i++ ) |
|---|
| 458 | { |
|---|
| 459 | rfield.ConTabRead[i] = 0.; |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | fclose(io); |
|---|
| 463 | return; |
|---|
| 464 | } |
|---|