writedataavs.c

Go to the documentation of this file.
00001 /*
00002     This file is part of magpar.
00003 
00004     Copyright (C) 2002-2009 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: writedataavs.c 2681 2009-07-31 04:30:53Z scholz $\n\n";
00025 static char const Td[] = "$Today: " __FILE__ "  " __DATE__ "  "  __TIME__  " $\n\n";
00026 
00027 #include "magpario.h"
00028 #include "util/util.h"
00029 
00030 #include <errno.h>
00031 
00032 #define NEDATA  0
00033 #define NEDATA2 0
00034 
00035 #if defined(ZLIB) && !defined(_WIN32) && !defined(__CYGWIN32__)
00036 EXTERN_C_BEGIN
00037 #include "zlib.h"
00038 EXTERN_C_END
00039 
00040 #define FPRINTF2(a,b)   gzprintf(a,b)
00041 #define FPRINTF3(a,b,c) gzprintf(a,b,c)
00042 #else
00043 #define FPRINTF2(a,b)   PetscFPrintf(PETSC_COMM_WORLD,a,b)
00044 #define FPRINTF3(a,b,c) PetscFPrintf(PETSC_COMM_WORLD,a,b,c)
00045 #endif
00046 
00047 #define D_FLT 50
00048 #define D_EID 8
00049 
00053 /* Writes the grid in AVS format */
00054 
00055 int WriteAVS(GridData *gdata)
00056 {
00057   MagparFunctionInfoBegin;
00058 
00059   int rank,size;
00060   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
00061   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
00062 
00063 #ifdef EXCH
00064 #define nvdata 15
00065 #else
00066 #define nvdata 12
00067 #endif
00068 
00069   int nmax;
00070   /* add 1 so we allocate at least once char even if gdata->ln_ele==0 */
00071   nmax=PetscMax(gdata->ln_ele,gdata->ln_vert)*(D_EID+nvdata*D_FLT)+1;
00072 
00073   char *wbufs,*wbufe;
00074   ierr = PetscMalloc(nmax*sizeof(char),&wbufs);CHKERRQ(ierr);
00075   wbufe=wbufs;
00076 
00077   FILE *fd;
00078   fd=NULL;
00079 
00080   char fmesh[256];
00081 #if defined(ZLIB) && !defined(_WIN32) && !defined(__CYGWIN32__)
00082   ierr = PetscSNPrintf(fmesh,255,"%s.%04i.gz",gdata->simname,gdata->inp);CHKERRQ(ierr);
00083   if (!rank) {
00084     fd = (FILE*) gzopen(fmesh, "wb");
00085     if (fd == NULL) {
00086       SETERRQ2(PETSC_ERR_FILE_OPEN,"Cannot open %s error %i\n",fmesh,errno);
00087     }
00088   }
00089 #else
00090   ierr = PetscSNPrintf(fmesh,255,"%s.%04i.inp",gdata->simname,gdata->inp);CHKERRQ(ierr);
00091   ierr = PetscFOpen(PETSC_COMM_WORLD,fmesh,"w",&fd);CHKERRQ(ierr);
00092   if (!rank && !fd) {
00093     SETERRQ2(PETSC_ERR_FILE_OPEN,"Cannot open %s error %i\n",fmesh,ierr);
00094   }
00095 #endif
00096 
00097   PetscPrintf(PETSC_COMM_WORLD,"Opening file %s\n",fmesh);
00098 
00099 #if !defined(ZLIB) || defined(_WIN32) || defined(__CYGWIN32__)
00100     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"%i %i %i %i %i\n",
00101       gdata->n_vert,gdata->n_ele,nvdata,NEDATA,NEDATA2);CHKERRQ(ierr);
00102 
00103     for (int i=0; i<gdata->n_vert; i++) {
00104       ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,
00105         "%i %g %g %g\n",
00106         i+1,
00107         PetscRealPart(gdata->vertxyz[ND*i+0]),
00108         PetscRealPart(gdata->vertxyz[ND*i+1]),
00109         PetscRealPart(gdata->vertxyz[ND*i+2])
00110       );CHKERRQ(ierr);
00111     }
00112 
00113     for (int i=0; i<gdata->ln_ele; i++) {
00114       int nchars;
00115       nchars=
00116       snprintf(
00117         wbufe,
00118         nmax-(wbufe-wbufs),
00119         "%i %i tet %i %i %i %i\n",
00120         gdata->elel2g[i]+1,
00121         gdata->eleprop[i]+1,
00122         gdata->elevert[NV*i+0]+1,
00123         gdata->elevert[NV*i+1]+1,
00124         gdata->elevert[NV*i+2]+1,
00125         gdata->elevert[NV*i+3]+1
00126       );
00127       /* check for problems of snprintf */
00128       assert(nchars>0);
00129 
00130       /* update pointer to the end of the array */
00131       wbufe += nchars;
00132 
00133       /* check if we write beyond allocated area */
00134       assert(wbufe-wbufs < nmax);
00135     }
00136     SynchronizedFastFPrintf(fd,wbufe-wbufs,nmax,wbufs,PETSC_FALSE);
00137     wbufe=wbufs;
00138 #endif
00139 
00140   if (!rank) {
00141     /* must not use CHKERRQ(ierr); here in order not to confuse gzprintf */
00142     ierr = FPRINTF3(fd,"%i",nvdata);
00143     for (int i=0;i<nvdata;i++) {
00144       ierr = FPRINTF2(fd," 1");
00145     }
00146     ierr = FPRINTF2(fd,"\n");
00147 
00148     ierr = FPRINTF2(fd,"M_x, none\n");
00149     ierr = FPRINTF2(fd,"M_y, none\n");
00150     ierr = FPRINTF2(fd,"M_z, none\n");
00151     ierr = FPRINTF2(fd,"Hdemag_x, none\n");
00152     ierr = FPRINTF2(fd,"Hdemag_y, none\n");
00153     ierr = FPRINTF2(fd,"Hdemag_z, none\n");
00154 #ifdef EXCH
00155     ierr = FPRINTF2(fd,"Hani_x, none\n");
00156     ierr = FPRINTF2(fd,"Hani_y, none\n");
00157     ierr = FPRINTF2(fd,"Hani_z, none\n");
00158     ierr = FPRINTF2(fd,"Hexch_x, none\n");
00159     ierr = FPRINTF2(fd,"Hexch_y, none\n");
00160     ierr = FPRINTF2(fd,"Hexch_z, none\n");
00161 #else
00162     ierr = FPRINTF2(fd,"Hexchani_x, none\n");
00163     ierr = FPRINTF2(fd,"Hexchani_y, none\n");
00164     ierr = FPRINTF2(fd,"Hexchani_z, none\n");
00165 #endif
00166     ierr = FPRINTF2(fd,"Hext_x, none\n");
00167     ierr = FPRINTF2(fd,"Hext_y, none\n");
00168     ierr = FPRINTF2(fd,"Hext_z, none\n");
00169   }
00170 
00171   PetscReal *ta_M;
00172   VecGetArray(gdata->M,&ta_M);
00173 
00174   PetscReal *ta_VHdem;
00175   if (gdata->VHdem!=PETSC_NULL) {
00176     VecGetArray(gdata->VHdem,&ta_VHdem);
00177   }
00178   else {
00179     ta_VHdem=PETSC_NULL;
00180   }
00181 
00182 #ifdef EXCH
00183   PetscReal *ta_VHexchonly;
00184   ierr = VecAXPY(gdata->VHexchani,-1.0,gdata->VHexchonly);CHKERRQ(ierr);
00185   VecGetArray(gdata->VHexchonly,&ta_VHexchonly);
00186 #endif
00187 
00188   PetscReal *ta_VHexchani;
00189   VecGetArray(gdata->VHexchani,&ta_VHexchani);
00190 
00191   PetscReal *ta_VHext;
00192   VecGetArray(gdata->VHext,&ta_VHext);
00193 
00194   for (int i=0; i<gdata->ln_vert; i++) {
00195     int nchars;
00196     /* write into buffer */
00197     nchars=
00198     snprintf(
00199       wbufe,
00200       nmax-(wbufe-wbufs),
00201       "%i %g %g %g %g %g %g %g %g %g "
00202 #ifdef EXCH
00203       "%g %g %g "
00204 #endif
00205       "%g %g %g\n",
00206       gdata->vertl2g[i]+1,
00207       ta_M[ND*i+0],
00208       ta_M[ND*i+1],
00209       ta_M[ND*i+2],
00210       gdata->VHdem!=PETSC_NULL ? ta_VHdem[ND*i+0]*gdata->hscale : 0.0,
00211       gdata->VHdem!=PETSC_NULL ? ta_VHdem[ND*i+1]*gdata->hscale : 0.0,
00212       gdata->VHdem!=PETSC_NULL ? ta_VHdem[ND*i+2]*gdata->hscale : 0.0,
00213       ta_VHexchani[ND*i+0]*gdata->hscale,
00214       ta_VHexchani[ND*i+1]*gdata->hscale,
00215       ta_VHexchani[ND*i+2]*gdata->hscale,
00216 #ifdef EXCH
00217       ta_VHexchonly[ND*i+0]*gdata->hscale,
00218       ta_VHexchonly[ND*i+1]*gdata->hscale,
00219       ta_VHexchonly[ND*i+2]*gdata->hscale,
00220 #endif
00221       ta_VHext[ND*i+0]*gdata->hscale,
00222       ta_VHext[ND*i+1]*gdata->hscale,
00223       ta_VHext[ND*i+2]*gdata->hscale
00224     );
00225 
00226     /* check for problems of snprintf */
00227     assert(nchars>0);
00228 
00229     /* update pointer to the end of the array */
00230     wbufe += nchars;
00231 
00232     /* check if we write beyond allocated area */
00233     assert(wbufe-wbufs < nmax);
00234   }
00235 
00236   VecRestoreArray(gdata->M,&ta_M);
00237   if (gdata->VHdem!=PETSC_NULL) {
00238     VecRestoreArray(gdata->VHdem,&ta_VHdem);
00239   }
00240   VecRestoreArray(gdata->VHexchani,&ta_VHexchani);
00241 #ifdef EXCH
00242   VecRestoreArray(gdata->VHexchonly,&ta_VHexchonly);
00243 #endif
00244   VecRestoreArray(gdata->VHext,&ta_VHext);
00245 
00246 #if defined(ZLIB) && !defined(_WIN32) && !defined(__CYGWIN32__)
00247   SynchronizedFastFPrintf(fd,wbufe-wbufs,nmax,wbufs,PETSC_TRUE);
00248 #else
00249   SynchronizedFastFPrintf(fd,wbufe-wbufs,nmax,wbufs,PETSC_FALSE);
00250 #endif
00251 
00252   wbufe=wbufs;
00253 
00254 #if defined(ZLIB) && !defined(_WIN32) && !defined(__CYGWIN32__)
00255   if (!rank) {
00256     gzclose(fd);
00257   }
00258 #else
00259   ierr = PetscFClose(PETSC_COMM_WORLD,fd);CHKERRQ(ierr);
00260 #endif
00261 
00262   ierr = PetscFree(wbufs);CHKERRQ(ierr);
00263 
00264 
00265   MagparFunctionInfoReturn(0);
00266 }
00267 

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