xref: /phasta/AcuStat/src/new_interface.c (revision 595995161822a203c8467e0e4a253d7bd7d6df32)
1 /* This file provides interface functions for 'partial ' random
2    access into the PHASTA input files
3 
4    Anil Karanam March 2001 */
5 
6 #include <stdio.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <time.h>
11 #include <math.h>
12 #include <mpi.h>
13 #include "phastaIO.h"
14 #include <FCMangle.h>
15 #include "new_interfaceAcuStat.h"
16 
17 //MR CHANGE
18 #include "commonAcuStat_c.h"
19 //MR CHANGE END
20 
21 #ifdef intel
22 #include <winsock2.h>
23 #else
24 #include <unistd.h>
25 #include <strings.h>
26 #endif
27 
28 
29 void
30 Write_AcuStat(  int* pid,
31                 int* stepstart,
32                 int* stepend,
33                 int* steptot,
34                 int* nshg,
35                 int* numVars,
36                 double* array1 ) {
37 
38     char fname[255];
39     char rfile[60];
40     char existingfile[30], linkfile[30];
41     int irstou;
42     int magic_number = 362436;
43     int* mptr = &magic_number;
44     double version=0.0;
45     int isize, nitems;
46     int iarray[10];
47     int nfiles;
48     int nfields;
49     int numparts;
50     int irank;
51     int nprocs;
52 
53 
54     irank = *pid;
55 
56     //  Retrieve and compute the parameters required for SyncIO
57     nfiles = outpar.nsynciofiles; //We use the same number of files as before
58     nfields = outpar.nsynciofieldswriterestart; // >=4
59     numparts = workfc.numpe;
60     nprocs= workfc.numpe;
61 
62     int nppf = numparts/nfiles;
63     int GPID;
64 
65     // Calculate number of parts each proc deal with and where it start and end ...
66     int nppp = numparts/nprocs;// nppp : Number of parts per proc ...
67     int startpart = irank * nppp +1;// Part id from which I (myrank) start ...
68     int endpart = startpart + nppp - 1;// Part id to which I (myrank) end ...
69 
70     int descriptor;
71     char filename[255],path[255],fieldtag_s[255];
72     bzero((void*)filename,255);
73     bzero((void*)fieldtag_s,255);
74 
75     //sprintf(filename,"restartAcuStat-dat.%d.%d",*stepend,((int)(irank/(nprocs/nfiles))+1));
76     sprintf(filename,"restartAcuStat.%d.%d.%d.%d",*stepstart,*stepend,*steptot,((int)(irank/(nprocs/nfiles))+1));
77     if (*pid==0) {
78 	    printf("Filename is %s \n",filename);
79     }
80 
81     initphmpiio(&nfields, &nppf, &nfiles, &f_descriptor, "write");
82     openfile(filename, "write", &f_descriptor);
83 
84     field_flag=0;
85     // solution
86     int i;
87     for ( i = 0; i < nppp; i++) { //This loop is useful only if several parts per processor
88 	    // GPID : global part id, corresponds to rank ...
89 	    // e.g : (in this example)
90 	    // proc 0 : 1--4
91 	    // proc 1 : 5--8 ...
92 	    GPID = startpart + i;
93 
94 	    // Write solution field ...
95 	    sprintf(fieldtag_s,"solution@%d",GPID);
96 
97 	    isize = (*nshg)*(*numVars);
98 	    nitems = 3;
99 	    iarray[ 0 ] = (*nshg);
100 	    iarray[ 1 ] = (*numVars);
101 	    iarray[ 2 ] = (*stepend);
102 	    writeheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, &isize, "double", phasta_iotype);
103 	    writedatablock( &f_descriptor, fieldtag_s, (void*)(array1), &isize, "double", phasta_iotype );
104     }
105     field_flag++;
106 
107     if (field_flag==nfields){
108       closefile(&f_descriptor, "write");
109       finalizephmpiio(&f_descriptor);
110       printf("file closed\n");
111       if (*pid==0) {
112         printf("\n");
113       }
114     }
115 
116     return;
117 }
118 
119 
120 
121 void
122 Write_AcuStat2(  int* pid,
123                 int* stepstart,
124                 int* stepend,
125                 int* steptot,
126                 int* nshg,
127                 int* numVars,
128                 int* ndoferrors,
129                 int* ndofybar,
130                 double* array1,
131                 double* array2,
132                 double* array3,
133                 double* array4 ) {
134 
135     char fname[255];
136     char rfile[60];
137     char existingfile[30], linkfile[30];
138     int irstou;
139     int magic_number = 362436;
140     int* mptr = &magic_number;
141     double version=0.0;
142     int isize, nitems;
143     int iarray[10];
144     int nfiles;
145     int nfields;
146     int numparts;
147     int irank;
148     int nprocs;
149 
150 
151     irank = *pid;
152 
153     //  Retrieve and compute the parameters required for SyncIO
154     nfiles = outpar.nsynciofiles; //We use the same number of files as before
155     nfields = outpar.nsynciofieldswriterestart; // >=4
156     numparts = workfc.numpe;
157     nprocs= workfc.numpe;
158 
159     int nppf = numparts/nfiles;
160     int GPID;
161 
162     // Calculate number of parts each proc deal with and where it start and end ...
163     int nppp = numparts/nprocs;// nppp : Number of parts per proc ...
164     int startpart = irank * nppp +1;// Part id from which I (myrank) start ...
165     int endpart = startpart + nppp - 1;// Part id to which I (myrank) end ...
166 
167     int descriptor;
168     char filename[255],fieldtag_s[255];
169     bzero((void*)filename,255);
170     bzero((void*)fieldtag_s,255);
171 
172     sprintf(filename,"restartAcuStat.%d.%d.%d.%d",*stepstart,*stepend,*steptot,((int)(irank/(nprocs/nfiles))+1));
173     if (*pid==0) {
174 	    printf("Filename is %s \n",filename);
175     }
176 
177     initphmpiio(&nfields, &nppf, &nfiles, &f_descriptor, "write");
178     openfile(filename, "write", &f_descriptor);
179 
180     field_flag=0;
181     // solution
182     int i;
183     for ( i = 0; i < nppp; i++) { //This loop is useful only if several parts per processor
184 	    // GPID : global part id, corresponds to rank ...
185 	    // e.g : (in this example)
186 	    // proc 0 : 1--4
187 	    // proc 1 : 5--8 ...
188 	    GPID = startpart + i;
189 
190 	    // Write solution field ...
191 	    sprintf(fieldtag_s,"solution@%d",GPID);
192 
193 	    isize = (*nshg)*(*numVars);
194 	    nitems = 3;
195 	    iarray[ 0 ] = (*nshg);
196 	    iarray[ 1 ] = (*numVars);
197 	    iarray[ 2 ] = (*stepend);
198 	    writeheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, &isize, "double", phasta_iotype);
199 	    writedatablock( &f_descriptor, fieldtag_s, (void*)(array1), &isize, "double", phasta_iotype );
200     }
201     field_flag++;
202 
203     // dwal
204     for ( i = 0; i < nppp; i++) { //This loop is useful only if several parts per processor
205 	    // GPID : global part id, corresponds to rank ...
206 	    // e.g : (in this example)
207 	    // proc 0 : 1--4
208 	    // proc 1 : 5--8 ...
209 	    GPID = startpart + i;
210 
211 	    // Write solution field ...
212 	    sprintf(fieldtag_s,"dwal@%d",GPID);
213 
214 	    isize = (*nshg)*1;
215 	    nitems = 3;
216 	    iarray[ 0 ] = (*nshg);
217 	    iarray[ 1 ] = 1;
218 	    iarray[ 2 ] = (*stepend);
219 	    writeheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, &isize, "double", phasta_iotype);
220 	    writedatablock( &f_descriptor, fieldtag_s, (void*)(array2), &isize, "double", phasta_iotype );
221     }
222     field_flag++;
223 
224     // errors
225     for ( i = 0; i < nppp; i++) { //This loop is useful only if several parts per processor
226 	    // GPID : global part id, corresponds to rank ...
227 	    // e.g : (in this example)
228 	    // proc 0 : 1--4
229 	    // proc 1 : 5--8 ...
230 	    GPID = startpart + i;
231 
232 	    // Write solution field ...
233 	    sprintf(fieldtag_s,"errors@%d",GPID);
234 
235 	    isize = (*nshg)*(*ndoferrors);
236 	    nitems = 3;
237 	    iarray[ 0 ] = (*nshg);
238 	    iarray[ 1 ] = (*ndoferrors);
239 	    iarray[ 2 ] = (*stepend);
240 	    writeheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, &isize, "double", phasta_iotype);
241 	    writedatablock( &f_descriptor, fieldtag_s, (void*)(array3), &isize, "double", phasta_iotype );
242     }
243     field_flag++;
244 
245     // ybar
246     for ( i = 0; i < nppp; i++) { //This loop is useful only if several parts per processor
247 	    // GPID : global part id, corresponds to rank ...
248 	    // e.g : (in this example)
249 	    // proc 0 : 1--4
250 	    // proc 1 : 5--8 ...
251 	    GPID = startpart + i;
252 
253 	    // Write solution field ...
254 	    sprintf(fieldtag_s,"ybar@%d",GPID);
255 
256 	    isize = (*nshg)*(*ndofybar);
257 	    nitems = 3;
258 	    iarray[ 0 ] = (*nshg);
259 	    iarray[ 1 ] = (*ndofybar);
260 	    iarray[ 2 ] = (*stepend);
261 	    writeheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, &isize, "double", phasta_iotype);
262 	    writedatablock( &f_descriptor, fieldtag_s, (void*)(array4), &isize, "double", phasta_iotype );
263     }
264     field_flag++;
265 
266 
267     if (field_flag==nfields){
268       closefile(&f_descriptor, "write");
269       finalizephmpiio(&f_descriptor);
270       printf("file closed\n");
271       if (*pid==0) {
272         printf("\n");
273       }
274     }
275 
276 
277     return;
278 }
279 
280 void
281 Write_Ybar(     int* pid,
282                 int* stepstart,
283                 int* stepend,
284                 int* steptot,
285                 int* nshg,
286                 int* numVars,
287                 double* array1) {
288 
289     char fname[255];
290     char rfile[60];
291     char existingfile[30], linkfile[30];
292     int irstou;
293     int magic_number = 362436;
294     int* mptr = &magic_number;
295     double version=0.0;
296     int isize, nitems;
297     int iarray[10];
298 
299     int nfiles;
300     int nfields;
301     int numparts;
302     int irank;
303     int nprocs;
304 
305     //  First, count the number of fields to write and store the result in
306     //countfieldstowriterestart();
307 
308     //  Retrieve and compute the parameters required for SyncIO
309     nfiles = outpar.nsynciofiles;
310     //nfields = outpar.nsynciofieldswriterestart;
311     nfields=1;
312     numparts = workfc.numpe;
313     irank = *pid; //workfc.myrank;
314     nprocs = workfc.numpe;
315     int nppf = numparts/nfiles;
316     int GPID;
317 
318     // Calculate number of parts each proc deal with and where it start and end ...
319     int nppp = numparts/nprocs;// nppp : Number of parts per proc ...
320     int startpart = irank * nppp +1;// Part id from which I (myrank) start ...
321     int endpart = startpart + nppp - 1;// Part id to which I (myrank) end ...
322 
323     int descriptor;
324     char filename[255],fieldtag_s[255];
325     bzero((void*)filename,255);
326     bzero((void*)fieldtag_s,255);
327 
328 //    sprintf(filename,"ybar.%d.%d",*stepno,((int)(irank/(nprocs/nfiles))+1));
329     sprintf(filename,"restartAcuStat.%d.%d.%d.%d",*stepstart,*stepend,*steptot,((int)(irank/(nprocs/nfiles))+1));
330 
331     initphmpiio(&nfields, &nppf, &nfiles, &f_descriptor, "write");
332 
333     if (*pid==0) {
334       printf("Filename is %s \n",filename);
335     }
336 
337     openfile(filename, "write", &f_descriptor);
338 
339     field_flag=0;
340 
341      int i;
342      for ( i = 0; i < nppp; i++) { //This loop is useful only if several parts per processor
343      // GPID : global part id, corresponds to rank ...
344         // e.g : (in this example)
345         // proc 0 : 1--4
346         // proc 1 : 5--8 ...
347         GPID = startpart + i;
348 
349         // Write ybar field ...
350         sprintf(fieldtag_s,"ybar@%d",GPID);
351 
352         isize = (*nshg)*(*numVars);
353         nitems = 3;
354         iarray[ 0 ] = (*nshg);
355         iarray[ 1 ] = (*numVars);
356         iarray[ 2 ] = (*stepend);
357         writeheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, &isize, "double", phasta_iotype);
358         writedatablock( &f_descriptor, fieldtag_s, (void*)(array1), &isize, "double", phasta_iotype );
359     }
360     field_flag++;
361 
362     if (field_flag==nfields){
363       closefile(&f_descriptor, "write");
364       finalizephmpiio(&f_descriptor);
365       if (*pid==0) {
366         printf("\n");
367       }
368     }
369 }
370 
371 void
372 Write_Restart(  int* pid,
373                 int* stepno,
374                 int* nshg,
375                 int* numVars,
376                 double* array1,
377                 double* array2 ) {
378 
379     char fname[255];
380     char rfile[60];
381     char existingfile[30], linkfile[30];
382     int irstou;
383     int magic_number = 362436;
384     int* mptr = &magic_number;
385     double version=0.0;
386     int isize, nitems;
387     int iarray[10];
388     int nfiles;
389     int nfields;
390     int numparts;
391     int irank;
392     int nprocs;
393 
394     //  First, count the number of fields to write and store the result in
395     //countfieldstowriterestart();
396 
397     //  Retrieve and compute the parameters required for SyncIO
398     nfiles = outpar.nsynciofiles;
399     nfields = outpar.nsynciofieldswriterestart;
400     numparts = workfc.numpe;
401     irank = *pid; //workfc.myrank;
402     nprocs = workfc.numpe;
403     int nppf = numparts/nfiles;
404     int GPID;
405 
406     // Calculate number of parts each proc deal with and where it start and end ...
407     int nppp = numparts/nprocs;// nppp : Number of parts per proc ...
408     int startpart = irank * nppp +1;// Part id from which I (myrank) start ...
409     int endpart = startpart + nppp - 1;// Part id to which I (myrank) end ...
410 
411     int descriptor;
412     char filename[255],fieldtag_s[255];
413     bzero((void*)filename,255);
414     bzero((void*)fieldtag_s,255);
415 
416     sprintf(filename,"restart-dat.%d.%d",*stepno,((int)(irank/(nprocs/nfiles))+1));
417 
418     initphmpiio(&nfields, &nppf, &nfiles, &f_descriptor, "write");
419 
420     if (*pid==0) {
421       printf("Filename is %s \n",filename);
422     }
423 
424     openfile(filename, "write", &f_descriptor);
425 
426     field_flag=0;
427 
428      int i;
429      for ( i = 0; i < nppp; i++) { //This loop is useful only if several parts per processor
430      // GPID : global part id, corresponds to rank ...
431         // e.g : (in this example)
432         // proc 0 : 1--4
433         // proc 1 : 5--8 ...
434         GPID = startpart + i;
435 
436         // Write solution field ...
437         sprintf(fieldtag_s,"solution@%d",GPID);
438 
439         isize = (*nshg)*(*numVars);
440         nitems = 3;
441         iarray[ 0 ] = (*nshg);
442         iarray[ 1 ] = (*numVars);
443         iarray[ 2 ] = (*stepno);
444         writeheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, &isize, "double", phasta_iotype);
445         writedatablock( &f_descriptor, fieldtag_s, (void*)(array1), &isize, "double", phasta_iotype );
446      }
447      field_flag++;
448 
449      for ( i = 0; i < nppp; i++) {
450 
451         // GPID : global part id, corresponds to rank ...
452         // e.g : (in this example)
453         // proc 0 : 1--4
454         // proc 1 : 5--8 ...
455         GPID = startpart + i;
456 
457         // Write solution field ...
458         sprintf(fieldtag_s,"time derivative of solution@%d",GPID);
459 
460         isize = (*nshg)*(*numVars);
461         nitems = 3;
462         iarray[ 0 ] = (*nshg);
463         iarray[ 1 ] = (*numVars);
464         iarray[ 2 ] = (*stepno);
465         writeheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, &isize, "double", phasta_iotype);
466         writedatablock( &f_descriptor, fieldtag_s, (void*)(array2), &isize, "double", phasta_iotype );
467      }
468      field_flag++;
469 
470      if (field_flag==nfields){
471        closefile(&f_descriptor, "write");
472        finalizephmpiio(&f_descriptor);
473        if (*pid==0) {
474          printf("\n");
475        }
476      }
477 }
478 
479 
480 void
481 Write_Error(  int* pid,
482               int* stepno,
483               int* nshg,
484               int* numVars,
485               double* array1 ) {
486 
487 
488     char fname[255];
489     char rfile[60];
490     int irstou;
491     int magic_number = 362436;
492     int* mptr = &magic_number;
493     double version=0.0;
494     int isize, nitems;
495     int iarray[10];
496 
497     /////////////////////////////// Start of writing using new-lib ////////////////////////////
498 
499     int nfiles;
500     int nfields;
501     int numparts;
502     int irank;
503     int nprocs;
504 
505     nfiles = outpar.nsynciofiles;
506     nfields = outpar.nsynciofieldswriterestart;
507     numparts = workfc.numpe;
508     irank = *pid; //workfc.myrank;
509     nprocs = workfc.numpe;
510 
511     int nppf = numparts/nfiles;
512     int GPID;
513 
514     // Calculate number of parts each  proc deal with and where it start and end ...
515     int nppp = numparts/nprocs;// nppp : Number of parts per proc ...
516     int startpart = irank * nppp +1;// Part id from which I (myrank) start ...
517     int endpart = startpart + nppp - 1;// Part id to which I (myrank) end ...
518 
519     field_flag++;
520 
521     char fieldtag[255];
522 
523     int i;
524     for ( i = 0; i < nppp; i++  ) {
525         GPID = startpart + i;
526         sprintf(fieldtag,"errors@%d",GPID);
527 
528         if(*pid==0) {
529           printf("\n");
530           printf("The %d/%d th field to be written is '%s'\n",field_flag,nfields,fieldtag);
531         }
532 
533         isize = (*nshg)*(*numVars);
534         nitems = 3;
535         iarray[ 0 ] = (*nshg);
536         iarray[ 1 ] = (*numVars);
537         iarray[ 2 ] = (*stepno);
538         writeheader( &f_descriptor, fieldtag, (void*)iarray, &nitems, &isize, "double", phasta_iotype);
539         writedatablock( &f_descriptor, fieldtag, (void*)array1, &isize, "double", phasta_iotype );
540     }
541 
542     if (field_flag==nfields){
543       closefile(&f_descriptor, "write");
544       finalizephmpiio(&f_descriptor);
545       if (*pid==0) {
546         printf("Last field %d '%s' finished! \n",nfields, fieldtag);
547         printf("\n");
548       }
549     }
550 
551 }
552 
553 void
554 Write_Displ(  int* pid,
555               int* stepno,
556               int* nshg,
557               int* numVars,
558               double* array1 ) { //TO BE UPDATED FOR SYNCIO
559 
560 
561     char fname[255];
562     char rfile[60];
563     int irstou;
564     int magic_number = 362436;
565     int* mptr = &magic_number;
566     time_t timenow = time ( &timenow);
567     double version=0.0;
568     int isize, nitems;
569     int iarray[10];
570 
571     sprintf(rfile,"restart.%d.%d",*stepno,*pid+1);
572     openfile(rfile,"append", &irstou);
573 
574     isize = (*nshg)*(*numVars);
575     nitems = 3;
576     iarray[ 0 ] = (*nshg);
577     iarray[ 1 ] = (*numVars);
578     iarray[ 2 ] = (*stepno);
579     writeheader( &irstou, "displacement", (void*)iarray, &nitems, &isize, "double", phasta_iotype );
580     writedatablock( &irstou, "displacement", (void*)(array1), &isize, "double", phasta_iotype );
581 
582     closefile( &irstou, "append" );
583 }
584 
585 void
586 Write_Field(  int *pid,
587               char* filemode,
588               char* fieldtag,
589               int* tagsize,
590               void* array,
591               char* arraytype,
592               int* nshg,
593               int* numvars,
594               int* stepno) {
595 
596     //printf("Rank is %d, field is %s, tagsize is %d, nshg is %d, numvars is %d\n",*pid,fieldtag,*tagsize,*nshg,*numvars);
597 
598     char *fieldlabel = (char *)malloc((*tagsize+1)*sizeof(char));
599     strncpy(fieldlabel, fieldtag, *tagsize);
600     fieldlabel[*tagsize] = '\0';
601 
602     int irstou;
603     int magic_number = 362436;
604     int* mptr = &magic_number;
605     double version=0.0;
606     int isize, nitems;
607     int iarray[10];
608 
609     char fmode[10];
610     if(!strncmp(filemode,"w",1))
611       strcpy(fmode,"write");
612     else // default is append
613       strcpy(fmode,"append");
614 
615     char datatype[10];
616     if(!strncmp(arraytype,"i",1))
617       strcpy(datatype,"int");
618     else // default is double
619       strcpy(datatype,"double");
620 
621 //   Old posix format
622 /*     openfile_(rfile, fmode, &irstou);
623 
624      nitems = 3; // assuming field will write 3 items in iarray
625      iarray[ 0 ] = (*nshg);
626      iarray[ 1 ] = (*numvars);
627      iarray[ 2 ] = (*stepno);
628 
629      isize = (*nshg)*(*numvars);
630      writeheader_( &irstou, fieldlabel, (void*)iarray, &nitems, &isize, datatype, phasta_iotype );
631 
632      nitems = (*nshg)*(*numvars);
633      writedatablock_( &irstou, fieldlabel, array, &nitems, datatype, phasta_iotype );
634      closefile_( &irstou, fmode);
635 */
636     /////////////////////////////// Start of writing using new-lib ////////////////////////////
637 
638     int nfiles;
639     int nfields;
640     int numparts;
641     int irank;
642     int nprocs;
643 
644 //    unsigned long long timer_start;
645 //    unsigned long long timer_end;
646 //    double time_span;
647 
648     nfiles = outpar.nsynciofiles;
649     nfields = outpar.nsynciofieldswriterestart;
650     numparts = workfc.numpe;
651     irank = *pid; //workfc.myrank;
652     nprocs = workfc.numpe;
653 
654     int nppf = numparts/nfiles;
655     int GPID;
656 
657     // Calculate number of parts each  proc deal with and where it start and end ...
658     int nppp = numparts/nprocs;// nppp : Number of parts per proc ...
659     int startpart = irank * nppp +1;// Part id from which I (myrank) start ...
660     int endpart = startpart + nppp - 1;// Part id to which I (myrank) end ...
661 
662     char fieldtag_s[255];
663     bzero((void*)fieldtag_s,255);
664 
665     strncpy(fieldlabel, fieldtag, *tagsize);
666 
667     field_flag++;
668     if(*pid==0) {
669       printf("\n");
670       printf("The %d/%d th field to be written is '%s'\n",field_flag,nfields,fieldlabel);
671     }
672 
673     int i;
674     for ( i = 0; i < nppp; i++  ) {
675         GPID = startpart + i;
676 
677         // Write solution field ...
678         sprintf(fieldtag_s,"%s@%d",fieldlabel,GPID);
679 
680         isize = (*nshg)*(*numvars);
681         nitems = 3;
682         iarray[ 0 ] = (*nshg);
683         iarray[ 1 ] = (*numvars);
684         iarray[ 2 ] = (*stepno);
685         writeheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, &isize, datatype, phasta_iotype);
686         writedatablock( &f_descriptor, fieldtag_s, array, &isize, datatype, phasta_iotype );
687     }
688 
689     if (field_flag==nfields){
690       closefile(&f_descriptor, "write");
691       finalizephmpiio(&f_descriptor);
692       if (*pid==0) {
693         printf("Last field %d '%s' finished! \n",nfields, fieldtag);
694         printf("\n");
695       }
696     }
697 
698     free(fieldlabel);
699 }
700 
701 void
702 Write_PhAvg(  int* pid,
703               char* filemode,
704               char* fieldtag,
705               int* tagsize,
706               int* iphase,
707               void* array,
708               char* arraytype,
709               int* nshg,
710               int* numvars,
711               int* stepno) {
712 
713     char rfile[32];
714     // assuming restart_phase_avg_<sn>.<iphase>.<pid+1>
715     sprintf(rfile,"restart_phase_avg_%d.%d.%d",*stepno,*iphase,*pid+1);
716 
717     char *fieldlabel = (char *)malloc((*tagsize+1)*sizeof(char));
718     strncpy(fieldlabel, fieldtag, *tagsize);
719     fieldlabel[*tagsize] = '\0';
720 
721     int irstou;
722     int isize, nitems;
723     int iarray[10];
724 
725     char fmode[10];
726     if(!strncmp(filemode,"w",1))
727       strcpy(fmode,"write");
728     else // default is append
729       strcpy(fmode,"append");
730 
731     char datatype[10];
732     if(!strncmp(arraytype,"i",1))
733       strcpy(datatype,"int");
734     else // default is double
735       strcpy(datatype,"double");
736 
737     openfile(rfile, fmode, &irstou);
738 
739     if(!strcmp(fmode,"write")) {
740       // may be create a routine for 'top' portion under write mode
741       int magic_number = 362436;
742       int* mptr = &magic_number;
743       time_t timenow = time ( &timenow);
744       double version=0.0;
745 
746       /* writing the top ascii header for the restart file */
747 
748       writestring( &irstou,"# PHASTA Input File Version 2.0\n");
749       writestring( &irstou,
750                     "# format \"keyphrase : sizeofnextblock usual headers\"\n");
751 
752       char fname[255];
753       bzero( (void*)fname, 255 );
754       sprintf(fname,"# Output generated by phasta version (NOT YET CURRENT): %lf \n", version);
755       writestring( &irstou, fname );
756 
757       bzero( (void*)fname, 255 );
758       gethostname(fname,255);
759       writestring( &irstou,"# This result was produced on: ");
760       writestring( &irstou, fname );
761       writestring( &irstou,"\n");
762 
763       bzero( (void*)fname, 255 );
764       sprintf(fname,"# %s\n", ctime( &timenow ));
765       writestring( &irstou, fname );
766 
767       isize = 1;
768       nitems = 1;
769       iarray[ 0 ] = 1;
770       writeheader( &irstou, "byteorder magic number ",
771                     (void*)iarray, &nitems, &isize, "integer", phasta_iotype );
772       writedatablock( &irstou, "byteorder magic number ",
773                        (void*)mptr, &isize, "integer", phasta_iotype );
774     }
775 
776     isize = (*nshg)*(*numvars);
777     nitems = 3; // assuming field will write 3 items in iarray
778     iarray[ 0 ] = (*nshg);
779     iarray[ 1 ] = (*numvars);
780     iarray[ 2 ] = (*stepno);
781     writeheader( &irstou, fieldlabel, (void*)iarray, &nitems, &isize, datatype, phasta_iotype );
782     writedatablock( &irstou, fieldlabel, array, &isize, datatype, phasta_iotype );
783 
784     closefile( &irstou, fmode);
785 
786     free(fieldlabel);
787 }
788 
789 void
790 Write_PhAvg2( int* pid,
791               char* filemode,
792               char* fieldtag,
793               int* tagsize,
794               int* iphase,
795               int* nphasesincycle,
796               void* array,
797               char* arraytype,
798               int* nshg,
799               int* numvars,
800               int* stepno) {
801 
802     int addtagsize; // phase number is added to the name of the field
803     if(*iphase<10)
804       addtagsize=1;
805     else if(*iphase<100)
806       addtagsize=2;
807     else if(*iphase<1000)
808       addtagsize=3;
809 
810     int tagsize2;
811     tagsize2=*tagsize+addtagsize;
812 
813     char *fieldlabel = (char *)malloc((tagsize2+1)*sizeof(char));
814     strncpy(fieldlabel, fieldtag, *tagsize);
815     fieldlabel[tagsize2] = '\0';
816 
817     char straddtagsize[10];
818     sprintf(straddtagsize,"%d",*iphase);
819 
820     if(*iphase<10) {
821       fieldlabel[tagsize2-1]=straddtagsize[0];
822     }
823     else if(*iphase<100) {
824       fieldlabel[tagsize2-2]=straddtagsize[0];
825       fieldlabel[tagsize2-1]=straddtagsize[1];
826     }
827     else if(*iphase<1000) {
828       fieldlabel[tagsize2-3]=straddtagsize[0];
829       fieldlabel[tagsize2-2]=straddtagsize[1];
830       fieldlabel[tagsize2-1]=straddtagsize[2];
831     }
832 
833     int irstou;
834     int magic_number = 362436;
835     int* mptr = &magic_number;
836     double version=0.0;
837     int isize, nitems;
838     int iarray[10];
839 
840     char fmode[10];
841     if(!strncmp(filemode,"w",1))
842       strcpy(fmode,"write");
843     else // default is append
844       strcpy(fmode,"append");
845 
846     char datatype[10];
847     if(!strncmp(arraytype,"i",1))
848       strcpy(datatype,"int");
849     else // default is double
850       strcpy(datatype,"double");
851 
852     /////////////////////////////// Start of writing using new-lib ////////////////////////////
853 
854     int nfiles;
855     int nfields;
856     int numparts;
857     int irank;
858     int nprocs;
859 
860     nfiles = outpar.nsynciofiles;
861     nfields = outpar.nsynciofieldswriterestart;
862     numparts = workfc.numpe;
863     irank = *pid; //workfc.myrank;
864     nprocs = workfc.numpe;
865 
866     int nppf = numparts/nfiles;
867     int GPID;
868 
869     // Calculate number of parts each  proc deal with and where it start and end ...
870     int nppp = numparts/nprocs;// nppp : Number of parts per proc ...
871     int startpart = irank * nppp +1;// Part id from which I (myrank) start ...
872     int endpart = startpart + nppp - 1;// Part id to which I (myrank) end ...
873 
874     char fieldtag_s[255];
875     bzero((void*)fieldtag_s,255);
876 
877     field_flag++;
878     if(*pid==0) {
879       printf("\n");
880       printf("The %d/%d th field to be written is '%s'\n",field_flag,nfields,fieldlabel);
881     }
882 
883     int i;
884     for ( i = 0; i < nppp; i++  ) {
885         GPID = startpart + i;
886 
887         // Write solution field ...
888         sprintf(fieldtag_s,"%s@%d",fieldlabel,GPID);
889 
890         isize = (*nshg)*(*numvars);
891         nitems = 3;
892         iarray[ 0 ] = (*nshg);
893         iarray[ 1 ] = (*numvars);
894         iarray[ 2 ] = (*stepno);
895         writeheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, &isize, "double", phasta_iotype);
896         writedatablock( &f_descriptor, fieldtag_s, array, &isize, "double", phasta_iotype );
897     }
898 
899     if (field_flag==nfields){
900       closefile(&f_descriptor, "write");
901       finalizephmpiio(&f_descriptor);
902       if (*pid==0) {
903         printf("\n");
904       }
905     }
906 
907     free(fieldlabel);
908 }
909 
910 
911 void
912 Write_d2wall(   int* pid,
913                 int* numnp,
914                 double* array1 ) {
915 
916     int isize, nitems;
917     int iarray[10];
918 
919     /////////////////////////////// Start of writing using new-lib ////////////////////////////
920 
921     int nfiles;
922     int nfields;
923     int numparts;
924     int irank;
925     int nprocs;
926 
927     //  First, count the number of fields to write and store the result in
928     //countfieldstowriterestart();
929 
930     //  Retrieve and compute the parameters required for SyncIO
931     nfiles = outpar.nsynciofiles;
932     nfields = 1; //outpar.nsynciofieldswriterestart;  // Only the distance to the walls in d2wall
933     numparts = workfc.numpe;
934     irank = *pid; //workfc.myrank;
935     nprocs = workfc.numpe;
936     int nppf = numparts/nfiles;
937     int GPID;
938 
939     // Calculate number of parts each proc deal with and where it start and end ...
940     int nppp = numparts/nprocs;// nppp : Number of parts per proc ...
941     int startpart = irank * nppp +1;// Part id from which I (myrank) start ...
942     int endpart = startpart + nppp - 1;// Part id to which I (myrank) end ...
943 
944     int descriptor;
945     char filename[255],fieldtag_s[255];
946     bzero((void*)filename,255);
947     bzero((void*)fieldtag_s,255);
948 
949     sprintf(filename,"d2wall.%d",((int)(irank/(nprocs/nfiles))+1));
950 
951     if (irank==0) {
952       printf("Filename is %s \n",filename);
953     }
954 
955     initphmpiio(&nfields, &nppf, &nfiles, &f_descriptor, "write");
956 
957     openfile(filename, "write", &f_descriptor);
958 
959     field_flag=0;
960 
961      int i;
962      for ( i = 0; i < nppp; i++) { //This loop is useful only if several parts per processor
963      // GPID : global part id, corresponds to rank ...
964         // e.g : (in this example)
965         // proc 0 : 1--4
966         // proc 1 : 5--8 ...
967         GPID = startpart + i;
968 
969         // Write solution field ...
970         sprintf(fieldtag_s,"d2wall@%d",GPID);
971 
972         isize = (*numnp);
973         nitems = 2;
974         iarray[ 0 ] = (*numnp);
975         iarray[ 1 ] = 1; //numVars = 1
976         writeheader( &f_descriptor, fieldtag_s, (void*)iarray, &nitems, &isize, "double", phasta_iotype);
977         writedatablock( &f_descriptor, fieldtag_s, (void*)(array1), &isize, "double", phasta_iotype );
978     }
979     field_flag++;
980 
981     if (field_flag==nfields){
982       closefile(&f_descriptor, "write");
983       finalizephmpiio(&f_descriptor);
984       if (irank==0) {
985         printf("\n");
986       }
987     }
988 }
989 
990