00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00034
00035
00036
00037
00038
00039
00040
00041
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
00062 if (!rank) {
00063 char msg[256];
00064
00065
00066 fscanf(fd,"%i\n",&nrows);
00067 PetscPrintf(PETSC_COMM_WORLD,"Reading column %i, %i lines\n",col,nrows);
00068
00069
00070 nrows += 2;
00071
00072 ierr = PetscMalloc(HSTEPCOL*nrows*sizeof(PetscReal),&xydata);CHKERRQ(ierr);
00073
00074
00075 for (int i=1; i<nrows-1; i++) {
00076 int k;
00077
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
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
00106 fgets(msg,256,fd);
00107 }
00108
00109
00110 xydata[0]=PETSC_MIN;
00111 xydata[1]=0.0;
00112
00113
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
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
00151 result=xydata[HSTEPCOL*tint+1];
00152
00153 return(result);
00154 }
00155