⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quadcg.c

📁 工程中有限元程序,采用C语言编制,包括所有经典的有限元问题!
💻 C
📖 第 1 页 / 共 2 页
字号:
/********     program quadcg        **********/
/*     2-d stress analysis using 4-node      */
/*  quadrilateral elements with temperature  */
/*         conjugate gradient approach       */
/*    t.r.chandrupatla and a.d.belegundu     */
/*********************************************/
#include <stdio.h>
#include <math.h>
main()
{
   FILE *fptr1, *fptr2;
   int n,i,j,k,m,i1,i2,i3,ii,jj,m1,nmin,nmax,nrt,nct,it,jt;
   int ip,nr,nc,in;
   char dummy[81], title[81], file1[81], file2[81], file3[81];
   int ne,nn,nq,nm,nd,nl,nen,ndn,ndim,npr,nmpc,lc,ipl;
   int *noc, *nu, *mat, *mpc;
   double *x, *thick, *pm, *u, *tempr, *s, *f, *beta;
   double *ad, *dd, *gg, *q;
   double xi,eta,th,bt,qe[8],c1,sv;
   double c, dj, al, pnu, tld, cnst, reaction, s1, s2, s3, ang, r;
   double b[3][8],d[3][3],db[3][8],str[3],tl[8],xni[4][2];
/*-------------------------------------------------------*/
   printf("\n");
   puts("Input file name < dr:fn.ext >: ");
   gets(file1);
   puts("Output file name < dr:fn.ext >: ");
   gets(file2);
   printf("\n");
   printf("  1) plane stress analysis\n");
   printf("  2) plane strain analysis\n");
   printf("     choose <1 or 2>  ");
   scanf("%d", &lc);
   if (lc < 1 || lc > 2)
      lc = 1;
   fptr1 = fopen(file1, "r");
   fgets(dummy,80,fptr1);
   fgets(title,80,fptr1);
   fgets(dummy,80,fptr1);
   fscanf(fptr1,"%d %d %d %d %d %d\n", &nn, &ne, &nm, &ndim, &nen, &ndn);
   fgets(dummy, 80, fptr1);
   fscanf(fptr1,"%d %d %d %d %d\n", &nd, &nl, &nmpc);
   npr = 3;  /* Material properties E, Nu, Alpha */
/* ----- memory allocation ----- */
   x = (double *) calloc(nn*ndim, sizeof(double));
   noc = (int *) calloc(ne*nen, sizeof(int));
   u = (double *) calloc(nd, sizeof(double));
   nu = (int *) calloc(nd, sizeof(int));
   mat = (int *) calloc(ne,sizeof(int));
   thick = (double *) calloc(ne, sizeof(double));
   f = (double *) calloc(nn*ndn, sizeof(double));
   ad = (double *) calloc(nn*ndn, sizeof(double));
   dd = (double *) calloc(nn*ndn, sizeof(double));
   gg = (double *) calloc(nn*ndn, sizeof(double));
   q = (double *) calloc(nn*ndn, sizeof(double));
   tempr = (double *) calloc(ne, sizeof(double));
   pm = (double *) calloc(nm*npr, sizeof(double));
   mpc = (int *) calloc(2*nmpc, sizeof(int));
   beta = (double *) calloc(3*nmpc, sizeof(double));
/* ----- allocate memory for stiffness ----- */
   s = (double *) calloc(ne*8*8, sizeof(double));
   printf("\n\n    PLOT CHOICE\n");
   printf("  1) no plot data\n");
   printf("  2) create data file for in-plane shear stress\n");
   printf("  3) create data file for von mises stress\n");
   printf("     choose <1 or 2 or 3> ");
   scanf("%d%*c", &ipl);
   if(ipl < 1 || ipl > 3)
     ipl = 1;         /* --- default is no data ---*/
   if(ipl > 1){
      printf("Output file name < dr:fn.ext >:\n");
      gets(file3);
      }
/* ----- total dof is  nq ----- */
     nq = ndn * nn;
/* ===============  read data  ==================== */
/* ----- coordinates ----- */
     fgets(dummy,80,fptr1);
     for (i = 0; i < nn; i++){
        fscanf(fptr1, "%d", &n);
        for (j = 0; j < ndim; j++){
		fscanf(fptr1, "%lf\n", &c);
                x[ndim*(n-1)+j] = c;
            }
         }
/* ----- connectivity, material, thickness, temp-change ----- */
   fgets(dummy,80,fptr1);
   for (i = 0; i < ne; i++) {
       fscanf(fptr1,"%d", &n);
       for (j = 0; j < nen; j++) {
           fscanf(fptr1,"%d", &k);
           noc[(n-1)*nen+j]=k;
       }
       fscanf(fptr1,"%d", &k);
       mat[n-1] = k;
       fscanf(fptr1,"%lf",&c);
       thick[n-1] = c;
       fscanf(fptr1,"%lf\n",&c);
       tempr[n-1] = c;
   }
/* ----- displacement bc  ----- */
   fgets(dummy,80,fptr1);
   for (i = 0; i < nd; i++) {
      fscanf(fptr1, "%d %lf\n", &k, &c);
      nu[i] = k;
      u[i] = c;
   }
/* ----- component loads ----- */
   fgets(dummy,80,fptr1);
   for (i = 0; i < nl; i++) {
      fscanf(fptr1, "%d %lf\n", &k, &c);
      f[k-1] = c;
   }
/* ----- material properties ----- */
   fgets(dummy,80,fptr1);
   for (i = 0; i < nm; i++){
      fscanf(fptr1, "%d", &k);
      for (j = 0; j < npr; j++) {
         fscanf(fptr1, "%lf\n", &c);
	 pm[(k-1)*npr+j] = c;
      }
   }
/* ----- multipoint constraints ----- */
   if (nmpc > 0) 
      { fgets(dummy,80,fptr1);
	for(j=0;j<nmpc;j++){
	   fscanf(fptr1,"%lf",&c);
	   beta[3*j]=c;
	   fscanf(fptr1,"%d",&k);
	   mpc[2*j]=k;
           fscanf(fptr1,"%lf",&c);
	   beta[3*j+1]=c;
           fscanf(fptr1,"%d",&k);
           mpc[2*j+1]=k;
	   fscanf(fptr1,"%lf",&c);
	   beta[3*j+2]=c;
	   }
	}
   fclose (fptr1);
/* ----- global stiffness matrix -----*/
/* ----- corner nodes and integrationpoints ----- */
    integ(xni);
    for (n = 0; n < ne; n++) {
	printf("forming stiffness matrix of element %d\n", n+1);
	dmatrix(n,pm,mat,npr,&pnu,&al,lc,d);

	/* --- element stiffness --- */
	elstif(n,lc,s,tl,xni,d,thick,tempr,x,al,pnu,noc);
	printf (".... placing in global locations\n");
	for (ii = 0; ii < nen; ii++) {
	   nrt = ndn * (noc[nen*n + ii] - 1);
	   for (it = 0; it < ndn; it++) {
	      nr = nrt + it;
	      i = ndn * ii + it;
	      f[nr] = f[nr] + tl[i];
	      gg[nr] = gg[nr] + s[64*n + 8*i + i];
	      }
	   }
    }

    /* ----- decide penalty parameter cnst ----- */
/* ----- GG() diagonal stiffness summation   */
     cnst = 0.;
     for (i = 0; i < nq; i++) {
	 if (cnst < gg[i])
	    cnst = gg[i];
	 }
     cnst = cnst * 10000.;
/* ----- Modify right hand side F() for Boundary Conditions ----- */
/* ----- Displacement BC */
   for (i = 0; i < nd; i++) {
      k = nu[i];
      f[k-1] = f[k-1] + cnst * u[i];
   }
/* ----- modify for multipoint constraints ----- */
   for (i = 0; i < nmpc; i++){
       i1 = mpc[2*i]-1;
       i2 = mpc[2*i+1]-1;
       f[i1] = f[i1] + cnst*beta[3*i]*beta[3*i+2];
       f[i2] = f[i2] + cnst*beta[3*i+1]*beta[3*i+2];
       }

/* ----- solution of equations using conjgate gradient method ----- */
   cgsolve(s,f,q,ad,dd,gg,noc,cnst,nu,mpc,nmpc,beta,nd,nq,ne);
/* ----- printing displacements ----- */
   fptr1 = fopen(file2, "w");
   printf("\n%s\n", title);
   fprintf(fptr1, "\n%s\n", title);
     if (lc == 1)
         fprintf(fptr1, "plane stress analysis\n");
     if (lc == 2)
         fprintf(fptr1, "plane strain analysis\n");
     fprintf(fptr1, "node#     x-displ      y-displ\n");
     printf ("node#     x-displ      y-displ\n");
     for (i = 0; i < nn; i++) {
	 printf(" %4d  %11.4e  %11.4e\n",i+1,q[2*i],q[2*i+1]);
	 fprintf(fptr1," %4d  %11.4e  %11.4e\n",i+1,q[2*i],q[2*i+1]);
	 }
/* ----- reaction calculation ----- */
   printf("node#    reaction\n");
   fprintf(fptr1, "node#     reaction\n");
   for (i = 0; i < nd; i++) {
      k = nu[i];
      reaction = cnst * (u[i] - q[k-1]);
      printf(" %4d  %11.4e\n", k, reaction);
      fprintf(fptr1, " %4d  %11.4e\n", k, reaction);
      }
     if (ipl > 1){
        fptr2 = fopen(file3, "w");
        if (ipl == 2)
	    fprintf(fptr2, "max. in-plane Shear Stress");
	    if (ipl == 3)
	       fprintf(fptr2, "von Mises stress");
	    fprintf(fptr2, "(element) for data in file  %s\n", file1);
        }
/* -----  stress calculations ----- */
     fprintf (fptr1, "elem#    von mises stresses at 4 integration points\n");
   /* ----- stresses at integration points ----- */
     for (n = 0; n < ne; n++) {
        fprintf (fptr1, "%4d ", n+1);
        for (ip = 0; ip < 4; ip++) {
	   xi = xni[ip][0];
	   eta = xni[ip][1];
	   dmatrix(n,pm,mat,npr,&pnu,&al,lc,d);
	   dbmat(n,x,noc,thick,&th,d,b,db,&dj,xi,eta);
	   /* --- stress evaluation --- */

	   for (i = 0; i < nen; i++) {
	      in = ndn * (noc[nen*n+i] - 1);
	      ii = ndn * i;
	      for (j = 0; j < ndn; j++) {
		 qe[ii + j] = q[in + j];
		 }
	      }
	   c1 = al * tempr[n];
	   if (lc == 2)
              c1 = c1 * (1 + pnu);

           for (i = 0; i < 3; i++) {
              c = 0;
              for (k = 0; k < 8; k++) {
                  c = c + db[i][k] * qe[k];
                  }
                 str[i] = c - c1 * (d[i][0] + d[i][1]);
              }
        /* --- von mises stress at integration point --- */
           c = 0;
	   if (lc == 2)
	      c = pnu * (str[0] + str[1]);
	   c1 = (str[0] - str[1]) * (str[0] - str[1]);
	   c1 = c1 + (str[1] - c) * (str[1] - c);
	   c1 = c1 + (c - str[0]) * (c - str[0]);
      	   sv = sqrt((double)(.5 * c1 + 3 * str[2] * str[2]));
	   fprintf(fptr1, " %10.4e ", sv);
        /* --- maximum shear stress r --- */
           c = .25 * (str[0]-str[1])*(str[0]-str[1]);
           c = c + str[2]*str[2];
	   r = sqrt((double) c);
	   if (ipl == 2)
              fprintf(fptr2," %f ", r);
	   if (ipl == 3)
	      fprintf(fptr2, " %f ", sv);
	}   
	   fprintf(fptr1, "\n");
	   if (ipl > 1)
	      fprintf(fptr2, "\n");
    }
     fclose(fptr1);
     printf("complete results are in file %s\n", file2);
     printf("view using a text processor\n");

     if (ipl > 1) {
        fclose(fptr2);
        printf("element stress data in file %s\n", file3);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -