-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtestOutputList.c
170 lines (134 loc) · 4.54 KB
/
testOutputList.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
/*******************************************************************************
* This file is part of SWIFT.
* Copyright (C) 2016 James Willis ([email protected]).
* 2018 Peter W. Draper ([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>
/* Includes. */
#include "swift.h"
#define Ntest 3
#define tol 1e-12
#define itol 1000
#define filename "output_list_params.yml"
/* Expected values from file */
const double time_values[Ntest] = {
0.,
10.,
12.,
};
/* Expected values from file */
const double a_values[Ntest] = {
0.01,
0.1,
0.5,
};
void test_no_cosmo(struct engine *e, const char *name, const int with_assert) {
message("Testing output time for %s without cosmology", name);
struct output_list *list = NULL;
double delta_time = 0;
double output_time = 0;
/* Test Time */
e->time_begin = 0;
e->time_end = 14;
e->time_base = (e->time_end - e->time_begin) / max_nr_timesteps;
e->ti_current = 0;
e->policy = 0;
/* initialize output_list */
output_list_init(&list, e, name, &delta_time);
output_list_print(list);
output_time = list->times[0];
for (int i = 0; i < Ntest; i++) {
/* Test last value */
if (with_assert) {
assert(fabs(output_time - time_values[i]) < tol);
}
/* Set current time */
e->ti_current = (output_time - e->time_begin) / e->time_base;
e->ti_current += itol;
/* Read next value */
integertime_t ti_next = 0;
output_list_read_next_time(list, e, name, &ti_next);
output_time = (double)(ti_next * e->time_base) + e->time_begin;
}
output_list_clean(&list);
};
void test_cosmo(struct engine *e, const char *name, const int with_assert) {
message("Testing output time for %s with cosmology", name);
struct output_list *list = NULL;
double delta_time = 0;
double output_time = 0;
/* Test Time */
e->time_base = log(e->time_end / e->cosmology->a_begin) / max_nr_timesteps;
e->ti_current = 0;
e->policy = engine_policy_cosmology;
/* initialize output_list */
output_list_init(&list, e, name, &delta_time);
output_list_print(list);
output_time = list->times[0];
for (int i = 0; i < Ntest; i++) {
/* Test last value */
if (with_assert) {
assert(fabs(output_time - a_values[i]) < tol);
}
/* Set current time */
e->ti_current = log(output_time / e->cosmology->a_begin) / e->time_base;
e->ti_current += 16;
/* Read next value */
integertime_t ti_next = 0;
output_list_read_next_time(list, e, name, &ti_next);
output_time = (double)exp(ti_next * e->time_base) * e->cosmology->a_begin;
}
output_list_clean(&list);
};
int main(int argc, char *argv[]) {
/* Initialize CPU frequency, this also starts time. */
unsigned long long cpufreq = 0;
clocks_set_cpufreq(cpufreq);
/* Create a structure to read file into. */
struct swift_params params;
/* Read the parameter file. */
parser_read_file(filename, ¶ms);
/* initialization of unit system */
struct unit_system us;
units_init_from_params(&us, ¶ms, "Units");
/* initialization of phys_const */
struct phys_const phys_const;
phys_const_init(&us, ¶ms, &phys_const);
/* initialization of cosmo */
struct cosmology cosmo;
cosmology_init(¶ms, &us, &phys_const, &cosmo);
/* Pseudo initialization of engine */
struct engine e;
bzero(&e, sizeof(struct engine));
e.cosmology = &cosmo;
e.parameter_file = ¶ms;
e.physical_constants = &phys_const;
e.internal_units = &us;
e.verbose = 1;
int with_assert = 1;
int without_assert = 0;
/* Test without cosmo */
test_no_cosmo(&e, "Time", with_assert);
/* Test with cosmo */
test_cosmo(&e, "Redshift", with_assert);
test_cosmo(&e, "ScaleFactor", with_assert);
test_cosmo(&e, "Time", without_assert);
cosmology_clean(&cosmo);
/* Write message and leave */
message("Test done");
return 0;
}