Dear all,
I am totally new to Fenics, so please excuse my low level questions …
I want to compute the electric field generated by of a meshed part which is at electrical potential of 100kV.
I have meshed th part with gmsh, and I would like to comute the electric field with dolfinX.
The meshed part is very simple (surface meshing):
So far, I have the following code:
import numpy as np
import matplotlib.pyplot as plt
import dolfinx
from mpi4py import MPI
from dolfinx.io import XDMFFile
from dolfinx.mesh import locate_entities_boundary
from dolfinx.fem import (FunctionSpace, dirichletbc, Function, Constant, locate_dofs_geometrical,
assemble_matrix, assemble_vector, set_bc)
from ufl import TrialFunction, TestFunction, inner, grad, dx
from petsc4py import PETSc
comm = MPI.COMM_WORLD
Load the mesh
with XDMFFile(comm, “generated_mesh_2D.xdmf”, “r”) as xdmf:
mesh = xdmf.read_mesh(name=“Grid”)
Define the function space
V = FunctionSpace(mesh, (“CG”, 1))
Define the boundary condition
u_D = Function(V)
u_D.interpolate(lambda x: np.full(x.shape[1], 100000)) # 100 kV
Boundary condition marker function
def boundary(x):
return np.full(x.shape[1], True)
dofs = locate_dofs_geometrical(V, boundary)
bc = dirichletbc(u_D, dofs)
Define the variational problem
u = TrialFunction(V)
v = TestFunction(V)
a = inner(grad(u), grad(v)) * dx
zero = Constant(mesh, 0.0) # Ensure the constant is properly initialized
L = zero * v * dx
Assemble and solve the system
A = assemble_matrix(a, bcs=[bc])
b = assemble_vector(L)
set_bc(b, [bc])
solver = PETSc.KSP().create(comm)
solver.setOperators(A)
solver.setType(PETSc.KSP.Type.PREONLY)
solver.getPC().setType(PETSc.PC.Type.LU)
phi = Function(V)
solver.solve(b, phi.vector)
Save the solution
with XDMFFile(MPI.COMM_WORLD, “electric_potential_solution.xdmf”, “w”) as file:
file.write_mesh(mesh)
file.write_function(phi)
But I get the following error:
AttributeError Traceback (most recent call last)
Cell In[38], line 40
37 L = zero * v * dx
39 # Assemble and solve the system
—> 40 A = assemble_matrix(a, bcs=[bc])
41 b = assemble_vector(L)
42 set_bc(b, [bc])
File ~/miniconda3/lib/python3.12/functools.py:909, in singledispatch..wrapper(*args, **kw)
905 if not args:
906 raise TypeError(f’{funcname} requires at least ’
907 ‘1 positional argument’)
→ 909 return dispatch(args[0].class)(*args, **kw)
File ~/miniconda3/lib/python3.12/site-packages/dolfinx/fem/assemble.py:243, in assemble_matrix(a, bcs, diagonal, constants, coeffs, block_mode)
219 “”“Assemble bilinear form into a matrix.
220
221 Args:
(…)
240
241 “””
242 bcs = if bcs is None else bcs
→ 243 A: la.MatrixCSR = create_matrix(a, block_mode)
244 _assemble_matrix_csr(A, a, bcs, diagonal, constants, coeffs)
245 return A
File ~/miniconda3/lib/python3.12/site-packages/dolfinx/fem/assemble.py:102, in create_matrix(a, block_mode)
92 def create_matrix(a: Form, block_mode: typing.Optional[la.BlockMode] = None) → la.MatrixCSR:
93 “”“Create a sparse matrix that is compatible with a given bilinear form.
94 Args:
95 a: Bilinear form to assemble.
(…)
100
101 “””
→ 102 sp = dolfinx.fem.create_sparsity_pattern(a)
103 sp.finalize()
104 if block_mode is not None:
File ~/miniconda3/lib/python3.12/site-packages/dolfinx/fem/init.py:37, in create_sparsity_pattern(a)
23 def create_sparsity_pattern(a: Form):
24 “”“Create a sparsity pattern from a bilinear form.
25
26 Args:
(…)
35
36 “””
—> 37 return _create_sparsity_pattern(a._cpp_object)
AttributeError: ‘Form’ object has no attribute ‘_cpp_object’
No idea of to deal with that.
First, I am not sure at all if this is the proper way to do it, and if by chance it is … how to solve this error message.
Could you please guide me during my first steps in dolfinx?
Many thanks in advance.
Best