GridFire 0.0.1a General Purpose Nuclear Network

Loading...
Searching...
No Matches
logging.cpp
Go to the documentation of this file.
1#include "gridfire/utils/logging.h"
2#include "gridfire/engine/engine_abstract.h"
3
4#include <sstream>
5#include <iomanip>
6#include <algorithm>
7#include <ranges>
8#include <string_view>
9#include <string>
10#include <iostream>
11#include <vector>
12
13std::string gridfire::utils::formatNuclearTimescaleLogString(
14 const DynamicEngine& engine,
15 std::vector<double> const& Y,
16 const double T9,
17 const double rho
18) {
19 auto const& result = engine.getSpeciesTimescales(Y, T9, rho);
20 if (!result) {
21 std::ostringstream ss;
22 ss << "Failed to get species timescales: " << result.error();
23 return ss.str();
24 }
25 const std::unordered_map<fourdst::atomic::Species, double>& timescales = result.value();
26
27 // Figure out how wide the "Species" column needs to be:
28 std::size_t maxNameLen = std::string_view("Species").size();
29 for (const auto &key: timescales | std::views::keys) {
30 std::string_view name = key.name();
31 maxNameLen = std::max(maxNameLen, name.size());
32 }
33
34 // Pick a fixed width for the timescale column:
35 constexpr int timescaleWidth = 12;
36
37 std::ostringstream ss;
38 ss << "== Timescales (s) ==\n";
39
40 // Header row
41 ss << std::left << std::setw(static_cast<int>(maxNameLen) + 2) << "Species"
42 << std::right << std::setw(timescaleWidth) << "Timescale (s)" << "\n";
43
44 // Underline
45 ss << std::string(static_cast<int>(maxNameLen) + 2 + timescaleWidth, '=') << "\n";
46
47 ss << std::scientific;
48
49 // Data rows
50 for (auto const& [species, timescale] : timescales) {
51 const std::string_view name = species.name();
52 ss << std::left << std::setw(static_cast<int>(maxNameLen) + 2) << name;
53
54 if (std::isinf(timescale)) {
55 ss << std::right << std::setw(timescaleWidth) << "inf" << "\n";
56 } else {
57 ss << std::right << std::setw(timescaleWidth)
58 << std::scientific << std::setprecision(3) << timescale << "\n";
59 }
60 }
61
62 // Footer underline
63 ss << std::string(static_cast<int>(maxNameLen) + 2 + timescaleWidth, '=') << "\n";
64
65 return ss.str();
66}
gridfire::DynamicEngine
Abstract class for engines supporting Jacobian and stoichiometry operations.
Definition engine_abstract.h:130
gridfire::DynamicEngine::getSpeciesTimescales
virtual std::expected< std::unordered_map< fourdst::atomic::Species, double >, expectations::StaleEngineError > getSpeciesTimescales(const std::vector< double > &Y, double T9, double rho) const =0
Compute timescales for all species in the network.
engine_abstract.h
Abstract interfaces for reaction network engines in GridFire.
logging.h
gridfire::utils::formatNuclearTimescaleLogString
std::string formatNuclearTimescaleLogString(const DynamicEngine &engine, const std::vector< double > &Y, const double T9, const double rho)
Formats a map of nuclear species timescales into a human-readable string.
Definition logging.cpp:13