readpatran.c

Go to the documentation of this file.
00001 /*
00002     This file is part of magpar.
00003 
00004     Copyright (C) 2002-2010 Werner Scholz
00005 
00006     www:   http://www.magpar.net/
00007     email: magpar(at)magpar.net
00008 
00009     magpar is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     magpar is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU General Public License for more details.
00018 
00019     You should have received a copy of the GNU General Public License
00020     along with magpar; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 static char const Id[] = "$Id: readpatran.c 2962 2010-02-04 19:50:44Z scholz $\n\n";
00025 static char const Td[] = "$Today: " __FILE__ "  " __DATE__ "  "  __TIME__  " $\n\n";
00026 
00027 #include "magpario.h"
00028 
00029 /* Reads in the grid data from a Patran Neutral file */
00030 
00031 int ReadPatran(GridData *gdata)
00032 {
00033   int          i;
00034 
00035   char         msg[256],fmesh[256];
00036   FILE         *fd;
00037 
00038   int          a1,a2,a3,a4;
00039   int          it, id, iv, kc, n1, n2, n3, n4, n5;
00040 
00041 
00042   MagparFunctionLogBegin;
00043 
00044   int rank,size;
00045   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
00046   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
00047 
00048   if (rank) {
00049     gdata->ln_vert=0;
00050     gdata->ln_ele=0;
00051 
00052     MagparFunctionInfoReturn(0);
00053   }
00054 
00055   /* TODO: read filename directly from option */
00056   ierr = PetscSNPrintf(fmesh,255,"%s%s",gdata->simname,".out");CHKERRQ(ierr);
00057   ierr = PetscFOpen(PETSC_COMM_WORLD,fmesh,"r",&fd);CHKERRQ(ierr);
00058   if (!rank && !fd) {
00059     SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open %s\n",fmesh);
00060   }
00061   PetscPrintf(PETSC_COMM_WORLD,"Opening file %s\n",fmesh);
00062 
00063   /* read in number of vertices */
00064   fscanf(fd,"%i %i %i %i %i %i %i %i %i\n",
00065     &it,&id,&iv,&kc,&n1,&n2,&n3,&n4,&n5
00066   );
00067   assert(it==25); /* packet type 25: title card */
00068   assert(id==0);
00069   assert(iv==0);
00070   assert(kc==1);
00071 
00072   fgets(msg,256,fd);
00073   PetscPrintf(PETSC_COMM_WORLD,"Title: %s\n",msg);
00074 
00075   fscanf(fd,"%i %i %i %i %i %i %i %i %i\n",
00076     &it,&id,&iv,&kc,&n1,&n2,&n3,&n4,&n5
00077   );
00078   assert(it==26); /* packet type 26: summary data */
00079   assert(id==0);
00080   assert(iv==0);
00081   assert(kc==1);
00082 
00083   gdata->n_vert=n1;
00084   gdata->ln_vert=gdata->n_vert;
00085   PetscPrintf(PETSC_COMM_WORLD,"Number of grid vertices: %i\n",gdata->n_vert);
00086   gdata->n_ele=n2;
00087   gdata->ln_ele=gdata->n_ele;
00088   PetscPrintf(PETSC_COMM_WORLD,"Number of grid elements: %i\n",gdata->n_ele);
00089   PetscPrintf(PETSC_COMM_WORLD,"Number of materials:     %i\n",n3);
00090   gdata->n_prop=n4;
00091   assert(gdata->n_prop>0);
00092   PetscPrintf(PETSC_COMM_WORLD,"Number of element prop.: %i\n",gdata->n_prop);
00093   PetscPrintf(PETSC_COMM_WORLD,"Number of coord. frames: %i\n",n5);
00094 
00095   fgets(msg,256,fd);
00096   PetscPrintf(PETSC_COMM_WORLD,"Neutral file creation date, Patran Version:\n%s\n",msg);
00097 
00098   /* *************************************************************************
00099      Read in all vertices
00100   */
00101 
00102   ierr = PetscMalloc(ND*gdata->n_vert*sizeof(PetscReal),&gdata->vertxyz);CHKERRQ(ierr);
00103 
00104   /* read data of all vertices */
00105   for (i=0; i<gdata->n_vert; i++) {
00106     fscanf(fd,"%i %i %i %i %i %i %i %i %i\n",
00107       &it,&id,&iv,&kc,&n1,&n2,&n3,&n4,&n5
00108     );
00109     assert(it==1); /* packet type 01: node data */
00110     assert(iv==0);
00111     assert(kc==2);
00112 
00113     /* assert ascending ordering without holes */
00114     assert(id-1==i);
00115 
00116 #ifdef PETSC_USE_SINGLE
00117     fscanf(fd,"%f %f %f\n",
00118 #else
00119     fscanf(fd,"%lf %lf %lf\n",
00120 #endif
00121       gdata->vertxyz+ND*i+0,
00122       gdata->vertxyz+ND*i+1,
00123       gdata->vertxyz+ND*i+2
00124     );
00125 
00126     fgets(msg,256,fd);
00127   }
00128 
00129   /* *************************************************************************
00130      Read in all elements
00131   */
00132   ierr = PetscMalloc(NV*gdata->n_ele*sizeof(int),&gdata->elevert);CHKERRQ(ierr);
00133   ierr = PetscMalloc(gdata->n_ele*sizeof(int),&gdata->eleprop);CHKERRQ(ierr);
00134 
00135   for (i=0; i<gdata->n_ele; i++) {
00136     fscanf(fd,"%i %i %i %i %i %i %i %i %i\n",
00137       &it,&id,&iv,&kc,&n1,&n2,&n3,&n4,&n5
00138     );
00139 
00140     assert(it==2); /* packet type 02: element data */
00141     assert(kc==2);
00142     assert(n1==0);
00143 
00144     /* assume tet elements */
00145     assert(iv==5);
00146 
00147     /* assert ascending ordering without holes */
00148 
00149     /* does not work due to unknown offset in Patran's element numbers
00150      * and does not really matter anyway (element numbering not interesting)
00151 
00152     assert(t_cnt==id-2);
00153     t_cnt=id-1;
00154     */
00155 
00156     fscanf(fd,"%i %i %i %i",&a1,&a2,&a3,&a4); /* a3: property id */
00157     assert(a1==NV); /* number of nodes per element */
00158 
00159     /* internal property ids start with 0 !!! */
00160     gdata->eleprop[i]=--a3;
00161     assert(gdata->eleprop[i]>=0);
00162     assert(gdata->eleprop[i]<gdata->n_prop);
00163 
00164     fgets(msg,256,fd);
00165     fscanf(fd,"%i %i %i %i\n",
00166       gdata->elevert+i*NV+0,
00167       gdata->elevert+i*NV+1,
00168       gdata->elevert+i*NV+2,
00169       gdata->elevert+i*NV+3
00170     );
00171     gdata->elevert[i*NV+0]--;
00172     gdata->elevert[i*NV+1]--;
00173     gdata->elevert[i*NV+2]--;
00174     gdata->elevert[i*NV+3]--;
00175   }
00176 
00177   ierr = PetscFClose(PETSC_COMM_WORLD,fd);CHKERRQ(ierr);
00178 
00179 
00180   MagparFunctionLogReturn(0);
00181 }
00182 

magpar - Parallel Finite Element Micromagnetics Package
Copyright (C) 2002-2010 Werner Scholz