xref: /phasta/phSolver/common/d2wall.c (revision 96040df829d9dc51fd7a97d28ea5d8fb6af07398)
1 #include <stdlib.h>
2 #include <FCMangle.h>
3 #include <new_interface.h>
4 #include <stdio.h>
5 #include "common_c.h"
6 #include "phastaIO.h"
7 #include "setsyncioparam.h"
8 
9 /*
10 void
11 read_d2wall(  int* pid,
12                int* numnp,
13                double* array1 ) {
14 
15 //    time_t timenow = time ( &timenow);
16     int isize, nitems;
17     int iarray[10];
18 
19     //MPI_Barrier(MPI_COMM_WORLD);
20 
21     /////////////////////////////// Start of writing using new-lib ////////////////////////////
22 
23     int nfiles;
24     int nfields;
25     int numparts;
26     int irank;
27     int nprocs;
28 
29     //  First, count the number of fields to write and store the result in
30     //countfieldstowriterestart();
31 
32     //  Retrieve and compute the parameters required for SyncIO
33     nfiles = outpar.nsynciofiles;
34     //    nfields = 1; //outpar.nsynciofieldswriterestart;  // Only the distance to the walls in d2wall
35     numparts = workfc.numpe;
36     irank = *pid; // workfc.myrank;
37     nprocs = workfc.numpe;
38     int nppf = numparts/nfiles;
39     int GPID;
40 
41     // Calculate number of parts each proc deal with and where it start and end ...
42     int nppp = numparts/nprocs;// nppp : Number of parts per proc ...
43     int startpart = irank * nppp +1;// Part id from which I (myrank) start ...
44     int endpart = startpart + nppp - 1;// Part id to which I (myrank) end ...
45 
46     int descriptor;
47     char filename[255],path[255],fieldtag_s[255];
48     bzero((void*)filename,255);
49     bzero((void*)fieldtag_s,255);
50 
51     sprintf(filename,"d2wall.%d",((int)(irank/(nprocs/nfiles))+1));
52 
53     queryphmpiio(filename, &nfields, &nppf);
54 
55     initphmpiio(&nfields, &nppf, &nfiles, &f_descriptor, "read");
56 
57     if (irank==0) {
58       printf("Filename is %s \n",filename);
59     }
60     openfile(filename, "read", &f_descriptor);
61 
62     field_flag=0;
63 
64     int i;
65     for ( i = 0; i < nppp; i++) { //This loop is useful only if several parts per processor
66     // GPID : global part id, corresponds to rank ...
67       // e.g : (in this example)
68       // proc 0 : 1--4
69       // proc 1 : 5--8 ...
70       GPID = startpart + i;
71 
72       // Write solution field ...
73       sprintf(fieldtag_s,"d2wall@%d",GPID);
74 
75       nitems = 2;
76       readheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, "double", phasta_iotype);
77       //iarray[ 0 ] = (*numnp); What we should get from readheader
78       //iarray[ 1 ] = 1;
79 
80       if (iarray[0] != (*numnp)) {
81         printf("ERROR - numnp in d2wall.dat not coherent %d %d/n", iarray[0], *numnp);
82       }
83 
84       isize = (*numnp);
85       readdatablock( &f_descriptor, fieldtag_s, (void*)(array1), &isize, "double", phasta_iotype );
86 
87     }
88     field_flag++;
89 
90     if (field_flag==1){
91 
92       closefile(&f_descriptor, "read");
93 
94       finalizephmpiio(&f_descriptor);
95 
96       if (irank==0) {
97         printf("\n");
98       }
99     }
100 }
101 */
102 
103 
104 void
105 read_d2wall(  int* pid,
106               int* numnp,
107               double* array1,
108               int* foundd2wall ) {
109 
110 //    time_t timenow = time ( &timenow);
111 
112     int isize, nitems;
113     int iarray[10];
114     int j;
115     for ( j = 0; j < 10; j++) {
116        //Initialize iarray to 0 so that we can assess the result of readheader
117        iarray[j] = 0;
118     }
119 
120     int nfiles;
121     int nfields;
122     int numparts;
123     int irank;
124     int nprocs;
125 
126     //  Retrieve and compute the parameters required for SyncIO
127     nfiles = outpar.nsynciofiles;
128     numparts = workfc.numpe;
129     irank = *pid; // workfc.myrank;
130     nprocs = workfc.numpe;
131     int nppf = numparts/nfiles;
132     int GPID;
133 
134     // Calculate number of parts each proc deal with and where it start and end ...
135     int nppp = numparts/nprocs;// nppp : Number of parts per proc ...
136     int startpart = irank * nppp +1;// Part id from which I (myrank) start ...
137     int endpart = startpart + nppp - 1;// Part id to which I (myrank) end ...
138 
139     int descriptor;
140     char filename[255],path[255],fieldtag_s[255];
141     bzero((void*)filename,255);
142     bzero((void*)fieldtag_s,255);
143     *foundd2wall = 0;
144     ////////////////////////////////////////////////////
145     // First we try to read dwal from the restart files.
146     ////////////////////////////////////////////////////
147 
148     sprintf(filename,"restart-dat.%d.%d", timdat.lstep, ((int)(irank/(nprocs/nfiles))+1));
149     queryphmpiio(filename, &nfields, &nppf);
150     initphmpiio(&nfields, &nppf, &nfiles, &f_descriptor, "read");
151 
152     if (irank==0) {
153       printf("Filename is %s \n",filename);
154     }
155     openfile(filename, "read", &f_descriptor);
156 
157     int i;
158     for ( i = 0; i < nppp; i++) { //This loop is useful only if several parts per processor
159     // GPID : global part id, corresponds to rank ...
160       // e.g : (in this example)
161       // proc 0 : 1--4
162       // proc 1 : 5--8 ...
163       GPID = startpart + i;
164 
165       // Write solution field ...
166       sprintf(fieldtag_s,"dwal@%d",GPID);
167 
168       nitems = 2;
169       readheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, "double", phasta_iotype);
170       //iarray[ 0 ] = (*numnp); What we should get from readheader
171       //iarray[ 1 ] = 1;
172 
173       if (iarray[0] == (*numnp)) {
174         if (irank==0) {
175           printf("d2wall field found in %s\n",filename);
176         }
177         *foundd2wall = 1;
178         isize = (*numnp);
179         readdatablock( &f_descriptor, fieldtag_s, (void*)(array1), &isize, "double", phasta_iotype );
180       }
181       else { //d2wall fields was not found in the restart file
182         *foundd2wall = 0;
183         if (irank==0) {
184           printf("d2wall field not found in %s - trying d2wall files now\n",filename);
185         }
186       }
187     }
188     closefile(&f_descriptor, "read");
189     finalizephmpiio(&f_descriptor);
190 
191     ////////////////////////////////////////////////////
192     // We try to read dwal from the d2wall files if not found in the restart files
193     ////////////////////////////////////////////////////
194 
195     int numd2wallfiles;
196     if (*foundd2wall == 0) {
197 
198       detectd2wallfiles(&numd2wallfiles);
199 
200       if (numd2wallfiles == outpar.nsynciofiles ) {
201         // Read the d2wall field from the d2wall files
202         bzero((void*)filename,255);
203         bzero((void*)fieldtag_s,255);
204 
205         sprintf(filename,"d2wall.%d",((int)(irank/(nprocs/nfiles))+1));
206         queryphmpiio(filename, &nfields, &nppf);
207         initphmpiio(&nfields, &nppf, &nfiles, &f_descriptor, "read");
208 
209         if (irank==0) {
210           printf("Filename is %s \n",filename);
211         }
212         openfile(filename, "read", &f_descriptor);
213 
214         int i;
215         for ( i = 0; i < nppp; i++) { //This loop is useful only if several parts per processor
216           // GPID : global part id, corresponds to rank ...
217           // e.g : (in this example)
218           // proc 0 : 1--4
219           // proc 1 : 5--8 ...
220           GPID = startpart + i;
221 
222           // Write solution field ...
223           sprintf(fieldtag_s,"d2wall@%d",GPID);
224 
225           nitems = 2;
226           readheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, "double", phasta_iotype);
227           //iarray[ 0 ] = (*numnp); What we should get from readheader
228           //iarray[ 1 ] = 1;
229 
230           if (iarray[0] == (*numnp)) {
231             if (irank==0) {
232               printf("d2wall field found in %s\n",filename);
233             }
234             *foundd2wall = 1;
235             isize = (*numnp);
236             readdatablock( &f_descriptor, fieldtag_s, (void*)(array1), &isize, "double", phasta_iotype );
237           }
238           else {
239             *foundd2wall = 0;
240               printf("WARNING - numnp not coherent in d2wall files: %d - %d\n",iarray[0],*numnp);
241               printf("WARNING - Recomputing the d2wall field for safety\n");
242             }
243           }
244 
245           closefile(&f_descriptor, "read");
246           finalizephmpiio(&f_descriptor);
247       }
248       else if (numd2wallfiles != 0) {
249         // The number of d2wall file should be either 0 or outpar.nsynciofiles
250         if (irank==0) {
251           printf("WARNING - Number of d2wall files not coherent: %d - %d\n",numd2wallfiles,outpar.nsynciofiles);
252           printf("WARNING - Recomputing the d2wall field for safety\n");
253           *foundd2wall = 0;
254         }
255       }
256     } // end of tentative reading from d2wall files
257 
258     if (irank==0) {
259       printf("\n");
260     }
261 }
262