deltaFlow
OutputFileCommon.H
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
33#ifndef OUTPUT_FILE_COMMON_H
34#define OUTPUT_FILE_COMMON_H
35
36#include <chrono>
37#include <cmath>
38#include <complex>
39#include <string>
40
41#ifdef _WIN32
42 #include <windows.h>
43#else
44 #include <unistd.h>
45#endif
46
47#include <Eigen/Dense>
48#include <fmt/chrono.h>
49#include <fmt/core.h>
50
51#include "Data.H"
52
57namespace OutputFileCommon {
58
60 inline std::string hostname() {
61 #ifdef _WIN32
62 char buf[MAX_COMPUTERNAME_LENGTH + 1];
63 DWORD bufLen = sizeof(buf);
64 if (GetComputerNameA(buf, &bufLen)) return std::string(buf);
65 #else
66 char buf[256];
67 if (gethostname(buf, sizeof(buf)) == 0) return std::string(buf);
68 #endif
69 return "unknown";
70 }
71
73 inline std::string timestamp() {
74 auto now = std::time(nullptr);
75 return fmt::format("{:%d-%b-%Y %H:%M:%S}", fmt::localtime(now));
76 }
77
79 inline std::string dateStr() {
80 auto now = std::time(nullptr);
81 return fmt::format("{:%d-%b-%Y}", fmt::localtime(now));
82 }
83
85 inline std::string timeStr() {
86 auto now = std::time(nullptr);
87 return fmt::format("{:%H:%M:%S}", fmt::localtime(now));
88 }
89
92 int nSlack = 0;
93 int nPV = 0;
94 int nPQ = 0;
95 };
96
98 inline BusTypeCounts countBusTypes(const BusData& busData) {
99 BusTypeCounts counts;
100 int nBus = static_cast<int>(busData.Type.size());
101 for (int i = 0; i < nBus; ++i) {
102 if (busData.Type(i) == 1) counts.nSlack++;
103 else if (busData.Type(i) == 2) counts.nPV++;
104 else counts.nPQ++;
105 }
106 return counts;
107 }
108
110 struct BusTotals {
111 double totalPl = 0.0;
112 double totalQl = 0.0;
113 double totalPg = 0.0;
114 double totalQg = 0.0;
115 double totalInjected = 0.0;
116 };
117
119 inline BusTotals computeBusTotals(const BusData& busData) {
120 BusTotals t;
121 t.totalPl = busData.Pl.sum();
122 t.totalQl = busData.Ql.sum();
123 t.totalPg = busData.Pg.sum();
124 t.totalQg = busData.Qg.sum();
125 t.totalInjected = t.totalQg - t.totalQl;
126 return t;
127 }
128
129}
130
131#endif
Data structures and utility functions for reading and displaying bus and branch data in power system ...
Small pieces of business/formatting logic shared by all OutputFile writers.
std::string timestamp()
Returns the current timestamp string.
std::string hostname()
Returns the current hostname.
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 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 Pg
Active power generation [MW or p.u.].
Definition Data.H:62
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
Eigen::VectorXi Type
Bus type (1=Slack, 2=PV, 3=PQ)
Definition Data.H:58
Aggregate bus totals (load/gen) used in the bus-data result tables.
Bus type counts (Slack/PV/PQ).