dimension.h 7.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2016   Matthieu Schaller (matthieu.schaller@durham.ac.uk).
 *
 * 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/>.
 *
 ******************************************************************************/
#ifndef SWIFT_DIMENSION_H
#define SWIFT_DIMENSION_H

Matthieu Schaller's avatar
Matthieu Schaller committed
22
23
24
25
26
27
/**
 * @file dimension.h
 * @brief Defines the dimensionality \f$d\f$ of the problem and (fast)
 * mathematical functions involving it
 */

28
29
30
31
32
33
34
/* Config parameters. */
#include "../config.h"

/* Local headers. */
#include "inline.h"
#include "vector.h"

35
36
#include <math.h>

37
38
39
40
41
/* First define some constants */
#if defined(HYDRO_DIMENSION_3D)

#define hydro_dimension 3.f
#define hydro_dimension_inv 0.3333333333f
42
43
#define hydro_dimension_unit_sphere ((float)(4. * M_PI / 3.))
#define hydro_dimension_unit_sphere_inv ((float)(3. * M_1_PI / 4.))
44
45
46
47
48

#elif defined(HYDRO_DIMENSION_2D)

#define hydro_dimension 2.f
#define hydro_dimension_inv 0.5f
49
50
#define hydro_dimension_unit_sphere ((float)M_PI)
#define hydro_dimension_unit_sphere_inv ((float)M_1_PI)
51
52
53
54
55

#elif defined(HYDRO_DIMENSION_1D)

#define hydro_dimension 1.f
#define hydro_dimension_inv 1.f
56
57
#define hydro_dimension_unit_sphere 2.f
#define hydro_dimension_unit_sphere_inv 0.5f
58
59
60

#else

61
#error "A problem dimensionality must be chosen in config.h !"
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

#endif

/**
 * @brief Returns the argument to the power given by the dimension
 *
 * Computes \f$x^d\f$.
 */
__attribute__((always_inline)) INLINE static float pow_dimension(float x) {

#if defined(HYDRO_DIMENSION_3D)

  return x * x * x;

#elif defined(HYDRO_DIMENSION_2D)

  return x * x;

#elif defined(HYDRO_DIMENSION_1D)

  return x;

#else

  error("The dimension is not defined !");
  return 0.f;

#endif
}

/**
 * @brief Returns the argument to the power given by the dimension plus one
 *
 * Computes \f$x^{d+1}\f$.
 */
__attribute__((always_inline)) INLINE static float pow_dimension_plus_one(
    float x) {

#if defined(HYDRO_DIMENSION_3D)

  const float x2 = x * x;
  return x2 * x2;

#elif defined(HYDRO_DIMENSION_2D)

  return x * x * x;

#elif defined(HYDRO_DIMENSION_1D)

  return x * x;

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
#else

  error("The dimension is not defined !");
  return 0.f;

#endif
}

/**
 * @brief Returns the argument to the power given by the dimension minus one
 *
 * Computes \f$x^{d-1}\f$.
 */
__attribute__((always_inline)) INLINE static float pow_dimension_minus_one(
    float x) {

#if defined(HYDRO_DIMENSION_3D)

  return x * x;

#elif defined(HYDRO_DIMENSION_2D)

  return x;

#elif defined(HYDRO_DIMENSION_1D)

  return 1.f;

141
142
143
144
145
146
147
148
#else

  error("The dimension is not defined !");
  return 0.f;

#endif
}

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
/**
 * @brief Inverts the given dimension by dimension matrix (in place)
 *
 * @param A A 3x3 matrix of which we want to invert the top left dxd part
 */
__attribute__((always_inline)) INLINE static void
invert_dimension_by_dimension_matrix(float A[3][3]) {

#if defined(HYDRO_DIMENSION_3D)

  float detA, Ainv[3][3];

  detA = A[0][0] * A[1][1] * A[2][2] + A[0][1] * A[1][2] * A[2][0] +
         A[0][2] * A[1][0] * A[2][1] - A[0][2] * A[1][1] * A[2][0] -
         A[0][1] * A[1][0] * A[2][2] - A[0][0] * A[1][2] * A[2][1];

  if (detA && !isnan(detA)) {
    Ainv[0][0] = (A[1][1] * A[2][2] - A[1][2] * A[2][1]) / detA;
    Ainv[0][1] = (A[0][2] * A[2][1] - A[0][1] * A[2][2]) / detA;
    Ainv[0][2] = (A[0][1] * A[1][2] - A[0][2] * A[1][1]) / detA;
    Ainv[1][0] = (A[1][2] * A[2][0] - A[1][0] * A[2][2]) / detA;
    Ainv[1][1] = (A[0][0] * A[2][2] - A[0][2] * A[2][0]) / detA;
    Ainv[1][2] = (A[0][2] * A[1][0] - A[0][0] * A[1][2]) / detA;
    Ainv[2][0] = (A[1][0] * A[2][1] - A[1][1] * A[2][0]) / detA;
    Ainv[2][1] = (A[0][1] * A[2][0] - A[0][0] * A[2][1]) / detA;
    Ainv[2][2] = (A[0][0] * A[1][1] - A[0][1] * A[1][0]) / detA;
  } else {
    Ainv[0][0] = 0.0f;
    Ainv[0][1] = 0.0f;
    Ainv[0][2] = 0.0f;
    Ainv[1][0] = 0.0f;
    Ainv[1][1] = 0.0f;
    Ainv[1][2] = 0.0f;
    Ainv[2][0] = 0.0f;
    Ainv[2][1] = 0.0f;
    Ainv[2][2] = 0.0f;
  }

  A[0][0] = Ainv[0][0];
  A[0][1] = Ainv[0][1];
  A[0][2] = Ainv[0][2];
  A[1][0] = Ainv[1][0];
  A[1][1] = Ainv[1][1];
  A[1][2] = Ainv[1][2];
  A[2][0] = Ainv[2][0];
  A[2][1] = Ainv[2][1];
  A[2][2] = Ainv[2][2];

#elif defined(HYDRO_DIMENSION_2D)

  float detA, Ainv[2][2];

  detA = A[0][0] * A[1][1] - A[0][1] * A[1][0];

  if (detA && !isnan(detA)) {
    Ainv[0][0] = A[1][1] / detA;
    Ainv[0][1] = -A[0][1] / detA;
    Ainv[1][0] = -A[1][0] / detA;
    Ainv[1][1] = A[0][0] / detA;
  } else {
    Ainv[0][0] = 0.0f;
    Ainv[0][1] = 0.0f;
    Ainv[1][0] = 0.0f;
    Ainv[1][1] = 0.0f;
  }

  A[0][0] = Ainv[0][0];
  A[0][1] = Ainv[0][1];
  A[1][0] = Ainv[1][0];
  A[1][1] = Ainv[1][1];

#elif defined(HYDRO_DIMENSION_1D)

  if (A[0][0] && !isnan(A[0][0])) {
    A[0][0] = 1.0f / A[0][0];
  } else {
    A[0][0] = 0.0f;
  }

#else

  error("The dimension is not defined !");

#endif
}

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
/**
 * @brief Get the radius of a dimension sphere with the given volume
 *
 * @param volume Volume of the dimension sphere
 * @return Radius of the dimension sphere
 */
__attribute__((always_inline)) INLINE static float get_radius_dimension_sphere(
    float volume) {

#if defined(HYDRO_DIMENSION_3D)

  return cbrtf(volume * hydro_dimension_unit_sphere_inv);

#elif defined(HYDRO_DIMENSION_2D)

  return sqrtf(volume * hydro_dimension_unit_sphere_inv);

#elif defined(HYDRO_DIMENSION_1D)

  return volume * hydro_dimension_unit_sphere_inv;

#else

  error("The dimension is not defined !");
  return 0.f;
260

261
262
263
#endif
}

264
/* ------------------------------------------------------------------------- */
265
266
#ifdef WITH_VECTORIZATION

267
268
269
270
271
272
/**
 * @brief Returns the argument to the power given by the dimension (vector
 * version)
 *
 * Computes \f$x^d\f$.
 */
273
274
275
276
277
__attribute__((always_inline)) INLINE static vector pow_dimension_vec(
    vector x) {

#if defined(HYDRO_DIMENSION_3D)

278
  return (vector)(vec_mul(vec_mul(x.v, x.v), x.v));
279
280
281

#elif defined(HYDRO_DIMENSION_2D)

282
  return (vector)(vec_mul(x.v, x.v));
283
284
285
286
287
288
289
290
291
292
293
294
295

#elif defined(HYDRO_DIMENSION_1D)

  return x;

#else

  error("The dimension is not defined !");
  return vec_set(0.f);

#endif
}

296
297
298
299
300
301
/**
 * @brief Returns the argument to the power given by the dimension plus one
 * (vector version)
 *
 * Computes \f$x^{d+1}\f$.
 */
302
303
304
305
306
__attribute__((always_inline)) INLINE static vector pow_dimension_plus_one_vec(
    vector x) {

#if defined(HYDRO_DIMENSION_3D)

307
308
  const vector x2 = (vector)(vec_mul(x.v, x.v));
  return (vector)(vec_mul(x2.v, x2.v));
309
310
311

#elif defined(HYDRO_DIMENSION_2D)

312
  return (vector)(vec_mul(x.v, vec_mul(x.v, x.v)));
313
314
315

#elif defined(HYDRO_DIMENSION_1D)

316
  return (vector)(vec_mul(x.v, x.v));
317
318
319
320
321
322
323
324
325
326
327

#else

  error("The dimension is not defined !");
  return vec_set(0.f);

#endif
}
#endif

#endif /* SWIFT_DIMENSION_H */