aboutsummaryrefslogtreecommitdiff
path: root/src/Startup.c
blob: c8277f6d4e729d822d9ae0fbc89fbb9a3b7ca29a (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
/*@@
  @file      Startup.c
  @date      Sun Jul 04 1999
  @author    Thomas Radke
  @desc
             Startup routines for LocalInterp/UniformCartesian
  @enddesc

  @history
  @date      22 Jan 2002
  @author    Jonathan Thornburg
  @hdesc     Move all local-interpolation code from LocalInterp to here
  @endhistory

  @version   $Header$
  @@*/

#include <stdlib.h>

#include "cctk.h"
#include "cctk_Interp.h"
#include "util_ErrorCodes.h"
#include "util_Table.h"
#include "Interpolate.h"

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


/********************************************************************
 ***************    External Routine Prototypes   *******************
 ********************************************************************/
void LocalInterp_Startup(void);

/********************************************************************
 ***************    Internal Routine Prototypes   *******************
 ********************************************************************/
static int InterpLocal_1stOrder (cGH *GH,
                                 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[]);
static int InterpLocal_2ndOrder (cGH *GH,
                                 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[]);
static int InterpLocal_3rdOrder (cGH *GH,
                                 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[]);
static int InterpLocal_NthOrder (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[]);


/********************************************************************
 ********************    External Routines   ************************
 ********************************************************************/

/*@@
  @routine   LocalInterp_Startup
  @date      Sun Jul 04 1999
  @author    Thomas Radke
  @desc
             The startup registration routine for LocalInterp.
             Registers the interpolation operators with the flesh.
  @enddesc
  @calls     CCTK_InterpRegisterOperatorLocal
  @@*/
void LocalInterp_Startup (void)
{
  CCTK_InterpRegisterOpLocalUniform (LocalInterp_InterpLocalUniform,
                                     "uniform cartesian", CCTK_THORNSTRING);

  CCTK_InterpRegisterOperatorLocal (InterpLocal_1stOrder,
                                    "first-order uniform cartesian");
  CCTK_InterpRegisterOperatorLocal (InterpLocal_2ndOrder,
                                    "second-order uniform cartesian");
  CCTK_InterpRegisterOperatorLocal (InterpLocal_3rdOrder,
                                    "third-order uniform cartesian");
}


/********************************************************************
 ********************    Internal Routines   ************************
 ********************************************************************/
/*@@
  @routine   InterpLocal_NthOrder
  @date      Wed 14 Feb 2001
  @author    Thomas Radke
  @desc
             LocalInterp's first/second/third order interpolation operators
             which are registered with the flesh's old interpolation API.
             These routines are just wrappers which translate their arguments
             to call LocalInterp's interpolation operator for the new API.
  @enddesc

  @returntype int
  @returndesc
              the return code of LocalInterp's interpolation operator
  @endreturndesc
 @@*/
static int InterpLocal_1stOrder (cGH *GH,
                                 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[])
{
  return (InterpLocal_NthOrder (GH, 1, num_points, num_dims,
                                num_in_arrays, num_out_arrays,
                                coord_dims, coord_arrays, coord_array_types,
                                interp_coord_arrays, interp_coord_array_types,
                                in_arrays, in_array_types,
                                out_arrays, out_array_types));
}


static int InterpLocal_2ndOrder (cGH *GH,
                                 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[])
{
  return (InterpLocal_NthOrder (GH, 2, num_points, num_dims,
                                num_in_arrays, num_out_arrays,
                                coord_dims, coord_arrays, coord_array_types,
                                interp_coord_arrays, interp_coord_array_types,
                                in_arrays, in_array_types,
                                out_arrays, out_array_types));
}


static int InterpLocal_3rdOrder (cGH *GH,
                                 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[])
{
  return (InterpLocal_NthOrder (GH, 3, num_points, num_dims,
                                num_in_arrays, num_out_arrays,
                                coord_dims, coord_arrays, coord_array_types,
                                interp_coord_arrays, interp_coord_array_types,
                                in_arrays, in_array_types,
                                out_arrays, out_array_types));
}


static int InterpLocal_NthOrder (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 i, table, retval;
  CCTK_INT *_coord_dims, *_in_array_types, *_out_array_types;
  CCTK_REAL *origin, *delta;
  char string[32];


  /* no information needed from the GH */
  (void *) (GH + 0);

  /* need to turn int's into CCTK_INT's */
  _coord_dims = malloc ((num_dims + num_in_arrays + num_out_arrays) *
                        sizeof (CCTK_INT));
  _in_array_types = _coord_dims + num_dims;
  _out_array_types = _in_array_types + num_in_arrays;
  for (i = 0; i < num_in_arrays; i++)
  {
    _in_array_types[i] = in_array_types[i];
  }
  for (i = 0; i < num_out_arrays; i++)
  {
    _out_array_types[i] = out_array_types[i];
  }

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

    _coord_dims[i] = coord_dims[i];
  }

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

  /* create a table with the interpolation order information */
  sprintf (string, "order = %d", order);
  table = Util_TableCreateFromString (string);

  /* call the interpolator function */
  retval = LocalInterp_InterpLocalUniform (num_dims, table, origin, delta,
                                           num_points, CCTK_VARIABLE_REAL,
                                           interp_coord_arrays, num_in_arrays,
                                           _coord_dims, _in_array_types,
                                           in_arrays, num_out_arrays,
                                           _out_array_types, out_arrays);

  /* free allocated resources */
  Util_TableDestroy (table);
  free (origin);
  free (_coord_dims);

  return (retval);
}