Changeset 12382


Ignore:
Timestamp:
06/07/12 10:34:56 (13 years ago)
Author:
Mathieu Morlighem
Message:

Added read options from binary file

Location:
issm/trunk-jpl/src/c
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/Makefile.am

    r12377 r12382  
    331331                                        ./solvers/solver_linear.cpp\
    332332                                        ./solvers/solver_nonlinear.cpp\
    333                                         ./solvers/solver_newton.cpp
     333                                        ./solvers/solver_newton.cpp\
     334                                        ./objects/Options/Option.cpp\
     335                                        ./objects/Options/Option.h\
     336                                        ./objects/Options/OptionDouble.cpp\
     337                                        ./objects/Options/OptionDouble.h\
     338                                        ./objects/Options/OptionChar.cpp\
     339                                        ./objects/Options/OptionChar.h\
     340                                        ./objects/Options/OptionUtilities.cpp\
     341                                        ./objects/Options/OptionUtilities.h
     342
    334343#}}}
    335344#DAKOTA sources  {{{1
  • issm/trunk-jpl/src/c/objects/IoModel.cpp

    r12377 r12382  
    867867}
    868868/*}}}*/
     869/*FUNCTION IoModel::FetchData(Option** poption,int data_enum){{{*/
     870void  IoModel::FetchData(Option** poption,int index){
     871
     872        extern int my_rank;
     873        extern int num_procs;
     874
     875        /*output: */
     876        int     code;
     877        Option *option      = NULL;
     878        char   *name        = NULL;
     879
     880        /*First get option name*/
     881        this->FetchData(&name,index);
     882        _printf_(true,"Option name is %s\n",name);
     883
     884        /*Get option value*/
     885        fid=this->SetFilePointerToData(&code,NULL,index+1);
     886        switch(code){
     887                case 3: {//double
     888                          double *value = NULL;
     889                          value=(double*)xmalloc(1*sizeof(double));
     890                          FetchData(value,index+1);
     891                          _printf_(true,"value = %g\n",*value);
     892                          option = new OptionDouble();
     893                          ((OptionDouble*)option)->values = value;
     894                          option->name  = name;
     895                          option->numel = 1;
     896                          option->ndims = 1;
     897                          option->size  = NULL;
     898                          break;
     899                          }
     900                case 4: {//char
     901                          char* value = NULL;
     902                          FetchData(&value,index+1);
     903                          _printf_(true,"value = %s\n",value);
     904                          option = new OptionChar();
     905                          ((OptionChar*)option)->values = value;
     906                          option->name  = name;
     907                          option->numel = 1;
     908                          option->ndims = 1;
     909                          option->size  = NULL;
     910                          break;
     911                          }
     912                default:
     913                          _error_("Option of format %i not supported yet",code);
     914        }
     915
     916        /*Assign output pointers: */
     917        *poption=option;
     918}
     919/*}}}*/
    869920/*FUNCTION IoModel::FetchData(int num,...){{{*/
    870921void  IoModel::FetchData(int num,...){
     
    10971148        xfree((void**)&string);
    10981149}
     1150/*FUNCTION IoModel::LastIndex{{{*/
     1151void IoModel::LastIndex(int *pindex){
     1152
     1153        extern int my_rank;
     1154        int        lastindex,index;
     1155        int        record_length;
     1156
     1157        /*Go find in the binary file, the position of the data we want to fetch: */
     1158        if(my_rank==0){
     1159
     1160                /*First set FILE* position to the beginning of the file: */
     1161                fseek(fid,0,SEEK_SET);
     1162
     1163                /*Now march through file looking for the correct data identifier: */
     1164                for(;;){
     1165                        /*Read enum for this size of first string name: */
     1166                        if(fread(&index,sizeof(int),1,fid)==0){
     1167                                /*Ok, we have reached the end of the file. break: */
     1168                                break;
     1169                        }
     1170
     1171                        /*read the record length, and use it to skip this record: */
     1172                        fread(&record_length,sizeof(int),1,fid);
     1173                        fseek(fid,record_length,SEEK_CUR);
     1174                        lastindex=index;
     1175                }
     1176        }
     1177        /*Broadcast code and vector type: */
     1178#ifdef _HAVE_MPI_
     1179        MPI_Bcast(&lastindex,1,MPI_INT,0,MPI_COMM_WORLD);
     1180#endif
     1181
     1182        /*Assign output pointers:*/
     1183        *pindex=lastindex;
     1184}
    10991185/*FUNCTION IoModel::SetFilePointerToData{{{*/
    11001186FILE* IoModel::SetFilePointerToData(int* pcode,int* pvector_type, int data_enum){
     
    11021188        extern int my_rank;
    11031189        extern int num_procs;
    1104        
     1190
    11051191        int found=0;
    11061192        int record_enum;
     
    11081194        int record_code; //1 to 7 number
    11091195        int vector_type; //nodal or elementary
    1110  
     1196
    11111197        /*Go find in the binary file, the position of the data we want to fetch: */
    11121198        if(my_rank==0){
    1113        
     1199
    11141200                /*First set FILE* position to the beginning of the file: */
    11151201                fseek(fid,0,SEEK_SET);
     
    11231209                                break;
    11241210                        }
    1125                        
     1211
    11261212                        /*Is this the record sought for? : */
    11271213                        if (data_enum==record_enum){
     
    11431229                }
    11441230        }
    1145         #ifdef _HAVE_MPI_
     1231#ifdef _HAVE_MPI_
    11461232        MPI_Bcast(&found,1,MPI_INT,0,MPI_COMM_WORLD);
    11471233        if(!found)_error_("%s %s ","could not find data with name",EnumToStringx(data_enum));
    1148         #endif
     1234#endif
    11491235
    11501236        /*Broadcast code and vector type: */
    1151         #ifdef _HAVE_MPI_
     1237#ifdef _HAVE_MPI_
    11521238        MPI_Bcast(&record_code,1,MPI_INT,0,MPI_COMM_WORLD);
    11531239        MPI_Bcast(&vector_type,1,MPI_INT,0,MPI_COMM_WORLD);
    11541240        if(record_code==5) MPI_Bcast(&vector_type,1,MPI_INT,0,MPI_COMM_WORLD);
    1155         #endif
     1241#endif
    11561242
    11571243        /*Assign output pointers:*/
  • issm/trunk-jpl/src/c/objects/IoModel.h

    r12377 r12382  
    1313class Elements;
    1414class Param;
    15 
     15class Option;
    1616
    1717class IoModel {
     
    2323        public:
    2424                /*This data needs to stay memory resident at all time, even if it's memory intensive: */
    25                 FILE        *fid;         //pointer to input file
     25                FILE *fid;         //pointer to input file
    2626                bool *my_elements;
    2727                bool *my_nodes;
     
    4141
    4242                /*Input/Output*/
    43                 void    CheckEnumSync(void);
    44                 void    Constant(bool  *poutput,int constant_enum);
    45                 void    Constant(int    *poutput,int constant_enum);
    46                 void    Constant(IssmDouble *poutput,int constant_enum);
    47                 void    Constant(char **poutput,int constant_enum);
    48                 Param  *CopyConstantObject(int constant_enum);
     43                void        CheckEnumSync(void);
     44                void        Constant(bool *poutput,int constant_enum);
     45                void        Constant(int *poutput,int constant_enum);
     46                void        Constant(IssmDouble *poutput,int constant_enum);
     47                void        Constant(char **poutput,int constant_enum);
     48                Param      *CopyConstantObject(int constant_enum);
    4949                IssmDouble *Data(int dataenum);
    50                 void    DeleteData(int num,...);
    51                 void    FetchConstants(void);
    52                 void    FetchData(bool*     pboolean,int data_enum);
    53                 void    FetchData(int*      pinteger,int data_enum);
    54                 void    FetchData(IssmDouble*   pscalar,int data_enum);
    55                 void    FetchData(char**    pstring,int data_enum);
    56                 void    FetchData(int** pmatrix,int* pM,int* pN,int data_enum);
    57                 void    FetchData(IssmDouble**  pscalarmatrix,int* pM,int* pN,int data_enum);
    58                 void    FetchData(char***   pstringarray,int* pnumstrings,int data_enum);
    59                 void    FetchData(IssmDouble*** pmatrixarray,int** pmdims,int** pndims, int* pnumrecords,int data_enum);
    60                 void    FetchData(int num,...);
    61                 void    FetchDataToInput(Elements* elements,int vector_enum,int default_vector_enum=NoneEnum,IssmDouble default_value=0);
    62                 FILE*   SetFilePointerToData(int* pcode,int* pvector_type, int data_enum);
     50                void        DeleteData(int num,...);
     51                void        FetchConstants(void);
     52                void        FetchData(bool* pboolean,int data_enum);
     53                void        FetchData(int* pinteger,int data_enum);
     54                void        FetchData(IssmDouble* pscalar,int data_enum);
     55                void        FetchData(char** pstring,int data_enum);
     56                void        FetchData(int** pmatrix,int* pM,int* pN,int data_enum);
     57                void        FetchData(IssmDouble**  pscalarmatrix,int* pM,int* pN,int data_enum);
     58                void        FetchData(char***   pstringarray,int* pnumstrings,int data_enum);
     59                void        FetchData(IssmDouble*** pmatrixarray,int** pmdims,int** pndims, int* pnumrecords,int data_enum);
     60                void        FetchData(Option **poption,int data_enum);
     61                void        FetchData(int num,...);
     62                void        FetchDataToInput(Elements* elements,int vector_enum,int default_vector_enum=NoneEnum,IssmDouble default_value=0);
     63                void        LastIndex(int *pindex);
     64                FILE*       SetFilePointerToData(int* pcode,int* pvector_type, int data_enum);
    6365};
    6466
  • issm/trunk-jpl/src/c/solutions/kriging.cpp

    r12381 r12382  
    136136void ProcessInputfile(double **px,double **py,double **pdata,int *pnobs,double **px_interp,double **py_interp,int *pninterp,Options **poptions,FILE* fid){
    137137
    138         int      ninterp,nobs;
     138        int      ninterp,nobs,numoptions;
    139139        double  *x        = NULL;
    140140        double  *y        = NULL;
     
    143143        double  *y_interp = NULL;
    144144        Options *options  = NULL;
     145        Option  *option   = NULL;
    145146
    146147        int dummy,M,N;
     
    148149        iomodel->fid=fid;
    149150        iomodel->CheckEnumSync();
    150 
    151         /*Read x*/
    152         fid=iomodel->SetFilePointerToData(&dummy,&dummy,0);
    153         if(my_rank==0){
    154                 if(fread(&M,sizeof(int),1,fid)!=1) _error_("could not read number of rows for matrix ");
    155         }
    156         MPI_Bcast(&M,1,MPI_INT,0,MPI_COMM_WORLD);
    157         if(my_rank==0){ 
    158                 if(fread(&N,sizeof(int),1,fid)!=1) _error_("could not read number of columns for matrix ");
    159         }
    160         MPI_Bcast(&N,1,MPI_INT,0,MPI_COMM_WORLD);
    161         if(M*N){
    162                 x=(double*)xmalloc(M*N*sizeof(double));
    163                 if(my_rank==0){ 
    164                         if(fread(x,M*N*sizeof(double),1,fid)!=1) _error_("could not read matrix ");
    165                 }
    166                 MPI_Bcast(x,M*N,MPI_DOUBLE,0,MPI_COMM_WORLD);
    167         }
    168         nobs=M*N;
    169 
    170         /*Read y*/
    171         fid=iomodel->SetFilePointerToData(&dummy,&dummy,1);
    172         if(my_rank==0){ 
    173                 if(fread(&M,sizeof(int),1,fid)!=1) _error_("could not read number of rows for matrix ");
    174         }
    175         MPI_Bcast(&M,1,MPI_INT,0,MPI_COMM_WORLD);
    176         if(my_rank==0){ 
    177                 if(fread(&N,sizeof(int),1,fid)!=1) _error_("could not read number of columns for matrix ");
    178         }
    179         MPI_Bcast(&N,1,MPI_INT,0,MPI_COMM_WORLD);
    180         if(M*N){
    181                 y=(double*)xmalloc(M*N*sizeof(double));
    182                 if(my_rank==0){ 
    183                         if(fread(y,M*N*sizeof(double),1,fid)!=1) _error_("could not read matrix ");
    184                 }
    185                 MPI_Bcast(y,M*N,MPI_DOUBLE,0,MPI_COMM_WORLD);
    186         }
    187         _assert_(M*N==nobs);
    188 
    189         /*Read data*/
    190         fid=iomodel->SetFilePointerToData(&dummy,&dummy,2);
    191         if(my_rank==0){ 
    192                 if(fread(&M,sizeof(int),1,fid)!=1) _error_("could not read number of rows for matrix ");
    193         }
    194         MPI_Bcast(&M,1,MPI_INT,0,MPI_COMM_WORLD);
    195         if(my_rank==0){ 
    196                 if(fread(&N,sizeof(int),1,fid)!=1) _error_("could not read number of columns for matrix ");
    197         }
    198         MPI_Bcast(&N,1,MPI_INT,0,MPI_COMM_WORLD);
    199         if(M*N){
    200                 data=(double*)xmalloc(M*N*sizeof(double));
    201                 if(my_rank==0){ 
    202                         if(fread(data,M*N*sizeof(double),1,fid)!=1) _error_("could not read matrix ");
    203                 }
    204                 MPI_Bcast(data,M*N,MPI_DOUBLE,0,MPI_COMM_WORLD);
    205         }
    206         _assert_(M*N==nobs);
    207 
    208         /*Read x_interp*/
    209         fid=iomodel->SetFilePointerToData(&dummy,&dummy,3);
    210         if(my_rank==0){ 
    211                 if(fread(&M,sizeof(int),1,fid)!=1) _error_("could not read number of rows for matrix ");
    212         }
    213         MPI_Bcast(&M,1,MPI_INT,0,MPI_COMM_WORLD);
    214         if(my_rank==0){ 
    215                 if(fread(&N,sizeof(int),1,fid)!=1) _error_("could not read number of columns for matrix ");
    216         }
    217         MPI_Bcast(&N,1,MPI_INT,0,MPI_COMM_WORLD);
    218         if(M*N){
    219                 x_interp=(double*)xmalloc(M*N*sizeof(double));
    220                 if(my_rank==0){ 
    221                         if(fread(x_interp,M*N*sizeof(double),1,fid)!=1) _error_("could not read matrix ");
    222                 }
    223                 MPI_Bcast(x_interp,M*N,MPI_DOUBLE,0,MPI_COMM_WORLD);
    224         }
    225         ninterp=N*M;
    226 
    227         /*Read y_interp*/
    228         fid=iomodel->SetFilePointerToData(&dummy,&dummy,4);
    229         if(my_rank==0){ 
    230                 if(fread(&M,sizeof(int),1,fid)!=1) _error_("could not read number of rows for matrix ");
    231         }
    232         MPI_Bcast(&M,1,MPI_INT,0,MPI_COMM_WORLD);
    233         if(my_rank==0){ 
    234                 if(fread(&N,sizeof(int),1,fid)!=1) _error_("could not read number of columns for matrix ");
    235         }
    236         MPI_Bcast(&N,1,MPI_INT,0,MPI_COMM_WORLD);
    237         if(M*N){
    238                 y_interp=(double*)xmalloc(M*N*sizeof(double));
    239                 if(my_rank==0){ 
    240                         if(fread(y_interp,M*N*sizeof(double),1,fid)!=1) _error_("could not read matrix ");
    241                 }
    242                 MPI_Bcast(y_interp,M*N,MPI_DOUBLE,0,MPI_COMM_WORLD);
    243         }
    244         _assert_(N*M==ninterp);
     151        iomodel->FetchData(&x,&M,&N,0);        nobs=M*N;
     152        iomodel->FetchData(&y,&M,&N,1);        _assert_(M*N==nobs);
     153        iomodel->FetchData(&data,&M,&N,2);     _assert_(M*N==nobs);
     154        iomodel->FetchData(&x_interp,&M,&N,3); ninterp=M*N;
     155        iomodel->FetchData(&y_interp,&M,&N,4); _assert_(M*N==ninterp);
    245156
    246157        /*Read options*/
    247158        options = new Options();
     159        iomodel->LastIndex(&N);
     160        numoptions=(int)((N-4)/2);
     161        for(int i=0;i<numoptions;i++){
     162                iomodel->FetchData(&option,5+2*i);
     163                options->AddOption(option);
     164                option=NULL;
     165        }
    248166
    249167        /*Assign output pointer*/
Note: See TracChangeset for help on using the changeset viewer.