Skip to content

Commit 9d41c18

Browse files
committed
Extract exp_* to classes
1 parent 166e971 commit 9d41c18

File tree

3 files changed

+80
-22
lines changed

3 files changed

+80
-22
lines changed

cpp/lib/Exponentials.hpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#pragma once
2+
3+
#include "constants.hpp"
4+
#include "grid.hpp"
5+
#include "hyper.hpp"
6+
7+
namespace ahr::exp {
8+
class Base {
9+
public:
10+
explicit Base(Grid const &grid) : grid(grid) {}
11+
12+
void update(HyperCoefficients hyper, Real dt) {
13+
this->hyper = hyper;
14+
this->dt = dt;
15+
}
16+
17+
protected:
18+
Grid const &grid;
19+
HyperCoefficients hyper{};
20+
Real dt{-1};
21+
};
22+
23+
class Eta : Base {
24+
public:
25+
using Base::Base;
26+
using Base::update;
27+
28+
Real operator()(Dim kx, Dim ky) {
29+
return std::exp(
30+
-(res * grid.kPerp2(kx, ky) + hyper.eta2 * std::pow(grid.kPerp2(kx, ky), hyper_order)) *
31+
dt / (1.0 + grid.kPerp2(kx, ky) * de * de));
32+
}
33+
};
34+
35+
class Nu : Base {
36+
public:
37+
using Base::Base;
38+
using Base::update;
39+
40+
Real operator()(Dim kx, Dim ky) {
41+
return std::exp(
42+
-(nu * grid.kPerp2(kx, ky) + hyper.nu_2 * std::pow(grid.kPerp2(kx, ky), hyper_order)) * dt);
43+
}
44+
};
45+
46+
class NuG : Base {
47+
public:
48+
using Base::Base;
49+
using Base::update;
50+
51+
Real operator()(Dim kx, Dim ky) {
52+
return std::exp(
53+
-(nu * grid.kPerp2(kx, ky) + hyper.nu_g * std::pow(grid.kPerp2(kx, ky), hyper_order)) * dt);
54+
}
55+
};
56+
57+
class GM : Base {
58+
public:
59+
using Base::Base;
60+
using Base::update;
61+
62+
Real operator()(Dim m) {
63+
return std::exp(-(Real(m) * nu_ei + std::pow(m, 2 * hyper_morder) * hyper.nu_ei) * dt);
64+
}
65+
};
66+
67+
} // namespace ahr::exp

cpp/lib/Naive.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,14 @@ void Naive::run(Dim N, Dim saveInterval) {
8181
divergent = false;
8282
} else if (dt == -1) {
8383
dt = getTimestep(dPhi, Grid::sliceXY(dGM, N_E), Grid::sliceXY(dGM, A_PAR));
84-
hyper = HyperCoefficients::calculate(dt, g);
8584
}
8685

86+
hyper = HyperCoefficients::calculate(dt, g);
87+
exp_nu.update(hyper, dt);
88+
exp_nu_g.update(hyper, dt);
89+
exp_eta.update(hyper, dt);
90+
exp_gm.update(hyper, dt);
91+
8792
spdlog::debug("dt: {}", dt);
8893

8994
// store results of nonlinear operators, as well as results of predictor step
@@ -396,7 +401,6 @@ void Naive::run(Dim N, Dim saveInterval) {
396401
Real tempDt =
397402
getTimestep(dPhi_Loop, Grid::sliceXY(dGM_Loop, N_E), Grid::sliceXY(dGM_Loop, A_PAR));
398403
dt = updateTimestep(dt, tempDt, noInc, relative_error);
399-
hyper = HyperCoefficients::calculate(dt, g);
400404

401405
spdlog::info("Moving on to next timestep: {}\n"
402406
"dt is: {}",
@@ -419,7 +423,6 @@ void Naive::run(Dim N, Dim saveInterval) {
419423
spdlog::info("t={} magnetic energy: {}, kinetic energy: {}", t, magnetic, kinetic);
420424

421425
// Log moment values when level is trace (most verbose)
422-
423426
for (Dim m = 0; m < g.M; ++m) {
424427
spdlog::trace(
425428
"t={} m={}:\n{}", t, m,

cpp/lib/Naive.hpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "Brackets.hpp"
4+
#include "Exponentials.hpp"
45
#include "Exporter.hpp"
56
#include "Filter.hpp"
67
#include "HermiteRunner.hpp"
@@ -97,25 +98,12 @@ class Naive : public ahr::HermiteRunner {
9798
// TODO other file/class
9899
// =================
99100

100-
[[nodiscard]] Real exp_nu(Dim kx, Dim ky) const {
101-
return std::exp(
102-
-(nu * g.kPerp2(kx, ky) + hyper.nu_2 * std::pow(g.kPerp2(kx, ky), hyper_order)) * dt);
103-
}
104-
105-
[[nodiscard]] Real exp_nu_g(Dim kx, Dim ky) const {
106-
return std::exp(
107-
-(nu * g.kPerp2(kx, ky) + hyper.nu_g * std::pow(g.kPerp2(kx, ky), hyper_order)) * dt);
108-
}
109-
110-
[[nodiscard]] Real exp_gm(Dim m) const {
111-
return std::exp(-(Real(m) * nu_ei + std::pow(m, 2 * hyper_morder) * hyper.nu_ei) * dt);
112-
}
113-
114-
[[nodiscard]] Real exp_eta(Dim kx, Dim ky) const {
115-
return std::exp(
116-
-(res * g.kPerp2(kx, ky) + hyper.eta2 * std::pow(g.kPerp2(kx, ky), hyper_order)) * dt /
117-
(1.0 + g.kPerp2(kx, ky) * de * de));
118-
}
101+
/// \defgroup @{
102+
exp::Nu exp_nu{g};
103+
exp::NuG exp_nu_g{g};
104+
exp::Eta exp_eta{g};
105+
exp::GM exp_gm{g};
106+
/// @}
119107

120108
/// getTimestep calculates flows and magnetic fields to determine a dt.
121109
/// It also updates bPerpMax in the process.

0 commit comments

Comments
 (0)