aboutsummaryrefslogtreecommitdiff
path: root/archive/2d.cube.order2.smooth0.c
blob: 34e17ca0b8f8302b21f998da3fad0b28cff4f9c8 (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
527
528
529
530
531
532
533
534
 /*@@
   @file      2d.cube.order2.smooth0.c
   @date      23 Oct 2001
   @author    Jonathan Thornburg <jthorn@aei.mpg.de>
   @desc
	Generalized interpolation for 2d, hypercube-shaped molecules,
	order=2, smoothing=0.  For details, see the header comments
	for "InterpLocalArrays.c" in this directory.
   @enddesc

   @version   $Id$
 @@*/

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

#include "util_ErrorCodes.h"
#include "cctk.h"
#include "InterpLocalArrays.h"

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

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

/*@@
   @routine    LocalInterp_ILA_2d_cube_ord2_sm0
   @date       23 Oct 2001
   @author     Jonathan Thornburg <jthorn@aei.mpg.de>
   @desc
	This function does generalized interpolation of one or more
	2d arrays to arbitrary points.  For details, see the header
	comments for InterpLocalArrays() (in "InterpLocalArrays.c"
	in this same directory).

	This function's arguments are all a subset of those of
	InterpLocalArrays() ; the only difference is that this function
	takes all its arguments explicitly, whereas  InputLocalArrays()
	takes some of them indirectly via a key/value parameter table.

  @returntype   int
  @returndesc	This function's return result is the same as that of
		InterpLocalArrays():
  		 0 ==> successful interpolation
                -1 ==> in case of any errors
  @endreturndesc

  @@*/
int LocalInterp_ILA_2d_cube_ord2_sm0
	(int param_table_handle,
	 const CCTK_REAL coord_system_origin[],
	 const CCTK_REAL grid_spacing[],
	 int N_interp_points,
	 const CCTK_INT interp_coord_type_codes[],
	 const void *const interp_coords[],
	 int N_input_arrays,
	 const CCTK_INT input_array_offsets[],
	 const CCTK_INT input_array_strides[],
	 const CCTK_INT input_array_min_subscripts[],
	 const CCTK_INT input_array_max_subscripts[],
	 const CCTK_INT input_array_type_codes[],
	 const void *const input_arrays[],
	 int N_output_arrays,
	 const CCTK_INT output_array_type_codes[],
	 void *const output_arrays[],
	 const CCTK_INT operand_indices[], const CCTK_INT opcodes[])
{
/*
 * Implementation notes:
 * 
 * The basic outline of this function is as follows:
 *
 *	compute "which derivatives are wanted" flags
 *		for each interpolation point
 *		{
 *		declare all the coefficients
 *		declare all the data-values variables
 *		***fetch*** interpolation point coordinates
 *		compute coefficients for all derivatives which are wanted
 *			for each output array
 *			{
 *			int part;
 *				for (part = 0 ; part <= 1 ; ++part)
 *				{
 *				if (this output array is computed
 *				    using a different input array
 *				    than the previous one || part != 0)
 *				   then ***fetch*** the input array values
 *					            into local variables
 *				  {
 *				fp result;
 *				switch	(opcode)
 *					{
 *				case 0:
 *					result = evaluate the interpolant
 *					break;
 *				case 1:
 *					result = evaluate the interpolant
 *					break;
 *				case ...
 *					}
 *				***store*** result in output array
 *				bool complex_flag = is datatype complex?
 *				if (! complex_flag)
 *				   then break;
 *				  }
 *				}
 *			}
 *		}
 *
 * Here "***fetch***" and "***store***" are all actually switches on
 * the various array datatypes.  For complex datatypes they offset the
 * 1D array position by  part  to handle real/imaginary components of
 * the complex values.
 *
 * At present we do all floating-point computations in type "fp"
 * (typically a typedef for CCTK_REAL), so arrays of higher precision
 * than this will incur extra rounding errors.  In practice these should
 * be negligible compared to the "truncation" interpolation errors.
 */

/*
 * Naming conventions:
 * input, output = 0-origin indices each selecting an input/output array
 * point = 0-origin index selecting an interpolation point
 */

/*
 * these are compile-time constants here; InterpLocalArrays() decoded
 * them and called us (as opposed to another function) based in part
 * on these values
 */
#define N_DIMS		2
#define MOLECULE_SIZE	3

/* layout of axes in N_dims-element arrays */
#define X_AXIS	0
#define Y_AXIS	1

/* input array size, strides, and subscripting computation */
const int stride_i = input_array_strides[X_AXIS];
const int stride_j = input_array_strides[Y_AXIS];
#define SUB2(i,j)	(i*stride_i + j*stride_j)

/* macros used by machine-generated interpolation coefficient expressions */
#define RATIONAL(num,den)	(num/den)

/*
 * compute flags specifying which derivatives are wanted
 */
bool want_I = false;
bool want_dx = false, want_dy = false;
  {
int output;
	for (output = 0 ; output < N_output_arrays ; ++output)
	{
	switch	(opcodes[output])
		{
	case 0:		want_I = true;		break;
	case 1:		want_dx = true;		break;
	case 2:		want_dy = true;		break;
	default:
		CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
			   "Generalized interpolation opcode %d not supported",
			   opcodes[output]);			/*NOTREACHED*/
		return UTIL_ERROR_BAD_INPUT;		/*** ERROR RETURN ***/
		}
	}
  }

/*
 * interpolate at each point
 */
  {
int point;
	for (point = 0 ; point < N_interp_points ; ++point)
	{
	/* declare all the interpolation coefficients */
	#include "coeffs/2d.cube.order2.smooth0.I.dcl.c"
	#include "coeffs/2d.cube.order2.smooth0.dx.dcl.c"
	#include "coeffs/2d.cube.order2.smooth0.dy.dcl.c"

	/* declare all the data-values variables */
	#include "coeffs/2d.cube.size3.data-var.dcl.c"

	/*
	 * ***fetch*** interpolation point coordinates
	 */
	fp interp_coords_fp[N_DIMS];
	int axis;
		for (axis = 0 ; axis < N_DIMS ; ++axis)
		{
		/* pointer to array of interp coords for this axis */
		const void *const interp_coords_ptr = interp_coords[axis];

		switch	(interp_coord_type_codes[axis])
			{
#ifdef CCTK_REAL
		case CCTK_VARIABLE_REAL:
			  {
			const CCTK_REAL *const interp_coords_ptr_real
				= (const CCTK_REAL *) interp_coords_ptr;
			interp_coords_fp[axis] = interp_coords_ptr_real[point];
			break;
			  }
#endif
#ifdef CCTK_REAL4
		case CCTK_VARIABLE_REAL4:
			  {
			const CCTK_REAL4 *const interp_coords_ptr_real4
				= (const CCTK_REAL4 *) interp_coords_ptr;
			interp_coords_fp[axis] = interp_coords_ptr_real4[point];
			break;
			  }
#endif
#ifdef CCTK_REAL8
		case CCTK_VARIABLE_REAL8:
			  {
			const CCTK_REAL8 *const interp_coords_ptr_real8
				= (const CCTK_REAL8 *) interp_coords_ptr;
			interp_coords_fp[axis] = interp_coords_ptr_real8[point];
			break;
			  }
#endif
		default:
			CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
				   "interp-coords datatype %d not supported",
				   interp_coord_type_codes[axis]);
								/*NOTREACHED*/
			return UTIL_ERROR_BAD_INPUT;	/*** ERROR RETURN ***/
			}
		}

	/*
	 * locate interpolation molecules with respect to the grid,
	 * i.e. compute interp_rel_(x,y)
	 */
	  {
	fp interp_rel_x, interp_rel_y;	/* (x,y) coordinates of interpolation */
					/* point relative to molecule center */
					/* (in units of the grid spacing) */
	const int center_i
		= LocalInterp_molecule_posn(coord_system_origin[X_AXIS],
					   grid_spacing[X_AXIS],
					   input_array_min_subscripts[X_AXIS],
					   input_array_max_subscripts[X_AXIS],
					   MOLECULE_SIZE,
					   interp_coords_fp[X_AXIS],
					   &interp_rel_x,
					   (int *) NULL, (int *) NULL);
	const int center_j
		= LocalInterp_molecule_posn(coord_system_origin[Y_AXIS],
					   grid_spacing[Y_AXIS],
					   input_array_min_subscripts[Y_AXIS],
					   input_array_max_subscripts[Y_AXIS],
					   MOLECULE_SIZE,
					   interp_coords_fp[Y_AXIS],
					   &interp_rel_y,
					   (int *) NULL, (int *) NULL);
	const int center_sub = SUB2(center_i, center_j);

	/*
	 * compute the coefficients for whichever derivatives are wanted
	 * using machine-generated coefficient expressions
	 * ... these expressions are polynomials in (x,y)
	 *     ==> we need these names for the relative coordinates
	 *	   (copying to fresh local variables will likely also
	 *	    give better register allocation, since [xy]_rel
	 *	    had their addresses taken and so probably won't be
	 *	    register-allocated)
	 */
	const fp x = interp_rel_x;
	const fp y = interp_rel_y;
	if (want_I)
	   then {
		#include "coeffs/2d.cube.order2.smooth0.I.coeff.c"
		}
	if (want_dx)
	   then {
		#include "coeffs/2d.cube.order2.smooth0.dx.coeff.c"
		}
	if (want_dy)
	   then {
		#include "coeffs/2d.cube.order2.smooth0.dy.coeff.c"
		}

	/*
	 * compute each output array at this point
	 */
	  {
	int output;
	const void *input_array_ptr = NULL;
		for (output = 0 ; output < N_output_arrays ; ++output)
		{
		const int input = operand_indices[output];
		const int input_offset = input_array_offsets[input];

		/*
		 * for each real/imag part of complex data values
		 * ... for real we'll break out of this loop at the bottom
		 *     after only a single iteration;
		 *     for complex we'll do both iterations
		 */
		int part;
			for (part = 0 ; part <= 1 ; ++part)
			{
			if ( (input_arrays[input] != input_array_ptr)
			     || (part != 0) )
			   then {
				/*
				 * ***fetch*** the input array values
				 *             into local variables
				 */
				input_array_ptr = input_arrays[input];
				switch	(input_array_type_codes[input])
					{
#ifdef CCTK_REAL
case CCTK_VARIABLE_REAL:
	  {
	const CCTK_REAL *const input_array_ptr_real
		= (const CCTK_REAL *) input_array_ptr;
	#undef DATA
	#define DATA(i,j)	\
		input_array_ptr_real[input_offset + center_sub + SUB2(i,j)]
	#include "coeffs/2d.cube.size3.data-var.assign.c"
	break;
	  }
#endif
#ifdef CCTK_REAL4
case CCTK_VARIABLE_REAL4:
	  {
	const CCTK_REAL4 *const input_array_ptr_real4
		= (const CCTK_REAL4 *) input_array_ptr;
	#undef DATA
	#define DATA(i,j)	\
		input_array_ptr_real4[input_offset + center_sub + SUB2(i,j)]
	#include "coeffs/2d.cube.size3.data-var.assign.c"
	break;
	  }
#endif
#ifdef CCTK_REAL8
case CCTK_VARIABLE_REAL8:
	  {
	const CCTK_REAL8 *const input_array_ptr_real8
		= (const CCTK_REAL8 *) input_array_ptr;
	#undef DATA
	#define DATA(i,j)	\
		input_array_ptr_real8[input_offset + center_sub + SUB2(i,j)]
	#include "coeffs/2d.cube.size3.data-var.assign.c"
	break;
	  }
#endif
#ifdef CCTK_COMPLEX
case CCTK_VARIABLE_COMPLEX:
	  {
	const CCTK_COMPLEX *const input_array_ptr_complex
		= (const CCTK_COMPLEX *) input_array_ptr;
	#undef DATA
	#define DATA(i,j)	\
   ( (const CCTK_REAL *)	\
     & input_array_ptr_complex[input_offset + center_sub + SUB2(i,j)] ) [part]
	#include "coeffs/2d.cube.size3.data-var.assign.c"
	break;
	  }
#endif
#ifdef CCTK_COMPLEX8
case CCTK_VARIABLE_COMPLEX8:
	  {
	const CCTK_COMPLEX8 *const input_array_ptr_complex8
		= (const CCTK_COMPLEX8 *) input_array_ptr;
	#undef DATA
	#define DATA(i,j)	\
   ( (const CCTK_REAL4 *)	\
     & input_array_ptr_complex8[input_offset + center_sub + SUB2(i,j)] ) [part]
	#include "coeffs/2d.cube.size3.data-var.assign.c"
	break;
	  }
#endif
#ifdef CCTK_COMPLEX16
case CCTK_VARIABLE_COMPLEX16:
	  {
	const CCTK_COMPLEX16 *const input_array_ptr_complex16
		= (const CCTK_COMPLEX16 *) input_array_ptr;
	#undef DATA
	#define DATA(i,j)	\
   ( (const CCTK_REAL8 *)	\
     & input_array_ptr_complex16[input_offset + center_sub + SUB2(i,j)] ) [part]
	#include "coeffs/2d.cube.size3.data-var.assign.c"
	break;
	  }
#endif
default:
	CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
		   "input datatype %d not supported",
		   input_array_type_codes[input]);		/*NOTREACHED*/
	return UTIL_ERROR_BAD_INPUT;			/*** ERROR RETURN ***/
					}
				}

			/*
			 * evaluate the interpolant
			 * as a linear combination of the data variables
			 */
			  {
			fp result;
			switch	(opcodes[output])
				{
			case 0:
				result =
	#include "coeffs/2d.cube.order2.smooth0.I.eval.c"
				break;
			case 1:
				result =
	#include "coeffs/2d.cube.order2.smooth0.dx.eval.c"
				break;
			case 2:
				result =
	#include "coeffs/2d.cube.order2.smooth0.dy.eval.c"
				break;
			default:
				CCTK_VWarn(1, __LINE__, __FILE__,
					   CCTK_THORNSTRING,
					   "opcode %d not supported",
					   opcodes[output]);	/*NOTREACHED*/
				return UTIL_ERROR_BAD_INPUT;
							/*** ERROR RETURN ***/
				}

			/*
			 * ***store*** the result in the output array
			 */
			switch	(output_array_type_codes[output])
				{
case CCTK_VARIABLE_REAL:
	  {
	CCTK_REAL *const output_array_ptr_real
		= (CCTK_REAL *) output_arrays[output];
	output_array_ptr_real[point] = result;
	break;
	  }
case CCTK_VARIABLE_REAL4:
	  {
	CCTK_REAL4 *const output_array_ptr_real4
		= (CCTK_REAL4 *) output_arrays[output];
	output_array_ptr_real4[point] = result;
	break;
	  }
case CCTK_VARIABLE_REAL8:
	  {
	CCTK_REAL8 *const output_array_ptr_real8
		= (CCTK_REAL8 *) output_arrays[output];
	output_array_ptr_real8[point] = result;
	break;
	  }
case CCTK_VARIABLE_COMPLEX:
	  {
	CCTK_COMPLEX *const output_array_ptr_complex
		= (CCTK_COMPLEX *) output_arrays[output];
	((CCTK_REAL *)  & output_array_ptr_complex[point]) [part]
		= result;
	break;
	  }
case CCTK_VARIABLE_COMPLEX8:
	  {
	CCTK_COMPLEX8 *const output_array_ptr_complex8
		= (CCTK_COMPLEX8 *) output_arrays[output];
	((CCTK_REAL4 *)  & output_array_ptr_complex8[point]) [part]
		= result;
	break;
	  }
case CCTK_VARIABLE_COMPLEX16:
	  {
	CCTK_COMPLEX16 *const output_array_ptr_complex16
		= (CCTK_COMPLEX16 *) output_arrays[output];
	((CCTK_REAL8 *)  & output_array_ptr_complex16[point]) [part]
		= result;
	break;
	  }
default:
	CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
		   "output datatype %d not supported",
		   output_array_type_codes[output]);		/*NOTREACHED*/
	return UTIL_ERROR_BAD_INPUT;			/*** ERROR RETURN ***/
				}

			/* decode datatype: is it real or complex? */
			  {
			bool complex_flag;
			switch	(output_array_type_codes[output])
				{
			case CCTK_VARIABLE_REAL:
			case CCTK_VARIABLE_REAL4:
			case CCTK_VARIABLE_REAL8:
				complex_flag = false;
				break;
			case CCTK_VARIABLE_COMPLEX:
			case CCTK_VARIABLE_COMPLEX8:
			case CCTK_VARIABLE_COMPLEX16:
				complex_flag = true;
				break;
			default:
				CCTK_VWarn(1, __LINE__, __FILE__,
					   CCTK_THORNSTRING,
					   "output datatype %d not supported",
					   output_array_type_codes[output]);
								/*NOTREACHED*/
				return UTIL_ERROR_BAD_INPUT;
							/*** ERROR RETURN ***/
				}
			
			/* skip part=1 (imaginary part) for real datatypes */
			if (! complex_flag)
			   then break;
			  }
			  }
			/* end of  for (part = ...)  loop */
			}
		/* end of  for (output = ...)  loop */
		}
	  }
	  }

	/* end of  for (point = ...)  loop */
	}

return 0;						/*** NORMAL RETURN ***/
  }
}