-
Notifications
You must be signed in to change notification settings - Fork 7
/
fluxvpot.c
1796 lines (1249 loc) · 59.4 KB
/
fluxvpot.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
#include "decs.h"
// get directions for cyclic variables
// signflux determines signature of relationship between flux and A_i and likewise the EMF_i = -\detg E_i
// As in fluxct.c:
// For evolution:
// d_t A1 = EMF1 = -\detg E1 = F2[B3] = -F3[B2]
// d_t A2 = EMF2 = -\detg E2 = F3[B1] = -F1[B3]
// d_t A3 = EMF3 = -\detg E3 = F1[B2] = -F2[B1]
//
// For initialization:
// A1 = F2[B3] = -F3[B2]
// A2 = F3[B1] = -F1[B3]
// A3 = F1[B2] = -F2[B1]
// \detg B1 = d_2 A3 - d_3 A2
// \detg B2 = d_3 A1 - d_1 A3
// \detg B3 = d_1 A2 - d_2 A1
//
// opposite ordering required to be used when F[odir1] doesn't exist because N[odir1]==1
// Should use signflux when changing between A_i <-> flux
int get_fluxpldirs(int *Nvec, int dir, int *fluxdir, int* pldir, int *plforflux, FTYPE *signflux)
{
int odir1,odir2;
// get cyclic other dimensions
get_odirs(dir,&odir1,&odir2);
if(Nvec[odir1]>1) { *fluxdir=odir1; *pldir=odir2; *plforflux = B1+ *pldir-1; *signflux=1.0; } // A[dir] = F[odir1][B1+odir2-1] // normal ordering
else if(Nvec[odir2]>1){ *fluxdir=odir2; *pldir=odir1; *plforflux = B1+ *pldir-1; *signflux=-1.0; } // A[dir] = F[odir2][B1+odir1-1] // opposite ordering
else{ *fluxdir=0; *pldir=0; *plforflux=0; }
// else{
// dualfprintf(fail_file,"Shouldn't reach EMF line integration with Nvec=%d %d %d\n",Nvec[1],Nvec[2],Nvec[3]);
// myexit(917515);
// }
return(0);
}
// cyclic other dimensions
// dir=1 odir1=2 odir2=3 so signflux=1 for this ordering
// dir=2 odir1=3 odir2=1 so signflux=1 for this ordering
// dir=3 odir1=1 odir2=2 so signflux=1 for this ordering
void get_odirs(int dir,int *odir1,int *odir2)
{
// m%3+1 gives next 1->2,2->3,3->1
// 3-(4-m)%3 = (dir+1)%3+1 gives previous 1->3,2->1,3->2
*odir1=dir%3+1; // if dir==3, then odir1=1
*odir2=(dir+1)%3+1; // if dir==3, then odir2=2
}
// set locations for vpot or emf and number of independent memory locations for each A_i or E_i to consider
// GODMARK: Notice the positions loc[][] are only for initializing field, while FLUXCTTOTH evolves A_i really at FACE
int set_location_fluxasemforvpot(int dir, int *numdirs, int *odir1, int *odir2, int *loc)
{
int fieldloc[NDIM];
get_numdirs_fluxasemforvpot(numdirs,fieldloc); // field loc has locations of each field component (not used here)
get_odirs(dir,odir1,odir2);
if(FLUXB==FLUXCTHLL){
// for this method we use F1/F2/F3 and locations are different
loc[*odir1]=FACE1-1 + *odir1;
loc[*odir2]=FACE1-1 + *odir2;
}
else{
loc[*odir1]=CORN1-1 + dir;
loc[*odir2]=CORN1-1 + dir;
}
return(0);
}
// set locations for vpot or emf and number of independent memory locations for each A_i or E_i to consider
int get_numdirs_fluxasemforvpot(int *numdirs, int *fieldloc)
{
///////////
//
// These choices must be made consistent with use of vpot2field()
//
///////////
if(! (FLUXB==FLUXCTHLL || FLUXB==FLUXCTSTAG) ){
*numdirs = 1;
fieldloc[1]=CENT;
fieldloc[2]=CENT;
fieldloc[3]=CENT;
}
else if(FLUXB==FLUXCTHLL){
// for this method we use F1/F2/F3 and locations are different
*numdirs=2;
fieldloc[1]=CENT;
fieldloc[2]=CENT;
fieldloc[3]=CENT;
}
else{
if(extrazones4emf==0){
if(DOENOFLUX==ENOFLUXRECON && dofluxreconevolvepointfield==1){
// for this method we use F1/F2/F3 but location is same corner
*numdirs=2;
}
else{
// unless emfextrazones4emf==0 and fluxrecon and doing point field, then don't need both directions
*numdirs=1;
}
}
else{
// then just using A not F1/F2/F3
*numdirs=1;
}
if(FLUXB==FLUXCTSTAG){
fieldloc[1]=FACE1;
fieldloc[2]=FACE2;
fieldloc[3]=FACE3;
}
else{
// non-HLL version
fieldloc[1]=CENT;
fieldloc[2]=CENT;
fieldloc[3]=CENT;
}
}
return(0);
}
// assumes normal field p
// pleft used as temp var if FLUXB==FLUXCTSTAG
// assigns conserved field in UEVOLVE form (i.e. with gdet always)
// implicitly Flux F1,F2,F3 are inputted into function
int vpot2field(SFTYPE time, FTYPE (*A)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3],FTYPE (*pfield)[NSTORE2][NSTORE3][NPR], FTYPE (*pstag)[NSTORE2][NSTORE3][NPR], FTYPE (*ucons)[NSTORE2][NSTORE3][NPR], FTYPE (*Bhat)[NSTORE2][NSTORE3][NPR], FTYPE (*F1)[NSTORE2][NSTORE3][NPR], FTYPE (*F2)[NSTORE2][NSTORE3][NPR], FTYPE (*F3)[NSTORE2][NSTORE3][NPR], FTYPE (*Atemp)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3], FTYPE (*uconstemp)[NSTORE2][NSTORE3][NPR])
{
int i,j,k,pl,pliter;
int numdirs, fieldloc[NDIM];
////////////////
//
// get whether using A or F1/F2/F3 and where final field located per direction of field
//
////////////////
get_numdirs_fluxasemforvpot(&numdirs,fieldloc);
////////////////
//
// One of 3 things:
// 1) single line-integrate vector potential (A_i) for FV method
// 2) double transverse quasi-deaverage (per A_i) for FLUXRECON method if evolving Bhat
// 3) single quasi-deaverage (per flux direction) for FLUXRECON if evolving point field
//
/////////////////
if(DOENOFLUX==NOENOFLUX){
// nothing to do
}
else if(DOENOFLUX==ENOFLUXRECON || DOENOFLUX == ENOFINITEVOLUME){
if(numdirs==2){
// difference between CTHLL and CTSTAG is position of flux
// then treat flux itself as flux and assume flux set as vector potential
// don't actually use A here
// if doing FLUXRECON and evolving points, then
vectorpot_useflux(STAGEM1, pfield, F1, F2, F3);
}
else{
// SUPERGODMARK: should tell where field is located
vectorpot_fluxreconorfvavg(STAGEM1, pfield, A, F1, F2, F3);
}
}
else{
dualfprintf(fail_file,"No vectorpot for this case of DOENOFLUX=%d\n",DOENOFLUX);
myexit(83465765);
}
////////////////
//
// Now use higher-order vector potential to obtain (primitive AND conserved) field that satisfies divb=0 in certain form
//
/////////////////
if(numdirs==1){
// use of A
if((FLUXB==ATHENA1)||(FLUXB==ATHENA2)||(FLUXB==FLUXCTTOTH)){
vpot2field_centeredfield(A,pfield,ucons);
}
else if(FLUXB==FLUXCD){
vpot2field_centeredfield(A,pfield,ucons); // probably wrong for FLUXCD? GODMARK
dualfprintf(fail_file,"FLUXCD probably not setup right in vpot2field()\n");
myexit(196726);
}
else if(FLUXB==FLUXCTSTAG){
// overwrites any old choice for unew for field only
// pstag is in general not a point value of field!
vpot2field_staggeredfield(A,pfield,ucons);
}
else{
dualfprintf(fail_file,"Undefined numdirs==1 with FLUXB==%d\n",FLUXB);
myexit(2752632);
}
}
else if(numdirs==2){
// use of F1/F2/F3
vpot2field_useflux(fieldloc,pfield,ucons,F1,F2,F3);
// FLUXCTHLL is just for testing how behaves without divb=0 control
// with fluxcthll always numdirs==2 so uses flux instead of A
// overwrites any old choice for unew for field only
// pstag is in general not a point value of field!
if(! (FLUXB==FLUXCTHLL || FLUXB==FLUXCTSTAG) ){
dualfprintf(fail_file,"Undefined numdirs==2 with FLUXB==%d\n",FLUXB);
myexit(23578234);
}
if(FLUXB==FLUXCTSTAG && extrazones4emf==0 && DOENOFLUX == ENOFLUXRECON){
// DEBUG:
// bound_pstag(STAGEM1,t,pfield, pstag, ucons, 1, USEMPI);
// then need to obtian Bhat so can compute divb
// In this case unew is inputted point conserved field
field_Bhat_fluxrecon(pfield,ucons,Bhat);
}
}
///////////////
//
// copy result of vector potential calculation to staggered field if doing FLUXCTSTAG
//
///////////////
if(FLUXB==FLUXCTSTAG){
copy_3d_fieldonly_fullloop(pfield,pstag);
// from now on, pfield is assumed to be at CENT
}
////////////////
//
// convert average or quasi-deaveraged field to point field (ulast and pfield)
// Ok to use ulastglobal as temporary space
//
/////////////////
ucons2upointppoint(time,pfield,pstag,ucons,uconstemp,pfield);
// Since above procedures changed pfield that is probably pcent that is p, we need to rebound p since pfield was reset to undefined values in ghost cells since A_i isn't determined everywhere
// alternatively for evolve_withvpot() could have inputted not the true p or some copy of it so wouldn't have to bound (except up to machine error difference when recomputed field using A_i)
int finalstep=1; // assume user wants to know that conserved quants changed
bound_prim(STAGEM1,finalstep, time,pfield,pstag,ucons, USEMPI);
return(0);
}
// convert conservative U to stag point P and CENT point U and CENT point primitive
int ucons2upointppoint(SFTYPE boundtime, FTYPE (*pfield)[NSTORE2][NSTORE3][NPR], FTYPE (*pstag)[NSTORE2][NSTORE3][NPR],FTYPE (*unew)[NSTORE2][NSTORE3][NPR],FTYPE (*ulast)[NSTORE2][NSTORE3][NPR],FTYPE (*pcent)[NSTORE2][NSTORE3][NPR])
{
int i,j,k,pl,pliter;
int numdirs, fieldloc[NDIM];
////////////////
//
// get whether using A or F1/F2/F3 and where final field located per direction of field
//
////////////////
get_numdirs_fluxasemforvpot(&numdirs,fieldloc);
// SUPERDEBUG:
//bound_pstag(STAGEM1, t, pfield, pfield, unew);
//bound_pstag(STAGEM1, t, pfield, pfield, unew);
// dualfprintf(fail_file,"DEBUG Got here\n");
// if restarting, then unew was read-in and then later assigned to unitial, so by the time init is done we have staggered fields in unew/unitial for any case
// from staggered field still need to fill pfield with centered version
// for first call to interpolation need real primitive, but don't yet have CENT field, so use staggered version as "guess"-- GODMARK not strictly grid-correct
////////////////
//
// convert average or quasi-deaveraged field to point field (ulast)
//
/////////////////
if(numdirs==1){
if(DOENOFLUX == ENOFINITEVOLUME){
// unew and ulast both inputted so function compares results with original
deaverage_fields_fv(pfield,unew,ulast);
}
else if(DOENOFLUX == ENOFLUXRECON){
// unew and ulast both inputted so function compares results with original
field_integrate_fluxrecon(STAGEM1,pfield,unew,ulast);
}
else{
// second order methods have same average and point value
copy_3d_fieldonly_fullloop(unew,ulast);
}
}
else{
// FLUXB==FLUXCTHLL has point value as conserved field value like ENOFLUXRECON has for non-field values
copy_3d_fieldonly_fullloop(unew,ulast);
}
////////////////
//
// Convert point staggered field to point CENT field for staggered field method
//
/////////////////
if(FLUXB==FLUXCTSTAG){
// ok to insert pfield for real primitive since any update to pfield is ok to be used since only used for shock indicator
interpolate_ustag2fieldcent(STAGEM1,boundtime, -1,-1,pfield,pstag,ulast,pcent);
// now pstagscratch will have correct staggered point value and ulast (if needed) will be replaced with center conserved value and pfield will contain centered field primitive value
// now field sits in both centered and staggered positions with initial data
}
return(0);
}
// initialize vector potential given user function
// assumes normal non-staggered field in pr
// Notice that if using F (flux), then *location* can be different for (e.g.) F1[B2] and F2[B1] while if using A_3 then no choice in varying positions
int init_vpot(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 (*F1)[NSTORE2][NSTORE3][NPR], FTYPE (*F2)[NSTORE2][NSTORE3][NPR], FTYPE (*F3)[NSTORE2][NSTORE3][NPR], FTYPE (*Atemp)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3])
{
FTYPE (*A)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3];
#if(TRACKVPOT)
A=vpot; // real t=0 value put here and used to track A_i for diagnostics
#else
A=Atemp; // dummy memory space not used till computation so safe.
#endif
// prim -> A using user function init_vpot_user()
init_vpot_justAcov(t, prim, A); // t is ok since always t=tstart
// A->Flux if required
init_vpot_toF(A, F1, F2, F3);
// assigns value to p and pstagscratch and unew (uses F1/F2/F3 if doing FLUXCTHLL)
// often user just uses init_vpot2field() unless not always using vector potential
init_vpot2field_user(t, A,prim,pstag,ucons,Bhat); // t is ok since always t=tstart
return(0);
}
// get A_i in PRIMECOORDS for FLUXB==FLUXCTSTAG at CORNi for each A_i, the natural staggered field locations for A_i (i.e. not all same location for all A_i)
int get_vpot_fluxctstag_primecoords(SFTYPE time, int i, int j, int k, FTYPE (*prim)[NSTORE2][NSTORE3][NPR], FTYPE *vpot)
{
int dir;
FTYPE X[NDIM],V[NDIM];
int loc;
FTYPE vpotuser[NDIM];
int ii,jj,kk;
int whichcoord;
int userdir;
// copied from init_vpot_justAcov()
// loop over A_l's
DIMENLOOP(dir){
if(dir==1) loc=CORN1;
else if(dir==2) loc=CORN2;
else if(dir==3) loc=CORN3;
bl_coord_ijk_2(i, j, k, loc, X, V); // face[odir2] and flux[odir2]
// dxdxprim_ijk(i, j, k, loc, dxdxp);
// get user vpot in user coordinates (assume same coordinates for all A_{userdir}
DLOOPA(userdir){
init_vpot_user(&whichcoord, userdir, time, i,j,k, loc, prim, V, &vpotuser[userdir]);
}
// convert from user coordinate to PRIMECOORDS
ucov_whichcoord2primecoords(whichcoord, i, j, k, loc, vpotuser);
// now copy the only component required
vpot[dir]=vpotuser[dir];
}// loop over A_i
return(0);
}
// initialize vector potential given user function
// assumes normal non-staggered field in pr
int init_vpot_justAcov(SFTYPE time, FTYPE (*prim)[NSTORE2][NSTORE3][NPR], FTYPE (*A)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3])
{
int Nvec[NDIM];
int odir1[NDIM],odir2[NDIM];
int numdirs;
int locvpot[NDIM][NDIM];
int dir;
Nvec[0]=0;
Nvec[1]=N1;
Nvec[2]=N2;
Nvec[3]=N3;
// get location of vpot and number of locations to set
DIMENLOOP(dir){
set_location_fluxasemforvpot(dir, &numdirs, &odir1[dir], &odir2[dir], locvpot[dir]);
// these quantities are associated with a single choice for relationship between A_i and F_j[B_k]
// get_fluxpldirs(Nvec, dir, &fluxdirlist[dir], &pldirlist[dir], &plforfluxlist[dir], &signforfluxlist[dir]);
}
if(numdirs==2){
// will be done in another function
}
else{
trifprintf("Initialize field from vector potential (really A)\n");
// DIMENLOOP(dir) COMPFULLLOOPP1 MACP1A0(A,dir,i,j,k) = 0.;
DIMENLOOP(dir) init_3dvpot_fullloopp1(0.0,A[dir]);
#if(TRACKVPOT)
init_3dvpot_fullloopp1(0.0,A[TT]);
#endif
}
trifprintf("Initialize field from vector potential assign\n");
// GODMARK: Caution: Possible to use quantity off grid
// (e.g. density) to define lower corner value of A, which then
// defines B at center for lower cells.
// Do have *grid* quantities for everywhre A is.
//////////// COMPFULLLOOPP1{
#pragma omp parallel private(dir) OPENMPGLOBALPRIVATEFULL // user could do anything
{
FTYPE X[NDIM],V[NDIM];
FTYPE dxdxp[NDIM][NDIM];
int otherdir;
FTYPE vpotuser[NDIM];
int userdir;
int whichcoord;
int loc;
int i,j,k,pl,pliter;
// int fluxdir,pldir,plforflux;
// int fluxdirlist[NDIM],pldirlist[NDIM],plforfluxlist[NDIM];
// FTYPE signforfluxlist[NDIM];
// FTYPE signforflux;
OPENMP3DLOOPVARSDEFINE; OPENMP3DLOOPSETUPFULLP1;
#pragma omp for schedule(OPENMPSCHEDULE(),OPENMPCHUNKSIZE(blocksize))
OPENMP3DLOOPBLOCK{
OPENMP3DLOOPBLOCK2IJK(i,j,k);
// can't define F1/F2/F3 in outermost part ofCOMPFULLLOOPP1 since don't exist there
// can only define as if doing COMPFULLLOOP
if(numdirs==2 && (i>OUTFULL1 || j>OUTFULL2 || k>OUTFULL3) ) continue;
// loop over A_l's
DIMENLOOP(dir){
// these quantities are associated with a single choice for relationship between A_i and F_j[B_k]
// fluxdir=fluxdirlist[dir];
// pldir=pldirlist[dir];
// plforflux=plforfluxlist[dir];
// signforflux=signforfluxlist[dir];
// loop over positions to get vector potential
for(otherdir=1;otherdir<=numdirs;otherdir++){
if(otherdir==1){
loc=locvpot[dir][odir1[dir]];
}
else if(otherdir==2){
loc=locvpot[dir][odir2[dir]];
}
bl_coord_ijk_2(i, j, k, loc, X, V); // face[odir2] and flux[odir2]
// dxdxprim_ijk(i, j, k, loc, dxdxp);
// get user vpot in user coordinates (assume same coordinates for all A_{userdir}
DLOOPA(userdir){
init_vpot_user(&whichcoord, userdir, time, i,j,k, loc, prim, V, &vpotuser[userdir]);
}
// convert from user coordinate to PRIMECOORDS
ucov_whichcoord2primecoords(whichcoord, i, j, k, loc, vpotuser);
// for numdirs=anything (used for input for 2Flux and for 2Flux required if doing TRACKVPOT
MACP1A0(A,dir,i,j,k) = vpotuser[dir];
}// end for loop over otherdirs
}// end over A_i
}// end over i,j,k
}// end parallel region
return(0);
}
// initialize vector potential given user function
// assumes normal non-staggered field in pr
// Notice that if using F (flux), then *location* can be different for (e.g.) F1[B2] and F2[B1] while if using A_3 then no choice in varying positions
int init_vpot_toF(FTYPE (*A)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3], FTYPE (*F1)[NSTORE2][NSTORE3][NPR], FTYPE (*F2)[NSTORE2][NSTORE3][NPR], FTYPE (*F3)[NSTORE2][NSTORE3][NPR])
{
int Nvec[NDIM];
FTYPE (*fluxvec[NDIM])[NSTORE2][NSTORE3][NPR];
int odir1[NDIM],odir2[NDIM];
int numdirs;
int locvpot[NDIM][NDIM];
int dir;
Nvec[0]=0;
Nvec[1]=N1;
Nvec[2]=N2;
Nvec[3]=N3;
fluxvec[1] = F1;
fluxvec[2] = F2;
fluxvec[3] = F3;
// get location of vpot and number of locations to set
DIMENLOOP(dir){
set_location_fluxasemforvpot(dir, &numdirs, &odir1[dir], &odir2[dir], locvpot[dir]);
// these quantities are associated with a single choice for relationship between A_i and F_j[B_k]
// get_fluxpldirs(Nvec, dir, &fluxdirlist[dir], &pldirlist[dir], &plforfluxlist[dir], &signforfluxlist[dir]);
}
if(numdirs==2){
// then fill F1/F2/F3 instead of A[]
trifprintf("Initialize field from vector potential (really flux)\n");
DIMENLOOP(dir){
if(Nvec[dir]>1){
init_3dnpr_fullloop(0.0,fluxvec[dir]);
}
}
}
else{
// then A already initialized
}
trifprintf("Initialize field from vector potential assign\n");
// GODMARK: Caution: Possible to use quantity off grid
// (e.g. density) to define lower corner value of A, which then
// defines B at center for lower cells.
// Do have *grid* quantities for everywhre A is.
//////////// COMPFULLLOOPP1{
#pragma omp parallel private(dir) OPENMPGLOBALPRIVATEFULL // user could do anything
{
int otherdir;
int userdir;
int whichcoord;
int loc;
int i,j,k,pl,pliter;
// int fluxdir,pldir,plforflux;
// int fluxdirlist[NDIM],pldirlist[NDIM],plforfluxlist[NDIM];
// FTYPE signforfluxlist[NDIM];
// FTYPE signforflux;
OPENMP3DLOOPVARSDEFINE; OPENMP3DLOOPSETUPFULLP1;
#pragma omp for schedule(OPENMPSCHEDULE(),OPENMPCHUNKSIZE(blocksize))
OPENMP3DLOOPBLOCK{
OPENMP3DLOOPBLOCK2IJK(i,j,k);
// can't define F1/F2/F3 in outermost part ofCOMPFULLLOOPP1 since don't exist there
// can only define as if doing COMPFULLLOOP
if(numdirs==2 && (i>OUTFULL1 || j>OUTFULL2 || k>OUTFULL3) ) continue;
// loop over A_l's
DIMENLOOP(dir){
// these quantities are associated with a single choice for relationship between A_i and F_j[B_k]
// fluxdir=fluxdirlist[dir];
// pldir=pldirlist[dir];
// plforflux=plforfluxlist[dir];
// signforflux=signforfluxlist[dir];
// loop over positions to get vector potential
for(otherdir=1;otherdir<=numdirs;otherdir++){
if(otherdir==1){
loc=locvpot[dir][odir1[dir]];
}
else if(otherdir==2){
loc=locvpot[dir][odir2[dir]];
}
// normal ordering is A[dir] = fluxvec[fluxdir][plforflux] with fluxdir=odir1 and plforflux from odir2
if(numdirs==2){
// then using F1/F2/F3
// Notice that fluxvec here has no \detg in it, as consistent with taking B = curlA ($\detg B^i = d_j A_k \epsilon^{ijk}$) when used
// Notice that both fluxes are assigned a value in some cases, as required
// e.g. F1[B2]=A3 and F2[B1]=-A3
if(otherdir==1 && Nvec[odir1[dir]]>1) MACP1A1(fluxvec,odir1[dir],i,j,k,B1-1+odir2[dir])=MACP1A0(A,dir,i,j,k);
if(otherdir==2 && Nvec[odir2[dir]]>1) MACP1A1(fluxvec,odir2[dir],i,j,k,B1-1+odir1[dir])=-MACP1A0(A,dir,i,j,k); // opposite ordering
}
else{
// then already assigned to A
}
}// end for loop over otherdirs
}// end over A_i
}// end over i,j,k
}// end parallel region
return(0);
}
// copy A_i to F_i[B1..B3] -- for use with some field evolution methods during time evolution
// Note can't just use this in init_vpot() since there F's normally associated with a single A_i might have different positions (e.g. FLUXCTHLL method)
int copy_vpot2flux(FTYPE (*A)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3], FTYPE (*F1)[NSTORE2][NSTORE3][NPR], FTYPE (*F2)[NSTORE2][NSTORE3][NPR], FTYPE (*F3)[NSTORE2][NSTORE3][NPR])
{
FTYPE (*fluxvec[NDIM])[NSTORE2][NSTORE3][NPR];
int Nvec[NDIM];
int odir1[NDIM],odir2[NDIM];
int numdirs;
int locvpot[NDIM][NDIM];
int dir;
Nvec[0]=0;
Nvec[1]=N1;
Nvec[2]=N2;
Nvec[3]=N3;
fluxvec[1] = F1;
fluxvec[2] = F2;
fluxvec[3] = F3;
// get location of vpot and number of locations to set
DIMENLOOP(dir){
set_location_fluxasemforvpot(dir, &numdirs, &odir1[dir], &odir2[dir], locvpot[dir]);
}
if(numdirs==2){
// 0-out flux (F1/F2/F3)
DIMENLOOP(dir){
if(Nvec[dir]>1){
init_3dnpr_fullloop(0.0,fluxvec[dir]);
}
}
// can't define F1/F2/F3 in outermost part of COMPFULLLOOPP1 since don't exist there
// can only define as if doing FULLLOOP
////// COMPFULLLOOP{
#pragma omp parallel private(dir) // no copyin since no use of non-array globals (yet)
{
int otherdir;
int i,j,k;
OPENMP3DLOOPVARSDEFINE; OPENMP3DLOOPSETUPFULL;
#pragma omp for schedule(OPENMPSCHEDULE(),OPENMPCHUNKSIZE(blocksize))
OPENMP3DLOOPBLOCK{
OPENMP3DLOOPBLOCK2IJK(i,j,k);
DIMENLOOP(dir){
// loop over positions to get vector potential
for(otherdir=1;otherdir<=numdirs;otherdir++){
// then using F1/F2/F3
// Notice that fluxvec here has no \detg in it, as consistent with taking B = curlA ($\detg B^i = d_j A_k \epsilon^{ijk}$) when used
// Notice that both fluxes are assigned a value in some cases, as required
if(otherdir==1 && Nvec[odir1[dir]]>1) MACP1A1(fluxvec,odir1[dir],i,j,k,B1-1+odir2[dir])=MACP1A0(A,dir,i,j,k);
if(otherdir==2 && Nvec[odir2[dir]]>1) MACP1A1(fluxvec,odir2[dir],i,j,k,B1-1+odir1[dir])=-MACP1A0(A,dir,i,j,k); // opposite ordering
}// end for loop over otherdirs
}// end over A_i
}// end over i,j,k
}//end if parallel region
}// end if numdirs==2 (and so actually need to copy vpot to flux)
return(0);
}
// once this function is done, ensure obtain higher-order with test=1102, etc.
// GODMARK: what about fields in direction with no dimension, like B3 in 2D? Can't obtain B3 from A_i??
// Seems fine B3=A2,1 +-A1,2
// What about B1,B2? Only needs A_3 since don't need B1=A_2,3 and B2=A_1,3 ? Can I assume those are zero?
// In 1D only have 0=A1,1 B3=A2,1 and B2=A3,1, so need to be able to obtain B1 somehow -- or just don't change B1 since must be constant in time
// Note if FLUXCTHLL or FLUXCTTOTH, then no single A_i is evolved, so can't evolve with A_i in that case since field is determined by 2 different versions of A_i that we don't track (nor should!)
int evolve_withvpot(SFTYPE time, FTYPE (*prim)[NSTORE2][NSTORE3][NPR],FTYPE (*pstag)[NSTORE2][NSTORE3][NPR],FTYPE (*unew)[NSTORE2][NSTORE3][NPR],FTYPE (*vpot)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3],FTYPE (*Bhat)[NSTORE2][NSTORE3][NPR], FTYPE (*F1)[NSTORE2][NSTORE3][NPR], FTYPE (*F2)[NSTORE2][NSTORE3][NPR], FTYPE (*F3)[NSTORE2][NSTORE3][NPR], FTYPE (*Atemp)[NSTORE1+SHIFTSTORE1][NSTORE2+SHIFTSTORE2][NSTORE3+SHIFTSTORE3], FTYPE (*uconstemp)[NSTORE2][NSTORE3][NPR])
{
if(EVOLVEWITHVPOT==0){
}
else{
// 1) fill F or vpot(A,prim);
// 2) call vpot2field()
// perhaps split init_vpot so can be used for this function too
// **TODO** essentially need a separate function that takes point A_i and fills F if needed rather than all at once as in init_vpot()
// only copies if necessary for how method is setup (e.g. staggered point evolution method)
// i.e. F is not really flux here, just place-holder for A_i at different positions as required by some other methods, such as FLUXCTTOTH or FLUXCTSTAG w/ higher-order corrections.
copy_vpot2flux(vpot,F1,F2,F3);
// Note that vpot2field bounds pstag and input prim as required since A_i doesn't exist everywhere
// GODMARK: use of many globals: ok for now since evolve_withvpot() not used for any other purpose
vpot2field(time, vpot,prim, pstag, unew, Bhat,F1,F2,F3,Atemp,uconstemp);
#if(0)
// DEBUG: Trying to get test=1102 to work with EVOLVEWITHVPOT (also turned on bound_flux_fluxrecon() in update_vpot() so E_i fully periodic in BZs before A_i iterated
bound_pstag(STAGEM1,t,prim, pstag, unew);
bound_prim(STAGEM1,t,prim, pstag, unew);
bound_uavg(STAGEM1,t,prim, pstag, unew);
#endif
}
return(0);
}
// find divb
// if higher order method, then must use conserved value U[] assumed to then exist and be used for field
void setfdivb(FTYPE *divb, FTYPE (*p)[NSTORE2][NSTORE3][NPR], FTYPE (*pstag)[NSTORE2][NSTORE3][NPR], FTYPE (*U)[NSTORE2][NSTORE3][NPR], FTYPE (*Bhat)[NSTORE2][NSTORE3][NPR], int i, int j, int k)
{
if((FLUXB==ATHENA1)||(FLUXB==ATHENA2)||(FLUXB==FLUXCTTOTH)){
if(DOENOFLUX==NOENOFLUX){ SETFDIVBFLUXCTTOTHPRIM((*divb),p,i,j,k);}
else{ SETFDIVBFLUXCTTOTH((*divb),U,i,j,k);}
}
else if(FLUXB==FLUXCD){
if(DOENOFLUX==NOENOFLUX){ SETFDIVBFLUXCDPRIM((*divb),p,i,j,k);}
else{ SETFDIVBFLUXCD((*divb),U,i,j,k);}
}
else if(FLUXB==FLUXCTHLL){
if(DOENOFLUX==NOENOFLUX){ SETFDIVBFLUXCTTOTHPRIM((*divb),p,i,j,k);} // fake
else{ SETFDIVBFLUXCTTOTH((*divb),U,i,j,k);} // fake
}
else if(FLUXB==FLUXCTSTAG){
// for STAG, always can use U since always used, but can use pstag too for lower order method
//if(DOENOFLUX==NOENOFLUX) SETFDIVBFLUXCTSTAG((*divb),pstag,i,j,k);
if(DOENOFLUX==ENOFLUXRECON && extrazones4emf==0){
// evolving point field but need Bhat to check divb=0
// apparently must use fixed stencil so could compute when divb requested instead of always
// now compute divBhat that should be 0 to machine accuracy
SETFDIVBFLUXCTSTAG((*divb),Bhat,i,j,k);
}
else{
if(DOENOFLUX==NOENOFLUX){
// then can use pstag itself that is always bounded properly
SETFDIVBFLUXCTSTAGPRIM((*divb),pstag,i,j,k);
}
else{
// then must use higher-order version
// for diagnostics in MPI can bound U before dumping, but should only bound MPI since no treatment of U for real boundaries
SETFDIVBFLUXCTSTAG((*divb),U,i,j,k);
}
}
// *divb =
// +(MACP0A1(U,ip1mac(i),j,k,B1)-MACP0A1(U,i,j,k,B1))/dx[1]
// +(MACP0A1(U,i,jp1mac(j),k,B2)-MACP0A1(U,i,j,k,B2))/dx[2]
// +(MACP0A1(U,i,j,kp1mac(k),B3)-MACP0A1(U,i,j,k,B3))/dx[3]
// ;
}
else{
dualfprintf(fail_file,"No such FLUXB==%d in setfdivb()\n",FLUXB);
myexit(817163);
}
}
// Used with FLUXB==FLUXCTHLL
// actually uses F1/F2/F3 instead of inputted A[]
// When using FLUXCTHLL, doesn't preserve divb, but gives correct CENT position of field given face vector potential
// If flux is really vector potential at corners and vector potential is direction-dependent quasi-deaveraged version
int vpot2field_useflux(int *fieldloc,FTYPE (*pfield)[NSTORE2][NSTORE3][NPR],FTYPE (*ufield)[NSTORE2][NSTORE3][NPR], FTYPE (*F1)[NSTORE2][NSTORE3][NPR], FTYPE (*F2)[NSTORE2][NSTORE3][NPR], FTYPE (*F3)[NSTORE2][NSTORE3][NPR])
{
int Nvec[NDIM];
FTYPE (*fluxvec[NDIM])[NSTORE2][NSTORE3][NPR];
Nvec[0]=0;
Nvec[1]=N1;
Nvec[2]=N2;
Nvec[3]=N3;
fluxvec[1] = F1;
fluxvec[2] = F2;
fluxvec[3] = F3;
// Assumes defined vector potential with signature such that
// B = +curlA
// \detg B^i = +d_j ([tijk] A_k)
// and HARM conventions for what F1/F2/F3 mean in terms of EMF-like object
// Note that dB/dt = -curlE has opposite sign prefactor that is folded into E when doing flux calculation, so flux is really -E
// So when letting dA/dt = -E = +emf = +flux with consistent cyclic indices
#pragma omp parallel
{
int i,j,k;
FTYPE igdetgnosing[NDIM];
int dir;
int pl,pliter;
int odir1,odir2;
int jj;
struct of_geom geomfdontuse[NDIM];
struct of_geom *ptrgeomf[NDIM];
OPENMP3DLOOPVARSDEFINE; OPENMP3DLOOPSETUPFULL; // doesn't depend upon dir, but blockijk must be private, so put in parallel loop
// assign memory
DLOOPA(jj) ptrgeomf[jj]=&(geomfdontuse[jj]);
/////////////////
//
// A_1
//
/////////////////
dir=1;
get_odirs(dir,&odir1,&odir2);
if(!(Nvec[odir1]==1 && Nvec[odir2]==1)){
//////// COMPFULLLOOP{ // COMPFULLLOOP allows since A_i exists atCOMPFULLLOOPP1 and so always accessing valid A_i
#pragma omp for schedule(OPENMPSCHEDULE(),OPENMPCHUNKSIZE(blocksize)) nowait // no wait allowed as in fluxct.c
OPENMP3DLOOPBLOCK{
OPENMP3DLOOPBLOCK2IJK(i,j,k);
// ufield doesn't require geometry
// myA[3]=-F2[B1]
// myA[2]=F3[B1];
MACP0A1(ufield,i,j,k,B1) = 0.0;
#if(N2>1)
MACP0A1(ufield,i,j,k,B1) += -(MACP0A1(F2,i,jp1mac(j),k,B1)-MACP0A1(F2,i,j,k,B1))/(dx[2]);
#endif
#if(N3>1)
MACP0A1(ufield,i,j,k,B1) += -(MACP0A1(F3,i,j,kp1mac(k),B1)-MACP0A1(F3,i,j,k,B1))/(dx[3]);
#endif
get_geometry(i, j, k, fieldloc[dir], ptrgeomf[dir]);
igdetgnosing[dir] = sign(ptrgeomf[dir]->gdet)/(fabs(ptrgeomf[dir]->gdet)+SMALL); // avoids 0.0 for any sign of ptrgeom->gdet
MACP0A1(pfield,i,j,k,B1-1+dir) = MACP0A1(ufield,i,j,k,B1-1+dir)*igdetgnosing[dir];
}// end 3D LOOP
}// end if doing A1
/////////////////
//
// A_2
//
/////////////////