-
Notifications
You must be signed in to change notification settings - Fork 9
/
physics.c
3409 lines (2794 loc) · 82.9 KB
/
physics.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 physics.c
\brief Problem independent physics
*/
#include "ko.h"
//********************************************************************
//calculates the state corresponding to the given primitives
//**********************************************************************
int
fill_struct_of_state(ldouble *pp, void* ggg, void* sss)
{
struct geometry *geom
= (struct geometry *) ggg;
struct struct_of_state *state
= (struct struct_of_state *) sss;
int i,j;
ldouble rho, uint, gamma, pgas, Tgas, Te, Ti, Sgas, STe, STi;
ldouble ucon[4], ucov[4], bcon[4], bcov[4], Bcon[4], bsq;
ldouble urfcon[4], urfcov[4], relgamma, Rij[4][4], Ehat, Trad, TradBB, kappaes;
struct opacities opac;
rho = pp[RHO];
uint = pp[UU];
state->rho = rho;
state->uint = uint;
gamma = GAMMA;
#ifndef FORCEGAMMAGASFIXED
#ifdef CONSISTENTGAMMA
gamma = pick_gammagas(geom->ix,geom->iy,geom->iz);
#endif
#endif
pgas = (gamma - 1.) * uint;
state->gamma = gamma;
state->pgas = pgas;
//ANDREW gas entropy is in different units than in pp[ENTR]!
Sgas = kB_over_mugas_mp*calc_Sfromu(rho, uint, geom->ix,geom->iy,geom->iz);
Tgas = calc_PEQ_Teifrompp(pp,&Te,&Ti,geom->ix,geom->iy,geom->iz);
state->Tgas = Tgas;
state->Sgas = Sgas;
ldouble netot=one_over_mue_mp*rho;
ldouble nnth=0.;
ldouble pnth=0.;
ldouble unth=0.;
#ifdef RELELECTRONS
nnth = calc_relel_ne(pp);
unth = calc_relel_uint(pp);
pnth = calc_relel_p(pp);
#endif
state->nenth=nnth;
state->uenth=unth;
state->penth=pnth;
state->ne = netot - nnth;
state->ni = one_over_mui_mp*rho;
state->Te = Te;
state->Ti = Ti;
state->pe = (state->ne)*K_BOLTZ*Te;
state->pi = (state->ni)*K_BOLTZ*Ti;
ldouble gammae=GAMMA;
ldouble gammai=GAMMA;
#ifdef GAMMAE
gammae=GAMMAE;
#endif
#ifdef GAMMAI
gammai=GAMMAI;
#endif
#if defined(CONSISTENTGAMMA) && !defined(FIXEDGAMMASPECIES)
gammae=calc_gammaintfromtemp(Te,ELECTRONS);
gammai=calc_gammaintfromtemp(Ti,IONS);
#endif
state->gammae=gammae;
state->gammai=gammai;
state->ue=(state->pe)/(gammae-1.);
state->ui=(state->pi)/(gammai-1.);
ldouble rhoeth = (state->ne)*MU_E*M_PROTON;
STe = calc_SefromrhoT(rhoeth, Te, ELECTRONS);
STi = calc_SefromrhoT(rho, Ti, IONS);
state->STe = STe;
state->STi = STi;
calc_ucon_ucov_from_prims(pp, geom, ucon, ucov);
DLOOPA(i)
{
state->ucon[i] = ucon[i];
state->ucov[i] = ucov[i];
}
#ifdef MAGNFIELD
calc_bcon_bcov_bsq_from_4vel(pp, ucon, ucov, geom, bcon, bcov, &bsq);
calc_Bcon_4vel(pp,ucon, bcon, Bcon);
DLOOPA(i)
{
state->bcon[i] = bcon[i];
state->bcov[i] = bcov[i];
state->Bcon[i] = Bcon[i];
}
state->bsq = bsq;
#else
state->bsq = 0.;
#endif
#ifdef RADIATION
calc_urcon_urcov_from_prims(pp, geom, urfcon, urfcov);
calc_Rij_M1(pp, geom, Rij);
calc_Ehat_from_Rij_ucov(Rij, ucov, &Ehat);
DLOOPA(i)
{
DLOOPA(j)
{
state->Rij[i][j] = Rij[i][j];
}
state->urfcon[i] = urfcon[i];
state->urfcov[i] = urfcov[i];
}
state->Ehat = Ehat;
TradBB = calc_LTE_TfromE(Ehat);
state->TradBB = TradBB;
#ifdef EVOLVEPHOTONNUMBER
relgamma = urfcon[0]*ucov[0] + urfcon[1]*ucov[1] +urfcon[2]*ucov[2] +urfcon[3]*ucov[3];
state->relgamma = relgamma;
ldouble nphhat = -relgamma * pp[NF];
Trad = calc_ncompt_Thatrad_nphhat(nphhat, Ehat);
#ifdef MAXDIFFTRADS
ldouble maxfac=MAXDIFFTRADS;
if (Trad > maxfac * TradBB)
{
pp[NF] *= (Trad / (maxfac * TradBB));
Trad = maxfac * TradBB;
}
#ifndef SYNCHROTRON
if (Trad < TradBB)
{
pp[NF] *= (Trad / TradBB);
Trad = TradBB;
}
#else
if (Trad < TradBB / maxfac)
{
pp[NF] *= (Trad * maxfac / TradBB);
Trad = TradBB / maxfac;
}
#endif //SYNCHROTRON
#endif //MAXDIFFTRADS
#else //EVOLVEPHOTONNUMBER
relgamma = -1.;
Trad = TradBB;
#endif
state->relgamma = relgamma;
state->Trad = Trad;
kappaes = calc_kappaes_with_temperatures(rho, Tgas, Te, Ti, Trad);
state->kappaes = kappaes;
ldouble kappaGasAbs, kappaRadAbs, kappaGasNum, kappaRadNum, kappaGasRoss, kappaRadRoss;
ldouble kappa = calc_kappa_from_state(pp, state, geom, &opac);
kappaGasAbs = opac.kappaGasAbs;
kappaRadAbs = opac.kappaRadAbs;
kappaGasNum = opac.kappaGasNum;
kappaRadNum = opac.kappaRadNum;
kappaGasRoss = opac.kappaGasRoss;
kappaRadRoss = opac.kappaRadRoss;
state->opac.kappaGasAbs = kappaGasAbs;
state->opac.kappaRadAbs = kappaRadAbs;
state->opac.kappaGasNum = kappaGasNum;
state->opac.kappaRadNum = kappaRadNum;
state->opac.kappaGasRoss = kappaGasRoss;
state->opac.kappaRadRoss = kappaRadRoss;
#endif // ifdef RADIATION
return 0;
}
//**********************************************************************
//Copies one struct_of_state to another
//**********************************************************************
int copy_state_to_state(void *sss1, void *sss2)
{
int i, j;
struct struct_of_state *state1
= (struct struct_of_state *) sss1;
struct struct_of_state *state2
= (struct struct_of_state *) sss2;
state2->rho = state1->rho;
state2->uint = state1->uint;
state2->gamma = state1->gamma;
state2->pgas = state1->pgas;
state2->Tgas = state1->Tgas;
state2->Te = state1->Te;
state2->Ti = state1->Ti;
state2->Sgas = state1->Sgas;
state2->STe = state1->STe;
state2->STi = state1->STi;
state2->ne = state1->ne;
state2->pe = state1->pe;
state2->ue = state1->ue;
state2->ni = state1->ni;
state2->ui = state1->ui;
state2->pi = state1->pi;
state2->nenth = state1->nenth;
state2->uenth = state1->uenth;
state2->penth = state1->penth;
for (i = 0; i < 4; i++)
{
state2->ucon[i] = state1->ucon[i];
state2->ucov[i] = state1->ucov[i];
}
#ifdef MAGNFIELD
for (i = 0; i < 4; i++)
{
state2->bcon[i] = state1->bcon[i];
state2->bcov[i] = state1->bcov[i];
state2->Bcon[i] = state1->Bcon[i];
}
state2->bsq = state1->bsq;
#endif
#ifdef RADIATION
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
state2->Rij[i][j] = state1->Rij[i][j];
}
state2->urfcon[i] = state1->urfcon[i];
state2->urfcov[i] = state1->urfcov[i];
}
state2->Ehat = state1->Ehat;
state2->TradBB = state1->TradBB;
state2->relgamma = state1->relgamma;
state2->Trad = state1->Trad;
state2->kappaes = state1->kappaes;
state2->opac.kappaGasAbs = state1->opac.kappaGasAbs;
state2->opac.kappaRadAbs = state1->opac.kappaRadAbs;
state2->opac.kappaGasNum = state1->opac.kappaGasNum;
state2->opac.kappaRadNum = state1->opac.kappaRadNum;
state2->opac.kappaGasRoss = state1->opac.kappaGasRoss;
state2->opac.kappaRadRoss = state1->opac.kappaRadRoss;
#endif // ifdef RADIATION
return 0;
}
//**********************************************************************
// Updates the state for a new value of nphoton
//**********************************************************************
int update_state_for_nphoton(ldouble *pp, void *ggg, void *sss)
{
struct geometry *geom
= (struct geometry *) ggg;
struct struct_of_state *state
= (struct struct_of_state *) sss;
// Trad changes when nphoton is changed and this also affects the opacities
#ifdef EVOLVEPHOTONNUMBER
ldouble nphhat = -state->relgamma * pp[NF];
state->Trad = calc_ncompt_Thatrad_nphhat(nphhat, state->Ehat);
ldouble kappaes = calc_kappaes_with_temperatures(state->rho, state->Tgas, state->Te, state->Ti, state->Trad);
state->kappaes = kappaes;
struct opacities opac;
ldouble kappaGasAbs, kappaRadAbs, kappaGasNum, kappaRadNum, kappaGasRoss, kappaRadRoss;
ldouble kappa=calc_kappa_from_state(pp, state, geom, &opac);
kappaGasAbs = opac.kappaGasAbs;
kappaRadAbs = opac.kappaRadAbs;
kappaGasNum = opac.kappaGasNum;
kappaRadNum = opac.kappaRadNum;
kappaGasRoss = opac.kappaGasRoss;
kappaRadRoss = opac.kappaRadRoss;
state->opac.kappaGasAbs = kappaGasAbs;
state->opac.kappaRadAbs = kappaRadAbs;
state->opac.kappaGasNum = kappaGasNum;
state->opac.kappaRadNum = kappaRadNum;
state->opac.kappaGasRoss = kappaGasRoss;
state->opac.kappaRadRoss = kappaRadRoss;
#endif // no change otherwise
return 0;
}
//**********************************************************************
//calculates fluid quantities for thermal electrons
//**********************************************************************
//Total number density of thermal electrons
ldouble calc_thermal_ne(ldouble *pp)
{
ldouble ne_relel=0.0;
ldouble ne_tot;
ldouble ne_th;
ne_tot = one_over_mue_mp * pp[RHO];
#ifdef RELELECTRONS
ne_relel=calc_relel_ne(pp);
//if(ne_relel/ne_tot > MAX_RELEL_FRAC_N)
// ne_relel = MAX_RELEL_FRAC_N*ne_tot;
#endif
return ne_tot - ne_relel;
}
//*****************************************************
//calculates left and right wave speeds at cell center
//******************************************************
// July 8, 17, Ramesh: This version computes wavespeeds in all three directions simultaneously,
// and save a little by not repeating certain direction-independent quantities.
// Also, computes wavespeeds only for the relevant directions.
//*****************************************************
//calculates left and right wave speeds at cell center
//*****************************************************
int
calc_wavespeeds_lr(int ix, int iy, int iz, ldouble *aaa)
{
ldouble (*gg)[5],(*GG)[5];
struct geometry geom;
fill_geometry(ix,iy,iz,&geom);
//temporary using local arrays
gg=geom.gg;
GG=geom.GG;
ldouble pp[NV];
int iv;
//picking up primitives
for(iv=0;iv<NV;iv++)
pp[iv]=get_u(p,iv,ix,iy,iz);
calc_wavespeeds_lr_pure(pp,&geom,aaa);
return 0;
}
//*************************************************
//calculates left and right wave speeds at cell center
//*************************************************
int
calc_wavespeeds_lr_pure(ldouble *pp,void *ggg,ldouble *aaa)
{
struct geometry *geom
= (struct geometry *) ggg;
ldouble (*gg)[5],(*GG)[5],gdet,gdetu;
gg=geom->gg;
GG=geom->GG;
int iv;
ldouble axhdl,axhdr,ayhdl,ayhdr,azhdl,azhdr;
ldouble axl0,axr0,ayl0,ayr0,azl0,azr0;
ldouble axl,axr,ayl,ayr,azl,azr;
axl=axr=ayl=ayr=azl=azr=1.;
ldouble utcon[4],ucon[4],ucov[4],cst1,cst2,cst3,cst4;
ldouble bcon[4],bcov[4],bsq;
ldouble cs2,va2,EF,EEloc;
ldouble rho,uu,pre,prerad;
//**********************************************************************
//***** four velocity **************************************************
//**********************************************************************
for(iv=1;iv<4;iv++)
utcon[iv]=pp[1+iv];
utcon[0]=0.;
conv_vels_both(utcon,ucon,ucov,VELPRIM,VEL4,gg,GG);
//**********************************************************************
//***** hydro: speed of sound ******************************************
//**********************************************************************
rho=pp[RHO];
uu=pp[UU];
ldouble gamma=GAMMA;
#ifdef CONSISTENTGAMMA
gamma=pick_gammagas(geom->ix,geom->iy,geom->iz);
#endif
ldouble gammam1=gamma-1.;
//gas pressure
pre=(gamma-1.)*uu;
ldouble preeff=pre;
ldouble uueff=uu;
#ifdef GASRADCOUPLEDWAVESPEEDS
#ifdef RADIATION // gas-radiation coupling is needed only with radiation
//radiation pressure
ldouble temp[4],Ehat;
calc_ff_Ehat(pp,&Ehat,temp,geom);
prerad=one_third*Ehat;
ldouble fcouple; //coupling coefficient
// Choose which version of gas-radiation coupling to use
#ifdef GASRADCOUPLEDWAVESPEEDS_SIMPLE
// Simple scheme just uses the total pressure to estimate hydro wave speed
// Still doubtful if this works
fcouple = 1.;
preeff += prerad;
uueff += Ehat;
#else
// More sophisticated method, which estimates the degree of coupling between gas and radiation (fcouple) and estimates the effective pressure for hydro wave speed
fcouple=estimate_gas_radiation_coupling(pp,ggg);
if (isnan(fcouple)) fcouple = 1.; // Ramesh: needed sometimes near the inner boundary
preeff += fcouple*prerad;
uueff += fcouple*Ehat;
#endif // ifdef GASRADCOUPLEDWAVESPEEDS_SIMPLE
#endif // ifdef RADIATION
#endif // ifdef GASRADCOUPLEDWAVESPEEDS
//Sound speed
//ANDREW changed the denominator
//cs2=gamma*preeff/(rho+uu+preeff); //old version
cs2=gamma*preeff/(rho+uueff+preeff); //new version
if(cs2>0.95) cs2=0.95;
//**********************************************************************
//***** magn: alfvenic speed ****** *************************************
//**********************************************************************
va2=0.;
#ifdef MAGNFIELD
calc_bcon_bcov_bsq_from_4vel(pp, ucon, ucov, geom, bcon, bcov, &bsq);
EF = rho + uu + pre;
EEloc = bsq + EF ;
va2 = bsq/EEloc ;
if(va2<0.) va2=0.;
#endif
//**********************************************************************
//***** mhd: fast magnetosonic speed ***********************************
//**********************************************************************
ldouble vtot2; //total characteristic velocity
vtot2=cs2 + va2 - cs2*va2;
#ifdef NONRELMHD //non-rel version
ldouble vx,vy,vz,cs,csx,csy,csz;
vx=pp[VX];
vy=pp[VY];
vz=pp[VZ];
cs=sqrt(vtot2);
csx=cs/sqrt(gg[1][1]);
csy=cs/sqrt(gg[2][2]);
csz=cs/sqrt(gg[3][3]);
axhdr=vx+csx;
axhdl=vx-csx;
ayhdr=vy+csy;
ayhdl=vy-csy;
azhdr=vz+csz;
azhdl=vz-csz;
#else //fully relativistic
//**********************************************************************
//algorithm from HARM to transform the fluid frame wavespeed into lab frame
//**********************************************************************
ldouble aret[6];
int ret;
ret=calc_wavespeeds_lr_core(ucon,GG,aret,vtot2,vtot2,vtot2);
if(ret<0) {printf("error occurred at %d | %d | %d\n",geom->ix,geom->iy,geom->iz);}
axhdl=aret[0];
axhdr=aret[1];
ayhdl=aret[2];
ayhdr=aret[3];
azhdl=aret[4];
azhdr=aret[5];
#endif
#ifdef RADIATION
//**********************************************************************
//***** radiation: characteristic wave speed ***************************
//**********************************************************************
ldouble aval[18];
int verbose=0;
//physical size of the cell
ldouble dx[3];
ldouble xx[4]={0.,geom->xx,geom->yy,geom->zz};
//ix,iy,iz could be the indices of a face, so the depth taken from left/right
dx[0]=my_max(get_size_x(geom->ix,0)*sqrt(gg[1][1]),get_size_x(geom->ix+1,0)*sqrt(gg[1][1]));
dx[1]=my_max(get_size_x(geom->iy,1)*sqrt(gg[2][2]),get_size_x(geom->iy+1,1)*sqrt(gg[2][2]));
dx[2]=my_max(get_size_x(geom->iz,2)*sqrt(gg[3][3]),get_size_x(geom->iz+1,2)*sqrt(gg[3][3]));
ldouble tautot[3];
calc_tautot(pp,geom,dx,tautot);
//compute radiative wavespeeds
calc_rad_wavespeeds(pp,geom,tautot,aval,verbose);
//unlimited by optical depth
axl0=aval[0];
axr0=aval[1];
ayl0=aval[2];
ayr0=aval[3];
azl0=aval[4];
azr0=aval[5];
//affected by optical depth - by default scaled as 1/tau
axl=aval[6+0];
axr=aval[6+1];
ayl=aval[6+2];
ayr=aval[6+3];
azl=aval[6+4];
azr=aval[6+5];
//in the other approach - radiative wavespeeds unlimited in optically thin medium (fcouple==0)
//and equal to gas wavespeeds in optically thick medium (fcouple==1)
#ifdef GASRADCOUPLEDWAVESPEEDS
axl=fcouple*axhdl+(1.-fcouple)*axl;
axr=fcouple*axhdr+(1.-fcouple)*axr;
ayl=fcouple*ayhdl+(1.-fcouple)*ayl;
ayr=fcouple*ayhdr+(1.-fcouple)*ayr;
azl=fcouple*azhdl+(1.-fcouple)*azl;
azr=fcouple*azhdr+(1.-fcouple)*azr;
#endif
#endif //RADIATION
#ifdef OVERWRITERADWAVESPEEDSWITHHD
axl=axl0=axl2=axhdl;
axr=axr0=axr2=axhdr;
ayl=ayl0=ayl2=ayhdl;
ayr=ayr0=ayr2=ayhdr;
azl=azl0=azl2=azhdl;
azr=azr0=azr2=azhdr;
#endif
//zeroing 'co-going' velocities
if(axhdl>0.) axhdl=0.;
if(axhdr<0.) axhdr=0.;
if(ayhdl>0.) ayhdl=0.;
if(ayhdr<0.) ayhdr=0.;
if(azhdl>0.) azhdl=0.;
if(azhdr<0.) azhdr=0.;
if(axl>0.) axl=0.;
if(axr<0.) axr=0.;
if(ayl>0.) ayl=0.;
if(ayr<0.) ayr=0.;
if(azl>0.) azl=0.;
if(azr<0.) azr=0.;
if(axl0>0.) axl0=0.;
if(axr0<0.) axr0=0.;
if(ayl0>0.) ayl0=0.;
if(ayr0<0.) ayr0=0.;
if(azl0>0.) azl0=0.;
if(azr0<0.) azr0=0.;
//saving and passing up
//hd:
aaa[0]=axhdl;
aaa[1]=axhdr;
aaa[2]=ayhdl;
aaa[3]=ayhdr;
aaa[4]=azhdl;
aaa[5]=azhdr;
//rad:
//unlimited by optical depth - used for calculation of timestep
aaa[6]=axl0;
aaa[7]=axr0;
aaa[8]=ayl0;
aaa[9]=ayr0;
aaa[10]=azl0;
aaa[11]=azr0;
//affected by optical depth
aaa[6+6]=axl;
aaa[6+7]=axr;
aaa[6+8]=ayl;
aaa[6+9]=ayr;
aaa[6+10]=azl;
aaa[6+11]=azr;
return 0;
}
int
calc_wavespeeds_lr_core(ldouble *ucon, ldouble GG[][5], ldouble *aret,
ldouble wspeed2x, ldouble wspeed2y, ldouble wspeed2z)
{
int ierr = 0;
ldouble Acov[4], Acon[4], Bcov[4], Bcon[4];
ldouble Asq, Bsq, Au, Bu, AB, Au2, Bu2, AuBu, A, B, discr, cst1, cst2;
// Compute direction-independent quantities first
Bcov[0] = 1.;
Bcov[1] = 0.;
Bcov[2] = 0.;
Bcov[3] = 0.;
indices_12(Bcov, Bcon, GG);
Bsq = dot(Bcon, Bcov);
Bu = dot(Bcov, ucon);
Bu2 = Bu * Bu;
// Now work on the relevant directions
if (TNX > 1) // x-direction is needed
{
Acov[0] = 0.;
Acov[1] = 1.;
Acov[2] = 0.;
Acov[3] = 0.;
indices_12(Acov, Acon, GG);
Asq = dot(Acon, Acov);
Au = dot(Acov, ucon);
AB = dot(Acon, Bcov);
Au2 = Au * Au;
AuBu = Au * Bu;
B = -2. * (AuBu * (1.0 - wspeed2x) - AB * wspeed2x);
A = Bu2 * (1.0 - wspeed2x) - Bsq * wspeed2x;
discr = 4.0 * wspeed2x * ((AB * AB - Asq * Bsq) * wspeed2x + (2.0 * AB * Au * Bu - Asq * Bu2 - Bsq * Au2) * (wspeed2x - 1.0));
if(discr < 0.) {printf("discr in x-wavespeeds lt 0\n"); ierr = -1;}
discr = sqrt(discr);
cst1 = (-B + discr) / (2. * A);
cst2 = (-B - discr) / (2. * A);
if(cst2 > cst1)
{
aret[0] = cst1; aret[1] = cst2;
}
else
{
aret[0] = cst2; aret[1] = cst1;
}
}
if (TNY > 1) // y-direction is needed
{
Acov[0] = 0.;
Acov[1] = 0.;
Acov[2] = 1.;
Acov[3] = 0.;
indices_12(Acov, Acon, GG);
Asq = dot(Acon, Acov);
Au = dot(Acov, ucon);
AB = dot(Acon, Bcov);
Au2 = Au * Au;
AuBu = Au * Bu;
B = -2. * (AuBu * (1.0 - wspeed2y) - AB * wspeed2y);
A = Bu2 * (1.0 - wspeed2y) - Bsq * wspeed2y;
discr = 4.0 * wspeed2y * ((AB * AB - Asq * Bsq) * wspeed2y + (2.0 * AB * Au * Bu - Asq * Bu2 - Bsq * Au2) * (wspeed2y - 1.0));
if(discr < 0.) {printf("discr in y-wavespeeds lt 0\n"); ierr = -1;}
discr = sqrt(discr);
cst1 = (-B + discr) / (2. * A);
cst2 = (-B - discr) / (2. * A);
if(cst2 > cst1)
{
aret[2] = cst1; aret[3] = cst2;
}
else
{
aret[2] = cst2; aret[3] = cst1;
}
}
if (TNZ > 1) // z-direction is needed
{
Acov[0] = 0.;
Acov[1] = 0.;
Acov[2] = 0.;
Acov[3] = 1.;
indices_12(Acov, Acon, GG);
Asq = dot(Acon, Acov);
Au = dot(Acov, ucon);
AB = dot(Acon, Bcov);
Au2 = Au * Au;
AuBu = Au * Bu;
B = -2. * (AuBu * (1.0 - wspeed2z) - AB * wspeed2z);
A = Bu2 * (1.0 - wspeed2z) - Bsq * wspeed2z;
discr = 4.0 * wspeed2z * ((AB * AB - Asq * Bsq) * wspeed2z + (2.0 * AB * Au * Bu - Asq * Bu2 - Bsq * Au2) * (wspeed2z - 1.0));
if(discr < 0.) {printf("discr in z-wavespeeds lt 0\n"); ierr = -1;}
discr = sqrt(discr);
cst1 = (-B + discr) / (2. * A);
cst2 = (-B - discr) / (2. * A);
if(cst2 > cst1)
{
aret[4] = cst1; aret[5] = cst2;
}
else
{
aret[4] = cst2; aret[5] = cst1;
}
}
if (ierr == 0)
{
return 0;
}
else
{
return -1;
}
}
//*************************************************************
//returns geometrical source terms for all conserved quantities
//*************************************************************
int f_metric_source_term_arb(ldouble *pp,void *ggg,ldouble *ss)
{
int i;
struct geometry *geom
= (struct geometry *) ggg;
ldouble (*gg)[5],(*GG)[5],gdet,gdetu;
int ix,iy,iz;
ix=geom->ix;
iy=geom->iy;
iz=geom->iz;
gg=geom->gg;
GG=geom->GG;
gdet=geom->gdet;
gdetu=gdet;
#if (GDETIN==0) //no metric determinant inside derivatives
gdetu=1.;
#endif
ldouble dlgdet[3];
dlgdet[0]=gg[0][4]; //D[gdet,x1]/gdet
dlgdet[1]=gg[1][4]; //D[gdet,x2]/gdet
dlgdet[2]=gg[2][4]; //D[gdet,x3]/gdet
//calculating stress energy tensor components
ldouble T[4][4];
calc_Tij(pp,geom,T);
indices_2221(T,T,gg);
int ii, jj;
for(ii=0;ii<4;ii++)
for(jj=0;jj<4;jj++)
{
if(isnan(T[ii][jj]))
{
printf("%d %d %e\n",ii,jj,T[ii][jj]);
my_err("nan in metric_source_terms\n");
}
}
ldouble rho=pp[RHO];
ldouble u=pp[UU];
ldouble vcon[4],ucon[4];
vcon[1]=pp[VX];
vcon[2]=pp[VY];
vcon[3]=pp[VZ];
ldouble S=pp[ENTR];
//converting to 4-velocity
conv_vels(vcon,ucon,VELPRIM,VEL4,gg,GG);
int k,l,iv;
for(iv=0;iv<NV;iv++)
ss[iv]=0.; // zero out all source terms initially
#ifdef RADIATION
ldouble Rij[4][4];
calc_Rij(pp,geom,Rij); //R^ij
indices_2221(Rij,Rij,gg); //R^i_j
//terms with Christoffels
for(k=0;k<4;k++)
for(l=0;l<4;l++)
{
ss[UU]+=gdetu*T[k][l]*get_gKr(l,0,k,ix,iy,iz);
ss[VX]+=gdetu*T[k][l]*get_gKr(l,1,k,ix,iy,iz);
ss[VY]+=gdetu*T[k][l]*get_gKr(l,2,k,ix,iy,iz);
ss[VZ]+=gdetu*T[k][l]*get_gKr(l,3,k,ix,iy,iz);
ss[EE0]+=gdetu*Rij[k][l]*get_gKr(l,0,k,ix,iy,iz);
ss[FX0]+=gdetu*Rij[k][l]*get_gKr(l,1,k,ix,iy,iz);
ss[FY0]+=gdetu*Rij[k][l]*get_gKr(l,2,k,ix,iy,iz);
ss[FZ0]+=gdetu*Rij[k][l]*get_gKr(l,3,k,ix,iy,iz);
}
#if (GDETIN==0) //terms with dloggdet if gdet not inside the derivatives
#ifdef EVOLVEPHOTONNUMBER
ldouble urfcon[4];
urfcon[0]=0.;
urfcon[1]=pp[FX0];
urfcon[2]=pp[FY0];
urfcon[3]=pp[FZ0];
conv_vels(urfcon,urfcon,VELPRIMRAD,VEL4,gg,GG);
#endif
for(l=1;l<4;l++)
{
ss[RHO]+=-dlgdet[l-1]*rho*ucon[l];
ss[UU]+=-dlgdet[l-1]*(T[l][0]+rho*ucon[l]);
ss[VX]+=-dlgdet[l-1]*(T[l][1]);
ss[VY]+=-dlgdet[l-1]*(T[l][2]);
ss[VZ]+=-dlgdet[l-1]*(T[l][3]);
ss[ENTR]+=-dlgdet[l-1]*S*ucon[l];
ss[EE0]+=-dlgdet[l-1]*(Rij[l][0]);
ss[FX0]+=-dlgdet[l-1]*(Rij[l][1]);
ss[FY0]+=-dlgdet[l-1]*(Rij[l][2]);
ss[FZ0]+=-dlgdet[l-1]*(Rij[l][3]);
#ifdef EVOLVEPHOTONNUMBER
ss[NF0]+=-dlgdet[l-1]*pp[NF0]*urfcon[l];
#endif
#ifdef EVOLVEELECTRONS
ss[ENTRE] += -dlgdet[l-1]*pp[ENTRE]*ucon[l];
ss[ENTRI] += -dlgdet[l-1]*pp[ENTRI]*ucon[l];
#ifdef RELELECTRONS
int ie;
for (ie=0; ie<NRELBIN; ie++)
{
ss[NEREL(ie)]+=-dlgdet[l-1]*pp[NEREL(ie)]*ucon[l];
}
#endif
#endif
}
#endif //GDETIN
#else //no RADIATION , pure hydro
//terms with Christoffels
for(k=0;k<4;k++)
for(l=0;l<4;l++)
{
ss[UU]+=gdetu*T[k][l]*get_gKr(l,0,k,ix,iy,iz);
ss[VX]+=gdetu*T[k][l]*get_gKr(l,1,k,ix,iy,iz);
ss[VY]+=gdetu*T[k][l]*get_gKr(l,2,k,ix,iy,iz);
ss[VZ]+=gdetu*T[k][l]*get_gKr(l,3,k,ix,iy,iz);
}
//terms with dloggdet
#if (GDETIN==0)
for(l=1;l<4;l++)
{
ss[RHO]+=-dlgdet[l-1]*rho*ucon[l];
ss[UU]+=-dlgdet[l-1]*(T[l][0]+rho*ucon[l]);
ss[VX]+=-dlgdet[l-1]*(T[l][1]);
ss[VY]+=-dlgdet[l-1]*(T[l][2]);
ss[VZ]+=-dlgdet[l-1]*(T[l][3]);
ss[ENTR]+=-dlgdet[l-1]*S*ucon[l];
}
#endif
#endif //RADIATION
#ifdef FORCEFREE
//calculating stress energy tensor components
ldouble T_ff[4][4];
calc_Tij_ff(pp,geom,T_ff);
indices_2221(T_ff,T_ff,gg);
for(ii=0;ii<4;ii++)
for(jj=0;jj<4;jj++)
{
if(isnan(T_ff[ii][jj]))
{
printf("%d %d %e\n",ii,jj,T[ii][jj]);
my_err("nan in force free metric_source_terms\n");
}
}
//terms with Christoffels
for(k=0;k<4;k++)
for(l=0;l<4;l++)
{
ss[VXFF]+=gdetu*T_ff[k][l]*get_gKr(l,1,k,ix,iy,iz);
ss[VYFF]+=gdetu*T_ff[k][l]*get_gKr(l,2,k,ix,iy,iz);
ss[VZFF]+=gdetu*T_ff[k][l]*get_gKr(l,3,k,ix,iy,iz);
}
//parallel velocity/enthalpy term
#ifndef FORCEFREE_PARALLEL_COLD // neglect pressure
#ifndef NO_FORCEFREE_PARALL_SOURCETERM
int derdir[3]={0,0,0};
ldouble uuffsource=calc_uuff_source(pp, geom,derdir);
ss[UUFF]=uuffsource;
#endif
#else
#endif
#endif //FORCEFREE
#ifdef SHEARINGBOX
//signs the same despite rho u^i u_t evolved because in koral source terms on lhs
//-2 rho (Omega \hat z) x \vec v
ss[VX]+=gdet*(2.*rho*SHEAROM*pp[VY]);
ss[VY]+=gdet*(-2.*rho*SHEAROM*pp[VX]);
ss[VZ]+=0.;
//2 q Omega^2 x \hat x
ss[VX]+=gdet*(2.*SHEARQ*rho*SHEAROM*SHEAROM*geom->xx);
ss[VY]+=0.;
ss[VZ]+=0.;
//-rho Omega^2 z \hat z
ss[VX]+=0.;
ss[VY]+=0.;
#ifndef SHEARINGBOXUNSTRATIFIED
ss[VZ]+=-gdet*rho*SHEAROM*SHEAROM*geom->zz;
#endif
//Omega^2 rho \vec v 2 q x \hat x
ss[UU]+=gdet*SHEAROM*SHEAROM*rho*2.*SHEARQ*geom->xx*pp[VX];
//Omega^2 rho \vec v z \hat z
ss[UU]+=gdet*SHEAROM*SHEAROM*rho*geom->zz*pp[VZ];
#endif