Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shapper simulator #923

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions divert_sim/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,40 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "divert_sim: data_shaper.csv",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/divert_sim",
"args": [
"-c", "data/test_shapper.json",
"-l", "1",
"--sep", "\\;",
"<", "${workspaceFolder}/data/data_shaper.csv"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build",
"miDebuggerPath": "/usr/bin/gdb"
},
{
"name": "divert_sim: day1.csv",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/divert_sim",
"args": [
"-c", "data/test_config.json",
"-s", "1",
"<", "${workspaceFolder}/data/day1.csv"
],
"stopAtEntry": false,
Expand Down Expand Up @@ -58,6 +85,7 @@
"request": "launch",
"program": "${workspaceFolder}/divert_sim",
"args": [
"-s", "1",
"-v", "2",
"<", "${workspaceFolder}/data/solar-vrms.csv"
],
Expand All @@ -82,6 +110,7 @@
"request": "launch",
"program": "${workspaceFolder}/divert_sim",
"args": [
"-s", "1",
"--sep", "\\;", "--kw",
"<", "${workspaceFolder}/data/Energy_and_Power_Day_2020-03-22.csv"
],
Expand Down
5 changes: 2 additions & 3 deletions divert_sim/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ CPPFLAGS := \
-D ENERGY_METER_STORE_STATE=0 \
-D ARDUINOJSON_ENABLE_PROGMEM=0 \
-D ENABLE_CONFIG_V1_IMPORT=0 \
-D ENABLE_CONFIG_CHANGE_NOTIFICATION=0
# \
-D ENABLE_CONFIG_CHANGE_NOTIFICATION=0 \
# -D ENABLE_DEBUG \
# -D SERIAL_OUTFD=STDERR_FILENO
# -D ENABLE_DEBUG_MICRO_TASK \
# -D ENABLE_DEBUG_DIVERT \
# -D ENABLE_DEBUG_INPUT_FILTER \
# -D ENABLE_DEBUG_EVSE_MAN \
Expand Down
3 changes: 3 additions & 0 deletions divert_sim/data/test_shapper.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"current_shaper_max_pwr": 3000
}
54 changes: 37 additions & 17 deletions divert_sim/divert_sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "event.h"
#include "event_log.h"
#include "manual.h"
#include "current_shaper.h"

#include "parser.hpp"
#include "cxxopts.hpp"
Expand All @@ -33,8 +34,9 @@ extern double smoothed_available_current;

int date_col = 0;
int grid_ie_col = -1;
int solar_col = 1;
int voltage_col = 1;
int solar_col = -1;
int voltage_col = -1;
int live_pwr_col = -1;

time_t simulated_time = 0;
time_t last_time = 0;
Expand All @@ -48,17 +50,19 @@ time_t parse_date(const char *dateStr)
if(6 != sscanf(dateStr, "%d-%d-%dT%d:%d:%dZ", &y, &M, &d, &h, &m, &s)) {
if(6 != sscanf(dateStr, "%d-%d-%dT%d:%d:%d+00:00", &y, &M, &d, &h, &m, &s)) {
if(6 != sscanf(dateStr, "%d-%d-%d %d:%d:%d", &y, &M, &d, &h, &m, &s)) {
if(3 != sscanf(dateStr, "%d:%d %s", &h, &m, ampm)) {
if(1 == sscanf(dateStr, "%d", &s)) {
return s;
}
} else {
y = 2020; M = 1; d = 1; s = 0;
if(12 == h) {
h -= 12;
}
if('P' == ampm[0]) {
h += 12;
if(6 != sscanf(dateStr, "%d/%d/%d %d:%d:%d", &d, &M, &y, &h, &m, &s)) {
if(3 != sscanf(dateStr, "%d:%d %s", &h, &m, ampm)) {
if(1 == sscanf(dateStr, "%d", &s)) {
return s;
}
} else {
y = 2020; M = 1; d = 1; s = 0;
if(12 == h) {
h -= 12;
}
if('P' == ampm[0]) {
h += 12;
}
}
}
}
Expand Down Expand Up @@ -112,6 +116,7 @@ int main(int argc, char** argv)
("d,date", "The date column", cxxopts::value<int>(date_col), "N")
("s,solar", "The solar column", cxxopts::value<int>(solar_col), "N")
("g,gridie", "The Grid IE column", cxxopts::value<int>(grid_ie_col), "N")
("l,livepwr", "The live power column", cxxopts::value<int>(live_pwr_col), "N")
("c,config", "Config options, either a file name or JSON", cxxopts::value<std::string>(config))
("v,voltage", "The Voltage column if < 50, else the fixed voltage", cxxopts::value<int>(voltage_arg), "N")
("kw", "values are KW")
Expand All @@ -128,6 +133,8 @@ int main(int argc, char** argv)
exit(0);
}

EpoxyTest::set_millis(millis());

fs::EpoxyFS.begin();
if(result.count("config-load") > 0) {
config_load_settings();
Expand Down Expand Up @@ -163,7 +170,7 @@ int main(int argc, char** argv)
kw = result.count("kw") > 0;

divert_type = grid_ie_col >= 0 ? 1 : 0;

if(voltage_arg >= 0) {
if(voltage_arg < 50) {
voltage_col = voltage_arg;
Expand All @@ -177,19 +184,25 @@ int main(int argc, char** argv)

evse.begin();
divert.begin();
shaper.begin(evse);

// Initialise the EVSE Manager
while (!evse.isConnected()) {
MicroTask.update();
}

divert.setMode(DivertMode::Eco);
if(solar_col >= 0 || grid_ie_col >= 0) {
divert.setMode(DivertMode::Eco);
}
if(live_pwr_col >= 0) {
shaper.setState(true);
}

CsvParser parser(std::cin);
parser.delimiter(sep.c_str()[0]);
int row_number = 0;

std::cout << "Date,Solar,Grid IE,Pilot,Charge Power,Min Charge Power,State,Smoothed Available" << std::endl;
std::cout << "Date,Solar,Grid IE,Pilot,Charge Power,Min Charge Power,State,Smoothed Available,Live Power,Smoother Live Power,Shapper Max Power" << std::endl;
for (auto& row : parser)
{
try
Expand All @@ -206,6 +219,8 @@ int main(int argc, char** argv)
grid_ie = get_watt(val.c_str());
} else if (solar_col == col) {
solar = get_watt(val.c_str());
} else if (live_pwr_col == col) {
shaper.setLivePwr(get_watt(val.c_str()));
} else if (voltage_col == col) {
voltage = stoi(field);
}
Expand All @@ -218,6 +233,7 @@ int main(int argc, char** argv)
int delta = simulated_time - last_time;
if(delta > 0) {
EpoxyTest::add_millis(delta * 1000);
DBUGVAR(millis());
}
}
last_time = simulated_time;
Expand All @@ -237,7 +253,11 @@ int main(int argc, char** argv)

double smoothed = divert.smoothedAvailableCurrent() * voltage;

std::cout << buffer << "," << solar << "," << grid_ie << "," << ev_pilot << "," << ev_watt << "," << min_ev_watt << "," << state << "," << smoothed << std::endl;
int live_power = shaper.getLivePwr();
int smoothed_live_pwr = shaper.getLivePwr();
int shapper_max_power = shaper.getMaxPwr();

std::cout << buffer << "," << solar << "," << grid_ie << "," << ev_pilot << "," << ev_watt << "," << min_ev_watt << "," << state << "," << smoothed << "," << live_power << "," << smoothed_live_pwr << "," << shapper_max_power << std::endl;
}
catch(const std::invalid_argument& e)
{
Expand Down
14 changes: 13 additions & 1 deletion divert_sim/interactive.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
"divert_PV_ratio": parseFloat($("#divert_PV_ratio").val()),
"divert_attack_smoothing_time": parseInt($("#divert_attack_smoothing_time").val()),
"divert_decay_smoothing_time": parseInt($("#divert_decay_smoothing_time").val()),
"divert_min_charge_time": parseInt($("#divert_min_charge_time").val())
"divert_min_charge_time": parseInt($("#divert_min_charge_time").val()),
"current_shaper_max_pwr": parseInt($("#current_shaper_max_pwr").val()),
"current_shaper_smoothing_time": parseInt($("#current_shaper_smoothing_time").val()),
"current_shaper_min_pause_time": parseInt($("#current_shaper_min_pause_time").val()),
"current_shaper_data_maxinterval": parseInt($("#current_shaper_data_maxinterval").val())
}), () => {
init_summary(profiles);
loadSummary("output/summary_master.csv", () => {
Expand Down Expand Up @@ -50,6 +54,14 @@ <h1>OpenEVSE Solar Divert Simulations</h1>
<input type="text" id="divert_decay_smoothing_time" value="200"><br/>
<label for="divert_min_charge_time">divert_min_charge_time</label>
<input type="text" id="divert_min_charge_time" value="600"><br/>
<label for="current_shaper_max_pwr">current_shaper_max_pwr</label>
<input type="text" id="current_shaper_max_pwr" value="6000"><br/>
<label for="current_shaper_smoothing_time">current_shaper_smoothing_time</label>
<input type="text" id="current_shaper_smoothing_time" value="60"><br/>
<label for="current_shaper_min_pause_time">current_shaper_min_pause_time</label>
<input type="text" id="current_shaper_min_pause_time" value="300"><br/>
<label for="current_shaper_data_maxinterval">current_shaper_data_maxinterval</label>
<input type="text" id="current_shaper_data_maxinterval" value="120"><br/>

<button onclick="run_simulation()">Run Simulation</button>

Expand Down
8 changes: 7 additions & 1 deletion divert_sim/run_simulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def run_simulation(dataset: str,
output: str,
config: bool = False, grid_ie_col: bool = False,
solar_col: bool = False, voltage_col: bool = False,
separator: str = ',', is_kw: bool = False) -> None:
separator: str = ',', is_kw: bool = False,
live_power_col: bool = False) -> None:
"""Run the divert_sim process on the given dataset and return the results"""
line_number = 0

Expand Down Expand Up @@ -73,6 +74,9 @@ def run_simulation(dataset: str,
if solar_col:
command.append("-s")
command.append(str(solar_col))
if live_power_col:
command.append("-l")
command.append(str(live_power_col))
if voltage_col:
command.append("-v")
command.append(str(voltage_col))
Expand All @@ -82,6 +86,8 @@ def run_simulation(dataset: str,
if is_kw:
command.append("--kw")

print(f"cat {input_data.name} | {' '.join(command)}")

divert_process = Popen(command, stdin=input_data, stdout=PIPE,
stderr=PIPE, universal_newlines=True)
while True:
Expand Down
26 changes: 14 additions & 12 deletions divert_sim/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,43 @@ def do_POST(self):
# Run the simulation
setup_summary('_interactive')
run_simulation('almostperfect', 'almostperfect_interactive',
config=json.dumps(config))
solar_col=1, config=json.dumps(config))

run_simulation('CloudyMorning', 'CloudyMorning_interactive',
config=json.dumps(config))
solar_col=1, config=json.dumps(config))

run_simulation('day1', 'day1_interactive',
config=json.dumps(config))
solar_col=1, config=json.dumps(config))

run_simulation('day2', 'day2_interactive',
config=json.dumps(config))
solar_col=1, config=json.dumps(config))

run_simulation('day3', 'day3_interactive',
config=json.dumps(config))
solar_col=1, config=json.dumps(config))

run_simulation('day1_grid_ie', 'day1_grid_ie_interactive',
grid_ie_col=2, config=json.dumps(config))
solar_col=1, grid_ie_col=2, config=json.dumps(config))

run_simulation('day2_grid_ie', 'day2_grid_ie_interactive',
grid_ie_col=2, config=json.dumps(config))
solar_col=1, grid_ie_col=2, config=json.dumps(config))

run_simulation('day3_grid_ie', 'day3_grid_ie_interactive',
grid_ie_col=2, config=json.dumps(config))
solar_col=1, grid_ie_col=2, config=json.dumps(config))

run_simulation('solar-vrms', 'solar-vrms_interactive',
voltage_col=2, config=json.dumps(config))
solar_col=1, voltage_col=2, config=json.dumps(config))

run_simulation('Energy_and_Power_Day_2020-03-22', 'Energy_and_Power_Day_2020-03-22_interactive',
separator=';', is_kw=True, config=json.dumps(config))
solar_col=1, separator=';', is_kw=True, config=json.dumps(config))

run_simulation('Energy_and_Power_Day_2020-03-31', 'Energy_and_Power_Day_2020-03-31_interactive',
separator=';', is_kw=True, config=json.dumps(config))
solar_col=1, separator=';', is_kw=True, config=json.dumps(config))

run_simulation('Energy_and_Power_Day_2020-04-01', 'Energy_and_Power_Day_2020-04-01_interactive',
separator=';', is_kw=True, config=json.dumps(config))
solar_col=1, separator=';', is_kw=True, config=json.dumps(config))

run_simulation('data_shaper', 'data_shaper_interactive',
live_power_col=1, separator=';', config=json.dumps(config))

self.wfile.write("OK".encode('utf-8'))

Expand Down
6 changes: 6 additions & 0 deletions divert_sim/simulations.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
width: 100%;
}

.shapper
{
height: 370px;
width: 100%;
}

.gridie
{
height: 740px;
Expand Down
Loading
Loading