DPsim
InterfaceVillasQueueless.h
1 /* Author: Niklas Eiling <niklas.eiling@eonerc.rwth-aachen.de>
2  * SPDX-FileCopyrightText: 2023-2024 Niklas Eiling <niklas.eiling@eonerc.rwth-aachen.de>
3  * SPDX-License-Identifier: MPL-2.0
4  */
5 
6 #pragma once
7 
8 #include <dpsim-models/Attribute.h>
9 #include <dpsim-models/Logger.h>
10 #include <dpsim-models/PtrFactory.h>
11 #include <dpsim-models/Task.h>
12 #include <dpsim/Config.h>
13 #include <dpsim/Definitions.h>
14 #include <dpsim/Interface.h>
15 #include <dpsim/Scheduler.h>
16 
17 #include <memory>
18 #include <villas/kernel/rt.hpp>
19 #include <villas/node.hpp>
20 #include <villas/node/exceptions.hpp>
21 #include <villas/node/memory.hpp>
22 #include <villas/pool.hpp>
23 #include <villas/sample.hpp>
24 #include <villas/signal.hpp>
25 #include <villas/signal_list.hpp>
26 
27 using namespace villas;
28 
29 namespace DPsim {
32  : public Interface,
33  public SharedFactory<InterfaceVillasQueueless> {
34 
35 public:
36  typedef std::shared_ptr<InterfaceVillasQueueless> Ptr;
37 
42  const String &nodeConfig, const String &name = "",
43  spdlog::level::level_enum logLevel = spdlog::level::level_enum::info);
44 
45  virtual void open() override;
46  virtual void close() override;
47 
48  // Function called by the Simulation to perform interface synchronization
49  virtual void syncExports() override;
50  // Function called by the Simulation to perform interface synchronization
51  virtual void syncImports() override;
52 
53  virtual CPS::Task::List getTasks() override;
54 
55  virtual void printVillasSignals() const;
56 
57  virtual ~InterfaceVillasQueueless() {
58  if (mOpened) {
59  try {
60  close();
61  } catch (const std::exception &e) {
62  SPDLOG_LOGGER_ERROR(mLog, "Error closing interface: {}", e.what());
63  }
64  }
65  }
66 
67 protected:
68  const String mNodeConfig;
69  node::Node *mNode;
70  node::Pool mSamplePool;
71 
72  virtual void writeToVillas();
73  virtual Int readFromVillas();
74  void createNode();
75  void createSignals();
76  Int mSequenceToDpsim;
77  Int mSequenceFromDpsim;
78 
79 public:
80  class PreStep : public CPS::Task {
81  public:
82  explicit PreStep(InterfaceVillasQueueless &intf)
83  : Task(intf.mName + ".Read"), mIntf(intf) {
84  for (const auto &[attr, _seqId, _blockOnRead, _syncOnStart] :
85  intf.mImportAttrsDpsim) {
86  mModifiedAttributes.push_back(attr);
87  }
88  }
89 
90  void execute(Real time, Int timeStepCount) override;
91 
92  private:
94  };
95 
96  class PostStep : public CPS::Task {
97  public:
98  explicit PostStep(InterfaceVillasQueueless &intf)
99  : Task(intf.mName + ".Write"), mIntf(intf) {
100  for (const auto &[attr, _seqId] : intf.mExportAttrsDpsim) {
101  mAttributeDependencies.push_back(attr);
102  }
103  mModifiedAttributes.push_back(Scheduler::external);
104  }
105 
106  void execute(Real time, Int timeStepCount) override;
107 
108  private:
110  };
111 };
112 } // namespace DPsim
Tasks to be defined by every component.
Definition: Task.h:25
Interface type that can be used to import and export simulation attributes over any node type support...