aboutsummaryrefslogtreecommitdiff
path: root/src/Operator.c
blob: 5d17ab822827164234b70c048fecf4be102677ca (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
/*@@
  @file      Operator.c
  @date      Tue Apr 15 18:22:45 1997
  @author    Paul Walker
  @desc 
             Definition of interpolation operators for regular uniform grids.
  @enddesc 

  @history
  @date      Sun Jul 04 1999
  @author    Thomas Radke
  @hdesc     conversion to Cactus 4.0 (copied from pughGetPoints.c)
  @date      Wed 31 Jan 2001
  @author    Thomas Radke
  @hdesc     translation of fortran interpolators into C
  @date      22 Jan 2002
  @author    Jonathan Thornburg
  @hdesc     Move all local-interpolation code from LocalInterp to here
  @endhistory

  @version   $Header$
  @@*/

#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "cctk.h"
#include "cctk_Parameters.h"
#include "CactusPUGH/PUGH/src/include/pugh.h"
#include "Interpolate.h"

/* the rcs ID and its dummy function to use it */
static const char *rcsid = "$Header$";
CCTK_FILEVERSION(CactusBase_LocalInterp_Operator_c)

/* uncomment this to get some debugging output */
/* #define PUGHINTERP_DEBUG 1 */



/* prototypes of routines defined in this source file */
static int LocalInterp_CheckArguments (cGH *GH,
                                      int num_dims,
                                      int num_points,
                                      int num_in_arrays,
                                      int num_out_arrays,
                                      const int interp_coord_array_types[]);

/******************************************************************************/

/*@@
  @routine    LocalInterp_InterpLocal
  @date       Sun Jul 04 1999
  @author     Thomas Radke
  @desc
              The interpolation operator registered with the CCTK
              under the name "uniform cartesian".

              Interpolates a list of non-distributed, processor-local
              input arrays to a list of output arrays (one-to-one)
              at a given number of interpolation points (indicated by
              their coordinates). The points are located on a coordinate
              system which is assumed to be a uniform cartesian.
  @enddesc

  @var        GH
  @vdesc      Pointer to CCTK grid hierarchy
  @vtype      cGH *
  @vio        in
  @endvar
  @var        order
  @vdesc      interpolation order
  @vtype      int
  @vio        in
  @endvar
  @var        num_points
  @vdesc      number of points to be interpolated on this processor
  @vtype      int
  @vio        in
  @endvar
  @var        num_dims
  @vdesc      dimensionality of the underlying grid
  @vtype      int
  @vio        in
  @endvar
  @var        num_in_arrays
  @vdesc      number of input arrays to interpolate from
  @vtype      int
  @vio        in
  @endvar
  @var        num_out_arrays
  @vdesc      number of output arrays to interpolate to
  @vtype      int
  @vio        in
  @endvar
  @var        coord_dims
  @vdesc      dimensions of the underlying grid
  @vtype      int [num_dims]
  @vio        in
  @endvar
  @var        coord_arrays
  @vdesc      list of grid coordinate arrays
  @vtype      void *const [num_dims]
  @vio        in
  @endvar
  @var        coord_array_types
  @vdesc      CCTK data type of coordinate arrays
  @vtype      int [num_dims]
  @vio        int
  @endvar
  @var        interp_coord_arrays
  @vdesc      coordinates of points to interpolate at
  @vtype      void *const [num_dims]
  @vio        in
  @endvar
  @var        interp_coord_array_types
  @vdesc      CCTK data type of coordinate arrays to interpolate at
  @vtype      int [num_dims]
  @vio        out
  @endvar
  @var        in_arrays
  @vdesc      list of input arrays to interpolate on
  @vtype      void *const [num_in_arrays]
  @vio        in
  @endvar
  @var        in_array_types
  @vdesc      CCTK data types of input arrays
  @vtype      int [num_in_arrays]
  @vio        in
  @endvar
  @var        out_arrays
  @vdesc      list of output arrays to interpolate to
  @vtype      void *const [num_out_arrays]
  @vio        out
  @endvar
  @var        out_array_types
  @vdesc      CCTK data types of output arrays
  @vtype      int [num_out_arrays]
  @vio        in
  @endvar

  @returntype int
  @returndesc
              0  - successful interpolation
              -1 - in case of any errors
  @endreturndesc
  @@*/
int LocalInterp_InterpLocal (cGH *GH,
                            int order,
                            int num_points,
                            int num_dims,
                            int num_in_arrays,
                            int num_out_arrays,
                            const int coord_dims[],
                            const void *const coord_arrays[],
                            const int coord_array_types[],
                            const void *const interp_coord_arrays[],
                            const int interp_coord_array_types[],
                            const void *const in_arrays[],
                            const int in_array_types[],
                            void *const out_arrays[],
                            const int out_array_types[])
{
  int dim, point, retval;
  CCTK_REAL *coords, *origin, *delta;
  const CCTK_REAL *const *data;

  /* check arguments */
  retval = LocalInterp_CheckArguments (GH, num_dims, num_points,
                                      num_in_arrays, num_out_arrays,
                                      interp_coord_array_types);
  if (retval <= 0)
  {
    return (retval);
  }
  for (dim = 0; dim < num_dims; dim++)
  {
    if (coord_array_types[dim] != CCTK_VARIABLE_REAL)
    {
      CCTK_WARN (1, "Coordinates should be of type CCTK_REAL");
      return (-1);
    }
    if (interp_coord_array_types[dim] != CCTK_VARIABLE_REAL)
    {
      CCTK_WARN (1, "Interpolation coordinates should be of type CCTK_REAL");
      return (-1);
    }
  }

  /* get the grid spacings - this assumes a cartesian grid */
  origin = (CCTK_REAL *) malloc (2 * num_dims * sizeof (CCTK_REAL));
  delta  = origin + num_dims;
  data   = (const CCTK_REAL *const *) coord_arrays;
  for (dim = 0; dim < num_dims; dim++)
  {
    origin[dim] = data[dim][0];
    delta[dim]  = data[dim][1] - data[dim][0];
  }

  /* sort the individual interpolation coordinate arrays into a single one */
  coords = (CCTK_REAL *) malloc (num_dims * num_points * sizeof (CCTK_REAL));
  data   = (const CCTK_REAL *const *) interp_coord_arrays;
  for (point = 0; point < num_points; point++)
  {
    for (dim = 0; dim < num_dims; dim++)
    {
      *coords++ = data[dim][point];
    }
  }
  coords -= num_dims * num_points;

  /* call the interpolator function */
  retval = LocalInterp_Interpolate (order,
                                   num_points, num_dims, num_out_arrays,
                                   coord_dims, coords, origin, delta,
                                   in_array_types, in_arrays,
                                   out_array_types, out_arrays);

  /* free allocated resources */
  free (coords);
  free (origin);

  return (retval);
}

/**************************************************************************/
/*                            local routines                              */
/**************************************************************************/

/*@@
  @routine    LocalInterp_CheckArguments
  @date       Thu 25 Jan 2001
  @author     Thomas Radke
  @desc
              Checks the interpolation arguments passed in via
              the flesh's general interpolation calling interface

              This routine also verifies that the parameters meet
              the limitations of LocalInterp's interpolation operators.
  @enddesc

  @var        GH
  @vdesc      Pointer to CCTK grid hierarchy
  @vtype      cGH *
  @vio        in
  @endvar
  @var        num_dims
  @vdesc      dimensionality of the underlying grid
  @vtype      int
  @vio        in
  @endvar
  @var        num_points
  @vdesc      number of points to interpolate at
  @vtype      int
  @vio        in
  @endvar
  @var        num_in_arrays
  @vdesc      number of passed input arrays
  @vtype      int
  @vio        in
  @endvar
  @var        num_out_arrays
  @vdesc      number of passed input arrays
  @vtype      int
  @vio        in
  @endvar
  @var        interp_coord_array_types
  @vdesc      types of passed coordinates to interpolate at
  @vtype      int [num_dims]
  @vio        in
  @endvar

  @returntype int
  @returndesc
              +1 for success
               0 for success but nothing to do
              -1 for failure (wrong parameters passed or limitations not met)
  @endreturndesc
  @@*/
static int LocalInterp_CheckArguments (cGH *GH,
                                      int num_dims,
                                      int num_points,
                                      int num_in_arrays,
                                      int num_out_arrays,
                                      const int interp_coord_array_types[])
{
  int i;


  /* check for invalid arguments */
  if (num_dims < 0 || num_points < 0 || num_in_arrays < 0 || num_out_arrays < 0)
  {
    return (-1);
  }

  /* check if there's anything to do at all */
  /* NOTE: num_points can be 0 in a collective call */
  if (num_dims == 0 || num_in_arrays == 0 || num_out_arrays == 0)
  {
    return (0);
  }

  /* for now we can only deal with coordinates of type CCTK_REAL */
  for (i = 0; i < num_dims; i++)
  {
    if (interp_coord_array_types[i] != CCTK_VARIABLE_REAL)
    {
      CCTK_WARN (1, "Interpolation coordinates must be of type CCTK_REAL");
      return (-1);
    }
  }

  /* LocalInterp's interpolation operators compute one output array
     per input array */
  if (num_in_arrays != num_out_arrays)
  {
    CCTK_WARN (1, "Number of input arrays must match number of output arrays");
    return (-1);
  }

  return (1);
}