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