hstep_file.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     Copyright (C) 2008 Stefan Tibus
00006 
00007     www:   http://www.magpar.net/
00008     email: magpar(at)magpar.net
00009 
00010     magpar is free software; you can redistribute it and/or modify
00011     it under the terms of the GNU General Public License as published by
00012     the Free Software Foundation; either version 2 of the License, or
00013     (at your option) any later version.
00014 
00015     magpar is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU General Public License for more details.
00019 
00020     You should have received a copy of the GNU General Public License
00021     along with magpar; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 */
00024 
00025 static char const Id[] = "$Id: hstep_file.c 1000 2008-07-31 16:37:20Z tibus $\n\n";
00026 static char const Td[] = "$Today: " __FILE__ "  " __DATE__ "  "  __TIME__  " $\n\n";
00027 
00028 #include "field.h"
00029 
00030 #define HSTEPCOL 2
00031 
00032 /*
00033 input file structure:
00034 -----------
00035 n
00036 x_0  y_0
00037 x_1  y_1
00038 x_2  y_2
00039 x_3  y_3
00040 ...
00041 x_n  y_n
00042 -----------
00043 */
00044 
00045 
00046 int read_hstep_file(FILE *fd,PetscReal **indata,const int col)
00047 {
00048   int            nrows;
00049   PetscReal      *xydata;
00050 
00051   MagparFunctionLogBegin;
00052 
00053   int rank,size;
00054   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
00055   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
00056 
00057   if (col<2) {
00058     SETERRQ1(PETSC_ERR_ARG_CORRUPT,"col=%i < 2!\n",col);
00059   }
00060 
00061   /* only the first processor reads the file */
00062   if (!rank) {
00063     char msg[256];
00064 
00065     /* get number of lines */
00066     fscanf(fd,"%i\n",&nrows);
00067     PetscPrintf(PETSC_COMM_WORLD,"Reading column %i, %i lines\n",col,nrows);
00068 
00069     /* account for additional first and last entry */
00070     nrows += 2;
00071 
00072     ierr = PetscMalloc(HSTEPCOL*nrows*sizeof(PetscReal),&xydata);CHKERRQ(ierr);
00073 
00074     /* get xydata from file (leave room for first ) */
00075     for (int i=1; i<nrows-1; i++) {
00076       int k;
00077       /* read x from first column */
00078 #ifdef PETSC_USE_SINGLE
00079       k=fscanf(fd,"%f",xydata+i*HSTEPCOL+0);
00080 #else
00081       k=fscanf(fd,"%lf",xydata+i*HSTEPCOL+0);
00082 #endif
00083       if (k!=1) {
00084         SETERRQ2(PETSC_ERR_FILE_UNEXPECTED,
00085           "xydata in corrupt in row %i col %i\n",
00086           i,1
00087         );
00088       }
00089 
00090       /* read y from column col */
00091       for (int j=1; j<col; j++) {
00092 #ifdef PETSC_USE_SINGLE
00093         k=fscanf(fd,"%f",xydata+i*HSTEPCOL+1);
00094 #else
00095         k=fscanf(fd,"%lf",xydata+i*HSTEPCOL+1);
00096 #endif
00097 
00098         if (k!=1) {
00099           SETERRQ2(PETSC_ERR_FILE_UNEXPECTED,
00100             "xydata in corrupt in row %i col %i\n",
00101             i,j
00102           );
00103         }
00104       }
00105       /* read remainder of this line */
00106       fgets(msg,256,fd);
00107     }
00108 
00109     /* set first xydata set */
00110     xydata[0]=PETSC_MIN;
00111     xydata[1]=0.0;
00112 
00113     /* set last xydata set */
00114     xydata[HSTEPCOL*(nrows-1)+0]=PETSC_MAX;
00115     xydata[HSTEPCOL*(nrows-1)+1]=0.0;
00116 
00117     PetscPrintf(PETSC_COMM_WORLD,
00118       "%14s %14s\n",
00119       "step","scale"
00120     );
00121 
00122     for (int i=0; i<nrows; i++) {
00123       PetscPrintf(PETSC_COMM_WORLD,
00124         "%14e %14e\n",
00125         xydata[HSTEPCOL*i+0],
00126         xydata[HSTEPCOL*i+1]
00127       );
00128     }
00129   }
00130 
00131   /* broadcast xydata to all processors */
00132   ierr = MPI_Bcast(&nrows,1,MPI_INT,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
00133   if (rank) {
00134     ierr = PetscMalloc(HSTEPCOL*nrows*sizeof(PetscReal),&xydata);CHKERRQ(ierr);
00135   }
00136   ierr = MPI_Bcast(xydata,nrows*HSTEPCOL,MPIU_SCALAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
00137   *indata=xydata;
00138 
00139   MagparFunctionLogReturn(0);
00140 }
00141 
00142 PetscReal get_scalf_hstep(PetscReal xydata[],PetscReal xval)
00143 {
00144   int       tint;
00145   PetscReal result;
00146 
00147   tint=0;
00148   while (xval > xydata[HSTEPCOL*(tint+1)]) tint++;
00149 
00150   /* calculate scaling factor */
00151   result=xydata[HSTEPCOL*tint+1];
00152 
00153   return(result);
00154 }
00155 

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