-
Notifications
You must be signed in to change notification settings - Fork 0
/
grrr.c
1560 lines (1234 loc) · 37 KB
/
grrr.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
/***
This is the main module of the GRanada Relativistic Runaway (GRRR)
simulator.
Alejandro Luque Estepa, 2013
***/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include "grrr.h"
static double truncated_bethe_fd(double gamma, double gamma2, double beta2);
static double bremsstrahlung_fd(double gamma);
static void cross(const double *a, const double *b, double *r);
static void rotate(double theta, const double *v, double *r);
static int argmax(const double *p, int sign, int n);
static void perp_unit_vectors(const double p[3], double pabs, double up[3],
double a[3], double b[3]);
static double moller_differential(double gamma, double gamma2, double beta2,
double Kp);
static double coulomb_differential(double gamma2, double beta2, double p2,
double rtheta);
static void particle_track_charge(particle_t *part, int change);
static void track_crossing(particle_t *part, double dr[3], double dp[3],
double t, double dt);
static double rnd_gauss(double mu, double sigma);
static double ranf (void);
/* Several pre-defined em fields. */
void emfield_static (double t, double *r, double *e, double *b);
void emfield_wave (double t, double *r, double *e, double *b);
void emfield_const (double t, double *r, double *e, double *b);
void emfield_pulse (double t, double *r, double *e, double *b);
void emfield_interf (double t, double *r, double *e, double *b);
void emfield_dipole (double t, double *r, double *e, double *b);
void emfield_eval (double t, double *r, double *e, double *b);
void emfield_front (double t, double *r, double *e, double *b);
void emfield_selfcons (double t, double *r, double *e, double *b);
void set_emfield_front(double l, int ninterp, double *values);
/* We will keep a global pointer to the beginning of the particle list
b.c. the first particle may get thermal and dissapear. */
particle_t *particle_head = NULL, *particle_tail = NULL;
/* We also have a counter of the total number of particles. */
int particle_count = 0;
/* When we use super-particles, we track the particle weight. */
double particle_weight = 1.0;
/* Name of this program. */
const char *invok_name = "grrr";
/* We store the crossing points trough a given wall in a linked list. */
crossing_t *crossing_head = NULL;
int crossing_count = 0;
/* These are parameters. Later they have to be set at run-time. */
double E0 = 7.0e5;
double U0 = 0.0;
double THETA = PI / 4;
double EB = 0.0;
double EBWIDTH = 0.0;
double B0 = 20e-6;
double KTH = 0.05 * MEV;
double GAMMATH;
double L = 3.0;
#define __NWALLS 64
int nwalls = 0;
double Z_WALL[__NWALLS];
int NINTERP = 0;
double *INTERP_VALUES = NULL;
/* If this flag is set we ignore all secondary particles. */
int ONLY_PRIMARIES = 0;
/* If set, delete a particle after it has crossed a wall. */
int DELETE_AT_WALL = FALSE;
/* If set, record only the first wall crossing (including all the particle's
ascendants */
int FIRST_WALL_ONLY = FALSE;
/* This is an unphysical cutoff to try to model more easily. */
double FD_CUTOFF = 100000 * MEV;
/* We will track the creation/destruction of particles to keep track
of the charge density. */
#define __NCELLS 32768
const int NCELLS = __NCELLS;
const double CELL_DZ = 0.25;
/* fixedcharge counts the number of "fixed" charge-carriers inside a cell.
That includes ions and thermalized electrons, which, for our purposes
are also considered immobile. */
double fixedcharge[__NCELLS];
/* mobilecharge counts the number of mobile charges inside each cell. */
double mobilecharge[__NCELLS];
/* And this contains the collective ez, calculated in ez_integrate.
Note that ez is evaluated in the cell boundaries and therefore
contains NCELLS+1 numbers. */
double ez[__NCELLS + 1];
/* We track_time inside the C module with this global. Note that t is only
read or changed in the high-level functions list_step* (actually, only
in list_step(...), since the others call this one). Thus you can use
lower-level functions without side-effects. */
double TIME = 0;
/* The function that computes em fields at any time in any point. */
emfield_func_t emfield_func = &emfield_static;
void call_emfield(emfield_func_t ef);
void
grrr_init(void)
/* Initializations must come here. */
{
int i;
for (i = 0; i < NCELLS; i++) {
fixedcharge[i] = 0.0;
}
TIME = 0;
}
void
set_emfield_callback(emfield_func_t ef)
/* Sets a function to calculate the em fields. */
{
emfield_func = ef;
}
void
emfield_eval(double t, double *r, double *e, double *b)
/* Evaluates the electric field at a given point. This is needed only
if you want to evaluates em fields directly from python. I am unable
to do this using only ctypes.*/
{
(*emfield_func) (t, r, e, b);
}
void
add_wall(double z)
/* Adds a wall to check crossings. */
{
if (nwalls >= __NWALLS) {
fprintf (stderr,
"%s: Error: Only up to __NWALLS=%d walls are allowed\n",
invok_name, __NWALLS);
exit(-1);
}
Z_WALL[nwalls++] = z;
}
particle_t*
particle_init(int ptype)
/* Initializes a particle of the provided type. The rest of the elements
are set to 0. */
{
particle_t *part;
static int nid = 0;
part = xcalloc(1, sizeof(particle_t));
part->ptype = ptype;
part->id = nid++;
part->charge = particle_charge[ptype];
part->mass = particle_mass[ptype];
part->next = NULL;
part->thermal = FALSE;
part->rightmost_wall = -1;
return part;
}
void
particle_delete(particle_t *part, int track)
/* Deletes a particle taking care of the prev/next links and freeing
memory. */
{
if (part->prev != NULL) {
part->prev->next = part->next;
} else {
/* We are at the beginning of the list and we have to update the
pointer to the first particle. */
particle_head = part->next;
}
if (part->next != NULL) {
part->next->prev = part->prev;
} else {
/* We are at the end of the list. */
particle_tail = part->prev;
}
if (track) particle_track_charge(part, -1);
free(part);
particle_count--;
}
void
particle_append(particle_t *part, int track)
/* Appends a particle to the end of the list. */
{
part->next = NULL;
part->prev = particle_tail;
if (particle_tail != NULL) {
particle_tail->next = part;
}
particle_tail = part;
/* If there were no particles on the list, we put newpart as the head. */
if (particle_head == NULL) {
particle_head = part;
}
if (track) particle_track_charge(part, 1);
particle_count++;
}
void
particle_birth(particle_t *part)
/* Sets the current t, r, p as the t0, r0 and p0 of a newly created particle */
{
int i;
part->t0 = TIME;
part->tau = 0;
part->nelastic = 0;
part->nionizing = 0;
for (i = 0; i < 3; i++) {
part->r0[i] = part->r[i];
part->p0[i] = part->p[i];
}
}
static void
particle_track_charge(particle_t *part, int change)
/* Tracks the creation / destruction of a particle into the charge array.
change must be 1 when you create one particle, -1 when you destroy it. */
{
int n;
n = (int) floor(part->r[Z] / CELL_DZ);
if (n < 0 || n >= NCELLS) {
if (emfield_func == &emfield_selfcons) {
fprintf (stderr,
"%s: Warning: particle at z=%g m outside tracking range.\n",
invok_name, part->r[Z]);
}
return;
}
fixedcharge[n] += -particle_weight * part->charge * change;
}
void
count_mobile(particle_t *plist)
/* Counts the number of mobile particles inside each cells. Updates
mobilecharge. */
{
particle_t *part;
int n;
memset(mobilecharge, 0, NCELLS * sizeof(double));
for (part = plist; part; part = part->next) {
n = (int) floor(part->r[Z] / CELL_DZ);
mobilecharge[n] += particle_weight * part->charge;
}
}
void
solve_ez(void)
/* Solves ez from fixedcharge and mobilecharge. The boundary condition
here is always ez->EB for z->infinity. If you want a different b.c.
you have to add a constant to ez.
*/
{
int i;
double field = EB;
for (i = 0; i < NCELLS + 1; i++) {
ez[NCELLS - i] = field;
if (NCELLS - 1 - i >= 0) {
field -= ((ELEMENTARY_CHARGE / EPSILON_0)
* (mobilecharge[NCELLS - 1 - i] + fixedcharge[NCELLS - 1 - i]));
}
}
}
void
list_clear(void)
/* Deletes all particles from the list and releases their memory. */
{
while (particle_head != NULL) {
particle_delete(particle_head, FALSE);
}
}
void
list_erase(particle_t *plist)
/* Erases all particles in a list, but does not change the main list. */
{
particle_t *part, *partnext;
for (part = plist; part; part = partnext) {
partnext = part->next;
free(part);
}
}
void
emfield_static(double t, double *r, double *e, double *b)
/* Calculates the electromagnetic field at a given time and location.
Returns it into the e and b pointers. */
{
b[X] = b[Z] = 0.0;
b[Y] = B0;
e[X] = e[Y] = 0.0;
e[Z] = -E0 * cos(2 * PI * r[Z] / L) + EB;
}
void
emfield_wave(double t, double *r, double *e, double *b)
/* Calculates the electromagnetic field at a given time and location.
Returns it into the e and b pointers. */
{
double cosphi;
cosphi = cos(2 * PI * (r[Z] - C * t) / L);
e[X] = 0.0;
e[Y] = E0 * cosphi;
e[Z] = 0.0;
//e[Z] = -EB * exp(-(r[X] * r[X] + r[Y] * r[Y]) / (EBWIDTH*EBWIDTH));
b[X] = -E0 * cosphi / C;
b[Y] = 0.0;
b[Z] = 0.0;
}
void
emfield_const(double t, double *r, double *e, double *b)
{
e[X] = 0.0;
e[Y] = 0.0;
e[Z] = EB;
b[X] = B0;
b[Y] = 0.0;
b[Z] = 0.0;
}
void
emfield_pulse(double t, double *r, double *e, double *b)
{
e[X] = 0.0;
e[Y] = 0.0;
b[X] = 0.0;
b[Y] = 0.0;
b[Z] = 0.0;
e[Z] = (r[Z] - U0 * t > L || r[Z] - U0 * t < 0)? E0: EB;
}
void
emfield_front(double t, double *r, double *e, double *b)
{
double xi, x, dx, e1, e2;
int n;
e[X] = 0.0;
e[Y] = 0.0;
b[X] = 0.0;
b[Y] = 0.0;
b[Z] = 0.0;
xi = r[Z] - U0 * t;
if(!isfinite(xi)) {
fprintf(stderr, "%s: xi is NaN encountered in emfield_front.\n",
invok_name);
exit(-1);
}
if (xi <= -L / 2) {
e[Z] = INTERP_VALUES[0];
return;
}
if (xi >= L / 2) {
e[Z] = INTERP_VALUES[NINTERP];
return;
}
x = NINTERP * (xi + L / 2) / L;
n = (int) floor(x);
dx = x - n;
e1 = INTERP_VALUES[n];
e2 = INTERP_VALUES[n + 1];
e[Z] = e1 + dx * (e2 - e1);
}
void
emfield_selfcons(double t, double *r, double *e, double *b)
{
double x, dx;
int n;
e[X] = 0.0;
e[Y] = 0.0;
b[X] = 0.0;
b[Y] = 0.0;
b[Z] = 0.0;
if (r[Z] < 0) {
e[Z] = ez[0];
return;
}
if (r[Z] >= NCELLS * CELL_DZ) {
e[Z] = ez[NCELLS];
return;
}
x = r[Z] / CELL_DZ;
n = (int) floor(x);
dx = x - n;
e[Z] = ez[n] + dx * (ez[n + 1] - ez[n]);
}
void
set_emfield_front(double l, int ninterp, double *values)
/* Sets the shape of the front that will be interpolated from in
emfield_front.
* l is the width of the front
* ninterp is the number of interpolating segments from -l/2 to l/2.
* values is an array with at least ninterp + 1 values. */
{
int i;
L = l;
NINTERP = ninterp;
if (INTERP_VALUES != NULL) {
free(INTERP_VALUES);
}
INTERP_VALUES = xmalloc((ninterp + 1) * sizeof(double));
memcpy(INTERP_VALUES, values, sizeof(double) * (ninterp + 1));
}
void
count_collisions(int trials, double t, double dt, double *values)
/* Counts the number of ionizing collisions minus thermalizations
per unit time in the front.
It counts the number of collisions during a time dt and
returns an average over n trials. */
{
int i, n;
double xi, x, dn, theta;
particle_t *part;
memset(values, 0, NINTERP * sizeof(double));
dn = 1.0 / trials / dt;
/* First the ionizing collisions. */
for (part = particle_head; part; part = part->next) {
int elastic;
xi = part->r[Z] - U0 * t;
if (xi < -L / 2 || xi > L / 2) continue;
x = NINTERP * (xi + L / 2) / L;
n = (int) floor(x);
elastic = elastic_collision(part, dt, &theta);
if (elastic) {
elastic_momentum(part->p, theta, part->p);
}
if (rk4_single(part, t, dt, FALSE)) {
values[n] -= 1.0 / dt;
}
for (i = 0; i < trials; i++) {
if (ionizing_collision(part, dt, NULL, NULL)) {
values[n] += dn;
}
}
}
}
void
emfield_interf(double t, double *r, double *e, double *b)
/* Calculates the electromagnetic field at a given time and location.
Returns it into the e and b pointers. */
{
double rprime[3], et[3], bt[3], e1[3], e2[3], b1[3], b2[3];
int i;
rotate(THETA, r, rprime);
emfield_wave(t, rprime, et, bt);
rotate(-THETA, et, e1);
rotate(-THETA, bt, b1);
rotate(-THETA, r, rprime);
emfield_wave(t, rprime, et, bt);
rotate(THETA, et, e2);
rotate(THETA, bt, b2);
for (i = 0; i < 3; i++) {
e[i] = e1[i] - e2[i];
b[i] = b1[i] - b2[i];
}
e[Z] += EB;
b[X] += B0;
// b[X] = b[Y] = b[Z] = 0;
}
void
emfield_celestin(double t, double *r, double *e, double *b)
/* Calculates the electromagnetic field at a given time and location.
Returns it into the e and b pointers. */
{
double a, a2;
int i;
a2 = NORM2(r);
a = sqrt(a2);
for (i = 0; i < 3; i++) {
b[i] = 0;
e[i] = (r[i] / a) * E0 / a;
}
}
void
emfield_dipole(double t, double *r, double *e, double *b)
/* The EM field created by a dipole at r=0,0,0 */
{
// Here a is the abs of r, but the r var is already taken.
double a, a2, a3;
double l, l2, l3;
double sinphi, cosphi;
a2 = NORM2(r);
a = sqrt(a2);
a3 = a * a2;
l = L / a;
l2 = l * l;
l3 = l * l2;
/* The prototype of sincos does not appear in math.h.
sincos((a - C * t) / L, &sinphi, &cosphi);
*/
sinphi = sin((a - C * t) / L);
cosphi = cos((a - C * t) / L);
e[X] = (E0 / a2) * ((-r[X] * r[Z] * l + 3 * r[X] * r[Z] * l3) * cosphi
+ ( 3 * r[X] * r[Z] * l2) * sinphi);
e[Y] = (E0 / a2) * ((-r[Y] * r[Z] * l + 3 * r[Y] * r[Z] * l3) * cosphi
+ ( 3 * r[Y] * r[Z] * l2) * sinphi);
e[Z] = (E0 / a2) * (((r[X] * r[X] + r[Y] * r[Y]) * l
+ 3 * (r[Z] * r[Z] + a2) * l3) * cosphi
+ ( 3 * (r[Z] * r[Z] + a2) * l2) * sinphi);
b[X] = (E0 / a / C) * l * r[Y] * (cosphi - l * sinphi);
b[Y] = -(E0 / a / C) * l * r[X] * (cosphi - l * sinphi);
b[Z] = 0.0;
}
static double
truncated_bethe_fd(double gamma, double gamma2, double beta2)
{
double A, T1, T2, T3, T5, T6;
A = BETHE_PREFACTOR / beta2;
T1 = log(M2C4 * (gamma2 - 1) * (gamma - 1) / (AIR_IONIZATION*AIR_IONIZATION));
T1 -= log(MC2 * (gamma - 1) / KTH / 2);
T2 = -(1 + 2 / gamma - 1 / gamma2) * log(2);
T2 -= -((1 + 2 / gamma - 1 / gamma2)
* log(2 * (MC2 * (gamma - 1) - KTH)
/ (MC2 * (gamma - 1))));
T3 = 1 / gamma2;
T5 = - (1 - KTH / (MC2 * (gamma - 1) - KTH));
T6 = + KTH*KTH / (2 * M2C4 * gamma2);
/* The total force on the particle is */
return A * (T1 + T2 + T3 + T5 + T6);
}
static double
moller_differential(double gamma, double gamma2, double beta2,
double Kp)
/* The moller differential cross-section. */
{
double A, T1, T2, T3;
A = BETHE_PREFACTOR / beta2;
T1 = ((gamma - 1)*(gamma - 1) * M2C4
/ (Kp*Kp * pow(MC2 * (gamma - 1) - Kp, 2)));
T2 = -((2 * gamma2 + 2 * gamma - 1)
/ (Kp * (MC2 * (gamma - 1) - Kp) * gamma2));
T3 = 1. / (M2C4 * gamma2);
return A * (T1 + T2 + T3);
}
static double
coulomb_differential(double gamma2, double beta2, double p2,
double theta)
/* The formula for the elastic coulomb scattering. */
{
double A, T, sin2;
sin2 = sin(theta / 2);
sin2 *= sin2;
A = COULOMB_PREFACTOR / (beta2 * beta2) / gamma2;
T = (1. - beta2 * sin2) / pow(sin2 + COULOMB_B / p2, 2);
/* The 2 pi sin(theta) comes from
d\Omega = 2 pi sin(theta) dtheta. */
return A * T * 2 * PI * sin(theta);
}
static double
bremsstrahlung_fd(double gamma)
/* The stopping power of Bremsstrahlung radiation. We use a linear
approximation that works quite well for energies above 10 MeV.
For lower energies Bremsstrahlung is anyway negligible against
collisional stopping. */
{
double K, fd;
K = MC2 * (gamma - 1);
fd = AIR_DENSITY * (BSTRAHLUNG_A * K + BSTRAHLUNG_B);
return (fd > 0)? fd: 0;
}
static void
cross(const double *a, const double *b, double *r)
{
/* {-az by + ay bz, az bx - ax bz, -ay bx + ax by} */
r[X] = -a[Z] * b[Y] + a[Y] * b[Z];
r[Y] = a[Z] * b[X] - a[X] * b[Z];
r[Z] = -a[Y] * b[X] + a[X] * b[Y];
}
static void
rotate(double theta, const double *v, double *r)
/* Rotates an the vector v an angle theta in the (y, z) plane. */
{
r[X] = v[X];
r[Y] = v[Y] * cos(theta) - v[Z] * sin(theta);
r[Z] = v[Y] * sin(theta) + v[Z] * cos(theta);
}
double
total_fd(double K)
/* This is for debugging only: check that we have the correct fd,
including the Bremsstrahlung term. */
{
double gamma, gamma2, beta2, fd;
if (K >= FD_CUTOFF) {
/* This introduces a constant Fd above a certain energy FD_CUTOFF.
Although non-physical this is simple to analyze and provides a
"zero-order" model. */
K = FD_CUTOFF;
}
gamma = 1 + K / MC2;
gamma2 = gamma * gamma;
beta2 = 1 - 1 / gamma2;
fd = truncated_bethe_fd(gamma, gamma2, beta2);
fd += bremsstrahlung_fd(gamma);
return fd;
}
int
drpdt(particle_t *part, double t, double *r, const double *p,
double *dr, double *dp, double *dtau, double h)
/* Computes the derivatives of r, p and tau for the particle *part.
Note that part->p and part->r are ignored. The particle is only needed
for the charge.
The result is multipled by h (use h=1.0) if you want the actual derivatives.
Returns 0 if succesful, 1 if the particle has been thermalized.
*/
{
double p2, gamma2, gamma, beta2, fd;
double e[3], b[3], mf[3];
int i;
p2 = NORM2(p);
gamma2 = 1. + p2 / (MC2 * M);
gamma = sqrt(gamma2);
/* We already know the derivative of the proper time. */
*dtau = h / gamma;
if (gamma < GAMMATH) {
/* The particle has been thermalized. No sense in continuing. */
return 1;
}
if(gamma < 2 * GAMMATH - 1) {
/* In this case, there can be no collisions where the secondary
energy is > Kth. The substracted term is zero.
Note: In a reimplementation it is more efficient to simply
ignore the substracted terms and not rely on the identity that
they are 0 when gamma = 2 * gammath - 1. */
gamma = 2 * GAMMATH - 1;
gamma2 = gamma * gamma;
}
beta2 = 1 - 1 / gamma2;
if (gamma <= 1 + FD_CUTOFF / MC2) {
fd = truncated_bethe_fd(gamma, gamma2, beta2);
fd += bremsstrahlung_fd(gamma);
} else {
/* We are above FD_CUTOFF: Use the FD corresponding to FD_CUTOFF. */
double gamma_, gamma2_, beta2_;
gamma_ = 1 + FD_CUTOFF / MC2;
gamma2_ = gamma_ * gamma_;
beta2_ = 1 - 1 / gamma2_;
fd = truncated_bethe_fd(gamma_, gamma2_, beta2_);
fd += bremsstrahlung_fd(gamma_);
}
(*emfield_func) (t, r, e, b);
for(i = 0; i < 3; i++){
/* The derivative of r is calculated from the relativistic velocity. */
dr[i] = p[i] / gamma / M;
}
/* The magnetic force is F = v x B. */
cross(dr, b, mf);
for(i = 0; i < 3; i++){
double lorentz;
lorentz = part->charge * ELEMENTARY_CHARGE * (e[i] + mf[i]);
dp[i] = -fd * p[i] / sqrt(p2) + lorentz;
dp[i] *= h;
dr[i] *= h;
}
return 0;
}
void
drpdt_all(particle_t *plist, double t, double dt)
/* Calculates dp and dr for all particles in plist */
{
particle_t *part;
int thermal;
if (emfield_func == &emfield_selfcons) {
count_mobile(plist);
solve_ez();
}
for (part = plist; part; part = part->next) {
part->thermal = drpdt(part, t, part->r, part->p,
part->dr, part->dp, &(part->dtau), dt);
}
}
int
rk4_single(particle_t *part, double t, double dt, int update)
/* Updates the r and p of particle *part by a time dt using a 4th order
Runge-Kutta solver.
If update is 0, just checks if the particle is being thermalized, without
updating momenta or position.
*/
{
double dr1[3], dr2[3], dr3[3], dr4[3];
double dp1[3], dp2[3], dp3[3], dp4[3];
double dtau1, dtau2, dtau3, dtau4;
double r[3], p[3], tau;
int i, thermal;
thermal = drpdt(part, t, part->r, part->p, dr1, dp1, &dtau1, dt);
if(thermal) return 1;
for (i = 0; i < 3; i++) {
r[i] = part->r[i] + dr1[i] / 2;
p[i] = part->p[i] + dp1[i] / 2;
}
tau = part->tau + dtau1 / 2;
thermal = drpdt(part, t + 0.5 * dt, r, p, dr2, dp2, &dtau2, dt);
if(thermal) return 1;
for (i = 0; i < 3; i++) {
r[i] = part->r[i] + dr2[i] / 2;
p[i] = part->p[i] + dp2[i] / 2;
}
tau = part->tau + dtau2 / 2;
thermal = drpdt(part, t + 0.5 * dt, r, p, dr3, dp3, &dtau3, dt);
if(thermal) return 1;
for (i = 0; i < 3; i++) {
r[i] = part->r[i] + dr3[i];
p[i] = part->p[i] + dp3[i];
}
tau = part->tau + dtau3;
thermal = drpdt(part, t + dt, r, p, dr4, dp4, &dtau4, dt);
if(thermal) return 1;
if (!update) return 0;
for (i = 0; i < 3; i++) {
part->r[i] = part->r[i] + dr1[i] / 6 + dr2[i] / 3 + dr3[i] / 3 + dr4[i] / 6;
part->p[i] = part->p[i] + dp1[i] / 6 + dp2[i] / 3 + dp3[i] / 3 + dp4[i] / 6;
}
part->tau = part->tau + dtau1 / 6 + dtau2 / 3 + dtau3 / 3 + dtau4 / 6;
return 0;
}
void
rk4(double t, double dt)
/* Implements a full RK4 step of all the particles in the list. */
{
particle_t *plist1, *plist2, *plist3;
particle_t *part1, *part2, *part3;
particle_t *part;
int i;
drpdt_all(particle_head, t, dt);
plist1 = rkstep(particle_head, particle_head, 0.5);
drpdt_all(plist1, t + 0.5 * dt, dt);
plist2 = rkstep(particle_head, plist1, 0.5);
drpdt_all(plist2, t + 0.5 * dt, dt);
plist3 = rkstep(particle_head, plist2, 1.0);
drpdt_all(plist3, t + dt, dt);
for(part = particle_head, part1 = plist1, part2 = plist2, part3 = plist3;
part;
part = part->next,
part1 = part1->next,
part2 = part2->next,
part3 = part3->next) {
double dr[3], dp[3], s;
for (i = 0; i < 3; i++) {
dr[i] = (part->dr[i] / 6 + part1->dr[i] / 3 +
part2->dr[i] / 3 + part3->dr[i] / 6);
dp[i] = (part->dp[i] / 6 + part1->dp[i] / 3 +
part2->dp[i] / 3 + part3->dp[i] / 6);
}
if (nwalls) {
track_crossing(part, dr, dp, t, dt);
}
/* We use two loops in the index in order to find the wall intersections. */
for (i = 0; i < 3; i++) {
part->r[i] += dr[i];
part->p[i] += dp[i];
}
part->tau = (part->tau + part->dtau / 6 + part1->dtau / 3 +
part2->dtau / 3 + part3->dtau / 6);
part->thermal = (part->thermal || part1->thermal || part2->thermal ||
part3->thermal);
}
list_erase(plist1);
list_erase(plist2);
list_erase(plist3);
}
static void
track_crossing(particle_t *part, double dr[3], double dp[3],
double t, double dt)
/* Checks if the particle is crossing the wall and if so adds
an element to the list of corssings. */
{
double s;
crossing_t *crossing;
int wall, i;
for (wall = 0; wall < nwalls; wall++) {
s = (Z_WALL[wall] - part->r[2]) / dr[2];
if (s <= 0.0 || s > 1.0) continue;
if (part->rightmost_wall < wall) part->rightmost_wall = wall;
if (FIRST_WALL_ONLY && part->rightmost_wall != wall) continue;
crossing = xcalloc(1, sizeof(crossing_t));
for (i = 0; i < 3; i++) {
crossing->r[i] = part->r[i] + dr[i] * s;
crossing->p[i] = part->p[i] + dp[i] * s;
}
crossing->ptype = part->ptype;
crossing->id = part->id;
crossing->wall = wall;
crossing->t = t + s * dt;
crossing->next = crossing_head;
crossing_head = crossing;
crossing_count++;
if (DELETE_AT_WALL) {
part->thermal = TRUE;
}
}
}
particle_t *
rkstep(particle_t *plist0, particle_t *plist1, double rkfactor)
/* Performs a Runke-Kutta inner step. It reads from plist0 and plist1
and creates a new particle list. This new list is not doubly linked
and does not touch particle_count, particle_head, etc.
For the new particles, we take position/momenta from plist0 and derivatives
from plist1; they can be the same.
*/
{
particle_t *part0, *part1, *newlist = NULL, *tail = NULL, *newpart;
int i;
for(part0 = plist0, part1 = plist1; part0;
part0 = part0->next, part1 = part1->next) {
newpart = particle_init(part0->ptype);