1 | /*!\file: kriging.cpp
|
---|
2 | * \brief: kriging main parallel program
|
---|
3 | */
|
---|
4 |
|
---|
5 | #include "./issm.h"
|
---|
6 |
|
---|
7 | /*Local prototypes*/
|
---|
8 | void ProcessArguments2(char** pbinfilename,char** poutbinfilename,char** plockfilename,char** prootpath,int argc,char **argv);
|
---|
9 | void ProcessInputfile(IssmDouble **px,IssmDouble **py,IssmDouble **pdata,int *pnobs,IssmDouble **px_interp,IssmDouble **py_interp,int *pninterp,Options **poptions,FILE* fid);
|
---|
10 |
|
---|
11 | int main(int argc,char **argv){
|
---|
12 |
|
---|
13 | /*I/O: */
|
---|
14 | FILE *output_fid = NULL;
|
---|
15 | FILE *input_fid = NULL;
|
---|
16 |
|
---|
17 | /*File names*/
|
---|
18 | char *lockfilename = NULL;
|
---|
19 | char *binfilename = NULL;
|
---|
20 | char *outbinfilename = NULL;
|
---|
21 | char *rootpath = NULL;
|
---|
22 |
|
---|
23 | /*Input*/
|
---|
24 | int ninterp,nobs;
|
---|
25 | IssmDouble *x = NULL;
|
---|
26 | IssmDouble *y = NULL;
|
---|
27 | IssmDouble *data = NULL;
|
---|
28 | IssmDouble *x_interp = NULL;
|
---|
29 | IssmDouble *y_interp = NULL;
|
---|
30 | Options *options = NULL;
|
---|
31 |
|
---|
32 | /*Output*/
|
---|
33 | IssmDouble *predictions = NULL;
|
---|
34 | IssmDouble *error = NULL;
|
---|
35 |
|
---|
36 | /*Initialize exception trapping: */
|
---|
37 | ExceptionTrapBegin();
|
---|
38 |
|
---|
39 | /*Initialize environment (MPI, PETSC, MUMPS, etc ...)*/
|
---|
40 | ISSM_MPI_Comm comm=EnvironmentInit(argc,argv);
|
---|
41 | IssmComm::SetComm(comm);
|
---|
42 |
|
---|
43 | ProcessArguments2(&binfilename,&outbinfilename,&lockfilename,&rootpath,argc,argv);
|
---|
44 |
|
---|
45 | /*Process input files*/
|
---|
46 | input_fid=pfopen(binfilename,"rb");
|
---|
47 | ProcessInputfile(&x,&y,&data,&nobs,&x_interp,&y_interp,&ninterp,&options,input_fid);
|
---|
48 | pfclose(input_fid,binfilename);
|
---|
49 |
|
---|
50 | _printf0_("call computational core:\n");
|
---|
51 | pKrigingx(&predictions,&error,x,y,data,nobs,x_interp,y_interp,ninterp,options);
|
---|
52 |
|
---|
53 | _printf0_("write results to disk:\n");
|
---|
54 | Results *results = new Results();
|
---|
55 | if(IssmComm::GetRank()==0){
|
---|
56 | output_fid=pfopen0(outbinfilename,"wb");
|
---|
57 | results->AddObject(new GenericExternalResult<double*>(results->Size()+1,0,predictions,ninterp,1,1,0));
|
---|
58 | results->AddObject(new GenericExternalResult<double*>(results->Size()+1,1,error,ninterp,1,1,0));
|
---|
59 | for(int i=0;i<results->Size();i++){
|
---|
60 | ExternalResult* result=xDynamicCast<ExternalResult*>(results->GetObjectByOffset(i));
|
---|
61 | result->WriteData(output_fid,1);
|
---|
62 | }
|
---|
63 | pfclose(output_fid,outbinfilename);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /*Close output and toolkits options file and write lock file if requested*/
|
---|
67 | _printf0_("write lock file:\n");
|
---|
68 | WriteLockFile(lockfilename);
|
---|
69 |
|
---|
70 | /*Free ressources */
|
---|
71 | xDelete<char>(lockfilename);
|
---|
72 | xDelete<char>(binfilename);
|
---|
73 | xDelete<char>(outbinfilename);
|
---|
74 | xDelete<char>(rootpath);
|
---|
75 | xDelete<IssmDouble>(x);
|
---|
76 | xDelete<IssmDouble>(y);
|
---|
77 | xDelete<IssmDouble>(data);
|
---|
78 | xDelete<IssmDouble>(x_interp);
|
---|
79 | xDelete<IssmDouble>(y_interp);
|
---|
80 | xDelete<IssmDouble>(predictions);
|
---|
81 | xDelete<IssmDouble>(error);
|
---|
82 | delete options;
|
---|
83 | delete results;
|
---|
84 |
|
---|
85 | /*Finalize environment:*/
|
---|
86 | EnvironmentFinalize();
|
---|
87 |
|
---|
88 | /*Finalize exception trapping: */
|
---|
89 | ExceptionTrapEnd();
|
---|
90 |
|
---|
91 | return 0; //unix success return;
|
---|
92 | }
|
---|
93 |
|
---|
94 | void ProcessArguments2(char** pbinfilename,char** poutbinfilename,char** plockfilename,char** prootpath,int argc,char **argv){
|
---|
95 | char *modelname = NULL;
|
---|
96 | char *binfilename = NULL;
|
---|
97 | char *outbinfilename = NULL;
|
---|
98 | char *lockfilename = NULL;
|
---|
99 | char *rootpatharg = NULL;
|
---|
100 | char *rootpath = NULL;
|
---|
101 |
|
---|
102 | if(argc<1)_error_("Usage error: no execution path provided");
|
---|
103 | if(argc<2)_error_("Usage error: missing model name");
|
---|
104 |
|
---|
105 | rootpatharg=argv[1];
|
---|
106 | if(strcmp(strstr(rootpatharg,"/"),"/")!=0){
|
---|
107 | rootpath = xNew<char>(strlen(rootpatharg)+2); sprintf(rootpath,"%s/",rootpatharg);
|
---|
108 | }
|
---|
109 | else{
|
---|
110 | rootpath = xNew<char>(strlen(rootpatharg)+1); sprintf(rootpath,"%s",rootpatharg);
|
---|
111 | }
|
---|
112 |
|
---|
113 | modelname=argv[2];
|
---|
114 | if(strstr(modelname,rootpath)==NULL){
|
---|
115 | binfilename = xNew<char>(strlen(rootpath)+strlen(modelname)+strlen(".bin") +1); sprintf(binfilename, "%s%s%s",rootpath,modelname,".bin");
|
---|
116 | outbinfilename = xNew<char>(strlen(rootpath)+strlen(modelname)+strlen(".outbin")+1); sprintf(outbinfilename,"%s%s%s",rootpath,modelname,".outbin");
|
---|
117 | lockfilename = xNew<char>(strlen(rootpath)+strlen(modelname)+strlen(".lock") +1); sprintf(lockfilename, "%s%s%s",rootpath,modelname,".lock");
|
---|
118 | }
|
---|
119 | else{
|
---|
120 | binfilename = xNew<char>(strlen(modelname)+strlen(".bin") +1); sprintf(binfilename, "%s%s",modelname,".bin");
|
---|
121 | outbinfilename = xNew<char>(strlen(modelname)+strlen(".outbin")+1); sprintf(outbinfilename,"%s%s",modelname,".outbin");
|
---|
122 | lockfilename = xNew<char>(strlen(modelname)+strlen(".lock") +1); sprintf(lockfilename, "%s%s",modelname,".lock");
|
---|
123 | }
|
---|
124 |
|
---|
125 | /*Clean up and assign output pointer*/
|
---|
126 | *pbinfilename=binfilename;
|
---|
127 | *poutbinfilename=outbinfilename;
|
---|
128 | *plockfilename=lockfilename;
|
---|
129 | *prootpath=rootpath;
|
---|
130 | }
|
---|
131 |
|
---|
132 | void ProcessInputfile(IssmDouble **px,IssmDouble **py,IssmDouble **pdata,int *pnobs,IssmDouble **px_interp,IssmDouble **py_interp,int *pninterp,Options **poptions,FILE* fid){
|
---|
133 |
|
---|
134 | int ninterp,nobs,numoptions;
|
---|
135 | IssmDouble *x = NULL;
|
---|
136 | IssmDouble *y = NULL;
|
---|
137 | IssmDouble *data = NULL;
|
---|
138 | IssmDouble *x_interp = NULL;
|
---|
139 | IssmDouble *y_interp = NULL;
|
---|
140 | Options *options = NULL;
|
---|
141 | Option *option = NULL;
|
---|
142 |
|
---|
143 | int M,N;
|
---|
144 | IoModel* iomodel = new IoModel();
|
---|
145 | iomodel->fid=fid;
|
---|
146 | iomodel->CheckEnumSync();
|
---|
147 | iomodel->independents=xNew<bool>(MaximumNumberOfDefinitionsEnum); for(int i=0;i<MaximumNumberOfDefinitionsEnum;i++) iomodel->independents[i]=false;
|
---|
148 | iomodel->FetchData(&x,&M,&N,0); nobs=M*N;
|
---|
149 | iomodel->FetchData(&y,&M,&N,1); _assert_(M*N==nobs);
|
---|
150 | iomodel->FetchData(&data,&M,&N,2); _assert_(M*N==nobs);
|
---|
151 | iomodel->FetchData(&x_interp,&M,&N,3); ninterp=M*N;
|
---|
152 | iomodel->FetchData(&y_interp,&M,&N,4); _assert_(M*N==ninterp);
|
---|
153 |
|
---|
154 | /*Read options*/
|
---|
155 | options = new Options();
|
---|
156 | iomodel->LastIndex(&N);
|
---|
157 | numoptions=(int)((N-4)/2);
|
---|
158 | for(int i=0;i<numoptions;i++){
|
---|
159 | iomodel->FetchData(&option,5+2*i);
|
---|
160 | options->AddOption(option);
|
---|
161 | option=NULL;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /*Assign output pointer*/
|
---|
165 | *px = x;
|
---|
166 | *py = y;
|
---|
167 | *pdata = data;
|
---|
168 | *pnobs = nobs;
|
---|
169 | *px_interp = x_interp;
|
---|
170 | *py_interp = y_interp;
|
---|
171 | *pninterp = ninterp;
|
---|
172 | *poptions = options;
|
---|
173 | delete iomodel;
|
---|
174 | }
|
---|