-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtestDistance.c
128 lines (103 loc) · 3.88 KB
/
testDistance.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
/*******************************************************************************
* This file is part of SWIFT.
* Copyright (C) 2016 Matthieu Schaller ([email protected]).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include <config.h>
/* Local includes. */
#include "swift.h"
/* System includes. */
#include <fenv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void compute_interaction(struct part *pi, struct part *pj, float mu_0, float a,
float H) {
/* Compute the distance between the two particles */
const float dx[3] = {pi->x[0] - pj->x[0], pi->x[1] - pj->x[1],
pi->x[2] - pj->x[2]};
const float r2 = dx[0] * dx[0] + dx[1] * dx[1] + dx[2] * dx[2];
if (r2 < pi->h * pi->h * kernel_gamma2) {
/* And interact them (density) */
runner_iact_density(r2, dx, pi->h, pj->h, pi, pj, a, H);
runner_iact_mhd_density(r2, dx, pi->h, pj->h, pi, pj, mu_0, a, H);
runner_iact_chemistry(r2, dx, pi->h, pj->h, pi, pj, a, H);
runner_iact_pressure_floor(r2, dx, pi->h, pj->h, pi, pj, a, H);
runner_iact_star_formation(r2, dx, pi->h, pj->h, pi, pj, a, H);
#ifdef EXTRA_HYDRO_LOOP
/* And interact them (gradient) */
runner_iact_gradient(r2, dx, pi->h, pj->h, pi, pj, a, H);
runner_iact_mhd_gradient(r2, dx, pi->h, pj->h, pi, pj, mu_0, a, H);
#endif
/* And interact them (force) */
runner_iact_force(r2, dx, pi->h, pj->h, pi, pj, a, H);
runner_iact_mhd_force(r2, dx, pi->h, pj->h, pi, pj, mu_0, a, H);
}
}
void test(void) {
/* Start with some values for the cosmological parameters */
const float a = (float)random_uniform(0.8, 1.);
const float H = 1.f;
const float mu_0 = 4. * M_PI;
/* Create two random particles (don't do this at home !) */
struct part pi, pj;
for (size_t i = 0; i < sizeof(struct part) / sizeof(float); ++i) {
*(((float *)&pi) + i) = (float)random_uniform(0., 2.);
*(((float *)&pj) + i) = (float)random_uniform(0., 2.);
}
/* Make the particle smoothing length, id and time-bin reasonable */
pi.h = 1.f;
pj.h = 1.f;
pi.id = 1ll;
pj.id = 2ll;
pi.time_bin = 1;
pj.time_bin = 1;
/* Place the first particle at (1, 1, 1) */
pi.x[0] = 1.;
pi.x[1] = 1.;
pi.x[2] = 1.;
/* Move the second particle at various distances from the first */
for (double dist = 1.0f; 1.0 + dist > 1.0; dist /= 2.) {
pj.x[0] = pi.x[0] + random_uniform(0., dist * pi.h);
pj.x[1] = pi.x[1] + random_uniform(0., dist * pi.h);
pj.x[2] = pi.x[2] + random_uniform(0., dist * pi.h);
compute_interaction(&pi, &pj, mu_0, a, H);
}
/* Also test 0 distance */
pj.x[0] = pi.x[0];
pj.x[1] = pi.x[1];
pj.x[2] = pi.x[2];
compute_interaction(&pi, &pj, mu_0, a, H);
}
int main(int argc, char *argv[]) {
/* Initialize CPU frequency, this also starts time. */
unsigned long long cpufreq = 0;
clocks_set_cpufreq(cpufreq);
/* Choke on FPEs */
#ifdef HAVE_FE_ENABLE_EXCEPT
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
#endif
/* Get some randomness going */
const int seed = time(NULL);
message("Seed = %d", seed);
srand(seed);
for (int i = 0; i < 100; ++i) {
message("Random test %d/100", i);
test();
}
message("All good");
return 0;
}