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