xref: /phasta/phSolver/common/test/phIOwriteFields.cc (revision f9955254598898457d54effc66a62b518236e3a3)
1 #include <mpi.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <cassert>
6 #include "phiostats.h"
7 #include "phIO.h"
8 #include "phstream.h" //for makeRStream and makeGRStream
9 #include "syncio.h"
10 #include "posixio.h"
11 #include "streamio.h"
12 
13 int main(int argc, char* argv[]) {
14   MPI_Init(&argc,&argv);
15   int rank;
16   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
17   int size;
18   MPI_Comm_size(MPI_COMM_WORLD, &size);
19   if( argc != 2 ) {
20     fprintf(stderr, "Usage: %s <numSyncFiles>\n",argv[0]);
21     MPI_Finalize();
22     return 1;
23   }
24   const char* phrase = "number of fishes";
25   const char* type = "double";
26   const char* iotype = "binary";
27   double blockArray[4] = {2.0,4.0,8.0,16.0};
28   int blockEntries = 4;
29   int one = 1;
30   int numFish = 0;
31   double fishWeight = 1.23;
32   int nfiles = 1;
33   int ppf = size/nfiles;
34   const char* filename[2] = {"water-dat.", "water.dat."};
35   rstream rs = makeRStream();
36      phio_fp file[3];
37   const char* modes[3]={"syncio", "posixio", "streamio"};
38   syncio_setup_write(nfiles, one, ppf, &(file[0]));
39   posixio_setup(&(file[1]), 'w');
40   streamio_setup_r(&(file[2]), rs, 'w');
41   fprintf(stderr, "%s\n" ,"Outside loop 1.0");
42   for(int i=0; i<3; i++) {
43     fprintf(stderr, "%s\n" ,"Within the i loop");
44     if(!rank) fprintf(stderr, "%s\n", modes[i]);
45     fprintf(stderr, "%s\n" ,"Before phastaio");
46     phastaio_initStats();
47     fprintf(stderr, "%s\n" ,"Opening files with ", filename[i], file[i] );
48     phio_openfile(filename[i], file[i]);
49     fprintf(stderr, "%s\n" ,"Entering for loop for ", atoi(argv[1]) );
50    // const char* str = "Number of times "+ nfiles ;
51     for (int j = 0; j < 2 ; j++) {
52       fprintf(stderr,"%s\n", "Inside loop");
53      //fprintf(stderr,"%d\n",atoi(argv[1]));
54       const char* str = "Number of times " + 10;
55       fprintf(stderr,"%s\n", "Writing the header time - " );
56       fprintf(stderr,"%s\n","Printing the int zero");
57       fprintf(stderr,"%d\n",zero);
58       fprintf(stderr,"%s\n","Printing the int one");
59       fprintf(stderr,"%d\n",one);
60       fprintf(stderr,"%s\n","Printing the file[i]");
61       fprintf(stderr,"%s\n",file[i] );
62       fprintf(stderr,"%s\n","Should have printed the file" );
63       fprintf(stderr,"%s\n","Printing the const char type, this is set to double");
64       fprintf(stderr,"%s\n",type);
65       fprintf(stderr,"%s\n","Printing the const char iotype, this is set to binary");
66       fprintf(stderr,"%s\n",iotype);
67       fprintf(stderr,"%s\n","Opening the file" );
68       phio_writeheader(file[i], str, &blockEntries, &one, &blockEntries, "integer", iotype);
69       fprintf(stderr,"%s\n","Printing the created string after writing the header ");
70       fprintf(stderr,"%s\n",str );
71       fprintf(stderr,"%s\n","Printing the loop number" );
72       fprintf(stderr,"%d\n",j );
73       fprintf(stderr,"%s\n", "Writing the data block time - ");
74      // fprintf(stderr,"%d\n",j );
75       phio_writedatablock(file[i], str, blockArray, &blockEntries, type, iotype);
76       fprintf(stderr,"%s\n","Printing the file[i] after writing the data block" );
77       fprintf(stderr,"%s\n",file[i] );
78       fprintf(stderr,"%s\n","Printing the string that has been created " );
79       fprintf(stderr,"%s\n",str );
80     }
81     phio_closefile(file[i]);
82     phastaio_printStats();
83   }
84   syncio_setup_read(nfiles, &(file[0]));
85   posixio_setup(&(file[1]), 'r');
86   streamio_setup_r(&(file[2]), rs, 'r');
87   for(int i=0; i<3; i++) {
88     if(!rank) fprintf(stderr, "%s\n", modes[i]);
89     phastaio_initStats();
90     phio_openfile(filename[i], file[i]);
91     //Str was added
92    // const char* str = "Number of times "+ nfiles ;
93    // for (int j = 0; j < nfiles ; j++) {
94     phio_readheader(file[i], phrase, &numFish, &one, type, iotype);
95     //  phio_readheader(file[i], str, &numFish, &one, type, iotype);
96      //Changing argument from file[i] to file[j]
97     assert(!numFish);
98     phio_readdatablock(file[i], phrase, &fishWeight, &numFish, type, iotype);
99     //  phio_readdatablock(file[i], str, &fishWeight, &numFish, type, iotype);
100     assert(fishWeight == 1.23);
101     phio_closefile(file[i]);
102     phastaio_printStats();
103   }
104   MPI_Finalize();
105   return 0;
106 }
107