/* --------------------------------------------------------------------  */
/*                          CALCULIX                                     */
/*                   - GRAPHICAL INTERFACE -                             */
/*                                                                       */
/*     A 3-dimensional pre- and post-processor for finite elements       */
/*              Copyright (C) 1996 Klaus Wittig                          */
/*                                                                       */
/*     This program is free software; you can redistribute it and/or     */
/*     modify it under the terms of the GNU General Public License as    */
/*     published by the Free Software Foundation; version 2 of           */
/*     the License.                                                      */
/*                                                                       */
/*     This program is distributed in the hope that it will be useful,   */
/*     but WITHOUT ANY WARRANTY; without even the implied warranty of    */ 
/*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
/*     GNU General Public License for more details.                      */
/*                                                                       */
/*     You should have received a copy of the GNU General Public License */
/*     along with this program; if not, write to the Free Software       */
/*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.         */
/* --------------------------------------------------------------------  */


#include <cgx.h>


extern double     gtol;

extern Scale     scale[1];
extern double     *value_n;
extern Summen    anz[1];
extern Edges     *edge;
extern Nodes     *n_anim;

extern Nodes     *node;
extern Datasets *lcase;

extern Alias     *alias;
extern Sets      *set; 
extern Points    *point;
extern Lines     *line;
extern Lcmb      *lcmb;
extern Gsur      *surf;
extern Gbod      *body;
extern Nurbs     *nurbs;
extern SumGeo    anzGeo[1];

extern unsigned char  printFlag;                     /* printf 1:on 0:off */
extern SpecialSet specialset[1];


extern int     num_olc;          /* nr-of-original-loadcases (lc from input-file) */

extern Elements  *e_enqire;     /* feld in dem e_enqire[elem[i].nr].xx abgelegt ist, statt elem[i].xx */



/* shape8h.f -- translated by f2c (version 20000121).
   You must link the resulting object file with the libraries:
	-lf2c -lm   (in that order)
*/
/* Subroutine */ 
int shape8h_(xi, et, ze, xl, xsj)
double *xi, *et, *ze, *xl, *xsj;
{
    static int i__, j, k;
    static double xs[9]	/* was [3][3] */, shp[60]	/* was [3][20]
	     */;


/*     shape functions and derivatives for a 8-node linear isoparametric */
/*     solid element */





/*     shape functions and their glocal derivatives */

/*     local derivatives of the shape functions: xi-derivative */

    /* Parameter adjustments */
    xl -= 4;

    /* Function Body */
    shp[0] = -(1. - *et) * (*ze + 1.) / 8.;
    shp[3] = (1. - *et) * (*ze + 1.) / 8.;
    shp[6] = (1. - *et) * (1. - *ze) / 8.;
    shp[9] = -(1. - *et) * (1. - *ze) / 8.;
    shp[12] = -(*et + 1.) * (*ze + 1.) / 8.;
    shp[15] = (*et + 1.) * (*ze + 1.) / 8.;
    shp[18] = (*et + 1.) * (1. - *ze) / 8.;
    shp[21] = -(*et + 1.) * (1. - *ze) / 8.;

/*     local derivatives of the shape functions: eta-derivative */

    shp[1] = -(1. - *xi) * (*ze + 1.) / 8.;
    shp[4] = -(*xi + 1.) * (*ze + 1.) / 8.;
    shp[7] = -(*xi + 1.) * (1. - *ze) / 8.;
    shp[10] = -(1. - *xi) * (1. - *ze) / 8.;
    shp[13] = (1. - *xi) * (*ze + 1.) / 8.;
    shp[16] = (*xi + 1.) * (*ze + 1.) / 8.;
    shp[19] = (*xi + 1.) * (1. - *ze) / 8.;
    shp[22] = (1. - *xi) * (1. - *ze) / 8.;

/*     local derivatives of the shape functions: zeta-derivative */

    shp[2] = (1. - *xi) * (1. - *et) / 8.;
    shp[5] = (*xi + 1.) * (1. - *et) / 8.;
    shp[8] = -(*xi + 1.) * (1. - *et) / 8.;
    shp[11] = -(1. - *xi) * (1. - *et) / 8.;
    shp[14] = (1. - *xi) * (*et + 1.) / 8.;
    shp[17] = (*xi + 1.) * (*et + 1.) / 8.;
    shp[20] = -(*xi + 1.) * (*et + 1.) / 8.;
    shp[23] = -(1. - *xi) * (*et + 1.) / 8.;

/*     computation of the local derivative of the global coordinates */
/*     (xs) */

    for (i__ = 1; i__ <= 3; ++i__) {
	for (j = 1; j <= 3; ++j) {
	    xs[i__ + j * 3 - 4] = 0.;
	    for (k = 1; k <= 8; ++k) {
		xs[i__ + j * 3 - 4] += xl[i__ + k * 3] * shp[j + k * 3 - 4];
	    }
	}
    }

/*     computation of the jacobian determinant */

    *xsj = xs[0] * (xs[4] * xs[8] - xs[7] * xs[5]) - xs[3] * (xs[1] * xs[8] - 
	    xs[7] * xs[2]) + xs[6] * (xs[1] * xs[5] - xs[4] * xs[2]);

    return(0);
} /* shape8h_ */




/* shape20h.f -- translated by f2c (version 20000121).
   You must link the resulting object file with the libraries:
	-lf2c -lm   (in that order)
*/
/* Subroutine */ 
int shape20h_(xi, et, ze, xl, xsj)
double *xi, *et, *ze, *xl, *xsj;
{
    static int i__, j, k;
    static double xs[9]	/* was [3][3] */, shp[60]	/* was [3][20]
	     */;


/*     shape functions and derivatives for a 20-node quadratic */
/*     isoparametric brick element. Watch out: 0<=xi<=2, -1<=et,ze<=1 !!! */





/*     shape functions and their glocal derivatives */

/*     local derivatives of the shape functions: xi-derivative */

    /* Parameter adjustments */
    xl -= 4;

    /* Function Body */
    shp[0] = *et * (*et * ((double)-.125 - *ze * (double).125) + (double).25 + *
	    ze * (*ze * (double).125 + (double).375)) - (double).125 + *ze * ((
	    double)-.25 - *ze * (double).125) + *xi * (*et * ((double)-.25 - *ze 
	    * (double).25) + (double).25 + *ze * (double).25);
    shp[3] = *et * (*et * (*ze * (double).125 + (double).125) + (double).25 + *
	    ze * ((double).125 - *ze * (double).125)) - (double).375 + *ze * (*
	    ze * (double).125 - (double).25) + *xi * (*et * ((double)-.25 - *ze *
	     (double).25) + (double).25 + *ze * (double).25);
    shp[6] = *et * (*et * ((double).125 - *ze * (double).125) + (double).25 + *
	    ze * ((double)-.125 - *ze * (double).125)) - (double).375 + *ze * (*
	    ze * (double).125 + (double).25) + *xi * (*et * (*ze * (double).25 - 
	    (double).25) + (double).25 - *ze * (double).25);
    shp[9] = *et * (*et * (*ze * (double).125 - (double).125) + (double).25 + *
	    ze * (*ze * (double).125 - (double).375)) - (double).125 + *ze * ((
	    double).25 - *ze * (double).125) + *xi * (*et * (*ze * (double).25 - 
	    (double).25) + (double).25 - *ze * (double).25);
    shp[12] = *et * (*et * ((double)-.125 - *ze * (double).125) - (double).25 + *
	    ze * ((double)-.375 - *ze * (double).125)) - (double).125 + *ze * ((
	    double)-.25 - *ze * (double).125) + *xi * (*et * (*ze * (double).25 
	    + (double).25) + (double).25 + *ze * (double).25);
    shp[15] = *et * (*et * (*ze * (double).125 + (double).125) - (double).25 + *
	    ze * (*ze * (double).125 - (double).125)) - (double).375 + *ze * (*
	    ze * (double).125 - (double).25) + *xi * (*et * (*ze * (double).25 + 
	    (double).25) + (double).25 + *ze * (double).25);
    shp[18] = *et * (*et * ((double).125 - *ze * (double).125) - (double).25 + *
	    ze * (*ze * (double).125 + (double).125)) - (double).375 + *ze * (*
	    ze * (double).125 + (double).25) + *xi * (*et * ((double).25 - *ze * 
	    (double).25) + (double).25 - *ze * (double).25);
    shp[21] = *et * (*et * (*ze * (double).125 - (double).125) - (double).25 + *
	    ze * ((double).375 - *ze * (double).125)) - (double).125 + *ze * ((
	    double).25 - *ze * (double).125) + *xi * (*et * ((double).25 - *ze * 
	    (double).25) + (double).25 - *ze * (double).25);
    shp[24] = *et * ((double)-.5 - *ze * (double).5) + (double).5 + *ze * (double)
	    .5 + *xi * (*et * (*ze * (double).5 + (double).5) - (double).5 - *ze 
	    * (double).5);
    shp[27] = *et * (*ze * (*ze * (double).25) - (double).25) + (double).25 + *
	    ze * (-(*ze) * (double).25);
    shp[30] = *et * (*ze * (double).5 - (double).5) + (double).5 - *ze * (double)
	    .5 + *xi * (*et * ((double).5 - *ze * (double).5) - (double).5 + *ze 
	    * (double).5);
    shp[33] = *et * (*ze * (-(*ze) * (double).25) + (double).25) - (double).25 + 
	    *ze * (*ze * (double).25);
    shp[36] = *et * (*ze * (double).5 + (double).5) + (double).5 + *ze * (double)
	    .5 + *xi * (*et * ((double)-.5 - *ze * (double).5) - (double).5 - *
	    ze * (double).5);
    shp[39] = *et * (*ze * (-(*ze) * (double).25) + (double).25) + (double).25 + 
	    *ze * (-(*ze) * (double).25);
    shp[42] = *et * ((double).5 - *ze * (double).5) + (double).5 - *ze * (double)
	    .5 + *xi * (*et * (*ze * (double).5 - (double).5) - (double).5 + *ze 
	    * (double).5);
    shp[45] = *et * (*ze * (*ze * (double).25) - (double).25) - (double).25 + *
	    ze * (*ze * (double).25);
    shp[48] = *et * (*et * (*ze * (double).25 + (double).25)) - (double).25 - *
	    ze * (double).25;
    shp[51] = *et * (*et * ((double)-.25 - *ze * (double).25)) + (double).25 + *
	    ze * (double).25;
    shp[54] = *et * (*et * (*ze * (double).25 - (double).25)) + (double).25 - *
	    ze * (double).25;
    shp[57] = *et * (*et * ((double).25 - *ze * (double).25)) - (double).25 + *
	    ze * (double).25;

/*     local derivatives of the shape functions: eta-derivative */

    shp[1] = *et * (*ze * (double).5 + (double).5) + *ze * ((double)-.25 - *ze * 
	    (double).25) + *xi * (*et * ((double)-.25 - *ze * (double).25) + (
	    double).25 + *ze * (*ze * (double).125 + (double).375) + *xi * ((
	    double)-.125 - *ze * (double).125));
    shp[4] = *xi * (*et * (*ze * (double).25 + (double).25) + (double).25 + *ze *
	     ((double).125 - *ze * (double).125) + *xi * ((double)-.125 - *ze * (
	    double).125));
    shp[7] = *xi * (*et * ((double).25 - *ze * (double).25) + (double).25 + *ze *
	     ((double)-.125 - *ze * (double).125) + *xi * (*ze * (double).125 - (
	    double).125));
    shp[10] = *et * ((double).5 - *ze * (double).5) + *ze * ((double).25 - *ze * 
	    (double).25) + *xi * (*et * (*ze * (double).25 - (double).25) + (
	    double).25 + *ze * (*ze * (double).125 - (double).375) + *xi * (*ze *
	     (double).125 - (double).125));
    shp[13] = *et * (*ze * (double).5 + (double).5) + *ze * (*ze * (double).25 + 
	    (double).25) + *xi * (*et * ((double)-.25 - *ze * (double).25) - (
	    double).25 + *ze * ((double)-.375 - *ze * (double).125) + *xi * (*ze 
	    * (double).125 + (double).125));
    shp[16] = *xi * (*et * (*ze * (double).25 + (double).25) - (double).25 + *ze 
	    * (*ze * (double).125 - (double).125) + *xi * (*ze * (double).125 + (
	    double).125));
    shp[19] = *xi * (*et * ((double).25 - *ze * (double).25) - (double).25 + *ze 
	    * (*ze * (double).125 + (double).125) + *xi * ((double).125 - *ze * (
	    double).125));
    shp[22] = *et * ((double).5 - *ze * (double).5) + *ze * (*ze * (double).25 - 
	    (double).25) + *xi * (*et * (*ze * (double).25 - (double).25) - (
	    double).25 + *ze * ((double).375 - *ze * (double).125) + *xi * ((
	    double).125 - *ze * (double).125));
    shp[25] = *xi * ((double)-.5 - *ze * (double).5 + *xi * (*ze * (double).25 + 
	    (double).25));
    shp[28] = *xi * (*ze * (*ze * (double).25) - (double).25);
    shp[31] = *xi * (*ze * (double).5 - (double).5 + *xi * ((double).25 - *ze * (
	    double).25));
    shp[34] = *ze * (*ze * (double).5) - (double).5 + *xi * (*ze * (-(*ze) * (
	    double).25) + (double).25);
    shp[37] = *xi * (*ze * (double).5 + (double).5 + *xi * ((double)-.25 - *ze * 
	    (double).25));
    shp[40] = *xi * (*ze * (-(*ze) * (double).25) + (double).25);
    shp[43] = *xi * ((double).5 - *ze * (double).5 + *xi * (*ze * (double).25 - (
	    double).25));
    shp[46] = *ze * (-(*ze) * (double).5) + (double).5 + *xi * (*ze * (*ze * (
	    double).25) - (double).25);
    shp[49] = *et * (*ze * (double)-1. - (double)1.) + *xi * (*et * (*ze * (
	    double).5 + (double).5));
    shp[52] = *xi * (*et * ((double)-.5 - *ze * (double).5));
    shp[55] = *xi * (*et * (*ze * (double).5 - (double).5));
    shp[58] = *et * (*ze * (double)1. - (double)1.) + *xi * (*et * ((double).5 - 
	    *ze * (double).5));

/*     local derivatives of the shape functions: zeta-derivative */

    shp[2] = *et * (*et * (double).25 - (double).25 - *ze * (double).5) + *ze * (
	    double).5 + *xi * (*et * ((double).375 - *et * (double).125 + *ze * (
	    double).25) - (double).25 - *ze * (double).25 + *xi * (*et * (double)
	    -.125 + (double).125));
    shp[5] = *xi * (*et * (*et * (double).125 + (double).125 - *ze * (double).25)
	     - (double).25 + *ze * (double).25 + *xi * (*et * (double)-.125 + (
	    double).125));
    shp[8] = *xi * (*et * ((double)-.125 - *et * (double).125 - *ze * (double)
	    .25) + (double).25 + *ze * (double).25 + *xi * (*et * (double).125 - 
	    (double).125));
    shp[11] = *et * ((double).25 - *et * (double).25 - *ze * (double).5) + *ze * 
	    (double).5 + *xi * (*et * (*et * (double).125 - (double).375 + *ze * 
	    (double).25) + (double).25 - *ze * (double).25 + *xi * (*et * (double)
	    .125 - (double).125));
    shp[14] = *et * (*et * (double).25 + (double).25 + *ze * (double).5) + *ze * 
	    (double).5 + *xi * (*et * ((double)-.375 - *et * (double).125 - *ze *
	     (double).25) - (double).25 - *ze * (double).25 + *xi * (*et * (
	    double).125 + (double).125));
    shp[17] = *xi * (*et * (*et * (double).125 - (double).125 + *ze * (double)
	    .25) - (double).25 + *ze * (double).25 + *xi * (*et * (double).125 + 
	    (double).125));
    shp[20] = *xi * (*et * ((double).125 - *et * (double).125 + *ze * (double)
	    .25) + (double).25 + *ze * (double).25 + *xi * (*et * (double)-.125 
	    - (double).125));
    shp[23] = *et * ((double)-.25 - *et * (double).25 + *ze * (double).5) + *ze *
	     (double).5 + *xi * (*et * (*et * (double).125 + (double).375 - *ze *
	     (double).25) + (double).25 - *ze * (double).25 + *xi * (*et * (
	    double)-.125 - (double).125));
    shp[26] = *xi * (*et * (double)-.5 + (double).5 + *xi * (*et * (double).25 - 
	    (double).25));
    shp[29] = *xi * (*et * (*ze * (double).5) - *ze * (double).5);
    shp[32] = *xi * (*et * (double).5 - (double).5 + *xi * (*et * (double)-.25 + 
	    (double).25));
    shp[35] = *et * (*ze * (double)1.) + *ze * (double)-1. + *xi * (*et * (*ze *
	     (double)-.5) + *ze * (double).5);
    shp[38] = *xi * (*et * (double).5 + (double).5 + *xi * (*et * (double)-.25 - 
	    (double).25));
    shp[41] = *xi * (*et * (*ze * (double)-.5) - *ze * (double).5);
    shp[44] = *xi * (*et * (double)-.5 - (double).5 + *xi * (*et * (double).25 + 
	    (double).25));
    shp[47] = *et * (*ze * (double)-1.) + *ze * (double)-1. + *xi * (*et * (*ze 
	    * (double).5) + *ze * (double).5);
    shp[50] = *et * (*et * (double)-.5) + (double).5 + *xi * (*et * (*et * (
	    double).25) - (double).25);
    shp[53] = *xi * (*et * (*et * (double)-.25) + (double).25);
    shp[56] = *xi * (*et * (*et * (double).25) - (double).25);
    shp[59] = *et * (*et * (double).5) - (double).5 + *xi * (*et * (*et * (
	    double)-.25) + (double).25);

/*     computation of the local derivative of the global coordinates */
/*     (xs) */

    for (i__ = 1; i__ <= 3; ++i__) {
	for (j = 1; j <= 3; ++j) {
	    xs[i__ + j * 3 - 4] = 0.;
	    for (k = 1; k <= 20; ++k) {
		xs[i__ + j * 3 - 4] += xl[i__ + k * 3] * shp[j + k * 3 - 4];
	    }
	}
    }

/*     computation of the jacobian determinant */

    *xsj = xs[0] * (xs[4] * xs[8] - xs[7] * xs[5]) - xs[3] * (xs[1] * xs[8] - 
	    xs[7] * xs[2]) + xs[6] * (xs[1] * xs[5] - xs[4] * xs[2]);

    return 0;
} /* shape20h_ */





/* shape10tet.f -- translated by f2c (version 20000121).
   You must link the resulting object file with the libraries:
	-lf2c -lm   (in that order)
*/
/* Subroutine */ 
int shape10tet_(xi, et, ze, xl, xsj)
double *xi, *et, *ze, *xl, *xsj;
{
    static int i__, j, k;
    static double xs[9]	/* was [3][3] */, shp[30]	/* was [3][10]
	     */;


/*     shape functions and derivatives for a 10-node quadratic */
/*     isoparametric tetrahedral element. 0<=xi,et,ze<=1,xi+et+ze<=1. */





/*     shape functions and their glocal derivatives */

/*     local derivatives of the shape functions: xi-derivative */

    /* Parameter adjustments */
    xl -= 4;

    /* Function Body */
    shp[0] = 1. - (1. - *xi - *et - *ze) * 4.;
    shp[3] = *xi * 4. - 1.;
    shp[6] = 0.;
    shp[9] = 0.;
    shp[12] = (1. - *xi * 2. - *et - *ze) * 4.;
    shp[15] = *et * 4.;
    shp[18] = *et * -4.;
    shp[21] = *ze * -4.;
    shp[24] = *ze * 4.;
    shp[27] = 0.;

/*     local derivatives of the shape functions: eta-derivative */

    shp[1] = 1. - (1. - *xi - *et - *ze) * 4.;
    shp[4] = 0.;
    shp[7] = *et * 4. - 1.;
    shp[10] = 0.;
    shp[13] = *xi * -4.;
    shp[16] = *xi * 4.;
    shp[19] = (1. - *xi - *et * 2. - *ze) * 4.;
    shp[22] = *ze * -4.;
    shp[25] = 0.;
    shp[28] = *ze * 4.;

/*     local derivatives of the shape functions: zeta-derivative */

    shp[2] = 1. - (1. - *xi - *et - *ze) * 4.;
    shp[5] = 0.;
    shp[8] = 0.;
    shp[11] = *ze * 4. - 1.;
    shp[14] = *xi * -4.;
    shp[17] = 0.;
    shp[20] = *et * -4.;
    shp[23] = (1. - *xi - *et - *ze * 2.) * 4.;
    shp[26] = *xi * 4.;
    shp[29] = *et * 4.;

/*     computation of the local derivative of the global coordinates */
/*     (xs) */

    for (i__ = 1; i__ <= 3; ++i__) {
	for (j = 1; j <= 3; ++j) {
	    xs[i__ + j * 3 - 4] = 0.;
	    for (k = 1; k <= 10; ++k) {
		xs[i__ + j * 3 - 4] += xl[i__ + k * 3] * shp[j + k * 3 - 4];
	    }
	}
    }

/*     computation of the jacobian determinant */

    *xsj = xs[0] * (xs[4] * xs[8] - xs[7] * xs[5]) - xs[3] * (xs[1] * xs[8] - 
	    xs[7] * xs[2]) + xs[6] * (xs[1] * xs[5] - xs[4] * xs[2]);

    return 0;
} /* shape10tet_ */




/* e_c3d.f -- translated by f2c (version 20000121).
   You must link the resulting object file with the libraries:
	-lf2c -lm   (in that order)
*/
/* Subroutine */ 
int e_c3d__(double *xl,unsigned char *elty)
{
    /* Initialized data */

    static double gauss3d1[3]	/* was [3][1] */ = { 0.,0.,0. };
    static double gauss3d2[24]	/* was [3][8] */ = { 
	    -.577350269189626,-.577350269189626,.577350269189626,
	    .577350269189626,-.577350269189626,.577350269189626,
	    -.577350269189626,-.577350269189626,-.577350269189626,
	    .577350269189626,-.577350269189626,-.577350269189626,
	    -.577350269189626,.577350269189626,.577350269189626,
	    .577350269189626,.577350269189626,.577350269189626,
	    -.577350269189626,.577350269189626,-.577350269189626,
	    .577350269189626,.577350269189626,-.577350269189626 };
    static double gauss3d3[81]	/* was [3][27] */ = { 
	    -.774596669241483,-.774596669241483,.774596669241483,0.,
	    -.774596669241483,.774596669241483,.774596669241483,
	    -.774596669241483,.774596669241483,-.774596669241483,
	    -.774596669241483,0.,0.,-.774596669241483,0.,.774596669241483,
	    -.774596669241483,0.,-.774596669241483,-.774596669241483,
	    -.774596669241483,0.,-.774596669241483,-.774596669241483,
	    .774596669241483,-.774596669241483,-.774596669241483,
	    -.774596669241483,0.,.774596669241483,0.,0.,.774596669241483,
	    .774596669241483,0.,.774596669241483,-.774596669241483,0.,0.,0.,
	    0.,0.,.774596669241483,0.,0.,-.774596669241483,0.,
	    -.774596669241483,0.,0.,-.774596669241483,.774596669241483,0.,
	    -.774596669241483,-.774596669241483,.774596669241483,
	    .774596669241483,0.,.774596669241483,.774596669241483,
	    .774596669241483,.774596669241483,.774596669241483,
	    -.774596669241483,.774596669241483,0.,0.,.774596669241483,0.,
	    .774596669241483,.774596669241483,0.,-.774596669241483,
	    .774596669241483,-.774596669241483,0.,.774596669241483,
	    -.774596669241483,.774596669241483,.774596669241483,
	    -.774596669241483 };
    static double gauss3d4[12]	/* was [3][4] */ = { .138196601125011,
	    .138196601125011,.138196601125011,.585410196624968,
	    .138196601125011,.138196601125011,.138196601125011,
	    .585410196624968,.138196601125011,.138196601125011,
	    .138196601125011,.585410196624968 };

    /* System generated locals */
    int i__1;

    /* Builtin functions */
    int s_cmp();

    /* Local variables */
    static int nope;
    extern /* Subroutine */ int shape10tet_();
    static int mint3d, kk;
    static double et, ze, xi;
    extern /* Subroutine */ int shape8h_(), shape20h_();
    static double xsj;

/*     contains Gauss point information */

/*     gauss3d1: hex, 1-point integration (1 integration point) */
/*     gauss3d2: hex, 2-point integration (8 integration points) */
/*     gauss3d3: hex, 3-point integration (27 integration points) */
/*     gauss3d4: tet, 4 integration points */

    /* Parameter adjustments */
    xl -= 4;

    /* Function Body */

/*     number of nodes per element */

      if((compare(elty,"C3D20R",6)==6)||(compare(elty,"C3D20",6)==6)) 
        nope=20;
      else if((compare(elty,"C3D8R",5)==5)||(compare(elty,"C3D8",4)==4))
        nope=8;
      else
         nope=10;

/*     number of integration points per element */

    if (compare(elty,"C3D8R",5)==5)
      mint3d = 1;
    else if ((compare(elty,"C3D8",4)==4)||(compare(elty,"C3D20R",6)==6))
      mint3d = 8;
    else if (compare(elty,"C3D20",5)==5)
      mint3d = 27;
    else
      mint3d = 4;

/*     computation of the matrix: loop over the Gauss points */

    i__1 = mint3d;
    for (kk = 1; kk <= i__1; ++kk) {
	if (compare(elty,"C3D8R",5)==5)
	{
	    xi = gauss3d1[kk * 3 - 3];
	    et = gauss3d1[kk * 3 - 2];
	    ze = gauss3d1[kk * 3 - 1];
	}
        else if ((compare(elty,"C3D8",4)==4)|| (compare(elty,"C3D20R",6)==6))
        {
	    xi = gauss3d2[kk * 3 - 3];
	    if (nope == 20)
            {
		xi += 1.;
	    }
	    et = gauss3d2[kk * 3 - 2];
	    ze = gauss3d2[kk * 3 - 1];
	}
        else if (compare(elty,"C3D20",5)==5)
        {
	    xi = gauss3d3[kk * 3 - 3] + 1.;
	    et = gauss3d3[kk * 3 - 2];
	    ze = gauss3d3[kk * 3 - 1];
	}
        else if (compare(elty,"C3D10",5)==5)
        {
	    xi = gauss3d4[kk * 3 - 3];
	    et = gauss3d4[kk * 3 - 2];
	    ze = gauss3d4[kk * 3 - 1];
	}

/*           calculation of the shape functions and their derivatives */
/*           in the gauss point */

/*           xl contains the coordinates of the nodes belonging to */
/*           the element */

	if (nope == 20) {
	    shape20h_(&xi, &et, &ze, &xl[4], &xsj);
	} else if (nope == 8) {
	    shape8h_(&xi, &et, &ze, &xl[4], &xsj);
	} else {
	    shape10tet_(&xi, &et, &ze, &xl[4], &xsj);
	}

/*           check the jacobian determinant */
/* printf("%s %d %d xi:%e et:%e xsj:%e\n", elty, mint3d, kk, xi,et,xsj);  */

	if (xsj < 1e-20) {
	    return(0);
	}

    }

    return(1);
} /* e_c3d__ */


/* check all angles if lower than MIN_ANGLE_TRI3 */
int checktr7(int elem)
{
  int i;
  double p0[3], p0p1[3], p2p0[3], p1p2[3], p1[3], p2[3], sp[3];

  p0[0]=node[e_enqire[elem].nod[0]].nx;
  p0[1]=node[e_enqire[elem].nod[0]].ny;
  p0[2]=node[e_enqire[elem].nod[0]].nz;
  p1[0]=node[e_enqire[elem].nod[1]].nx;
  p1[1]=node[e_enqire[elem].nod[1]].ny;
  p1[2]=node[e_enqire[elem].nod[1]].nz;
  v_result(p0,p1,p0p1);
  p0[0]=node[e_enqire[elem].nod[2]].nx;
  p0[1]=node[e_enqire[elem].nod[2]].ny;
  p0[2]=node[e_enqire[elem].nod[2]].nz;
  v_result(p1,p0,p1p2);
  p1[0]=node[e_enqire[elem].nod[0]].nx;
  p1[1]=node[e_enqire[elem].nod[0]].ny;
  p1[2]=node[e_enqire[elem].nod[0]].nz;
  v_result(p0,p1,p2p0);
  v_norm(p0p1,p0);
  v_norm(p1p2,p1);
  v_norm(p2p0,p2);


  sp[0]=v_sprod(p0,p1);
  sp[1]=v_sprod(p1,p2);
  sp[2]=v_sprod(p2,p0);
  for (i=0; i<3; i++) if(sp[i]*sp[i]>MIN_ANGLE_TRI3) return(i+1);

  return(0);
}


int calcBadElements(unsigned char *setname)
{
  int i,j,n;
  int   setNr, nodeset;
  double xl[20][3];
  unsigned char elty[MAX_LINE_LENGTH];

  operateAlias( setname, "se" );
  setNr=getSetNr(setname);
  if( set[setNr].name == (unsigned char *)NULL )
  {
    errMsg(" ERROR: setNr:%d is undefined\n", setNr);
    return(-1);
  }

  delSet(specialset->njby);
  nodeset=pre_seta( specialset->njby, "i", 0);
  set[nodeset].anz_e=0;

  /* cycle through all elements in the set and check the jacobian matrix */

  for (i=0; i<set[setNr].anz_e; i++)
  {
    if(e_enqire[set[setNr].elem[i]].type == 1)
    {
      for(j=0; j<8; j++)
      { 
        xl[j][0]= node[e_enqire[set[setNr].elem[i]].nod[j]].nx;
        xl[j][1]= node[e_enqire[set[setNr].elem[i]].nod[j]].ny;
        xl[j][2]= node[e_enqire[set[setNr].elem[i]].nod[j]].nz;
      }
      strcpy(elty,"C3D8");
      if (!e_c3d__(&xl[0][0], elty)) seta( nodeset, "e", set[setNr].elem[i]);
      strcpy(elty,"C3D8R");
      if (!e_c3d__(&xl[0][0], elty)) seta( nodeset, "e", set[setNr].elem[i]);
    }
    else if(e_enqire[set[setNr].elem[i]].type == 4)
    {
      for(j=0; j<12; j++)
      { 
        xl[j][0]= node[e_enqire[set[setNr].elem[i]].nod[j]].nx;
        xl[j][1]= node[e_enqire[set[setNr].elem[i]].nod[j]].ny;
        xl[j][2]= node[e_enqire[set[setNr].elem[i]].nod[j]].nz;
      }
      for(n=16; n<20; n++)
      { 
        xl[j][0]= node[e_enqire[set[setNr].elem[i]].nod[n]].nx;
        xl[j][1]= node[e_enqire[set[setNr].elem[i]].nod[n]].ny;
        xl[j][2]= node[e_enqire[set[setNr].elem[i]].nod[n]].nz;
        j++;
      }
      for(n=12; n<16; n++)
      { 
        xl[j][0]= node[e_enqire[set[setNr].elem[i]].nod[n]].nx;
        xl[j][1]= node[e_enqire[set[setNr].elem[i]].nod[n]].ny;
        xl[j][2]= node[e_enqire[set[setNr].elem[i]].nod[n]].nz;
        j++;
      }
      strcpy(elty,"C3D20");
      if (!e_c3d__(&xl[0][0], elty)) seta( nodeset, "e", set[setNr].elem[i]);
      strcpy(elty,"C3D20R");
      if (!e_c3d__(&xl[0][0], elty)) seta( nodeset, "e", set[setNr].elem[i]);
    }
    else if(e_enqire[set[setNr].elem[i]].type == 6)
    {
      for(j=0; j<10; j++)
      { 
        xl[j][0]= node[e_enqire[set[setNr].elem[i]].nod[j]].nx;
        xl[j][1]= node[e_enqire[set[setNr].elem[i]].nod[j]].ny;
        xl[j][2]= node[e_enqire[set[setNr].elem[i]].nod[j]].nz;
      }
      strcpy(elty,"C3D10");
      if (!e_c3d__(&xl[0][0], elty)) seta( nodeset, "e", set[setNr].elem[i]);
    }
    else if(e_enqire[set[setNr].elem[i]].type == 7)
    {
      if (checktr7(set[setNr].elem[i])) seta( nodeset, "e", set[setNr].elem[i]);      
    }
  }
  return(set[nodeset].anz_e);
}




