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