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