-
Notifications
You must be signed in to change notification settings - Fork 9
/
nonthermal.c
2105 lines (1774 loc) · 52.1 KB
/
nonthermal.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 nonthermal.c
\brief Routines relating to nonthermal relativistic electrons
*/
#include "ko.h"
//*********************************************/
//* set arrays for gamma faces and centers
//*********************************************/
//ANDREW changed so RELGAMMAMIN and RELGAMMAMAX are the lower and upper bin edges
int set_relel_gammas()
{
#ifdef RELELECTRONS
int ie;
ldouble relel_log_gammas[NRELBIN];
ldouble relel_log_gammas_e[NRELBIN+1];
#ifdef RELEL_INTEGRATE_SIMPSON // Simpson's rule in log space -- no bin edges
if(NRELBIN % 2 == 0) {printf("NRELBIN MUST BE ODD TO USE SIMPSON"); getch(); return -1;}
log10binspace = (log10(RELGAMMAMAX) - log10(RELGAMMAMIN))/((double)(NRELBIN-1.));
logbinspace = log10binspace * log(10.);
logbinspace_inv = 1./logbinspace;
binspace = pow(10., log10binspace);
binspace_inv = 1./binspace;
if(PROCID==0) printf("nonthermal bin spacing: %e %e \n",logbinspace,binspace);
relel_gammas_e[0] = RELGAMMAMIN;
relel_log_gammas_e[0] = log(RELGAMMAMIN);
relel_log_gammas[0] = log(RELGAMMAMIN);
relel_gammas[0] = RELGAMMAMIN;
#else // rectangular bins with separate edges
log10binspace = (log10(RELGAMMAMAX) - log10(RELGAMMAMIN))/((double)(NRELBIN));
logbinspace = log10binspace * log(10.);
logbinspace_inv = 1./logbinspace;
binspace = pow(10., log10binspace);
binspace_inv = 1./binspace;
if(PROCID==0) printf("nonthermal bin spacing: %e %e \n",logbinspace,binspace);
relel_gammas_e[0] = RELGAMMAMIN;
relel_log_gammas_e[0] = log(RELGAMMAMIN);
relel_log_gammas[0] = relel_log_gammas_e[0] + 0.5*logbinspace;
relel_gammas[0] = exp(relel_log_gammas[0]);
#endif
relel_gammas_inv[0] = 1./relel_gammas[0];
relel_gammas_e_inv[0] = 1./relel_gammas_e[0];
// gammas of bin centers and and edges
for(ie=1; ie<NRELBIN; ie++)
{
relel_log_gammas_e[ie] = relel_log_gammas_e[ie-1] + logbinspace;
relel_log_gammas[ie] = relel_log_gammas[ie-1] + logbinspace;
relel_gammas_e[ie] = exp(relel_log_gammas_e[ie]);
relel_gammas[ie] = exp(relel_log_gammas[ie]);
relel_gammas_inv[ie] = 1./relel_gammas[ie];
relel_gammas_e_inv[ie] = 1./relel_gammas_e[ie];
}
relel_log_gammas_e[NRELBIN] = log(RELGAMMAMAX);
relel_gammas_e[NRELBIN] = RELGAMMAMAX; //equal to relel_gammas_e[NRELBIN-1] for simpson
relel_gammas_e_inv[NRELBIN] = 1./RELGAMMAMAX;
#ifdef RELEL_CONST_INJPARAMS
my_err("RELEL_CONST_INJPARAMS is deprecated, use RELEL_HEAT_FIX_LIMITS instead!");
#endif
//precompute injection arrays
#if defined(RELEL_HEAT_FIX_LIMITS) && defined(RELEL_HEAT_FIX_INDEX)
ldouble p_index = RELEL_HEAT_INDEX;
ldouble injmax = RELEL_INJ_MAX;
ldouble injmin = RELEL_INJ_MIN;
//normalization
if(p_index < 0)
if(PROCID==0)
printf("WARNING: RELEL_HEAT_INDEX < 0 corresponds to positive power law!");
if(p_index!=2 && p_index!=1)
{
xx_relel_inj = (pow(injmax, 2.-p_index) - pow(injmin, 2.-p_index))/(2.-p_index) - (pow(injmax, 1.-p_index) - pow(injmin, 1.-p_index))/(1.-p_index);
}
else if (p_index == 2)
{
xx_relel_inj = 1./injmax - 1./injmin + log(injmax/injmin);
}
else if (p_index == 1)
{
xx_relel_inj = injmax - injmin - log(injmax/injmin);
}
//power array
for(ie=0; ie<NRELBIN; ie++) relel_injpow[ie] = pow(relel_gammas[ie], -p_index);
#endif
#if defined(RELEL_HEAT_FIX_FRAC) && defined(RELEL_HEAT_FIX_INDEX)
ldouble eta= RELEL_HEAT_FRAC/(1.-RELEL_HEAT_FRAC);
ldouble lhs = 6.*(RELEL_HEAT_INDEX-2.)*eta;
ldouble a = 0.25 * pow(lhs, 0.25);
gamma_inj_fixed_ratio = -4.*gsl_sf_lambert_Wm1(-a);
#endif
#endif
return 0;
}
//*******************************************************
//calculates fluid quantities for relativistic electrons
//*******************************************************
//Numerically integrate an array q of size NRELBIN
ldouble integrate_relel_q(ldouble *q)
{
int ie;
ldouble dgamma;
ldouble qint=0.;
#ifdef RELELECTRONS
#if defined(RELEL_INTEGRATE_SIMPSON) // Simpson's rule in log space
dgamma = one_third*logbinspace;
for (ie=1; ie<=(NRELBIN-1)/2; ie++)
{
qint += relel_gammas[2*ie-2]*q[2*ie-2];
qint += 4.*relel_gammas[2*ie-1]*q[2*ie-1];
qint += relel_gammas[2*ie]*q[2*ie];
}
qint *= dgamma;
#elif defined(RELEL_INTEGRATE_LOGSPACE) // rectangular integration in log space
dgamma = logbinspace;
for (ie=0; ie<NRELBIN; ie++)
{
qint += dgamma*relel_gammas[ie]*q[ie];
}
#else // rectangular integration in regular space
for (ie=0; ie<NRELBIN; ie++)
{
dgamma = relel_gammas_e[ie+1]-relel_gammas_e[ie];
qint += (dgamma*q[ie]);
}
#endif
#endif
return qint;
}
//Total number density of relativistic electrons
ldouble calc_relel_ne(ldouble *pp)
{
ldouble ne_relel=0.;
#ifdef RELELECTRONS
ldouble ne_tot=pp[RHO]*one_over_mue_mp;
ldouble q[NRELBIN];
ldouble dgamma;
int ie;
for (ie=0; ie<NRELBIN; ie++)
{
q[ie] = pp[NEREL(ie)];
}
ne_relel = integrate_relel_q(q);
if (ne_relel < 0.)
ne_relel=0.;
#endif
return ne_relel;
}
//Total energy density of relativistic electrons
ldouble calc_relel_uint(ldouble *pp)
{
ldouble u_relel=0.;
#ifdef RELELECTRONS
ldouble g2, g1;
ldouble dgamma;
int ie;
ldouble q[NRELBIN];
for (ie=0; ie<NRELBIN; ie++)
{
q[ie] = pp[NEREL(ie)]*(relel_gammas[ie]-1.);
}
u_relel = integrate_relel_q(q);
u_relel *= M_ELECTR;
if (u_relel < 0.)
u_relel=0.;
#endif
return u_relel;
}
//Total pressure of relativistic electrons
ldouble calc_relel_p(ldouble *pp)
{
ldouble p_relel=0.;
#ifdef RELELECTRONS
ldouble p2, p1;
ldouble dgamma;
int ie;
ldouble q[NRELBIN];
for (ie=0; ie<NRELBIN; ie++)
{
q[ie] = pp[NEREL(ie)]*(relel_gammas[ie]-relel_gammas_inv[ie]);
}
p_relel = integrate_relel_q(q);
p_relel *= (M_ELECTR/3.);
if (p_relel < 0.)
p_relel=0.;
#endif
return p_relel;
}
//*********************************************/
//* Determine gamma_inj_min and gamma_inj_max
//*********************************************/
//this is an approximate form for calculating gamma_inj_min
//it is exact when gamma_inj_max ->infinity and theta>1
ldouble calc_gammainj_min_jointhermal(ldouble theta, ldouble delta_nth, ldouble p_index, ldouble gammamax)
{
ldouble ratio=1., gamma_min=0;
#ifdef RELELECTRONS
#ifdef RELEL_HEAT_FIX_LIMITS
gamma_min=RELEL_INJ_MIN;
#else
#if defined CALC_GAMMAMIN_FIT
// fitting function to exact solution, works will all p>1
// see mathematica notebook plaw_tail_full_simple.nb
ldouble p0 = 2.2;
ldouble y0 = 5.1;
ldouble y1 = 2.01;
ldouble al = 2.4;
ldouble be = 0.6;
ldouble de = 0.04;
ldouble eta= delta_nth/(1.-delta_nth);
ldouble gamma_pk = 2*theta;
ldouble y;
if(p_index < 1)
my_err("in calc_gammainj_min, injection power law index must be >= 1!");
else if(p_index < p0)
y = y0 * pow(gammamax, de*(p0-p_index));
else
{
ldouble pmax = al*pow(eta,-be);
if(p_index<pmax)
{
ldouble aa = y1/y0;
ldouble bb = (p0 - p_index) / (p0 - pmax);
y = y0 * pow(aa, bb);
}
else
y = y1;
}
gamma_min = y * gamma_pk;
#elif defined(CALC_GAMMAMIN_FAST) // assumes p > 2
if(p_index <= 2.) my_err("in calc_gammainj_min, injection power law index must be > 2!");
#if defined(RELEL_HEAT_FIX_FRAC) && defined(RELEL_HEAT_FIX_INDEX)
ratio = gamma_inj_fixed_ratio;
#else
ldouble eta= delta_nth/(1.-delta_nth);
ldouble lhs = 6.*(p_index-2.)*eta;
ldouble a = 0.25 * pow(lhs, 0.25);
ratio = -4.*gsl_sf_lambert_Wm1(-a);
#endif
gamma_min = ratio*theta;
#else
// iterative -- assumes p > 2
ldouble ep=1.e-8;
if(fabs(p_index-2.0) < ep)
p_index = 2.0 + ep;
ldouble eta= delta_nth/(1.-delta_nth);
ldouble gmin_init = 10*theta;
ldouble gamma_pk = 2*theta;
ldouble gmin_guess = gmin_init;
ldouble converge_crit = 0.05;
int itermax = 25;
ldouble pp2=2.+p_index;
ldouble pm2=2.-p_index;
ldouble prefac = 1./(6*eta*pm2*theta*theta*theta*theta);
ldouble gmaxp2 = pow(gammamax, pm2);
ldouble gminp4;
ldouble rhs, gmin, relchange, deriv;
int i;
int sol_found=0;
for(i=0; i<itermax; i++)
{
gminp4 = gmin_guess*gmin_guess*gmin_guess*gmin_guess;
rhs = prefac*(gmaxp2*pow(gmin_guess,pp2) - gminp4);
gmin = theta*log(rhs);
relchange = fabs(1.-gmin/gmin_guess);
gmin_guess = gmin;
if(gmin < 2*gamma_pk)
{
gmin_guess = gmin_init*2;
gmin_init = gmin_guess;
}
if(relchange < converge_crit)
{
sol_found = 1;
break;
}
}
if(sol_found==1)
gamma_min = gmin_guess;
else
gamma_min = gamma_pk;
#endif //CALC_GAMMAMIN_FIT
#endif //RELEL_HEAT_FIX_LIMITS
//Floor & ceiling on gammamin
if(gamma_min < RELEL_INJ_MIN)
gamma_min = RELEL_INJ_MIN;
if(gamma_min > RELEL_INJ_MAX)
gamma_min = RELEL_INJ_MAX;
#endif //RELELECTRONS
return gamma_min;
}
//calculate gamma_inj_max from synchrotron cooling timescale
ldouble calc_gammainj_max_syncool(ldouble bsq_cgs, ldouble dtau_cgs)
{
ldouble gamma_max=1.;
#ifdef RELELECTRONS
gamma_max = RELEL_INJ_MAX;
#ifndef RELEL_HEAT_FIX_LIMITS
#ifdef RELEL_SYN
ldouble bsq=bsq_cgs;
//synchrotron timescale set by equating cooling timescale to dtau
gamma_max = 7.83994e8 / (bsq * dtau_cgs);
if (gamma_max > RELEL_INJ_MAX)
gamma_max = RELEL_INJ_MAX;
#endif //RELEL_SYN
#endif //RELEL_HEAT_FIX_LIMITS
#endif
return gamma_max;
}
int reconnection_plaw_params_from_state(ldouble *pp, void *ggg, void *sss, ldouble* delta_back, ldouble* pindex_back)
{
ldouble delta=0.;
ldouble pindex=10.;
struct geometry *geom
= (struct geometry *) ggg;
struct struct_of_state *state
= (struct struct_of_state *) sss;
#ifdef RELELECTRONS
#if defined(RELEL_HEAT_RECONNECTION)
//David's fit to the reconnection electron heating fraction
//fit for data generated at Te=Ti as a function of sigma_w and beta
//and mion = mproton
// get beta = gas pressure / magn pressure
// and sigma = magn energy density / enthalpy
ldouble beta=0.;
ldouble sigma=0.;
ldouble betamax=0.;
ldouble betanorm=0.;
ldouble rho=state->rho;
ldouble bsq=state->bsq;
ldouble Ti = state->Ti;
ldouble Te = state->Te;
ldouble ue = state->ue;
ldouble ui = state->ui;
ldouble pion = state->pi;
ldouble gammae = state->gammae;
ldouble gammai = state->gammai;
ldouble enth_tot = rho + gammai*ui + gammae*ue;
#ifdef MAGNFIELD
beta = 2.*pion/bsq;
sigma = bsq/rho;//enth_tot;
betamax = 0.25/sigma;
//ANDREW TODO LIMITS??
if(sigma<1.e-10) sigma=1.e-10;
if(!isfinite(enth_tot)) sigma=1.e-10;
if(!isfinite(beta)) beta=1.e10; //no magnetic field
if(!isfinite(betamax)) betamax=1.e10; //no magnetic field
betanorm = beta/betamax;
if(betanorm>1.) betanorm=1.;
if(betanorm<0.) betanorm=0.;
#endif
//Andrew's fit to David's fit to simulation numbers
//TODO find a better fit!!
ldouble ap = 1.8 + 0.7/sqrt(sigma);
ldouble bp = 3.7*pow(sigma, -0.19);
ldouble cp = 23.4*pow(sigma, 0.26);
pindex = ap + bp*tanh(cp*beta);
ldouble ae = 1-1./(4.2*pow(sigma,0.55) + 1);
ldouble be = 0.63*pow(sigma,0.07);
ldouble ce = -68*pow(sigma,0.13);
delta = ae + be*tanh(ce*beta);
//TODO is this fraction of the total or the ratio?
//delta = 1./((1+115.994*beta)*(1+0.2838673/sqrt(sigma)));
//ldouble rtsigi = 1./sqrt(sigma);
//pindex = 1. + 2*(1+3.65*rtsigi)/(1+pow(betanorm,0.422)) + 0.435*rtsigi;
//ANDREW TODO CHECK FLOORS
if(!isfinite(delta))
{
//printf("problem with David delta fit: %d %d %d : %e %e %e %e %e\n",geom->ix,geom->iy,geom->iz,delta,beta,sigma,Te,Ti);
delta = 0;
//print_primitives(pp);
}
if(delta>1)
delta=1.;
if(delta<0)
delta=0.;
if(!isfinite(pindex));
{
//printf("problem with David pindex fit: %d %d %d : %e %e %e %e %e\n",geom->ix,geom->iy,geom->iz, delta,beta,sigma,Te,Ti);
pindex = 10.;
delta=0.;
}
if(pindex>10)
pindex=10;
if(pindex<1.01)
pindex=1.01;
#endif
#endif
*pindex_back=pindex;
*delta_back=delta;
return 0;
}
//*********************************************/
//* Thermal Physics
//*********************************************/
//Calculate total gas adiabatic index with nonthermal population
ldouble calc_gammaint_relel(ldouble* pp, ldouble Te, ldouble Ti)
{
ldouble gamma;
#ifndef RELELECTRONS
gamma = calc_gammaintfromTei(Te,Ti);
#else
ldouble nur=calc_relel_ne(pp);
ldouble pur=calc_relel_p(pp);
ldouble uur=calc_relel_uint(pp);
if (nur==0. || pp[RHO]==0.)
{
gamma = calc_gammaintfromTei(Te,Ti);
}
else
{
ldouble the= kB_over_me*Te;
ldouble gammae=GAMMAE;
#ifndef FIXEDGAMMASPECIES
gammae=calc_gammaintfromtheta(the);
#endif
ldouble peth=calc_thermal_ne(pp)*K_BOLTZ*Te;
ldouble ueth=peth/(gammae-1.);
ldouble thi= kB_over_mui_mp*Ti;
ldouble gammai=GAMMAI;
#ifndef FIXEDGAMMASPECIES
gammai=calc_gammaintfromtheta(thi);
#endif
ldouble pi=kB_over_mui_mp*Ti*pp[RHO];
ldouble ui=pi/(gammai-1.);
gamma = 1. + (peth+pi+pur)/(ueth+ui+uur);
}
#endif
return gamma;
}
//ANDREW -- do without pp, or precompute uur and nur?
//calc ugas from Te and Ti using info in pp for density and energy density
//pp[RHO] and pp[NEREL(ie)] must be already set to use this function!
ldouble calc_PEQ_ugasfrom_Tei_relel(ldouble *pp, ldouble Te,ldouble Ti)
{
ldouble uur = calc_relel_uint(pp);
ldouble gammae=GAMMAE;
ldouble gammai=GAMMAI;
#ifndef FIXEDGAMMASPECIES
gammae=calc_gammaintfromtemp(Te,ELECTRONS);
gammai=calc_gammaintfromtemp(Ti,IONS);
#endif
ldouble ueth = K_BOLTZ*calc_thermal_ne(pp)*Te/(gammae - 1.);
ldouble uith = kB_over_mui_mp*pp[RHO]* Ti/(gammai - 1.);
ldouble u = uur + ueth + uith;
return u;
}
// Calculate and return the chemical potential for a given theta and thermal number density
// Expression consistent with the entropy S3.
ldouble
chemical_potential_short(ldouble theta, ldouble neth)
{
ldouble mu;
mu = M_ELECTR * (1. - 0.6*log1p(2.5*theta) + theta*(4-1.5*log(theta*(theta+0.4)) + log(neth)));
return mu;
}
// Calculate chemical potential from entropy density (s*n)
// pressure, energy density, number density, temperature
ldouble
chemical_potential_long(ldouble p, ldouble u, ldouble sn, ldouble neth, ldouble Te)
{
return ((p + u - Te*sn)/neth);
}
//*********************************************/
//* Injection
//*********************************************/
//Apply viscous heating / particle injection for relativistic distribution in a single cell
int
apply_relel_visc_heating(ldouble pp[NV], ldouble durelel, ldouble p_index, ldouble injmin, ldouble injmax, ldouble dtau)
{
#ifdef RELELECTRONS
int ie;
ldouble xx,pref, gamma, neur, scale;
ldouble qheat_app_tot;
ldouble qheat[NRELBIN];
ldouble nheat[NRELBIN];
// total relel energy before heating
ldouble uint0 = calc_relel_uint(pp);
ldouble nenth = calc_relel_ne(pp);
//Skip heating if it would give negative net energy in the distribution, or if already at maximum #
if((uint0 + durelel) > 0. && nenth/(pp[RHO]*one_over_mue_mp) < MAX_RELEL_FRAC_N)
{
// Prefactor (precomputed if constant using constant heating params)
// ANDREW TODO case where 1<p_index<2
#if defined(RELEL_HEAT_FIX_LIMITS) && defined(RELEL_HEAT_FIX_INDEX)
xx=xx_relel_inj;
#else
if (p_index!=2 && p_index!=1)
{
xx = (pow(injmax, 2.-p_index) - pow(injmin, 2.-p_index))/(2.-p_index) - (pow(injmax, 1.-p_index) - pow(injmin, 1.-p_index))/(1.-p_index);
}
else if (p_index == 2)
{
xx = 1./injmax - 1./injmin + log(injmax/injmin);
}
else if (p_index == 1)
{
xx = injmax - injmin - log(injmax/injmin);
}
#endif
pref = durelel / (xx * M_ELECTR);
//printf("%e %e\n",numdensGU2CGS(pref), numdensGU2CGS(pref)/timeGU2CGS(dtau));
//exit(-1);
//Calculate and heating rate in all bins, and the total energy added
qheat_app_tot=0.;
for (ie=0; ie<NRELBIN; ie++)
{
gamma = relel_gammas[ie];
if ((gamma < injmin) || (gamma > injmax))
{
nheat[ie] = 0.;
}
else
{
#if defined(RELEL_HEAT_FIX_LIMITS) && defined(RELEL_HEAT_FIX_INDEX)
nheat[ie] = pref * relel_injpow[ie];
#else
nheat[ie] = pref * pow(gamma, -p_index);
#endif
}
qheat[ie] = (gamma-1.)*nheat[ie];
}
qheat_app_tot = M_ELECTR*integrate_relel_q(qheat);
//Normalization
//Scale so that durelel is total numerically integrated energy added
//And apply the change in each bin
#ifndef NORELELINJSCALE
if (fabs(qheat_app_tot) > 0.)
{
scale = durelel / qheat_app_tot;
if(scale < 0.) scale = 0.;
for (ie=0; ie<NRELBIN; ie++)
{
pp[NEREL(ie)] += scale*nheat[ie];
}
}
//printf("%e\n",scale);
#endif
// Ceiling: Not too many rel. electrons
nenth = calc_relel_ne(pp);
if (nenth/(pp[RHO]*one_over_mue_mp) > MAX_RELEL_FRAC_N)
{
for (ie=0; ie<NRELBIN; ie++)
{
ldouble scale2 = MAX_RELEL_FRAC_N/(nenth/(pp[RHO]*one_over_mue_mp));
pp[NEREL(ie)] *= scale2;
}
}
// Floor: No negative rel. electron bin numbers
for (ie=0; ie<NRELBIN; ie++)
{
if (pp[NEREL(ie)] < 0.)
pp[NEREL(ie)] = 0.;
}
}
#endif
return 0;
}
//*********************************************/
//* Adiabatic Cooling
//*********************************************/
//Apply adiabatic compression/expansion to relativistic electron distribution
//in all bins
ldouble
relel_adiab(ldouble dt)
{
#ifdef RELELECTRONS
int ii;
#pragma omp parallel for private(ii) schedule (static)
for(ii=0;ii<Nloop_0;ii++) //domain
{
int ix,iy,iz,iv;
int i,ie,ig;
ldouble pp[NV];
ldouble pp0[NV];
ldouble uu[NV];
ldouble ngammas[NRELBIN];
ldouble ngammas0[NRELBIN];
ldouble q[NRELBIN]; //derivative
ldouble napp[NRELBIN];
ldouble qapp[NRELBIN];
ldouble theta;
#if defined(RELEL_ADIAB_LOGSPACE) || defined(RELEL_ADIAB_LOGSPACE_LF)
ldouble x[NRELBIN]; //gamma*n(gamma)
ldouble x0[NRELBIN];
#endif
ix=loop_0[ii][0];
iy=loop_0[ii][1];
iz=loop_0[ii][2];
if(is_cell_active(ix,iy,iz)==0)
continue;
struct geometry geom;
fill_geometry(ix,iy,iz,&geom);
//Calculate proper time step
ldouble vcon[4];
ldouble ucon[4];
ldouble ucov[4];
ldouble dtau;
vcon[1]=get_u(p, VX, ix, iy, iz);
vcon[2]=get_u(p, VY, ix, iy, iz);
vcon[3]=get_u(p, VZ, ix, iy, iz);
vcon[0]=0.;
conv_vels_both(vcon,ucon,ucov,VELPRIM,VEL4,geom.gg,geom.GG);
dtau = dt/ucon[0];
//Fill vectors of primitives & conserved quantities
for (i=0; i < NV; i++)
{
pp0[i] = get_u(p, i, ix, iy, iz);
pp[i] = pp0[i];
uu[i] = get_u(u, i, ix, iy, iz);
}
//Calculate thermal electron number and energy before adiabatic compression/expansion
ldouble ueth, neth;
neth = calc_thermal_ne(pp);
//#ifdef RELELENTROPY
//ueth=calc_ufromS4n(pp[ENTRE],neth,ELECTRONS,ix,iy,iz);
//#else
ueth=calc_ufromSerho(pp[ENTRE],neth*MU_E*M_PROTON,ELECTRONS,ix,iy,iz);
//#endif
//Calculate expansion four-divergence
ldouble div;
#ifdef RELEL_ADIAB_ART
div = RELEL_DIV_ART / timeCGS2GU(1.);
#else
int derdir[3] = {0,0,0};
calc_fluid_div_lab(pp, &geom, dt, &div, MHD, derdir);
//ldouble shear[4][4];
//calc_shear_lab(pp, &geom, shear, &div, MHD, derdir);
#endif
//Constant velocity in gamma space, ignore the 1/gamma (gamma >> 1)
theta = one_third*div;
// Make temporary arrays
for (ie=0; ie<NRELBIN; ie++)
{
ngammas0[ie] = pp0[NEREL(ie)];
ngammas[ie] = ngammas0[ie];
#if defined(RELEL_ADIAB_LOGSPACE) || defined(RELEL_ADIAB_LOGSPACE_LF)
x0[ie] = relel_gammas[ie]*ngammas0[ie];
x[ie] = x0[ie];
#endif
//bin number change expected
qapp[ie] = -1*dtau*M_ELECTR*theta*(relel_gammas[ie]-relel_gammas_inv[ie])*ngammas0[ie];
}
//compute expected total energy loss or gain
ldouble de1 = 0.;
ldouble de2 = 0.;
de1 = integrate_relel_q(qapp);
//Compute derivative with finite diff and apply
#if defined(RELEL_ADIAB_LOGSPACE)
relel_adiab_derivs_logspace(theta, dtau, x, q);
for(ie=0; ie<NRELBIN; ie++)
x[ie] = x0[ie] + dtau*q[ie];
#elif defined(RELEL_ADIAB_LOGSPACE_LF)
relel_adiab_derivs_logspace_lf(theta, dtau, ngammas, q);
for(ie=0; ie<NRELBIN; ie++)
x[ie] = x0[ie] + dtau*q[ie];
#else
relel_adiab_derivs(theta, dtau, ngammas, q);
for(ie=0; ie<NRELBIN; ie++)
ngammas[ie] = ngammas0[ie] + dtau*q[ie];
#endif
/*
//2nd order RK Heun
#ifdef RELEL_ADIAB_RK2
ldouble q2[NRELBIN];
#if defined(RELEL_ADIAB_LOGSPACE)
relel_adiab_derivs_logspace(theta, dtau, x, q2);
for (ie=0; ie<(NRELBIN); ie++)
x[ie] = x0[ie] + 0.5 * dtau * (q[ie] + q2[ie]);
#elif defined(RELEL_ADIAB_LOGSPACE_LF)
for (ie=0; ie<(NRELBIN); ie++)
ngammas[ie] = x[ie]*relel_gammas_inv[ie];
relel_adiab_derivs_logspace_lf(theta, dtau, ngammas, q2);
for (ie=0; ie<(NRELBIN); ie++)
x[ie] = x0[ie] + 0.5 * dtau * (q[ie] + q2[ie]);
#else
relel_adiab_derivs(theta, dtau, ngammas, q2);
for (ie=0; ie<(NRELBIN); ie++)
ngammas[ie] = ngammas0[ie] + 0.5 * dtau * (q[ie] + q2[ie]);
#endif
#endif
*/
//Determine final heating rate and total energy change
for (ie=0; ie<NRELBIN; ie++)
{
#if defined(RELEL_ADIAB_LOGSPACE) || defined(RELEL_ADIAB_LOGSPACE_LF)
napp[ie] = x[ie]*relel_gammas_inv[ie] - pp0[NEREL(ie)];
#else
napp[ie] = ngammas[ie] - pp0[NEREL(ie)];
#endif
qapp[ie] = M_ELECTR*(relel_gammas[ie]-1.)*napp[ie];
}
de2 = integrate_relel_q(qapp);
//Compute thermal energy gain from bottom or top bin
ldouble dueth=0.;
ldouble dneth=0.;
ldouble Senew;
ldouble gdot;
//Expansion
if (relel_gammas[0] != 1. && div > 0)
{
gdot = theta*(relel_gammas_e[0] - relel_gammas_e_inv[0]);
dueth = fabs(M_ELECTR*gdot*ngammas0[0]*(relel_gammas_e[0] - 1.)*dtau);
}
//Compression
else if(div < 0.)
{
gdot = theta*(relel_gammas_e[NRELBIN] - relel_gammas_e_inv[NRELBIN]);
dueth = fabs(M_ELECTR*gdot*ngammas0[NRELBIN-1]*(relel_gammas_e[NRELBIN] - 1.)*dtau);
}
//NORMALIZATION
#ifndef NO_RELEL_ADIAB_SCALE
ldouble frac= de1/de2;
if(calc_relel_uint(pp)>0. && fabs(de2)>0.)
{
for(ie=0;ie<NRELBIN;ie++)
napp[ie] *= frac;
}
#endif
//Apply the adiabatic change to the bins
for(ie=0;ie<NRELBIN;ie++)
{
pp[NEREL(ie)] = pp0[NEREL(ie)] + napp[ie];
//Floor
if(pp[NEREL(ie)]<0.) pp[NEREL(ie)]=0.;
}
//Apply the combination of edge heating and zeroed bins to thermal electrons
dneth = calc_thermal_ne(pp) - neth; //total change in thermal particle number
apply_du_dn_2_species(pp, ueth, neth, dueth, dneth, &geom, ELECTRONS, &Senew);
pp[ENTRE] = Senew;
//Convert primitive to conserved and save
p2u_mhd(pp, uu, &geom);
for (i=0; i<NV; i++)
{
set_u(u, i, ix, iy, iz, uu[i]);
set_u(p, i, ix, iy, iz, pp[i]);
}
}
#endif
return 0;
}
//Zero out nonthermal bins below thermal peak and move energy to thermal.
//pre-calculate thetae and neth (from state)
ldouble remove_lowgamma_electrons(ldouble thetae, ldouble neth, ldouble *pp)
{
ldouble du=0.;
#ifdef RELELECTRONS
//ldouble Te,thetae;
ldouble pref;
ldouble uintold;
int i,ie;
//energy in rel electrons before zeroing
uintold = calc_relel_uint(pp);
//zero out all bins below the approximate location of the thermal peak
ldouble pkgamma = 2.*thetae; //high theta approx
int pkbin = NRELBIN;
int zeroed_bins = 0;
for (ie=0;ie<NRELBIN;ie++)
{
if(relel_gammas[ie] >= pkgamma) //approx location of the peak of the thermal distribution function
{
pkbin = ie;
break;
}
else
{
if (pp[NEREL(ie)] != 0.)
{
pp[NEREL(ie)] = 0.;
zeroed_bins++;
}
}
}
//above the thermal peak, zero out bins where n_th > n_neth
if (pkbin<NRELBIN)
{
ldouble gamma,ngamma_th,ngamma_nth;
ldouble pref = 1./(2.*thetae*thetae*thetae) + 1./(8.*thetae*thetae*thetae*thetae*thetae);
for(ie=pkbin;ie<NRELBIN;ie++)
{
gamma = relel_gammas[ie];
ngamma_th = neth*pref*gamma*gamma*exp(-gamma/thetae); //assuming theta >> 1
ngamma_nth = pp[NEREL(ie)];
if (ngamma_nth > ngamma_th)
{
break;
}
else
{
pp[NEREL(ie)] = 0.;
zeroed_bins++;
}
}
}
//calculate the missing energy to add to the thermal electron distribution
if (zeroed_bins>0)
{
du = fabs(uintold - calc_relel_uint(pp));
}
#endif //RELELECTRONS
return du;
}
///////////////////////////////////////////////////////////////////////////
//Derivative methods for explicit relel adiabatic evolution
///////////////////////////////////////////////////////////////////////////
//Compute dn/dtau using given array of ngamma values and put derivatives in q
int
relel_adiab_derivs(ldouble theta, ldouble dtau, ldouble *ngammas, ldouble *q)
{
#ifdef RELELECTRONS
int ie;
ldouble ngdown, ngup, ngup2;
ldouble gdown,gup;
for (ie=0; ie<(NRELBIN); ie++)
{
// Current bin is always downwind
ngdown = theta*(relel_gammas[ie]-relel_gammas_inv[ie])*ngammas[ie];
gdown = relel_gammas[ie];
// Expansion
if (theta > 0.)
{
if (ie==NRELBIN-1)
{
ngup = 0.;
gup = relel_gammas[NRELBIN-1]*binspace;
}
else
{
ngup = theta*(relel_gammas[ie+1]-relel_gammas_inv[ie+1])*ngammas[ie+1];
gup = relel_gammas[ie+1];
}
}
//Compression
else if (theta < 0.)
{
if (ie==0)
{
ngup=0.;
gup=relel_gammas[0]/binspace;
}
else
{
ngup = theta*(relel_gammas[ie-1]-relel_gammas_inv[ie-1])*ngammas[ie-1];
gup = relel_gammas[ie-1];
}
}
//Derivative
q[ie] = (ngup - ngdown) / (gup - gdown);
}