NAG Library Manual, Mark 28.5
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_mesh_dim2_gen_boundary (d06bac) Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 *
 * Mark 28.5, 2022.
 */

#include <nag.h>
#include <stdio.h>

/* Structure to allow data to be passed into */
/* the user-supplied function fbnd */

struct user {
  /* details of the ellipse containing the NAG logo */

  double xa, xb, x0, y0;
};

#ifdef __cplusplus
extern "C" {
#endif
static double NAG_CALL fbnd(Integer, double, double, Nag_Comm *);
#ifdef __cplusplus
}
#endif

#define EDGE(I, J) edge[3 * (J - 1) + I - 1]
#define LINED(I, J) lined[4 * (J - 1) + I - 1]
#define CONN(I, J) conn[3 * (J - 1) + I - 1]
#define COOR(I, J) coor[2 * (J - 1) + I - 1]
#define COORCH(I, J) coorch[2 * (J - 1) + I - 1]
#define CRUS(I, J) crus[2 * (J - 1) + I - 1]

int main(void) {
  const Integer sdcrus = 4, nvint = 0;
  struct user ellipse;
  Nag_Comm comm;
  double x0, xa, xb, xmax, xmin, y0, ymax, ymin;
  Integer exit_status, i, itrace, j, k, ncomp, nedge, nelt, nlines;
  Integer npropa, nv, nvb, reftk, l, nvmax, nedmx;
  char pmesh[2];
  double *coor = 0, *coorch = 0, *crus = 0, *rate = 0, *weight = 0;
  Integer *conn = 0, *edge = 0, *lcomp = 0, *lined = 0, *nlcomp = 0;
  NagError fail;

  INIT_FAIL(fail);

  exit_status = 0;

  printf(" nag_mesh_dim2_gen_boundary (d06bac) Example Program Results\n\n");
  fflush(stdout);

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  /* Initialize boundary mesh inputs: */
  /* the number of line and of the characteristic points of */
  /* the boundary mesh */
  scanf("%" NAG_IFMT " %" NAG_IFMT " %" NAG_IFMT " %*[^\n] ", &nlines, &nvmax,
        &nedmx);

  /* Allocate memory */

  if (!(coor = NAG_ALLOC(2 * nvmax, double)) ||
      !(coorch = NAG_ALLOC(2 * nlines, double)) ||
      !(crus = NAG_ALLOC(2 * sdcrus, double)) ||
      !(rate = NAG_ALLOC(nlines, double)) || !(weight = NAG_ALLOC(1, double)) ||
      !(conn = NAG_ALLOC(3 * (2 * nvmax + 5), Integer)) ||
      !(edge = NAG_ALLOC(3 * nedmx, Integer)) ||
      !(lined = NAG_ALLOC(4 * nlines, Integer)) ||
      !(lcomp = NAG_ALLOC(nlines, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* The lines of the boundary mesh */

  for (j = 1; j <= nlines; ++j) {
    for (i = 1; i <= 4; ++i)
      scanf("%" NAG_IFMT "", &LINED(i, j));
    scanf("%lf", &rate[j - 1]);
  }
  scanf("%*[^\n] ");

  /* The ellipse boundary which envelops  */
  /* the NAG Logo, the N, the A and the G */
  for (j = 1; j <= nlines; ++j)
    scanf("%lf", &COORCH(1, j));
  scanf("%*[^\n] ");
  for (j = 1; j <= nlines; ++j)
    scanf("%lf", &COORCH(2, j));
  scanf("%*[^\n] ");
  for (j = 1; j <= sdcrus; ++j)
    scanf("%lf", &CRUS(1, j));
  scanf("%*[^\n] ");
  for (j = 1; j <= sdcrus; ++j)
    scanf("%lf", &CRUS(2, j));
  scanf("%*[^\n] ");

  /* The number of connected components  */
  /* to the boundary and their information */
  scanf("%" NAG_IFMT "%*[^\n] ", &ncomp);

  /* Allocate memory */

  if (!(nlcomp = NAG_ALLOC(ncomp, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  j = 0;
  for (i = 0; i < ncomp; ++i) {
    scanf("%" NAG_IFMT "", &nlcomp[i]);
    scanf("%*[^\n] ");
    l = j + NAG_IABS(nlcomp[i]);
    for (k = j; k < l; ++k)
      scanf("%" NAG_IFMT "", &lcomp[k]);
    scanf("%*[^\n] ");

    j += NAG_IABS(nlcomp[i]);
  }
  scanf(" %1s %*[^\n] ", pmesh);

  /* Data passed to the user-supplied function */

  xmin = COORCH(1, 4);
  xmax = COORCH(1, 2);
  ymin = COORCH(2, 1);
  ymax = COORCH(2, 3);

  xa = (xmax - xmin) / 2.0;
  xb = (ymax - ymin) / 2.0;

  x0 = (xmin + xmax) / 2.0;
  y0 = (ymin + ymax) / 2.0;

  comm.p = (Pointer)&ellipse;

  ellipse.xa = xa;
  ellipse.xb = xb;
  ellipse.x0 = x0;
  ellipse.y0 = y0;

  itrace = -1;

  /* Call to the boundary mesh generator */

  /* nag_mesh_dim2_gen_boundary (d06bac).
   * Generates a boundary mesh
   */
  nag_mesh_dim2_gen_boundary(nlines, coorch, lined, fbnd, crus, sdcrus, rate,
                             ncomp, nlcomp, lcomp, nvmax, nedmx, &nvb, coor,
                             &nedge, edge, itrace, 0, &comm, &fail);
  if (fail.code == NE_NOERROR) {
    if (pmesh[0] == 'N') {
      printf(" Boundary mesh characteristics\n");
      printf(" nvb   =%6" NAG_IFMT "\n", nvb);
      printf(" nedge =%6" NAG_IFMT "\n", nedge);
    } else if (pmesh[0] == 'Y') {
      /* Output the mesh to view it using the NAG Graphics Library */

      printf(" %10" NAG_IFMT "%10" NAG_IFMT "\n", nvb, nedge);

      for (i = 1; i <= nvb; ++i)
        printf("  %4" NAG_IFMT "  %15.6e  %15.6e  \n", i, COOR(1, i),
               COOR(2, i));

      for (i = 1; i <= nedge; ++i)
        printf(" %4" NAG_IFMT "%4" NAG_IFMT "%4" NAG_IFMT "%4" NAG_IFMT "\n", i,
               EDGE(1, i), EDGE(2, i), EDGE(3, i));
    } else {
      printf("Problem with the printing option Y or N\n");
      exit_status = -1;
      goto END;
    }
  } else {
    printf("Error from nag_mesh_dim2_gen_boundary (d06bac).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Initialize mesh control parameters */

  itrace = 0;
  npropa = 1;

  /* Call to the 2D Delaunay-Voronoi mesh generator */

  /* nag_mesh_dim2_gen_delaunay (d06abc).
   * Generates a two-dimensional mesh using a Delaunay-Voronoi
   * process
   */
  nag_mesh_dim2_gen_delaunay(nvb, nvint, nvmax, nedge, edge, &nv, &nelt, coor,
                             conn, weight, npropa, itrace, 0, &fail);
  if (fail.code == NE_NOERROR) {
    if (pmesh[0] == 'N') {
      printf(" Complete mesh characteristics (Delaunay-Voronoi)\n");
      printf("   nv (rounded to nearest 10) =%6" NAG_IFMT "\n",
             10 * ((nv + 5) / 10));
      printf(" nelt (rounded to nearest 10) =%6" NAG_IFMT "\n",
             10 * ((nelt + 5) / 10));
    } else if (pmesh[0] == 'Y') {
      /* Output the mesh to view it using the NAG Graphics Library */

      printf(" %10" NAG_IFMT "%10" NAG_IFMT "\n", nv, nelt);

      for (i = 1; i <= nv; ++i)
        printf("  %15.6e  %15.6e  \n", COOR(1, i), COOR(2, i));

      reftk = 0;
      for (k = 1; k <= nelt; ++k)
        printf(" %10" NAG_IFMT "%10" NAG_IFMT "%10" NAG_IFMT "%10" NAG_IFMT
               "\n",
               CONN(1, k), CONN(2, k), CONN(3, k), reftk);
    } else {
      printf("Problem with the printing option Y or N\n");
      exit_status = -1;
      goto END;
    }
  } else {
    printf("Error from nag_mesh_dim2_gen_delaunay (d06abc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Call to the 2D Advancing front mesh generator */

  /* nag_mesh_dim2_gen_front (d06acc).
   * Generates a two-dimensional mesh using an Advancing-front
   * method
   */
  nag_mesh_dim2_gen_front(nvb, nvint, nvmax, nedge, edge, &nv, &nelt, coor,
                          conn, weight, itrace, 0, &fail);
  if (fail.code == NE_NOERROR) {
    if (pmesh[0] == 'N') {
      printf(" Complete mesh characteristics (Advancing Front)\n");
      printf("   nv (rounded to nearest 10) =%6" NAG_IFMT "\n",
             10 * ((nv + 5) / 10));
      printf(" nelt (rounded to nearest 10) =%6" NAG_IFMT "\n",
             10 * ((nelt + 5) / 10));
    } else if (pmesh[0] == 'Y') {
      /* Output the mesh to view it using the NAG Graphics Library */

      printf(" %10" NAG_IFMT "%10" NAG_IFMT "\n", nv, nelt);

      for (i = 1; i <= nv; ++i)
        printf("  %15.6e  %15.6e  \n", COOR(1, i), COOR(2, i));

      reftk = 0;
      for (k = 1; k <= nelt; ++k)
        printf(" %10" NAG_IFMT "%10" NAG_IFMT "%10" NAG_IFMT "%10" NAG_IFMT
               "\n",
               CONN(1, k), CONN(2, k), CONN(3, k), reftk);
    } else {
      printf("Problem with the printing option Y or N\n");
      exit_status = -1;
      goto END;
    }
  } else {
    printf("Error from nag_mesh_dim2_gen_front (d06acc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

END:
  NAG_FREE(coor);
  NAG_FREE(coorch);
  NAG_FREE(crus);
  NAG_FREE(rate);
  NAG_FREE(weight);
  NAG_FREE(conn);
  NAG_FREE(edge);
  NAG_FREE(lcomp);
  NAG_FREE(lined);
  NAG_FREE(nlcomp);

  return exit_status;
}

static double NAG_CALL fbnd(Integer i, double x, double y, Nag_Comm *pcomm) {
  double ret_val, d1, d2;
  double radius2, x0, xa, xb, y0;
  struct user *ellipse = (struct user *)pcomm->p;

  xa = ellipse->xa;
  xb = ellipse->xb;
  x0 = ellipse->x0;
  y0 = ellipse->y0;

  ret_val = 0.0;

  switch (i) {
  case 1:

    /* line 1,2,3, and 4: ellipse centred in (X0,Y0) with */
    /* XA and XB as coefficients */

    d1 = (x - x0) / xa;
    d2 = (y - y0) / xb;

    ret_val = d1 * d1 + d2 * d2 - 1.0;
    break;

  case 2:

    /* line 7, lower arc on letter n, is a circle centred in (X0,Y0)
     *         with radius SQRT(RADIUS2)
     */

    x0 = 0.5;
    y0 = 6.25;
    radius2 = 20.3125;
    d1 = x - x0;
    d2 = y - y0;

    ret_val = d1 * d1 + d2 * d2 - radius2;
    break;

  case 3:

    /* line 11, upper arc on letter n, is a circle centred in (X0,Y0)
     * with radius SQRT(RADIUS2)
     */

    x0 = 1.0;
    y0 = 4.0;
    radius2 = 9.0 + (11.0 - y0) * (11.0 - y0);
    d1 = x - x0;
    d2 = y - y0;

    ret_val = d1 * d1 + d2 * d2 - radius2;
    break;

  case 4:
    /* line 15, upper arc on letter a, is a circle centred in (X0,Y0)
     * with radius SQRT(RADIUS2) touching point (5,11).
     */

    x0 = 8.5;
    y0 = 2.75;
    radius2 = (x0 - 5.0) * (x0 - 5.0) + (11.0 - y0) * (11.0 - y0);
    d1 = x - x0;
    d2 = y - y0;

    ret_val = d1 * d1 + d2 * d2 - radius2;
    break;

  case 5:

    /* line 25, lower arc on hat of 'a', is a circle centred in (X0,Y0)
     * with radius SQRT(RADIUS2) touching point (11,10).
     */

    x0 = 8.5;
    y0 = 4.0;
    radius2 = 2.5 * 2.5 + (10.0 - y0) * (10.0 - y0);
    d1 = x - x0;
    d2 = y - y0;

    ret_val = d1 * d1 + d2 * d2 - radius2;
    break;
  case 6:

    /* lines 20, 21 and 22, belly of letter a, is an ellipse centered
     * in (X0, Y0) with semi-axes 3.5 and 2.75.
     */

    x0 = 8.5;
    y0 = 5.75;
    d1 = (x - x0) / 3.5;
    d2 = (y - y0) / 2.75;

    ret_val = d1 * d1 + d2 * d2 - 1.0;
    break;
  case 7:

    /* lines 43, 44 and 45, outer curve on bottom of 'g', is an ellipse
     * centered in (X0, Y0) with semi-axes 3.5 and 2.5.
     */

    x0 = 17.5;
    y0 = 2.5;
    d1 = (x - x0) / 3.5;
    d2 = (y - y0) / 2.5;

    ret_val = d1 * d1 + d2 * d2 - 1.0;
    break;
  case 8:

    /* lines 28, 29 and 30, inner curve on bottom of 'g', is an ellipse
     * centered in (X0, Y0) with semi-axes 2.0 and 1.5.
     */

    x0 = 17.5;
    y0 = 2.5;
    d1 = (x - x0) / 2.0;
    d2 = (y - y0) / 1.5;

    ret_val = d1 * d1 + d2 * d2 - 1.0;
    break;
  case 9:

    /* line 42, inner curve on lower middle of 'g', is an ellipse
     * centered in (X0, Y0) with semi-axes 1.5 and 0.5.
     */

    x0 = 17.5;
    y0 = 5.5;
    d1 = (x - x0) / 1.5;
    d2 = (y - y0) / 0.5;

    ret_val = d1 * d1 + d2 * d2 - 1.0;
    break;
  case 10:

    /* line 31, outer curve on lower middle of 'g', is an ellipse
     * centered in (X0, Y0) with semi-axes 2.0 and 1.5.
     */

    x0 = 17.5;
    y0 = 5.5;
    d1 = (x - x0) / 3.0;
    d2 = (y - y0) / 1.5;

    ret_val = d1 * d1 + d2 * d2 - 1.0;
    break;
  case 11:

    /* line 41, inner curve on upper middle of 'g', is an ellipse
     * centered in (X0, Y0) with semi-axes 1.0 and 1.0.
     */

    x0 = 17.0;
    y0 = 5.5;
    d1 = (x - x0);
    d2 = (y - y0);

    ret_val = d1 * d1 + d2 * d2 - 1.0;
    break;
  case 12:

    /* line 32, outer curve on upper middle of 'g', is an ellipse
     * centered in (X0, Y0) with semi-axes 1.5 and 1.1573.
     */

    x0 = 16.0;
    y0 = 5.5;
    d1 = (x - x0) / 1.5;
    d2 = (y - y0) / 1.1573;

    ret_val = d1 * d1 + d2 * d2 - 1.0;
    break;
  case 13:

    /* lines 33, 33, 34, 39 and 40, upper portion of 'g', is an ellipse
     * centered in (X0, Y0) with semi-axes 3.0 and 2.75.
     */

    x0 = 17.0;
    y0 = 9.25;
    d1 = (x - x0) / 3.0;
    d2 = (y - y0) / 2.75;

    ret_val = d1 * d1 + d2 * d2 - 1.0;
    break;
  default:
    break;
  }

  return ret_val;
}