
#include <extUtil.h>

/* attach.f -- translated by f2c (version 19950110).
*/


int distattachhe20_(double *xi,double *et,double *pneigh,double *pnode,double *dist,double *p)
{
    /* System generated locals */
    double d__1, d__2, d__3;

    /* Local variables */
    static int i;

/*     calculates the global coordinates p within an 8-node 2-D element */
/*     (given by global coordinates pneigh)of a point with given local */
/*     coordinates (xi,et) and determines the distance from this point */
/*     (dist)to the point with global coordinates pnode */

    /* Parameter adjustments */
    --p;
    --pnode;
    pneigh -= 4;

    /* Function Body */
    for (i = 1; i <= 3; ++i) {
	p[i] = pneigh[i + 3] * (1. - *xi) * (1. - *et) * (-(*xi) - *et - 1.) /
		 4. + pneigh[i + 6] * (*xi + 1.) * (1. - *et) * (*xi - *et -
		1.) / 4. + pneigh[i + 9] * (*xi + 1.) * (*et + 1.) * (*xi + *
		et - 1.) / 4. + pneigh[i + 12] * (1. - *xi) * (*et + 1.) * (-(
		*xi) + *et - 1.) / 4. + pneigh[i + 15] * (1. - *xi * *xi) * (
		1. - *et) / 2. + pneigh[i + 18] * (1. - *et * *et) * (*xi +
		1.) / 2. + pneigh[i + 21] * (1. - *xi * *xi) * (*et + 1.) /
		2. + pneigh[i + 24] * (1. - *et * *et) * (1. - *xi) / 2.;
    }

/* Computing 2nd power */
    d__1 = p[1] - pnode[1];
/* Computing 2nd power */
    d__2 = p[2] - pnode[2];
/* Computing 2nd power */
    d__3 = p[3] - pnode[3];
    *dist = sqrt(d__1 * d__1 + d__2 * d__2 + d__3 * d__3);

    return 0;
} /* distattachhe20_ */



int distattachhe8_(double *xi,double * et, double *pneigh, double *pnode, double *dist, double *p)
{
    /* System generated locals */
    double d__1, d__2, d__3;


    /* Local variables */
    static int i;

/*     calculates the global coordinates p within an 8-node 2-D element */
/*     (given by global coordinates pneigh)of a point with given local */
/*     coordinates (xi,et) and determines the distance from this point */
/*     (dist)to the point with global coordinates pnode */

    /* Parameter adjustments */
    --p;
    --pnode;
    pneigh -= 4;

    /* Function Body */
    for (i = 1; i <= 3; ++i) {
	p[i] = pneigh[i + 3] * (1. - *xi) * (1. - *et) / 4. +
               pneigh[i + 6] * (*xi + 1.) * (1. - *et) / 4. +
               pneigh[i + 9] * (*xi + 1.) * (*et + 1.) / 4. + 
               pneigh[i + 12] * (1. - *xi) * (*et + 1.) / 4.;
    }

/* Computing 2nd power */
    d__1 = p[1] - pnode[1];
/* Computing 2nd power */
    d__2 = p[2] - pnode[2];
/* Computing 2nd power */
    d__3 = p[3] - pnode[3];
    *dist = sqrt(d__1 * d__1 + d__2 * d__2 + d__3 * d__3);

    return 0;
} /* distattachhe8_ */



int shapefhe20_(double *xi, double *et, double *coef)
{

/*     calculates the shape function at (xi,et) */

    /* Parameter adjustments */
    --coef;

    /* Function Body */
    coef[1] = -1.;
    coef[2] = (1. - *xi) * (1. - *et) * (-(*xi) - *et - 1.) / 4.;
    coef[3] = (*xi + 1.) * (1. - *et) * (*xi - *et - 1.) / 4.;
    coef[4] = (*xi + 1.) * (*et + 1.) * (*xi + *et - 1.) / 4.;
    coef[5] = (1. - *xi) * (*et + 1.) * (-(*xi) + *et - 1.) / 4.;
    coef[6] = (1. - *xi * *xi) * (1. - *et) / 2.;
    coef[7] = (1. - *et * *et) * (*xi + 1.) / 2.;
    coef[8] = (1. - *xi * *xi) * (*et + 1.) / 2.;
    coef[9] = (1. - *et * *et) * (1. - *xi) / 2.;

    return 0;
} /* shapef_ */


int shapefhe8_(double *xi, double *et, double *coef)
{

/*     calculates the shape function at (xi,et) */

    /* Parameter adjustments */
    --coef;

    /* Function Body */
    coef[1] = -1.;
    coef[2] = (1. - *xi) * (1. - *et) / 4.;
    coef[3] = (*xi + 1.) * (1. - *et) / 4.;
    coef[4] = (*xi + 1.) * (*et + 1.) / 4.;
    coef[5] = (1. - *xi) * (*et + 1.) / 4.;

    return 0;
} /* shapef_ */

int attachhe20_(double *co, int *nk, int *neigh, int *node, double *xi_loc__,double *et_loc__)
{
    /* System generated locals */
    double d__1;

    /* Local variables */
    static double aold[9]	/* was [3][3] */;
    static int imin, jmin;
    static double a[9]	/* was [3][3] */;
    static int i, j;
    static double p[3], pnode[3], etold[9]	/* was [3][3] */, xiold[9]	
	    /* was [3][3] */;
    static int ii;
    static double et[9]	/* was [3][3] */, xi[9]	/* was [3][3] */,
	    pneigh[24]	/* was [3][8] */;
    static double distmin;


/*     projects a node ('node') on the face of a twenty-node element. The
*/
/*     eight face nodes are contained in neigh. */

    /* Parameter adjustments */
    --neigh;
    co -= 4;

    /* Function Body */
    for (i = 1; i <= 8; ++i) {
	for (j = 1; j <= 3; ++j) {
	    pneigh[j + i * 3 - 4] = co[j + neigh[i] * 3];
	}
    }

    for (j = 1; j <= 3; ++j) {
	pnode[j - 1] = co[j + *node * 3];
    }

/*     initialisation */

    for (i = -1; i <= 1; ++i) {
	for (j = -1; j <= 1; ++j) {
	    xi[i + j * 3 + 4] = i * .01;
	    et[i + j * 3 + 4] = j * .01;
	    distattachhe20_(&xi[i + j * 3 + 4], &et[i + j * 3 + 4], pneigh, pnode,
		     &a[i + j * 3 + 4], p);
	}
    }

/*     minimizing the distance from the face to the node */

    for (ii = 1; ii <= 1000000; ++ii) {
	distmin = a[4];
	imin = 0;
	jmin = 0;
	for (i = -1; i <= 1; ++i) {
	    for (j = -1; j <= 1; ++j) {
		if (a[i + j * 3 + 4] < distmin) {
		    distmin = a[i + j * 3 + 4];
		    imin = i;
		    jmin = j;
		}
	    }
	}

/*       exit if minimum found */

	if (imin == 0 && jmin == 0) {
	    goto L10;
	}

	for (i = -1; i <= 1; ++i) {
	    for (j = -1; j <= 1; ++j) {
		aold[i + j * 3 + 4] = a[i + j * 3 + 4];
		xiold[i + j * 3 + 4] = xi[i + j * 3 + 4];
		etold[i + j * 3 + 4] = et[i + j * 3 + 4];
	    }
	}

	for (i = -1; i <= 1; ++i) {
	    for (j = -1; j <= 1; ++j) {
		if (i + imin >= -1 && i + imin <= 1 && j + jmin >= -1 && j +
			jmin <= 1) {
		    a[i + j * 3 + 4] = aold[i + imin + (j + jmin) * 3 + 4];
		    xi[i + j * 3 + 4] = xiold[i + imin + (j + jmin) * 3 + 4];
		    et[i + j * 3 + 4] = etold[i + imin + (j + jmin) * 3 + 4];
		} else {
		    xi[i + j * 3 + 4] += imin * .01;
		    et[i + j * 3 + 4] += jmin * .01;

/* Computing MIN */
		    d__1 = xi[i + j * 3 + 4];
		    xi[i + j * 3 + 4] = smin(d__1,1.);
/* Computing MAX */
		    d__1 = xi[i + j * 3 + 4];
		    xi[i + j * 3 + 4] = smax(d__1,-1.);
/* Computing MIN */
		    d__1 = et[i + j * 3 + 4];
		    et[i + j * 3 + 4] = smin(d__1,1.);
/* Computing MAX */
		    d__1 = et[i + j * 3 + 4];
		    et[i + j * 3 + 4] = smax(d__1,-1.);

		    distattachhe20_(&xi[i + j * 3 + 4], &et[i + j * 3 + 4],
			    pneigh, pnode, &a[i + j * 3 + 4], p);
		}
	    }
	}
    }
L10:

    distattachhe20_(&xi[4], &et[4], pneigh, pnode, &a[4], p);
    for (i = 1; i <= 3; ++i) {
	co[i + *node * 3] = p[i - 1];
    }

    *xi_loc__ = xi[4];
    *et_loc__ = et[4];

    return 0;
} /* attach_ */



int attachhe8_(double *co, int *nk, int *neigh, int *node,double *xi_loc__,double *et_loc__)
{
    /* System generated locals */
    double d__1;

    /* Local variables */
    static double aold[9]	/* was [3][3] */;
    static int imin, jmin;
    static double a[9]	/* was [3][3] */;
    static int i, j;
    static double p[3], pnode[3], etold[9]	/* was [3][3] */, xiold[9]	
	    /* was [3][3] */;
    static int ii;
    static double et[9]	/* was [3][3] */, xi[9]	/* was [3][3] */,
	    pneigh[24]	/* was [3][8] */;
    static double distmin;


/*     projects a node ('node') on the face of a twenty-node element. The
*/
/*     eight face nodes are contained in neigh. */

    /* Parameter adjustments */
    --neigh;
    co -= 4;

    /* Function Body */
    for (i = 1; i <= 8; ++i) {
	for (j = 1; j <= 3; ++j) {
	    pneigh[j + i * 3 - 4] = co[j + neigh[i] * 3];
	}
    }

    for (j = 1; j <= 3; ++j) {
	pnode[j - 1] = co[j + *node * 3];
    }

/*     initialisation */

    for (i = -1; i <= 1; ++i) {
	for (j = -1; j <= 1; ++j) {
	    xi[i + j * 3 + 4] = i * .01;
	    et[i + j * 3 + 4] = j * .01;
	    distattachhe8_(&xi[i + j * 3 + 4], &et[i + j * 3 + 4], pneigh, pnode,
		     &a[i + j * 3 + 4], p);
	}
    }

/*     minimizing the distance from the face to the node */

    for (ii = 1; ii <= 1000000; ++ii) {
	distmin = a[4];
	imin = 0;
	jmin = 0;
	for (i = -1; i <= 1; ++i) {
	    for (j = -1; j <= 1; ++j) {
		if (a[i + j * 3 + 4] < distmin) {
		    distmin = a[i + j * 3 + 4];
		    imin = i;
		    jmin = j;
		}
	    }
	}

/*       exit if minimum found */

	if (imin == 0 && jmin == 0) {
	    goto L10;
	}

	for (i = -1; i <= 1; ++i) {
	    for (j = -1; j <= 1; ++j) {
		aold[i + j * 3 + 4] = a[i + j * 3 + 4];
		xiold[i + j * 3 + 4] = xi[i + j * 3 + 4];
		etold[i + j * 3 + 4] = et[i + j * 3 + 4];
	    }
	}

	for (i = -1; i <= 1; ++i) {
	    for (j = -1; j <= 1; ++j) {
		if (i + imin >= -1 && i + imin <= 1 && j + jmin >= -1 && j +
			jmin <= 1) {
		    a[i + j * 3 + 4] = aold[i + imin + (j + jmin) * 3 + 4];
		    xi[i + j * 3 + 4] = xiold[i + imin + (j + jmin) * 3 + 4];
		    et[i + j * 3 + 4] = etold[i + imin + (j + jmin) * 3 + 4];
		} else {
		    xi[i + j * 3 + 4] += imin * .01;
		    et[i + j * 3 + 4] += jmin * .01;

/* Computing MIN */
		    d__1 = xi[i + j * 3 + 4];
		    xi[i + j * 3 + 4] = smin(d__1,1.);
/* Computing MAX */
		    d__1 = xi[i + j * 3 + 4];
		    xi[i + j * 3 + 4] = smax(d__1,-1.);
/* Computing MIN */
		    d__1 = et[i + j * 3 + 4];
		    et[i + j * 3 + 4] = smin(d__1,1.);
/* Computing MAX */
		    d__1 = et[i + j * 3 + 4];
		    et[i + j * 3 + 4] = smax(d__1,-1.);

		    distattachhe8_(&xi[i + j * 3 + 4], &et[i + j * 3 + 4],
			    pneigh, pnode, &a[i + j * 3 + 4], p);
		}
	    }
	}
    }
L10:

    distattachhe8_(&xi[4], &et[4], pneigh, pnode, &a[4], p);
    for (i = 1; i <= 3; ++i) {
	co[i + *node * 3] = p[i - 1];
    }

    *xi_loc__ = xi[4];
    *et_loc__ = et[4];

    return 0;
} /* attach_ */



