Fenics/Example

From ScientificComputing
Jump to: navigation, search

As an example for using fenics, we are running a script that solves a differential equation:

[eu-login-01 ~]$ module load stack/2024-06 gcc/12.2.0  openmpi/4.1.6 fenics-dolfinx/0.8.0 py-fenics-ufl/2024.1.0.post1 py-fenics-ffcx/0.8.0 py-fenics-dolfinx/0.8.0 py-fenics-basix/0.8.0 fenics-ufcx/0.8.0 fenics-basix/0.8.0 fenics-ufcx/0.8.0 fenics-basix/0.8.0 python/3.11.6 py-mpi4py/3.1.4 py-petsc4py/3.20.1
[eu-login-01 ~]$ ls -ltr
total 4
-rwxr-xr-x 1 euler T0000 219 Sep 14 08:20 test.py
[eu-login-01 ~]$ cat test.py
#!/usr/bin/env python

from mpi4py import MPI
from petsc4py.PETSc import ScalarType  # type: ignore
import numpy as np
import ufl
from dolfinx import fem, io, mesh, plot
from dolfinx.fem.petsc import LinearProblem
from ufl import ds, dx, grad, inner

msh = mesh.create_rectangle(
    comm=MPI.COMM_WORLD,
    points=((0.0, 0.0), (2.0, 1.0)),
    n=(32, 16),
    cell_type=mesh.CellType.triangle,
)
V = fem.functionspace(msh, ("Lagrange", 1))

facets = mesh.locate_entities_boundary(
    msh,
    dim=(msh.topology.dim - 1),
    marker=lambda x: np.isclose(x[0], 0.0) | np.isclose(x[0], 2.0),
)

dofs = fem.locate_dofs_topological(V=V, entity_dim=1, entities=facets)
bc = fem.dirichletbc(value=ScalarType(0), dofs=dofs, V=V)

u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
x = ufl.SpatialCoordinate(msh)
f = 10 * ufl.exp(-((x[0] - 0.5) ** 2 + (x[1] - 0.5) ** 2) / 0.02)
g = ufl.sin(5 * x[0])
a = inner(grad(u), grad(v)) * dx
L = inner(f, v) * dx + inner(g, v) * ds

problem = LinearProblem(a, L, bcs=[bc], petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()

with io.XDMFFile(msh.comm, "poisson1.xdmf", "w") as file:
    file.write_mesh(msh)
    file.write_function(uh)
[eu-login-1 ~]$ sbatch --mem-per-cpu=4g --wrap="python3 ./test.py"
Generic job.
Job <27435524> is submitted to queue <normal.4h>.

The resource usage summary as well as the job logs can be found in the LSF log file lsf.o27435524.