-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmisc.c
More file actions
1816 lines (1557 loc) · 51.1 KB
/
misc.c
File metadata and controls
1816 lines (1557 loc) · 51.1 KB
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 misc.c
\brief misceleanous routines
*/
#include "ko.h"
//**********************************************************************
/*! \fn int initialize_consants()
\brief Set constants to be used throughout
*/
//**********************************************************************
void initialize_constants()
{
// Conversion Factors
tempcgs2gu = 1.;
tempgu2cgs = 1.;
lencgs2gu = 1. / MASSCM;
lengu2cgs = MASSCM;
numdenscgs2gu = MASSCM*MASSCM*MASSCM;
numdensgu2cgs = (1./MASSCM/MASSCM/MASSCM);
timecgs2gu = 1. /MASSCM*CCC;
timegu2cgs = 1. / timecgs2gu;
velcgs2gu = 1. / CCC;
velgu2cgs = CCC;
rhocgs2gu = GGG/CCC/CCC*MASSCM*MASSCM;
rhogu2cgs = 1./GGG*CCC*CCC/MASSCM/MASSCM;
surfdenscgs2gu = GGG/CCC/CCC*MASSCM;
surfdensgu2cgs = 1. / surfdenscgs2gu;
masscgs2gu = GGG/CCC/CCC/MASSCM;
massgu2cgs = 1. / masscgs2gu;
kappacgs2gu = 1. /GGG*CCC*CCC/MASSCM;
kappagu2cgs = 1. / kappacgs2gu;
endencgs2gu = GGG*MASSCM*MASSCM/CCC/CCC/CCC/CCC;
endengu2cgs = 1. / endencgs2gu;
heatcoolcgs2gu = endencgs2gu * timegu2cgs;
heatcoolgu2cgs = 1. / heatcoolcgs2gu;
fluxcgs2gu = GGG*MASSCM*MASSCM/CCC/CCC/CCC/CCC/CCC;
fluxgu2cgs = 1. / fluxcgs2gu;
ergcgs2gu = GGG/MASSCM/CCC/CCC/CCC/CCC;
erggu2cgs = 1. / ergcgs2gu;
chargecgs2gu = sqrt(GGG/MASSCM/CCC/CCC/CCC/CCC)*sqrt(1./MASSCM);
chargegu2cgs = 1. / chargecgs2gu;
crosscgs2gu = 1. / MASSCM/MASSCM;
crossgu2cgs = 1. / crosscgs2gu;
// Physical constants
k_boltz_cgs = K_BOLTZ_CGS * 1.;
k_boltz_cgs_inv = 1. / k_boltz_cgs;
k_boltz = K_BOLTZ;
k_boltz_inv = (1. / K_BOLTZ);
m_proton_cgs = M_PROTON_CGS;
m_proton = M_PROTON;
m_electr_cgs = M_ELECTR_CGS;
m_electr = M_ELECTR;
mpe_ratio = MPE_RATIO;
sigma_rad_cgs = SIGMA_RAD_CGS;
sigma_rad = SIGMA_RAD;
h_cgs = H_CGS;
sigmath_cgs = SIGMATH_CGS;
a_rad = A_RAD;
z_ratio = Z_RATIO;
e_charge = E_CHARGE;
sigma_thomson = SIGMA_THOMPSON;
// Miscellaneous coefficients
fourpi = 4. * Pi;
fourmpi = 4. * M_PI;
per_volume_per_time_cgs2gu = 1. / (lencgs2gu * lencgs2gu * lencgs2gu * timecgs2gu);
sigma_rad_over_pi = SIGMA_RAD / Pi;
four_sigmarad = 4. * sigma_rad;
one_over_four_sigmarad = 1. / four_sigmarad;
k_over_mecsq = 1.69e-10;
kB_over_mui_mp = (K_BOLTZ / MU_I / M_PROTON);
kB_over_mue_mp = (K_BOLTZ / MU_E / M_PROTON);
kB_over_mugas_mp = (K_BOLTZ / MU_GAS / M_PROTON);
mugas_mp_over_kB = 1. / kB_over_mugas_mp;
kB_over_mp = (K_BOLTZ / M_PROTON);
kB_over_me = (K_BOLTZ / M_ELECTR);
one_over_kB_2p70118 = k_boltz_inv / 2.70118;
one_over_mugas_mp = (1. / MU_GAS / M_PROTON);
one_over_mui_mp = (1. / MU_I / M_PROTON);
one_over_mue_mp = (1. / MU_E / M_PROTON);
mui_over_mue = (MU_I/MU_E);
four_third = 4. / 3.;
one_third = 1. / 3.;
two_third = 2. / 3.;
log_2p6 = log(2.6);
one_over_log_2p6 = 1. / log_2p6;
// Coordinate specific factors
#if (MYCOORDS==JETCOORDS)
hypx1in = log(RMIN-MKSR0);
hypx1brk= log(HYPRBRK-MKSR0);
hypx1out= hyperexp_x1max(RMAX, HYPRBRK, MKSR0);
#ifdef CYLINDRIFY
set_cyl_params();
#endif
#endif //MYCOORDS==JETCOORDS
// cutoff factors for hybrid force-free
#ifdef FORCEFREE
#if defined(HYBRID_FORCEFREE) && !defined(HYBRID_FORCEFREE_XCUT)
ldouble sigcut = HYBRID_FORCEFREE_SIGMACUT;
ldouble tanhwidth = HYBRID_FORCEFREE_WIDTH;
if(tanhwidth<=0.) // step function cutoff
{
ffinv_lower_cutoff=sigcut;
ffinv_upper_cutoff=sigcut;
}
else //cutoff at f(sigma) = 1/64 and f(sigma) = 63/64
{
ldouble fac=pow(3.,tanhwidth)*pow(7.,0.5*tanhwidth);
ffinv_upper_cutoff = sigcut*fac;
ffinv_lower_cutoff = sigcut/fac;
}
if(PROCID==0) printf("ff cutoffs %e %e\n",ffinv_lower_cutoff,ffinv_upper_cutoff);
#endif
#endif
return;
}
//**********************************************************************
/*! \fn int calc_cells_under_horiz()
\brief calculate the number of cells beneath the horizon for bhdisk problems
*/
//**********************************************************************
int
calc_cells_under_horiz()
{
//consistency check -- how many cell centers are under horizon?
#ifdef BHDISK_PROBLEMTYPE
ldouble xx[4],xxBL[4];
int ix;
if(TOI==0 && TOJ==0 && TOK==0) // only do on 0th tile
{
cells_under_horizon=0;
for(ix=0;ix<NX;ix++)
{
get_xx(ix,0,0,xx); //BH problem types should have r coord independent of theta,phi
coco_N(xx,xxBL,MYCOORDS,BLCOORDS);
if(xxBL[1]>rhorizonBL)
break;
else
cells_under_horizon+=1;
}
if(cells_under_horizon<3)
{
printf("There are only %d cells under horizon at rh=%.2f! increase to at least 4\n",
cells_under_horizon,rhorizonBL);
//exit(-1);
}
if(cells_under_horizon==NX)
{
printf("All cells on inner tile are under horizon! \n");
exit(-1);
}
printf("There are %d cells under the horizon at rh=%.2f\n",
cells_under_horizon,rhorizonBL);
}
#endif
return 0;
}
//**********************************************************************
/*! \fn int print_scalings()
\brief Print out key parameters and scalings of the simulation
*/
//**********************************************************************
int
print_scalings()
{
printf("\n ***************************************\n\n");
printf("BH mass: %.6f\nspin: %.6f\n\nscalings (GU->CGS):\nrho: %.6e\nmdot: %.6e\nsigma: %.6e\nlen: %.6e\ntime: %.6e\nenden:"
"%.6e\nflux: %.6e\nT(1,1): %.6e\nkbt: %.6e\nkb/me: %.6e\nsigma_rad: %.6e\nkappa: %.6e\nGt: %.6e\nmass: %.6e\n\n"
"rhorizonBL: %.6f\nrISCOBL: %.6f\netaNT: %.6f\n\n->mdotEdd: %.6e\n->lumEdd: %.6e\n\nGMc2: %.6e\nGMc3: %.6e\n"
"Mdot_Edd: %.6e g/s\nLdot_Edd: %.6e erg/s\n",
MASS,BHSPIN,
rhoGU2CGS(1.),
rhoGU2CGS(1.)*velGU2CGS(1.)*lenGU2CGS(1.)*lenGU2CGS(1.),
rhoGU2CGS(1.)*lenGU2CGS(1.),
lenGU2CGS(1.),
timeGU2CGS(1.),
endenGU2CGS(1.),
fluxGU2CGS(1.),
calc_PEQ_Tfromurho(1.,1.,0,0,0),
K_BOLTZ/MU_GAS/M_PROTON,
K_BOLTZ/M_ELECTR,
SIGMA_RAD,
kappaGU2CGS(1.),
kappaGU2CGS(1.)*rhoGU2CGS(1.)*endenGU2CGS(1.)*CCC,
massGU2CGS(1.),
rhorizonBL,
rISCOBL,
etaNT,
rhoGU2CGS(1.)*velGU2CGS(1.)*lenGU2CGS(1.)*lenGU2CGS(1.)/calc_mdotEdd(),
rhoGU2CGS(1.)*velGU2CGS(1.)*lenGU2CGS(1.)*lenGU2CGS(1.)*CCC0*CCC0/calc_lumEdd(),
GMC2,
GMC3,
calc_mdotEdd(),
calc_lumEdd()
);
printf("\n ***************************************\n\n");
return 0;
}
//**********************************************************************
/*! \fn int set_initial_profile()
\brief Sets the initial distributions of quantities
Details are to be supplied in the file: PROBLEMS/XXX/init.c
*/
//**********************************************************************
int
set_initial_profile()
{
if(PROCID==0) {printf("Initializing problem... \n");fflush(stdout);}
int ix,iy,iz;
#pragma omp parallel for private(ix,iy,iz) schedule (static)
for(ix=0;ix<NX;ix++)
{
for(iy=0;iy<NY;iy++)
{
for(iz=0;iz<NZ;iz++)
{
#include PR_INIT
}
}
}
#ifdef MPI
MPI_Barrier(MPI_COMM_WORLD);
#endif
#ifdef OMP
#pragma omp barrier
#endif
if(PROCID==0) printf("done!\n");
return 0;
}
//**********************************************************************
/*! \fn void am_i_sane()
\brief Checks that there are no conflicts in the chosen settings
*/
//**********************************************************************
void
am_i_sane()
{
if(!(MYCOORDS==SPHCOORDS || MYCOORDS==CYLCOORDS || MYCOORDS==MINKCOORDS ||
MYCOORDS==KERRCOORDS || MYCOORDS==KSCOORDS || MYCOORDS==MKS1COORDS ||
MYCOORDS==MKS2COORDS || MYCOORDS==MCYL1COORDS || MYCOORDS==MSPH1COORDS ||
MYCOORDS==MKER1COORDS))
{
if(GDETIN==0)
{
printf("GDETIN==1 does not work with this coordinate system!\n");
exit(-1);
}
#ifndef METRICNUMERIC
//printf("this coordinate system requires METRICNUMERIC!\n");
//exit(-1);
#endif
}
if(HFRAC+HEFRAC+MFRAC != 1.)
{
printf("Chosen H, He, and Metal abundance does not sum to 1.\n");
exit(-1);
}
if (INT_ORDER > 2)
{
printf("Only INT_ORDER=1 and INT_ORDER=2 supported!\n");
exit(-1);
}
#ifdef CORRECT_POLARAXIS
if (TNY == 1)
{
printf("\nERROR!! Using CORRECT_POLARAXIS on a 2D problem in r-phi. No evolution will occur! Switch off polar axis correction and recompile!\n\n");
exit(-1);
}
#endif
#ifdef TRANSMITTING_YBC
if(PROCID==0)
{
printf("Note: Using TRANSMITTING_YBC. Recommended MINY >= 0.005. Difussion around pole dominates transmission otherwise.\n");
}
if (TNZ % 2 != 0)
{
printf("\nERROR!! Using transmitting y boundary. TNZ = %d must be divisible by 2!\n\n", TNZ);
exit(-1);
}
if (NTZ % 2 != 0)
{
printf("\nERROR!! Using transmitting y boundary. NTZ = %d must be divisible by 2!\n\n", NTZ);
exit(-1);
}
#endif
#ifdef PRECOMPUTE_MY2OUT
if (PROCID==0){
printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("\nWarning -- using precomputed MYCOORDS --> OUTCOORDS for floors, averages, and boundary conditions\n");
printf("Check your bcs.c!!\n");
#ifdef BHDISK_PROBLEMTYPE
if(!((OUTCOORDS == BLCOORDS) || (OUTCOORDS == KSCOORDS))) {
printf("For BHDISK_PROBLEMTYPE PRECOMPUTE_MY2OUT currently only works with OUTCOORDS=BLCOORDS!\n");
printf("(It should also work with KSCOORDS, but just being safe for now...)\n");
exit(-1);
}
#endif
printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");
}
#endif
#ifdef FORCEFREE
#ifdef NONRELMHD
printf("FORCEFREE does not work with NONRELMHD!\n");
exit(-1);
#endif
#ifdef RADIATION
printf("FORCEFREE does not work yet with RADIATION!\n");
exit(-1);
#endif
#ifndef MAGNFIELD
printf("FORCEFREE requires MAGNFIELD!\n");
exit(-1);
#endif
#if (VELPRIM!=VELR)
printf("FORCEFREE requries VELPRIM==VELR!\n");
exit(-1);
#endif
#if(GDETIN==0)
printf("FORCEFREE does not work with GDETIN==0!\n");
exit(-1);
#endif
#ifdef CORRECT_POLARAXIS_3D
printf("FORCEFREE does not work with CORRECT_POLARAXIS_3D!\n");
exit(-1);
#endif
#endif
#ifdef PWPOTENTIAL
printf("PWPOTENTIAL has been removed!\n");
exit(-1);
#endif
#ifdef NCOMPTONIZATION
printf("NCOMPTONIZATION has been replaced by EVOLVEPHOTONNUMBER!\n");
exit(-1);
#endif
#ifdef RADIATION
#if defined(COMPTONIZATIONFLAG)
if (PROCID == 0)
{
printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("\nAutomatically switching on COMPTONIZATION\n");
printf("Please define NO_COMPTONIZATION if Comptonization should remain off\n");
#ifdef EVOLVEPHOTONNUMBER
printf("EVOLVEPHOTONNUMBER is ON\n");
#else
printf("EVOLVEPHOTONNUMBER is OFF\n");
#endif
printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");
}
#endif
#endif
#ifdef COLDOPACITIES
printf("COLDOPACITIES no longer defined!\n");
exit(-1);
#endif
#ifdef EVOLVEELECTRONS
#ifdef FIXEDTETIRATIO
printf("FIXEDTETIRATIO can't be used together with EVOLVEELECTRONS\n");
exit(-1);
#endif
#if defined(RADIATION) && !defined(SKIPRADSOURCE)
#ifdef HEATELECTRONSATENDRK2
printf("HEATELECTRONSATENDRK2 only works without RADIATION!");
exit(-1);
#endif
#endif
#endif
#ifdef SHEARINGBOX
#ifdef MPI
printf("SHEARINGBOX BC not implemented into MPI\n");
exit(-1);
#endif
#ifndef NONRELMHD
printf("SHEARINGBOX requires NONRELMHD.\n");
exit(-1);
#endif
#if (MYCOORDS!=MINKCOORDS)
printf("SHEARINGBOX requires MINKCOORDS.\n");
exit(-1);
#endif
#endif // ifdef SHEARINGBOX
#ifdef NONRELMHD
if(MYCOORDS!=SPHCOORDS && MYCOORDS!=CYLCOORDS && MYCOORDS!=MINKCOORDS)
{
printf("NONRELMHD implemented only for flat coords so far.\n");
exit(-1);
}
#ifdef EVOLVEPHOTONNUMBER
printf("NONRELMHD not implemented for EVOLVEPHOTONNUMBER.\n");
exit(-1);
#endif
#endif // ifdef NONRELMHD
#ifdef CONSISTENTGAMMA
#ifndef EVOLVEELECTRONS
printf("CONSISTENTGAMMA works only with EVOLVEELECTRONS\n");
exit(-1);
#endif
#endif
#ifdef RELELECTRONS
#ifndef EVOLVEELECTRONS
printf("RELELECTRONS requires EVOLVEELECTRONS\n");
exit(-1);
#endif
#ifndef NRELBIN
printf("RELELECTRONS requires NRELBIN\n");
exit(-1);
#endif
#ifndef RELGAMMAMIN
printf("RELELECTRONS requires RELGAMMAMIN\n");
exit(-1);
#endif
#ifndef RELGAMMAMAX
printf("RELELECTRONS requires RELGAMMAMAX\n");
exit(-1);
#endif
#endif
#ifdef PWPOTENTIAL
if(MYCOORDS!=SPHCOORDS && MYCOORDS!=CYLCOORDS)
{
printf("PWPOTENTIAL implemented only for SPHCOORDS or CYLCOORDS so far.\n");
exit(-1);
}
#endif
if (FLUXMETHOD == HLLC_FLUX)
{
printf("\n HLLC_FLUX is not supported anymore!\n");
}
#ifdef SUBZONES
printf("\nSUBZONES is not supported anymore!!\n\n");
exit(0);
#endif
#ifdef EVOLVEINTENSITIES
printf("\nEVOLVEINTENSITIES not supported anymore!!\n\n");
exit(0);
#endif
#if (RADCLOSURE==VETCLOSURE)
printf("\nVETCLOSURE is not supported anymore!!\n\n");
exit(0);
#endif
#ifdef METRICTIMEDEPENDENT
printf("\nMETRICTIMEDEPENDENT is not currently supported!!\n\n");
exit(0);
#endif
#ifdef NUMRADWAVESPEEDS
printf("NUMRADWAVESPEEDS is not currently supported!\n");
exit(-1);
#endif
#ifdef RADOUTPUTINFF
printf("RADOUTPUTINFF no longer supported!\n");
exit(-1);
#endif
#ifdef RADOUTPUTINZAMO
printf("RADOUTPUTINZAMO no longer supported!\n");
exit(-1);
#endif
#ifdef RESTARTFROMMHD
if(PROCID==0) printf("RESTARTFROMMHD\n");
if(PROCID==0) printf("urad/uu: %e ue/uu: %e\n", INITURADFRAC, INITUEFRAC);
#endif
return;
}
//**********************************************************************
// allocate arrays
//**********************************************************************
int
initialize_arrays()
{
long long i,j,k;
/********************* Basic arrays ***************************/
long long Ngrid=(SX)*(SY)*(SZ);
long long GridSize=Ngrid*sizeof(ldouble);
long long Nprim=Ngrid*NV;
long long PrimSize=Nprim*sizeof(ldouble);
long long Navg=Ngrid*(NV+NAVGVARS);
long long AvgSize=Navg*sizeof(ldouble);
long long Ngridmet=(SX)*(SY)*(SZMET);
long long Nmet=Ngridmet*gSIZE;
long long MetSize=Nmet*sizeof(ldouble);
long long Nkris=(SX)*(SY)*(SZMET)*64;
long long KrisSize=Nkris*sizeof(ldouble);
long long Ntensor=Ngrid*16;
long long TensorSize=Ntensor*sizeof(ldouble);
//grid
if((x=(ldouble*)malloc((NX+NY+NZ+6*NG)*sizeof(ldouble)))==NULL) my_err("malloc err.\n");
if((xb=(ldouble*)malloc((NX+1+NY+1+NZ+1+6*NG)*sizeof(ldouble)))==NULL) my_err("malloc err.\n");
#ifdef PRECOMPUTE_MY2OUT
//arrays for MYCOORDS->OUTCOORDS transformation
if((xout=(ldouble*)malloc((Ngrid*3)*sizeof(ldouble)))==NULL) my_err("malloc err.\n");
if((dxdx_my2out=(ldouble*)malloc((Ngridmet*16)*sizeof(ldouble)))==NULL) my_err("malloc err.\n");
if((dxdx_out2my=(ldouble*)malloc((Ngridmet*16)*sizeof(ldouble)))==NULL) my_err("malloc err.\n");
//outcoords at x-faces // only if postproc==1 ??
long long Ngrid_xface=(SX+1)*(SY)*(SZ);
long long Ngrid_yface=(SX)*(SY+1)*(SZ);
long long Ngrid_zface=(SX)*(SY)*(SZ+1);
if((xbout_xface=(ldouble*)malloc((Ngrid_xface*3)*sizeof(ldouble)))==NULL) my_err("malloc err.\n");
if((xbout_yface=(ldouble*)malloc((Ngrid_yface*3)*sizeof(ldouble)))==NULL) my_err("malloc err.\n");
if((xbout_zface=(ldouble*)malloc((Ngrid_zface*3)*sizeof(ldouble)))==NULL) my_err("malloc err.\n");
#endif
//primitives at cell centers
if((p=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
//quantities to average in time
if((pavg=(ldouble*)malloc(AvgSize))==NULL) my_err("malloc err.\n");
//conserved averages
if((u=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
//flags at cell centers
if((cellflag=(int*)malloc(Ngrid*NFLAGS*sizeof(int)))==NULL) my_err("malloc err.\n");
//metric at cell centers
if((g=(ldouble*)malloc(MetSize))==NULL) my_err("malloc err.\n");
if((G=(ldouble*)malloc(MetSize))==NULL) my_err("malloc err.\n");
//Kristofels at cell centers
if((gKr=(ldouble*)malloc(KrisSize))==NULL) my_err("malloc err.\n");
//primitives at cell centers at initial state - used for fixed boundary conditions
if((pinit=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
//primitives at cell centers at the beginning and end of explicit operator
if((upreexplicit=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((ppreexplicit=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
//primitives at cell centers at the end of implicit operator
if((ppostimplicit=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
//arrays for temporary use (e.g., vector potential, mimic_dynamo)
if((pproblem1=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((ptemp1=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((pvecpot=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
//arrays for avg time
if((avgselftime=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
//arrays for radiation tensor
#ifdef RADIATION
#if (RADVISCOSITY==SHEARVISCOSITY)
if((Rijviscprev=(ldouble*)malloc(TensorSize))==NULL) my_err("malloc err.\n");
if((Rijviscglobal=(ldouble*)malloc(TensorSize))==NULL) my_err("malloc err.\n");
if((radvisclasttime=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
#endif
#endif
//arrays for viscous heating
if((vischeating=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((vischeatingnegebalance=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((vischeatingnegibalance=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((vischeatingtimesdeltae=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
//these will aggregate over time so should start with zeros
for(i=0;i<Ngrid;i++)
vischeatingnegebalance[i]=vischeatingnegibalance[i]=0.;
//gamma of gas at the beginnning of timestep
if((gammagas=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
#ifdef FORCEFREE
if((ffinvarr=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
#endif
/****************** extra arrays, used only for time evolution **********************/
//we might need some of these arrays in postproc (if doingpostproc_avg==1)
#ifdef DIVIDEVISCHEATBYDT
if(1.)
#else
if(doingpostproc==0 || doingpostproc_avg==1)
#endif
{
//buffer for sending/receiving messages
#ifdef MPI
if((msgbufs=(ldouble**)malloc(MPIMSGBUFSIZE*sizeof(ldouble*)))==NULL) my_err("malloc err.\n");
for(i=0;i<MPIMSGBUFSIZE;i++)
if((msgbufs[i]=(ldouble*)malloc(my_max3(NX*NY*NV*NG,NY*NZ*NV*NG,NZ*NX*NV*NG)*sizeof(ldouble)))==NULL) my_err("malloc err.\n");
#endif
long long NMetX = (SX+1)*(SY)*(SZMET)*gSIZE;
long long MetXSize=NMetX*sizeof(ldouble);
long long NMetY = (SX)*(SY+1)*(SZMET)*gSIZE;
long long MetYSize=NMetY*sizeof(ldouble);
long long NMetZ = (SX)*(SY)*(SZMET+1)*gSIZE;
long long MetZSize=NMetZ*sizeof(ldouble);
long long NMetVec = (SX)*(SY)*(SZMET)*16;
long long MetVecSize=NMetVec*sizeof(ldouble);
//metric at cell x-faces
if((gbx=(ldouble*)malloc(MetXSize))==NULL) my_err("malloc err.\n");
if((Gbx=(ldouble*)malloc(MetXSize))==NULL) my_err("malloc err.\n");
//metric at cell y-faces
if((gby=(ldouble*)malloc(MetYSize))==NULL) my_err("malloc err.\n");
if((Gby=(ldouble*)malloc(MetYSize))==NULL) my_err("malloc err.\n");
//metric at cell z-faces
if((gbz=(ldouble*)malloc(MetZSize))==NULL) my_err("malloc err.\n");
if((Gbz=(ldouble*)malloc(MetZSize))==NULL) my_err("malloc err.\n");
//Fluxes and wavespeeds
long long NfluxX = (SX+1)*(SY)*(SZ)*NV;
long long fluxXSize = NfluxX*sizeof(double);
long long NfluxY = (SX)*(SY+1)*(SZ)*NV;
long long fluxYSize = NfluxY*sizeof(double);
long long NfluxZ = (SX)*(SY)*(SZ+1)*NV;
long long fluxZSize = NfluxZ*sizeof(double);
//wavespeeds hd and rad - max(al,ar)
if((ahdx=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((ahdy=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((ahdz=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((aradx=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((arady=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((aradz=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
//wavespeeds hd and rad - leftgoing
if((ahdxl=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((ahdyl=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((ahdzl=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((aradxl=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((aradyl=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((aradzl=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
//wavespeeds hd and rad - rightgoing
if((ahdxr=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((ahdyr=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((ahdzr=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((aradxr=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((aradyr=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((aradzr=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
//left-interpolated primitives at cell faces
if((pbLx=(ldouble*)malloc(fluxXSize))==NULL) my_err("malloc err.\n");
if((pbLy=(ldouble*)malloc(fluxYSize))==NULL) my_err("malloc err.\n");
if((pbLz=(ldouble*)malloc(fluxZSize))==NULL) my_err("malloc err.\n");
//right-interpolated primitives at cell faces
if((pbRx=(ldouble*)malloc(fluxXSize))==NULL) my_err("malloc err.\n");
if((pbRy=(ldouble*)malloc(fluxYSize))==NULL) my_err("malloc err.\n");
if((pbRz=(ldouble*)malloc(fluxZSize))==NULL) my_err("malloc err.\n");
//corrected flux at faces
if((flbx=(ldouble*)malloc(fluxXSize))==NULL) my_err("malloc err.\n");
if((flby=(ldouble*)malloc(fluxYSize))==NULL) my_err("malloc err.\n");
if((flbz=(ldouble*)malloc(fluxZSize))==NULL) my_err("malloc err.\n");
//flux based on left-interpolated conserved at cell faces
if((flLx=(ldouble*)malloc(fluxXSize))==NULL) my_err("malloc err.\n");
if((flLy=(ldouble*)malloc(fluxYSize))==NULL) my_err("malloc err.\n");
if((flLz=(ldouble*)malloc(fluxZSize))==NULL) my_err("malloc err.\n");
//flux based on right-interpolated conserved at cell faces
if((flRx=(ldouble*)malloc(fluxXSize))==NULL) my_err("malloc err.\n");
if((flRy=(ldouble*)malloc(fluxYSize))==NULL) my_err("malloc err.\n");
if((flRz=(ldouble*)malloc(fluxZSize))==NULL) my_err("malloc err.\n");
//auxiliary primitive arrays
if((pproblem2=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((ptm1=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((ut0=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((ut1=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((ut2=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((ut3=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((dut0=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((dut1=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((dut2=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((drt0=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((drt1=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((drt2=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((uforget=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((u_bak_fixup=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
if((p_bak_fixup=(ldouble*)malloc(PrimSize))==NULL) my_err("malloc err.\n");
//timesteps required by each cell
if((cell_tstepden=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
if((cell_dt=(ldouble*)malloc(GridSize))==NULL) my_err("malloc err.\n");
#ifdef MAGNFIELD
//electromotive force at corners
long long Nemf = (NX+1)*(NY+1)*(NZ+1)*3;
long long EmfSize = Nemf*sizeof(ldouble);
if((emf=(ldouble*)malloc(EmfSize))==NULL) my_err("malloc err.\n");
#endif
}
init_all_kappa_table();
return 0;
}
//**********************************************************************
//Free arrays at end
//**********************************************************************
int
free_arrays()
{
free(cellflag);
free(x);
free(xb);
free(p);
free(pavg);
free(pinit);
free(upreexplicit);
free(ppreexplicit);
free(ppostimplicit);
free(pproblem1);
free(pproblem2);
free(avgselftime);
free(vischeating);
free(vischeatingnegebalance);
free(vischeatingnegibalance);
free(vischeatingtimesdeltae);
free(ptemp1);
free(pvecpot);
#ifdef RADIATION
#if (RADVISCOSITY==SHEARVISCOSITY)
free(Rijviscprev);
free(Rijviscglobal);
free(radvisclasttime);
#endif
#endif
#ifdef MAGNFIELD
free(emf);
#endif
free(ptm1);
#ifdef MPI
int i;
for(i=0;i<MPIMSGBUFSIZE;i++)
free(msgbufs[i]);
free(msgbufs);
#endif
free(px);
free(py);
free(pz);
free(u);
free(g);
free(G);
free(gKr);
free(pbLx);
free(pbRx);
free(pbLy);
free(pbRy);
free(pbLz);
free(pbRz);
free(flbx);
free(flby);
free(flbz);
free(flLx);
free(flRx);
free(flLy);
free(flRy);
free(flLz);
free(flRz);
free(gbx);
free(gby);
free(gbz);
free(Gbx);
free(Gby);
free(Gbz);
free(ut0);
free(ut1);
free(ut2);
free(ut3);
free(dut0);
free(dut1);
free(dut2);
free(drt0);
free(drt1);
free(drt2);
free(uforget);
free(u_bak_fixup);
free(p_bak_fixup);
free(aradx);
free(arady);
free(aradz);
free(ahdx);
free(ahdy);
free(ahdz);
free(aradxl);
free(aradyl);
free(aradzl);
free(ahdxl);
free(ahdyl);
free(ahdzl);
free(aradxr);
free(aradyr);
free(aradzr);
free(ahdxr);
free(ahdyr);
free(ahdzr);
free(cell_dt);
free(cell_tstepden);
free(gammagas);
#ifdef USE_PLANCK_TABLE
free(chiantilogkappa);
free(chiantilogT);
#endif
#ifdef USE_CHIANTI_ISM_TABLE
for(i=0,i<=ChiantiISMTableLength,i++)
free(ChiantiISMTable[idx]);
free(ChiantiISMTable);
#endif
//#ifdef SUTHERLAND_DOPITA_LAMBDA
//free(temperaturelog);
//free(Lambdalog);
//#endif
#ifdef FORCEFREE
free(ffinvarr);
#endif
return 0;
}
//**********************************************************************
// Initialize pointers to entropy functions
//**********************************************************************
void
init_pointers()
{
#if defined(CONSISTENTGAMMA) && !defined(FIXEDGAMMASPECIES)
calc_SefromrhoT=&calc_S3fromrhoT;
calc_Sefromrhou=&calc_S3fromrhou;
calc_TfromSerho=&calc_TfromS3rho;
calc_ufromSerho=&calc_ufromS3rho;
#else
calc_SefromrhoT=&calc_S2fromrhoT;
calc_Sefromrhou=&calc_S2fromrhou;
calc_TfromSerho=&calc_TfromS2rho;
calc_ufromSerho=&calc_ufromS2rho;
#endif
}
//**********************************************************************
// initialize gammagas at init
//**********************************************************************
int
fill_arrays_at_init()
{
int ii;
for(ii=0;ii<Nloop_02;ii++)
{
int ix,iy,iz;
ix=loop_02[ii][0];
iy=loop_02[ii][1];
iz=loop_02[ii][2];
set_u_scalar(gammagas, ix, iy, iz, GAMMA);
}
return 0;
}
//**********************************************************************
//inverse of a general matrix using gsl
//**********************************************************************
int
inverse_matrix(ldouble *a, ldouble *ia, int N)
{
gsl_matrix *m
= gsl_matrix_alloc (N, N);
gsl_matrix *im
= gsl_matrix_alloc (N, N);
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
gsl_matrix_set(m,i,j,a[i*N+j]);
gsl_permutation * p = gsl_permutation_alloc (N);
int s;
gsl_linalg_LU_decomp (m, p, &s);
gsl_linalg_LU_invert (m, p, im);
for(i=0;i<N;i++)
for(j=0;j<N;j++)
ia[i*N+j]=gsl_matrix_get(im,i,j);
gsl_matrix_free(m);
gsl_matrix_free(im);
gsl_permutation_free(p);
return 0;
}
//**********************************************************************
//multiply 4by4 matrices
//**********************************************************************
int
multiply_44matrices(ldouble T1[][4],ldouble T2[][4],ldouble Tout[][4])
{
int i,j,k;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
Tout[i][j]=0.;
for(k=0;k<4;k++)
{
Tout[i][j] += T1[i][k] * T2[k][j];
}
}
}
return 0;
}
//**********************************************************************
//inverse 4by4 matrix
//**********************************************************************
int
inverse_44matrix(ldouble a[][4], ldouble ia[][4])
{
ldouble mat[16],dst[16];
int i,j;