Debian Codegraph @

src/read_data_falcON_hdf5_utils.cc

raw
1 // -*- C++ -*-
2 ////////////////////////////////////////////////////////////////////////////////
3 ///
4 /// \file read_falcON_utils.cc
5 ///
6 /// \brief towards reading falcON HDF5 snapshots into splash
7 /// \date 12-Aug-2015
8 //
9 // Copyright (C) 2015 Walter Dehnen.
10 //
11 ////////////////////////////////////////////////////////////////////////////////
12
13 ////////////////////////////////////////////////////////////////////////////////
14 //
15 // PART 1: interfaces with C-linkage
16 // this part may be used like a header file
17 //
18 ////////////////////////////////////////////////////////////////////////////////
19 extern "C" {
20 /// maximum number of particles types expected
21 /// (actually falcON currently only supports 3 particle types)
22 static constexpr int max_num_types = 6;
23
24 //
25 // 1.1 routines to be provided by SPLASH and called from PART 2 below
26 //
27
28 ///
29 /// transfer data to splash
30 ///
31 /// \param[in] icol column of data
32 /// \param[in] ndat number of data == number of particles of type
33 /// \param[in] data array of ndat data
34 /// \param[in] type index of particle type
35 ///
36 /// \note called by read_falcON_snapshot()
37 void read_falcON_data_into_splash(const int*icol, const int*ndat,
38 const double*data, const int*type);
39
40 ///
41 /// tell splash about the label of a data column
42 ///
43 /// \param[in] icol column of data
44 /// \param[in] name label for this column
45 ///
46 /// \note called by read_falcON_snapshot()
47 void set_splash_block_label(const int*icol, const char*name);
48
49 ///
50 /// tell splash about the label of a particle type
51 ///
52 /// \param[in] ityp column of data
53 /// \param[in] name label for this column
54 ///
55 /// \note called by read_falcON_snapshot()
56 void set_splash_particle_label(const int*ityp, const char*name);
57
58 //
59 // 1.2 routines provided here, but to be called from SPLASH
60 //
61
62 ///
63 /// set debugging level
64 ///
65 /// \param[in] debug debugging level
66 ///
67 /// \note currently only debug=0,1,2,3 are distinguished.
68 /// 0 means no output in case of an error,
69 /// 1 means diagnostic output in case of error,
70 /// 2 also prints errors from the HDF5 library
71 /// 3 may print extra information (used in debugging this file)
72 void set_falcON_debugging_level(const int*debug);
73
74 ///
75 /// opens a falcON HDF5 snapshot file
76 ///
77 /// \param[in] filename name of data file
78 /// \param[out] ierr non-zero if file couldn't be opened
79 ///
80 /// \note precondition: none
81 /// \note postcondition: open_falcON_snapshot() can be called
82 /// \note We close any previously opened file (only one snapshot file can be
83 /// open at any time with this implementation), but warn if the
84 /// filename matches with the currently open file, if any.
85 /// \note Use num_falcON_snapshots() for the number of snapshots in the file
86 void open_falcON_file(const char*filename, int*ierr);
87
88 ///
89 /// queries whether a file is open
90 ///
91 int falcON_file_is_open();
92
93 ///
94 /// closes the currently open (if any) falcON HDF5 snapshot file.
95 ///
96 /// \note precondition: none
97 /// \note postcondition: falcON_file_is_open() will return false
98 void close_falcON_file();
99
100 ///
101 /// queries if there is another snapshot present the currently open file
102 ///
103 /// \note precondition: falcON_file_is_open()
104 int have_falcON_snapshot(int*ierr);
105
106 ///
107 /// query the number of snapshots in the currently open file
108 ///
109 /// \note precondition: falcON_file_is_open()
110 /// \note When using have_falcON_snapshot(), it may not be necessary to call
111 /// this function, see main() in PART 3 below for an example.
112 /// \note This function is trivial for falcON HDF5-based snapshot files,
113 /// but non-trivial for NEMO snapshot files, which we may support in
114 /// the future without changing this interface.
115 int num_falcON_snapshots(int*ierr);
116
117 ///
118 /// opens the next falcON snapshot in the currently open file and
119 /// reads its header
120 ///
121 /// \param[out] ntype number of particle types
122 /// \param[out] npart number of particles per type
123 /// \param[out] ncol number of splash columns
124 /// \param[out] dimX number of spatial dimensions
125 /// \param[out] dimV number of velocity dimensions
126 /// \param[out] time simulation time of snapshot
127 /// \param[out] hper if hper[d]!=0, dimension d is periodic with |x|<hper
128 ///
129 /// \note precondition: have_falcON_snapshot()
130 /// \note postcondition: read_falcON_snapshot() can be called
131 /// \note particles types are (currently): gas,sink,std
132 /// \note closes any previously opened snasphots: this implemention only
133 /// allows one open snapshot at any time.
134 /// \note corresponds to 'read_XXX_header()' in systems where each snapshot
135 /// lives in its own file.
136 void open_falcON_snapshot(int*ntype, int npart[max_num_types],
137 int*ncol, int*dimX, int*dimV, double*time,
138 double hper[3], int*ierr);
139
140 ///
141 /// read all data present in the snapshot
142 ///
143 /// \note precondition: a snapshot has been opened via open_falcON_snapshot()
144 /// \note calls all functions declared in interface 1.1
145 /// \note generates splash column indices 0 to ncol-1
146 /// \note see also IMPORTANT NOTE below
147 /// \note tested in PART 3
148 void read_falcON_snapshot(int*ierr);
149
150 //
151 // IMPORTANT NOTE on falcON fields versus splash columns.
152 //
153 // With the exception of field 'krnH', each scalar falcON field generates
154 // exactly one splash column. The column name is identical to the field name,
155 // except for field 'key', when the column name is 'id'.
156 // For gas particles, field 'krnH' becomes column 'h' with values reduced by
157 // a factor 2 to account for the convention in splash. For sink particles,
158 // field 'krnH' becomes column 'snkR', denoting the sink radius.
159 //
160 // Vector fields, create 3 columns with names consisting of the first letter
161 // plus 'x', 'y', 'z', i.e. 'az' for the z-component of the acceleration.
162 // The only exceptions are the positions with column names 'x', 'y', 'z'.
163 //
164 // Tensor fields, create 6 columns with names consisting of the capitalised
165 // first letter followed by 'xx', 'xy', 'xz', 'yy', 'yz', 'zz', i.e. 'Txy'
166 // for a component of the tidal field.
167 //
168
169 } // extern "C"
170 ////////////////////////////////////////////////////////////////////////////////
171 //
172 // PART 2 C++ implemention of interface 1.2
173 //
174 ////////////////////////////////////////////////////////////////////////////////
175 #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 5
176 //
177 // The HDF5 library may have been compiled with another (older) GNU C++ ABI
178 // than the one used in the rest of the falcON.2 code. Here, we must use the
179 // same C++ ABI as used with the HDF5 C++ library. This is controlled by the
180 // following macro, see also
181 //
182 // https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html
183 //
184 # define _GLIBCXX_USE_CXX11_ABI 0
185 #endif
186 #if __cplusplus < 201103L
187 # error requiring C++11
188 #endif
189 #include <iostream>
190 #include <cassert>
191 #include <type_traits>
192 #include <memory>
193 #include <vector>
194 #include <array>
195 #include <map>
196 #include <H5Cpp.h>
197 ///
198 /// 2.1 auxiliary functionality
199 /// (symbols from the anonymous namespace have internal linkage
200 ///
201 namespace {
202 /// simple struct with data for each set of particles
203 struct Particles {
204 std::unique_ptr<H5::Group> group; ///< HDF5 group
205 std::string name; ///< name: 'sink', 'gas', 'std'
206 std::size_t number; ///< number of particles
207 // (I couldn't figure out to avoid this via aggregate initialisation)
208 Particles(H5::Group*g, std::string const&t, std::size_t n)
209 : group(g), name(t), number(n) {}
210 };
211 //
212 using name_count_map = std::map<std::string,hsize_t>;
213 using name_and_count = name_count_map::value_type;
214 // static data
215 int Debug = 0; ///< debug level (0 or not 0)
216 int IndexSnap = 0; ///< index of next snapshot
217 int NumSnap = 0; ///< number of snapshots in file
218 int SplashCol = 0; ///< column for splash data
219 int NumCol = 0; ///< number of splash columns
220 name_count_map Fields; ///< field:dims map of all fields
221 std::vector<Particles> Part; ///< data for particle sets
222 std::vector<double> Buffer; ///< data buffer for input
223 std::unique_ptr<H5::H5File> File; ///< file
224 std::string FileName; ///< name of currently open file
225 const hsize_t k[1] = {3};
226 const H5::ArrayType VectorType ///< HDF5 data type for double[3]
227 {H5::PredType::NATIVE_DOUBLE,1,k};
228 // closes a splash column
229 inline void close_column(const char*name)
230 {
231 if(SplashCol >= NumCol)
232 throw "number of columns exceeds expected " + std::to_string(NumCol);
233 set_splash_block_label(&SplashCol,name);
234 SplashCol++;
235 }
236 // closes a splash column
237 inline void close_column(std::string const&name)
238 {
239 close_column(name.c_str());
240 }
241 // closes currently open snapshot
242 inline void close_snapshot()
243 {
244 SplashCol = 0;
245 NumCol = 0;
246 Part.clear();
247 Fields.clear();
248 }
249 // select field for this particle type?
250 // NOTE Without selecting, we would potentially read meaningless data. This
251 // will not be necessary in future versions of falcON.
252 inline bool select(std::string const&field, std::string const&ptype)
253 {
254 return
255 (field=="spin" || field=="eabs" || field=="maxA")?
256 (ptype=="sink") :
257 (field=="snum" || field=="uin" || field=="entr" || field=="dlKt" ||
258 field=="dlKe" || field=="srho" || field=="alfa" || field=="divv" ||
259 field=="dlht" || field=="vsig" || field=="fact" || field=="csnd" ||
260 field=="pres" || field=="vort" || field=="dtdv" || field=="qmin" ||
261 field=="delE" || field=="coll")?
262 (ptype=="gas") :
263 (field=="krnH" || field=="maxR") ?
264 (ptype=="sink" || ptype=="gas") : true;
265 }
266 // try to read a component of a falcON field into a splash column
267 void read_column(std::string const&field, const hsize_t comp,
268 const hsize_t dims)
269 {
270 bool read = false;
271 // loop particle types
272 for(int type=0; type!=int(Part.size()); ++type) {
273 const auto&part = Part[std::size_t(type)];
274 if(!select(field,part.name))
275 continue;
276 H5::DataSet data;
277 try {
278 data = part.group->openDataSet(field);
279 } catch(...) {
280 continue; // field not present: continue with next particle type
281 }
282 // obtain size of data set
283 auto space = data.getSpace();
284 hsize_t count[2];
285 auto rank = space.getSimpleExtentDims(count);
286 assert(part.number==count[0]);
287 const int ndat = int(part.number);
288 // read component
289 Buffer.resize(part.number);
290 switch(dims) {
291 case 1:
292 // scalar field
293 assert(rank==1);
294 data.read(Buffer.data(),H5::PredType::NATIVE_DOUBLE,space,space);
295 // reduce smoothing length by factor 2
296 if(field=="krnH" && part.name=="gas")
297 for(auto&x:Buffer) x*=0.5;
298 if(Debug>2) std::clog<<"reading "<<ndat<<" '"<<field<<"' for '"
299 <<part.name<<"' (type="<<type<<") into col="
300 <<SplashCol<<std::endl;
301 read_falcON_data_into_splash(&SplashCol,&ndat,Buffer.data(),&type);
302 read = true;
303 // account for special case 'krnH'
304 if(field=="krnH") {
305 if(part.name=="gas") {
306 close_column("h");
307 read = false;
308 } else if(part.name=="sink") {
309 close_column("snkR");
310 read = false;
311 }
312 }
313 break;
314 default: {
315 // component of vector/tensor field
316 assert(rank==2);
317 assert(dims==count[1]);
318 count[1] = 1;
319 const hsize_t start[2]={0,comp};
320 space.selectHyperslab(H5S_SELECT_SET,count,start);
321 data.read(Buffer.data(),H5::PredType::NATIVE_DOUBLE,
322 H5::DataSpace{1,count},space);
323 if(Debug>2) std::clog<<"reading "<<ndat<<" '"<<field<<'['<<comp
324 <<"]' for '"<<part.name<<"' (type="<<type
325 <<") into col="<<SplashCol<<std::endl;
326 read_falcON_data_into_splash(&SplashCol,&ndat,Buffer.data(),&type);
327 read = true;
328 }
329 }
330 }
331 // if any data have been read: set column label
332 if(read) {
333 std::string name;
334 if(field=="pos")
335 name = comp==0? "x" : comp==1? "y" : "z";
336 else if(dims==3) {
337 name = field[0];
338 name+= comp==0? "x" : comp==1? "y" : "z";
339 } else if(dims==6) {
340 name = static_cast<char>(std::toupper(field[0]));
341 name+= comp==0? "xx": comp==1? "xy": comp==2? "xz":
342 comp==3? "yy": comp==4? "yz": "zz";
343 } else
344 name=field;
345 close_column(name);
346 }
347 }
348 // read a field
349 inline void read_field(name_and_count const&field)
350 {
351 for(hsize_t comp=0; comp!=field.second; ++comp)
352 read_column(field.first, comp, field.second);
353 }
354 // try to read a falcON field
355 inline void read_field(std::string const&field)
356 {
357 const auto field_iter = Fields.find(field);
358 if(field_iter==Fields.end())
359 std::clog<<"WARNING: falcON field '"<<field<<"' not present in file"
360 <<std::endl;
361 else
362 read_field(*field_iter);
363 }
364 }
365 //
366 // 2.2 implement routines declared in 1.2
367 //
368 void set_falcON_debugging_level(const int*d)
369 {
370 Debug = *d;
371 }
372 //
373 void open_falcON_file(const char*file, int*ierr)
374 {
375 *ierr = 1;
376 IndexSnap = 0;
377 close_snapshot();
378 // suppress error messages from the HDF5 C library
379 if(Debug<2) H5::Exception::dontPrint();
380 if(file==nullptr) {
381 if(Debug) std::clog<<"open_falcON_file(): empty filename"<<std::endl;
382 return close_falcON_file();
383 }
384 if(FileName==file)
385 std::clog<<"open_falcON_file('"<<file<<"'): WARNING: "
386 <<"a falcON hdf5 snapshot file of the same name is already open, "
387 <<"re-opening it will effectively rewind to the first snapshot."
388 <<std::endl;
389 // open HDF5 file
390 FileName = file;
391 try {
392 File.reset(new H5::H5File(file,H5F_ACC_RDONLY));
393 } catch(H5::Exception const&exc) {
394 if(Debug) std::clog<<"open_falcON_file(): "
395 <<"cannot open HDF5 file '"<<file<<"' (HDF5 says \""
396 <<exc.getDetailMsg()<<"\")"<<std::endl;
397 return close_falcON_file();
398 }
399 // open attribute 'falcON' to establish that this is a falcON snapshot file
400 try {
401 auto attr = File->openAttribute("falcON");
402 } catch(...) {
403 if(Debug) std::clog<<"open_falcON_file(): file '"<<file
404 <<"' is an HDF5 file, but not a falcON snapshot file"
405 <<std::endl;
406 return close_falcON_file();
407 }
408 // read number of snapshots
409 try {
410 auto attr = File->openAttribute("num_snapshots");
411 attr.read(H5::PredType::NATIVE_UINT32,&NumSnap);
412 } catch(H5::Exception const&exc) {
413 // should never happen
414 if(Debug) std::clog<<"open_falcON_file('"<<file<<"'): failed to "
415 <<"read num_snapshots (HDF5 says \""
416 <<exc.getDetailMsg()<<"\")"<<std::endl;
417 return close_falcON_file();
418 }
419 *ierr = 0;
420 }
421 //
422 int falcON_file_is_open()
423 {
424 return bool(File);
425 }
426 //
427 void close_falcON_file()
428 {
429 close_snapshot();
430 IndexSnap = NumSnap = 0;
431 File.reset();
432 FileName.clear();
433 }
434 //
435 int have_falcON_snapshot(int*ierr)
436 {
437 *ierr = !File;
438 if(*ierr && Debug)
439 std::clog<<"have_falcON_snapshot(): no snapshot file open"<<std::endl;
440 return IndexSnap < NumSnap;
441 }
442 //
443 int num_falcON_snapshots(int*ierr)
444 {
445 *ierr = !File;
446 if(*ierr && Debug)
447 std::clog<<"num_falcON_snapshots(): no snapshot file open"<<std::endl;
448 return NumSnap;
449 }
450 //
451 void open_falcON_snapshot(int*ntyp, int npart[max_num_types],
452 int*ncol, int*dimX, int*dimV,
453 double*time, double hper[3], int*ierr)
454 {
455 *ierr = 1;
456 *ntyp = 0;
457 *ncol = 0;
458 *dimX = 3;
459 *dimV = 3;
460 close_snapshot();
461 // check preconditions
462 if(!File) {
463 if(Debug) std::clog<<"open_falcON_snapshot(): "
464 <<"no snapshot file open"<<std::endl;
465 return;
466 }
467 if(IndexSnap >= NumSnap) {
468 if(Debug) std::clog<<"open_falcON_snapshot(): no more than "
469 <<NumSnap<<" snapshots in file"<<std::endl;
470 return;
471 }
472 try {
473 // open snapshot group
474 auto name = "snapshot" + std::to_string(IndexSnap++);
475 auto snap = File->openGroup(name);
476 // read time and hper
477 auto attr = snap.openAttribute("time");
478 attr.read(H5::PredType::NATIVE_DOUBLE,time);
479 attr = snap.openAttribute("hper");
480 attr.read(VectorType,hper);
481 // read npart[] and open particle sets
482 std::array<std::string,3> types = {{"sink","gas","std"}};
483 for(const auto&type:types) {
484 name = "N";
485 name+= type;
486 unsigned number;
487 try {
488 attr = snap.openAttribute(name);
489 attr.read(H5::PredType::NATIVE_UINT32,&number);
490 } catch(...) {
491 // should never go here
492 number = 0;
493 }
494 if(number) {
495 // open HDF5 group for particles
496 const auto part = snap.openGroup(type);
497 Part.emplace_back(new H5::Group(part),type,number);
498 npart[(*ntyp)++] = int(number);
499 // collect (field:dims) pairs and count columns
500 for(hsize_t fld=0; fld!=part.getNumObjs(); ++fld) {
501 const auto field = part.getObjnameByIdx(fld);
502 const auto have_field = Fields.count(field);
503 if(have_field) {
504 // field 'krnH' generates an extra column for each particle type
505 if(field=="krnH") (*ncol)++;
506 } else {
507 hsize_t count[2];
508 const auto dims = 1==part.openDataSet(field).getSpace().
509 getSimpleExtentDims(count)? 1:count[1];
510 Fields[field] = dims;
511 (*ncol)+= int(dims);
512 }
513 if(Debug>2) std::clog<<"counting columns: type="<<type
514 <<" fld="<<fld<<"='"<<field<<"' cols->"
515 <<(*ncol)<<std::endl;
516 }
517 }
518 }
519 } catch(H5::Exception const&exc) {
520 // should never go here
521 if(Debug) std::clog<<"open_falcON_snapshot(): HDF5 error: \""
522 <<exc.getDetailMsg()<<'\"'<<std::endl;
523 *ntyp = 0;
524 *ncol = 0;
525 return close_snapshot();
526 }
527 NumCol = *ncol;
528 *ierr = 0;
529 }
530 //
531 void read_falcON_snapshot(int*ierr)
532 {
533 *ierr = 0;
534 std::vector<std::string> PreferredOrder =
535 {"pos", "key", "vel", "acc", "mass", "pot", "pex", "rung",
536 "krnH", "srho", "uin", "entr", "divv", "dlKt", "dlKe", "alfa"};
537 try {
538 auto FieldsToRead=Fields;
539 // read some fields in preferred order
540 for(const auto&field:PreferredOrder)
541 if(FieldsToRead.erase(field))
542 read_field(field);
543 // read remaining fields
544 for(const auto&field:FieldsToRead)
545 read_field(field);
546 } catch(H5::Exception const&exc) {
547 // catch any HDF5 error
548 if(Debug) std::clog<<"read_falcON_snapshot(): HDF5 error: \""
549 <<exc.getDetailMsg()<<'\"'<<std::endl;
550 return;
551 } catch(std::string const&exc) {
552 // catch error from column overflow
553 if(Debug)
554 std::clog<<exc<<" -- this either means that read_falcON_snapshot() "
555 <<"has been called more than once, or that "
556 <<"there is an error in file read_falcON_utils.cc"<<std::endl;
557 return;
558 }
559 // set particle type names
560 for(int type=0; type!=int(Part.size()); ++type)
561 set_splash_particle_label(&type, Part[std::size_t(type)].name.c_str());
562 *ierr = 0;
563 }
564 #if(0) // untested
565 //
566 void query_falcON_fields(int*nfld, char*flds, int*ierr)
567 {
568 *ierr = 1;
569 *nfld = 0;
570 // check precondition
571 if(Part.empty()) {
572 if(Debug) std::clog<<"query_falcON_fields(): no snapshot open"<<std::endl;
573 return;
574 }
575 // set particle type names
576 for(int type=0; type!=int(Part.size()); ++type)
577 set_splash_particle_label(&type, Part[std::size_t(type)].name.c_str());
578 // copy fields
579 for(const auto&field:Fields) {
580 assert(field.first.size() < 5);
581 assert(*nfld < max_num_cols);
582 strcpy(flds,field.first.c_str());
583 flds+= 5;
584 (*nfld)++;
585 }
586 *ierr = 0;
587 }
588 //
589 void read_falcON_field(const char*field, int*ierr)
590 {
591 *ierr = 1;
592 if(field==nullptr) {
593 if(Debug)
594 std::clog<<"read_falcON_field(): empty field"<<std::endl;
595 return;
596 }
597 try {
598 read_field(field);
599 } catch(H5::Exception const&exc) {
600 // catch any HDF5 error
601 if(Debug) std::clog<<"read_falcON_field(): HDF5 error: \""
602 <<exc.getDetailMsg()<<'\"'<<std::endl;
603 return;
604 } catch(std::string const&exc) {
605 // catch error from column overflow
606 if(Debug)
607 std::clog<<exc<<std::endl;
608 return;
609 }
610 *ierr = 0;
611 }
612 #endif
613 #ifdef TestMain
614 ////////////////////////////////////////////////////////////////////////////////
615 //
616 // PART 3 test above implementation
617 //
618 ////////////////////////////////////////////////////////////////////////////////
619 #include <iomanip>
620 #include <cstdlib>
621 #include <algorithm>
622 //
623 // 3.1 implement interface 1.1 using C++
624 //
625 namespace {
626 struct data_per_particle_type {
627 std::string name;
628 std::size_t number;
629 std::vector< std::vector<double> > columns;
630 };
631 std::vector<data_per_particle_type> particle_data;
632 std::vector<std::string> column_labels;
633 }
634 //
635 void set_splash_block_label(const int*icol, const char*name)
636 {
637 column_labels.at(std::size_t(*icol)) = name;
638 }
639 //
640 void set_splash_particle_label(const int*type, const char*name)
641 {
642 particle_data.at(std::size_t(*type)).name = name;
643 }
644 //
645 void read_falcON_data_into_splash(const int*icol, const int*ndat,
646 const double*from, const int*type)
647 {
648 auto&part = particle_data.at(std::size_t(*type));
649 assert(*ndat > 0);
650 assert(part.number == std::size_t(*ndat));
651 part.columns.at(std::size_t(*icol)).resize(std::size_t(*ndat));
652 std::copy(from,from+*ndat,part.columns[std::size_t(*icol)].begin());
653 }
654 //
655 // 3.2 an executable that dumps a falcON file
656 //
657 int main(int argc, const char**argv)
658 {
659 // obtain program parameters
660 if(argc<2) {
661 std::clog<<"usage: '"<<argv[0]
662 <<" input_file [lines_per_type [debug_level]]'"<<std::endl;
663 return 0;
664 }
665 const auto input = argv[1];
666 const auto lines = argc<3? 10 : std::atoi(argv[2]);
667 const auto debug = argc<4? 0 : std::atoi(argv[3]);
668 if(lines<=0) {
669 std::clog<<"ERROR: lines="<<lines<<"<=0"<<std::endl;
670 return 1;
671 }
672 if(debug<0) {
673 std::clog<<"ERROR: debug="<<debug<<"<0"<<std::endl;
674 return 1;
675 }
676 // set debug level
677 set_falcON_debugging_level(&debug);
678 // open falcON snapshot file
679 int ierr;
680 open_falcON_file(input,&ierr);
681 if(ierr) {
682 std::clog<<"ERROR: open_falcON_file() failed"<<std::endl;
683 return 1;
684 }
685 if(!have_falcON_snapshot(&ierr)) {
686 std::clog<<"WARNING: have_falcON_snapshot()==false after "
687 <<"open_falcON_file()"<<std::endl;
688 return 1;
689 }
690 // loop snapshots
691 while(have_falcON_snapshot(&ierr)) {
692 // open snapshot and read header
693 int dimX, dimV, ntype, ncol, npart[max_num_types];
694 double time, hper[3];
695 open_falcON_snapshot(&ntype,npart,&ncol,&dimX,&dimV,&time,hper,&ierr);
696 if(ierr) {
697 std::clog<<"ERROR: open_falcON_snapshot() failed"<<std::endl;
698 return 1;
699 }
700 if(ntype<=0) {
701 std::clog<<"WARNING: open_falcON_snapshot() returned ntype<=0"
702 <<std::endl;
703 return 1;
704 }
705 if(ncol<=0) {
706 std::clog<<"WARNING: open_falcON_snapshot() returned ncol<=0"
707 <<std::endl;
708 return 1;
709 }
710 if(debug)
711 std::clog<<"DEBUG: open_falcON_snapshot(): ntype="<<ntype
712 <<" ncol="<<ncol<<std::endl;
713 // prepare particle data for reading
714 particle_data.resize(std::size_t(ntype));
715 column_labels.resize(std::size_t(ncol));
716 for(int type=0; type!=ntype; ++type) {
717 auto&part = particle_data.at(std::size_t(type));
718 assert(npart[type]>0);
719 part.number = std::size_t(npart[type]);
720 part.columns.resize(std::size_t(ncol));
721 }
722 // read particle data
723 if(debug)
724 std::clog<<"DEBUG: now calling read_falcON_snapshot()"
725 <<std::endl;
726 read_falcON_snapshot(&ierr);
727 if(ierr) {
728 std::clog<<"ERROR: read_falcON_snapshot() failed"<<std::endl;
729 return 1;
730 }
731 // print header for columns
732 for(int c=0; c!=ncol; ++c)
733 std::cout<<"=============";
734 std::cout<<'\n';
735 std::cout<<" time="<<time
736 <<" hper=["<<hper[0]<<' '<<hper[1]<<' '<<hper[2]<<']';
737 for(int type=0; type!=ntype; ++type)
738 std::cout<<" n_"<<Part[std::size_t(type)].name<<'='<<npart[type];
739 std::cout<<'\n';
740 for(int c=0; c!=ncol; ++c)
741 std::cout<<"-------------";
742 std::cout<<'\n';
743 for(int c=0; c!=ncol; ++c)
744 std::cout<<" col "<<std::setw(2)<<c;
745 std::cout<<'\n';
746 for(int c=0; c!=ncol; ++c)
747 std::cout<<' '<<std::setw(12)<<column_labels.at(std::size_t(c));
748 std::cout<<'\n';
749 // loop particle types and print columns
750 for(int type=0; type!=ntype; ++type) {
751 const auto&part = particle_data.at(std::size_t(type));
752 const auto n = lines<npart[type]? lines : npart[type];
753 for(int c=0; c!=ncol; ++c)
754 std::cout<<"-------------";
755 std::cout<<'\n';
756 std::cout<<" '"<<part.name<<"' particles:\n";
757 for(int c=0; c!=ncol; ++c)
758 std::cout<<"-------------";
759 std::cout<<'\n';
760 for(int i=0; i<n; ++i) {
761 for(int c=0; c!=ncol; ++c) {
762 const auto&column = part.columns.at(std::size_t(c));
763 if(column.empty()) std::cout<<" -";
764 else std::cout<<' '<<std::setw(12)<<column.at(std::size_t(i));
765 }
766 std::cout<<'\n';
767 }
768 if(n<npart[type]) {
769 for(int c=0; c!=ncol; ++c)
770 std::cout<<" ...";
771 std::cout<<'\n';
772 }
773 std::cout<<std::flush;
774 }
775 }
776 // close snapshot file
777 close_falcON_file();
778 }
779 #endif