deltaFlow
DatFileWriter.C
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Saud Zahir
3 *
4 * This file is part of deltaFlow.
5 *
6 * deltaFlow is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * deltaFlow is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with deltaFlow. If not, see
18 * <https://www.gnu.org/licenses/>.
19 */
20
21#include "DatFileWriter.H"
22
23#include <cctype>
24#include <cmath>
25#include <fstream>
26
27#include "Display.H"
28#include "OutputFileCommon.H"
29#include "Version.H"
30
32 const std::string& jobName,
33 const std::string& inputFile,
34 const std::string& solverName,
35 const std::string& formatName,
36 const BusData& busData,
37 const BranchData& branchData,
38 const std::vector<std::pair<int, double>>& iterationHistory,
39 int totalIterations,
40 double finalError,
41 double tolerance,
42 bool converged,
43 double elapsedSec,
44 double basemva
45) const {
46 std::string datFile = jobName + ".dat";
47 std::ofstream out(datFile);
48 if (!out.is_open()) return false;
49
50 int nBus = busData.V.size();
51 int nBranch = branchData.From.size();
52 int W = Display::pageWidth;
53
54 out << Display::fileBanner();
55 out << fmt::format("\n deltaFlow v{:<32s}Date {:>14s} Time {:>8s}\n",
57
58 out << Display::sectionHeader("I N P U T P R O C E S S I N G");
59 out << fmt::format(" Input File : {}\n", inputFile);
60 out << fmt::format(" Input Format : {}\n", formatName);
61
62 auto counts = OutputFileCommon::countBusTypes(busData);
63
64 out << fmt::format(" Number of Buses : {:>6d}\n", nBus);
65 out << fmt::format(" Slack Buses : {:>6d}\n", counts.nSlack);
66 out << fmt::format(" PV Buses : {:>6d}\n", counts.nPV);
67 out << fmt::format(" PQ Buses : {:>6d}\n", counts.nPQ);
68 out << fmt::format(" Number of Branches : {:>6d}\n", nBranch);
69 out << fmt::format(" Base MVA : {:>10.1f}\n", basemva);
70
71 std::string solverBanner;
72 for (const char &c : solverName) {
73 solverBanner += std::toupper(c);
74 solverBanner += ' ';
75 }
76 out << Display::sectionHeader(solverBanner + " S O L V E R E X E C U T I O N");
77 out << fmt::format(" Method : {}\n", solverName);
78 out << fmt::format(" Max Iterations : {:>6d}\n", static_cast<int>(iterationHistory.size()) > 0 ?
79 static_cast<int>(iterationHistory.back().first) : totalIterations);
80 out << fmt::format(" Convergence Tol. : {:.6e}\n", tolerance);
81 out << "\n";
82
83 out << fmt::format(" {:>6s} {:>16s} {:>12s} {:>8s}\n",
84 "Iter", "Max Mismatch", "Tolerance", "Status");
85 out << " " << std::string(W - 4, '-') << "\n";
86
87 for (const auto& [iter, error] : iterationHistory) {
88 std::string status;
89 if (error < tolerance)
90 status = "CONV";
91 else
92 status = "----";
93 out << fmt::format(" {:>6d} {:>16.6e} {:>12.6e} {:>8s}\n",
94 iter, error, tolerance, status);
95 }
96
97 out << " " << std::string(W - 4, '-') << "\n";
98 out << "\n";
99
100 if (converged) {
101 out << fmt::format(" {} CONVERGED after {} iterations.\n", solverName, totalIterations);
102 out << fmt::format(" Final max mismatch = {:.6e}\n", finalError);
103 } else {
104 out << fmt::format(" *** WARNING: {} DID NOT CONVERGE after {} iterations.\n", solverName, totalIterations);
105 out << fmt::format(" Final max mismatch = {:.6e}\n", finalError);
106 }
107
108 out << Display::sectionHeader("B U S D A T A R E S U L T S");
109 out << fmt::format(" {:>4s} {:>9s} {:>9s} {:>10s} {:>10s} {:>10s} {:>10s} {:>10s}\n",
110 "Bus", "Voltage", "Angle", "Load", "Load", "Gen", "Gen", "Injected");
111 out << fmt::format(" {:>4s} {:>9s} {:>9s} {:>10s} {:>10s} {:>10s} {:>10s} {:>10s}\n",
112 "No.", "Mag.", "Degree", "MW", "Mvar", "MW", "Mvar", "Mvar");
113 out << " " << std::string(W - 4, '=') << "\n";
114
115 for (int i = 0; i < nBus; ++i) {
116 double injectedMvar = busData.Qg(i) - busData.Ql(i);
117 out << fmt::format(" {:>4d} {:>9.4f} {:>9.4f} {:>10.4f} {:>10.4f} {:>10.4f} {:>10.4f} {:>10.4f}\n",
118 i + 1, busData.V(i), busData.delta(i),
119 busData.Pl(i), busData.Ql(i), busData.Pg(i), busData.Qg(i), injectedMvar);
120 }
121
122 out << " " << std::string(W - 4, '=') << "\n";
123
124 auto totals = OutputFileCommon::computeBusTotals(busData);
125
126 out << fmt::format(" Total{:>27.4f} {:>10.4f} {:>10.4f} {:>10.4f} {:>10.4f}\n",
127 totals.totalPl, totals.totalQl, totals.totalPg, totals.totalQg, totals.totalInjected);
128
129 out << "\n\n";
130 out << " JOB TIME SUMMARY\n";
131 out << fmt::format(" TOTAL CPU TIME (SEC) = {:>12.5f}\n", elapsedSec);
132 out << fmt::format(" WALLCLOCK TIME (SEC) = {:>12d}\n", static_cast<int>(std::round(elapsedSec)));
133 out << "\n";
134
135 out << Display::sectionHeader("A N A L Y S I S C O M P L E T E");
136 if (converged) {
137 out << Display::center("THE ANALYSIS HAS BEEN COMPLETED SUCCESSFULLY") << "\n";
138 } else {
139 out << Display::center("*** THE ANALYSIS HAS NOT CONVERGED ***") << "\n";
140 }
141 out << "\n";
142
143 out.close();
144 return true;
145}
Writer for the detailed data output file (.dat).
Display and formatting utilities for terminal and file output.
Shared helpers (hostname/timestamp, bus-type counting, loss calc) used by the OutputFile writer class...
bool write(const std::string &jobName, const std::string &inputFile, const std::string &solverName, const std::string &formatName, const BusData &busData, const BranchData &branchData, const std::vector< std::pair< int, double > > &iterationHistory, int totalIterations, double finalError, double tolerance, bool converged, double elapsedSec, double basemva=100.0) const
Writes the detailed data file (.dat) with full input/output records.
std::string center(const std::string &text)
Returns a centered string within the page width.
Definition Display.H:91
constexpr int pageWidth
Standard output page width.
Definition Display.H:51
std::string sectionHeader(const std::string &title)
Returns a section header for output files.
Definition Display.H:226
std::string fileBanner()
Returns a full plain-text banner for output/log files.
Definition Display.H:217
std::string dateStr()
Returns the current date string.
BusTotals computeBusTotals(const BusData &busData)
Computes total load/gen/injected Mvar across all buses.
std::string timeStr()
Returns the current time string.
BusTypeCounts countBusTypes(const BusData &busData)
Counts buses by type (1=Slack, 2=PV, else PQ).
Contains all relevant data for each transmission line or transformer branch.
Definition Data.H:83
Eigen::VectorXi From
From bus indices.
Definition Data.H:84
Contains all relevant data for each bus in the power system.
Definition Data.H:55
Eigen::VectorXd Ql
Reactive power load [MVAr or p.u.].
Definition Data.H:65
Eigen::VectorXd V
Voltage magnitude [p.u.].
Definition Data.H:60
Eigen::VectorXd Pg
Active power generation [MW or p.u.].
Definition Data.H:62
Eigen::VectorXd delta
Voltage angle [rad or deg].
Definition Data.H:61
Eigen::VectorXd Pl
Active power load [MW or p.u.].
Definition Data.H:64
Eigen::VectorXd Qg
Reactive power generation [MVAr or p.u.].
Definition Data.H:63