GADGET-4
test_symtensors.cc
Go to the documentation of this file.
1/*******************************************************************************
2 * \copyright This file is part of the GADGET4 N-body/SPH code developed
3 * \copyright by Volker Springel. Copyright (C) 2014-2020 by Volker Springel
4 * \copyright (vspringel@mpa-garching.mpg.de) and all contributing authors.
5 *******************************************************************************/
6
12#include "gadgetconfig.h"
13
14#ifdef DEBUG_SYMTENSORS
15
16#include <math.h>
17#include <mpi.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include "../data/allvars.h"
23#include "../data/dtypes.h"
24#include "../data/intposconvert.h"
25#include "../data/symtensors.h"
26#include "../sort/cxxsort.h"
27#include "../system/system.h"
28
29static bool compare_list(const int &a, const int &b) { return a < b; }
30
31static void symtensor_test_tensor4_contraction_with_tensor1(void)
32{
35
36 int I4[3][3][3][3];
37 int I3[3][3][3];
38 int I1[3] = {0, 1, 2};
39
40 int count = 0;
41
42 for(int i = 0; i < 3; i++)
43 for(int j = 0; j < 3; j++)
44 for(int k = 0; k < 3; k++)
45 for(int l = 0; l < 3; l++)
46 {
47 if(i <= j && j <= k && k <= l)
48 {
49 I4[i][j][k][l] = count++;
50 }
51 else
52 {
53 int li[] = {i, j, k, l};
54
55 mycxxsort(li, li + 4, compare_list);
56
57 I4[i][j][k][l] = I4[li[0]][li[1]][li[2]][li[3]];
58 }
59
60 // printf("%c%c%c%c: %d\n", 'X' + i, 'X' + j, 'X' + k, 'X' + l, I4[i][j][k][l]);
61 }
62
63 count = 0;
64
65 for(int i = 0; i < 3; i++)
66 for(int j = 0; j < 3; j++)
67 for(int k = 0; k < 3; k++)
68 {
69 if(i <= j && j <= k)
70 {
71 I3[i][j][k] = count++;
72 }
73 else
74 {
75 int li[] = {i, j, k};
76
77 mycxxsort(li, li + 3, compare_list);
78
79 I3[i][j][k] = I3[li[0]][li[1]][li[2]];
80 }
81
82 // printf("%c%c: %d\n", 'X' + i, 'X' + j, I2[i][j]);
83 }
84
85 for(int i = 0; i < 15; i++)
86 T4[i] = get_random_number();
87
88 for(int i = 0; i < 3; i++)
89 T1[i] = get_random_number();
90
91 /* now fill in the whole general matrix based on the symmetric one */
92 double M4[3][3][3][3];
93 for(int i = 0; i < 3; i++)
94 for(int j = 0; j < 3; j++)
95 for(int k = 0; k < 3; k++)
96 for(int l = 0; l < 3; l++)
97 M4[i][j][k][l] = T4[I4[i][j][k][l]];
98
99 /* now fill in the whole general matrix based on the symmetric one */
100 double M1[3];
101 for(int i = 0; i < 3; i++)
102 M1[i] = T1[I1[i]];
103
104 /* result of full matrix reduction */
105 double R3[3][3][3];
106 for(int i = 0; i < 3; i++)
107 for(int j = 0; j < 3; j++)
108 for(int k = 0; k < 3; k++)
109 {
110 R3[i][j][k] = 0;
111
112 for(int l = 0; l < 3; l++)
113 R3[i][j][k] += M4[i][j][k][l] * M1[l];
114 }
115
116 /* now let's compare the result */
117
118 symtensor3<double> S3 = T4 * T1; // reduction of symmetric 4-tensor with symmetric 2-tensor
119
120 printf("Result comparison:\n");
121
122 for(int i = 0; i < 3; i++)
123 for(int j = 0; j < 3; j++)
124 for(int k = 0; k < 3; k++)
125 {
126 if(i <= j && j <= k)
127 {
128 printf("%c%c%c: %g %g\n", 'X' + i, 'X' + j, 'X' + k, S3[I3[i][j][k]], R3[i][j][k]);
129 }
130 }
131
132 printf("\n");
133}
134
135static void symtensor_test_tensor4_contraction_with_tensor2(void)
136{
139
140 int I4[3][3][3][3];
141 int I2[3][3];
142
143 int count = 0;
144
145 for(int i = 0; i < 3; i++)
146 for(int j = 0; j < 3; j++)
147 for(int k = 0; k < 3; k++)
148 for(int l = 0; l < 3; l++)
149 {
150 if(i <= j && j <= k && k <= l)
151 {
152 I4[i][j][k][l] = count++;
153 }
154 else
155 {
156 int li[] = {i, j, k, l};
157
158 mycxxsort(li, li + 4, compare_list);
159
160 I4[i][j][k][l] = I4[li[0]][li[1]][li[2]][li[3]];
161 }
162
163 // printf("%c%c%c%c: %d\n", 'X' + i, 'X' + j, 'X' + k, 'X' + l, I4[i][j][k][l]);
164 }
165
166 // printf("count=%d\n\n", count);
167
168 count = 0;
169
170 for(int i = 0; i < 3; i++)
171 for(int j = 0; j < 3; j++)
172 {
173 if(i <= j)
174 {
175 I2[i][j] = count++;
176 }
177 else
178 {
179 int li[] = {i, j};
180
181 mycxxsort(li, li + 2, compare_list);
182
183 I2[i][j] = I2[li[0]][li[1]];
184 }
185
186 // printf("%c%c: %d\n", 'X' + i, 'X' + j, I2[i][j]);
187 }
188
189 // printf("count=%d\n\n", count);
190
191 for(int i = 0; i < 15; i++)
192 T4[i] = get_random_number();
193
194 for(int i = 0; i < 6; i++)
195 T2[i] = get_random_number();
196
197 /* now fill in the whole general matrix based on the symmetric one */
198 double M4[3][3][3][3];
199 for(int i = 0; i < 3; i++)
200 for(int j = 0; j < 3; j++)
201 for(int k = 0; k < 3; k++)
202 for(int l = 0; l < 3; l++)
203 M4[i][j][k][l] = T4[I4[i][j][k][l]];
204
205 /* now fill in the whole general matrix based on the symmetric one */
206 double M2[3][3];
207 for(int i = 0; i < 3; i++)
208 for(int j = 0; j < 3; j++)
209 M2[i][j] = T2[I2[i][j]];
210
211 /* result of full matrix reduction */
212 double R2[3][3];
213 for(int i = 0; i < 3; i++)
214 for(int j = 0; j < 3; j++)
215 {
216 R2[i][j] = 0;
217
218 for(int k = 0; k < 3; k++)
219 for(int l = 0; l < 3; l++)
220 R2[i][j] += M4[i][j][k][l] * M2[k][l];
221 }
222
223 /* now let's compare the result */
224
225 symtensor2<double> S2 = T4 * T2; // reduction of symmetric 4-tensor with symmetric 2-tensor
226
227 printf("Result comparison:\n");
228
229 for(int i = 0; i < 3; i++)
230 for(int j = 0; j < 3; j++)
231 {
232 if(i <= j)
233 {
234 printf("%c%c: %g %g\n", 'X' + i, 'X' + j, S2[I2[i][j]], R2[i][j]);
235 }
236 }
237
238 printf("\n");
239}
240
241static void symtensor_test_tensor4_contraction_with_tensor3(void)
242{
245
246 int I4[3][3][3][3];
247 int I3[3][3][3];
248
249 int count = 0;
250
251 for(int i = 0; i < 3; i++)
252 for(int j = 0; j < 3; j++)
253 for(int k = 0; k < 3; k++)
254 for(int l = 0; l < 3; l++)
255 {
256 if(i <= j && j <= k && k <= l)
257 {
258 I4[i][j][k][l] = count++;
259 }
260 else
261 {
262 int li[] = {i, j, k, l};
263
264 mycxxsort(li, li + 4, compare_list);
265
266 I4[i][j][k][l] = I4[li[0]][li[1]][li[2]][li[3]];
267 }
268
269 // printf("%c%c%c%c: %d\n", 'X' + i, 'X' + j, 'X' + k, 'X' + l, I4[i][j][k][l]);
270 }
271
272 // printf("count=%d\n\n", count);
273
274 count = 0;
275
276 for(int i = 0; i < 3; i++)
277 for(int j = 0; j < 3; j++)
278 for(int k = 0; k < 3; k++)
279 {
280 if(i <= j && j <= k)
281 {
282 I3[i][j][k] = count++;
283 }
284 else
285 {
286 int li[] = {i, j, k};
287
288 mycxxsort(li, li + 3, compare_list);
289
290 I3[i][j][k] = I3[li[0]][li[1]][li[2]];
291 }
292
293 // printf("%c%c: %d\n", 'X' + i, 'X' + j, I2[i][j]);
294 }
295
296 // printf("count=%d\n\n", count);
297
298 for(int i = 0; i < 15; i++)
299 T4[i] = get_random_number();
300
301 for(int i = 0; i < 10; i++)
302 T3[i] = get_random_number();
303
304 /* now fill in the whole general matrix based on the symmetric one */
305 double M4[3][3][3][3];
306 for(int i = 0; i < 3; i++)
307 for(int j = 0; j < 3; j++)
308 for(int k = 0; k < 3; k++)
309 for(int l = 0; l < 3; l++)
310 M4[i][j][k][l] = T4[I4[i][j][k][l]];
311
312 /* now fill in the whole general matrix based on the symmetric one */
313 double M3[3][3][3];
314 for(int i = 0; i < 3; i++)
315 for(int j = 0; j < 3; j++)
316 for(int k = 0; k < 3; k++)
317 M3[i][j][k] = T3[I3[i][j][k]];
318
319 /* result of full matrix reduction */
320 double R1[3];
321 for(int i = 0; i < 3; i++)
322 {
323 R1[i] = 0;
324
325 for(int j = 0; j < 3; j++)
326 for(int k = 0; k < 3; k++)
327 for(int l = 0; l < 3; l++)
328 R1[i] += M4[i][j][k][l] * M3[j][k][l];
329 }
330
331 /* now let's compare the result */
332
333 vector<double> S1 = T4 * T3; // reduction of symmetric 4-tensor with symmetric 2-tensor
334
335 printf("Result comparison:\n");
336
337 for(int i = 0; i < 3; i++)
338 printf("%c: %g %g\n", 'X' + i, S1[i], R1[i]);
339
340 printf("\n");
341}
342
343void symtensor_test(void)
344{
345 symtensor_test_tensor4_contraction_with_tensor1();
346 symtensor_test_tensor4_contraction_with_tensor2();
347 symtensor_test_tensor4_contraction_with_tensor3();
348
349 Terminate("Done with test");
350}
351
352#endif
double mycxxsort(T *begin, T *end, Tcomp comp)
Definition: cxxsort.h:39
#define Terminate(...)
Definition: macros.h:15
void symtensor_test(void)
double get_random_number(void)
Definition: system.cc:49