camr.cc 17 KB
Newer Older
jclamber's avatar
jclamber committed
1
// ============================================================================
2
// Copyright Jean-Charles LAMBERT - 2007-2016
jclamber's avatar
jclamber committed
3 4 5
//           Centre de donneeS Astrophysiques de Marseille (CeSAM)              
// e-mail:   Jean-Charles.Lambert@lam.fr                                      
// address:  Aix Marseille Universite, CNRS, LAM 
jclamber's avatar
jclamber committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//           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"
21

jclamber's avatar
jclamber committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35
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="";
36

jclamber's avatar
jclamber committed
37 38 39 40 41 42 43 44 45
  // 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_"); 
46
  if (found!=(int) std::string::npos) {
jclamber's avatar
jclamber committed
47 48 49 50 51 52
    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";
53
    testhydrofile = indir + "/hydro_" + s_run_index + ".out00001";
54 55 56 57 58 59 60 61 62
    // check gravity file is present
    std::string gravityfile   = indir + "/grav_" + s_run_index + ".out00001";
    if (grav.open(gravityfile)) {
      is_gravity = true;
      grav.close();
    } else {
      is_gravity = false;
      std::cerr << "GRAVITY files are missing....\n";
    }
jclamber's avatar
jclamber committed
63 64 65
    if (verbose)
      std::cerr << "Run index = " << s_run_index <<  "  infile=[" << infile << "]\n";
  }
66 67 68



69 70 71 72 73 74
  // readHeader
  if (amr.open(infile)) {
    readHeader();
    amr.close();
  }

jclamber's avatar
jclamber committed
75 76 77 78 79 80 81
  //computeNbody();
  //loadData();
}
// ============================================================================
//
CAmr::~CAmr()
{
82 83
   amr.close();
   hydro.close();
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
84
   grav.close();
jclamber's avatar
jclamber committed
85 86 87 88 89
}
// ----------------------------------------------------------------------------
//
bool CAmr::isValid()
{    
90
  if (amr.open(infile) && hydro.open(testhydrofile)) {
jclamber's avatar
jclamber committed
91
    valid=true;
92
    //readHeader();
jclamber's avatar
jclamber committed
93
    amr.close();
94
    hydro.close();
jclamber's avatar
jclamber committed
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
    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);
  
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
  amr.readDataBlock((char *) &header.boxlen);
  amr.skipBlock(3); // noutput,iout,ifout
                    // tout
                    // aout
  amr.readDataBlock((char *) &header.time);
  amr.skipBlock(4); // dtold
                    // dtnew
                    // nstep,nstep_coarse
                    // const,mass_tot_0,rho_tot
  len1=amr.readFRecord();
  amr.readData((char *) &header.omega_m   ,sizeof(double),1);
  amr.readData((char *) &header.omega_l   ,sizeof(double),1);
  amr.readData((char *) &header.omega_k   ,sizeof(double),1);
  amr.readData((char *) &header.omega_b   ,sizeof(double),1);
  amr.readData((char *) &header.h0        ,sizeof(double),1);
  amr.readData((char *) &header.aexp_ini  ,sizeof(double),1);
  amr.readData((char *) &header.boxlen_ini,sizeof(double),1);
  len2=amr.readFRecord();
  assert(amr.good() && len1==len2);

  len1=amr.readFRecord();
  amr.readData((char *) &header.aexp,sizeof(double),1);
  amr.readData((char *) &header.hexp,sizeof(double),1);
  amr.readData((char *) &header.aexp_old,sizeof(double),1);
  amr.readData((char *) &header.epot_tot_int,sizeof(double),1);
  amr.readData((char *) &header.epot_tot_old,sizeof(double),1);
  len2=amr.readFRecord();
  assert(amr.good() && len1==len2);

163
  if (len2==len1) ; // remove warning....
jclamber's avatar
jclamber committed
164 165 166 167
  return 1;
}
// ============================================================================
// loadData
168 169 170 171 172
template int CAmr::loadData<float> (uns::CParticles<float> * particles,
                   const unsigned int req_bits);
template int CAmr::loadData<double> (uns::CParticles<double> * particles,
                   const unsigned int req_bits);
template <class T> int CAmr::loadData(uns::CParticles<T> * particles,
jclamber's avatar
jclamber committed
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
                   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;
  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);
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
223
    // --------------
jclamber's avatar
jclamber committed
224 225 226 227
    // 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);
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
228
    hydro.skipBlock(); // ncpu
jclamber's avatar
jclamber committed
229 230
    hydro.readDataBlock((char *) &nvarh);
    //std::cerr << "nvarh = " << nvarh << "\n" ;
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
231 232 233 234
    hydro.skipBlock(4); // ndim,levelmax,nboundary,gamma

    // --------------
    // Open GRAV file and skip header
235 236
    nvarg=0;
    if (is_gravity && (req_bits&POT_BIT || req_bits&ACC_BIT)) {
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
237 238 239 240 241 242 243 244 245
      std::string gravfile = indir + "/grav_" + s_run_index + ".out" + osf.str();
      //if (verbose) std::cerr << "CAmr::loadData hydrofile-> ["<<hydrofile << "]\n";
      grav.open(gravfile,count_only);
      grav.skipBlock(); // ncpu
      grav.readDataBlock((char *) &nvarg); // buggy in new RAMSES version
      //nvarg=ndim+1; // this is MUST be the real value
                    // ndim = 3; nvarg =4, phi,ax,ay,az
                    // ndim = 2; nvarg =3, phi,ax,ay
      if (verbose) {
246
        //std::cerr << "\nWARNING\nWe assume that you are using ramses data files produces with new RAMSES released\n";
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
247 248 249
      }
      grav.skipBlock(2); //  levelmax,nboundary
    }
jclamber's avatar
jclamber committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    // 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];
      
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
267
      double * xg=NULL, *var=NULL, *varg=NULL;
jclamber's avatar
jclamber committed
268 269 270 271
      int * son=NULL;
      if (ngrida>0) {
        xg = new double[ngrida*ndim];
        son= new int   [ngrida*twotondim];
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
272
        if (!count_only) {
jclamber's avatar
jclamber committed
273
          var= new double[ngrida*twotondim*nvarh];
274
          if (is_gravity && (req_bits&POT_BIT || req_bits&ACC_BIT)) {
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
275 276 277
            varg= new double[ngrida*twotondim*nvarg];
          }
        }
jclamber's avatar
jclamber committed
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
      }
      // 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();
            }
          }
        }
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
323 324 325
        //
        // Read GRAV data
        //
326
        if (is_gravity && (req_bits&POT_BIT || req_bits&ACC_BIT)) {
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340
          grav.skipBlock(2);
          if (!count_only && ngridfile[ilevel][j]>0) {
            // Read grav variables
            for (int ind=0;ind<twotondim;ind++) {
              for (int ivar=0; ivar<nvarg; ivar++) {
                if (j==icpu) {
                  grav.readDataBlock((char *) &varg[ivar*ngrida*twotondim+ind*ngrida]);
                }
                else grav.skipBlock();
              }
            }
          }
        }

jclamber's avatar
jclamber committed
341 342 343 344 345 346 347 348 349
      }      
      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
350 351 352 353
            double pz=0.0;
            if (ndim > 2) {
              pz=xg[2*ngrida+i]+xc[2][ind]-xbound[2]; // z
            }
jclamber's avatar
jclamber committed
354 355 356 357 358
            bool ok_cell =       (
                !(son[ind*ngrida+i]>0 && ilevel<lmax) && // cells is NOT refined
                (ilevel>=lmin)                        &&
                ((px+dx2)>=xmin)                      &&
                ((py+dx2)>=ymin)                      &&
jclamber's avatar
jclamber committed
359
                ((ndim<3) || ((pz+dx2)>=zmin))        &&
jclamber's avatar
jclamber committed
360 361
                ((px-dx2)<=xmax)                      &&
                ((py-dx2)<=ymax)                      &&
jclamber's avatar
jclamber committed
362
                ((ndim<3) || ((pz-dx2)<=zmax)));
jclamber's avatar
jclamber committed
363 364 365 366
            if (ok_cell) {
              if (!count_only) {
                bool take=false;
                if (req_bits&POS_BIT) {
367 368
                  particles->pos.push_back(px*header.boxlen);  // x
                  particles->pos.push_back(py*header.boxlen);  // y
jclamber's avatar
jclamber committed
369 370 371 372
                  if (ndim>2)
                    particles->pos.push_back(pz*header.boxlen);  // z
                  else
                    particles->pos.push_back(0.0);  // z
jclamber's avatar
jclamber committed
373 374 375 376
                  particles->load_bits |= POS_BIT;
                  take=true;
                }
                if (req_bits&VEL_BIT) {
377 378
                  particles->vel.push_back(var[1*ngrida*twotondim+ind*ngrida+i]); // vx
                  particles->vel.push_back(var[2*ngrida*twotondim+ind*ngrida+i]); // vy
jclamber's avatar
jclamber committed
379 380 381 382
                  if (ndim>2)
                    particles->vel.push_back(var[3*ngrida*twotondim+ind*ngrida+i]); // vz
                  else
                    particles->vel.push_back(0.0); // vz
jclamber's avatar
jclamber committed
383 384 385
                  particles->load_bits |= VEL_BIT;
                  take=true;
                }
386
                // gas density
387
                double rho = var[0*ngrida*twotondim+ind*ngrida+i];
388
                if (req_bits&MASS_BIT) {                
jclamber's avatar
jclamber committed
389
                  particles->mass.push_back(rho*dx*header.boxlen*dx*header.boxlen*dx*header.boxlen);
jclamber's avatar
jclamber committed
390 391 392 393
                  particles->load_bits |= MASS_BIT;
                  take=true;
                }
                if (req_bits&HSML_BIT) {
jclamber's avatar
jclamber committed
394
                  particles->hsml.push_back(dx*header.boxlen); // hsml
jclamber's avatar
jclamber committed
395 396 397 398 399 400
                  particles->load_bits |= HSML_BIT;
                }
                if (req_bits&RHO_BIT) {                  
                  particles->rho.push_back(rho); // rho var(i,ind,1) * scale_nH
                  particles->load_bits |= RHO_BIT;
                }
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
401 402 403
                //
                // Get gavitationnal data
                //
404
                if (is_gravity && (req_bits&POT_BIT && (nvarg==ndim+1))) {
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
405 406 407
                  particles->phi.push_back(varg[0*ngrida*twotondim+ind*ngrida+i]);
                  particles->load_bits |= POT_BIT;
                }
408
                if (is_gravity && (req_bits&ACC_BIT && nvarg>=ndim )) {
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
                  int offset=0;
                  if (nvarg==ndim+1) { // there is pot/ax/ay/az
                    offset=1;          //  shift to ax
                  }
                  for (int k=0; k<nvarg; k++) {
                    particles->acc.push_back(varg[(offset+k)*ngrida*twotondim+ind*ngrida+i]);
                  }
                  if (ndim<3) { // ndim = 2 , set az=0.0
                    particles->acc.push_back(0.0);
                  }
                  particles->load_bits |= ACC_BIT;
                }
                //
                // Get hydrodinamic data
                //
                // with 2D simulations, hydro variables from var[] array are shifted to the left
jclamber's avatar
jclamber committed
425 426 427 428
                int offset_2d=-1;
                if (ndim>2) offset_2d=0;

                if (req_bits&TEMP_BIT && nvarh>(4+offset_2d)) {
429
                  double temp=0.0;
430
                  if (rho!=0.0) {
jclamber's avatar
jclamber committed
431
                    temp=std::max(0.0,var[(4+offset_2d)*ngrida*twotondim+ind*ngrida+i]/rho);
432
                  }
jclamber's avatar
jclamber committed
433 434 435
                  particles->temp.push_back(temp);
                  particles->load_bits |= TEMP_BIT;
                }
jclamber's avatar
jclamber committed
436
                if (req_bits&METAL_BIT && nvarh>(5+offset_2d)) {
437
                  double metal= var[(5+offset_2d)*ngrida*twotondim+ind*ngrida+i];
jclamber's avatar
jclamber committed
438 439 440
                  particles->metal.push_back(metal);
                  particles->load_bits |= METAL_BIT;
                }
jclamber's avatar
jclamber committed
441
                if (req_bits&METAL_BIT && nvarh<=5) {
442
                  double metal= -1.0; // we put -1.0 when no metellicity
jclamber's avatar
jclamber committed
443 444 445
                  particles->metal.push_back(metal);
                  particles->load_bits |= METAL_BIT;
                }
jclamber's avatar
jclamber committed
446 447 448 449
                if (req_bits&ID_BIT) {
                  particles->id.push_back(-1.); // no id for gas, use "-1"
                  particles->load_bits |= ID_BIT;
                }
jclamber's avatar
jclamber committed
450
                if (take|| !req_bits) { // !req_bits for uns_info and siplay=f
jclamber's avatar
jclamber committed
451 452
                  particles->indexes.push_back(0); // GAS particles
                  particles->ngas++; // One more gas particles
jclamber's avatar
jclamber committed
453

jclamber's avatar
jclamber committed
454
                }
jclamber's avatar
jclamber committed
455 456
                particles->ntot++; // one more total particles
                                   // this variable count all particles
jclamber's avatar
jclamber committed
457 458 459
              }
              nbody++;
            }
460 461 462 463 464 465 466
            else {
//              if (ilevel>=lmin) {
//              std::cerr << "Not ok lmin="<<lmin<<" ilevel="<<ilevel<<" xmin="<<xmin<<" xmax="<<xmax
//                           <<" ymin="<<ymin<<" ymax="<<ymax
//                          <<" zmin="<<zmin<<" zmax="<<zmax<<"\n";
//              }
            }
jclamber's avatar
jclamber committed
467 468 469 470 471 472 473 474 475 476 477
          }
        }
        // garbage collecting
        delete [] xg;
        //delete [] x;
        delete [] son;
        if (!count_only) 
          delete [] var;
      }
    } // ilevel
    amr.close();    
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
478
    hydro.close();
479 480 481
    if (is_gravity) {
      grav.close();
    }
jclamber's avatar
jclamber committed
482 483 484 485
  } //for (int icpu=0 .... 
  return nbody;
}
} // end of namespace ramses