8#include <unordered_map>
16 const int nx =
phi.nx(), ny =
phi.ny(), nz =
phi.nz();
17 const double inv_dx2 = 1.0 / (
phi.dx() *
phi.dx());
22 for (
int k = 1; k < nz - 1; ++k)
23 for (
int j = 1; j < ny - 1; ++j)
24 for (
int i = 1; i < nx - 1; ++i)
25 b[
phi.grid().flat(i, j, k)] = -source(i, j, k);
37 const std::vector<DirichletBC>& bcs,
40 const int nx =
phi.nx(), ny =
phi.ny(), nz =
phi.nz();
42 const double inv_dx2 = 1.0 / (
phi.dx() *
phi.dx());
44 auto flat = [&](
int i,
int j,
int k) ->
idx {
45 return static_cast<idx>(k * ny * nx + j * nx + i);
48 std::unordered_map<int, double> bc_map;
49 bc_map.reserve(bcs.size());
50 for (
const auto&
e : bcs)
51 bc_map[
e.flat_idx] =
e.value;
53 constexpr int DI[6] = {1, -1, 0, 0, 0, 0};
54 constexpr int DJ[6] = {0, 0, 1, -1, 0, 0};
55 constexpr int DK[6] = {0, 0, 0, 0, 1, -1};
56 constexpr double penalty = 1e10;
61 for (
const auto&
e : bcs) {
62 b[
e.flat_idx] = penalty *
e.value;
63 const int ei =
e.flat_idx % nx;
64 const int ej = (
e.flat_idx / nx) % ny;
65 const int ek =
e.flat_idx / (nx * ny);
66 for (
int d = 0; d < 6; ++d) {
67 int ni = ei + DI[d], nj = ej + DJ[d], nk = ek + DK[d];
68 if (ni < 0 || ni >= nx || nj < 0 || nj >= ny || nk < 0 || nk >= nz)
70 int nidx = flat(ni, nj, nk);
71 if (bc_map.count(nidx))
73 double sigma_face = 0.5 * (coeff(ei, ej, ek) + coeff(ni, nj, nk));
74 b[nidx] += sigma_face * inv_dx2 *
e.value;
79 for (
int k = 0; k < nz; ++k)
80 for (
int j = 0; j < ny; ++j)
81 for (
int i = 0; i < nx; ++i) {
82 int id = flat(i, j, k);
83 if (bc_map.count(
id)) {
84 Av[id] = penalty * v[id];
88 for (
int d = 0; d < 6; ++d) {
89 int ni = std::max(0, std::min(i + DI[d], nx - 1));
90 int nj = std::max(0, std::min(j + DJ[d], ny - 1));
91 int nk = std::max(0, std::min(k + DK[d], nz - 1));
92 int nidx = flat(ni, nj, nk);
93 double c_face = 0.5 * (coeff(i, j, k) + coeff(ni, nj, nk));
94 double v_nb = bc_map.count(nidx) ? 0.0 : v[nidx];
95 Av_ijk += c_face * inv_dx2 * (v[id] - v_nb);
131 const int nx = sigma.
nx(), ny = sigma.
ny(), nz = sigma.
nz();
132 for (
int k = 0; k < nz; ++k)
133 for (
int j = 0; j < ny; ++j)
134 for (
int i = 0; i < nx; ++i) {
135 const double neg_s = -sigma(i, j, k);
136 J.
x(i, j, k) *= neg_s;
137 J.
y(i, j, k) *= neg_s;
138 J.
z(i, j, k) *= neg_s;
146 const int nx = J.
x.
nx(), ny = J.
x.
ny(), nz = J.
x.
nz();
147 const float dx = J.
x.
dx(), ox = J.
x.
ox(), oy = J.
x.
oy(), oz = J.
x.
oz();
151 for (
int k = 0; k < nz; ++k)
152 for (
int j = 0; j < ny; ++j)
153 for (
int i = 0; i < nx; ++i)
154 src(i, j, k) = -
MU0 * Jc(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)
Compute .
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]
Elliptic solvers and vector calculus on 3D field containers.
CallableOp< F > make_op(F f, idx rows, idx cols)
SPDOp< Op > assume_spd(Op op)
constexpr real phi
Golden ratio.
void neg_laplacian_3d(const Vector &x, Vector &y, int nx, int ny, int nz, double inv_dx2)
Compute on a 3D grid.
void curl_3d(const ScalarField3D &ax, const ScalarField3D &ay, const ScalarField3D &az, ScalarField3D &bx, ScalarField3D &by, ScalarField3D &bz)
Compute with central differences.
void matvec(const Matrix &A, const Vector &x, Vector &y, Backend b=default_backend)
y = A * x
void gradient_3d(const ScalarField3D &phi, ScalarField3D &gx, ScalarField3D &gy, ScalarField3D &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)
void divergence_3d(const ScalarField3D &fx, const ScalarField3D &fy, const ScalarField3D &fz, ScalarField3D &out)
Compute with central differences.
Umbrella include for operator concepts and adapters.
Umbrella include for all linear solvers.
Higher-order stencil and grid-sweep utilities.