HARM
harm and utilities
 All Data Structures Files Functions Variables Typedefs Macros Pages
init.koral.c
Go to the documentation of this file.
1 // GODMARK: With WALD, symmetry and noise and high bsq/rho
2 
7 // issues:
8 
9 // runnew8: RADTUBE: oscillations and slow. averageiter=6.7
10 // runnew9: RADSHADOW : FAILINFOs without mathematica solution and CASE and slow. prad1 fills-in shadow. maybe radfixups worked to help that.
11 // runnew10: RADDBLSHADOW: Odd spot in prad0 in top-right in im8p0s0l0001.r8 . More fake substructure near left wall, but more narrow shadow beam.
12 // runnew11: ATMSTATIC: prad0,1 evolve majorly, but that's correct.
13 // runnew12: RADATM: fine.
14 // runnew13: RADBEAM2D: ok
15 // runnew14: RADWALL: Bit noisy. Probably need to use stronger shock condition for radiation.
16 // runnew15: RADWAVE: Unsure if right. too fast.
17 // runnew16: RADBONDI : goes crazy, bad in prad.
18 // runnew17: RADDOT: CASEGEN failures!!! and "Bad inv" problems. Has stripes along certain directions unlike with MINM and older code. Maybe PARA shock reduction will help here too. But expanding flow? Or try 2Dify shock flattener.
19 // RADNT: IC look odd, but need to look at next step.
20 // runnew23: raddonut still was running and very bad behavior
21 // runnew18: RADCYLBEAM? was still running.
22 // runnew19: RADBEAM2DKSVERT: Goes crazy.
23 // runnew20: RADCYLBEAMCART : Maybe ok.
24 
25 
26 
27 #include "decs.h"
28 
29 static SFTYPE rhomax=0,umax=0,uradmax=0,utotmax=0,pmax=0,pradmax=0,ptotmax=0,bsq_max=0; // OPENMPMARK: These are ok file globals since set using critical construct
30 static SFTYPE beta,randfact,rin,rinfield,routfield; // OPENMPMARK: Ok file global since set as constant before used
31 static FTYPE rhodisk;
32 static FTYPE nz_func(FTYPE R) ;
33 static FTYPE taper_func2(FTYPE R,FTYPE rin, FTYPE rpow) ;
34 static int fieldprim(int whichmethod, int whichinversion, int *whichvel, int*whichcoord, int ii, int jj, int kk, FTYPE *pr);
35 
37 // WALD STUFF
38 FTYPE B0WALD; // set later
39 // DOWALDDEN=0 : Turn off doing WALD
40 // DOWALDDEN=1 : among other things, set densities as floor-like with below b^2/rho at horizon. Should also choose FIELDTYPE==FIELDWALD.
41 // DOWALDDEN=2 : monopole case against disk equator
42 int DOWALDDEN=0;
43 int WALDWHICHACOV=3; // which component : -1 is all of them
44 //FTYPE BSQORHOWALD=50.0; // leads to too large b^2/rho and uu0 cylindrical shock forms at r\sim 2r_g and remains forever (at least at 128x64)
48 
49 
50 
51 //FTYPE thindiskrhopow=-3.0/2.0; // can make steeper like -0.7
52 //FTYPE thindiskrhopow=-0.2; // closer to NT73
53 FTYPE thindiskrhopow=-0.6; // closer to thick disk // SUPERMADNEW
54 
56 int inittypeglobal; // for bounds to communicate detail of what doing
57 
58 #define SLOWFAC 1.0 /* reduce u_phi by this amount */
59 #define MAXPASSPARMS 10
60 
61 // GODMARK: Thetarot. see whats in init that overwrites restart input. You might need to set THETAROT directly in
62 
63 //#define THETAROTMETRIC (0.5*0.7)
64 //#define USER_THETAROTMETRIC (M_PI*0.25)
65 #define USER_THETAROTMETRIC (0.0) // WALD
66 #define USER_THETAROTPRIMITIVES (0.0) // probably want to choose 0, so initial conditions are as if no tilt // WALD -> make same as USER_THETAROTMETRIC
67 
68 
69 
70 #define NOFIELD -1
71 #define DISK1FIELD 0
72 #define DISK2FIELD 1
73 #define VERTFIELD 2
74 #define DISK1VERT 3
75 #define DISK2VERT 4
76 #define BLANDFORDQUAD 5
77 #define TOROIDALFIELD 6
78 #define OHSUGAFIELD 7
79 #define MONOPOLAR 8
80 #define OLEKFIELD 9
81 #define FIELDJONMAD 10
82 #define FIELDWALD 11
83 #define MONOPOLE 12
84 #define SPLITMONOPOLE 13
85 
86 
87 
88 
89 // NOTE on units:
90 // Many things below are so far in code units, not physical units, so they don't need conversion. This includes:
91 //
92 // Rin, Rout, tf, DTdumpgen
93 
94 
95 
112 
113 
114 
115 int RADBEAM2DKSVERT_BEAMNO; // global for bounds.koral.c
116 
121 
129 
134 
139 
140 
141 
142 
180 
181 
182 
190 
191 
203 
204 
205 
225 
232 
234 
235 static int get_full_rtsolution(int *whichvel, int *whichcoord, int opticallythick, FTYPE *pp,FTYPE *X, FTYPE *V,struct of_geom **ptrptrgeom);
236 static int make_nonrt2rt_solution(int *whichvel, int *whichcoord, int opticallythick, FTYPE *pp,FTYPE *X, FTYPE *V,struct of_geom **ptrptrgeom);
237 static int donut_analytical_solution(int *whichvel, int *whichcoord, int opticallythick, FTYPE *pp,FTYPE *X, FTYPE *V,struct of_geom **ptrptrgeom, FTYPE *ptptr);
238 static int process_solution(int *whichvel, int *whichcoord, int opticallythick, FTYPE *pp,FTYPE *X, FTYPE *V,struct of_geom **ptrptrgeom, FTYPE *ptptr);
239 
240 
241 
242 
244 
246 {
247  int funreturn;
248 
250 
251 
252  // set periodicity in x1,x2,x3 directions
253 
254  // fully periodic problems
255  if(WHICHPROBLEM==FLATNESS){
257  }
258  // if ever only 1D problems
260  periodicx1=0;
262  }
263  // if ever only 1D problems
264  else if(WHICHPROBLEM==RADWAVE){
265  periodicx1=1;
266  }
267  // problems with no necessary symmetry
270  }
271  // periodic in x3
272  else if(WHICHPROBLEM==RADCYLBEAM){
274  periodicx3=1;
275  }
276  // periodic in x2
277  else if(WHICHPROBLEM==RADSHADOW){
279  periodicx2=1;
280  }
281  // spherical polar problems:
284  periodicx3=1;
285  }
286  else if(WHICHPROBLEM==KOMIPROBLEM){
287  periodicx1=0;
289  }
290  // periodic in x3
291  else if(WHICHPROBLEM==RADCYLJET){
293  periodicx3=1;
294  }
295  // assume spherical polar problems:
296  else{
298  periodicx3=1;
299  }
300 
301  // Also: SET USEROMIO to 0 or 1 in mympi.definit.h (needs to be 0 for TEXTOUTPUT)
302  if(PRODUCTION==0||DOWALDDEN!=0){ // for now DOWALDDEN!=0
303  binaryoutput=TEXTOUTPUT; // WALDPRODUCTION
304  // KRAKEN: comment out above. And change mympi.definit.h's USEROMIO 0 to 1 for the "choice" version.
305  }
306 
307  // binaryoutput=TEXTOUTPUT;
308 
309 
310 
311  return(0);
312 
313 }
314 
315 
317 {
318  // defaults
319  // h_over_r=0.3;
320  h_over_r=0.2;
322 
323  if(WHICHPROBLEM==RADDONUT){
325  // h_over_r=0.02;
326  h_over_r=0.2; // SUPERMADNEW
327  }
328  else{
329  h_over_r=0.2;
331  }
332  }
333 
334 
335 
336 
338 
339 
340  if(WHICHPROBLEM==RADCYLJET){
341  static int firsttime=1;
342  if(firsttime==1){
343  firsttime=0;
344  int itid;
345  for(itid=0;itid<numprocs;itid++){
346  if(itid==myid){
347  FILE *fstar;
348  if((fstar=fopen("star.txt","rt"))==NULL){
349  dualfprintf(fail_file,"Couldn't open star.txt, assume values not used.\n");
350  }
351  else{
352  logfprintf("opened star.txt and got contents\n");
354  fclose(fstar);
355  }
356  }
357 #if(USEMPI)
358  MPI_Barrier(MPI_COMM_WORLD);
359 #endif
360  }
361  }
362  }
363 
364 
365  return(0);
366 }
367 
368 int set_fieldfrompotential(int *fieldfrompotential)
369 {
370  int pl,pliter;
371 
372  // default (assume all fields are from potential)
373  PLOOPBONLY(pl) fieldfrompotential[pl-B1+1]=1;
374 
375  // force B3=0 so only using poloidal part of Wald solution.
376  //int pl=B3; fieldfrompotential[pl-B1+1]=0;
377 
378  //In the case of Komissarov's tests, set up the field directly in all cases
380  PLOOPBONLY(pl) fieldfrompotential[pl-B1+1]=0;
381  }
382 
383 
384 
385 
386  return(0);
387 }
388 
389 
390 int init_conservatives(FTYPE (*prim)[NSTORE2][NSTORE3][NPR],FTYPE (*pstag)[NSTORE2][NSTORE3][NPR], FTYPE (*Utemp)[NSTORE2][NSTORE3][NPR], FTYPE (*U)[NSTORE2][NSTORE3][NPR])
391 {
392  int funreturn;
393  int fieldfrompotential[NDIM];
394 
395  set_fieldfrompotential(fieldfrompotential);
396 
397  funreturn=user1_init_conservatives(fieldfrompotential, prim,pstag, Utemp, U);
398  if(funreturn!=0) return(funreturn);
399 
400 
401  return(0);
402 
403 }
404 
405 
407 {
408  int funreturn;
409 
410  funreturn=user1_post_init_specific_init();
411  if(funreturn!=0) return(funreturn);
412 
413 
414 
415  trifprintf("WHICHPROBLEM: %d\n",WHICHPROBLEM);
416  // print out units and some constants
417  trifprintf("Constants\n");
418  trifprintf("LBAR=%g TBAR=%g VBAR=%g RHOBAR=%g MBAR=%g UBAR=%g TEMPBAR=%g\n",LBAR,TBAR,VBAR,RHOBAR,MBAR,UBAR,TEMPBAR);
419  trifprintf("ARAD_CODE=%26.20g OPACITYBAR=%g\n",ARAD_CODE,OPACITYBAR);
420  trifprintf("ARAD_CODE_DEF=%g\n",ARAD_CODE_DEF);
421  trifprintf("GAMMAMAXRAD=%g\n",GAMMAMAXRAD);
422 
423  trifprintf("MASSCM=%g 1 koral unit = %g harm units (g/cm^3)\n",MASSCM,KORAL2HARMRHO(1.0));
424 
425  if(myid==0){
426  // 22 things
427 #define DIMVARLIST GGG,CCCTRUE,MSUNCM,MPERSUN,LBAR,TBAR,VBAR,RHOBAR,MBAR,ENBAR,UBAR,TEMPBAR,ARAD_CODE_DEF,XFACT,YFACT,ZFACT,MUMEAN,MUMEAN,OPACITYBAR,MASSCM,KORAL2HARMRHO(1.0),TEMPMIN
428 #if(REALTYPE==FLOATYPE || REALTYPE==DOUBLETYPE)
429 #define DIMTYPELIST "%21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g\n"
430 #elif(REALTYPE==LONGDOUBLETYPE)
431 #define DIMTYPELIST "%26.20Lg %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g %26.20g\n"
432 #else
433 #error "no such type."
434 #endif
435 
436  FILE *dimfile;
437  dimfile=fopen("dimensions.txt","wt");
438  if(dimfile!=NULL){
439  fprintf(dimfile,DIMTYPELIST,DIMVARLIST);
440  fclose(dimfile);
441  }
442  else{
443  dualfprintf(fail_file,"Could not open dimensions.txt\n");
444  myexit(394582526);
445  }
446  }
447 
448 
449  // override anything set by restart file.
450 #if(PRODUCTION==0)
451  debugfail=2;
452 #else
453  debugfail=0;
454 #endif
455 
456  tf = 200000;
457 
458 
459 
460  return(0);
461 }
462 
463 
464 
465 int init_consts(void)
466 {
467  // Lunit=Tunit=Munit=1.0;
468 
469  // units can be used for user to read in data, but otherwise for rest of code all that matters is Mfactor and Jfactor
470  Mfactor=Jfactor=1.0;
471 
472  MBH=1.0;
473 
474  return(0);
475 
476 }
477 
478 
479 
480 
481 
482 int init_global(void)
483 {
484  int pl,pliter;
485  int funreturn;
486 
487  funreturn=user1_init_global();
488  if(funreturn!=0) return(funreturn);
489 
490 
491  init_defcoord(); // just avoids splitting function call, here sets a
492 
493 
494  // default
496 
497 
498  // ERADLIMIT=UUMIN; // set same for now
499  ERADLIMIT=UUMINLIMIT; // seems fine.
500 
501  if(WHICHPROBLEM==RADDONUT){
502  ERADLIMIT=1E-20; // choose so smallest value PRAD0 likely to obtain, but no smaller else problems with YFL4 (well, still issues)
503  }
504 
505 
506 
507  // maximum radiation frame lorentz factor
508  //GAMMAMAXRAD=10000.0; // problems with PARA or TIMEORDER=3 for NLEFT=0.99999 with RADBEAM2D, so stick to gammamax=100 in general unless for test.
509  // NOTE: Even PARA,TO=3 can handle cartesian beams like RADBEAM2D or RADSHADOW without problems, but if injection gamma > GAMMAMAXRAD, then that limiting process causes problems currently. Looking into it.
510  GAMMAMAXRAD=100.0;
511 
512 
513 
515  // overrides for more detailed problem dependence
516  // TIMEORDER=2; // no need for 4 unless higher-order or cold collapse problem.
517  // TIMEORDER=4;
518  TIMEORDER=3; // more smooth accurate solution than TIMEORDER=4 or 2 (midpoint or TVD)
519 
521  // lim[1]=lim[2]=lim[3]=DONOR;
522  // lim[1]=lim[2]=lim[3]=MINM;
523  lim[1]=lim[2]=lim[3]=MC;
524  // lim[1]=lim[2]=lim[3]=WENO5BND;
525  //lim[1]=lim[2]=lim[3]=PARAFLAT;
526  //lim[1]=lim[2]=lim[3]=PARALINE;
527  }
528  else{
529  // lim[1]=lim[2]=lim[3]=DONOR;
530  // lim[1]=lim[2]=lim[3]=MINM;
531  //lim[1]=lim[2]=lim[3]=MC;
532  // lim[1]=lim[2]=lim[3]=WENO5BND;
533  //lim[1]=lim[2]=lim[3]=PARAFLAT;
534  lim[1]=lim[2]=lim[3]=PARALINE;
535  }
536 
537  // cour=0.1;
538  // cour=0.5;
539  // cour=0.9; // works fine, but 0.8 more generally good. Although sometimes cour=0.9 actually gives a bit smoother solution.
540  // cour=0.8;
541  cour=0.49999; // 0.8 is too unstable for RADBEAM2D with curved flow relative to grid.
542 
543  if(DOWALDDEN){
544  PALLLOOP(pl) fluxmethod[pl]=HLLFLUX; // lower errors in unresolved regions
545  lim[1]=lim[2]=lim[3]=MC; // to preserve symmetry better
546  }
547  else{
548  PALLLOOP(pl) fluxmethod[pl]=LAXFFLUX; //HLLFLUX;
549  // HLL leads to problems with radiation and realistic opacities.
550  PALLLOOP(pl) if(RADFULLPL(pl)) fluxmethod[pl]=LAXFFLUX;
551  }
552 
553  //FLUXB=FLUXCTTOTH;
555 
556 
557  // rescaletype=1;
558  rescaletype=4;
559 
560  // if(DOWALDDEN) rescaletype=4;
561  if(DOWALDDEN) rescaletype=5; // like 4, but b^2/rho scales as 1/r away from horizon
562 
563  BSQORHOLIMIT=1E3; // was 1E2 but latest BC test had 1E3 // CHANGINGMARK // was 2E2 but
564  BSQOULIMIT=1E9; // was 1E3 but latest BC test had 1E4. was 1E5 but needed like 1E7 to 1E8 to avoid gastemperature in funnel being repeatedly forced up even when Compton and other processes keep low. Also makes next solution guess for implicit solver very different, and takes longer to converge. // Up to 1E9 to allow T same for higher BSQORHOLIMIT=1E3
565  UORHOLIMIT=1E10; // has to be quite high, else hit floor in high optical depth cases and run-away injection of u and then rho.
566  RHOMIN = 1E-4;
567  UUMIN = 1E-6;
568  //OSMARK: where is DTr1 defined? what is DTfake?
569  DTfake=MAX(1,DTr/10);
570 
571 
572 
573 
574  /*************************************************/
575  /*************************************************/
576  /*************************************************/
577 
578  if(WHICHPROBLEM==FLATNESS){
579 
580  // lim[1]=lim[2]=lim[3]=MINM;
581  // cour=0.5;
582  gam=gamideal=5.0/3.0;
583  cooling=KORAL;
584 
585  BCtype[X1UP]=PERIODIC; // OUTFLOW;
587  BCtype[X2UP]=PERIODIC; // OUTFLOW;
589  BCtype[X3UP]=PERIODIC; // OUTFLOW;
591 
592  int idt;
593  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.05; // default dumping period
594 
595  DTr = 100; //number of time steps for restart dumps
596  tf = 10.0; //final time
597  }
598 
599  /*************************************************/
600  /*************************************************/
601  /*************************************************/
602 
603 
605 
606  // TIMEORDER=3;
607  // lim[1]=lim[2]=lim[3]=PARALINE;
608 
609  // cour=0.1;
610  // cour=0.5;
611  // cour=0.9; // works fine, but 0.8 more generally good.
612  cour=0.8;
613  gam=gamideal=5.0/3.0;
614  cooling=KORAL;
615 
620  BCtype[X3UP]=OUTFLOW;
622 
623  // sigmarad = 1.56E-64
624  // arad=4*sigmarad/c
625  // NOTE: Koral code has different values than paper
626  ARAD_CODE=4.0*1.56E-64*(TEMPBAR*TEMPBAR*TEMPBAR*TEMPBAR); // to match koral and avoiding real units
627 
628 
629  int idt;
630  // for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=1E3;
631 
632  DTr = 100; //number of time steps for restart dumps
634  tf=1E5;
635  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=100.0; // Koral output steps
636  //for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.1; // testing
637  }
638  else if(WHICHPROBLEM==RADPULSE){
639  tf = 35; //final time
640  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.1;
641  }
642  else if(WHICHPROBLEM==RADPULSE3D){
643  tf = 70; //final time
644  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.1;
645  }
646 
647  // DODIAGEVERYSUBSTEP = 1;
648 
649  }
650 
651  /*************************************************/
652  /*************************************************/
653  /*************************************************/
654 
655  // TOTRY: SEe if koral is going Erf<0
656  // TOTRY: matching F/E and seeing koral fails.
657 
659  //cour=0.8; // this or with old MINDTSET, causes Erf<0 for default koral test
660  // cour=0.5;
661  // lim[1]=lim[2]=lim[3]=MINM;
662  // lim[1]=lim[2]=lim[3]=PARALINE;
663  // gam=gamideal=5.0/3.0;
664  gam=gamideal=4.0/3.0; // koral now
665  cooling=KORAL;
666 
667  // RADBEAMFLAT_FRATIO=0.995; // koral at some point.
668  RADBEAMFLAT_FRATIO=0.99995;
669  RADBEAMFLAT_ERAD=1./RHOBAR; // 1g/cm^3 worth of energy density in radiation
670  RADBEAMFLAT_RHO=1./RHOBAR; // 1g/cm^3
671  RADBEAMFLAT_UU=0.1/RHOBAR; // 0.1g/cm^3 worth of energy density in fluid
672 
673  // avoid hitting gamma ceiling
675 
676 
681  BCtype[X3UP]=PERIODIC;
683 
684  int idt;
685  // for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.05; // default dumping period
686  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.1; // like problem24 in koral
687 
688  DTr = 100; //number of time steps for restart dumps
689  tf = 10.0; //final time
690  }
691 
692  /*************************************************/
693  /*************************************************/
694  /*************************************************/
695 
696  if(WHICHPROBLEM==RADTUBE){
697 
698  // 1,2,3,31,4,41,5
699  //#define NTUBE 1
700 #define NTUBE 31 // harder near t=0 at discontinuity
701  //#define NTUBE 5
702  //#define NTUBE 3
703 
704  // lim[1]=lim[2]=lim[3]=MINM; // NTUBE=1 has issues near cusp, so use MINM
705  // should have PARA(LINE) not oscillate so much at cusp
706  // Also should eliminate PARA's zig-zag steps in internal energy density in other tests.
707  //cour=0.5;
708  cooling=KORAL;
709 
710  // arad = 4*sigmarad/c (so removed /4. from koral sigma setup).
711  // Note, sigmarad or arad is not arbitrary -- value chosen to IC in radiative-hydro balance for each separately the left and right states.
712  if(NTUBE==1){
713  gam=gamideal=5./3.;
714  ARAD_CODE=(1e-8/pow(calc_PEQ_Tfromurho(3.e-5/(gam-1.),1.),4.));
715  }
716  else if(NTUBE==2){
717  gam=gamideal=5./3.;
718  ARAD_CODE=(2e-5/pow(calc_PEQ_Tfromurho(4.e-3/(gam-1.),1.),4.));
719  }
720  else if(NTUBE==3){
721  gam=gamideal=2.;
722  ARAD_CODE=(2./pow(calc_PEQ_Tfromurho(60./(gam-1.),1.),4.));
723  }
724  else if(NTUBE==31){
725  gam=gamideal=2.;
726  ARAD_CODE=(2./pow(calc_PEQ_Tfromurho(60./(gam-1.),1.),4.));
727  }
728  else if(NTUBE==4){
729  gam=gamideal=5./3.;
730  ARAD_CODE=(.18/pow(calc_PEQ_Tfromurho(6.e-3/(gam-1.),1.),4.));
731  }
732  else if(NTUBE==41){
733  gam=gamideal=5./3.;
734  ARAD_CODE=(.18/pow(calc_PEQ_Tfromurho(6.e-3/(gam-1.),1.),4.));
735  }
736  else if(NTUBE==5){
737  gam=gamideal=2.;
738  ARAD_CODE=(2./pow(calc_PEQ_Tfromurho(60./(gam-1.),1.),4.));
739  }
740 
741 
742  trifprintf("RADTUBE NTUBE=%d ARAD_CODE=%g SIGMARAD_CODE=%g\n",NTUBE,ARAD_CODE,ARAD_CODE/4.0);
743 
744 
747  BCtype[X2UP]=OUTFLOW; // NOTEMARK: Koral sets fixed BCs. We can do that following the IC choices, but not necessary.
749  BCtype[X3UP]=PERIODIC;
751 
752  int idt;
753  if(NTUBE==5){
754  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=1.0;
755  }
756  else{
757  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=2.0;
758  }
759 
760  DTr = 100; //number of time steps for restart dumps
761  // tf = 100.0; //final time (seems almost good enough to get quasi-steady solution for these steady tube tests)
762  if(NTUBE==5) tf=15.0; // koral paper shows about t=13, and code has no problem going much further than 15.
763  else tf = 3E2; //final time (good enough to see any evolution and errored evolution)
764  }
765 
766  /*************************************************/
767  /*************************************************/
768  /*************************************************/
769 
770  if(WHICHPROBLEM==RADSHADOW){
771 
772  // lim[1]=lim[2]=lim[3]=MINM; // NTUBE=1 has issues near cusp, so use MINM
773  // should have PARA(LINE) not oscillate so much at cusp
774  // Also should eliminate PARA's zig-zag steps in internal energy density in other tests.
775  // cour=0.5;
776  gam=gamideal=1.4;
777  cooling=KORAL;
778  ARAD_CODE=1E7*1E-5*(2.5E-9/7.115025791e-10); // tuned so radiation energy flux puts in something much higher than ambient, while initial ambient radiation energy density lower than ambient gas internal energy.
779 
780  RADSHADOW_NLEFT=0.99999;
781  RADSHADOW_ANGLE=0.0;
782  RADSHADOW_TLEFTOTAMB=100.0;
783  RADSHADOW_BEAMY=0.3;
784 
785  // avoid hitting gamma ceiling
786  GAMMAMAXRAD=MAX(GAMMAMAXRAD,2.0*1.0/sqrt(1.0-RADSHADOW_NLEFT*RADSHADOW_NLEFT));
787 
788 
793  BCtype[X3UP]=PERIODIC;
795 
796  int idt;
797  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.5;
798 
799  DTr = 100; //number of time steps for restart dumps
800  // tf = 100.0; //final time (seems almost good enough to get quasi-steady solution for these steady tube tests)
801  tf = 10.0; //final time
802  }
803 
804 
805 
806 
807  /*************************************************/
808  /*************************************************/
809  /*************************************************/
810 
812 
813  // lim[1]=lim[2]=lim[3]=MINM; // NTUBE=1 has issues near cusp, so use MINM
814  // lim[1]=lim[2]=lim[3]=MC; // MC gets totally bonkers answer with NLEFT=0.99999
815  //lim[1]=lim[2]=lim[3]=PARALINE; // bonkers answer for NLEFT=0.99999, ok for NLEFT=0.99
816  // should have PARA(LINE) not oscillate so much at cusp
817  // Also should eliminate PARA's zig-zag steps in internal energy density in other tests.
818  //cour=0.5;
819  // cour=0.49; // doesn't help oscillations for NLEFT=0.99999 with MINM
820  gam=gamideal=1.4;
821  cooling=KORAL;
822  // ARAD_CODE=1E-30;
823  // ARAD_CODE=1E7*1E-5*(2.5E-9/7.115025791e-10); // tuned so radiation energy flux puts in something much higher than ambient, while initial ambient radiation energy density lower than ambient gas internal energy.
824  ARAD_CODE=1E7*1E-5*1E-10*(6E-9/1.7E-25); // tuned so radiation energy flux puts in something much higher than ambient, while initial ambient radiation energy density lower than ambient gas internal energy. And also similar value as in Figure 11 koral paper plot. As long as prad0<<u and prad0<<rho, solution is independent of ARAD because 4-force off radiation on the fluid is negligible. Then kappa just sets what rho becomes \tau\sim 1 and nothing about the fluid is affected.
825 
826 
827 
828  // RADDBLSHADOW_NLEFT=0.99999; // Works well with MINM (only 49 total failures at relatively early time for otherwise default setup). very hard on code -- only MINM with jon choice for CASES works.
829  // RADDBLSHADOW_NLEFT=0.99; // koral paper
830  // RADDBLSHADOW_NLEFT=0.999; // latest koral (ok to use, weak oscillations with LAXF)
831  //PALLLOOP(pl) fluxmethod[pl]=HLLFLUX; // smaller oscillations even at 0.99999
832 
833 
834  // RADDBLSHADOW_NLEFT=0.7;
835  // RADDBLSHADOW_NLEFT=0.93;
836 
837  // angle=0.4; // koral paper
838  // angle=0.3; // latest koral
839 
840  // RADDBLSHADOW_NLEFT=0.99; // what's in HARMRAD
841  // RADDBLSHADOW_NLEFT=0.99999; // works but noisy
842  RADDBLSHADOW_NLEFT=0.999;
843  RADDBLSHADOW_ANGLE=0.4;
845  RADDBLSHADOW_BEAMY=0.3;
846 
847  // avoid hitting gamma ceiling
848  GAMMAMAXRAD=MAX(GAMMAMAXRAD,2.0*1.0/sqrt(1.0-RADDBLSHADOW_NLEFT*RADDBLSHADOW_NLEFT));
849 
850 
854  BCtype[X2DN]=ASYMM;
855  BCtype[X3UP]=PERIODIC;
857 
858  int idt;
859  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=2E-1;
860 
861  DTr = 100; //number of time steps for restart dumps
862  // tf = 100.0; //final time (seems almost good enough to get quasi-steady solution for these steady tube tests)
863  // tf = 200.0; //final time (far past plot so see evolves or stationary).
864  tf = 20.0; //final time (far past plot so see evolves or stationary).
865  }
866 
867 
868  /*************************************************/
869  /*************************************************/
870  /*************************************************/
871 
873 
874  RADBEAM2D_BEAMNO=1; // 1-4
875  // whether constant or radially varying background
876  // ==0 doesn't make much sense for Minkowski without gravity, because flow reverses due to chosen high density
878 
879 
880  //lim[1]=lim[2]=lim[3]=MINM; // NTUBE=1 has issues near cusp, so use MINM
881  // cour=0.5;
882  // cour=0.2; // doesn't seem to help avoid failures for this test
883 
884  a=0.0; // no spin in case use MCOORD=KSCOORDS
885 
886  if(!(ISSPCMCOORDNATIVE(MCOORD))){
887  dualfprintf(fail_file,"Must choose MCOORD (currently %d) to be spherical polar grid type for RADBEAM2D\n",MCOORD);
888  myexit(3434628752);
889  }
890 
891  gam=gamideal=1.4;
892  cooling=KORAL;
893  // ARAD_CODE=ARAD_CODE_DEF*1E5; // tuned so radiation energy flux puts in something much higher than ambient, while initial ambient radiation energy density lower than ambient gas internal energy.
894 
896  if(MCOORD==KSCOORDS||BLCOORDS){
897  BCtype[X1DN]=HORIZONOUTFLOW; // if SPCMINKMETRIC with no gravity, suckingin on boundary can leave prad0 very small and then pradi~c for no good reason
898  }
899  else{
900  // BCtype[X1DN]=OUTFLOW;
902  }
905  // BCtype[X3UP]=FREEOUTFLOW;
908 
909 
910 
911  FTYPE DTOUT1;
912  if (RADBEAM2D_BEAMNO==1){
913  tf = 10.0*(M_PI*0.5)*3.0; //final time
914  DTOUT1=tf/100.0; //dt for basic output
915  }
916  else if (RADBEAM2D_BEAMNO==2){
917  tf = 10.0*(M_PI*0.5)*6.0; //final time
918  DTOUT1=tf/100.0; //dt for basic output
919  }
920  else if (RADBEAM2D_BEAMNO==3){
921  tf = 10.0*(M_PI*0.5)*15.0; //final time
922  DTOUT1=tf/100.0; //dt for basic output
923  }
924  else if (RADBEAM2D_BEAMNO==4){
925  tf = 10.0*(M_PI*0.5)*40.0; //final time
926  DTOUT1=tf/100.0; //dt for basic output
927  }
928 
929  int idt;
930  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=DTOUT1;
931  // for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.001; // testing
932 
933  DTr = 100; //number of time steps for restart dumps
934 
935  // DODIAGEVERYSUBSTEP = 1;
936 
937  }
938  /*************************************************/
939  /*************************************************/
940  /*************************************************/
941 
943 
944  RADBEAM2DKSVERT_BEAMNO=5; // 1-5
945  // whether constant or radially varying background
946  // ==0 doesn't make much sense for Minkowski without gravity, because flow reverses due to chosen high density
948 
949 
950  // lim[1]=lim[2]=lim[3]=MINM; // NTUBE=1 has issues near cusp, so use MINM
951  // cour=0.5;
952 
953 
954  a=0.0; // no spin in case use MCOORD=KSCOORDS
955 
956  if(!(ISSPCMCOORDNATIVE(MCOORD))){
957  dualfprintf(fail_file,"Must choose MCOORD (currently %d) to be spherical polar grid type for RADBEAM2DKSVERT\n",MCOORD);
958  myexit(3434628752);
959  }
960 
961  gam=gamideal=1.4;
962  cooling=KORAL;
963  // ARAD_CODE=ARAD_CODE_DEF*1E5; // tuned so radiation energy flux puts in something much higher than ambient, while initial ambient radiation energy density lower than ambient gas internal energy.
964 
966  //BCtype[X1DN]=OUTFLOW;
970  // BCtype[X3UP]=FREEOUTFLOW;
973 
974 
975  FTYPE DTOUT1;
976  if (RADBEAM2DKSVERT_BEAMNO==1){
977  DTOUT1=1; //dt for basic output
978  }
979  else if (RADBEAM2DKSVERT_BEAMNO==2){
980  DTOUT1=.4; //dt for basic output
981  }
982  else if (RADBEAM2DKSVERT_BEAMNO==3){
983  DTOUT1=1.; //dt for basic output
984  }
985  else if (RADBEAM2DKSVERT_BEAMNO==4){
986  DTOUT1=.25; //dt for basic output
987  }
988  else if (RADBEAM2DKSVERT_BEAMNO==5){
989  DTOUT1=.4; //dt for basic output
990  }
991 
992  int idt;
993  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=DTOUT1;
994  // for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.001;
995 
996  DTr = 100; //number of time steps for restart dumps
997  tf = 20.0; //final time
998 
999  // DODIAGEVERYSUBSTEP = 1;
1000 
1001  }
1002 
1003  /*************************************************/
1004  /*************************************************/
1005  /*************************************************/
1006 
1007  if(WHICHPROBLEM==ATMSTATIC){
1008 
1009  // lim[1]=lim[2]=lim[3]=MINM;
1010  // lim[1]=lim[2]=lim[3]=PARALINE; // actually more error in u^r than MINM for inner radial boundary points (~factor of two larger u^r).
1011  // NOTE: with FTYPE as double, not enough precision to have good convergence for u^r -- just noise. See makefile.notes for how to go to ldouble and then has same error as koral.
1012  // cour=0.5;
1013 
1014 
1015  if(!(ISSPCMCOORDNATIVE(MCOORD))){
1016  dualfprintf(fail_file,"Must choose MCOORD (currently %d) to be spherical polar grid type for ATMSTATIC\n",MCOORD);
1017  myexit(3434628752);
1018  }
1019 
1020  a=0.0; // no spin in case use MCOORD=KSCOORDS
1021  gam=gamideal=1.4;
1022  cooling=KORAL;
1023  ARAD_CODE=0.0;
1024 
1025  // HORIZONOUTFLOW or HORIZONOUTFLOWSTATIC leads to little bit more static solution near inner radial boundary due to higher-order interpolation. Could also fix values as in Koral, but odd to fix values for incoming flow.
1026  // BCtype[X1UP]=HORIZONOUTFLOWSTATIC;
1027  // BCtype[X1DN]=HORIZONOUTFLOWSTATIC;
1028 
1029  // FIXEDUSEPANALYTIC gives solution just like koral where no boundary effects, but a bit odd to generally fix values for incoming flow.
1032 
1033  BCtype[X2UP]=PERIODIC;
1034  BCtype[X2DN]=PERIODIC;
1035  BCtype[X3UP]=PERIODIC;
1036  BCtype[X3DN]=PERIODIC;
1037 
1038 
1039  int idt;
1040  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=1E2;
1041 
1042  DTr = 100; //number of time steps for restart dumps
1043  tf = 1E7; //final time
1044 
1045  // FLUXDISSIPATION=0.0;
1046 
1047  // DODIAGEVERYSUBSTEP = 1;
1048 
1049  }
1050 
1051 
1052  /*************************************************/
1053  /*************************************************/
1054  /*************************************************/
1055 
1056  if(WHICHPROBLEM==RADATM){
1057 
1058  //lim[1]=lim[2]=lim[3]=MINM; // MINM gets larger error and jump in v1 at outer edge
1059  // lim[1]=lim[2]=lim[3]=PARALINE;
1060  // Koral uses MINMOD_THETA2 (MC?)
1061  // koral paper uses MP5
1062  // lim[1]=lim[2]=lim[3]=MC;
1063  // cour=0.5;
1064 
1065 
1066  if(!(ISSPCMCOORDNATIVE(MCOORD))){
1067  dualfprintf(fail_file,"Must choose MCOORD (currently %d) to be spherical polar grid type for RADATM\n",MCOORD);
1068  myexit(3434628753);
1069  }
1070 
1071  a=0.0; // no spin in case use MCOORD=KSCOORDS
1072  gam=gamideal=1.4;
1073  cooling=KORAL;
1074  // ARAD_CODE=0.0;
1075 
1076  // BCtype[X1UP]=RADATMBEAMINFLOW;
1077  // BCtype[X1DN]=RADATMBEAMINFLOW;
1078  // really same as above, just simpler to avoid mistakes and can focus on init.c
1081 
1082  // BCtype[X1UP]=HORIZONOUTFLOW;
1083  // BCtype[X1DN]=HORIZONOUTFLOW;
1084  //BCtype[X1DN]=OUTFLOW;
1085 
1086  BCtype[X2UP]=PERIODIC;
1087  BCtype[X2DN]=PERIODIC;
1088  BCtype[X3UP]=PERIODIC;
1089  BCtype[X3DN]=PERIODIC;
1090 
1091 
1092  int idt;
1093  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=1E6;
1094  //for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=1E5; // DEBUG
1095 
1096  DTr = 100; //number of time steps for restart dumps
1097  tf = 2E9; //final time
1098 
1099  // tf=1E8; // profiling // SUPERTODOMARK
1100 
1101  // DODIAGEVERYSUBSTEP = 1;
1102 
1103  }
1104 
1105  /*************************************************/
1106  /*************************************************/
1107  /*************************************************/
1108 
1109  if(WHICHPROBLEM==RADWALL){
1110 
1111  // lim[1]=lim[2]=lim[3]=MINM; // Messy with PARALINE
1112  // cour=0.5;
1113 
1114 
1115  gam=gamideal=5.0/3.0;
1116  cooling=KORAL;
1117 
1118  BCtype[X1UP]=OUTFLOW;
1121  BCtype[X2DN]=ASYMM;
1122  BCtype[X3UP]=PERIODIC;
1123  BCtype[X3DN]=PERIODIC;
1124 
1125  int idt;
1126  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.5;
1127 
1128  DTr = 100; //number of time steps for restart dumps
1129  tf = 50.0; //final time
1130  }
1131 
1132  /*************************************************/
1133  /*************************************************/
1134  /*************************************************/
1135 
1136  if(WHICHPROBLEM==RADWAVE){
1137 
1138  // lim[1]=lim[2]=lim[3]=MINM; // generates glitch at extrema in prad0
1139  //lim[1]=lim[2]=lim[3]=MC; // less of a glitch near extrema in prad0
1140  // cour=0.5;
1141 
1142 
1143  cooling=KORAL;
1144  gam=gamideal=5./3.;
1145 
1146 
1147  // KORALTODO: See Jiang, Stone, Davis (2012) for S6.1.2 for linear MHD-radiation compressible wave tests
1148 
1149  RADWAVE_NWAVE=5; // 1,2,3,4,5 . And for 5 can choose NUMERO=41,11,1
1150  // RADWAVE_NWAVE=1; // GOOD
1151  // RADWAVE_NWAVE=2; // GOOD
1152  // RADWAVE_NWAVE=3; // GOOD
1153  // RADWAVE_NWAVE=4; // gets noisy in prad1 by t~30 with MINM or MC -- check koral when Olek makes it work. KORALTODO
1154  // RADWAVE_NUMERO=11; // GOOD
1155  RADWAVE_NUMERO=104;
1156  //RADWAVE_NUMERO=41; // OK if don't use check if can do explicit. So use this to show how should more generally improve the tau based suppression check! But, DAMPS significantly! Smaller IMPCONV doesn't help. Check with koral KORALTODO. MC doesn't help/change much.
1157  //RADWAVE_NUMERO=1; // wierd jello oscillations in prad0, and no wave motion -- like in koral though. KORALTODO. With only implicit, jello is different (smaller IMPCONV doesn't help and larger IMPEPS doesn't help).
1158 
1159  // NUMERO=41 corresponds to Jiang et al. (2002) PP=100, sigma=10 (2nd row, 2nd column in Table B1) 11 to PP=0.01, sigma=0.01 (1st row, 1st column).
1160 
1161  //NUMERO 1 was supposed to be his original test (1st row, 1st column) but, as you mention, it turned out to be jelly. The reason is that the initial conditions from the table were not precise enough to hit the acoustic mode and much faster radiation mode quickly dominates causing the jelly behavior. I had difficult time with that and decided to derive the numbers by myself (PROBLEMS/RADWAVE/disp107.nb). They were a bit different and made the difference so that one sees only the acoustic mode. The reason is most likely the fact that my dispersion relation and the code are somewhat relativistic.
1162 
1163 
1164 
1165 
1166  // defaults
1168 
1169 
1170  /* Pre-SASHA
1171  if(RADWAVE_NWAVE==5){ //sound wave with radiation set up according to Jiang+12
1172 
1173  if(RADWAVE_NUMERO==41){
1174  RADWAVE_PP=100.;
1175  RADWAVE_CC=1.e2;
1176  RADWAVE_KAPPA=10.;
1177  RADWAVE_RHOFAC=0.01;
1178  RADWAVE_DRRE=(1.e-3*RADWAVE_RHOFAC);
1179  RADWAVE_DRIM=0.;
1180  RADWAVE_DVRE=(4.06372e-6*RADWAVE_RHOFAC);
1181  RADWAVE_DVIM=(6.90937e-6*RADWAVE_RHOFAC);
1182  RADWAVE_DURE=(9.88671e-4*RADWAVE_RHOFAC);
1183  RADWAVE_DUIM=(6.97077e-6*RADWAVE_RHOFAC);
1184  RADWAVE_DERE=(-4.52724e-5*RADWAVE_RHOFAC);
1185  RADWAVE_DEIM=(2.78566e-5*RADWAVE_RHOFAC);
1186  RADWAVE_DFRE=(-5.83678e-6*RADWAVE_RHOFAC);
1187  RADWAVE_DFIM=(-9.48194e-6*RADWAVE_RHOFAC);
1188  RADWAVE_OMRE=0.0255331;
1189  RADWAVE_OMIM=0.0434128;
1190  RADWAVE_DTOUT1=1.e-0;
1191  }
1192 
1193  if(RADWAVE_NUMERO==11){
1194  RADWAVE_PP=0.01;
1195  RADWAVE_CC=1.e2;
1196  RADWAVE_KAPPA=0.01;
1197  RADWAVE_RHOFAC=0.01;
1198  RADWAVE_DRRE=(1.e-3*RADWAVE_RHOFAC);
1199  RADWAVE_DRIM=0.;
1200  RADWAVE_DVRE=(9.99998e-6*RADWAVE_RHOFAC);
1201  RADWAVE_DVIM=(8.48878e-9*RADWAVE_RHOFAC);
1202  RADWAVE_DURE=(1.66666e-3*RADWAVE_RHOFAC);
1203  RADWAVE_DUIM=(2.82938e-6*RADWAVE_RHOFAC);
1204  RADWAVE_DERE=(1.95853e-8*RADWAVE_RHOFAC);
1205  RADWAVE_DEIM=(1.91123e-7*RADWAVE_RHOFAC);
1206  RADWAVE_DFRE=(-1.33508e-5*RADWAVE_RHOFAC);
1207  RADWAVE_DFIM=(4.23463e-6*RADWAVE_RHOFAC);
1208  RADWAVE_OMRE=6.28317e-2;
1209  RADWAVE_OMIM=5.33366e-5;
1210  RADWAVE_DTOUT1=1.e-0;
1211  }
1212 
1213 
1214  if(RADWAVE_NUMERO==1){
1215  RADWAVE_PP=0.01;
1216  RADWAVE_CC=1.e4;
1217  RADWAVE_KAPPA=0.01;
1218  RADWAVE_DRRE=1.e-3;
1219  RADWAVE_DRIM=0.;
1220  RADWAVE_DVRE=9.7548e-8;
1221  RADWAVE_DVIM=7.92788e-9;
1222  //RADWAVE_DPRE=1.61075e-3;
1223  //RADWAVE_DPIM=2.07402e-4;
1224  RADWAVE_DURE=1.57546e-3;
1225  RADWAVE_DUIM=2.57783e-4;
1226  RADWAVE_DERE=1.6874e-8; // 1.79137e-8
1227  RADWAVE_DEIM=9.48966e-9; // 8.56498e-9
1228  RADWAVE_DFRE=-1.77115e-6; // -1.32035e-6
1229  RADWAVE_DFIM=3.65291e-6; // 3.88814e-6
1230  RADWAVE_OMRE=6.12912e-4; // 7.99077
1231  RADWAVE_OMIM=4.98123e-5; // 0.512336
1232  RADWAVE_DTOUT1=1.e-2;
1233  }
1234 
1235  RADWAVE_RHOZERO=1.;
1236  RADWAVE_KK=2.*Pi;
1237  RADWAVE_UINT=((1./RADWAVE_CC/RADWAVE_CC)*RADWAVE_RHOZERO/gam/(gam-1.-1./RADWAVE_CC/RADWAVE_CC)) ; // to get proper sound speed
1238  RADWAVE_TEMP=(calc_PEQ_Tfromurho(RADWAVE_UINT,RADWAVE_RHOZERO)) ; // temperature from rho and uint
1239  ARAD_CODE=((3.0*RADWAVE_PP*(gam-1.)*RADWAVE_UINT/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP)); //to get the proper radiation to gas pressure ratio, PP=4 sig T^4 / P
1240  RADWAVE_ERAD=(calc_LTE_EfromT(RADWAVE_TEMP)) ; // to get thermal equilibrium, E=4 sig T^4
1241  }
1242 
1243 
1244  if(RADWAVE_NWAVE==1){ //density wave advected with the gas
1245  // NO RADIATION
1246  RADWAVE_PP=0.1;
1247  RADWAVE_CC=1.e6;
1248  RADWAVE_VX=1.e-3;
1249  RADWAVE_DTOUT1=(.05/RADWAVE_VX);
1250  RADWAVE_RHOZERO=1.;
1251  RADWAVE_AAA=1.e-5;
1252  RADWAVE_ERAD=1.;
1253  RADWAVE_KK=2.*Pi;
1254  RADWAVE_UINT=(1./RADWAVE_CC/RADWAVE_CC)*RADWAVE_RHOZERO/gam/(gam-1.-1./RADWAVE_CC/RADWAVE_CC);
1255  RADWAVE_TEMP=calc_PEQ_Tfromurho(RADWAVE_UINT,RADWAVE_RHOZERO);
1256  ARAD_CODE=(3.0*RADWAVE_PP*(gam-1.)*RADWAVE_UINT/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP);
1257  }
1258 
1259  if(RADWAVE_NWAVE==2){ //hydro sound wave
1260  // NO RADIATION
1261  RADWAVE_PP=0.01;
1262  RADWAVE_CC=1.e6;
1263  RADWAVE_DTOUT1=(.05*RADWAVE_CC);
1264  RADWAVE_VX=0.;
1265  RADWAVE_RHOZERO=1.;
1266  RADWAVE_AAA=1.e-5;
1267  RADWAVE_ERAD=1.;
1268  RADWAVE_KK=2.*Pi;
1269  RADWAVE_UINT=(1./RADWAVE_CC/RADWAVE_CC)*RADWAVE_RHOZERO/gam/(gam-1.-1./RADWAVE_CC/RADWAVE_CC);
1270  RADWAVE_TEMP=calc_PEQ_Tfromurho(RADWAVE_UINT,RADWAVE_RHOZERO);
1271  ARAD_CODE=(3.0*RADWAVE_PP*(gam-1.)*RADWAVE_UINT/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP);
1272  }
1273 
1274  if(RADWAVE_NWAVE==3){ //radiative density wave advected with the gas
1275  // FLUXDISSIPATION=(0.0);
1276  RADWAVE_PP=10.;
1277  RADWAVE_CC=1.e6;
1278  RADWAVE_VX=1.e-2;
1279  RADWAVE_DTOUT1=(.0005/RADWAVE_VX);
1280  RADWAVE_RHOZERO=1.;
1281  RADWAVE_AAA=1.e-5;
1282  RADWAVE_KK=2.*Pi;
1283  RADWAVE_UINT=(1./RADWAVE_CC/RADWAVE_CC)*RADWAVE_RHOZERO/gam/(gam-1.-1./RADWAVE_CC/RADWAVE_CC);
1284  RADWAVE_TEMP=calc_PEQ_Tfromurho(RADWAVE_UINT,RADWAVE_RHOZERO);
1285  ARAD_CODE=(3.0*RADWAVE_PP*(gam-1.)*RADWAVE_UINT/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP);
1286  RADWAVE_ERAD=calc_LTE_EfromT(RADWAVE_TEMP);
1287  RADWAVE_KAPPAES=10.;
1288  }
1289 
1290 
1291  if(RADWAVE_NWAVE==4){ //sound wave with radiation, set up without the phase shifts etc.
1292  // FLUXDISSIPATION=(0.0);
1293  RADWAVE_PP=1.;
1294  RADWAVE_CC=1.e2;
1295  RADWAVE_DTOUT1=(.005*RADWAVE_CC);
1296  RADWAVE_VX=0.;
1297  RADWAVE_RHOZERO=1.;
1298  RADWAVE_AAA=1.e-1;
1299  RADWAVE_KK=2.*Pi;
1300  RADWAVE_UINT=(1./RADWAVE_CC/RADWAVE_CC)*RADWAVE_RHOZERO/gam/(gam-1.-1./RADWAVE_CC/RADWAVE_CC);
1301  RADWAVE_TEMP=calc_PEQ_Tfromurho(RADWAVE_UINT,RADWAVE_RHOZERO);
1302  ARAD_CODE=(3.0*RADWAVE_PP*(gam-1.)*RADWAVE_UINT/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP);
1303  RADWAVE_ERAD=calc_LTE_EfromT(RADWAVE_TEMP);
1304  RADWAVE_KAPPA=100.;
1305  RADWAVE_ERADFACTOR=.5;
1306  RADWAVE_GASFACTOR=.5;
1307  }
1308  */ // end pre-SASHA
1309 
1310  // start post-SASHA
1311 
1312  if(RADWAVE_NWAVE==5){ //sound wave with radiation set up according to Jiang+12
1313 
1314  if(RADWAVE_NUMERO==1) { // sound wave
1315  RADWAVE_WAVETYPE=0; //sound wave
1316  RADWAVE_RHOFAC=0.001;
1317  RADWAVE_B0=0.;
1318  RADWAVE_PP=0.01;
1319  RADWAVE_CC=10;
1320  RADWAVE_KAPPA=0;
1323  RADWAVE_DVRE=0.00010000000000000002*RADWAVE_RHOFAC;
1327  RADWAVE_DURE=0.000015228426395939093*RADWAVE_RHOFAC;
1333  RADWAVE_DFRE=-2.4365482233502554e-8*RADWAVE_RHOFAC;
1337  RADWAVE_OMRE=0.6283185307179587;
1338  RADWAVE_OMIM=0;
1339  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1340  }
1341 
1342  if(RADWAVE_NUMERO==10) { // fast magnetosonic wave
1343  RADWAVE_WAVETYPE=2; //fast
1344  RADWAVE_RHOFAC=0.001;
1345  RADWAVE_B0=0.10075854437197568;
1346  RADWAVE_PP=0.01;
1347  RADWAVE_CC=10;
1348  RADWAVE_KAPPA=0;
1351  RADWAVE_DVRE=0.0001602940583015828*RADWAVE_RHOFAC;
1353  RADWAVE_DV2RE=-0.00009790871410382318*RADWAVE_RHOFAC;
1355  RADWAVE_DURE=0.00001522842639593907*RADWAVE_RHOFAC;
1357  RADWAVE_DB2RE=0.00016230255678865884*RADWAVE_RHOFAC;
1361  RADWAVE_DFRE=-3.905642029683238e-8*RADWAVE_RHOFAC;
1363  RADWAVE_DF2RE=2.3855930340017838e-8*RADWAVE_RHOFAC;
1365  RADWAVE_OMRE=1.007157271948693;
1366  RADWAVE_OMIM=0;
1367  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1368  }
1369 
1370  if(RADWAVE_NUMERO==11) { // slow magnetosonic wave
1371  RADWAVE_WAVETYPE=1; //slow
1372  RADWAVE_RHOFAC=0.001;
1373  RADWAVE_B0=0.10075854437197568;
1374  RADWAVE_PP=0.01;
1375  RADWAVE_CC=10;
1376  RADWAVE_KAPPA=0;
1379  RADWAVE_DVRE=0.00006177069527516586*RADWAVE_RHOFAC;
1381  RADWAVE_DV2RE=0.00010011836806552759*RADWAVE_RHOFAC;
1383  RADWAVE_DURE=0.00001522842639593909*RADWAVE_RHOFAC;
1385  RADWAVE_DB2RE=-0.0000625515978604029*RADWAVE_RHOFAC;
1387  RADWAVE_DERE=-4.0433761094240253e-25*RADWAVE_RHOFAC;
1389  RADWAVE_DFRE=-1.5050727782781537e-8*RADWAVE_RHOFAC;
1391  RADWAVE_DF2RE=-2.4394323183478818e-8*RADWAVE_RHOFAC;
1393  RADWAVE_OMRE=0.3881167249671897;
1394  RADWAVE_OMIM=0;
1395  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1396  }
1397 
1398  if(RADWAVE_NUMERO==101) { // radiation-modified sound wave
1399  RADWAVE_WAVETYPE=0; //sound
1400  RADWAVE_RHOFAC=0.001;
1401  RADWAVE_B0=0.;
1402  RADWAVE_PP=0.1;
1403  RADWAVE_CC=10;
1404  RADWAVE_KAPPA=0.1;
1407  RADWAVE_DVRE=0.0000997992249118626*RADWAVE_RHOFAC;
1408  RADWAVE_DVIM=2.552072175928721e-6*RADWAVE_RHOFAC;
1411  RADWAVE_DURE=0.000015155652908079845*RADWAVE_RHOFAC;
1412  RADWAVE_DUIM=7.696929719530536e-7*RADWAVE_RHOFAC;
1415  RADWAVE_DERE=1.3314776991134588e-10*RADWAVE_RHOFAC;
1416  RADWAVE_DEIM=3.6001746512388956e-8*RADWAVE_RHOFAC;
1417  RADWAVE_DFRE=-2.5247126486226934e-7*RADWAVE_RHOFAC;
1418  RADWAVE_DFIM=7.400407810152034e-8*RADWAVE_RHOFAC;
1421  RADWAVE_OMRE=0.627057023634126;
1422  RADWAVE_OMIM=0.016035142398657175;
1423  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1424  }
1425 
1426  if(RADWAVE_NUMERO==102) { // radiation-modified sound wave
1427  RADWAVE_WAVETYPE=0; //sound
1428  RADWAVE_RHOFAC=0.001;
1429  RADWAVE_B0=0.;
1430  RADWAVE_PP=10;
1431  RADWAVE_CC=10;
1432  RADWAVE_KAPPA=10;
1435  RADWAVE_DVRE=0.0002662507979198814*RADWAVE_RHOFAC;
1436  RADWAVE_DVIM=0.0000633514446524509*RADWAVE_RHOFAC;
1439  RADWAVE_DURE=0.000011706978034894262*RADWAVE_RHOFAC;
1440  RADWAVE_DUIM=1.881532710186292e-6*RADWAVE_RHOFAC;
1443  RADWAVE_DERE=0.00020541918444857084*RADWAVE_RHOFAC;
1444  RADWAVE_DEIM=0.00014985861843019722*RADWAVE_RHOFAC;
1445  RADWAVE_DFRE=-0.000020730815318727886*RADWAVE_RHOFAC;
1446  RADWAVE_DFIM=0.000037755564579364684*RADWAVE_RHOFAC;
1449  RADWAVE_OMRE=1.67290310151504;
1450  RADWAVE_OMIM=0.39804886622888025;
1451  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1452  }
1453 
1454  if(RADWAVE_NUMERO==103) { // radiation-modified sound wave
1455  RADWAVE_WAVETYPE=0;
1456  RADWAVE_RHOFAC=0.001;
1457  RADWAVE_B0=0.;
1458  RADWAVE_PP=0.1;
1459  RADWAVE_CC=10;
1460  RADWAVE_KAPPA=10;
1463  RADWAVE_DVRE=0.00009290985655754982*RADWAVE_RHOFAC;
1464  RADWAVE_DVIM=0.000014438241113392203*RADWAVE_RHOFAC;
1467  RADWAVE_DURE=0.00001179766900341804*RADWAVE_RHOFAC;
1468  RADWAVE_DUIM=3.0429210480592464e-6*RADWAVE_RHOFAC;
1471  RADWAVE_DERE=1.9819771721015766e-6*RADWAVE_RHOFAC;
1472  RADWAVE_DEIM=2.206454751998895e-6*RADWAVE_RHOFAC;
1473  RADWAVE_DFRE=-4.3677706188561827e-7*RADWAVE_RHOFAC;
1474  RADWAVE_DFIM=4.316214437243016e-7*RADWAVE_RHOFAC;
1477  RADWAVE_OMRE=0.5837698456145599;
1478  RADWAVE_OMIM=0.09071814442518213;
1479  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1480  }
1481 
1482  if(RADWAVE_NUMERO==104) { // radiation-modified sound wave
1483  RADWAVE_WAVETYPE=0;
1484  RADWAVE_RHOFAC=0.001;
1485  RADWAVE_B0=0.;
1486  RADWAVE_PP=10;
1487  RADWAVE_CC=10;
1488  RADWAVE_KAPPA=0.1;
1491  RADWAVE_DVRE=0.00007748046325109175*RADWAVE_RHOFAC;
1492  RADWAVE_DVIM=3.583193353788357e-6*RADWAVE_RHOFAC;
1495  RADWAVE_DURE=9.141343124149056e-6*RADWAVE_RHOFAC;
1496  RADWAVE_DUIM=3.83221342644378e-7*RADWAVE_RHOFAC;
1499  RADWAVE_DERE=-1.5219307455357163e-7*RADWAVE_RHOFAC;
1500  RADWAVE_DEIM=9.380406060306363e-7*RADWAVE_RHOFAC;
1501  RADWAVE_DFRE=-0.000019366644866579624*RADWAVE_RHOFAC;
1502  RADWAVE_DFIM=-7.930468856866346e-7*RADWAVE_RHOFAC;
1505  RADWAVE_OMRE=0.4868241082927276;
1506  RADWAVE_OMIM=0.02251386783330655;
1507  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1508  }
1509 
1510  if(RADWAVE_NUMERO==1001){ //radiation-modified fast magnetosonic
1511  RADWAVE_WAVETYPE=2; //fast
1512  RADWAVE_RHOFAC=0.001;
1513  RADWAVE_B0=0.10075854437197568;
1514  RADWAVE_PP=0.1;
1515  RADWAVE_CC=10;
1516  RADWAVE_KAPPA=0.1;
1519  RADWAVE_DVRE=0.00016025131429328265*RADWAVE_RHOFAC;
1520  RADWAVE_DVIM=7.238312005077197e-7*RADWAVE_RHOFAC;
1521  RADWAVE_DV2RE=-0.00009795442630848571*RADWAVE_RHOFAC;
1522  RADWAVE_DV2IM=9.836789501779977e-7*RADWAVE_RHOFAC;
1523  RADWAVE_DURE=0.000015198360895974991*RADWAVE_RHOFAC;
1524  RADWAVE_DUIM=4.815752909936621e-7*RADWAVE_RHOFAC;
1525  RADWAVE_DB2RE=0.00016234366410161697*RADWAVE_RHOFAC;
1526  RADWAVE_DB2IM=-8.96662164240542e-7*RADWAVE_RHOFAC;
1527  RADWAVE_DERE=1.4842118188293356e-9*RADWAVE_RHOFAC;
1528  RADWAVE_DEIM=6.063223162955078e-8*RADWAVE_RHOFAC;
1529  RADWAVE_DFRE=-3.9543271084234507e-7*RADWAVE_RHOFAC;
1530  RADWAVE_DFIM=8.51051304663626e-8*RADWAVE_RHOFAC;
1531  RADWAVE_DF2RE=2.3667952154599258e-7*RADWAVE_RHOFAC;
1532  RADWAVE_DF2IM=2.1118238693659835e-8*RADWAVE_RHOFAC;
1533  RADWAVE_OMRE=1.0068887034237715;
1534  RADWAVE_OMIM=0.004547965563908265;
1535  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1536  }
1537 
1538  if(RADWAVE_NUMERO==1101){ //radiation-modified slow magnetosonic
1539  RADWAVE_WAVETYPE=1; //slow
1540  RADWAVE_RHOFAC=0.001;
1541  RADWAVE_B0=0.10075854437197568;
1542  RADWAVE_PP=0.1;
1543  RADWAVE_CC=10;
1544  RADWAVE_KAPPA=0.1;
1547  RADWAVE_DVRE=0.0000615332754996702*RADWAVE_RHOFAC;
1548  RADWAVE_DVIM=1.8313980164851912e-6*RADWAVE_RHOFAC;
1549  RADWAVE_DV2RE=0.00009897721183016215*RADWAVE_RHOFAC;
1550  RADWAVE_DV2IM=6.541857910619384e-6*RADWAVE_RHOFAC;
1551  RADWAVE_DURE=0.000015017423510649482*RADWAVE_RHOFAC;
1552  RADWAVE_DUIM=1.2229894345580098e-6*RADWAVE_RHOFAC;
1553  RADWAVE_DB2RE=-0.00006148820918323782*RADWAVE_RHOFAC;
1554  RADWAVE_DB2IM=-5.883153382953974e-6*RADWAVE_RHOFAC;
1555  RADWAVE_DERE=1.9170283012363001e-10*RADWAVE_RHOFAC;
1556  RADWAVE_DEIM=2.187214582100525e-8*RADWAVE_RHOFAC;
1557  RADWAVE_DFRE=-1.6518053269343755e-7*RADWAVE_RHOFAC;
1558  RADWAVE_DFIM=7.175204181969396e-8*RADWAVE_RHOFAC;
1559  RADWAVE_DF2RE=-2.2367888827266106e-7*RADWAVE_RHOFAC;
1560  RADWAVE_DF2IM=-7.43141463935117e-8*RADWAVE_RHOFAC;
1561  RADWAVE_OMRE=0.38662497252216144;
1562  RADWAVE_OMIM=0.011507013108777591;
1563  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1564  }
1565 
1566  if(RADWAVE_NUMERO==1002){ //radiation-modified fast magnetosonic, opt THICK
1567  RADWAVE_WAVETYPE=2; //fast
1568  RADWAVE_RHOFAC=0.001;
1569  RADWAVE_B0=0.10075854437197568;
1570  RADWAVE_PP=10;
1571  RADWAVE_CC=10;
1572  RADWAVE_KAPPA=10;
1575  RADWAVE_DVRE=0.0002784991109850316*RADWAVE_RHOFAC;
1576  RADWAVE_DVIM=0.000052380393168228656*RADWAVE_RHOFAC;
1577  RADWAVE_DV2RE=-0.000028109315354609682*RADWAVE_RHOFAC;
1578  RADWAVE_DV2IM=6.2558750019767086e-6*RADWAVE_RHOFAC;
1579  RADWAVE_DURE=0.000011730539980454472*RADWAVE_RHOFAC;
1580  RADWAVE_DUIM=1.7129010401392157e-6*RADWAVE_RHOFAC;
1581  RADWAVE_DB2RE=0.00011016964855189374*RADWAVE_RHOFAC;
1582  RADWAVE_DB2IM=-4.033370850235303e-6*RADWAVE_RHOFAC;
1583  RADWAVE_DERE=0.0002072941184085973*RADWAVE_RHOFAC;
1584  RADWAVE_DEIM=0.00013636362726103654*RADWAVE_RHOFAC;
1585  RADWAVE_DFRE=-0.00001833308481505651*RADWAVE_RHOFAC;
1586  RADWAVE_DFIM=0.00003636638174654657*RADWAVE_RHOFAC;
1587  RADWAVE_DF2RE=2.6758144678721757e-7*RADWAVE_RHOFAC;
1588  RADWAVE_DF2IM=1.2427179588260004e-6*RADWAVE_RHOFAC;
1589  RADWAVE_OMRE=1.7498615222037273;
1590  RADWAVE_OMIM=0.3291157167389043;
1591  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1592  }
1593 
1594  if(RADWAVE_NUMERO==1003){ //radiation-modified fast magnetosonic, opt THICK
1595  RADWAVE_WAVETYPE=2;
1596  RADWAVE_RHOFAC=0.001;
1597  RADWAVE_B0=0.10075854437197568;
1598  RADWAVE_PP=0.1;
1599  RADWAVE_CC=10;
1600  RADWAVE_KAPPA=10;
1603  RADWAVE_DVRE=0.00015921491906000263*RADWAVE_RHOFAC;
1604  RADWAVE_DVIM=4.267309467171878e-6*RADWAVE_RHOFAC;
1605  RADWAVE_DV2RE=-0.00009865659642379489*RADWAVE_RHOFAC;
1606  RADWAVE_DV2IM=6.116424112313706e-6*RADWAVE_RHOFAC;
1607  RADWAVE_DURE=0.000013105512199532442*RADWAVE_RHOFAC;
1608  RADWAVE_DUIM=2.2690789485828284e-6*RADWAVE_RHOFAC;
1609  RADWAVE_DB2RE=0.00016304450066324336*RADWAVE_RHOFAC;
1610  RADWAVE_DB2IM=-5.540155699476499e-6*RADWAVE_RHOFAC;
1611  RADWAVE_DERE=2.9534637071306857e-6*RADWAVE_RHOFAC;
1612  RADWAVE_DEIM=1.5968078176265759e-6*RADWAVE_RHOFAC;
1613  RADWAVE_DFRE=-2.7219589023767955e-7*RADWAVE_RHOFAC;
1614  RADWAVE_DFIM=6.086535550665933e-7*RADWAVE_RHOFAC;
1615  RADWAVE_DF2RE=3.2386284213141576e-9*RADWAVE_RHOFAC;
1616  RADWAVE_DF2IM=2.3827073246896776e-8*RADWAVE_RHOFAC;
1617  RADWAVE_OMRE=1.0003768401215956;
1618  RADWAVE_OMIM=0.02681229614532269;
1619  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1620  }
1621 
1622  if(RADWAVE_NUMERO==1004){ //radiation-modified fast magnetosonic, opt thin
1623  RADWAVE_WAVETYPE=2;
1624  RADWAVE_RHOFAC=0.001;
1625  RADWAVE_B0=0.10075854437197568;
1626  RADWAVE_PP=10;
1627  RADWAVE_CC=10;
1628  RADWAVE_KAPPA=0.1;
1631  RADWAVE_DVRE=0.00015164754212433659*RADWAVE_RHOFAC;
1632  RADWAVE_DVIM=2.9243074320866887e-6*RADWAVE_RHOFAC;
1633  RADWAVE_DV2RE=-0.00011147156590526742*RADWAVE_RHOFAC;
1634  RADWAVE_DV2IM=6.596175366942675e-7*RADWAVE_RHOFAC;
1635  RADWAVE_DURE=9.205934027867234e-6*RADWAVE_RHOFAC;
1636  RADWAVE_DUIM=7.437886816058989e-7*RADWAVE_RHOFAC;
1637  RADWAVE_DB2RE=0.00017478715294162717*RADWAVE_RHOFAC;
1638  RADWAVE_DB2IM=-1.8658034881621974e-6*RADWAVE_RHOFAC;
1639  RADWAVE_DERE=-4.7021288964385735e-7*RADWAVE_RHOFAC;
1640  RADWAVE_DEIM=1.9823405974044627e-6*RADWAVE_RHOFAC;
1641  RADWAVE_DFRE=-0.00003794222976803031*RADWAVE_RHOFAC;
1642  RADWAVE_DFIM=-3.18097469382448e-7*RADWAVE_RHOFAC;
1643  RADWAVE_DF2RE=0.00002693491298739615*RADWAVE_RHOFAC;
1644  RADWAVE_DF2IM=2.670466935767298e-6*RADWAVE_RHOFAC;
1645  RADWAVE_OMRE=0.952829608545529;
1646  RADWAVE_OMIM=0.018373965490963148;
1647  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1648  }
1649 
1650  if(RADWAVE_NUMERO==1102){ //radiation-modified slow magnetosonic, opt THICK
1651  RADWAVE_WAVETYPE=1; //slow
1652  RADWAVE_RHOFAC=0.001;
1653  RADWAVE_B0=0.10075854437197568;
1654  RADWAVE_PP=10;
1655  RADWAVE_CC=10;
1656  RADWAVE_KAPPA=10;
1659  RADWAVE_DVRE=0.00008342687348874559*RADWAVE_RHOFAC;
1660  RADWAVE_DVIM=0.000012082877629545738*RADWAVE_RHOFAC;
1661  RADWAVE_DV2RE=0.00011363325579507992*RADWAVE_RHOFAC;
1662  RADWAVE_DV2IM=0.00027269723955801375*RADWAVE_RHOFAC;
1663  RADWAVE_DURE=9.461886706340277e-6*RADWAVE_RHOFAC;
1664  RADWAVE_DUIM=1.2137574760252086e-6*RADWAVE_RHOFAC;
1665  RADWAVE_DB2RE=-0.0000803822945939874*RADWAVE_RHOFAC;
1666  RADWAVE_DB2IM=-0.00030311425160368743*RADWAVE_RHOFAC;
1667  RADWAVE_DERE=0.000025966624942266354*RADWAVE_RHOFAC;
1668  RADWAVE_DEIM=0.00009678910913258438*RADWAVE_RHOFAC;
1669  RADWAVE_DFRE=-0.000019826286725607242*RADWAVE_RHOFAC;
1670  RADWAVE_DFIM=5.476096510182763e-6*RADWAVE_RHOFAC;
1671  RADWAVE_DF2RE=3.6607454416002e-6*RADWAVE_RHOFAC;
1672  RADWAVE_DF2IM=-1.1474975240229893e-6*RADWAVE_RHOFAC;
1673  RADWAVE_OMRE=0.5241865057284164;
1674  RADWAVE_OMIM=0.0759189591904107;
1675  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1676  }
1677 
1678  if(RADWAVE_NUMERO==1103){ //radiation-modified slow magnetosonic, opt THICK
1679  RADWAVE_WAVETYPE=1;
1680  RADWAVE_RHOFAC=0.001;
1681  RADWAVE_B0=0.10075854437197568;
1682  RADWAVE_PP=0.1;
1683  RADWAVE_CC=10;
1684  RADWAVE_KAPPA=10;
1687  RADWAVE_DVRE=0.000055107074739986735*RADWAVE_RHOFAC;
1688  RADWAVE_DVIM=6.81771795916397e-6*RADWAVE_RHOFAC;
1689  RADWAVE_DV2RE=0.00007683477442788993*RADWAVE_RHOFAC;
1690  RADWAVE_DV2IM=0.000018069585727045885*RADWAVE_RHOFAC;
1691  RADWAVE_DURE=0.000010353641210907202*RADWAVE_RHOFAC;
1692  RADWAVE_DUIM=2.5489931913014527e-6*RADWAVE_RHOFAC;
1693  RADWAVE_DB2RE=-0.00004163521053362644*RADWAVE_RHOFAC;
1694  RADWAVE_DB2IM=-0.00001542206148990843*RADWAVE_RHOFAC;
1695  RADWAVE_DERE=9.058920389631598e-7*RADWAVE_RHOFAC;
1696  RADWAVE_DEIM=1.8594869909418343e-6*RADWAVE_RHOFAC;
1697  RADWAVE_DFRE=-3.830409107982378e-7*RADWAVE_RHOFAC;
1698  RADWAVE_DFIM=1.992679542320905e-7*RADWAVE_RHOFAC;
1699  RADWAVE_DF2RE=2.114058251324126e-9*RADWAVE_RHOFAC;
1700  RADWAVE_DF2IM=-6.394153931776251e-9*RADWAVE_RHOFAC;
1701  RADWAVE_OMRE=0.346247962327932;
1702  RADWAVE_OMIM=0.04283698530951345;
1703  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1704  }
1705 
1706  if(RADWAVE_NUMERO==1104){ //radiation-modified slow magnetosonic, opt thin
1707  RADWAVE_WAVETYPE=1;
1708  RADWAVE_RHOFAC=0.001;
1709  RADWAVE_B0=0.10075854437197568;
1710  RADWAVE_PP=10;
1711  RADWAVE_CC=10;
1712  RADWAVE_KAPPA=0.1;
1715  RADWAVE_DVRE=0.00005019053133722909*RADWAVE_RHOFAC;
1716  RADWAVE_DVIM=2.3866183515916313e-6*RADWAVE_RHOFAC;
1717  RADWAVE_DV2RE=0.00006765290591654829*RADWAVE_RHOFAC;
1718  RADWAVE_DV2IM=3.826394449423638e-6*RADWAVE_RHOFAC;
1719  RADWAVE_DURE=9.134497469805529e-6*RADWAVE_RHOFAC;
1720  RADWAVE_DUIM=2.481944791269412e-7*RADWAVE_RHOFAC;
1721  RADWAVE_DB2RE=-0.00003511412717901583*RADWAVE_RHOFAC;
1722  RADWAVE_DB2IM=-1.2206629792761417e-6*RADWAVE_RHOFAC;
1723  RADWAVE_DERE=-7.354752234301807e-8*RADWAVE_RHOFAC;
1724  RADWAVE_DEIM=6.007447328384492e-7*RADWAVE_RHOFAC;
1725  RADWAVE_DFRE=-0.000012540740009734634*RADWAVE_RHOFAC;
1726  RADWAVE_DFIM=-5.536217727919607e-7*RADWAVE_RHOFAC;
1727  RADWAVE_DF2RE=-0.000014894816213683286*RADWAVE_RHOFAC;
1728  RADWAVE_DF2IM=-5.731053967780647e-6*RADWAVE_RHOFAC;
1729  RADWAVE_OMRE=0.3153564090576144;
1730  RADWAVE_OMIM=0.0149955653605657;
1731  RADWAVE_DTOUT1=2*M_PI/RADWAVE_OMRE/10.;
1732  }
1733  RADWAVE_RHOZERO=1.;
1734  RADWAVE_KK=2.*Pi;
1735  RADWAVE_UINT=((1./RADWAVE_CC/RADWAVE_CC)*RADWAVE_RHOZERO/gam/(gam-1.-1./RADWAVE_CC/RADWAVE_CC)) ; // to get proper sound speed
1736  RADWAVE_TEMP=(calc_PEQ_Tfromurho(RADWAVE_UINT,RADWAVE_RHOZERO)) ; // temperature from rho and uint
1737  ARAD_CODE=((3.*RADWAVE_PP*(gam-1.)*RADWAVE_UINT/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP)); //to get the proper radiation to gas pressure ratio, PP=4 sig T^4 / P
1738  RADWAVE_ERAD=(calc_LTE_EfromT(RADWAVE_TEMP)) ; // to get thermal equilibrium, E=4 sig T^4
1739 
1740  dualfprintf(fail_file,"RADWAVE_RHOZERO=%g, RADWAVE_KK=%g, RADWAVE_UINT=%g, RADWAVE_TEMP=%g, ARAD_CODE=%g, RADWAVE_ERAD=%21.15g\n",
1742  if(RADWAVE_NWAVE==5){
1743  FILE *out;
1744  if((out=fopen("radtestparams.dat","wt"))==NULL){
1745  dualfprintf(fail_file,"Couldn't write radtestparams.dat file\n");
1746  myexit(1);
1747  }
1748  else{
1749  fprintf(out,"#%20s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s %21s\n",
1750  "RADWAVE_RHOZERO",
1751  "RADWAVE_KK",
1752  "RADWAVE_UINT",
1753  "RADWAVE_ERAD",
1754  "RADWAVE_DRRE",
1755  "RADWAVE_RHOFAC",
1756  "RADWAVE_B0",
1757  "RADWAVE_PP",
1758  "RADWAVE_CC",
1759  "RADWAVE_KAPPA",
1760  "RADWAVE_DRRE",
1761  "RADWAVE_DRIM",
1762  "RADWAVE_DVRE",
1763  "RADWAVE_DVIM",
1764  "RADWAVE_DV2RE",
1765  "RADWAVE_DV2IM",
1766  "RADWAVE_DURE",
1767  "RADWAVE_DUIM",
1768  "RADWAVE_DB2RE",
1769  "RADWAVE_DB2IM",
1770  "RADWAVE_DERE",
1771  "RADWAVE_DEIM",
1772  "RADWAVE_DFRE",
1773  "RADWAVE_DFIM",
1774  "RADWAVE_DF2RE",
1775  "RADWAVE_DF2IM",
1776  "RADWAVE_OMRE",
1777  "RADWAVE_OMIM",
1778  "RADWAVE_DTOUT1",
1779  "RADWAVE_WAVETYPE");
1780  fprintf(out,"%21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g\n",
1782  RADWAVE_KK,
1783  RADWAVE_UINT,
1784  RADWAVE_ERAD,
1785  RADWAVE_DRRE,
1787  RADWAVE_B0,
1788  RADWAVE_PP,
1789  RADWAVE_CC,
1790  RADWAVE_KAPPA,
1791  RADWAVE_DRRE,
1792  RADWAVE_DRIM,
1793  RADWAVE_DVRE,
1794  RADWAVE_DVIM,
1795  RADWAVE_DV2RE,
1796  RADWAVE_DV2IM,
1797  RADWAVE_DURE,
1798  RADWAVE_DUIM,
1799  RADWAVE_DB2RE,
1800  RADWAVE_DB2IM,
1801  RADWAVE_DERE,
1802  RADWAVE_DEIM,
1803  RADWAVE_DFRE,
1804  RADWAVE_DFIM,
1805  RADWAVE_DF2RE,
1806  RADWAVE_DF2IM,
1807  RADWAVE_OMRE,
1808  RADWAVE_OMIM,
1810  (double)RADWAVE_WAVETYPE
1811  );
1812  fclose(out);
1813  }
1814  }
1815 
1816  }
1817 
1818 
1819  if(RADWAVE_NWAVE==1){ //density wave advected with the gas
1820  // NO RADIATION
1821  RADWAVE_PP=0.1;
1822  RADWAVE_CC=1.e6;
1823  RADWAVE_VX=1.e-3;
1824  RADWAVE_DTOUT1=(.05/RADWAVE_VX);
1825  RADWAVE_RHOZERO=1.;
1826  RADWAVE_AAA=1.e-5;
1827  RADWAVE_ERAD=1.;
1828  RADWAVE_KK=2.*Pi;
1832  }
1833 
1834  if(RADWAVE_NWAVE==2){ //hydro sound wave
1835  // NO RADIATION
1836  RADWAVE_PP=0.01;
1837  RADWAVE_CC=1.e6;
1838  RADWAVE_DTOUT1=(.05*RADWAVE_CC);
1839  RADWAVE_VX=0.;
1840  RADWAVE_RHOZERO=1.;
1841  RADWAVE_AAA=1.e-5;
1842  RADWAVE_ERAD=1.;
1843  RADWAVE_KK=2.*Pi;
1847  }
1848 
1849  if(RADWAVE_NWAVE==3){ //radiative density wave advected with the gas
1850  // FLUXDISSIPATION=(0.0);
1851  RADWAVE_PP=10.;
1852  RADWAVE_CC=1.e6;
1853  RADWAVE_VX=1.e-2;
1854  RADWAVE_DTOUT1=(.0005/RADWAVE_VX);
1855  RADWAVE_RHOZERO=1.;
1856  RADWAVE_AAA=1.e-5;
1857  RADWAVE_KK=2.*Pi;
1862  RADWAVE_KAPPAES=10.;
1863  }
1864 
1865 
1866  if(RADWAVE_NWAVE==4){ //sound wave with radiation, set up without the phase shifts etc.
1867  // FLUXDISSIPATION=(0.0);
1868  RADWAVE_PP=1.;
1869  RADWAVE_CC=1.e2;
1870  RADWAVE_DTOUT1=(.005*RADWAVE_CC);
1871  RADWAVE_VX=0.;
1872  RADWAVE_RHOZERO=1.;
1873  RADWAVE_AAA=1.e-1;
1874  RADWAVE_KK=2.*Pi;
1879  RADWAVE_KAPPA=100.;
1880  RADWAVE_ERADFACTOR=.5;
1881  RADWAVE_GASFACTOR=.5;
1882  }
1883 
1884 // if(RADWAVE_NWAVE==5){ //fast radiation-modified magnetosonic waves #1001 //Sasha
1885 // FLUXDISSIPATION=(0.0);
1886 // RADWAVE_PP=0.1;
1887 // RADWAVE_CC=10.;
1888 // RADWAVE_DTOUT1=(.005*RADWAVE_CC);
1889 // RADWAVE_VX=0.;
1890 // RADWAVE_RHOZERO=1.;
1891 // RADWAVE_AAA=1.e-1;
1892 // RADWAVE_KK=2.*Pi;
1893 // RADWAVE_UINT=(1./RADWAVE_CC/RADWAVE_CC)*RADWAVE_RHOZERO/gam/(gam-1.-1./RADWAVE_CC/RADWAVE_CC);
1894 // RADWAVE_TEMP=calc_PEQ_Tfromurho(RADWAVE_UINT,RADWAVE_RHOZERO);
1895 // ARAD_CODE=(3.0*RADWAVE_PP*(gam-1.)*RADWAVE_UINT/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP/RADWAVE_TEMP);
1896 // RADWAVE_ERAD=calc_LTE_EfromT(RADWAVE_TEMP);
1897 // RADWAVE_KAPPA=0.1;
1898 // RADWAVE_ERADFACTOR=.5;
1899 // RADWAVE_GASFACTOR=.5;
1900 // }
1901 
1902 
1903  // end post-SASHA
1904 
1905 
1906  BCtype[X1UP]=PERIODIC;
1907  BCtype[X1DN]=PERIODIC;
1908  BCtype[X2UP]=PERIODIC;
1909  BCtype[X2DN]=PERIODIC;
1910  BCtype[X3UP]=PERIODIC;
1911  BCtype[X3DN]=PERIODIC;
1912 
1913  int idt;
1914  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=RADWAVE_DTOUT1;
1915 
1916  DTr = 100; //number of time steps for restart dumps
1917  if(RADWAVE_VX==0.0) tf = MAX(100.0*RADWAVE_DTOUT1,5.0/RADWAVE_CC);
1918  else tf = MAX(100.0*RADWAVE_DTOUT1,5.0/MIN(RADWAVE_VX,RADWAVE_CC));
1919 
1920  if(RADWAVE_NWAVE==5){
1921  tf = 1.1*2*M_PI/RADWAVE_OMRE;
1922  }
1923 
1924 
1925 
1926  // DODIAGEVERYSUBSTEP = 1;
1927 
1928  }
1929 
1930  /*************************************************/
1931  /*************************************************/
1932  /*************************************************/
1933 
1935 
1936  lim[1]=lim[2]=lim[3]=MINM; // ok with fast shock
1937  //lim[1]=lim[2]=lim[3]=PARALINE; // good with fast shock
1938  // lim[1]=lim[2]=lim[3]=MC; // not too good with fast shock
1939  // lim[1]=lim[2]=lim[3]=MP5; // kinda ok with fast shock
1940  cour=0.499;
1942  gam=gamideal=4./3.;
1943  GAMMAMAX=100.0;
1944  BSQORHOLIMIT=1E10;
1945  BSQOULIMIT=1E10;
1946  UORHOLIMIT=1E10;
1947 
1948 
1951  BCtype[X2UP]=OUTFLOW;
1952  BCtype[X2DN]=OUTFLOW;
1953  BCtype[X3UP]=PERIODIC;
1954  BCtype[X3DN]=PERIODIC;
1955 
1956  DTr = 100; //number of time steps for restart dumps
1957 
1958  //set final time
1959 
1960  //fast shock
1961  if(WHICHKOMI==1){
1962  tf = 2.5;
1963  }
1964  //slow shock
1965  else if(WHICHKOMI==2){
1966  tf = 2.0;
1967  }
1968  //fast switch-off rarefaction
1969  else if(WHICHKOMI==3){
1970  tf = 1.0;
1971  }
1972  //slow switch-on rarefaction
1973  else if(WHICHKOMI==4){
1974  tf = 2.0;
1975  }
1976  //alfven wave
1977  else if(WHICHKOMI==5){
1978  tf = 2.0;
1979  }
1980  //compound wave
1981  else if(WHICHKOMI==6){
1982  tf = 1.5; //also 0.1 and 0.75 are other times
1983  }
1984  //Shock tube 1
1985  else if(WHICHKOMI==7){
1986  tf = 1.0;
1987  }
1988  //Shock tube 2
1989  else if(WHICHKOMI==8){
1990  tf = 1.0;
1991  }
1992  //Collision
1993  else if(WHICHKOMI==9){
1994  tf = 1.22;
1995  }
1996 
1997  if(WHICHKOMI>=101 && WHICHKOMI<=109){
1998  GAMMAMAX=2000.0;
1999  }
2000 
2001 
2002  int idt;
2003  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.1*tf;
2004  }
2005 
2006 
2007 
2008  /*************************************************/
2009  /*************************************************/
2010  /*************************************************/
2011 
2012  if(WHICHPROBLEM==RADBONDI){
2013 
2014  // lim[1]=lim[2]=lim[3]=MINM; // too low order for ~100 points
2015  // lim[1]=lim[2]=lim[3]=PARALINE;
2016  // cour=0.5;
2017 
2018 
2019  if(!(ISSPCMCOORDNATIVE(MCOORD))){
2020  dualfprintf(fail_file,"Must choose MCOORD (currently %d) to be spherical polar grid type for RADBONDI\n",MCOORD);
2021  myexit(3434628752);
2022  }
2023 
2024  a=0.0; // no spin in case use MCOORD=KSCOORDS
2025  cooling=KORAL;
2026  // ARAD_CODE=ARAD_CODE_DEF*1E5; // tuned so radiation energy flux puts in something much higher than ambient, while initial ambient radiation energy density lower than ambient gas internal energy.
2027 
2029  // BCtype[X1DN]=OUTFLOW;
2030  BCtype[X1DN]=HORIZONOUTFLOW; // although more specific extrapolation based upon solution might work better
2031  BCtype[X2UP]=PERIODIC;
2032  BCtype[X2DN]=PERIODIC;
2033  // BCtype[X3UP]=FREEOUTFLOW;
2034  BCtype[X3UP]=OUTFLOW;
2035  BCtype[X3DN]=OUTFLOW;
2036 
2037 
2038 
2039  RADBONDI_TESTNO=2;
2040 
2041  if(RADBONDI_TESTNO==0){ // E1T6 (#1 in koral Table 5)
2042  RADBONDI_PRADGAS=1.2e-4/RHOBAR;
2043  RADBONDI_TGAS0=1e6/TEMPBAR;
2045  }
2046 
2047  if(RADBONDI_TESTNO==1){ // E10T5 (#2 in koral Table 5 and #1 in Fragile paper)
2048  RADBONDI_PRADGAS=1.2e-7/RHOBAR;
2049  RADBONDI_TGAS0=1e5/TEMPBAR;
2050  RADBONDI_MDOTPEREDD=10.;
2051  }
2052 
2053  if(RADBONDI_TESTNO==2){ // E10T6 (#3 in koral Table 5 and #3 in Fragile paper)
2054  RADBONDI_PRADGAS=1.2e-4/RHOBAR;
2055  RADBONDI_TGAS0=1.e6/TEMPBAR;
2056  RADBONDI_MDOTPEREDD=10.;
2057  }
2058 
2059  if(RADBONDI_TESTNO==3){ // E10T7 (#4 in koral Table 5 and #5 in Fragile paper)
2060  RADBONDI_PRADGAS=1.2e-1/RHOBAR;
2061  RADBONDI_TGAS0=1e7/TEMPBAR;
2062  RADBONDI_MDOTPEREDD=10.;
2063  }
2064 
2065  // koral skips E30T6 that is #6 in Fragile paper
2066 
2067  if(RADBONDI_TESTNO==4){ // E100T6 (#5 in koral Table 5 and #7 in Fragile paper)
2068  RADBONDI_PRADGAS=1.2e-5/RHOBAR; // note koral paper has 1.2E-4 that is wrong.
2069  RADBONDI_TGAS0=1e6/TEMPBAR;
2070  RADBONDI_MDOTPEREDD=100.;
2071  }
2072 
2073  // koral skips E300T6 that is #8 in Fragile paper
2074 
2075  RADBONDI_MDOTEDD=(2.23/16.*1e18*MPERSUN)/(MBAR/TBAR); //Mdot converted to code units
2076  gam=gamideal=(1.+1./3.*((1.+RADBONDI_PRADGAS)/(.5+RADBONDI_PRADGAS)));
2077 
2078  trifprintf("RADBONDI: %g %g %g %g %g\n",RADBONDI_PRADGAS,RADBONDI_TGAS0,RADBONDI_MDOTPEREDD,RADBONDI_MDOTEDD,gam);
2079 
2080  int idt;
2081  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=10.0;
2082 
2083  DTr = 100; //number of time steps for restart dumps
2084  tf = 100*DTdumpgen[0]; // 100 dumps(?)
2085 
2086  // DODIAGEVERYSUBSTEP = 1;
2087 
2088  }
2089 
2090 
2091  /*************************************************/
2092  /*************************************************/
2093  /*************************************************/
2094 
2095  if(WHICHPROBLEM==RADDOT){
2096 
2097  // lim[1]=lim[2]=lim[3]=MINM; // NTUBE=1 has issues near cusp, so use MINM
2098  // cour=0.5;
2099 
2100  a=0.0; // no spin in case use MCOORD=KSCOORDS
2101  gam=gamideal=4.0/3.0;
2102  cooling=KORAL;
2103  ARAD_CODE=ARAD_CODE_DEF*1E-20; // tuned so radiation energy flux puts in something much higher than ambient, while initial ambient radiation energy density lower than ambient gas internal energy.
2104 
2105  BCtype[X1UP]=OUTFLOW;
2106  BCtype[X1DN]=OUTFLOW;
2107  BCtype[X2UP]=OUTFLOW;
2108  BCtype[X2DN]=OUTFLOW;
2109  BCtype[X3UP]=OUTFLOW;
2110  BCtype[X3DN]=OUTFLOW;
2111 
2112 
2113  int idt;
2114  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.05;
2115 
2116  DTr = 100; //number of time steps for restart dumps
2117  tf = 100.0*DTdumpgen[0];
2118 
2119  // DODIAGEVERYSUBSTEP = 1;
2120 
2121  }
2122 
2123  /*************************************************/
2124  /*************************************************/
2125  /*************************************************/
2126 
2128 
2129 
2130  // NOTENOTENOTE: Also do following before running with RADDONUT:
2131  // 1) coord.c: rbr=5E2 -> 1E2 OR use Sasha or Jon coordinate setup for all parameters.
2132  // 2) DORADFIXUPS 0
2133  // 3) BSQORHOLIMIT=1E2;
2134  // 4) #define CONNDERTYPE DIFFGAMMIE
2135  // 5) #define N1 32
2136 
2137  TIMEORDER=2; // faster and sufficient
2138 
2139 
2140  int set_fieldtype(void);
2141  int FIELDTYPE=set_fieldtype();
2142 
2143  if(FIELDTYPE==FIELDJONMAD){
2144  // then funnel becomes too optically thick and traps radiation and accelerates radiation down into BH, leading to bad physical energy conservation even if total energy-momentum conservation equations used very well.
2145  BSQORHOLIMIT=3E2; // back to 100 if using YFL1 and AVOIDTAUFLOOR==2 in phys.tools.rad.c
2146  BSQOULIMIT=1E9;
2147  }
2148  else{
2149  BSQORHOLIMIT=1E2;
2150  BSQOULIMIT=1E8;
2151  }
2152 
2153 
2154 
2155  if(DOWALDDEN){// override if doing wald
2157  }
2158 
2159  UULIMIT=1E-20; // something small
2160 
2161 
2162  // Torus setups:
2163  // 3) gam=gamideal=5.0/3.0; RADNT_ELL=4.5; RADNT_UTPOT=0.9999999; RADNT_ROUT=2.0; RADNT_RHODONUT=3.0; RADDONUT_OPTICALLYTHICKTORUS=1; RADNT_KKK=1.e-1 * (1.0/powl(RADNT_RHODONUT,gam-1.0)); // where KKK doesn't matter.
2164  // ATM setups:
2165  // 1,2,3) RADNT_ROUT=2.0; RADNT_RHOATMMIN=RADNT_RHODONUT*1E-4; RADNT_TGASATMMIN = 1.e9/TEMPBAR; RADNT_UINTATMMIN= (calc_PEQ_ufromTrho(RADNT_TGASATMMIN,RADNT_RHOATMMIN)); RADNT_TRADATMMIN = 1.e7/TEMPBAR; RADNT_ERADATMMIN= (calc_LTE_EfromT(RADNT_TRADATMMIN));
2166 
2167 
2168 
2169 
2171  // DONUT selections
2172  RADNT_DONUTTYPE=DONUTTHINDISK2; // SUPERMADNEW
2173  //RADNT_DONUTTYPE=DONUTOLEK;
2174  //RADNT_DONUTTYPE=DONUTOHSUGA;
2175  RADDONUT_OPTICALLYTHICKTORUS=1; // otherwise, pressure only from gas.
2176  RADNT_INFLOWING=0;
2177  RADNT_OMSCALE=1.0;
2178  // gas (not radiation) EOS \gamma value:
2179  gam=gamideal=5.0/3.0; // Ohsuga choice, assumes pairs not important.
2180 
2181  FTYPE gamtorus;
2182 
2183  if(WHICHPROBLEM==RADDONUT){
2185  // NOTEMARK: SUPERNOTE: Should set njet=0.0 in coord.c for thin disk.
2186  // NOTEMARK: Also choose npow=4.0;
2187  // NOTEMARK: Choose rbr=1E2;
2188  // NOTEMARK: Choose Qjet=1.9
2189  // NOTEMARK: htheta=0.1;
2190  // NOTEMARK: h0=0.1 instead of h0=0.3
2191  // NOTEMARK: Force only theta1=th0;
2192  // h_over_r=0.1;
2193  h_over_r=0.2;// SUPERMADNEW
2194  h_over_r_jet=2.0*h_over_r;
2195  }
2197  // h_over_r=0.3;
2198  h_over_r=0.2;
2199  h_over_r_jet=2.0*h_over_r;
2200  }
2201 
2202  }
2203 
2204 
2205 
2206 
2207 
2209  // DONUT TYPE and PARAMETERS
2211 
2213  RADDONUT_OPTICALLYTHICKTORUS=1; // otherwise, pressure only from gas.
2214  if(RADDONUT_OPTICALLYTHICKTORUS==1) gamtorus=4.0/3.0; // then should be as if gam=4/3 so radiation supports torus properly at t=0
2215  else gamtorus=gam;
2216 
2217  if(1){// SUPERMADNEW
2218  RADNT_RHODONUT=1E-2;
2219  RADNT_RHODONUT*=40.0;
2220 
2221 
2222  if(a==0.8 && FIELDTYPE==FIELDJONMAD){
2223  RADNT_RHODONUT/=(2.0*138.0);
2224  RADNT_RHODONUT/=(2.8); // Mdot\sim 135Ledd/c^2
2225  RADNT_RHODONUT*=(4.4); // Mdot\sim 135Ledd/c^2
2226  RADNT_RHODONUT*=(3.75); // Mdot\sim 135Ledd/c^2
2227  }
2228  if(a==0.8 && FIELDTYPE!=FIELDJONMAD){
2229  RADNT_RHODONUT/=(33.0);
2230  RADNT_RHODONUT/=(2.7); // Mdot\sim 135Ledd/c^2
2231  RADNT_RHODONUT*=(2.6); // Mdot\sim 135Ledd/c^2
2232  RADNT_RHODONUT*=(1.4); // Mdot\sim 135Ledd/c^2
2233  }
2234  if(a==0.0 && FIELDTYPE==FIELDJONMAD){
2235  RADNT_RHODONUT/=(2.0*138.0);
2236  RADNT_RHODONUT/=(2.8); // Mdot\sim 135Ledd/c^2
2237  RADNT_RHODONUT*=(3.9); // Mdot\sim 135Ledd/c^2
2238  RADNT_RHODONUT*=(1.8); // Mdot\sim 135Ledd/c^2
2239  }
2240  if(a==0.0 && FIELDTYPE!=FIELDJONMAD){
2241  RADNT_RHODONUT/=(33.0);
2242  RADNT_RHODONUT/=(12.5); // Mdot\sim 135Ledd/c^2
2243  RADNT_RHODONUT*=(1.4); // Mdot\sim 135Ledd/c^2
2244  RADNT_RHODONUT*=(0.71); // Mdot\sim 135Ledd/c^2
2245  }
2246  RADNT_TRADATMMIN = 1.e5/TEMPBAR;
2248 
2249  }
2250  if(0){
2251  RADNT_RHODONUT=1E-2; // NT73 with MBH=10msun, a=0, Mdot=5Ledd/c^2
2252  RADNT_TRADATMMIN = 1.e5/TEMPBAR;
2254  }
2255  if(0){
2256  RADNT_RHODONUT=1E-10;
2257  RADNT_TRADATMMIN = 1.e5/TEMPBAR;
2259  }
2260  if(0){
2261  RADNT_RHODONUT=1.0;
2262  RADNT_TRADATMMIN = 1.e7/TEMPBAR;
2264  }
2265 
2266  }
2267  else if(RADNT_DONUTTYPE==DONUTOLEK){
2268  if(1){
2269  RADDONUT_OPTICALLYTHICKTORUS=1; // otherwise, pressure only from gas.
2270  // Mdot~135Ledd/c^2
2271  //
2272  if(RADDONUT_OPTICALLYTHICKTORUS==1) gamtorus=4.0/3.0; // then should be as if gam=4/3 so radiation supports torus properly at t=0
2273  else gamtorus=gam;
2274 
2275  // RADNT_RHODONUT=1E-5;
2276  RADNT_RHODONUT=3.0; // gives 0.26 final density peak if RAD_ELL=3.5
2277  //RADNT_RHODONUT = KORAL2HARMRHO(1.0); // equivalent to koral's non-normalization
2278  // RADNT_ELL=4.5; // torus specific angular momentum
2279  RADNT_ELL=4.5; // torus specific angular momentum
2280  RADNT_UTPOT=0.9999999; // scales rin for donut
2281  RADNT_KKK=1.e-1 * (1.0/pow(RADNT_RHODONUT,gamtorus-1.0)); // no effect with the scaling with density put in.
2282 
2283  RADNT_TRADATMMIN = 1.e7/TEMPBAR;
2285 
2286  }
2287 
2288 
2289  if(0){
2290  // THIN DISK with Mdot~7Ledd/c^2
2291  RADDONUT_OPTICALLYTHICKTORUS=0; // otherwise, pressure only from gas.
2292 
2293  if(RADDONUT_OPTICALLYTHICKTORUS==1) gamtorus=4.0/3.0; // then should be as if gam=4/3 so radiation supports torus properly at t=0
2294  else gamtorus=gam;
2295 
2296  RADNT_RHODONUT=3.0/2E4; // gives 0.26 final density peak if RAD_ELL=3.5
2297  RADNT_ELL=4.5; // torus specific angular momentum
2298  RADNT_UTPOT=0.9999999; // scales rin for donut
2299  RADNT_KKK=1.e-1 * ((gamtorus-1.0)/(gamtorus)/pow(RADNT_RHODONUT,gamtorus-1.0)); // no effect with the scaling with density put in.
2300 
2301  RADNT_TRADATMMIN = 1.e7/TEMPBAR;
2303  }
2304 
2305 
2306  }
2307  else{
2308  RADDONUT_OPTICALLYTHICKTORUS=1; // otherwise, pressure only from gas.
2309 
2310  if(RADDONUT_OPTICALLYTHICKTORUS==1) gamtorus=4.0/3.0; // then should be as if gam=4/3 so radiation supports torus properly at t=0
2311  else gamtorus=gam;
2312 
2313  RADNT_RHODONUT=1E-2; // actual torus maximum density
2314  RADNT_DONUTRADPMAX=20.0; // radius of pressure maximum
2315  RADNT_HOVERR=0.5; // H/R\sim c_s/v_K at torus pressure maximum
2316  RADNT_LPOW=0.1; // l\propto r^lpow , lpow=0.5 would be Keplerian, lpow=0 would be constant angular momentum.
2317 
2318  RADNT_TRADATMMIN = 1.e7/TEMPBAR;
2320  }
2321 
2322 
2324  // DONUT atmosphere:
2325  RADNT_ROUT=2.0; // what radius ATMMIN things are defining
2326  //RADNT_RHOATMMIN=KORAL2HARMRHO(1.e-4);
2327  // RADNT_RHOATMMIN= KORAL2HARMRHO(1.e-2); // current koral choice
2329  // RADNT_TGASATMMIN = 1.e11/TEMPBAR;
2330  RADNT_TGASATMMIN = 1.e9/TEMPBAR;
2331 
2332  if(1){ // SUPERMADNEW
2334  }
2335 
2337  // need external radiation energy density to be lower than interior of torus, else drives photons into torus from overpressured atmosphere and is more difficult to evolve.
2338  // RADNT_TRADATMMIN = 1.e9/TEMPBAR;
2339 
2340  trifprintf("RADNT_RHODONUT=%g RADNT_RHOATMMIN=%g RADNT_RHOATMMIN=%g RADNT_UINTATMMIN=%g RADNT_ERADATMMIN=%g\n",RADNT_RHODONUT,RADNT_RHOATMMIN,RADNT_RHOATMMIN,RADNT_UINTATMMIN,RADNT_ERADATMMIN);
2341 
2342 
2343 
2344  // TOTRY: Om not happening even if set!
2345 
2346  // lim[1]=lim[2]=lim[3]=MINM; // too low order for ~100 points
2347  // if(WHICHPROBLEM==RADDONUT) lim[1]=lim[2]=lim[3]=PARALINE; // try later
2348  // cour=0.5;
2349 
2351  dualfprintf(fail_file,"Must choose MCOORD (currently %d) to be spherical polar grid type for RADNT,\n",MCOORD);
2352  myexit(3434628752);
2353  }
2355  dualfprintf(fail_file,"Must choose MCOORD (currently %d) to be CYLMINKMETRIC for RADCYLBEAM.\n",MCOORD);
2356  myexit(2493434634);
2357  }
2359  dualfprintf(fail_file,"Must choose MCOORD (currently %d) to be CARTMINKMETRIC2 for RADCYLBEAMCART.\n",MCOORD);
2360  myexit(2493434635);
2361  }
2362 
2364  else cooling=KORAL;
2365 
2366  // ARAD_CODE=ARAD_CODE_DEF*1E5; // tuned so radiation energy flux puts in something much higher than ambient, while initial ambient radiation energy density lower than ambient gas internal energy.
2367  // GAMMAMAXRAD=1000.0; // Koral limits for this problem.
2368  GAMMAMAXRAD=50.0L; // Koral limits for this problem.
2369  GAMMAMAXRADFAIL=50.0L;
2370  GAMMAMAX=15.0L; // MHD
2371 
2372 
2374  //
2375  // BOUNDARY CONDITIONS
2376 
2377  if(WHICHPROBLEM==RADCYLBEAM){
2378  // BCtype[X1DN]=ASYMM;
2379  // BCtype[X1DN]=SYMM;
2380  BCtype[X1DN]=CYLAXIS;
2382  BCtype[X2DN]=OUTFLOW;
2383  BCtype[X2UP]=OUTFLOW;
2384  BCtype[X3UP]=PERIODIC;
2385  BCtype[X3DN]=PERIODIC;
2386  }
2387  else if(WHICHPROBLEM==RADCYLBEAMCART){
2392  BCtype[X3UP]=PERIODIC;
2393  BCtype[X3DN]=PERIODIC;
2394  }
2395  else{
2396 
2397  BCtype[X1DN]=HORIZONOUTFLOW; // although more specific extrapolation based upon solution might work better
2398 
2399  if(WHICHPROBLEM==RADNT || WHICHPROBLEM==RADFLATDISK) BCtype[X1UP]=RADNTBC; // inflow analytic
2400  else if(WHICHPROBLEM==RADDONUT){
2401  // BCtype[X1UP]=RADNTBC; // inflow analytic
2402  //else BCtype[X1UP]=FIXEDUSEPANALYTIC; // fixed analytic // little silly for most of outer boundary, so avoid // KORALTODO: Also causes hellish problems with solution and implicit solver at the X1UP boundary surface (not just near torus)
2404  // BCtype[X1DN]=OUTFLOW;
2405  // BCtype[X1UP]=OUTFLOW;
2406 
2407  // if(DOWALDDEN) BCtype[X1UP]=OUTFLOW;
2408  }
2409 
2410  if(WHICHPROBLEM==RADFLATDISK) BCtype[X2DN]=ASYMM; // if non-zero Rin_array[2]
2411  else if(WHICHPROBLEM==RADNT || WHICHPROBLEM==RADDONUT){
2412  BCtype[X2DN]=POLARAXIS; // assumes Rin_array[2]=0
2413  }
2414 
2415 
2416 
2417  if(WHICHPROBLEM==RADNT || WHICHPROBLEM==RADFLATDISK) BCtype[X2UP]=RADNTBC; // disk condition (with ASYMM done first)
2418  else if(WHICHPROBLEM==RADDONUT){
2419  // BCtype[X2UP]=ASYMM; // with donut, let free, so ASYMM condition across equator (hemisphere)
2420  BCtype[X2UP]=POLARAXIS; // assumes Rin_array[2]=pi (full sphere)
2421  }
2422 
2423  if(DOWALDDEN==2) BCtype[X2UP]=WALDMONOBC;
2424 
2425  BCtype[X3UP]=PERIODIC;
2426  BCtype[X3DN]=PERIODIC;
2427  }
2428 
2429 
2430 
2431 
2433  // DUMP PERIODS
2434 
2435  int idt;
2437  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=10.0;
2438  }
2439  else{
2440  // for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=1.0;
2441  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=4.0;
2442  //for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.1;
2443  }
2444 
2445  if(totalsize[1]<=64){
2446  DTr = 100; //number of time steps for restart dumps
2447  }
2448  else{
2449  DTr = 1000;
2450  }
2451 
2452  if(WHICHPROBLEM==RADDONUT && totalsize[1]>64){
2453  // then, not testing, so full production mode for dumps
2454  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=50.0;
2456  DTdumpgen[IMAGEDUMPTYPE]=4.0;
2457  DTr=5000;
2458  if(PRODUCTION>=2){
2459  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=100.0;
2460  DTdumpgen[DEBUGDUMPTYPE]=400.0;
2462  DTdumpgen[IMAGEDUMPTYPE]=4.0;
2463  DTdumpgen[ENERDUMPTYPE]=4.0;
2464  DTr=5000;
2465  }
2466  }
2467 
2468 
2469 
2470  // tf = 100*DTdumpgen[0]; // 100 dumps(?)
2471  // tf = 2000*DTdumpgen[0]; // koral in default setup does 1000 dumps
2472  tf = 1E5;
2473 
2474  if(DOWALDDEN) tf=400.0;
2475 
2476  // DODIAGEVERYSUBSTEP = 1;
2477 
2478  // if(WHICHPROBLEM==RADDONUT) DODIAGEVERYSUBSTEP = 1;
2479 
2480  }
2481 
2482 
2483  /*************************************************/
2484  /*************************************************/
2485  /*************************************************/
2486 
2487  if(WHICHPROBLEM==RADCYLJET){
2488 
2489  TIMEORDER=2;
2490  // lim[1]=lim[2]=lim[3]=MC;
2491  // lim[1]=lim[2]=lim[3]=DONOR;
2492  lim[1]=lim[2]=lim[3]=MINM;
2493  //lim[1]=lim[2]=lim[3]=MC;
2494  // lim[1]=lim[2]=lim[3]=WENO5BND;
2495  //lim[1]=lim[2]=lim[3]=PARAFLAT;
2496  //lim[1]=lim[2]=lim[3]=PARALINE;
2497 
2498  cour=0.1;
2499 
2500  // int set_fieldtype(void);
2501  // int FIELDTYPE=set_fieldtype();
2502 
2503  BSQORHOLIMIT=1E2;
2504  BSQOULIMIT=1E8;
2505  cooling=KORAL;
2506 
2507  //gam=gamideal=5.0/3.0; // 4/3 if pairs
2508  gam=gamideal=4.0/3.0; // 4/3 if pairs or to compare with radiation-dominated case
2509 
2510  GAMMAMAXRAD=50.0L; // increase for higher jet speeds
2511  GAMMAMAXRADFAIL=50.0L; // increase for higher jet speeds
2512  GAMMAMAX=15.0L; // increase for higher jet speeds
2513 
2514 
2515  // 1: original Jane version
2516  // 2: pure jet version with reflective edge, uniform within, and radiative flux at boundary
2517  // 3: have high density in boundary that is fixed.
2518  // 4: hot jet but otherwise like #1
2519  // 5: rigid wall with panalytic setting of boundary conditions as flux, but outflow effectively
2520  // 6: like #1, but 2D and with absorption opacity so T matters
2521  RADCYLJET_TYPE=6;
2522 
2523 
2524  if(RADCYLJET_TYPE==6){
2525  //lim[1]=lim[2]=lim[3]=MINM;
2526  lim[1]=lim[2]=lim[3]=PARALINE;
2527  }
2528  else{
2529  lim[1]=lim[2]=lim[3]=MINM;
2530  }
2531 
2533  //
2534  // BOUNDARY CONDITIONS
2535 
2536  if(RADCYLJET_TYPE==1 || RADCYLJET_TYPE==4){
2537  // BCtype[X1DN]=ASYMM;
2538  // BCtype[X1DN]=SYMM;
2539  BCtype[X1DN]=CYLAXIS;
2540  BCtype[X1UP]=OUTFLOW;
2541  BCtype[X2DN]=OUTFLOW;
2542  BCtype[X2UP]=OUTFLOW;
2543  BCtype[X3UP]=PERIODIC;
2544  BCtype[X3DN]=PERIODIC;
2545  }
2546  else if(RADCYLJET_TYPE==2||RADCYLJET_TYPE==3){
2547  // BCtype[X1DN]=ASYMM;
2548  // BCtype[X1DN]=SYMM;
2549  BCtype[X1DN]=CYLAXIS;
2551  BCtype[X2DN]=OUTFLOW;
2552  BCtype[X2UP]=OUTFLOW;
2553  BCtype[X3UP]=PERIODIC;
2554  BCtype[X3DN]=PERIODIC;
2555  }
2556  else if(RADCYLJET_TYPE==5){
2557  BCtype[X1DN]=CYLAXIS;
2558  BCtype[X1UP]=FREEOUTFLOW; //FIXEDUSEPANALYTIC;
2559  BCtype[X2DN]=OUTFLOW;
2560  BCtype[X2UP]=OUTFLOW;
2561  BCtype[X3UP]=PERIODIC;
2562  BCtype[X3DN]=PERIODIC;
2563  }
2564  else if(RADCYLJET_TYPE==6){
2565  // BCtype[X1DN]=ASYMM;
2566  // BCtype[X1DN]=SYMM;
2567  BCtype[X1DN]=CYLAXIS;
2568  BCtype[X1UP]=OUTFLOW;
2570  BCtype[X2UP]=OUTFLOW;
2571  BCtype[X3UP]=PERIODIC;
2572  BCtype[X3DN]=PERIODIC;
2573  }
2574 
2575 
2576 
2578  // DUMP PERIODS
2579 
2580  int idt;
2581  // for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=1.0;
2582  // for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.01;
2583  //for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=0.1;
2584  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=1.0;
2585  //for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=10.0;
2586 
2587  tf = 200000;
2588 
2589  // DODIAGEVERYSUBSTEP = 1;
2590 
2591  // if(WHICHPROBLEM==RADDONUT) DODIAGEVERYSUBSTEP = 1;
2592 
2593  }
2594 
2595 
2596  /*************************************************/
2597  /*************************************************/
2598  /*************************************************/
2599 
2600 
2601 
2602 
2603 
2604 
2605 
2606 
2607  return(0);
2608 
2609 }
2610 
2611 
2612 int init_defcoord(void)
2613 {
2614 
2615  // set global THETAROTPRIMITIVES
2616  if(ALLOWMETRICROT){
2617  THETAROTPRIMITIVES=USER_THETAROTPRIMITIVES; // 0 to M_PI : what thetarot to use when primitives are set
2618  }
2619  else{
2620  THETAROTPRIMITIVES=0.0; // DO NOT CHANGE
2621  }
2622 
2623  if(ALLOWMETRICROT){
2624  THETAROTMETRIC = USER_THETAROTMETRIC; // defines metric generally
2625  }
2626  else{
2627  THETAROTMETRIC = 0.0;
2628  }
2629 
2630 
2631  /*************************************************/
2632  /*************************************************/
2633  /*************************************************/
2634  if(WHICHPROBLEM==FLATNESS){
2635  a=0.0; // no spin in case use MCOORD=KSCOORDS
2636 
2638  Rin_array[1]=0;
2639  Rin_array[2]=0;
2640  Rin_array[3]=0;
2641 
2642  Rout_array[1]=1.0;
2643  Rout_array[2]=1.0;
2644  Rout_array[3]=1.0;
2645  }
2646 
2647 
2648  /*************************************************/
2649  /*************************************************/
2650  /*************************************************/
2651 
2653  a=0.0; // no spin in case use MCOORD=KSCOORDS
2654 
2656  Rin_array[1]=-50.0;
2657  Rin_array[2]=-1.0;
2658  Rin_array[3]=-1.0;
2659 
2660  Rout_array[1]=50.0;
2661  Rout_array[2]=1.0;
2662  Rout_array[3]=1.0;
2663 
2664  }
2665 
2666  /*************************************************/
2667  /*************************************************/
2668  /*************************************************/
2669 
2670  if(WHICHPROBLEM==RADPULSE3D){
2671  a=0.0; // no spin in case use MCOORD=KSCOORDS
2672 
2674  Rin_array[1]=-50.0;
2675  Rin_array[2]=-50.0;
2676  Rin_array[3]=-50.0;
2677 
2678  Rout_array[1]=50.0;
2679  Rout_array[2]=50.0;
2680  Rout_array[3]=50.0;
2681  }
2682 
2683  /*************************************************/
2684  /*************************************************/
2685  /*************************************************/
2687  a=0.0; // no spin in case use MCOORD=KSCOORDS
2688 
2690  Rin_array[1]=0;
2691  Rin_array[2]=0;
2692  Rin_array[3]=0;
2693 
2694  Rout_array[1]=1.0;
2695  Rout_array[2]=1.0;
2696  Rout_array[3]=1.0;
2697  }
2698 
2699  /*************************************************/
2700  /*************************************************/
2701  /*************************************************/
2702  if(WHICHPROBLEM==RADTUBE){
2703  a=0.0; // no spin in case use MCOORD=KSCOORDS
2704 
2705  if(NTUBE==5){
2707  Rin_array[1]=-20.0;
2708  Rin_array[2]=-1.0;
2709  Rin_array[3]=-1.0;
2710 
2711  Rout_array[1]=20.0;
2712  Rout_array[2]=1.0;
2713  Rout_array[3]=1.0;
2714  }
2715  else{
2717  Rin_array[1]=-15.0;
2718  Rin_array[2]=-1.0;
2719  Rin_array[3]=-1.0;
2720 
2721  Rout_array[1]=15.0;
2722  Rout_array[2]=1.0;
2723  Rout_array[3]=1.0;
2724  }
2725  }
2726  /*************************************************/
2727  /*************************************************/
2728  /*************************************************/
2729  if(WHICHPROBLEM==RADSHADOW){
2730  a=0.0; // no spin in case use MCOORD=KSCOORDS
2731 
2733  Rin_array[1]=-1.0;
2734  Rin_array[2]=-1.0;
2735  Rin_array[3]=-1.0;
2736 
2737  Rout_array[1]=3.0;
2738  Rout_array[2]=1.0;
2739  Rout_array[3]=1.0;
2740  }
2741 
2742  /*************************************************/
2743  /*************************************************/
2744  /*************************************************/
2746  a=0.0; // no spin in case use MCOORD=KSCOORDS
2747 
2749  Rin_array[1]=-6.0;
2750  Rin_array[2]=0.0;
2751  Rin_array[3]=-1.0;
2752 
2753  Rout_array[1]=3.0;
2754  Rout_array[2]=1.5;
2755  Rout_array[3]=1.0;
2756  }
2757 
2758  /*************************************************/
2759  /*************************************************/
2760  /*************************************************/
2762  a=0.0; // no spin in case use MCOORD=KSCOORDS
2763 
2765  Rin_array[1]=0;
2766  Rin_array[2]=0;
2767  Rin_array[3]=0;
2768 
2769  Rout_array[1]=1.0;
2770  Rout_array[2]=1.0;
2771  Rout_array[3]=1.0;
2772 
2773 
2774 
2775  if (RADBEAM2D_BEAMNO==1){
2776  Rin_array[1]=2.6;
2777  Rout_array[1]=3.5;
2778  }
2779  else if (RADBEAM2D_BEAMNO==2){
2780  Rin_array[1]=5.5;
2781  Rout_array[1]=7.5;
2782  }
2783  else if (RADBEAM2D_BEAMNO==3){
2784  Rin_array[1]=14.5;
2785  Rout_array[1]=20.5;
2786  }
2787  else if (RADBEAM2D_BEAMNO==4){
2788  Rin_array[1]=30;
2789  Rout_array[1]=50;
2790  }
2791 
2792  Rin_array[2]=0.99*M_PI*0.5;
2793  Rout_array[2]=1.01*M_PI*0.5;
2794 
2795  Rin_array[3]=0.0;
2796  // Rout_array[3]=M_PI*0.25;
2797  Rout_array[3]=M_PI*0.5;
2798 
2799 
2800  }
2801  /*************************************************/
2802  /*************************************************/
2803  /*************************************************/
2805  a=0.0; // no spin in case use MCOORD=KSCOORDS
2806 
2808  Rin_array[1]=0;
2809  Rin_array[2]=0;
2810  Rin_array[3]=0;
2811 
2812  Rout_array[1]=1.0;
2813  Rout_array[2]=1.0;
2814  Rout_array[3]=1.0;
2815 
2816 
2817 
2818  if (RADBEAM2DKSVERT_BEAMNO==1){
2819  Rin_array[1]=2.3;
2820  Rout_array[1]=3.5;
2821  }
2822  else if (RADBEAM2DKSVERT_BEAMNO==2){
2823  Rin_array[1]=5.5;
2824  Rout_array[1]=12.5;
2825  }
2826  else if (RADBEAM2DKSVERT_BEAMNO==3){
2827  // for MKS
2828  //Rin_array[1]=2.5;
2829  // Rout_array[1]=3.0;
2830  // for UNI
2831  Rin_array[1]=14.5;
2832  Rout_array[1]=20.5;
2833  }
2834  else if (RADBEAM2DKSVERT_BEAMNO==4){
2835  Rin_array[1]=30;
2836  Rout_array[1]=50;
2837  }
2838  else if (RADBEAM2DKSVERT_BEAMNO==5){
2839  Rin_array[1]=5.5;
2840  Rout_array[1]=12.5;
2841  }
2842 
2843  // NOTE: For testing radiation through polar axis region with axis *on* grid.
2844  Rin_array[2]= -0.25*Pi/2.;
2845  Rout_array[2]=+0.27*Pi/2.;
2846 
2847  Rin_array[3]=0.;
2848  Rout_array[3]=0.01*Pi/4.; // 8.*Pi/4.
2849 
2850 
2851  }
2852  /*************************************************/
2853  /*************************************************/
2854  /*************************************************/
2855  if(WHICHPROBLEM==ATMSTATIC){
2856  a=0.0; // no spin in case use MCOORD=KSCOORDS
2857 
2859 
2860  Rin_array[1]=1E6;
2861  Rout_array[1]=2E6;
2862 
2863  Rin_array[2]=0.99*M_PI*0.5;
2864  Rout_array[2]=1.01*M_PI*0.5;
2865 
2866  Rin_array[3]=-1.0;
2867  Rout_array[3]=1.0;
2868 
2869  }
2870  /*************************************************/
2871  /*************************************************/
2872  /*************************************************/
2873  if(WHICHPROBLEM==RADATM){
2874  a=0.0; // no spin in case use MCOORD=KSCOORDS
2875 
2877 
2878  Rin_array[1]=1E6;
2879  Rout_array[1]=1.4E6;
2880 
2881  Rin_array[2]=0.99*M_PI*0.5;
2882  Rout_array[2]=1.01*M_PI*0.5;
2883 
2884  Rin_array[3]=-1.0;
2885  Rout_array[3]=1.0;
2886 
2887  }
2888 
2889  /*************************************************/
2890  /*************************************************/
2891  /*************************************************/
2892  if(WHICHPROBLEM==RADWALL){
2893  a=0.0; // no spin in case use MCOORD=KSCOORDS
2894 
2896  Rin_array[1]=-6;
2897  Rin_array[2]=0;
2898  Rin_array[3]=0;
2899 
2900  Rout_array[1]=3.0;
2901  Rout_array[2]=1.5;
2902  Rout_array[3]=1.0;
2903  }
2904 
2905  /*************************************************/
2906  /*************************************************/
2907  /*************************************************/
2908  if(WHICHPROBLEM==RADWAVE){
2909  a=0.0; // no spin in case use MCOORD=KSCOORDS
2910 
2912  Rin_array[1]=0;
2913  Rin_array[2]=0;
2914  Rin_array[3]=0;
2915 
2916  Rout_array[1]=1.0;
2917  Rout_array[2]=1.0;
2918  Rout_array[3]=1.0;
2919 
2920  }
2921 
2922  /*************************************************/
2923  /*************************************************/
2924  /*************************************************/
2925 #if(WHICHPROBLEM==KOMIPROBLEM)
2926  FTYPE xl, xc, xr;
2927  a=0.0; // no spin in case use MCOORD=KSCOORDS
2928 
2930 
2931  //fast shock
2932  if(WHICHKOMI==1){
2933  xl = -1.0;
2934  xc = 0.0;
2935  xr = 1.0;
2936  }
2937  //slow shock
2938  else if(WHICHKOMI==2){
2939  xl = -0.5;
2940  xc = 0.0;
2941  xr = 1.5;
2942  }
2943  //fast switch-off rarefaction
2944  else if(WHICHKOMI==3){
2945  xl = -1.0;
2946  xc = 0.0;
2947  xr = 1.0;
2948  }
2949  //slow switch-on rarefaction
2950  else if(WHICHKOMI==4){
2951  xl = -0.75;
2952  xc = 0.0;
2953  xr = 1.2;
2954  }
2955  //alfven wave
2956  else if(WHICHKOMI==5){
2957  xl = -1.0;
2958  xc = 0.0;
2959  xr = 1.5;
2960  }
2961  //compound wave (plot is -0.5 to 1.5, but need 4 cells between -0.025 and 0.0 according to Figure 5)
2962  else if(WHICHKOMI==6){
2963  xl = -0.5;
2964  xc = 0.0;
2965  xr = 1.5;
2966  }
2967  //Shock tube 1
2968  else if(WHICHKOMI==7){
2969  xl = -1.0;
2970  xc = 0.0;
2971  xr = 1.5;
2972  }
2973  //Shock tube 2
2974  else if(WHICHKOMI==8){
2975  xl = -1.2;
2976  xc = 0.0;
2977  xr = 1.2;
2978  }
2979  //Collision
2980  else if(WHICHKOMI==9){
2981  xl = -1.0;
2982  xc = 0.0;
2983  xr = 1.0;
2984  }
2985 
2986 
2987  if(WHICHKOMI==101){
2988  xl = -0.5;
2989  xc = 0.0;
2990  xr = 1.5;
2991  }
2992  if(WHICHKOMI==102){
2993  xl = -0.5;
2994  xc = 0.0;
2995  xr = 1.5;
2996  }
2997  if(WHICHKOMI==103){
2998  xl = -1.5;
2999  xc = 0.0;
3000  xr = 0.5;
3001  }
3002  if(WHICHKOMI==104){
3003  xl = -0.5;
3004  xc = 0.0;
3005  xr = 1.5;
3006  }
3007  if(WHICHKOMI==105){
3008  xl = -0.5;
3009  xc = 0.0;
3010  xr = 1.5;
3011  }
3012  if(WHICHKOMI==106){
3013  xl = -0.5;
3014  xc = 0.0;
3015  xr = 1.5;
3016  }
3017  if(WHICHKOMI==107){
3018  xl = -0.5;
3019  xc = 0.0;
3020  xr = 1.5;
3021  }
3022  if(WHICHKOMI==108){
3023  xl = -1.0;
3024  xc = 0.0;
3025  xr = 2.0;
3026  }
3027  if(WHICHKOMI==109){
3028  xl = -1.0;
3029  xc = 0.0;
3030  xr = 2.0;
3031  }
3032 
3033  Rin = Rin_array[1]=xl;
3034  Rin_array[2]=0;
3035  Rin_array[3]=0;
3036 
3037  Rout = Rout_array[1]=xr;
3038  Rout_array[2]=1.0;
3039  Rout_array[3]=1.0;
3040 
3041 #endif
3042 
3043 
3044  /*************************************************/
3045  /*************************************************/
3046  /*************************************************/
3047  if(WHICHPROBLEM==RADBONDI){
3048  a=0.0; // no spin in case use MCOORD=KSCOORDS
3049 
3050  // TOTRY: Change horizon interpolation to be like koral.
3051  // PURE HYDRO: entropy inversions don't lead to major problems.
3052  // TRY pure HD.
3053  // TRY moving the inner boundary outward a bit.
3054 
3055  // KORALTODO: 2.5 to 2E4 in paper
3056  // RADBONDI_MINX=3.5;
3057  // RADBONDI_MAXX=2e3;
3058 
3059  if(MCOORD==KSCOORDS){
3060  RADBONDI_MINX=1.9; // few cells inside horizon // 1.9 good for N1=512
3061  RADBONDI_MAXX=2e4;
3062  }
3063  else{
3064  RADBONDI_MINX=2.5;
3065  RADBONDI_MAXX=2e4;
3066  }
3067 
3068  //#define LOGXGRID
3069  // FTYPE LOGPAR1=2.2;
3070  // FTYPE LOGPAR2=2.;
3071 
3072  // defcoord = UNIFORMCOORDS;
3073  defcoord = LOGRUNITH; // Uses R0, Rin, Rout and Rin_array,Rout_array for 2,3 directions
3074 
3075 
3076  if(RADBONDI_TESTNO==1){
3077  if(MCOORD==KSCOORDS){
3078  RADBONDI_MINX=1.9; // few cells inside horizon // 1.9 good for N1=512
3079  }
3080  else{
3081  RADBONDI_MINX=2.5;
3082  }
3083  R0=(1.85/1.9)*RADBONDI_MINX; // due to cold temp, energy equation evolution requires (with PARA) more cells near BH. Something like MP5 (that does some avg->point conversions) as in Koral wouldn't need to do that.
3084  // also may require not so large tolerance, but so far not limiting solution like energy equation does.
3085  }
3086  else{
3087  // normal MINX ok.
3088  R0=0.9*RADBONDI_MINX;
3089  // can reduce noise in u_g in solutions if use larger R0.
3090  }
3091 
3092 
3093  Rin_array[2]=.99*Pi/2.;
3094  Rout_array[2]=1.01*Pi/2.;
3095  Rin_array[3]=-1.;
3096  Rout_array[3]=1.;
3097 
3098 
3099  if(0){
3101  // 2D MAGBONDI TEST
3102  RADBONDI_MINX=1.9;
3103  RADBONDI_MAXX=2e4;
3104  // RADBONDI_MAXX=2e2;
3105  R0=1.88;
3106  Rin_array[2]=0.0;
3107  Rout_array[2]=Pi;
3108  }
3109 
3110  if(0){
3111  if(RADBONDI_TESTNO==3){
3112  // normal 1D non-MAG BONDI=3 test that ran for paper
3113  RADBONDI_MINX=2.5;
3114  RADBONDI_MAXX=2e4;
3115  R0=2.2;
3116  Rin_array[2]=.99*Pi/2.;
3117  Rout_array[2]=1.01*Pi/2.;
3118  }
3119  }
3120 
3121 
3123 
3126 
3127 
3128  if(R0>=RADBONDI_MINX){
3129  dualfprintf(fail_file,"Must have R0=%g < RADBONDI_MINX=%g\n",R0,RADBONDI_MINX);
3130  myexit(243532469);
3131  }
3132 
3133  Rhor=rhor_calc(0);
3135  dualfprintf(fail_file,"WARNING: Have boundary oustide horizon in KS coords\n");
3136  }
3137 
3138 
3139  }
3140 
3141  /*************************************************/
3142  /*************************************************/
3143  /*************************************************/
3144  if(WHICHPROBLEM==RADDOT){
3145  a=0.0; // no spin in case use MCOORD=KSCOORDS
3146 
3148  Rin_array[1]=0;
3149  Rin_array[2]=0;
3150  Rin_array[3]=0;
3151 
3152  Rout_array[1]=1.0;
3153  Rout_array[2]=1.0;
3154  Rout_array[3]=1.0;
3155 
3156  }
3157 
3158  /*************************************************/
3159  /*************************************************/
3160  /*************************************************/
3161  if(WHICHPROBLEM==RADNT){
3162  a=0.0; // no spin in case use MCOORD=KSCOORDS
3163 
3164  if(1){
3165  // RADNT_MINX=1.7; // allowed in KSCOORDS
3166  RADNT_MINX=1.9; // allowed in KSCOORDS (latest koral)
3167  RADNT_MAXX=50.0;
3168  }
3169  else{
3170  RADNT_MINX=1.5*Rhor;
3171  RADNT_MAXX=40.0; // 27.8
3172  }
3173 
3174  // KORALTODO: Why doesn't koral just use same log coords as used for RADBONDI? Change of feature set.
3175  // defcoord = UNIFORMCOORDS;
3176  defcoord = LOGRUNITH; // Uses R0, Rin, Rout and Rin_array,Rout_array for 2,3 directions
3177  R0=0.0;
3178  Rin=RADNT_MINX;
3179  Rout=RADNT_MAXX;
3180 
3181  // Rin_array[2]=0.2*Pi/4.;
3182  Rin_array[2]=0.0*Pi/4.;
3183  Rout_array[2]=Pi/2.;
3184  Rin_array[3]=-1.;
3185  Rout_array[3]=1.;
3186 
3187  }
3188 
3189  /*************************************************/
3190  /*************************************************/
3191  /*************************************************/
3193  a=0.0; // no spin in case use MCOORD=KSCOORDS
3194 
3195  RADNT_MINX=4.0;
3196  RADNT_MAXX=100.0;
3197 
3198  if(0){
3199  // KORALTODO: The below use of log radial grid leads to opacity related failure of problem when using pre-recent koral kappa (i.e. not zero)
3200  defcoord = LOGRUNITH; // Uses R0, Rin, Rout and Rin_array,Rout_array for 2,3 directions
3201  R0=0.0;
3202  Rin=RADNT_MINX;
3203  Rout=RADNT_MAXX;
3204  }
3205  else{
3207  Rin_array[1]=RADNT_MINX;
3209  }
3210 
3211 
3212  // Rin_array[2]=0.0*Pi/4.;
3213  // Rin_array[2]=0.01*Pi/4.;
3214  Rin_array[2]=0.1*Pi/4.;
3215  Rout_array[2]=Pi/2.;
3216  Rin_array[3]=-1.;
3217  Rout_array[3]=1.;
3218 
3219  }
3220 
3221  /*************************************************/
3222  /*************************************************/
3223  /*************************************************/
3224  if(WHICHPROBLEM==RADDONUT){
3225  // a=0.0; // no spin in case use MCOORD=KSCOORDS
3226 
3227  // metric stuff first
3228  a = 0.8 ; // WALD
3229 
3230 
3231  if(1){
3232  RADNT_MINX=1.7; // allows in KSCOORDS
3233  // RADNT_MAXX=50.0;
3234  // RADNT_MAXX=60.0;
3235  // RADNT_MAXX=400.0; // what was using before, but problems at outer radial edge develop after t\sim 5600
3236  RADNT_MAXX=1E4;
3237  }
3238  else{
3239  RADNT_MINX=1.8*Rhor;
3240  RADNT_MAXX=40.0; // 27.8
3241  }
3242 
3243  // KORALTODO: Why doesn't koral just use same log coords as used for RADBONDI?
3244  // defcoord = UNIFORMCOORDS;
3245  // defcoord = LOGRUNITH; // Uses R0, Rin, Rout and Rin_array,Rout_array for 2,3 directions
3246  Rin=RADNT_MINX;
3247  Rout=RADNT_MAXX;
3248 
3249  if(DOWALDDEN==0) defcoord=JET6COORDS;
3250  else if(DOWALDDEN){
3251  // Rout=2000.0; // new normal
3252  Rout=400.0; // newer normal
3254  }
3255  // else if(DOWALDDEN==2) defcoord=JET6COORDS;
3256 
3257 
3258  if(0){
3259  Rin_array[1]=Rin;
3260  Rout_array[1]=Rout;
3261  Rin_array[2]=0.0*Pi/4.; // but koral currently uses 0.5*Pi/4
3262  Rout_array[2]=Pi; // KORALNOTE: Different from KORAL code test
3263  Rin_array[3]=-1.;
3264  Rout_array[3]=1.;
3266  }
3267 
3268 
3269  Rhor=rhor_calc(0);
3270 
3271 
3272  // hslope = 0.3;
3273  hslope = 1.04*pow(h_over_r,2.0/3.0);
3274  // NOTEMARK: Should change h0 in coord.c from h0=0.3 to h0=0.1 or something for thin disks
3275 
3276 
3277  if(DOWALDDEN){ // to give more resolution near BH
3278  R0=0.7;
3279  }
3280  else{
3281  // R0=0.0;
3282  R0=0.2;
3283  }
3284 
3285 
3286 
3287  if(Rout<1E3){
3288  Rin=1.05;
3289  }
3290  else{
3291  Rin=1.1;
3292  // setRin_withchecks(&Rin);
3293  }
3294 
3295  if(DOWALDDEN && a<0.9 && totalsize[1]>64) Rin=1.4;
3296 
3297  if(totalsize[1]<32*4&&DOWALDDEN==0){
3298  dualfprintf(fail_file,"RADDONUT setup for 128x64 with that grid\n");
3299  //myexit(28634693);
3300  }
3301 
3302  }
3303 
3304  /*************************************************/
3305  /*************************************************/
3306  /*************************************************/
3307  if(WHICHPROBLEM==RADCYLBEAM){
3308  a=0.0; // no spin in case use MCOORD=KSCOORDS
3309 
3310  if(WHICHPROBLEM==RADCYLBEAM) MBH=0.0; // because CYLMINKMETRIC really has gravity if choose MBH!=0
3311 
3312  // TOTRY: find azimuthal flux.
3313 
3314  RADNT_FULLPHI=(Pi/2.5);
3315  // RADNT_FULLPHI=(2.0*Pi);
3316 
3317  RADNT_MINX=0.0; // all the way to the R=0 origin
3318  RADNT_MAXX=20.0;
3319 
3320  if(1){
3321  defcoord = LOGRUNITH; // Uses R0, Rin, Rout and Rin_array,Rout_array for 2,3 directions
3322  R0=-1.0;
3323  Rin=RADNT_MINX;
3324  Rout=RADNT_MAXX;
3325 
3326  Rin_array[2]=-1.0;
3327  Rout_array[2]=1.0;
3328 
3329  Rin_array[3]=0.0;
3331  }
3332  else{
3334 
3335  Rin_array[1]=RADNT_MINX;
3337 
3338  Rin_array[2]=-1.0;
3339  Rout_array[2]=1.0;
3340 
3341  Rin_array[3]=0.0;
3343  }
3344 
3345  }
3346 
3347  /*************************************************/
3348  /*************************************************/
3349  /*************************************************/
3351  a=0.0;
3352 
3353  RADNT_MINX=-20.0;
3354  RADNT_MAXX=+20.0;
3355 
3357 
3358  Rin_array[1]=RADNT_MINX;
3360 
3361  Rin_array[2]=RADNT_MINX;
3363 
3364  Rin_array[3]=0.0;
3365  Rout_array[3]=1.0;
3366 
3367  }
3368 
3369 
3370  /*************************************************/
3371  /*************************************************/
3372  /*************************************************/
3373  if(WHICHPROBLEM==RADCYLJET){
3374  a=0.0; // no spin in case use MCOORD=KSCOORDS
3375 
3376  MBH=0.0; // because CYLMINKMETRIC really has gravity if choose MBH!=0
3377 
3378  // TOTRY: find azimuthal flux.
3379 
3380  // RADNT_FULLPHI=(Pi/2.5);
3381  RADNT_FULLPHI=(2.0*Pi);
3382 
3383  if(RADCYLJET_TYPE==1 || RADCYLJET_TYPE==4){
3384  RADNT_MINX=1E-2; // all the way to the R=0 origin
3385  RADNT_MAXX=10.0;
3386  }
3387  else if(RADCYLJET_TYPE==2||RADCYLJET_TYPE==3){
3388  RADNT_MINX=1E-2; // all the way to the R=0 origin
3389  RADNT_MAXX=1.0;
3390  }
3391  else if(RADCYLJET_TYPE==5){
3392  RADNT_MINX=1E-2; // all the way to the R=0 origin
3393  RADNT_MAXX=1.0;
3394  }
3395  else if(RADCYLJET_TYPE==6){
3396  RADNT_MINX=1E-2; // all the way to the R=0 origin
3397  RADNT_MAXX=15.0;
3398  }
3399 
3401  defcoord = LOGRUNITH; // Uses R0, Rin, Rout and Rin_array,Rout_array for 2,3 directions
3402  R0=-1.0;
3403  Rin=RADNT_MINX;
3404  Rout=RADNT_MAXX;
3405  Rin_array[1]=RADNT_MINX;
3407 
3408  Rin_array[2]=-1.0;
3409  Rout_array[2]=1.0;
3410 
3411  Rin_array[3]=0.0;
3413  }
3414  if(RADCYLJET_TYPE==5){
3416 
3417  Rin_array[1]=RADNT_MINX;
3419 
3420  Rin_array[2]=-1.0;
3421  Rout_array[2]=1.0;
3422 
3423  Rin_array[3]=0.0;
3425  }
3426  if(RADCYLJET_TYPE==6){
3427  defcoord = LOGRUNITH; // Uses R0, Rin, Rout and Rin_array,Rout_array for 2,3 directions
3428  R0=-0.2;
3429  Rin=RADNT_MINX;
3430  Rout=RADNT_MAXX;
3431  Rin_array[1]=RADNT_MINX;
3433 
3434  Rin_array[2]=0.0;
3435  Rout_array[2]=30.0*45.0; //Rout*10.0;
3436 
3437  Rin_array[3]=0.0;
3439  }
3440 
3441  }
3442 
3443 
3444  /*************************************************/
3445  /*************************************************/
3446  /*************************************************/
3447 
3448 
3449  return(0);
3450 }
3451 
3452 
3453 int init_grid(void)
3454 {
3455 
3456  init_defcoord(); // just avoids splitting function call, here sets R0,Rin,Rout
3457 
3458  return(0);
3459 }
3460 
3461 
3462 
3463 // assumes normalized density
3464 int init_atmosphere(int *whichvel, int*whichcoord,int i, int j, int k, FTYPE *pr)
3465 {
3466  int funreturn;
3467 
3468  if(WHICHPROBLEM==RADDONUT){
3469  if(0&&(RADNT_DONUTTYPE==DONUTTHINDISK || RADNT_DONUTTYPE==DONUTTHINDISK2)){ // keep as 0&& because not setting E properly in init_atmosphere()
3470  funreturn=user1_init_atmosphere(whichvel, whichcoord,i, j, k, pr);
3471  if(funreturn!=0) return(funreturn);
3472  return(0);
3473  }
3474  else{
3475  // NO atmosphere
3476  // tells to do no coordinate transformations
3477  *whichvel=WHICHVEL;
3478  *whichcoord=PRIMECOORDS;
3479  return(-1);
3480  }
3481  }
3482  else{
3483  // NO atmosphere
3484  // tells to do no coordinate transformations
3485  *whichvel=WHICHVEL;
3486  *whichcoord=PRIMECOORDS;
3487  return(-1);
3488  }
3489 
3490 
3491  return(-1); // no atmosphere set, so don't do anything at all
3492 
3493 }
3494 
3495 int init_grid_post_set_grid(FTYPE (*prim)[NSTORE2][NSTORE3][NPR], FTYPE (*pstag)[NSTORE2][NSTORE3][NPR], FTYPE (*ucons)[NSTORE2][NSTORE3][NPR], FTYPE (*vpot)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3], FTYPE (*Bhat)[NSTORE2][NSTORE3][NPR], FTYPE (*panalytic)[NSTORE2][NSTORE3][NPR], FTYPE (*pstaganalytic)[NSTORE2][NSTORE3][NPR], FTYPE (*vpotanalytic)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3], FTYPE (*Bhatanalytic)[NSTORE2][NSTORE3][NPR], FTYPE (*F1)[NSTORE2][NSTORE3][NPR+NSPECIAL], FTYPE (*F2)[NSTORE2][NSTORE3][NPR+NSPECIAL], FTYPE (*F3)[NSTORE2][NSTORE3][NPR+NSPECIAL], FTYPE (*Atemp)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3])
3496 {
3497  int i,j,k;
3498  FTYPE X[NDIM],V[NDIM],r,th;
3499  extern void check_spc_singularities_user(void);
3500 
3501 
3502 
3503 
3504  // some calculations, althogh perhaps calculated already, definitely need to make sure computed
3505  Rhor=rhor_calc(0);
3506  Risco=rmso_calc(PROGRADERISCO);
3507 
3508 
3509 
3510  int set_fieldtype(void);
3511  int FIELDTYPE=set_fieldtype();
3512 
3513 
3514 
3515  // defaults
3516  randfact = 0.1;
3517  beta=100.0;
3518  rin=6.0;
3519  rinfield=rin;
3520  routfield=12.0;
3521 
3522  if(WHICHPROBLEM==RADDONUT){
3523 
3524  if(FIELDTYPE==DISK2FIELD){
3525  //rin=6.0; // old setting
3526  rinfield=rin=9.0;
3527  routfield=13.0;
3528  // beta=100.0; // was used for rada0.94 etc.
3529  beta = 10.0;
3530  }
3531  else if(FIELDTYPE==FIELDJONMAD){
3532  rin=9.0;
3533  rinfield=12;
3534  beta = 10.0;
3535  }
3536  else if(FIELDTYPE==FIELDWALD || FIELDTYPE==MONOPOLE){
3537  rin=9.0;
3538  rinfield=1.0;
3539  routfield=2.0;
3540  // so field at horizon is BSQORHOWALD
3542  }
3543 
3545  // rinfield=1.1*Risco;
3546  rinfield=Risco;
3547  // beta=1E30;
3548  beta=10.0;
3549 
3550  if(FIELDTYPE==FIELDJONMAD) rin=0.0;
3551  else rin=rinfield;
3552  }
3553 
3554  // SUPERMADNEW
3556  rin=1.1*Risco;
3557  rinfield=1.1*Risco;
3558 
3559  rin=0.0;
3560 
3561  rinfield=20.0; // works for any a for these models
3562  routfield=40.0;
3563 
3564  //rinfield=Risco;
3565  // beta=1E30;
3566  if(FIELDTYPE==FIELDJONMAD){
3567  beta=20.0; // so for MAD will have ~1 mri wavelength per half-height H.
3568  }
3569  else{
3570  beta=5.0; // so perturbations are at 2 but rest less and tends to be beta\sim 20
3571  }
3572  }
3573 
3574 
3575  }
3576 
3577 
3578 
3579 
3580 
3581  if(WHICHPROBLEM==RADDOT){
3582  RADDOT_XDOT=(20.0/41.0)*(Rout_array[1]-Rin_array[1]) + Rin_array[1];
3583  RADDOT_YDOT=(10.0/41.0)*(Rout_array[2]-Rin_array[2]) + Rin_array[2];
3584  RADDOT_ZDOT=(20.0/41.0)*(Rout_array[3]-Rin_array[3]) + Rin_array[3];
3585 
3586  // get X1,X2,X3 of dot assuming UNIFORMCOORDS
3587  FTYPE myX[NDIM]={0.0};
3588  FTYPE dxdxp[NDIM][NDIM];
3589  dxdxprim_ijk(0, 0, 0, CENT, dxdxp);
3590  myX[1]= startx[1] + (RADDOT_XDOT-Rin_array[1])/dxdxp[1][1];
3591  myX[2]= startx[2] + (RADDOT_YDOT-Rin_array[2])/dxdxp[2][2];
3592  myX[3]= startx[3] + (RADDOT_ZDOT-Rin_array[3])/dxdxp[3][3];
3593 
3594  trifprintf("RADDOT: myX: %g %g %g\n",myX[1],myX[2],myX[3]);
3595 
3596  // get nearest i,j,k
3597  extern void icoord_round(FTYPE *X,int loc, int *i, int *j, int *k);
3598  icoord_round(myX,CENT,&RADDOT_IDOT,&RADDOT_JDOT,&RADDOT_KDOT);
3599 
3600  RADDOT_FYDOT=0.3;
3601  RADDOT_LTEFACTOR=1.;
3602  RADDOT_URFX=0.;
3603  RADDOT_F1=100;
3604  RADDOT_F2=10000;
3605 
3606  trifprintf("RADDOT: %g %g %g : %d %d %d\n",RADDOT_XDOT,RADDOT_YDOT,RADDOT_ZDOT,RADDOT_IDOT,RADDOT_JDOT,RADDOT_KDOT);
3607  }
3608 
3610  trifprintf("BEGIN check_rmin\n");
3611  // check rmin
3612  check_rmin();
3613  trifprintf("END check_rmin\n");
3614  }
3615 
3616  if(1){
3617  // check that singularities are properly represented by code
3618  trifprintf("BEGIN check_spc_singularities_user\n");
3619  // SUPERGODMARK: Goes very slowly sometimes randomly for unknown reasons.
3620  dualfprintf(fail_file,"WARNING: check_spc_singularities_user() oddly stalls sometimes...\n");
3621  check_spc_singularities_user();
3622  trifprintf("END check_spc_singularities_user\n");
3623  dualfprintf(fail_file,"WARNING: done with check_spc_singularities_user(), but it sometimes stalls or goes very very slow for no apparently good reason. E.g., on NAUTILUS with -O0, very slow checks. But just putting dualfprintf before and after the above call leads to quick finish.\n");
3624  }
3625 
3626  return(0);
3627 
3628 }
3629 
3630 
3631 
3632 
3633 int init_primitives(FTYPE (*prim)[NSTORE2][NSTORE3][NPR], FTYPE (*pstag)[NSTORE2][NSTORE3][NPR], FTYPE (*ucons)[NSTORE2][NSTORE3][NPR], FTYPE (*vpot)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3], FTYPE (*Bhat)[NSTORE2][NSTORE3][NPR], FTYPE (*panalytic)[NSTORE2][NSTORE3][NPR], FTYPE (*pstaganalytic)[NSTORE2][NSTORE3][NPR], FTYPE (*vpotanalytic)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3], FTYPE (*Bhatanalytic)[NSTORE2][NSTORE3][NPR], FTYPE (*F1)[NSTORE2][NSTORE3][NPR+NSPECIAL], FTYPE (*F2)[NSTORE2][NSTORE3][NPR+NSPECIAL], FTYPE (*F3)[NSTORE2][NSTORE3][NPR+NSPECIAL], FTYPE (*Atemp)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3])
3634 {
3635  int funreturn;
3636  int inittype;
3637  FTYPE thetarotorig;
3638 
3639 
3640  thetarotorig=THETAROT;
3641  THETAROT = THETAROTPRIMITIVES; // define rho,u,v,B as if no rotation (but metric might still be used, so still use set_grid_all() in initbase.c)
3642 
3643 
3644  inittype=1;
3645 
3646  funreturn=user1_init_primitives(inittype, prim, pstag, ucons, vpot, Bhat, panalytic, pstaganalytic, vpotanalytic, Bhatanalytic, F1, F2, F3,Atemp);
3647  if(funreturn!=0) return(funreturn);
3648 
3649  THETAROT = thetarotorig; // back to previous version
3650 
3651  return(0);
3652 
3653 
3654 }
3655 
3656 
3657 
3658 
3659 
3660 
3661 
3662 
3663 
3664 
3665 
3666 
3667 
3668 // When setting primitives, put conditionals around PRAD? or URAD? variables to if want to be able to set EOMRADTYPE to EOMRADNONE and have work as non-radiation problem
3669 int init_dsandvels(int inittype, int pos, int *whichvel, int*whichcoord, SFTYPE time, int i, int j, int k, FTYPE *pr, FTYPE *pstag)
3670 {
3671  int init_dsandvels_koral(int *whichvel, int*whichcoord, int i, int j, int k, FTYPE *pr, FTYPE *pstag);
3672 
3673  // assume inittype not used, pos==CENT, and time doesn't matter (e.g. only used at t=0)
3674 
3675  init_dsandvels_koral(whichvel, whichcoord, i, j, k, pr, pstag);
3676 
3677 
3678  int set_fieldtype(void);
3679  int FIELDTYPE=set_fieldtype();
3680 
3681  if(FIELDTYPE==SPLITMONOPOLE || FIELDTYPE==MONOPOLE || FIELDTYPE==FIELDWALD){
3682  // generate field with same whichcoord as init_dsandvels_koral() set
3683  int whichmethod=0;
3684  int whichinversion=0;
3685  fieldprim(whichmethod, whichinversion, whichvel, whichcoord, i, j, k, pr); // assume pstag set as average of pr
3686  }
3687 
3688 
3689  // assume any floor set at t=0 is part of solution
3690  if(DOYFL){
3691  FTYPE rhofloor=pr[RHO]*NUMEPSILON*10.0;
3692  FTYPE vfloor=NUMEPSILON*10.0;
3693  FTYPE enfloor=ERADLIMIT+(pr[URAD0]+pr[UU])*NUMEPSILON*10.0;
3694  if(YFL1>=0) pr[YFL1] = SMALL + rhofloor; // rho floor
3695  if(YFL2>=0) pr[YFL2] = SMALL + rhofloor*vfloor*vfloor; // -T^t_t-rho u^r floor
3696  if(YFL3>=0) pr[YFL3] = SMALL + rhofloor*vfloor; // T^t_phi floor
3697  if(YFL4>=0) pr[YFL4] = SMALL + enfloor; // -R^t_t floor
3698  if(YFL5>=0) pr[YFL5] = SMALL + enfloor*vfloor; // R^t_\phi floor
3699  }
3700 
3701  return(0);
3702 }
3703 
3704 
3705 // unnormalized density
3706 int init_dsandvels_koral(int *whichvel, int*whichcoord, int i, int j, int k, FTYPE *pr, FTYPE *pstag)
3707 {
3708  FTYPE X[NDIM],V[NDIM];
3709  int pl,pliter;
3710 
3711  // coord(i, j, k, CENT, X);
3712  // bl_coord(X, V);
3713  // r=V[1];
3714  // th=V[2];
3715 
3716  /*************************************************/
3717  /*************************************************/
3718  /*************************************************/
3719  if(WHICHPROBLEM==FLATNESS){
3720 
3721 
3722  pr[RHO] = 1./RHOBAR ; // i.e. 1g/cm^3
3723  pr[UU] = 0.1/RHOBAR; // i.e. c^2 * 1g/cm^3 of energy density
3724  pr[U1] = 0 ;
3725  pr[U2] = 0 ;
3726  pr[U3] = 0 ;
3727 
3728  // just define some field
3729  pr[B1]=0.0;
3730  pr[B2]=0.0;
3731  pr[B3]=0.0;
3732 
3733  if(FLUXB==FLUXCTSTAG){
3734  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
3735  PLOOPBONLY(pl) pstag[pl]=pr[pl];
3736  }
3737 
3738  if(PRAD0>=0){
3739  pr[URAD0] = 1./RHOBAR; // i.e. c^2 * 1g/cm^3 of energy density
3740  pr[URAD1] = 0 ;
3741  pr[URAD2] = 0 ;
3742  pr[URAD3] = 0 ;
3743  }
3744 
3745  *whichvel=WHICHVEL;
3746  *whichcoord=CARTMINKMETRIC2;
3747 
3748  // KORALTODO: no transformation for radiation. Would give same result as assuming in fluid frame because vfluid=0 here and F=0 here.
3749 
3750  return(0);
3751  }
3752 
3753 
3754 
3755 
3756 
3757  /*************************************************/
3758  /*************************************************/
3760 
3761 
3762  pr[RHO] = RADBEAMFLAT_RHO ;
3763  pr[UU] = RADBEAMFLAT_UU;
3764  pr[U1] = 0 ;
3765  pr[U2] = 0 ;
3766  pr[U3] = 0 ;
3767 
3768  // just define some field
3769  pr[B1]=0.0;
3770  pr[B2]=0.0;
3771  pr[B3]=0.0;
3772 
3773  if(FLUXB==FLUXCTSTAG){
3774  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
3775  PLOOPBONLY(pl) pstag[pl]=pr[pl];
3776  }
3777 
3778 
3779 
3780  if(PRAD0>=0){
3781  // new way: correctly transform -- also how koral currently setup
3782  //E, F^i in orthonormal fluid frame
3783  FTYPE pradffortho[NPR];
3784  pradffortho[PRAD0] = RADBEAMFLAT_ERAD;
3785  pradffortho[PRAD1] = 0;
3786  pradffortho[PRAD2] = 0;
3787  pradffortho[PRAD3] = 0;
3788 
3789 
3790  // Transform these fluid frame E,F^i to lab frame coordinate basis primitives
3791  *whichvel=VEL4;
3792  *whichcoord=MCOORD;
3793  prad_fforlab(whichvel, whichcoord, FF2LAB, i,j,k,CENT,NULL,pradffortho,pr, pr);
3794  }
3795  else if(PRAD0>=0){
3796  // old way: don't transform, leave as radiation frame E.
3797  pr[PRAD0] = RADBEAMFLAT_ERAD;
3798  pr[PRAD1] = 0 ;
3799  pr[PRAD2] = 0 ;
3800  pr[PRAD3] = 0 ;
3801  *whichvel=WHICHVEL;
3802  *whichcoord=MCOORD;
3803  }
3804  else{
3805  *whichvel=WHICHVEL;
3806  *whichcoord=MCOORD;
3807  }
3808 
3809  return(0);
3810  }
3811 
3812 
3813 
3814 
3815  /*************************************************/
3816  /*************************************************/
3818  // now avoids use of real units as in Koral paper (unlike Koral code)
3819 
3820  FTYPE Trad,Tgas,ERAD,uint;
3821  FTYPE xx,yy,zz,rsq;
3822  coord(i, j, k, CENT, X);
3823  bl_coord(X, V);
3824  xx=V[1];
3825  yy=V[2];
3826  zz=V[3];
3827 
3828 
3830  rsq=(xx)*(xx)+(yy)*(yy)+(zz)*(zz);
3831  }
3832  else if(WHICHPROBLEM==RADPULSEPLANAR){
3833  rsq=(xx)*(xx);
3834 
3835  }
3836 
3837  //FTYPE RHO_AMB (1.e0) // in grams per cm^3
3838  // FTYPE RHO_AMB=(MPERSUN*MSUN/(LBAR*LBAR*LBAR)); // in grams per cm^3 to match koral's units with rho=1
3839  FTYPE RHO_AMB=1.0;
3840  FTYPE T_AMB=1.0E6/TEMPBAR;
3841 
3842  FTYPE BLOBP=100.;
3843  FTYPE BLOBW=5.;
3844 
3845  // radiation temperature is distributed
3846  Trad=T_AMB*(1.+BLOBP*exp(-rsq/(BLOBW*BLOBW)));
3847  ERAD=calc_LTE_EfromT(Trad);
3848 
3849  //flat gas profiles
3850  Tgas=T_AMB;
3851  FTYPE rho;
3852  rho=RHO_AMB;
3853  uint=calc_PEQ_ufromTrho(Tgas,rho);
3854 
3855  // dualfprintf(fail_file,"IC i=%d Trad=%g ERAD=%g Tgas=%g rho=%g uint=%g\n",i,Trad,ERAD,Tgas,rho,uint);
3856 
3857 
3858  pr[RHO] = rho;
3859  pr[UU] = uint;
3860  pr[U1] = 0 ;
3861  pr[U2] = 0 ;
3862  pr[U3] = 0 ;
3863 
3864  // just define some field
3865  pr[B1]=0.0;
3866  pr[B2]=0.0;
3867  pr[B3]=0.0;
3868 
3869  if(FLUXB==FLUXCTSTAG){
3870  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
3871  PLOOPBONLY(pl) pstag[pl]=pr[pl];
3872  }
3873 
3874  FTYPE Fx,Fy,Fz;
3875  Fx=Fy=Fz=0.0;
3876 
3877  if(PRAD0>=0){
3878  pr[URAD0] = ERAD ;
3879  pr[URAD1] = Fx ;
3880  pr[URAD2] = Fy ;
3881  pr[URAD3] = Fz ;
3882  }
3883 
3884  // KORALTODO: no transformation, but only because tuned units to be like koral and so ERAD gives same value and also because no Flux. Also, would give same result as assuming in fluid frame because vfluid=0 here and F=0 here.
3885 
3886  *whichvel=WHICHVEL;
3887  *whichcoord=CARTMINKMETRIC2;
3888  return(0);
3889  }
3890 
3891 
3892 
3893  /*************************************************/
3894  /*************************************************/
3895  if(WHICHPROBLEM==RADTUBE){
3896 
3897  FTYPE rho,mx,my,mz,m,ERAD,uint,E0,Fx,Fy,Fz,pLTE;
3898  FTYPE rho0,Tgas0,ur,Tgas,Trad,r,rcm,prad,pgas,vx,ut,ux;
3899  FTYPE xx,yy,zz;
3900  coord(i, j, k, CENT, X);
3901  bl_coord(X, V);
3902  xx=V[1];
3903  yy=V[2];
3904  zz=V[3];
3905 
3906 
3907  // set fluid values. Also set radiation ff primitives (E,F^i), which are set in fluid frame orthonormal basis
3908  if(xx<(Rout_array[1]+Rin_array[1])/2.){
3909  rho=1.;
3910  if(NTUBE==1) {uint = 3.e-5 / (gamideal - 1.); ERAD=1.e-8; Fx=1.e-2*ERAD;ux=0.015;}
3911  if(NTUBE==2) {uint = 4.e-3 / (gamideal - 1.);ERAD=2.e-5; Fx=1.e-2*ERAD;ux=0.25;}
3912  if(NTUBE==3 || NTUBE==31) {uint = 60. / (gamideal - 1.);ERAD=2.; Fx=1.e-2*ERAD;ux=10.;}
3913  if(NTUBE==4 || NTUBE==41) {uint = 6.e-3 / (gamideal - 1.);ERAD=0.18; Fx=1.e-2*ERAD;ux=0.69;}
3914  if(NTUBE==5) {uint = 60. / (gamideal - 1.);ERAD=2.; Fx=1.e-2*ERAD;ux=1.25;}
3915  }
3916  else{
3917  if(NTUBE==1) {rho=2.4;uint = 1.61e-4/ (gamideal - 1.); ERAD=2.51e-7; Fx=1.e-2*ERAD;ux=6.25e-3;}
3918  if(NTUBE==2) {rho=3.11;uint = 0.04512 / (gamideal - 1.);ERAD=3.46e-3; Fx=1.e-2*ERAD;ux=0.0804;}
3919  if(NTUBE==3 || NTUBE==31) {rho=8.0;uint = 2.34e3 / (gamideal - 1.);ERAD=1.14e3; Fx=1.e-2*ERAD;ux=1.25;}
3920  if(NTUBE==4 || NTUBE==41) {rho=3.65;uint =3.59e-2 / (gamideal - 1.);ERAD=1.30; Fx=1.e-2*ERAD;ux=0.189;}
3921  if(NTUBE==5) {rho=1.0;uint = 60. / (gamideal - 1.);ERAD=2.; Fx=1.e-2*ERAD;ux=1.10;}
3922  }
3923  Fy=Fz=0.0;
3924 
3925 
3926  pr[RHO] = rho;
3927  pr[UU] = uint;
3928  pr[U1] = ux ; // ux is 4-velocity
3929  pr[U2] = 0 ;
3930  pr[U3] = 0 ;
3931 
3932  // just define some field
3933  pr[B1]=0.0;
3934  pr[B2]=0.0;
3935  pr[B3]=0.0;
3936 
3937  if(FLUXB==FLUXCTSTAG){
3938  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
3939  PLOOPBONLY(pl) pstag[pl]=pr[pl];
3940  }
3941 
3942  *whichvel=VEL4;
3943  *whichcoord=CARTMINKMETRIC2;
3944 
3945  if(PRAD0>=0){
3946  pr[PRAD0] = 0 ; // so triggers failure if used
3947  pr[PRAD1] = 0 ;
3948  pr[PRAD2] = 0 ;
3949  pr[PRAD3] = 0 ;
3950 
3951 
3952 
3953  //E, F^i in orthonormal fluid frame
3954  FTYPE pradffortho[NPR];
3955  pradffortho[PRAD0] = ERAD;
3956  pradffortho[PRAD1] = Fx;
3957  pradffortho[PRAD2] = Fy;
3958  pradffortho[PRAD3] = Fz;
3959 
3960 
3961  // Transform these fluid frame E,F^i to lab frame coordinate basis primitives
3962  prad_fforlab(whichvel, whichcoord, FF2LAB, i,j,k,CENT,NULL,pradffortho,pr, pr);
3963 
3964  // PLOOPRADONLY(pl) dualfprintf(fail_file,"FOO1: i=%d pl=%d pr=%g\n",ptrgeomreal->i,pl,pr[pl]);
3965 
3966  }
3967 
3968 
3969 
3970  return(0);
3971 
3972 
3973  }
3974 
3975 
3976  /*************************************************/
3977  /*************************************************/
3979 
3980  // FTYPE MASS=10.0;
3981  FTYPE TAMB=1.e7/TEMPBAR;
3982  FTYPE RHOAMB=1.e-4;
3983  FTYPE RHOBLOB=1.e3;
3984  FTYPE BLOBW=0.22;
3985 
3986  FTYPE Trad,Tgas,ERAD;
3987  FTYPE xx,yy,zz,rsq;
3988  coord(i, j, k, CENT, X);
3989  bl_coord(X, V);
3990  xx=V[1];
3991  yy=V[2];
3992  zz=V[3];
3993 
3994  FTYPE rho,uint,Fx,Fy,Fz,pLTE;
3995 
3996  /*****************************/
3997 
3998  // FTYPE pamb=calc_PEQ_ufromTrho(TAMB,RHOAMB);
3999  rsq=xx*xx+yy*yy+zz*zz;
4000  rho=(RHOBLOB-RHOAMB)*exp(-sqrt(rsq)/(BLOBW*BLOBW))+RHOAMB;
4001  Tgas=TAMB*RHOAMB/rho;
4002  // Paper says T = T0*rho/RHOAMB
4003  // Tgas = TAMB*rho/RHOAMB;
4004  // for constant gas pressure, P=\rho T implies rho T = constant so that T\propto 1/rho
4005  uint=calc_PEQ_ufromTrho(Tgas,rho);
4006 
4007  Trad=TAMB;
4008  ERAD=calc_LTE_EfromT(Trad);
4009 
4010  // dualfprintf(fail_file,"i=%d j=%d rho=%g Trad=%g uint=%g ERAD=%g\n",i,j,rho,Trad,uint,ERAD);
4011 
4012  Fx=0.;
4013  Fy=0.;
4014  Fz=0.;
4015 
4016  FTYPE VV=0.; // assumed orthonormal 4-velocity
4017 
4018  pr[RHO] = rho;
4019  pr[UU] = uint;
4020  pr[U1] = -VV ;
4021  pr[U2] = 0 ;
4022  pr[U3] = 0 ;
4023 
4024  // just define some field
4025  pr[B1]=0.0;
4026  pr[B2]=0.0;
4027  pr[B3]=0.0;
4028 
4029  if(FLUXB==FLUXCTSTAG){
4030  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
4031  PLOOPBONLY(pl) pstag[pl]=pr[pl];
4032  }
4033 
4034  *whichvel=VEL4;
4035  *whichcoord=CARTMINKMETRIC2;
4036 
4037 
4038  if(PRAD0>=0){
4039  pr[PRAD0] = 0 ; // so triggers failure if used
4040  pr[PRAD1] = 0 ;
4041  pr[PRAD2] = 0 ;
4042  pr[PRAD3] = 0 ;
4043 
4044  //E, F^i in orthonormal fluid frame
4045  FTYPE pradffortho[NPR];
4046  pradffortho[PRAD0] = ERAD;
4047  pradffortho[PRAD1] = Fx;
4048  pradffortho[PRAD2] = Fy;
4049  pradffortho[PRAD3] = Fz;
4050 
4051  // Transform these fluid frame E,F^i to lab frame coordinate basis primitives
4052  prad_fforlab(whichvel, whichcoord, FF2LAB, i,j,k,CENT,NULL,pradffortho,pr, pr);
4053  // PLOOP(pliter,pl) dualfprintf(fail_file,"pl=%d pr=%g\n",pl,pr[pl]);
4054 
4055  }
4056 
4057 
4058  return(0);
4059  }
4060 
4061 
4062 
4063  /*************************************************/
4064  /*************************************************/
4066 
4067 
4068  RADBEAM2D_RHOAMB=1.e0/RHOBAR;
4069  RADBEAM2D_TAMB=1e7/TEMPBAR;
4070  RADBEAM2D_BLOB=0; // whether to put blob in way of beam
4071  RADBEAM2D_BLOBW=.1;
4072  RADBEAM2D_BLOBP=100000.;
4073  RADBEAM2D_BLOBX=10.;
4074  RADBEAM2D_BLOBZ=Pi/20.;
4076  RADBEAM2D_PAR_E=1e-4/RHOBAR;
4077 
4078  // BEAM PROPERTIES
4079  RADBEAM2D_IFBEAM=1; // whether to have a beam
4081  // RADBEAM2D_NLEFT=0.95; // >~0.95 and code fails with SPCMINKMETRIC for BEAMNO=1
4082  // RADBEAM2D_NLEFT=0.99; // testing GODMARK KORALTODO
4083  //RADBEAM2D_NLEFT=0.999; // code
4084  // RADBEAM2D_NLEFT=0.99999; // paper // major problems with SPCMINKMETRIC
4085  RADBEAM2D_NLEFT=0.9999; // works with harmrad paper setup with PPM
4086 
4087  // avoid hitting gamma ceiling
4089 
4090 
4091  if (RADBEAM2D_BEAMNO==1){
4092  RADBEAM2D_BEAML=2.9;
4093  RADBEAM2D_BEAMR=3.1;
4094  }
4095  else if (RADBEAM2D_BEAMNO==2){
4096  RADBEAM2D_BEAML=5.8;
4097  RADBEAM2D_BEAMR=6.2;
4098  }
4099  else if (RADBEAM2D_BEAMNO==3){
4100  RADBEAM2D_BEAML=15.5;
4101  RADBEAM2D_BEAMR=16.5;
4102  }
4103  else if (RADBEAM2D_BEAMNO==4){
4104  RADBEAM2D_BEAML=37;
4105  RADBEAM2D_BEAMR=43;
4106  }
4107 
4108 
4109  FTYPE Fx,Fy,Fz;
4110 
4111  FTYPE xx,yy,zz,rsq;
4112  coord(i, j, k, CENT, X);
4113  bl_coord(X, V);
4114  xx=V[1];
4115  yy=V[2];
4116  zz=V[3];
4117 
4118  *whichvel=VEL4;
4119  *whichcoord=MCOORD;
4120 
4121  // if BLOB, override density with blob density
4122  FTYPE rhoblob;
4124 
4125  //zaczynam jednak od profilu analitycznego:
4126  FTYPE ERADAMB;
4127  FTYPE rho,uint,Vr;
4129  Vr=0.;
4130  rho=RADBEAM2D_RHOAMB;
4131  if(RADBEAM2D_BLOB) rho=rhoblob;
4134  // ERADAMB=calc_LTE_Efromurho(uint,rho);
4135  }
4136  else{
4137  FTYPE r=V[1];
4138  FTYPE mD=RADBEAM2D_PAR_D/(r*r*sqrt(2./r*(1.-2./r)));
4139  FTYPE mE=RADBEAM2D_PAR_E/(pow(r*r*sqrt(2./r),gamideal)*pow(1.-2./r,(gamideal+1.)/4.));
4140  Vr=sqrt(2./r)*(1.-2./r);
4141 
4142  // get metric grid geometry for these ICs
4143  int getprim=0;
4144  struct of_geom geomrealdontuse;
4145  struct of_geom *ptrgeomreal=&geomrealdontuse;
4146  gset(getprim,*whichcoord,i,j,k,ptrgeomreal);
4147 
4148  FTYPE W=1./sqrt(1.-Vr*Vr*ptrgeomreal->gcov[GIND(1,1)]);
4149  rho=RADBEAM2D_PAR_D/(r*r*sqrt(2./r));
4150  if(RADBEAM2D_BLOB) rho += rhoblob;
4152  // FTYPE ERAD=calc_LTE_EfromT(T);
4153  uint=mE/W;
4154  ERADAMB=calc_LTE_Efromurho(uint,rho);
4155  }
4156 
4157  Fx=Fy=Fz=0;
4158 
4159  //test!
4160  //Vr=0.7;
4161 
4162  pr[RHO] = rho ;
4163  pr[UU] = uint;
4164  pr[U1] = -Vr; // VEL4
4165  pr[U2] = 0 ; // static in VEL4
4166  pr[U3] = 0 ; // static in VEL4
4167 
4168  // just define some field
4169  pr[B1]=0.0;
4170  pr[B2]=0.0;
4171  pr[B3]=0.0;
4172 
4173  if(FLUXB==FLUXCTSTAG){
4174  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
4175  PLOOPBONLY(pl) pstag[pl]=pr[pl];
4176  }
4177 
4178  if(PRAD0>=0){
4179  pr[PRAD0] = ERADAMB;
4180  pr[PRAD1] = 0.0 ; // static in VEL4
4181  pr[PRAD2] = 0.0 ;
4182  pr[PRAD3] = 0.0 ;
4183 
4184  //E, F^i in orthonormal fluid frame
4185  FTYPE pradffortho[NPR];
4186  pradffortho[PRAD0] = ERADAMB;
4187  pradffortho[PRAD1] = Fx;
4188  pradffortho[PRAD2] = Fy;
4189  pradffortho[PRAD3] = Fz;
4190 
4191  // Transform these fluid frame E,F^i to lab frame coordinate basis primitives
4192  prad_fforlab(whichvel, whichcoord, FF2LAB, i,j,k,CENT,NULL,pradffortho,pr, pr);
4193  }
4194 
4195  return(0);
4196  }
4197 
4198  /*************************************************/
4199  /*************************************************/
4200  if(WHICHPROBLEM==ATMSTATIC){
4201 
4202  FTYPE xx,yy,zz,rsq;
4203  coord(i, j, k, CENT, X);
4204  bl_coord(X, V);
4205  xx=V[1];
4206  yy=V[2];
4207  zz=V[3];
4208 
4209  *whichvel=VEL4;
4210  *whichcoord=MCOORD;
4211 
4212 
4213  FTYPE rho0=1.;
4214  FTYPE r0=2.e6;
4215  FTYPE u0=0.0001;
4216  FTYPE r=xx;
4217 
4218  FTYPE rho,uint;
4219  rho=rho0*r0/r;
4220  uint=u0*r0*r0/r/r;
4221 
4222  // FTYPE E=exp(1.);
4223 
4224  uint=(4*r*u0 - 4*r*u0*gamideal - 2*r*rho0 + 2*r0*rho0 + r*r0*rho0*Log(-2 + r) - r*r0*rho0*Log(r) - r*r0*rho0*Log(-2 + r0) + r*r0*rho0*Log(r0))/(4*r - 4*r*gamideal);
4225 
4226  FTYPE uradx,urady,uradz;
4227  uradx=urady=uradz=0.;
4228 
4229  pr[RHO] = rho ;
4230  pr[UU] = uint;
4231  pr[U1] = 0 ;
4232  pr[U2] = 0 ;
4233  pr[U3] = 0 ;
4234 
4235  // just define some field
4236  pr[B1]=0.0;
4237  pr[B2]=0.0;
4238  pr[B3]=0.0;
4239 
4240  if(FLUXB==FLUXCTSTAG){
4241  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
4242  PLOOPBONLY(pl) pstag[pl]=pr[pl];
4243  }
4244 
4245 
4246  if(PRAD0>=0){
4247  // pr[PRAD0] = ERADLIMIT;
4248  pr[PRAD0] = uint*1E-20;
4249  pr[PRAD1] = uradx ;
4250  pr[PRAD2] = urady ;
4251  pr[PRAD3] = uradz ;
4252  }
4253  // no transformations required since only setting fluid-frame E that is PRAD0 itself. (i.e. urad(xyz)=0 and ufluid=0)
4254 
4255  // *whichvel=WHICHVEL;
4256  *whichvel=VEL4;
4257  *whichcoord=MCOORD;
4258 
4259  return(0);
4260  }
4261 
4262 
4263  /*************************************************/
4264  /*************************************************/
4265  if(WHICHPROBLEM==RADATM){
4266 
4267 
4268  // KORALTODO: t=0 R^t_t [lab] R^t_x [lab] or prad0 (E rad frame] prad1 [rad 4-vel] are off by about 16% compared to koral UNITS CONSTANTS.
4269 
4270  RADATM_MDOTEDD=(2.23/16.*1e18*MPERSUN)/(MBAR/TBAR);
4271  RADATM_LUMEDD=(1.25e38*MPERSUN)/(ENBAR/TBAR);
4273  // RADATM_FERATIO=.99999; // koral code
4274  RADATM_FERATIO=.99; // koral paper
4275 
4276  // avoid hitting gamma ceiling
4278 
4279 
4280 #define WHICHRADATM 0 // 0,1,2,3
4281 
4282  if(WHICHRADATM==0){
4283  RADATM_FRATIO=1E-10; // 1 = edd limit. They ran 1E-10, 0.1, 0.5, 1.0.
4284  }
4285  if(WHICHRADATM==1){
4286  RADATM_FRATIO=.1; // 1 = edd limit. They ran 1E-10, 0.1, 0.5, 1.0.
4287  }
4288  if(WHICHRADATM==2){
4289  RADATM_FRATIO=.5; // 1 = edd limit. They ran 1E-10, 0.1, 0.5, 1.0.
4290  }
4291  if(WHICHRADATM==3){
4292  RADATM_FRATIO=1.0; // 1 = edd limit. They ran 1E-10, 0.1, 0.5, 1.0.
4293  }
4294 
4295 
4296  RADATM_RHOAMB=1E-15/RHOBAR; // 1E-15 is in cgs
4297  RADATM_TAMB=1.e6/TEMPBAR;
4298 
4299 
4300  FTYPE MINX=Rin_array[1];
4301  FTYPE kappaesperrho=calc_kappaes_user(1,0, 0,0,0);
4302  FTYPE FLUXLEFT=RADATM_FRATIO/kappaesperrho/pow(MINX,2.0);
4303 
4304 
4305 
4306  FTYPE xx,yy,zz,rsq;
4307  coord(i, j, k, CENT, X);
4308  bl_coord(X, V);
4309  xx=V[1];
4310  yy=V[2];
4311  zz=V[3];
4312 
4313  *whichvel=VEL4;
4314  *whichcoord=MCOORD;
4315 
4316  //at outern boundary
4317  FTYPE f;
4319  f = (FTYPE)kappaesperrho*FLUXLEFT*MINX*MINX;
4320  }
4321  else f=0;
4322 
4323 
4325  FTYPE KKK=p0/pow(RADATM_RHOAMB,gamideal);
4326  FTYPE C3=gamideal*KKK/(gamideal-1.)*pow(RADATM_RHOAMB,gamideal-1.)-(1.-f)*(1./MINX+0.*1./MINX/MINX+0.*4./3./MINX/MINX/MINX);
4327 
4328  FTYPE rho=pow((gamideal-1.0)/gamideal/KKK*(C3+(1.-f)*(1./xx + 0.*1./xx/xx + 0.*4./3./xx/xx/xx)),1./(gamideal-1.0));
4329 
4330  FTYPE pre=KKK*pow(rho,gamideal);
4331 
4332  FTYPE uint=pre/(gamideal-1.0);
4333 
4334  FTYPE Fz=0;
4335  FTYPE Fy=0.;
4336  FTYPE Fx=FLUXLEFT*(MINX/xx)*(MINX/xx);
4337 
4338  FTYPE ERAD;
4339  if(RADATM_THINRADATM){
4340  ERAD=Fx/RADATM_FERATIO;
4341  }
4342  else{
4343  ERAD=calc_LTE_EfromT(calc_PEQ_Tfromurho(uint,rho));
4344  }
4345 
4346  if(0){//DEBUG:
4347  dualfprintf(fail_file,"i=%d f=%g p0=%g KKK=%g C3=%g rho=%g uint=%g Fx=%g ERAD=%g : kappaesperrho=%g \n",i,f,p0,KKK,C3,rho,uint,Fx,ERAD , kappaesperrho);
4348 
4349  dualfprintf(fail_file,"i=%d f=%g p0=%g KKK=%g C3=%g rho=%g uint=%g Fx=%g ERAD=%g : kappaesperrho=%g \n",i,f,p0*UBAR,KKK*UBAR/pow(RHOBAR,gamideal),C3,rho*RHOBAR,uint*UBAR,Fx*ENBAR/TBAR/LBAR/LBAR,ERAD*UBAR , kappaesperrho*OPACITYBAR);
4350  }
4351 
4352 
4353 
4354  pr[RHO] = rho ;
4355  pr[UU] = uint;
4356  pr[U1] = 0 ;
4357  pr[U2] = 0 ;
4358  pr[U3] = 0 ;
4359 
4360  // just define some field
4361  pr[B1]=0.0;
4362  pr[B2]=0.0;
4363  pr[B3]=0.0;
4364 
4365  if(FLUXB==FLUXCTSTAG){
4366  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
4367  PLOOPBONLY(pl) pstag[pl]=pr[pl];
4368  }
4369 
4370 
4371  *whichvel=VEL4;
4372  *whichcoord=MCOORD;
4373 
4374  if(PRAD0>=0){
4375  pr[PRAD0] = 0 ; // so triggers failure if used
4376  pr[PRAD1] = 0 ;
4377  pr[PRAD2] = 0 ;
4378  pr[PRAD3] = 0 ;
4379 
4380  //E, F^i in orthonormal fluid frame
4381  FTYPE pradffortho[NPR];
4382  pradffortho[PRAD0] = ERAD;
4383  pradffortho[PRAD1] = Fx;
4384  pradffortho[PRAD2] = Fy;
4385  pradffortho[PRAD3] = Fz;
4386 
4387  // Transform these fluid frame E,F^i to lab frame coordinate basis primitives
4388  prad_fforlab(whichvel, whichcoord, FF2LAB, i,j,k,CENT,NULL,pradffortho, pr, pr);
4389 
4390  if(0){ // DEBUG
4391 
4392  // get metric grid geometry for these ICs
4393  int getprim=0;
4394  struct of_geom geomrealdontuse;
4395  struct of_geom *ptrgeomreal=&geomrealdontuse;
4396  gset(getprim,*whichcoord,i,j,k,ptrgeomreal);
4397 
4398  dualfprintf(fail_file,"AFTER: i=%d rho=%g uint=%g vx=%g ERAD=%g uradx=%g\n",i,pr[RHO]*RHOBAR,pr[UU]*UBAR,pr[U1]*sqrt(ptrgeomreal->gcov[GIND(1,1)])*VBAR,pr[URAD0]*UBAR,pr[URAD1]*sqrt(ptrgeomreal->gcov[GIND(1,1)])*VBAR);
4399  }
4400 
4401  // compared to koral, this is how koral would get CGS:
4402  // fprintf(stderr,"i=%d f=%g p0=%g KKK=%g C3=%g rho=%g uint=%g Fx=%g ERAD=%g : kappaesperrho=%g \n",ix,f,endenGU2CGS(p0),endenGU2CGS(KKK)/powl(rhoGU2CGS(1.0),GAMMA),C3,rhoGU2CGS(pp[0]),endenGU2CGS(pp[1]),fluxGU2CGS(Fx), endenGU2CGS(E) , kappaGU2CGS(KAPPAES));
4403  // fprintf(stderr,"AFTER: i=%d rho=%g uint=%g vx=%g ERAD=%g uradx=%g\n",ix,rhoGU2CGS(pp[0]),endenGU2CGS(pp[1]),velGU2CGS(pp[2]),endenGU2CGS(pp[6]),velGU2CGS(pp[7]));
4404 
4405 
4406  }
4407 
4408 
4409 
4410 
4411 
4412 
4413 
4414  return(0);
4415  }
4416 
4417 
4418 
4419  /*************************************************/
4420  /*************************************************/
4421  if(WHICHPROBLEM==RADWALL){
4422 
4423 
4424  // direct assignments since simple
4425  pr[RHO] = 1.0 ;
4426  pr[UU] = 1.0;
4427  pr[U1] = 0 ;
4428  pr[U2] = 0 ;
4429  pr[U3] = 0 ;
4430 
4431  // just define some field
4432  pr[B1]=0.0;
4433  pr[B2]=0.0;
4434  pr[B3]=0.0;
4435 
4436  if(FLUXB==FLUXCTSTAG){
4437  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
4438  PLOOPBONLY(pl) pstag[pl]=pr[pl];
4439  }
4440 
4441 
4442  if(PRAD0>=0){
4443  // direct assignments since simple
4444  pr[PRAD0] = 1.0;
4445  pr[PRAD1] = 0 ;
4446  pr[PRAD2] = 0 ;
4447  pr[PRAD3] = 0 ;
4448  }
4449 
4450  // no transformations required since only setting fluid-frame E that is PRAD0 itself since ufluid=F=0
4451 
4452  *whichvel=WHICHVEL;
4453  *whichcoord=CARTMINKMETRIC2;
4454  return(0);
4455  }
4456 
4457 
4458 
4459 
4460  /*************************************************/
4461  /*************************************************/
4462  if(WHICHPROBLEM==RADWAVE){
4463 
4464  FTYPE rho,ERAD,uint,Fx,Fy,Fz;
4465  FTYPE vx;
4466  FTYPE xx,yy,zz;
4467  coord(i, j, k, CENT, X);
4468  bl_coord(X, V);
4469  xx=V[1];
4470  yy=V[2];
4471  zz=V[3];
4472 
4473 
4474  // default
4475  Fx=Fy=Fz=0.0;
4476 
4477 
4478  FTYPE time=0.;
4479 
4480  //Jiang+12 waves
4481  if(RADWAVE_NWAVE==5){
4482 
4483  //printf("RHOZERO = %g\nUINT = %g\nT = %g\nERAD = %g\nARAD = %g\n",RADWAVE_RHOZERO,RADWAVE_UINT,RADWAVE_TEMP,RADWAVE_ERAD,ARAD_RAD_CODE);getchar();
4484 
4485 
4487  //FTYPE RADWAVE_DURE=RADWAVE_DPRE/(gam-1.); FTYPE RADWAVE_DUIM=RADWAVE_DPIM/(gam-1.);
4489  FTYPE cs=1/RADWAVE_CC;
4490  vx=0. + RADWAVE_DVRE*exp(-RADWAVE_OMIM*time)*(cos(RADWAVE_OMRE*time-RADWAVE_KK*xx)-RADWAVE_DVIM/RADWAVE_DVRE*sin(RADWAVE_OMRE*time-RADWAVE_KK*xx)) ; //RADWAVE_DVRE absolute!
4493  Fz=Fy=0.;
4494 
4495  //rho=RADWAVE_RHOZERO;
4496  //uint=RADWAVE_UINT;
4497  //ERAD=RADWAVE_ERAD;
4498  //vx=0;
4499  //Fx=0.;
4500  }
4501 
4502  //hydro density wave
4503  if(RADWAVE_NWAVE==1){
4504  rho=RADWAVE_RHOZERO*(1.+RADWAVE_AAA*cos(RADWAVE_KK*xx));
4505  uint=RADWAVE_UINT;
4506  vx=RADWAVE_VX;
4507  ERAD=1E-10*uint; // no radiation
4508  Fx=Fz=Fy=0.;
4509  }
4510 
4511  //hydro sound wave
4512  if(RADWAVE_NWAVE==2){
4513  rho=RADWAVE_RHOZERO*(1.+RADWAVE_AAA*cos(RADWAVE_KK*xx));
4514  uint=RADWAVE_UINT*(1.+gam*RADWAVE_AAA*cos(RADWAVE_KK*xx));
4515  FTYPE cs=1./RADWAVE_CC;
4516  vx=RADWAVE_AAA*cos(RADWAVE_KK*xx)*cs;
4517  ERAD=RADWAVE_ERAD; // KORALTODO: Why does koral not set #define RADIATION for this test? avoid test.
4518  Fx=Fz=Fy=0.;
4519  }
4520 
4521  //radiative hydro density wave
4522  if(RADWAVE_NWAVE==3){
4523  rho=RADWAVE_RHOZERO*(1.+RADWAVE_AAA*cos(RADWAVE_KK*xx));
4524  uint=RADWAVE_UINT;
4525  vx=RADWAVE_VX;
4526  ERAD=RADWAVE_ERAD;
4527  Fx=Fz=Fy=0.;
4528  }
4529 
4530  //radiative sound wave
4531  if(RADWAVE_NWAVE==4){
4534  FTYPE cs=1./RADWAVE_CC;
4537  Fx=Fz=Fy=0.;
4538  }
4539 
4540 
4541 
4542  pr[RHO] = rho;
4543  pr[UU] = uint;
4544  pr[U1] = vx ; // vx is 3-velocity
4545  pr[U2] = 0 ;
4546  pr[U3] = 0 ;
4547 
4548  // just define some field
4549  pr[B1]=0.0;
4550  pr[B2]=0.0;
4551  pr[B3]=0.0;
4552 
4553  if(FLUXB==FLUXCTSTAG){
4554  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
4555  PLOOPBONLY(pl) pstag[pl]=pr[pl];
4556  }
4557 
4558 
4559  *whichvel=VEL3;
4560  *whichcoord=CARTMINKMETRIC2;
4561 
4562  if(PRAD0>=0){
4563  pr[PRAD0] = 0 ; // so triggers failure if used
4564  pr[PRAD1] = 0 ;
4565  pr[PRAD2] = 0 ;
4566  pr[PRAD3] = 0 ;
4567 
4568  //E, F^i in orthonormal fluid frame
4569  FTYPE pradffortho[NPR];
4570  pradffortho[PRAD0] = ERAD;
4571  pradffortho[PRAD1] = Fx;
4572  pradffortho[PRAD2] = Fy;
4573  pradffortho[PRAD3] = Fz;
4574 
4575 
4576  // Transform these fluid frame E,F^i to lab frame coordinate basis primitives
4577  prad_fforlab(whichvel, whichcoord, FF2LAB, i,j,k,CENT,NULL, pradffortho, pr, pr);
4578  // PLOOPRADONLY(pl) dualfprintf(fail_file,"FOO1: i=%d pl=%d pr=%g\n",ptrgeomreal->i,pl,pr[pl]);
4579  }
4580 
4581 
4582  return(0);
4583 
4584 
4585  }
4586 
4587 
4588 
4589 
4590 
4591 
4592  /*************************************************/
4593  /*************************************************/
4595 
4596  coord(i, j, k, CENT, X);
4597  bl_coord(X, V);
4598  FTYPE x = V[1];
4599 
4600  if(WHICHKOMI>=1 && WHICHKOMI<=9){
4601  *whichvel=VEL4;
4602  *whichcoord=CARTMINKMETRIC2;
4603 
4604  FTYPE pleft[NPR], pright[NPR], P;
4605  //zero out initial conditions
4606  PALLLOOP(pl) pleft[pl] = 0.;
4607  PALLLOOP(pl) pright[pl] = 0.;
4608 
4609 
4610  //fast shock
4611  if(WHICHKOMI==1){
4612  //left state
4613  pleft[U1] = 25.0;
4614  pleft[U2] = 0.0;
4615  pleft[U3] = 0.0;
4616  pleft[B1] = 20.0;
4617  pleft[B2] = 25.02;
4618  pleft[B3] = 0.0;
4619  P = 1.0;
4620  pleft[RHO] = 1.;
4621  pleft[UU] = P/(gam-1);
4622 
4623  //right state
4624  pright[U1] = 1.091;
4625  pright[U2] = 0.3923;
4626  pright[U3] = 0.0;
4627  pright[B1] = 20.0;
4628  pright[B2] = 49.0;
4629  pright[B3] = 0.0;
4630  P = 367.5;
4631  pright[RHO] = 25.48;
4632  pright[UU] = P/(gam-1);
4633  }
4634  //slow shock
4635  else if(WHICHKOMI==2){
4636  //left state
4637  pleft[U1] = 1.53;
4638  pleft[U2] = 0.0;
4639  pleft[U3] = 0.0;
4640  pleft[B1] = 10.0;
4641  pleft[B2] = 18.28;
4642  pleft[B3] = 0.0;
4643  P = 10.0;
4644  pleft[RHO] = 1.;
4645  pleft[UU] = P/(gam-1);
4646 
4647  //right state
4648  pright[U1] = .9571;
4649  pright[U2] = -0.6822;
4650  pright[U3] = 0.0;
4651  pright[B1] = 10.0;
4652  pright[B2] = 14.49;
4653  pright[B3] = 0.0;
4654  P = 55.36;
4655  pright[RHO] = 3.323;
4656  pright[UU] = P/(gam-1);
4657  }
4658  //fast switch-off rarefaction
4659  else if(WHICHKOMI==3){
4660  //left state
4661  pleft[U1] = -2.0;
4662  pleft[U2] = 0.0;
4663  pleft[U3] = 0.0;
4664  pleft[B1] = 2.0;
4665  pleft[B2] = 0.0;
4666  pleft[B3] = 0.0;
4667  P = 1.0;
4668  pleft[RHO] = 0.1;
4669  pleft[UU] = P/(gam-1);
4670 
4671  //right state
4672  pright[U1] = -0.212;
4673  pright[U2] = -0.590;
4674  pright[U3] = 0.0;
4675  pright[B1] = 2.0;
4676  pright[B2] = 4.710;
4677  pright[B3] = 0.0;
4678  P = 10.0;
4679  pright[RHO] = 0.562;
4680  pright[UU] = P/(gam-1);
4681  }
4682  //slow switch-on rarefaction
4683  else if(WHICHKOMI==4){
4684  //left state
4685  pleft[U1] = -0.765;
4686  pleft[U2] = -1.386;
4687  pleft[U3] = 0.0;
4688  pleft[B1] = 1.0;
4689  pleft[B2] = 1.022;
4690  pleft[B3] = 0.0;
4691  P = 0.1;
4692  pleft[RHO] = 1.78e-3;
4693  pleft[UU] = P/(gam-1);
4694 
4695  //right state
4696  pright[U1] = 0.0;
4697  pright[U2] = 0.0;
4698  pright[U3] = 0.0;
4699  pright[B1] = 1.0;
4700  pright[B2] = 0.0;
4701  pright[B3] = 0.0;
4702  P = 1.0;
4703  pright[RHO] = 1.0;
4704  pright[UU] = P/(gam-1);
4705  }
4706  //alfven wave and compound wave
4707  else if(WHICHKOMI==5 || WHICHKOMI==6){
4708  //left state
4709  pleft[U1] = 0;
4710  pleft[U2] = 0;
4711  pleft[U3] = 0.0;
4712  pleft[B1] = 3.0;
4713  pleft[B2] = 3.0;
4714  pleft[B3] = 0.0;
4715  P = 1.0;
4716  pleft[RHO] = 1.0;
4717  pleft[UU] = P/(gam-1);
4718 
4719  //right state
4720  pright[U1] = 3.70;
4721  pright[U2] = 5.76;
4722  pright[U3] = 0.0;
4723  pright[B1] = 3.0;
4724  pright[B2] = -6.857;
4725  pright[B3] = 0.0;
4726  P = 1.0;
4727  pright[RHO] = 1.0;
4728  pright[UU] = P/(gam-1);
4729  }
4730  //Shock tube 1
4731  else if(WHICHKOMI==7){
4732  //left state
4733  pleft[U1] = 0.0;
4734  pleft[U2] = 0.0;
4735  pleft[U3] = 0.0;
4736  pleft[B1] = 1.0;
4737  pleft[B2] = 0.0;
4738  pleft[B3] = 0.0;
4739  P = 1000.0;
4740  pleft[RHO] = 1.0;
4741  pleft[UU] = P/(gam-1);
4742 
4743  //right state
4744  pright[U1] = 0.0;
4745  pright[U2] = 0.0;
4746  pright[U3] = 0.0;
4747  pright[B1] = 1.0;
4748  pright[B2] = 0.0;
4749  pright[B3] = 0.0;
4750  P = 1.0;
4751  pright[RHO] = 0.1;
4752  pright[UU] = P/(gam-1);
4753  }
4754  //Shock tube 2
4755  else if(WHICHKOMI==8){
4756  //left state
4757  pleft[U1] = 0.0;
4758  pleft[U2] = 0.0;
4759  pleft[U3] = 0.0;
4760  pleft[B1] = 0.0;
4761  pleft[B2] = 20.0;
4762  pleft[B3] = 0.0;
4763  P = 30.;
4764  pleft[RHO] = 1.0;
4765  pleft[UU] = P/(gam-1);
4766 
4767  //right state
4768  pright[U1] = 0.0;
4769  pright[U2] = 0.0;
4770  pright[U3] = 0.0;
4771  pright[B1] = 0.0;
4772  pright[B2] = 0.0;
4773  pright[B3] = 0.0;
4774  P = 1.0;
4775  pright[RHO] = 0.1;
4776  pright[UU] = P/(gam-1);
4777  }
4778  //Collision
4779  else if(WHICHKOMI==9){
4780  //left state
4781  pleft[U1] = 5.0;
4782  pleft[U2] = 0.0;
4783  pleft[U3] = 0.0;
4784  pleft[B1] = 10.0;
4785  pleft[B2] = 10.0;
4786  pleft[B3] = 0.0;
4787  P = 1.0;
4788  pleft[RHO] = 1.0;
4789  pleft[UU] = P/(gam-1);
4790 
4791  //right state
4792  pright[U1] = -5.0;
4793  pright[U2] = 0.0;
4794  pright[U3] = 0.0;
4795  pright[B1] = 10.0;
4796  pright[B2] = -10.0;
4797  pright[B3] = 0.0;
4798  P = 1.0;
4799  pright[RHO] = 1.0;
4800  pright[UU] = P/(gam-1);
4801  }
4802 
4803  if(WHICHKOMI==5 || WHICHKOMI==6){ // Alfven wave or compound wave
4804  FTYPE xcl,xcr,xint;
4805  // xcl=xcr=0.0;
4806  if(WHICHKOMI==5){ xcl=-0.5; xcr=0.0; }
4807  if(WHICHKOMI==6){ xcl=-0.025; xcr=0.0; } // 4 cells according to figure 5 in Komi 1999, but seems to really be 2 cells unless expanded range of box relative to WHICHKOMI==5 for odd reason
4808  if(x<=xcl) PALLLOOP(pl) pr[pl] = pleft[pl];
4809  else if(x>xcr) PALLLOOP(pl) pr[pl] = pright[pl];
4810  else{
4811  // FTYPE xint=(x- xcl)/(xcr - xcl);
4812  // default left-right state values
4813  // PALLLOOP(pl) pr[pl] = pleft[pl] + (pright[pl]-pleft[pl])*xint;
4814 
4815  // but rotate field by \pi
4816  if(WHICHKOMI==5){ xcl=-0.5; xcr=0.0; }
4817  if(WHICHKOMI==6){ xcl=-0.025; xcr=0.0; } // 4 cells according to figure 5 in Komi 1999, but seems to really be 2 cells unless expanded range of box relative to WHICHKOMI==5 for odd reason
4818  FTYPE phi0;
4819  if(x<=xcl) phi0=0.0;
4820  else if((x>xcl)&&(x<xcr)) phi0=0.5*M_PI*(x-xcl)/(xcr-xcl);
4821  else if(x>=xcr) phi0=0.5*M_PI;
4822 
4823  FTYPE phi1;
4824  if(x<=xcl) phi1=0.0;
4825  else if((x>xcl)&&(x<xcr)) phi1=M_PI*(x-xcl)/(xcr-xcl);
4826  else if(x>=xcr) phi1=M_PI;
4827 
4828  // xint=sin(phi0);
4829  // rotate all by pi
4830  PALLLOOP(pl) pr[pl] = pleft[pl] + (pright[pl]-pleft[pl])*sin(phi0);
4831  // PALLLOOP(pl) if(pl==B2) pr[pl] = pleft[B2]*sin(phi1) + pright[B2]*cos(phi1);
4832  // set B3 so bsq is constant
4833 
4834  FTYPE usq = pr[U1]*pr[U1]+pr[U2]*pr[U2]+pr[U3]*pr[U3];
4835  FTYPE gamma = sqrt(1.0 + fabs(usq));
4836 
4837  FTYPE usqleft = pleft[U1]*pleft[U1]+pleft[U2]*pleft[U2]+pleft[U3]*pleft[U3];
4838  FTYPE gammaleft = sqrt(1.0 + fabs(usqleft));
4839 
4840  // see komi_fake_alfven.nb
4841  FTYPE mybsqconst=Power(pleft[B3],2)/Power(gammaleft,2) +
4842  (-1.*pleft[B1]*pleft[U1] - 1.*pleft[B2]*pleft[U2])*
4843  (pleft[B1]*pleft[U1] + pleft[B2]*pleft[U2]) +
4844  Power(pleft[B1]/gammaleft + (pleft[U1]*
4845  (pleft[B1]*pleft[U1] + pleft[B2]*pleft[U2]))/gammaleft,2) +
4846  Power(pleft[B2]/gammaleft + (pleft[U2]*
4847  (pleft[B1]*pleft[U1] + pleft[B2]*pleft[U2]))/gammaleft,2);
4848 
4849  dualfprintf(fail_file,"x=%g mybsqconst=%g\n",x,mybsqconst);
4850 
4851  FTYPE disc1=Power(gamma,2)*mybsqconst - 1.*Power(pr[B1],2) - 1.*Power(pr[B2],2) -
4852  2.*Power(pr[B1],2)*Power(pr[U1],2) +
4853  Power(gamma,2)*Power(pr[B1],2)*Power(pr[U1],2) -
4854  1.*Power(pr[B1],2)*Power(pr[U1],4) - 4.*pr[B1]*pr[B2]*pr[U1]*pr[U2] +
4855  2.*Power(gamma,2)*pr[B1]*pr[B2]*pr[U1]*pr[U2] -
4856  2.*pr[B1]*pr[B2]*Power(pr[U1],3)*pr[U2] - 2.*Power(pr[B2],2)*Power(pr[U2],2) +
4857  Power(gamma,2)*Power(pr[B2],2)*Power(pr[U2],2) -
4858  1.*Power(pr[B1],2)*Power(pr[U1],2)*Power(pr[U2],2) -
4859  1.*Power(pr[B2],2)*Power(pr[U1],2)*Power(pr[U2],2) -
4860  2.*pr[B1]*pr[B2]*pr[U1]*Power(pr[U2],3) - 1.*Power(pr[B2],2)*Power(pr[U2],4);
4861 
4862  FTYPE disc2=Power(gamma,2)*mybsqconst - 1.*Power(pr[B1],2) - 1.*Power(pr[B2],2) -
4863  2.*Power(pr[B1],2)*Power(pr[U1],2) +
4864  Power(gamma,2)*Power(pr[B1],2)*Power(pr[U1],2) -
4865  1.*Power(pr[B1],2)*Power(pr[U1],4) - 4.*pr[B1]*pr[B2]*pr[U1]*pr[U2] +
4866  2.*Power(gamma,2)*pr[B1]*pr[B2]*pr[U1]*pr[U2] -
4867  2.*pr[B1]*pr[B2]*Power(pr[U1],3)*pr[U2] - 2.*Power(pr[B2],2)*Power(pr[U2],2) +
4868  Power(gamma,2)*Power(pr[B2],2)*Power(pr[U2],2) -
4869  1.*Power(pr[B1],2)*Power(pr[U1],2)*Power(pr[U2],2) -
4870  1.*Power(pr[B2],2)*Power(pr[U1],2)*Power(pr[U2],2) -
4871  2.*pr[B1]*pr[B2]*pr[U1]*Power(pr[U2],3) - 1.*Power(pr[B2],2)*Power(pr[U2],4);
4872 
4873  if(disc1>=0.0){ PALLLOOP(pl) if(pl==B3) pr[pl] = +sqrt(disc1); }
4874  else if(disc2>=0){ PALLLOOP(pl) if(pl==B3) pr[pl] = -sqrt(disc2); }
4875  else PALLLOOP(pl){ if(pl==B3) pr[pl] = 0.0; }
4876 
4877  // PALLLOOP(pl) if(pl==B2){ xint=cos(phi0); pr[pl] = pleft[pl] + (pright[pl]-pleft[pl])*xint; }
4878  // PALLLOOP(pl) if(pl==B3){ xint=sin(phi0); pr[pl] = pleft[B2] + (pright[B2]-pleft[B2])*xint; }
4879 
4880  }
4881  }
4882  else{
4883  if (x<=0) {
4884  PALLLOOP(pl) pr[pl] = pleft[pl];
4885  }
4886  else if (x>0) {
4887  PALLLOOP(pl) pr[pl] = pright[pl];
4888  }
4889  }
4890  }
4891 
4892  if(WHICHKOMI>=101 && WHICHKOMI<=109){
4893  FTYPE E[NDIM],B[NDIM];
4894  FTYPE x0,dx0;
4895  FTYPE bcon[NDIM],vcon[NDIM],econ[NDIM];
4896  FTYPE phi0;
4897  FTYPE KK;
4898  FTYPE B0;
4899  FTYPE muf;
4900 
4901  // defaults
4903  pr[RHO]=pr[UU]=0;
4904  }
4905  else{
4906  pr[RHO]=0.1; // all fields below are order unity overall, but can pass through zero.
4907  pr[UU]=pr[RHO]; // so relativistically hot
4908  }
4909 
4910  pr[U1]=pr[U2]=pr[U3]=0.0;
4911  pr[B2]=pr[B3]=0;
4912  pr[B1]=0;
4913 
4914 
4915  extern void vbtopr(FTYPE *vcon,FTYPE *bcon,struct of_geom *geom, FTYPE *pr);
4916  extern void computeKK(FTYPE *pr, struct of_geom *geom, FTYPE *KK);
4917  extern void EBvetatopr(FTYPE *Econ, FTYPE *Bcon, FTYPE *veta, struct of_geom *geom, FTYPE *pr);
4918  extern int EBtopr(FTYPE *E,FTYPE *B,struct of_geom *geom, FTYPE *pr);
4919  extern int EBtopr_2(FTYPE *E,FTYPE *B,struct of_geom *geom, FTYPE *pr);
4920 
4921 
4922  int TESTNUMBER=WHICHKOMI-101; // to translate to init.komtests.c numbering
4923 
4924  *whichvel=WHICHVEL;
4925  //*whichvel=VEL4;
4926  *whichcoord=MCOORD;
4927  // get metric grid geometry for these ICs
4928  int getprim=0;
4929  struct of_geom geomdontuse;
4930  struct of_geom *ptrgeom=&geomdontuse;
4931  gset(getprim,*whichcoord,i,j,k,ptrgeom);
4932 
4933  // *whichvel=WHICHVEL;
4934  // *whichcoord=PRIMECOORDS;
4935  // struct of_geom geomdontuse;
4936  // struct of_geom *ptrgeom=&geomdontuse;
4937  // int loc=CENT;
4938  // get_geometry(i,j,k,loc,ptrgeom);
4939 
4940 
4941  if(TESTNUMBER==0){ // Fast wave
4942  tf = 1;
4943  int idt;
4944  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=tf/10.0;
4945 
4946  // tf = 1;
4947  // DTd=1E-5;
4948 
4949 
4950  E[1]=0;
4951  E[2]=0;
4952  B[3]=0.0;
4953  B[1]=0.0;
4954  x0=0.0;
4955  dx0=0.1;
4956  if(x-x0<=-dx0) B[2]=1.0;
4957  else if((x-x0>-dx0)&&(x-x0<dx0)) B[2]=1.0-(0.3/(2.0*dx0))*((x-x0)+dx0);
4958  else if(x-x0>=dx0) B[2]=0.7;
4959 
4960  muf=1.0;
4961 
4962  E[3]=1.0-muf*B[2];
4963 
4964  // for(k=1;k<=3;k++) E[k]=-E[k]; // switch for GRFFE formulation sign convention
4965 
4966  // int jj;
4967  // SLOOPA(jj) E[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
4968  // SLOOPA(jj) B[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
4969  EBtopr(E,B,ptrgeom,pr);
4970  //EBtopr_2(E,B,ptrgeom,pr);
4971 
4972  // pr[U1]=0.9;
4973 
4974  //dualfprintf(fail_file,"pr[U1]=%21.15g pr[U2]=%21.15g\n",pr[U1],pr[U2]);
4975 
4976  computeKK(pr,ptrgeom,&KK);
4977 
4978  dualfprintf(fail_file,"i=%d KK=%21.15g\n",i,KK);
4979 
4980  }
4981  if(TESTNUMBER==1){ // comoving Fast wave (NOT a Komissarov test)
4982  //tf = 1;
4983  // DTd=tf/10.0;
4984 
4985  tf = 1;
4986  int idt;
4987  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=1E-5;
4988 
4989 
4990  bcon[3]=0.0;
4991  bcon[1]=0.0;
4992  x0=0.0;
4993  dx0=0.1;
4994  if(x-x0<=-dx0) bcon[2]=1.0;
4995  else if((x-x0>-dx0)&&(x-x0<dx0)) bcon[2]=1.0-(0.3/(2.0*dx0))*((x-x0)+dx0);
4996  else if(x-x0>=dx0) bcon[2]=0.7;
4997 
4998  // for(k=1;k<=3;k++) E[k]=-E[k]; // switch for GRFFE formulation sign convention
4999 
5000 
5001  vcon[1]=0.9999;
5002  vcon[2]=vcon[3]=0;
5003 
5004  // int jj;
5005  // SLOOPA(jj) vcon[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5006  // SLOOPA(jj) bcon[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5007  vbtopr(vcon,bcon,ptrgeom,pr);
5008 
5009  computeKK(pr,ptrgeom,&KK);
5010 
5011  dualfprintf(fail_file,"i=%d KK=%21.15g\n",i,KK);
5012 
5013  }
5014  if(TESTNUMBER==2){ // (nondegenerate) Alfven wave (not going to work with HARM)
5015  tf = 2;
5016  int idt;
5017  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=tf/10.0;
5018 
5019  bcon[1]=bcon[2]=1.0;
5020 
5021  x0=0.0;
5022  if(x-x0<=-0.1) bcon[3]=1.0;
5023  else if((x-x0>-0.1)&&(x-x0<0.1)) bcon[3]=1.0+3.0/2.0*((x-x0)+0.1);
5024  else if(x-x0>=0.1) bcon[3]=1.3;
5025 
5026  econ[2]=econ[3]=0.0;
5027 
5028  // can be + or -
5029  //FTYPE CONSTECON1=1.3;
5030  // econ[1]=-sqrt(-CONSTECON1+bcon[1]*bcon[1]+bcon[2]*bcon[3]+bcon[3]*bcon[3]);
5031  // econ[1]=1-0.5*bcon[3];
5032  FTYPE CONSTECON1=1.0;
5033  econ[1]=sqrt(-CONSTECON1 + bcon[3]*bcon[3]);
5034  // econ[1]=0.0;
5035  // econ[1]=-bcon[3];
5036 
5037  vcon[1]=-0.5;
5038  vcon[2]=vcon[3]=0;
5039 
5040  // int jj;
5041  // SLOOPA(jj) econ[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5042  // SLOOPA(jj) bcon[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5043  // SLOOPA(jj) vcon[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5044  EBvetatopr(econ, bcon, vcon, ptrgeom, pr);
5045  // vbtopr(vcon,bcon,ptrgeom,pr);
5046 
5047  computeKK(pr,ptrgeom,&KK);
5048 
5049  dualfprintf(fail_file,"i=%d KK=%21.15g\n",i,KK);
5050 
5051  }
5052 
5053  if(TESTNUMBER==3){ // Degenerate Alfven wave
5054  tf = 2;
5055  int idt;
5056  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=tf/10.0;
5057  bcon[1]=0.0;
5058 
5059 
5060  x0=0.0;
5061  if(x-x0<=-0.1) phi0=0.0;
5062  else if((x-x0>-0.1)&&(x-x0<0.1)) phi0=5.0/2.0*M_PI*((x-x0)+0.1);
5063  else if(x-x0>=0.1) phi0=M_PI*0.5;
5064 
5065  bcon[2]=2.0*cos(phi0);
5066  bcon[3]=2.0*sin(phi0);
5067 
5068 
5069  vcon[1]=0.5;
5070  vcon[2]=vcon[3]=0;
5071 
5072  // int jj;
5073  // SLOOPA(jj) vcon[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5074  // SLOOPA(jj) bcon[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5075  vbtopr(vcon,bcon,ptrgeom,pr);
5076 
5077  computeKK(pr,ptrgeom,&KK);
5078 
5079  dualfprintf(fail_file,"i=%d KK=%21.15g\n",i,KK);
5080 
5081 
5082  }
5083  if(TESTNUMBER==4){ // Three-wave problem
5084  tf = .75;
5085  int idt;
5086  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=tf/10.0;
5087 
5088  x0=0.5;
5089  if(x<x0){
5090  B[1]=1.0;
5091  B[2]=1.5;
5092  B[3]=3.5;
5093  E[1]=-1.0;
5094  E[2]=-0.5;
5095  E[3]=0.5;
5096  }
5097  else{
5098  B[1]=1.0;
5099  B[2]=2.0;
5100  B[3]=2.3;
5101  E[1]=-1.5;
5102  E[2]=1.3;
5103  E[3]=-0.5;
5104  }
5105 
5106  // for(k=1;k<=3;k++) E[k]=-E[k]; // switch for GRFFE formulation sign convention
5107 
5108  // int jj;
5109  // SLOOPA(jj) E[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5110  // SLOOPA(jj) B[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5111  EBtopr(E,B,ptrgeom,pr);
5112  //EBtopr_2(E,B,ptrgeom,pr);
5113 
5114  computeKK(pr,ptrgeom,&KK);
5115 
5116  dualfprintf(fail_file,"i=%d KK=%21.15g\n",i,KK);
5117 
5118 
5119  }
5120 
5121  if(TESTNUMBER==5){ // B^2-E^2<0 problem
5122  tf = .02;
5123  int idt;
5124  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=tf/10.0;
5125 
5126  x0=0.5;
5127  if(x<x0){
5128  B[0]=0.0;
5129  B[1]=1.0;
5130  B[2]=1.0;
5131  B[3]=1.0;
5132  E[0]=0.0;
5133  E[1]=0.0;
5134  E[2]=0.5;
5135  E[3]=-0.5;
5136  }
5137  else{
5138  B[0]=0.0;
5139  B[1]=1.0;
5140  B[2]=-1.0;
5141  B[3]=-1.0;
5142  E[0]=0.0;
5143  E[1]=0.0;
5144  E[2]=0.5;
5145  E[3]=-0.5;
5146  }
5147 
5148  // for(k=1;k<=3;k++) E[k]=-E[k]; // switch for GRFFE formulation sign convention
5149 
5150  // int jj;
5151  // SLOOPA(jj) E[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5152  // SLOOPA(jj) B[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5153  EBtopr(E,B,ptrgeom,pr);
5154  //EBtopr_2(E,B,ptrgeom,pr);
5155 
5156  computeKK(pr,ptrgeom,&KK);
5157 
5158  dualfprintf(fail_file,"i=%d KK=%21.15g\n",i,KK);
5159 
5160 
5161  }
5162  if(TESTNUMBER==6){ // smoothed B^2-E^2<0 problem
5163  tf = .02;
5164  int idt;
5165  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=tf/10.0;
5166 
5167  x0=0.5;
5168  if(x-x0<-0.1){
5169  B[1]=1.0;
5170  B[2]=1.0;
5171  B[3]=1.0;
5172  E[1]=0.0;
5173  E[2]=0.5;
5174  E[3]=-0.5;
5175  }
5176  else if(x-x0>0.1){
5177  B[1]=1.0;
5178  B[2]=-1.0;
5179  B[3]=-1.0;
5180  E[1]=0.0;
5181  E[2]=0.5;
5182  E[3]=-0.5;
5183  }
5184  else if((x-x0>=-0.1)&&(x-x0<=0.1)){
5185  B[1]=1.0;
5186  B[2]=1.0+(x-x0+0.1)*(-2.0/0.2);
5187  B[3]=1.0+(x-x0+0.1)*(-2.0/0.2);
5188  E[1]=0.0;
5189  E[2]=0.5;
5190  E[3]=-0.5;
5191  }
5192 
5193  // for(k=1;k<=3;k++) E[k]=-E[k]; // switch for GRFFE formulation sign convention
5194 
5195 
5196  // int jj;
5197  // SLOOPA(jj) E[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5198  // SLOOPA(jj) B[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5199  EBtopr(E,B,ptrgeom,pr);
5200  //EBtopr_2(E,B,ptrgeom,pr);
5201 
5202  computeKK(pr,ptrgeom,&KK);
5203 
5204  dualfprintf(fail_file,"i=%d KK=%21.15g\n",i,KK);
5205 
5206 
5207  }
5208 
5209  if(TESTNUMBER==7){ // Komissarov 2004 C3.1 Alfven wave
5210  // PARA generates crap on left side, but wave doesn't move
5211  // MC does very well
5212  // no obvious difference between HLL and LAXF
5213  // Athena1/2 ok
5214  tf = 2.0;
5215  int idt;
5216  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=tf/10.0;
5217 
5218  // B[1]=B[2]=E[3]=E[2]=0;
5219  B[1]=B[2]=E[3]=1.0;
5220  E[2]=0;
5221 
5222 
5223  if(x<=0.5){
5224  B[3]=1.0;
5225  }
5226  else if(x>=0.2+0.5){
5227  B[3]=1.3;
5228  }
5229  else{
5230  B[3]=1.0+0.15*(1.0+sin(5.0*M_PI*(x-0.1-0.5)));
5231  }
5232  E[1]=-B[3];
5233 
5234  // for(k=1;k<=3;k++) E[k]=-E[k]; // switch for GRFFE formulation sign convention
5235 
5236  // int jj;
5237  // SLOOPA(jj) E[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5238  // SLOOPA(jj) B[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5239  EBtopr(E,B,ptrgeom,pr);
5240  //EBtopr_2(E,B,ptrgeom,pr);
5241 
5242  computeKK(pr,ptrgeom,&KK);
5243 
5244  dualfprintf(fail_file,"i=%d KK=%21.15g\n",i,KK);
5245 
5246 
5247  }
5248  if(TESTNUMBER==8){ // Komissarov 2004 C3.2 Current Sheet
5249  tf = 1.0;
5250  int idt;
5251  for(idt=0;idt<NUMDUMPTYPES;idt++) DTdumpgen[idt]=tf/10.0;
5252 
5253  E[1]=E[2]=E[3]=0.0;
5254  B[3]=0.0;
5255  B[1]=1.0;
5256 
5257  //B0=0.5; // fine
5258  B0 = 2.0;
5259 
5260  if(x<=0.5){
5261  B[2]=B0;
5262  }
5263  else{
5264  B[2]=-B0;
5265  }
5266 
5267 
5268  // int jj;
5269  // SLOOPA(jj) E[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5270  // SLOOPA(jj) B[jj]*=sqrt(ptrgeom->gcon[GIND(jj,jj)]);
5271  EBtopr(E,B,ptrgeom,pr);
5272  //EBtopr_2(E,B,ptrgeom,pr);
5273 
5274  computeKK(pr,ptrgeom,&KK);
5275 
5276  dualfprintf(fail_file,"i=%d KK=%21.15g\n",i,KK);
5277 
5278 
5279  }
5280 
5281  }// end over init.komtests.c from FFDE code tests
5282 
5283 
5284 
5285 
5286  if(FLUXB==FLUXCTSTAG){
5287  //can ignore half a cell shift for B1: it does not change across the interface so does not matter
5288  PLOOPBONLY(pl) pstag[pl]=pr[pl];
5289  }
5290 
5291 
5292  pr[PRAD0] = 10.0*ERADLIMIT ; // so doesn't hit floor and confuse debug info
5293  pr[PRAD1] = 0 ;
5294  pr[PRAD2] = 0 ;
5295  pr[PRAD3] = 0 ;
5296 
5297 
5298 
5299  return(0);
5300  }
5301 
5302 
5303 
5304 
5305 
5306  /*************************************************/
5307  /*************************************************/
5308  if(WHICHPROBLEM==RADBONDI){
5309 
5310 
5311  FTYPE xx,yy,zz,rsq;
5312  coord(i, j, k, CENT, X);
5313  bl_coord(X, V);
5314  xx=V[1];
5315  yy=V[2];
5316  zz=V[3];
5317 
5318  *whichcoord=MCOORD;
5319  // get metric grid geometry for these ICs
5320  int getprim=0;
5321  struct of_geom geomrealdontuse;
5322  struct of_geom *ptrgeomreal=&geomrealdontuse;
5323  gset(getprim,*whichcoord,i,j,k,ptrgeomreal);
5324 
5325 
5326  FTYPE rho,ERAD,uint;
5327  FTYPE rho0,Tgas0,ur,Tgas,Trad,r,rcm,prad,pgas,vx,ut;
5328 
5329  FTYPE Fx,Fy,Fz;
5330  Fx=Fy=Fz=0;
5331 
5332  //at outern boundary
5333  r=RADBONDI_MAXX;
5334  ur=-sqrt(2./r);
5335  rho0=-RADBONDI_MDOTPEREDD*RADBONDI_MDOTEDD/(4.*Pi*r*r*ur);
5336  Tgas0=RADBONDI_TGAS0;
5337 
5338  //at given cell
5339  r=xx;
5340  ur=-sqrt(2./r);
5341  ut=sqrt((-1.-ur*ur*ptrgeomreal->gcov[GIND(1,1)])/ptrgeomreal->gcov[GIND(0,0)]);
5342  vx=ur/ut;
5343  rho=-RADBONDI_MDOTPEREDD*RADBONDI_MDOTEDD/(4.*Pi*r*r*ur);
5344  Tgas=Tgas0*pow(rho/rho0,gam-1.);
5345 
5346  uint=calc_PEQ_ufromTrho(Tgas,rho);
5347 
5348  pgas=rho*Tgas;
5349  prad=RADBONDI_PRADGAS*pgas;
5350  ERAD=prad*3.;
5351 
5352  pr[RHO] = rho ;
5353  pr[UU] = uint;
5354  // KORAL sets VEL3 type quantity vx, but then feeds that directly into koral's prad_ff2lab() that takes in relative 4-velocity. So set ur instead as 4-velocity and assume not extremely close to hole.
5355  // pr[U1] = vx;
5356  pr[U1] = ur;
5357  pr[U2] = 0 ;
5358  pr[U3] = 0 ;
5359 
5360  // just define some field
5361  pr[B1]=0.0;
5362  pr[B2]=0.0;
5363  pr[B3]=0.0;
5364 
5365  if(FLUXB==FLUXCTSTAG){
5366  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
5367  PLOOPBONLY(pl) pstag[pl]=pr[pl];
5368  }
5369 
5370 
5371  // *whichvel=VEL3;
5372  *whichvel=VEL4;
5373 
5374  if(PRAD0>=0){
5375  pr[PRAD0] = ERAD;
5376  pr[PRAD1] = 0 ;
5377  pr[PRAD2] = 0 ;
5378  pr[PRAD3] = 0 ;
5379 
5380 
5381  pr[PRAD0] = 0 ; // so triggers failure if used
5382  pr[PRAD1] = 0 ;
5383  pr[PRAD2] = 0 ;
5384  pr[PRAD3] = 0 ;
5385 
5386  //E, F^i in orthonormal fluid frame
5387  FTYPE pradffortho[NPR];
5388  pradffortho[PRAD0] = ERAD;
5389  pradffortho[PRAD1] = Fx;
5390  pradffortho[PRAD2] = Fy;
5391  pradffortho[PRAD3] = Fz;
5392 
5393 
5394  if(0){//DEBUG:
5395  dualfprintf(fail_file,"i=%d rho=%g uint=%g ERAD=%g\n",i,rho,uint,ERAD);
5396 
5397  dualfprintf(fail_file,"i=%d rho=%g uint=%g ERAD=%g\n",i,rho*RHOBAR,uint*UBAR,ERAD*UBAR);
5398  }
5399 
5400 
5401  // Transform these fluid frame E,F^i to lab frame coordinate basis primitives
5402  prad_fforlab(whichvel, whichcoord, FF2LAB, i,j,k,CENT,ptrgeomreal, pradffortho, pr, pr);
5403 
5404  if(0){ // DEBUG
5405  dualfprintf(fail_file,"AFTER: i=%d rho=%g uint=%g vx=%g ERAD=%g uradx=%g\n",i,pr[RHO]*RHOBAR,pr[UU]*UBAR,pr[U1]*sqrt(ptrgeomreal->gcov[GIND(1,1)])*VBAR,pr[URAD0]*UBAR,pr[URAD1]*sqrt(ptrgeomreal->gcov[GIND(1,1)])*VBAR);
5406  }
5407  }
5408 
5409  return(0);
5410  }
5411 
5412 
5413 
5414 
5415 
5416 
5417 
5418  /*************************************************/
5419  /*************************************************/
5420  if(WHICHPROBLEM==RADDOT){
5421 
5422  FTYPE xx,yy,zz,rsq;
5423  coord(i, j, k, CENT, X);
5424  bl_coord(X, V);
5425  xx=V[1];
5426  yy=V[2];
5427  zz=V[3];
5428 
5429  pr[RHO] = 1.0;
5430  pr[UU] = 1.0;
5431  pr[U1] = 0 ;
5432  pr[U2] = 0 ;
5433  pr[U3] = 0 ;
5434 
5435  // just define some field
5436  pr[B1]=0.0;
5437  pr[B2]=0.0;
5438  pr[B3]=0.0;
5439 
5440  if(FLUXB==FLUXCTSTAG){
5441  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
5442  PLOOPBONLY(pl) pstag[pl]=pr[pl];
5443  }
5444 
5445 
5446  *whichvel=VEL4;
5447  *whichcoord=MCOORD;
5448 
5449  if(PRAD0>=0){
5450  pr[PRAD0] = 0 ; // so triggers failure if used
5451  pr[PRAD1] = 0 ;
5452  pr[PRAD2] = 0 ;
5453  pr[PRAD3] = 0 ;
5454 
5455  //E, F^i in orthonormal fluid frame
5456  FTYPE pradffortho[NPR];
5457  pradffortho[PRAD0] = RADDOT_LTEFACTOR*calc_LTE_Efromurho(pr[RHO],pr[UU]);
5458  pradffortho[PRAD1] = 0;
5459  pradffortho[PRAD2] = 0;
5460  pradffortho[PRAD3] = 0;
5461 
5462  if(startpos[1]+i==RADDOT_IDOT && startpos[2]+j==RADDOT_JDOT && startpos[3]+k==RADDOT_KDOT){
5463  // dualfprintf(fail_file,"GOT INITIAL DOT\n");
5464  if(N1==1) pradffortho[PRAD0] *= RADDOT_F1;
5465  else{
5466  pradffortho[PRAD0]*=RADDOT_F2;
5467  pradffortho[PRAD2]=RADDOT_FYDOT*pradffortho[PRAD0];
5468  }
5469  }// end if DOT
5470 
5471 
5472  // Transform these fluid frame E,F^i to lab frame coordinate basis primitives
5473  prad_fforlab(whichvel, whichcoord, FF2LAB, i,j,k,CENT,NULL, pradffortho, pr, pr);
5474  }
5475 
5476  return(0);
5477  }
5478 
5479 
5480 
5481 
5483  FTYPE r,th,ph;
5484  coord(i, j, k, CENT, X);
5485  bl_coord(X, V);
5486  r=V[1];
5487  th=V[2];
5488  ph=V[3];
5489 
5490  *whichcoord=MCOORD; // not just BLCOORD, in case setting for inside horizon too when MCOORD=KSCOORDS or if using SPCMINKMETRIC
5491  *whichvel=VEL3; // use 3-velocity since later set donut using VEL3
5492  // get metric grid geometry for these ICs
5493  int getprim=0;
5494  struct of_geom geomraddontuse;
5495  struct of_geom *ptrgeomrad=&geomraddontuse;
5496  gset(getprim,*whichcoord,i,j,k,ptrgeomrad);
5497 
5498 
5499  // KORAL:
5500  // atmtype=0 : -1.5 -2.5
5501  // atmtype=1 : -2.0 -2.5
5502 
5503  if(1){ // SUPERMADNEW
5504  int set_fieldtype(void);
5505  int FIELDTYPE=set_fieldtype();
5506  if(FIELDTYPE==FIELDJONMAD){
5507  // so bsq/rho isn't super high at large radii with current choice for magnetic field
5508  pr[RHO]=RADNT_RHOATMMIN*pow(r/RADNT_ROUT,-1.1);
5509  }
5510  }
5511  else{
5512  pr[RHO]=RADNT_RHOATMMIN*pow(r/RADNT_ROUT,-1.5);
5513  }
5514 
5515  pr[UU]=RADNT_UINTATMMIN*pow(r/RADNT_ROUT,-2.5);
5516 
5517  set_zamo_velocity(*whichvel,ptrgeomrad,pr); // only sets U1-U3 to zamo
5518 
5519 
5520  // just define some field
5521  pr[B1]=0.0;
5522  pr[B2]=0.0;
5523  pr[B3]=0.0;
5524 
5525  if(FLUXB==FLUXCTSTAG){
5526  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
5527  PLOOPBONLY(pl) pstag[pl]=pr[pl];
5528  }
5529 
5530 
5531  // KORAL:
5532  // atmtype=0 : pr[URAD0]=ERADATMMIN and zamo vels
5533  // atmtype=1 : pr[URAD0]=ERADATMMIN and ncon={0,-1,0,0} with set_ncon_velocity(whichvel,1000.0,ncon,ptrgeomrad,uconreal);
5534  // atmtype=2 : pr[URAD0]=ERADATMMIN*pow(rout/r,4) pr[URAD1-URAD3] with ncon={0,-gammamax*(pow(r/rout,1.)),0,0} again using set_non_velocity() with gammamax=10.
5535 
5536  int returndonut;
5537  // set as in fluid frame
5538  FTYPE pradffortho[NPR],pradfforthoatm[NPR];
5539  if(1){
5540 
5541  if(PRAD0>=0){
5542  // choose atmosphere level of radiation
5543  pradfforthoatm[PRAD0]=RADNT_ERADATMMIN*pow(r/RADNT_ROUT,-2.5);
5544  pradfforthoatm[PRAD1]=0;
5545  pradfforthoatm[PRAD2]=0;
5546  pradfforthoatm[PRAD3]=0;
5547 
5548  // copy as backup or atmosphere
5549  pradffortho[PRAD0]=pradfforthoatm[PRAD0];
5550  pradffortho[PRAD1]=pradfforthoatm[PRAD1];
5551  pradffortho[PRAD2]=pradfforthoatm[PRAD2];
5552  pradffortho[PRAD3]=pradfforthoatm[PRAD3];
5553  }
5554 
5555 
5556  // So donut already has ambient in whichvel whichcoord, now get donut
5557  if(WHICHPROBLEM==RADDONUT){
5558 
5559  // donut expects fluid frame values in pr. JCM sets as output in fluid frame so use same conversion below.
5560  if(PRAD0>=0){
5561  pr[PRAD0]=pradffortho[PRAD0];
5562  pr[PRAD1]=pradffortho[PRAD1];
5563  pr[PRAD2]=pradffortho[PRAD2];
5564  pr[PRAD3]=pradffortho[PRAD3];
5565  }
5566 
5567  // ADD DONUT
5568  returndonut=get_full_rtsolution(whichvel,whichcoord,RADDONUT_OPTICALLYTHICKTORUS, pr,X,V,&ptrgeomrad);
5569 
5570  if(PRAD0>=0){
5571  // donut returns fluid frame orthonormal values for radiation in pp
5572  pradffortho[PRAD0]=pr[PRAD0];
5573  pradffortho[PRAD1]=pr[PRAD1];
5574  pradffortho[PRAD2]=pr[PRAD2];
5575  pradffortho[PRAD3]=pr[PRAD3];
5576 
5577  // if(i==21 && j==3 && k==0){
5578  // dualfprintf(fail_file,"ZOOM: CHECK: ijk=%d %d %d : %g %g %g %g\n",i,j,k,pradffortho[PRAD0],pradffortho[PRAD1],pradffortho[PRAD2],pradffortho[PRAD3]);
5579  // }
5580  }
5581 
5582 
5583  }
5584 
5585 
5586  if(PRAD0>=0){
5587 
5588  // if(i==21 && j==3 && k==0){
5589  // PLOOP(pliter,pl) dualfprintf(fail_file,"ZOOM: CHECKPREpradfforlab: pl=%d pr=%21.15g\n",pl,pr[pl]);
5590  // }
5591 
5592  // Transform these fluid frame E,F^i to lab frame coordinate basis primitives
5593  prad_fforlab(whichvel, whichcoord, FF2LAB, i,j,k,CENT,ptrgeomrad, pradffortho, pr, pr);
5594 
5595  // if(i==21 && j==3 && k==0){
5596  // PLOOP(pliter,pl) dualfprintf(fail_file,"ZOOM: CHECKPOSTpradfforlab: pl=%d pr=%21.15g\n",pl,pr[pl]);
5597  // }
5598 
5599  if(debugfail>=2 && returndonut==0 && pradfforthoatm[PRAD0]>pradffortho[PRAD0]){
5600  dualfprintf(fail_file,"WARNING: Torus radiation pressure below atmosphere.\n");
5601  dualfprintf(fail_file,"CHECKPOST: ijk=%d %d %d : %g %g %g %g\n",i,j,k,pr[PRAD0],pr[PRAD1],pr[PRAD2],pr[PRAD3]);
5602  }
5603  }
5604 
5605 
5606 #if(0)
5607  // report zamo
5608  FTYPE prreport[NPR];
5609  set_zamo_velocity(*whichvel,ptrgeomrad,prreport);
5610  dualfprintf(fail_file,"ZAMO: ijk=%d %d %d : %g %g %g : fluid: %g %g %g\n",i,j,k,prreport[U1],prreport[U2],prreport[U3],pr[U1],pr[U2],pr[U3]);
5611  int jj,kk;
5612  DLOOP(jj,kk) dualfprintf(fail_file,"gn%d%d=%26.20g\n",jj+1,kk+1,ptrgeomrad->gcon[GIND(jj,kk)]);
5613  DLOOP(jj,kk) dualfprintf(fail_file,"gv%d%d=%26.20g\n",jj+1,kk+1,ptrgeomrad->gcov[GIND(jj,kk)]);
5614 #endif
5615 
5616  }
5617  else{
5618 
5619 
5620  if(PRAD0>=0){
5621  // like latest koral that assumes radiation frame is zamo and RADNT_ERADATMMIN is actually in that frame, so no transformation for E
5622  // So this is somewhat inconsistent with boundary conditions
5623  // KORAL:
5624  pr[PRAD0] = RADNT_ERADATMMIN; // assumed as lab-frame ZAMO frame value
5625  set_zamo_velocity(*whichvel,ptrgeomrad,&pr[URAD1-U1]); // only sets URAD1-URAD3 to zamo
5626  }
5627  }
5628 
5629  // dualfprintf(fail_file,"returning: whichvel=%d whichcoord=%d\n",*whichvel,*whichcoord);
5630 
5631  return(0);
5632  }
5633 
5634 
5635 
5636 
5637  /*************************************************/
5638  /*************************************************/
5640  FTYPE r,th,ph;
5641  coord(i, j, k, CENT, X);
5642  bl_coord(X, V);
5643  r=V[1];
5644  th=V[2];
5645  ph=V[3];
5646 
5647  *whichcoord=MCOORD; // not BLCOORD, in case setting for inside horizon too when MCOORD=KSCOORDS or if using SPCMINKMETRIC
5648  *whichvel=VEL3;
5649 
5650 
5651  // as in koral:
5652  pr[RHO]=1.0;
5653  pr[UU]=0.1;
5654  pr[U1]=0.0;
5655  pr[U2]=0.0;
5656  pr[U3]=0.0;
5657 
5658 
5659  // just define some field
5660  pr[B1]=0.0;
5661  pr[B2]=0.0;
5662  pr[B3]=0.0;
5663 
5664  // radiation primitives directly
5665  pr[URAD0]=0.0001;
5666  pr[URAD1]=0.0;
5667  pr[URAD2]=0.0;
5668  pr[URAD3]=0.0;
5669 
5670 
5671  if(FLUXB==FLUXCTSTAG){
5672  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
5673  PLOOPBONLY(pl) pstag[pl]=pr[pl];
5674  }
5675 
5676  return(0);
5677  }
5678 
5679  /*************************************************/
5680  /*************************************************/
5682  FTYPE r,th,ph;
5683  coord(i, j, k, CENT, X);
5684  bl_coord(X, V);
5685  r=V[1];
5686  th=V[2];
5687  ph=V[3];
5688 
5689  *whichcoord=MCOORD; // not BLCOORD, in case setting for inside horizon too when MCOORD=KSCOORDS or if using SPCMINKMETRIC
5690  *whichvel=VEL3;
5691 
5692 #define fsx(x) ((x)>0 ? exp(-1.0/(x)) : 0.0)
5693 #define gsx(x) (fsx(x)/(fsx(x) + fsx(1.0-(x))))
5694 #define stepfunction(x) (1.0 - gsx((x)-0.5))
5695 #define stepfunctionab(x,a,b) (((a)-(b))*stepfunction(x) + (b))
5696 #define stepfunctionab2(x,a,b) (((a)-(b))*stepfunction(x)*stepfunction(x) + (b))
5697 
5698  FTYPE rhojet=1E-5;
5699  FTYPE E0perRho0;
5700  FTYPE Rho0;
5701  FTYPE Ehatjet;
5702  FTYPE Ehatstar;
5703 
5704  FTYPE vz0=0.5; // 0.99;
5705  FTYPE vr0=0;
5706 
5707  FTYPE L0;
5708  FTYPE E0;
5709  FTYPE Ehat0;
5710  FTYPE Fr0;
5711 
5712  if(RADCYLJET_TYPE==1){
5713  Rho0=1.0*rhojet;
5714  E0perRho0=1E-6;
5715  L0=0;
5716  E0=E0perRho0*Rho0*rhojet;
5717  Ehat0=E0+vr0*L0;
5718  Fr0=-L0-vr0*E0;
5719  Ehatjet=Ehat0*1E-2;
5720  Ehatstar=Ehat0;
5721  }
5722  else if(RADCYLJET_TYPE==4){
5723  Rho0=1000.0*rhojet;
5724  E0perRho0=1E2;
5725  L0=0;
5726  E0=E0perRho0*Rho0*rhojet;
5727  Ehat0=E0+vr0*L0;
5728  Fr0=-L0-vr0*E0;
5729  Ehatjet=Ehat0*0.01;
5730  Ehatstar=Ehat0;
5731  }
5732 
5733 
5735  FTYPE r1=RADNT_MAXX;
5736 
5737  pr[RHO]=stepfunctionab(r,rhojet,Rho0);
5738  pr[U1]=vr0*stepfunctionab(r,1.0,0.001); // R
5739  pr[U2]=vz0*stepfunctionab(r,1,0.001); // z
5740  pr[U3]=0.0; // phi
5741 
5742 
5743  // just define some field
5744  pr[B1]=0.0;
5745  pr[B2]=0.0;
5746  pr[B3]=0.0;
5747 
5748  // radiation primitives directly
5749  pr[URAD0]=Ehat0*stepfunctionab(r,Ehatjet/Ehat0,Ehatstar/Ehat0); // not quite right.
5750  pr[URAD1]=pr[U1];
5751  pr[URAD2]=pr[U2];
5752  pr[URAD3]=pr[U3];
5753 
5754  // ensure total pressure is constant by varying gas temperature, but assume LTE at t=0
5755  // P = arad T^4 + rho*T = arad T^4 + (gam-1)*u = Ehat0 + (gam-1)*u ->
5756  // u = (P - Ehat0)/(gam-1)
5757  // assume rad pressure in star balances
5758  if(RADCYLJET_TYPE==1){
5759  FTYPE ptot=1.1*(4.0/3.0-1.0)*Ehatstar;
5760  pr[UU]=( ptot - (4.0/3.0-1.0)*pr[URAD0])/(gam-1.0);
5761  }
5762  else{
5763  pr[UU]=0.1*pr[URAD0];
5764  }
5765 
5766  if(FLUXB==FLUXCTSTAG){
5767  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
5768  PLOOPBONLY(pl) pstag[pl]=pr[pl];
5769  }
5770 
5771  // THINGS TO TRY:
5772 
5773  return(0);
5774  }
5775  else if(WHICHPROBLEM==RADCYLJET && (RADCYLJET_TYPE==2||RADCYLJET_TYPE==3)){
5776  FTYPE r,th,ph;
5777  coord(i, j, k, CENT, X);
5778  bl_coord(X, V);
5779  r=V[1];
5780  th=V[2];
5781  ph=V[3];
5782 
5783  *whichcoord=MCOORD; // not BLCOORD, in case setting for inside horizon too when MCOORD=KSCOORDS or if using SPCMINKMETRIC
5784  *whichvel=VEL3;
5785 
5786  FTYPE rhojet=0.1;
5787  FTYPE vz0=0.5; // 0.99;
5788  FTYPE vr0=0;
5789  FTYPE Ehat0=0.1*rhojet;
5790 
5791  FTYPE Ehatjet=Ehat0*1E-2;
5792 
5794  FTYPE r1=RADNT_MAXX;
5795 
5796  pr[RHO]=rhojet;
5797  pr[U1]=vr0;
5798  pr[U2]=vz0;
5799  pr[U3]=0.0; // phi
5800 
5801 
5802  // just define some field
5803  pr[B1]=0.0;
5804  pr[B2]=0.0;
5805  pr[B3]=0.0;
5806 
5807  // radiation primitives directly
5808  pr[URAD0]=Ehatjet;
5809  pr[URAD1]=pr[U1];
5810  pr[URAD2]=pr[U2];
5811  pr[URAD3]=pr[U3];
5812 
5813  FTYPE ptot=1.5*(4.0/3.0-1.0)*Ehatjet;
5814  pr[UU]=( ptot - (4.0/3.0-1.0)*pr[URAD0])/(gam-1.0);
5815 
5816  if(FLUXB==FLUXCTSTAG){
5817  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
5818  PLOOPBONLY(pl) pstag[pl]=pr[pl];
5819  }
5820 
5821  return(0);
5822  }
5823  else if(WHICHPROBLEM==RADCYLJET && (RADCYLJET_TYPE==5)){
5824  FTYPE r,th,ph;
5825  coord(i, j, k, CENT, X);
5826  bl_coord(X, V);
5827  r=V[1];
5828  th=V[2];
5829  ph=V[3];
5830 
5831  *whichcoord=MCOORD; // not BLCOORD, in case setting for inside horizon too when MCOORD=KSCOORDS or if using SPCMINKMETRIC
5832  *whichvel=VEL3;
5833 
5834  FTYPE rhojet=0.1*100.0;
5835  FTYPE vz0=0.5; // 0.99;
5836  FTYPE vr0=0;
5837  FTYPE Ehat0=0.1*rhojet;
5838 
5839  FTYPE Ehatjet=Ehat0*1E-2;
5840 
5842  FTYPE r1=RADNT_MAXX;
5843 
5844  pr[RHO]=rhojet;
5845  pr[U1]=vr0;
5846  pr[U2]=vz0;
5847  pr[U3]=0.0; // phi
5848 
5849 
5850  // just define some field
5851  pr[B1]=0.0;
5852  pr[B2]=0.0;
5853  pr[B3]=0.0;
5854 
5855  // radiation primitives directly
5856  pr[URAD0]=Ehatjet;
5857  pr[URAD1]=pr[U1];
5858  pr[URAD2]=pr[U2];
5859  pr[URAD3]=pr[U3];
5860 
5861  pr[UU]=0.1*pr[RHO];
5862 
5863  RADCYLJET_RHOJET=pr[RHO];
5864  RADCYLJET_UJET=pr[UU];
5865  RADCYLJET_EHATJET=pr[URAD0];
5866  RADCYLJET_VRSTAR=pr[U1];
5867 
5868 
5869  if(FLUXB==FLUXCTSTAG){
5870  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
5871  PLOOPBONLY(pl) pstag[pl]=pr[pl];
5872  }
5873 
5874  return(0);
5875  }
5876  /*************************************************/
5877  /*************************************************/
5878  else if(WHICHPROBLEM==RADCYLJET && (RADCYLJET_TYPE==6)){
5879  FTYPE r,th,ph;
5880  coord(i, j, k, CENT, X);
5881  bl_coord(X, V);
5882  r=V[1];
5883  th=V[2];
5884  ph=V[3];
5885 
5886  *whichcoord=MCOORD; // not BLCOORD, in case setting for inside horizon too when MCOORD=KSCOORDS or if using SPCMINKMETRIC
5887  *whichvel=VEL3;
5888 
5889  FTYPE Rho0=1E-5*100.0*100.0/79.4*40.0;
5890  pr[RHO]=Rho0;
5891 
5892  pr[U1]=0.0;
5893  pr[U2]=0.0;
5894  pr[U3]=0.0;
5895 
5896 
5897  // just define some field
5898  pr[B1]=0.0;
5899  pr[B2]=0.0;
5900  pr[B3]=0.0;
5901 
5902  // radiation primitives directly
5903  pr[URAD1]=pr[U1];
5904  pr[URAD2]=pr[U2];
5905  pr[URAD3]=pr[U3];
5906 
5907  // ensure thermal equilibrium in star
5908  FTYPE Tstar;
5909  if(WHICHRADSOURCEMETHOD==SOURCEMETHODNONE) Tstar=1510.0*1.0E7/TEMPBAR/0.8;
5910  else Tstar=6.0E7/TEMPBAR;
5911  // P = (arad/3)T^4 + rho T
5912  pr[UU]=u_rho0_T_simple(i,j,k,CENT,pr[RHO],Tstar);
5913  pr[URAD0]=calc_LTE_EfromT(Tstar);
5914 
5915  if(1){
5916  static int firsttime=1;
5917  if(firsttime){
5918  // cs2 ~ gam*Ptot/rho and need vz0>cs.
5919  // look at sqrt(cs2tot) in SM to check or obtain here.
5920  FTYPE ptot=((gam-1.0)*pr[UU] + (4.0/3.0-1.0)*pr[URAD0]);
5921  FTYPE ptrue;
5922  if(WHICHRADSOURCEMETHOD==SOURCEMETHODNONE) ptrue=(gam-1.0)*pr[UU];
5923  else ptrue=ptot;
5924  FTYPE gamptot=(gam*(gam-1.0)*pr[UU] + (4.0/3.0)*(4.0/3.0-1.0)*pr[URAD0]);
5925  FTYPE cs2 = gamptot/pr[RHO];
5926  dualfprintf(fail_file,"STAR: ptrue=%21.15g ptot=%21.15g ptrue/rho=%21.15g cs2=%21.15g cs=%21.15g\n",ptrue,ptot,ptrue/pr[RHO],cs2,sqrt(cs2));
5927  firsttime=0;
5928  }
5929  }
5930 
5931  if(FLUXB==FLUXCTSTAG){
5932  // assume pstag later defined really using vector potential or directly assignment of B3 in axisymmetry
5933  PLOOPBONLY(pl) pstag[pl]=pr[pl];
5934  }
5935 
5936  // really "star" values
5937  RADCYLJET_RHOJET=pr[RHO];
5938  RADCYLJET_UJET=pr[UU];
5939  RADCYLJET_EHATJET=pr[URAD0];
5940  RADCYLJET_TEMPJET=Tstar;
5941  RADCYLJET_VRSTAR=pr[U1];
5942 
5943  static int firsttime=1;
5944  if(myid==0&&firsttime==1&&i==0&&j==0&&k==0){
5945  firsttime=0;
5946  FILE *fstar;
5947  if((fstar=fopen("star.txt","wt"))==NULL){
5948  dualfprintf(fail_file,"Couldn't open star.txt\n");
5949  myexit(1);
5950  }
5951  else{
5952  fprintf(fstar,"%d %21.15g %21.15g %21.15g %21.15g %21.15g\n",RADCYLJET_TYPE,RADCYLJET_RHOJET,RADCYLJET_UJET,RADCYLJET_EHATJET,RADCYLJET_TEMPJET,RADCYLJET_VRSTAR);
5953  fclose(fstar);
5954  }
5955  }
5956 
5957  return(0);
5958  }
5959 
5960 
5961 
5962 
5963  return(0);
5964 }
5965 
5966 
5967 
5968 
5969 
5970 // analytical solution for RADDONUT donut
5971 // get full radiative donut solution
5972 // input pp has backup (e.g. atmosphere) values in *whichvel, *whichcoord with geometry ptrgeom
5973 // input pp[PRAD0-PRAD3] are fluid frame orthonormal values
5974 // RETURNS: pp and can change whichvel and whichcoord and ptrptrgeom (these must all 3 be consistent with the returned pp)
5975 static int get_full_rtsolution(int *whichvel, int *whichcoord, int opticallythick, FTYPE *pp,FTYPE *X, FTYPE *V,struct of_geom **ptrptrgeom)
5976 {
5977  int jj,kk;
5978  int pliter,pl;
5979  int i=(*ptrptrgeom)->i;
5980  int j=(*ptrptrgeom)->j;
5981  int k=(*ptrptrgeom)->k;
5982  int loc=(*ptrptrgeom)->p;
5983  FTYPE r=V[1];
5984  FTYPE E,Fx,Fy,Fz;
5985  FTYPE ppback[NPR];
5986  PLOOP(pliter,pl) ppback[pl]=pp[pl]; // for initial backup, used to see if torus below atmosphere in donut
5987  // whichvelback and whichcoordback hold values before any make_nonrt2rt_solution call
5988  int whichvelback=*whichvel;
5989  int whichcoordback=*whichcoord;
5990 
5991  // get actual BL-coords in case ptrgeom not, because we need BL-coords to get sizes of cells in finite differences below
5992  int whichcoordreal=MCOORD;
5993  int getprim=0;
5994  struct of_geom geombldontuse;
5995  struct of_geom *ptrgeombl=&geombldontuse;
5996  gset(0,whichcoordreal,i,j,k,ptrgeombl); // for local differences using V.
5997 
5998 
5999  // get donut with whichvel / whichcoord / ptrptrgeom (or changed inside)
6000  int anret=make_nonrt2rt_solution(whichvel, whichcoord, opticallythick, pp,X,V,ptrptrgeom);
6001  if(DOWALDDEN){
6002  anret=1; // never inside donut for Wald
6003  }
6004 
6005 
6006 
6007  // see if inside donut solution or outside
6008  if(anret==0){ // then inside donut
6009  int anretlocal=0;
6010 
6012  if(PRAD0>=0){
6013  E=pp[PRAD0];
6014  Fx=pp[PRAD1];
6015  Fy=pp[PRAD2];
6016  Fz=pp[PRAD3];
6017  }
6018 
6019 
6020 
6022 
6023  //estimating F = -1/chi E,i
6024  //http://www.astro.wisc.edu/~townsend/resource/teaching/astro-310-F08/23-rad-diffusion.pdf
6025  // -(1/chi)dPrad/dx = Frad/c
6026  FTYPE kappa,kappaes,chi;
6027  // use of V assumes user knows which coordinates they are in (e.g. r,th,ph vs. x,y,z)
6028  FTYPE Tg=calc_PEQ_Tfromurho(pp[UU],pp[RHO]);
6029  FTYPE bsq,B;
6030  bsq_calc(pp,*ptrptrgeom,&bsq);
6031  B=sqrt(bsq);
6032  chi=
6033  calc_kappa_user(pp[RHO],B,Tg,Tg,1.0,V[1],V[2],V[3]) // Planck Tr=Tg at t=0
6034  +
6035  calc_kappaes_user(pp[RHO],calc_PEQ_Tfromurho(pp[UU],pp[RHO]),V[1],V[2],V[3]);
6036 
6037  FTYPE Vtemp1[NDIM],Vtemp2[NDIM];
6038  FTYPE Xtemp1[NDIM],Xtemp2[NDIM];
6039  FTYPE pptemp[NPR],E1=0,E2=0;
6040  getprim=(whichcoordback==PRIMECOORDS); // as consistent with pptemp being fed-in as backup
6041  int anretmin=0;
6042  struct of_geom geomtdontuse;
6043  struct of_geom *ptrgeomt=&geomtdontuse;
6044 
6046 
6047  //r dimension
6048  Xtemp1[0]=X[0];
6049  Xtemp1[1]=1.01*X[1];
6050  Xtemp1[2]=1.0*X[2];
6051  Xtemp1[3]=1.0*X[3];
6052  bl_coord(Xtemp1,Vtemp1); // only needed for Vtemp1[1] for radius in make_nonrt2rt_solution()
6053  gset_X(getprim,whichcoordback,i,j,k,NOWHERE,Xtemp1,ptrgeomt);
6054  PLOOP(pliter,pl) pptemp[pl]=ppback[pl]; // ppback that holds unchanged pp, not modified by previous make_nonrt2rt_solution() call.
6055 
6056  anretlocal=make_nonrt2rt_solution(&whichvelback, &whichcoordback, opticallythick, pptemp,Xtemp1,Vtemp1,&ptrgeomt);
6057  if(anretlocal<0) anretmin=-1;
6058  if(PRAD0>=0){
6059  E1=pptemp[PRAD0]; // fluid frame E
6060  }
6061 
6062  Xtemp2[0]=X[0];
6063  Xtemp2[1]=.99*X[1];
6064  Xtemp2[2]=1.0*X[2];
6065  Xtemp2[3]=1.0*X[3];
6066  bl_coord(Xtemp2,Vtemp2); // only needed for Vtemp2[1] for radius in make_nonrt2rt_solution()
6067  gset_X(getprim,whichcoordback,i,j,k,NOWHERE,Xtemp2,ptrgeomt);
6068  PLOOP(pliter,pl) pptemp[pl]=ppback[pl]; // ppback that holds unchanged pp, not modified by previous make_nonrt2rt_solution() call.
6069 
6070  anretlocal=make_nonrt2rt_solution(&whichvelback, &whichcoordback, opticallythick, pptemp,Xtemp2,Vtemp2,&ptrgeomt);
6071  if(anretlocal<0) anretmin=-1;
6072  if(PRAD0>=0){
6073  E2=pptemp[PRAD0]; // fluid frame E
6074  }
6075 
6076  // fluid frame Fx
6077  // Fx=(E2-E1)/(.02*V[1]*(ptrgeombl->gcov[GIND(1,1)]))/chi/3.;
6078  Fx=-THIRD*(E2-E1)/((Vtemp2[1]-Vtemp1[1])*sqrt(fabs(ptrgeombl->gcov[GIND(1,1)])))/chi;
6079  // dualfprintf(fail_file,"E2=%g E1=%g Fx=%g\n",E2,E1,Fx);
6080 
6082 
6083  //th dimension
6084  Xtemp1[0]=X[0];
6085  Xtemp1[1]=1.0*X[1];
6086  Xtemp1[2]=1.01*X[2];
6087  Xtemp1[3]=1.0*X[3];
6088  bl_coord(Xtemp1,Vtemp1); // only needed for Vtemp1[2] for theta in make_nonrt2rt_solution()
6089  gset_X(getprim,whichcoordback,i,j,k,NOWHERE,Xtemp1,ptrgeomt);
6090  PLOOP(pliter,pl) pptemp[pl]=ppback[pl]; // ppback that holds unchanged pp, not modified by previous make_nonrt2rt_solution() call.
6091 
6092  anretlocal=make_nonrt2rt_solution(&whichvelback, &whichcoordback, opticallythick, pptemp,Xtemp1,Vtemp1,&ptrgeomt);
6093  if(anretlocal<0) anretmin=-1;
6094  if(PRAD0>=0){
6095  E1=pptemp[PRAD0]; // fluid frame E1
6096  }
6097 
6098  Xtemp2[0]=X[0];
6099  Xtemp2[1]=1.0*X[1];
6100  Xtemp2[2]=0.99*X[2];
6101  Xtemp2[3]=1.0*X[3];
6102  bl_coord(Xtemp2,Vtemp2); // only needed for Vtemp2[2] for theta in make_nonrt2rt_solution()
6103  gset_X(getprim,whichcoordback,i,j,k,NOWHERE,Xtemp2,ptrgeomt);
6104  PLOOP(pliter,pl) pptemp[pl]=ppback[pl]; // ppback that holds unchanged pp, not modified by previous make_nonrt2rt_solution() call.
6105 
6106  anretlocal=make_nonrt2rt_solution(&whichvelback, &whichcoordback, opticallythick, pptemp,Xtemp2,Vtemp2,&ptrgeomt);
6107  if(anretlocal<0) anretmin=-1;
6108  if(PRAD0>=0){
6109  E2=pptemp[PRAD0]; // fluid frame E2
6110  }
6111 
6112  // fluid frame Fy
6113  // Fy=(E2-E1)/(.02*V[2]*(ptrgeombl->gcov[GIND(2,2)]))/chi/3.;
6114  Fy=-THIRD*(E2-E1)/((Vtemp2[2]-Vtemp1[2])*sqrt(fabs(ptrgeombl->gcov[GIND(2,2)])))/chi;
6115  //dualfprintf(fail_file,"E2=%g E1=%g Fy=%g\n",E2,E1,Fy);
6116 
6118 
6119  //ph dimension
6120  // Fz=0.; // fluid frame Fz
6121  Xtemp1[0]=X[0];
6122  Xtemp1[1]=1.0*X[1];
6123  Xtemp1[2]=1.0*X[2];
6124  Xtemp1[3]=1.01*X[3];
6125  bl_coord(Xtemp1,Vtemp1); // only needed for Vtemp1[3] for phi in make_nonrt2rt_solution()
6126  gset_X(getprim,whichcoordback,i,j,k,NOWHERE,Xtemp1,ptrgeomt);
6127  PLOOP(pliter,pl) pptemp[pl]=ppback[pl]; // ppback that holds unchanged pp, not modified by previous make_nonrt2rt_solution() call.
6128 
6129  anretlocal=make_nonrt2rt_solution(&whichvelback, &whichcoordback, opticallythick, pptemp,Xtemp1,Vtemp1,&ptrgeomt);
6130  if(anretlocal<0) anretmin=-1;
6131  if(PRAD0>=0){
6132  E1=pptemp[PRAD0]; // fluid frame E1
6133  }
6134 
6135  Xtemp2[0]=X[0];
6136  Xtemp2[1]=1.0*X[1];
6137  Xtemp2[2]=1.0*X[2];
6138  Xtemp2[3]=0.99*X[3];
6139  bl_coord(Xtemp2,Vtemp2); // only needed for Vtemp2[3] for phi in make_nonrt2rt_solution()
6140  gset_X(getprim,whichcoordback,i,j,k,NOWHERE,Xtemp2,ptrgeomt);
6141  PLOOP(pliter,pl) pptemp[pl]=ppback[pl]; // ppback that holds unchanged pp, not modified by previous make_nonrt2rt_solution() call.
6142 
6143  anretlocal=make_nonrt2rt_solution(&whichvelback, &whichcoordback, opticallythick, pptemp,Xtemp2,Vtemp2,&ptrgeomt);
6144  if(anretlocal<0) anretmin=-1;
6145  if(PRAD0>=0){
6146  E2=pptemp[PRAD0]; // fluid frame E2
6147  }
6148 
6149  // fluid frame Fz
6150  Fz=-THIRD*(E2-E1)/((Vtemp2[3]-Vtemp1[3])*sqrt(fabs(ptrgeombl->gcov[GIND(3,3)])))/chi;
6151  //dualfprintf(fail_file,"E2=%g E1=%g Fz=%g\n",E2,E1,Fz);
6152 
6153 
6154 
6156 
6157  if(anretmin<0){ // then ended up with one point outside solution
6158  Fx=Fy=Fz=0.;
6159  // dualfprintf(fail_file,"OUTSIDE: ijk=%d %d %d\n",i,j,k);
6160  }
6161  else{ // then inside solution
6162  // dualfprintf(fail_file,"INSIDE: ijk=%d %d %d\n",i,j,k);
6163  FTYPE MAXFOE;
6164 
6165  MAXFOE=0.70; // KORALTODO: SUPERGODMARK: Kraken failed with this set to 0.99 where hit 3vel calculation with disc=+1E-16. So just marginal beyond v=c, but with 0.99 why did that happen?
6166 
6167  // if(r>1E3) MAXFOE=0.1;
6168 
6169 
6170  FTYPE Fl=sqrt(Fx*Fx+Fy*Fy+Fz*Fz);
6171  if(Fl>MAXFOE*E){
6172  Fx=Fx/Fl*MAXFOE*E;
6173  Fy=Fy/Fl*MAXFOE*E;
6174  Fz=Fz/Fl*MAXFOE*E;
6175  FTYPE Flnew=sqrt(Fx*Fx+Fy*Fy+Fz*Fz);
6176  if(PRODUCTION==0) dualfprintf(fail_file,"limited: Fl=%g E=%g Fx=%g Fy=%g Fz=%g : Flnew=%g\n",Fl,E,Fx,Fy,Fz,Flnew);
6177  }
6178  }
6179 
6180 
6181  if(PRAD0>=0){
6182  //saving ff values to pp[] (so any function using this function should know pp has fluid frame orthonormal values in pp[PRAD0-PRAD3] as was in the input as well.
6183  pp[PRAD1]=Fx;
6184  pp[PRAD2]=Fy;
6185  pp[PRAD3]=Fz;
6186  }
6187 
6188 
6189 
6190  }// end if adding donut
6191 
6192 
6193  if(anret==0) return(anret);
6194  else return(-1);
6195 
6196 }
6197 
6198 
6199 // convert non-radiative solution to radiative one by splitting total pressure up into gas + radiation pressure with optical depth corrections
6200 // expects pp[PRAD0-PRAD3] to be fluid frame orthonormal, while pp[U1-U3] is ptrgeom whichvel whichcoord lab frame value (doesn't change U1-U3)
6201 // returns: pp and can change whichvel and whichcoord
6202 static int make_nonrt2rt_solution(int *whichvel, int *whichcoord, int opticallythick, FTYPE *pp,FTYPE *X, FTYPE *V, struct of_geom **ptrptrgeom)
6203 {
6204  // get location
6205  int i=(*ptrptrgeom)->i;
6206  int j=(*ptrptrgeom)->j;
6207  int k=(*ptrptrgeom)->k;
6208  int p=(*ptrptrgeom)->p;
6209 
6210 
6211 
6213  //
6214  // choose non-radiative solution to start from
6215  //
6217  // total torus pressure
6218  FTYPE pt;
6219  int usingback=donut_analytical_solution(whichvel, whichcoord, opticallythick, pp, X, V, ptrptrgeom, &pt);
6220  //int usingback=process_solution(whichvel, whichcoord, opticallythick, pp, X, V, ptrptrgeom, &pt);
6221 
6222  // assign to names
6223  FTYPE rho=pp[RHO];
6224  FTYPE uint=pp[UU];
6225  FTYPE E=pp[URAD0];
6226  FTYPE Fx=pp[URAD1];
6227  FTYPE Fy=pp[URAD2];
6228  FTYPE Fz=pp[URAD3];
6229 
6230  // dualfprintf(fail_file,"rho=%g usingback=%d\n",rho,usingback);
6231 
6233  //
6234  // get actual temperature and separate internal energies for gas and radiation
6235  //
6237  FTYPE P,aaa,bbb;
6238  P=pt; // torus total pressure
6239  FTYPE Teq=calc_PEQ_Tfromurho(uint,rho);
6240 
6241 
6242  // initial equilibrium optically thick version of gas temperature
6243  FTYPE Tgas=Teq;
6244 
6245  // loop over to get correct gas temperature since kappa(Tgas) that affects Tgas.
6246 #define NUMTgasITERS 4 // for now, was 4.
6247  int iter=0;
6248  for(iter=0;iter<NUMTgasITERS;iter++){
6249 
6250  // 2-stream approximation for pressure
6251  FTYPE kappaabs,kappaes,kappatot;
6252  // use of V assumes user knows which coordinates they are in (e.g. r,th,ph vs. x,y,z)
6253  FTYPE bsq,B;
6254  bsq_calc(pp,*ptrptrgeom,&bsq);
6255  B=sqrt(bsq);
6256  kappaabs=calc_kappa_user(rho,B,Tgas,Tgas,1.0,V[1],V[2],V[3]); // Planck and Tr=Tgas at t=0
6257  kappaes=calc_kappaes_user(rho,Tgas,V[1],V[2],V[3]);
6258  kappatot=kappaabs+kappaes;
6259 
6260  // fake integral of opacity as if opacity roughly uniform and heading out away in angular distance at fixed angluar extent. Could integrate to pole, but harder with MPI.
6261  FTYPE r=V[1];
6262  FTYPE th=V[2];
6263  FTYPE dl;
6264  dl = r*MAX(h_over_r-fabs(th-M_PI*0.5),0.0);
6265 
6266  FTYPE dxdxp[NDIM][NDIM];
6267  if(p==NOWHERE) dxdxprim_ijk(i, j, k, CENT, dxdxp); // don't use p, use CENT, because nowhere when differencing
6268  else dxdxprim_ijk(i, j, k, p, dxdxp); // don't use p, use CENT, because nowhere when differencing
6269  dl = r*MAX(dl,dx[2]*dxdxp[2][2]);
6270 
6271  FTYPE tautot,tauabs;
6272  tautot = kappatot*dl+SMALL;
6273  tauabs = kappaabs*dl+SMALL;
6274  FTYPE gtau = (tautot/2.0 + 1.0/sqrt(3.0))/(tautot/2.0 + 1.0/sqrt(3.0) + 1.0/(3.0*tauabs));
6275 
6276  // dualfprintf(fail_file,"dl=%g tautot=%g tauabs=%g\n",dl,tautot,tauabs);
6277 
6278 
6279 
6280  if(usingback==0){// && gtau>1E-10){
6281  gtau=1.0; // TESTING
6282  opticallythick=1;
6283  //solving for T satisfying P=pgas+prad=bbb T + aaa T^4
6284  aaa=(4.0/3.0-1.0)*ARAD_CODE*gtau; // Prad=(gamma-1)*urad*g[tau] and gamma=4/3 and urad=arad*T^4*g[tau]
6285  bbb=rho;
6286 
6287  // dualfprintf(fail_file,"aaa=%g bbb=%g P=%g gtau=%g\n",aaa,bbb,P,gtau);
6288 
6289 
6290  FTYPE naw1=cbrt(9.*aaa*Power(bbb,2.) - Sqrt(3.)*Sqrt(27.*Power(aaa,2.)*Power(bbb,4.) + 256.*Power(aaa,3.)*Power(P,3.)));
6291  Tgas=-Sqrt((-4.*Power(0.6666666666666666,0.3333333333333333)*P)/naw1 + naw1/(Power(2.,0.3333333333333333)*Power(3.,0.6666666666666666)*aaa))/2. + Sqrt((4.*Power(0.6666666666666666,0.3333333333333333)*P)/naw1 - naw1/(Power(2.,0.3333333333333333)*Power(3.,0.6666666666666666)*aaa) + (2.*bbb)/(aaa*Sqrt((-4.*Power(0.6666666666666666,0.3333333333333333)*P)/naw1 + naw1/(Power(2.,0.3333333333333333)*Power(3.,0.6666666666666666)*aaa))))/2.;
6292  // cap Tgas for optically thin regions where density can be very low
6293  if(Tgas>1E12/TEMPBAR) Tgas=1E12/TEMPBAR;
6294  else{
6295  if(fabs(rho*Tgas + aaa*pow(Tgas,4.0)*gtau - P)>1E-5){
6296  dualfprintf(fail_file,"2: NOT right equation: rho=%26.21g Tgas=%26.21g aaa=%26.21g Ptried=%26.21g P=%26.21g\n",rho,Tgas,aaa,rho*Tgas + aaa*pow(Tgas,4.),P);
6297  }
6298  }
6299 
6300  if(!isfinite(Tgas)) Tgas=TEMPMIN;
6301 
6302  //dualfprintf(fail_file,"Tgas=%g\n",Tgas);
6303 
6304 
6305  }
6306  else{
6307 
6308  // gtau=SMALL; // say atmosphere is always optically thin, to avoid overly radiative atmosphere for low rest-mass densities.
6309  opticallythick=0;
6310  Tgas = P/rho;
6311  }
6312 
6313 
6314 
6315  if(opticallythick){
6316  uint=calc_PEQ_ufromTrho(Tgas,rho);
6317  // overwrite uint only if actually optically thick, otherwise stick with original pressure that assumed pure gas-based pressure
6318 
6319  E=calc_LTE_EfromT(Tgas); // assume LTE, so thermal equilibrium between gas and radiation
6320  Fx=Fy=Fz=0.;
6321  }
6322  else{
6323  // else keep E that is background E.
6324  // stick with uint -- GODMARK: WHY?
6325  // GODMARK: Should set uint more generally, not just if(opticallythick) above
6326  }
6327 
6328  // FTYPE rhoatm=RADNT_RHOATMMIN*pow(r/RADNT_ROUT,-1.5);
6329  // dualfprintf(fail_file,"rhodonut4=%g uint=%g Tgas=%g gtau=%g tautot=%g tauabs=%g dl=%g E=%g usingback=%d ratiorho=%g\n",rho,uint,Tgas,gtau,tautot,tauabs,dl,E,usingback,rho/rhoatm);
6330  }
6331 
6332 
6333  // if doing radiation, then modify radiation using this result
6334  pp[RHO]=rho;
6335  pp[UU]=uint;
6336  if(PRAD0>=0){
6337  // fluid frame orthonormal values for radiation
6338  pp[PRAD0]=E;
6339  pp[PRAD1]=Fx;
6340  pp[PRAD2]=Fy;
6341  pp[PRAD3]=Fz;
6342  }
6343 
6344  if(usingback){
6345  return(-1); // tells to not form radiative flux for atmosphere
6346  }
6347  return 0;
6348 }
6349 
6350 
6351 
6352 
6353 // analytical solution for RADDONUT donut
6354 // expects pp[PRAD0-PRAD3] to be fluid frame orthonormal, while pp[U1-U3] is ptrgeom whichvel whichcoord lab frame value (doesn't change U1-U3)
6355 // RETURNS: pp, uTptr, ptptr and can return whichvel and whichcoord and ptrptrgeom
6356 static int donut_analytical_solution(int *whichvel, int *whichcoord, int opticallythick, FTYPE *pp,FTYPE *X, FTYPE *V,struct of_geom **ptrptrgeom, FTYPE *ptptr)
6357 {
6358  int usingback=0;
6359  FTYPE Vphi=0.0,Vr=0.0,Vh=0.0;
6360  FTYPE D,W,uT=1.0,uphi,uPhi,rho,ucon[NDIM],uint,E,Fx,Fy,Fz;
6361  // FTYPE rho,uint,uT=1.0,E,Fx,Fy,Fz;
6362 
6363  // set backup atmosphere value for primitives
6364  int pliter,pl;
6365  FTYPE ppback[NPR];
6366  PLOOP(pliter,pl) ppback[pl]=pp[pl];
6367 
6368  // get location (NOTE: not affected by whichcoord)
6369  FTYPE r=V[1];
6370  FTYPE th=V[2];
6371  FTYPE Rcyl=fabs(r*sin(th));
6372  int i=(*ptrptrgeom)->i;
6373  int j=(*ptrptrgeom)->j;
6374  int k=(*ptrptrgeom)->k;
6375  int p=(*ptrptrgeom)->p;
6376 
6377 
6378  // choose \gamma ideal gas constant for torus (KORALTODO: Could interpolate based upon some estimate of \tau)
6379  FTYPE gamtorus;
6380  if(opticallythick==1) gamtorus=4.0/3.0; // then should be as if gam=4/3 so radiation supports torus properly at t=0
6381  else gamtorus=gam;
6382  // total torus pressure that will be distributed among gas and radiation pressure
6383  FTYPE pt;
6384 
6385 
6386 
6387 
6388  // get density and velocity for torus
6390 
6391  int mycoords=BLCOORDS;
6392  if(*whichcoord!=mycoords){
6393  // get metric grid geometry for these ICs
6394  int getprim=0;
6395  gset_X(getprim,mycoords,i,j,k,NOWHERE,X,*ptrptrgeom);
6396  }
6397  *whichcoord=mycoords;
6398  *whichvel=VEL3;
6399 
6400 
6401 
6402 
6403  /* region outside disk */
6404  Rhor=rhor_calc(0);
6405  if(0){
6406  Risco=rmso_calc(PROGRADERISCO);
6407  }
6408  else{ // SUPERMADNEW
6409  Risco=10.0;
6410  }
6411  R = MAX(Rhor,r*sin(th)) ;
6412 
6414  // Set H : disk height
6415  FTYPE H = h_over_r*R;
6416  FTYPE Hisco = h_over_r*Risco;
6417 
6418  // or try:
6419  // FTYPE xrpow=0.5;
6420  FTYPE xrpow=1.0;
6421  FTYPE xr=pow(r,xrpow);
6422  FTYPE xr0=pow(Risco,xrpow);
6423  FTYPE xr1=pow(1.0,xrpow);
6424  // H = 0.1*h_over_r*R + h_over_r*R * (MAX(0.0,xr-xr0)/xr) ;
6425  // Hisco = 0.1*h_over_r*Risco + h_over_r*Risco * (MAX(0.0,xr0-xr0)/xr0) ;
6426  if(r>=Risco){
6427  H = h_over_r*R ;
6428  }
6429  else{
6430  // H = h_over_r*R * pow(r/Risco,2.0);
6431  H = h_over_r*R * pow(r/Risco,.5); // SUPERMADNEW
6432  }
6433 
6434  FTYPE z = r*cos(th) ;
6435  FTYPE zisco = Risco*cos(th) ;
6436 
6438  // SET DENSITY
6439  //
6440  // simple power-law with some vertical distribution
6441  //
6442  FTYPE rhoisco;
6443  if(1){
6444  // below Gaussian for isothermal gas, but works better for adiabatic EOS too.
6445  rhoisco=RADNT_RHODONUT * exp(-zisco*zisco/(2.*Hisco*Hisco))*pow(Risco,thindiskrhopow);
6446  rho=RADNT_RHODONUT * exp(-z*z/(2.*H*H))*pow(r,thindiskrhopow);
6447  }
6448  if(0){
6449  // for adiabatic EOS, need improved vertical distribution
6450  FTYPE NN=1.0/(gamtorus-1.0);
6451  rhoisco=RADNT_RHODONUT * pow(1.0-zisco*zisco/(Hisco*Hisco),NN)*pow(Risco/Risco,thindiskrhopow); if(rhoisco<0.0||!isfinite(rhoisco)) rhoisco=0.0;
6452  rho=RADNT_RHODONUT * pow(1.0-z*z/(H*H),NN)*pow(r/Risco,thindiskrhopow); if(rho<0.0||!isfinite(rho)) rho=0.0;
6453  }
6454  // density shouldn't peak at ISCO, but further out even for H/R=0.05 (see Penna et al. 2010 figure 11).
6455  FTYPE Rtrans;
6456  // Rtrans=2.0*Risco;
6457  Rtrans=1.5*Risco; // SUPERMADNEW
6458  if(r<Rtrans){
6459  // FTYPE rhopowisco=10.0;
6460  FTYPE rhopowisco=7.0; // this gives similar result to HD case, although not MHD-turbulence case, but want kinda HD equilibrium as much as possible.
6461  rho = rho*pow(r/Rtrans,rhopowisco);
6462  }
6463 
6464 
6465  // see if should still use backup non-torus values
6466  if(rho<ppback[RHO]){
6467  usingback=1;
6468  }
6469 
6470  if(DOWALDDEN){
6471  usingback=1;
6472  }
6473 
6475  // ensure c_s/v_k = (H/R) , which should be violated in the ISCO such that K=P/rho^Gamma=constant but H/R still thins towards the horizon.
6476  // P = (gamma-1)*u and cs2 = \gamma P/(\rho+u+P)
6477  FTYPE omegakep=1./(pow(r,1.5) + a);
6478  FTYPE omegakepisco=1./(pow(Risco,1.5) + a);
6479  FTYPE omega,omegaisco=omegakepisco;
6480 
6481  FTYPE sigma=r*r+a*a*cos(th)*cos(th);
6482  FTYPE gppvsr=sigma + a*a*(1+2*r/sigma)*sin(th)*sin(th);
6483  FTYPE sigmaisco=Risco*Risco+a*a*cos(th)*cos(th);
6484  FTYPE gppvsrisco=sigmaisco + a*a*(1+2*Risco/sigmaisco)*sin(th)*sin(th);
6485  if(1||r>=Risco){ // final HD solution is Keplerian over all radii!
6486  omega=omegakep;
6487  }
6488  else{
6489  omega=omegaisco*gppvsrisco/gppvsr;
6490  }
6491  uPhi = omega ; // Omega = v^\phi = 3-vel = d\phi/dt
6492  uPhi*=SLOWFAC;
6493  // FTYPE grr=(*ptrptrgeom)->gcov[GIND(3,3)]; // BL
6494  FTYPE gtt=-(1.0-2.0*r/sigma);
6495  FTYPE gpp=sin(th)*sin(th)*(sigma+a*a*(1.0+2.0*r/sigma)*sin(th)*sin(th)); // KS
6496  uT = 1.0/pow(-gtt-uPhi*uPhi*gpp,0.5); // approximate \gamma = 1/sqrt(1-v^2) ~ 1/sqrt(1-vphi^2)
6497  FTYPE Delta=r*r-2*r+a*a;
6498  FTYPE AA=pow(r*r+a*a,2.0)-a*a*Delta*sin(th)*sin(th);
6499  FTYPE gttbl=-(1.0-2.0*r/sigma);
6500  FTYPE gppbl=AA*sin(th)*sin(th)/sigma;
6501  FTYPE uTbl = 1.0/pow(-gttbl-uPhi*uPhi*gppbl,0.5); // \gamma = 1/sqrt(1-v^2) ~ 1/sqrt(1-vphi^2) for BL-coords with u^r=u^\theta=0 in BL coords
6502 
6504  // SET
6505  if(r>=Risco){
6506  Vphi=uPhi; // now 3-vel
6507  }
6508  else{
6509  // Vphi=uPhi/pow(r/Risco,4.0)/0.6/0.80;
6510  Vphi=uPhi;
6511  }
6512  // set radial velocity
6513  // if(r<Risco){
6514  if(r<1.1*Rhor){
6515  Vr = ppback[U1]; // zamo vel
6516  Vh = ppback[U2]; // zamo vel
6517  }
6518  else{
6519  Vr=Vh=0.0;
6520  }
6521 
6523  // TEST if velocity gives reasonable u^t
6524  FTYPE prtest[NPR];
6525  FTYPE ucontest[NDIM];
6526  FTYPE others[NUMOTHERSTATERESULTS];
6527  prtest[U1]=Vr;
6528  prtest[U2]=Vh;
6529  prtest[U3]=Vphi;
6530  // dualfprintf(fail_file,"BEFORE real ut\n");
6531  // int badut=ucon_calc_3vel(prtest,*ptrptrgeom,ucon,others);
6532  failed=-1; // GLOBAL: indicate not really failure, so don't print out debug info
6533  int badut=ucon_calc_whichvel(*whichvel,prtest,*ptrptrgeom,ucon,others);
6534  failed=0; // GLOBAL: reset failure flag since just test
6535  //if(badut==0) dualfprintf(fail_file,"real ut=%g\n",ucon[TT]);
6536 
6537  // CHECK TEST
6538  if(usingback==0){
6539  // CATCH
6540  // if(r<Risco || badut){// uT>10.0 || !isfinite(uT) || uTbl>10.0 || !isfinite(uTbl) || uT<1.0-1E-7 || uTbl<1.0-1E-7){
6541  if(badut==1){
6542  // uT=10.0;
6543  // uTbl=10.0;
6544  Vphi=0.0;//uPhi*uT; ///pow(r/Risco,4.0); // Vphi is 4-vel = d\phi/d\tau = u^t \Omega
6545  // set radial velocity
6546  Vr = 0. ; // zero zamo vel
6547  Vh = 0. ; // zero zamo vel
6548  *whichvel=VELREL4;
6549  if(*whichcoord==BLCOORDS) *whichcoord=KSCOORDS;
6550  }
6551  else if(r<1.1*Rhor){
6552  // uT=10.0;
6553  // uTbl=10.0;
6554  Vphi=0.0;//uPhi*uT; ///pow(r/Risco,4.0); // Vphi is 4-vel = d\phi/d\tau = u^t \Omega
6555  // set radial velocity
6556  Vr = 0. ; // zero zamo vel
6557  Vh = 0. ; // zero zamo vel
6558  *whichvel=VELREL4;
6559  if(*whichcoord==BLCOORDS) *whichcoord=KSCOORDS;
6560  }
6561  else{
6562  // i.e. keep whichvel, whichcoord the same
6563  *whichvel=VEL3;
6564  *whichcoord=BLCOORDS;
6565  }
6566  //dualfprintf(fail_file,"uT=%g uTbl=%g\n",uT,uTbl);
6567  }
6568  else{
6569  // i.e. keep whichvel, whichcoord the same
6570  *whichvel=VEL3;
6571  *whichcoord=BLCOORDS;
6572  }
6573 
6574  // OVERRIDE
6575  if(r>1.2*Rhor){
6576  *whichvel=VEL3;
6577  *whichcoord=BLCOORDS;
6578  }
6579 
6580 
6581  // set uint
6582  FTYPE uintfact = (1.0 + randfact * (ranc(0,0) - 0.5));
6583  uint = rho*pow(H/R,2.0)*pow(r*omega,2.0)/(gamtorus*(gamtorus-1.0))*uintfact;
6584  FTYPE uintisco = rhoisco*pow(Hisco/Risco,2.0)*pow(Risco*omegaisco,2.0)/(gamtorus*(gamtorus-1.0))*uintfact;
6585 
6586  // Set pressure
6587  // K = P/rho^Gamma
6588  FTYPE pint=(gamtorus-1.0)*uint;
6589  FTYPE KK= pint*pow(rho,-gamtorus);
6590  FTYPE pintisco=(gamtorus-1.0)*uintisco;
6591  FTYPE KKisco= pintisco*pow(rhoisco,-gamtorus);
6592  if(r>=Risco){
6593  pint = pint;
6594  }
6595  else{
6596  // Have constant K inside isco, not uint
6597  pint = pintisco*pow(rhoisco,-gamtorus)*pow(rho,gamtorus);
6598  }
6599  // get total pressure
6600  pt = uint * (gamtorus-1.0); // torus pressure
6601 
6602 
6603 
6604 
6605 
6606  // dualfprintf(fail_file,"rhodonut1=%g uint=%g\n",rho,uint);
6607 
6608  // if(fabs(r-Risco)<0.1*Risco && fabs(th-0.5*M_PI)<0.2*h_over_r){
6609  // dualfprintf(fail_file,"rhodonut5=%g uint=%g : r,th=%g %g usingback=%d RADNT_RHODONUT=%g rhoisco=%g\n",rho,uint,r,th,usingback,RADNT_RHODONUT,rhoisco);
6610  // }
6611 
6612 
6613  }
6614  // get density and velocity for torus
6615  else if(RADNT_DONUTTYPE==DONUTTHINDISK){
6616 
6617  int mycoords=BLCOORDS;
6618  if(*whichcoord!=mycoords){
6619  // get metric grid geometry for these ICs
6620  int getprim=0;
6621  gset_X(getprim,mycoords,i,j,k,NOWHERE,X,*ptrptrgeom);
6622  }
6623  *whichcoord=mycoords;
6624  *whichvel=VEL3;
6625 
6626 
6627  SFTYPE sth, cth;
6628  // struct of_geom geomdontuse;
6629  // struct of_geom *ptrgeom=&geomdontuse;
6630  /* for disk interior */
6631  FTYPE R,H,nz,z,S,cs ;
6632  SFTYPE rh;
6633  // int pl,pliter;
6634 
6635 
6636  /* region outside disk */
6637  Rhor=rhor_calc(0);
6638  Risco=rmso_calc(PROGRADERISCO);
6639  R = MAX(Rhor,r*sin(th)) ;
6640 
6641  //if(R < rin) { // assume already have atmosphere as background solution
6642  FTYPE SMALLESTH=1E-15;
6643 
6644  // H = SMALLESTH + h_over_r*R ; // NOTEMARK: Could choose H=constant
6645 
6646  // FTYPE Rtrans=1.5*Risco;
6647  FTYPE xrpow=0.5;
6648  FTYPE xr=pow(r,xrpow);
6649  FTYPE xr0=pow(Risco,xrpow);
6650  FTYPE xr1=pow(1.0,xrpow);
6651 
6652  FTYPE HS = h_over_r*R;
6653  FTYPE HSisco;
6654  HSisco = h_over_r*Risco;
6655 
6656  FTYPE Hisco;
6657  if(0){
6658  H = 0.1*h_over_r*R + h_over_r*R * (MAX(0.0,xr-xr0)/xr) ; // NOTEMARK: Could choose H=constant
6659  Hisco = 0.1*h_over_r*R;
6660  }
6661  else{
6662  H = HS;
6663  Hisco = HSisco;
6664  }
6665 
6666  // fix nz
6667  nz = nz_func(R) ;
6668  nz = pow(R,-1.5);
6669  FTYPE nzisco = nz_func(Risco) ;
6670  if(nz>1.0) nz=1.0;
6671  if(r<Risco) nz=nzisco;
6672  if(!isfinite(nz)) nz=1.0;
6673 
6674  z = r*cos(th) ;
6675  FTYPE zisco = Risco*cos(th) ;
6676  S = 1./(HS*HS*nz) ;
6677  FTYPE Sisco=1./(HSisco*HSisco*nzisco) ;
6678  cs = H*nz ;
6679  FTYPE csisco = Hisco*nzisco ;
6680 
6681  FTYPE rho0=RADNT_RHODONUT*(S/sqrt(2.*M_PI*HS*HS)) * exp(-z*z/(2.*H*H))*pow(r,3.0/2.0+thindiskrhopow);
6682  FTYPE rho0isco=RADNT_RHODONUT*(Sisco/sqrt(2.*M_PI*HSisco*HSisco)) * exp(-zisco*zisco/(2.*Hisco*Hisco))*pow(Risco,3.0/2.0+thindiskrhopow);
6683  if(r>Risco){
6684  rho = rho0 * (MAX(0.0,xr-xr1)/xr) ;
6685  }
6686  else{
6687  FTYPE rhopowisco=10.0;
6688  rho = (rho0isco * (MAX(0.0,xr-xr1)/xr)) * pow(r/Risco,rhopowisco);
6689  }
6690  uint = rho*cs*cs/(gamtorus - 1.) ;
6691  Vr = 0. ;
6692  uPhi = 1./(pow(r,1.5) + a) ;
6693  FTYPE uPhiisco = 1./(pow(Risco,1.5) + a) ;
6694  // solution for 3-vel
6695 
6696 
6697  // uint = uint * (1. + (1.+3.*(rin/R)*(rin/R))*randfact * (ranc(0,0) - 0.5));
6698  uint *= (1.0 + randfact * (ranc(0,0) - 0.5));
6699  uPhi*=SLOWFAC;
6700  Vphi=uPhi; // approximate
6701  FTYPE Vphiisco=uPhiisco;
6702 
6704  // get total pressure
6705  pt = uint * (gamtorus-1.0); // torus pressure
6706 
6707  // see if should still use backup non-torus values
6708  if(rho<ppback[RHO]){
6709  usingback=1;
6710  }
6711 
6712  if(DOWALDDEN){
6713  usingback=1;
6714  }
6715 
6716 
6717 
6718  if(r<Risco){
6719  FTYPE sigma=r*r+a*a*cos(th)*cos(th);
6720  FTYPE gpp=sigma + a*a*(1+2*r/sigma)*sin(th)*sin(th);
6721  FTYPE sigmaisco=Risco*Risco+a*a*cos(th)*cos(th);
6722  FTYPE gppisco=sigmaisco + a*a*(1+2*Risco/sigmaisco)*sin(th)*sin(th);
6723  Vphi=Vphiisco*gppisco/gpp;
6724  }
6725 
6726  if(1||(r<1.3*Rhor || r<Risco) && usingback==0){
6727  *whichvel=VELREL4;
6728  if(*whichcoord==BLCOORDS) *whichcoord==KSCOORDS;
6729  }
6730  else{
6731  *whichvel=VEL3;
6732  // *whichcoord=BLCOORDS;
6733  }
6734 
6735  // if(r<Risco){ // TEMP
6736  // return -1; //outside disk
6737  // }
6738 
6739 
6740 
6741  // dualfprintf(fail_file,"nz=%g z=%g S=%g cs=%g H=%g h_over_r=%g\n",nz,z,S,cs,H,h_over_r);
6742 
6743  // dualfprintf(fail_file,"rhodonut1=%g uint=%g\n",rho,uint);
6744 
6745 
6746  }
6747  else if(RADNT_DONUTTYPE==DONUTOLEK){
6748 
6749  int mycoords=BLCOORDS;
6750  // int mycoords=KSCOORDS;
6751  if(*whichcoord!=mycoords){
6752  // get metric grid geometry for these ICs
6753  int getprim=0;
6754  gset_X(getprim,mycoords,i,j,k,NOWHERE,X,*ptrptrgeom);
6755  }
6756  *whichcoord=mycoords;
6757  *whichvel=VEL3;
6758 
6759  FTYPE podpierd=-(((*ptrptrgeom)->gcon[GIND(0,0)])-2.*RADNT_ELL*((*ptrptrgeom)->gcon[GIND(0,3)])+RADNT_ELL*RADNT_ELL*((*ptrptrgeom)->gcon[GIND(3,3)]));
6760  FTYPE ut=-1./sqrt(podpierd);
6761  if(!isfinite(ut)) ut=-1.0; // so skips donut, but doesn't give false in condition below (i.e. condition controlled by only podpierd)
6762 
6763  ut/=RADNT_UTPOT; //rescales rin
6764  // below assumes no torus in unbound region
6765  if(ut<-1 || podpierd<0. || r<3. || RADNT_DONUTTYPE==NODONUT || RADNT_INFLOWING){
6766  // allow torus to be unbound with radiation -- not a problem
6767  // if(podpierd<0. || r<3. || RADNT_DONUTTYPE==NODONUT || RADNT_INFLOWING)
6768  //return -1; //outside donut
6769  usingback=1;
6770  }
6771 
6772  FTYPE h=-1./ut;
6773  FTYPE eps=(h-1.)/gamtorus;
6774  if(usingback==0){
6775  // from P=K rho^gamma
6776  rho=pow(eps*(gamtorus-1.)/RADNT_KKK,1./(gamtorus-1.));
6777  uint=rho*eps;
6778  pt = uint * (gamtorus-1.0); // torus pressure
6779  uphi=-RADNT_ELL*ut; // i.e. constant angular momentum torus
6780  uT=((*ptrptrgeom)->gcon[GIND(0,0)])*ut+((*ptrptrgeom)->gcon[GIND(0,3)])*uphi;
6781  uPhi=((*ptrptrgeom)->gcon[GIND(3,3)])*uphi+((*ptrptrgeom)->gcon[GIND(0,3)])*ut;
6782  Vphi=uPhi/uT;
6783  Vr=0.;
6784  }
6785 
6786 
6787 
6788  // see if should still use backup non-torus values
6789  if(rho<ppback[RHO]){
6790  usingback=1;
6791  }
6792  if(DOWALDDEN){
6793  usingback=1;
6794  }
6795 
6796 
6797 
6798 
6799  // dualfprintf(fail_file,"rhodonut1=%g eps=%g uint=%g usingback=%d\n",rho,eps,uint,usingback);
6800 
6801  }
6802  else if(RADNT_DONUTTYPE==DONUTOHSUGA){
6803 
6804  int mycoords=BLCOORDS;
6805  if(*whichcoord!=mycoords){
6806  // get metric grid geometry for these ICs
6807  int getprim=0;
6808  gset_X(getprim,mycoords,i,j,k,NOWHERE,X,*ptrptrgeom);
6809  }
6810  *whichcoord=mycoords;
6811  *whichvel=VEL3;
6812 
6813  FTYPE rs=2.0; // Schwarzschild radius
6814  FTYPE r0=RADNT_DONUTRADPMAX; // pressure maximum radius // FREEPAR
6815  FTYPE l0=pow(r0,1.5)/(r0-rs); // angular momentum at pressure maximum [type in S3.3 in Ohsuga in text for l0]
6816  // FTYPE aa=0.46; // FREEPAR
6817  FTYPE aa=RADNT_LPOW; // FREEPAR
6818  FTYPE ll=l0*pow(Rcyl/r0,aa); // l ~ r^aa (i.e. non-constant angular momentum)
6819  FTYPE rhoc=RADNT_RHODONUT; // density at center of torus
6820  FTYPE nn=1.0/(gamtorus-1.0); //3.0; // -> 1 + 1/n = gamma -> n = 1/(gamma-1) : gamma=4/3 -> n=3
6821  // so p = P = K \rho^((n+1)/n) = K \rho^(1 + 1/n) = K \rho^\gamma [typo in S3.3 in Ohsuga in text part]
6822  // Neutron stars are well modeled by polytropes with index about in the range between n=0.5 and n=1.
6823  // A polytrope with index n=1.5 is a good model for fully convective star cores (like those of red giants), brown dwarfs, giant gaseous planets (like Jupiter), or even for rocky planets.
6824  // Main sequence stars like our Sun and relativistic degenerate cores like those of white dwarfs are usually modeled by a polytrope with index n=3, corresponding to the Eddington standard model of stellar structure.
6825  //A polytrope with index n=5 has an infinite radius. It corresponds to the simplest plausible model of a self-consistent stellar system, first studied by A. Schuster in 1883.
6826  //A polytrope with index n=\infty corresponds to what is called isothermal sphere, that is an isothermal self-gravitating sphere of gas, whose structure is identical with the structure of a collisionless system of stars like a globular cluster.
6827  //Note that the higher the polytropic index, the more condensed at the centre is the density distribution.
6828  // FTYPE phi = -(1.0+(*ptrptrgeom)->gcov[GIND(0,0)]); // rough potential, accurate for a=0
6829  FTYPE phi = -1.0/(r-rs);
6830  FTYPE phieq = -1.0/(Rcyl-rs);
6831  FTYPE phir0 = -1.0/(r0-rs); // estimate, good enough for that large radius
6832  FTYPE phieff=phi + 0.5*pow(ll/Rcyl,2.0)/(1.0-aa);
6833  FTYPE phieffr0=phir0 + 0.5*pow(l0/r0,2.0)/(1.0-aa);
6834  FTYPE phieffeq=phieq + 0.5*pow(ll/Rcyl,2.0)/(1.0-aa);
6835  FTYPE rhot,rhotarg,rhoteq,rhotargeq;
6836  if(0){
6837  // Ohsuga & Mineshige (2011):
6838  FTYPE epsilon0=1.45E-3; // FREEPAR // shifts how hot the flow is
6839  rhotarg=1.0 - (1.0/(gamtorus*epsilon0*epsilon0*phir0*phir0))*(phieff-phieffr0)/(nn+1.0);
6840  rhotargeq=1.0 - (1.0/(gamtorus*epsilon0*epsilon0*phir0*phir0))*(phieffeq-phieffr0)/(nn+1.0);
6841  rhot = rhoc*pow(rhotarg,nn); // [typo in paper Eq18]
6842  rhoteq = rhoc*pow(rhotargeq,nn); // [typo in paper Eq18]
6843  pt = rhoc*gamtorus*epsilon0*epsilon0*phir0*phir0*pow(rhot/rhoc,1.0+1.0/nn);
6844  }
6845  else{
6846  // Kato et al. (2004):
6847  //FTYPE vs0=5.6E-3; // FREEPAR // Table1
6848  // FTYPE vs0=1E-1;
6849  FTYPE HoR0=RADNT_HOVERR;
6850  FTYPE vphi0=l0/r0;
6851  FTYPE vs0=HoR0*vphi0;
6852  rhotarg=1.0 - (gamtorus/(vs0*vs0))*(phieff-phieffr0)/(nn+1.0);
6853  rhotargeq=1.0 - (gamtorus/(vs0*vs0))*(phieffeq-phieffr0)/(nn+1.0);
6854  rhot = rhoc*pow(rhotarg,nn);
6855  rhoteq = rhoc*pow(rhotargeq,nn);
6856  pt = rhoc*vs0*vs0/(gamtorus)*pow(rhot/rhoc,1.0+1.0/nn);
6857  FTYPE Eth0=vs0*vs0/(gamtorus*fabs(phir0));
6858  }
6859 
6860  if(r<3.0 || rhotarg<0.0 || phieff<phieffr0 || rhot<0.0 || rhot>rhoc || phieffeq>0.0 || rhoteq>rhoc || rhoteq<0.0 ){
6861  usingback=1;
6862  }
6863 
6864  rho=rhot; // torus density
6865  uint=pt/(gamtorus-1.0); // torus internal energy density assuming ideal gas with gamtorus
6866 
6867  // see if should still use backup non-torus values
6868  if(rho<ppback[RHO]){
6869  usingback=1;
6870  }
6871 
6872  if(DOWALDDEN){
6873  usingback=1;
6874  }
6875 
6876  Vphi=ll/(r*r);
6877  Vr=0.;
6878 
6879  dualfprintf(fail_file,"donutohsuga: r(%d)=%g th(%d)=%g Rcyl=%g : l0=%g ll=%g phi=%g phieff=%g phir0=%g phieffr0=%g rhot=%g rhotarg=%g pt=%g Vphi=%g\n",r,(*ptrptrgeom)->i,th,(*ptrptrgeom)->j,Rcyl,l0,ll,phi,phieff,phir0,phieffr0,rhot,rhotarg,pt,Vphi);
6880 
6881  }
6882 
6883 
6884 
6886  // see if should still use backup non-torus values
6887  // could use existing backup, or change.
6888  if(usingback==1){
6889 
6890  //use existing backup
6891  rho=ppback[RHO];
6892  uint=ppback[UU];
6893 
6894  // fix total pressure
6895  pt = pressure_rho0_u_simple(i,j,k,CENT,rho,uint);
6896  // if(EOMRADTYPE!=EOMRADNONE) pt += pp[URAD0]*(4.0/3.0-1.0); // don't include if later say optically thin
6897 
6898 
6899  // get KSCOORDS ZAMO since original ppback[] might have been (e.g.) BLCOORDS/VEL3
6900  // overwrite ppback with MCOORD ZAMO
6901  *whichvel=VELREL4;
6902  *whichcoord=MCOORD;
6903  int getprim=0;
6904  gset(getprim,*whichcoord,i,j,k,*ptrptrgeom);
6905  set_zamo_velocity(*whichvel,*ptrptrgeom,ppback);
6906 
6907  // get velocities from new backup
6908  Vr=ppback[U1];
6909  Vh=ppback[U2];
6910  Vphi=ppback[U3];
6911 
6912  // use existing backup
6913  if(URAD0>=0){
6914  // still fluid-frame
6915  E=ppback[URAD0];
6916  Fx=ppback[URAD1];
6917  Fy=ppback[URAD2];
6918  Fz=ppback[URAD3];
6919  }
6920 
6921  }
6922  else{
6923 
6924  if(URAD0>=0){
6925  // not setting radiation here, so use backup for now
6926  E=pp[URAD0];
6927  Fx=pp[URAD1];
6928  Fy=pp[URAD2];
6929  Fz=pp[URAD3];
6930  }
6931  }
6932 
6934  //
6935  // assign torus density-velocity values for return
6936  //
6938  pp[RHO]=rho;
6939  pp[UU]=uint;
6940  *ptptr=pt;
6941  pp[U1]=Vr;
6942  pp[U2]=Vh;
6943  pp[U3]=Vphi;
6944  if(URAD0>=0){
6945  pp[URAD0]=E;
6946  pp[URAD1]=Fx;
6947  pp[URAD2]=Fy;
6948  pp[URAD3]=Fz;
6949  }
6950 
6951  return(usingback);
6952 }
6953 
6954 
6955 
6956 
6957 // return pp (if not already set) for gas primitives as well as assumed total pressure consistent with original simulation's pressure and ideal gas constant gamtorus.
6958 // use instead of donut_analytical_solution() everywhere function called (currently just 1 location)
6959 static int process_solution(int *whichvel, int *whichcoord, int opticallythick, FTYPE *pp,FTYPE *X, FTYPE *V,struct of_geom **ptrptrgeom, FTYPE *ptptr)
6960 {
6961 
6962  FTYPE gamtorus=4.0/3.0; // whatever you had in original restart file.
6963  // total torus pressure that will be distributed among gas and radiation pressure
6964  *ptptr=(gamtorus-1.0)*pp[UU];
6965 
6966  int usingback=0; // never use backup, always a solution
6967 
6968  return(usingback);
6969 }
6970 
6971 
6972 
6973 
6974 // take global primitive data (say read in by restart_read()) and create radiation component.
6975 // to be called externally after restart_init()
6977 {
6978  int i,j,k;
6979 
6980  DUMPGENLOOP{ // could make this OpenMP'ed, but just done once. Needs to be no more or less than what restart_init() and so restart_read() and so dumpgen() and so DUMPGENLOOP uses.
6981  int whichvel=WHICHVEL, whichcoord=PRIMECOORDS; // for the below to make sense and work (without further transformation calls), this should be WHICHVEL and PRIMECOORDS always.
6982  int loc=CENT;
6983  FTYPE X[NDIM],V[NDIM];
6984  bl_coord_ijk_2(i,j,k,loc,X,V);
6985  struct of_geom geomrealdontuse;
6986  struct of_geom *ptrgeomrad=&geomrealdontuse;
6987  get_geometry(i,j,k,loc,ptrgeomrad);
6988  int thick=1; // see how used
6989  FTYPE *pr=&GLOBALMACP0A1(pglobal,i,j,k,0);
6990  int returndonut=get_full_rtsolution(&whichvel,&whichcoord,thick, pr,X,V,&ptrgeomrad);
6991  FTYPE pradffortho[NPR];
6992  if(PRAD0>=0){
6993  // donut returns fluid frame orthonormal values for radiation in pr
6994  pradffortho[PRAD0]=pr[PRAD0];
6995  pradffortho[PRAD1]=pr[PRAD1];
6996  pradffortho[PRAD2]=pr[PRAD2];
6997  pradffortho[PRAD3]=pr[PRAD3];
6998  }
6999  prad_fforlab(&whichvel, &whichcoord, FF2LAB, i,j,k,loc,ptrgeomrad, pradffortho, pr, pr);
7000  // now all pr is PRIMECOORDS, WHICHVEL in lab-frame.
7001  }
7002 
7003  return(0);
7004 }
7005 
7006 
7007 
7008 
7009 
7011 {
7012  return(
7013  sqrt(
7014  (3.*a*a - 4.*a*sqrt(R) + R*R)/
7015  pow(R*(a + pow(R,1.5)),2)
7016  )
7017  ) ;
7018 
7019 
7020 }
7021 
7022 static FTYPE taper_func_exp(FTYPE R,FTYPE rin, FTYPE POTENTIALorPRESSURE) // MAVARA added June 3 2013
7023 {
7024  // POTENTIALorPRESSURE = 1. if used in setting pressure, 2. if used for potential
7025  FTYPE softer = .3; // 2.0 works ok for resolution of 64 cells for inner 40 M radius
7026  if(R <= rin)
7027  return(0.) ;
7028  else
7029  return(1. - POTENTIALorPRESSURE * exp((0.95*rin - R)*softer)) ;
7030 
7031 }
7032 
7034 {
7035  if(R < rin)
7036  return(0.) ;
7037  else
7038  return(pow( 1.-pow(0.95*rin/R,0.5) , 1.)) ; // was 3 and 2 for powers before 11/10/2012 MAVARA
7039 }
7040 
7041 
7042 int set_fieldtype(void)
7043 {
7044  int FIELDTYPE;
7045 
7046  if(WHICHPROBLEM==RADDONUT){
7047 
7049  // FIELDTYPE=DISK2FIELD;
7050  FIELDTYPE=FIELDJONMAD; // SUPERMADNEW
7051 
7052  }
7053  else if(RADNT_DONUTTYPE==DONUTOLEK){
7054  //FIELDTYPE=VERTFIELD; // DISK2VERT//DISK2FIELD
7055  FIELDTYPE=DISK2FIELD;
7056 
7057  //FIELDTYPE=OLEKFIELD;
7058  //FIELDTYPE=FIELDJONMAD;
7059 
7060  if(DOWALDDEN==1){ // nothing to do with densities, just for simplicity
7061  FIELDTYPE=FIELDWALD; // WALD field
7062  }
7063  else if(DOWALDDEN==2) FIELDTYPE=MONOPOLE;
7064 
7065 
7066  }
7067  else if(RADNT_DONUTTYPE==DONUTOHSUGA){
7068  FIELDTYPE=OHSUGAFIELD;
7069  }
7070  else{
7071  //FIELDTYPE=VERTFIELD;
7072  FIELDTYPE=DISK2FIELD; // default
7073  //FIELDTYPE=DISK1FIELD;
7074  }
7075  }
7076  else if(WHICHPROBLEM==RADBONDI){
7077  //FIELDTYPE=MONOPOLAR; // for mag bondi
7078  FIELDTYPE=NOFIELD;
7079  }
7080  else{
7081  FIELDTYPE=NOFIELD;
7082  }
7083 
7084 
7085  return(FIELDTYPE);
7086 }
7087 
7088 
7089 
7090 FTYPE setgpara(FTYPE myr, FTYPE th, FTYPE thpower)
7091 {
7092  FTYPE fneg,fpos;
7093  FTYPE gpara;
7094 
7095  fneg=1.0-pow(cos(th),thpower);
7096  fpos=1.0+pow(cos(th),thpower);
7097  gpara=0.5*(myr*fneg + 2.0*fpos*(1.0-log(fpos)));
7098  // remove BZ77 Paraboloidal divb!=0 at pole
7099  gpara=gpara-2.0*(1.0-log(2.0));
7100 
7101  return(gpara);
7102 
7103 
7104 }
7105 
7107 {
7108  FTYPE setgpara(FTYPE myr, FTYPE th, FTYPE thpower);
7109  FTYPE rshift,myr,rpower,myz,myR,myvert;
7110  FTYPE thother,thpower,gparalow,gparahigh,mygpara;
7111  FTYPE aphi;
7112 
7113 
7114  rshift=4.0;
7115  rpower=0.75;
7116  thpower=4.0;
7117 
7118 
7119  myr=pow(r+rshift,rpower);
7120  myz=myr*cos(th);
7121  myR=myr*sin(th);
7122  myvert = (th>M_PI*0.5) ? (myr*sin(th)) : (myr*sin(-th));
7123 
7124  thother=M_PI-th;
7125  gparalow=setgpara(myr,th,thpower);
7126  gparahigh=setgpara(myr,thother,thpower);
7127  mygpara=(th<0.5*M_PI) ? gparalow : gparahigh;
7128 
7129  // GOOD:
7130  // aphi=mygpara;
7131  // aphi=mygpara*cos(th); // B1 diverges at pole
7132  // aphi=mygpara*cos(th)*sin(th); // doesn't diverge as much
7133  // aphi=mygpara*cos(th)*sin(th)*sin(th); // old choice before subtracted original BZ77 problem
7134  aphi=mygpara*cos(th); // latest choice
7135  //aphi=myvert*cos(th); // vert with quad
7136  //aphi=myR*cos(th);
7137 
7138  // BAD:
7139  // aphi=myvert;
7140 
7141 
7142 
7143  return(aphi);
7144 
7145 
7146 }
7147 
7148 
7149 
7150 // assumes normal field in pr
7151 // SUPERNOTE: A_i must be computed consistently across all CPUs. So, for example, cannot use randomization of vector potential here.
7152 int init_vpot_user(int *whichcoord, int l, SFTYPE time, int i, int j, int k, int loc, FTYPE (*prim)[NSTORE2][NSTORE3][NPR], FTYPE *V, FTYPE *A)
7153 {
7154  SFTYPE rho_av, p_av,u_av,q;
7155  FTYPE r,th,ph;
7156  FTYPE vpot;
7158 
7159  int set_fieldtype(void);
7160  int FIELDTYPE=set_fieldtype();
7161 
7162 
7163 
7164  FTYPE FRACAPHICUT;
7165 
7166 
7167  //#define FRACAPHICUT 0.1
7168  if(FIELDTYPE==DISK2FIELD){
7169  FRACAPHICUT=0.2; // for weak field
7170  // FRACAPHICUT=0.001; // for disk-filling field that is more MAD like
7171 
7172 
7174  FRACAPHICUT=1E-6;
7175  }
7176 
7177  }
7178 
7179 
7180 
7181 
7182  vpot=0.0;
7183 
7184  FTYPE *pr = &MACP0A1(prim,i,j,k,0);
7185 
7186 #if(1)
7187  int ibound=0,jbound=0,kbound=0;
7188  if(i==-N1BND || i==N1-1+N1BND) ibound=1 && N1NOT1;
7189  if(j==-N2BND || j==N2-1+N2BND) jbound=1 && N2NOT1;
7190  if(k==-N3BND || k==N3-1+N3BND) kbound=1 && N3NOT1;
7191 
7192  int iout=0,jout=0,kout=0;
7193  if(i<-N1BND || i>N1-1+N1BND) iout=1 && N1NOT1;
7194  if(j<-N2BND || j>N2-1+N2BND) jout=1 && N2NOT1;
7195  if(k<-N3BND || k>N3-1+N3BND) kout=1 && N3NOT1;
7196 #endif
7197 
7198  // since init_vpot() is called for all i,j,k, can't use
7199  // non-existence values, so limit averaging:
7200  if(iout||jout||kout){
7201  rho_av=p_av=0.0;
7202  }
7203  else if(ibound||jbound||kbound){
7204  // if(ibound||jbound||kbound){
7205  // if(i==-N1BND && j==-N2BND){
7206  rho_av = pr[RHO];
7207  p_av = pressure_rho0_u_simple(i,j,k,CENT,pr[RHO],pr[UU]);
7208  if(EOMRADTYPE!=EOMRADNONE) p_av += pr[URAD0]*(4.0/3.0-1.0);
7209  }
7210  /* else if(i==-N1BND){ */
7211  /* rho_av = AVGN_2(prim,i,j,k,RHO); */
7212  /* u_av = AVGN_2(prim,i,j,k,UU); // simple cheat to avoid defining new AVGN macros */
7213  /* p_av = pressure_rho0_u_simple(i,j,k,loc,rho_av,u_av); */
7214  /* if(EOMRADTYPE!=EOMRADNONE) p_av += AVGN_2(prim,i,j,k,URAD0)*(4.0/3.0-1.0); */
7215  /* } */
7216  /* else if(j==-N2BND){ */
7217  /* rho_av = AVGN_1(prim,i,j,k,RHO); */
7218  /* u_av = AVGN_1(prim,i,j,k,UU); */
7219  /* p_av = pressure_rho0_u_simple(i,j,k,loc,rho_av,u_av); */
7220  /* if(EOMRADTYPE!=EOMRADNONE) p_av += AVGN_1(prim,i,j,k,URAD0)*(4.0/3.0-1.0); */
7221  /* } */
7222  else{ // normal cells
7223  if(loc==CORN3){
7224  rho_av = AVGN_for3(prim,i,j,k,RHO);
7225  u_av = AVGN_for3(prim,i,j,k,UU);
7226  p_av = pressure_rho0_u_simple(i,j,k,loc,rho_av,u_av);
7227  if(EOMRADTYPE!=EOMRADNONE) p_av += AVGN_for3(prim,i,j,k,URAD0)*(4.0/3.0-1.0);
7228  }
7229  else if(loc==CORN2){
7230  rho_av = AVGN_for2(prim,i,j,k,RHO);
7231  u_av = AVGN_for2(prim,i,j,k,UU);
7232  p_av = pressure_rho0_u_simple(i,j,k,loc,rho_av,u_av);
7233  if(EOMRADTYPE!=EOMRADNONE) p_av += AVGN_for2(prim,i,j,k,URAD0)*(4.0/3.0-1.0);
7234  }
7235  else if(loc==CORN1){
7236  rho_av = AVGN_for1(prim,i,j,k,RHO);
7237  u_av = AVGN_for1(prim,i,j,k,UU);
7238  p_av = pressure_rho0_u_simple(i,j,k,loc,rho_av,u_av);
7239  if(EOMRADTYPE!=EOMRADNONE) p_av += AVGN_for1(prim,i,j,k,URAD0)*(4.0/3.0-1.0);
7240  }
7241  else{
7242  dualfprintf(fail_file,"No such setup for loc=%d\n",loc);
7243  myexit(34782985);
7244  }
7245  }
7246 
7247 
7248 
7249 
7250  if(FIELDTYPE==TOROIDALFIELD){
7251 
7252  if(l==2){// A_\theta (MCOORD)
7253 
7254  r=V[1];
7255  th=V[2];
7256 
7257  // q = r*r*r*fabs(sin(th)) * 1.0 ; // constant B^\phi
7258  q = r*r*r;
7259 
7260  q=q/(r); // makes more uniform in radius
7261 
7262  q = q*(p_av / ptotmax - FRACAPHICUT); // weight by pressure
7263  // q = (rho_av / rhomax - FRACAPHICUT);
7264 
7265  if(q<0.0) q=0.0;
7266 
7267  vpot += q;
7268 
7269  }
7270  }
7271 
7272 
7273  FTYPE rpow;
7274  rpow=3.0/4.0; // Using rpow=1 leads to quite strong field at large radius, and for standard atmosphere will lead to \sigma large at all radii, which is very difficult to deal with -- especially with grid sectioning where outer moving wall keeps opening up highly magnetized region
7275  // FTYPE FIELDROT=M_PI*0.5;
7276  FTYPE FIELDROT=0.0;
7277  FTYPE hpow=2.0;
7278  // FTYPE rpow=1.0; // previous SUPERMAD, now just use 3/4
7279 
7280 
7281  if(l==2){// A_\theta
7282 
7283  r=V[1];
7284  th=V[2];
7285  ph=V[3];
7286 
7287 
7288  /* vertical field version*/
7289  if((FIELDTYPE==VERTFIELD)||(FIELDTYPE==DISK1VERT)||(FIELDTYPE==DISK2VERT)){
7290  vpot += -(pow(r,rpow)*pow(sin(th),hpow)*sin(FIELDROT)*sin(ph));
7291  }
7292 
7293 
7294  }
7295 
7296  if(l==3){// A_\phi
7297 
7298  r=V[1];
7299  th=V[2];
7300  ph=V[3];
7301 
7302 
7303  // Blandford quadrapole field version
7304  if(FIELDTYPE==BLANDFORDQUAD){
7305  vpot += setblandfordfield(r,th);
7306  }
7307 
7308  /* vertical field version*/
7309  if((FIELDTYPE==VERTFIELD)||(FIELDTYPE==DISK1VERT)||(FIELDTYPE==DISK2VERT)){
7310  //vpot += 0.5*pow(r,rpow)*sin(th)*sin(th) ;
7311  vpot += pow(r,rpow)*pow(sin(th),hpow)*(cos(FIELDROT) - cos(ph)*cot(th)*sin(FIELDROT));
7312  }
7313 
7314 
7315  if(FIELDTYPE==MONOPOLAR){
7316  vpot += (1.0-cos(th));
7317  }
7318 
7319  if(FIELDTYPE==OLEKFIELD){
7320  vpot += MAX(pow(r,4.0)*pow(rho_av,2.0)*1E40-0.02,0.0)*pow(sin(th),4.0);
7321  }
7322 
7323 #define JONMADHPOW (4.0)
7324 #define JONMADR0 (0.0)
7325 #define JONMADROUT (300.0)
7326 
7327  if(FIELDTYPE==FIELDJONMAD){
7328  if(r>=JONMADR0 && r<JONMADROUT){
7329  vpot += MAX(pow(r-JONMADR0,rpow)*1E40-0.02,0.0)*(pow(sin(th),1+JONMADHPOW));
7330  }
7331  else if(r>=JONMADROUT){
7332  // to go monopolar
7333  vpot += MAX(pow(JONMADROUT-JONMADR0,rpow)*1E40-0.02,0.0)*(pow(sin(th),1+JONMADHPOW/(r/JONMADROUT)));
7334  }
7335  if(V[2]<1E-5 || V[2]>M_PI-1E-5){
7336  vpot=0;
7337  }
7338 
7339 
7340 
7341  }
7342 
7343 
7344 
7345 
7346 
7347  /* field-in-disk version */
7348  if(FIELDTYPE==DISK1FIELD || FIELDTYPE==DISK1VERT){
7349  q = rho_av / rhomax - 0.2;
7350  if(r<rin) q=0.0;
7351  if (q > 0.) vpot += q;
7352  }
7353 
7354  if(FIELDTYPE==OHSUGAFIELD){
7355  q = rho_av / rhomax - 0.2;
7356  if(r<rin) q=0.0;
7357  if (q > 0.) vpot += q;
7358  }
7359 
7360 
7361  if(FIELDTYPE==DISK2FIELD || FIELDTYPE==DISK2VERT){
7362  // average of density that lives on CORN3
7363 
7364 
7365  //#define FRACAPHICUT 0.1
7366  //#define FRACAPHICUT 0.1
7367 
7368  // q = (rho_av / rhomax - FRACAPHICUT);
7369  // q = (p_av / ptotmax - FRACAPHICUT); // was used for rada0.94, etc. models.
7371  q = (p_av);
7372  // dualfprintf(fail_file,"qorig=%g\n",q);
7373  if(rho_av/(rhomax*pow(r,thindiskrhopow)) - FRACAPHICUT<0) q=0;
7374  // dualfprintf(fail_file,"qnew=%g : %g %g\n",q,rho_av/rhomax,FRACAPHICUT);
7375  }
7376  else{
7377  q = 1E-30*(p_av / ptotmax*1E30 - FRACAPHICUT);
7378  // dualfprintf(fail_file,"qorig=%g\n",q);
7379  if(rho_av/rhomax-FRACAPHICUT<0) q=0;
7380  // dualfprintf(fail_file,"qnew=%g : %g %g\n",q,rho_av/rhomax,FRACAPHICUT);
7381  }
7382 
7383  //#define QPOWER 0.5
7384 #define QPOWER (1.0)
7385 
7386 #define POWERNU (2.0) // 2.5 for toroidal field SUPERMAD paper
7387  //#define POWERNU (4.0)
7388 
7389  // if (q > 0.) vpot += q*q*pow(r*fabs(sin(th)),POWERNU);
7390  FTYPE fact1,fact2,SSS,TTT;
7391  fact1=pow(fabs(q),QPOWER)*pow(r*fabs(sin(th)),POWERNU);
7392  // fact1=pow(fabs(q),QPOWER)*pow(r,POWERNU); // for SUPERMAD paper
7393  if(r<rin) fact1=0.0;
7394  SSS=rin*0.5;
7395  TTT=0.28;
7396  fact2=sin(log(r/SSS)/TTT);
7397  fact2=1.0; // forces to avoid flipping.
7398 
7399  if (q > 0.) vpot += fact1*fact2;
7400  // if (q > 0.) vpot += q*q;
7401  if(PRODUCTION==0) dualfprintf(fail_file,"ijk=%d %d %d : ptotmax=%g p_av=%g q=%g %g %g rin=%g vpot=%g : rho_av=%g rhomax=%g\n",i,j,k,ptotmax,p_av,q,fact1,fact2,rin,vpot,rho_av,rhomax);
7402  }
7403 
7404 
7405 
7406  }
7407 
7408  if(FIELDTYPE==SPLITMONOPOLE || FIELDTYPE==MONOPOLE || FIELDTYPE==FIELDWALD){
7409 
7410  r=V[1];
7411  th=V[2];
7412  ph=V[3];
7413 
7414 
7415  if(FIELDTYPE==SPLITMONOPOLE || FIELDTYPE==MONOPOLE) return(0); // otherwise setup poloidal components using vector potential
7416  else if(FIELDTYPE==FIELDWALD){
7417 
7418  FTYPE mcov[NDIM],mcon[NDIM],kcov[NDIM],kcon[NDIM];
7419 
7420 
7421  mcon[TT]=0;
7422  mcon[RR]=0;
7423  mcon[TH]=0;
7424  mcon[PH]=1.0;
7425 
7426  kcon[TT]=1.0;
7427  kcon[RR]=0;
7428  kcon[TH]=0;
7429  kcon[PH]=0;
7430 
7431  // get metric grid geometry for these ICs
7432  int getprim=0;
7433  struct of_geom geomrealdontuse;
7434  struct of_geom *ptrgeomreal=&geomrealdontuse;
7435  *whichcoord = MCOORD;
7436  gset_genloc(getprim,*whichcoord,i,j,k,loc,ptrgeomreal);
7437 
7438  lower_vec(mcon,ptrgeomreal,mcov);
7439  lower_vec(kcon,ptrgeomreal,kcov);
7440 
7441 
7442  // below so field is b^2/rho=BSQORHOWALD at horizon
7443  FTYPE Rhorlocal=rhor_calc(0);
7444  B0WALD=sqrt(BSQORHOWALD*RADNT_RHOATMMIN*pow(Rhorlocal/RADNT_ROUT,-1.5));
7446  aforwald=a;
7447 
7448  if(1||fabs(TILTWALD-0.0)<1E-10){ // 1|| for now since issue with uu0 at maximum near outer radius when tilt=90deg-1E-5
7449  if(l==WALDWHICHACOV || WALDWHICHACOV==-1){
7450  vpot += -0.5*B0WALD*(mcov[l]+2.0*aforwald*kcov[l]);
7451  }
7452 
7453 
7454  }
7455  else{ // need all A_i's for tilted field
7456 
7457  // STEP1:
7458  // r,\theta,\phi are such that BH spin stays in z-hat and field is tilted. So we need to provide r,\theta,\phi in coordinates wher z-hat is along spin axis.
7459 
7460  // Original V at this point is with tilted BH spin, but solution below is for spin along z-axis, so find Vmetric[corresponding to when BH spin is along z-axis]
7461  FTYPE Vmetric[NDIM];
7462  rotate_VtoVmetric(MCOORD,TILTWALD,V,Vmetric);
7463 
7464  FTYPE rV=Vmetric[1];
7465  FTYPE thV=Vmetric[2];
7466  FTYPE phV=Vmetric[3];
7467 
7468  // STEP2: Now get solution in setup where BH spin along z-axis always and it is the field that is rotated
7469  // B0y=0
7470  FTYPE B0z=1.0; // B0z
7471  FTYPE B0x=0.0; // B0x
7472 
7473  FTYPE delta,sigma,rp,rm,psi;
7474 
7475  delta= rV*rV - 2.0*MBH*rV + a*a;
7476  sigma = rV*rV + a*a*cos(thV)*cos(thV);
7477  rp = MBH + sqrt(MBH*MBH - a*a);
7478  rm = MBH - sqrt(MBH*MBH - a*a);
7479 
7480  psi = phV ; // + a/(rp-rm)*log((rV-rp)/(rV-rm)); // ph originally is phi[BL] and psi is phi[KS].
7481 
7482  FTYPE Acovblnonrot[NDIM];
7483 
7484  Acovblnonrot[0] = -1.*a*B0z + (a*B0z*MBH*rV*(1. + Power(Cos(thV),2)))/sigma +
7485  (a*B0x*MBH*Cos(thV)*(rV*Cos(psi) - 1.*a*Sin(psi))*Sin(thV))/sigma;
7486 
7487  Acovblnonrot[1] = -1.*B0x*(-1.*MBH + rV)*Cos(thV)*Sin(psi)*Sin(thV);
7488 
7489  Acovblnonrot[2] = -1.*B0x*(Power(rV,2)*Power(Cos(thV),2) + Power(a,2)*Cos(2.*thV) -
7490  1.*MBH*rV*Cos(2.*thV))*Sin(psi) -
7491  1.*a*B0x*Cos(psi)*(MBH*Power(Cos(thV),2) + rV*Power(Sin(thV),2));
7492 
7493 
7494  Acovblnonrot[3] = -1.*B0x*Cos(thV)*(delta*Cos(psi) +
7495  (MBH*(Power(a,2) + Power(rV,2))*(rV*Cos(psi) - 1.*a*Sin(psi)))/
7496  sigma)*Sin(thV) + B0z*(0.5*(Power(a,2) + Power(rV,2)) -
7497  (1.*Power(a,2)*MBH*rV*(1. + Power(Cos(thV),2)))/sigma)*
7498  Power(Sin(thV),2);
7499 
7500  // STEP3: now take Acovblnonrot -> Acovksnonrot
7501  // NOTE: bl2ks transformation assumes BH spin is pointing z-hat, just like Acovblnonrot[] assumes, so have to apply this here before rotation vector.
7502  FTYPE Acovksnonrot[NDIM];
7503  int jj;
7504  DLOOPA(jj) Acovksnonrot[jj]=Acovblnonrot[jj];
7505  bltoks_ucov(i,j,k,loc,Acovksnonrot);
7506 
7507  // STEP4: now take Acovksnonrot -> Acovks.
7508  // That is, while coordinate position was correctly mapped in STEP1, coordinate vector still needs to be rotated
7509  FTYPE Acovks[NDIM];
7510  DLOOPA(jj) Acovks[jj]=Acovksnonrot[jj];
7511  transVmetrictoV_ucov(TILTWALD,Vmetric,Acovks);
7512 
7513  // now pluck out only the A_l one wanted
7514  vpot += Acovks[l];
7515 
7516  }
7517  }
7518  }
7519 
7520 
7522  //
7523  // finally assign what's returned
7524  //
7526  *A = vpot;
7527  *whichcoord = MCOORD;
7528 
7529 
7530 
7531  return(0);
7532 
7533 }
7534 
7535 
7536 
7537 
7538 
7539 
7540 // setup primitive field (must use whichcoord inputted)
7541 // sets CENT value of field primitive
7542 static int fieldprim(int whichmethod, int whichinversion, int *whichvel, int*whichcoord, int ii, int jj, int kk, FTYPE *pr)
7543 {
7544 
7545 
7546 
7548  //
7549  // Get FIELDTYPE
7550  //
7552  int set_fieldtype(void);
7553  int FIELDTYPE=set_fieldtype();
7554 
7555 
7557  //
7558  // get X and V
7559  //
7561  FTYPE X[NDIM],V[NDIM];
7562  coord(ii, jj, kk, CENT, X);
7563  bl_coord(X, V);
7564  FTYPE r=V[1];
7565  FTYPE th=V[2];
7566  FTYPE ph=V[3];
7567 
7568 
7570  //
7571  // Get ptrgeom
7572  //
7574  struct of_geom geomdontuse;
7575  struct of_geom *ptrgeom=&geomdontuse;
7576  FTYPE realX[NDIM];
7577  if(*whichcoord==PRIMECOORDS){
7578  get_geometry(ii,jj,kk,CENT,ptrgeom);
7579  int j;
7580  DLOOPA(j) realX[j]=X[j];
7581  // dualfprintf(fail_file,"using PRIMECOORDS\n");
7582  }
7583  else{
7584  // get metric grid geometry for these ICs
7585  int getprim=0;
7586  gset(getprim,*whichcoord,ii,jj,kk,ptrgeom);
7587  int j;
7588  DLOOPA(j) realX[j]=V[j];
7589  // dualfprintf(fail_file,"using WHICHCOORD=%d\n",*whichcoord);
7590  }
7591 
7592 
7593 
7595  //
7596  // set field
7597  //
7599  FTYPE Rhorlocal=rhor_calc(0);
7600  B0WALD=sqrt(BSQORHOWALD*RADNT_RHOATMMIN*pow(Rhorlocal/RADNT_ROUT,-1.5));
7601  //
7602  if(FIELDTYPE==SPLITMONOPOLE){
7603  if(th<M_PI*0.5) pr[B1]=B0WALD/ptrgeom->gdet;
7604  else pr[B1]=-B0WALD/ptrgeom->gdet;
7605  pr[B2]=pr[B3]=0;
7606  }
7607  else if(FIELDTYPE==MONOPOLE){
7608  // Ruben's talk says they set $\dF^{tr} = C\sin{\theta}/\detg$.
7609  pr[B1]=B0WALD/ptrgeom->gdet;
7610  pr[B2]=pr[B3]=0;
7611  }
7612  else if(FIELDTYPE==FIELDWALD){
7613 
7614  int doit=ii==N1-1 && jj==N2/4-1 && kk==0;
7615  doit=0; // no debug normally
7616 
7617 
7618  FTYPE Fcov[NDIM][NDIM];
7619  FTYPE Mcon[NDIM][NDIM];
7620  FTYPE etacov[NDIM],etacon[NDIM];
7621  FTYPE Ecov[NDIM],Econ[NDIM],Bcov[NDIM],Bcon[NDIM];
7622  FTYPE alpha;
7623  FTYPE Jcon[NDIM];
7624  int j,k;
7625 
7626  void Fcov_numerical(int whichcoord, FTYPE *X, FTYPE (*Fcov)[NDIM]);
7627  void Jcon_numerical(int whichcoord, FTYPE *X, FTYPE *Jcon);
7628  extern void MtoF(int which, FTYPE Max[NDIM][NDIM],struct of_geom *geom, FTYPE faraday[NDIM][NDIM]);
7629  extern void lower_A(FTYPE (*Acon)[NDIM], struct of_geom *geom, FTYPE (*Acov)[NDIM]);
7630  extern void raise_A(FTYPE (*Acov)[NDIM], struct of_geom *geom, FTYPE (*Acon)[NDIM]);//extern void raise_A(FTYPE Acov[NDIM][NDIM], struct of_geom *geom, FTYPE Acon[NDIM][NDIM]);
7631  extern int EBtopr(FTYPE *E,FTYPE *B,struct of_geom *geom, FTYPE *pr);
7632  extern int EBtopr_2(FTYPE *E,FTYPE *B,struct of_geom *geom, FTYPE *pr);
7633 
7634 
7635  FTYPE prold[NPR];
7636  int pliter,pl;
7637  PLOOP(pliter,pl) prold[pl]=pr[pl];
7638 
7639  // check that J^\mu=0
7640  Jcon_numerical(*whichcoord, X, Jcon);
7641 
7642  FTYPE Fcon[NDIM][NDIM];
7643  FTYPE Fud[NDIM][NDIM];
7644  FTYPE Fdu[NDIM][NDIM];
7645  FTYPE Mud[NDIM][NDIM];
7646  FTYPE Mdu[NDIM][NDIM];
7647  FTYPE Mcov[NDIM][NDIM];
7648  if(whichmethod==0){
7649  // first get F_{\mu\nu}
7650  Fcov_numerical(*whichcoord, X, Fcov);
7651 
7652  // get *F^{\mu\nu}
7653  MtoF(3,Fcov,ptrgeom,Mcon);
7654  }
7655  else{
7656  // int j,k;
7657  DLOOP(j,k) Mcon[j][k]=0.0;
7658  SLOOPA(j) Mcon[j][0] = Bcon[j] = prold[B1+j-1]; // use original field from staggered field to centered from A_i
7659  SLOOPA(j) Mcon[0][j] = -Mcon[j][0];
7660 
7661  if(1){ // set *F_{\phi t}=0 so no angular momentum flux
7662  FTYPE myBd3=0.0;
7663  FTYPE Bu1=Bcon[1];
7664  FTYPE Bu2=Bcon[2];
7665  FTYPE Ed1=Mcon[2][3]; // *F^{23} = E_1 = F_{10}/detg
7666  FTYPE Ed2=Mcon[3][1]; // *F^{31} = E_2 = F_{20}/detg
7667  FTYPE Ed3=Mcon[1][2]; // *F^{12} = E_3 = F_{30}/detg
7668  FTYPE gv00=ptrgeom->gcov[GIND(0,0)];
7669  FTYPE gv01=ptrgeom->gcov[GIND(0,1)];
7670  FTYPE gv02=ptrgeom->gcov[GIND(0,2)];
7671  FTYPE gv03=ptrgeom->gcov[GIND(0,3)];
7672  FTYPE gv13=ptrgeom->gcov[GIND(1,3)];
7673  FTYPE gv23=ptrgeom->gcov[GIND(2,3)];
7674  FTYPE gv33=ptrgeom->gcov[GIND(3,3)];
7675 
7676  FTYPE denom=(-Power(gv03,2) + gv00*gv33);
7677  // if(fabs(denom)<0.2 || fabs(r-Rhor)/Rhor<0.3){
7678  if(fabs(r-Rhor)/Rhor<0.3){
7679  Bcon[3] = Mcon[3][0] = 0.0;
7680  }
7681  else{
7682  Bcon[3] = Mcon[3][0] =(Bu1*gv01*gv03 + Bu2*gv02*gv03 - Bu1*gv00*gv13 - Ed3*gv02*gv13 +
7683  Ed2*gv03*gv13 - Bu2*gv00*gv23 + Ed3*gv01*gv23 - Ed1*gv03*gv23 -
7684  Ed2*gv01*gv33 + Ed1*gv02*gv33 + myBd3)/denom;
7685  // Bcon[3] = Mcon[3][0] = (-(Bu1*gv01*gv03) - Bu2*gv02*gv03 + Bu1*gv00*gv13 + Bu2*gv00*gv23 + myBd3)/(pow(gv03,2) - gv00*gv33);
7686  }
7687 
7688 
7689  Mcon[0][3] = -Mcon[3][0];
7690  }
7691 
7692 
7693 
7694  MtoF(0,Mcon,ptrgeom,Fcov);
7695  }
7696 
7697  raise_A(Fcov,ptrgeom,Fcon);
7698  indices_2221(Fcon,Fud,ptrgeom);
7699  indices_2212(Fcon,Fdu,ptrgeom);
7700 
7701  indices_2221(Mcon,Mud,ptrgeom);
7702  indices_2212(Mcon,Mdu,ptrgeom);
7703  lower_A(Mcon,ptrgeom,Mcov);
7704 
7705 
7706  // dualfprintf(fail_file,"MtoF\n");
7707  // DLOOP dualfprintf(fail_file,"Mcon[%d][%d]=%21.15g\n",j,k,Mcon[j][k]);
7708 
7709 
7710 
7711  // T^\mu_\nu
7712  FTYPE Tud[NDIM][NDIM];
7713  int ll,mm;
7714  FTYPE Fsq=0.0;
7715  DLOOP(j,k) Fsq += Fcon[j][k]*Fcov[j][k];
7716  DLOOP(j,k) Tud[j][k] = 0.0;
7717  DLOOP(j,k){
7718  DLOOPA(ll) Tud[j][k] += Fud[j][ll]*Fdu[k][ll];
7719  Tud[j][k] += - 0.25*delta(j,k)*Fsq;
7720  }
7721  if(doit) DLOOP(j,k) dualfprintf(fail_file,"Tud[%d][%d]=%g\n",j,k,Tud[j][k]);
7722 
7723 
7724  // lapse
7725  // define \eta_\alpha
7726  // assume always has 0 value for space components
7727  alpha = 1./sqrt(-ptrgeom->gcon[GIND(0,0)]);
7728 
7729  etacov[TT]=-alpha; // any constant will work.
7730  SLOOPA(j) etacov[j]=0.0; // must be 0
7731 
7732  // shift
7733  // define \eta^\beta
7734  raise_vec(etacov,ptrgeom,etacon);
7735  // dualfprintf(fail_file,"raise\n");
7736 
7737  // DLOOPA dualfprintf(fail_file,"etacon[%d]=%21.15g etacov[%d]=%21.15g\n",j,etacon[j],j,etacov[j]);
7738 
7739  // Betacon
7740  FTYPE Betacon[NDIM],Betacov[NDIM];
7741  DLOOPA(j) Betacon[j]=0.0;
7742  DLOOP(j,k) Betacon[j]+=etacov[k]*Mcon[k][j];
7743  lower_vec(Betacon,ptrgeom,Betacov);
7744 
7745  // dualfprintf(fail_file,"Fcov\n");
7746  //DLOOP dualfprintf(fail_file,"Fcov[%d][%d]=%21.15g\n",j,k,Fcov[j][k]);
7747  // dualfprintf(fail_file,"%21.15g %21.15g\n",j,k,Fcov[0][3],Fcov[3][0]);
7748 
7749  // then get Eeta^\alpha
7750  FTYPE Eetacov[NDIM],Eetacon[NDIM];
7751  DLOOPA(j) Eetacov[j]=0.0;
7752  DLOOP(j,k) Eetacov[j]+=etacon[k]*Fcov[j][k];
7753  raise_vec(Eetacov,ptrgeom,Eetacon);
7754  if(doit) dualfprintf(fail_file,"etacon=%g %g %g %g Eetacov[3]=%21.15g Fcov03=%g\n",etacon[0],etacon[1],etacon[2],etacon[3],Eetacov[3],Fcov[0][3]);
7755  //DLOOPA dualfprintf(fail_file,"Eetacon[%d]=%2.15g\n",j,Eetacon[j]);
7756 
7757  // T^\mu_\nu from Eeta and Beta
7758  extern void EBtoT(FTYPE *Econ,FTYPE *Bcon,struct of_geom *geom, FTYPE (*T)[NDIM]);
7759  FTYPE TfromEB[NDIM][NDIM];
7760  EBtoT(Eetacon,Betacon,ptrgeom,TfromEB);
7761  if(doit) DLOOP(j,k) dualfprintf(fail_file,"TudfromEB[%d][%d]=%g\n",j,k,TfromEB[j][k]);
7762 
7763 
7764 
7765  if(whichinversion==0){
7766 
7767  FTYPE U[NPR];
7768  DLOOPA(j) U[UU+j] = Tud[0][j];
7769  SLOOPA(j) U[B1+j-1] = Bcon[j] = Mcon[j][0]; // B^i = \dF^{it}
7770  Bcon[TT]=0.0; // force
7771 
7772 
7773  if(doit) dualfprintf(fail_file,"CONS: %g %g %g %g : %g %g %g: F=%g %g\n",U[UU],U[U1],U[U2],U[U3],Bcon[1],Bcon[2],Bcon[3],Fcov[0][3],Fcov[3][0]);
7774 
7775  struct of_newtonstats newtonstats; setnewtonstatsdefault(&newtonstats);
7776  int eomtypelocal=EOMFFDE;
7777  // int eomtypelocal=EOMTYPE;
7778  struct of_state qdontuse;
7779  struct of_state *qptr=&qdontuse;
7780  // assume if needed rest of pr already set
7781  SLOOPA(j) pr[B1+j-1]=Bcon[j];
7782  pr[RHO]=pr[UU]=0.0;
7783  //SLOOPA(j) pr[U1+j-1]=0.0; // only valid if WHICHVEL==VELREL4 // just assume use "old" versions
7784  if(doit) dualfprintf(fail_file,"BEFORE Utoprimgen()\n");
7785  if(doit) PLOOP(pliter,pl) dualfprintf(fail_file,"oldpr[%d]=%g\n",pl,pr[pl]);
7786  Utoprimgen(0,0,0,0,1,&eomtypelocal,CAPTYPEBASIC,0,1,EVOLVEUTOPRIM,UNOTHING,U,qptr,ptrgeom,0,pr,pr,&newtonstats);
7787  PLOOP(pliter,pl) if(pl>=URAD1 && pl<=URAD3) pr[pl]=prold[pl];
7788  if(doit) dualfprintf(fail_file,"AFTER Utoprim()\n");
7789  if(doit) PLOOP(pliter,pl) dualfprintf(fail_file,"newpr[%d]=%g\n",pl,pr[pl]);
7790 
7791  if(EOMTYPE!=EOMFFDE && EOMTYPE!=EOMFFDE2){
7792  PLOOP(pliter,pl){
7793  if(pl==RHO || pl==UU || pl>B3 || pl>=URAD1 && pl<=URAD3) pr[pl]=prold[pl];
7794  }
7795 
7796  struct of_state qdontuse4;
7797  struct of_state *qptr4=&qdontuse4;
7798  get_state(pr,ptrgeom,qptr4);
7799  FTYPE Ueomtype[NPR],Ueomtypeabs[NPR];
7800  primtoflux(UNOTHING,pr,qptr4,TT,ptrgeom,Ueomtype,Ueomtypeabs);
7801  eomtypelocal=EOMTYPE; // now do EOMTYPE
7802  Utoprimgen(0,0,0,0,1,&eomtypelocal,CAPTYPEBASIC,0,1,EVOLVEUTOPRIM,UNOTHING,Ueomtype,qptr4,ptrgeom,0,pr,pr,&newtonstats);
7803  // PLOOP(pliter,pl) if(pl>=URAD1 && pl<=URAD3) pr[pl]=prold[pl];
7804  if(doit) PLOOP(pliter,pl) dualfprintf(fail_file,"newprMHD[%d]=%g\n",pl,pr[pl]);
7805 
7806  }
7807 
7808 
7809 
7810  }
7811  else if(whichinversion==1){
7812 
7813 
7814  // DLOOPA dualfprintf(fail_file,"Econ[%d]=%21.15g Bcon[%d]=%21.15g\n",j,Econ[j],j,Bcon[j]);
7815 
7816 
7817  // Use Eeta,Beta to get primitives (v will be in WHICHVEL)
7818  // ASSUMES FORCE-FREE!
7819  if(doit) dualfprintf(fail_file,"BEFORE EBtopr()\n");
7820  if(doit) dualfprintf(fail_file,"%g %g %g %g : %g %g %g %g\n",Eetacon[0],Eetacon[1],Eetacon[2],Eetacon[3],Betacon[0],Betacon[1],Betacon[2],Betacon[3]);
7821  EBtopr(Eetacon,Betacon,ptrgeom,pr);
7822  if(doit) dualfprintf(fail_file,"AFTER EBtopr()\n");
7823  if(doit) PLOOP(pliter,pl) dualfprintf(fail_file,"newpr[%d]=%g\n",pl,pr[pl]);
7824  }
7825 
7827  // revert radiation primitives to unmodified values (in whichvel, whichcoord)
7828  PLOOP(pliter,pl) if(pl>=URAD1 && pl<=URAD3) pr[pl]=prold[pl];
7829  }
7830  // if(EOMTYPE!=EOMFFDE&& EOMTYPE!=EOMFFDE2){
7831  // // revert rho and ug and anything beyond field
7832  // PLOOP(pliter,pl) if((pl>=RHO && pl<=UU) || (pl>B3 && pl>URAD3)) pr[pl]=prold[pl];
7833  // }
7834 
7835 
7836  // some checks
7837  struct of_state qdontuse2;
7838  struct of_state *qptr2=&qdontuse2;
7839  get_state(pr,ptrgeom,qptr2);
7840  bcon_calc(pr,qptr2->ucon,qptr2->ucov,qptr2->bcon);
7841  FTYPE fdd03;
7842  fdd03 = ptrgeom->gdet * (qptr2->ucon[1]*qptr2->bcon[2] - qptr2->ucon[2]*qptr2->bcon[1]) ;
7843  if(doit) dualfprintf(fail_file,"fdd03=%g\n",fdd03);
7844 
7845  FTYPE mhdflux[NDIM][NDIM];
7846  DLOOPA(j) mhd_calc(pr, j, ptrgeom, qptr2, mhdflux[j], NULL);
7847  if(doit) dualfprintf(fail_file,"mhdflux=%g %g %g\n",mhdflux[1][0],mhdflux[2][0],mhdflux[3][0]);
7848  FTYPE mhdfluxma[NDIM][NDIM];
7849  DLOOPA(j) mhd_calc_ma(pr, j, ptrgeom, qptr2, mhdfluxma[j], NULL, NULL, NULL);
7850  if(doit) dualfprintf(fail_file,"mhdfluxma=%g %g %g\n",mhdfluxma[1][0],mhdfluxma[2][0],mhdfluxma[3][0]);
7851  FTYPE mhdfluxem[NDIM][NDIM];
7852  DLOOPA(j) mhd_calc_em(pr, j, ptrgeom, qptr2, mhdfluxem[j], NULL);
7853  if(doit) dualfprintf(fail_file,"mhdfluxem=%g %g %g\n",mhdfluxem[1][0],mhdfluxem[2][0],mhdfluxem[3][0]);
7854 
7855 
7856 
7857  // transform WHICHVEL back to *whichvel (only for plasma, not radiation)
7858  FTYPE ucontemp[NDIM];
7859  // dualfprintf(fail_file,"BEFORE pr2ucon\n");
7860  int return1=pr2ucon(WHICHVEL,pr,ptrgeom,ucontemp);
7861  // dualfprintf(fail_file,"AFTER pr2ucon\n");
7862  if(failed || return1){
7863  for(pl=RHO;pl<=U3;pl++) pr[pl]=prold[pl];
7864  failed=0;
7865  }
7866  else{
7867  // dualfprintf(fail_file,"BEFORE ucon2pr: %d\n",*whichvel);
7868  ucon2pr(*whichvel,ucontemp,ptrgeom,pr);
7869  // dualfprintf(fail_file,"AFTER ucon2pr\n");
7870  // PLOOP(pliter,pl) dualfprintf(fail_file,"from ucon2pr[%d]=%g\n",pl,pr[pl]);
7871 
7872  if(0&&r<Rhor){
7873  for(pl=RHO;pl<=U3;pl++) pr[pl]=prold[pl] + (pr[pl]-prold[pl])/(Rhor-0.0)*(r-0.0);
7874  }
7875  else{
7876  // keep
7877  }
7878  }
7879 
7880 
7881  if(EOMTYPE==EOMFFDE || EOMTYPE==EOMFFDE2){
7882  // pr[U1]=pr[U2]=pr[U3]=0.0;
7883  // dualfprintf(fail_file,"EBtopr\n");
7884  }
7885  else{ // just use atmosphere (normally a ZAMO) frame for fluid frame
7886  // for(pl=RHO;pl<=U3;pl++) pr[pl]=prold[pl];
7887  }
7888 
7889 
7890  int BOOSTFIELD=0; // for moving BH problem // WALD
7891 
7892  if(BOOSTFIELD){
7893  // BOOST of field
7894  FTYPE xx=r*sin(th)*cos(ph);
7895  FTYPE yy=r*sin(th)*sin(ph);
7896  FTYPE zz=r*cos(th);
7897  FTYPE lambdatrans[NDIM][NDIM];
7898  FTYPE ilambdatrans[NDIM][NDIM];
7899  // assume time doesn't change or mix with space
7900  lambdatrans[TT][TT]=1.0;
7901  SLOOPA(j) lambdatrans[TT][j] = lambdatrans[j][TT] = 0.0;
7902 
7903  ilambdatrans[TT][TT]=1.0;
7904  SLOOPA(j) ilambdatrans[TT][j] = ilambdatrans[j][TT] = 0.0;
7905 
7906  // rest come from definitions of {x,y,z}(r,\theta,\phi)
7907  // assumes orthonormal to orhonormal!
7908  lambdatrans[1][RR] = sin(th)*cos(ph);
7909  lambdatrans[1][TH] = cos(th)*cos(ph);
7910  lambdatrans[1][PH] = -sin(ph);
7911 
7912  lambdatrans[2][RR] = sin(th)*sin(ph);
7913  lambdatrans[2][TH] = cos(th)*sin(ph);
7914  lambdatrans[2][PH] = cos(ph);
7915 
7916  lambdatrans[3][RR] = cos(th);
7917  lambdatrans[3][TH] = -sin(th);
7918  lambdatrans[3][PH] = 0.0;
7919 
7920  // Cart 2 SPC
7921  ilambdatrans[1][RR] = sin(th)*cos(ph);
7922  ilambdatrans[1][TH] = cos(th)*cos(ph);
7923  ilambdatrans[1][PH] = -sin(ph);
7924 
7925  ilambdatrans[2][RR] = sin(th)*sin(ph);
7926  ilambdatrans[2][TH] = cos(th)*sin(ph);
7927  ilambdatrans[2][PH] = cos(ph);
7928 
7929  ilambdatrans[3][RR] = cos(th);
7930  ilambdatrans[3][TH] = -sin(th);
7931  ilambdatrans[3][PH] = 0.0;
7932 
7933  // quasi-orthonormal
7934  FTYPE finalvec[NDIM];
7935  finalvec[TT]=0.0;
7936  finalvec[RR]=0.3; // x
7937  finalvec[TH]=0; // y
7938  finalvec[PH]=0; // z
7939 
7940 
7941  // transform from ortho Cart to ortho SPC
7942  FTYPE tempcomp[NDIM];
7943  DLOOPA(j) tempcomp[j]=0.0;
7944  DLOOP(j,k){
7945  tempcomp[k] += ilambdatrans[j][k]*finalvec[j];
7946  }
7947  DLOOPA(j) finalvec[j]=tempcomp[j]; // spc
7948 
7949 
7950  // add non-ortho coordinate basis velocity to ortho
7951  finalvec[TT]=0.0;
7952  finalvec[RR]=pr[U1] + finalvec[1]/sqrt(ptrgeom->gcov[GIND(1,1)]);
7953  finalvec[TH]=pr[U2] + finalvec[2]/sqrt(ptrgeom->gcov[GIND(2,2)]);
7954  finalvec[PH]=pr[U3] + finalvec[3]/sqrt(ptrgeom->gcov[GIND(3,3)]);
7955 
7956  pr[U1] = finalvec[RR];
7957  pr[U2] = finalvec[TH];
7958  pr[U3] = finalvec[PH];
7959  }
7960 
7961 
7962  // stick J^\mu into dump file
7963 #if(0)
7964  for(k=U1;k<=U1+3;k++){
7965  pr[k] = Jcon[k-U1];
7966  }
7967 #endif
7968  // dualfprintf(fail_file,"ii=%d jj=%d\n",ii,jj);
7969 
7970 #if(0)
7971  // E.B
7972  k=U1;
7973  pr[k]=0;
7974  DLOOPA(j) pr[k]+=Ecov[j]*Bcon[j];
7975 
7976  // B^2-E^2
7977  k=UU;
7978  pr[k]=0;
7979  DLOOPA(j) pr[k]+=Bcov[j]*Bcon[j];
7980  // DLOOPA pr[k]+=Bcon[j];
7981  DLOOPA(j) pr[k]-=Ecov[j]*Econ[j];
7982 
7983  pr[B1]=Econ[1]*geom.g/etacov[TT];
7984 #endif
7985 
7986 
7987 #if(0)
7988  struct of_state q;
7989  FTYPE faradaytest[NDIM][NDIM];
7990 
7991  // check where faraday changed
7992  get_state(pr,ptrgeom,&q);
7993 
7994  faraday_calc(0,q.bcon,q.ucon,&geom,faradaytest);
7995  // DLOOP dualfprintf(fail_file,"%21.15g %21.15g\n",faradaytest[j][k],Fcov[j][k]);
7996  DLOOP{
7997  if(fabs(faradaytest[j][k]-Fcov[j][k])>1E-10){
7998  dualfprintf(fail_file,"1 %d %d : %21.15g %21.15g\n",ii,jj,faradaytest[j][k],Fcov[j][k]);
7999  }
8000  }
8001  if(fabs(faradaytest[0][3])>1E-10) dualfprintf(fail_file,"1 Fcov=%21.15g faraday=%21.15g\n",Fcov[0][3],faradaytest[0][3]);
8002 #endif
8003 
8004  }// end if FIELDWALD
8005 
8006 
8007  return(0);
8008 }
8009 
8010 // compute after A_\mu -> Bstag^i -> Bcent^i to use proper centered field in inversion to get centered velocity for, e.g., Wald problem
8011 int init_postvpot(int i, int j, int k, FTYPE *pr, FTYPE *pstag, FTYPE *ucons){
8012 
8013  int set_fieldtype(void);
8014  int FIELDTYPE=set_fieldtype();
8015 
8016  if(FIELDTYPE==SPLITMONOPOLE || FIELDTYPE==MONOPOLE || FIELDTYPE==FIELDWALD){
8017 
8018  int whichvel=WHICHVEL;
8019  int whichcoord=PRIMECOORDS;
8020  int whichmethod=1;
8021  int whichinversion=0;
8022  fieldprim(whichmethod, whichinversion, &whichvel, &whichcoord, i, j, k, pr);
8023 
8024  }
8025 
8026  return(0);
8027 }
8028 
8029 
8030 // Also tried:
8031 
8032 // For final conservation and noise
8033 // 1) uniform grid near hole for half of grid (/home/jon/coordradial.nb)
8034 // 2) Turned off POLEDEATH and GAMMAPOLEDEATH
8035 // 3) advance_standard_orig() instead.
8036 
8037 // For hot MHD part in cylindrical part of jet that gets in way
8038 // 1) Cleaning/cooling off as floor held in wave front. Still happens.
8039 
8040 // For initial energy-momentum flux:
8041 // 1) u^i at r>3
8042 // 2) B_\phi (this function) called after the second set_grid_all() and before copy_prim2panalytic() in initbase.c
8043 // 3) F_{it}=0 before used but after set
8044 // 4) a=0 before first set_grid_all, a=0.8 before second
8045 // 5) Wald A_\mu has zero a term.
8046 // 6) Only use A_\phi
8047 // 7) fieldfrompotential[B3]=0 so only use poloidal Wald.
8048 // 8) Ecov=0
8049 // 9) Econ=0
8050 void shortout_Bd3(FTYPE (*prim)[NSTORE2][NSTORE3][NPR])
8051 {
8052  int i,j,k,pliter,pl;
8053  struct of_geom geomdontuse;
8054  struct of_geom *ptrgeom=&geomdontuse;
8055  FULLLOOP{
8056  get_geometry(i,j,k,CENT,ptrgeom);
8057  FTYPE Bcov[NDIM];
8058  FTYPE *pr = &MACP0A1(prim,i,j,k,0);
8059 
8060  FTYPE Bu1,Bu2,gcon03,gcon13,gcon23,gcon33;
8061  FTYPE gcov01,gcov02,gcov11,gcov12,gcov21,gcov22,gcov03,gcov13,gcov23;
8062 
8063  Bu1=pr[B1];
8064  Bu2=pr[B2];
8065 
8066  gcon03=ptrgeom->gcon[GIND(0,3)];
8067  gcon13=ptrgeom->gcon[GIND(1,3)];
8068  gcon23=ptrgeom->gcon[GIND(2,3)];
8069  gcon33=ptrgeom->gcon[GIND(3,3)];
8070 
8071  gcov01=ptrgeom->gcov[GIND(0,1)];
8072  gcov02=ptrgeom->gcov[GIND(0,2)];
8073  gcov11=ptrgeom->gcov[GIND(1,1)];
8074  gcov12=gcov21=ptrgeom->gcov[GIND(1,2)];
8075  gcov22=ptrgeom->gcov[GIND(2,2)];
8076  gcov03=ptrgeom->gcov[GIND(0,3)];
8077  gcov13=ptrgeom->gcov[GIND(1,3)];
8078  gcov23=ptrgeom->gcov[GIND(2,3)];
8079 
8080  FTYPE myBd3=0.0;
8081 
8082  FTYPE ftemp=(1.0 - gcon03*gcov03 - gcon13*gcov13 - gcon23*gcov23);
8083  FTYPE igdetnosing=sign(ftemp)/(fabs(ftemp)+SMALL);
8084  pl=B3; pr[pl] = (myBd3*gcon33 + Bu1*gcon03*gcov01 + Bu2*gcon03*gcov02 + Bu1*gcon13*gcov11 + Bu2*gcon13*gcov12 + Bu1*gcon23*gcov21 + Bu2*gcon23*gcov22)*igdetnosing;
8085 
8086  FTYPE ucon[NDIM];
8087  pr2ucon(WHICHVEL,pr,ptrgeom,ucon);
8088 
8089  FTYPE X[NDIM],V[NDIM];
8090  coord(i, j, k, CENT, X);
8091  bl_coord(X, V);
8092  FTYPE r,th,ph;
8093  r=V[1];
8094  th=V[2];
8095  ph=V[3];
8096 
8097  if(r>3.0) ucon[1]=ucon[2]=ucon[3]=0.0;
8098 
8099  ucon2pr(WHICHVEL,ucon,ptrgeom,pr);
8100  }
8101 
8102  extern void filterffde(int i, int j, int k, FTYPE *pr);
8103 
8104  COMPFULLLOOP{
8105  filterffde(i,j,k,GLOBALMAC(pglobal,i,j,k));
8106  }
8107 
8108 }
8109 
8110 
8111 //#define FCOVDERTYPE DIFFNUMREC
8112 #define FCOVDERTYPE DIFFGAMMIE
8113 
8114 // see conn_func() for notes
8115 #if((REALTYPE==DOUBLETYPE)||(REALTYPE==FLOATTYPE))
8116 #define FCOVDXDELTA 1E-5
8117 #elif(REALTYPE==LONGDOUBLETYPE)
8118 #define FCOVDXDELTA 1E-6
8119 #endif
8120 
8121 void Fcov_numerical(int whichcoord, FTYPE *X, FTYPE (*Fcov)[NDIM])
8122 {
8123  int j,k,l;
8124  FTYPE Xhk[NDIM], Xlk[NDIM];
8125  FTYPE Xhj[NDIM], Xlj[NDIM];
8126  FTYPE Vhk[NDIM], Vlk[NDIM];
8127  FTYPE Vhj[NDIM], Vlj[NDIM];
8128  FTYPE mcovhj,mcovlj,kcovhj,kcovlj;
8129  FTYPE mcovhk,mcovlk,kcovhk,kcovlk;
8130  FTYPE mcov_func_mcoord(struct of_geom *ptrgeom, FTYPE* X, int i, int j); // i not used
8131  FTYPE kcov_func_mcoord(struct of_geom *ptrgeom, FTYPE* X, int i, int j); // i not used
8132  extern int dfridr(FTYPE (*func)(struct of_geom *,FTYPE*,int,int), struct of_geom *ptrgeom, FTYPE *X,int ii, int jj, int kk, FTYPE *ans);
8133 
8134 
8135  // setup dummy grid location since dxdxp doesn't need to know if on grid since don't store dxdxp (needed for dfridr())
8136  struct of_geom geom;
8137  struct of_geom *ptrgeom;
8138  ptrgeom=&geom;
8139  ptrgeom->i=0;
8140  ptrgeom->j=0;
8141  ptrgeom->k=0;
8142  ptrgeom->p=NOWHERE;
8143 
8144  // get V in case whichcoord!=PRIMECOORDS
8145  // FTYPE V[NDIM];
8146  // FTYPE dxdxp[NDIM][NDIM];
8147  // FTYPE idxdxp[NDIM][NDIM];
8148  // if(whichcoord!=PRIMECOORDS){
8149  // bl_coord(X, V);
8150  // dxdxprim(X, V, dxdxp);
8151  // idxdxprim(dxdxp, idxdxp);
8152  // }
8153  // else{
8154  // DLOOPA(j) V[j]=X[j];
8155  // // PRIMECOORDS, then no transformation
8156  // DLOOP(j,k) idxdxp[j][k]=0.0;
8157  // DLOOPA(j) idxdxp[j][j]=1.0;
8158  // }
8159 
8160 
8161  // GET Fcov
8162  if(FCOVDERTYPE==DIFFGAMMIE){
8163 
8164  for(k=0;k<NDIM;k++){
8165  for(j=0;j<NDIM;j++){
8166 
8167  for(l=0;l<NDIM;l++) Xlk[l]=Xhk[l]=Xlj[l]=Xhj[l]=X[l]; // location of derivative
8168  Xhk[k]+=FCOVDXDELTA; // shift up
8169  Xlk[k]-=FCOVDXDELTA; // shift down
8170 
8171  Xhj[j]+=FCOVDXDELTA; // shift up
8172  Xlj[j]-=FCOVDXDELTA; // shift down
8173 
8174  if(whichcoord!=PRIMECOORDS){
8175  bl_coord(Xhk, Vhk);
8176  bl_coord(Xlk, Vlk);
8177  bl_coord(Xhj, Vhj);
8178  bl_coord(Xlj, Vlj);
8179  }
8180  else{
8181  int jj;
8182  DLOOPA(jj){
8183  Vhk[jj]=Xhk[jj];
8184  Vlk[jj]=Xlk[jj];
8185  Vhj[jj]=Xhj[jj];
8186  Vlj[jj]=Xlj[jj];
8187  }
8188  }
8189 
8190 
8191  // dualfprintf(fail_file,"got here1: k=%d j=%d\n",k,j);
8192 
8193 
8194  mcovhj=mcov_func_mcoord(ptrgeom,Xhk,0,j); // 0 not used
8195  // dualfprintf(fail_file,"got here1.1: k=%d j=%d\n",k,j);
8196  mcovlj=mcov_func_mcoord(ptrgeom,Xlk,0,j); // 0 not used
8197  // dualfprintf(fail_file,"got here1.2: k=%d j=%d\n",k,j);
8198  mcovhk=mcov_func_mcoord(ptrgeom,Xhj,0,k); // 0 not used
8199  // dualfprintf(fail_file,"got here1.3: k=%d j=%d\n",k,j);
8200  mcovlk=mcov_func_mcoord(ptrgeom,Xlj,0,k); // 0 not used
8201  // dualfprintf(fail_file,"got here1.4: k=%d j=%d\n",k,j);
8202 
8203  kcovhj=kcov_func_mcoord(ptrgeom,Xhk,0,j); // 0 not used
8204  // dualfprintf(fail_file,"got here1.5: k=%d j=%d\n",k,j);
8205  kcovlj=kcov_func_mcoord(ptrgeom,Xlk,0,j); // 0 not used
8206  // dualfprintf(fail_file,"got here1.6: k=%d j=%d\n",k,j);
8207  kcovhk=kcov_func_mcoord(ptrgeom,Xhj,0,k); // 0 not used
8208  // dualfprintf(fail_file,"got here1.7: k=%d j=%d\n",k,j);
8209  kcovlk=kcov_func_mcoord(ptrgeom,Xlj,0,k); // 0 not used
8210  // dualfprintf(fail_file,"got here1.8: k=%d j=%d\n",k,j);
8211 
8212  // dualfprintf(fail_file,"got here2: j=%d k=%d idxdxp=%g %g\n",j,k,idxdxp[j][k],idxdxp[k][j]);
8213 
8214  aforwald=a;
8215 
8216  // F_{\mu\nu} = A_\nu,\mu - A_\mu,\nu
8217  // A_\phi only
8218  Fcov[j][k]=0.0;
8219 
8220  if(j==WALDWHICHACOV || WALDWHICHACOV==-1){
8221  Fcov[j][k] += 0.5*B0WALD*(
8222  +(mcovhj - mcovlj) / (Vhk[k] - Vlk[k])
8223  +2.0*aforwald*(
8224  +(kcovhj - kcovlj) / (Vhk[k] - Vlk[k])
8225  )
8226  );
8227  }
8228  if(k==WALDWHICHACOV || WALDWHICHACOV==-1){
8229  Fcov[j][k] += 0.5*B0WALD*(
8230  -(mcovhk - mcovlk) / (Vhj[j] - Vlj[j])
8231  +2.0*aforwald*(
8232  -(kcovhk - kcovlk) / (Vhj[j] - Vlj[j])
8233  )
8234  );
8235  }
8236  }// j
8237  }// k
8238  }
8239  else if(FCOVDERTYPE==DIFFNUMREC){
8240 
8241  aforwald=a;
8242 
8243  for(k=0;k<NDIM;k++) for(j=0;j<NDIM;j++){
8244  Fcov[j][k]=0.0;
8245 
8246  // 0 in dfridr not used
8247  FTYPE ans1; dfridr(mcov_func_mcoord,ptrgeom,X,0,j,k,&ans1);
8248  FTYPE ans2; dfridr(mcov_func_mcoord,ptrgeom,X,0,k,j,&ans2);
8249  FTYPE ans3; dfridr(kcov_func_mcoord,ptrgeom,X,0,j,k,&ans3);
8250  FTYPE ans4; dfridr(kcov_func_mcoord,ptrgeom,X,0,k,j,&ans4);
8251  if(j==WALDWHICHACOV || WALDWHICHACOV==-1){
8252  Fcov[j][k] += B0WALD*(ans1 +2.0*aforwald*(ans3));
8253  }
8254  if(k==WALDWHICHACOV || WALDWHICHACOV==-1){
8255  Fcov[j][k] += B0WALD*(ans2 +2.0*aforwald*(ans4));
8256  }
8257  }
8258 
8259  dualfprintf(fail_file,"NOT SETUP FOR NUMREC\n");
8260  myexit(23483466);
8261 
8262  }// end DIFFNUMREC
8263 
8264 }// end Fcov_numerical()
8265 
8266 
8267 
8268 
8269 
8270 // returns MCOORD m_\mu form of m^\mu={0,0,0,1} value for jth element
8271 FTYPE mcov_func_mcoord(struct of_geom *ptrgeom, FTYPE* X, int ii, int jj) // i not used
8272 {
8273  FTYPE mcon[NDIM];
8274  FTYPE mcov[NDIM];
8275 
8276  int getprim=0;
8277  int whichcoord=MCOORD;
8278  gset_X(getprim, whichcoord, ptrgeom->i, ptrgeom->j, ptrgeom->k, ptrgeom->p, X, ptrgeom);
8279 
8280  // dualfprintf(fail_file,"got here3.3\n");
8281  mcon[TT]=0.0;
8282  mcon[RR]=0.0;
8283  mcon[TH]=0.0;
8284  mcon[PH]=1.0;
8285  // dualfprintf(fail_file,"got here3.4\n");
8286 
8287  // lower only needs geom->gcov
8288  lower_vec(mcon,ptrgeom,mcov);
8289  // dualfprintf(fail_file,"got here3.5\n");
8290 
8291  return(mcov[jj]);
8292 }
8293 
8294 // returns MCOORD k_\mu form of k^\mu={1,0,0,0} value for jth element
8295 FTYPE kcov_func_mcoord(struct of_geom *ptrgeom, FTYPE* X, int ii, int jj) // i not used
8296 {
8297  FTYPE kcon[NDIM];
8298  FTYPE kcov[NDIM];
8299 
8300  int getprim=0;
8301  int whichcoord=MCOORD;
8302  gset_X(getprim, whichcoord, ptrgeom->i, ptrgeom->j, ptrgeom->k, ptrgeom->p, X, ptrgeom);
8303 
8304  kcon[TT]=1.0;
8305  kcon[RR]=0.0;
8306  kcon[TH]=0.0;
8307  kcon[PH]=0.0;
8308 
8309  // lower only needs geom->gcov
8310  lower_vec(kcon,ptrgeom,kcov);
8311 
8312  return(kcov[jj]);
8313 }
8314 
8315 void Jcon_numerical(int whichcoord, FTYPE *X, FTYPE *Jcon)
8316 {
8317  int j,k,l;
8318  FTYPE Xh[NDIM], Xl[NDIM];
8319  FTYPE Vh[NDIM], Vl[NDIM];
8320  FTYPE Fconh,Fconl;
8321  FTYPE Fcon_func_mcoord(struct of_geom *ptrgeom, FTYPE* X, int i, int j);
8322  extern int dfridr(FTYPE (*func)(struct of_geom *,FTYPE*,int,int), struct of_geom *ptrgeom, FTYPE *X,int ii, int jj, int kk, FTYPE *ans);
8323 
8324 
8325  // setup dummy grid location since dxdxp doesn't need to know if on grid since don't store dxdxp (needed for dfridr())
8326  struct of_geom geom;
8327  struct of_geom *ptrgeom;
8328  ptrgeom=&geom;
8329  ptrgeom->i=0;
8330  ptrgeom->j=0;
8331  ptrgeom->k=0;
8332  ptrgeom->p=NOWHERE;
8333 
8334 
8335 
8336 
8337  if(FCOVDERTYPE==DIFFGAMMIE){
8338 
8339  for(k=0;k<NDIM;k++){
8340 
8341  Jcon[k] = 0;
8342  for(j=0;j<NDIM;j++){
8343 
8344  for(l=0;l<NDIM;l++) Xl[l]=Xh[l]=X[l]; // location of derivative
8345  Xh[j]+=FCOVDXDELTA; // shift up
8346  Xl[j]-=FCOVDXDELTA; // shift down
8347 
8348  if(whichcoord!=PRIMECOORDS){
8349  bl_coord(Xh, Vh);
8350  bl_coord(Xl, Vl);
8351  }
8352  else{
8353  int jj;
8354  DLOOPA(jj){
8355  Vh[jj]=Xh[jj];
8356  Vl[jj]=Xl[jj];
8357  }
8358  }
8359 
8360  // F^{kj}
8361  Fconh=Fcon_func_mcoord(ptrgeom,Xh,k,j);
8362  Fconl=Fcon_func_mcoord(ptrgeom,Xl,k,j);
8363 
8364  // J^\mu = {F^{\mu\nu}}_{;\nu}
8365  // \detg J^k = F^{kj}_{;j} = (\detg F^{kj})_{,j} <--- thing actually computed and returned
8366  Jcon[k] += (Fconh - Fconl) / (Vh[j] - Vl[j]) ;
8367  } // j
8368  }// k
8369 
8370  }
8371  else if(FCOVDERTYPE==DIFFNUMREC){
8372 
8373  for(k=0;k<NDIM;k++){
8374  Jcon[k] = 0;
8375  for(j=0;j<NDIM;j++){
8376  FTYPE ans1; dfridr(Fcon_func_mcoord,ptrgeom,X,0,j,k,&ans1);
8377  Jcon[k]+=ans1;
8378  }
8379  }
8380 
8381  // get V in case whichcoord!=PRIMECOORDS
8382  FTYPE V[NDIM];
8383  FTYPE dxdxp[NDIM][NDIM];
8384  FTYPE idxdxp[NDIM][NDIM];
8385  if(whichcoord!=PRIMECOORDS){
8386  bl_coord(X, V);
8387  dxdxprim(X, V, dxdxp);
8388  idxdxprim(dxdxp, idxdxp);
8389  }
8390  else{
8391  DLOOPA(j) V[j]=X[j];
8392  // PRIMECOORDS, then no transformation
8393  DLOOP(j,k) idxdxp[j][k]=0.0;
8394  DLOOPA(j) idxdxp[j][j]=1.0;
8395  }
8396 
8397  // get whichcoord version -- assumes Fcon is in whichcoord
8398  FTYPE Jconorig[NDIM];
8399  DLOOPA(j) Jconorig[j]=Jcon[j];
8400  DLOOPA(j) Jcon[j]=0.0;
8401  DLOOP(j,k){
8402  Jcon[k] += Jconorig[j] * idxdxp[k][j];
8403  }
8404 
8405  }
8406 
8407 
8408 
8409 
8410 
8411 }
8412 
8413 #undef FCOVDERTYPE
8414 #undef FCOVDXDELTA
8415 
8416 
8417 
8418 
8419 // returns MCOORD F^{ii jj}
8420 FTYPE Fcon_func_mcoord(struct of_geom *ptrgeom, FTYPE* X, int ii, int jj)
8421 {
8422  int getprim=0;
8423  int whichcoord=MCOORD;
8424  gset_X(getprim, whichcoord, ptrgeom->i, ptrgeom->j, ptrgeom->k, ptrgeom->p, X, ptrgeom);
8425 
8426  FTYPE Fcov[NDIM][NDIM];
8427  void Fcov_numerical(int whichcoord, FTYPE *X, FTYPE (*Fcov)[NDIM]);
8428  Fcov_numerical(MCOORD, X, Fcov);
8429 
8430  // get covariant Maxwell from contravariant
8431  FTYPE Fcon[NDIM][NDIM];
8432  extern void raise_A(FTYPE (*Acov)[NDIM], struct of_geom *geom, FTYPE (*Acon)[NDIM]);//extern void raise_A(FTYPE Acov[NDIM][NDIM], struct of_geom *geom, FTYPE Acon[NDIM][NDIM]);
8433  raise_A(Fcov, ptrgeom, Fcon) ;
8434 
8435  // return gdet*F^{ii jj}
8436  return(ptrgeom->gdet*Fcon[ii][jj]);
8437 }
8438 
8439 
8440 
8441 
8442 
8443 
8444 
8445 
8446 
8447 
8449 {
8450  int funreturn;
8451  int fieldfrompotential[NDIM];
8452 
8453  funreturn=user1_init_vpot2field_user(time, fieldfrompotential, A, prim, pstag, ucons, Bhat);
8454  if(funreturn!=0) return(funreturn);
8455 
8456  return(0);
8457 
8458 
8459 }
8460 
8461 
8462 
8463 // assumes we are fed the true densities
8465 {
8466 
8468  int funreturn=getmax_densities_full(prim,&rhomax,&umax,&uradmax,&utotmax,&pmax,&pradmax,&ptotmax);
8469 
8470 
8471  if(0){
8472  FTYPE parms[MAXPASSPARMS];
8473  int eqline;
8474 
8475  eqline=1;
8476  parms[0]=rin;
8477  parms[1]=rhodisk;
8478 
8479  funreturn+=user1_normalize_densities(eqline, parms, prim, &rhomax, &umax);
8480  }
8481 
8482  return(funreturn);
8483 }
8484 
8485 
8486 
8487 // assumes we are fed the true densities
8489 {
8490  int funreturn;
8491 
8492  funreturn=user1_getmax_densities_full(prim,rhomax, umax, uradmax, utotmax, pmax, pradmax, ptotmax);
8493  if(funreturn!=0) return(funreturn);
8494 
8495  return(0);
8496 }
8497 
8498 
8499 // get maximum b^2 and p_tot
8500 int get_maxes(FTYPE (*prim)[NSTORE2][NSTORE3][NPR], FTYPE *bsq_max, FTYPE *ptot_max, FTYPE *beta_min)
8501 {
8502  int funreturn;
8503  int eqslice=0;
8504  FTYPE parms[MAXPASSPARMS];
8505 
8506  int set_fieldtype(void);
8507  int FIELDTYPE=set_fieldtype();
8508 
8509  if(FIELDTYPE==VERTFIELD || FIELDTYPE==BLANDFORDQUAD || FIELDTYPE==DISK2FIELD || FIELDTYPE==FIELDJONMAD || FIELDTYPE==FIELDWALD){
8510  eqslice=1;
8511  }
8512  else{
8513  eqslice=0;
8514  }
8515 
8516  if(FIELDTYPE==FIELDJONMAD || FIELDTYPE==FIELDWALD){
8517  parms[0]=rinfield;
8518  parms[1]=routfield;
8519  }
8520  else{
8521  parms[0]=rinfield;
8522  parms[1]=routfield;
8523  }
8524 
8525  parms[2]=0.05; // for THETAEQ for near equator only so easy to understand how beta enters. Still should check vertical distribution.
8526 
8527  funreturn=user1_get_maxes(eqslice, parms,prim, bsq_max, ptot_max, beta_min);
8528  if(funreturn!=0) return(funreturn);
8529 
8530  return(0);
8531 }
8532 
8533 
8534 // assumes normal field definition
8536 {
8537  int funreturn;
8538 
8539  int set_fieldtype(void);
8540  int FIELDTYPE=set_fieldtype();
8541 
8542  if(FIELDTYPE!=NOFIELD && FIELDTYPE!=FIELDWALD && DOWALDDEN==0){
8543  dualfprintf(fail_file,"DID NORM FIELD\n");
8544 
8545  funreturn=user1_normalize_field(beta, prim, pstag, ucons, vpot, Bhat);
8546  if(funreturn!=0) return(funreturn);
8547  }
8548 
8549  return(0);
8550 
8551 }
8552 
8553 
8554 
8555 #undef SLOWFAC
8556 
8557 
8558 // UUMIN/RHOMIN used for atmosphere
8559 
8560 // for each WHICHVEL possibility, set atmosphere state for any coordinate system
8561 // which=0 : initial condition
8562 // which=1 : evolution condition (might also include a specific angular momentum or whatever)
8563 // which==1 assumes pr set to something locally reasonable, and we adjust to that slowly
8564 
8565 #define TAUADJUSTATM (10.0) // timescale for boundary to adjust to using preset inflow
8566 int set_atmosphere(int whichcond, int whichvel, struct of_geom *ptrgeom, FTYPE *pr)
8567 {
8568  int funreturn;
8569  int atmospheretype;
8570 
8571  atmospheretype=1; // default
8572 
8573  funreturn=user1_set_atmosphere(atmospheretype, whichcond, whichvel, ptrgeom, pr);
8574  if(funreturn!=0) return(funreturn);
8575 
8576  return(0);
8577 
8578 }
8579 
8580 
8581 
8582 int set_density_floors(struct of_geom *ptrgeom, FTYPE *pr, FTYPE *prfloor, FTYPE *prceiling)
8583 {
8584  int funreturn;
8585 
8586  int pliter,pl;
8587  PLOOP(pliter,pl){
8588  prfloor[RHO]=RHOMINLIMIT;
8589  prfloor[UU]=UUMINLIMIT;
8590 
8591  prceiling[RHO]=BIG;
8592  prceiling[UU]=BIG;
8593 
8594  if(PRAD0>=0){
8595  prfloor[PRAD0]=ERADLIMIT;
8596  prceiling[PRAD0]=BIG;
8597  }
8598  }
8599 
8600  // default is for spherical flow near BH
8601  if(WHICHPROBLEM==RADDONUT){
8602  // KORALTODO: floor currently causes injection of hot matter and run-away problems with radiation.
8603  funreturn=set_density_floors_default(ptrgeom, pr, prfloor, prceiling);
8604 
8605 
8606  // absolute floor, because when magnetic field is zero in some region, then density can go to zero rather than being limited, and then radiation can radically push it around or density gradient can be too extreme for code.
8607  FTYPE V[NDIM];
8608  bl_coord_ijk(ptrgeom->i,ptrgeom->j,ptrgeom->k,ptrgeom->p,V);
8609  // FTYPE lowcoef=MIN(1E-7,RADNT_RHOATMMIN/RADNT_RHODONUT);
8610  FTYPE lowcoef=MIN(1E-9,RADNT_RHOATMMIN/RADNT_RHODONUT); // SUPERMADNEW
8611  FTYPE lowpow=-2.0;
8612  FTYPE rholimit=RADNT_RHODONUT*(lowcoef*pow(V[1],lowpow));
8613  if(pr[RHO]<rholimit) pr[RHO]=rholimit;
8614 
8615  if(funreturn!=0) return(funreturn);
8616  }
8617 
8618  return(0);
8619 }
8620 
8621 int set_density_floors_alt(struct of_geom *ptrgeom, struct of_state *q, FTYPE *pr, FTYPE *U, FTYPE bsq, FTYPE *prfloor, FTYPE *prceiling)
8622 {
8623  int funreturn;
8624 
8625  int pliter,pl;
8626  PLOOP(pliter,pl){
8627  prfloor[RHO]=RHOMINLIMIT;
8628  prfloor[UU]=UUMINLIMIT;
8629 
8630  prceiling[RHO]=BIG;
8631  prceiling[UU]=BIG;
8632 
8633  if(PRAD0>=0){
8634  prfloor[PRAD0]=ERADLIMIT;
8635  prceiling[PRAD0]=BIG;
8636  }
8637  }
8638 
8639  // default is for spherical flow near BH
8640  if(WHICHPROBLEM==RADDONUT){
8641  // KORALTODO: floor currently causes injection of hot matter and run-away problems with radiation.
8642  funreturn=set_density_floors_default_alt(ptrgeom, q, pr, U, bsq, prfloor, prceiling);
8643 
8644  FTYPE V[NDIM];
8645  bl_coord_ijk(ptrgeom->i,ptrgeom->j,ptrgeom->k,ptrgeom->p,V);
8646  // FTYPE lowcoef=MIN(1E-7,RADNT_RHOATMMIN/RADNT_RHODONUT);
8647  FTYPE lowcoef=MIN(1E-9,RADNT_RHOATMMIN/RADNT_RHODONUT); // SUPERMADNEW
8648  FTYPE lowpow=-2.0;
8649  FTYPE rholimit=RADNT_RHODONUT*(lowcoef*pow(V[1],lowpow));
8650  if(pr[RHO]<rholimit) pr[RHO]=rholimit;
8651 
8652 
8653 
8654  if(funreturn!=0) return(funreturn);
8655  }
8656 
8657  return(0);
8658 }
8659 
8660 
8661 
8662 
8663 
8664 
8665 // Setup problem-dependent grid sectioning
8666 int theproblem_set_enerregiondef(int forceupdate, int timeorder, int numtimeorders, long int thenstep, FTYPE thetime, int (*enerregiondef)[NDIM] )
8667 {
8668 
8669  // Torus problem
8670  // torus_set_enerregiondef(forceupdate, timeorder, numtimeorders, thenstep, thetime, enerregiondef);
8671  //jet_set_enerregiondef(forceupdate, timeorder, numtimeorders, thenstep, thetime, enerregiondef);
8672 
8673  if(1){
8674  enerregiondef[POINTDOWN][1]=0;
8675  enerregiondef[POINTUP][1]=totalsize[1]-1;
8676  enerregiondef[POINTDOWN][2]=0;
8677  enerregiondef[POINTUP][2]=totalsize[2]-1;
8678  enerregiondef[POINTDOWN][3]=0;
8679  enerregiondef[POINTUP][3]=totalsize[3]-1;
8680  }
8681 
8683 
8684 
8685  // see advance.c: Whether to allow shift in evolved quantities to preserve conservation and divb=0. Set to zero if exposing that surface in time. Set to 1 if absorbing that surface in time and relying on it to inject a solution.
8693 
8694 
8695 
8696  enerregiondef[POINTDOWN][2]=0;
8697 
8698  enerregiondef[POINTUP][2]=N2BND*2 + round(((thetime+0.0)/45.0)*((FTYPE)(totalsize[2]-1))*(45.0/Rout_array[2]));
8699 
8700  if(enerregiondef[POINTUP][2]>totalsize[2]-1){
8701  enerregiondef[POINTUP][2]=totalsize[2]-1;
8702  }
8703  }
8704 
8705  return(0);
8706 }
8707 
8708 
8709 int theproblem_set_enerregionupdate(int forceupdate, int timeorder, int numtimeorders, long int thenstep, FTYPE thetime, int *updateeverynumsteps, int *everynumsteps)
8710 {
8711 
8713  //
8714  // Setup update period
8715  //
8717 
8719  //
8720  // number of steps after which position/size of active section is updated
8721  //
8723  if(N3==1){
8724  // *updateeverynumsteps=100;
8725  *updateeverynumsteps=1; // update every step since otherwise flow runs into wall at outer boundary
8726  }
8727  else{
8728  // *updateeverynumsteps=10;
8729  *updateeverynumsteps=1; // update every step since otherwise flow runs into wall at outer boundary
8730  }
8731 
8733  //
8734  //number of steps after which position/size of active section is reported to file
8735  //
8737  *everynumsteps = *updateeverynumsteps*100;
8738 
8739  return(0);
8740 }
8741 
8742 
8743 // specify MPI task rank ordering
8744 // example user-dependent code
8745 int theproblem_set_myid(void)
8746 {
8747  int retval;
8748 
8749  // default is to do nothing
8750  // retval=jet_set_myid();
8751  retval=0;
8752 
8753  // do other things?
8754 
8755  return(retval);
8756 
8757 }
8758 
8759 
8760 void adjust_flux(SFTYPE fluxtime, FTYPE (*prim)[NSTORE2][NSTORE3][NPR], FTYPE (*F1)[NSTORE2][NSTORE3][NPR+NSPECIAL], FTYPE (*F2)[NSTORE2][NSTORE3][NPR+NSPECIAL], FTYPE (*F3)[NSTORE2][NSTORE3][NPR+NSPECIAL])
8761 {
8762 
8763  // X1UP
8765  int i,j,k,pl ;
8766  FTYPE sth ;
8767  FTYPE X[NDIM],V[NDIM],r,th ;
8768  int inboundloop[NDIM];
8769  int outboundloop[NDIM];
8770  int innormalloop[NDIM];
8771  int outnormalloop[NDIM];
8773  int riin,riout,rjin,rjout,rkin,rkout;
8774  int dosetbc[COMPDIM*2];
8775  int ri;
8776  int boundvartype=BOUNDFLUXTYPE;
8777 
8779  //
8780  // set bound loop
8781  //
8783  set_boundloop(boundvartype, inboundloop,outboundloop,innormalloop,outnormalloop,inoutlohi, &riin, &riout, &rjin, &rjout, &rkin, &rkout, dosetbc);
8784  //enerregion=ACTIVEREGION; // now replaces TRUEGLOBALENERREGION
8785  // localenerpos=enerposreg[enerregion];
8786 
8787 
8788 
8789  // enforce true reflective condition on (near) polar axis
8790  if(mycpupos[1]==0){
8791  LOOPX1dir{
8792  i=0;
8793 
8794  PALLLOOP(pl) if(pl!=U1 && pl!=URAD1) MACP0A1(F1,i,j,k,pl)=0;
8795  }
8796  }
8797 
8798  struct of_geom geomdontuse;
8799  struct of_geom *ptrgeom=&geomdontuse;
8800 
8801  // prescribe outer boundary condition on wall
8802  if(mycpupos[1]==ncpux1-1){
8803  LOOPX1dir{
8804  i=N1;
8805 
8806  PALLLOOP(pl) if(pl!=U1 && pl!=URAD1) MACP0A1(F1,i,j,k,pl)=0;
8807 
8808  if(1){
8809  get_geometry(i,j,k,FACE1,ptrgeom) ;
8810 
8811  FTYPE t0=1.0;
8812  FTYPE interptime=(fluxtime<1.0 ? (fluxtime/t0) : 1.0);
8813  if(interptime>1.0) interptime=1.0;
8814  if(interptime<0.0) interptime=0.0;
8815 
8816  // interptime=0;
8817 
8818 
8819  // ensure fake pressure equilibrium
8820  FTYPE Ehatstar=1.0*RADCYLJET_EHATJET; // 1.0 ensures pressure equlibrium, regardless of actual physics
8821  FTYPE ustar=1.0*RADCYLJET_UJET; // 1.0 ensures pressure equlibrium, regardless of actual physics
8822  FTYPE rhostar=1.0*RADCYLJET_RHOJET;
8823 
8824  // but drive radiative flux into jet as if vr0*Ehatstar
8825  FTYPE vr0; //=-1E-6; //RADCYLJET_VRSTAR;
8826  // vr0=0.0;
8827  // should really set flux directly, or based upon flux-limited diffusion set vr
8828  // But jump is infinite, but can imagine as if not.
8829 
8830  // set flux of radiation: R^t_x1. Just set Ehat=Ehat0 since non-rel injection, and compute literal flux
8831  FTYPE prflux[NPR];
8832  PALLLOOP(pl) prflux[pl]=0;
8833  prflux[RHO]=0.0; //rhostar; // this has no effect
8834  // prflux[UU]=ustar; // this term ensures pressure equilibrium
8835  prflux[UU]=0.0; //MACP0A1(prim,i,j,k,UU);
8836  prflux[U1]=0.0;
8837  prflux[U2]=0.0;
8838  prflux[U3]=0.0;
8839  prflux[URAD0]=Ehatstar*(1.0-interptime) + interptime*Ehatstar*10.0;
8840 
8841  // prflux[URAD1]=interptime*vr0/sqrt(fabs(ptrgeom->gcov[GIND(1,1)])); // assume vr0 is orthonormal, so get coordinate out of it.
8842  FTYPE kappa=calc_kappaes_user(rhostar,1.0,0,0,0);
8843  vr0=sqrt(1.0/(3.0*kappa*t0));
8844  static int firsttime=1;
8845  if(firsttime) dualfprintf(fail_file,"vr0=%21.15g\n",vr0);
8846  firsttime=0;
8847  prflux[URAD1]=vr0/sqrt(fabs(ptrgeom->gcov[GIND(1,1)])); // assume vr0 is orthonormal, so get coordinate out of it.
8848  prflux[URAD2]=0.0;
8849  prflux[URAD3]=0.0;
8850 
8851  struct of_state q;
8852  get_state(prflux, ptrgeom, &q);
8853  FTYPE fluxrad[NPR];
8854  primtoflux(UEVOLVE, prflux, &q, 1, ptrgeom, fluxrad, NULL);
8855 
8856  //int pliter;
8857  //PLOOP(pliter,pl) dualfprintf(fail_file,"pl=%d fluxrad=%21.15g F1=%21.15g\n",pl,fluxrad[pl],MACP0A1(F1,i,j,k,pl));
8858  //myexit(0);
8859 
8860  // at least pressure term needs to be there
8861  //MACP0A1(F1,i,j,k,U1)=fluxrad[U1]; // use original outflowed, so pressure balance maintained regardless of vr
8862  //MACP0A1(F1,i,j,k,URAD1)=fluxrad[URAD1]; // use original outflowed, so pressure balance maintained regardless of vrad_r
8863  // will this lead, for large energy flux, inconsistently small momentum flux?
8864 
8865  // setup so exactly pressure balanced, so no momentum flux, implying no motion of boundary.
8866  // MACP0A1(F1,i,j,k,U1)=MACP0A1(F1,im1mac(i),j,k,U1);
8867  //MACP0A1(F1,i,j,k,URAD1)=MACP0A1(F1,im1mac(i),j,k,URAD1);
8868 
8869  // But, even with pressure balance, *still* inject energy. So while boundary cannot move, we still leak in energy.
8870  // MACP0A1(F1,i,j,k,U1)=fluxrad[URAD1];
8871  MACP0A1(F1,i,j,k,U1)=MACP0A1(F1,im1mac(i),j,k,U1);
8872 
8873  MACP0A1(F1,i,j,k,URAD1)=fluxrad[URAD1];
8874  MACP0A1(F1,i,j,k,URAD0)=fluxrad[URAD0];
8875 
8876  }
8877  // IDEAS (for failures)
8878 
8879  // *) Turn on flux slowly so not shock. Slower.
8880  // *) Why cylindrical boundary goofy and evolving? (not body forces change, but maybe non-linear conserves)
8881  // e.g. gdet R^r_r = gdet (Ehat \gamma^2 vr^2 + Erad/3) = r Erad . So linear, so should be ok. Must be body force not right really?
8882 
8883  }
8884  }
8885  }
8886 
8887 
8888  // X2DN
8890  int i,j,k,pl ;
8891  FTYPE sth ;
8892  FTYPE X[NDIM],V[NDIM],r,th ;
8893  int inboundloop[NDIM];
8894  int outboundloop[NDIM];
8895  int innormalloop[NDIM];
8896  int outnormalloop[NDIM];
8897  int inoutlohi[NUMUPDOWN][NUMUPDOWN][NDIM];
8898  int riin,riout,rjin,rjout,rkin,rkout;
8899  int dosetbc[COMPDIM*2];
8900  int ri;
8901  int boundvartype=BOUNDFLUXTYPE;
8902 
8904  //
8905  // set bound loop
8906  //
8908  set_boundloop(boundvartype, inboundloop,outboundloop,innormalloop,outnormalloop,inoutlohi, &riin, &riout, &rjin, &rjout, &rkin, &rkout, dosetbc);
8909  //enerregion=ACTIVEREGION; // now replaces TRUEGLOBALENERREGION
8910  // localenerpos=enerposreg[enerregion];
8911 
8912 
8913  // X1DN
8914  // enforce true reflective condition on (near) polar axis
8915  if(mycpupos[1]==0){
8916  LOOPX1dir{
8917  i=0;
8918 
8919  PALLLOOP(pl) if(pl!=U1 && pl!=URAD1) MACP0A1(F1,i,j,k,pl)=0;
8920  }
8921  }
8922 
8923  struct of_geom geomdontuse;
8924  struct of_geom *ptrgeom=&geomdontuse;
8925 
8926  // prescribe outer boundary condition on wall
8927  if(mycpupos[2]==0){
8928  LOOPX2dir{
8929  j=0;
8930 
8931  // PALLLOOP(pl) if(pl!=U2 && pl!=URAD2) MACP0A1(F2,i,j,k,pl)=0;
8932 
8933  if(1){
8934  get_geometry(i,j,k,FACE2,ptrgeom) ;
8935  bl_coord_ijk(i,j,k,FACE2,V);
8936 
8937  FTYPE t0=1.0;
8938  FTYPE interptime=(fluxtime<1.0 ? (fluxtime/t0) : 1.0);
8939  if(interptime>1.0) interptime=1.0;
8940  if(interptime<0.0) interptime=0.0;
8941 
8942  FTYPE prflux[NPR];
8943  extern int jetbound(int i, int j, int k, int loc, FTYPE *prin, FTYPE *prflux, FTYPE (*prim)[NSTORE2][NSTORE3][NPR]);
8944  int insidejet=jetbound(i,j,k,FACE2,MAC(prim,i,j,k),prflux,prim);
8945 
8946 
8947  if(insidejet==0){
8948  //PALLLOOP(pl) if(pl!=U2 && pl!=URAD2) MACP0A1(F2,i,j,k,pl)=0;
8949  // pl=U2; MACP0A1(F2,i,j,k,pl)=MACP0A1(F2,i,jp1mac(j),k,pl);
8950  // pl=URAD2; MACP0A1(F2,i,j,k,pl)=MACP0A1(F2,i,jp1mac(j),k,pl);
8951  }
8952  else{
8953  struct of_state q;
8954  get_state(prflux, ptrgeom, &q);
8955  FTYPE flux[NPR];
8956  primtoflux(UEVOLVE, prflux, &q, 2, ptrgeom, flux, NULL);
8957 
8958  // assign flux
8959  PALLLOOP(pl) if(pl!=U2 && pl!=URAD2) MACP0A1(F2,i,j,k,pl)=0;
8960  PALLLOOP(pl) MACP0A1(F2,i,j,k,pl)=flux[pl];
8961  }
8962 
8963  }
8964  // IDEAS
8965 
8966  }
8967  }
8968  }
8969 
8970 }
8971 
8972 int jetbound(int i, int j, int k, int loc, FTYPE *prin, FTYPE *prflux, FTYPE (*prim)[NSTORE2][NSTORE3][NPR])
8973 {
8974 
8975  struct of_geom geomdontuse;
8976  struct of_geom *ptrgeom=&geomdontuse;
8977  get_geometry(i,j,k,loc,ptrgeom) ;
8978  FTYPE X[NDIM],V[NDIM],r,th ;
8979  bl_coord_ijk(i,j,k,loc,V);
8980 
8981 
8982 
8983  int insidejet;
8984  FTYPE width=1.0;
8985 
8986 
8987  if(V[1]<=width){ // ||1 if want fixed conditions outside of jet as well.
8988  insidejet=1;
8989  }
8990  else{
8991  insidejet=0;
8992  }
8993 
8994  int pliter,pl;
8995  if(insidejet==0){
8996  PALLLOOP(pl) prflux[pl]=prin[pl];
8997 
8998  }
8999  else{
9000 
9001 
9002  // these are really star values
9003  FTYPE Ehatstar=RADCYLJET_EHATJET;
9004  FTYPE ustar=RADCYLJET_UJET;
9005  FTYPE Tstar=RADCYLJET_TEMPJET;
9006  FTYPE pradstar; pradstar=(4.0/3.0-1.0)*calc_LTE_EfromT(Tstar);
9007  FTYPE ujet;
9008  if(WHICHRADSOURCEMETHOD==SOURCEMETHODNONE) ujet=ustar*1E-2*99.99;
9009  else ujet=ustar*1E-2;
9010 
9011 
9012  FTYPE rhostar=RADCYLJET_RHOJET;
9013  FTYPE rhojet; rhojet=rhostar*1E-2;
9014 
9015  // FTYPE vz0;
9016 
9017 
9018  // FTYPE interpi=exp(-V[1]*V[1]/(2.0*width*width));
9019 
9020  // gamma^2 = 1+usq -> usq = gamma^2-1 -> |u| = sqrt(gamma^2-1)
9021  FTYPE gammacore; gammacore = 7.0;
9022  FTYPE uzcore; uzcore=sqrt(gammacore*gammacore-1.0);
9023  FTYPE rhocore; rhocore=rhojet;
9024 
9025  FTYPE efluxcore; efluxcore=rhocore*uzcore*uzcore;
9026 
9027  FTYPE eflux; eflux = efluxcore*stepfunctionab2(V[1],1,0);
9028 
9029  FTYPE uz=uzcore*stepfunctionab2(V[1],1,0); // orthonormal uz
9030  // don't use stepfunctionab() on vz because then drops in gamma very fast.
9031  // gamma^2 = 1/(1-v^2) -> 1-v^2 = 1/gamma^2 -> v^2 = 1-1/gamma^2 -> |v| = sqrt(1-1/gamma^2) or |v| = uz0/gamma
9032  // FTYPE vz0=sqrt(1.0-1.0/(gamma*gamma));
9033  FTYPE rho; rho = eflux/(uz*uz);
9034  if(rho>rhostar) rho=rhostar;
9035 
9036  //stepfunctionab2(V[1],rhojet,rhostar); // so that rho drops faster so no density edge next to jet
9037  //FTYPE ug=stepfunctionab(V[1],ujet,ustar);
9038 
9039 
9040  FTYPE Tjet = 1.62*Tstar;
9041  FTYPE Temp;
9042  Temp = stepfunctionab2(V[1],Tjet,Tstar);
9043  FTYPE ug;
9044  ug = Temp*rho/(gam-1.0);
9045  // FTYPE ug=stepfunctionab(V[1],ujet,ustar);
9046  //FTYPE Tstar=1.0E7/TEMPBAR;
9047  // // P = (arad/3)T^4 + rho T
9048  // pr[UU]=u_rho0_T_simple(i,j,k,CENT,pr[RHO],Tstar);
9049  // pr[URAD0]=calc_LTE_EfromT(Tstar);
9050 
9051  // set flux of radiation: R^t_x1. Just set Ehat=Ehat0 since non-rel injection, and compute literal flux
9052  PALLLOOP(pl) prflux[pl]=0;
9053  prflux[RHO]=rho;
9054  prflux[UU]=ug;
9055  FTYPE Tgas=compute_temp_simple(i,j,k,loc,prflux[RHO],prflux[UU]);
9056 
9057  prflux[U1]=0.0; //MACP0A1(prim,i,jp1mac(j),k,U1);
9058  prflux[U2]=uz/sqrt(fabs(ptrgeom->gcov[GIND(2,2)]));
9059  prflux[U3]=0.0; //MACP0A1(prim,i,jp1mac(j),k,U3);
9060 
9061  prflux[URAD0]=calc_LTE_EfromT(Tgas); // Thermal equilibrium // Ehatstar;
9062  prflux[URAD1]=0.0; //MACP0A1(prim,i,jp1mac(j),k,URAD1);
9063  prflux[URAD2]=prflux[U2]; // optically thick
9064  prflux[URAD3]=0.0; //MACP0A1(prim,i,jp1mac(j),k,URAD3);
9065 
9066  FTYPE ucon[NDIM];
9067  FTYPE others[NUMOTHERSTATERESULTS];
9068  ucon_calc(prflux,ptrgeom,ucon,others);
9069 
9070  static int firsttime=1;
9071  if(firsttime==1){
9072  // cs2 ~ gam*Ptot/rho and need vz0>cs.
9073  // look at sqrt(cs2tot) in SM to check or obtain here.
9074  FTYPE ptot=((gam-1.0)*prflux[UU] + (4.0/3.0-1.0)*prflux[URAD0]);
9075  FTYPE ptrue;
9076  if(WHICHRADSOURCEMETHOD==SOURCEMETHODNONE) ptrue=(gam-1.0)*prflux[UU];
9077  else ptrue=ptot;
9078  FTYPE gamptot=(gam*(gam-1.0)*prflux[UU] + (4.0/3.0)*(4.0/3.0-1.0)*prflux[URAD0]);
9079  FTYPE cs2 = gamptot/prflux[RHO];
9080  dualfprintf(fail_file,"JET: ptrue=%21.15g ptot=%21.15g cs2=%21.15g cs=%21.15g\n",ptrue,ptot,cs2,sqrt(cs2));
9081 
9082  dualfprintf(fail_file,"Tgasinj=%21.15g u/rho=%21.15g prad0/rho=%21.15g\n",Tgas,prflux[UU]/prflux[RHO],prflux[URAD0]/prflux[RHO]);
9083  dualfprintf(fail_file,"TEST: %21.15g\n",0.99*stepfunctionab(1E-2,1.0,0.0));
9084  dualfprintf(fail_file,"TESTUCON0: %21.15g %21.15g %21.15g %21.15g\n",ucon[TT],ucon[1]*sqrt(ptrgeom->gcov[GIND(1,1)]),ucon[2]*sqrt(ptrgeom->gcov[GIND(2,2)]),ucon[3]*sqrt(ptrgeom->gcov[GIND(3,3)]));
9085  dualfprintf(fail_file,"Need: pradstar/rhojet=%21.15g > %21.15g and taujet=%21.15g >>1\n",pradstar/prflux[RHO],ucon[TT]*ucon[TT]*calc_kappaes_user(prflux[RHO],Temp,0,0,0)*width*width/(2.0*Rout_array[2]),calc_kappaes_user(prflux[RHO],Temp,0,0,0)*width);
9086 
9087  FTYPE ljet,ljet1,ljet2,ljet3;
9088  ljet1 = prflux[RHO]*ucon[TT]*ucon[TT]*M_PI*width*width;
9089  ljet2 = (prflux[UU] + (gam-1.0)*prflux[UU])*ucon[TT]*ucon[TT]*M_PI*width*width;
9090  ljet3 = (prflux[URAD0] + (4.0/3.0-1.0)*prflux[URAD0])*ucon[TT]*ucon[TT]*M_PI*width*width;
9091  ljet=ljet1+ljet2+ljet3;
9092 
9093 
9094  FTYPE lrad,lradalt1,lradalt2;
9095  lrad = (pradstar/calc_kappaes_user(prflux[RHO],Temp,0,0,0))*(2.0*M_PI*Rout_array[2]); // best estimate, except rho and T vary inside jet so only applies to core of jet evaluated first. Assumes diffusion scale is width, and it could be smaller in which case flux closer to lradalt2.
9096  lradalt1 = (pradstar/calc_kappaes_user(rhostar,Tstar,0,0,0))*(2.0*M_PI*Rout_array[2]); // underestimate
9097  lradalt2 = (ARAD_CODE*pow(Tstar,4.0)/4.0)*(2.0*M_PI*width*Rout_array[2]); // overestimate
9098 
9099  dualfprintf(fail_file,"Need: ljet=%21.15g < lrad=%21.15g lradalt1=%21.15g lradalt2=%21.15g\n",ljet,lrad,lradalt1,lradalt2);
9100  dualfprintf(fail_file,"ljet1=%21.15g ljet2=%21.15g ljet3=%21.15g\n",ljet1,ljet2,ljet3);
9101 
9102  //sm: set lrad=(-Rud01)*dx2*dx3*gdet if(ti==51)
9103  //sm: set lradtot=SUM(lrad) print {lradtot}
9104 
9105  firsttime=0;
9106  }
9107  }
9108  return(0);
9109 }
9110 
9111 
9112 void adjust_fluxctstag_vpot(SFTYPE fluxtime, FTYPE (*prim)[NSTORE2][NSTORE3][NPR], int *Nvec, FTYPE (*vpot)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3])
9113 {
9114  // not used
9115 }
9116 
9117 void adjust_fluxcttoth_vpot(SFTYPE fluxtime, FTYPE (*prim)[NSTORE2][NSTORE3][NPR], int *Nvec, FTYPE (*vpot)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3])
9118 {
9119  // not used
9120 }
9121 
9122 
9123 void adjust_fluxcttoth_emfs(SFTYPE fluxtime, FTYPE (*prim)[NSTORE2][NSTORE3][NPR], FTYPE (*emf)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3] )
9124 {
9125  // not used
9126 }
9127 
9128 void adjust_fluxctstag_emfs(SFTYPE fluxtime, FTYPE (*prim)[NSTORE2][NSTORE3][NPR], int *Nvec, FTYPE (*fluxvec[NDIM])[NSTORE2][NSTORE3][NPR+NSPECIAL])
9129 {
9130  int i,j,k;
9131 
9132  if(DOWALDDEN&&0){
9133  if(mycpupos[1]==ncpux1-1){
9134  dualfprintf(fail_file,"did modify\n");
9135  FULLLOOP{
9136  if(i==N1){
9137  MACP1A1(fluxvec,2,N1,j,k,B1)=MACP1A1(fluxvec,1,N1,j,k,B2)=0.0;
9138  }
9139  }
9140  }
9141  }
9142  else{
9143  // not used
9144  }
9145 }
9146 
9147 
9148 //**********************************************************************
9149 //******* user opacities ****************************************************
9150 //**********************************************************************
9151 
9153 // ff and s (but means something different for ff and s)
9154 //listlog10xi = Range[-8, 8, 1];
9155 //listxi = 10^listlog10xi;
9156 
9157 //step1mexpf = 0.2
9158 //listlog101mexpf = Range[-3, 0, step1mexpf];
9159 //list1mexpf = 10^listlog101mexpf;
9160 //list1mexpf = Sort[Join[{0}, list1mexpf]];
9161 //listexpf = 1 - list1mexpf;
9163 
9164 
9166 // dc:
9167 //step1mexpf = 0.2*2
9168 //listlog101mexpf = Range[-3, 0, step1mexpf];
9169 //list1mexpf = 10^listlog101mexpf;
9170 //list1mexpf = Sort[Join[{0}, list1mexpf]];
9171 //listexpf = 1 - list1mexpf;
9172 
9173 //stepthetag = 0.5*2
9174 //listlogthetag = Range[-4, 1, stepthetag];
9175 //listthetag = 10^listlogthetag;
9176 
9177 //stepthetae = 0.5
9178 //listlogthetae = Range[-5, 2, stepthetae];
9179 //listthetae = 10^listlogthetae;
9181 
9182 
9183 // as long as scaled-out prefactor, then near Te=Tg and \mu=0 should be order unity and shouldn't be beyond float limit of 1E-30
9184 // and accuracy of double not needed even if iterations in implicit solver required double, because calculations are not that accurate.
9185 //#define TBLTYPE static const float
9186 //#include "opacitytables.c"
9187 
9188 // Steps:
9189 //
9190 // 1) For .nb's in #2, choose MBH, etc. so cut-off chosen and chosen same way for all .nb's.
9191 // 1.5) Ensure size of tables at bottom is consistent as hardcoded size
9192 // 2) run jon@physics-179:/data/jon/pseudotensor@gmail.com/harm_math/opacityfits\ doublecompton_opacity_mu_cutoff2.nb and freefree_opacity_mu_cutoff.nb and synch_opacity_mu_cutoff.nb (with picktemp=0,whichtemp=1 and picktemp=1,whichtemp=1 and picktemp=1,whichtemp=3)
9193 // 3) Run "sed" at bottom of each .nb after everything done.
9194 // 4) In opacityfits directory run: cat energyopacityffmu.dat4 numberopacityffmu.dat4 energyopacitydcmu.dat4 numberopacitydcmu.dat4 energyopacityedcmu.dat4 numberopacityedcmu.dat4 energyopacitysamu.dat4 numberopacitysamu.dat4 energyopacitysbmu.dat4 numberopacitysbmu.dat4 energyopacityscmu.dat4 numberopacityscmu.dat4 > opacitytables.temp.c ; sed 's/"//g' opacitytables.temp.c > opacitytables.c
9195 // 5) copy opacitytables.c to local harmgit directory
9196 
9197 // Opal table in GN93hz
9198 //TABLE # 73 $G&N'93 Solar$ X=0.7000 Y=0.2800 Z=0.0200 dXc=0.0000 dXo=0.0000
9199 //#include "opacityopaltables.c"
9200 
9201 
9202 
9204 //#define KAPPA_ES_KNCORRF(f) (0.75*((-1.*(1. + 3.*(f)))/Power(1. + 2.*(f),2) + (0.5*Log(1. + 2.*(f)))/(f) + ((1. + (f))*((2. + 2.*(f))/(1. + 2.*(f)) - (1.*Log(1. + 2.*(f)))/(f)))/Power((f),2)))
9205 //#define KAPPA_ES_KNCORR(rhocode,Tcode) (KAPPA_ES_KNCORREP(K_BOLTZ*(Tcode)*TEMPBAR/(MELE*CCCTRUE*CCCTRUE)))
9206 #define KAPPA_ES_FERMICORR(rhocode,Tcode) (1.0/(1.0+2.7E11*((rhocode)*RHOBAR)/prpow((Tcode)*TEMPBAR,2.0))) // Buchler and Yueh 1976 (just Fermi part). Fewer electrons when near Fermi fluid limit.
9207 #define KAPPA_ES_KNCORR(rhocode,Tcode) (1.0/(1.0+prpow((Tcode)*TEMPBAR/4.5E8,0.86))) // Buchler and Yueh 1976 . Klein-Nishina for thermal electrons.
9208 
9209 #define KAPPA_ES_CODE(rhocode,Tcode) (0.2*(1.0+XFACT)*KAPPA_ES_FERMICORR(rhocode,Tcode)*KAPPA_ES_KNCORR(rhocode,Tcode)/OPACITYBAR)
9210 #define KAPPA_ES_BASIC_CODE(rhocode,Tcode) (0.2*(1.0+XFACT)/OPACITYBAR)
9211 
9212 // INELASTIC COMPTON TERM
9213 // see DOCOMPTON in physics.tools.rad.c:
9214 // in term2, doesn't change photon energy, so that in scattering-dominated atmospheres, photons move through unchanged by temperature of gas.
9215 // Eq A7 in http://adsabs.harvard.edu/abs/2012ApJ...752...18K used first in http://adsabs.harvard.edu/cgi-bin/bib_query?arXiv:0904.4123 as based upon a calculation in http://adsabs.harvard.edu/abs/2000thas.book.....P .
9216 // Also interesting for next steps:
9217 //1) Conservative form of Kompaneet's equation and dealing with the diffusion term implicitly: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.13.798 and related: http://www.osti.gov/scitech/biblio/891567 . We should ensure the 4-force is consistent (numerically and analytically) with what's used in this equation for n.
9218 //2) Relativistic corrections: http://adsabs.harvard.edu/cgi-bin/bib_query?arXiv:1201.5606 and http://adsabs.harvard.edu/abs/2002nmgm.meet.2329S . It looks like nothing more difficult as far as actually using the expressions in place of non-relativistic version.
9219 //One semi-relevant application: http://www.aanda.org/articles/aa/full_html/2009/45/aa12061-09/aa12061-09.html
9220 #define KAPPA_FORCOMPT_RELCORREP(ep) ((1.0 + 3.683*(ep)+4.0*(ep)*(ep))/(1.0 + (ep))) // Sadowski et al. (2014) Eq 26 and 27.
9221 #define KAPPA_FORCOMPT_RELCORR(rhocode,Tcode) (KAPPA_FORCOMPT_RELCORREP(K_BOLTZ*(Tcode)*TEMPBAR/(MELE*CCCTRUE*CCCTRUE)))
9222 #define KAPPA_FORCOMPT_CODE(rhocode,Tcode) (0.2*(1.0+XFACT)*KAPPA_ES_FERMICORR(rhocode,Tcode)*KAPPA_FORCOMPT_RELCORR(rhocode,Tcode)/OPACITYBAR)
9223 
9224 
9225 // COMPTON ALTERNATIVES
9226 //1) 1201.5606v2.pdf eq. 4.8 with Teff=Te different.
9227 //2) 891567.pdf eq. 10.
9228 //3) art%3A10.1007%2FBF03035735.pdf (what we are approximating when not using full Kompaneets). How could solve.
9229 //4) compton.pdf
9230 
9231 
9232 
9234 #define KAPPA_ZETA(Tgcode,Trcode) ((TEMPMIN+Trcode)/(TEMPMIN+Tgcode))
9235 //#define KAPPA_FF_CODE(rhocode,Tgcode,Trcode) (4.0E22*(1.0+XFACT)*(1.0-ZFACT)*((rhocode)*RHOBAR)*prpow((Tgcode)*TEMPBAR,-0.5)*prpow((Trcode)*TEMPBAR,-3.0)*prlog(1.0+1.6*KAPPA_ZETA(Tgcode,Trcode))*(1.0+4.4E-10*(Tgcode*TEMPBAR))/OPACITYBAR) // ASSUMPTION: Thermal ele and no pairs. See Rybicki & Lightman Eq~5.25 and McKinney & Uzdensky (2012) . For Tr,Tg split, see Ramesh notes.
9236 #define KAPPA_FF_CODE(rhocode,Tgcode,Trcode) (4.0E22*(1.0+XFACT)*(1.0-ZFACT)*((rhocode)*RHOBAR)*prpow((Tgcode)*TEMPBAR,-0.5)*prpow((Trcode)*TEMPBAR,-3.0)*prlog(1.0+1.6*KAPPA_ZETA(Tgcode,Trcode))*(1.0+4.4E-10*(Tgcode*TEMPBAR))/OPACITYBAR) // ASSUMPTION: Thermal ele and no pairs. See Rybicki & Lightman Eq~5.25 and McKinney & Uzdensky (2012) . For Tr,Tg split, see Ramesh notes.
9237 
9239 // FREE-FREE STUFF
9240 // see freefree_opacity.nb, freefree_opacity_fitenergyopacity.nb, freefree_opacity_fitnumberopacity.nb
9241 // accounts for self-absorption and energy vs. number opacity behavior
9242 //#define KAPPA_FF_ZETAFF_LEN(xv,lenv) (1.0/((lenv) + (0.99366835740822140451*Power((xv),3.001486183352440767))/ Prlog(1. + (0.96029003648876359763*(xv))/(0.62006021009771899259 + 1.3708624629986290167*Power((lenv),0.3428937745657171798)))))
9243 
9244 //#define KAPPAN_FF_ZETAFF_LEN(xv,lenv) (1.0/((lenv) + ((0.44920722974573544008 + 3.1077547389879800477*Power((lenv),0.23000000000000001))*Power((xv),2.066660783319324679))/ Log(1. + (10. + 58.528227977634806223/Power((lenv),0.25))*(xv))))
9245 
9246 //#define KAPPA_FF_PREFACTOR_CODE(rhocode,Tecode) (1.2E24*RHOBAR*rhocode*prpow(Tecode*TEMPBAR,-3.5)/OPACITYBAR)
9247 //#define KAPPA_FF_ZETA(Tecode,Trcode) ((TEMPMIN+Trcode)/(TEMPMIN+Tecode))
9248 // pretau = Lengthcgs * (rhocgs * KAPPA_FF_PREFACTORcgs) = CODELENGTH * rhocode * KAPPA_FF_PREFACTOR_CODE
9249 //#define KAPPA_FF_PRETAU_CODE(length,rhocode,Tecode) (length*rhocode*KAPPA_FF_PREFACTOR_CODE)
9250 
9251 //#define KAPPA_FF_CODE(rhocode,Tgcode,Trcode,ffzeta,pretau) (KAPPA_FF_PREFACTOR_CODE(rhocode,Tecode)*(1.0+XFACT)*(1.0-ZFACT)*KAPPA_FF_ZETAFF_LEN(ffzeta,pretau))
9252 //#define KAPPAN_FF_CODE(rhocode,Tgcode,Trcode,ffzeta,pretau) (KAPPA_FF_PREFACTOR_CODE(rhocode,Tecode)*(1.0+XFACT)*(1.0-ZFACT)*KAPPAN_FF_ZETAFF_LEN(ffzeta,pretau))
9253 
9254 #define KAPPAN_FF_CODE(rhocode,Tgcode,Trcode) KAPPA_FF_CODE(rhocode,Tgcode,Trcode)
9255 
9257 // BOUND-FREE and other low energy stuff
9258 #define KAPPA_BF_CODE(rhocode,Tgcode,Trcode) (3.0E25*(ZFACT)*(1.0+XFACT+0.75*YFACT)*((rhocode)*RHOBAR)*prpow((Tgcode)*TEMPBAR,-0.5)*prpow((Trcode)*TEMPBAR,-3.0)*prlog(1.0+1.6*KAPPA_ZETA(Tgcode,Trcode))/OPACITYBAR) // ASSUMPTION: Number of electrons similar to for solar abundances for 1+X+(3/4)Y term. For Tr,Tg split, see Ramesh notes.
9259 #define KAPPA_CHIANTIBF_CODE(rhocode,Tgcode,Trcode) (4.0E34*((rhocode*RHOBAR))*(ZFACT/ZSOLAR)*YELE*prpow((Tgcode)*TEMPBAR,-1.7)*prpow((Trcode)*TEMPBAR,-3.0)/OPACITYBAR) // *XFACT literally from Fig 34.1 in Draine book, but for solar n_H\sim n_b\sim 1/cm^3 only
9260 #define KAPPA_HN_CODE(rhocode,Tgcode,Trcode) (1.1E-25*prpow(ZFACT,0.5)*prpow((rhocode)*RHOBAR,0.5)*prpow((Tgcode)*TEMPBAR,7.7)/OPACITYBAR) // other sources cite 2.5E-31 (Z/0.02)(rho)^(1/2)(T)^9
9261 #define KAPPA_MOL_CODE(rhocode,Tgcode,Trcode) (0.1*ZFACT/OPACITYBAR)
9262 // see opacities.nb
9263 #define KAPPA_GENFF_CODE(rhocode,Tgcode,Trcode) (1.0/(1.0/(KAPPA_MOL_CODE(rhocode,Tgcode,Trcode)+KAPPA_HN_CODE(rhocode,Tgcode,Trcode)) + 1.0/(KAPPA_CHIANTIBF_CODE(rhocode,Tgcode,Trcode)+KAPPA_BF_CODE(rhocode,Tgcode,Trcode)+KAPPA_FF_CODE(rhocode,Tgcode,Trcode)))) // for 1.3E3K \le T \le 1E9K or higher. Numerically better to have kappa bottom out at low T so no diverent opacity as T->0
9264 
9267 //#define KAPPA_SYN_PREFACTOR_CODE(Bcode,Tecode) ((3.618472945417517e62*(YELE))/((Bcode)*(BFIELDBAR)*(MUMEAN)*Power((Tecode),5)*Power((TEMPBAR),5)*OPACITYBAR))
9268 
9269 //#define SYNZETA(Bcode,Tecode,Trcode) ((4.92270742942408e22*(Trcode))/((Bcode)*(BFIELDBAR)*Power((Tecode),2)*(TEMPBAR)))
9270 
9271 
9273 //#define KAPPA_SYN_ULTRAREL_ZETA_LEN(xv,len) (1.0/((lenv) + 1.7593661568669912098*Power((xv),1.6666666666666667) + (1.4263215513887232471 + 0.3471650561326893869*Power((lenv),0.4523670248787104997))*Power((xv),2.111111111111111) + 0.23204675605082615204*Power((xv),2.5555555555555554) + (0.24443559757524314548 + 0.0088705237575917670473*Power((lenv),0.4271869510615121102))*Power((xv),3)))
9274 //#define KAPPA_SYN_ULTRAREL_CODE(Bcode,Tecode,Trcode,Trcode,synzeta,pretau) (KAPPA_SYN_PREFACTOR_CODE(Bcode,Tecode)*KAPPA_SYN_ULTRAREL_ZETA_LEN(synzeta,pretau))
9275 
9276 //#define KAPPA_SYN_T5E8K_ZETA_LEN(xv,len) (1.0/((lenv) + 0.00052202316156059881506*Power((xv),1.6666666666666667) + Power(0.14001850794282671986 + 0.11613125770482513044*Power((lenv),0.201800070232941442),3.3333333333333334814)* Power((xv),2.111111111111111) + 0.00019353241505903052391*Power((xv),2.5555555555555554) + (0.00027994589708735966894 + 0.000020878066604903371264*Power((lenv),0.1833333333333333481) + 0.00025453146537652838978*Power((lenv),0.3142857142857142794) + 0.000054874224183531537607*Power((lenv),0.5500000000000000444))* Power((xv),3)))
9277 //#define KAPPA_SYN_T5E8K_CODE(Bcode,Tecode,Trcode,Trcode,synzeta,pretau) (KAPPA_SYN_PREFACTOR_CODE(Bcode,Tecode)*KAPPA_SYN_T5E8K_ZETA_LEN(synzeta,pretau))
9278 
9279 //#define KAPPA_SYN_T2E9K_ZETA_LEN(Bcode,Trcode) (1/((lenv) + (0.28275620637731327697 + 1.1378561908017619967e-11*Power((lenv),0.060928927736612376))*Power((xv),1.6666666666666667) + (5.4268557992144783249 + 0.043977350144590657277*Power((lenv),0.5779288733465596239))*Power((xv),2.111111111111111) - 0.46233132906151664546*Power((xv),2.5555555555555554) + (0.19718535926423731898 + 0.0031122781491543971728*Power((lenv),0.4612049073380375952))*Power((xv),3)))
9280 //#define KAPPA_SYN_T2E9K_CODE(Bcode,Tecode,Trcode,Trcode,synzeta,pretau) (KAPPA_SYN_PREFACTOR_CODE(Bcode,Tecode)*KAPPA_SYN_T2E9K_ZETA_LEN(synzeta,pretau))
9281 
9283 //#define KAPPAN_SYN_ULTRAREL_ZETA_LEN(xv,len) (1.0/((lenv) + Power(0.86449798151937196078 + 0.24768973803440103021*Power((lenv),0.1626980491028581777),5.)* Power((xv),1.6666666666666667) - 1.*(0.37887607591106965651 + 0.22518459751734409835*Power((lenv),0.4285631946482474364))* Power((xv),1.8333333333333332593) + Power(0.73281421484706721348 + 0.21306057253116089667*Power((lenv),0.2099676374949942248), 3.3333333333333334814)*Power((xv),2)))
9284 //#define KAPPAN_SYN_ULTRAREL_CODE(Bcode,Tecode,Trcode,Trcode,synzeta,pretau) (KAPPA_SYN_PREFACTOR_CODE(Bcode,Tecode)*KAPPAN_SYN_ULTRAREL_ZETA_LEN(synzeta,pretau))
9285 
9286 //#define KAPPAN_SYN_T5E8K_ZETA_LEN(xv,len) (1.0/((lenv) + Power(0.163252088813858387 + 0.18257330106897970423*Power((lenv),0.1627658778956655172),5.)* Power((xv),1.6666666666666667) + Power(0.069760711506617445465 + 0.1433016264153323116*Power((lenv),0.2012366029583787241), 3.3333333333333334814)*Power((xv),2)))
9287 //#define KAPPAN_SYN_T5E8K_CODE(Bcode,Tecode,Trcode,Trcode,synzeta,pretau) (KAPPA_SYN_PREFACTOR_CODE(Bcode,Tecode)*KAPPAN_SYN_5E8K_ZETA_LEN(synzeta,pretau))
9288 
9289 //#define KAPPAN_SYN_T2E9K_ZETA_LEN(xv,len) (1.0/((lenv) + Power(0.59088832521110612461 + 0.4071501348167751444*Power((lenv),0.1132964803598243697),5.)* Power((xv),1.6666666666666667) + (0.20483642960172801044 + 0.5219984832002366737*Power((lenv),0.1499999999999999944) + 0.02830076259937870306*Power((lenv),0.5999999999999999778))*Power((xv),2)))
9290 //#define KAPPAN_SYN_T2E9K_CODE(Bcode,Tecode,Trcode,Trcode,synzeta,pretau) (KAPPA_SYN_PREFACTOR_CODE(Bcode,Tecode)*KAPPAN_SYN_2E9K_ZETA_LEN(synzeta,pretau))
9291 
9292 
9293 //****************************************//
9294 //****************************************//
9295 //****************************************//
9296 // Problem setup constants that only modifies things in init.c (not init.h)
9297 //****************************************//
9298 //****************************************//
9299 
9300 #if(WHICHPROBLEM==FLATNESS)
9301 
9302 #define KAPPA 0.
9303 #define KAPPAES 0.
9304 
9305 // assume KAPPA defines fraction of FF opacity
9306 #define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA*KAPPA_FF_CODE(rho,Tg,Tr))
9307 // assume KAPPAES defines fraction of ES opacity
9308 #define KAPPAESUSER(rho,T) (rho*KAPPAES*KAPPA_ES_BASIC_CODE(rho,T))
9309 
9310 
9311 #endif
9312 
9313 //****************************************//
9314 //****************************************//
9315 
9316 
9317 #if(WHICHPROBLEM==RADPULSE || WHICHPROBLEM==RADPULSEPLANAR || WHICHPROBLEM==RADPULSE3D)
9318 
9319 
9320 
9321 
9322 #if(WHICHPROBLEM==RADPULSEPLANAR)
9323 
9324 #define KAPPA 0.
9325 //#define KAPPAES (0.0)
9326 //#define KAPPAES (1E-7)
9327 //#define KAPPAES (1E-4*1.09713E-18*1E6)
9328 //#define KAPPAES (1E-4*1.09713E-18*1E3)
9329 //#define KAPPAES (1E-4*1.09713E-18*1E-0)
9330 //#define KAPPAES (1E-4*1.09713E-18*0.2)
9331 //#define KAPPAES (1E-4*1.09713E-18*1E-1)
9332 //#define KAPPAES (1E-4*1.09713E-18*1E-2)
9333 //#define KAPPAES (1E-4*1.09713E-18*1E-3)
9334 //#define KAPPAES (1E-4*1.09713E-18*1E-3*1E-10)
9335 
9336 // assume KAPPA defines fraction of FF opacity
9337 //#define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA*KAPPA_FF_CODE(rho,Tg,Tr))
9338 // assume KAPPAES defines fraction of ES opacity
9339 //#define KAPPAESUSER(rho,T) (rho*KAPPAES*KAPPA_ES_BASIC_CODE(rho,T))
9340 
9341 #define KAPPAES (1E3) // takes VERY long time with sub-cycling, but works.
9342 //#define KAPPAES (1E2) // goes with sub-cycling at "ok" rate for this test.
9343 //#define KAPPAES (10.0)
9344 //#define KAPPAES (1E-1)
9345 //#define KAPPAES (1.0)
9346 //#define KAPPAES (1E-10)
9347 
9348 #define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA)
9349 #define KAPPAESUSER(rho,T) (rho*KAPPAES)
9350 
9351 
9352 #else // PULSE and PULSE3D
9353 
9354 // KAPPAs are fraction of physical FF and ES opacities
9355 #define KAPPA 0.
9356 #define KAPPAES (SMALL)
9357 
9358 // assume KAPPA defines fraction of FF opacity
9359 #define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA*KAPPA_FF_CODE(rho,Tg,Tr))
9360 // assume KAPPAES defines fraction of ES opacity
9361 #define KAPPAESUSER(rho,T) (rho*KAPPAES*KAPPA_ES_BASIC_CODE(rho,T))
9362 
9363 
9364 #endif
9365 
9366 
9367 #endif
9368 
9369 
9370 //****************************************//
9371 //****************************************//
9372 
9373 
9374 #if(WHICHPROBLEM==RADBEAMFLAT)
9375 
9376 
9377 #define KAPPA 0.
9378 #define KAPPAES 0.
9379 
9380 // assume KAPPA defines fraction of FF opacity
9381 #define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA*KAPPA_FF_CODE(rho,Tg,Tr))
9382 // assume KAPPAES defines fraction of ES opacity
9383 #define KAPPAESUSER(rho,T) (rho*KAPPAES*KAPPA_ES_BASIC_CODE(rho,T))
9384 
9385 
9386 #endif
9387 
9388 //****************************************//
9389 //****************************************//
9390 
9391 
9392 #if(WHICHPROBLEM==RADTUBE)
9393 
9394 #define KAPPAESUSER(rho,T) (0.0)
9395 
9396 #if(NTUBE==1)
9397 #define KAPPAUSER(rho,B,Tg,Tr) (0.4*rho)
9398 #elif(NTUBE==2)
9399 #define KAPPAUSER(rho,B,Tg,Tr) (0.2*rho)
9400 #elif(NTUBE==3)
9401 #define KAPPAUSER(rho,B,Tg,Tr) (0.3*rho)
9402 #elif(NTUBE==31)
9403 #define KAPPAUSER(rho,B,Tg,Tr) (25*rho)
9404 #elif(NTUBE==4)
9405 #define KAPPAUSER(rho,B,Tg,Tr) (0.08*rho)
9406 #elif(NTUBE==41)
9407 #define KAPPAUSER(rho,B,Tg,Tr) (0.7*rho)
9408 #elif(NTUBE==5)
9409 #define KAPPAUSER(rho,B,Tg,Tr) (1000*rho)
9410 #endif
9411 
9412 
9413 #endif
9414 
9415 //****************************************//
9416 //****************************************//
9417 
9418 
9419 #if(WHICHPROBLEM==RADSHADOW || WHICHPROBLEM==RADDBLSHADOW)
9420 
9421 //#define KAPPAUSER(rho,B,Tg,Tr) (rho*1E2)
9422 #define KAPPAUSER(rho,B,Tg,Tr) (rho*1.0) // paper
9423 #define KAPPAESUSER(rho,T) (rho*0.0)
9424 
9425 
9426 #endif
9427 
9428 
9429 //****************************************//
9430 //****************************************//
9431 
9432 
9433 //****************************************//
9434 //****************************************//
9435 
9436 
9437 #if(WHICHPROBLEM==RADBEAM2D || WHICHPROBLEM==RADBEAM2DKS || WHICHPROBLEM==RADBEAM2DKSVERT)
9438 
9439 
9440 #define KAPPA 0.
9441 #define KAPPAES 0.
9442 
9443 // assume KAPPA defines fraction of FF opacity
9444 #define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA*KAPPA_FF_CODE(rho,Tg,Tr))
9445 // assume KAPPAES defines fraction of ES opacity
9446 #define KAPPAESUSER(rho,T) (rho*KAPPAES*KAPPA_ES_BASIC_CODE(rho,T))
9447 
9448 
9449 #endif
9450 
9451 
9452 #if(WHICHPROBLEM==ATMSTATIC)
9453 
9454 
9455 #define KAPPA 0.
9456 #define KAPPAES 0.
9457 
9458 // assume KAPPA defines fraction of FF opacity
9459 #define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA*KAPPA_FF_CODE(rho,Tg,Tr))
9460 // assume KAPPAES defines fraction of ES opacity
9461 #define KAPPAESUSER(rho,T) (rho*KAPPAES*KAPPA_ES_BASIC_CODE(rho,T))
9462 
9463 
9464 #endif
9465 
9466 
9467 #if(WHICHPROBLEM==RADATM)
9468 
9469 
9470 #define KAPPA 0.
9471 #define KAPPAES 1. // only scattering
9472 
9473 // assume KAPPA defines fraction of FF opacity
9474 #define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA*KAPPA_FF_CODE(rho,Tg,Tr))
9475 // assume KAPPAES defines fraction of ES opacity
9476 #define KAPPAESUSER(rho,T) (rho*KAPPAES*KAPPA_ES_BASIC_CODE(rho,T))
9477 
9478 
9479 #endif
9480 
9481 
9482 #if(WHICHPROBLEM==RADWALL)
9483 
9484 
9485 #define KAPPA 0.
9486 #define KAPPAES 0.
9487 
9488 // assume KAPPA defines fraction of FF opacity
9489 #define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA*KAPPA_FF_CODE(rho,Tg,Tr))
9490 // assume KAPPAES defines fraction of ES opacity
9491 #define KAPPAESUSER(rho,T) (rho*KAPPAES*KAPPA_ES_BASIC_CODE(rho,T))
9492 
9493 
9494 #endif
9495 
9496 
9497 
9498 
9499 #if(WHICHPROBLEM==RADWAVE)
9500 
9501 #define KAPPAUSER(rho,B,Tg,Tr) (rho*RADWAVE_KAPPA)
9502 #define KAPPAESUSER(rho,T) (rho*RADWAVE_KAPPAES)
9503 
9504 #endif
9505 
9506 
9507 
9508 #if(WHICHPROBLEM==KOMIPROBLEM)
9509 
9510 #define KAPPAUSER(rho,B,Tg,Tr) (0.)
9511 #define KAPPAESUSER(rho,T) (0.)
9512 
9513 #endif
9514 
9515 
9516 #if(WHICHPROBLEM==RADBONDI)
9517 
9518 
9519 #define KAPPA 1.0
9520 #define KAPPAES 1.0
9521 
9522 // assume KAPPA defines fraction of FF opacity
9523 #define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA*KAPPA_FF_CODE(rho,Tg,Tr))
9524 // assume KAPPAES defines fraction of ES opacity
9525 #define KAPPAESUSER(rho,T) (rho*KAPPAES*KAPPA_ES_BASIC_CODE(rho,T))
9526 
9527 
9528 #endif
9529 
9530 
9531 #if(WHICHPROBLEM==RADDOT)
9532 
9533 
9534 #define KAPPA 0.
9535 #define KAPPAES 0.
9536 
9537 // assume KAPPA defines fraction of FF opacity
9538 #define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA*KAPPA_FF_CODE(rho,Tg,Tr))
9539 // assume KAPPAES defines fraction of ES opacity
9540 #define KAPPAESUSER(rho,T) (rho*KAPPAES*KAPPA_ES_BASIC_CODE(rho,T))
9541 
9542 
9543 #endif
9544 
9545 #if(WHICHPROBLEM==RADNT || WHICHPROBLEM==RADFLATDISK)
9546 
9547 #define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA_ES_CODE(rho,Tg)/1E14*0.1) // wierd use of kappa_{es} in koral
9548 //#define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA_ES_CODE(rho,T)/1E14*0.0)
9549 #define KAPPAESUSER(rho,T) (0.0)
9550 
9551 #endif
9552 
9553 #if(WHICHPROBLEM==RADDONUT)
9554 // kappa can't be zero or else flux will be nan
9555 //#define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA_ES_CODE(rho,T)/1E14*1.0) // wierd use of kappa_{es} in koral
9556 //#define KAPPAESUSER(rho,T) (0.0)
9557 
9558 // KORALNOTE: Different than koral code test, but as if full problem.
9559 #define KAPPA 1.0
9560 #define KAPPAES 1.0
9561 
9562 // KORALTODO: Put a lower limit on T~1E4K so not overly wrongly opaque in spots where u_g->0 anomologously?
9563 // assume KAPPA defines fraction of FF opacity
9564 //#define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA*KAPPA_FF_CODE(rho,Tg+TEMPMIN))
9565 // accounts for low temperatures so non-divergent and more physical
9566 
9567 #define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA*(KAPPA_GENFF_CODE(SMALL+rho,Tg+TEMPMIN,Tr+TEMPMIN)))
9568 
9569 // TODOMARK: Compute FF number opacity
9570 #define KAPPANUSER(rho,B,Tg,Tr) (rho*KAPPA*(KAPPA_GENFF_CODE(SMALL+rho,Tg+TEMPMIN,Tr+TEMPMIN)))
9571 
9572 // assume KAPPAES defines fraction of ES opacity
9573 #define KAPPAESUSER(rho,T) (rho*KAPPAES*KAPPA_ES_CODE(rho,T))
9574 
9575 
9576 #endif
9577 
9578 #if(WHICHPROBLEM==RADCYLBEAM || WHICHPROBLEM==RADCYLBEAMCART)
9579 
9580 #define KAPPAUSER(rho,B,Tg,Tr) (rho*KAPPA_ES_BASIC_CODE(rho,Tg)/1E14*0.0) // note 0.0
9581 #define KAPPAESUSER(rho,T) (0.0)
9582 
9583 #endif
9584 
9585 #if(WHICHPROBLEM==RADCYLJET)
9586 
9587 #define KAPPAUSER(rho,B,Tg,Tr) (rho*(KAPPA_FF_CODE(SMALL+rho,Tg+TEMPMIN,Tr+TEMPMIN))) // SMALL
9588 #define KAPPAESUSER(rho,Tg) (rho*KAPPA_ES_BASIC_CODE(rho,Tg)/100.0)
9589 
9590 #endif
9591 
9592 
9593 #ifndef KAPPANUSER
9594 // in case haven't defined KAPPANUSER, just use energy opacity
9595 #define KAPPANUSER(rho,B,Tg,Tr) KAPPAUSER(rho,B,Tg,Tr)
9596 #endif
9597 
9598 
9599 
9600 // KAPPAUSER is optical depth per unit length per unit rest-mass energy density
9601 // calc_kappa_user and calc_kappan_user and calc_kappaes_user return optical depth per unit length.
9602 
9603 
9604 
9605 #define ISKAPPAEABS 0
9606 #define ISKAPPANABS 1
9607 #define ISKAPPAEEMIT 2
9608 #define ISKAPPANEMIT 3
9609 #define ISKAPPAES 4
9610 
9611 #define E 2.718281828459045
9612 
9613 // general fits from mean opacity paper
9614 static FTYPE kappa_func_fits(int which, FTYPE rho, FTYPE B, FTYPE Tg, FTYPE Tr, FTYPE varexpf)
9615 {
9616 
9617 // KORALTODO: Put a lower limit on T~1E4K so not overly wrongly opaque in spots where u_g->0 anomologously?
9618 // accounts for low temperatures so non-divergent and more physical
9619 
9620 
9621 
9622  if(WHICHFIT==ISFITORIG){
9623  if(which==ISKAPPAEABS || which==ISKAPPAEEMIT || which==ISKAPPANABS || which==ISKAPPANEMIT){
9624  // energy/number and absorb/emit treated using the same opacity
9625  return(rho*(KAPPA_GENFF_CODE(SMALL+rho,Tg+TEMPMIN,Tr+TEMPMIN)));
9626  }
9627  else if(which==ISKAPPAES){
9628  return(rho*KAPPA_ES_CODE(rho,Tg+TEMPMIN));
9629  }
9630  }
9631  else if(WHICHFIT==ISFITNEW){
9632  FTYPE kappa,kappaemit,kappan,kappanemit,kappaes;
9633  kappa_func_fits_all(SMALL+rho, B, Tg+TEMPMIN, Tr+TEMPMIN, varexpf, &kappa, &kappaemit, &kappan, &kappanemit, &kappaes);
9634  if(which==ISKAPPAEABS) return(kappa);
9635  else if(which==ISKAPPANABS) return(kappan);
9636  else if(which==ISKAPPAEEMIT) return(kappaemit);
9637  else if(which==ISKAPPANEMIT) return(kappanemit);
9638  else if(which==ISKAPPAES) return(kappaes);
9639  }
9640 
9641  return(0.0); // should never reach here
9642 }
9643 
9644 
9645 // general fits from mean opacity paper, giving back all opacities
9646 int kappa_func_fits_all(FTYPE rho, FTYPE B, FTYPE Tg, FTYPE Tr, FTYPE varexpf, FTYPE *kappa, FTYPE *kappaemit, FTYPE *kappan, FTYPE *kappanemit, FTYPE *kappaes)
9647 {
9648 
9649 // KORALTODO: Put a lower limit on T~1E4K so not overly wrongly opaque in spots where u_g->0 anomologously?
9650 // accounts for low temperatures so non-divergent and more physical
9651 
9652 #if(WHICHFIT==ISFITORIG)
9653  dualfprintf(fail_file,"Shouldn't be here\n");
9654  myexit(759275529);
9655 #endif
9656 
9657 
9658  FTYPE Te=Tg; // assume electrons and gas/ions/protons are same temperature
9659 
9660  // "real" here means cgs and Gaussian for B and Kelvin for temperature
9661  FTYPE rhoreal=rho*RHOBAR;
9662  FTYPE nereal=3.0110683499999995e23*rhoreal*(1.0 + XFACT);
9663  FTYPE Breal=B*BFIELDBAR;
9664  FTYPE Tereal=Te*TEMPBAR;
9665  FTYPE Tgreal=Tg*TEMPBAR;
9666  FTYPE Trreal=Tr*TEMPBAR;
9667  FTYPE xi = Trreal/Tereal;
9668 
9669  FTYPE thetae = Tereal/TEMPELE;
9670  FTYPE thetaesq=thetae*thetae;
9671  FTYPE thetaecubed=thetaesq*thetae;
9672 
9674  //
9675  // free-free for e-i
9676  // below in [cm^{-1}]
9677  //
9679 
9680  // ff prefactor
9681  FTYPE kappaffreal=1.2E24*pow(Tereal,-7.0/2.0)*pow(rhoreal,2.0)*(1.0+XFACT)*(1.0-ZFACT);
9682  // see nizisq.nb -- ensured continuous and differentiable at thetae=1
9683  // FTYPE Rei = (1.0+2.0*thetae+2.0*thetae*thetae)/(1.0+3.8*thetae+5.1*thetaesq+(8.0/M_PI)*thetaecubed)*log(1.0+3.42*thetae);
9684  FTYPE Reilow = 1.0 + 1.7644624334086192*Power(thetae,1.34);
9685  FTYPE Reihigh = 1.4019447514099515*Power(thetae,0.5)*(1.5 + Log(0.48 + 1.123*thetae));
9686  FTYPE Rei = (thetae>=1.0 ? Reihigh : Reilow);
9687 
9688  kappaffreal *= Rei;
9689 
9690 
9691  FTYPE kappanffreal=kappaffreal; // same prefactor
9692  FTYPE kappaemitffreal=kappaffreal; // same prefactor
9693  FTYPE kappanemitffreal=kappaffreal; // same prefactor
9694 
9695  // absorption
9696 
9697  // XRB Ledd
9698  FTYPE aa = 0.3564568198863157 - 0.19982886985727735*Power(1. - 1.*varexpf,0.5646962005387126) + 0.18848660385247928*Power(varexpf,13.940235230123522);
9699  FTYPE bb = 3.0641759806549147 + 0.25543546368991477*Power(1. - 1.*varexpf,0.3128511829901932) + 0.07219025685305303*Power(varexpf,1.3638629108321003);
9700  FTYPE cc = 5.99474041542733 - 1.4436038489430345*Power(1. - 1.*varexpf,0.12828451539717775) - 1.4051865724895833*Power(varexpf,3.0775239339815585);
9701  // FTYPE aa = 0.9315361341095171 - 0.6768085524145425*Power(1 - varexpf,0.7198306274197313) + 1.9002183262398797*Power(varexpf,37.829441097625605);
9702  // FTYPE bb = 3.1012402220434816 + 0.4612339875024576*Power(1 - varexpf,0.03596567451632021) - 0.02585416821144859*Power(varexpf,114.93181787377999);
9703  // FTYPE cc = 10.042113525722476 - 9.063716681172405*Power(1 - varexpf,2.4433708236615708e-9) - 0.6884461811691391*Power(varexpf,4.432030473409409);
9704 
9705  kappaffreal *= aa*pow(xi,-bb)*log(1.0+cc*xi);
9706 
9707  // List(4. - 2.06*Power(1. - 1.*varvarvar,1.) + 21.*Power(varvarvar,5.),3.1503757694129613 + 0.0008942404015805927*Power(1. - 1.*varvarvar,10.174621780103456) - 0.41243605382204196*Power(varvarvar,59.06672963853068),4.552732466653472e-7 + 2.3916452081662603*Power(1. - 1.*varvarvar,0.5522198226571239) + 5.27043093753271*Power(varvarvar,69.17066973904404))
9708 
9709 #if(EVOLVENRAD)
9710  // XRB Ledd
9711  FTYPE aan = 4. - 2.06*Power(1. - 1.*varexpf,1.) + 21.*Power(varexpf,5.);
9712  FTYPE bbn = 3.1503757694129613 + 0.0008942404015805927*Power(1. - 1.*varexpf,10.174621780103456) - 0.41243605382204196*Power(varexpf,59.06672963853068);
9713  FTYPE ccn = 4.552732466653472e-7 + 2.3916452081662603*Power(1. - 1.*varexpf,0.5522198226571239) + 5.27043093753271*Power(varexpf,69.17066973904404);
9714  // FTYPE aan=1.27*Power(E,1.842068074395237*Power(varexpf,2) + 1.151292546497023*Power(varexpf,4) + 2.8206667389177063*Power(varexpf,70.));
9715  // FTYPE bbn=3.0976213586221544 - 0.03091777713803534*Power(1 - varexpf,4.613930997481447) - 0.3099700393164486*Power(varexpf,162.16873912467364);
9716  // FTYPE ccn=0.00010386408308833163 + 5.934327626527228*Power(1 - varexpf,0.5880281963526498) + 62558.02370357485*Power(varexpf,1.3444132516555986e6);
9717 
9718  kappanffreal *= aan*pow(xi,-bbn)*log(1.0+ccn*xi);
9719 #endif
9720 
9721  // emission (just Planck varexpf=1 but using actual direct fit instead of fit over many varexpf)
9722 
9723  // XRB Ledd
9724  FTYPE aae = 0.5316463286647214;
9725  FTYPE bbe = 3.140128803410833;
9726  FTYPE cce = 4.522392868247536;
9727  // FTYPE aae=0.3759100641660466;
9728  // FTYPE bbe=3.0775444410210264;
9729  // FTYPE cce=9.354365388578165;
9730 
9731  kappaemitffreal *= aae*pow(xi,-bbe)*log(1.0+cce*xi);
9732 
9733 #if(EVOLVENRAD)
9734  // XRB Ledd
9735  FTYPE aane = 20.;
9736  FTYPE bbne = 2.6669560547974855;
9737  FTYPE ccne = 5.;
9738  // FTYPE aane=7.418554613651609;
9739  // FTYPE bbne=2.0551425132443293;
9740  // FTYPE ccne=62558.213216069445;
9741 
9742  kappanemitffreal *= aane*pow(xi,-bbne)*log(1.0+ccne*xi);
9743 #endif
9744 
9745  // TODO: Need to interpolate kappaffreal and kappanffreal towards
9746  // Planck when varexpf->1. Like when varexp=0.999 to 1.0, or
9747  // whever fits seem to become off.
9748 
9750  //
9751  // Add free-free for e-e
9752  // below in [cm^{-1}]
9753  //
9755  //FTYPE Ree = thetae*(0.851+2.0*thetae)/(1.0+3.8*thetae+5.1*thetaesq+(8.0/M_PI)*thetaecubed)*log(1.0+10.4*thetaesq);
9756  FTYPE Reelow =1.706666666666667*thetae*(1 + 1.1*thetae + Power(thetae,2) - 1.063803438337589*Power(thetae,2.5));
9757  FTYPE Reehigh = 2.489326395711546*Power(thetae,0.5)*(1.28 + Log(1.123*thetae));
9758  FTYPE Ree = (thetae>=1.0 ? Reehigh : Reelow);
9759 
9760  // just add-in factor by which free-free e-e adds-in (see nizisq.nb)
9761  FTYPE factorffee=0.5*(1.0+XFACT)*Ree/((1.0-ZFACT)*Rei);
9762  FTYPE kappaffeereal = kappaffreal*factorffee;
9763  FTYPE kappanffeereal = kappanffreal*factorffee;
9764  FTYPE kappaemitffeereal = kappaemitffreal*factorffee;
9765  FTYPE kappanemitffeereal = kappanemitffreal*factorffee;
9766 
9768  //
9769  // Add bound-free
9770  // below in [cm^{-1}]
9771  //
9773 
9774  // just add-in factor by which bound-free adds-in
9775  FTYPE factorbf=750.0*ZFACT*(1.0+XFACT+0.75*YFACT)/((1.0+XFACT)*(1.0-ZFACT));
9776  FTYPE kappabfreal = kappaffreal*factorbf;
9777  FTYPE kappanbfreal = kappanffreal*factorbf;
9778  FTYPE kappaemitbfreal = kappaemitffreal*factorbf;
9779  FTYPE kappanemitbfreal = kappanemitffreal*factorbf;
9780 
9782  //
9783  // Add Chianti
9784  // below in [cm^{-1}]
9785  // [TODO: Could use ff as prefactor to these as well for more uniform treatment as Trreal and Tereal become different.]
9786  // [TODO: Could use each ff prefactor for each kappa type (4 types) so opacities behave uniformly as Trreal and Tereal become different. But not clear if all scale the same with temperature. At least for Chianti it's reasonable.]
9787  // [TODO: integrate real low-temp opacities]
9788  //
9790 
9791  // FTYPE kappachiantirealbase = 30.0*1E33*pow(rhoreal,2.0)*(0.1+ZFACT/ZSOLAR)*XFACT*(1.0+XFACT)*pow(Tereal,-1.7)*pow(Trreal,-3.0);
9792  // just add-in factor by which chianti adds-in (see nizisq.nb)
9793  FTYPE factorchianti=2.50672E10*XFACT*(0.1+ZFACT/ZSOLAR)*pow(Tereal,-1.2)/(1.0-ZFACT);
9794  FTYPE kappachiantireal = kappaffreal*factorchianti;
9795  FTYPE kappanchiantireal = kappanffreal*factorchianti;
9796  FTYPE kappaemitchiantireal = kappaemitffreal*factorchianti;
9797  FTYPE kappanemitchiantireal = kappanemitffreal*factorchianti;
9798 
9800  //
9801  // Add H^-
9802  // below in [cm^{-1}]
9803  //
9805 
9806  // FTYPE kappahminusbase = 33.0*1E-25*pow(ZFACT,0.5)*pow(rhoreal,1.5)*pow(Tereal,7.7);
9807  // just add-in factor by which H^- adds-in (see nizisq.nb), but remove Rei
9808  FTYPE factorhm=2.75739E-48*pow(Tereal,11.2)*pow(ZFACT,0.5)/(pow(rhoreal,0.5)*(1.0+XFACT)*(1.0-ZFACT)*Rei);
9809  FTYPE kappahmreal = kappaffreal*factorhm;
9810  FTYPE kappanhmreal = kappanffreal*factorhm;
9811  FTYPE kappaemithmreal = kappaemitffreal*factorhm;
9812  FTYPE kappanemithmreal = kappanemitffreal*factorhm;
9813 
9815  //
9816  // Add Chianti that fits Opal
9817  // below in [cm^{-1}]
9818  //
9820 
9821  // just add-in factor by which chianti opal adds-in
9822  FTYPE factorchiantiopal=3E-13*pow(Tereal,1.6)*pow(rhoreal,-0.4);
9823  FTYPE kappachiantiopalreal = kappaffreal*factorchianti*factorchiantiopal;
9824  FTYPE kappanchiantiopalreal = kappanffreal*factorchianti*factorchiantiopal;
9825  FTYPE kappaemitchiantiopalreal = kappaemitffreal*factorchianti*factorchiantiopal;
9826  FTYPE kappanemitchiantiopalreal = kappanemitffreal*factorchianti*factorchiantiopal;
9827 
9829  //
9830  // Add H^- that fits Opal
9831  // below in [cm^{-1}]
9832  //
9834 
9835  // just add-in factor by which H^- opal adds-in
9836  FTYPE factorhmopal=1E4*pow(Tereal,-1.2);
9837  FTYPE kappahmopalreal = kappaffreal*factorhm*factorhmopal;
9838  FTYPE kappanhmopalreal = kappanffreal*factorhm*factorhmopal;
9839  FTYPE kappaemithmopalreal = kappaemitffreal*factorhm*factorhmopal;
9840  FTYPE kappanemithmopalreal = kappanemitffreal*factorhm*factorhmopal;
9841 
9843  //
9844  // Add molecular (without T dependence)
9845  // below in [cm^{-1}]
9846  //
9848 
9849  FTYPE kappamolreal = 3.0*ZFACT*rhoreal;
9850  FTYPE kappanmolreal = kappamolreal;
9851  FTYPE kappaemitmolreal = kappamolreal;
9852  FTYPE kappanemitmolreal = kappamolreal;
9853 
9855  //
9856  // Low-density interpolation
9857  // below in [cm^{-1}]
9858  //
9860  FTYPE kappalowdensityreal = 1.0/ ( 1.0/(kappamolreal + kappahmreal) + 1.0/(kappachiantireal + kappaffreal + kappaffeereal + kappabfreal) );
9861  FTYPE kappanlowdensityreal = 1.0/ ( 1.0/(kappanmolreal + kappanhmreal) + 1.0/(kappanchiantireal + kappanffreal + kappanffeereal + kappanbfreal) );
9862  FTYPE kappaemitlowdensityreal = 1.0/ ( 1.0/(kappaemitmolreal + kappaemithmreal) + 1.0/(kappaemitchiantireal + kappaemitffreal + kappaemitffeereal + kappaemitbfreal) );
9863  FTYPE kappanemitlowdensityreal = 1.0/ ( 1.0/(kappanemitmolreal + kappanemithmreal) + 1.0/(kappanemitchiantireal + kappanemitffreal + kappanemitffeereal + kappanemitbfreal) );
9864 
9866  //
9867  // full range density interpolation
9868  // below in [cm^{-1}]
9869  //
9871  FTYPE kappadensityreal = 1.0/ ( 1.0/(kappamolreal + kappahmopalreal) + 1.0/(kappachiantiopalreal) + 1.0/(kappachiantireal + kappaffreal + kappaffeereal + kappabfreal) );
9872  FTYPE kappandensityreal = 1.0/ ( 1.0/(kappanmolreal + kappanhmopalreal) + 1.0/(kappanchiantiopalreal) + 1.0/(kappanchiantireal + kappanffreal + kappanffeereal + kappanbfreal) );
9873  FTYPE kappaemitdensityreal = 1.0/ ( 1.0/(kappaemitmolreal + kappaemithmopalreal) + 1.0/(kappaemitchiantiopalreal) + 1.0/(kappaemitchiantireal + kappaemitffreal + kappaemitffeereal + kappaemitbfreal) );
9874  FTYPE kappanemitdensityreal = 1.0/ ( 1.0/(kappanemitmolreal + kappanemithmopalreal) + 1.0/(kappanemitchiantiopalreal) + 1.0/(kappanemitchiantireal + kappanemitffreal + kappanemitffeereal + kappanemitbfreal) );
9875 
9876 
9878  //
9879  // Cylco-Synchrotron
9880  // below in [cm^{-1}]
9881  //
9883 
9884  FTYPE nuM = 1.5*QCHARGE*Breal/(2.0*M_PI*MELE*CCCTRUE0)*thetaesq;
9885  FTYPE phi = (K_BOLTZ*Trreal)/(HPLANCK*nuM);
9886 
9887  // synch prefactor
9888  FTYPE kappasyreal=2.13E-11*nereal/Breal*pow(Tereal/1E10,-5.0);
9889 
9890  // low-temp factor
9891  FTYPE q0 = 1.0/ (1.0+pow(3.3*thetae,-5.0));
9892  FTYPE q1 = 1.0 + pow(3.3*thetae,-5.0);
9893  FTYPE qsyn = exp(log(q0) + ( 0.5 + (1.0/M_PI)*atan(3.0+log(phi)) )*(log(q1) - log(q0)) );
9894  kappasyreal *= qsyn;
9895 
9896  FTYPE kappansyreal=kappasyreal; // same prefactor
9897  FTYPE kappaemitsyreal=kappasyreal; // same prefactor
9898  FTYPE kappanemitsyreal=kappasyreal; // same prefactor
9899 
9900  // absorption
9901 
9902  // XRB Ledd
9903  FTYPE aas=0.001 - 0.00030000000000000003*Power(1. - 1.*varexpf,0.1) + 9.999*Power(varexpf,100);
9904  FTYPE bbs=0.34915123451867575 + 0.6639854085243424*Power(1. - 1.*varexpf,0.21551972527431834) + 0.6969780620639547*Power(varexpf,109.95164261078826);
9905  FTYPE ccs=3.5 - 0.5*Power(1. - 1.*varexpf,0.1) + 1.7999999999999998*Power(varexpf,10);
9906  FTYPE dds=80.*Power(2.718281828459045,1.611809565095832*Power(varexpf,2) + 1.701470224678968*Power(varexpf,19.123258595041275));
9907  FTYPE ees=2.9814806065542103 + 1.0252733145171038*Power(1. - 1.*varexpf,0.03215194092829952);
9908 
9909  kappasyreal *= 1.0/( 1.0/(aas*pow(phi,-bbs)*log(1.0+ccs*phi)) + 1.0/(dds*pow(phi,-ees)) );
9910 
9911 #if(EVOLVENRAD)
9912  // XRB Ledd
9913  FTYPE aans=0.0012 + 0.00005000000000000013*Power(1. - 1.*varexpf,0.05) - 0.0009099999999999999*Power(varexpf,7);
9914  FTYPE bbns=2.65 + 0.040000000000000036*Power(1. - 1.*varexpf,0.02) - 0.5499999999999998*Power(varexpf,30);
9915  FTYPE ccns=1.31 - 0.06000000000000005*Power(1. - 1.*varexpf,0.02) + 1.69*Power(varexpf,8);
9916  FTYPE ddns=Power(2.718281828459045,1.5686159179138452 + 1.151292546497023*Power(varexpf,2) + 1.151292546497023*Power(varexpf,4) + 2.9834230661458117*Power(varexpf,195.52338999877892));
9917  FTYPE eens=2.974042326402226 + 0.23373841071210855*Power(1. - 1.*varexpf,11.45996831663565) - 0.1009858740118661*Power(varexpf,201.41910724608374);
9918 
9919  kappansyreal *= 1.0/( 1.0/(aans*pow(phi,-bbns)*log(1.0+ccns*phi)) + 1.0/(ddns*pow(phi,-eens)) );
9920 #endif
9921 
9922  // emission (just Planck varexpf=1 but using actual direct fit instead of fit over many varexpf)
9923 
9924  // XRB Ledd
9925  FTYPE aaes=0.3188964065440192;
9926  FTYPE bbes=1.0768900596611533;
9927  FTYPE cces=0.0026066604097452917;
9928  FTYPE ddes=1.2627623470514082;
9929  FTYPE eees=2.987395175467846;
9930 
9931  kappaemitsyreal *= 1.0/( 1.0/(aaes*pow(phi,-bbes)*log(1.0+cces*phi)) + 1.0/(ddes*pow(phi,-eees)) );
9932 
9933 #if(EVOLVENRAD)
9934  // XRB Ledd
9935  FTYPE aanes=0.00029169186404094216;
9936  FTYPE bbnes=1.3218673308302118;
9937  FTYPE ccnes=1.1634612365295334;
9938  FTYPE ddnes=120.01337286010565;
9939  FTYPE eenes=2.3252442378326856;
9940 
9941  kappanemitsyreal *= 1.0/( 1.0/(aanes*pow(phi,-bbnes)*log(1.0+ccnes*phi)) + 1.0/(ddnes*pow(phi,-eenes)) );
9942 #endif
9943 
9945  //
9946  // DC
9947  // below in [cm^{-1}]
9948  //
9950 
9951  // FTYPE thetar = K_BOLTZ*Trreal/(MELE*CCCTRUE0*CCCTRUE0);
9952  FTYPE thetar = Trreal/TEMPELE;
9953 
9954  // dc prefactor
9955  FTYPE kappadcreal=7.36E-46*nereal*Trreal*Trreal*varexpf;
9956 
9957  // high-thetae factor
9958  FTYPE pdc = pow(1.0+thetae,-3.0);
9959  kappadcreal *= pdc;
9960 
9961  FTYPE kappandcreal=kappadcreal; // same prefactor
9962  FTYPE kappaemitdcreal=kappadcreal; // same prefactor
9963  FTYPE kappanemitdcreal=kappadcreal; // same prefactor
9964 
9965  // absorption
9966 
9967  // XRB Ledd
9968  FTYPE aadc=3.10482346702441e-8 + 4.162489998316388*Power(1. - 1.*varexpf,1.6896115787181434) + 6.702210386421933*Power(varexpf,0.941782674568028);
9969  FTYPE bbdc=0.04202835820213653 - 0.03337036710130055*Power(1. - 1.*varexpf,0.4693413976733784) - 0.002097439704449637*Power(varexpf,0.021742118711173992);
9970  FTYPE ccdc=3.8020287401719655 + 0.2009643395406986*Power(1. - 1.*varexpf,0.25767159028663056) - 0.18040020940821044*Power(varexpf,33.009903857413704);
9971  FTYPE dddc=0.11793169411613985 - 0.06262831233321572*Power(1. - 1.*varexpf,0.34961114748511685) + 0.016889330731907487*Power(varexpf,35.3723390749425);
9972 
9973  kappadcreal *= 1.0/( 1.0/aadc + 1.0/(bbdc*pow(thetar,-ccdc)) + 1.0/(dddc*pow(thetar,-ccdc/3.0)) );
9974 
9975 #if(EVOLVENRAD)
9976  // XRB Ledd
9977  FTYPE aandc=87.4586627372423 - 76.35909344479292*Power(1. - 1.*varexpf,0.13586634790456995) + 29.385274489218205*Power(varexpf,285.27285622653653);
9978  FTYPE bbndc=1.1604225625247175 - 1.1192436863350592*Power(1. - 1.*varexpf,0.1339258484550137) + 0.1955857773358507*Power(varexpf,18.1030753003189);
9979  FTYPE ccndc=3.9257505990037376 + 0.04266044504755229*Power(1. - 1.*varexpf,181.71820529516145) - 0.8002705331174753*Power(varexpf,21.623589714867247);
9980  FTYPE ddndc=2.8625119194776856 - 2.716652341873481*Power(1. - 1.*varexpf,0.1055549863784506) + 1.8741117433895793*Power(varexpf,309.10043138151474);
9981 
9982  kappandcreal *= 1.0/( 1.0/aandc + 1.0/(bbndc*pow(thetar,-ccndc)) + 1.0/(ddndc*pow(thetar,-ccndc/3.0)) );
9983 #endif
9984 
9985  // emission
9986 
9987  // XRB Ledd
9988  FTYPE aaedc=6.335417556116487 - 0.058933985431348646*Power(1. - 1.*varexpf,10.71634135441363) + 0.48765467682596686*Power(varexpf,1.7536859250821957);
9989  FTYPE bbedc=0.008747549563345837 + 0.014220493179111593*Power(1. - 1.*varexpf,0.36109285953630127) + 0.028227296616735897*Power(varexpf,1.556532326060757);
9990  FTYPE ccedc=3.7824716575816524 + 0.18426457897750437*Power(1. - 1.*varexpf,0.3661610552743282) - 0.1602799611377681*Power(varexpf,15.388944611235807);
9991  FTYPE ddedc=0.11936934944026895 - 0.025640260828114658*Power(1. - 1.*varexpf,0.39821885979760463) + 0.014988119208161788*Power(varexpf,26.289513268487767);
9992 
9993  kappaemitdcreal *= 1.0/( 1.0/aaedc + 1.0/(bbedc*pow(thetar,-ccedc)) + 1.0/(ddedc*pow(thetar,-ccedc/3.0)) );
9994 
9995 #if(EVOLVENRAD)
9996  // XRB Ledd
9997  FTYPE aanedc=197.97520843920014 - 94.77219201589888*Power(1. - 1.*varexpf,0.9245008022924389) - 81.66905945089535*Power(varexpf,1.010185534936844);
9998  FTYPE bbnedc=4.798508367755025e-11 + 1.0490039263100823*Power(1. - 1.*varexpf,0.24861894212727034) + 1.3060425347854707*Power(varexpf,1.1248777879043648);
9999  FTYPE ccnedc=3.4389272678970677 + 0.441710530722887*Power(1. - 1.*varexpf,0.3614676129393117) - 0.4179301735940477*Power(varexpf,14.840910987577235);
10000  FTYPE ddnedc=3.375250751641187 - 1.3820659504372945*Power(1. - 1.*varexpf,0.31579132247596853) + 1.3742704814428137*Power(varexpf,31.28257475560334);
10001 
10002  kappanemitdcreal *= 1.0/( 1.0/aanedc + 1.0/(bbnedc*pow(thetar,-ccnedc)) + 1.0/(ddnedc*pow(thetar,-ccnedc/3.0)) );
10003 #endif
10004 
10005  // Planck for interpolation to it when expf->1. dcpl same as edcpl
10006  FTYPE aadcpl=6.8308477566235427614037721740062696378080011960029;
10007  FTYPE bbdcpl=0.03737660642072567319753249796331010429215268940195;
10008  FTYPE ccdcpl=3.6263522982307391011876292625424350179419071859114;
10009  FTYPE dddcpl=0.13396823846337876662165099344371722644585939843797;
10010 
10011  // ndcpl same as nedcpl
10012  FTYPE aandcpl=116.24082637910697352499285024420803245395311274652;
10013  FTYPE bbndcpl=1.3436648321999717296470441601823998776061527255987;
10014  FTYPE ccndcpl=3.0309933617509186060061141161349078396542823722783;
10015  FTYPE ddndcpl=4.7183006844597535352187951503958715513547062753028;
10016 
10017  // But for DC varexp fits, varexp=1 matches to direct Planck fit very well, so no need to specially interpolate near expf=1.
10018 
10020  //
10021  // Get final absorption/emission opacities
10022  //
10024 
10025  FTYPE kappareal = kappadensityreal + kappasyreal + kappadcreal;
10026  FTYPE kappanreal = kappandensityreal + kappansyreal + kappandcreal;
10027  FTYPE kappaemitreal = kappaemitdensityreal + kappaemitsyreal + kappaemitdcreal;
10028  FTYPE kappanemitreal = kappanemitdensityreal + kappanemitsyreal + kappanemitdcreal;
10029 
10031  //
10032  // Electron Scattering
10033  //
10035  //#define KAPPA_ES_KNCORRF(f) (0.75*((-1.*(1. + 3.*(f)))/Power(1. + 2.*(f),2) + (0.5*Log(1. + 2.*(f)))/(f) + ((1. + (f))*((2. + 2.*(f))/(1. + 2.*(f)) - (1.*Log(1. + 2.*(f)))/(f)))/Power((f),2)))
10036  //#define KAPPA_ES_KNCORR(rhocode,Tcode) (KAPPA_ES_KNCORREP(K_BOLTZ*(Tcode)*TEMPBAR/(MELE*CCCTRUE*CCCTRUE)))
10037 
10038  // Buchler and Yueh 1976 (just Fermi part). Fewer electrons when near Fermi fluid limit.
10039  FTYPE kappa_es_fermicorr = (1.0/(1.0+2.7E11*prpow(rhoreal,2.0)/prpow(Tereal,2.0)));
10040 
10041  // Buchler and Yueh 1976 . Klein-Nishina for thermal electrons.
10042  FTYPE kappa_es_kncorr = (1.0/(1.0+prpow(Tereal/4.5E8,0.86)));
10043 
10045  // below in [cm^{-1}]
10046  FTYPE kappaesreal = 0.2*(1.0+XFACT)*(rhoreal)*kappa_es_fermicorr*kappa_es_kncorr;
10047 
10048 
10050  //
10051  // Return code value of opacity
10052  //
10054  static FTYPE overopacitybaralt=1.0/(OPACITYBAR*RHOBAR); // for those opacities in cm^{-1}
10055 
10056  // return results
10057  *kappa=kappareal*overopacitybaralt;
10058  *kappan=kappanreal*overopacitybaralt;
10059  *kappaemit=kappaemitreal*overopacitybaralt;
10060  *kappanemit=kappanemitreal*overopacitybaralt;
10061  *kappaes=kappaesreal*overopacitybaralt;
10062 
10063 
10064  // TODO: check Ree and Rei with Te for small Te
10065  // check all expressions. ensure rhoreal Tereal all right.
10066  // check Gcompt
10067  // run code
10068 
10069  return(0);
10070 }
10071 
10072 FTYPE Gcompt(FTYPE rho0, FTYPE Tgas, FTYPE Tradff, FTYPE Ruu)
10073 {
10074 
10075 // INELASTIC COMPTON TERM
10076 // see DOCOMPTON in physics.tools.rad.c:
10077 // in term2, doesn't change photon energy, so that in scattering-dominated atmospheres, photons move through unchanged by temperature of gas.
10078 // Eq A7 in http://adsabs.harvard.edu/abs/2012ApJ...752...18K used first in http://adsabs.harvard.edu/cgi-bin/bib_query?arXiv:0904.4123 as based upon a calculation in http://adsabs.harvard.edu/abs/2000thas.book.....P .
10079 // Also interesting for next steps:
10080 //1) Conservative form of Kompaneet's equation and dealing with the diffusion term implicitly: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.13.798 and related: http://www.osti.gov/scitech/biblio/891567 . We should ensure the 4-force is consistent (numerically and analytically) with what's used in this equation for n.
10081 //2) Relativistic corrections: http://adsabs.harvard.edu/cgi-bin/bib_query?arXiv:1201.5606 and http://adsabs.harvard.edu/abs/2002nmgm.meet.2329S . It looks like nothing more difficult as far as actually using the expressions in place of non-relativistic version.
10082 //One semi-relevant application: http://www.aanda.org/articles/aa/full_html/2009/45/aa12061-09/aa12061-09.html
10083 
10084  FTYPE Te=Tgas; // assumes Te=Tgas
10085  FTYPE Tereal = Te*TEMPBAR;
10086  FTYPE rhoreal=rho0*RHOBAR;
10087 
10088  FTYPE thetae = Tereal/TEMPELE;
10089 
10090  // Sadowski et al. (2014) Eq 26 and 27.
10091  FTYPE kappa_forcompt_relcorr = (1.0 + 3.683*thetae+4.0*thetae*thetae)/(1.0 + thetae);
10092 
10093  // Buchler and Yueh 1976 (just Fermi part). Fewer electrons when near Fermi fluid limit.
10094  FTYPE kappa_es_fermicorr = 1.0/(1.0+2.7E11*rhoreal/prpow(Tereal,2.0));
10095 
10096  FTYPE kappa_forcompt_code = 0.2*(1.0+XFACT)*kappa_es_fermicorr*kappa_forcompt_relcorr/OPACITYBAR;
10097 
10098  // FTYPE neleobar=1.0/MUMEAN; // to have neleobar*kappaes=kappaesperele*mb*rho/(MUMEAN*mb)
10099  // ASSUMPTION: Tion=Tele
10100  FTYPE preterm3 = -4.0*rho0*kappa_forcompt_code*(Tgas - Tradff)*(TEMPBAR/TEMPELE)*Ruu; // kappaes with its internal *rho already accounts for being number density of electrons involved, so no need to use MUMEAN again here.
10101 
10102  // f[pl] = ((uu[pl] - uu0[pl]) + (sign[pl] * localdt * Gdpl[pl]))*extrafactor[pl]; -> T^t_t[new] = T^t_t[old] - Gdpl[UU] -> dT^t_t = -Gdpl[UU] = +Gd[TT]
10103  // Ruu>0, so if Tgas>Trad, then preterm3<0. Then egas should drop.
10104  // We have dT^t_t = G_t = Gd_t = -Gdpl_t = preterm3 u_t > 0, so G_t>0 so T^t_t rises so -T^t_t drops so egas drops.
10105 
10106  return(preterm3);
10107 }
10108 
10109 
10110 
10111 // energy absorption
10112 FTYPE calc_kappa_user(FTYPE rho, FTYPE B, FTYPE Tg,FTYPE Tr,FTYPE varexpf, FTYPE x,FTYPE y,FTYPE z)
10113 {
10114  // if(WHICHPROBLEM==RADDONUT && nstep>100){
10115  // return(0.0);
10116  // }
10117  // else return(KAPPAUSER(rho,B,Tg,Tr));
10118 
10119 
10120 #if(WHICHPROBLEM==RADDONUT)
10121  return(kappa_func_fits(ISKAPPAEABS,rho,B,Tg,Tr,varexpf));
10122 #else
10123  return(KAPPAUSER(rho,B,Tg,Tr));
10124 #endif
10125 
10126 }
10127 
10128 // energy emission
10129  FTYPE calc_kappaemit_user(FTYPE rho, FTYPE B, FTYPE Tg,FTYPE Tr,FTYPE varexpf, FTYPE x,FTYPE y,FTYPE z)
10130 {
10131  // if(WHICHPROBLEM==RADDONUT && nstep>100){
10132  // return(0.0);
10133  // }
10134  // else return(KAPPAUSER(rho,B,Tg,Tr));
10135 
10136 
10137 #if(WHICHPROBLEM==RADDONUT)
10138  return(kappa_func_fits(ISKAPPAEEMIT,rho,B,Tg,Tr,varexpf));
10139 #else
10140  return(KAPPAUSER(rho,B,Tg,Tr));
10141 #endif
10142 
10143 }
10144 
10145 // number absorption
10146  FTYPE calc_kappan_user(FTYPE rho, FTYPE B, FTYPE Tg,FTYPE Tr,FTYPE varexpf, FTYPE x,FTYPE y,FTYPE z)
10147 {
10148  // if(WHICHPROBLEM==RADDONUT && nstep>100){
10149  // return(0.0);
10150  // }
10151  // else return(KAPPANUSER(rho,B,Tg,Tr));
10152 #if(WHICHPROBLEM==RADDONUT)
10153  return(kappa_func_fits(ISKAPPANABS,rho,B,Tg,Tr,varexpf));
10154 #else
10155  return(KAPPANUSER(rho,B,Tg,Tr));
10156 #endif
10157 
10158 }
10159 
10160 // number emission
10161  FTYPE calc_kappanemit_user(FTYPE rho, FTYPE B, FTYPE Tg,FTYPE Tr,FTYPE varexpf, FTYPE x,FTYPE y,FTYPE z)
10162 {
10163  // if(WHICHPROBLEM==RADDONUT && nstep>100){
10164  // return(0.0);
10165  // }
10166  // else return(KAPPANUSER(rho,B,Tg,Tr));
10167 #if(WHICHPROBLEM==RADDONUT)
10168  return(kappa_func_fits(ISKAPPANEMIT,rho,B,Tg,Tr,varexpf));
10169 #else
10170  return(KAPPANUSER(rho,B,Tg,Tr));
10171 #endif
10172 
10173 }
10174 
10175 //scattering
10176 FTYPE calc_kappaes_user(FTYPE rho, FTYPE T,FTYPE x,FTYPE y,FTYPE z)
10177 {
10178 
10179 #if(WHICHPROBLEM==RADDONUT)
10180  return(kappa_func_fits(ISKAPPAES,rho,0,T,T,1.0));
10181 #else
10182  return(KAPPAESUSER(rho,T));
10183 #endif
10184 
10185 }
10186 
10187 
10188 
10189 
10190 
10191 
10192 int coolfunc_user(FTYPE h_over_r, FTYPE *pr, struct of_geom *geom, struct of_state *q,FTYPE (*dUcomp)[NPR])
10193 {
10194  return(0); // nothing yet
10195 }
10196 
10197 
10198 
10199 #define JET6LIKEUSERCOORD 0
10200 #define UNIHALFUSERCOORD 1
10201 #define ORIGWALD 2
10202 
10203 #define WHICHUSERCOORD JET6LIKEUSERCOORD
10204 
10205 
10206 // for defcoord=JET6COORDS like USERCOORDS
10208 
10209 
10210 void set_coord_parms_nodeps_user(int defcoordlocal)
10211 {
10212  if(1){
10213  // see jet3coords_checknew.nb
10214 
10216  // RADIAL GRID SETUP
10218  npow=1.0; //don't change it, essentially equivalent to changing cpow2
10219 
10220  //radial hyperexponential grid
10221 
10222 
10223  cpow2=1.0; //exponent prefactor (the larger it is, the more hyperexponentiation is)
10224  // cpow3=0.01;
10225  cpow3=1.0;
10226  //radius at which hyperexponentiation kicks in
10227  // rbr = 1E3;
10228  if(DOWALDDEN){
10229  rbr = 5E7; // WALD 5E2->5E7
10230  //power exponent
10231  npow2=4.0; // WALD: 6.0->4.0
10232  }
10233  else{
10234  rbr = 5E2; // WALD 5E2->5E7
10235  //power exponent
10236  npow2=6.0; // WALD: 6.0->4.0
10237  }
10238 
10239 
10240 
10241  // must be same as in dxdxp()
10242  // GODMARK: Note njet here is overwritten by njet later, but could have been different values if setup variable names differently.
10243  if(0){ // first attempt
10244  r1jet=2.8;
10245  njet=0.3;
10246  r0jet=7.0;
10247  rsjet=21.0;
10248  Qjet=1.7;
10249  }
10250  else if(0){ // chosen to resolve disk then resolve jet
10251  r1jet=2.8;
10252  njet=0.3;
10253  r0jet=20.0;
10254  rsjet=80.0;
10255  Qjet=1.8;
10256  }
10257  else if(1){
10258  r1jet=2.8;
10259  njet=0.3;
10260  r0jet=15.0;
10261  rsjet=40.0;
10262  Qjet=1.3; // chosen to help keep jet resolved even within disk region
10263  }
10264 
10265  // for switches from normal theta to ramesh theta
10266  rs=40.0; // shift
10267  r0=20.0; // divisor
10268 
10269  // for theta1
10270  // hslope=0.3 ; // resolve inner-radial region near equator
10271  r0jet3=20.0; // divisor
10272  rsjet3=0.0; // subtractor
10273 
10274  // for theta2
10275  h0=0.3; // inner-radial "hslope" for theta2
10276  //h0=0.1; // inner-radial "hslope" for theta2 // for thinner disks, change this.
10277  // GODMARK: Note that this overwrites above njet!
10278  // power \theta_j \propto r^{-njet}
10279  if(DOWALDDEN==1){
10280  njet=1.0;
10281  }
10282  else if(DOWALDDEN==2) njet=0.0;
10283 
10284 
10285  // see fix_3dpoledtissue.nb
10286 #if(0)
10287  ntheta=21.0;
10288  htheta=0.15;
10289  rsjet2=5.0;
10290  r0jet2=2.0;
10291 #else
10292  ntheta=5.0;
10293  htheta=0.15;
10294  rsjet2=5.0;
10295  r0jet2=2.0;
10296 #endif
10297 
10298  }
10299 
10300 }
10301 
10302 
10303 void set_coord_parms_deps_user(int defcoordlocal)
10304 {
10305  if(1){
10306  x1br = log( rbr - R0 ) / npow; //the corresponding X[1] value
10307  }
10308 
10309 }
10310 
10311 void write_coord_parms_user(int defcoordlocal, FILE *out)
10312 {
10313  if(1){
10314  fprintf(out,"%21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g %21.15g\n",npow,r1jet,njet,r0jet,rsjet,Qjet,ntheta,htheta,rsjet2,r0jet2,rsjet3,r0jet3,rs,r0,npow2,cpow2,rbr,x1br,cpow3);
10315  }
10316 
10317 }
10318 void read_coord_parms_user(int defcoordlocal, FILE *in)
10319 {
10320 
10321  if(1){
10322  fscanf(in,HEADER19IN,&npow,&r1jet,&njet,&r0jet,&rsjet,&Qjet,&ntheta,&htheta,&rsjet2,&r0jet2,&rsjet3,&r0jet3,&rs,&r0,&npow2,&cpow2,&rbr,&x1br,&cpow3);
10323 
10324  }
10325 }
10326 
10327 void read_coord_parms_mpi_user(int defcoordlocal)
10328 {
10329  if(1){
10330 #if(USEMPI)
10331  MPI_Bcast(&npow, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10332  MPI_Bcast(&r1jet, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10333  MPI_Bcast(&njet, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10334  MPI_Bcast(&r0jet, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10335  MPI_Bcast(&rsjet, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10336  MPI_Bcast(&Qjet, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10337  MPI_Bcast(&ntheta, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10338  MPI_Bcast(&htheta, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10339  MPI_Bcast(&rsjet2, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10340  MPI_Bcast(&r0jet2, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10341  MPI_Bcast(&rsjet3, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10342  MPI_Bcast(&r0jet3, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10343  MPI_Bcast(&rs, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10344  MPI_Bcast(&r0, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10345  MPI_Bcast(&npow2, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10346  MPI_Bcast(&cpow2, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10347  MPI_Bcast(&rbr, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10348  MPI_Bcast(&x1br, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10349  MPI_Bcast(&cpow3, 1, MPI_FTYPE, MPIid[0], MPI_COMM_GRMHD);
10350 #endif
10351  }
10352 
10353 }
10354 
10355 
10357 {
10358  extern FTYPE mysin(FTYPE th);
10359 
10360  if(1){
10361 
10362 #if(0) // no change in exponentiation
10363  // JET3COORDS-like radial grid
10364  V[1] = R0+exp(pow(X[1],npow)) ;
10365 #elif(WHICHUSERCOORD==UNIHALFUSERCOORD)
10366 
10367  Rout=2000.0;
10368  theexp = npow*X[1];
10369  npow=1.0;
10370  FTYPE gconst1=1.0;
10371  FTYPE gconst2=gconst1*.000001;
10372  V[1] = R0 + gconst1*X[1] + gconst2*exp(theexp);
10373 
10374 
10375 #elif(WHICHUSERCOORD==JET6LIKEUSERCOORD)
10376 
10377 
10378 #define cr(x) (exp(-1.0/(x)))
10379 #define tr(x) (cr(x)/(cr(x) + cr(1.0-(x))))
10380 #define trans(x,L,R) ((x)<=(L) ? 0.0 : ((x)>=(R) ? 1.0 : tr(((x)-(L))/((R)-(L)))) )
10381 #define transR(x,center,width) ( 0.5*(1.0-tanh(+((x)-(center))/(width))))
10382 #define transL(x,center,width) ( 0.5*(1.0-tanh(-((x)-(center))/(width))))
10383 #define transM(x,center,width) ( exp(-pow(((x)-(center))/((width)*0.5),2.0) ) )
10384 
10385 #define plateau(x,L,R,W) (trans(x,(L)-0.5*(W),(L)+0.5*(W))*(1.0-trans(x,(R)-0.5*(W),(R)+0.5*(W))))
10386 
10387 
10388  if(1){
10389  FTYPE theexp = npow*X[1];
10390  if( X[1] > x1br ) {
10391  theexp += cpow2 * pow(X[1]-x1br,npow2);
10392  }
10393  V[1] = R0+exp(theexp);
10394  }
10395  else{
10396 
10397 #define line1r(x,w) (Rout)
10398 #define line2r(x,w) (Routeq)
10399 #define line3r(x,w) (Rout))
10400  //#define wparsam(x,r) (h0 + pow( ((r)-rsjet3)/r0jet3 , -njet))
10401  //#define wparsam(x,r) (h0 + pow( ((r)-0.0)/4.2 , -njet))
10402  //#define wparsam(x,r) (h0 + pow(0.15 + ((r)-0.0)/10.0 , -njet))
10403 #define thetajon(x,w,xp1,xp2) (line1r(x,w)*(1.0-trans(x,xp1,xp2)) + line2r(x,w)*trans(x,xp1,xp2))
10404 
10405  //startx[1] = pow(log(Rin-R0),1.0/npow);
10406  //dx[1] = (pow(log(Rout-R0),1.0/npow)-pow(log(Rin-R0),1.0/npow)) / totalsize[1];
10407  //startx[1] = log(Rin-R0)/npow;
10408 
10409  //
10410  FTYPE mysx1=log(Rin-R0)/npow;
10411 
10412 #define lineeqr(x,w) (R0 + exp(npow*(X[1]-mysx1)*0.70 + npow*mysx1 ) )
10413 #define linepoler(x,w) (R0 + exp(npow*X[1]))
10414 #define thetaLr(x,wp,weq,xp1,xp2) ( linepoler(x,wp)*(1.0-trans(x,xp1,xp2)) + lineeqr(x,weq)*trans(x,xp1,xp2) )
10415 #define thetajon2(x,wp,weq,xp1,xp2) ( x<0.5 ? thetaLr(x,wp,weq,xp1,xp2) : thetaLr(1.0-x,wp,weq,xp1,xp2) )
10416 
10417  FTYPE Routvsx2,R0vsx2;
10418  FTYPE Routeq = 100.0;
10419 
10420  if(1){
10421 
10422  FTYPE xpole=0.25;
10423  FTYPE eqslope=0.5;
10424  FTYPE xeq=0.5;
10425 
10426  V[1] = thetajon2(X[2],0,0,xpole,xeq);
10427 
10428  }
10429 
10430  if(0){
10431  // FTYPE poleslope=wparsam(X[2],V[1]);
10432  FTYPE xpole=0.25;
10433  // FTYPE eqslope=0.1;
10434  FTYPE eqslope=0.5;
10435  FTYPE xeq=0.5;
10436  Routvsx2 = thetajon2(X[2], 0.0, 0.0, xpole, xeq);
10437 
10438  FTYPE R0eq=0.0;
10439 
10440  R0vsx2 = R0eq + R0*((Routvsx2-Routeq)/(Rout-Routeq));
10441  }
10442 
10443 
10444  if(0){
10445  //V[1] = R0+exp(npow*X[1]);
10446  // go startx[1] to totalsize[1]*dx[1]+startx[1] at pole and equator and everywhere.
10447  // But go to reduced value of V[1] near equator related to Rout as will be used at pole via setting of X[1]'s first and last values.
10448  //FTYPE AA = 1.0 - Routeq/Rout;
10449  // V[1] = Rin + (V[1]-Rin)*(1.0 - AA*sin(X[2]*M_PI));
10450  // V[1] *= (1.0 - AA*sin(X[2]*M_PI));
10451 
10452  Routvsx2 = thetajon(X[2],0.0,0.25,0.75);
10453 
10454  R0vsx2=R0;
10455 
10456  }
10457 
10458  // V[1] = R0 + exp(npow*( (X[1]-startx[1])*Routvsx2/Rout+startx[1]) );
10459 
10460  // V[1] = R0 + exp(npow*X[1]);
10461 
10462  // V[1] = R0 + (V[1]-R0)*Routvsx2/Rout + (1.0-Routvsx2/Rout)*(0.38+0.018);
10463 
10464 
10465 
10466 
10467  if(0){
10468  V[1] = R0+exp(npow*X[1]);
10469  // go startx[1] to totalsize[1]*dx[1]+startx[1] at pole and equator and everywhere.
10470  // But go to reduced value of V[1] near equator related to Rout as will be used at pole via setting of X[1]'s first and last values.
10471  // FTYPE Routeq = 100.0;
10472  FTYPE AA = 1.0 - Routeq/Rout;
10473  V[1] = Rin + (V[1]-Rin)*(1.0 - AA*sin(X[2]*M_PI));
10474  }
10475  }
10476 
10477 
10478  // FTYPE npowtrue,npowlarger=10.0;
10479  // FTYPE npowrs=1E3;
10480  // FTYPE npowr0=2E2;
10481  // npowtrue = npow + (npowlarger-npow)*(0.5+1.0/M_PI*atan((V[1]-npowrs)/npowr0));
10482  // V[1] = R0+exp(pow(X[1],npowtrue)) ;
10483 #elif(0)
10484  // avoid jump in grid at rbr
10485  // determine switches
10486  FTYPE r0rbr=rbr/2.0;
10487  FTYPE switch0 = 0.5+1.0/M_PI*atan((V[1]-rbr)/r0rbr); // 1 for outer r
10488 
10489  FTYPE V1 = R0+exp(npow*X[1]);
10490  FTYPE V2 = R0+exp(npow*X[1] + cpow2 * pow(cpow3*(X[1]-x1br*1.0),npow2));
10491 
10492  V[1] = V1*(1.0-switch0) + V2*switch0;
10493 
10494 #endif
10495 
10496 
10497 
10498 
10499 
10500 
10501 
10502 
10503  FTYPE theta1,theta2,arctan2;
10504 
10505 
10506 #if(0)
10507  // JET3COORDS-based:
10508  FTYPE myhslope=2.0-Qjet*pow(V[1]/r1jet,-njet*(0.5+1.0/M_PI*atan(V[1]/r0jet-rsjet/r0jet)));
10509  theta1 = M_PI * X[2] + ((1. - myhslope) * 0.5) * mysin(2. * M_PI * X[2]);
10510 #else
10511  // RAMESH BASED
10512  // myhslope here is h2 in MCAF paper
10513  // // h0 here is h3 in MCAF paper
10514  //FTYPE njetvsr;
10515  //if(V[1]<rbr) njetvsr=njet;
10516  // else njetvsr=njet/(V[1])*rbr;
10517  //else njetvsr=
10518  //njetvsr=njet;
10519 
10520  FTYPE localrbr=Rout; //500.0; // rbr;
10521  // FTYPE localrbr=rbr;
10522  FTYPE localrbrr0=100.0; //MAX(100.0,localrbr/2.0);
10523 
10524  FTYPE switch0 = 0.5+1.0/M_PI*atan((V[1]-localrbr)/localrbrr0); // large r
10525  FTYPE switch2 = 1.0-switch0; // small r
10526 
10527  // switch0=0.0; switch2=1.0;
10528 
10529  FTYPE myhslope1=h0 + pow( (V[1]-rsjet3)/r0jet3 , njet);
10530  FTYPE myhslope2=h0 + pow( (localrbr-rsjet3)/r0jet3 , njet);
10531  FTYPE myhslope = myhslope1*switch2 + myhslope2*switch0;
10532 
10533  // myhslope = pow(pow(myhslope1,-2.0) + pow(myhslope2,-2.0),-0.5);
10534 
10535  myhslope=myhslope1;
10536 
10537  // determine theta2
10538  FTYPE myx2;
10539  if(X[2]>1.0) myx2=2.0-X[2];
10540  else if(X[2]<0.0) myx2=-X[2];
10541  else myx2=X[2];
10542 
10543  FTYPE th2 = 0.5*M_PI*(1.0 + atan(myhslope*(myx2-0.5))/atan(myhslope*0.5));
10544 
10545  if(X[2]>1.0) th2=2.0*M_PI-th2;
10546  else if(X[2]<0.0) th2=-th2;
10547 
10548  // determine theta0
10549  // JET3COORDS-based:
10550  myhslope1=2.0-Qjet*pow(V[1]/r1jet,-njet*(0.5+1.0/M_PI*atan(V[1]/r0jet-rsjet/r0jet)));
10551  myhslope2=2.0-Qjet*pow(localrbr/r1jet,-njet*(0.5+1.0/M_PI*atan(localrbr/r0jet-rsjet/r0jet)));
10552  myhslope = myhslope1*switch2 + myhslope2*switch0;
10553  // myhslope here is h0 in MCAF paper
10554  FTYPE th0 = M_PI * X[2] + ((1. - myhslope) * 0.5) * mysin(2. * M_PI * X[2]);
10555 
10556 
10557  // determine switches (only function of radius and not x2 or theta)
10558  switch0 = 0.5+1.0/M_PI*atan((V[1]-rs)/r0); // switch in .nb file // switch0->1 as r->infinity
10559  switch2 = 1.0-switch0; // for inner radial region
10560 
10561  // this works because all functions are monotonic, so final result is monotonic. Also, th(x2=1)=Pi and th(x2=0)=0 as required
10562  theta1 = th0*switch2 + th2*switch0; // th0 is activated for small V[1] and th2 is activated at large radii. Notice that sum of switch2+switch0=1 so normalization correct.
10563  // theta1=th0;
10564  theta1=th2;
10565 
10566 #endif
10567 
10568  if(0){
10569  // fix_3dpoledtissue.nb based:
10570  theta2 = M_PI*0.5*(htheta*(2.0*X[2]-1.0)+(1.0-htheta)*pow(2.0*X[2]-1.0,ntheta)+1.0);
10571 
10572  // generate interpolation factor
10573  arctan2 = 0.5 + 1.0/M_PI*(atan( (V[1]-rsjet2)/r0jet2) );
10574 
10575  // now interpolate between them
10576  V[2] = theta2 + arctan2*(theta1-theta2);
10577  }
10578 
10579 
10580  //V[2] = theta1;
10581 
10582  if(1){
10583  FTYPE fraceq=0.3;
10584  FTYPE fracpole=(1.0-fraceq)/2.0;
10585  FTYPE x2p1=0.0+fracpole;
10586  FTYPE x2p2=1.0-fracpole;
10587  FTYPE swide=0.04; //1E-1;
10588 
10589  // s(x) = 0.5 + 0.5 tanh((x-a)/b)
10590 
10591  // FTYPE switchh0 = 0.5+1.0/M_PI*atan((X[2]-x2p1)/swide);
10592  // FTYPE switchh2 = 0.5+1.0/M_PI*atan((X[2]-x2p2)/swide);
10593 
10594  FTYPE switchh0 = 0.5+0.5*tanh((X[2]-x2p1)/swide);
10595  FTYPE switchh2 = 0.5+0.5*tanh((X[2]-x2p2)/swide);
10596 
10597  FTYPE eqh=0.1;
10598  FTYPE theq = M_PI * X[2] + ((1. - eqh) * 0.5) * mysin(2. * M_PI * X[2]);
10599 
10600  FTYPE thup=switchh0*theq + (1.0-switchh0)*theta1;
10601 
10602  V[2]=switchh2*theta1 + (1.0-switchh2)*thup;
10603 
10604  }
10605  else{
10606  V[2]=theta1;
10607  }
10608 
10609  if(0){ // Sam Gralla
10610  FTYPE transwidth=0.06; //1E-1;
10611  FTYPE xcent=0.5; // fixed
10612  FTYPE transR=0.5*(1.0-tanh(+(X[2] - xcent)/transwidth));
10613  FTYPE transL=0.5*(1.0-tanh(-(X[2] - xcent)/transwidth));
10614 
10615  h0=0.0;
10616  // FTYPE wpar=h0 + pow( (V[1]-rsjet3)/r0jet3 , -njet);
10617  FTYPE wpar=pow( (V[1])/1 , -njet);
10618 
10619  FTYPE line1 = wpar*X[2];
10620  FTYPE line2 = M_PI + wpar*(X[2]-1.0);
10621 
10622  V[2] = line1*transR + line2*transL;
10623 
10624  // dualfprintf(fail_file,"X[2]=%g V[2]=%g\n",X[2],V[2]);
10625 
10626  }
10627 
10628  if(1){ // Sam Gralla 2
10629 
10630  h0=0.0;
10631 
10632 #define line1(x,w) ((x)*(w))
10633 #define line2(x,w) ((x)*(w)+M_PI-(w))
10634 #define line3(x,w) ((x)*(w))
10635  //#define wparsam(x,r) (h0 + pow( ((r)-rsjet3)/r0jet3 , -njet))
10636  //#define wparsam(x,r) (h0 + pow( ((r)-0.0)/4.2 , -njet))
10637 #define wparsam(x,r) (h0 + pow(0.15 + ((r)-0.0)/10.0 , -njet))
10638  //#define wparsam(x,r) (h0 + pow(0.19 + ((r)-0.0)/20.0 , -njet)) // widerjet, good for MHD or tilt=90deg
10639 #define thetasam(x,r,w,xp1,xp2) (line1(x,w)*(1.0-trans(x,xp1,xp2)) + line2(x,w)*trans(x,xp1,xp2))
10640 
10641  V[2] = thetasam(X[2],V[1],wparsam(X[2],V[1]),0.25,0.75);
10642  // V[2] = thetasam(X[2],V[1],1.0/V[1],0.2,0.8);
10643 
10644  // dualfprintf(fail_file,"tr=%g %g %g %g\n",tr(0.5),line1(0.5,wparsam(0.5,V[1])),trans(X[2],0.2,0.8),line2(0.5,wparsam(0.5,V[1])));
10645 
10646 
10647  }
10648 
10649  if(0){ // Sam Gralla 3
10650 
10651  h0=0.0;
10652 
10653 #define lineeq(x,w) ((x)*(w)+(0.5*M_PI)-(0.5*w))
10654 #define linepole(x,w) (line1(x,w))
10655 #define thetaL(x,wp,weq,xp1,xp2) ( linepole(x,wp)*(1.0-trans(x,xp1,xp2)) + lineeq(x,weq)*trans(x,xp1,xp2) )
10656 #define thetasam2(x,wp,weq,xp1,xp2) ( x<0.5 ? thetaL(x,wp,weq,xp1,xp2) : -thetaL(1.0-x,wp,weq,xp1,xp2)+M_PI )
10657 
10658  FTYPE poleslope=wparsam(X[2],V[1]);
10659  FTYPE xpole=0.25;
10660  // FTYPE eqslope=0.1;
10661  FTYPE eqslope=0.5;
10662  FTYPE xeq=0.5;
10663  V[2] = thetasam2(X[2], poleslope, eqslope, xpole, xeq);
10664 
10665 
10666  }
10667 
10668 
10669 
10670 
10671 
10672  // default is uniform \phi grid
10673  V[3]=2.0*M_PI*X[3];
10674  }
10675 
10676 
10677  if(WHICHUSERCOORD==ORIGWALD){
10678  V[1] = R0+exp(X[1]) ;
10679  V[2] = M_PI * X[2] + ((1. - hslope) / 2.) * sin(2. * M_PI * X[2]);
10680  V[3]=2.0*M_PI*X[3];
10681  }
10682 
10683 
10684 }
10685 
10686 
10688 {
10689  dualfprintf(fail_file,"Should not be computing USERCOORDS analytically\n");
10690  myexit(34698346);
10691  dxdxp[3][3] = 2.0*M_PI;
10692 
10693 }
10694 void set_points_user(void)
10695 {
10696  if(WHICHUSERCOORD==ORIGWALD){
10697  startx[1] = log(Rin-R0);
10698  startx[2] = 0.;
10699  dx[1] = log((Rout-R0)/(Rin-R0)) / totalsize[1];
10700  dx[2] = 1. / totalsize[2];
10701  dx[3] = 2.0*M_PI;
10702  }
10703 
10705  startx[1] = 0.3999985081775278946780799743777598329673;
10706  startx[2] = 0.;
10707  startx[3] = 0.;
10708 
10709  FTYPE endx1=21.40529883372801383045167738115556702610;
10710  dx[1] = (endx1-startx[1]) / totalsize[1];
10711  dx[2] = 1. / totalsize[2];
10712  dx[3] = 1.0/totalsize[3];
10713  }
10714 
10716  startx[1] = pow(log(Rin-R0),1.0/npow);
10717  startx[2] = 0.;
10718  startx[3] = 0.;
10719  dx[1] = (pow(log(Rout-R0),1.0/npow)-pow(log(Rin-R0),1.0/npow)) / totalsize[1];
10720  if(DOWALDDEN==1) dx[2] = 1. / totalsize[2];
10721  else if(DOWALDDEN==2) dx[2] = 0.5 / totalsize[2];
10722  dx[3] = 1.0/totalsize[3];
10723 
10724 #if(1)
10725  startx[1] = log(Rin-R0)/npow;
10726 
10727  trifprintf( "ITERATIVE dx1: Rout=%21.15g R0=%21.15g npow=%21.15g cpow2=%21.15g cpow3=%21.15g npow2=%21.15g x1br=%21.15g rbr=%21.15g\n",Rout,R0,npow,cpow2,cpow3,npow2,x1br,rbr);
10728 
10729  FTYPE x1max0, x1max,dxmax;
10730  int iter;
10731  const FTYPE RELACC = NUMEPSILON*100.0;
10732  const int ITERMAX = 100;
10733 
10734 
10735  if( Rout < rbr ) {
10736  x1max = log(Rout-R0)/npow;
10737  }
10738  else {
10739  x1max0 = 1.1*x1br;
10740  x1max = 1.2*x1br;
10741 
10742  //find the root via iterations
10743  for( iter = 0; iter < ITERMAX; iter++ ) {
10744 
10745  // trifprintf( "iter=%d x1max=%21.15g x2max0=%21.15g\n",iter,x1max0,x1max);
10746 
10747  if( fabs((x1max - x1max0)/x1max) < RELACC ) {
10748  break;
10749  }
10750  x1max0 = x1max;
10751 
10752  if(1){
10753  dxmax= (pow( (log(Rout-R0) - npow*x1max0)/cpow2, 1./npow2 ) + x1br*1.0) - x1max0;
10754  }
10755  else{
10756  // f-f0 = (x-x0)*dfdx -> if f=Rout -> x = (Rout-f0)/dfdx+x0
10757 
10758  FTYPE dVdx1=(npow + cpow2*npow2*cpow3*pow(cpow3*(x1max0-x1br*1.0),npow2-1.0)) * exp(npow*x1max0 + cpow2 * pow(cpow3*(x1max0-x1br*1.0),npow2));
10759  FTYPE V0 = R0 + exp(npow*x1max0 + cpow2 * pow(cpow3*(x1max0-x1br*1.0),npow2));
10760 
10761  dxmax=(Rout-V0)/dVdx1; // x-x0
10762 
10763  dualfprintf(fail_file,"dVdx1=%g V0=%g dxmax=%g x1max=%g x1max0=%g\n",dVdx1,V0,dxmax,x1max,x1max0);
10764  }
10765 
10766  // need a slight damping factor
10767  FTYPE dampingfactor=0.5;
10768  x1max = x1max0 + dampingfactor*dxmax;
10769 
10770 
10771  }
10772 
10773  if( iter == ITERMAX ) {
10774  trifprintf( "Error: iteration procedure for finding x1max has not converged: x1max = %g, dx1max/x1max = %g, iter = %d\n",
10775  x1max, (x1max-x1max0)/x1max, iter );
10776  exit(1);
10777  }
10778  else {
10779  trifprintf( "x1max = %g (dx1max/x1max = %g, itno = %d)\n", x1max, (x1max-x1max0)/x1max, iter );
10780  }
10781  }
10782 
10783  dx[1] = ( x1max - startx[1] ) /totalsize[1];
10784 #endif
10785 
10786 
10787  }
10788 
10789 }
10790 FTYPE setRin_user(int ihor, FTYPE ihoradjust)
10791 {
10792  if(1){
10793  FTYPE ftemp;
10794 
10795  // see jet3coords_checknew.nb (and fix_3dpolestissue.nb) to have chosen Rin and ihor and compute required R0
10796  if(npow==1.0){
10797  ftemp=ihoradjust/(FTYPE)totalsize[1];
10798  return(R0+pow((Rhor-R0)/pow(Rout-R0,ftemp),1.0/(1.0-ftemp)));
10799  }
10800  else if(npow2>0){
10801  return(1.2);
10802  }
10803  else{
10804  dualfprintf(fail_file,"ihoradjust=%21.15g totalsize[1]=%d Rhor=%21.15g R0=%21.15g npow=%21.15g Rout=%21.15g\n",ihoradjust,totalsize[1],Rhor,R0,npow,Rout);
10805  return(R0+exp( pow((totalsize[1]*pow(log(Rhor-R0),1.0/npow) - ihoradjust*pow(log(Rout-R0),1.0/npow))/(totalsize[1]-ihoradjust),npow)));
10806  }
10807  }
10808 
10809  if(WHICHUSERCOORD==ORIGWALD){
10810  FTYPE ftemp=ihoradjust/(FTYPE)totalsize[1];
10811  return(R0+pow((Rhor-R0)/pow(Rout-R0,ftemp),1.0/(1.0-ftemp)));
10812 
10813  }
10814 }
10815 
10816 
10817 #define MAXIHOR 10
10818 #define FRACN1 (0.1)
10819 #define ADJUSTFRACT (0.25)
10820 
10821 int setihor_user(void)
10822 {
10823  // set to smaller of either totalsize[1]*0.1 or MAXIHOR
10824  if(totalsize[1]*FRACN1>MAXIHOR) return((int)MAXIHOR);
10825  else return((int)((FTYPE)totalsize[1]*(FTYPE)FRACN1));
10826 }
10827 #undef MAXIHOR
10828 #undef FRACN1
10829 #undef ADJUSTFRACT