SPH
Trigger.h
Go to the documentation of this file.
1 #pragma once
2 
7 
9 #include "system/Statistics.h"
10 
12 
13 enum class TriggerEnum {
14  ONE_TIME,
15  REPEATING,
16 };
17 
19 class ITrigger : public Polymorphic {
20 public:
22  virtual TriggerEnum type() const = 0;
23 
28  virtual bool condition(const Storage& storage, const Statistics& stats) = 0;
29 
33  virtual AutoPtr<ITrigger> action(Storage& storage, Statistics& stats) = 0;
34 };
35 
37 class PeriodicTrigger : public ITrigger {
38 private:
39  Float _period;
40  Float _lastAction;
41 
42 public:
44  explicit PeriodicTrigger(const Float period, const Float startTime)
45  : _period(period)
46  , _lastAction(startTime - EPS) {}
47 
48  virtual TriggerEnum type() const override {
50  }
51 
52  virtual bool condition(const Storage& UNUSED(storage), const Statistics& stats) override {
53  const Float t = stats.get<Float>(StatisticsId::RUN_TIME);
54  if (t > _lastAction + _period) {
55  _lastAction = t;
56  return true;
57  } else {
58  return false;
59  }
60  }
61 };
62 
Simplified implementation of std::unique_ptr, using only default deleter.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
constexpr Float EPS
Definition: MathUtils.h:30
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
Statistics gathered and periodically displayed during the run.
@ RUN_TIME
Current time of the simulation in code units. Does not necessarily have to be 0 when run starts.
TriggerEnum
Definition: Trigger.h:13
@ REPEATING
Execute the trigger every time step.
@ ONE_TIME
Execute the trigger only once.
Wrapper of pointer that deletes the resource from destructor.
Definition: AutoPtr.h:15
Interface for triggering generic actions during the run.
Definition: Trigger.h:19
virtual TriggerEnum type() const =0
Returns the type of the trigger.
virtual bool condition(const Storage &storage, const Statistics &stats)=0
Returns true if the trigger should be executed.
virtual AutoPtr< ITrigger > action(Storage &storage, Statistics &stats)=0
Action executed when the condition is fulfilled.
Trigger executing given action every period.
Definition: Trigger.h:37
virtual TriggerEnum type() const override
Returns the type of the trigger.
Definition: Trigger.h:48
virtual bool condition(const Storage &UNUSED(storage), const Statistics &stats) override
Definition: Trigger.h:52
PeriodicTrigger(const Float period, const Float startTime)
period Period in simulation time of triggered action.
Definition: Trigger.h:44
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
Base class for all polymorphic objects.
Definition: Object.h:88