uns.cc 16.6 KB
Newer Older
jcl's avatar
jcl committed
1
// ============================================================================
2
// Copyright Jean-Charles LAMBERT - 2008-2016
jclamber's avatar
jclamber committed
3
//           Centre de donneeS Astrophysiques de Marseille (CeSAM)
4 5 6 7 8 9 10
// e-mail:   Jean-Charles.Lambert@lam.fr
// address:  Aix Marseille Universite, CNRS, LAM
//           Laboratoire d'Astrophysique de Marseille
//           Pole de l'Etoile, site de Chateau-Gombert
//           38, rue Frederic Joliot-Curie
//           13388 Marseille cedex 13 France
//           CNRS UMR 7326
jcl's avatar
jcl committed
11 12 13 14 15 16 17 18 19
// ============================================================================

#include "uns.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <assert.h>
#include "snapshotinterface.h"
#include "snapshotgadget.h"
20
#include "snapshotgadgeth5.h"
21
#include "snapshotramses.h"
jcl's avatar
jcl committed
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 {
32
// static variable to store DATA string
33 34
template <class T> std::map<std::string, StringData> CunsOut2<T>::s_mapStringValues;
template <class T> std::map<std::string, int> CunsIn2<T>::s_mapCompInt;
jclamber's avatar
jclamber committed
35

36 37
// ----------------------------------------------------------------------------
// READING OPERATIONS
38
template <class T>  CunsIn2<T>::CunsIn2(const std::string _name ,const std::string _comp ,const std::string _time, const bool verb)
39 40 41 42 43
{
  init(_name,_comp,_time,verb);
}
// ----------------------------------------------------------------------------
// constructor for READING operations
44
template <class T>  CunsIn2<T>::CunsIn2(const char * _name ,const char * _comp, const char * _time,
45 46 47 48 49 50
               const bool verb)
{
  init(_name,_comp,_time,verb);
}
// ----------------------------------------------------------------------------
//
51
template <class T>  void CunsIn2<T>::init(const std::string _name ,const std::string _comp ,const std::string _time, const bool verb )
52
{
53
  if (verb) {
54
    std::cerr << "CunsIn2::CunsIn2 -- UNSIO version = "<<uns::getVersion()<< "\n";
55
  }
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
56

57 58 59 60
  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
61

62
  initMap();
63 64 65 66 67 68 69 70 71 72 73

  // 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");
74
  CunsOut2<T>::initializeStringMap(verbose);
75 76 77
  if (simname == "-") { // we assume here that "-"
    tryNemo();          // is standard input and a NEMO stream...
  } else {
78
    if (tools::Ctools::isFileExist(simname)) { // file exist
79
      if ( tools::Ctools::isDirectory(simname)) {
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
80
        PRINT("try RAMSES 1st\n");
81
        tryRamses();             // try ramses
82 83 84 85 86
        if (!valid) {
          trySimDB();              // try DataBase
        }
      } else {
        // ! ) { // not a directory
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
87
        PRINT("try GADGET\n");
88 89
        tryGadget();               // try gadget
        if (!valid) {
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
90
          PRINT("try RAMSES\n");
jclamber's avatar
jclamber committed
91
          tryRamses();             // try ramses
92 93
        }
        if (!valid) {              // gadget failed
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
94
          PRINT("try NEMO\n");
95 96
          tryNemo();               // try nemo
        }
97
        if (!valid) {
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
98
          PRINT("try HDF5\n");
99 100
          tryGadgetH5();           // try gadgetHDF5
        }
101
        if (!valid) {              // nemo
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
102
          PRINT("try LIST\n");
103
          trySnapList();           // try snapshotlist
104
        }
105
        if (!valid) {
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
106
          PRINT("try DATABASE\n");
107 108
          trySimDB();              // try DataBase
        }
jcl's avatar
jcl committed
109 110
      }
    }
111
    else {                       // file does not exist
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
112
      PRINT("try GADGET 2nd\n");
113 114
      tryGadget();               // try gadget parallel output
      if (!valid) {
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
115
        PRINT("try DATABASE2\n");
116 117
        trySimDB();              // try DataBase
      }
118
    }
jcl's avatar
jcl committed
119
  }
120 121 122
  if (valid && verb) {
    std::cerr << "File      : " << snapshot->getFileName() << "\n";
    std::cerr << "Interface : " << snapshot->getInterfaceType() << "\n";
123
  }
124 125
  if (!valid){
    std::cerr << "\nFile ["<< snapshot->getFileName() <<"], unknown UNS file format, aborting.....\n\n";
jcl's avatar
jcl committed
126
  }
127 128 129
}
// ----------------------------------------------------------------------------
// destructor for READING operations
130
template <class T>  CunsIn2<T>::~CunsIn2()
131 132 133 134
{
  if (snapshot) delete snapshot;
}
// ----------------------------------------------------------------------------
135 136
// tryGadget binary 1 and 2
template <class T> void CunsIn2<T>::tryGadget()
137 138
{
  PRINT("tryGadget("<<simname<<")\n");
139 140 141 142 143 144 145 146 147
  snapshot = new CSnapshotGadgetIn<T>(simname, sel_comp, sel_time, verbose);
  valid = snapshot->isValidData();
}
// ----------------------------------------------------------------------------
// tryGadget HDF5
template <class T> void CunsIn2<T>::tryGadgetH5()
{
  PRINT("tryGadgetH5("<<simname<<")\n");
  snapshot = new CSnapshotGadgetH5In<T>(simname, sel_comp, sel_time, verbose);
148 149 150 151
  valid = snapshot->isValidData();
}
// ----------------------------------------------------------------------------
// tryRamses
152
template <class T> void CunsIn2<T>::tryRamses()
153 154
{
  PRINT("tryRamses("<<simname<<")\n");
155
  snapshot = new CSnapshotRamsesIn<T>(simname, sel_comp, sel_time, verbose);
156 157 158 159
  valid = snapshot->isValidData();
}
// ----------------------------------------------------------------------------
// tryNemo
160
template <class T> void CunsIn2<T>::tryNemo()
161 162
{
  PRINT("tryNemo("<<simname<<")\n");
163
  snapshot = new CSnapshotNemoIn<T>(simname, sel_comp, sel_time, verbose);
164 165 166 167
  valid = snapshot->isValidData();
}
// ----------------------------------------------------------------------------
// trySim
168
template <class T> void CunsIn2<T>::trySimDB()
169
{
170
#ifndef NOSQLITE3
171
  snapshot = new CSnapshotSimIn<T>(simname, sel_comp, sel_time, verbose);
172 173
  valid = snapshot->isValidData();
  if (valid && verbose) {
174
    std::cerr << "CunsIn2::trySimDB() It's recorded to sqlite3 database...\n";
175
  }
jcl's avatar
jcl committed
176
#else
177
  valid = false;
jcl's avatar
jcl committed
178
#endif
179

180 181 182
}
// ----------------------------------------------------------------------------
// trySnapList
183
template <class T> void CunsIn2<T>::trySnapList()
184
{
185
  snapshot = new CSnapshotList<T>(simname, sel_comp, sel_time, verbose);
186 187 188 189
  valid = snapshot->isValidData();
}
// ----------------------------------------------------------------------------
// nextFrame
190
template <class T> int CunsIn2<T>::nextFrame(const char *  _bits) {
191 192 193 194 195 196
  std::string bits(_bits);
  int ok=snapshot->nextFrame(bits);
  return ok;
}
// ----------------------------------------------------------------------------
// getData
197
template <class T> bool CunsIn2<T>::getData(const std::string  comp,const std::string  prop,
198
                     unsigned int * size,T ** farray) {
199
  T * data=NULL;
200 201 202 203 204 205 206 207
  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;
jcl's avatar
jcl committed
208
  }
209 210 211 212
  return ok;
}
// ----------------------------------------------------------------------------
// getData
213
template <class T> bool CunsIn2<T>::getData(const std::string  prop,
214
                     unsigned int * size,T ** farray) {
215
  T * data=NULL;
216 217 218 219 220 221 222 223
  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;
jclamber's avatar
jclamber committed
224
  }
225 226 227 228
  return ok;
}
// ----------------------------------------------------------------------------
// getData
229
template <class T> bool CunsIn2<T>::getData(const std::string  prop,T * fvalue) {
230 231 232 233 234 235
  bool ok=snapshot->getData(prop,fvalue);
  return ok;
}
// ----------------------------------------------------------------------------
// getData
// int
236
template <class T> bool CunsIn2<T>::getData(const std::string  comp,const std::string  prop,
237
                     unsigned int * size,int ** iarray) {
238 239 240 241 242 243 244 245
  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;
jclamber's avatar
jclamber committed
246
  }
247 248 249 250 251
  return ok;
}
// ----------------------------------------------------------------------------
// getData
// int
252
template <class T> bool CunsIn2<T>::getData(const std::string  prop,
253
                     unsigned int * size,int ** iarray) {
254 255 256 257 258 259 260 261
  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;
jclamber's avatar
jclamber committed
262
  }
263 264 265 266 267
  return ok;
}
// ----------------------------------------------------------------------------
// getData
// int
268
template <class T> bool CunsIn2<T>::getData(const std::string  prop,int * ivalue) {
269 270 271 272 273 274 275 276 277
  bool ok=snapshot->getData(prop,ivalue);
  return ok;
}



// ----------------------------------------------------------------------------
// WRITING OPERATIONS
//  ---------------------------------------------------------------------------
278 279

//  ---------------------------------------------------------------------------
280
// constructor
281
template <class T> CunsOut2<T>::CunsOut2(const std::string _name, const std::string _type, const bool _verb )
282 283 284 285 286
{
  simname  = tools::Ctools::fixFortran(_name.c_str(),false);
  simtype  = tools::Ctools::fixFortran(_type.c_str(),false);
  verbose = _verb;
  snapshot= NULL;
jclamber's avatar
jclamber committed
287
  if (verbose) {
288
    std::cerr << "CunsOut2<T>::CunsOut2 -- UNSIO version = "<<uns::getVersion()<< "\n";
jclamber's avatar
jclamber committed
289
  }
290 291
  initializeStringMap(verbose);
  simtype = tools::Ctools::tolower(simtype);
292
  if (simtype == "gadget2" || simtype == "gadget1") {
293
    snapshot = new CSnapshotGadgetOut<T>(simname,simtype,verbose);
294 295
  } else {
    if (simtype == "nemo") {
296
      snapshot = new CSnapshotNemoOut<T>(simname,simtype,verbose);
jclamber's avatar
jclamber committed
297
    }
298
    else {
299 300 301 302 303
      if (simtype == "gadget3") {
        snapshot = new CSnapshotGadgetH5Out<T>(simname,simtype,verbose);
      }
      else {
        std::cerr << "Unkonwn UNS output file format => ["<<simtype<<"]"
304
                << " aborting program...... \n\n";
305 306
        std::exit(1);
      }
jcl's avatar
jcl committed
307 308
    }
  }
309 310 311
}
// ----------------------------------------------------------------------------
// destructor for READING operations
312
template <class T> CunsOut2<T>::~CunsOut2()
313 314 315 316 317
{
  if (snapshot) delete snapshot;
}
// ----------------------------------------------------------------------------
// setData comp prop farray
318
template <class T> int CunsOut2<T>::setData(const std::string  comp,const std::string  prop,
319
            unsigned int  size,T * farray, const bool _addr) {
320 321 322 323 324
  int status = snapshot->setData(comp,prop,size,farray,_addr);
  return status;
}
// ----------------------------------------------------------------------------
// setData prop farray
325
template <class T> int CunsOut2<T>::setData(const std::string  prop,
326
            unsigned int  size,T * farray, const bool _addr) {
327 328 329 330 331
  int status = snapshot->setData(prop,size,farray,_addr);
  return status;
}
// ----------------------------------------------------------------------------
// setData prop fvalue
332
template <class T> int CunsOut2<T>::setData(const std::string  prop,T fvalue) {
333 334 335 336 337
  int status = snapshot->setData(prop,fvalue);
  return status;
}
// ----------------------------------------------------------------------------
// setData comp prop iarray
338
template <class T> int CunsOut2<T>::setData(const std::string  comp,const std::string  prop,
339
                   unsigned int  size,int * iarray, const bool _addr) {
340 341 342 343 344
  int status = snapshot->setData(comp,prop,size,iarray,_addr);
  return status;
}
// ----------------------------------------------------------------------------
// setData prop iarray
345
template <class T> int CunsOut2<T>::setData(const std::string  prop,
346
                     unsigned int  size,int * iarray, const bool _addr) {
347 348 349 350 351
  int status = snapshot->setData(prop,size,iarray,_addr);
  return status;
}
// ----------------------------------------------------------------------------
// setData prop ivalue
352
template <class T> int CunsOut2<T>::setData(const std::string  prop,int ivalue) {
353 354 355 356 357
  int status = snapshot->setData(prop,ivalue);
  return status;
}
// ----------------------------------------------------------------------------
// setData
358
template <class T> int CunsOut2<T>::save()
359 360 361 362 363
{
  return snapshot->save();
}
// ----------------------------------------------------------------------------
// initializeStringMap
364
template <class T> void  CunsOut2<T>::initializeStringMap(const bool verbose)
365 366
{

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
  CunsOut2<T>::s_mapStringValues["time"       ] = uns::Time;
  CunsOut2<T>::s_mapStringValues["redshift"   ] = uns::Redshift;
  CunsOut2<T>::s_mapStringValues["pos"        ] = uns::Pos;
  CunsOut2<T>::s_mapStringValues["vel"        ] = uns::Vel;
  CunsOut2<T>::s_mapStringValues["mass"       ] = uns::Mass;
  CunsOut2<T>::s_mapStringValues["id"         ] = uns::Id;
  CunsOut2<T>::s_mapStringValues["rho"        ] = uns::Rho;
  CunsOut2<T>::s_mapStringValues["hsml"       ] = uns::Hsml;
  CunsOut2<T>::s_mapStringValues["u"          ] = uns::U;
  CunsOut2<T>::s_mapStringValues["aux"        ] = uns::Aux;
  CunsOut2<T>::s_mapStringValues["acc"        ] = uns::Acc;
  CunsOut2<T>::s_mapStringValues["pot"        ] = uns::Pot;
  CunsOut2<T>::s_mapStringValues["eps"        ] = uns::Eps;
  CunsOut2<T>::s_mapStringValues["keys"       ] = uns::Keys;
  CunsOut2<T>::s_mapStringValues["age"        ] = uns::Age;
  CunsOut2<T>::s_mapStringValues["temp"       ] = uns::Temp;
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
383 384 385
  CunsOut2<T>::s_mapStringValues["ne"         ] = uns::Temp;
  CunsOut2<T>::s_mapStringValues["nh"         ] = uns::Nh;
  CunsOut2<T>::s_mapStringValues["sfr"        ] = uns::Sfr;
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
  CunsOut2<T>::s_mapStringValues["metal"      ] = uns::Metal;
  CunsOut2<T>::s_mapStringValues["gas_metal"  ] = uns::GasMetal;
  CunsOut2<T>::s_mapStringValues["stars_metal"] = uns::StarsMetal;
  CunsOut2<T>::s_mapStringValues["nsel"       ] = uns::Nsel;
  CunsOut2<T>::s_mapStringValues["nbody"      ] = uns::Nbody;
  CunsOut2<T>::s_mapStringValues["ngas"       ] = uns::Ngas;
  CunsOut2<T>::s_mapStringValues["nhalo"      ] = uns::Nhalo;
  CunsOut2<T>::s_mapStringValues["ndisk"      ] = uns::Ndisk;
  CunsOut2<T>::s_mapStringValues["nbulge"     ] = uns::Nbulge;
  CunsOut2<T>::s_mapStringValues["nstars"     ] = uns::Nstars;
  CunsOut2<T>::s_mapStringValues["nbndry"     ] = uns::Nbndry;
  CunsOut2<T>::s_mapStringValues["gas"        ] = uns::Gas;
  CunsOut2<T>::s_mapStringValues["halo"       ] = uns::Halo;
  CunsOut2<T>::s_mapStringValues["dm"         ] = uns::Halo;
  CunsOut2<T>::s_mapStringValues["ndm"        ] = uns::Halo;
  CunsOut2<T>::s_mapStringValues["bulge"      ] = uns::Bulge;
  CunsOut2<T>::s_mapStringValues["disk"       ] = uns::Disk;
  CunsOut2<T>::s_mapStringValues["stars"      ] = uns::Stars;
  CunsOut2<T>::s_mapStringValues["bndry"      ] = uns::Bndry;
  CunsOut2<T>::s_mapStringValues["all"        ] = uns::All;
  CunsOut2<T>::s_mapStringValues["gas_mpv"    ] = uns::GasMPV;
  CunsOut2<T>::s_mapStringValues["halo_mpv"   ] = uns::HaloMPV;
  CunsOut2<T>::s_mapStringValues["bulge_mpv"  ] = uns::BulgeMPV;
  CunsOut2<T>::s_mapStringValues["disk_mpv"   ] = uns::DiskMPV;
  CunsOut2<T>::s_mapStringValues["stars_mpv"  ] = uns::StarsMPV;
  CunsOut2<T>::s_mapStringValues["bndry_mpv"  ] = uns::BndryMPV;
  CunsOut2<T>::s_mapStringValues["zs"         ] = uns::Zs;
  CunsOut2<T>::s_mapStringValues["zsmt"       ] = uns::ZSMT;
  CunsOut2<T>::s_mapStringValues["im"         ] = uns::Im;
  CunsOut2<T>::s_mapStringValues["ssl"        ] = uns::Ssl;
  CunsOut2<T>::s_mapStringValues["cm"         ] = uns::Cm;
  CunsOut2<T>::s_mapStringValues["czs"        ] = uns::Czs;
  CunsOut2<T>::s_mapStringValues["czsmt"      ] = uns::Czsmt;
419 420 421 422
  //
  CunsOut2<T>::s_mapStringValues["header"     ] = uns::Header;
  // EXTRA
  CunsOut2<T>::s_mapStringValues["EXTRA"      ] = uns::Extra;
423 424 425 426
  // HYDRO
  CunsOut2<T>::s_mapStringValues["hydro"      ] = uns::Hydro;
  CunsOut2<T>::s_mapStringValues["nvarh"      ] = uns::Nvarh;

427
  if (verbose) {
428 429
    std::cout << "CunsOut2<T>::initializeStringMap s_mapStringValues contains "
              << CunsOut2<T>::s_mapStringValues.size() << " entries." << std::endl;
jcl's avatar
jcl committed
430
  }
431 432
}

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Templates instantiation MUST be declared **AFTER** templates declaration
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// C++11
//extern template class CunsIn2<float>;
template class CunsIn2<float>;

//extern template class CunsIn2<double>;
template class CunsIn2<double>;

// C++11
//extern template class CunsOut2<float>;
template class CunsOut2<float>;

//extern template class CunsOut2<double>;
template class CunsOut2<double>;
449

jcl's avatar
jcl committed
450
}
451 452 453
// ----------------------------------------------------------------------------
// End of file
// ----------------------------------------------------------------------------