xref: /phasta/phSolver/common/d2wall.c (revision 60ca4d1e110f7a14e43bc7ceffb28d8cc6faa627)
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 
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];
141     memset((void*)filename,0,255);
142     *foundd2wall = 0;
143     ////////////////////////////////////////////////////
144     // First we try to read dwal from the restart files.
145     ////////////////////////////////////////////////////
146 
147     sprintf(filename,"restart-dat.%d.%d", timdat.lstep, ((int)(irank/(nprocs/nfiles))+1));
148     queryphmpiio(filename, &nfields, &nppf);
149     initphmpiio(&nfields, &nppf, &nfiles, &f_descriptor, "read");
150 
151     if (irank==0) {
152       printf("Filename is %s \n",filename);
153     }
154     openfile(filename, "read", &f_descriptor);
155 
156     int i;
157     for ( i = 0; i < nppp; i++) { //This loop is useful only if several parts per processor
158       nitems = 2;
159       readheader( &f_descriptor, "dwal", (void*)iarray, &nitems, "double", phasta_iotype);
160       //iarray[ 0 ] = (*numnp); What we should get from readheader
161       //iarray[ 1 ] = 1;
162 
163       if (iarray[0] == (*numnp)) {
164         if (irank==0) {
165           printf("d2wall field found in %s\n",filename);
166         }
167         *foundd2wall = 1;
168         isize = (*numnp);
169         readdatablock( &f_descriptor, "dwal", (void*)(array1), &isize, "double", phasta_iotype );
170       }
171       else { //d2wall fields was not found in the restart file
172         *foundd2wall = 0;
173         if (irank==0) {
174           printf("d2wall field not found in %s - trying d2wall files now\n",filename);
175         }
176       }
177     }
178     closefile(&f_descriptor, "read");
179     finalizephmpiio(&f_descriptor);
180 
181     ////////////////////////////////////////////////////
182     // We try to read dwal from the d2wall files if not found in the restart files
183     ////////////////////////////////////////////////////
184 
185     int numd2wallfiles;
186     if (*foundd2wall == 0) {
187 
188       detectd2wallfiles(&numd2wallfiles);
189 
190       if (numd2wallfiles == outpar.nsynciofiles ) {
191         // Read the d2wall field from the d2wall files
192         memset((void*)filename,0,255);
193 
194         sprintf(filename,"d2wall.%d",((int)(irank/(nprocs/nfiles))+1));
195         queryphmpiio(filename, &nfields, &nppf);
196         initphmpiio(&nfields, &nppf, &nfiles, &f_descriptor, "read");
197 
198         if (irank==0) {
199           printf("Filename is %s \n",filename);
200         }
201         openfile(filename, "read", &f_descriptor);
202 
203         int i;
204         for ( i = 0; i < nppp; i++) { //This loop is useful only if several parts per processor
205           nitems = 2;
206           readheader( &f_descriptor, "d2wall", (void*)iarray, &nitems, "double", phasta_iotype);
207           //iarray[ 0 ] = (*numnp); What we should get from readheader
208           //iarray[ 1 ] = 1;
209 
210           if (iarray[0] == (*numnp)) {
211             if (irank==0) {
212               printf("d2wall field found in %s\n",filename);
213             }
214             *foundd2wall = 1;
215             isize = (*numnp);
216             readdatablock( &f_descriptor, "d2wall", (void*)(array1), &isize, "double", phasta_iotype );
217           }
218           else {
219             *foundd2wall = 0;
220               printf("WARNING - numnp not coherent in d2wall files: %d - %d\n",iarray[0],*numnp);
221               printf("WARNING - Recomputing the d2wall field for safety\n");
222             }
223           }
224 
225           closefile(&f_descriptor, "read");
226           finalizephmpiio(&f_descriptor);
227       }
228       else if (numd2wallfiles != 0) {
229         // The number of d2wall file should be either 0 or outpar.nsynciofiles
230         if (irank==0) {
231           printf("WARNING - Number of d2wall files not coherent: %d - %d\n",numd2wallfiles,outpar.nsynciofiles);
232           printf("WARNING - Recomputing the d2wall field for safety\n");
233           *foundd2wall = 0;
234         }
235       }
236     } // end of tentative reading from d2wall files
237 
238     if (irank==0) {
239       printf("\n");
240     }
241 }
242