aboutsummaryrefslogtreecommitdiff
path: root/src/AMRPlus/AMRfilereaderPlus.C
blob: e7232e621c1129d6de24ef02948f3529cf41cd70 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <stdio.h>
#include <stdlib.h>
#include "AMRfilereaderPlus.h"
#include "FlexArrayTmpl.H"
void AMRfilereaderPlus::printGridInfo(AMRgridPlus &g){
	int i;
    printf("Grid level=%u step=%u maxtime=%u\n",g.level,g.timestep,g.maxtime);
    printf("\trank=%u dims[",g.rank);
    for(i=0;i<g.rank-1;i++) printf("%u,",g.dims[i]);
    printf("%u]\n",g.dims[g.rank-1]);
    printf("\tTimerefine=%u dx[0]=%lf origin[",g.timerefinement,g.delta[0]);
    for(i=0;i<g.rank-1;i++) printf("%lf,",g.origin[i]);
    printf("%lf]\n",g.origin[g.rank-1]);
    printf("\tData Pointer is %u\n",(unsigned long)(g.data));
    if(g.data){
      float *fdata = (float *)(g.data);
      for(int i=0;i<3;i++)
	printf("\t\tData[%u]=%f\n",i,fdata[i]);
    }
  }
 void AMRfilereaderPlus::printGridInfo(){
    // print it all out...
    printf("MaxLevel=%u\n",maxlevel);
    for(int i=0;i<grids.getSize();i++){
      printf("Grid[%u]--------------------\n",i);
      printGridInfo(grids[i]);
    }
  }
  void AMRfilereaderPlus::printActiveGrids(){
    // print it all out...
    printf("Num Active Grids=%u\n",activeGrids.getSize());
    for(int i = 0; i < activeGrids.getSize(); i++){
      printf("  Grid[%u] mapped from %u --------------------\n",
	     i,activeGrids[i]);
      printGridInfo(grids[activeGrids[i]]);
    }
  }
#define MAXIMIZE(u, v) {(u)=((u)>(v))?(u):(v);}
#define MINIMIZE(u, v) {(u)=((u)<(v))?(u):(v);}

void AMRfilereaderPlus::buildInfoTable(){
  // Load all grids Info
  int index=0;
  AMRgridPlus g;
  while(getGridInfo(g,index++)){
    if(this->debug) printf("buildInfoTable: getGrid index=%u\n",index);
    if(this->debug) printGridInfo(g);
    int i=grids.getSize();
    g.data=0; // zero out the data
    if(g.level>maxlevel)
      maxlevel=g.level;
    if(!i){
      smin=g.scalarmin;
      smax=g.scalarmax;
      //--Brad's additions
      //sminArray[i]=g.scalarmin;
      //smaxArray[i]=g.scalarmax;
      //--
      mintime = g.timestep;
      maxtime = g.timestep;
      maxtimeres = g.timerefinement;
      maxlevel= g.level;
      bounds[0]=g.origin[0];
      bounds[1]=g.origin[0]+g.delta[0]*g.dims[0];
      bounds[2]=g.origin[1];
      bounds[3]=g.origin[1]+g.delta[1]*g.dims[1];
      bounds[4]=g.origin[2];
      bounds[5]=g.origin[2]+g.delta[2]*g.dims[2];
    }
    else{
      //--Brad's additions
      //sminArray[i]=g.scalarmin;
      //smaxArray[i]=g.scalarmax;
      //--
	MINIMIZE(smin, g.scalarmin);
	MAXIMIZE(smax, g.scalarmax);
	if (g.level==0){
	    MINIMIZE(bounds[0], g.origin[0]);
	    MAXIMIZE(bounds[1], g.origin[0]+g.delta[0]*g.dims[0]);
	    MINIMIZE(bounds[2], g.origin[1]);
	    MAXIMIZE(bounds[3], g.origin[1]+g.delta[1]*g.dims[1]);
	    MINIMIZE(bounds[4], g.origin[2]);
	    MAXIMIZE(bounds[5], g.origin[2]+g.delta[2]*g.dims[2]);
	}
    }
    if(g.timestep<mintime) 
      mintime=g.timestep;
    if(g.timestep>maxtime) 
      maxtime=g.timestep;
    if(g.timerefinement>maxtimeres) 
      maxtimeres=g.timerefinement;
    grids.append(g); 
  }
}

void AMRfilereaderPlus::loadGrids(){
  if(!gridloading) return;
  for(int i=0;i<activeGrids.getSize();i++){
    if(this->debug) printf("buildInfoTable: getGrid index=%u activegridindex %u\n",i,activeGrids[i]);
    if(this->debug) printGridInfo(grids[activeGrids[i]]);
    getGridData(grids[activeGrids[i]],activeGrids[i]);
  }
}

void AMRfilereaderPlus::reclaimGrids(){
  if(!gridloading) return;
  for(int i=0;i<grids.getSize();i++){
    int f=0;
    for(int j=0;j<activeGrids.getSize();j++){
      if(activeGrids[j]==i){
	f=1;
	break;
      }
    }
    if(!f){
      free((grids[i]).data);
      (grids[i]).data=0;
    }
  }
}

void AMRfilereaderPlus::purgeGrids(){
  for(int i=0;i<grids.getSize();i++){
    if((grids[i]).data)
      free((grids[i]).data);
    (grids[i]).data=0;
  }
}

AMRfilereaderPlus::AMRfilereaderPlus(IObase &f):AMRgridreaderPlus(f),debug(0),gridloading(1){
  // we need to build a table from the file
  // then select grids based on the timestep
  buildInfoTable(); // initialize the convertor
  levelmask.setSize(maxlevel+1);
  for(int i=0;i<=maxlevel;i++)
    levelmask[i]=1; // all levels visible is default
  showAllLevels();
  setTime(mintime);
}

void AMRfilereaderPlus::setTime(int timestep){
  // Make Grid Selections
  if(timestep<mintime || timestep>maxtime){
    printf("timestep %u is out of range %u:%u\n",
	   timestep,mintime,maxtime);
    return;
  }
  activeGrids.purge();
  current_time=timestep;
  if(this->debug) printf("setTime(%u): mintime=%u maxtime=%u\n",current_time,mintime,maxtime);
  for(int i=0;i<grids.getSize();i++){
    if(this->debug) printf("\tgrids[%u].timestep=%u maxtime=%u\n",i,grids[i].timestep,
	   grids[i].maxtime);
    if(current_time>=grids[i].timestep && 
       current_time<grids[i].maxtime && levelmask[grids[i].level]){
      activeGrids.append(i);
      if(this->debug) printf("\t\tAppendGrid number %u\n",i);
    }
  }
  if(this->debug) puts("load grids");
  loadGrids();
  if(this->debug) puts("reclaim grids");
  reclaimGrids();
}
	
void AMRfilereaderPlus::showAllLevels(){
  for(int i=0;i<levelmask.getSize();i++) levelmask[i]=1;
}
	
// For C interface

int AMRfilereaderPlus::getGrids(AMRgridPlus *g){ // assumes number of 
  for(int i=0;i<grids.getSize();i++)
    g[i]=grids[activeGrids[i]];
  return activeGrids.getSize();
}

// For C++ interface
int AMRfilereaderPlus::getGrids(FlexArray<AMRgridPlus> &g){
  g.setSize(activeGrids.getSize());
  for(int i=0;i<g.getSize();i++)
    g[i]=grids[activeGrids[i]];
  return activeGrids.getSize();
}