-
Notifications
You must be signed in to change notification settings - Fork 9
/
u2p.c
2518 lines (2031 loc) · 59.4 KB
/
u2p.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! \file u2p.c
\brief MHD Conserved to primitives conversion
*/
#include "ko.h"
static FTYPE dpdWp_calc_vsq(FTYPE Wp, FTYPE D, FTYPE vsq,FTYPE gamma);
static FTYPE compute_idwmrho0dp(FTYPE wmrho0,FTYPE gamma);
static FTYPE compute_idrho0dp(FTYPE wmrho0);
static int f_u2p_hot(ldouble Wp, ldouble* cons,ldouble *f,ldouble *df,ldouble *err,ldouble pgamma);
static FTYPE pressure_wmrho0_idealgas(FTYPE rho0, FTYPE wmrho0,FTYPE gamma);
static FTYPE compute_inside_entropy_wmrho0_idealgas(FTYPE rho0, FTYPE wmrho0,FTYPE gamma);
static FTYPE compute_specificentropy_wmrho0_idealgas(FTYPE rho0, FTYPE wmrho0,FTYPE gamma);
static FTYPE compute_dspecificSdwmrho0_wmrho0_idealgas(FTYPE rho0, FTYPE wmrho0,FTYPE gamma);
static FTYPE compute_dspecificSdrho_wmrho0_idealgas(FTYPE rho0, FTYPE wmrho0, FTYPE gamma);
static int f_u2p_entropy(ldouble Wp, ldouble* cons, ldouble *f, ldouble *df, ldouble *err,ldouble pgamma);
//**********************************************************************
//calculates primitives in given cell basing on global array u[]
// type: not used
// int setflags -- is always set to 1 in the current code
//**********************************************************************
int
calc_primitives(int ix,int iy,int iz,int type,int setflags)
{
int iv,u2pret,u2pretav;
ldouble uu[NV],uuav[NV],pp[NV],ppav[NV];
ldouble (*gg)[5],(*GG)[5],gdet,gdetu;
struct geometry geom;
fill_geometry(ix,iy,iz,&geom);
//temporary local arrays
gg=geom.gg;
GG=geom.GG;
gdet=geom.gdet;gdetu=gdet;
#if (GDETIN==0) //gdet out of derivatives
gdetu=1.;
#endif
int corrected[3]={0,0,0}, fixups[2]={0,0};
for(iv=0;iv<NV;iv++)
{
uu[iv]=get_u(u,iv,ix,iy,iz);
pp[iv]=get_u(p,iv,ix,iy,iz);
}
if(setflags)
{
set_cflag(ENTROPYFLAG,ix,iy,iz,0);
set_cflag(ENTROPYFLAG2,ix,iy,iz,0);
}
//u to p inversion is done here
if(is_cell_corrected_polaraxis(ix,iy,iz))
{
u2p_solver_Bonly(uu,pp,&geom); // invert only the magnetic field, the rest will be overwritten
}
else
{
u2p(uu,pp,&geom,corrected,fixups,type); // regular inversion
}
//set flags for entropy solver
if(corrected[0]==1 && setflags) //hd correction - entropy solver
{
set_cflag(ENTROPYFLAG,ix,iy,iz,1);
}
if(corrected[2]==1 && setflags) //borrowing energy from radiation didn't work
{
set_cflag(ENTROPYFLAG2,ix,iy,iz,1);
}
//check hd floors
int floorret=0;
if(is_cell_active(ix,iy,iz) && !is_cell_corrected_polaraxis(ix,iy,iz))
{
floorret=check_floors_mhd(pp,VELPRIM,&geom);
}
//check rad floors
#ifdef RADIATION
floorret=0;
if(is_cell_active(ix,iy,iz) && !is_cell_corrected_polaraxis(ix,iy,iz))
{
floorret=check_floors_rad(pp,VELPRIMRAD,&geom);
}
#endif
//set new primitives and conserved
for(iv=0;iv<NV;iv++)
{
set_u(p,iv,ix,iy,iz,pp[iv]);
}
//set flags for fixups of unsuccessful cells
if(setflags)
{
if(fixups[0]>0)
{
set_cflag(HDFIXUPFLAG,ix,iy,iz,1);
global_int_slot[GLOBALINTSLOT_NTOTALMHDFIXUPS]++;
}
else
set_cflag(HDFIXUPFLAG,ix,iy,iz,0);
if(fixups[1]>0)
{
set_cflag(RADFIXUPFLAG,ix,iy,iz,-1);
global_int_slot[GLOBALINTSLOT_NTOTALRADFIXUPS]++;
}
else
set_cflag(RADFIXUPFLAG,ix,iy,iz,0);
}
return 0;
}
//**********************************************************************
//high-level u2p solver
// type: not used
//**********************************************************************
int
u2p(ldouble *uu0, ldouble *pp, void *ggg, int corrected[3], int fixups[2], int type)
{
struct geometry *geom
= (struct geometry *) ggg;
int gix,giy,giz;
mpi_local2globalidx(geom->ix,geom->iy,geom->iz,&gix,&giy,&giz);
ldouble uu[NV];
int iv;
PLOOP(iv) uu[iv]=uu0[iv];
ldouble (*gg)[5], (*GG)[5], gdet, gdetu, gdetu_inv;
gg=geom->gg;
GG=geom->GG;
gdet=geom->gdet;gdetu=gdet;
#if (GDETIN==0) //gdet out of derivatives
gdetu=1.;
#endif
gdetu_inv = 1. / gdetu;
int verbose=0;
int mhdcor=0;
int radcor=0;
corrected[0]=corrected[1]=corrected[2]=0;
fixups[0]=fixups[1]=0;
ldouble ppbak[NV];
for(iv=0;iv<NV;iv++)
ppbak[iv]=pp[iv];
// force-free inversion flag
int ffflag=0, mhdflag=1;
ldouble ffval=0.;
#ifdef FORCEFREE
ffflag = get_cflag(FFINVFLAG, geom->ix,geom->iy,geom->iz);
mhdflag = get_cflag(MHDINVFLAG, geom->ix,geom->iy,geom->iz);
ffval = get_u_scalar(ffinvarr, geom->ix,geom->iy,geom->iz);
if(ffval>1 || ffval<0)
{
printf("ffval out of bounds %.2f!\n",ffval);
exit(-1);
}
if(ffval>0 && ffflag!=1)
{
printf("ffflag inconsistent with ffval: %.2f %d!\n",ffval,ffflag);
exit(-1);
}
if(ffval<1 && mhdflag!=1)
{
printf("mhdflag inconsistent with ffval: %.2f %d!\n",ffval,mhdflag);
exit(-1);
}
#endif
//************************************
//magneto-hydro part
//************************************
// initial flags
int ret=0;
int u2pret=-1;
int u2pretff=-1,u2pretmhd=-1;
int entropyinvmhd=0;
//************************************
// actual u2p inversion
ldouble ppff[NV],ppmhd[NV];
for(iv=0;iv<NV;iv++)
{
ppff[iv]=pp[iv];
ppmhd[iv]=pp[iv];
}
if(ffflag==1) // FF inversion
{
u2pretff = u2p_solver_ff(uu,ppff,ggg,verbose);
}
if(mhdflag==1) // MHD inversion
{
// check for negative uu[RHO], set rho to floor, and demand a fixup
// ANDREW -- moved this into the mhd inversion section
// ANDREW -- do we need to demand a fixup for hybrid?
// ANDREW -- is it a problem uu is updated in hybrid?
ldouble alpha=geom->alpha;
if(uu[RHO] * alpha * gdetu_inv < 0.)
{
if(verbose)
printf("%4d > %4d %4d %4d > neg rhout (%e %e)- requesting fixup\n",
PROCID,gix,giy,giz,pp[RHO],uu[RHO]*alpha*gdetu_inv);
ppmhd[RHO]=RHOFLOOR;
uu[RHO]=RHOFLOOR*gdetu/alpha; // ANDREW -- changed, since uu[RHO]=rho*gamma*gdet/alpha.
global_int_slot[GLOBALINTSLOT_NTOTALMHDFIXUPS]++; // count as fixup
ret=-2; // request fixup in all cases if density hits the floor
}
#ifdef ENFORCEENTROPY
u2pretmhd = -1; // go straight to entropy inversion
#else
u2pretmhd = u2p_solver_mhd(uu,ppmhd,ggg,U2P_HOT,0); // invert using the hot energy equation
#endif
if(ALLOWENTROPYU2P) // Inversion with entropy equation -- on by default (see choices.h)
{
if(u2pretmhd<0) // true if energy equation failed, or if ENFORCEENTROPY is defined
{
ret=-1;
entropyinvmhd=1;
if(verbose>2)
{
printf("%4d > u2p_entr START %d > %d %d %d \n",
PROCID, u2pretmhd, geom->ix + TOI, geom->iy + TOJ,geom->iz + TOK);
}
// invert using entropy equation
u2pretmhd = u2p_solver_mhd(uu,ppmhd,ggg,U2P_ENTROPY,0);
if(u2pretmhd<0) // entropy inversion also failed
{
ret=-2;
if(verbose>1)
{
printf("%4d > u2p_entr ERROR %4d > %4d %4d %4d\n",
PROCID,u2pretmhd,geom->ix+TOI,geom->iy+TOJ,geom->iz+TOK);
}
} // if(u2pretmhd<0) // second time -- entropy eqn
} // if(u2pretmhd<0) // first time -- energy eqn
} // if(ALLOWENTROPYU2P)
} // if(mhdflag==1)
// combine ff and mhd inversions (or pick one)
#ifdef FORCEFREE
#ifdef HYBRID_FORCEFREE
// determine fallbacks for hybrid failures
if(ffflag==1 && mhdflag==1)
{
if(u2pretmhd<0. && u2pretff<0.)
{
mhdflag=0;
ffflag=0;
}
else if(u2pretmhd<0)
{
mhdflag=0; // mhd inversion failed, revert to ff
}
else if(u2pretff<0.)
{
ffflag=0.; // ff inversion failed, revert to mhd
}
}
// copy inverted primitives to pp
if(ffflag==1 && mhdflag==1) //hybrid ff-mhd inversion
{
if(u2pretmhd<0. || u2pretff<0.) u2pret=-2;
else u2pret = 1;
if(u2pret>=0)
{
for(iv=0;iv<NV;iv++)
pp[iv] = ffval*ppff[iv] + (1.-ffval)*ppmhd[iv];
}
}
else if(mhdflag==1) //pure mhd inversion
{
u2pret = u2pretmhd;
if(u2pret>=0)
{
for(iv=0;iv<NV;iv++)
pp[iv]=ppmhd[iv];
}
// if(u2pret==-1)printf("! %d %d %d %d %d \n",mhdflag,ffflag,u2pretmhd,u2pretff,u2pret);
}
else if(ffflag==1) // pure force-free inversion
{
u2pret = u2pretff;
if(u2pret>=0)
{
for(iv=0;iv<NV;iv++)
pp[iv]=ppff[iv];
}
}
else
{
u2pret=-2;
}
#else // FORCEFREE, not hybrid
u2pret=u2pretff;
if(u2pret>=0)
{
for(iv=0;iv<NV;iv++) //pure force-free inversion
pp[iv]=ppff[iv];
}
#endif // HYBRID_FORCEFREE
#else // MHD only
u2pret = u2pretmhd;
if(u2pret>=0)
{
for(iv=0;iv<NV;iv++) //pure mhd inversion
pp[iv]=ppmhd[iv];
}
#endif // FORCEFREE
if(u2pret<0) // inversion failed
{
//leave primitives unchanged
if(verbose>1)
{
printf("%4d > MHDU2PFAIL %d %d > %4d %4d %4d > ",
PROCID,u2pret,ret,geom->ix+TOI,geom->iy+TOJ,geom->iz+TOK);
printf("u2p prim. unchanged \n %d %d %.1f | u2pretmhd %d u2pretff %d\n\n",
mhdflag,ffflag,ffval,u2pretmhd,u2pretff);
}
for(iv=0;iv<NV;iv++)
pp[iv]=ppbak[iv];
ret=-3;
}
if(ret<-1) // request fixup when entropy failed or we hit the floor
{
fixups[0]=1;
if(verbose > 1)
{
printf("%4d > MHD FIXUPS %d %d > %4d %4d %4d \n",
PROCID,u2pret,ret,geom->ix+TOI,geom->iy+TOJ,geom->iz+TOK);
}
}
else
{
fixups[0]=0;
}
// add to counter if we used entropy in mhd inversion
if(entropyinvmhd==1)
{
mhdcor=1;
}
//************************************
//radiation part
//************************************
#ifdef RADIATION
//Do the radiative inversion from u2p_rad.c
u2p_rad(uu,pp,geom,&radcor);
if(radcor>0) //rad fixups only for critical failure in implicit
{
fixups[1]=1;
if(verbose > 1)
{
printf("%4d > RAD FIXUPS %d > %4d %4d %4d \n",
PROCID,radcor,geom->ix+TOI,geom->iy+TOJ,geom->iz+TOK);
}
}
#endif // RADIATION
//************************************
//output
//************************************
if(mhdcor>0) corrected[0]=1;
if(radcor>0) corrected[1]=1;
// ANDREW REMOVED BALANCEENTROPYWITHRADIATION
// so corrected[2] is always 0
return ret;
}
//**********************************************************************
//checks if hydro primitives make sense
//**********************************************************************
int
check_floors_mhd(ldouble *pp, int whichvel, void *ggg)
{
int verbose=0;
int ret=0;
int iv;
#if !defined(SKIPALLFLOORS)
struct geometry *geom
= (struct geometry *) ggg;
ldouble (*gg)[5],(*GG)[5];
gg=geom->gg;
GG=geom->GG;
ldouble uu[NV],pporg[NV];
p2u_mhd(pp,uu,ggg);
ldouble pgamma=GAMMA;
#ifdef CONSISTENTGAMMA
pgamma=pick_gammagas(geom->ix,geom->iy,geom->iz);
#endif
//**********************************************************************
// rho too small
if(pp[RHO]<RHOFLOOR)
{
for(iv=0;iv<NVMHD;iv++)
{
pporg[iv]=pp[iv];
}
if(verbose>0)
{
printf("hd_floors CASE 1-0 at %d %d %d (%e) \n",
geom->ix+TOI,geom->iy+TOJ,geom->iz+TOK,pp[RHO]);
}
pp[RHO]=RHOFLOOR;
ret=-1;
}
//**********************************************************************
// rho too small,
// floor scaling as r^-3/2
#ifdef RHOFLOOR_BH
ldouble xxBL[4];
#ifdef PRECOMPUTE_MY2OUT
get_xxout(geom->ix, geom->iy, geom->iz, xxBL);
#else
coco_N(geom->xxvec,xxBL,MYCOORDS,BLCOORDS);
#endif
ldouble rr = xxBL[1] / rhorizonBL;
ldouble rhofloor = RHOFLOOR_BH_NORM / sqrt(rr*rr*rr);
if(pp[RHO]<rhofloor)
{
for(iv=0;iv<NVMHD;iv++)
{
pporg[iv]=pp[iv];
}
if(verbose>0)
{
printf("hd_floors CASE 1-1 at %d %d %d (%e)\n",
geom->ix+TOI,geom->iy+TOJ,geom->iz+TOK,pp[RHO]);
}
pp[RHO]=rhofloor;
ret=-1;
}
#endif
//***********************************************************************
// rho too small,
// use the initial atmosphere as the floor on both density and pressure
#ifdef RHOFLOOR_INIT
ldouble xxBL[4], rout = 2.;
#ifdef PRECOMPUTE_MY2OUT
get_xxout(geom->ix, geom->iy, geom->iz, xxBL);
#else
coco_N(geom->xxvec,xxBL,MYCOORDS,BLCOORDS);
#endif
ldouble rr = xxBL[1] / rout;
ldouble rhofloor = RHOATMMIN / sqrt(rr*rr*rr);
ldouble uintfloor = UINTATMMIN / sqrt(rr*rr*rr*rr*rr);
if((pp[RHO] < rhofloor || pp[UU] < uintfloor))
{
for(iv = 0; iv < NVMHD; iv++)
{
pporg[iv] = pp[iv];
}
if(verbose>0)
{
printf("hd_floors CASE 1-3 at %d %d %d (%e)\n",
geom->ix+TOI, geom->iy+TOJ, geom->iz+TOK,pp[RHO]);
}
pp[RHO] = rhofloor;
pp[UU] = uintfloor;
ret=-1;
}
#endif
// ANDREW -- added counter for absolute density floors
if(geom->ifacedim==-1) // cell center
{
if(ret==-1) set_cflag(RHOFLOORFLAG,geom->ix,geom->iy,geom->iz,1);
else set_cflag(RHOFLOORFLAG,geom->ix,geom->iy,geom->iz,0);
}
//**********************************************************************
// uint too small (too cold)
if(pp[UU]<UURHORATIOMIN*pp[RHO])
{
for(iv=0;iv<NVMHD;iv++)
{
pporg[iv]=pp[iv];
}
if(verbose>0)
{
printf("hd_floors CASE 2 at %d,%d,%d (%e %e) \n",
geom->ix+TOI,geom->iy+TOJ,geom->iz+TOK,pp[RHO],pp[UU]);
//getchar();
}
pp[UU] = UURHORATIOMIN*pp[RHO]; //increasing uint
ret=-1;
}
//**********************************************************************
// uint too large (too hot)
if(pp[UU]>UURHORATIOMAX*pp[RHO])
{
for(iv=0;iv<NVMHD;iv++)
{
pporg[iv]=pp[iv];
}
if(verbose>0)
{
printf("hd_floors CASE 3 at %d,%d,%d (%e %e)\n",
geom->ix+TOI,geom->iy+TOJ,geom->iz+TOK,pp[RHO],pp[UU]);
//getchar();
}
pp[UU] = UURHORATIOMAX*pp[RHO]; //decreasing uint
ret=-1;
}
//**********************************************************************
//too magnetized
#ifdef MAGNFIELD
#ifndef FORCEFREE // NO magnetic floors for force-free or hybrid force-free
ldouble ucon[4],ucov[4];
ldouble bcon[4],bcov[4],bsq;
ldouble etacon[4],etarel[4];
ldouble f=1.;
ldouble fuu=1.;
int rhofloored=0;
int uufloored=0;
for(iv=1;iv<4;iv++)
ucon[iv]=pp[1+iv];
calc_ucon_ucov_from_prims(pp, geom, ucon, ucov);
calc_bcon_bcov_bsq_from_4vel(pp, ucon, ucov, geom, bcon, bcov, &bsq);
// check density vs bsq
if(bsq>B2RHORATIOMAX*pp[RHO])
{
if(verbose>0)
{
printf("mag_floors CASE 1 at %d,%d,%d (%e %e)\n",
geom->ix+TOI,geom->iy+TOJ,geom->iz,pp[RHO],bsq);
}
f=bsq/(B2RHORATIOMAX*pp[RHO]); //increase factor, f>1
rhofloored=1;
}
// check ugas vs bsq
if(bsq>B2UURATIOMAX*pp[UU])
{
if(verbose>0)
{
printf("mag_floors CASE 2 at (%d,%d,%d): %e %e\n",
geom->ix+TOI,geom->iy+TOJ,geom->iz,pp[UU],bsq);
}
fuu=bsq/(B2UURATIOMAX*pp[UU]); //increase factor, fuu>1
uufloored=1;
}
// floors were activated, apply them
if((rhofloored==1 || uufloored==1))
{
// save backups
for(iv=0;iv<NVMHD;iv++)
{
pporg[iv]=pp[iv];
}
#if(B2RHOFLOORFRAME==DRIFTFRAME) // new mass in drift frame, Ressler+2017
ldouble betapar,betasq,betasqmax,gammapar;
ldouble udotB,QdotB,Bsq,Bmag,wold,wnew;
ldouble xx,vpar;
ldouble Bcon[4],Bcov[4],ucondr[4],vcon[4],ucont[4];
// old enthalpy
wold = pporg[RHO] + pporg[UU]*pgamma;
// apply the floors to the scalars
pp[RHO] = pporg[RHO]*f;
pp[UU] = pporg[UU]*fuu;
// new enthalpy
wnew = pp[RHO] + pp[UU]*pgamma;
// parallel velocity and Lorentz
betapar = -bcon[0]/((bsq)*ucon[0]);
betasq = betapar*betapar*bsq;
betasqmax = 1. - 1./(GAMMAMAXHD*GAMMAMAXHD);
if(betasq>betasqmax)
{
betasq=betasqmax;
printf("floored parallel velocity\n");
}
gammapar = 1./sqrt(1.-betasq);
// drift frame velocity
for(iv=0;iv<4;iv++)
ucondr[iv] = gammapar*(ucon[iv] + betapar*bcon[iv]);
// magnetic field
Bcon[0]=0.;
Bcon[1]=pp[B1];
Bcon[2]=pp[B2];
Bcon[3]=pp[B3];
indices_21(Bcon,Bcov,gg);
Bsq = dot(Bcon,Bcov);
Bmag = sqrt(Bsq);
// conserved parallel momentum
udotB = dot(ucon,Bcov);
QdotB = udotB*wold*ucon[0];
// get new parallel velocity
xx = 2.*QdotB/(Bmag*wnew*ucondr[0]);
vpar = xx / (ucondr[0]*(1.+sqrt(1.+xx*xx)));
// new three velocity
vcon[0]=1.;
for(iv=1;iv<4;iv++)
{
vcon[iv]=vpar*Bcon[iv]/(Bmag) + ucondr[iv]/ucondr[0];
}
// to VELPRIM
conv_vels(vcon,ucont,VEL3,VELPRIM,gg,GG);
pp[VX] = ucont[1];
pp[VY] = ucont[2];
pp[VZ] = ucont[3];
#elif(B2RHOFLOORFRAME==ZAMOFRAME) //new mass in ZAMO frame
ldouble dpp[NV],duu[NV];
ldouble drho=pp[RHO]*(f-1.);
ldouble dugas=pp[UU]*(fuu-1.);
for(iv=0;iv<NVMHD;iv++)
dpp[iv]=0.0;
// normal observer
calc_normalobs_ncon(GG, geom->alpha, etacon);
conv_vels_ut(etacon,etarel,VEL4,VELPRIM,gg,GG);
dpp[RHO]=drho;
dpp[UU]=dugas;
//dpp[UU]=0.; //ANDREW this is a change,we used to keep UU fixed here
dpp[VX] = etarel[1];
dpp[VY] = etarel[2];
dpp[VZ] = etarel[3];
dpp[ENTR] = 0.;
dpp[B1] = dpp[B2] = dpp[B3] = 0.;
// delta conserved in ZAMO
p2u_mhd(dpp,duu,geom);
for(iv=0;iv<NVMHD;iv++)
{
uu[iv]+=duu[iv];
}
// find new prims after adding delta conserved in ZAMO
int rettemp=0;
rettemp = u2p_solver_mhd(uu,pp,geom,U2P_HOT,0);
if(rettemp<0)
rettemp = u2p_solver_mhd(uu,pp,geom,U2P_ENTROPY,0);
// adding new mass in ZAMO frame failed
if(rettemp<0)
{
printf("u2p failed after imposing ZAMO bsq over rho floors at %d %d %d\n",
geom->ix+TOI,geom->iy+TOJ,geom->iz+TOK);
#ifdef B2RHOFLOOR_BACKUP_FFFRAME
// if zamo frame fails, do fluid frame instead of crashing
for(iv=0;iv<NVMHD;iv++)
pp[iv]=pporg[iv];
pp[RHO]*=f;
pp[UU]*=fuu; //ANDREW this is a change, used to increase UU by same factor f
//pp[UU]*=f;
#else
// No backup, crash
print_primitives(pp);
exit(-1);
#endif
}
#elif(B2RHOFLOORFRAME==FFFRAME) //new mass in fluid frame
pp[RHO]*=f;
pp[UU]*=fuu; //ANDREW this is a change, used to increase UU by same factor f
//pp[UU]*=f;
#endif //B2RHOFLOORFRAME
// ANDREW
// TODO this seems wrong for species?
// TODO if uint changes above, we will not have ue + ui = uint using this method!
// TODO multiply both ui,ue by fuu?
#ifdef EVOLVEELECTRONS
//keep energy density in ions and electrons fixed after applying the floors
ldouble Tg,Te,Ti;
Tg=calc_PEQ_Teifrompp(pporg,&Te,&Ti,geom->ix,geom->iy,geom->iz);
ldouble ne=calc_thermal_ne(pporg); //thermal only
ldouble ni=pporg[RHO]/MU_I/M_PROTON;
ldouble pe=K_BOLTZ*ne*Te;
ldouble pi=K_BOLTZ*ni*Ti;
ldouble gammae=GAMMAE;
ldouble gammai=GAMMAI;
#ifdef CONSISTENTGAMMA
#ifndef FIXEDGAMMASPECIES
gammae=calc_gammaintfromtemp(Te,ELECTRONS);
gammai=calc_gammaintfromtemp(Ti,IONS);
#endif
#endif
ldouble ue=pe/(gammae-1.);
ldouble ui=pi/(gammai-1.);
//calculate new entropy using updated pp[]
ldouble mass, gamma, theta;
ldouble Tenew,Tinew, Senew, Sinew;
ldouble n, pnew;
//electrons
mass = M_ELECTR;
gamma = GAMMAE;
n = calc_thermal_ne(pp);
#ifdef CONSISTENTGAMMA
Tenew=solve_Teifromnmu(n, mass, ue,ELECTRONS); //solves in parallel for gamma and temperature
theta=K_BOLTZ*Tenew/mass;
#ifndef FIXEDGAMMASPECIES
gamma=calc_gammaintfromtheta(theta); //the same gamma as just solved
#endif
#endif
pnew=(ue)*(gamma-1.);
Tenew=pnew/K_BOLTZ/n;
Senew=calc_SefromrhoT(n*MU_E*M_PROTON,Tenew,ELECTRONS);
pp[ENTRE]=Senew;
//ions
mass = M_PROTON;
gamma = GAMMAI;
n = pp[RHO]/MU_I/M_PROTON;//calc_thermal_ne(pp);
#ifdef CONSISTENTGAMMA
Tinew=solve_Teifromnmu(n, mass, ui,IONS); //solves in parallel for gamma and temperature
theta=K_BOLTZ*Tinew/mass;
#ifndef FIXEDGAMMASPECIES
gamma=calc_gammaintfromtheta(theta); //the same gamma as just solved
#endif
#endif
pnew=(ui)*(gamma-1.);
Tinew=pnew/K_BOLTZ/n;
Sinew=calc_SefromrhoT(pp[RHO],Tinew,IONS);
pp[ENTRI]=Sinew;
#endif //EVOLVEELECTRONS
ret=-1;
} // if(rhofloored==1 || uufloored==1)
#endif //ndef FORCEFREE
#endif //MAGNFIELD
//**********************************************************************
//too fast
if(VELPRIM==VELR)
{
ldouble qsq=0.;
int i,j;
for(i=1;i<4;i++)
for(j=1;j<4;j++)
qsq+=pp[UU+i]*pp[UU+j]*gg[i][j];
ldouble gamma2=1.+qsq;
if(gamma2>GAMMAMAXHD*GAMMAMAXHD)
{
ldouble qsqmax=GAMMAMAXHD*GAMMAMAXHD-1.;
ldouble A=sqrt(qsqmax/qsq);
for(j=1;j<4;j++)
pp[UU+j]*=A;
if(verbose>0)
{
printf("hd_floors CASE 4 at %d,%d,%d (%e)",
geom->ix+TOI,geom->iy+TOJ,geom->iz,sqrt(gamma2));
}
ret = -1;
}
}
//**********************************************************************
//Species temperature floors/ceilings
//TODO ANDREW will not keep them consistent ue + ui = ugas!
#ifdef EVOLVEELECTRONS
ldouble mue,mui;
mui=MU_I;
mue=MU_E;
ldouble Tgas=calc_PEQ_Tfromurho(pp[UU],pp[RHO],geom->ix,geom->iy,geom->iz);
//Electrons
ldouble Teloc,Teloc0;
ldouble neth=calc_thermal_ne(pp);
ldouble rhoeth=MU_E*M_PROTON*neth;
Teloc=calc_TfromSerho(pp[ENTRE],rhoeth,ELECTRONS,geom->ix,geom->iy,geom->iz);
Teloc0=Teloc;
// absolute floor
if(Teloc<TEMPEMINIMAL)
{
Teloc=TEMPEMINIMAL;
}
// relative floor
ldouble Teminimal=TEMPEMINIMALFRACTION*Tgas;
if(Teloc<Teminimal)
{
Teloc=Teminimal;
}
// ceiling
ldouble Temaximal=TEMPEMAXIMALFRACTION*Tgas;
if(Teloc>Temaximal)
{
Teloc=Temaximal;
}
//Ion Temperature
ldouble Tiloc,Tiloc0;
Tiloc=calc_TfromSerho(pp[ENTRI],pp[RHO],IONS,geom->ix,geom->iy,geom->iz);
Tiloc0=Tiloc;
// absolute floor
if(Tiloc<TEMPIMINIMAL)
{
Tiloc=TEMPIMINIMAL;
}
// relative floor
ldouble Timinimal=TEMPIMINIMALFRACTION*Tgas;
if(Tiloc<Timinimal)
{
Tiloc=Timinimal;
}
// celing
ldouble Timaximal=TEMPIMAXIMALFRACTION*Tgas;
if(Tiloc>Timaximal)
{
Tiloc=Timaximal;
}
if(Teloc!=Teloc0) //update temperature of electrons
{
pp[ENTRE]=calc_SefromrhoT(rhoeth,Teloc,ELECTRONS);
ret=-1;
}
if(Tiloc!=Tiloc0) //update temperature of ioms
{
pp[ENTRI]=calc_SefromrhoT(pp[RHO],Tiloc,IONS);
ret=-1;
}
#ifdef RELELECTRONS
int ie;
ldouble ne_relel,ne_tot,uint_relel,uint_tot,p_relel,p_tot;
//No negative rel. electron numbers
for (ie=0; ie<NRELBIN; ie++)
{
if (pp[NEREL(ie)] < 0.0)
{
pp[NEREL(ie)] = 0.0;
}
}
//Not too many rel. electrons
ldouble relfracn, relfracu,relfracp;
ne_relel = calc_relel_ne(pp);
ne_tot = pp[RHO]/MU_E/M_PROTON;
relfracn = ne_relel/ne_tot;
if (relfracn > MAX_RELEL_FRAC_N)
{
for (ie=0; ie<NRELBIN; ie++)
{
pp[NEREL(ie)] *= (MAX_RELEL_FRAC_N/relfracn);
}
}
//Not too much rel electron energy
uint_relel = calc_relel_uint(pp);
uint_tot = pp[UU];
relfracu = uint_relel/uint_tot;
if (relfracu > MAX_RELEL_FRAC_U)
{
for (ie=0; ie<NRELBIN; ie++)
{
pp[NEREL(ie)] *= (MAX_RELEL_FRAC_U/relfracu);
}
}
#endif //RELELECTRONS
#endif //EVOLVEELECTRONS
//updates entropy after floor corrections
if(ret<0)
{
pp[ENTR]=calc_Sfromu(pp[RHO],pp[UU],geom->ix,geom->iy,geom->iz);
if(verbose) printf("FLOORED %d %d %d\n",ret,geom->ix,geom->ifacedim);
}
#endif //SKIPALLFLOORS
return ret;
}
//**********************************************************************
// MHD solver wrapper
//**********************************************************************
int
u2p_solver_mhd(ldouble *uu, ldouble *pp, void *ggg,int Etype,int verbose)
{
#ifdef NONRELMHD
return u2p_solver_nonrel(uu,pp,ggg,Etype,verbose);