This repository has been archived on 2018-12-13. You can view files and clone it, but cannot push or open issues or pull requests.
arma-flow/src/executor.cpp

109 lines
4.1 KiB
C++
Raw Normal View History

2018-04-08 15:21:54 +00:00
//
// arma-flow/executor.cpp
//
// @author CismonX
//
2018-04-05 11:01:10 +00:00
#include "executor.hpp"
#include "factory.hpp"
2018-04-08 15:21:54 +00:00
#include "writer.hpp"
2018-04-05 11:01:10 +00:00
namespace flow
{
executor::executor() : factory_(factory::get()) {}
2018-04-05 11:01:10 +00:00
void executor::execute(int argc, char** argv) const
{
2018-05-29 13:19:48 +00:00
// Get components.
auto args = factory_->get_args();
auto input = factory_->get_reader();
auto calc = factory_->get_calc();
auto writer = factory_->get_writer();
2018-04-08 15:21:54 +00:00
// Parse options.
2018-04-05 11:01:10 +00:00
args->parse(argc, argv);
2018-04-08 15:21:54 +00:00
// Read data from file.
2018-04-05 11:01:10 +00:00
std::string path_to_nodes, path_to_edges;
2018-12-13 12:33:02 +00:00
if (!args->input_file_path(path_to_nodes, path_to_edges)) {
2018-04-05 11:01:10 +00:00
args->help();
2018-12-13 12:33:02 +00:00
}
2018-04-05 11:01:10 +00:00
const auto remove = args->remove_first_line();
2018-12-13 12:33:02 +00:00
if (!input->from_csv_file(path_to_nodes, remove)) {
2018-04-08 15:21:54 +00:00
writer::error("Failed to read node data from file.");
2018-12-13 12:33:02 +00:00
}
2018-04-05 11:01:10 +00:00
const auto nodes = input->get_mat();
2018-12-13 12:33:02 +00:00
if (!input->from_csv_file(path_to_edges, remove)) {
2018-04-08 15:21:54 +00:00
writer::error("Failed to read edge data from file.");
2018-12-13 12:33:02 +00:00
}
2018-04-05 11:01:10 +00:00
const auto edges = input->get_mat();
2018-04-08 15:21:54 +00:00
// Get options.
2018-04-05 11:01:10 +00:00
const auto verbose = args->verbose();
2018-04-08 15:21:54 +00:00
auto max = 0U;
2018-12-13 12:33:02 +00:00
if (!args->max_iterations(max) && verbose) {
2018-04-08 15:21:54 +00:00
writer::notice("Max number of iterations not specified. Defaulted to 100.");
2018-12-13 12:33:02 +00:00
}
2018-04-08 15:21:54 +00:00
auto epsilon = 0.0;
2018-12-13 12:33:02 +00:00
if (!args->accuracy(epsilon) && verbose) {
2018-04-08 15:21:54 +00:00
writer::notice("Accuracy not specified. Defaulted to 0.00001.");
2018-12-13 12:33:02 +00:00
}
if (epsilon < 0 || epsilon > 1) {
2018-04-08 15:21:54 +00:00
writer::error("Invalid accuracy.");
2018-12-13 12:33:02 +00:00
}
2018-04-05 11:01:10 +00:00
std::string output_path;
2018-12-13 12:33:02 +00:00
if (!args->output_file_path(output_path) && verbose) {
2018-04-08 15:21:54 +00:00
writer::notice("Output file path not specified. Defaulted to result-*.csv.");
2018-12-13 12:33:02 +00:00
}
writer->set_output_path_prefix(output_path);
unsigned short_circuit_node;
const auto short_circuit = args->short_circuit(short_circuit_node);
std::complex<double> transition_impedance;
2018-12-13 12:33:02 +00:00
if (short_circuit && !args->transition_impedance(transition_impedance) && verbose) {
writer::notice("Transition impedance not specified, Defaulted to 0.");
2018-12-13 12:33:02 +00:00
}
const auto ignore_load = args->ignore_load();
2018-04-08 15:21:54 +00:00
// Initialize calculation.
calc->init(nodes, edges, verbose, epsilon, short_circuit, ignore_load,
short_circuit_node, transition_impedance);
const auto admittance = calc->node_admittance();
writer->to_csv_file("node-admittance-real.csv", admittance.first);
writer->to_csv_file("node-admittance-imag.csv", admittance.second);
2018-04-08 15:21:54 +00:00
calc->iterate_init();
2018-04-05 11:01:10 +00:00
2018-04-08 15:21:54 +00:00
// Do iteration.
unsigned num_iterations;
do {
num_iterations = calc->solve();
if (calc->get_max() <= epsilon) {
writer::println("Finished. Total number of iterations: ", num_iterations);
const auto result = calc->result();
if (verbose) {
writer::println("Result [V, theta(in rads), P, Q]:");
2018-04-08 15:21:54 +00:00
writer::print_mat(result);
}
writer->to_csv_file("flow.csv", result, "V,theta,P,Q");
goto flow_done;
2018-04-08 15:21:54 +00:00
}
} while (num_iterations < max);
writer::error("Exceeds max number of iterations. Aborted.");
flow_done:
// Calculate three-phase short circuit.
2018-12-13 12:33:02 +00:00
if (!short_circuit) {
return;
2018-12-13 12:33:02 +00:00
}
const auto impedance = calc->node_impedance();
writer->to_csv_file("node-impedance-real.csv", impedance.first);
writer->to_csv_file("node-impedance-imag.csv", impedance.second);
const auto i_f = calc->short_circuit_current();
writer::print_complex("Three-phase short circuit current: ", i_f);
const auto u_f = calc->short_circuit_voltage();
writer->to_csv_file("short-circuit-voltage.csv", u_f, "Ui(real),Ui(imag)");
const auto i = calc->short_circuit_edge_current();
writer->to_csv_file("short-circuit-edge-current.csv", i, "Iij(real),Iij(imag)");
2018-04-05 11:01:10 +00:00
}
}