1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/lib/hooks/ebm.cpp

118 lines
2.2 KiB
C++
Raw Normal View History

/* Energy-based Metric hook.
2019-01-30 01:53:52 +01:00
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
2019-01-30 01:53:52 +01:00
#include <vector>
2019-03-26 15:33:47 +01:00
#include <villas/hook.hpp>
#include <villas/sample.hpp>
#include <villas/timing.hpp>
2019-01-30 01:53:52 +01:00
2019-03-26 15:33:47 +01:00
namespace villas {
namespace node {
class EBMHook : public Hook {
protected:
std::vector<std::pair<int, int>> phases;
double energy;
2019-03-26 15:33:47 +01:00
struct Sample *last;
2019-03-26 15:33:47 +01:00
public:
using Hook::Hook;
virtual
void parse(json_t *json)
{
int ret;
size_t i;
json_error_t err;
json_t *json_phases, *json_phase;
2021-02-16 14:15:14 +01:00
Hook::parse(json);
2020-08-28 19:49:36 +02:00
2021-02-16 14:15:14 +01:00
ret = json_unpack_ex(json, &err, 0, "{ s: o }",
"phases", &json_phases
);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-hook-ebm");
if (!json_is_array(json_phases))
throw ConfigError(json_phases, "node-config-hook-ebm-phases");
json_array_foreach(json_phases, i, json_phase) {
int voltage, current;
ret = json_unpack_ex(json_phase, &err, 0, "[ i, i ]",
&voltage, &current
);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-hook-ebm-phases");
phases.emplace_back(voltage, current);
}
2019-04-23 11:19:18 +02:00
2019-06-23 16:13:23 +02:00
state = State::PARSED;
}
virtual
void start()
{
2019-06-23 16:13:23 +02:00
assert(state == State::PREPARED);
energy = 0;
last = nullptr;
2019-06-23 16:13:23 +02:00
state = State::STARTED;
}
virtual
void periodic()
{
2019-06-23 16:13:23 +02:00
assert(state == State::STARTED);
2021-02-16 14:15:14 +01:00
logger->info("Energy: {}", energy);
}
virtual
Hook::Reason process(struct Sample *smp)
2019-03-26 15:33:47 +01:00
{
double P, P_last, dt;
2019-06-23 16:13:23 +02:00
assert(state == State::STARTED);
2019-03-26 15:33:47 +01:00
if (last) {
for (auto phase : phases) {
// Trapazoidal rule
dt = time_delta(&last->ts.origin, &smp->ts.origin);
P = smp->data[phase.first].f * smp->data[phase.second].f;
P_last = last->data[phase.first].f * last->data[phase.second].f;
energy += dt * (P_last + P) / 2.0;
}
sample_decref(last);
}
sample_incref(smp);
last = smp;
2019-06-23 16:13:23 +02:00
return Reason::OK;
2019-01-30 01:53:52 +01:00
}
};
// Register hook
static char n[] = "ebm";
static char d[] = "Energy-based Metric";
static HookPlugin<EBMHook, n, d, (int) Hook::Flags::PATH | (int) Hook::Flags::NODE_READ | (int) Hook::Flags::NODE_WRITE> p;
2019-03-26 15:33:47 +01:00
} // namespace node
} // namespace villas