/* nag_rand_field_2d_predef_setup (g05zrc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg05.h>

static void display_results(Integer approx, Integer *m, double rho,
                            double *eig, Integer icount, double *lam);
static void read_input_data(Nag_Variogram *cov, Integer *np, double *params,
                            Nag_NormType *norm, double *var, double *xmin,
                            double *xmax, double *ymin, double *ymax,
                            Integer *ns, Integer *maxm, Nag_EmbedScale *corr,
                            Nag_EmbedPad *pad);

int main(void)
{
  /*  Scalars */
  Integer exit_status = 0;
  double rho, var, xmax, xmin, ymax, ymin;
  Integer approx, icount, np;
  /*  Arrays */
  double eig[3], params[5];
  double *lam = 0, *xx = 0, *yy = 0;
  Integer m[2], maxm[2], ns[2];
  /* Nag types */
  Nag_Variogram cov;
  Nag_NormType norm;
  Nag_EmbedPad pad;
  Nag_EmbedScale corr;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_rand_field_2d_predef_setup (g05zrc) Example Program Results\n\n");
  /* Get problem specifications from data file */
  read_input_data(&cov, &np, params, &norm, &var, &xmin, &xmax, &ymin, &ymax,
                  ns, maxm, &corr, &pad);
  if (!(lam = NAG_ALLOC(maxm[0] * maxm[1], double)) ||
      !(xx = NAG_ALLOC(ns[0], double)) || !(yy = NAG_ALLOC(ns[1], double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /* Get square roots of the eigenvalues of the embedding matrix. These are
   * obtained from the setup for simulating two-dimensional random fields,
   * with a predefined variogram, by the circulant embedding method using
   * nag_rand_field_2d_predef_setup (g05zrc).
   */
  nag_rand_field_2d_predef_setup(ns, xmin, xmax, ymin, ymax, maxm, var, cov,
                                 norm, np, params, pad, corr, lam, xx, yy, m,
                                 &approx, &rho, &icount, eig, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_rand_field_2d_predef_setup (g05zrc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  /* Output results */
  display_results(approx, m, rho, eig, icount, lam);
END:
  NAG_FREE(lam);
  NAG_FREE(xx);
  NAG_FREE(yy);
  return exit_status;
}

void read_input_data(Nag_Variogram *cov, Integer *np, double *params,
                     Nag_NormType *norm, double *var, double *xmin,
                     double *xmax, double *ymin, double *ymax,
                     Integer *ns, Integer *maxm, Nag_EmbedScale *corr,
                     Nag_EmbedPad *pad)
{
  Integer j;
  char nag_enum_arg[40];

  /* Read in covariance function name and convert to value using
   * nag_enum_name_to_value (x04nac).
   */
  scanf("%*[^\n] %39s%*[^\n]", nag_enum_arg);
  *cov = (Nag_Variogram) nag_enum_name_to_value(nag_enum_arg);
  /* Read in parameters */
  scanf("%" NAG_IFMT "%*[^\n]", np);
  for (j = 0; j < *np; j++)
    scanf("%lf", &params[j]);
  scanf("%*[^\n]");
  /* Read choice of norm to use, and convert name to value. */
  scanf(" %39s%*[^\n]", nag_enum_arg);
  *norm = (Nag_NormType) nag_enum_name_to_value(nag_enum_arg);
  /* Read in variance of random field */
  scanf("%lf%*[^\n]", var);
  /* Read in domain endpoints */
  scanf("%lf %lf%*[^\n]", xmin, xmax);
  scanf("%lf %lf%*[^\n]", ymin, ymax);
  /* Read in number of sample points in each direction */
  scanf("%" NAG_IFMT " %" NAG_IFMT "%*[^\n]", &ns[0], &ns[1]);
  /* Read in maximum size of embedding matrix */
  scanf("%" NAG_IFMT " %" NAG_IFMT "%*[^\n]", &maxm[0], &maxm[1]);
  /* Read name of scaling in case of approximation and convert to value. */
  scanf(" %39s%*[^\n]", nag_enum_arg);
  *corr = (Nag_EmbedScale) nag_enum_name_to_value(nag_enum_arg);
  /* Read in choice of padding and convert name to value. */
  scanf(" %39s%*[^\n]", nag_enum_arg);
  *pad = (Nag_EmbedPad) nag_enum_name_to_value(nag_enum_arg);
}

void display_results(Integer approx, Integer *m, double rho, double *eig,
                     Integer icount, double *lam)
{
  /*  Scalars */
  Integer i, j;

  /* Display size of embedding matrix */
  printf("\nSize of embedding matrix = %" NAG_IFMT "\n\n", m[0] * m[1]);
  /* Display approximation information if approximation used. */
  if (approx == 1) {
    printf("Approximation required\n\n");
    printf("rho = %10.5f\n", rho);
    printf("eig = ");
    for (j = 0; j < 3; j++)
      printf("%10.5f", eig[j]);
    printf("\nicount = %" NAG_IFMT "\n", icount);
  }
  else {
    printf("Approximation not required\n");
  }
  /* Display square roots of the eigenvalues of the embedding matrix. */
  printf("\nSquare roots of eigenvalues of embedding matrix:\n\n");
  for (i = 0; i < m[0]; i++) {
    for (j = 0; j < m[1]; j++) {
      printf("%8.4f", lam[i + j * m[0]]);
    }
    printf("\n");
  }
}