Skip to content

Commit

Permalink
use the DataLogger for trace logging in DP_Ph1_Capacitor
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Schroeder <[email protected]>
  • Loading branch information
JTS22 committed Jul 28, 2023
1 parent 88be7bd commit f66fe17
Show file tree
Hide file tree
Showing 134 changed files with 370 additions and 317 deletions.
2 changes: 1 addition & 1 deletion docs/hugo/content/en/docs/Development/Guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This is a summary of general guidelines for the development of DPsim.

## Logging

There are two ways to log information in DPsim: The `CPS::Logger` (called the **Text Logger**), which can write to `.log` files and to the CLI, as well as the `DPsim::DataLogger` (called the **Data Logger**), which can write to `.csv` files.
There are two ways to log information in DPsim: The `CPS::Logger` (called the **Text Logger**), which can write to `.log` files and to the CLI, as well as the `CPS::DataLogger` (called the **Data Logger**), which can write to `.csv` files.

### Log Levels
The log levels that can be used with the Text Logger are as follows:
Expand Down
14 changes: 7 additions & 7 deletions docs/hugo/content/en/docs/Overview/Attributes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ In general, attributes are instances of the `Attribute<T>` class, but they are u

Through the template parameter `T` of the `Attribute<T>` class, attributes can have different value types, most commonly `Real`, `Complex`, `Matrix`, or `MatrixComp`. Additionally, attributes can fall into one of two categories:
**Static** attributes have a fixed value which can only be changed explicitly through the attribute's `set`-method or through a mutable reference obtained through `get`.
**Dynamic** attributes on the other hand can dynamically re-compute their value from other attributes every time they are read. This can for example be used to create a scalar attribute of type `Real` whose value always contains the magnitude of another, different attribute of type `Complex`.
**Dynamic** attributes on the other hand can dynamically re-compute their value from other attributes every time they are read. This can for example be used to create a scalar attribute of type `Real` whose value always contains the magnitude of another, different attribute of type `Complex`.

Any simulation component or class which inherits from `IdentifiedObject` contains an instance of an `AttributeList`.
This list can be used to store all the attributes present in this component and later access them via a `String` instead of having to use the member variable directly.
For reasons of code clarity and runtime safety, the member variables should still be used whenever possible.
For reasons of code clarity and runtime safety, the member variables should still be used whenever possible.

## Creating and Storing Attributes
Normally, a new attribute is created by using the `create` or `createDynamic` method of an `AttributeList` object.
These two methods will create a new attribute of the given type and insert it into the `AttributeList` under the given name. After the name, `create` can take an additional parameter of type `T` which will be used as the initial value for this attribute.
Afterwards, a pointer to the attribute is returned which can then be stored in a component's member variable. Usually this is done in the
component's constructor in an initialization list:

```cpp
```cpp
/// Component class Base::Ph1::PiLine

public:
Expand Down Expand Up @@ -53,7 +53,7 @@ Simulation::Simulation(String name, Logger::Level logLevel) :
mTimeStep(AttributeStatic<Real>::make(0.001)),
mSplitSubnets(AttributeStatic<Bool>::make(true)),
mSteadyStateInit(AttributeStatic<Bool>::make(false)),
//...
//...
{
// ...
}
Expand Down Expand Up @@ -83,7 +83,7 @@ Real read3 = **attr; //read3 = 0.003
```
## Working with Dynamic Attributes
In general, dynamic attributes can be accessed via the same `get` and `set`-methods described above for static attributes. However,
In general, dynamic attributes can be accessed via the same `get` and `set`-methods described above for static attributes. However,
dynamic attributes can additionally have **dependencies** on other attributes which affect the behavior of these methods.
Usually, this is used to dynamically compute the attribute's value from the value of another attribute. In the simplest case, a dynamic
attribute can be set to **reference** another (static or dynamic) attribute using the `setReference`-method. After this method has been called,
Expand Down Expand Up @@ -150,7 +150,7 @@ required attribute pointer, one can either directly access the public member var
auto r1 = DP::Ph1::Resistor::make("r_1");
r1->setParameters(5);

auto logger = DataLogger::make("simName");
auto logger = CPS::DataLogger::make("simName");
// Access the attribute through the member variable
logger->logAttribute("i12", r1->mIntfCurrent);

Expand Down Expand Up @@ -187,4 +187,4 @@ void DP::Ph1::Inductor::mnaAddPostStepDependencies(
}
```
Here, the MNA post step depends on the solution vector of the system, `leftVector`, and modifies `mIntfVoltage` and `mIntfCurrent`.
Therefore, this task needs to be scheduled after the system solution that computes `leftVector` and before tasks that require the voltage and current interface vectors of the inductance, e.g. the task logging these values.
Therefore, this task needs to be scheduled after the system solution that computes `leftVector` and before tasks that require the voltage and current interface vectors of the inductance, e.g. the task logging these values.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
#include <iostream>
#include <fstream>

#include <dpsim/Definitions.h>
#include <dpsim/Scheduler.h>
#include <dpsim-models/Filesystem.h>
#include <dpsim-models/PtrFactory.h>
#include <dpsim-models/Attribute.h>
#include <dpsim-models/SimNode.h>
#include <dpsim-models/Task.h>

namespace DPsim {
namespace CPS {

class DataLogger : public SharedFactory<DataLogger> {

Expand All @@ -35,17 +33,18 @@ namespace DPsim {

std::map<String, CPS::AttributeBase::Ptr> mAttributes;

void logDataLine(Real time, Real data);
void logDataLine(Real time, const Matrix& data);
void logDataLine(Real time, const MatrixComp& data);

public:
typedef std::shared_ptr<DataLogger> Ptr;
typedef std::vector<DataLogger::Ptr> List;

DataLogger(Bool enabled = true);
DataLogger(String name, Bool enabled = true, UInt downsampling = 1);

void logDataLine(Real time, Real data);
void logDataLine(Real time, std::vector<Real> data);
void logDataLine(Real time, const Matrix& data);
void logDataLine(Real time, const MatrixComp& data);

void open();
void close();
void reopen() {
Expand Down Expand Up @@ -78,7 +77,7 @@ namespace DPsim {
for (auto attr : logger.mAttributes) {
mAttributeDependencies.push_back(attr.second);
}
mModifiedAttributes.push_back(Scheduler::external);
mModifiedAttributes.push_back(nullptr); //nullptr = Scheduler::external, but this cannot be used because the scheduler is not linked yet.
}

void execute(Real time, Int timeStepCount);
Expand Down
3 changes: 3 additions & 0 deletions dpsim-models/include/dpsim-models/MNASimPowerComp.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace CPS {
Bool mHasPreStep;
Bool mHasPostStep;

protected:
Real mSimulationTime;

public:
using Type = VarType;
using Ptr = std::shared_ptr<MNASimPowerComp<VarType>>;
Expand Down
16 changes: 13 additions & 3 deletions dpsim-models/include/dpsim-models/TopologicalPowerComp.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <memory>

#include <dpsim-models/Logger.h>
#include <dpsim-models/DataLogger.h>
#include <dpsim-models/IdentifiedObject.h>
#include <dpsim-models/TopologicalTerminal.h>
#include <dpsim-models/TopologicalNode.h>
Expand All @@ -33,6 +34,9 @@ namespace CPS {
Logger::Log mSLog;
/// Component logger control for internal variables
Logger::Level mLogLevel;
// Data logger
DataLogger::Ptr mDataLogger;

/// Determine state of the simulation, e.g. to implement
/// special behavior for components during initialization
Behaviour mBehaviour = Behaviour::MNASimulation;
Expand All @@ -50,13 +54,19 @@ namespace CPS {
* std::max(Logger::Level::info, logLevel). But because of excessive
* logging to Level::info that is currently infeasible. */
mSLog(Logger::get(Logger::LoggerType::COMPONENT, name, logLevel, std::max(Logger::Level::warn, logLevel))),
mLogLevel(logLevel) { }
mLogLevel(logLevel) {
mDataLogger = DataLogger::make(name + "_trace", logLevel == Logger::Level::trace);
}

/// Basic constructor that takes name and log level and sets the UID to name as well
TopologicalPowerComp(String name, Logger::Level logLevel = Logger::Level::off)
: TopologicalPowerComp(name, name, logLevel) { }
/// Destructor - does not do anything
virtual ~TopologicalPowerComp() { }
/// Destructor
virtual ~TopologicalPowerComp() {
if (mDataLogger) {
mDataLogger->close();
}
}

/// Returns nodes connected to this component
virtual TopologicalNode::List topologicalNodes() = 0;
Expand Down
1 change: 1 addition & 0 deletions dpsim-models/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_library(dpsim-models STATIC
Logger.cpp
DataLogger.cpp
MathUtils.cpp
Attribute.cpp
TopologicalNode.cpp
Expand Down
26 changes: 25 additions & 1 deletion dpsim-models/src/DP/DP_Ph1_Capacitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ void DP::Ph1::Capacitor::mnaCompInitialize(Real omega, Real timeStep, Attribute<
(**mIntfCurrent)(0, freq) = mEquivCond(freq,0) * (**mIntfVoltage)(0,freq) + mEquivCurrent(freq,0);
}

#if defined(DEBUG_BUILD)
std::vector<String> columns;

for (UInt freq = 0; freq < mNumFreqs; freq++) {
columns.push_back("voltage_abs_" + freq);
columns.push_back("voltage_angle_" + freq);
}

mDataLogger->open();
mDataLogger->setColumnNames(columns);
#endif

SPDLOG_LOGGER_DEBUG(mSLog,
"\n--- MNA initialization ---"
"\nInitial voltage {:s}"
Expand Down Expand Up @@ -240,15 +252,27 @@ void DP::Ph1::Capacitor::MnaPostStepHarm::execute(Real time, Int timeStepCount)

void DP::Ph1::Capacitor::mnaCompUpdateVoltage(const Matrix& leftVector) {
// v1 - v0
#if defined(DEBUG_BUILD)
std::vector<Real> logData;
#endif

for (UInt freq = 0; freq < mNumFreqs; freq++) {
(**mIntfVoltage)(0,freq) = 0;
if (terminalNotGrounded(1))
(**mIntfVoltage)(0,freq) = Math::complexFromVectorElement(leftVector, matrixNodeIndex(1), mNumFreqs, freq);
if (terminalNotGrounded(0))
(**mIntfVoltage)(0,freq) = (**mIntfVoltage)(0,freq) - Math::complexFromVectorElement(leftVector, matrixNodeIndex(0), mNumFreqs, freq);

SPDLOG_LOGGER_TRACE(mSLog, "Voltage {:e}<{:e}", std::abs((**mIntfVoltage)(0,freq)), std::arg((**mIntfVoltage)(0,freq)));
#if defined(DEBUG_BUILD)
logData.push_back(std::abs((**mIntfVoltage)(0,freq)));
logData.push_back(std::arg((**mIntfVoltage)(0,freq)));
#endif
}

#if defined(DEBUG_BUILD)
mDataLogger->logDataLine(mSimulationTime, logData);
#endif

}

void DP::Ph1::Capacitor::mnaCompUpdateVoltageHarm(const Matrix& leftVector, Int freqIdx) {
Expand Down
21 changes: 18 additions & 3 deletions dpsim/src/DataLogger.cpp → dpsim-models/src/DataLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

#include <iomanip>

#include <dpsim/DataLogger.h>
#include <dpsim-models/DataLogger.h>
#include <dpsim-models/Logger.h>

using namespace DPsim;
using namespace CPS;

DataLogger::DataLogger(Bool enabled) :
mLogFile(),
Expand All @@ -34,6 +34,9 @@ DataLogger::DataLogger(String name, Bool enabled, UInt downsampling) :
}

void DataLogger::open() {
if (!mEnabled) {
return;
}
mLogFile = std::ofstream(mFilename, std::ios_base::out|std::ios_base::trunc);
if (!mLogFile.is_open()) {
// TODO: replace by exception
Expand All @@ -43,7 +46,9 @@ void DataLogger::open() {
}

void DataLogger::close() {
mLogFile.close();
if (mLogFile) {
mLogFile.close();
}
}

void DataLogger::setColumnNames(std::vector<String> names) {
Expand All @@ -65,6 +70,16 @@ void DataLogger::logDataLine(Real time, Real data) {
mLogFile << '\n';
}

void DataLogger::logDataLine(Real time, std::vector<Real> data) {
if (!mEnabled)
return;

mLogFile << std::scientific << std::right << std::setw(14) << time;
for (const auto& it : data)
mLogFile << ", " << std::right << std::setw(13) << it;
mLogFile << '\n';
}

void DataLogger::logDataLine(Real time, const Matrix& data) {
if (!mEnabled)
return;
Expand Down
1 change: 1 addition & 0 deletions dpsim-models/src/MNASimPowerComp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void MNASimPowerComp<VarType>::mnaUpdateCurrent(const Matrix& leftVector) {

template<typename VarType>
void MNASimPowerComp<VarType>::mnaPreStep(Real time, Int timeStepCount) {
mSimulationTime = time;
this->mnaCompPreStep(time, timeStepCount);
};

Expand Down
4 changes: 2 additions & 2 deletions dpsim-villas/examples/cxx/FileExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int main(int argc, char* argv[]) {
sim.setSystem(sys);
sim.setTimeStep(timeStep);
sim.setFinalTime(10.0);

std::string fileConfig = R"STRING({
"type": "file",
"uri": "logs/output.csv",
Expand All @@ -67,7 +67,7 @@ int main(int argc, char* argv[]) {
sim.addInterface(intf);

// Logger
auto logger = DataLogger::make(simName);
auto logger = CPS::DataLogger::make(simName);
logger->logAttribute("v1", n1->mVoltage);
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("v3", n3->mVoltage);
Expand Down
4 changes: 2 additions & 2 deletions dpsim-villas/examples/cxx/MqttExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int main(int argc, char* argv[]) {
sim.setSystem(sys);
sim.setTimeStep(timeStep);
sim.setFinalTime(10.0);

std::string mqttConfig = R"STRING({
"type": "mqtt",
"format": "json",
Expand All @@ -71,7 +71,7 @@ int main(int argc, char* argv[]) {
intf->exportAttribute(ll->mIntfCurrent->deriveCoeff<Complex>(0, 0), 1, true, "v_load");

// Logger
auto logger = DataLogger::make(simName);
auto logger = CPS::DataLogger::make(simName);
logger->logAttribute("v1", n1->mVoltage);
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("v3", n3->mVoltage);
Expand Down
4 changes: 2 additions & 2 deletions dpsim-villas/examples/cxx/SharedMemExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int main(int argc, char* argv[]) {
sim.setSystem(sys);
sim.setTimeStep(timeStep);
sim.setFinalTime(2.0);

std::string shmemConfig = R"STRING(
{
"type": "shmem",
Expand All @@ -71,7 +71,7 @@ int main(int argc, char* argv[]) {
sim.addInterface(intf);

// Logger
auto logger = DataLogger::make(simName);
auto logger = CPS::DataLogger::make(simName);
logger->logAttribute("v1", n1->mVoltage);
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("v3", n3->mVoltage);
Expand Down
4 changes: 2 additions & 2 deletions dpsim-villas/examples/cxx/ShmemDistributedDirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int main(int argc, char *argv[]) {
sim.setFinalTime(finalTime);

// Logging
auto logger = DataLogger::make(simName);
auto logger = CPS::DataLogger::make(simName);
logger->logAttribute("v1", n1->mVoltage);
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("r12", r12->mIntfCurrent);
Expand Down Expand Up @@ -117,7 +117,7 @@ int main(int argc, char *argv[]) {
sim.setFinalTime(finalTime);

// Logging
auto logger = DataLogger::make(simName);
auto logger = CPS::DataLogger::make(simName);
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("r02", r02->mIntfCurrent);
logger->logAttribute("vecs", ecs->mIntfVoltage);
Expand Down
2 changes: 1 addition & 1 deletion dpsim-villas/examples/cxx/ShmemDistributedReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int main(int argc, char* argv[]) {
SystemComponentList{ vs1, r12, r02 });

// Logging
auto logger = DataLogger::make(simName);
auto logger = CPS::DataLogger::make(simName);
logger->logAttribute("v1", n1->mVoltage);
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("r12", r12->mIntfCurrent);
Expand Down
2 changes: 1 addition & 1 deletion dpsim-villas/examples/cxx/ShmemExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int main(int argc, char* argv[]) {
sim.addInterface(std::shared_ptr<Interface>(&intf));

// Logger
auto logger = DataLogger::make(simName);
auto logger = CPS::DataLogger::make(simName);
logger->logAttribute("v1", n1->mVoltage);
logger->logAttribute("v2", n2->mVoltage);
logger->logAttribute("v3", n3->mVoltage);
Expand Down
2 changes: 1 addition & 1 deletion dpsim-villas/examples/cxx/Shmem_WSCC-9bus_Ctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int main(int argc, char *argv[]) {

InterfaceShmem intf("/dpsim1-villas", "/villas-dpsim1", nullptr, false);

auto logger = DataLogger::make(simName);
auto logger = CPS::DataLogger::make(simName);

// Register exportable node voltages
UInt o = 0;
Expand Down
2 changes: 1 addition & 1 deletion dpsim/examples/cxx/CIM/CIGRE_MV_PowerFlowTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int main(int argc, char** argv){
CIM::Reader reader(Logger::Level::info, Logger::Level::off, Logger::Level::info);
SystemTopology system = reader.loadCIM(system_freq, filenames, CPS::Domain::SP);

auto logger = DPsim::DataLogger::make(simName);
auto logger = CPS::DataLogger::make(simName);
for (auto node : system.mNodes)
{
logger->logAttribute(node->name() + ".V", node->attribute("v"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int main(int argc, char** argv){
CSVReader csvreader(loadProfilePath, assignList, Logger::Level::info);
csvreader.assignLoadProfile(system, time_begin, time_step, time_end, CSVReader::Mode::MANUAL);

auto logger = DPsim::DataLogger::make(simName);
auto logger = CPS::DataLogger::make(simName);
for (auto node : system.mNodes)
{
logger->logAttribute(node->name(), node->attribute("v"));
Expand Down
Loading

0 comments on commit f66fe17

Please sign in to comment.