aboutsummaryrefslogtreecommitdiff
path: root/src/AMRPlus/AMRPlusConv.C
blob: 4d1c137af3474336cfd77e5d05cec9d867d6ba54 (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
157
158
159
160
161
162
163
164
165
166
167
168
// generated by Fast Light User Interface Designer (fluid) version 1.00

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream.h>
#include "IEEEIO.hh"
#include "AmrFileReader.hh"
#include "AMRwriterPlus.h"
#include "AMRgridPlus.h"



int main(int argc, char** argv) {
    IObase *ifile, *ofile;
    AMRwriterPlus *writer;
    AmrGridReader *reader;
    AmrFileReader *freader;
    int maxlev;
   
    if (argc!=2){
	cerr<<"Usage: addscalars <filename>"<<endl;
	exit(0);
    }
   char *fname=strdup(argv[1]);
   ifile=new IEEEIO(fname, IObase::Read);
   if (!ifile->isValid()){
	cerr<<fname<<" is not a valid IEEEIO file."<<endl;
	exit(0);
    }
    
    freader=new AmrFileReader(*ifile);
    maxlev=freader->nLevels();
    
    reader=new AmrGridReader(*ifile);
    if (reader==NULL){
	cerr<<fname<<" is not a valid AMR file."<<endl;
	exit(0);
    }
    char *outname=(char *)malloc(strlen(fname)+5);
    strcpy(outname, fname);
    strcat(outname, "plus");
    
    ofile=new IEEEIO(outname, IObase::Create);
    if (!ofile->isValid()){
	cerr<<"Cannot create "<<outname<<" for output."<<endl;
	exit(0);
    }
    writer = new AMRwriterPlus(*ofile);
    if (writer==NULL){
	cerr<<"Something is fishy in denmark. (i.e. at line "<<__LINE__<<")"<<endl;
	exit(0);
    }
    
    
    AMRgridPlus grid;
    // unsigned long numgrids=reader->getNumGrids();
    unsigned long datasz;
    
    
    //prime things:
    reader->getGridInfo(grid, 0);
    grid.data=malloc(grid.nbytes);
    datasz=grid.nbytes;
    FlexArray< AmrGrid> gridlist;
    
    
    int tmin, tmax;
    int numlev = freader->getNumLevels();
    FlexArray<int> levmask;
    int lcnt=numlev, tflag=2, tstep=0;
    double t1, t2, tdiff=0, finetimeref=1.0;
    int ts1,ii;
    levmask.setSize(numlev);
    for(ii=0;ii<numlev;ii++) levmask[ii]=0;
    int idx=0;
    int fixtime=0, fixtimeval;
    AmrGrid *tgrid;
    while ( (tgrid=reader->getGridInfo(grid, idx))!=NULL && (lcnt  || tflag)){
		IObase::DataType atype;
		int length;
		int attrnum;
		
		if ((grid.level==0) && tflag){
		    //Getll the time of root grid...
		    double tm;
		    attrnum=ifile->readAttributeInfo("time",atype,length);
		    if(attrnum>=0) ifile->readAttribute(attrnum,&tm);
		    if (tflag==2){
			t1=tm;
			ts1=grid.timestep;
			tflag=1;
		    } else { //tflag==1;
		    t2=tm;
			tdiff=(t2-t1)/(grid.timestep-(float)ts1);
			tstep=grid.timestep;
			if (tdiff!=0) tflag=0;
		    }
		    
		}
		//If this is a level we havent already seen...
		if (levmask[grid.level]==0){
		    //Get all the refinement info...
		    attrnum=ifile->readAttributeInfo("time_refinement",atype,length);
		    if(attrnum>=0){
			ifile->readAttribute(attrnum,&(grid.timeref));
		    }
		    attrnum=ifile->readAttributeInfo("spatial_refinement",atype,length);
		    if(attrnum>=0){
			ifile->readAttribute(attrnum,&(grid.spaceref));
	    	    }
		    attrnum=ifile->readAttributeInfo("grid_placement_refinement",atype,length);
		    if(attrnum>=0){
			ifile->readAttribute(attrnum,&(grid.placeref));
		    }
		    //feed the refinement info to the writer...
		    writer->setLevelRefinement(grid.level, 
			grid.timeref, grid.spaceref, grid.placeref);
		    //and mark the level as done.
		    
		    if (grid.level==numlev-1)finetimeref=grid.timeref;
		    levmask[grid.level]=1;
		    lcnt--;
		}
		idx++;
	    }
    tdiff/=finetimeref;

    //Coarse steps should be composed of  
    if (finetimeref!=tstep &&(tstep!=0)){
	cout<<"Mismatch between max time refinement and timesteps"<<endl;
	cout<<"Timeref="<<finetimeref<<" | Coarse step="<<tstep<<endl;
	cout<<"Should I adjust: (C)oarse timestep,  or "<<endl;
	cout<<"should I (I)gnore it?"<<endl;
	char c=cin.get();
	if (c=='C' || c=='c') fixtime=1;
	fixtimeval=tstep/finetimeref;
    }
    
    freader->getTimeRange(tmin, tmax);
    ii=0;
    
    tgrid=reader->getGridInfo(grid, 0);
    writer->setType((IObase::DataType)grid.datatype);
    writer->setTopLevelParameters(grid.rank, grid.origin, grid.delta, tdiff, maxlev);
    
    
    while (tgrid!=NULL){
	if (grid.nbytes>datasz) {
	    datasz=grid.nbytes;
	    grid.data=realloc(grid.data, datasz);
	    }
	reader->getGridData(grid, ii);
	if (fixtime) grid.timestep/=fixtimeval;
	
	
  
	writer->write(&grid, 1); //calc scalars & write
	ii++;
	tgrid=reader->getGridInfo(grid, ii);
	
    }
    delete ifile;
    delete ofile;
    
    
}