SPH
DeathStar.cpp
Go to the documentation of this file.
1 #include "Sph.h"
2 #include <iostream>
3 
4 using namespace Sph;
5 
6 // Custom domain, created by a boolean subtraction of two other domains.
7 class SubtractDomain : public IDomain {
8 private:
9  IDomain& primary;
10  IDomain& subtracted;
11 
12 public:
13  SubtractDomain(IDomain& primary, IDomain& subtracted)
14  : primary(primary)
15  , subtracted(subtracted) {}
16 
17  virtual Vector getCenter() const override {
18  // It does not have to be the EXACT geometric center, so let's return the center of
19  // the primary domain for simplicity
20  return primary.getCenter();
21  }
22 
23  virtual Box getBoundingBox() const override {
24  // Subtracted domain can only shrink the bounding box, so it's OK to return the primary one
25  return primary.getBoundingBox();
26  }
27 
28  virtual Float getVolume() const override {
29  // Estimate the actual volume by the volume of the primary domain. In this example, this can only
30  // cause underestimation of the number of particles.
31  return primary.getVolume();
32  }
33 
34  virtual Float getSurfaceArea() const override {
35  // Similar to the argument in getVolume
36  return primary.getSurfaceArea();
37  }
38 
39  virtual bool contains(const Vector& v) const override {
40  // The main part - vector is contained in the domain if it is contained in the primary domain
41  // and NOT contained in the subtracted domain
42  return primary.contains(v) && !subtracted.contains(v);
43  }
44 
45  // Additional functions needed by some components of the code that utilize the IDomain interface (such as
46  // boundary conditions); they are not needed in this example, so let's simply mark them as not
47  // implemented.
48 
49  virtual void getSubset(ArrayView<const Vector>, Array<Size>&, const SubsetType) const override {
51  }
52 
55  }
56 
57  virtual void project(ArrayView<Vector>, Optional<ArrayView<Size>>) const override {
59  }
60 
61  virtual void addGhosts(ArrayView<const Vector>, Array<Ghost>&, const Float, const Float) const override {
63  }
64 };
65 
66 
67 class DeathStar : public IRun {
68 public:
69  virtual void setUp(SharedPtr<Storage> storage) override {
70 
71  // base spherical domain without the crater with radius 1000m
72  SphericalDomain primarySphere(Vector(0._f), 1.e3_f);
73 
74  // sphere we are going to subtract to create the crater with radius 500m
75  SphericalDomain subtractedSphere(Vector(1.2e3_f, 0._f, 0._f), 5.e2_f);
76 
77  // compose the domains
78  SubtractDomain domain(primarySphere, subtractedSphere);
79 
80  // the rest is the same as in "Hello Asteroid"
81  InitialConditions ic(settings);
82  BodySettings body;
83  ic.addMonolithicBody(*storage, domain, body);
84 
85  settings.set(RunSettingsId::RUN_NAME, std::string("Death star"));
86  settings.set(RunSettingsId::RUN_END_TIME, 1._f);
87  }
88 
89  virtual void tearDown(const Storage& storage, const Statistics& stats) override {
90  BinaryOutput io(Path("output.ssf"));
91  io.dump(storage, stats);
92  }
93 };
94 
95 int main() {
96  try {
97  DeathStar simulation;
98  Storage storage;
99  simulation.run(storage);
100  } catch (const Exception& e) {
101  std::cout << "Error during simulation: " << e.what() << std::endl;
102  return -1;
103  }
104  return 0;
105 }
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
Definition: Assert.h:100
int main()
Definition: DeathStar.cpp:95
SubsetType
Definition: Domain.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
Includes common headers.
BasicVector< Float > Vector
Definition: Vector.h:539
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
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
Helper object defining three-dimensional interval (box).
Definition: Box.h:17
virtual void setUp(SharedPtr< Storage > storage) override
Prepares the run, creates logger, output, ...
Definition: DeathStar.cpp:69
virtual void tearDown(const Storage &storage, const Statistics &stats) override
Called after the run.
Definition: DeathStar.cpp:89
Generic exception.
Definition: Exceptions.h:10
virtual const char * what() const noexcept
Definition: Exceptions.h:18
Base class for computational domains.
Definition: Domain.h:29
virtual Float getSurfaceArea() const =0
Returns the surface area of the domain.
virtual Box getBoundingBox() const =0
Returns the bounding box of the domain.
virtual Float getVolume() const =0
Returns the total volume of the domain.
virtual bool contains(const Vector &v) const =0
Checks if the given point lies inside the domain.
virtual Vector getCenter() const =0
Returns the center of the domain.
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
Wrapper of type value of which may or may not be present.
Definition: Optional.h:23
Object representing a path on a filesystem.
Definition: Path.h:17
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
virtual Float getVolume() const override
Returns the total volume of the domain.
Definition: DeathStar.cpp:28
virtual void getSubset(ArrayView< const Vector >, Array< Size > &, const SubsetType) const override
Returns an array of indices, marking vectors with given property by their index.
Definition: DeathStar.cpp:49
virtual Vector getCenter() const override
Returns the center of the domain.
Definition: DeathStar.cpp:17
virtual Box getBoundingBox() const override
Returns the bounding box of the domain.
Definition: DeathStar.cpp:23
virtual void addGhosts(ArrayView< const Vector >, Array< Ghost > &, const Float, const Float) const override
Duplicates positions located close to the boundary, placing copies ("ghosts") symmetrically to the ot...
Definition: DeathStar.cpp:61
virtual Float getSurfaceArea() const override
Returns the surface area of the domain.
Definition: DeathStar.cpp:34
virtual void project(ArrayView< Vector >, Optional< ArrayView< Size >>) const override
Projects vectors outside of the domain onto its boundary.
Definition: DeathStar.cpp:57
SubtractDomain(IDomain &primary, IDomain &subtracted)
Definition: DeathStar.cpp:13
virtual bool contains(const Vector &v) const override
Checks if the given point lies inside the domain.
Definition: DeathStar.cpp:39
virtual void getDistanceToBoundary(ArrayView< const Vector >, Array< Float > &) const override
Returns distances of particles lying close to the boundary.
Definition: DeathStar.cpp:53
@ RUN_NAME
User-specified name of the run, used in some output files.
Definition: MemoryPool.h:5