forked from kcroker/Gadget-2.0.7-ngravs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngravs.c
735 lines (592 loc) · 23.5 KB
/
ngravs.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <mpi.h>
#include <sys/types.h>
#include <unistd.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_math.h>
#include "allvars.h"
#include "proto.h"
/*! \file ngravs.c
* \brief defines multiple types of gravitational interaction between particles
*
* This file contains functions that determine
* the differing force laws between differing particle types. These functions are
* used to compute acceleration, potential, and softened contributions during the tree walk
* in forcetree.c and the Fourier kernel for free-space boundary conditions.
*
* If you wish to add altered interactions between your 6 particle types in simulation,
* this is where you define those altered interactions. You must then 'wire' them manually
* to the particle types, follow the examples below.
*
* Beware that GADGET-2 computes acceleration adjustements to a target due to sources
* so if your force laws violate SEP, you must be careful about the ordering of source
* and target.
*
*/
/*! This function must be modified to point to your desired
* extensions to gravity. It determines which force laws
* are used to compute interactions.
*/
void wire_grav_maps(void) {
int i,j;
// KC 8/11/14 Wiring
//
// NOTE: For all interaction functions
//
// InteractionFunctions[TARGET][SOURCE]
//
// NOTE: NgravsNames[][] is used to index things used by the simulation
// code, like lattice correction tables. So please make a unique identifier!
//
#if !defined NGRAVS_STOCK_TESTING && !defined NGRAVS_ACCUMULATOR_TESTING
/////////////////////// WIRING FOR RESEARCH RUNS ///////////////////
//
// Here is where you wire for your research runs. If you wish to verify force accuracy,
// profile, or otherwise, enable the below defines in the Makefile.
// Please see comments below in NEWTONIAN COMPARISONS for guidance in
// defining the wiring appropriately. There is some subtlty with respect to
// source and target.
//
////////////////////////////////////////////////////////////////////
///////////////// END WIRING FOR RESEARCH RUNS ///////////////////////
#elif defined NGRAVS_STOCK_TESTING && !defined NGRAVS_ACCUMULATOR_TESTING
////////////////////// WIRING FOR NEWTONIAN COMPARISONS //////////////
//
// This code automatically populates all gravitational types with
// the Newtonian interaction. This is for comparison against unmodified
// Gadget-2 in consistency, force accuracy, and performance
//
//////////////////////////////////////////////////////////////////////
for(i = 0; i < N_GRAVS; ++i) {
for(j = 0; j < N_GRAVS; ++j) {
//////////////////////// NAMES ////////////////////////////
NgravsNames[i][j] = "Newton";
AccelFxns[i][j] = newtonian;
AccelSplines[i][j] = plummer;
#if defined PERIODIC
// KC 12/6/14
// Note that if these are computing lattice forces from exotic objects
// where scale parameters depend on the mass, the computed value of
// the correction should be such that:
//
// (summed total mass in node) * (*LatticeForce[][])(...) = (best approximation)
//
// For Newtonian things, this is always the case.
//
LatticeForce[i][j] = ewald_force;
LatticePotential[i][j] = ewald_psi;
LatticeZero[i][j] = 2.8372975;
#endif
////////////////////////// MESH AND POTENTIAL //////////////////////////////
// KC 11/25/14
// Note that these are the k-space Greens Functions
// for a source at the origin
#ifdef PMGRID
GreensFxns[i][j] = pgdelta;
#endif
// KC 11/25/14
// Note that these are also the position space Greens functions
// for a source at the origin
#if defined OUTPUTPOTENTIAL || defined PMGRID
PotentialFxns[i][j] = newtonian_pot;
PotentialSplines[i][j] = plummer_pot;
// KC 1/26/15
// This is used by the pm_nonperiodic when setting up the free space
// fft kernel. Nothing else should ever encounter it as other computations
// will hit the softening length.
PotentialZero[i][j] = -1 / (sqrt(M_PI) * (((double) ASMTH) / (2*PMGRID)));
#endif
}
}
////////////////////// END NEWTONIAN COMPARISONS WIRING ///////////////////
#elif !defined NGRAVS_STOCK_TESTING && defined NGRAVS_ACCUMULATOR_TESTING
///////////////// ACCUMULATOR TESTING RUNS ////////////////////////////
//
// This code specifically tests the N_\perp addition with a force law
// (BAM) which violates SEP. Here the N_\perp correction is an exact
// correction.
//
///////////////////////////////////////////////////////////////////////
NgravsNames[0][0] = "Newton";
NgravsNames[0][1] = "SourceBAM";
NgravsNames[1][0] = "TargetBAM";
NgravsNames[1][1] = "BAMBAM";
AccelFxns[0][0] = newtonian;
AccelSplines[0][0] = plummer;
AccelFxns[0][1] = AccelSplines[0][1] = sourcebambaryon;
AccelFxns[1][0] = AccelSplines[1][0] = sourcebaryonbam;
AccelFxns[1][1] = AccelSplines[1][1] = bambam;
#if defined OUTPUTPOTENTIAL || defined PMGRID
// These won't be used, as the simulation is non-periodic
GreensFxns[0][0] = none;
GreensFxns[0][1] = none;
GreensFxns[1][0] = none;
GreensFxns[1][1] = none;
PotentialFxns[0][0] = newtonian_pot;
PotentialSplines[0][0] = plummer_pot;
PotentialFxns[0][1] = PotentialSplines[0][1] = sourcebambaryon_pot;
PotentialFxns[1][0] = PotentialSplines[1][0] = sourcebaryonbam_pot;
PotentialFxns[1][1] = PotentialSplines[1][1] = bambam_pot;
// BAM has distinct limiting cases here. These values correspond to
// unit masses and are used in computing the Greens function kernel for
// non-periodic fft
PotentialZero[0][0] = -1 / (sqrt(M_PI) * (((double) ASMTH) / (2*PMGRID)));
PotentialZero[0][1] = -8*BAM_EPSILON;
PotentialZero[1][0] = -8*BAM_EPSILON;
PotentialZero[1][1] = -4*BAM_EPSILON;
#endif
//////////////// END ACCUMULATOR TESTING WIRING ///////////////////////////
#else
printf("ngravs: unsupported testing options defined in the Makefile. Cannot do (explicit) accumulator tests and Newtonian comparions at the same time");
endrun(1000);
#endif
}
/*! Establish the mapping between particle type and the native
* gravitational force between two particles of this type.
* Then call the user modified wiring function to initialize the table of
* AccelFxn function pointers to point to the appropriate compute functions.
* After, perform sanity checks on the user.
*/
void init_grav_maps(void) {
int i, j;
int counts[N_GRAVS];
#ifdef BAMTEST
double q;
if(ThisTask == 0) {
for(q=1.0e-4; q < 10.0; q += 1.0e-4)
printf("%e %e\n", q, bambam(2*BAM_EPSILON*M_PI, 2*BAM_EPSILON*M_PI, q, 1));
printf("\n");
for(q=1.0e-4; q < 10.0; q += 1.0e-4)
printf("%e %e\n", q, sourcebaryonbam(4*BAM_EPSILON*M_PI, BAM_EPSILON*M_PI, q, 1));
printf("\n");
for(q=1.0e-4; q < 10.0; q += 1.0e-4)
printf("%e %e\n", q, sourcebambaryon(BAM_EPSILON*M_PI, 4*BAM_EPSILON*M_PI, q, 1));
MPI_Barrier(MPI_COMM_WORLD);
exit(1001);
}
else
MPI_Barrier(MPI_COMM_WORLD);
#endif
// KC 10/17/14
// Code consistency checks
if(ThisTask == 0) {
#if !defined PEANOHILBERT && (defined PMGRID || defined PLACEHIRESREGION || defined PERIODIC)
printf("ngravs: Gravitational type ordering required for ngravs extension of Fourier mesh code. This ordering has been implemented on top of Peano-Hilbert ordering within the local task. Please enable PEANOHILBERT and recompile.\n");
endrun(1000);
#endif
}
memset(counts, 0, sizeof(int)*N_GRAVS);
/* KC 8/9/14 */
TypeToGrav[0] = All.GravityGas;
#ifdef PMGRID
if(TypeToGrav[0]) {
printf("ngravs: Fourier mesh code requires that gas particles must interact under gravity type 0. Please rewire and recompile.\n");
endrun(1000);
}
#endif
TypeToGrav[1] = All.GravityHalo;
TypeToGrav[2] = All.GravityDisk;
TypeToGrav[3] = All.GravityBulge;
TypeToGrav[4] = All.GravityStars;
TypeToGrav[5] = All.GravityBndry;
// Sanity check
for(i = 0; i < 6; ++i) {
if(TypeToGrav[i] >= N_GRAVS || TypeToGrav[i] < 0) {
printf("ngravs: native interaction %d declared for type %d does not exist. We better stop\n", TypeToGrav[i], i);
endrun(1000);
}
// See how many times we use this interaction
counts[TypeToGrav[i]]++;
}
// Provide some output
for(i = 0; i < N_GRAVS; ++i) {
if(ThisTask == 0) {
if(counts[i] > 0)
printf("ngravs: %d particle types natively interact via gravity %d\n", counts[i], i);
if(counts[i] == 6 && N_GRAVS > 1)
printf("ngravs: ALL types natively interact via gravity %d. Recomple with N_GRAVS=1 for best memory performance\n", i);
}
}
// First set the AccelFxns to null, so that we can check for missing settings
memset(AccelFxns, 0, N_GRAVS*N_GRAVS*sizeof(gravity));
memset(AccelSplines, 0, N_GRAVS*N_GRAVS*sizeof(gravity));
#if defined PERIODIC
memset(LatticeForce, 0, N_GRAVS*N_GRAVS*sizeof(latforce));
memset(LatticePotential, 0, N_GRAVS*N_GRAVS*sizeof(latpot));
#endif
#if defined OUTPUTPOTENTIAL || defined PMGRID
memset(PotentialFxns, 0, N_GRAVS*N_GRAVS*sizeof(gravity));
memset(PotentialSplines, 0, N_GRAVS*N_GRAVS*sizeof(gravity));
#endif
#ifdef PMGRID
memset(GreensFxns, 0, N_GRAVS*N_GRAVS*sizeof(gravity));
#endif
// This is the function you should alter
wire_grav_maps();
// Now run the sanity check
for(i = 0; i < N_GRAVS; ++i) {
for(j = 0; j < N_GRAVS; ++j) {
// Check for null
if(!AccelFxns[i][j]) {
printf("ngravs: unwired spot for acceleration [TARGET][SOURCE]: [%d][%d]. Please fix and recompile.\n", i, j);
endrun(1000);
}
if(!AccelSplines[i][j]) {
printf("ngravs: unwired spot for softening spline [TARGET][SOURCE]: [%d][%d]. Please fix and recompile.\n", i, j);
endrun(1000);
}
#if defined PERIODIC && (!defined PMGRID || defined FORCETEST)
if(!LatticeForce[i][j]) {
printf("ngravs: uwired spot for lattice correction force [TARGET][SOURCE]: [%d][%d]. Please fix and recompile.\n", i, j);
endrun(1000);
}
if(!LatticePotential[i][j]) {
printf("ngravs: uwired spot for lattice potential [TARGET][SOURCE]: [%d][%d]. Please fix and recompile.\n", i, j);
endrun(1000);
}
#endif
#if defined OUTPUTPOTENTIAL || defined PMGRID
if(!PotentialFxns[i][j]) {
printf("ngravs: uwired spot for potential [TARGET][SOURCE]: [%d][%d]. Please fix and recompile.\n", i, j);
endrun(1000);
}
if(!PotentialSplines[i][j]) {
printf("ngravs: uwired spot for softening potential spline [TARGET][SOURCE]: [%d][%d]. Please fix and recompile.\n", i, j);
endrun(1000);
}
#endif
#ifdef PMGRID
if(!GreensFxns[i][j]) {
printf("ngravs: unwired spot for Green's function [TARGET][SOURCE]: [%d][%d]. Please fix and recompile.\n", i, j);
endrun(1000);
}
#endif
#ifndef NGRAVS_L3VIOLATION
// Check that the forces are symmetric for unit mass
// Short circuit on the diagonal
if( i != j && ( (*AccelFxns[i][j])(1,1,0.5,3,1) != (*AccelFxns[j][i])(1,1,0.5,3,1))) {
printf("ngravs: force between particles which natively interact through gravities %d and %d is not symmetric. Newton's 3rd law violated. Stopping.\n", i, j);
endrun(1000);
}
if( i != j && ( (*AccelSplines[i][j])(1,1,0.5,3,1) != (*AccelSplines[j][i])(1,1,0.5,3,1))) {
printf("ngravs: softened (spline) force between particles which natively interact through gravities %d and %d is not symmetric. Newton's 3rd law violated. Stopping.\n", i, j);
endrun(1000);
}
#if defined PERIODIC
// Check that the lattice correction forces are symmetric for unit mass
// Short circuit on the diagonal
if( i != j && (LatticeForce[i][j] != LatticeForce[j][i])) {
printf("ngravs: lattice correction force between particles which natively interact through gravities %d and %d is not symmetric. Newton's 3rd law violated. Stopping.\n", i, j);
endrun(1000);
}
if( LatticePotential[i][j] != LatticePotential[j][i]) {
printf("ngravs: lattice correction potential between particles which natively interact through gravities %d and %d is not symmetric. Newton's 3rd law violated. Stopping.\n", i, j);
endrun(1000);
}
#endif
#ifdef PMGRID
// Check that Green's functions are symmetric
// Short circuit on the diagonal
if( i != j && ((*GreensFxns[i][j])(1,1,0.5,3,1) != (*GreensFxns[j][i])(1,1,0.5,3,1)) ) {
printf("ngravs: Green's functions for particles which natively interact through gravities %d and %d is not symmetric. Newton's 3rd law violated. Stopping.\n", i, j);
endrun(1000);
}
#endif
#if defined OUTPUT_POTENTIAL || defined PMGRID
// Check that the Potential functions are symmetric
// Short circuit on the diagonal
if( i != j && ((*PotentialFxns[i][j])(1,1,0.5,3,1) != (*PotentialFxns[j][i])(1,1,0.5,3,1))) {
printf("ngravs: potential functions for particles which natively interact through gravities %d and %d is not symmetric. Newton's 3rd law violated. Stopping.\n", i, j);
endrun(1000);
}
if( i != j && ((*PotentialSplines[i][j])(1,1,0.5,3,1) != (*PotentialSplines[j][i])(1,1,0.5,3,1)) ) {
printf("ngravs: softened (spline) potential between particles which natively interact through gravities %d and %d is not symmetric. Newton's 3rd law violated. Stopping.\n", i, j);
endrun(1000);
}
#endif
#else
if(!ThisTask)
printf("ngravs: Newton's 3rd law violations *permitted*, no equality check was performed.\n");
#endif
}
}
}
/*! This is no gravity. It returns 0.0 regardless of input
*/
double none(double target, double source, double h, double r, long N){
return 0.0;
}
// KC 10/18/14
//
// WARNING: Note that, presumably to eliminate a plethora of unnecessary negations,
// Gadget-2 works with the positive of the acceleration.
//
// ALL ACCELERATION SIGNS APPEARING HERE ARE TO BE INVERTED FROM WHAT YOU NORMALLY WOULD WRITE
//
// WARNING: These give the normalized accelerations! They include an additional factor of 1/r
// so that taking the vector magnitude of this function's return value times
// the dx_i gives the correct force.
//
/*! This is Newtonian gravity, and is the usual baryon-baryon interaction
*/
double newtonian(double target, double source, double h, double r, long N) {
// Note newtonian does not violate SEP
return source / (r*r*r);
}
/*! This is **inverted** Newtonian gravity, for use in the Hohmann & Wolfarth scenario
*/
double neg_newtonian(double target, double source, double h, double r, long N) {
// Note newtonian does not violate SEP
return -source / (r*r*r);
}
/*! This is the usual Newtonian gravitational potential
*
*/
double newtonian_pot(double target, double source, double h, double r, long N) {
return source / r;
}
/*! This is the **inverted** Newtonian potential
*/
double neg_newtonian_pot(double target, double source, double h, double r, long N) {
return -source / r;
}
// KC 10/18/14
// NOTE: Green's functions are not inverted from their usual sign
/*! This is the box periodic Green's function for a point source of unit mass
*/
double pgdelta(double kx, double ky, double kz, double h, long N) {
return 1.0/(kx * kx + ky * ky + kz * kz);
}
/*! This is the **inverted** box periodic Green's function for a point source of unit mass,
* for use in the Hohmann & Wolfarth scenario
*/
double neg_pgdelta(double kx, double ky, double kz, double h, long N) {
return -1.0/(kx * kx + ky * ky + kz * kz);
}
/*! This is the Plummer spline used by GADGET-2
*/
double plummer(double target, double source, double h, double r, long N) {
double h_inv;
h_inv = 1/h;
r *= h_inv;
if(r < 0.5)
return source * h_inv * h_inv * h_inv *
(10.666666666667 + r * r * (32.0 * r - 38.4));
else
return source * h_inv * h_inv * h_inv *
(21.333333333333 - 48.0 * r +
38.4 * r * r - 10.666666666667 * r * r * r - 0.066666666667 / (r * r * r));
}
/*! This is the "inverted" Plummer spline for use in the Hohmann & Wolfarth scenario
*/
double neg_plummer(double target, double source, double h, double r, long N) {
double h_inv;
// KC 10/26/14
// It remains a question whether calling a fxn with 5 things on the stack is going to be
// slower than just multiplying things out every time and calling something with 3 things
h_inv = 1/h;
r *= h_inv;
if(r < 0.5)
return -source * h_inv * h_inv * h_inv *
(10.666666666667 + r * r * (32.0 * r - 38.4));
else
return -source * h_inv * h_inv * h_inv *
(21.333333333333 - 48.0 * r +
38.4 * r * r - 10.666666666667 * r * r * r - 0.066666666667 / (r * r * r));
}
/*! This is the potential of the Plummer spline used by GADGET-2
*/
double plummer_pot(double target, double source, double h, double r, long N) {
double h_inv;
h_inv = 1/h;
r *= h_inv;
if(r < 0.5)
return source * h_inv * (-2.8 + r * r * (5.333333333333 + r * r * (6.4 * r - 9.6)));
else
return source * h_inv *
(-3.2 + 0.066666666667 / r + r * r * (10.666666666667 +
r * (-16.0 + r * (9.6 - 2.133333333333 * r))));
}
/*! This is the negative potential of the Plummer spline, for use in Hohmann & Wolfarth scenario
*/
double neg_plummer_pot(double target, double source, double h, double r, long N) {
double h_inv;
h_inv = 1/h;
r *= h_inv;
if(r < 0.5)
return -source * h_inv * (-2.8 + r * r * (5.333333333333 + r * r * (6.4 * r - 9.6)));
else
return -source * h_inv *
(-3.2 + 0.066666666667 / r + r * r * (10.666666666667 +
r * (-16.0 + r * (9.6 - 2.133333333333 * r))));
}
/*! This is Newtonian gravity with a Yukawa modification
* c.f. Shirata, Yoshida, et. al 2005
*/
double newyukawa(double target, double source, double h, double r, long N) {
#define YUKAWA_ALPHA 1
#define YUKAWA_LAMBDA_INV 1e-2
return source / (r * r * r) * -YUKAWA_ALPHA * expm1f(-r*YUKAWA_LAMBDA_INV);
}
/*! This is the BAM-BAM interaction
* c.f. http://arxiv.org/abs/1408.2702
*/
double bambam(double target, double source, double h, double r, long N) {
// Note apparent SEP violation
// Note naturally softened
// Note adjustment of the internal scale by N. Thus the scale is determined by the average mass content of the cell.
// In the case where the all BAM halos have the same mass parameter, this correction is the *exact* correction.
double eta, rho;
double eta3;
double reta, reta2;
eta = 4.0*M_PI*BAM_EPSILON/(target+source/N);
rho = 2*target*source/M_PI;
reta = r * eta;
reta2 = reta * reta;
eta3 = eta * eta * eta;
if(reta < 0.1) {
// orig taylor: reta - (reta)^3/3 + (reta)^5/5 - (reta)^7/7
// we want to divide out by the r
// so: rho*(eta - r^2eta^3/3 + r^4eta^5/5 - r^6eta^7/7)
// the differentiate termwise to get the force
// also multiply by -1: F \def -\grad pot
// also must divide by an additional 1/r to give the unit vector in code!
//
return rho * eta3 * (2.0/3.0 - 4.0*reta2/5.0 + 6.0*reta2*reta2/7.0);
}
else
return rho * eta3 * (atan(reta)/(reta2*reta) - 1.0/(reta2*(1+reta2)));
}
/*! This is the BAM-Baryon interaction sourced by a BAM.
* Note that here the target is a baryon!!
* The force laws are necessarily symmetric, but the computation GADGET-2 uses
* is not the force, but the adjustment to the acceleration of the target.
* So you have to be careful here.
*/
double sourcebambaryon(double target, double source, double h, double r, long N) {
double eta, rho;
double eta3;
double reta, reta2;
rho = 2*target*source/M_PI;
eta = 4.0*M_PI*BAM_EPSILON*N/source;
reta = r * eta;
reta2 = reta * reta;
eta3 = eta * eta * eta;
if(reta < 0.1) {
// orig taylor: reta - (reta)^3/3 + (reta)^5/5 - (reta)^7/7
// we want to divide out by the r
// so: rho*(eta - r^2eta^3/3 + r^4eta^5/5 - r^6eta^7/7)
// the differentiate termwise to get the force
// also multiply by -1: F \def -\grad pot
// also must divide by an additional 1/r to give the unit vector in code!
//
return rho * eta3 * (2.0/3.0 - 4.0*reta2/5.0 + 6.0*reta2*reta2/7.0);
}
else
return rho * eta3 * (atan(reta)/(reta2*reta) - 1.0/(reta2*(1+reta2)));
}
/*! This is the BAM-Baryon interaction sourced by a baryon.
Note that here the target is a BAM!!
The force laws are necessarily symmetric, but the computation GADGET-2 uses
is not the force, but the adjustment to the acceleration of the target.
So you have to be careful here.
*/
double sourcebaryonbam(double target, double source, double h, double r, long N) {
// Note apparent SEP violation
// Note naturally softened
double eta, rho;
double eta3;
double reta, reta2;
eta = 4.0*M_PI*BAM_EPSILON/target;
rho = 2*target*source/M_PI;
reta = r * eta;
reta2 = reta * reta;
eta3 = eta * eta * eta;
if(reta < 0.1) {
// orig taylor: reta - (reta)^3/3 + (reta)^5/5 - (reta)^7/7
// we want to divide out by the r
// so: rho*(eta - r^2eta^3/3 + r^4eta^5/5 - r^6eta^7/7)
// the differentiate termwise to get the force
// also multiply by -1: F \def -\grad pot
// also must divide by an additional 1/r to give the unit vector in code!
//
return rho * eta3 * (2.0/3.0 - 4.0*reta2/5.0 + 6.0*reta2*reta2/7.0);
}
else
return rho * eta3 * (atan(reta)/(reta2*reta) - 1.0/(reta2*(1+reta2)));
}
/*! This is the BAM-BAM potential (or free-space Greens fxn in position representation)
* c.f. http://arxiv.org/abs/1408.2702
*/
double bambam_pot(double target, double source, double h, double r, long N) {
// Use a 7th order Taylor polynomial if tan(x) x < 1/10
// The error at x = 1/10 for tan(x) is then < 10^-7
// Its not beyond double precision, but its almost at float.
//
// This will be kinda slow...
//
double eta;
double rho;
double reta, reta2, reta4;
rho = 2*target*source/M_PI;
eta = 4.0*M_PI*BAM_EPSILON/(target+source/N);
reta = r * eta;
reta2 = reta * reta;
reta4 = reta2 * reta2;
if(reta < 0.1) {
// orig taylor: reta - (reta)^3/3 + (reta)^5/5 - (reta)^7/7
// we want to divide out by the r
// so: rho*(eta - r^2eta^3/3 + r^4eta^5/5 - r^6eta^7/7)
//
return rho * eta*(1 - reta2/3.0 + reta4/5.0 - reta2*reta4/7.0);
}
else
return rho * atan(reta)/r;
}
// Now note that the advantage of the simple taylor series is that it is easy to compute the
// derivative and turn that into a force.
/*! This is the BAM-Baryon potential. Here a baryon acts as a source.
*/
double sourcebaryonbam_pot(double target, double source, double h, double r, long N) {
double eta;
double rho;
double reta, reta2, reta4;
rho = 2*target*source/M_PI;
eta = 4.0*BAM_EPSILON*M_PI*N/target;
reta = r * eta;
reta2 = reta * reta;
reta4 = reta2 * reta2;
if(reta < 0.1) {
// orig taylor: reta - (reta)^3/3 + (reta)^5/5 - (reta)^7/7
// we want to divide out by the r
// so: rho*(eta - r^2eta^3/3 + r^4eta^5/5 - r^6eta^7/7)
//
return rho * eta*(1 - reta2/3.0 + reta4/5.0 - reta2*reta4/7.0);
}
else
return rho * atan(reta)/r;
}
/*! This is the BAM-Baryon potential. Here a BAM acts as a source.
*/
double sourcebambaryon_pot(double target, double source, double h, double r, long N) {
double eta;
double rho;
double reta, reta2, reta4;
rho = 2*target*source/M_PI;
eta = 4.0*BAM_EPSILON*M_PI*N/source;
reta = r * eta;
reta2 = reta * reta;
reta4 = reta2 * reta2;
if(reta < 0.1) {
// orig taylor: reta - (reta)^3/3 + (reta)^5/5 - (reta)^7/7
// we want to divide out by the r
// so: rho*(eta - r^2eta^3/3 + r^4eta^5/5 - r^6eta^7/7)
//
return rho * eta*(1 - reta2/3.0 + reta4/5.0 - reta2*reta4/7.0);
}
else
return rho * atan(reta)/r;
}