aboutsummaryrefslogtreecommitdiff
path: root/src/AmrFileReader.cc
blob: 420721064f9c75328ec71dbcd84cdab395888884 (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
#include <stdio.h>
#include <stdlib.h>
#include "AmrFileReader.hh"
#include "FlexArrayTmpl.H"
void AmrFileReader::printGridInfo(AmrGrid &g){
    printf("Grid level=%u step=%u maxtime=%u\n",g.level,g.timestep,g.maxtime);
    printf("\trank=%u dims[",g.rank);
    for(int 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(int 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 AmrFileReader::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 AmrFileReader::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]]);
    }
  }

void AmrFileReader::buildInfoTable(){
  // Load all grids Info
  int index=0;
  AmrGrid g;
  while(gridreader.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
    grids.append(g);
    if(g.level>maxlevel)
      maxlevel=g.level;
    printf("Gridnum[%u] timestep=%u\n",i,g.timestep);
    if(!i){
      printf("i=%u INITIALIZE TIME*********************\n",i);
      mintime = g.timestep;
      maxtime = g.timestep;
      maxtimeres = g.timerefinement;
      maxlevel= g.level;
    }
    if(g.timestep<mintime) 
      mintime=g.timestep;
    if(g.timestep>maxtime) 
      maxtime=g.timestep;
    if(g.timerefinement>maxtimeres) 
      maxtimeres=g.timerefinement;
  }
}

void AmrFileReader::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]]);
    gridreader.getGridData(grids[activeGrids[i]],activeGrids[i]);
  }
}

void AmrFileReader::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 AmrFileReader::purgeGrids(){
  for(int i=0;i<grids.getSize();i++){
    if((grids[i]).data)
      free((grids[i]).data);
    (grids[i]).data=0;
  }
}

AmrFileReader::AmrFileReader(IObase &f):gridreader(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 AmrFileReader::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 AmrFileReader::showAllLevels(){
  for(int i=0;i<levelmask.getSize();i++) levelmask[i]=1;
}
	
// For C interface

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

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