SPH
Plot.h
Go to the documentation of this file.
1 #pragma once
2 
7 
10 #include "physics/Integrals.h"
11 #include "post/Analysis.h"
12 
14 
15 struct PlotPoint;
16 struct ErrorPlotPoint;
17 class AffineMatrix2;
18 
19 class IDrawPath : public Polymorphic {
20 public:
22  virtual void addPoint(const PlotPoint& point) = 0;
23 
25  virtual void closePath() = 0;
26 
28  virtual void endPath() = 0;
29 };
30 
34 class IDrawingContext : public Polymorphic {
35 public:
39  virtual void drawPoint(const PlotPoint& point) = 0;
40 
42  virtual void drawErrorPoint(const ErrorPlotPoint& point) = 0;
43 
48  virtual void drawLine(const PlotPoint& from, const PlotPoint& to) = 0;
49 
55 
62  virtual void setStyle(const Size index) = 0;
63 
67  virtual void setTransformMatrix(const AffineMatrix2& matrix) = 0;
68 };
69 
70 
75 class IPlot : public Polymorphic {
76 protected:
77  struct {
80  } ranges;
81 
82 public:
84  Interval rangeX() const {
85  return ranges.x;
86  }
87 
89  Interval rangeY() const {
90  return ranges.y;
91  }
92 
94  virtual std::string getCaption() const = 0;
95 
99  virtual void onTimeStep(const Storage& storage, const Statistics& stats) = 0;
100 
102  virtual void clear() = 0;
103 
105  virtual void plot(IDrawingContext& dc) const = 0;
106 };
107 
112 template <typename TDerived>
113 class SpatialPlot : public IPlot {
114 protected:
118 
119 public:
123  explicit SpatialPlot(const QuantityId id, const Optional<Size> binCnt = NOTHING)
124  : id(id)
125  , binCnt(binCnt) {}
126 
127  virtual std::string getCaption() const override {
128  return getMetadata(id).quantityName;
129  }
130 
131  virtual void onTimeStep(const Storage& storage, const Statistics& UNUSED(stats)) override;
132 
133  virtual void clear() override;
134 
135  virtual void plot(IDrawingContext& dc) const override;
136 
137 private:
138  Float getX(const Vector r) const {
139  return static_cast<const TDerived*>(this)->getX(r);
140  }
141 };
142 
144 class AxialDistributionPlot : public SpatialPlot<AxialDistributionPlot> {
145 private:
146  Vector axis;
147 
148 public:
151  , axis(axis) {}
152 
153  INLINE Float getX(const Vector& r) const {
154  return getLength(r - dot(r, axis) * axis);
155  }
156 };
157 
159 class RadialDistributionPlot : public SpatialPlot<RadialDistributionPlot> {
160 public:
162 
163  INLINE Float getX(const Vector& r) const {
164  return getLength(r);
165  }
166 };
167 
173 class TemporalPlot : public IPlot {
174 public:
176  struct Params {
179 
182 
184  Float minRangeY = 0._f;
185 
187  bool shrinkY = false;
188 
192 
194  Float period = 0._f;
195  };
196 
197 
198 private:
200  IntegralWrapper integral;
201 
203  Queue<PlotPoint> points;
204 
206  Float lastTime = -INFTY;
207 
209  const Params params;
210 
212  Float actPeriod;
213 
214 public:
216  TemporalPlot(const IntegralWrapper& integral, const Params& params)
217  : integral(integral)
218  , params(params) {
219  SPH_ASSERT(params.segment > 0._f);
220  actPeriod = params.period;
221  }
222 
223  virtual std::string getCaption() const override {
224  return integral.getName();
225  }
226 
227  virtual void onTimeStep(const Storage& storage, const Statistics& stats) override;
228 
229  virtual void clear() override;
230 
231  virtual void plot(IDrawingContext& dc) const override;
232 
233 private:
235  bool isExpired(const Float x, const Float t) const;
236 };
237 
238 
242 class HistogramPlot : public IPlot {
243 protected:
246 
249 
254 
257 
259  std::string name;
260 
262 
263 public:
266  const Float period,
267  const std::string& name)
268  : id(id)
269  , interval(interval)
270  , period(period)
271  , name(name) {}
272 
274  : id(id)
275  , interval(interval)
276  , period(period) {
278  }
279 
280  virtual std::string getCaption() const override {
281  return name;
282  }
283 
284  virtual void onTimeStep(const Storage& storage, const Statistics& stats) override;
285 
286  virtual void clear() override;
287 
288  virtual void plot(IDrawingContext& dc) const override;
289 };
290 
294 class AngularHistogramPlot : public IPlot {
296  Array<PlotPoint> points;
297 
298  Float period;
299 
300  Float lastTime = -INFTY;
301 
302 public:
303  explicit AngularHistogramPlot(const Float period);
304 
305  virtual std::string getCaption() const override {
306  return "Angular histogram of velocities";
307  }
308 
309  virtual void onTimeStep(const Storage& storage, const Statistics& stats) override;
310 
311  virtual void clear() override;
312 
313  virtual void plot(IDrawingContext& dc) const override;
314 };
315 
316 class SfdPlot : public IPlot {
317 private:
318  Post::HistogramSource source;
320  Float period;
321  std::string name;
322 
323  Float lastTime = -INFTY;
324  Array<PlotPoint> sfd;
325 
326 public:
328  SfdPlot(const Flags<Post::ComponentFlag> connectivity, const Float period);
329 
331  SfdPlot(const Float period);
332 
333  virtual std::string getCaption() const override;
334 
335  virtual void onTimeStep(const Storage& storage, const Statistics& stats) override;
336 
337  virtual void clear() override;
338 
339  virtual void plot(IDrawingContext& dc) const override;
340 };
341 
342 enum class AxisScaleEnum {
343  LOG_X = 1 << 0,
344  LOG_Y = 1 << 1,
345 };
346 
348 class DataPlot : public IPlot {
349 private:
350  Array<PlotPoint> values;
351  std::string name;
352 
353 public:
354  DataPlot(const Array<Post::HistPoint>& points, const Flags<AxisScaleEnum> scale, const std::string& name);
355 
356  virtual std::string getCaption() const override;
357 
358  virtual void onTimeStep(const Storage& storage, const Statistics& stats) override;
359 
360  virtual void clear() override;
361 
362  virtual void plot(IDrawingContext& dc) const override;
363 };
364 
366 class MultiPlot : public IPlot {
367 private:
368  Array<AutoPtr<IPlot>> plots;
369 
370 public:
371  explicit MultiPlot(Array<AutoPtr<IPlot>>&& plots)
372  : plots(std::move(plots)) {}
373 
374  virtual std::string getCaption() const override {
375  return plots[0]->getCaption();
376  }
377 
378  virtual void onTimeStep(const Storage& storage, const Statistics& stats) override;
379 
380  virtual void clear() override;
381 
382  virtual void plot(IDrawingContext& dc) const override;
383 };
384 
388 Array<Float> getLinearTics(const Interval& interval, const Size minCount);
389 
391 Array<Float> getLogTics(const Interval& interval, const Size minCount);
392 
Various function for interpretation of the results of a simulation.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
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
Integrals of motion and other integral quantities.
constexpr Float INFTY
Definition: MathUtils.h:38
#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
Class defining additional operators from existing ones.
const NothingType NOTHING
Definition: Optional.h:16
AxisScaleEnum
Definition: Plot.h:342
Array< Float > getLinearTics(const Interval &interval, const Size minCount)
Returns the tics to be drawn on a linear axis of a plot.
Definition: Plot.cpp:423
Array< Float > getLogTics(const Interval &interval, const Size minCount)
Returns the tics to be drawn on a logarithmic axis of a plot.
Definition: Plot.cpp:458
QuantityMetadata getMetadata(const QuantityId key)
Returns the quantity information using quantity ID.
Definition: QuantityIds.cpp:27
QuantityId
Unique IDs of basic quantities of SPH particles.
Definition: QuantityIds.h:19
Double-ended queue.
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 dot(const BasicVector< float > &v1, const BasicVector< float > &v2)
Make sure the vector is trivially constructible and destructible, needed for fast initialization of a...
Definition: Vector.h:548
2D affine matrix
Definition: Point.h:50
Differential histogram of angular distribution.
Definition: Plot.h:294
virtual void onTimeStep(const Storage &storage, const Statistics &stats) override
Updates the plot with new data.
Definition: Plot.cpp:221
AngularHistogramPlot(const Float period)
Definition: Plot.cpp:218
virtual std::string getCaption() const override
Returns the caption of the plot.
Definition: Plot.h:305
virtual void clear() override
Clears all cached data, prepares for next run.
Definition: Plot.cpp:258
virtual void plot(IDrawingContext &dc) const override
Draws the plot into the drawing context.
Definition: Plot.cpp:263
Wrapper of pointer that deletes the resource from destructor.
Definition: AutoPtr.h:15
Plots a dependence of given quantity on the distance from given axis.
Definition: Plot.h:144
INLINE Float getX(const Vector &r) const
Definition: Plot.h:153
AxialDistributionPlot(const Vector &axis, const QuantityId id, const Optional< Size > binCnt=NOTHING)
Definition: Plot.h:149
Plots given array of points.
Definition: Plot.h:348
virtual void plot(IDrawingContext &dc) const override
Draws the plot into the drawing context.
Definition: Plot.cpp:379
virtual void onTimeStep(const Storage &storage, const Statistics &stats) override
Updates the plot with new data.
Definition: Plot.cpp:371
virtual std::string getCaption() const override
Returns the caption of the plot.
Definition: Plot.cpp:367
DataPlot(const Array< Post::HistPoint > &points, const Flags< AxisScaleEnum > scale, const std::string &name)
Definition: Plot.cpp:348
virtual void clear() override
Clears all cached data, prepares for next run.
Definition: Plot.cpp:375
Differential histogram of quantities.
Definition: Plot.h:242
Post::ExtHistogramId id
ID of a quantity from which the histogram is constructed.
Definition: Plot.h:245
Float period
Period of redrawing the histogram. Zero means the histogram is drawn every time step.
Definition: Plot.h:256
HistogramPlot(const QuantityId id, const Optional< Interval > interval, const Float period)
Definition: Plot.h:273
virtual void plot(IDrawingContext &dc) const override
Draws the plot into the drawing context.
Definition: Plot.cpp:202
HistogramPlot(const Post::HistogramId id, const Optional< Interval > interval, const Float period, const std::string &name)
Definition: Plot.h:264
virtual std::string getCaption() const override
Returns the caption of the plot.
Definition: Plot.h:280
std::string name
Displayed name of the histogram.
Definition: Plot.h:259
Float lastTime
Definition: Plot.h:261
virtual void clear() override
Clears all cached data, prepares for next run.
Definition: Plot.cpp:198
Optional< Interval > interval
Definition: Plot.h:253
Array< Post::HistPoint > points
Points representing the histogram.
Definition: Plot.h:248
virtual void onTimeStep(const Storage &storage, const Statistics &stats) override
Updates the plot with new data.
Definition: Plot.cpp:175
Definition: Plot.h:19
virtual void endPath()=0
Finalizes the path. Does not connect the last point to anything.
virtual void closePath()=0
Closes the path, connecting to the first point on the path.
virtual void addPoint(const PlotPoint &point)=0
Adds a next point on the path.
Abstraction of a drawing context.
Definition: Plot.h:34
virtual void drawLine(const PlotPoint &from, const PlotPoint &to)=0
Draws a line connecting two points.
virtual AutoPtr< IDrawPath > drawPath()=0
Draws a path connecting points.
virtual void setStyle(const Size index)=0
Changes the current drawing style.
virtual void drawErrorPoint(const ErrorPlotPoint &point)=0
Adds a point with error bars to the plot.
virtual void setTransformMatrix(const AffineMatrix2 &matrix)=0
Applies the given tranformation matrix on all primitives.
virtual void drawPoint(const PlotPoint &point)=0
Adds a single point to the plot.
Interface for constructing generic plots from quantities stored in storage.
Definition: Plot.h:75
virtual void onTimeStep(const Storage &storage, const Statistics &stats)=0
Updates the plot with new data.
struct IPlot::@12 ranges
Interval rangeX() const
Returns the plotted range in x-coordinate.
Definition: Plot.h:84
virtual void plot(IDrawingContext &dc) const =0
Draws the plot into the drawing context.
Interval y
Definition: Plot.h:79
Interval x
Definition: Plot.h:78
virtual std::string getCaption() const =0
Returns the caption of the plot.
virtual void clear()=0
Clears all cached data, prepares for next run.
Interval rangeY() const
Returns the plotted range in y-coordinate.
Definition: Plot.h:89
Helper integral wrapping another integral and converting the returned value to scalar.
Definition: Integrals.h:228
virtual std::string getName() const override
Returns the name of the integral.
Definition: Integrals.h:250
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
Helper object used for drawing multiple plots into the same device.
Definition: Plot.h:366
MultiPlot(Array< AutoPtr< IPlot >> &&plots)
Definition: Plot.h:371
virtual void clear() override
Clears all cached data, prepares for next run.
Definition: Plot.cpp:405
virtual void onTimeStep(const Storage &storage, const Statistics &stats) override
Updates the plot with new data.
Definition: Plot.cpp:396
virtual std::string getCaption() const override
Returns the caption of the plot.
Definition: Plot.h:374
virtual void plot(IDrawingContext &dc) const override
Draws the plot into the drawing context.
Definition: Plot.cpp:412
Plots a dependence of given quantity on the distance from the origin.
Definition: Plot.h:159
INLINE Float getX(const Vector &r) const
Definition: Plot.h:163
RadialDistributionPlot(const QuantityId id, const Optional< Size > binCnt=NOTHING)
Definition: Plot.cpp:77
Definition: Plot.h:316
virtual void clear() override
Clears all cached data, prepares for next run.
Definition: Plot.cpp:325
virtual std::string getCaption() const override
Returns the caption of the plot.
Definition: Plot.cpp:290
virtual void plot(IDrawingContext &dc) const override
Draws the plot into the drawing context.
Definition: Plot.cpp:331
virtual void onTimeStep(const Storage &storage, const Statistics &stats) override
Updates the plot with new data.
Definition: Plot.cpp:294
SfdPlot(const Flags< Post::ComponentFlag > connectivity, const Float period)
Plots SFD of components, given their connectivity.
Definition: Plot.cpp:277
Base class for plots showing a dependence of given quantity on a spatial coordinate.
Definition: Plot.h:113
Optional< Size > binCnt
Definition: Plot.h:117
virtual void clear() override
Clears all cached data, prepares for next run.
Definition: Plot.cpp:62
Array< PlotPoint > points
Definition: Plot.h:116
virtual void plot(IDrawingContext &dc) const override
Draws the plot into the drawing context.
Definition: Plot.cpp:68
QuantityId id
Definition: Plot.h:115
virtual std::string getCaption() const override
Returns the caption of the plot.
Definition: Plot.h:127
virtual void onTimeStep(const Storage &storage, const Statistics &UNUSED(stats)) override
Definition: Plot.cpp:17
SpatialPlot(const QuantityId id, const Optional< Size > binCnt=NOTHING)
Constructs the spatial plot.
Definition: Plot.h:123
Object holding various statistics about current run.
Definition: Statistics.h:22
Container storing all quantities used within the simulations.
Definition: Storage.h:230
Plot of temporal dependence of a scalar quantity.
Definition: Plot.h:173
TemporalPlot(const IntegralWrapper &integral, const Params &params)
Creates a plot showing the whole history of given integral.
Definition: Plot.h:216
virtual void plot(IDrawingContext &dc) const override
Draws the plot into the drawing context.
Definition: Plot.cpp:149
virtual void clear() override
Clears all cached data, prepares for next run.
Definition: Plot.cpp:142
virtual std::string getCaption() const override
Returns the caption of the plot.
Definition: Plot.h:223
virtual void onTimeStep(const Storage &storage, const Statistics &stats) override
Updates the plot with new data.
Definition: Plot.cpp:85
HistogramSource
Source data used to construct the histogram.
Definition: Analysis.h:217
HistogramId
Quantity from which the histogram is constructed.
Definition: Analysis.h:191
Overload of std::swap for Sph::Array.
Definition: Array.h:578
Point with error bars.
Definition: Point.h:43
Point in 2D plot.
Definition: Point.h:16
Base class for all polymorphic objects.
Definition: Object.h:88
std::string quantityName
Full name of the quantity (i.e. 'Density', 'Deviatoric stress', ...)
Definition: QuantityIds.h:242
Parameters of the plot.
Definition: Plot.h:176
Float segment
Plotted time segment.
Definition: Plot.h:178
Interval fixedRangeX
Fixed x-range for the plot. If empty, a dynamic range is used.
Definition: Plot.h:181
Float minRangeY
Minimal size of the y-range.
Definition: Plot.h:184
Float period
Time that needs to pass before a new point is added.
Definition: Plot.h:194
bool shrinkY
When discarting points out of plotted range, shrink y-axis to fit currently visible points.
Definition: Plot.h:187