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