8#include <unordered_map>
21 : grid_(nx, ny, nz, static_cast<double>(dx)),
28 const float gx = (x - ox_) /
dx();
29 const float gy = (y - oy_) /
dx();
30 const float gz = (z - oz_) /
dx();
32 if (gx < 0 || gx >=
nx() - 1 || gy < 0 || gy >=
ny() - 1 || gz < 0 || gz >=
nz() - 1)
35 const int i0 =
static_cast<int>(gx);
36 const int j0 =
static_cast<int>(gy);
37 const int k0 =
static_cast<int>(gz);
38 const float tx = gx - i0, ty = gy - j0, tz = gz - k0;
40 auto v = [&](
int di,
int dj,
int dk) {
41 return static_cast<float>(grid_(i0 + di, j0 + dj, k0 + dk));
44 * ((1 - ty) * ((1 - tx) * v(0, 0, 0) + tx * v(1, 0, 0))
45 + ty * ((1 - tx) * v(0, 1, 0) + tx * v(1, 1, 0)))
47 * ((1 - ty) * ((1 - tx) * v(0, 0, 1) + tx * v(1, 0, 1))
48 + ty * ((1 - tx) * v(0, 1, 1) + tx * v(1, 1, 1)));
60 : x(nx, ny, nz, dx, ox, oy, oz),
61 y(nx, ny, nz, dx, ox, oy, oz),
62 z(nx, ny, nz, dx, ox, oy, oz) {
71 auto v = g.to_vector();
87 const int nx = g.
nx(), ny = g.
ny(), nz = g.
nz();
88 const double inv_dx2 = 1.0 / (g.
dx() * g.
dx());
89 const int N = nx * ny * nz;
91 auto flat = [&](
int i,
int j,
int k) ->
idx {
92 return static_cast<idx>(k * ny * nx + j * nx + i);
96 for (
int k = 1; k < nz - 1; ++k)
97 for (
int j = 1; j < ny - 1; ++j)
98 for (
int i = 1; i < nx - 1; ++i)
99 b[flat(i, j, k)] = -source.
grid()(i, j, k);
106 auto result =
num::cg(A, b, xv, tol,
static_cast<idx>(max_iter));
107 phi.grid().from_vector(xv);
113 const std::vector<DirichletBC>& bcs,
118 const int nx = g.
nx(), ny = g.
ny(), nz = g.
nz();
119 const int N = nx * ny * nz;
120 const double inv_dx2 = 1.0 / (g.
dx() * g.
dx());
122 auto flat = [&](
int i,
int j,
int k) ->
idx {
123 return static_cast<idx>(k * ny * nx + j * nx + i);
126 std::unordered_map<int, double> bc_map;
127 bc_map.reserve(bcs.size());
128 for (
const auto&
e : bcs)
129 bc_map[
e.flat_idx] =
e.value;
131 constexpr int DI[6] = {1, -1, 0, 0, 0, 0};
132 constexpr int DJ[6] = {0, 0, 1, -1, 0, 0};
133 constexpr int DK[6] = {0, 0, 0, 0, 1, -1};
134 constexpr double penalty = 1e10;
139 for (
const auto&
e : bcs) {
140 b[
e.flat_idx] = penalty *
e.value;
141 const int ei =
e.flat_idx % nx;
142 const int ej = (
e.flat_idx / nx) % ny;
143 const int ek =
e.flat_idx / (nx * ny);
144 for (
int d = 0; d < 6; ++d) {
145 int ni = ei + DI[d], nj = ej + DJ[d], nk = ek + DK[d];
146 if (ni < 0 || ni >= nx || nj < 0 || nj >= ny || nk < 0 || nk >= nz)
148 int nidx = flat(ni, nj, nk);
149 if (bc_map.count(nidx))
151 double sigma_face = 0.5 * (
cg(ei, ej, ek) +
cg(ni, nj, nk));
152 b[nidx] += sigma_face * inv_dx2 *
e.value;
157 for (
int k = 0; k < nz; ++k)
158 for (
int j = 0; j < ny; ++j)
159 for (
int i = 0; i < nx; ++i) {
160 int id = flat(i, j, k);
161 if (bc_map.count(
id)) {
162 Av[id] = penalty * v[id];
166 for (
int d = 0; d < 6; ++d) {
167 int ni = std::max(0, std::min(i + DI[d], nx - 1));
168 int nj = std::max(0, std::min(j + DJ[d], ny - 1));
169 int nk = std::max(0, std::min(k + DK[d], nz - 1));
170 int nidx = flat(ni, nj, nk);
171 double c_face = 0.5 * (
cg(i, j, k) +
cg(ni, nj, nk));
172 double v_nb = bc_map.count(nidx) ? 0.0 : v[nidx];
173 Av_ijk += c_face * inv_dx2 * (v[id] - v_nb);
181 auto result =
num::cg(A, b, xv, tol,
static_cast<idx>(max_iter));
182 phi.grid().from_vector(xv);
212 const int nx = sg.
nx(), ny = sg.
ny(), nz = sg.
nz();
213 for (
int k = 0; k < nz; ++k)
214 for (
int j = 0; j < ny; ++j)
215 for (
int i = 0; i < nx; ++i) {
216 const double neg_s = -sg(i, j, k);
217 J.
x.
grid()(i, j, k) *= neg_s;
218 J.
y.
grid()(i, j, k) *= neg_s;
219 J.
z.
grid()(i, j, k) *= neg_s;
227 const int nx = J.
x.
nx(), ny = J.
x.
ny(), nz = J.
x.
nz();
228 const float dx = J.
x.
dx(), ox = J.
x.
ox(), oy = J.
x.
oy(), oz = J.
x.
oz();
232 const Grid3D& gJ = Jc.grid();
234 for (
int k = 0; k < nz; ++k)
235 for (
int j = 0; j < ny; ++j)
236 for (
int i = 0; i < nx; ++i)
237 gs(i, j, k) = -
MU0 * gJ(i, j, k);
static VectorField3D gradient(const ScalarField3D &phi)
Compute .
static SolverResult solve_var_poisson(ScalarField3D &phi, const ScalarField3D &coeff, const std::vector< DirichletBC > &bcs, double tol=1e-6, int max_iter=500)
Solve with Dirichlet data.
static SolverResult solve_poisson(ScalarField3D &phi, const ScalarField3D &source, double tol=1e-6, int max_iter=500)
Solve with zero Dirichlet boundaries.
static VectorField3D curl(const VectorField3D &A)
static ScalarField3D divergence(const VectorField3D &f)
Compute .
static VectorField3D solve_magnetic_field(const VectorField3D &J, double tol=1e-6, int max_iter=500)
static VectorField3D current_density(const ScalarField3D &sigma, const ScalarField3D &phi)
Compute current density J = -sigma*grad(phi) [A/m^2].
static constexpr double MU0
mu_0 [H/m]
float sample(float x, float y, float z) const
ScalarField3D(int nx, int ny, int nz, float dx, float ox=0.0f, float oy=0.0f, float oz=0.0f)
CallableOp< F > make_op(F f, idx rows, idx cols)
constexpr real phi
Golden ratio.
void curl_3d(const Grid3D &ax, const Grid3D &ay, const Grid3D &az, Grid3D &bx, Grid3D &by, Grid3D &bz)
Compute with central differences.
void neg_laplacian_3d(const Vector &x, Vector &y, int nx, int ny, int nz, double inv_dx2)
Compute on a 3D grid.
void matvec(const Matrix &A, const Vector &x, Vector &y, Backend b=default_backend)
y = A * x
void scale(Vector &v, real alpha, Backend b=default_backend)
Compute .
void divergence_3d(const Grid3D &fx, const Grid3D &fy, const Grid3D &fz, Grid3D &out)
Compute with central differences.
void gradient_3d(const Grid3D &phi, Grid3D &gx, Grid3D &gy, Grid3D &gz)
Compute with central differences.
SolverResult cg(const Matrix &A, const Vector &b, Vector &x, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
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.
ScalarField3D z
x, y, z components on the same grid layout
VectorField3D(int nx, int ny, int nz, float dx, float ox=0.0f, float oy=0.0f, float oz=0.0f)
void scale(float s)
Multiply all components by scalar s.