DPsim
Event.h
1 /* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2  * EONERC, RWTH Aachen University
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7  *********************************************************************************/
8 
9 #pragma once
10 
11 #include <deque>
12 #include <queue>
13 
14 #include <dpsim-models/Attribute.h>
15 #include <dpsim-models/Base/Base_Ph1_Switch.h>
16 #include <dpsim-models/Base/Base_Ph3_Switch.h>
17 #include <dpsim-models/Definitions.h>
18 #include <dpsim-models/Logger.h>
19 #include <dpsim-models/PtrFactory.h>
20 #include <dpsim/Config.h>
21 
22 namespace DPsim {
23 
24 class EventComparator;
25 class EventQueue;
26 
27 class Event {
28 
29  friend class EventComparator;
30  friend class EventQueue;
31 
32 protected:
33  CPS::Real mTime;
34 
35 public:
36  using Ptr = std::shared_ptr<Event>;
37 
38  virtual void execute() = 0;
39 
40  Event(CPS::Real t) : mTime(t) {}
41 
42  virtual ~Event() {}
43 };
44 
46 public:
47  bool operator()(const Event::Ptr &l, const Event::Ptr &r) {
48  return l->mTime > r->mTime;
49  }
50 };
51 
52 template <typename T>
53 class AttributeEvent : public Event, public SharedFactory<AttributeEvent<T>> {
54 protected:
55  typename CPS::Attribute<T>::Ptr mAttribute;
56  T mNewValue;
57 
58 public:
59  AttributeEvent(CPS::Real t, typename CPS::Attribute<T>::Ptr attr, T val)
60  : Event(t), mAttribute(attr), mNewValue(val) {}
61 
62  void execute() { mAttribute->set(mNewValue); }
63 };
64 
65 class SwitchEvent : public Event, public SharedFactory<SwitchEvent> {
66 
67 protected:
68  std::shared_ptr<CPS::Base::Ph1::Switch> mSwitch;
69  CPS::Bool mNewState;
70 
71 public:
73 
74  SwitchEvent(CPS::Real t, const std::shared_ptr<CPS::Base::Ph1::Switch> &sw,
75  CPS::Bool state)
76  : Event(t), mSwitch(sw), mNewState(state) {}
77 
78  void execute() {
79  if (mNewState)
80  mSwitch->close();
81  else
82  mSwitch->open();
83  }
84 };
85 
86 class SwitchEvent3Ph : public Event, public SharedFactory<SwitchEvent3Ph> {
87 
88 protected:
89  std::shared_ptr<CPS::Base::Ph3::Switch> mSwitch;
90  CPS::Bool mNewState;
91 
92 public:
94 
95  SwitchEvent3Ph(CPS::Real t, const std::shared_ptr<CPS::Base::Ph3::Switch> &sw,
96  CPS::Bool state)
97  : Event(t), mSwitch(sw), mNewState(state) {}
98 
99  void execute() {
100  if (mNewState)
101  mSwitch->closeSwitch();
102  else
103  mSwitch->openSwitch();
104  }
105 };
106 
107 class EventQueue {
108 
109 protected:
110  std::priority_queue<Event::Ptr, std::deque<Event::Ptr>, EventComparator>
111  mEvents;
112 
113 public:
115  void addEvent(Event::Ptr e);
117  void handleEvents(CPS::Real currentTime);
118 };
119 } // namespace DPsim