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
10V = fem.functionspace(domain, ("P",1))
11
12
13uh = fem.Function(V)
14
15
16import numpy as np
17def u(x, mod=np):
18 return mod.sin(mod.pi*x[0])*mod.sin(mod.pi*x[1])
19
20uh.interpolate(lambda x: u(x))
21
22
23from dolfinx import geometry
24
25def fun_eval(u, points,
26 domain=domain):
27 u_values = []
28 bb_tree = geometry.bb_tree(domain, domain.topology.dim)
29 cells = []
30 points_on_proc = []
31
32 cell_candidates = geometry.compute_collisions_points(bb_tree,
33 points.T)
34
35 colliding_cells = geometry.compute_colliding_cells(domain,
36 cell_candidates,
37 points.T)
38 for i, point in enumerate(points.T):
39 if len(colliding_cells.links(i)) > 0:
40 points_on_proc.append(point)
41 cells.append(colliding_cells.links(i)[0])
42
43 points_on_proc = np.array(points_on_proc, dtype=np.float64)
44 u_values = u.eval(points_on_proc, cells)
45 return u_values
46
47
48import numpy as np
49nx = ny = 101
50xx0 = np.linspace(0., 1., nx)
51xx1 = np.linspace(0., 1., ny)
52X0, X1 = np.meshgrid(xx0, xx1, indexing='ij')
53points = np.zeros((3, nx*ny))
54points[0] = X0.reshape(-1)
55points[1] = X1.reshape(-1)
56
57yh = fun_eval(uh, points)
58Yh = yh.reshape((nx,ny))
59
60import matplotlib.pyplot as plt
61
62fig = plt.figure()
63ax = fig.add_subplot()
64levels=10
65cb = ax.contourf(X0, X1, Yh, levels = levels)
66fig.colorbar(cb)
67Y = u([X0, X1])
68cl = ax.contour(X0, X1, Y, levels = levels, colors='w')
69ax.clabel(cl)
70plt.show()