OneSimulation docs Website Free books Plans on Gumroad

Docs › Build a first case in each physics package › Electrostatics

5.6 Electrostatics

onesim.physics.electrostatics

PoissonEquation solves \(-\nabla\cdot(\epsilon\nabla\phi)=\rho_q\) with \(\mathbf E=-\nabla\phi\). Specify permittivity in F/m and volume charge in C/m\(^3\). FixedPotential sets volts. NormalElectricDisplacement prescribes outward \(\mathbf D\cdot\mathbf n\) in C/m\(^2\) and its zero counterpart represents insulation. For an all-flux problem check the integrated charge and flux compatibility and set a supported reference for the additive potential constant. Sample the potential and electric field on the same line, with separate axes or panels.

import numpy as np
from onesim.cad.d3 import Box
from onesim.materials import VacuumPermittivity
from onesim.meshing import StructuredTetrahedralMesher
from onesim.numerics import Numerics
from onesim.numerics.fem import FiniteElement, Lagrange
from onesim.numerics.linear import SparseDirect
from onesim.physics.electrostatics import FixedPotential, PoissonEquation
from onesim.simulation import Problem, Simulation

box = Box(length=1.0, width=1.0, height=1.0)
electro = PoissonEquation(domain=box.volume, permittivity=VacuumPermittivity())
problem = Problem(
    name="Uniform electric field", geometry=box, physics=(electro,),
    boundary_conditions=(
        FixedPotential(boundary=box.boundaries.all, physics=electro,
                       value=lambda x: 2*x),
    ),
)
mesh = StructuredTetrahedralMesher(cells=(2, 2, 2)).generate(box)
sim = Simulation(
    problem=problem, mesh=mesh,
    numerics=Numerics(spatial=FiniteElement(element=Lagrange(order=1)),
                      linear_solver=SparseDirect()),
)
sim.validate().require_valid()
result = sim.solve_stationary()
np.testing.assert_allclose(
    result.field(electro.potential).evaluate([[0.25, 0.4, 0.6]]), [0.5],
    atol=1e-10,
)

import matplotlib.pyplot as plt
import onesimulation_plot_theme as theme

x = np.linspace(.01,.99,101)
points = np.column_stack((x,np.full_like(x,.4),np.full_like(x,.6)))
potential = result.field(electro.potential).evaluate(points)
electric_x = result.field(electro.electric_field).evaluate(points)[:,0]
np.testing.assert_allclose(electric_x,-2,atol=1e-9)
theme.use_theme()
fig,(a,b)=plt.subplots(2,1,sharex=True,figsize=(6.8,3.8),layout="constrained")
a.plot(x,potential);a.set_ylabel("Potential [V]")
b.plot(x,electric_x);b.set(xlabel="World x [m]",ylabel="Electric field x [V/m]")
plt.show()
Figure computed with OneSimulation: recipe poisson equation

For supplied charge from another mesh, see test_cell_projection_poisson.py. That test projects a native cell field to FE quadrature through an explicit FieldCoupling.