DPsim
Loading...
Searching...
No Matches
Event.h
Go to the documentation of this file.
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
18#include <dpsim-models/Logger.h>
20#include <dpsim/Config.h>
21
22namespace DPsim {
23
24class EventComparator;
25class EventQueue;
26
27class Event {
28
29 friend class EventComparator;
30 friend class EventQueue;
31
32protected:
34
35public:
36 using Ptr = std::shared_ptr<Event>;
37
38 virtual void execute() = 0;
39
41
42 virtual ~Event() {}
43};
44
46public:
47 bool operator()(const Event::Ptr &l, const Event::Ptr &r) {
48 return l->mTime > r->mTime;
49 }
50};
51
52template <typename T>
53class AttributeEvent : public Event, public SharedFactory<AttributeEvent<T>> {
54protected:
57
58public:
60 : Event(t), mAttribute(attr), mNewValue(val) {}
61
62 void execute() { mAttribute->set(mNewValue); }
63};
64
65class SwitchEvent : public Event, public SharedFactory<SwitchEvent> {
66
67protected:
68 std::shared_ptr<CPS::Base::Ph1::Switch> mSwitch;
70
71public:
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
86class SwitchEvent3Ph : public Event, public SharedFactory<SwitchEvent3Ph> {
87
88protected:
89 std::shared_ptr<CPS::Base::Ph3::Switch> mSwitch;
91
92public:
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
108
109protected:
110 std::priority_queue<Event::Ptr, std::deque<Event::Ptr>, EventComparator>
112
113public:
115 void addEvent(Event::Ptr e);
117 void handleEvents(CPS::Real currentTime);
118};
119} // namespace DPsim
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:250
CPS::Attribute< T >::Ptr mAttribute
Definition Event.h:55
AttributeEvent(CPS::Real t, typename CPS::Attribute< T >::Ptr attr, T val)
Definition Event.h:59
bool operator()(const Event::Ptr &l, const Event::Ptr &r)
Definition Event.h:47
friend class EventComparator
Definition Event.h:29
friend class EventQueue
Definition Event.h:30
virtual ~Event()
Definition Event.h:42
std::shared_ptr< Event > Ptr
Definition Event.h:36
virtual void execute()=0
Event(CPS::Real t)
Definition Event.h:40
CPS::Real mTime
Definition Event.h:33
void handleEvents(CPS::Real currentTime)
Definition Event.cpp:16
std::priority_queue< Event::Ptr, std::deque< Event::Ptr >, EventComparator > mEvents
Definition Event.h:111
void addEvent(Event::Ptr e)
Definition Event.cpp:14
CPS::Bool mNewState
Definition Event.h:90
std::shared_ptr< CPS::Base::Ph3::Switch > mSwitch
Definition Event.h:89
SwitchEvent3Ph(CPS::Real t, const std::shared_ptr< CPS::Base::Ph3::Switch > &sw, CPS::Bool state)
Definition Event.h:95
void execute()
Definition Event.h:78
CPS::Bool mNewState
Definition Event.h:69
SwitchEvent(CPS::Real t, const std::shared_ptr< CPS::Base::Ph1::Switch > &sw, CPS::Bool state)
Definition Event.h:74
std::shared_ptr< CPS::Base::Ph1::Switch > mSwitch
Definition Event.h:68
static std::shared_ptr< SwitchEvent > make(Args &&...args)
Definition PtrFactory.h:19
double Real
Definition Definitions.h:62
bool Bool
Definition Definitions.h:64