writelog.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: writelog.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 #include "util/util.h"
00029 #include "field/field.h"
00030 
00031 #ifdef ADDONS
00032 #include "addons/addons.h"
00033 #endif
00034 
00035 #ifdef SPINTORQ
00036 #include "spintorq/spintorq.h"
00037 #endif
00038 
00039 #ifdef SUNDIALS_VERSION
00040 #include "llg/llg.h"
00041 #endif
00042 
00043 static int     doinit=1;
00044 static FILE    *logfile; 
00046 static Vec     V1Mtot[ND]; 
00048 int Vec3VolAvg(Vec v,PetscReal* res)
00049 {
00050   MagparFunctionInfoBegin;
00051 
00052   /* calculate Mtot (ignoring Js) */
00053   for (int i=0;i<ND;i++) {
00054     ierr = VecDot(v,V1Mtot[i],&res[i]);CHKERRQ(ierr);
00055   }
00056 
00057   MagparFunctionInfoReturn(0);
00058 }
00059 
00060 
00061 int WriteLogInit(GridData *gdata)
00062 {
00063   MagparFunctionLogBegin;
00064 
00065   int rank,size;
00066   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
00067   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
00068 
00069   PetscReal *vvol;
00070   ierr = VecGetArray(gdata->vertvol,&vvol);CHKERRQ(ierr);
00071 
00072   /* calculate magnetic volume */
00073   PetscReal Vtot;
00074   Vtot=0.0;
00075   for (int j=0;j<gdata->ln_vert;j++) {
00076     if (gdata->propdat[NP*gdata->vertprop[j]+4]>0.0) {
00077       Vtot += vvol[j];
00078     }
00079   }
00080   PetscReal temp;
00081   ierr = PetscGlobalSum(&Vtot,&temp,PETSC_COMM_WORLD);CHKERRQ(ierr);
00082   Vtot=temp;
00083 
00084   PetscInfo2(0,"Using total volume %g (%g)\n",Vtot,gdata->totvol);
00085   /* Vtot, gdata->totvol might be different because the latter is sum over gdata->elevol */
00086 
00087   /* calculate average M only over magnetic regions */
00088   for (int i=0;i<ND;i++) {
00089     ierr = VecDuplicate(gdata->M,&V1Mtot[i]);CHKERRQ(ierr);
00090 
00091     PetscReal *v1m;
00092     ierr = VecGetArray(V1Mtot[i],&v1m);CHKERRQ(ierr);
00093     for (int j=0;j<gdata->ln_vert;j++) {
00094       if (gdata->propdat[NP*gdata->vertprop[j]+4]>0.0) {
00095         v1m[ND*j+i]=vvol[j];
00096       }
00097     }
00098     ierr = VecRestoreArray(V1Mtot[i],&v1m);CHKERRQ(ierr);
00099 
00100     ierr = VecScale(V1Mtot[i],1.0/Vtot);CHKERRQ(ierr);
00101   }
00102 
00103   ierr = VecRestoreArray(gdata->vertvol,&vvol);CHKERRQ(ierr);
00104 
00105   if (rank) {
00106     MagparFunctionLogReturn(0);
00107   }
00108 
00109   char fmesh[256];
00110   ierr = PetscSNPrintf(fmesh,255,"%s%s",gdata->simname,".log");CHKERRQ(ierr);
00111   ierr = PetscFOpen(PETSC_COMM_WORLD,fmesh,"a",&logfile);CHKERRQ(ierr);
00112   if (!logfile) {
00113     SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open %s\n",fmesh);
00114   }
00115   PetscPrintf(PETSC_COMM_WORLD,"Opening file %s\n",fmesh);
00116 
00117   PetscGetDate(fmesh,255);
00118   ierr = PetscFPrintf(PETSC_COMM_WORLD,logfile,"#.date: %s\n",fmesh);
00119 
00120   /*     1   2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   20 */
00121   ierr = PetscFPrintf(PETSC_COMM_WORLD,logfile,
00122     "#.%3s %4s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s\n",
00123     ".1:",
00124     " 2:",
00125     " 3:",
00126     " 4:",
00127     " 5:",
00128     " 6:",
00129     " 7:",
00130     " 8:",
00131     " 9:",
00132     "10:",
00133     "11:",
00134     "12:",
00135     "13:",
00136     "14:",
00137     "15:",
00138     "16:",
00139     "17:",
00140     "18:",
00141     "20:"
00142   );CHKERRQ(ierr);
00143   /*     1   2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   20*/
00144   ierr = PetscFPrintf(PETSC_COMM_WORLD,logfile,
00145     "#.%3s %4s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s\n",
00146     ".eq",
00147     "inp",
00148     "time",
00149     "Hext",
00150     "Etot",
00151     "J//Hext",
00152     "Mx",
00153     "My",
00154     "Mz",
00155     "|M|",
00156     "Edem",
00157 #ifdef EXCH
00158     "Eanionly",
00159 #else
00160     "Eexchani",
00161 #endif
00162     "Eext",
00163 #ifdef EXCH
00164     "Eexchonly",
00165 #else
00166     "-",
00167 #endif
00168     "devNorm",
00169     "mx|dM/dt|",
00170     "timestep",
00171     "tCPU",
00172     "CPUtstep"
00173   );CHKERRQ(ierr);
00174   /*     1   2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   20*/
00175   ierr = PetscFPrintf(PETSC_COMM_WORLD,logfile,
00176     "#.%3s %4s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s\n",
00177     /* "eq",           */ "..-",
00178     /* "inp",          */ "-",
00179     /* " 3:time",      */ "(ns)",
00180     /* " 4:Hext",      */ "(kA/m)",
00181     /* " 5:Etot",      */ "(J/m^3)",
00182     /* " 6:J//Hext",   */ "[J/Javg]",
00183     /* " 7:Mx",        */ "(1)",
00184     /* " 8:My",        */ "(1)",
00185     /* " 9:Mz",        */ "(1)",
00186     /* "10:|M|",       */ "(1)",
00187     /* "11:Edem",      */ "(J/m^3)",
00188     /* "12:Eexchani",  */ "(J/m^3)",
00189     /* "13:Eext",      */ "(J/m^3)",
00190 #ifdef EXCH
00191     /* "14:Eexchonly", */ "(J/m^3)",
00192 #else
00193     /* "14:void",      */ "-",
00194 #endif
00195     /* "15:devNorm",   */ "(1)",
00196     /* "16:mx|dM/dt|", */ "(1)",
00197     /* "17:timestep",  */ "(ns)",
00198     /* "18:tCPU",      */ "(s)",
00199     /* "20:CPUtstep",  */ "(s)"
00200   );CHKERRQ(ierr);
00201 
00202   MagparFunctionLogReturn(0);
00203 }
00204 
00205 
00206 int WriteLogData(GridData *gdata)
00207 {
00208   static PetscLogDouble tCPUstart=-1; 
00209   static PetscLogDouble tCPUlast;
00210   static PetscReal      timelast;
00211 
00212   MagparFunctionInfoBegin;
00213 
00214   int rank,size;
00215   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
00216   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
00217 
00218   if (doinit) {
00219     tCPUstart=t_t1;
00220     tCPUlast=t_t1;
00221     timelast=gdata->time;
00222     ierr = WriteLogInit(gdata);CHKERRQ(ierr);
00223     doinit=0;
00224   }
00225 
00226   PetscReal devNorm;
00227   devNorm=RenormVec(gdata->M,1.0,PETSC_MAX,ND);
00228 
00229   /* calculate Mtot (ignoring Js) */
00230   PetscReal Mtot[ND];
00231   ierr = Vec3VolAvg(gdata->M,Mtot);CHKERRQ(ierr);
00232 
00233   /* calculate average magnetization parallel to the external field */
00234   PetscReal valMpHext;
00235   ierr = MpHext(gdata->M,&valMpHext);CHKERRQ(ierr);
00236 
00237   PetscReal hext;
00238   hext = Hexternal_hext();
00239 
00240 /* Edem<0 occurs often in airboxes (should be zero)
00241    and due to magnetostatic field of other parts (not self-demag),
00242    which acts like an external field.
00243    As a result, lots of output when Hdemag_Energy function is called for each
00244    volume individually (with -logpid 1).
00245    Thus, better done in WriteLogData, where we check Edem for the whole model.
00246 */
00247 
00248   if (gdata->Edem<0) {
00249     PetscPrintf(MPI_COMM_WORLD,
00250       "Warning: Edem<0 at t=%g ns: Edem=%g J/m^3\n",
00251       gdata->time*gdata->tscale*1e9,
00252       gdata->Edem*gdata->escale
00253     );
00254   }
00255 
00256 /*       1   2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   20*/
00257   ierr = PetscFPrintf(PETSC_COMM_WORLD,logfile,
00258     "  %3d %4d %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e\n",
00259     /* "eq",           "",        */ gdata->equil,
00260     /* "inp",          "",        */ gdata->inp >= 0 ? gdata->inp : 0,
00261     /* " 3:time",      "(ns)",    */ gdata->time*gdata->tscale*1e9,
00262     /* " 4:Hext",      "(kA/m)",  */ hext*gdata->hscale/(MU0*1000.0),
00263     /* " 5:Etot",      "(J/m^3)", */ gdata->Etot*gdata->escale,
00264     /* " 6:J//Hext",   "[J/Javg]",*/ valMpHext,
00265     /* " 7:Mx",        "(1)",     */ Mtot[0],
00266     /* " 8:My",        "(1)",     */ Mtot[1],
00267     /* " 9:Mz",        "(1)",     */ Mtot[2],
00268     /* "10:|M|",       "(1)",     */ sqrt(Mtot[0]*Mtot[0]+Mtot[1]*Mtot[1]+Mtot[2]*Mtot[2]),
00269     /* "11:Edem",      "(J/m^3)", */ gdata->Edem*gdata->escale,
00270 #ifdef EXCH
00271     /* "12:Eanionly",  "(J/m^3)", */ (gdata->Eexchani-gdata->Eexchonly)*gdata->escale,
00272 #else
00273     /* "12:Eexchani",  "(J/m^3)", */ gdata->Eexchani*gdata->escale,
00274 #endif
00275     /* "13:Eext",      "(J/m^3)", */ gdata->Eext*gdata->escale,
00276 #ifdef EXCH
00277     /* "14:Eexchonly", "(J/m^3)", */ gdata->Eexchonly*gdata->escale,
00278 #else
00279     /* "14:void",      "",        */ -1.0,
00280 #endif
00281     /* "15:devNorm",   "(1)",     */ devNorm,
00282     /* "16:vequil",    "(1)",     */ gdata->vequil,
00283     /* "17:timestep",  "(ns)",    */ (gdata->time-timelast)*gdata->tscale*1e9,
00284     /* "18:tCPU",      "(s)",     */ t_t1-tCPUstart,
00285     /* "20:CPUtstep",  "(s)",     */ t_t1-tCPUlast /* CPU time for last time step */
00286   );
00287 
00288   tCPUlast=t_t1;
00289   timelast=gdata->time;
00290 
00291   MagparFunctionInfoReturn(0);
00292 }
00293 
00294 
00295 int WriteLog(GridData *gdata)
00296 {
00297   MagparFunctionInfoBegin;
00298 
00299   ierr = WriteLogData(gdata);CHKERRQ(ierr);
00300   ierr = WriteLogPid(gdata);CHKERRQ(ierr);
00301 
00302 #ifdef SUNDIALS_VERSION
00303   /* write sundials logfile */
00304   ierr = WriteLogPVode(gdata);CHKERRQ(ierr);
00305 #endif
00306 
00307 #ifdef ADDONS
00308   /* write addons logfile */
00309   ierr = WriteLogExt(gdata);CHKERRQ(ierr);
00310 #endif
00311 
00312 #ifdef SPINTORQ
00313   /* write addons logfile */
00314   ierr = WriteLogGMR(gdata);CHKERRQ(ierr);
00315 #endif
00316 
00317   MagparFunctionInfoReturn(0);
00318 }
00319 

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