aboutsummaryrefslogtreecommitdiff
path: root/src/boxinbox.cc
blob: c5864fc631b741816ea6b38bf08fb28fa3197ce8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <IEEEIO.hh>
#include "AMRwriter.hh"
typedef char *charp;
struct CommandLine {
  char *programname;
  charp infile[32];
  char outfile[128];
  int maxlevel;
  
  void setDefaults(){
    programname="";
    for(int i=0;i<32;i++) infile[i]=0;
    strcpy(outfile,"amrout.ieee");
    maxlevel=0;
  }

  CommandLine(){
    setDefaults();
  }

  CommandLine(int argc,char *argv[]){
    setDefaults();
    parse(argc,argv);
  }

  void parse(int argc,char *argv[]){
    programname=argv[0];
    for(int i=1;i<argc;i++){
      char *str=argv[i];
      if(*str=='-'){
	char *val=argv[i+1];
	str++;
	//printf("read option[%u]=[%s]\n",i,str);
	if(*str=='i')      { 
	  puts("option -i: input files");
	  i++;
	  for(maxlevel=0;i<argc && 
		(*(argv[i])!='-') && 
		maxlevel<32;
	      maxlevel++,i++)
	    infile[maxlevel]=argv[i];
	  i--;
	}
	else if(*str=='o') {
	  puts("option -o: output files");
	  strcpy(outfile,val);
	}
      }
    }
    if(!maxlevel) {
      usage();
      exit(0);
    }
  }
  void usage(){
    printf("Incorrect usage.  You should use\n");
    printf("%s -i <infile Level0> <infile Level1> ...<infile LevelN> -o <outfile>\n",programname);
    puts("Files must appear in order as they are read in.");
  }
};

int main(int argc, char *argv[]){
  CommandLine cmdln(argc,argv);
  typedef IEEEIO *IEEEIOp;
  int ninfiles;
  IEEEIOp infid[32],outfid;
  AMRwriter *amrfile;
  int i;
  
  for(i=0;i<cmdln.maxlevel;i++){
    infid[i]=new IEEEIO(cmdln.infile[i],IObase::Read);
    if(!infid[i]->isValid()) {
      printf("file %s is not an IEEEIO file\n",cmdln.infile[i]);
      exit(0);
    }
    else
      printf("Opening file %s as level %u for reading\n",
	     cmdln.infile[i],i);
  }
  outfid = new IEEEIO(cmdln.outfile,IObase::Write);
  if(!outfid->isValid()) {
    printf("file %s could not be opened for writing\n",cmdln.outfile);
    exit(0);
  }
  amrfile = new AMRwriter(*outfid);
  printf("Opened file %s for output\n",cmdln.outfile);
  
  // lets get info about the toplevel grid
  IObase::DataType type;
  int rank,dims[3];
  int nelements,attribnum;
  double delta[3],timestep,origin[3];

  infid[0]->seek(0);
  infid[0]->readInfo(type,rank,dims);
  amrfile->setType(type);
  
  // read delta (spacing between grid points at toplevel grid)
  attribnum=infid[0]->readAttributeInfo("delta",type,nelements);
  infid[0]->readAttribute(attribnum,delta);
  // read timestep size (in floating point realtime)
  //attribnum=infid[0]->readAttributeInfo("",type,nelements);
  //infid[0]->readAttribute(attribnum,timestep);
  timestep=1.0;
  // read origin (floating point origin).
  attribnum=infid[0]->readAttributeInfo("origin",type,nelements);
  infid[0]->readAttribute(attribnum,origin);

  printf("Setting toplevel params origin[%lf,%lf,%lf],Timestep[%lf],delta[%lf,%lf,%lf],maxlevel[%u]\n",
	 origin[0],origin[1],origin[2],
	 timestep,
	 delta[0],delta[1],delta[2],
	 cmdln.maxlevel);
  amrfile->setTopLevelParameters(rank,origin,delta,timestep,cmdln.maxlevel);
  amrfile->setRefinement(1, // timerefinement of 1 because we currently
			 // don't save intermedite timesteps.  In the future
			 // this would be 2 if you dump the intermediate
			 // timesteps
			 2 // spatial refinement between levels is 2 in each
			 // dimension
			 );
  // now we read all of the data from the input files
  // and write them out into the AMR file
  for(int n=0,ndatasets=infid[0]->nDatasets();n<ndatasets;n++){
    printf("Reading step %u\n",n);
    for(int level=0;level<cmdln.maxlevel;level++){
      printf("\tReading Level %u\n",level);
      char *data;
      infid[level]->seek(n); // make everyone seek to the same place
      amrfile->setTime(n);
      // this is assuming that all levels are dumped at the same time
      // rather than dumping them at the rate which they are computed.
      // (eg. this is a snapshot of the entire heirarchy at each timestep).
      // however this library can handle each level stepping at a different
      // rate.
      printf("Readinfo on %u\n",n);
      infid[level]->readInfo(type,rank,dims); // read in the data from src file
      printf("Info is type(%u) rank(%u) dims[%u,%u,%u]\n",
	     type,rank,dims[0],dims[1],dims[2]);
      data = new char[IObase::nBytes(type,rank,dims)]; // allocate space
      infid[level]->read(data); // read the actual data in
      amrfile->setLevel(level); // set the level to write to in amr output file
      // read the floating point origin attribute
      attribnum=infid[level]->readAttributeInfo("origin",type,nelements);
      infid[level]->readAttribute(attribnum,origin); // read it
      // write out the data to the AMR file
      amrfile->write(origin,dims,data); // write the data out
      delete data; // free the data storage
    }
  }
}