Hello,
I am new to Fenicsx and FEM, I studied on tutorials and previous discussions but I could not solve this problem, it is a simple question for you but I would be very happy if you answer it, it is important for me. I want to solve the 2D Poisson problem in the relevant mesh I created and I want to keep the stiffness matrix, right-hand side and solution as a matrix form. I wrote a code like the one below for this, but I still get the error in the rest of the code. Also, will my code enable 2D Poisson solving using Nedelec basis functions on the relevant mesh? Also, how can I add the 0 or another Dirichlet boundary condition to this scenario ? Thanks
import gmsh
import numpy as np
import ufl
from mpi4py import MPI
from dolfinx import mesh, fem, plot, io
from dolfinx.io import gmshio
import pyvista
from petsc4py import PETSc
import scipy.sparse
Initialize visualization
pyvista.start_xvfb()
def create_mesh_with_num_triangles(target_num_triangles):
if gmsh.isInitialized():
gmsh.finalize()
gmsh.initialize()
gmsh.model.add(“Mesh with target triangles”)
rectangle_tag = gmsh.model.occ.addRectangle(0, 0, 0, 1, 1)
gmsh.model.occ.synchronize()
average_edge_length = np.sqrt(2 / target_num_triangles)
gmsh.model.mesh.setSize(gmsh.model.getEntities(0), average_edge_length)
gmsh.model.mesh.generate(2)
gmsh.model.addPhysicalGroup(2, [rectangle_tag], 1)
gmsh.write(“target_mesh.msh”)
gmsh.finalize()
MPI.COMM_WORLD.Barrier()
return gmshio.read_from_msh(“target_mesh.msh”, MPI.COMM_WORLD, gdim=2)[0]
def visualize_mesh(mesh):
topology, cell_types, geometry = plot.vtk_mesh(mesh, mesh.geometry.dim)
grid = pyvista.UnstructuredGrid(topology, cell_types, geometry)
plotter = pyvista.Plotter()
plotter.add_mesh(grid, show_edges=True)
plotter.view_xy()
if not pyvista.OFF_SCREEN:
plotter.show()
else:
plotter.screenshot(“target_mesh.png”)
Create and visualize mesh
mesh = create_mesh_with_num_triangles(100)
visualize_mesh(mesh)
Setup function space and problem
V = fem.FunctionSpace(mesh, ufl.FiniteElement(“Nedelec 1st kind H(curl)”, mesh.ufl_cell(), 1))
sigma = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
a = fem.form(ufl.inner(ufl.curl(sigma), ufl.curl(v)) * ufl.dx)
L = fem.form(ufl.inner(ufl.as_vector((1, 1)), v) * ufl.dx)
Assembly system
A = dolfinx.fem.petsc.assemble_matrix(dolfinx.fem.form(a))
A.assemble()
b = fem.assemble_vector(L)
b.ghostUpdate(addv=PETSc.InsertMode.ADD, mode=PETSc.ScatterMode.REVERSE) # Ensure correct assembly
Solver setup
solver = PETSc.KSP().create(MPI.COMM_WORLD)
solver.setType(PETSc.KSP.Type.PREONLY)
solver.getPC().setType(PETSc.PC.Type.LU)
solver.setOperators(A)
u = fem.Function(V)
#Solve
solver.solve(b, u.vector)
Save solutions
with io.XDMFFile(MPI.COMM_WORLD, “solution.xdmf”, “w”) as xdmf:
xdmf.write_mesh(mesh)
xdmf.write_function(u)
Error: ---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[16], line 56
54 A.assemble()
55 b = fem.assemble_vector(L)
—> 56 b.ghostUpdate(addv=PETSc.InsertMode.ADD, mode=PETSc.ScatterMode.REVERSE) # Ensure correct assembly
58 # Solver setup
59 solver = PETSc.KSP().create(MPI.COMM_WORLD)
AttributeError: ‘Vector’ object has no attribute ‘ghostUpdate’
I used 0.7.2. Dolfinx version.
Thank you,
Chen