1from mpi4py import MPI
2from dolfinx import mesh
3
4
5domain = mesh.create_unit_square(MPI.COMM_WORLD, 16, 16)
6
7from dolfinx import fem
8
9
10Vh = fem.functionspace(domain, ("P",1))
11
12
13uh = fem.Function(Vh)
14
15
16import ufl
17from dolfinx.fem.petsc import LinearProblem
18def uex(x, mod=ufl):
19 return mod.sin(mod.pi*x[0])*mod.sin(mod.pi*x[1])
20
21x = ufl.SpatialCoordinate(domain)
22u = ufl.TrialFunction(Vh)
23v = ufl.TestFunction(Vh)
24a = ufl.dot(u,v)*ufl.dx
25L = uex(x)*v*ufl.dx
26problem = LinearProblem(a, L, bcs=[])
27Phu = problem.solve()
28
29
30from dolfinx import io
31from pathlib import Path
32results_folder = Path("results")
33results_folder.mkdir(exist_ok=True, parents=True)
34filename = results_folder / "phu"
35Phu.name = "Phu"
36with io.VTXWriter(domain.comm, filename.with_suffix(".bp"), [Phu]) as vtx:
37 vtx.write(0.0)
38with io.XDMFFile(domain.comm, filename.with_suffix(".xdmf"), "w") as xdmf:
39 xdmf.write_mesh(domain)
40 xdmf.write_function(Phu, 0.0)