uns.cc 13 KB
Newer Older
jcl's avatar
jcl committed
1
// ============================================================================
jclamber's avatar
jclamber committed
2
// Copyright Jean-Charles LAMBERT - 2008-2013
jcl's avatar
jcl committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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 6110                                                   
// ============================================================================

#include "uns.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <assert.h>
#include "snapshotinterface.h"
#include "snapshotgadget.h"
19
#include "snapshotramses.h"
jcl's avatar
jcl committed
20 21 22 23 24 25 26 27 28 29 30 31
#include "snapshotnemo.h"
#include "snapshotsim.h"
#include "snapshotlist.h"
#include "userselection.h"
#include "ctools.h"

#define DEBUG 0
#include "unsdebug.h"

namespace uns {
  // static variable to store DATA string
  std::map<std::string, StringData> CunsOut::s_mapStringValues;
jclamber's avatar
jclamber committed
32
  std::map<std::string, int> CunsIn::s_mapCompInt;
jcl's avatar
jcl committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  // ----------------------------------------------------------------------------
  // READING OPERATIONS
  CunsIn::CunsIn(const std::string _name ,const std::string _comp ,const std::string _time, const bool verb)
  {
    init(_name,_comp,_time,verb);
  }

  // ----------------------------------------------------------------------------
  // constructor for READING operations
  CunsIn::CunsIn(const char * _name ,const char * _comp, const char * _time,
                 const bool verb)
  {
    init(_name,_comp,_time,verb);
  }
  // ----------------------------------------------------------------------------
  //
  void CunsIn::init(const std::string _name ,const std::string _comp ,const std::string _time, const bool verb )
  {
    valid = false;
    simname  = tools::Ctools::fixFortran(_name.c_str(),false);
    sel_comp = tools::Ctools::fixFortran(_comp.c_str(),false);
    sel_time = tools::Ctools::fixFortran(_time.c_str(),false);
    
jclamber's avatar
jclamber committed
56 57 58 59 60 61 62 63 64 65
    // initialise some maps
    CunsIn::s_mapCompInt["gas"        ] = 0;
    CunsIn::s_mapCompInt["halo"       ] = 1;
    CunsIn::s_mapCompInt["dm"         ] = 1;
    CunsIn::s_mapCompInt["disk"       ] = 2;
    CunsIn::s_mapCompInt["bulge"      ] = 3;
    CunsIn::s_mapCompInt["stars"      ] = 4;
    CunsIn::s_mapCompInt["bndry"      ] = 5;
    CunsIn::s_mapCompInt["all"        ] =-1;

jcl's avatar
jcl committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79
    // to lower
    //simname  = tools::Ctools::tolower(simname);
    //sel_comp = tools::Ctools::tolower(sel_comp);
    //sel_time = tools::Ctools::tolower(sel_time);
    
    verbose=verb;
    snapshot = NULL;
    PRINT("name    ["<< simname  <<"]\n");
    PRINT("sel_comp["<< sel_comp <<"]\n");
    PRINT("sel_time["<< sel_time <<"]\n");
    CunsOut::initializeStringMap(verbose);
    if (simname == "-") { // we assume here that "-"
      tryNemo();          // is standard input and a NEMO stream...      
    } else {
80 81
      if (tools::Ctools::isFileExist(simname)   && // file exist
          ! tools::Ctools::isDirectory(simname)) { // not a directory
jcl's avatar
jcl committed
82
        tryGadget();               // try gadget 
83 84 85
        if (!valid) {              // gadget failed
          tryRamses();             // try ramses
        }
jcl's avatar
jcl committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        if (!valid) {              // gadget failed
          tryNemo();               // try nemo   
        }
        if (!valid) {              // nemo 
          trySnapList();           // try snapshotlist   
        }
        if (!valid) {
          trySimDB();              // try DataBase       
        }
      }
      else {                       // file does not exist
        tryGadget();               // try gadget parallel output
        if (!valid) {
          trySimDB();              // try DataBase       
        }
      }
    }
    if (valid && verb) {
      std::cerr << "File      : " << snapshot->getFileName() << "\n";
      std::cerr << "Interface : " << snapshot->getInterfaceType() << "\n";
    }
107 108 109
    if (!valid){
      std::cerr << "\nFile ["<< snapshot->getFileName() <<"], unknown UNS file format, aborting.....\n\n";
    }
jcl's avatar
jcl committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  }
  // ----------------------------------------------------------------------------
  // destructor for READING operations  
  CunsIn::~CunsIn()
  {
    if (snapshot) delete snapshot;
  }
  
  // ----------------------------------------------------------------------------
  // tryGadget                                                                   
  void CunsIn::tryGadget()
  {
    PRINT("tryGadget("<<simname<<")\n");
    snapshot = new CSnapshotGadgetIn(simname, sel_comp, sel_time, verbose);
    valid = snapshot->isValidData();
  }
  // ----------------------------------------------------------------------------
127 128 129 130 131 132 133 134
  // tryRamses
  void CunsIn::tryRamses()
  {
    PRINT("tryRamses("<<simname<<")\n");
    snapshot = new CSnapshotRamsesIn(simname, sel_comp, sel_time, verbose);
    valid = snapshot->isValidData();
  }
  // ----------------------------------------------------------------------------
jcl's avatar
jcl committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  // tryNemo                                                                     
  void CunsIn::tryNemo()
  {
    PRINT("tryNemo("<<simname<<")\n");
    snapshot = new CSnapshotNemoIn(simname, sel_comp, sel_time, verbose);
    valid = snapshot->isValidData();
  }
  // ----------------------------------------------------------------------------
  // trySim                                                                      
  void CunsIn::trySimDB()
  {
#ifndef NOSQLITE3  
    snapshot = new CSnapshotSimIn(simname, sel_comp, sel_time, verbose);
    valid = snapshot->isValidData();
    if (valid && verbose) {
jclamber's avatar
jclamber committed
150
      std::cerr << "CunsIn::trySimDB() It's recorded to sqlite3 database...\n";
jcl's avatar
jcl committed
151 152 153 154 155 156 157 158 159 160 161 162
    }
#else
    valid = false;
#endif
  }
  // ----------------------------------------------------------------------------
  // trySnapList                                                                      
  void CunsIn::trySnapList()
  {
    snapshot = new CSnapshotList(simname, sel_comp, sel_time, verbose);
    valid = snapshot->isValidData();
  }
jclamber's avatar
jclamber committed
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
  // ----------------------------------------------------------------------------
  // nextFrame
  int CunsIn::nextFrame(const char *  _bits) {
    std::string bits(_bits);
    int ok=snapshot->nextFrame(bits);
    return ok;
  }
    
  // ----------------------------------------------------------------------------
  // getData
  bool CunsIn::getData(const std::string  comp,const std::string  prop,
		       int * size,float ** farray) {
    float * data=NULL;
    int nbody=0;
    *size=0;
    bool ok=snapshot->getData(comp,prop,&nbody,&data);
    if (ok) {
      int dim=1;
      if (prop=="pos" || prop == "vel" || prop == "acc") dim=3;
      *farray = data;
      *size=nbody*dim;	
    }
    return ok;
  }
  // ----------------------------------------------------------------------------
  // getData
  bool CunsIn::getData(const std::string  prop,
		       int * size,float ** farray) {
    float * data=NULL;
    int nbody=0;
    *size=0;
    bool ok=snapshot->getData(prop,&nbody,&data);
    if (ok) {
      int dim=1;
      if (prop=="pos" || prop == "vel" || prop == "acc") dim=3;
      *farray = data;
      *size=nbody*dim;	
    }
    return ok;
  }
  // ----------------------------------------------------------------------------
  // getData
  bool CunsIn::getData(const std::string  prop,float * fvalue) {
    bool ok=snapshot->getData(prop,fvalue);
    return ok;
  }
  // ----------------------------------------------------------------------------
  // getData
  // int
  bool CunsIn::getData(const std::string  comp,const std::string  prop,
		       int * size,int ** iarray) {
    int * data=NULL;
    int nbody=0;
    *size=0;
    bool ok=snapshot->getData(comp,prop,&nbody,&data);
    if (ok) {
      int dim=1;
      *iarray = data;
      *size=nbody*dim;	
    }
    return ok;
  }
jclamber's avatar
jclamber committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  // ----------------------------------------------------------------------------
  // getData
  // int
  bool CunsIn::getData(const std::string  prop,
		       int * size,int ** iarray) {
    int * data=NULL;
    int nbody=0;
    *size=0;
    bool ok=snapshot->getData(prop,&nbody,&data);
    if (ok) {
      int dim=1;
      *iarray = data;
      *size=nbody*dim;	
    }
    return ok;
  }
  // ----------------------------------------------------------------------------
  // getData
  // int
  bool CunsIn::getData(const std::string  prop,int * ivalue) {
    bool ok=snapshot->getData(prop,ivalue);
    return ok;
  }
jcl's avatar
jcl committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
  // ----------------------------------------------------------------------------
  // WRITING OPERATIONS
  //  ---------------------------------------------------------------------------
  // constructor
  CunsOut::CunsOut(const std::string _name, const std::string _type, const bool _verb )
  {
    simname  = tools::Ctools::fixFortran(_name.c_str(),false);
    simtype  = tools::Ctools::fixFortran(_type.c_str(),false);
    verbose = _verb;
    snapshot= NULL;
    initializeStringMap(verbose);
    simtype = tools::Ctools::tolower(simtype);
    if (simtype == "gadget2") {    
      snapshot = new CSnapshotGadgetOut(simname,simtype,verbose);
    } else {
      if (simtype == "nemo") {
        snapshot = new CSnapshotNemoOut(simname,simtype,verbose);
      }
      else {
        std::cerr << "Unkonwn UNS output file format => ["<<simtype<<"]"
jclamber's avatar
jclamber committed
268
		  << " aborting program...... \n\n";
jcl's avatar
jcl committed
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
        std::exit(1);      
      }
    }
  }
  // ----------------------------------------------------------------------------
  // destructor for READING operations  
  CunsOut::~CunsOut()
  {
    if (snapshot) delete snapshot;
  }
  // ----------------------------------------------------------------------------
  // initializeStringMap
  void  CunsOut::initializeStringMap(const bool verbose)
  {
    
    CunsOut::s_mapStringValues["time"       ] = uns::Time;
    CunsOut::s_mapStringValues["redshift"   ] = uns::Redshift;
    CunsOut::s_mapStringValues["pos"        ] = uns::Pos;
    CunsOut::s_mapStringValues["vel"        ] = uns::Vel;
    CunsOut::s_mapStringValues["mass"       ] = uns::Mass;
    CunsOut::s_mapStringValues["id"         ] = uns::Id;
    CunsOut::s_mapStringValues["rho"        ] = uns::Rho;
    CunsOut::s_mapStringValues["hsml"       ] = uns::Hsml;
    CunsOut::s_mapStringValues["u"          ] = uns::U;
    CunsOut::s_mapStringValues["aux"        ] = uns::Aux;
    CunsOut::s_mapStringValues["acc"        ] = uns::Acc;
    CunsOut::s_mapStringValues["pot"        ] = uns::Pot;
296
    CunsOut::s_mapStringValues["eps"        ] = uns::Eps;
jcl's avatar
jcl committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    CunsOut::s_mapStringValues["keys"       ] = uns::Keys;
    CunsOut::s_mapStringValues["age"        ] = uns::Age;
    CunsOut::s_mapStringValues["temp"       ] = uns::Temp;
    CunsOut::s_mapStringValues["metal"      ] = uns::Metal;
    CunsOut::s_mapStringValues["gas_metal"  ] = uns::GasMetal;
    CunsOut::s_mapStringValues["stars_metal"] = uns::StarsMetal;
    CunsOut::s_mapStringValues["nsel"       ] = uns::Nsel;
    CunsOut::s_mapStringValues["nbody"      ] = uns::Nbody;
    CunsOut::s_mapStringValues["ngas"       ] = uns::Ngas;
    CunsOut::s_mapStringValues["nhalo"      ] = uns::Nhalo;
    CunsOut::s_mapStringValues["ndisk"      ] = uns::Ndisk;
    CunsOut::s_mapStringValues["nbulge"     ] = uns::Nbulge;
    CunsOut::s_mapStringValues["nstars"     ] = uns::Nstars;
    CunsOut::s_mapStringValues["nbndry"     ] = uns::Nbndry;
    CunsOut::s_mapStringValues["gas"        ] = uns::Gas;
    CunsOut::s_mapStringValues["halo"       ] = uns::Halo;
313
    CunsOut::s_mapStringValues["dm"         ] = uns::Halo;
jclamber's avatar
jclamber committed
314
    CunsOut::s_mapStringValues["ndm"        ] = uns::Halo;
jcl's avatar
jcl committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328
    CunsOut::s_mapStringValues["bulge"      ] = uns::Bulge;
    CunsOut::s_mapStringValues["disk"       ] = uns::Disk;
    CunsOut::s_mapStringValues["stars"      ] = uns::Stars;
    CunsOut::s_mapStringValues["bndry"      ] = uns::Bndry;
    CunsOut::s_mapStringValues["all"        ] = uns::All;
    CunsOut::s_mapStringValues["gas_mpv"    ] = uns::GasMPV;
    CunsOut::s_mapStringValues["halo_mpv"   ] = uns::HaloMPV;
    CunsOut::s_mapStringValues["bulge_mpv"  ] = uns::BulgeMPV;
    CunsOut::s_mapStringValues["disk_mpv"   ] = uns::DiskMPV;
    CunsOut::s_mapStringValues["stars_mpv"  ] = uns::StarsMPV;
    CunsOut::s_mapStringValues["bndry_mpv"  ] = uns::BndryMPV;
    CunsOut::s_mapStringValues["zs"         ] = uns::Zs;
    CunsOut::s_mapStringValues["zsmt"       ] = uns::ZSMT;
    CunsOut::s_mapStringValues["im"         ] = uns::Im;
jclamber's avatar
jclamber committed
329
    CunsOut::s_mapStringValues["ssl"        ] = uns::Ssl;
330
    CunsOut::s_mapStringValues["cm"         ] = uns::Cm;
jcl's avatar
jcl committed
331 332 333 334
    CunsOut::s_mapStringValues["czs"        ] = uns::Czs;
    CunsOut::s_mapStringValues["czsmt"      ] = uns::Czsmt;
    if (verbose) {
      std::cout << "CunsOut::initializeStringMap s_mapStringValues contains "
jclamber's avatar
jclamber committed
335
		<< CunsOut::s_mapStringValues.size() << " entries." << std::endl;
jcl's avatar
jcl committed
336 337 338 339 340 341 342 343
    }
  }
  
  
}
// ---------------------------------------------------------------------------- 
// End of file                                                                 
// ----------------------------------------------------------------------------