DPsim
DataLogger.h
1 /* Copyright 2017-2024 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 <fstream>
12 #include <iostream>
13 #include <map>
14 
15 #include <dpsim-models/Attribute.h>
16 #include <dpsim-models/Filesystem.h>
17 #include <dpsim-models/PtrFactory.h>
18 #include <dpsim-models/SimNode.h>
19 #include <dpsim-models/Task.h>
20 #include <dpsim/DataLoggerInterface.h>
21 #include <dpsim/Definitions.h>
22 #include <dpsim/Scheduler.h>
23 
24 namespace DPsim {
25 
26 class DataLogger : public DataLoggerInterface, public SharedFactory<DataLogger> {
27 
28 protected:
29  std::ofstream mLogFile;
30  String mName;
31  Bool mEnabled;
32  UInt mDownsampling;
33  fs::path mFilename;
34 
35  virtual void logDataLine(Real time, Real data);
36  virtual void logDataLine(Real time, const Matrix &data);
37  virtual void logDataLine(Real time, const MatrixComp &data);
38 
39 public:
40  typedef std::shared_ptr<DataLogger> Ptr;
41 
42  DataLogger(Bool enabled = true);
43  DataLogger(String name, Bool enabled = true, UInt downsampling = 1);
44  virtual ~DataLogger(){};
45 
46  virtual void start() override;
47  virtual void stop() override;
48 
49  virtual void setColumnNames(std::vector<String> names);
50  void logPhasorNodeValues(Real time, const Matrix &data, Int freqNum = 1);
51  void logEMTNodeValues(Real time, const Matrix &data);
52 
53  virtual void log(Real time, Int timeStepCount) override;
54 
55  virtual CPS::Task::Ptr getTask() override;
56 
57  class Step : public CPS::Task {
58  public:
59  Step(DataLogger &logger) : Task(logger.mName + ".Write"), mLogger(logger) {
60  for (auto attr : logger.mAttributes) {
61  mAttributeDependencies.push_back(attr.second);
62  }
63  mModifiedAttributes.push_back(Scheduler::external);
64  }
65 
66  void execute(Real time, Int timeStepCount);
67 
68  private:
69  DataLogger &mLogger;
70  };
71 };
72 } // namespace DPsim
Tasks to be defined by every component.
Definition: Task.h:25