I think this is exactly what I need, thank you very much. However, I’m facing a simple problem. That is not realted to this topic anymore, I can create a new topic on the discourse and copy paste the following :
I’ve implemented a simple 1D helmholtz problem with the fem on collab strategy, with boundary condition. Even if I’ve installed the Dolfinx version from the complex branch, I have a message error saying :
---------------------------------------------------------------------------
ArityMismatch Traceback (most recent call last)
<ipython-input-6-9c15c347e209> in <cell line: 46>()
44
45 # Solving the linear system
---> 46 problem = LinearProblem(a, L, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
47 p_solution = problem.solve()
48
14 frames
/usr/local/lib/python3.10/dist-packages/ufl/algorithms/check_arities.py in check_integrand_arity(expr, arguments, complex_mode)
203 for arg, conj in arg_tuples:
204 if arg.number() == 0 and not conj:
--> 205 raise ArityMismatch("Failure to conjugate test function in complex Form")
206 elif arg.number() > 0 and conj:
207 raise ArityMismatch(f"Argument {arg} is spuriously conjugated in complex Form")
ArityMismatch: Failure to conjugate test function in complex Form
Here it’s my code, I don’t think my FE code is the problem but more probably from installation. So I give here the first notebook collab cell that might donwload the complex dolfinx branch, and then my FE code:
[1]
import os
arch = os.getenv("ARGS", "complex")
try:
import google.colab # noqa: F401
except ImportError:
import ufl
import dolfinx
else:
try:
import ufl
import dolfinx
except ImportError:
if arch != "complex":
!wget "https://fem-on-colab.github.io/releases/fenicsx-install-real.sh" -O "/tmp/fenicsx-install.sh" && bash "/tmp/fenicsx-install.sh"
else:
!wget "https://fem-on-colab.github.io/releases/fenicsx-install-complex.sh" -O "/tmp/fenicsx-install.sh" && bash "/tmp/fenicsx-install.sh"
import ufl
import dolfinx
[2]
import numpy as np
import dolfinx
from mpi4py import MPI
from petsc4py import PETSc
from ufl import TrialFunction, TestFunction, dx, grad, inner, div, ds, as_vector, Measure
from basix.ufl import element
from dolfinx.fem import Function, FunctionSpace, assemble_scalar, form, functionspace
from dolfinx.fem.petsc import LinearProblem
from dolfinx.mesh import create_interval
from dolfinx.io import XDMFFile
# Problem parameters
Lf = 1.0 # Domain length (1 meter)
rho_0 = 1.21 # Volumic masse of air (kg/m^3)
c = 343.0 # Speed of sound in the air (m/s)
omega = 2 * np.pi * 1000 # Angular frequency
k = omega / c # Wave number
Z_n = rho_0 * c # Acoustic impedance
# 1D mesh
mesh = create_interval(MPI.COMM_WORLD, 100, [0, Lf])
# Space function
Ve = element("Lagrange", mesh.basix_cell(), 2)
V = functionspace(mesh, ("Lagrange", 2))
# Test and Trial functions
p = TrialFunction(V)
v = TestFunction(V)
# Variational formulation
a = inner(grad(p), grad(v)) * dx - k**2 * inner(p, v) * dx
# Source term
f = Function(V)
L = inner(f, v) * dx
# Boundary condition : Impedance x = Lf Neuman condition x = 0
ds_boundary = Measure("ds", domain=mesh, subdomain_data=None)
n = as_vector((1,))
a += (1j * omega * rho_0 / Z_n) * p * v * ds_boundary(1)
L += omega**2 * rho_0 * 1.0 * v * ds_boundary(0)
# Solving the linear system
problem = LinearProblem(a, L, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
p_solution = problem.solve()
# Save + Visualization
with XDMFFile(MPI.COMM_WORLD, "solution_helmholtz.xdmf", "w") as xdmf:
xdmf.write_mesh(mesh)
xdmf.write_function(p_solution)
try:
import pyvista
from dolfinx.plot import create_vtk_mesh
topology, cell_types, x = create_vtk_mesh(mesh)
grid = pyvista.UnstructuredGrid(topology, cell_types, x)
grid["Solution"] = p_solution.x.array.real
plotter = pyvista.Plotter()
plotter.add_mesh(grid, show_edges=True)
plotter.show()
except ImportError:
pass
Thanks a lot,
Pierre