aboutsummaryrefslogtreecommitdiff
path: root/src/interpolate.c
blob: fcaa51ac1c2604efe53213d32b5a579d1bf7e2eb (plain)
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
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "cctk.h"
#include "cctk_Parameters.h"

#include "util_ErrorCodes.h"
#include "util_Table.h"

#include "reflection.h"



CCTK_INT
ReflectionSymmetry_Interpolate (CCTK_POINTER_TO_CONST restrict const cctkGH_,
                                CCTK_INT const N_dims,
                                CCTK_INT const local_interp_handle,
                                CCTK_INT const param_table_handle,
                                CCTK_INT const coord_system_handle,
                                CCTK_INT const N_interp_points,
                                CCTK_INT const interp_coords_type,
                                CCTK_POINTER_TO_CONST restrict const interp_coords[],
                                CCTK_INT const N_input_arrays,
                                CCTK_INT const input_array_indices[],
                                CCTK_INT const N_output_arrays,
                                CCTK_INT const output_array_types[],
                                CCTK_POINTER restrict const output_arrays[],
                                CCTK_INT const faces)
{
  cGH const * restrict const cctkGH = cctkGH_;
  DECLARE_CCTK_PARAMETERS;
  
  int do_reflection[6];
  
  CCTK_POINTER_TO_CONST restrict new_interp_coords[3];
  CCTK_INT newfaces;
  
  CCTK_INT * restrict operand_indices;
  CCTK_INT * restrict operation_codes;
  CCTK_INT * restrict output_array_indices;
  
  int dir;
  
  int iret;
  
  int m;
  int n;
  int d;
  
  int ierr;
  
  
  
  /* Get symmetry information */
  do_reflection[0] = reflection_x;
  do_reflection[1] = reflection_upper_x;
  do_reflection[2] = reflection_y;
  do_reflection[3] = reflection_upper_y;
  do_reflection[4] = reflection_z;
  do_reflection[5] = reflection_upper_z;
  
  newfaces = faces;
  for (d=0; d<6; ++d) {
    if (do_reflection[d]) {
      assert (newfaces & (1 << d));
      newfaces &= ~ (1 << d);
    }
  }
  
  
  
  /* Fold coordinates */
  assert (interp_coords_type == CCTK_VARIABLE_REAL);
  for (dir=0; dir<3; ++dir) {
    assert (! do_reflection[2*dir+1]);
    
    if (do_reflection[2*dir]) {
      new_interp_coords[dir]
        = malloc (N_interp_points * sizeof (CCTK_REAL));
      assert (N_interp_points == 0 || new_interp_coords[dir]);
      
      for (n=0; n<N_interp_points; ++n) {
        CCTK_REAL const pos = ((CCTK_REAL const *)interp_coords[dir])[n];
        CCTK_REAL const newpos = fabs(pos);
        ((CCTK_REAL *)new_interp_coords[dir])[n] = newpos;
      }
    } else {
      new_interp_coords[dir] = interp_coords[dir];
    }
  }
  
  
  
  /* Recursive call */
  iret = SymmetryInterpolateFaces
    (cctkGH_, 
     N_dims, local_interp_handle, param_table_handle, coord_system_handle,
     N_interp_points, interp_coords_type, new_interp_coords,
     N_input_arrays, input_array_indices,
     N_output_arrays, output_array_types, output_arrays,
     newfaces);
  
  
  
  /* Free coordinates */
  for (dir=0; dir<3; ++dir) {
    if (do_reflection[2*dir]) {
      free (new_interp_coords[dir]);
    }
  }
  
  
  
  /* Find output variable indices */
  operand_indices = malloc (N_output_arrays * sizeof *operand_indices);
  assert (operand_indices);
  ierr = Util_TableGetIntArray
    (param_table_handle, N_output_arrays, operand_indices, "operand_indices");
  if (ierr == UTIL_ERROR_TABLE_NO_SUCH_KEY) {
    assert (N_output_arrays == N_input_arrays);
    for (m=0; m<N_output_arrays; ++m) {
      operand_indices[m] = m;   /* set output index to input index */
    }
  } else {
    assert (ierr == N_output_arrays);
  }
  
  operation_codes = malloc (N_output_arrays * sizeof *operation_codes);
  assert (operation_codes);
  ierr = Util_TableGetIntArray
    (param_table_handle, N_output_arrays, operation_codes, "operation_codes");
  if (ierr == UTIL_ERROR_TABLE_NO_SUCH_KEY) {
    assert (N_output_arrays == N_input_arrays);
    for (m=0; m<N_output_arrays; ++m) {
      operation_codes[m] = 0;     /* do not take derivatives */
    }
  } else {
    assert (ierr == N_output_arrays);
  }
  
  output_array_indices
    = malloc (N_output_arrays * sizeof *output_array_indices);
  assert (output_array_indices);
  for (m=0; m<N_output_arrays; ++m) {
    assert (operand_indices[m]>=0 && operand_indices[m]<N_input_arrays);
    output_array_indices[m] = input_array_indices[operand_indices[m]];
     assert (output_array_indices[m]==-1
             || (output_array_indices[m]>=0
                 && output_array_indices[m]<CCTK_NumVars()));
  }
  
  
  
  /* Unfold tensor types */
  for (m=0; m<N_output_arrays; ++m) {
    if (output_array_indices[m]!=-1) {
      
      int vi, gi;
      int numvars, firstvar;
      char * groupname;
      
      int table;
      char tensortypealias[1000];
      enum tensortype { UNKNOWN,
                        SCALAR, VECTOR, SYMTENSOR, SYMTENSOR3, TENSOR,
                        WEYLSCALARS_REAL, MANUALCARTESIAN };
      enum tensortype ttype;
      CCTK_INT tensorparity;
      int tcomponent;
      
      int parities[3];
      int check_dir[3];
      int needs_checking;
      
      
      
      vi = output_array_indices[m];
      assert (vi>=0 && vi<CCTK_NumVars());
      gi = CCTK_GroupIndexFromVarI (vi);
      assert (gi>=0 && gi<CCTK_NumGroups());
      numvars = CCTK_NumVarsInGroupI(gi);
      assert (numvars>0);
      firstvar = CCTK_FirstVarIndexI(gi);
      assert (firstvar>=0);
      table = CCTK_GroupTagsTableI(gi);
      assert (table>=0);
  
      
      
      /* Get and check tensor type information */
      ierr = Util_TableGetString
        (table, sizeof tensortypealias, tensortypealias, "tensortypealias");
      if (ierr == UTIL_ERROR_TABLE_NO_SUCH_KEY) {
        groupname = CCTK_GroupName(gi);
        assert (groupname);
        CCTK_VWarn (4, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Tensor type alias not declared for group \"%s\" -- assuming a scalar",
                    groupname);
        free (groupname);
        strcpy (tensortypealias, "scalar");
      } else if (ierr<0) {
        groupname = CCTK_GroupName(gi);
        assert (groupname);
        CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Error in tensor type alias declaration for group \"%s\"",
                    groupname);
        free (groupname);
      }
      
      ttype = UNKNOWN;
      tcomponent = 0;
      if (CCTK_EQUALS (tensortypealias, "scalar")) {
        /* scalar */
        if (numvars != 1) {
          groupname = CCTK_GroupName(gi);
          assert (groupname);
          CCTK_VWarn (4, __LINE__, __FILE__, CCTK_THORNSTRING,
                      "Group \"%s\" has the tensor type alias \"scalar\", but contains more than 1 element",
                      groupname);
          free (groupname);
        }
        ttype = SCALAR;
        tcomponent = 0;
      } else if (CCTK_EQUALS (tensortypealias, "4scalar")) {
        /* 4-scalar */
        assert (numvars == 1);
        ttype = SCALAR;
        tcomponent = 0;
      } else if (CCTK_EQUALS (tensortypealias, "u")
                 || CCTK_EQUALS (tensortypealias, "d"))
      {
        /* vector */
        assert (numvars == 3);
        ttype = VECTOR;
        tcomponent = vi - firstvar;
      } else if (CCTK_EQUALS (tensortypealias, "4u")
                 || CCTK_EQUALS (tensortypealias, "4d"))
      {
        /* 4-vector */
        assert (numvars == 4);
        if (vi == firstvar) {
          ttype = SCALAR;
          tcomponent = 0;
        } else {
          ttype = VECTOR;
          tcomponent = vi - firstvar - 1;
        }
      } else if (CCTK_EQUALS (tensortypealias, "uu_sym")
                 || CCTK_EQUALS (tensortypealias, "dd_sym"))
      {
        /* symmetric tensor */
        assert (numvars == 6);
        ttype = SYMTENSOR;
        tcomponent = vi - firstvar;
      } else if (CCTK_EQUALS (tensortypealias, "uu")
                 || CCTK_EQUALS (tensortypealias, "ud")
                 || CCTK_EQUALS (tensortypealias, "du")
                 || CCTK_EQUALS (tensortypealias, "dd"))
      {
        /* non-symmetric tensor */
        assert (numvars == 9);
        ttype = TENSOR;
        tcomponent = vi - firstvar;
      } else if (CCTK_EQUALS (tensortypealias, "4uu_sym")
                 || CCTK_EQUALS (tensortypealias, "4dd_sym"))
      {
        /* symmetric 4-tensor */
        assert (numvars == 10);
        if (vi == firstvar) {
          ttype = SCALAR;
          tcomponent = 0;
        } else if (vi <= firstvar+3) {
          ttype = VECTOR;
          tcomponent = vi - firstvar - 1;
        } else {
          ttype = SYMTENSOR;
          tcomponent = vi - firstvar - 4;
        }
      } else if (CCTK_EQUALS (tensortypealias, "ddd_sym")) {
        /* 3rd rank tensor, symmetric in last 2 indices */
        assert (numvars == 18);
        ttype = SYMTENSOR3;
        tcomponent = vi - firstvar;
      } else if (CCTK_EQUALS (tensortypealias, "weylscalars_real")) {
        /* Weyl scalars, stored as 10 real numbers */
        assert (numvars == 10);
        ttype = WEYLSCALARS_REAL;
        tcomponent = vi - firstvar;
      } else if (CCTK_EQUALS (tensortypealias, "ManualCartesian")) {
      /* Reflection symmetries specified by hand */
      ttype = MANUALCARTESIAN;
      tcomponent = vi - firstvar;
      } else {
        groupname = CCTK_GroupName(gi);
        assert (groupname);
        CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Illegal tensor type alias \"%s\" for group \"%s\"", 
                    tensortypealias, groupname);
        free (groupname);
      }
      
      switch (ttype) {
      case SCALAR:
        assert (tcomponent>=0 && tcomponent<1);
        break;
      case VECTOR:
        assert (tcomponent>=0 && tcomponent<3);
        break;
      case SYMTENSOR:
        assert (tcomponent>=0 && tcomponent<6);
        break;
      case SYMTENSOR3:
        assert (tcomponent>=0 && tcomponent<18);
        break;
      case TENSOR:
        assert (tcomponent>=0 && tcomponent<9);
        break;
      case WEYLSCALARS_REAL:
        assert (tcomponent>=0 && tcomponent<10);
        break;
      case MANUALCARTESIAN:
        /* No restriction on number of components */
        break;
      default:
        assert (0);
      }
      
      ierr = Util_TableGetInt (table, & tensorparity, "tensorparity");
      if (ierr == UTIL_ERROR_TABLE_NO_SUCH_KEY) {
        tensorparity = +1;
      } else if (ierr<0) {
        groupname = CCTK_GroupName(gi);
        assert (groupname);
        CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Error in tensor parity declaration for group \"%s\"",
                    groupname);
        free (groupname);
      }
      
      
      
      /* Calculate parities */
      parities[0] = parities[1] = parities[2] = +1;
      switch (ttype) {
      case SCALAR:
        /* do nothing */
        break;
      case VECTOR:
        parities[tcomponent] = -1;
        break;
      case SYMTENSOR:
        switch (tcomponent) {
        case 0: break;
        case 1: parities[0] = parities[1] = -1; break;
        case 2: parities[0] = parities[2] = -1; break;
        case 3: break;
        case 4: parities[1] = parities[2] = -1; break;
        case 5: break;
        default: assert (0);
        }
        break;
      case SYMTENSOR3:
        switch (tcomponent % 6) {
        case 0: break;
        case 1: parities[0] = parities[1] = -1; break;
        case 2: parities[0] = parities[2] = -1; break;
        case 3: break;
        case 4: parities[1] = parities[2] = -1; break;
        case 5: break;
        default: assert (0);
        }
        switch (tcomponent / 6) {
        case 0: parities[0] *= -1; break;
        case 1: parities[1] *= -1; break;
        case 2: parities[2] *= -1; break;
        default: assert (0);
        }
        break;
      case TENSOR:
        switch (tcomponent) {
        case 0: break;
        case 1: parities[0] = parities[1] = -1; break;
        case 2: parities[0] = parities[2] = -1; break;
        case 3: parities[1] = parities[0] = -1; break;
        case 4: break;
        case 5: parities[1] = parities[2] = -1; break;
        case 6: parities[2] = parities[0] = -1; break;
        case 7: parities[2] = parities[1] = -1; break;
        case 8: break;
        default: assert (0);
        }
        break;
      case WEYLSCALARS_REAL: {
        static int const weylparities[10][3] =
          {{+1,+1,+1},
           {-1,-1,-1},
           {+1,+1,+1},
           {-1,-1,-1},
           {+1,+1,+1},
           {-1,-1,-1},
           {+1,+1,+1},
           {-1,-1,-1},
           {+1,+1,+1},
           {-1,-1,-1}};
        for (dir=0; dir<3; ++dir) {
          parities[dir] = weylparities[tcomponent][dir];
        }
        break;
      }
      case MANUALCARTESIAN:
        ReflectionSymmetry_GetManualParities(table, gi, parities);
        break;
      default:
        assert (0);
      }
      
      
      
      /* Take derivatives into account */
      {
        int code = operation_codes[m];
        while (code) {
          d = code % 10 - 1;
          code /= 10;
          assert (d>=0 && d<3);
          parities[d] *= -1;
        }
      }
      
      
      
      /* Are there negative parities? */
      needs_checking = 0;
      for (dir=0; dir<3; ++dir) {
        check_dir[dir] = do_reflection[2*dir] && parities[dir] < 0;
        needs_checking |= check_dir[dir];
      }
      
      
      
      /* Loop over all points and unfold */
      if (needs_checking) {
        for (n=0; n<N_interp_points; ++n) {
          int parity = tensorparity;
          /* Is the point outside the domain? */
          for (dir=0; dir<3; ++dir) {
            if (check_dir[dir]) {
              CCTK_REAL const pos = ((CCTK_REAL const *)interp_coords[dir])[n];
              if (pos < 0) {
                /* Reflect the tensor component */
                parity *= -1;
              }
            }
          }
          ((CCTK_REAL *)output_arrays[m])[n] *= parity;
        }
      }
      
    }
    } /* for m */
  
  
  
  /* Free output variable indices */
  free (operand_indices);
  free (operation_codes);
  free (output_array_indices);
  
  
  
  /* Return */
  return iret;
}

void  ReflectionSymmetry_GetManualParities(int table, int gi, int *parities)
{
  char cartsyms[100];
  char *groupname = NULL;
  int i = 0;
  int ierr = -1;

  /* Get and check tensor type information */
  ierr = Util_TableGetString
    (table, sizeof cartsyms, cartsyms, "cartesianreflectionparities");
  if (ierr == UTIL_ERROR_TABLE_NO_SUCH_KEY) {
    groupname = CCTK_GroupName(gi);
    assert (groupname);
    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
		"Cartesian refection parity not declared for group \"%s\" -- aborting",
		groupname);
    assert(0);
  } else if (ierr<0) {
    groupname = CCTK_GroupName(gi);
    assert (groupname);
    CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
		"Error in tensor type alias declaration for group \"%s\"",
		groupname);
    free (groupname);
  }

  if (strlen(cartsyms) != 3)
  {
    CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
		"Invalid format for cartesianreflectionparities: must be xxx where x is + or - for group %s",
		groupname);
  }

  for (i = 0; i < 3; i++)
  {
    switch (cartsyms[i])
    {
    case '+':
      parities[i] = 1;
      break;
    case '-':
      parities[i] = -1;
      break;
    default:
      CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
		  "Invalid format for cartesianreflectionparities: must be xxx where x is + or - for group %s",
		  groupname);
    }
  }
}