u = w.sub(0).collapse() p = w.sub(1).collapse() # We can calculate the :math:`L^2` norms of u and p as follows:: print("Norm of velocity coefficient vector: %.15g" % u.vector().norm()) print("Norm of pressure coefficient vector: %.15g" % p.vector().norm()) # Check pressure norm pnorm = p.vector().norm() assert np.isclose(pnorm, 4147.69457577) # Finally, we can save and plot the solutions:: # Save solution in XDMF format with XDMFFile(MPI.comm_world, "velocity.xdmf") as ufile_xdmf: ufile_xdmf.write(u) with XDMFFile(MPI.comm_world, "pressure.xdmf") as pfile_xdmf: pfile_xdmf.write(p) # Plot solution plt.figure() plot(u, title="velocity") # plt.figure() plot(p, title="pressure" + str(MPI.rank(mesh.mpi_comm()))) # Display plots plt.show()
import os, matplotlib if 'DISPLAY' not in os.environ: matplotlib.use('agg') from dolfin import * from dolfin.plotting import plot import matplotlib.pyplot as plt # Create mesh and define function spaces mesh = UnitSquareMesh(MPI.comm_world, 64, 64) P1 = FunctionSpace(mesh, "CG", 1) # Define function v = Expression("sin(10.0*x[0])*sin(10.0*x[1])", degree=2) # Compute projection (L2-projection) Pv = project(v, V=P1) # Compute interpolation (evaluating dofs) PIv = Function(P1) PIv.interpolate(v) # Plot functions plt.figure() plot(v, mesh=mesh, title="v") plt.figure() plot(Pv, title="Pv") plt.figure() plot(PIv, title="PI v") plt.show()
class DirichletBoundary(SubDomain): def inside(self, x, on_boundary): return numpy.logical_and(x[:, 0] < DOLFIN_EPS, on_boundary) # Define variational problem u = TrialFunction(V) v = TestFunction(V) f = Expression("9.0*pi*pi*sin(3.0*pi*x[0])", degree=2) g = Expression("3.0*pi*cos(3.0*pi*x[0])", degree=2) a = dot(grad(u), grad(v)) * dx L = f * v * dx + g * v * ds # Define boundary condition u0 = Constant(0.0) bc = DirichletBC(V, u0, DirichletBoundary()) # Compute solution u = Function(V) solve(a == L, u, bc) # Save solution to file file = XDMFFile(MPI.comm_world, "poisson.xdmf") file.write(u) # Plot solution plot(u) plt.savefig('plot.pdf') plt.show()
import matplotlib.pyplot as plt # Create mesh and define function spaces mesh0 = UnitSquareMesh(MPI.comm_world, 16, 16) mesh1 = UnitSquareMesh(MPI.comm_world, 64, 64) # Create expression on P3 u0 = Expression("sin(10.0*x[0])*sin(10.0*x[1])", degree=3) # Define projection space P1 = FunctionSpace(mesh1, "CG", 1) # Define projection variation problem v = TestFunction(P1) u1 = TrialFunction(P1) a = v * u1 * dx L = v * u0 * dx # Compute solution u1 = Function(P1) solve(a == L, u1) # Plot functions plt.figure() plot(u0, mesh=mesh0, title="u0") plt.savefig("plot0.pdf") plt.figure() plot(u1, title="u1") plt.savefig("plot1.pdf") plt.show()
from dolfin.plotting import plot # Form compiler options # parameters["form_compiler"]["optimize"] = True # parameters["form_compiler"]["cpp_optimize"] = True n = 100 eps = 1e-8 mesh = IntervalMesh.create(MPI.comm_world, n, [-2.0, +2.0], cpp.mesh.GhostMode.none) mesh.geometry.coord_mapping = dolfin.fem.create_coordinate_map(mesh) x = SpatialCoordinate(mesh)[0] plot(cos(x), title='cos', mesh=mesh) plot(sin(x), title='sin', mesh=mesh) plot(tan(x), title='tan', mesh=mesh) mesh = IntervalMesh.create(MPI.comm_world, n, [0.0 + eps, 1.0 - eps], cpp.mesh.GhostMode.none) mesh.geometry.coord_mapping = dolfin.fem.create_coordinate_map(mesh) x = SpatialCoordinate(mesh)[0] plt.figure() plot(acos(x), title='acos', mesh=mesh) plt.figure() plot(asin(x), title='asin', mesh=mesh) plt.figure() plot(atan(x), title='atan', mesh=mesh)