Hi! I just learned FEniCS for a short time. I am trying to calculate the magnetic field produced by a permanent magnet in vacuum. Since no current is involved, I chose to calculate the magnetic scalar potential to solve this problem.The partial differential equations satisfy Poisson’s equation or Laplace’s equation,
\nabla\cdot\nabla\Phi=\nabla\cdot \vec{M}
I followed the Poisson Problem tutorial to convert it to weak form,
\int_\Omega {\nabla \Phi \cdot \nabla vdx}=\int_\Omega {\nabla\cdot \vec{M}dx}+\int_{\partial\Omega}{{\partial\Phi} \over {\partial n}}vds
What confuses me is how to set vector parameter in FEniCS, which in my case refers to the magnetization vector \vec{M} .The following code is still not working. And, is it possible to select specific nodes to set the vector parameter?
import math
import numpy as np
from dolfinx import *
from dolfinx.mesh import create_unit_square, locate_entities, locate_entities_boundary, meshtags
from mpi4py import MPI
from petsc4py.PETSc import ScalarType
from ufl import (FacetNormal, Measure, SpatialCoordinate, TestFunction, TrialFunction,
div, dot, dx, grad, inner, lhs, rhs)
#creat mesh
mesh = mesh.create_unit_square(MPI.COMM_WORLD, 20, 20)
V = fem.FunctionSpace(mesh,("DG",1))
#Q = VectorFunctionSpace(mesh, ("DG", 2))
u = TrialFunction(V)
v = TestFunction(V)
u_0 = 4*math.pi*1e-7
Ms = 1e4/0.012566
Mv = fem.Function(V)
def Omega_m(x):
return np.logical_and(np.logical_and(x[0]>=0.25, x[0]<=0.75),
np.logical_and(x[1]>=0.25, x[1]<=0.75))
def Omega_v(x):
return np.logical_or(np.logical_or(x[0]<=0.25, x[0]>=0.75),
np.logical_or(x[1]<=0.25, x[1]>=0.75))
def D_bc(x):
return np.logical_or(np.logical_or(np.isclose(x[0], 0), np.isclose(x[0], 1)),
np.logical_or(np.isclose(x[1], 0), np.isclose(x[1], 1)))
cells_m = locate_entities(mesh, mesh.topology.dim, Omega_m)
cells_v = locate_entities(mesh, mesh.topology.dim, Omega_v)
#magnetization vector for magnet and vacuum,I know it is not a vector yet.
Mv.x.array[cells_m] = np.full_like(cells_m, Ms, dtype=ScalarType)
Mv.x.array[cells_v] = np.full_like(cells_v, 0, dtype=ScalarType)
dofs = fem.locate_dofs_geometrical(V, D_bc)
bcs = [fem.dirichletbc(ScalarType(0), dofs, V)]
dx = Measure('dx', mesh)
a = inner(grad(u), grad(v)) * dx
f = div(Mv)
L = f * v * dx
problem = fem.petsc.LinearProblem(a, L, bcs=bcs, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()
Looking forward to everyone’s help, many thanks!