-
Notifications
You must be signed in to change notification settings - Fork 9
/
silo.c
2113 lines (1773 loc) · 56.7 KB
/
silo.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 silo.c
\brief Routines for SILO output files
*/
#ifndef NOSILO
//KORAL - silo.c
//routines for writing a silo file with quadratic mesh
//used both on the go and separately
#include "ko.h"
#include <silo.h>
#include <string.h>
/*********************************************/
/* writes silo file in dumps */
/*********************************************/
int fprint_silofile(ldouble time, int num, char* folder, char* prefix)
{
char bufor[50];
sprintf(bufor,"%s/%s%04d.silo",folder,prefix,num);
mpi_exchangedata();
calc_avgs_throughout();
DBfile *file = NULL; // The Silo file pointer
char *coordnames[3]; // Names of the coordinates
ldouble *nodex; // The coordinate arrays
ldouble *nodey;
ldouble *nodez;
ldouble *coordinates[3];// The array of coordinatearrays
int dimensions[3]; // The number of nodes
// Create the Silo file
file = DBCreate(bufor, DB_CLOBBER, DB_LOCAL, NULL,DB_PDB);
// Name the coordinate axes ‘X’ and ‘Y’
coordnames[0] = strdup("X");
coordnames[1] = strdup("Y");
coordnames[2] = strdup("Z");
// Give the cartesian coordinates of the mesh
int ix,iy,iz,iv,imx,imy,imz;
int i,j;
ldouble pp[NV],uu[NV],xxvec[4],xxveccar[4],xxvecsph[4],xx1[4],xx2[4];
//number of zones
int nx=NX;
int ny=NY;
int nz=NZ;
//number of nodes
int nnx=NX+1;
int nny=NY+1;
int nnz=NZ+1;
if(NY==1) nny=NY;
if(NZ==1) nnz=NZ;
#ifdef FULLPHI //printing one more cell in phi to close the sphere
nz++;nnz++;
#endif
// Allocate arrays for saved quantities
nodex=(ldouble *)malloc(nnx*nny*nnz*sizeof(ldouble));
nodey=(ldouble *)malloc(nnx*nny*nnz*sizeof(ldouble));
nodez=(ldouble *)malloc(nnx*nny*nnz*sizeof(ldouble));
ldouble *rho = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *entr = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *uint = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *temp = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Omega = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *muBe = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Be = (ldouble*)malloc(nx*ny*nz*sizeof(double));
int *entropyinv = (int*)malloc(nx*ny*nz*sizeof(int));
ldouble *expansion = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *NHr = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *entrlnT = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *u0 = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *u1 = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *u2 = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *u3 = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *lorentz = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *lorentz_perp = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *lorentz_par = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *ffinv = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *vx = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *vy = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *vz = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Edotx = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Edoty = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Edotz = (ldouble*)malloc(nx*ny*nz*sizeof(double));
#ifdef MAGNFIELD
ldouble *Bangle = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *bsq = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *B1comp = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *B2comp = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *B3comp = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Bx = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *By = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Bz = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *phi = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *phi_accum = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *beta = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *sigma = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *sigmaw = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *betainv = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *divB = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *jdens = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *OmegaF = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Qtheta = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Qphi = (ldouble*)malloc(nx*ny*nz*sizeof(double));
#ifdef BATTERY
ldouble *dBxdtbat = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *dBydtbat = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *dBzdtbat = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *phibat = (ldouble*)malloc(nx*ny*nz*sizeof(double));
#endif
#ifdef MIMICDYNAMO
if(doingpostproc)
{
set_bc(time,0);
mimic_dynamo(1.);
}
ldouble *Bxdyn = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Bydyn = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Bzdyn = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *phidyn = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Avecdyn = (ldouble*)malloc(nx*ny*nz*sizeof(double));
#endif
#endif //MAGNFIELD
#ifdef PRINTVISCHEATINGTOSILO
ldouble *dtauarr= (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *vischeat= (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *vischeatnege= (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *vischeatnegi= (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *deltae= (ldouble*)malloc(nx*ny*nz*sizeof(double));
#endif
#ifdef PRINTCOULOMBTOSILO
ldouble *coulomb = (ldouble*)malloc(nx*ny*nz*sizeof(double));
#endif
#ifdef PRINTGAMMATOSILO
ldouble *gammag = (ldouble*)malloc(nx*ny*nz*sizeof(double));
#endif
#ifdef EVOLVEELECTRONS
ldouble *tempe = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *tempi = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *ue = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *ui = (ldouble*)malloc(nx*ny*nz*sizeof(double));
#endif
#ifdef RELELECTRONS
ldouble *gammabrk = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *G0relel = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *G0icrelel = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *G0synrelel = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *urelel = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *uratio_tot = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *uratio_th = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *nrelel = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *neth = (ldouble*)malloc(nx*ny*nz*sizeof(double));
int ie;
ldouble gammapbrk[NRELBIN]; //shared gammabreak array for nonthermal electrons
for(ie=0; ie<NRELBIN; ie++) gammapbrk[ie] = pow(relel_gammas[ie], RELEL_HEAT_INDEX + 0.5);
#endif
#ifdef RADIATION
ldouble *trad = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *tradlte = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *taucoupling = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *tausca = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *tauabs = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *taueff = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Erad = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Ehat = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Fx = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Fy = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Fz = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Nph = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *uradx = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *urady = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *uradz = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *Gtff = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *G0icth = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *tauscar = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *tauabsr = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *taueffr = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *tauscar2 = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *tauabsr2 = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *taueffr2 = (ldouble*)malloc(nx*ny*nz*sizeof(double));
ldouble *entrrad = (ldouble*)malloc(nx*ny*nz*sizeof(double));
#endif //RADIATION
//first fill coordinates on nodes
#pragma omp parallel for private(ix,iy,iz,iv,imx,imy,imz,i,j,pp,uu,xxvec,xxveccar,xxvecsph,xx1,xx2) schedule (static)
#ifdef PRINTZGC_RIGHT
for(iz=NG;iz<=nz+NG;iz++)
#else
for(iz=0;iz<=nz;iz++)
#endif
{
#ifdef PRINTYGC_RIGHT
for(iy=NG;iy<=ny+NG;iy++)
#else
for(iy=0;iy<=ny;iy++)
#endif
{
#if defined(PRINTXGC_RIGHT)
for(ix=NG;ix<=nx+NG;ix++)
#elif defined(PRINTXGC_LEFT)
for(ix=-NG;ix<=nx-NG;ix++)
#else
for(ix=0;ix<=nx;ix++)
#endif
{
if(NZ==1 && iz>0) continue;
if(NY==1 && iy>0) continue;
int iix,iiy,iiz;
iix=ix;
iiy=iy;
iiz=iz;
if(NZ>1 && NY>1)
{
xxvec[0]=0.;xxvec[1]=get_xb(iix,0);xxvec[2]=get_xb(iiy,1);xxvec[3]=get_xb(iiz,2);
}
else if(NZ==1 && NY==1)
{
xxvec[0]=0.;xxvec[1]=get_xb(iix,0);xxvec[2]=get_x(iiy,1);xxvec[3]=get_x(iiz,2);
}
else if(NZ==1)
{
xxvec[0]=0.;xxvec[1]=get_xb(iix,0);xxvec[2]=get_xb(iiy,1);xxvec[3]=get_x(iiz,2);
}
else if(NY==1)
{
xxvec[0]=0.;xxvec[1]=get_xb(iix,0);xxvec[2]=get_x(iiy,1);xxvec[3]=get_xb(iiz,2);
}
coco_N(xxvec,xxvecsph,MYCOORDS,SPHCOORDS);
coco_N(xxvec,xxveccar,MYCOORDS,MINKCOORDS);
imz=iz;
imy=iy;
imx=ix;
#ifdef PRINTZGC_RIGHT
imz=iz-NG;
#endif
#ifdef PRINTXGC_RIGHT
imx=ix-NG;
#endif
#ifdef PRINTXGC_LEFT
imx=ix+NG;
#endif
#ifdef PRINTYGC_RIGHT
imy=iy-NG;
#endif
int nodalindex=imz*(nny*nnx) + imy*nnx + imx;
//coordinates
ldouble coordscale=1.;
#ifdef COORDSINPCINSILO
coordscale=1./(PARSECCGS/MASSCM);
#endif
#if(SILOCOORDS==SPHCOORDS)
nodex[nodalindex]=xxvecsph[1]*coordscale;
nodey[nodalindex]=xxvecsph[2]*coordscale;
nodez[nodalindex]=xxvecsph[3]*coordscale;
#else
nodex[nodalindex]=xxveccar[1]*coordscale;
nodey[nodalindex]=xxveccar[2]*coordscale;
nodez[nodalindex]=xxveccar[3]*coordscale;
#endif
}
}
}
//then fill the zones with values
#pragma omp parallel for private(ix,iy,iz,iv,imx,imy,imz,i,j,pp,uu,xxvec,xxveccar,xxvecsph,xx1,xx2) schedule (static)
#ifdef PRINTZGC_RIGHT
for(iz=NG;iz<nz+NG;iz++)
#else
for(iz=0;iz<nz;iz++)
#endif
{
#ifdef PRINTYGC_RIGHT
for(iy=NG;iy<ny+NG;iy++)
#else
for(iy=0;iy<ny;iy++)
#endif
{
#if defined(PRINTXGC_RIGHT)
for(ix=NG;ix<nx+NG;ix++)
#elif defined(PRINTXGC_LEFT)
for(ix=-NG;ix<nx-NG;ix++)
#else
for(ix=0;ix<nx;ix++)
#endif
{
int iix,iiy,iiz;
iix=ix;
iiy=iy;
iiz=iz;
struct geometry geom;
fill_geometry(iix,iiy,iiz,&geom);
struct geometry geomout;
fill_geometry_arb(iix,iiy,iiz,&geomout,OUTCOORDS);
//cell dimensions
ldouble dxph[3],dx[3];
get_cellsize_out(ix, iy, iz, dx);
dxph[0]=dx[0]*sqrt(geomout.gg[1][1]);
dxph[1]=dx[1]*sqrt(geomout.gg[2][2]);
dxph[2]=dx[2]*sqrt(geomout.gg[3][3]);
// cell coordinates
get_xx(iix,iiy,iiz,xxvec);
coco_N(xxvec,xxvecsph,MYCOORDS,SPHCOORDS);
coco_N(xxvec,xxveccar,MYCOORDS,MINKCOORDS);
ldouble r=xxvecsph[1];
ldouble th=xxvecsph[2];
ldouble ph=xxvecsph[3];
// metric determinant
ldouble gdet,gdetu;
gdet=geomout.gdet;
gdetu=gdet;
#if (GDETIN==0) //gdet out of derivatives
gdetu=1.;
#endif
// coordinates and gdet of cells +/- 1 in radius
ldouble gdet1,gdet2;
#ifdef PRECOMPUTE_MY2OUT
get_xxout(ix-1, iiy, iiz, xx1);
get_xxout(ix+1, iiy, iiz, xx2);
#else
get_xx(iix-1,iiy,iiz,xx1);
get_xx(iix+1,iiy,iiz,xx2);
coco_N(xx1,xx1,MYCOORDS,OUTCOORDS);
coco_N(xx2,xx2,MYCOORDS,OUTCOORDS);
#endif
gdet1=calc_gdet_arb(xx1,OUTCOORDS);
gdet2=calc_gdet_arb(xx2,OUTCOORDS);
// compute zonal index
imz=iz;
imy=iy;
imx=ix;
#ifdef PRINTZGC_RIGHT
imz=iz-NG;
#endif
#ifdef PRINTXGC_RIGHT
imx=ix-NG;
#endif
#ifdef PRINTXGC_LEFT
imx=ix+NG;
#endif
#ifdef PRINTYGC_RIGHT
imy=iy-NG;
#endif
int zonalindex=imz*(ny*nx) + imy*nx + imx;
// copy over primitives
for(iv=0;iv<NV;iv++)
{
if(doingavg)
pp[iv]=get_uavg(pavg,iv,ix,iy,iz);
else
pp[iv]=get_u(p,iv,iix,iiy,iiz);
}
// get (or recompute) gamma
ldouble gamma=GAMMA;
#ifdef CONSISTENTGAMMA
#ifdef EVOLVEELECTRONS
gamma=calc_gammagas(pp,ix,iy,iz);
set_u_scalar(gammagas,ix,iy,iz,gamma);
#endif
gamma=pick_gammagas(ix,iy,iz);
#endif
// gas expansion
ldouble exploc=0.;
int derdir[3] = {0,0,0};
ldouble shear[4][4];
calc_shear_lab(pp, &geom, shear, &exploc, MHD, derdir);
//primitives to OUTCOORDS
#ifdef PRECOMPUTE_MY2OUT
trans_pall_coco_my2out(pp,pp,&geom,&geomout);
#else
trans_pall_coco(pp, pp, MYCOORDS,OUTCOORDS, xxvec,&geom,&geomout);
#endif
// entropy inversion flag
entropyinv[zonalindex]=get_cflag(ENTROPYFLAG3,ix,iy,iz);
// other quantities
ldouble vel[4],vcov[4],vcon[4],velprim[4];
ldouble Tit[4],Tij[4][4];
ldouble rhouconr,rhoucont;
ldouble tempeloc,tempiloc;
ldouble pregas,premag,ueloc,uiloc;
ldouble nethloc,nrelelloc,G0relelloc,G0ic_relel_loc,G0syn_relel_loc,G0ic_th_loc,urelelloc,gbrkloc;
ldouble bcon[4],bcov[4];
if(doingavg==0) //using snapshot data
{
rho[zonalindex] = pp[RHO];
entr[zonalindex] = pp[ENTR];
uint[zonalindex] = pp[UU];
pregas = (gamma-1.)*pp[UU];
vel[0] = 0.;
vel[1]=pp[VX];
vel[2]=pp[VY];
vel[3]=pp[VZ];
conv_vels(vel,vel,VELPRIM,VEL4,geomout.gg,geomout.GG);
for(i=0;i<4;i++) vcon[i]=vel[i];
indices_21(vcon,vcov,geomout.gg);
rhouconr = rho[zonalindex]*vcon[1];
rhoucont = rho[zonalindex]*vcon[0];
// angular velocity
Omega[zonalindex]=vel[3]/vel[0];
#ifdef MAGNFIELD
B1comp[zonalindex] = pp[B1];
B2comp[zonalindex] = pp[B2];
B3comp[zonalindex] = pp[B3];
calc_bcon_prim(pp,bcon,&geomout);
indices_21(bcon,bcov,geomout.gg);
bsq[zonalindex] = dotB(bcon,bcov);
premag = 0.5*bsq[zonalindex];
beta[zonalindex] = pregas/premag;
betainv[zonalindex] = 1./beta[zonalindex];
sigma[zonalindex] = bsq[zonalindex]/(rho[zonalindex]);
sigmaw[zonalindex] = bsq[zonalindex]/(rho[zonalindex] + gamma*uint[zonalindex]);
#endif
// stress energy
calc_Tij(pp,&geomout,Tij);
indices_2221(Tij,Tij,geomout.gg);
Tit[1]=Tij[1][0];
Tit[2]=Tij[2][0];
Tit[3]=Tij[3][0];
// Bernoulli flux & Bernoulli number
muBe[zonalindex] = -(Tij[1][0] + rhouconr)/rhouconr;
Be[zonalindex] = -(Tij[0][0] + rhoucont)/rhoucont;
#ifdef MAGNFIELD
Qtheta[zonalindex]=2.*M_PI/Omega[zonalindex]/dx[1]*fabs(bcon[2])/sqrt(rho[zonalindex]);
Qphi[zonalindex]=2.*M_PI/Omega[zonalindex]/dx[2]*fabs(bcon[3])/sqrt(rho[zonalindex]);
#ifdef SHEARINGBOX
Qtheta[zonalindex]=2.*M_PI/SHEAROM/dx[2]*fabs(bcon[3])/sqrt(rho[zonalindex]);
Qphi[zonalindex]=2.*M_PI/SHEAROM/dx[1]*fabs(bcon[2])/sqrt(rho[zonalindex]);
#endif
//to calculate magn. field angle
ldouble brbphi,bsq,bfake[4];
#ifdef BHDISK_PROBLEMTYPE
calc_angle_brbphibsq(ix,iy,iz,&brbphi,&bsq,bfake,bfake);
Bangle[zonalindex]=-brbphi/bsq;
#else
Bangle[zonalindex]=-1.;
#endif
if(ix==0 || (NY>1 && iy==0) || (NZ>1 && iz==0)) //divB left-biased
divB[zonalindex]=0;
else
divB[zonalindex]=calc_divB(ix,iy,iz);
#endif //MAGNFIELD
#ifdef RADIATION
taucoupling[zonalindex]=estimate_gas_radiation_coupling(pp,&geomout);
#endif //RADIATION
// electrons & ions
ueloc=uiloc=0.;
#ifdef EVOLVEELECTRONS
// temperatures
calc_PEQ_Teifrompp(pp,&tempeloc,&tempiloc,ix,iy,iz);
// electrons
ldouble ne=calc_thermal_ne(pp);
ldouble pe=K_BOLTZ*ne*tempeloc;
ldouble gammae=GAMMAE;
#ifdef CONSISTENTGAMMA
#ifndef FIXEDGAMMASPECIES
gammae=calc_gammaintfromtemp(tempeloc,ELECTRONS);
#endif
#endif
ueloc=pe/(gammae-1.);
//ions
ldouble ni=pp[RHO]/MU_I/M_PROTON; //number density of photons and electrons
ldouble pi=K_BOLTZ*ni*tempiloc;
ldouble gammai=GAMMAI;
#ifdef CONSISTENTGAMMA
#ifndef FIXEDGAMMASPECIES
gammai=calc_gammaintfromtemp(tempiloc,IONS);
#endif
#endif
uiloc=pi/(gammai-1.);
#endif //EVOLVEELECTRONS
#ifdef PRINTVISCHEATINGTOSILO
deltae[zonalindex]=calc_ViscousElectronHeatingFraction(&get_u(p,0,ix,iy,iz),&geomout);
vischeat[zonalindex]=get_u_scalar(vischeating,ix,iy,iz);
vischeatnege[zonalindex]=get_u_scalar(vischeatingnegebalance,ix,iy,iz);;
vischeatnegi[zonalindex]=get_u_scalar(vischeatingnegibalance,ix,iy,iz);;
dtauarr[zonalindex]=-1.;
#endif //PRINTVISCHEATINGTOSILO
}
else //using averaged data
{
rho[zonalindex] = get_uavg(pavg,RHO,ix,iy,iz);
entr[zonalindex] = get_uavg(pavg,ENTR,ix,iy,iz);
uint[zonalindex] = get_uavg(pavg,UU,ix,iy,iz);
pregas = get_uavg(pavg,AVGPGAS,ix,iy,iz);
gamma = 1. + pregas/uint[zonalindex];
// velocity
vel[0]=get_uavg(pavg,AVGRHOUCON(0),ix,iy,iz)/get_uavg(pavg,RHO,ix,iy,iz);
vel[1]=get_uavg(pavg,AVGRHOUCON(1),ix,iy,iz)/get_uavg(pavg,RHO,ix,iy,iz);
vel[2]=get_uavg(pavg,AVGRHOUCON(2),ix,iy,iz)/get_uavg(pavg,RHO,ix,iy,iz);
vel[3]=get_uavg(pavg,AVGRHOUCON(3),ix,iy,iz)/get_uavg(pavg,RHO,ix,iy,iz);
for(i=0;i<4;i++) vcon[i]=vel[i];
indices_21(vcon,vcov,geomout.gg);
rhouconr=get_uavg(pavg,AVGRHOUCON(1),ix,iy,iz);
rhoucont=get_uavg(pavg,AVGRHOUCON(0),ix,iy,iz);
// angular velocity
Omega[zonalindex]=vel[3]/vel[0];
// ANDREW these are 4-velocites, not VELR, so I don't think this is right...
//updates pp[VI] to have rho-weighted velocities there
//pp[VX]=vel[1];
//pp[VY]=vel[2];
//pp[VZ]=vel[3];
#ifdef MAGNFIELD
B1comp[zonalindex] = get_uavg(pavg,B1,ix,iy,iz);
B2comp[zonalindex] = get_uavg(pavg,B2,ix,iy,iz);
B3comp[zonalindex] = get_uavg(pavg,B3,ix,iy,iz);
bcon[1]=get_uavg(pavg,AVGBCON(1),ix,iy,iz);
bcon[2]=get_uavg(pavg,AVGBCON(2),ix,iy,iz);
bcon[3]=get_uavg(pavg,AVGBCON(3),ix,iy,iz);
bsq[zonalindex]=get_uavg(pavg,AVGBSQ,ix,iy,iz);
premag=0.5*bsq[zonalindex];
beta[zonalindex]=pregas/0.5/bsq[zonalindex];
betainv[zonalindex]=1./beta[zonalindex];
sigma[zonalindex]=bsq[zonalindex]/(rho[zonalindex] + uint[zonalindex] + pregas);
sigmaw[zonalindex]=bsq[zonalindex]/(rho[zonalindex] + gamma*uint[zonalindex]);
#endif
// stress energy tensor
/*
for(i=0;i<4;i++)
for(j=0;j<4;j++)
Tij[i][j]=get_uavg(pavg,AVGRHOUCONUCOV(i,j),ix,iy,iz)
+ GAMMA*get_uavg(pavg,AVGUUUCONUCOV(i,j),ix,iy,iz)
+ get_uavg(pavg,AVGBSQUCONUCOV(i,j),ix,iy,iz)
+ delta(i,j)*(GAMMA*get_uavg(pavg,UU,ix,iy,iz) + 1./2.*get_uavg(pavg,AVGBSQ,ix,iy,iz))
- get_uavg(pavg,AVGBCONBCOV(i,j),ix,iy,iz);
*/
// averaged stress energy tensor
for(i=0;i<4;i++)
for(j=0;j<4;j++)
Tij[i][j]=get_uavg(pavg,AVGTIJ(i,j),ix,iy,iz);
Tit[1]=Tij[1][0];
Tit[2]=Tij[2][0];
Tit[3]=Tij[3][0];
// Bernoulli flux / number
muBe[zonalindex]=-(Tij[1][0] + rhouconr)/rhouconr;
Be[zonalindex]=-(Tij[0][0] + rhoucont)/rhoucont;
#ifdef MAGNFIELD
Qtheta[zonalindex]=2.*M_PI/Omega[zonalindex]/dx[1]*fabs(bcon[2])/sqrt(rho[zonalindex]);
Qphi[zonalindex]=2.*M_PI/Omega[zonalindex]/dx[2]*fabs(bcon[3])/sqrt(rho[zonalindex]);
ldouble brbphi,bsq,bfake[4];
#ifdef BHDISK_PROBLEMTYPE
calc_angle_brbphibsq(ix,iy,iz,&brbphi,&bsq,bfake,bfake); //to calculate magn. field angle
Bangle[zonalindex]=-brbphi/bsq;
#else
Bangle[zonalindex]=-1.;
#endif
if(ix==0 || (NY>1 && iy==0) || (NZ>1 && iz==0)) //divB left-biased
divB[zonalindex]=0;
else
divB[zonalindex]=calc_divB(ix,iy,iz);
#endif // MAGNFIELD
#ifdef RADIATION
taucoupling[zonalindex]=estimate_gas_radiation_coupling(pp,&geomout);
#endif // RADIATION
#ifdef EVOLVEELECTRONS
ldouble pe,pi;
pe=get_uavg(pavg,AVGPE,ix,iy,iz);
pi=get_uavg(pavg,AVGPI,ix,iy,iz);
#ifndef CONSISTENTGAMMA
pi=pregas-pe;
#endif
//electrons
ldouble ne=calc_thermal_ne(pp);
tempeloc=pe/K_BOLTZ/ne;
ldouble gammae=GAMMAE;
#ifdef CONSISTENTGAMMA
#ifndef FIXEDGAMMASPECIES
gammae=calc_gammaintfromtemp(tempeloc,ELECTRONS);
#endif
#endif
ueloc=pe/(gammae-1.);
//ions
ldouble ni=rho[zonalindex]/MU_I/M_PROTON;
tempiloc=pi/K_BOLTZ/ni;
ldouble gammai=GAMMAI;
#ifdef CONSISTENTGAMMA
#ifndef FIXEDGAMMASPECIES
gammai=calc_gammaintfromtemp(tempiloc,IONS);
#endif
#endif
uiloc=pi/(gammai-1.);
#endif // EVOLVEELECTRONS
#ifdef PRINTVISCHEATINGTOSILO
vischeat[zonalindex]=get_uavg(pavg,AVGVISCHEATING,ix,iy,iz);
vischeatnege[zonalindex]=get_uavg(pavg,AVGVISCHEATINGNEGE,ix,iy,iz);
vischeatnegi[zonalindex]=get_uavg(pavg,AVGVISCHEATINGNEGI,ix,iy,iz);
deltae[zonalindex]=-1.;
dtauarr[zonalindex]=-1.;
#endif //PRINTVISCHEATINGTOSILO
} //doingavg
// Nonthermal electron quantities
#ifdef EVOLVEELECTRONS
#ifdef RELELECTRONS
nethloc=calc_thermal_ne(pp);
nrelelloc = calc_relel_ne(pp);
urelelloc = calc_relel_uint(pp);
G0relelloc = -1.*calc_relel_G0_fluidframe(pp,&geomout, 0.0, 0);
G0ic_relel_loc = -1*calc_relel_G0_fluidframe_direct(pp, &geomout, 3);
G0syn_relel_loc = -1*calc_relel_G0_fluidframe_direct(pp, &geomout, 1);
//calculate synchrotron break frequency
//ANDREW speed this up by saving an array
gbrkloc=RELEL_INJ_MIN;
//absolute maximum of g^4*n for g > RELGAMMAMIN
ldouble nbrk=pp[NEREL(0)]*gammapbrk[0];
ldouble nbrk2;
for(ie=1;ie<NRELBIN;ie++)
{
if (relel_gammas[ie] < RELEL_INJ_MIN)
{
gbrkloc=RELEL_INJ_MIN;
nbrk = pp[NEREL(ie)]*gammapbrk[ie];
}
else
{
nbrk2 = pp[NEREL(ie)]*gammapbrk[ie];
if(nbrk2 > nbrk)
{
nbrk=nbrk2;
gbrkloc=relel_gammas[ie];
}
}
}
#endif //RELELECTRONS
#endif //EVOLVEELECTRONS
// put quantities into arrays
expansion[zonalindex]=exploc;
lorentz[zonalindex]=fabs(vel[0])/sqrt(fabs(geomout.GG[0][0]));
u0[zonalindex]=fabs(vel[0]);
u1[zonalindex]=vel[1];
u2[zonalindex]=vel[2];
u3[zonalindex]=vel[3];
#ifdef MAGNFIELD
//OmegaF[zonalindex] = Omega[zonalindex] - (u1[zonalindex]/u0[zonalindex])*(B3comp[zonalindex]/B1comp[zonalindex]);
OmegaF[zonalindex]=Omega[zonalindex];
if(fabs(u2[zonalindex]*B3comp[zonalindex])>1.e-8)
OmegaF[zonalindex] -= (u2[zonalindex]*B3comp[zonalindex])/(u0[zonalindex]*B2comp[zonalindex]);
ldouble velff[4];
ldouble vpar;
vpar = get_driftvel_velr(pp,velff,&geomout);
conv_vels(velff,velff,VELR,VEL4,geomout.gg,geomout.GG);
lorentz_perp[zonalindex]=fabs(velff[0])/sqrt(fabs(geomout.GG[0][0]));
lorentz_par[zonalindex]=1./sqrt(1+pow(lorentz[zonalindex],-2)-pow(lorentz_perp[zonalindex],-2));
#endif
#ifdef MAGNFIELD
#ifdef FORCEFREE // must have VELPRIM=VELR
//ffinv[zonalindex]=get_cflag(FFINVFLAG,ix,iy,iz);
ffinv[zonalindex] = get_u_scalar(ffinvarr, geom.ix,geom.iy,geom.iz);
int derdir2[3] = {2,2,2};
ldouble jcon[4],jcov[4];
calc_current(&geom,jcon,derdir2);
indices_21(jcon,jcov,geom.gg); // not in lab frame
//ldouble FF[4][4];
//calc_faraday(FF,geom.ix,geom.iy,geom.iz,0);
jdens[zonalindex] = sqrt(fabs(dot(jcon,jcov)));
#else
jdens[zonalindex]=0.;
ffinv[zonalindex]=0.;
#endif
#endif
#ifdef EVOLVEELECTRONS
tempi[zonalindex]=tempiloc; //ion temperature
tempe[zonalindex]=tempeloc; //electron temperature
ui[zonalindex]=uiloc; //ion energy density
ue[zonalindex]=ueloc; //electron energy density
#ifdef RELELECTRONS
gammabrk[zonalindex]=gbrkloc;
urelel[zonalindex]=urelelloc;
nrelel[zonalindex]=nrelelloc;
neth[zonalindex]=nethloc;
uratio_tot[zonalindex]=urelelloc/uint[zonalindex];
uratio_th[zonalindex]=urelelloc/ueloc;
G0relel[zonalindex]=G0relelloc;
G0icrelel[zonalindex]=G0ic_relel_loc;
G0synrelel[zonalindex]=G0syn_relel_loc;
#endif
#endif
ldouble temploc=calc_PEQ_Tfromurho(uint[zonalindex],rho[zonalindex],ix,iy,iz);
temp[zonalindex]=temploc;
entrlnT[zonalindex]=kB_over_mugas_mp*entr[zonalindex]/pp[RHO];
#ifdef CGSOUTPUT
rho[zonalindex]=rhoGU2CGS(rho[zonalindex]);
uint[zonalindex]=endenGU2CGS(uint[zonalindex]);
#ifdef MAGNFIELD
bsq[zonalindex]=4.*M_PI*endenGU2CGS(bsq[zonalindex]);
#endif
#ifdef EVOLVEELECTRONS
ui[zonalindex]=endenGU2CGS(ui[zonalindex]);
ue[zonalindex]=endenGU2CGS(ue[zonalindex]);
#ifdef RELELECTRONS
urelel[zonalindex]=endenGU2CGS(urelel[zonalindex]);
nrelel[zonalindex]=numdensGU2CGS(nrelel[zonalindex]);
neth[zonalindex]=numdensGU2CGS(neth[zonalindex]);
G0relel[zonalindex]=endenGU2CGS(G0relel[zonalindex])*timeCGS2GU(1.);
G0icrelel[zonalindex]=endenGU2CGS(G0icrelel[zonalindex])*timeCGS2GU(1.);
G0synrelel[zonalindex]=endenGU2CGS(G0synrelel[zonalindex])*timeCGS2GU(1.);
#endif
#endif
#ifdef PRINTVISCHEATINGTOSILO
vischeat[zonalindex] =endenGU2CGS(vischeat[zonalindex])*timeCGS2GU(1.);
vischeatnege[zonalindex]=endenGU2CGS(vischeatnege[zonalindex])*timeCGS2GU(1.);
vischeatnegi[zonalindex]=endenGU2CGS(vischeatnegi[zonalindex])*timeCGS2GU(1.);
#endif
#endif //CGSOUTPUT
#ifdef PRINTCOULOMBTOSILO
coulomb[zonalindex]=calc_CoulombCoupling(pp,&geomout);
#endif
#ifdef PRINTGAMMATOSILO
gammag[zonalindex]=gamma;
#endif
// vector velocities and energy fluxes
vx[zonalindex]=vel[1];
vy[zonalindex]=vel[2];
vz[zonalindex]=vel[3];
Edotx[zonalindex]=Tit[1];
Edoty[zonalindex]=Tit[2];
Edotz[zonalindex]=Tit[3];
// transform velocity to cartesian
if (MYCOORDS==SCHWCOORDS || MYCOORDS==KSCOORDS || MYCOORDS==KERRCOORDS || MYCOORDS==SPHCOORDS ||
MYCOORDS==MKS1COORDS || MYCOORDS==MKS2COORDS || MYCOORDS==MKS3COORDS || MYCOORDS==JETCOORDS ||
MYCOORDS==MSPH1COORDS || MYCOORDS==MKER1COORDS)
{
vel[2]*=r;
vel[3]*=r*sin(th);
vx[zonalindex] = sin(th)*cos(ph)*vel[1]
+ cos(th)*cos(ph)*vel[2]
- sin(ph)*vel[3];
vy[zonalindex] = sin(th)*sin(ph)*vel[1]
+ cos(th)*sin(ph)*vel[2]
+ cos(ph)*vel[3];
vz[zonalindex] = cos(th)*vel[1]
- sin(th)*vel[2];
Tit[2]*=r;
Tit[3]*=r*sin(th);
Edotx[zonalindex] = sin(th)*cos(ph)*Tit[1]
+ cos(th)*cos(ph)*Tit[2]
- sin(ph)*Tit[3];
Edoty[zonalindex] = sin(th)*sin(ph)*Tit[1]
+ cos(th)*sin(ph)*Tit[2]
+ cos(ph)*Tit[3];
Edotz[zonalindex] = cos(th)*Tit[1]
- sin(th)*Tit[2];
}
// vector magnetic field quantities
#ifdef MAGNFIELD
//magnetic field
Bx[zonalindex]=bcon[1];
By[zonalindex]=bcon[2];
Bz[zonalindex]=bcon[3];
#ifdef BATTERY // battery field
ldouble dBdtbat[4];
estimate_Bgrowth_battery(ix,iy,iz,dBdtbat);
dBxdtbat[zonalindex]=dBdtbat[1];
dBydtbat[zonalindex]=dBdtbat[2];
dBzdtbat[zonalindex]=dBdtbat[3];
#endif
#ifdef MIMICDYNAMO // dynamo field
ldouble ppdyn[NV]; int idyn;
PLOOP(idyn) ppdyn[idyn]=get_u(p,idyn,ix,iy,iz);
ppdyn[B1]=get_u(pvecpot,1,ix,iy,iz);
ppdyn[B2]=get_u(pvecpot,2,ix,iy,iz);
ppdyn[B3]=get_u(pvecpot,3,ix,iy,iz);
#ifdef PRECOMPUTE_MY2OUT
trans_pmhd_coco_my2out(ppdyn, ppdyn, &geom, &geomout);
#else
trans_pmhd_coco(ppdyn, ppdyn, MYCOORDS,OUTCOORDS, xxvec,&geom,&geomout);
#endif
ldouble bcondyn[4],bcovdyn[4];
calc_bcon_prim(ppdyn,bcondyn,&geomout);
//indices_21(bcondyn,bcovdyn,geomout.gg);
Bxdyn[zonalindex]=bcondyn[1];
Bydyn[zonalindex]=bcondyn[2];
Bzdyn[zonalindex]=bcondyn[3];
#endif
// calculate vector potential
//ANDREW changed phi defn to match harmpi
int iphimin,iphimax;
iphimin=0;
iphimax=ny-1;
#if defined(CORRECT_POLARAXIS) || defined(CORRECT_POLARAXIS_3D)
//iphimin=NCCORRECTPOLAR;
#ifndef HALFTHETA
//iphimax=ny-NCCORRECTPOLAR-1;
#endif
#endif
if(iy==iphimin)
{
//phi[zonalindex]=geom.gdet*get_u(p,B1,ix,iy,iz)*get_size_x(iy,1)*2.*M_PI;
phi_accum[zonalindex]=geom.gdet*get_u(p,B1,ix,iy,iz)*get_size_x(iy,1);//*2.*M_PI;
phi[zonalindex] = phi_accum[zonalindex] - 0.5*geom.gdet*get_u(p,B1,ix,iy,iz)*get_size_x(iy,1);
}
else if(iy>iphimin && iy<=iphimax)
{
imz=iz;imy=iy;imx=ix;
#ifdef PRINTXGC_RIGHT
imx=ix-NG;
#endif
#ifdef PRINTXGC_LEFT
imx=ix-NG;
#endif
#ifdef PRINTYGC_RIGHT
imy=iy-NG;
#endif
int idx=imz*(ny*nx) + (imy-1)*nx + imx;
//phi[zonalindex]=phi[idx]+geom.gdet*get_u(p,B1,ix,iy,iz)*get_size_x(iy,1)*2.*M_PI;
phi_accum[zonalindex]=phi_accum[idx] + geom.gdet*get_u(p,B1,ix,iy,iz)*get_size_x(iy,1); //*2.*M_PI;
phi[zonalindex] = phi_accum[zonalindex] - 0.5*geom.gdet*get_u(p,B1,ix,iy,iz)*get_size_x(iy,1);
}
#ifdef MIMICDYNAMO
Avecdyn[zonalindex]=get_u(ptemp1,B3,ix,iy,iz);
if(iy==iphimin)
{
phidyn[zonalindex]=geom.gdet*get_u(pvecpot,1,ix,iy,iz)*get_size_x(iy,1)*2.*M_PI;
}
else if(iy>iphimin && iy<=iphimax)
{
imz=iz;imy=iy;imx=ix;
#ifdef PRINTXGC_RIGHT
imx=ix-NG;
#endif
#ifdef PRINTXGC_LEFT
imx=ix+NG;
#endif
#ifdef PRINTYGC_RIGHT
imy=iy-NG;
#endif
int idx=imz*(ny*nx) + (imy-1)*nx + imx;
phidyn[zonalindex]=phidyn[idx]+geom.gdet*get_u(pvecpot,1,ix,iy,iz)*get_size_x(iy,1)*2.*M_PI;
}
#endif
#ifdef BATTERY
if(iy==iphimin)
{
phibat[zonalindex]=geom.gdet*dBdtbat[1]*get_size_x(iy,1)*2.*M_PI;
}
else if(iy>iphimin && iy<=iphimax)
{
imz=iz;imy=iy;imx=ix;
#ifdef PRINTXGC_RIGHT
imx=ix-NG;
#endif
#ifdef PRINTXGC_LEFT
imx=ix+NG;
#endif