camr.cc 11.6 KB
Newer Older
jclamber's avatar
jclamber committed
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
// ============================================================================
// Copyright Jean-Charles LAMBERT - 2007-2012                                       
// e-mail:   Jean-Charles.Lambert@oamp.fr                                      
// address:  Dynamique des galaxies                                            
//           Laboratoire d'Astrophysique de Marseille                          
//           Pole de l'Etoile, site de Chateau-Gombert                         
//           38, rue Frederic Joliot-Curie                                     
//           13388 Marseille cedex 13 France                                   
//           CNRS U.M.R 7326                                                   
// ============================================================================

#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <cmath>
#include "camr.h"
#include "cfortio.h"
#include "snapshotramses.h"
namespace ramses {
const double CAmr::XH = 0.76;
const double CAmr::mH = 1.6600000e-24;
const double CAmr::kB = 1.3806200e-16;

// ============================================================================
//
CAmr::CAmr(const std::string _indir, const bool _v)
{
  nbody=0;
  //pos = mass = vel = NULL;
  verbose=_v;
  indir = _indir;
  infile="";
  
  // keep filename untill last /
  int found=indir.find_last_of("/");
  if (found != (int) std::string::npos && (int) indir.rfind("output_")<found) {
    indir.erase(found,indir.length()-found);
  }
  if (verbose)
    std::cerr << "indir =[" << indir <<"]\n";
  
  found=(int) indir.rfind("output_"); 
  if (found!=std::string::npos) {
    s_run_index= indir.substr(found+7,indir.length()-1); // output_ = 7 characters
    
    while ((found=s_run_index.find_last_of("/"))>0) { // remove trailing "/"
      s_run_index.erase(found,found);
    }
    infile = indir + "/amr_" + s_run_index + ".out00001";
    if (verbose)
      std::cerr << "Run index = " << s_run_index <<  "  infile=[" << infile << "]\n";
  }
  //computeNbody();
  //loadData();
}
// ============================================================================
//
CAmr::~CAmr()
{
}
// ----------------------------------------------------------------------------
//
bool CAmr::isValid()
{    
  if (amr.open(infile)) {
    valid=true;
    readHeader();
    amr.close();
    if (verbose)
      std::cerr << "ncpu="<<ncpu<<"  ndim="<<ndim<< "\n";// "npart=" << npart << "\n";
    xbound[0] = nx/2;
    xbound[1] = ny/2;
    xbound[2] = nz/2;
    twotondim = pow(2,ndim);  
    ordering = "hilbert";
    scale_nH =  XH/mH * 0.276090728884102e-29;
  }
  else
    valid=false;
  amr.close();
  return valid;
}
// ============================================================================
// readHeader
int CAmr::readHeader()
{
  int len1,len2;
  amr.readDataBlock((char *) &ncpu);

  amr.readDataBlock((char *) &ndim);

  len1=amr.readFRecord();
  amr.readData((char *) &nx,sizeof(int),1);
  amr.readData((char *) &ny,sizeof(int),1);
  amr.readData((char *) &nz,sizeof(int),1);
  len2=amr.readFRecord();
  assert(amr.good() && len1==len2);

  amr.readDataBlock((char *) &nlevelmax);
  if (verbose)
    std::cerr << "AMR Nlevel max="<<nlevelmax<<"\n";
  amr.readDataBlock((char *) &ngridmax);

  amr.readDataBlock((char *) &nboundary);
  
  amr.readDataBlock((char *) &ngrid_current);
  
  amr.skipBlock();
  
  return 1;
}
// ============================================================================
// loadData
int CAmr::loadData(uns::CParticles * particles,
                   const unsigned int req_bits)
{
  int ngridfile  [nlevelmax][ncpu+nboundary];
  int ngridlevel [nlevelmax][ncpu          ];
  int ngridbound [nlevelmax][     nboundary];
  double xc[3][8];
  int ngrida;
  std::string infile;
  
  nbody = 0;
  int cpt=0;
  bool count_only=false;
  //if (index==NULL)  count_only=true;

  // loop on all cpus/files
  for (int icpu=0; icpu<ncpu; icpu++) {
    std::ostringstream osf;
    osf << std::fixed << std::setw(5) << std::setfill('0') <<icpu+1;
    infile = indir + "/amr_" + s_run_index + ".out" + osf.str();
    if (verbose) std::cerr << "CAmr::loadData infile-> ["<<infile << "]\n";
    amr.open(infile);
    amr.skipBlock(21);
    
    amr.readDataBlock((char *) &ngridlevel);
    memcpy(ngridfile,ngridlevel,sizeof(int)*nlevelmax*ncpu);
    
    amr.skipBlock();    
    
    if (nboundary>0) {
      amr.skipBlock(2);
      
      amr.readDataBlock((char *) &ngridbound);
      // must convert the following line
      //ngridfile(ncpu+1:ncpu+nboundary,1:nlevelmax)=ngridbound
      for (int i=0;i<nlevelmax;i++) {
        // copy grid level
        memcpy(&ngridfile [i][0],
               &ngridlevel[i][0],sizeof(int)*ncpu);
        // copy gridbound
        memcpy(&ngridfile [i][ncpu],
               &ngridbound[i][0],sizeof(int)*nboundary);
      }
    }
    amr.skipBlock();    
    // ROM: comment the single following line for old stuff
    amr.skipBlock();    
    if (ordering=="bisection") 
      amr.skipBlock(5);
    else 
      amr.skipBlock();
    amr.skipBlock(3);
    // Open HYDRO file and skip header
    std::string hydrofile = indir + "/hydro_" + s_run_index + ".out" + osf.str();
    //if (verbose) std::cerr << "CAmr::loadData hydrofile-> ["<<hydrofile << "]\n";
    hydro.open(hydrofile,count_only);
    hydro.skipBlock();
    hydro.readDataBlock((char *) &nvarh);
    //std::cerr << "nvarh = " << nvarh << "\n" ;
    hydro.skipBlock(4);
    // loop over levels
    for (int ilevel=0; ilevel<lmax; ilevel++) {
      
      // Geometry
      double dx=pow(0.5,ilevel+1);
      double dx2=0.5*dx;
      for (int ind=0; ind<twotondim; ind++) {
        int iz=ind/4;
        int iy=(ind-4*iz)/2;
        int ix=(ind-2*iy-4*iz);
        xc[0][ind]=(ix-0.5)*dx;
        xc[1][ind]=(iy-0.5)*dx;
        xc[2][ind]=(iz-0.5)*dx;
      }
      // allocate work arrays
      ngrida=ngridfile[ilevel][icpu];
      
      double * xg=NULL, *var=NULL;
      int * son=NULL;
      if (ngrida>0) {
        xg = new double[ngrida*ndim];
        son= new int   [ngrida*twotondim];
        if (!count_only) 
          var= new double[ngrida*twotondim*nvarh];
      }
      // Loop over domains
      for (int j=0; j<(nboundary+ncpu); j++) {
        if (ngridfile[ilevel][j]>0) {
          amr.skipBlock(); // Skip grid index
          amr.skipBlock(); // Skip next index
          amr.skipBlock(); // Skip prev index
          //
          // Read grid center
          //
          for (int idim=0;idim<ndim;idim++) {
            if (j==icpu) {
              amr.readDataBlock((char *) &xg[idim*ngrida]);
            } 
            else amr.skipBlock();
          }
          amr.skipBlock();       // Skip father index
          amr.skipBlock(2*ndim); // Skip nbor index
          //
          // Read son index
          //
          for (int ind=0;ind<twotondim;ind++) {
            if (j==icpu) {
              amr.readDataBlock((char *) &son[ind*ngrida]);
            }
            else amr.skipBlock();
          }          
          amr.skipBlock(twotondim); // Skip cpu map          
          amr.skipBlock(twotondim); // Skip refinement map
        }
        //
        // Read HYDRO data
        //
        hydro.skipBlock(2);
        if (!count_only && ngridfile[ilevel][j]>0) {
          // Read hydro variables
          for (int ind=0;ind<twotondim;ind++) {
            for (int ivar=0; ivar<nvarh; ivar++) {
              if (j==icpu) {
                hydro.readDataBlock((char *) &var[ivar*ngrida*twotondim+ind*ngrida]);
              }
              else hydro.skipBlock();
            }
          }
        }
      }      
      if (ngrida>0) {
        //  Loop over cells
        for (int ind=0;ind<twotondim;ind++) {
          // Store data cube
          for (int i=0;i<ngrida;i++) {
            // compute cell center
            double px=xg[0*ngrida+i]+xc[0][ind]-xbound[0]; // x
            double py=xg[1*ngrida+i]+xc[1][ind]-xbound[1]; // y
            double pz=xg[2*ngrida+i]+xc[2][ind]-xbound[2]; // z
            bool ok_cell =       (
                !(son[ind*ngrida+i]>0 && ilevel<lmax) && // cells is NOT refined
                (ilevel>=lmin)                        &&
                ((px+dx2)>=xmin)                      &&
                ((py+dx2)>=ymin)                      &&
                ((pz+dx2)>=zmin)                      &&
                ((px-dx2)<=xmax)                      &&
                ((py-dx2)<=ymax)                      &&
                ((pz-dx2)<=zmax) );
            if (ok_cell) {
              if (!count_only) {
#if 0
                int idx=index[nbody];
                if (idx!=-1) { // it's a valide particle
                  pos[3*cpt+0] = px;
                  pos[3*cpt+1] = py;
                  pos[3*cpt+2] = pz;
                  rneib[cpt]   = dx;
                  rho[cpt] = var[0*ngrida*twotondim+ind*ngrida+i];
#if 1
                  temp[cpt]= std::max(0.0,var[4*ngrida*twotondim+ind*ngrida+i]/rho[cpt]);
                  //temp[cpt]= var[4*ngrida*twotondim+ind*ngrida+i]/rho[cpt];
#else
                  float p=var[4*ngrida*twotondim+ind*ngrida+i]; // pressure
                  float alpha=0.28;                 // fraction of He in mass
                  float mu=4./(8.-5.*alpha);
                  float mh=1.67e-24;                // proton mass in g
                  float kb=1.38e-16;                // Boltzmann constant in erg.K-1
                  temp[cpt]= mu*mh/kb*p/rho[cpt]*1.e14;    // Temperature en K
                  //temp[cpt] /= (11604.5/1000.);            // Temperature en Kev
                  temp[cpt]=std::max((float)0.0,temp[cpt]);
                  
#endif
                  cpt++;
                }
#else
                bool take=false;
                if (req_bits&POS_BIT) {
                  particles->pos.push_back(px);  // x
                  particles->pos.push_back(py);  // y
                  particles->pos.push_back(pz);  // z
                  particles->load_bits |= POS_BIT;
                  take=true;
                }
                if (req_bits&VEL_BIT) {
                  particles->vel.push_back(-666.);  // vx
                  particles->vel.push_back(-666.);  // vy
                  particles->vel.push_back(-666.);  // vz
                  particles->load_bits |= VEL_BIT;
                  take=true;
                }
                if (req_bits&MASS_BIT) {
                  particles->mass.push_back(-666.);
                  particles->load_bits |= MASS_BIT;
                  take=true;
                }
                if (req_bits&HSML_BIT) {
                  particles->hsml.push_back(dx); // hsml
                  particles->load_bits |= HSML_BIT;
                }
                float rho = var[0*ngrida*twotondim+ind*ngrida+i];
                float temp= std::max(0.0,var[4*ngrida*twotondim+ind*ngrida+i]/rho);
                if (req_bits&RHO_BIT) {                  
                  particles->rho.push_back(rho); // rho var(i,ind,1) * scale_nH
                  particles->load_bits |= RHO_BIT;
                }
                if (req_bits&TEMP_BIT) {                  
                  particles->temp.push_back(temp);
                  particles->load_bits |= TEMP_BIT;
                }
                if (take) {
                  particles->indexes.push_back(0); // GAS particles
                  particles->ngas++; // One more gas particles
                  particles->ntot++; // one more total particles
                }
#endif
              }
              nbody++;
            }
          }
        }
        // garbage collecting
        delete [] xg;
        //delete [] x;
        delete [] son;
        if (!count_only) 
          delete [] var;
      }
    } // ilevel
    amr.close();    
    hydro.close();    
  } //for (int icpu=0 .... 
  return nbody;
}
} // end of namespace ramses