OneSimulation docs Website Free books Plans on Gumroad

Docs › Worked physics and service recipes › Magnetostatics recipes

6.13 Magnetostatics recipes

The solenoid chapter exercises the current source, embedded winding support and magnetic insulation. The manufactured trace below isolates the tangential condition and the gauge from winding geometry.

6.13.1 Imposed tangential potential and a uniform magnetic field

Use a known divergence-free vector potential as tangential trace. The resulting flux density is the curl of that field. This example exercises a nonzero trace and the Coulomb constraint.

import numpy as np
import matplotlib.pyplot as plt
import onesimulation_plot_theme as theme
from onesim.cad.d3 import Box
from onesim.materials import VacuumPermeability
from onesim.meshing import StructuredTetrahedralMesher
from onesim.numerics import Numerics
from onesim.numerics.fem import FiniteElement, Nedelec
from onesim.numerics.linear import SparseDirect
from onesim.physics.magnetostatics import (
    CoulombGauge,
    MagneticPotentialBoundary,
    MagneticVectorPotential,
    Magnetostatics,
)
from onesim.simulation import Problem, Simulation

box = Box(length=1, width=1, height=1)
model = Magnetostatics(
    domain=box.volume,
    permeability=VacuumPermeability(),
    formulation=MagneticVectorPotential(
        gauge=CoulombGauge(multiplier_reference_dof=0)
    ),
)


def vector_potential(x, y, z):
    return np.column_stack((-0.5 * y, 0.5 * x, np.zeros_like(x)))


problem = Problem(
    name="Uniform B patch",
    geometry=box,
    physics=(model,),
    boundary_conditions=(
        MagneticPotentialBoundary(
            physics=model, boundary=box.boundaries.all, value=vector_potential
        ),
    ),
)
mesh = StructuredTetrahedralMesher(cells=(2, 2, 2)).generate(box)
result = Simulation(
    problem=problem,
    mesh=mesh,
    numerics=Numerics(
        spatial=FiniteElement(element=Nedelec(1)), linear_solver=SparseDirect()
    ),
).solve_stationary()
x = np.linspace(0.02, 0.98, 101)
p = np.column_stack((x, np.full_like(x, 0.37), np.full_like(x, 0.51)))
B = result.field(model.magnetic_flux_density).evaluate(p)
np.testing.assert_allclose(B, np.broadcast_to((0.0, 0.0, 1.0), B.shape), atol=1e-8)
theme.use_theme()
fig, ax = plt.subplots(figsize=(6.6, 3.3), layout="constrained")
for i, d in enumerate("xyz"):
    ax.plot(x, B[:, i], label=f"B {d}")
ax.set(xlabel="World x [m]", ylabel="Magnetic flux density [T]")
ax.legend(frameon=False)
plt.show()
Figure computed with OneSimulation: recipe magnetic potential boundary