// -*- C++ -*- //////////////////////////////////////////////////////////////////////////////// /// /// \file read_falcON_utils.cc /// /// \brief towards reading falcON HDF5 snapshots into splash /// \date 12-Aug-2015 // // Copyright (C) 2015 Walter Dehnen. // //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // // PART 1: interfaces with C-linkage // this part may be used like a header file // //////////////////////////////////////////////////////////////////////////////// extern "C" { /// maximum number of particles types expected /// (actually falcON currently only supports 3 particle types) static constexpr int max_num_types = 6; // // 1.1 routines to be provided by SPLASH and called from PART 2 below // /// /// transfer data to splash /// /// \param[in] icol column of data /// \param[in] ndat number of data == number of particles of type /// \param[in] data array of ndat data /// \param[in] type index of particle type /// /// \note called by read_falcON_snapshot() void read_falcON_data_into_splash(const int*icol, const int*ndat, const double*data, const int*type); /// /// tell splash about the label of a data column /// /// \param[in] icol column of data /// \param[in] name label for this column /// /// \note called by read_falcON_snapshot() void set_splash_block_label(const int*icol, const char*name); /// /// tell splash about the label of a particle type /// /// \param[in] ityp column of data /// \param[in] name label for this column /// /// \note called by read_falcON_snapshot() void set_splash_particle_label(const int*ityp, const char*name); // // 1.2 routines provided here, but to be called from SPLASH // /// /// set debugging level /// /// \param[in] debug debugging level /// /// \note currently only debug=0,1,2,3 are distinguished. /// 0 means no output in case of an error, /// 1 means diagnostic output in case of error, /// 2 also prints errors from the HDF5 library /// 3 may print extra information (used in debugging this file) void set_falcON_debugging_level(const int*debug); /// /// opens a falcON HDF5 snapshot file /// /// \param[in] filename name of data file /// \param[out] ierr non-zero if file couldn't be opened /// /// \note precondition: none /// \note postcondition: open_falcON_snapshot() can be called /// \note We close any previously opened file (only one snapshot file can be /// open at any time with this implementation), but warn if the /// filename matches with the currently open file, if any. /// \note Use num_falcON_snapshots() for the number of snapshots in the file void open_falcON_file(const char*filename, int*ierr); /// /// queries whether a file is open /// int falcON_file_is_open(); /// /// closes the currently open (if any) falcON HDF5 snapshot file. /// /// \note precondition: none /// \note postcondition: falcON_file_is_open() will return false void close_falcON_file(); /// /// queries if there is another snapshot present the currently open file /// /// \note precondition: falcON_file_is_open() int have_falcON_snapshot(int*ierr); /// /// query the number of snapshots in the currently open file /// /// \note precondition: falcON_file_is_open() /// \note When using have_falcON_snapshot(), it may not be necessary to call /// this function, see main() in PART 3 below for an example. /// \note This function is trivial for falcON HDF5-based snapshot files, /// but non-trivial for NEMO snapshot files, which we may support in /// the future without changing this interface. int num_falcON_snapshots(int*ierr); /// /// opens the next falcON snapshot in the currently open file and /// reads its header /// /// \param[out] ntype number of particle types /// \param[out] npart number of particles per type /// \param[out] ncol number of splash columns /// \param[out] dimX number of spatial dimensions /// \param[out] dimV number of velocity dimensions /// \param[out] time simulation time of snapshot /// \param[out] hper if hper[d]!=0, dimension d is periodic with |x|= 5 // // The HDF5 library may have been compiled with another (older) GNU C++ ABI // than the one used in the rest of the falcON.2 code. Here, we must use the // same C++ ABI as used with the HDF5 C++ library. This is controlled by the // following macro, see also // // https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html // # define _GLIBCXX_USE_CXX11_ABI 0 #endif #if __cplusplus < 201103L # error requiring C++11 #endif #include #include #include #include #include #include #include #include /// /// 2.1 auxiliary functionality /// (symbols from the anonymous namespace have internal linkage /// namespace { /// simple struct with data for each set of particles struct Particles { std::unique_ptr group; ///< HDF5 group std::string name; ///< name: 'sink', 'gas', 'std' std::size_t number; ///< number of particles // (I couldn't figure out to avoid this via aggregate initialisation) Particles(H5::Group*g, std::string const&t, std::size_t n) : group(g), name(t), number(n) {} }; // using name_count_map = std::map; using name_and_count = name_count_map::value_type; // static data int Debug = 0; ///< debug level (0 or not 0) int IndexSnap = 0; ///< index of next snapshot int NumSnap = 0; ///< number of snapshots in file int SplashCol = 0; ///< column for splash data int NumCol = 0; ///< number of splash columns name_count_map Fields; ///< field:dims map of all fields std::vector Part; ///< data for particle sets std::vector Buffer; ///< data buffer for input std::unique_ptr File; ///< file std::string FileName; ///< name of currently open file const hsize_t k[1] = {3}; const H5::ArrayType VectorType ///< HDF5 data type for double[3] {H5::PredType::NATIVE_DOUBLE,1,k}; // closes a splash column inline void close_column(const char*name) { if(SplashCol >= NumCol) throw "number of columns exceeds expected " + std::to_string(NumCol); set_splash_block_label(&SplashCol,name); SplashCol++; } // closes a splash column inline void close_column(std::string const&name) { close_column(name.c_str()); } // closes currently open snapshot inline void close_snapshot() { SplashCol = 0; NumCol = 0; Part.clear(); Fields.clear(); } // select field for this particle type? // NOTE Without selecting, we would potentially read meaningless data. This // will not be necessary in future versions of falcON. inline bool select(std::string const&field, std::string const&ptype) { return (field=="spin" || field=="eabs" || field=="maxA")? (ptype=="sink") : (field=="snum" || field=="uin" || field=="entr" || field=="dlKt" || field=="dlKe" || field=="srho" || field=="alfa" || field=="divv" || field=="dlht" || field=="vsig" || field=="fact" || field=="csnd" || field=="pres" || field=="vort" || field=="dtdv" || field=="qmin" || field=="delE" || field=="coll")? (ptype=="gas") : (field=="krnH" || field=="maxR") ? (ptype=="sink" || ptype=="gas") : true; } // try to read a component of a falcON field into a splash column void read_column(std::string const&field, const hsize_t comp, const hsize_t dims) { bool read = false; // loop particle types for(int type=0; type!=int(Part.size()); ++type) { const auto&part = Part[std::size_t(type)]; if(!select(field,part.name)) continue; H5::DataSet data; try { data = part.group->openDataSet(field); } catch(...) { continue; // field not present: continue with next particle type } // obtain size of data set auto space = data.getSpace(); hsize_t count[2]; auto rank = space.getSimpleExtentDims(count); assert(part.number==count[0]); const int ndat = int(part.number); // read component Buffer.resize(part.number); switch(dims) { case 1: // scalar field assert(rank==1); data.read(Buffer.data(),H5::PredType::NATIVE_DOUBLE,space,space); // reduce smoothing length by factor 2 if(field=="krnH" && part.name=="gas") for(auto&x:Buffer) x*=0.5; if(Debug>2) std::clog<<"reading "<2) std::clog<<"reading "<(std::toupper(field[0])); name+= comp==0? "xx": comp==1? "xy": comp==2? "xz": comp==3? "yy": comp==4? "yz": "zz"; } else name=field; close_column(name); } } // read a field inline void read_field(name_and_count const&field) { for(hsize_t comp=0; comp!=field.second; ++comp) read_column(field.first, comp, field.second); } // try to read a falcON field inline void read_field(std::string const&field) { const auto field_iter = Fields.find(field); if(field_iter==Fields.end()) std::clog<<"WARNING: falcON field '"<openAttribute("falcON"); } catch(...) { if(Debug) std::clog<<"open_falcON_file(): file '"<openAttribute("num_snapshots"); attr.read(H5::PredType::NATIVE_UINT32,&NumSnap); } catch(H5::Exception const&exc) { // should never happen if(Debug) std::clog<<"open_falcON_file('"<= NumSnap) { if(Debug) std::clog<<"open_falcON_snapshot(): no more than " <openGroup(name); // read time and hper auto attr = snap.openAttribute("time"); attr.read(H5::PredType::NATIVE_DOUBLE,time); attr = snap.openAttribute("hper"); attr.read(VectorType,hper); // read npart[] and open particle sets std::array types = {{"sink","gas","std"}}; for(const auto&type:types) { name = "N"; name+= type; unsigned number; try { attr = snap.openAttribute(name); attr.read(H5::PredType::NATIVE_UINT32,&number); } catch(...) { // should never go here number = 0; } if(number) { // open HDF5 group for particles const auto part = snap.openGroup(type); Part.emplace_back(new H5::Group(part),type,number); npart[(*ntyp)++] = int(number); // collect (field:dims) pairs and count columns for(hsize_t fld=0; fld!=part.getNumObjs(); ++fld) { const auto field = part.getObjnameByIdx(fld); const auto have_field = Fields.count(field); if(have_field) { // field 'krnH' generates an extra column for each particle type if(field=="krnH") (*ncol)++; } else { hsize_t count[2]; const auto dims = 1==part.openDataSet(field).getSpace(). getSimpleExtentDims(count)? 1:count[1]; Fields[field] = dims; (*ncol)+= int(dims); } if(Debug>2) std::clog<<"counting columns: type="<" <<(*ncol)< PreferredOrder = {"pos", "key", "vel", "acc", "mass", "pot", "pex", "rung", "krnH", "srho", "uin", "entr", "divv", "dlKt", "dlKe", "alfa"}; try { auto FieldsToRead=Fields; // read some fields in preferred order for(const auto&field:PreferredOrder) if(FieldsToRead.erase(field)) read_field(field); // read remaining fields for(const auto&field:FieldsToRead) read_field(field); } catch(H5::Exception const&exc) { // catch any HDF5 error if(Debug) std::clog<<"read_falcON_snapshot(): HDF5 error: \"" < #include #include // // 3.1 implement interface 1.1 using C++ // namespace { struct data_per_particle_type { std::string name; std::size_t number; std::vector< std::vector > columns; }; std::vector particle_data; std::vector column_labels; } // void set_splash_block_label(const int*icol, const char*name) { column_labels.at(std::size_t(*icol)) = name; } // void set_splash_particle_label(const int*type, const char*name) { particle_data.at(std::size_t(*type)).name = name; } // void read_falcON_data_into_splash(const int*icol, const int*ndat, const double*from, const int*type) { auto&part = particle_data.at(std::size_t(*type)); assert(*ndat > 0); assert(part.number == std::size_t(*ndat)); part.columns.at(std::size_t(*icol)).resize(std::size_t(*ndat)); std::copy(from,from+*ndat,part.columns[std::size_t(*icol)].begin()); } // // 3.2 an executable that dumps a falcON file // int main(int argc, const char**argv) { // obtain program parameters if(argc<2) { std::clog<<"usage: '"<