DPsim
DataLogger.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 <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/Definitions.h>
21 #include <dpsim/Scheduler.h>
22 
23 namespace DPsim {
24 
25 class DataLogger : public SharedFactory<DataLogger> {
26 
27 protected:
28  std::ofstream mLogFile;
29  String mName;
30  Bool mEnabled;
31  UInt mDownsampling;
32  fs::path mFilename;
33 
34  std::map<String, CPS::AttributeBase::Ptr> mAttributes;
35 
36  void logDataLine(Real time, Real data);
37  void logDataLine(Real time, const Matrix &data);
38  void logDataLine(Real time, const MatrixComp &data);
39 
40 public:
41  typedef std::shared_ptr<DataLogger> Ptr;
42  typedef std::vector<DataLogger::Ptr> List;
43 
44  DataLogger(Bool enabled = true);
45  DataLogger(String name, Bool enabled = true, UInt downsampling = 1);
46 
47  void open();
48  void close();
49  void reopen() {
50  close();
51  open();
52  }
53 
54  void logPhasorNodeValues(Real time, const Matrix &data, Int freqNum = 1);
55  void logEMTNodeValues(Real time, const Matrix &data);
56 
57  void setColumnNames(std::vector<String> names);
58 
59  void logAttribute(const String &name, CPS::AttributeBase::Ptr attr,
60  UInt rowsMax = 0, UInt colsMax = 0);
61 
63  void logAttribute(const std::vector<String> &name,
65 
66  void log(Real time, Int timeStepCount);
67 
68  CPS::Task::Ptr getTask();
69 
70  class Step : public CPS::Task {
71  public:
72  Step(DataLogger &logger) : Task(logger.mName + ".Write"), mLogger(logger) {
73  for (auto attr : logger.mAttributes) {
74  mAttributeDependencies.push_back(attr.second);
75  }
76  mModifiedAttributes.push_back(Scheduler::external);
77  }
78 
79  void execute(Real time, Int timeStepCount);
80 
81  private:
82  DataLogger &mLogger;
83  };
84 };
85 } // namespace DPsim
Tasks to be defined by every component.
Definition: Task.h:25