Skip to content

Commit

Permalink
Merge pull request #104 from bigladder/feature/ensure-network-is-DAG
Browse files Browse the repository at this point in the history
Add check to ensure network is a directed acyclical graph
  • Loading branch information
michael-okeefe authored Nov 15, 2024
2 parents 255c1e1 + 676bbbd commit 7f7448a
Show file tree
Hide file tree
Showing 13 changed files with 866 additions and 530 deletions.
54 changes: 31 additions & 23 deletions app/erin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "erin/all.h"
#include "compilation_settings.h"

int exit_code = EXIT_SUCCESS;

erin::Log get_standard_log(erin::Logger& logger)
{
using namespace erin;
Expand Down Expand Up @@ -140,7 +142,8 @@ CLI::App* add_run(CLI::App& app)
if (!ifs.good())
{
Log_error(log, "Could not open input file stream on input file");
return EXIT_FAILURE;
exit_code = EXIT_FAILURE;
return;
}
auto name_only = std::filesystem::path(toml_filename).filename();
toml::value data = toml::parse(ifs, name_only.string());
Expand All @@ -152,7 +155,8 @@ CLI::App* add_run(CLI::App& app)
if (!maybe_sim.has_value())
{
Log_error(log, "Simulation returned without value");
return EXIT_FAILURE;
exit_code = EXIT_FAILURE;
return;
}
Simulation s = std::move(maybe_sim.value());
if (verbose)
Expand All @@ -169,7 +173,6 @@ CLI::App* add_run(CLI::App& app)
save_reliability_curves,
verbose,
show_seed);
return EXIT_SUCCESS;
};

subcommand->callback([&]() { run_it(); });
Expand Down Expand Up @@ -199,7 +202,8 @@ CLI::App* add_graph(CLI::App& app)
if (!ifs.good())
{
Log_error(log, "Could not open input file stream on input file");
return EXIT_FAILURE;
exit_code = EXIT_FAILURE;
return;
}
auto name_only = std::filesystem::path(toml_filename).filename();
auto data = toml::parse(ifs, name_only.string());
Expand All @@ -211,7 +215,8 @@ CLI::App* add_graph(CLI::App& app)
if (!maybe_sim.has_value())
{
Log_error(log, "Could not parse sim data from TOML");
return EXIT_FAILURE;
exit_code = EXIT_FAILURE;
return;
}
Simulation s = std::move(maybe_sim.value());
std::string dot_data =
Expand All @@ -221,19 +226,19 @@ CLI::App* add_graph(CLI::App& app)
if (!ofs.good())
{
std::cout << "Could not open output file stream on output file" << std::endl;
return EXIT_FAILURE;
exit_code = EXIT_FAILURE;
return;
}
ofs << dot_data << std::endl;
ofs.close();
return EXIT_SUCCESS;
};

subcommand->callback([&]() { graph(); });

return subcommand;
}

CLI::App* add_checkNetwork(CLI::App& app)
CLI::App* add_check_network(CLI::App& app)
{
auto subcommand = app.add_subcommand("check", "Check network for issues");

Expand All @@ -249,7 +254,8 @@ CLI::App* add_checkNetwork(CLI::App& app)
if (!ifs.good())
{
Log_error(log, "Could not open input file stream on input file");
return EXIT_FAILURE;
exit_code = EXIT_FAILURE;
return;
}
auto name_only = std::filesystem::path(toml_filename).filename();
auto data = toml::parse(ifs, name_only.string());
Expand All @@ -260,7 +266,8 @@ CLI::App* add_checkNetwork(CLI::App& app)
auto maybe_sim = read_from_toml(data, validationInfo, component_tags_in_use, log);
if (!maybe_sim.has_value())
{
return EXIT_FAILURE;
exit_code = EXIT_FAILURE;
return;
}
Simulation s = std::move(maybe_sim.value());
std::vector<std::string> issues = erin::check_network(s.the_model);
Expand All @@ -271,10 +278,10 @@ CLI::App* add_checkNetwork(CLI::App& app)
{
std::cout << issue << std::endl;
}
return EXIT_FAILURE;
exit_code = EXIT_FAILURE;
return;
}
std::cout << "No issues found with network." << std::endl;
return EXIT_SUCCESS;
};

subcommand->callback([&]() { check_network(); });
Expand Down Expand Up @@ -302,7 +309,8 @@ CLI::App* add_update(CLI::App& app)
if (!ifs.good())
{
std::cout << "Could not open input file stream on input file" << std::endl;
return EXIT_FAILURE;
exit_code = EXIT_FAILURE;
return;
}
using namespace erin;
auto name_only = std::filesystem::path(input_filename).filename();
Expand Down Expand Up @@ -472,11 +480,11 @@ CLI::App* add_update(CLI::App& app)
if (!ofs.good())
{
std::cout << "Could not open ouptut file stream for output file" << std::endl;
return EXIT_FAILURE;
exit_code = EXIT_FAILURE;
return;
}
ofs << data;
ofs.close();
return EXIT_SUCCESS;
};

subcommand->callback([&]() { update(); });
Expand Down Expand Up @@ -511,7 +519,8 @@ CLI::App* add_pack_loads(CLI::App& app)
if (!ifs.good())
{
erin::Log_error(log, "Could not open input file stream on input file");
return EXIT_FAILURE;
exit_code = EXIT_FAILURE;
return;
}
auto toml_filename_only = std::filesystem::path(toml_filename).filename();
auto data = toml::parse(ifs, toml_filename_only.string());
Expand All @@ -523,10 +532,11 @@ CLI::App* add_pack_loads(CLI::App& app)
auto maybeLoads = parse_loads(load_table, explicit_validation, file_validation, log);
if (!maybeLoads.has_value())
{
return EXIT_FAILURE;
exit_code = EXIT_FAILURE;
return;
}
std::vector<erin::Load> loads = std::move(maybeLoads.value());
return erin::write_packed_loads(loads, loads_filename);
exit_code = erin::write_packed_loads(loads, loads_filename);
};

subcommand->callback([&]() { pack_loads(); });
Expand All @@ -536,16 +546,14 @@ CLI::App* add_pack_loads(CLI::App& app)

int main(int argc, char** argv)
{
int result = EXIT_SUCCESS;

CLI::App app {"erin"};
app.require_subcommand(0);
app.require_subcommand(0, 1);

add_version(app);
add_limits(app);
add_run(app);
add_graph(app);
add_checkNetwork(app);
add_check_network(app);
add_update(app);
add_pack_loads(app);

Expand All @@ -560,5 +568,5 @@ int main(int argc, char** argv)
std::cout << app.help() << std::endl;
}

return result;
return exit_code;
}
55 changes: 55 additions & 0 deletions docs/examples/ex_bad_network.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[simulation_info]
input_format_version = "0.2"
rate_unit = "kW"
quantity_unit = "kJ"
time_unit = "hours"
max_time = 4
############################################################
[loads.building_electrical]
time_unit = "hours"
rate_unit = "kW"
time_rate_pairs = [
[0.0, 1.0],
[1.0, 10.0],
[3.0, 1.0],
[4.0, 0.0],
]
############################################################
[components.electric_utility]
type = "source"
outflow = "electricity"
[components.electric_bus]
type = "mux"
num_inflows = 2
num_outflows = 1
flow = "electricity"
[components.transformer]
type = "converter"
constant_efficiency = 0.98
inflow = "electricity"
outflow = "electricity"
lossflow = "electricity"
[components.cluster_01_electric]
type = "load"
inflow = "electricity"
loads_by_scenario.blue_sky = "building_electrical"
############################################################
[network]
connections = [
["electric_utility:OUT(0)", "electric_bus:IN(0)", "electricity"],
["electric_bus:OUT(0)", "transformer:IN(0)", "electricity"],
["transformer:OUT(0)", "cluster_01_electric:IN(0)", "electricity"],
["transformer:OUT(1)", "electric_bus:IN(1)", "electricity"],
]
############################################################
[dist.immediately]
type = "fixed"
value = 0
time_unit = "hours"
############################################################
[scenarios.blue_sky]
time_unit = "hours"
occurrence_distribution = "immediately"
duration = 4
max_occurrences = 1

Loading

0 comments on commit 7f7448a

Please sign in to comment.