-
Notifications
You must be signed in to change notification settings - Fork 7
/
fixup.c
3734 lines (2900 loc) · 127 KB
/
fixup.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
// all fixup stuff only called for non-B advance
#include "decs.h"
static int simple_average(int startpl, int endpl, int i, int j, int k,PFTYPE (*lpflagfailorig)[NSTORE2][NSTORE3],FTYPE (*pv)[NSTORE2][NSTORE3][NPR],FTYPE (*ptoavg)[NSTORE2][NSTORE3][NPR]);
static int general_average(int startpl, int endpl, int i, int j, int k, PFTYPE mypflag, PFTYPE (*lpflagfailorig)[NSTORE2][NSTORE3],FTYPE (*pv)[NSTORE2][NSTORE3][NPR],FTYPE (*ptoavg)[NSTORE2][NSTORE3][NPR], struct of_geom *geom);
static int fixup_nogood(int startpl, int endpl, int i, int j, int k, FTYPE (*pv)[NSTORE2][NSTORE3][NPR],FTYPE (*ptoavg)[NSTORE2][NSTORE3][NPR],FTYPE (*pbackup)[NSTORE2][NSTORE3][NPR], struct of_geom *ptrgeom);
static int fixuputoprim_accounting(int i, int j, int k, PFTYPE mypflag, PFTYPE (*lpflag)[NSTORE2][NSTORE3][NUMPFLAGS],FTYPE (*pv)[NSTORE2][NSTORE3][NPR],FTYPE (*ptoavg)[NSTORE2][NSTORE3][NPR], struct of_geom *geom, FTYPE *pr0, FTYPE (*ucons)[NSTORE2][NSTORE3][NPR], int finalstep);
static int fixup_negdensities(int *fixed, int startpl, int endpl, int i, int j, int k, PFTYPE mypflag, FTYPE (*pv)[NSTORE2][NSTORE3][NPR],FTYPE (*ptoavg)[NSTORE2][NSTORE3][NPR], struct of_geom *geom, FTYPE *pr0, FTYPE (*ucons)[NSTORE2][NSTORE3][NPR], int finalstep);
static int superdebug_utoprim(FTYPE *pr0, FTYPE *pr, struct of_geom *ptrgeom, int whocalled);
/* apply floors to density, internal energy */
// currently called before bound, which assumes bound sets boundary
// values exactly as wanted without any fixing.
#define JONFIXUP 1 // 0=gammie 1=jon's
#if 0
void fixup(int stage,FTYPE (*pv)[NSTORE2][NSTORE3][NPR],int finalstep)
{
int i, j, k;
COMPZLOOP{ pfixup(MAC(pv,i,j,k), i, j, k);}
}
#endif
// operations that require synch of boundary zones in MPI, or that require use of boundary zones at all
// operations that only need to be done inside computational loop
int pre_fixup(int stage,FTYPE (*pv)[NSTORE2][NSTORE3][NPR])
{
// grab b^2 flags (above fixup may change u or rho, so must do this after)
get_bsqflags(stage,pv);
return(0);
}
// operations that require synch of boundary zones in MPI, or that require use of boundary zones at all
// this function actually changes primitives
int post_fixup(int stageit,int finalstep, SFTYPE boundtime, FTYPE (*pv)[NSTORE2][NSTORE3][NPR],FTYPE (*pbackup)[NSTORE2][NSTORE3][NPR],FTYPE (*ucons)[NSTORE2][NSTORE3][NPR])
{
int stage,stagei,stagef;
int boundstage;
#if(UTOPRIMADJUST!=0)
////////////////////////////////////
//
// utoprim fixup of primitive solution
if(SIMULBCCALC<=0){ stagei=STAGEM1; stagef=STAGEM1; }
else if(SIMULBCCALC==1) { stagei=STAGE0; stagef=STAGE2;}
else if(SIMULBCCALC==2) { stagei=STAGE0; stagef=STAGE5;}
if(SIMULBCCALC>=1) boundstage=STAGE0;
else boundstage=STAGEM1;
for(stage=stagei;stage<=stagef;stage++){
// first bound failure flag
// OPTMARK: could optimize bound of pflag since often failures don't occur (just ask if any failures first), although probably negligible performance hit
if(stage<STAGE2){
bound_pflag(boundstage, finalstep, boundtime, GLOBALPOINT(pflag), USEMPI);
if(stage!=STAGEM1) boundstage++;
}
// check for bad solutions and set as failure if good is reasonably bad
#if(CHECKSOLUTION)
fixup_checksolution(stage,pv,finalstep);
// check solution changed pflag, so have to bound again
if(stage<STAGE2){
bound_pflag(boundstage, finalstep, boundtime, pflag, USEMPI);
if(stage!=STAGEM1) boundstage++;
}
#endif
// fixup before new solution (has to be here since need previous stage's failure flag)
fixup_utoprim(stage,pv,pbackup,ucons,finalstep);
#if(0)
// GODMARK: I don't see why need to bound pflag since already done with using pflag
if(stage<STAGE2){
if(stage!=STAGEM1){
bound_pflag(boundstage, finalstep, boundtime, pflag, USEMPI);
boundstage++;
}
}
#endif
}
#endif
////////////////////////////////////
//
// standard fixup of floor
// in this case stageit=-1, so does all stages
// fixup(stageit,pv,finalstep);
return(0);
}
// this function just reports problems, but doesn't fix them
int post_fixup_nofixup(int stageit, int finalstep, SFTYPE boundtime, FTYPE (*pv)[NSTORE2][NSTORE3][NPR],FTYPE (*pbackup)[NSTORE2][NSTORE3][NPR],FTYPE (*ucons)[NSTORE2][NSTORE3][NPR])
{
fixup_utoprim_nofixup(STAGEM1,pv,pbackup,ucons,finalstep);
return(0);
}
#if(JONFIXUP==1)
int fixup(int stage,FTYPE (*pv)[NSTORE2][NSTORE3][NPR],FTYPE (*ucons)[NSTORE2][NSTORE3][NPR], int finalstep)
{
int i, j, k;
struct of_geom geomdontuse;
struct of_geom *ptrgeom=&geomdontuse;
COMPZLOOP{
// dualfprintf(fail_file,"i=%d j=%d k=%d\n",i,j,k); fflush(fail_file);
get_geometry(i,j,k,CENT,ptrgeom);
if(fixup1zone(MAC(pv,i,j,k),MAC(ucons,i,j,k), ptrgeom,finalstep)>=1)
FAILSTATEMENT("fixup.c:fixup()", "fixup1zone()", 1);
}
return(0);
}
#else
// GAMMIE OLD FIXUP (not kept up to date)
int fixup(int stage,FTYPE (*pv)[NSTORE2][NSTORE3][NPR],FTYPE (*ucons)[NSTORE2][NSTORE3][NPR],int finalstep)
{
int i,j,k,pl,pliter;
int ip, jp, kp, im, jm, km;
FTYPE bsq, del;
FTYPE ftempA,ftempB;
FTYPE prfloor[NPR];
struct of_geom geomdontuse;
struct of_geom *ptrgeom=&geomdontuse;
COMPZLOOP {
get_geometry(i,j,k, CENT,ptrgeom) ;
// densities
if(DOEVOLVERHO||DOEVOLVEUU) set_density_floors(ptrgeom,MAC(pv,i,j,k),prfloor);
if(DOEVOLVERHO ){
/* floor on density (momentum *not* conserved) */
if (MACP0A1(pv,i,j,k,RHO) < prfloor[RHO]) {
#if(FLOORDIAGS)
fladd[RHO] +=
dVF * ptrgeom->gdet * (prfloor[RHO] - MACP0A1(pv,i,j,k,RHO));
#endif
MACP0A1(pv,i,j,k,RHO) = prfloor[RHO];
}
}
if(DOEVOLVEUU){
/* floor on internal energy */
if (MACP0A1(pv,i,j,k,UU) < prfloor[UU]) {
#if(FLOORDIAGS)
fladd[UU] +=
dVF * ptrgeom->gdet * (prfloor[UU] - MACP0A1(pv,i,j,k,UU));
#endif
MACP0A1(pv,i,j,k,UU) = prfloor[UU]; // REBECCAMARK
}
}
/* limit gamma wrt normal observer */
#if(WHICHVEL==VELREL4)
if(limit_gamma(GAMMAMAX,MAC(pv,i,j,k),MAC(ucons,i,j,k), ptrgeom,-1)>=1) // need general accounting for entire routine.
FAILSTATEMENT("fixup.c:fixup()", "limit_gamma()", 1);
#endif
}
return(0);}
#endif
// choose whether within correctable/diagnosticable region
int diag_fixup_correctablecheck(int docorrectucons, struct of_geom *ptrgeom)
{
int is_within_correctable_region;
int docorrectuconslocal;
if(DOONESTEPDUACCOUNTING){
docorrectuconslocal=docorrectucons;
}
else{
///////////
//
// determine if within correctable region
//
///////////
if( DOENOFLUX != NOENOFLUX ) {
is_within_correctable_region=((ptrgeom->i)>=Uconsevolveloop[FIS])&&((ptrgeom->i)<=Uconsevolveloop[FIE])&&((ptrgeom->j)>=Uconsevolveloop[FJS])&&((ptrgeom->j)<=Uconsevolveloop[FJE])&&((ptrgeom->k)>=Uconsevolveloop[FKS])&&((ptrgeom->k)<=Uconsevolveloop[FKE]);
}
else{
is_within_correctable_region=1; // assume diag_fixup() only called where ok to do change to ucons!
}
///////////
//
// determine if should do correction to ucons
// only correct once -- should really put correction somewhere else.
//
///////////
docorrectuconslocal=docorrectucons && is_within_correctable_region;
}
return(docorrectuconslocal);
}
// record who called the diag_fixup routine
int count_whocalled(struct of_geom *ptrgeom, int finalstep, int whocalled)
{
int tscale;
/////////////////////
//
// Count times in diag_fixup() and who called diag_fixup()
// count every time corrects, not just on conserved quantity tracking time
//
/////////////////////
if(DODEBUG){
if(whocalled>=NUMFAILFLOORFLAGS || whocalled<COUNTNOTHING || ptrgeom->i<-N1BND || ptrgeom->i>N1-1+N1BND ||ptrgeom->j<-N2BND || ptrgeom->j>N2-1+N2BND ||ptrgeom->k<-N3BND || ptrgeom->k>N3-1+N3BND ){
dualfprintf(fail_file,"In diag_fixup() whocalled=%d for i=%d j=%d k=%d\n",whocalled,ptrgeom->i,ptrgeom->j,ptrgeom->k);
myexit(24683463);
}
if(whocalled!=COUNTNOTHING){
int indexfinalstep;
indexfinalstep=0;
TSCALELOOP(tscale) GLOBALMACP0A3(failfloorcount,ptrgeom->i,ptrgeom->j,ptrgeom->k,indexfinalstep,tscale,whocalled)++;
if(finalstep){
indexfinalstep=1;
// iterate finalstep version
TSCALELOOP(tscale) GLOBALMACP0A3(failfloorcount,ptrgeom->i,ptrgeom->j,ptrgeom->k,indexfinalstep,tscale,whocalled)++;
}
}// end if counting something
}// end if DODEBUG
return(0);
}
// compute dU and account
// Assumes Ui,Uf are UDIAG form
// Assumes ucons is UEVOLVE form
int diag_fixup_dUandaccount(FTYPE *Ui, FTYPE *Uf, FTYPE *ucons, struct of_geom *ptrgeom, int finalstep, int whocalled, int docorrectuconslocal)
{
FTYPE dUincell[NPR];
int is_within_diagnostic_region;
FTYPE deltaUavg[NPR],Uiavg[NPR];
FTYPE Uprefixup[NPR],Upostfixup[NPR];
int pliter,pl;
int enerregion;
/////////////
//
// Get deltaUavg[] and also modify ucons if required and should
//
/////////////
if(DOENOFLUX != NOENOFLUX) { //SASMARKx: adjust the conserved quantity to correspond to the adjusted primitive quanitities
// Correction to conserved quantities not exactly accurate because using point values where should use averaged values
// notice that geometry comes after subtractions/additions of EOMs
UtoU(UDIAG,UEVOLVE,ptrgeom,Ui,Uprefixup); // convert from UDIAG -> UEVOLVE
UtoU(UDIAG,UEVOLVE,ptrgeom,Uf,Upostfixup); // convert from UDIAG -> UEVOLVE
PALLLOOP(pl) deltaUavg[pl] = Uf[pl]-Ui[pl];
if(docorrectuconslocal){
// correct ucons if requested
//adjust the averaged conserved quantity by the same amt. as the point conserved quantity
PALLLOOP(pl) ucons[pl] += Upostfixup[pl] - Uprefixup[pl];
// old code: UtoU(UDIAG,UEVOLVE,ptrgeom,Uf,ucons); // convert from UNOTHING->returntype (jon's comment)
// the above line actually converts fixed up U from diagnostic form of U (with gdet)
// to evolution form of U (maybe withnogdet) and replaces the avg. conserved quantity (ADT)
}
}
else if(0){
// this method doesn't work:
UtoU(UEVOLVE,UDIAG,ptrgeom,ucons,Uiavg); // convert from UNOTHING->returntype
if(docorrectuconslocal){
// notice that geometry comes after subtractions/additions of EOMs
UtoU(UDIAG,UEVOLVE,ptrgeom,Uf,ucons); // convert from UNOTHING->returntype
}
PALLLOOP(pl) deltaUavg[pl] = Uf[pl]-Uiavg[pl];
}
else{
// original HARM method
// don't modify ucons
PALLLOOP(pl) deltaUavg[pl] = Uf[pl]-Ui[pl];
}
//only do aggregate accounting, after the fact (just before taking the new time step)
if(DOONESTEPDUACCOUNTING && docorrectuconslocal < 0 || DOONESTEPDUACCOUNTING==0){
///////////////////
//
// get correction
//
//////////////////
PALLLOOP(pl){
// dUincell means already (e.g.) (dU0)*(\detg')*(dV') = integral of energy in cell = dUint0 in SM
// So compare this to (e.g.) (U0)*(\detg')*(dV') = U0*gdet*dV in SM
dUincell[pl]=dVF * deltaUavg[pl];
if(DOFLOORDIAG){
// only store this diagnostic once (not for each enerregion)
// Note that unlike failfloorcount[], failfloordu[] is independent of fladd and fladdterms that are integrated simultaneously rather than in dump_ener.c
// Also note that failfloordu not stored in restart file, so like spatial debug info it is lost upon restart.
GLOBALMACP0A1(failfloordu,ptrgeom->i,ptrgeom->j,ptrgeom->k,pl)+=dUincell[pl];
}
}// end over pl's
//////////////
//
// Loop over ENERREGIONs
//
//////////////
ENERREGIONLOOP(enerregion){
// setup pointers to enerregion diagnostics
enerpos=enerposreg[enerregion];
fladd=fladdreg[enerregion];
fladdterms=fladdtermsreg[enerregion];
///////////
//
// determine if within diagnostic region
//
///////////
is_within_diagnostic_region=WITHINENERREGION(enerpos,ptrgeom->i,ptrgeom->j,ptrgeom->k);
/////////////////////////
//
// diagnostics (both for enerregion and single-region types)
//
/////////////////////////
if(is_within_diagnostic_region){
PALLLOOP(pl){
// dUincell means already (e.g.) (dU0)*(\detg')*(dV') = integral of energy in cell = dUint0 in SM
// So compare this to (e.g.) (U0)*(\detg')*(dV') = U0*gdet*dV in SM
dUincell[pl]=dVF * deltaUavg[pl];
fladdterms[whocalled][pl] += (SFTYPE)dUincell[pl];
fladd[pl] += dUincell[pl];
}// end over pl's
}// end if within diagnostic region
}// end over enerregions
}// end if doing accounting
return(0);
}
// single call in step_ch.c:post_advance() to do all diag_fixup() diagnostic dU stores. Still allows counts by other diag_fixup calls.
// for DOONESTEPDUACCOUNTING==1
int diag_fixup_allzones(int truestep, int finalstep, FTYPE (*pf)[NSTORE2][NSTORE3][NPR], FTYPE (*ucons)[NSTORE2][NSTORE3][NPR])
{
if(truestep && finalstep){
int i, j, k, pliter,pl;
struct of_geom *ptrgeom;
struct of_geom geomdontuse;
if(DOENOFLUX!=NOENOFLUX){
dualfprintf(fail_file,"Cannot use diag_fixup_allzones() with DOENOFLUX==NOENOFLUX\n");
myexit(3487622211);
}
ptrgeom=&(geomdontuse);
COMPZLOOP{
get_geometry(i, j, k, CENT, ptrgeom);
// account for change of conserved quantities
// Primitives have been modified by fixup1zone() in advance.c (floors).
// During this call, called from post_advance(), pf also modified by bounds (poledeath,gammadeath) and also post_fixup() (failures, checks, limits).
//
// ucons=unewglobal that stores last steps final substep version of ucum that is full U[]
// ucons has yet to be modified at all, so is true conserved quantity without corrections (as long as avoided corrections to ucons during other diag_fixup calls).
// GODMARK: So this method only works if NOENOFLUX==1, since otherwise *need* to modify U[] during modification of p[] since know how much to modify.
int docorrectucons=-1; // -1 is like 1, but is used to tell if coming from this function (if -1) or not (if 1)
diag_fixup_Ui_pf(docorrectucons,MAC(ucons,i,j,k),MAC(pf,i,j,k),ptrgeom,finalstep,COUNTONESTEP);
}
}
return(0);
}
// account for changes by tracking conserved quantities
// accounts for both failures and floor recoveries
// this modifies unew if on finalstep to be consistent with floor-limited primitive
// diagnostics only for actions on conservative quantities
// assume COUNT types are of PFTYPE
int diag_fixup(int docorrectucons, FTYPE *pr0, FTYPE *pr, FTYPE *ucons, struct of_geom *ptrgeom, int finalstep, int whocalled)
{
struct of_state q;
FTYPE Uicent[NPR],Ufcent[NPR];
int failreturn;
void UtoU(int inputtype, int returntype,struct of_geom *ptrgeom,FTYPE *Uin, FTYPE *Uout);
FTYPE deltaUavg[NPR],Uiavg[NPR];
FTYPE Uprefixup[NPR],Upostfixup[NPR];
int docorrectuconslocal;
#if(DOSUPERDEBUG)
superdebug_utoprim(pr0,pr,ptrgeom,whocalled);
// collect values for non-failed and failed zones
#endif
// count whocalled diag_fixup()
count_whocalled(ptrgeom, finalstep, whocalled);
/////////////////////////////////////////
//
// Account for changes in primitives or conserved quantities due to fixups (floor or failures or any other thing that can call diag_fixup()
//
// only account if on full timestep
// ucum (unew) only inverted to primitives on final substep. Any other conserved or primitive corrections do not matter since they only affected fluxes that go into true conserved quantity that is ucum (unew)
//
/////////////////////////////////////////
if(finalstep > 0){
///////////
// determine if within correctable region
///////////
docorrectuconslocal=diag_fixup_correctablecheck(docorrectucons,ptrgeom);
////////////////////////
//
// Get Uicent and Ufcent. Don't do this inside enerregion because no point since assume diag_fixup() called in limited regions of i,j,k anyways.
//
// only account if within active zones for that region
//
// Only valid if not higher order method or if MERGED method where conserved (except field) are at points as also the primitives are
//
////////////////////////
// before any changes
failreturn=get_state(pr0,ptrgeom,&q);
if(failreturn>=1) dualfprintf(fail_file,"get_state(1) failed in fixup.c, why???\n");
failreturn=primtoU(UDIAG,pr0,&q,ptrgeom,Uicent);
if(failreturn>=1) dualfprintf(fail_file,"primtoU(1) failed in fixup.c, why???\n");
// after any changes
failreturn=get_state(pr,ptrgeom,&q);
if(failreturn>=1) dualfprintf(fail_file,"get_state(2) failed in fixup.c, why???\n");
failreturn=primtoU(UDIAG,pr,&q,ptrgeom,Ufcent);
if(failreturn>=1) dualfprintf(fail_file,"primtoU(2) failed in fixup.c, why???\n");
// if Uicent and Ufcent are both from pi and pf at CENT, then B1,B2,B3 entries are agreeably located even for FLUXB==FLUXCTSTAG
// Get deltaUavg[] and also modify ucons if required and should
diag_fixup_dUandaccount(Uicent, Ufcent, ucons, ptrgeom, finalstep, whocalled, docorrectuconslocal);
}// end if finalstep>0
return(0);
}
// like diag_fixup(), but input initial conserved quantity as Ui and final primitive as pf
// Must use this when pi[Ui] doesn't exist and had to use non-hot-MHD inversion.
// Assumes Ui is like unewglobal, so UEVOLVE type
// Assume ultimately hot MHD equations are used, so need to get new Uf that'll differ from Ui
// Also don't know Uf quite yet.
int diag_fixup_Ui_pf(int docorrectucons, FTYPE *Uievolve, FTYPE *pf, struct of_geom *ptrgeom, int finalstep, int whocalled)
{
struct of_state q;
FTYPE Ufcent[NPR],Uicent[NPR],ucons[NPR];
int failreturn;
int pliter,pl,enerregion;
void UtoU(int inputtype, int returntype,struct of_geom *ptrgeom,FTYPE *Uin, FTYPE *Uout);
int docorrectuconslocal;
// count whocalled diag_fixup()
count_whocalled(ptrgeom, finalstep, whocalled);
if(finalstep > 0){
// determine if within correctable region
docorrectuconslocal=diag_fixup_correctablecheck(docorrectucons,ptrgeom);
//////////////////
//
// Get ucons
//
//////////////////
// GODMARK: NOENOFLUX==0 not accounted for (have to change unewglobal or something like that)
PLOOP(pliter,pl) ucons[pl]=Uievolve[pl];
//////////////////
//
// Get Ufcent(pf[cent])
//
//////////////////
failreturn=get_state(pf,ptrgeom,&q);
if(failreturn>=1) dualfprintf(fail_file,"get_state(2) failed in fixup.c, why???\n");
failreturn=primtoU(UDIAG,pf,&q,ptrgeom,Ufcent);
if(failreturn>=1) dualfprintf(fail_file,"primtoU(2) failed in fixup.c, why???\n");
//////////////////
//
// Get Uicent
//
//////////////////
// UEVOLVE -> UDIAG
// Assumes that Uievolve is like unewglobal and is at general U[] position (i.e. U[B1..B3] staggered and otherwise centered for FLUXB==FLUXCTSTAG)
UtoU(UEVOLVE,UDIAG,ptrgeom,Uievolve,Uicent);
// Override B1..B3 with correct centered versions (correct both for value and geometry)
// ensure B^i is really at center even if FLUXB==FLUXCTSTAG (that would have Ui[B1..B3] at staggered)
// Assumes, as very generally true, that U[B1..B3] never change and can never be adjusted.
PLOOPBONLY(pl) Uicent[pl]=Ufcent[pl];
////////////////
//
// Get deltaUavg[] and also modify ucons if required and should
//
////////////////
diag_fixup_dUandaccount(Uicent, Ufcent, ucons, ptrgeom, finalstep, whocalled, docorrectuconslocal);
}// end if finalstep>0
return(0);
}
// account for changes by tracking conserved quantities
// accounts for both failures and floor recoveries
// only called on final step of RK once unew is defined since only on final step is unew modified if floor encountered
// ONLY used by phys.ffde.c inversion routine when E^2>B^2
// Assume Ui and Uf in UDIAG form
int diag_fixup_U(int docorrectucons, FTYPE *Ui, FTYPE *Uf, FTYPE *ucons, struct of_geom *ptrgeom, int finalstep,int whocalled)
{
FTYPE Uicent[NPR],Ufcent[NPR];
struct of_state q;
int failreturn;
int pliter,pl,enerregion, tscale;
void UtoU(int inputtype, int returntype,struct of_geom *ptrgeom,FTYPE *Uin, FTYPE *Uout);
int docorrectuconslocal;
// count whocalled diag_fixup()
count_whocalled(ptrgeom, finalstep, whocalled);
if(finalstep>0){ // only account if on full timestep (assume only called if finalstep==1
///////////
// determine if within correctable region
///////////
docorrectuconslocal=diag_fixup_correctablecheck(docorrectucons,ptrgeom);
///////////
//
// First get correction (don't do inside enerregion loop since would be overly expensive and assume will need correction for at least one enerregion)
//
///////////
///////////
//
// Change ucons (GODMARK: assumes Uf at CENT since uses single ptrgeom -- even though ucons is normally capable of being staggered for B1..B3)
//
///////////
if(DOENOFLUX != NOENOFLUX){ // JONMARK
// notice that geometry comes after subtractions/additions of EOMs
UtoU(UDIAG,UEVOLVE,ptrgeom,Uf,ucons); // convert from UDIAG->UEVOLVE
}
// get Uicent and Ufcent
// Assumes that Ui is like unewglobal and is at general U[] position (i.e. U[B1..B3] staggered and otherwise centered for FLUXB==FLUXCTSTAG)
PLOOP(pliter,pl){
Uicent[pl]=Ui[pl];
Ufcent[pl]=Uf[pl];
}
// ensure B^i is really at center even if FLUXB==FLUXCTSTAG (that would have Ui[B1..B3] at staggered)
// Assumes, as very generally true, that U[B1..B3] never change and can never be adjusted.
PLOOPBONLY(pl) Uicent[pl]=Ufcent[pl];
// Get deltaUavg[] and also modify ucons if required and should
diag_fixup_dUandaccount(Uicent, Ufcent, ucons, ptrgeom, finalstep, whocalled, docorrectuconslocal);
}
return(0);
}
// 0 = primitive (adds rho,u in comoving frame)
// 1 = conserved but rho,u added in ZAMO frame
// 2 = conserved but ignore strict rho,u change for ZAMO frame and instead conserved momentum (doesn't keep desired u/rho, b^2/rho, or b^2/u and so that itself can cause problems
#define FIXUPTYPE 1
// finalstep==0 is non-accounting, finalstep==1 is accounting
int fixup1zone(FTYPE *pr, FTYPE *ucons, struct of_geom *ptrgeom, int finalstep)
{
int pliter,pl;
int ip, jp, im, jm;
FTYPE bsq, del;
FTYPE r, th, X[NDIM];
FTYPE ftempA,ftempB;
struct of_state q;
struct of_state dq;
FTYPE prfloor[NPR];
FTYPE prdiag[NPR];
FTYPE pr0[NPR];
FTYPE prnew[NPR];
FTYPE U[NPR];
int checkfl[NPR];
int failreturn;
int didchangeprim;
FTYPE scalemin[NPR];
// FTYPE ucovzamo[NDIM];
// FTYPE uconzamo[NDIM];
FTYPE dpr[NPR];
FTYPE dU[NPR];
// FTYPE P,Pnew;
int jj;
int badinversion;
// assign general floor variables
// whether to check floor condition
PALLLOOP(pl){
checkfl[pl]=0;
pr0[pl]=pr[pl];
prdiag[pl]=pr0[pl];
}
// shouldn't fail since before and after states should be ok, as
// long as would have changed the value. Check will occur if
// simulation continues ok. Could place check inside if below.
didchangeprim=0;
////////////
//
// Set which quantities to check
//
////////////
if(DOEVOLVERHO){
checkfl[RHO]=1;
}
if(DOEVOLVEUU){
checkfl[UU]=1;
}
////////////
//
// Only apply floor if cold or hot GRMHD
//
////////////
if(DOEVOLVERHO||DOEVOLVEUU){
//////////////
//
// get floor value
//
//////////////
set_density_floors(ptrgeom,pr,prfloor);
scalemin[RHO]=RHOMINLIMIT;
scalemin[UU]=UUMINLIMIT;
//////////////
//
// Set super low floor
//
//////////////
PALLLOOP(pl){
if(checkfl[pl]){
if(prfloor[pl]<scalemin[pl]) prfloor[pl]=scalemin[pl];
}
}
/////////////////////////////
//
// Get new primitive if went beyond floow
//
/////////////////////////////
PALLLOOP(pl){
if ( checkfl[pl]&&(prfloor[pl] > pr[pl]) ){
didchangeprim=1;
//dualfprintf(fail_file,"%d : %d %d %d : %d : %d : %21.15g - %21.15g\n",pl,ptrgeom->i,ptrgeom->j,ptrgeom->k,ptrgeom->p,checkfl[pl],prfloor[pl],pr[pl]);
// only add on full step since middle step is not really updating primitive variables
prnew[pl]=prfloor[pl];
}
else prnew[pl]=pr[pl];
}
//////////////////////////
//
// ONLY do something if want lower than floor
//
//////////////////////////
if(didchangeprim){
#if(FIXUPTYPE==0)
// effectively adds mass/internal energy in comoving frame, which can lead to instabilities as momentum is added
// For example, occurs on poles where u^r\sim 0 (stagnation surface) which launches artificially high u^t stuff only because goes below floor for a range of radii and so adds momentum to low density material
//
PALLLOOP(pl){
pr[pl]=prnew[pl];
}
#elif(FIXUPTYPE==1 || FIXUPTYPE==2)
// mass and internal energy added in frame not necessarily the comoving frame
// using a frame not directly associated with comoving frame avoids arbitrary energy-momentum growth
// GODMARK: FIXUPTYPE==1 doesn't exactly match between when b^2/\rho_0>BSQORHOULIMIT such that amount of mass added will force equality of b^2/\rho_0==BSQORHOLIMIT, so this may lead to problems.
// physically FIXUPTYPE==1 models some non-local transport of baryons and energy to a location that supposedly occurs when b^2/rho_0 is too large.
// FIXUPTYPE==2 models a local injection of baryons/energy with momentum conserved and energy-momentum conserved if mass injected. Injection will slow flow. Essentially there is an ad hoc conversion of kinetic/thermal energy into mass energy.
// compute original conserved quantities
failreturn=get_state(pr,ptrgeom,&q);
if(failreturn>=1) dualfprintf(fail_file,"get_state(1) failed in fixup.c, why???\n");
failreturn=primtoU(UNOTHING,pr,&q,ptrgeom,U);
if(failreturn>=1) dualfprintf(fail_file,"primtoU(1) failed in fixup.c, why???\n");
// get change in primitive quantities
PALLLOOP(pl) dpr[pl]=0.0; // default
// use ZAMO velocity as velocity of inserted fluid
for(pl=RHO;pl<=UU;pl++) dpr[pl]=prnew[pl]-pr[pl];
set_zamo_velocity(WHICHVEL,ptrgeom,dpr);
// get change in conserved quantities
failreturn=get_state(dpr,ptrgeom,&dq);
failreturn=primtoU(UNOTHING,dpr,&dq,ptrgeom,dU);
if(failreturn>=1) dualfprintf(fail_file,"primtoU(2) failed in fixup.c, why???\n");
if(FIXUPTYPE==1){
// then done, dU is right
}
else if(FIXUPTYPE==2){
// then don't allow momentum to change regardless of meaning for implied rho,u
dU[U1]=dU[U2]=dU[U3]=0.0;
pl=UU;
if ( checkfl[pl]&&(prfloor[pl] > pr[pl]) ){
// then must change dU[UU]
}
else dU[UU]=0.0; // if only mass added, then no change needed to energy-momentum
}
// get final new conserved quantity
PALLLOOP(pl) U[pl]+=dU[pl];
// pr finally changes here
// get primitive associated with new conserved quantities
struct of_newtonstats newtonstats;
failreturn=Utoprimgen(finalstep,OTHERUTOPRIM,UNOTHING,U,ptrgeom,pr,&newtonstats);
badinversion = (failreturn>=1 || IFUTOPRIMFAIL(GLOBALMACP0A1(pflag,ptrgeom->i,ptrgeom->j,ptrgeom->k,FLAGUTOPRIMFAIL)));
if(badinversion){
if(debugfail>=2) dualfprintf(fail_file,"Utoprimgen failed in fixup.c");
// if problem with Utoprim, then just modify primitive quantities as normal without any special constraints
PALLLOOP(pl){
pr[pl]=prnew[pl];
}
}
#endif
}// end if didchangeprim
}// end if cold or hot GRMHD
///////////
//
// since inflow check is on boundary values, no need for inflow check here
//
///////////
///////////////////////////////
//
// account for primitive changes
//
///////////////////////////////
if(didchangeprim&&FLOORDIAGS){// FLOORDIAGS includes fail diags
int docorrectucons=1;
diag_fixup(docorrectucons,prdiag, pr, ucons, ptrgeom, finalstep,COUNTFLOORACT);
// now prdiag=pr as far as diag_fixup() is concerned, so next changes are new changes (i.e. don't cumulative w.r.t. pr0 multiple times, since that (relative to pr each time) would add each prior change to each next change)
PALLLOOP(pl) prdiag[pl]=pr[pl];
}
////////////////////
//
// limit gamma wrt normal observer
//
////////////////////
#if(WHICHVEL==VELREL4)
int docorrectucons=1;
didchangeprim=0;
failreturn=limit_gamma(GAMMAMAX,pr,ucons,ptrgeom,-1);
if(failreturn>=1) FAILSTATEMENT("fixup.c:fixup()", "limit_gamma()", 1);
if(failreturn==-1) didchangeprim=1;
if(didchangeprim&&FLOORDIAGS){// FLOORDIAGS includes fail diags
diag_fixup(docorrectucons,prdiag, pr, ucons, ptrgeom, finalstep,COUNTLIMITGAMMAACT);
PALLLOOP(pl) prdiag[pl]=pr[pl];
}
#endif// end if WHICHVEL==VEL4REL
//////////////////////////
// now keep track of modified primitives via conserved quantities
// if(didchangeprim){
// assume once we go below floor, all hell will break loose unless we calm the storm by shutting down this zone's relative velocity
// normal observer velocity
// i.e. consider this a failure
//GLOBALMACP0A1(pflag,ptrgeom->i,ptrgeom->j,ptrgeom->k,FLAGUTOPRIMFAIL)= 1;
// }
return(0);
}
// number of vote checks per zone
#define MAXVOTES 8
#define NUMCHECKS 2
#define ISGAMMACHECK 0
#define ISUUCHECK 0
// GODMARK: function is 2D right now, but works in 3D, it just uses only x-y plane for checking
// check whether solution seems reasonable
// useful if b^2/rho\gg 1 or try to approach stationary model where variations in space shouldn't be large zone to zone
// checks whether u or gamma is much different than surrounding zones.
// if true, then flag as failure, else reasonable solution and keep
// can't assume failed zones are reasonably set
// fixup_checksolution() currently only uses pflag[FLAGUTOPRIMFAIL]
int fixup_checksolution(int stage, FTYPE (*pv)[NSTORE2][NSTORE3][NPR],int finalstep)
{
// int inboundloop[NDIM];
// int outboundloop[NDIM];
// int innormalloop[NDIM];
// int outnormalloop[NDIM];
// int inoutlohi[NUMUPDOWN][NUMUPDOWN][NDIM];
// int riin,riout,rjin,rjout,rkin,rkout;
// int dosetbc[COMPDIM*2];
// int ri;
// int boundvartype=BOUNDINTTYPE;
// extra memory