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