SPH
HelloAsteroid.cpp
Go to the documentation of this file.
1 #include "Sph.h"
2 #include <iostream>
3 
4 using namespace Sph;
5 
6 // Simulations are performed by IRun object. User must derive the class and implement functions setUp, which
7 // sets up the simulation parameters and initial conditions of the simulation, and tearDown, which is called
8 // after the simulation ends.
9 class HelloAsteroid : public IRun {
10 public:
11  virtual void setUp(SharedPtr<Storage> storage) override {
12 
13  // The easiest way to set up the initial conditions is via InitialConditions object.
14  // Here we pass the default settings of the simulation, stored in variable 'settings'. For a list of
15  // settings, see RunSettingsId enum.
16  InitialConditions ic(settings);
17 
18  // Domain specifying the asteroid - sphere centered at the origin with radius 1000m.
19  SphericalDomain domain(Vector(0._f), 1.e3_f);
20 
21  // Create an object holding all material parameters, initialized to defaults
22  BodySettings body;
23 
24  // Set up the number of particles in the asteroid. For all material (or body-specific) parameters, see
25  // BodySettingsId enum.
27 
28  // Add the body to the main particle storage
29  ic.addMonolithicBody(*storage, domain, body);
30 
31  // Name the simulation
32  settings.set(RunSettingsId::RUN_NAME, std::string("Hello asteroid"));
33 
34  // Last thing - set up the duration of the simulation to 1 second
35  settings.set(RunSettingsId::RUN_END_TIME, 1._f);
36  }
37 
38  virtual void tearDown(const Storage& storage, const Statistics& stats) override {
39  // Save the result of the simulation to custom binary format.
40  BinaryOutput io(Path("output.ssf"));
41  io.dump(storage, stats);
42  }
43 };
44 
45 int main() {
46 
47  try {
48  HelloAsteroid simulation;
49 
50  // To start the simulation, we need to provide it with a particle storage. Since our simulation
51  // creates the initial conditions itself, we can simply create an empty storage here.
52  Storage storage;
53  simulation.run(storage);
54 
55  } catch (const Exception& e) {
56  // An exception may be thrown either from user-defined setUp or tearDown function, but also
57  // while running the simulation, so we should always catch it.
58  std::cout << "Error in simulation: " << e.what() << std::endl;
59  return -1;
60  }
61 
62  return 0;
63 }
int main()
Includes common headers.
BasicVector< Float > Vector
Definition: Vector.h:539
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
Generic exception.
Definition: Exceptions.h:10
virtual const char * what() const noexcept
Definition: Exceptions.h:18
virtual void tearDown(const Storage &storage, const Statistics &stats) override
Called after the run.
virtual void setUp(SharedPtr< Storage > storage) override
Prepares the run, creates logger, output, ...
Defines the interface for a run.
Definition: IRun.h:61
Statistics run(Storage &storage)
Runs the simulation.
Definition: IRun.cpp:187
Object for adding one or more bodies with given material into a Storage.
Definition: Initial.h:106
BodyView addMonolithicBody(Storage &storage, const BodySettings &body)
Creates a monolithic body by filling given domain with particles.
Definition: Initial.cpp:81
Object representing a path on a filesystem.
Definition: Path.h:17
Settings & set(const TEnum idx, TValue &&value, std::enable_if_t<!std::is_enum< std::decay_t< TValue >>::value, int >=0)
Saves a value into the settings.
Definition: Settings.h:226
Spherical domain, defined by the center of sphere and its radius.
Definition: Domain.h:102
Object holding various statistics about current run.
Definition: Statistics.h:22
Container storing all quantities used within the simulations.
Definition: Storage.h:230
@ PARTICLE_COUNT
Number of SPH particles in the body.
@ RUN_NAME
User-specified name of the run, used in some output files.
Definition: MemoryPool.h:5