Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
71 lines (59 loc) · 2.25 KB
/
Copy pathmain.cpp
File metadata and controls
71 lines (59 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright (C) 2025 Samuel Wyss
*
* This file is part of numericxx.
*
* Numericxx is free software: you can redistribute it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Numericxx is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with numericxx. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <fmt/chrono.h>
#include <fmt/core.h>
#include <fmt/printf.h>
#include <numericxx.hpp>
constexpr double INIT_F = 1.0;
double rhs(const double t, const double f) { return -2.0 * f; }
double sol(const double t, const double f) { return std::exp(-2.0 * t); }
int main(const int argc, const char **argv) {
const auto n = static_cast<size_t>(std::stoull(argv[1]));
const auto dt = static_cast<double>(std::stod(argv[2]));
double t = 0.0;
double f = INIT_F;
// timing
const std::chrono::time_point<std::chrono::high_resolution_clock> start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < n; ++i) {
f = forward_euler(rhs, t, f, dt);
t += dt;
}
const std::chrono::time_point<std::chrono::high_resolution_clock> end = std::chrono::high_resolution_clock::now();
const auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
fmt::print("loop time: {}\n", duration);
fmt::print("iteration time: {}\n", duration / n);
fmt::print("final value: {}\n", f);
fmt::print("\n");
// comparison
t = 0.0;
f = INIT_F;
double act = sol(t, f);
double err = std::fabs(f - act) / act * 100;
fmt::print("t, actual, calculated, error(%)\n");
fmt::print("{:.8e}, {:.8e}, {:.8e}, {:.8e}\n", t, act, f, err);
for (size_t i = 0; i < n; ++i) {
f = forward_euler(rhs, t, f, dt);
t += dt;
act = sol(t, f);
err = std::fabs(f - act) / act * 100;
fmt::print("{:.8e}, {:.8e}, {:.8e}, {:.8e}\n", t, act, f, err);
}
return EXIT_SUCCESS;
}