SPH
Output.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "io/Path.h"
12 #include "physics/Constants.h"
13 #include "quantities/Storage.h"
14 
16 
21 class OutputFile {
22 private:
23  mutable Size dumpNum = 0;
24  Path pathMask;
25 
26 public:
27  OutputFile() = default;
28 
40  OutputFile(const Path& pathMask, const Size firstDumpIdx = 0);
41 
46  Path getNextPath(const Statistics& stats) const;
47 
51  bool hasWildcard() const;
52 
54  Path getMask() const;
55 
61  static Optional<Size> getDumpIdx(const Path& path);
62 
68  static Optional<OutputFile> getMaskFromPath(const Path& path, const Size firstDumpIdx = 0);
69 };
70 
76 class IOutput : public Polymorphic {
77 protected:
79 
80 public:
82  explicit IOutput(const OutputFile& fileMask);
83 
88  virtual Expected<Path> dump(const Storage& storage, const Statistics& stats) = 0;
89 };
90 
94 class IInput : public Polymorphic {
95 public:
100  virtual Outcome load(const Path& path, Storage& storage, Statistics& stats) = 0;
101 };
102 
103 
108 enum class OutputQuantityFlag {
110  POSITION = 1 << 0,
111 
113  VELOCITY = 1 << 1,
114 
116  SMOOTHING_LENGTH = 1 << 2,
117 
119  MASS = 1 << 3,
120 
122  PRESSURE = 1 << 4,
123 
125  DENSITY = 1 << 5,
126 
128  ENERGY = 1 << 6,
129 
131  DEVIATORIC_STRESS = 1 << 7,
132 
134  DAMAGE = 1 << 8,
135 
137  ANGULAR_FREQUENCY = 1 << 9,
138 
141 
143  MATERIAL_ID = 1 << 11,
144 
146  INDEX = 1 << 12,
147 };
148 
149 class ITextColumn;
150 
152 class TextOutput : public IOutput {
153 public:
154  enum class Options {
156  DUMP_ALL = 1 << 0,
157 
159  SCIENTIFIC = 1 << 1,
160  };
161 
162 private:
163  std::string runName;
164 
166  Flags<Options> options;
167 
170 
171 public:
180  TextOutput(const OutputFile& fileMask,
181  const std::string& runName,
182  Flags<OutputQuantityFlag> quantities,
183  Flags<Options> options = EMPTY_FLAGS);
184 
186 
194 
195  virtual Expected<Path> dump(const Storage& storage, const Statistics& stats) override;
196 };
197 
202 class TextInput : public IInput {
203 private:
205 
206 public:
207  explicit TextInput(Flags<OutputQuantityFlag> quantities);
208 
210 
211  virtual Outcome load(const Path& path, Storage& storage, Statistics& stats) override;
212 };
213 
215 class GnuplotOutput : public TextOutput {
216 private:
217  std::string scriptPath;
218 
219 public:
220  GnuplotOutput(const OutputFile& fileMask,
221  const std::string& runName,
222  const std::string& scriptPath,
223  const Flags<OutputQuantityFlag> quantities,
224  const Flags<Options> flags)
225  : TextOutput(fileMask, runName, quantities, flags)
226  , scriptPath(scriptPath) {}
227 
228  virtual Expected<Path> dump(const Storage& storage, const Statistics& stats) override;
229 };
230 
231 
237 enum class RunTypeEnum {
239  SPH,
240 
243 
245  RUBBLE_PILE,
246 
248  NBODY,
249 };
250 
251 static RegisterEnum<RunTypeEnum> sRunType({
252  { RunTypeEnum::SPH, "sph", "Main SPH simulation" },
253  { RunTypeEnum::STABILIZATION, "stabilization", "Convergence SPH phase" },
254  { RunTypeEnum::RUBBLE_PILE, "rubble-pile", "Simulation creating rubble-pile target" },
255  { RunTypeEnum::NBODY, "nbody", "Main N-body simulation" },
256 });
257 
258 enum class BinaryIoVersion : int64_t {
259  FIRST = 0,
260  V2018_04_07 = 20180407,
261  V2018_10_24 = 20181024,
262  V2021_03_20 = 20210320,
264 };
265 
335 class BinaryOutput : public IOutput {
336  friend class BinaryInput;
337 
338 private:
339  static constexpr Size PADDING_SIZE = 164;
340 
341  RunTypeEnum runTypeId;
342 
343 public:
344  explicit BinaryOutput(const OutputFile& fileMask, const RunTypeEnum runTypeId = RunTypeEnum::SPH);
345 
346  virtual Expected<Path> dump(const Storage& storage, const Statistics& stats) override;
347 };
348 
352 class BinaryInput : public IInput {
353 public:
354  virtual Outcome load(const Path& path, Storage& storage, Statistics& stats) override;
355 
356  struct Info {
359 
362 
365 
367  struct QuantityInfo {
368 
371 
374 
377  };
378 
380 
383 
386 
389 
394 
399 
402  };
403 
405  Expected<Info> getInfo(const Path& path) const;
406 };
407 
408 enum class CompressedIoVersion : int {
409  FIRST = 0,
410 };
411 
412 enum class CompressionEnum {
413  NONE,
414  RLE,
415 };
416 
417 class CompressedOutput : public IOutput {
418 private:
419  CompressionEnum compression;
420  RunTypeEnum runTypeId;
421 
422 public:
423  explicit CompressedOutput(const OutputFile& fileMask,
424  const CompressionEnum compression,
425  const RunTypeEnum runTypeId = RunTypeEnum::SPH);
426 
427  virtual Expected<Path> dump(const Storage& storage, const Statistics& stats) override;
428 };
429 
430 class CompressedInput : public IInput {
431 public:
432  virtual Outcome load(const Path& path, Storage& storage, Statistics& stats) override;
433 
434  struct Info {
437 
440 
443 
446  };
447 
448  Expected<Info> getInfo(const Path& path) const;
449 };
450 
454 class VtkOutput : public IOutput {
455 private:
457 
458 public:
459  VtkOutput(const OutputFile& fileMask, const Flags<OutputQuantityFlag> flags);
460 
461  virtual Expected<Path> dump(const Storage& storage, const Statistics& stats) override;
462 };
463 
468 class Hdf5Input : public IInput {
469 public:
470  virtual Outcome load(const Path& path, Storage& storage, Statistics& stats) override;
471 };
472 
477 class MpcorpInput : public IInput {
478 private:
479  Float rho;
480  Float albedo;
481 
482 public:
483  explicit MpcorpInput(const Float rho = 2700._f, const Float albedo = 0.2_f)
484  : rho(rho)
485  , albedo(albedo) {}
486 
487  virtual Outcome load(const Path& path, Storage& storage, Statistics& stats) override;
488 };
489 
491 
493  enum class Radius {
496 
499  };
500 
502 
504  struct Conversion {
505 
507 
509 
510  Float velocity = Constants::au * 2._f * PI / (365.25_f * 86400._f);
511 
513 
516  Vector omega = Vector(0._f);
517 
521 
524  Array<Size> colors{ 3, 13 };
525 };
526 
527 
533 class PkdgravOutput : public IOutput {
534 private:
536  PkdgravParams params;
537 
538 public:
539  PkdgravOutput(const OutputFile& fileMask, PkdgravParams&& params);
540 
541  virtual Expected<Path> dump(const Storage& storage, const Statistics& stats) override;
542 
543 private:
544  INLINE Float getRadius(const Float h, const Float m, const Float rho) const {
545  switch (params.radius) {
547  // 4/3 pi r^3 rho = m
548  return cbrt(3._f * m / (4._f * PI * rho));
550  return h / 3._f;
551  default:
553  }
554  }
555 };
556 
561 class PkdgravInput : public IInput {
562 public:
563  virtual Outcome load(const Path& path, Storage& storage, Statistics& stats) override;
564 };
565 
569 class TabInput : public IInput {
570 private:
571  // AutoPtr to avoid instantiation, ITextColumn is forward-declared only
572  AutoPtr<TextInput> input;
573 
574 public:
575  TabInput();
576 
578 
579  virtual Outcome load(const Path& path, Storage& storage, Statistics& stats) override;
580 };
581 
582 
583 class NullOutput : public IOutput {
584 public:
586  : IOutput(Path("file")) {} // we don't dump anything, but IOutput asserts non-empty path
587 
588  virtual Expected<Path> dump(const Storage& UNUSED(storage), const Statistics& UNUSED(stats)) override {
589  return Path();
590  }
591 };
592 
593 
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
Definition: Assert.h:100
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
@ NONE
No collision took place.
Definitions of physical constaints (in SI units).
Helper object for converting enums to string, listing all available values of enum,...
Wrapper of type containing either a value or an error message.
const EmptyFlags EMPTY_FLAGS
Definition: Flags.h:16
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
INLINE Float cbrt(const Float f)
Returns a cubed root of a value.
Definition: MathUtils.h:84
constexpr Float PI
Mathematical constants.
Definition: MathUtils.h:361
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
Return value of function that may fail, containing either SUCCEES (true) or error message.
OutputQuantityFlag
List of quantities we can write to text output.
Definition: Output.h:108
@ STRAIN_RATE_CORRECTION_TENSOR
Symmetric tensor correcting kernel gradient for linear consistency.
@ DEVIATORIC_STRESS
Deviatoric stress tensor, always a traceless tensor stored in components xx, yy, xy,...
@ PRESSURE
Pressure, reduced by yielding and fracture model (multiplied by 1-damage); always a scalar quantity.
@ VELOCITY
Current velocities of particles, always a vector quantity.
@ DAMAGE
Damage, reducing the pressure and deviatoric stress.
@ POSITION
Positions of particles, always a vector quantity.
@ ENERGY
Specific internal energy, always a scalar quantity.
@ SMOOTHING_LENGTH
Smoothing lenghts of particles.
@ ANGULAR_FREQUENCY
Angular frequency of particles, used in N-body simulations.
@ DENSITY
Density, always a scalar quantity.
@ INDEX
Index of particle.
@ MASS
Particle masses, always a scalar quantity.
@ MATERIAL_ID
ID of material, indexed from 0 to (#bodies - 1).
RunTypeEnum
Type of simulation, stored as metadata in the binary file.
Definition: Output.h:237
@ SPH
Main SPH simulation.
@ RUBBLE_PILE
Pre-stabilization phase, using N-body solver to get a realistic rubble-pile body.
@ NBODY
Main N-body simulation, using initial conditions either from SPH (handoff) or manually specified.
@ STABILIZATION
Confergence phase using SPH solver, used to obtain stable initial conditions.
BinaryIoVersion
Definition: Output.h:258
@ V2021_03_20
added wallclock time and build date
@ V2018_04_07
serializing (incorrectly!!) enum type
@ V2018_10_24
reverted enum (storing zero instead of hash), storing type of simulation
CompressedIoVersion
Definition: Output.h:408
CompressionEnum
Definition: Output.h:412
Object representing a path on a filesystem, similar to std::filesystem::path in c++17.
ValueEnum
QuantityId
Unique IDs of basic quantities of SPH particles.
Definition: QuantityIds.h:19
OrderEnum
Definition: Quantity.h:15
Container for storing particle quantities and materials.
BasicVector< Float > Vector
Definition: Vector.h:539
Generic dynamically allocated resizable storage.
Definition: Array.h:43
Wrapper of pointer that deletes the resource from destructor.
Definition: AutoPtr.h:15
Input for the binary file, generated by BinaryOutput.
Definition: Output.h:352
virtual Outcome load(const Path &path, Storage &storage, Statistics &stats) override
Loads data from the file into the storage.
Definition: Output.cpp:718
Expected< Info > getInfo(const Path &path) const
Opens the file and reads header info without reading the rest of the file.
Definition: Output.cpp:814
Output saving data to binary data without loss of precision.
Definition: Output.h:335
virtual Expected< Path > dump(const Storage &storage, const Statistics &stats) override
Saves data from particle storage into the file.
Definition: Output.cpp:512
BinaryOutput(const OutputFile &fileMask, const RunTypeEnum runTypeId=RunTypeEnum::SPH)
Definition: Output.cpp:508
virtual Outcome load(const Path &path, Storage &storage, Statistics &stats) override
Loads data from the file into the storage.
Definition: Output.cpp:977
Expected< Info > getInfo(const Path &path) const
Definition: Output.cpp:1030
CompressedOutput(const OutputFile &fileMask, const CompressionEnum compression, const RunTypeEnum runTypeId=RunTypeEnum::SPH)
Definition: Output.cpp:846
virtual Expected< Path > dump(const Storage &storage, const Statistics &stats) override
Saves data from particle storage into the file.
Definition: Output.cpp:933
Wrapper of type that either contains a value of given type, or an error message.
Definition: Expected.h:25
Extension of text output that runs given gnuplot script on dumped text data.
Definition: Output.h:215
virtual Expected< Path > dump(const Storage &storage, const Statistics &stats) override
Saves data from particle storage into the file.
Definition: Output.cpp:339
GnuplotOutput(const OutputFile &fileMask, const std::string &runName, const std::string &scriptPath, const Flags< OutputQuantityFlag > quantities, const Flags< Options > flags)
Definition: Output.h:220
Reader of HDF5 files generated by the code miluphcuda.
Definition: Output.h:468
virtual Outcome load(const Path &path, Storage &storage, Statistics &stats) override
Loads data from the file into the storage.
Definition: Output.cpp:1253
Interface for loading quantities of SPH particles from a file.
Definition: Output.h:94
virtual Outcome load(const Path &path, Storage &storage, Statistics &stats)=0
Loads data from the file into the storage.
Interface for saving quantities of SPH particles to a file.
Definition: Output.h:76
OutputFile paths
Definition: Output.h:78
IOutput(const OutputFile &fileMask)
Constructs output given the file name of the output.
Definition: Output.cpp:98
virtual Expected< Path > dump(const Storage &storage, const Statistics &stats)=0
Saves data from particle storage into the file.
Base class for conversion of quantities into the output data.
Definition: Column.h:30
Reads data from Minor Planet Center Orbit Database exports.
Definition: Output.h:477
virtual Outcome load(const Path &path, Storage &storage, Statistics &stats) override
Loads data from the file into the storage.
Definition: Output.cpp:1333
MpcorpInput(const Float rho=2700._f, const Float albedo=0.2_f)
Definition: Output.h:483
NullOutput()
Definition: Output.h:585
virtual Expected< Path > dump(const Storage &UNUSED(storage), const Statistics &UNUSED(stats)) override
Definition: Output.h:588
Helper file generating file names for output files.
Definition: Output.h:21
static Optional< Size > getDumpIdx(const Path &path)
Extracts the dump index from given path generated by OutputFile.
Definition: Output.cpp:49
Path getNextPath(const Statistics &stats) const
Returns path to the next output file.
Definition: Output.cpp:28
Path getMask() const
Returns the file mask as given in constructor.
Definition: Output.cpp:94
static Optional< OutputFile > getMaskFromPath(const Path &path, const Size firstDumpIdx=0)
Attemps to get the OutputFile from one of the path generated from it.
Definition: Output.cpp:72
bool hasWildcard() const
Returns true if the file mask contains (at least one) wildcard.
Definition: Output.cpp:89
OutputFile()=default
Object representing a path on a filesystem.
Definition: Path.h:17
Input for pkdgrav output files (ss.xxxxx.bt).
Definition: Output.h:561
virtual Outcome load(const Path &path, Storage &storage, Statistics &stats) override
Loads data from the file into the storage.
Definition: Output.cpp:1391
Dumps data into a file that can be used as an input for pkdgrav code by Richardson et al.
Definition: Output.h:533
virtual Expected< Path > dump(const Storage &storage, const Statistics &stats) override
Saves data from particle storage into the file.
Definition: Output.cpp:1357
PkdgravOutput(const OutputFile &fileMask, PkdgravParams &&params)
Definition: Output.cpp:1351
Object holding various statistics about current run.
Definition: Statistics.h:22
Container storing all quantities used within the simulations.
Definition: Storage.h:230
Simple text input file, having particle masses, positions and velocities on separate lines.
Definition: Output.h:569
TabInput()
Definition: Output.cpp:1491
virtual Outcome load(const Path &path, Storage &storage, Statistics &stats) override
Loads data from the file into the storage.
Definition: Output.cpp:1498
Input for the text file, generated by TextOutput or conforming to the same format.
Definition: Output.h:202
virtual Outcome load(const Path &path, Storage &storage, Statistics &stats) override
Loads data from the file into the storage.
Definition: Output.cpp:260
TextInput & addColumn(AutoPtr< ITextColumn > &&column)
Definition: Output.cpp:330
TextInput(Flags< OutputQuantityFlag > quantities)
Definition: Output.cpp:256
Output saving data to text (human readable) file.
Definition: Output.h:152
TextOutput(const OutputFile &fileMask, const std::string &runName, Flags< OutputQuantityFlag > quantities, Flags< Options > options=EMPTY_FLAGS)
Creates a new text file output.
Definition: Output.cpp:180
virtual Expected< Path > dump(const Storage &storage, const Statistics &stats) override
Saves data from particle storage into the file.
Definition: Output.cpp:192
@ DUMP_ALL
Dumps all quantity values from the storage; this overrides the list of selected particles.
@ SCIENTIFIC
Writes all numbers in scientific format.
TextOutput & addColumn(AutoPtr< ITextColumn > &&column)
Adds a new column to be saved into the file.
Definition: Output.cpp:251
XML-based output format used by Visualization ToolKit (VTK)
Definition: Output.h:454
VtkOutput(const OutputFile &fileMask, const Flags< OutputQuantityFlag > flags)
Definition: Output.cpp:1095
virtual Expected< Path > dump(const Storage &storage, const Statistics &stats) override
Saves data from particle storage into the file.
Definition: Output.cpp:1103
constexpr Float au
Astronomical unit (exactly)
Definition: Constants.h:38
constexpr Float M_sun
Definition: Constants.h:51
Information about stored quantities.
Definition: Output.h:367
OrderEnum order
Order (=number or derivatives)
Definition: Output.h:373
QuantityId id
ID of the quantity.
Definition: Output.h:370
ValueEnum value
Value type of the quantity.
Definition: Output.h:376
Optional< std::string > buildDate
Definition: Output.h:398
Size wallclockTime
Wallclock time of the snapshot (in seconds)
Definition: Output.h:385
Size particleCnt
Number of particles in the file.
Definition: Output.h:361
Float timeStep
Current timestep of the run.
Definition: Output.h:388
BinaryIoVersion version
Format version of the file.
Definition: Output.h:401
Optional< RunTypeEnum > runType
Definition: Output.h:393
Size materialCnt
Number of materials in the file.
Definition: Output.h:364
Float runTime
Run time of the snapshot.
Definition: Output.h:382
Size quantityCnt
Number of quantities in the file.
Definition: Output.h:358
Array< QuantityInfo > quantityInfo
Definition: Output.h:379
Size particleCnt
Number of particles in the file.
Definition: Output.h:436
CompressedIoVersion version
Format version of the file.
Definition: Output.h:445
RunTypeEnum runType
Type of the simulation.
Definition: Output.h:442
Float runTime
Run time of the snapshot.
Definition: Output.h:439
Conversion factors for pkdgrav.
Definition: Output.h:504
Radius radius
Definition: Output.h:501
struct PkdgravParams::Conversion conversion
Float vaporThreshold
Definition: Output.h:520
Array< Size > colors
Definition: Output.h:524
Vector omega
Definition: Output.h:516
Radius
Conversion formula from SPH particles to hard spheres in pkdgrav.
Definition: Output.h:493
@ FROM_DENSITY
Compute sphere radius so that its mass is equivalent to the mass of SPH particle.
@ FROM_SMOOTHING_LENGTH
Compute sphere radius using R = h/3 formula.
Base class for all polymorphic objects.
Definition: Object.h:88
Helper class for adding individual enums to the enum map.
Definition: EnumMap.h:175