SPH
Diagnostics.cpp
Go to the documentation of this file.
1 #include "sph/Diagnostics.h"
3 #include "quantities/IMaterial.h"
4 #include "system/Factory.h"
5 #include "system/Statistics.h"
6 #include "thread/Scheduler.h"
7 
9 
13  finder->build(SEQUENTIAL, r);
14 
17 
19  for (Size i = 0; i < r.size(); ++i) {
20  // only smaller h to go through each pair only once
21  finder->findLowerRank(i, r[i][H] * radius, neighs);
22  for (auto& n : neighs) {
23  if (getSqrLength(r[i] - r[n.index]) < sqr(limit * (r[i][H] + r[n.index][H]))) {
24  pairs.push(ParticlePairingDiagnostic::Pair{ i, n.index });
25  }
26  }
27  }
28  return pairs;
29 }
30 
32  const Statistics& UNUSED(stats)) const {
33  Array<Pair> pairs = getPairs(storage);
34  if (pairs.empty()) {
35  return SUCCESS;
36  } else {
37  DiagnosticsError error;
38  error.description = "Particle pairs found";
39  for (Pair& p : pairs) {
40  error.offendingParticles[p.i1];
41  error.offendingParticles[p.i2];
42  }
43  return DiagnosticsReport(error);
44  }
45 }
46 
48  const Statistics& UNUSED(stats)) const {
51  finder->build(SEQUENTIAL, r);
53 
54  struct Pair {
55  Size i1, i2;
56  };
57  Array<Pair> pairs;
58  for (Size i = 0; i < r.size(); ++i) {
59  finder->findLowerRank(i, r[i][H] * radius, neighs);
60  for (auto& n : neighs) {
61  const Size j = n.index;
62  if (abs(r[i][H] - r[j][H]) > limit * (r[i][H] + r[j][H])) {
63  pairs.push(Pair{ i, j });
64  }
65  }
66  }
67  if (pairs.empty()) {
68  return SUCCESS;
69  } else {
70  DiagnosticsError error;
71  error.description = "Discontinuity in smoothing lengths found";
72  for (Pair& p : pairs) {
73  const Float actual = abs(r[p.i1][H] - r[p.i2][H]);
74  const Float expected = limit * (r[p.i1][H] + r[p.i2][H]);
75  const std::string message =
76  "dH = " + std::to_string(actual) + " (limit = " + std::to_string(expected) + ")";
77  error.offendingParticles[p.i1] = message;
78  error.offendingParticles[p.i2] = message;
79  }
80  return DiagnosticsReport(error);
81  }
82 }
83 
85  : factor(timescaleFactor) {}
86 
88  const Statistics& UNUSED(stats)) const {
89  const Float dt2 = sqr(factor);
90  ArrayView<const Vector> r, v, dv;
91  tie(r, v, dv) = storage.getAll<Vector>(QuantityId::POSITION);
92  DiagnosticsError error;
93  error.description = "Probable CFL instability detected";
94  for (Size i = 0; i < r.size(); ++i) {
95  const Float limit = r[i][H] / dt2;
96  if (getLength(dv[i]) > limit) {
97  error.offendingParticles[i] =
98  "dv = " + std::to_string(getLength(dv[i])) + " (limit = " + std::to_string(limit) + ")";
99  }
100  }
101  if (error.offendingParticles.empty()) {
102  return SUCCESS;
103  } else {
104  return DiagnosticsReport(error);
105  }
106 }
107 
109  const Float dt = stats.get<Float>(StatisticsId::TIMESTEP_VALUE);
111  tie(u, du) = storage.getAll<Float>(QuantityId::ENERGY);
112  DiagnosticsError error;
113  error.description = "Particle overcooling detected";
114  for (Size matId = 0; matId < storage.getMaterialCnt(); ++matId) {
115  MaterialView material = storage.getMaterial(matId);
116  const Interval range = material->range(QuantityId::ENERGY);
117  for (Size i : material.sequence()) {
118  const Float u1 = u[i] + du[i] * dt;
119  if (u1 < range.lower()) {
120  error.offendingParticles[i] =
121  "u = " + std::to_string(u1) + " (limit = " + std::to_string(range.lower()) + ")";
122  }
123  }
124  }
125  if (error.offendingParticles.empty()) {
126  return SUCCESS;
127  } else {
128  return DiagnosticsReport(error);
129  }
130 }
131 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Looking for problems in SPH simulation and reporting potential errors.
BasicOutcome< DiagnosticsError > DiagnosticsReport
Definition: Diagnostics.h:25
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
Base class for all particle materials.
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
INLINE auto abs(const T &f)
Definition: MathUtils.h:276
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
const SuccessTag SUCCESS
Global constant for successful outcome.
Definition: Outcome.h:141
@ POSITION
Positions (velocities, accelerations) of particles, always a vector quantity,.
@ ENERGY
Specific internal energy, always a scalar quantity.
SequentialScheduler SEQUENTIAL
Global instance of the sequential scheduler.
Definition: Scheduler.cpp:43
Interface for executing tasks (potentially) asynchronously.
StaticArray< T0 &, sizeof...(TArgs)+1 > tie(T0 &t0, TArgs &... rest)
Creates a static array from a list of l-value references.
Definition: StaticArray.h:281
Statistics gathered and periodically displayed during the run.
@ TIMESTEP_VALUE
Current value of timestep.
INLINE Float getLength(const Vector &v)
Returns the length of the vector. Enabled only for vectors of floating-point precision.
Definition: Vector.h:579
INLINE Float getSqrLength(const Vector &v)
Definition: Vector.h:574
@ H
Definition: Vector.h:25
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
INLINE TCounter size() const
Definition: ArrayView.h:101
Generic dynamically allocated resizable storage.
Definition: Array.h:43
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
INLINE bool empty() const noexcept
Definition: Array.h:201
Expected-like class that does not contain any value.
Definition: Outcome.h:44
CourantInstabilityDiagnostic(const Float timescaleFactor)
Definition: Diagnostics.cpp:84
virtual DiagnosticsReport check(const Storage &storage, const Statistics &stats) const override
Definition: Diagnostics.cpp:87
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
INLINE Float lower() const
Returns lower bound of the interval.
Definition: Interval.h:74
Non-owning wrapper of a material and particles with this material.
Definition: IMaterial.h:30
INLINE IndexSequence sequence()
Returns iterable index sequence.
Definition: IMaterial.h:75
virtual DiagnosticsReport check(const Storage &storage, const Statistics &stats) const override
virtual DiagnosticsReport check(const Storage &storage, const Statistics &stats) const override
Checks for particle pairs, returns SUCCESS if no pair is found.
Definition: Diagnostics.cpp:31
Array< Pair > getPairs(const Storage &storage) const
Returns the list of particles forming pairs, i.e. particles on top of each other or very close.
Definition: Diagnostics.cpp:10
static const Settings & getDefaults()
\brief Returns a reference to object containing default values of all settings.
virtual DiagnosticsReport check(const Storage &storage, const Statistics &stats) const override
Definition: Diagnostics.cpp:47
Array with fixed number of allocated elements.
Definition: StaticArray.h:19
Object holding various statistics about current run.
Definition: Statistics.h:22
TValue get(const StatisticsId idx) const
Returns value of a statistic.
Definition: Statistics.h:88
Container storing all quantities used within the simulations.
Definition: Storage.h:230
Size getMaterialCnt() const
Return the number of materials in the storage.
Definition: Storage.cpp:437
StaticArray< Array< TValue > &, 3 > getAll(const QuantityId key)
Retrieves quantity buffers from the storage, given its key and value type.
Definition: Storage.cpp:163
MaterialView getMaterial(const Size matIdx) const
Returns an object containing a reference to given material.
Definition: Storage.cpp:366
Array< TValue > & getValue(const QuantityId key)
Retrieves a quantity values from the storage, given its key and value type.
Definition: Storage.cpp:191
Creating code components based on values from settings.
AutoPtr< ISymmetricFinder > getFinder(const RunSettings &settings)
Definition: Factory.cpp:156
Object containing a reported error message.
Definition: Diagnostics.h:15
std::string description
Description of the encountered problem.
Definition: Diagnostics.h:17
std::map< Size, std::string > offendingParticles
Problematic particles and optional error message for each of them.
Definition: Diagnostics.h:22