Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
err_order.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes 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 License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22// Author: Pedro Peixoto <ppeixoto@usp.br>
23// Author: Yeshua Giacalone <yeshua.giacalone@mail.polimi.it>
24
25#ifndef VULPES_POSTPROCESSING_ERR_ORDER_HPP_
26#define VULPES_POSTPROCESSING_ERR_ORDER_HPP_
27
28#include <cmath>
29#include <cstddef>
30#include <filesystem>
31#include <fstream>
32#include <iomanip>
33#include <iostream>
34#include <stdexcept>
35#include <string>
36#include <utility>
37#include <vector>
38
40
41namespace vulpes {
42namespace postprocessing {
43
55inline std::vector<double> computeOrder(
56 const std::vector<double>& err, const std::vector<double>& h) {
57 if (err.empty() || err.size() != h.size()) {
58 throw std::invalid_argument(
59 "Vectors err and h must have the same non-zero size.");
60 }
61
62 std::vector<double> orders;
63 std::size_t n = err.size() - 1;
64
65 for (std::size_t i = 0; i < n; ++i) {
66 double ratio_err = err[i] / err[i + 1];
67 double ratio_h = h[i] / h[i + 1];
68 orders.push_back(std::log(ratio_err) / std::log(ratio_h));
69 }
70
71 return orders;
72}
73
90 const std::vector<double>& h_vec,
91 const std::vector<std::pair<std::string, std::vector<double>>>& series,
92 const std::string& output_dir = "",
93 const std::string& filename_stem = "convergence") {
94 std::vector<std::vector<double>> orders;
95 orders.reserve(series.size());
96 for (const auto& [name, errors] : series) {
97 orders.push_back(computeOrder(errors, h_vec));
98 }
99 if (!mpi::root()) {
100 return;
101 }
102
103 constexpr int w_h = 12;
104 constexpr int w_err = 15;
105 constexpr int w_ord = 10;
106 const int total_width =
107 w_h + static_cast<int>(series.size()) * (w_err + w_ord);
108
109 std::cout << "\n========== Convergence Table ==========\n";
110 std::cout << std::setw(w_h) << "h";
111 for (const auto& [name, errs] : series) {
112 std::cout << std::setw(w_err) << name << std::setw(w_ord) << "Order";
113 }
114 std::cout << "\n" << std::string(total_width, '-') << "\n";
115
116 for (std::size_t i = 0; i < h_vec.size(); ++i) {
117 std::cout << std::scientific << std::setprecision(3) << std::setw(w_h)
118 << h_vec[i];
119 for (std::size_t s = 0; s < series.size(); ++s) {
120 std::cout << std::scientific << std::setprecision(3) << std::setw(w_err)
121 << series[s].second[i];
122 if (i == 0) {
123 std::cout << std::setw(w_ord) << "---";
124 } else {
125 std::cout << std::fixed << std::setprecision(2) << std::setw(w_ord)
126 << orders[s][i - 1];
127 }
128 }
129 std::cout << "\n";
130 }
131 std::cout << std::endl;
132
133 if (!output_dir.empty()) {
134 std::filesystem::create_directories(output_dir);
135 const std::string csv_path = output_dir + "/" + filename_stem + ".csv";
136 std::ofstream csv(csv_path);
137 if (!csv.is_open()) {
138 std::cerr << "WARNING: could not write " << csv_path << "\n";
139 return;
140 }
141 csv << "h";
142 for (const auto& [name, errs] : series) {
143 csv << "," << name << ",order_" << name;
144 }
145 csv << "\n";
146 for (std::size_t i = 0; i < h_vec.size(); ++i) {
147 csv << h_vec[i];
148 for (std::size_t s = 0; s < series.size(); ++s) {
149 csv << "," << series[s].second[i] << ",";
150 if (i > 0) {
151 csv << orders[s][i - 1];
152 }
153 }
154 csv << "\n";
155 }
156 std::cout << "Convergence table saved to " << csv_path << "\n";
157 }
158}
159
160} // namespace postprocessing
161} // namespace vulpes
162
163#endif // VULPES_POSTPROCESSING_ERR_ORDER_HPP_
static bool root()
Return true if the rank in MPI_COMM_WORLD is zero.
Definition mpi_wrappers.hpp:132
Definition err_order.hpp:42
void printConvergenceTable(const std::vector< double > &h_vec, const std::vector< std::pair< std::string, std::vector< double > > > &series, const std::string &output_dir="", const std::string &filename_stem="convergence")
Print a unified convergence table for one or more named error series.
Definition err_order.hpp:89
std::vector< double > computeOrder(const std::vector< double > &err, const std::vector< double > &h)
Estimate convergence orders from a sequence of errors and mesh sizes.
Definition err_order.hpp:55
Definition cell_manager.hpp:38