numerics 0.1.0
Loading...
Searching...
No Matches
fields.cpp
Go to the documentation of this file.
1/// @file src/pde/fields.cpp
2/// @brief Implementations for num::ScalarField3D, VectorField3D, FieldSolver,
3/// MagneticSolver.
4#include "pde/fields.hpp"
6#include "pde/stencil.hpp"
7#include <algorithm>
8#include <unordered_map>
9
10namespace num {
11
12// ScalarField3D
13
14ScalarField3D::ScalarField3D(int nx, int ny, int nz, float dx, float ox,
15 float oy, float oz)
16 : grid_(nx, ny, nz, static_cast<double>(dx)), ox_(ox), oy_(oy), oz_(oz) {}
17
18float ScalarField3D::sample(float x, float y, float z) const {
19 const float gx = (x - ox_) / dx();
20 const float gy = (y - oy_) / dx();
21 const float gz = (z - oz_) / dx();
22
23 if (gx < 0 || gx >= nx() - 1 || gy < 0 || gy >= ny() - 1 || gz < 0 ||
24 gz >= nz() - 1)
25 return 0.0f;
26
27 const int i0 = static_cast<int>(gx);
28 const int j0 = static_cast<int>(gy);
29 const int k0 = static_cast<int>(gz);
30 const float tx = gx - i0, ty = gy - j0, tz = gz - k0;
31
32 auto v = [&](int di, int dj, int dk) {
33 return static_cast<float>(grid_(i0 + di, j0 + dj, k0 + dk));
34 };
35 return (1 - tz) * ((1 - ty) * ((1 - tx) * v(0, 0, 0) + tx * v(1, 0, 0)) +
36 ty * ((1 - tx) * v(0, 1, 0) + tx * v(1, 1, 0))) +
37 tz * ((1 - ty) * ((1 - tx) * v(0, 0, 1) + tx * v(1, 0, 1)) +
38 ty * ((1 - tx) * v(0, 1, 1) + tx * v(1, 1, 1)));
39}
40
41// VectorField3D
42
43VectorField3D::VectorField3D(int nx, int ny, int nz, float dx, float ox,
44 float oy, float oz)
45 : x(nx, ny, nz, dx, ox, oy, oz), y(nx, ny, nz, dx, ox, oy, oz),
46 z(nx, ny, nz, dx, ox, oy, oz) {}
47
48std::array<float, 3> VectorField3D::sample(float px, float py, float pz) const {
49 return {x.sample(px, py, pz), y.sample(px, py, pz), z.sample(px, py, pz)};
50}
51
52void VectorField3D::scale(float s) {
53 auto sc = [&](Grid3D &g) {
54 auto v = g.to_vector();
55 num::scale(v, static_cast<real>(s));
56 g.from_vector(v);
57 };
58 sc(x.grid());
59 sc(y.grid());
60 sc(z.grid());
61}
62
63// FieldSolver
64
66 const ScalarField3D &source, double tol,
67 int max_iter) {
68 const Grid3D &g = phi.grid();
69 const int nx = g.nx(), ny = g.ny(), nz = g.nz();
70 const double inv_dx2 = 1.0 / (g.dx() * g.dx());
71 const int N = nx * ny * nz;
72
73 auto flat = [&](int i, int j, int k) -> idx {
74 return static_cast<idx>(k * ny * nx + j * nx + i);
75 };
76
77 Vector b(N, 0.0);
78 for (int k = 1; k < nz - 1; ++k)
79 for (int j = 1; j < ny - 1; ++j)
80 for (int i = 1; i < nx - 1; ++i)
81 b[flat(i, j, k)] = -source.grid()(i, j, k);
82
83 Vector xv = phi.grid().to_vector();
84 auto matvec = [&](const Vector &v, Vector &Av) {
85 neg_laplacian_3d(v, Av, nx, ny, nz, inv_dx2);
86 };
87 auto result = cg_matfree(matvec, b, xv, tol, static_cast<idx>(max_iter));
88 phi.grid().from_vector(xv);
89 return result;
90}
91
93 const ScalarField3D &coeff,
94 const std::vector<DirichletBC> &bcs,
95 double tol, int max_iter) {
96 const Grid3D &g = phi.grid();
97 const Grid3D &cg = coeff.grid();
98 const int nx = g.nx(), ny = g.ny(), nz = g.nz();
99 const int N = nx * ny * nz;
100 const double inv_dx2 = 1.0 / (g.dx() * g.dx());
101
102 auto flat = [&](int i, int j, int k) -> idx {
103 return static_cast<idx>(k * ny * nx + j * nx + i);
104 };
105
106 std::unordered_map<int, double> bc_map;
107 bc_map.reserve(bcs.size());
108 for (const auto &e : bcs)
109 bc_map[e.flat_idx] = e.value;
110
111 constexpr int DI[6] = {1, -1, 0, 0, 0, 0};
112 constexpr int DJ[6] = {0, 0, 1, -1, 0, 0};
113 constexpr int DK[6] = {0, 0, 0, 0, 1, -1};
114 constexpr double penalty = 1e10;
115
116 // Symmetric penalty elimination: for each BC node, add coeff*phi_bc to
117 // the RHS of neighbouring free nodes so the system stays SPD.
118 Vector b(N, 0.0);
119 for (const auto &e : bcs) {
120 b[e.flat_idx] = penalty * e.value;
121 const int ei = e.flat_idx % nx;
122 const int ej = (e.flat_idx / nx) % ny;
123 const int ek = e.flat_idx / (nx * ny);
124 for (int d = 0; d < 6; ++d) {
125 int ni = ei + DI[d], nj = ej + DJ[d], nk = ek + DK[d];
126 if (ni < 0 || ni >= nx || nj < 0 || nj >= ny || nk < 0 || nk >= nz)
127 continue;
128 int nidx = flat(ni, nj, nk);
129 if (bc_map.count(nidx))
130 continue;
131 double sigma_face = 0.5 * (cg(ei, ej, ek) + cg(ni, nj, nk));
132 b[nidx] += sigma_face * inv_dx2 * e.value;
133 }
134 }
135
136 auto matvec = [&](const Vector &v, Vector &Av) {
137 for (int k = 0; k < nz; ++k)
138 for (int j = 0; j < ny; ++j)
139 for (int i = 0; i < nx; ++i) {
140 int id = flat(i, j, k);
141 if (bc_map.count(id)) {
142 Av[id] = penalty * v[id];
143 continue;
144 }
145 double Av_ijk = 0.0;
146 for (int d = 0; d < 6; ++d) {
147 int ni = std::max(0, std::min(i + DI[d], nx - 1));
148 int nj = std::max(0, std::min(j + DJ[d], ny - 1));
149 int nk = std::max(0, std::min(k + DK[d], nz - 1));
150 int nidx = flat(ni, nj, nk);
151 double c_face = 0.5 * (cg(i, j, k) + cg(ni, nj, nk));
152 double v_nb = bc_map.count(nidx) ? 0.0 : v[nidx];
153 Av_ijk += c_face * inv_dx2 * (v[id] - v_nb);
154 }
155 Av[id] = Av_ijk;
156 }
157 };
158
159 Vector xv = phi.grid().to_vector();
160 auto result = cg_matfree(matvec, b, xv, tol, static_cast<idx>(max_iter));
161 phi.grid().from_vector(xv);
162 return result;
163}
164
166 VectorField3D out(phi.nx(), phi.ny(), phi.nz(), phi.dx(), phi.ox(), phi.oy(),
167 phi.oz());
168 gradient_3d(phi.grid(), out.x.grid(), out.y.grid(), out.z.grid());
169 return out;
170}
171
173 ScalarField3D out(f.x.nx(), f.x.ny(), f.x.nz(), f.x.dx(), f.x.ox(), f.x.oy(),
174 f.x.oz());
175 divergence_3d(f.x.grid(), f.y.grid(), f.z.grid(), out.grid());
176 return out;
177}
178
180 VectorField3D B(A.x.nx(), A.x.ny(), A.x.nz(), A.x.dx(), A.x.ox(), A.x.oy(),
181 A.x.oz());
182 curl_3d(A.x.grid(), A.y.grid(), A.z.grid(), B.x.grid(), B.y.grid(),
183 B.z.grid());
184 return B;
185}
186
187// MagneticSolver
188
190 const ScalarField3D &phi) {
192 const Grid3D &sg = sigma.grid();
193 const int nx = sg.nx(), ny = sg.ny(), nz = sg.nz();
194 for (int k = 0; k < nz; ++k)
195 for (int j = 0; j < ny; ++j)
196 for (int i = 0; i < nx; ++i) {
197 const double neg_s = -sg(i, j, k);
198 J.x.grid()(i, j, k) *= neg_s;
199 J.y.grid()(i, j, k) *= neg_s;
200 J.z.grid()(i, j, k) *= neg_s;
201 }
202 return J;
203}
204
206 double tol, int max_iter) {
207 const int nx = J.x.nx(), ny = J.x.ny(), nz = J.x.nz();
208 const float dx = J.x.dx(), ox = J.x.ox(), oy = J.x.oy(), oz = J.x.oz();
209
210 auto make_source = [&](const ScalarField3D &Jc) {
211 ScalarField3D src(nx, ny, nz, dx, ox, oy, oz);
212 const Grid3D &gJ = Jc.grid();
213 Grid3D &gs = src.grid();
214 for (int k = 0; k < nz; ++k)
215 for (int j = 0; j < ny; ++j)
216 for (int i = 0; i < nx; ++i)
217 gs(i, j, k) = -MU0 * gJ(i, j, k);
218 return src;
219 };
220
221 ScalarField3D Ax(nx, ny, nz, dx, ox, oy, oz);
222 ScalarField3D Ay(nx, ny, nz, dx, ox, oy, oz);
223 ScalarField3D Az(nx, ny, nz, dx, ox, oy, oz);
224
225 FieldSolver::solve_poisson(Ax, make_source(J.x), tol, max_iter);
226 FieldSolver::solve_poisson(Ay, make_source(J.y), tol, max_iter);
227 FieldSolver::solve_poisson(Az, make_source(J.z), tol, max_iter);
228
229 VectorField3D A(nx, ny, nz, dx, ox, oy, oz);
230 A.x = Ax;
231 A.y = Ay;
232 A.z = Az;
233 return FieldSolver::curl(A);
234}
235
236} // namespace num
static VectorField3D gradient(const ScalarField3D &phi)
Definition fields.cpp:165
static SolverResult solve_var_poisson(ScalarField3D &phi, const ScalarField3D &coeff, const std::vector< DirichletBC > &bcs, double tol=1e-6, int max_iter=500)
Definition fields.cpp:92
static SolverResult solve_poisson(ScalarField3D &phi, const ScalarField3D &source, double tol=1e-6, int max_iter=500)
Definition fields.cpp:65
static VectorField3D curl(const VectorField3D &A)
Definition fields.cpp:179
static ScalarField3D divergence(const VectorField3D &f)
Compute div(f) via central finite differences.
Definition fields.cpp:172
double dx() const
Definition grid3d.hpp:30
int ny() const
Definition grid3d.hpp:24
int nz() const
Definition grid3d.hpp:27
int nx() const
Definition grid3d.hpp:21
static VectorField3D solve_magnetic_field(const VectorField3D &J, double tol=1e-6, int max_iter=500)
Definition fields.cpp:205
static VectorField3D current_density(const ScalarField3D &sigma, const ScalarField3D &phi)
Compute current density J = -sigma*grad(phi) [A/m^2].
Definition fields.cpp:189
static constexpr double MU0
mu_0 [H/m]
Definition fields.hpp:156
float oy() const
Definition fields.hpp:57
float ox() const
Definition fields.hpp:54
int nx() const
Definition fields.hpp:42
float dx() const
Definition fields.hpp:51
float sample(float x, float y, float z) const
Definition fields.cpp:18
ScalarField3D(int nx, int ny, int nz, float dx, float ox=0.0f, float oy=0.0f, float oz=0.0f)
Definition fields.cpp:14
int ny() const
Definition fields.hpp:45
Grid3D & grid()
Definition fields.hpp:35
float oz() const
Definition fields.hpp:60
int nz() const
Definition fields.hpp:48
double real
Definition types.hpp:10
constexpr real phi
Golden ratio.
Definition math.hpp:44
void curl_3d(const Grid3D &ax, const Grid3D &ay, const Grid3D &az, Grid3D &bx, Grid3D &by, Grid3D &bz)
Definition stencil.hpp:342
void neg_laplacian_3d(const Vector &x, Vector &y, int nx, int ny, int nz, double inv_dx2)
Definition stencil.hpp:273
std::size_t idx
Definition types.hpp:11
void matvec(const Matrix &A, const Vector &x, Vector &y, Backend b=default_backend)
y = A * x
Definition matrix.cpp:120
void scale(Vector &v, real alpha, Backend b=default_backend)
v *= alpha
Definition vector.cpp:29
constexpr real e
Definition math.hpp:43
void divergence_3d(const Grid3D &fx, const Grid3D &fy, const Grid3D &fz, Grid3D &out)
Definition stencil.hpp:320
void gradient_3d(const Grid3D &phi, Grid3D &gx, Grid3D &gy, Grid3D &gz)
Definition stencil.hpp:302
SolverResult cg_matfree(MatVecFn matvec, const Vector &b, Vector &x, real tol=1e-6, idx max_iter=1000)
Matrix-free conjugate gradient for Ax = b where A is SPD.
Definition cg.cpp:78
SolverResult cg(const Matrix &A, const Vector &b, Vector &x, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
Conjugate gradient solver for Ax = b.
Definition cg.cpp:8
3D scalar and vector fields on Cartesian grids, with PDE field solvers.
Umbrella include for all linear solvers.
Higher-order stencil and grid-sweep utilities.
std::array< float, 3 > sample(float px, float py, float pz) const
Trilinear-interpolated field vector at world position.
Definition fields.cpp:48
ScalarField3D z
x, y, z components on the same grid layout
Definition fields.hpp:92
VectorField3D(int nx, int ny, int nz, float dx, float ox=0.0f, float oy=0.0f, float oz=0.0f)
Definition fields.cpp:43
ScalarField3D x
Definition fields.hpp:92
void scale(float s)
Multiply all components by scalar s.
Definition fields.cpp:52
ScalarField3D y
Definition fields.hpp:92