Task: Implement make.code.defn for CactusNumerical/InterpToArray thorn

thorn_name: CactusNumerical/InterpToArray
src_filename: make.code.defn
interface: # Interface definition for thorn InterpToArray

IMPLEMENTS: InterpToArray

CCTK_INT FUNCTION                                      \
    InterpGridArrays                                   \
        (CCTK_POINTER_TO_CONST IN cctkGH,              \
         CCTK_INT              IN N_dims,              \
         CCTK_INT              IN order,               \
         CCTK_INT              IN N_interp_points,     \
         CCTK_POINTER_TO_CONST IN interp_coords,       \
         CCTK_INT              IN N_input_arrays,      \
         CCTK_INT        ARRAY IN input_array_indices, \
         CCTK_INT              IN N_output_arrays,     \
         CCTK_POINTER          IN output_arrays)
USES FUNCTION InterpGridArrays

PUBLIC:

REAL scalars[nscalars] TYPE=scalar

REAL arrays1d[narrays1d] TYPE=array DIM=1 DISTRIB=constant SIZE=array1d_npoints_i
REAL arrays2d[narrays2d] TYPE=array DIM=2 DISTRIB=constant SIZE=array2d_npoints_i,array2d_npoints_j
REAL arrays3d[narrays3d] TYPE=array DIM=3 DISTRIB=constant SIZE=array3d_npoints_i,array3d_npoints_j,array3d_npoints_k

REAL parrays1d[nparrays1d] TYPE=array DIM=1 DISTRIB=default SIZE=parray1d_npoints_i GHOSTSIZE=nghosts
REAL parrays2d[nparrays2d] TYPE=array DIM=2 DISTRIB=default SIZE=parray2d_npoints_i,parray2d_npoints_j GHOSTSIZE=nghosts,nghosts
REAL parrays3d[nparrays3d] TYPE=array DIM=3 DISTRIB=default SIZE=parray3d_npoints_i,parray3d_npoints_j,parray3d_npoints_k GHOSTSIZE=nghosts,nghosts,nghosts

schedule: # Schedule definitions for thorn InterpToArray

SCHEDULE InterpToArray AT analysis
{
  LANG: C
  OPTIONS: global
  STORAGE:  scalars arrays1d arrays2d arrays3d parrays1d parrays2d parrays3d
  TRIGGERS: scalars arrays1d arrays2d arrays3d parrays1d parrays2d parrays3d
} "Interpolate to grid arrays"

param: # Parameter definitions for thorn InterpToArray

RESTRICTED:

# CCTK_InterpGridArrays parameters

STRING interpolator_name "Name of the interpolator" STEERABLE=always
{
  ".*" :: "must be a registered interpolator"
} "Lagrange polynomial interpolation"

STRING interpolator_options "Options for the interpolator" STEERABLE=always
{
  ".*" :: "must be a valid option specification"
} "order=2"

STRING interpolator_coordinates "Coordinate system" STEERABLE=always
{
  ".*" :: "must be a registered coordinate system"
} "cart3d"

# InterpGridArrays ie. CarpetInterp2 parameters

BOOLEAN use_carpetinterp2 "Use InterpGridArrays rather than CCTK_InterpGridArrays" STEERABLE=always
{
} "no"

INT carpetinterp2_interpolator_order "Order of interpolation for CarpetInterp2" STEERABLE=always
{
  0:* :: "any order supported by CarpetInterp2"
} 2


# Common parameters

INT nghosts "Number of ghost zones"
{
  0:* :: ""
} 0



INT nscalars "Number of grid scalars"
{
  0:100 :: ""
} 0

STRING scalar_vars[100] "Names of the grid functions that should be interpolated on a point" STEERABLE=always
{
  "^$"                                                 :: "do not interpolate"
  "^[A-Za-z][A-Za-z0-9_]*[:][:][A-Za-z][A-Za-z0-9_]*$" :: "grid function name"
} ""

REAL scalar_x0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL scalar_y0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL scalar_z0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0



INT narrays1d "Number of 1D grid arrays"
{
  0:100 :: ""
} 0

STRING array1d_vars[100] "Names of the grid functions that should be interpolated on a line" STEERABLE=always
{
  "^$"                                                 :: "do not interpolate"
  "^[A-Za-z][A-Za-z0-9_]*[:][:][A-Za-z][A-Za-z0-9_]*$" :: "grid function name"
} ""

INT array1d_spacederivs[100] "Space derivative orders for each grid function"
{
  0:* :: ""
} 0

INT array1d_timederivs[100] "Time derivative order for each grid function"
{
  0:* :: ""
} 0

REAL array1d_x0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL array1d_y0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL array1d_z0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

INT array1d_npoints_i "Number of grid points for the 1D grid arrays in the i direction"
{
  0:* :: ""
} 0

REAL array1d_dx_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL array1d_dy_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL array1d_dz_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0



INT narrays2d "Number of 2D grid arrays"
{
  0:100 :: ""
} 0

STRING array2d_vars[100] "Names of the grid functions that should be interpolated on a 2D grid" STEERABLE=always
{
  "^$"                                                 :: "do not interpolate"
  "^[A-Za-z][A-Za-z0-9_]*[:][:][A-Za-z][A-Za-z0-9_]*$" :: "grid function name"
} ""

REAL array2d_x0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL array2d_y0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL array2d_z0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

INT array2d_npoints_i "Number of grid points for the 2D grid arrays in the i direction"
{
  0:* :: ""
} 0

REAL array2d_dx_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL array2d_dy_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL array2d_dz_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

INT array2d_npoints_j "Number of grid points for the 2D grid arrays in the j direction"
{
  0:* :: ""
} 0

REAL array2d_dx_j "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL array2d_dy_j "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL array2d_dz_j "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0



INT narrays3d "Number of 3D grid arrays"
{
  0:100 :: ""
} 0

STRING array3d_vars[100] "Names of the grid functions that should be interpolated on a 3D grid" STEERABLE=always
{
  "^$"                                                 :: "do not interpolate"
  "^[A-Za-z][A-Za-z0-9_]*[:][:][A-Za-z][A-Za-z0-9_]*$" :: "grid function name"
} ""

REAL array3d_x0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL array3d_y0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL array3d_z0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

INT array3d_npoints_i "Number of grid points for the 3D grid arrays in the i direction"
{
  0:* :: ""
} 0

REAL array3d_dx_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL array3d_dy_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL array3d_dz_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

INT array3d_npoints_j "Number of grid points for the 3D grid arrays in the j direction"
{
  0:* :: ""
} 0

REAL array3d_dx_j "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL array3d_dy_j "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL array3d_dz_j "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

INT array3d_npoints_k "Number of grid points for the 3D grid arrays in the k direction"
{
  0:* :: ""
} 0

REAL array3d_dx_k "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL array3d_dy_k "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL array3d_dz_k "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0



INT nparrays1d "Number of 1D parallel grid arrays"
{
  0:100 :: ""
} 0

STRING parray1d_vars[100] "Names of the grid functions that should be interpolated on a line" STEERABLE=always
{
  "^$"                                                 :: "do not interpolate"
  "^[A-Za-z][A-Za-z0-9_]*[:][:][A-Za-z][A-Za-z0-9_]*$" :: "grid function name"
} ""

INT parray1d_spacederivs[100] "Space derivative orders for each grid function"
{
  0:* :: ""
} 0

INT parray1d_timederivs[100] "Time derivative order for each grid function"
{
  0:* :: ""
} 0

REAL parray1d_x0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL parray1d_y0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL parray1d_z0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

INT parray1d_npoints_i "Number of grid points for the 1D grid parrays in the i direction"
{
  0:* :: ""
} 0

REAL parray1d_dx_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL parray1d_dy_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL parray1d_dz_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0



INT nparrays2d "Number of 2D parallel grid arrays"
{
  0:100 :: ""
} 0

STRING parray2d_vars[100] "Names of the grid functions that should be interpolated on a 2D grid" STEERABLE=always
{
  "^$"                                                 :: "do not interpolate"
  "^[A-Za-z][A-Za-z0-9_]*[:][:][A-Za-z][A-Za-z0-9_]*$" :: "grid function name"
} ""

REAL parray2d_x0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL parray2d_y0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL parray2d_z0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

INT parray2d_npoints_i "Number of grid points for the 2D parallel grid arrays in the i direction"
{
  0:* :: ""
} 0

REAL parray2d_dx_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL parray2d_dy_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL parray2d_dz_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

INT parray2d_npoints_j "Number of grid points for the 2D parallel grid arrays in the j direction"
{
  0:* :: ""
} 0

REAL parray2d_dx_j "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL parray2d_dy_j "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL parray2d_dz_j "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0



INT nparrays3d "Number of 3D grid parrays"
{
  0:100 :: ""
} 0

STRING parray3d_vars[100] "Names of the grid functions that should be interpolated on a 3D grid" STEERABLE=always
{
  "^$"                                                 :: "do not interpolate"
  "^[A-Za-z][A-Za-z0-9_]*[:][:][A-Za-z][A-Za-z0-9_]*$" :: "grid function name"
} ""

REAL parray3d_x0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL parray3d_y0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

REAL parray3d_z0 "Origin" STEERABLE=always
{
  *:* :: ""
} 0.0

INT parray3d_npoints_i "Number of grid points for the 3D parallel grid arrays in the i direction"
{
  0:* :: ""
} 0

REAL parray3d_dx_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL parray3d_dy_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL parray3d_dz_i "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

INT parray3d_npoints_j "Number of grid points for the 3D parallel grid arrays in the j direction"
{
  0:* :: ""
} 0

REAL parray3d_dx_j "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL parray3d_dy_j "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL parray3d_dz_j "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

INT parray3d_npoints_k "Number of grid points for the 3D parallel grid arrays in the k direction"
{
  0:* :: ""
} 0

REAL parray3d_dx_k "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL parray3d_dy_k "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

REAL parray3d_dz_k "Spacing" STEERABLE=always
{
  0.0:* :: ""
} 0.0

context: interp.c:
```
#include <assert.h>
#include <stdlib.h>

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

#include "util_Table.h"



void
InterpToArray (CCTK_ARGUMENTS)
{
  DECLARE_CCTK_ARGUMENTS;
  DECLARE_CCTK_PARAMETERS;
  
  
  
  int interpolator = -1;
  if (!use_carpetinterp2) {
    interpolator = CCTK_InterpHandle (interpolator_name);
    assert (interpolator >= 0);
  }
  
  int options_table = -1;
  if (!use_carpetinterp2) {
    options_table = Util_TableCreateFromString (interpolator_options);
    assert (options_table >= 0);
  }
  
  int coord_handle = -1;
  if (!use_carpetinterp2) {
    coord_handle = CCTK_CoordSystemHandle (interpolator_coordinates);
    assert (coord_handle >= 0);
  }
  
  

  /* Scalars */
  {
    int const nvars = nscalars;
    if (nvars > 0) {
      int const npoints = 1;
      
      CCTK_REAL * restrict const
        coordsx = malloc (npoints * sizeof * coordsx);
      assert (npoints==0 || coordsx);
      CCTK_REAL * restrict const
        coordsy = malloc (npoints * sizeof * coordsy);
      assert (npoints==0 || coordsy);
      CCTK_REAL * restrict const
        coordsz = malloc (npoints * sizeof * coordsz);
      assert (npoints==0 || coordsz);
      CCTK_POINTER_TO_CONST coords[3];
      coords[0] = coordsx;
      coords[1] = coordsy;
      coords[2] = coordsz;
      
      {
        int const n = 0;
        coordsx[n] = scalar_x0;
        coordsy[n] = scalar_y0;
        coordsz[n] = scalar_z0;
      }
      
      CCTK_INT * restrict const
        inputs = malloc (nvars * sizeof * inputs);
      assert (inputs);
      
      for (int n=0; n<nvars; ++n) {
        inputs[n] = CCTK_VarIndex (scalar_vars[n]);
        if (inputs[n] < 0) {
          inputs[n] = -1;
        }
      }
      
      CCTK_INT * restrict const
        output_types = malloc (nvars * sizeof * output_types);
      assert (output_types);
      
      for (int n=0; n<nvars; ++n) {
        output_types[n] = CCTK_VARIABLE_REAL;
      }
      
      CCTK_POINTER * restrict const outputs = malloc (nvars * sizeof * outputs);
      assert (outputs);
      
      for (int n=0; n<nvars; ++n) {
        outputs[n] = &scalars[npoints * n];
      }
      
      if (use_carpetinterp2)
      {
        int const ierr = InterpGridArrays
          (cctkGH, 3, carpetinterp2_interpolator_order,
           npoints, coords,
           nvars, inputs,
           nvars, outputs);
        assert (! ierr);
      }
      else
      {
        int const ierr = CCTK_InterpGridArrays
          (cctkGH, 3, interpolator, options_table, coord_handle,
           npoints, CCTK_VARIABLE_REAL, coords,
           nvars, inputs,
           nvars, output_types, outputs);
        assert (! ierr);
      }
      
      free (coordsx);
      free (coordsy);
      free (coordsz);
      free (inputs);
      free (output_types);
      free (outputs);
    }
  }
  
  
  
  /* 1D Arrays */
  {
    int const nvars = narrays1d;
    if (nvars > 0) {
      int const npoints = array1d_npoints_i;
      
      CCTK_REAL * restrict const
        coordsx = malloc (npoints * sizeof * coordsx);
      assert (npoints==0 || coordsx);
      CCTK_REAL * restrict const
        coordsy = malloc (npoints * sizeof * coordsy);
      assert (npoints==0 || coordsy);
      CCTK_REAL * restrict const
        coordsz = malloc (npoints * sizeof * coordsz);
      assert (npoints==0 || coordsz);
      CCTK_POINTER_TO_CONST coords[3];
      coords[0] = coordsx;
      coords[1] = coordsy;
      coords[2] = coordsz;
      
#pragma omp parallel for
      for (int i=0; i<array1d_npoints_i; ++i) {
        int const n = i;
        coordsx[n] = array1d_x0 + i * array1d_dx_i;
        coordsy[n] = array1d_y0 + i * array1d_dy_i;
        coordsz[n] = array1d_z0 + i * array1d_dz_i;
      }
      
      CCTK_INT * restrict const
        inputs = malloc (nvars * sizeof * inputs);
      assert (inputs);
      
      for (int n=0; n<nvars; ++n) {
        inputs[n] = CCTK_VarIndex (array1d_vars[n]);
        assert (inputs[n] >= 0);
        if (inputs[n] < 0) {
          inputs[n] = -1;
        }
      }
      
      CCTK_INT * restrict const
        output_types = malloc (nvars * sizeof * output_types);
      assert (output_types);
      
      for (int n=0; n<nvars; ++n) {
        output_types[n] = CCTK_VARIABLE_REAL;
      }
      
      CCTK_POINTER * restrict const
        outputs = malloc (nvars * sizeof * outputs);
      assert (outputs);
      
      for (int n=0; n<nvars; ++n) {
        outputs[n] = &arrays1d[npoints * n];
      }
      
      CCTK_INT * restrict const
        operation_codes = malloc (nvars * sizeof * operation_codes);
      assert (operation_codes);
      
      for (int n=0; n<nvars; ++n) {
        operation_codes[n] = array1d_spacederivs[n];
      }
      
      {
        int const ierr = Util_TableSetIntArray
          (options_table, nvars, operation_codes, "operation_codes");
        assert (! ierr);
      }
      
      CCTK_INT * restrict const
        time_deriv_order = malloc (nvars * sizeof * time_deriv_order);
      assert (time_deriv_order);
      
      for (int n=0; n<nvars; ++n) {
        time_deriv_order[n] = array1d_timederivs[n];
      }
      
      {
        int const ierr = Util_TableSetIntArray
          (options_table, nvars, time_deriv_order, "time_deriv_order");
        assert (! ierr);
      }
      
      if (use_carpetinterp2)
      {
        int const ierr = InterpGridArrays
          (cctkGH, 3, carpetinterp2_interpolator_order,
           npoints, coords,
           nvars, inputs,
           nvars, outputs);
        assert (! ierr);
      }
      else
      {
        int const ierr = CCTK_InterpGridArrays
          (cctkGH, 3, interpolator, options_table, coord_handle,
           npoints, CCTK_VARIABLE_REAL, coords,
           nvars, inputs,
           nvars, output_types, outputs);
        assert (! ierr);
      }
      
      {
        int const ierr = Util_TableDeleteKey (options_table, "operation_codes");
        assert (! ierr);
      }
      
      {
        int const ierr =
          Util_TableDeleteKey (options_table, "time_deriv_order");
        assert (! ierr);
      }
      
      free (coordsx);
      free (coordsy);
      free (coordsz);
      free (inputs);
      free (output_types);
      free (outputs);
      free (operation_codes);
      free (time_deriv_order);
    }
  }
  
  
  
  /* 2D Arrays */
  {
    int const nvars = narrays2d;
    if (nvars > 0) {
      int const npoints = array2d_npoints_i * array2d_npoints_j;
      
      CCTK_REAL * restrict const
        coordsx = malloc (npoints * sizeof * coordsx);
      assert (npoints==0 || coordsx);
      CCTK_REAL * restrict const
        coordsy = malloc (npoints * sizeof * coordsy);
      assert (npoints==0 || coordsy);
      CCTK_REAL * restrict const
        coordsz = malloc (npoints * sizeof * coordsz);
      assert (npoints==0 || coordsz);
      CCTK_POINTER_TO_CONST coords[3];
      coords[0] = coordsx;
      coords[1] = coordsy;
      coords[2] = coordsz;
      
#pragma omp parallel for
      for (int j=0; j<array2d_npoints_j; ++j) {
        for (int i=0; i<array2d_npoints_i; ++i) {
          const int n = i + array2d_npoints_i * j;
          coordsx[n] = array2d_x0 + i * array2d_dx_i + j * array2d_dx_j;
          coordsy[n] = array2d_y0 + i * array2d_dy_i + j * array2d_dy_j;
          coordsz[n] = array2d_z0 + i * array2d_dz_i + j * array2d_dz_j;
        }
      }
      
      CCTK_INT * restrict const
        inputs = malloc (nvars * sizeof * inputs);
      assert (inputs);
      
      for (int n=0; n<nvars; ++n) {
        inputs[n] = CCTK_VarIndex (array2d_vars[n]);
        if (inputs[n] < 0) {
          inputs[n] = -1;
        }
      }
      
      CCTK_INT * restrict const
        output_types = malloc (nvars * sizeof * output_types);
      assert (output_types);
      
      for (int n=0; n<nvars; ++n) {
        output_types[n] = CCTK_VARIABLE_REAL;
      }
      
      CCTK_POINTER * restrict const
        outputs = malloc (nvars * sizeof * outputs);
      assert (outputs);
      
      for (int n=0; n<nvars; ++n) {
        outputs[n] = &arrays2d[npoints * n];
      }
      
      if (use_carpetinterp2)
      {
        int const ierr = InterpGridArrays
          (cctkGH, 3, carpetinterp2_interpolator_order,
           npoints, coords,
           nvars, inputs,
           nvars, outputs);
        assert (! ierr);
      }
      else
      {
        int const ierr = CCTK_InterpGridArrays
          (cctkGH, 3, interpolator, options_table, coord_handle,
           npoints, CCTK_VARIABLE_REAL, coords,
           nvars, inputs,
           nvars, output_types, outputs);
        assert (! ierr);
      }
      
      free (coordsx);
      free (coordsy);
      free (coordsz);
      free (inputs);
      free (output_types);
      free (outputs);
    }
  }
  
  
  
  /* 3D Arrays */
  {
    int const nvars = narrays3d;
    if (nvars > 0) {
      int const npoints =
        array3d_npoints_i * array3d_npoints_j * array3d_npoints_k;
      
      CCTK_REAL * restrict const
        coordsx = malloc (npoints * sizeof * coordsx);
      assert (npoints==0 || coordsx);
      CCTK_REAL * restrict const
        coordsy = malloc (npoints * sizeof * coordsy);
      assert (npoints==0 || coordsy);
      CCTK_REAL * restrict const
        coordsz = malloc (npoints * sizeof * coordsz);
      assert (npoints==0 || coordsz);
      CCTK_POINTER_TO_CONST coords[3];
      coords[0] = coordsx;
      coords[1] = coordsy;
      coords[2] = coordsz;
      
#pragma omp parallel for
      for (int k=0; k<array3d_npoints_k; ++k) {
        for (int j=0; j<array3d_npoints_j; ++j) {
          for (int i=0; i<array3d_npoints_i; ++i) {
            int const n = i + array3d_npoints_i * (j + array3d_npoints_j * k);
            coordsx[n] = array3d_x0 + i * array3d_dx_i + j * array3d_dx_j + k * array3d_dx_k;
            coordsy[n] = array3d_y0 + i * array3d_dy_i + j * array3d_dy_j + k * array3d_dy_k;
            coordsz[n] = array3d_z0 + i * array3d_dz_i + j * array3d_dz_j + k * array3d_dz_k;
          }
        }
      }
      
      CCTK_INT * restrict const
        inputs = malloc (nvars * sizeof * inputs);
      assert (inputs);
      
      for (int n=0; n<nvars; ++n) {
        inputs[n] = CCTK_VarIndex (array3d_vars[n]);
        if (inputs[n] < 0) {
          inputs[n] = -1;
        }
      }
      
      CCTK_INT * restrict const
        output_types = malloc (nvars * sizeof * output_types);
      assert (output_types);
      
      for (int n=0; n<nvars; ++n) {
        output_types[n] = CCTK_VARIABLE_REAL;
      }
      
      CCTK_POINTER * restrict const
        outputs = malloc (nvars * sizeof * outputs);
      assert (outputs);
      
      for (int n=0; n<nvars; ++n) {
        outputs[n] = &arrays3d[npoints * n];
      }
      
      if (use_carpetinterp2)
      {
        int const ierr = InterpGridArrays
          (cctkGH, 3, carpetinterp2_interpolator_order,
           npoints, coords,
           nvars, inputs,
           nvars, outputs);
        assert (! ierr);
      }
      else
      {
        int const ierr = CCTK_InterpGridArrays
          (cctkGH, 3, interpolator, options_table, coord_handle,
           npoints, CCTK_VARIABLE_REAL, coords,
           nvars, inputs,
           nvars, output_types, outputs);
        assert (! ierr);
      }
      
      free (coordsx);
      free (coordsy);
      free (coordsz);
      free (inputs);
      free (output_types);
      free (outputs);
    }
  }
  
  
  
  /* Parallel 1D Arrays */
  {
    int const nvars = nparrays1d;
    if (nvars > 0) {
      int const group = CCTK_GroupIndex ("InterpToArray::parrays1d");
      assert (group >= 0);
      cGroupDynamicData dyndata;
      {
        int const ierr = CCTK_GroupDynamicData (cctkGH, group, &dyndata);
        assert (! ierr);
      }
      assert (dyndata.dim == 1);
      int const npoints = dyndata.lsh[0];
      
      CCTK_REAL * restrict const
        coordsx = malloc (npoints * sizeof * coordsx);
      assert (npoints==0 || coordsx);
      CCTK_REAL * restrict const
        coordsy = malloc (npoints * sizeof * coordsy);
      assert (npoints==0 || coordsy);
      CCTK_REAL * restrict const
        coordsz = malloc (npoints * sizeof * coordsz);
      assert (npoints==0 || coordsz);
      CCTK_POINTER_TO_CONST coords[3];
      coords[0] = coordsx;
      coords[1] = coordsy;
      coords[2] = coordsz;
      
#pragma omp parallel for
      for (int i=0; i<dyndata.lsh[0]; ++i) {
        int const n = i;
        coordsx[n] = (parray1d_x0 +
                      (dyndata.lbnd[0] + i) * parray1d_dx_i);
        coordsy[n] = (parray1d_y0 +
                      (dyndata.lbnd[0] + i) * parray1d_dy_i);
        coordsz[n] = (parray1d_z0 +
                      (dyndata.lbnd[0] + i) * parray1d_dz_i);
      }
      
      CCTK_INT * restrict const
        inputs = malloc (nvars * sizeof * inputs);
      assert (inputs);
      
      for (int n=0; n<nvars; ++n) {
        inputs[n] = CCTK_VarIndex (parray1d_vars[n]);
        assert (inputs[n] >= 0);
        if (inputs[n] < 0) {
          inputs[n] = -1;
        }
      }
      
      CCTK_INT * restrict const
        output_types = malloc (nvars * sizeof * output_types);
      assert (output_types);
      
      for (int n=0; n<nvars; ++n) {
        output_types[n] = CCTK_VARIABLE_REAL;
      }
      
      CCTK_POINTER * restrict const
        outputs = malloc (nvars * sizeof * outputs);
      assert (outputs);
      
      for (int n=0; n<nvars; ++n) {
        outputs[n] = &parrays1d[npoints * n];
      }
      
      CCTK_INT * restrict const
        operation_codes = malloc (nvars * sizeof * operation_codes);
      assert (operation_codes);
      
      for (int n=0; n<nvars; ++n) {
        operation_codes[n] = parray1d_spacederivs[n];
      }
      
      {
        int const ierr = Util_TableSetIntArray
          (options_table, nvars, operation_codes, "operation_codes");
        assert (! ierr);
      }
      
      CCTK_INT * restrict const
        time_deriv_order = malloc (nvars * sizeof * time_deriv_order);
      assert (time_deriv_order);
      
      for (int n=0; n<nvars; ++n) {
        time_deriv_order[n] = parray1d_timederivs[n];
      }
      
      {
        int const ierr = Util_TableSetIntArray
          (options_table, nvars, time_deriv_order, "time_deriv_order");
        assert (! ierr);
      }
      
      if (use_carpetinterp2)
      {
        int const ierr = InterpGridArrays
          (cctkGH, 3, carpetinterp2_interpolator_order,
           npoints, coords,
           nvars, inputs,
           nvars, outputs);
        assert (! ierr);
      }
      else
      {
        int const ierr = CCTK_InterpGridArrays
          (cctkGH, 3, interpolator, options_table, coord_handle,
           npoints, CCTK_VARIABLE_REAL, coords,
           nvars, inputs,
           nvars, output_types, outputs);
        assert (! ierr);
      }
      
      {
        int const ierr = Util_TableDeleteKey (options_table, "operation_codes");
        assert (! ierr);
      }
      
      {
        int const ierr =
          Util_TableDeleteKey (options_table, "time_deriv_order");
        assert (! ierr);
      }
      
      free (coordsx);
      free (coordsy);
      free (coordsz);
      free (inputs);
      free (output_types);
      free (outputs);
      free (operation_codes);
      free (time_deriv_order);
    }
  }
  
  
  
  /* Parallel 2D Arrays */
  {
    int const nvars = nparrays2d;
    if (nvars > 0) {
      int const group = CCTK_GroupIndex ("InterpToArray::parrays2d");
      assert (group >= 0);
      cGroupDynamicData dyndata;
      {
        int const ierr = CCTK_GroupDynamicData (cctkGH, group, &dyndata);
        assert (! ierr);
      }
      assert (dyndata.dim == 2);
      int const npoints = dyndata.lsh[0] * dyndata.lsh[1];
      
      CCTK_REAL * restrict const
        coordsx = malloc (npoints * sizeof * coordsx);
      assert (npoints==0 || coordsx);
      CCTK_REAL * restrict const
        coordsy = malloc (npoints * sizeof * coordsy);
      assert (npoints==0 || coordsy);
      CCTK_REAL * restrict const
        coordsz = malloc (npoints * sizeof * coordsz);
      assert (npoints==0 || coordsz);
      CCTK_POINTER_TO_CONST coords[3];
      coords[0] = coordsx;
      coords[1] = coordsy;
      coords[2] = coordsz;
      
#pragma omp parallel for
      for (int j=0; j<dyndata.lsh[1]; ++j) {
        for (int i=0; i<dyndata.lsh[0]; ++i) {
          int const n = i + dyndata.lsh[0] * j;
          coordsx[n] = (parray2d_x0 +
                        (dyndata.lbnd[0] + i) * parray2d_dx_i +
                        (dyndata.lbnd[1] + j) * parray2d_dx_j);
          coordsy[n] = (parray2d_y0 +
                        (dyndata.lbnd[0] + i) * parray2d_dy_i +
                        (dyndata.lbnd[1] + j) * parray2d_dy_j);
          coordsz[n] = (parray2d_z0 +
                        (dyndata.lbnd[0] + i) * parray2d_dz_i +
                        (dyndata.lbnd[1] + j) * parray2d_dz_j);
        }
      }
      
      CCTK_INT * restrict const
        inputs = malloc (nvars * sizeof * inputs);
      assert (inputs);
      
      for (int n=0; n<nvars; ++n) {
        inputs[n] = CCTK_VarIndex (parray2d_vars[n]);
        if (inputs[n] < 0) {
          inputs[n] = -1;
        }
      }
      
      CCTK_INT * restrict const
        output_types = malloc (nvars * sizeof * output_types);
      assert (output_types);
      
      for (int n=0; n<nvars; ++n) {
        output_types[n] = CCTK_VARIABLE_REAL;
      }
      
      CCTK_POINTER * restrict const
        outputs = malloc (nvars * sizeof * outputs);
      assert (outputs);
      
      for (int n=0; n<nvars; ++n) {
        outputs[n] = &parrays2d[npoints * n];
      }
      
      if (use_carpetinterp2)
      {
        int const ierr = InterpGridArrays
          (cctkGH, 3, carpetinterp2_interpolator_order,
           npoints, coords,
           nvars, inputs,
           nvars, outputs);
        assert (! ierr);
      }
      else
      {
        int const ierr = CCTK_InterpGridArrays
          (cctkGH, 3, interpolator, options_table, coord_handle,
           npoints, CCTK_VARIABLE_REAL, coords,
           nvars, inputs,
           nvars, output_types, outputs);
        assert (! ierr);
      }
      
      free (coordsx);
      free (coordsy);
      free (coordsz);
      free (inputs);
      free (output_types);
      free (outputs);
    }
  }
  
  
  
  /* Parallel 3D Arrays */
  {
    int const nvars = nparrays3d;
    if (nvars > 0) {
      int const group = CCTK_GroupIndex ("InterpToArray::parrays3d");
      assert (group >= 0);
      cGroupDynamicData dyndata;
      {
        int const ierr = CCTK_GroupDynamicData (cctkGH, group, &dyndata);
        assert (! ierr);
      }
      assert (dyndata.dim == 3);
      int const npoints = dyndata.lsh[0] * dyndata.lsh[1] * dyndata.lsh[2];
      
      CCTK_REAL * restrict const
        coordsx = malloc (npoints * sizeof * coordsx);
      assert (npoints==0 || coordsx);
      CCTK_REAL * restrict const
        coordsy = malloc (npoints * sizeof * coordsy);
      assert (npoints==0 || coordsy);
      CCTK_REAL * restrict const
        coordsz = malloc (npoints * sizeof * coordsz);
      assert (npoints==0 || coordsz);
      CCTK_POINTER_TO_CONST coords[3];
      coords[0] = coordsx;
      coords[1] = coordsy;
      coords[2] = coordsz;
      
#pragma omp parallel for
      for (int k=0; k<dyndata.lsh[2]; ++k) {
        for (int j=0; j<dyndata.lsh[1]; ++j) {
          for (int i=0; i<dyndata.lsh[0]; ++i) {
            int const n = i + dyndata.lsh[0] * (j + dyndata.lsh[1] * k);
            coordsx[n] = (parray3d_x0 +
                          (dyndata.lbnd[0] + i) * parray3d_dx_i +
                          (dyndata.lbnd[1] + j) * parray3d_dx_j +
                          (dyndata.lbnd[2] + k) * parray3d_dx_k);
            coordsy[n] = (parray3d_y0 +
                          (dyndata.lbnd[0] + i) * parray3d_dy_i +
                          (dyndata.lbnd[1] + j) * parray3d_dy_j +
                          (dyndata.lbnd[2] + k) * parray3d_dy_k);
            coordsz[n] = (parray3d_z0 +
                          (dyndata.lbnd[0] + i) * parray3d_dz_i +
                          (dyndata.lbnd[1] + j) * parray3d_dz_j +
                          (dyndata.lbnd[2] + k) * parray3d_dz_k);
          }
        }
      }
      
      CCTK_INT * restrict const
        inputs = malloc (nvars * sizeof * inputs);
      assert (inputs);
      
      for (int n=0; n<nvars; ++n) {
        inputs[n] = CCTK_VarIndex (parray3d_vars[n]);
        if (inputs[n] < 0) {
          inputs[n] = -1;
        }
      }
      
      CCTK_INT * restrict const
        output_types = malloc (nvars * sizeof * output_types);
      assert (output_types);
      
      for (int n=0; n<nvars; ++n) {
        output_types[n] = CCTK_VARIABLE_REAL;
      }
      
      CCTK_POINTER * restrict const
        outputs = malloc (nvars * sizeof * outputs);
      assert (outputs);
      
      for (int n=0; n<nvars; ++n) {
        outputs[n] = &parrays3d[npoints * n];
      }
      
      if (use_carpetinterp2)
      {
        int const ierr = InterpGridArrays
          (cctkGH, 3, carpetinterp2_interpolator_order,
           npoints, coords,
           nvars, inputs,
           nvars, outputs);
        assert (! ierr);
      }
      else
      {
        int const ierr = CCTK_InterpGridArrays
          (cctkGH, 3, interpolator, options_table, coord_handle,
           npoints, CCTK_VARIABLE_REAL, coords,
           nvars, inputs,
           nvars, output_types, outputs);
        assert (! ierr);
      }
      
      free (coordsx);
      free (coordsy);
      free (coordsz);
      free (inputs);
      free (output_types);
      free (outputs);
    }
  }
  
  
  
  if (options_table >= 0)
  {
    int const ierr = Util_TableDestroy (options_table);
    assert (! ierr);
  }
}

```


combined_doc_context: ## README (README):
```
Cactus Code Thorn InterpToArray
Thorn Author(s)     : Erik Schnetter <schnetter@cct.lsu.edu>
Thorn Maintainer(s) : Erik Schnetter <schnetter@cct.lsu.edu>
--------------------------------------------------------------------------

Purpose of the thorn:

Interpolate grid functions to grid arrays on a regular grid of
locations, so that these can e.g. be output more easily.

```

## Documentation (.tex files):
Files: documentation.tex

```tex
=== documentation.tex ===
% *======================================================================*
%  Cactus Thorn template for ThornGuide documentation
%  Author: Ian Kelley
%  Date: Sun Jun 02, 2002
%
%  Thorn documentation in the latex file doc/documentation.tex
%  will be included in ThornGuides built with the Cactus make system.
%  The scripts employed by the make system automatically include
%  pages about variables, parameters and scheduling parsed from the
%  relevant thorn CCL files.
%
%  This template contains guidelines which help to assure that your
%  documentation will be correctly added to ThornGuides. More
%  information is available in the Cactus UsersGuide.
%
%  Guidelines:
%   - Do not change anything before the line
%       % START CACTUS THORNGUIDE",
%     except for filling in the title, author, date, etc. fields.
%        - Each of these fields should only be on ONE line.
%        - Author names should be separated with a \\ or a comma.
%   - You can define your own macros, but they must appear after
%     the START CACTUS THORNGUIDE line, and must not redefine standard
%     latex commands.
%   - To avoid name clashes with other thorns, 'labels', 'citations',
%     'references', and 'image' names should conform to the following
%     convention:
%       ARRANGEMENT_THORN_LABEL
%     For example, an image wave.eps in the arrangement CactusWave and
%     thorn WaveToyC should be renamed to CactusWave_WaveToyC_wave.eps
%   - Graphics should only be included using the graphicx package.
%     More specifically, with the "\includegraphics" command.  Do
%     not specify any graphic file extensions in your .tex file. This
%     will allow us to create a PDF version of the ThornGuide
%     via pdflatex.
%   - References should be included with the latex "\bibitem" command.
%   - Use \begin{abstract}...\end{abstract} instead of \abstract{...}
%   - Do not use \appendix, instead include any appendices you need as
%     standard sections.
%   - For the benefit of our Perl scripts, and for future extensions,
%     please use simple latex.
%
% *======================================================================*
%
% Example of including a graphic image:
%    \begin{figure}[ht]
% 	\begin{center}
%    	   \includegraphics[width=6cm]{MyArrangement_MyThorn_MyFigure}
% 	\end{center}
% 	\caption{Illustration of this and that}
% 	\label{MyArrangement_MyThorn_MyLabel}
%    \end{figure}
%
% Example of using a label:
%   \label{MyArrangement_MyThorn_MyLabel}
%
% Example of a citation:
%    \cite{MyArrangement_MyThorn_Author99}
%
% Example of including a reference
%   \bibitem{MyArrangement_MyThorn_Author99}
%   {J. Author, {\em The Title of the Book, Journal, or periodical}, 1 (1999),
%   1--16. {\tt http://www.nowhere.com/}}
%
% *======================================================================*

% If you are using CVS use this line to give version information

\documentclass{article}

% Use the Cactus ThornGuide style file
% (Automatically used from Cactus distribution, if you have a
%  thorn without the Cactus Flesh download this from the Cactus
%  homepage at www.cactuscode.org)
\usepackage{../../../../doc/latex/cactus}

\begin{document}

% The author of the documentation
\author{Erik Schnetter \textless schnetter@cct.lsu.edu\textgreater}

% The title of the document (not necessarily the name of the Thorn)
\title{InterpToArray}

\date{June 30, 2010}

\maketitle

% Do not delete next line
% START CACTUS THORNGUIDE

% Add all definitions used in this documentation here
%   \def\mydef etc

% Add an abstract for this thorn's documentation
\begin{abstract}
  Interpolate grid functions to grid arrays on a regular grid of
  locations, so that these can e.g.\ be output more easily.
\end{abstract}

% The following sections are suggestive only.
% Remove them or add your own.

\section{Introduction}
Some post-processing and visualisation tools cannot handle mesh
refinement or multi-block systems.  These tools then require
interpolating the result onto a regular, uniform grid.  This is what
this thorn does.

InterpToArray can interpolate to 0D, 1D, 2D, and 3D grids.  The
interpolation result is stored in grid arrays.  These can be either
replicated or distributed over all processes.  Note that using
replicated (DISTRIB=constant) grid arrays can require a substantial
amount of memory on each process.

\section{Numerical Implementation}
InterpToArray uses standard Cactus interpolators such as PUGHInterp or
CarpetInterp.

\section{Using This Thorn}
The variables which should be interpolated are specified by
parameters; currently, up to 100 variables can be interpolated.

The interpolator and its options are also specified via parameters.

Finally, the locations of the points of the regular grid are also
specified via parameters.  These grid points are specified via an
\emph{origin} and \emph{offsets}, and they do not have to be aligned
with the coordinate axes.  For example, a 2D grid is specified via
three (real) vectors $x^a$, $di^a$, and $dj^a$, and via its (integer)
size $ni$ and $nj$.  The coordinates of the grid points are then given
via
$$
x^a(i,j) = x^a + i \cdot di^a + j \cdot dj^a
$$
for $0 \le i < ni$ and $0 \le j < nj$.  Grids with other dimensions
are specified equivalently.

% Do not delete next line
% END CACTUS THORNGUIDE

\end{document}

```



Reference solution:
src_code: # Main make.code.defn file for thorn InterpToArray

# Source files in this directory
SRCS = interp.c

# Subdirectories containing source files
SUBDIRS =