Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some typos and outdated documentation in the demos #2199

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/demo/demo_cahn-hilliard.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@

lmbda = 1.0e-02 # surface parameter
dt = 5.0e-06 # time step
theta = 0.5 # time stepping family, e.g. theta=1 -> backward Euler, theta=0.5 -> Crank-Nicolson
theta = 0.5 # time stepping family, e.g. theta=1 -> backward Euler, theta=0.5 -> Crank-Nicholson

# A unit square mesh with 96 cells edges in each direction is created,
# and on this mesh a
Expand Down
17 changes: 9 additions & 8 deletions python/demo/demo_elasticity.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
dtype = PETSc.ScalarType
# -

# ## Operators nullspace
# ## Operator's nullspace
#
# Smoother aggregation algebraic multigrid solver require the so-called
# Smooth aggregation algebraic multigrid solvers require the so-called
# 'near-nullspace', which is the nullspace of the operator in the
# absence of boundary conditions. The below function builds a PETSc
# NullSpace object. For this 3D elasticity problem the nullspace is
Expand Down Expand Up @@ -123,7 +123,7 @@ def σ(v):
L = form(inner(f, v) * dx)

# A homogeneous (zero) boundary condition is created on $x_0 = 0$ and
# $x_1 = 1$ by finding all boundary facets on $x_0 = 0$ and # $x_1 = 1$,
# $x_1 = 1$ by finding all boundary facets on $x_0 = 0$ and $x_1 = 1$,
# and then creating a Dirichlet boundary condition object.

facets = locate_entities_boundary(msh, dim=2,
Expand Down Expand Up @@ -157,12 +157,12 @@ def σ(v):
set_bc(b, [bc])
# -

# Create the a near nullspace and attach it to the PETSc matrix:
# Create the near-nullspace and attach it to the PETSc matrix:

null_space = build_nullspace(V)
A.setNearNullSpace(null_space)

# Set PETsc solver options, create a PETSc Krylov solver, and attach the
# Set PETSc solver options, create a PETSc Krylov solver, and attach the
# matrix `A` to the solver:

# +
Expand All @@ -188,18 +188,19 @@ def σ(v):
solver.setOperators(A)
# -

# Create a solution Function `uh` and solve:
# Create a solution {py:class}`Function<dolfinx.fem.Function>`, `uh`, and
# solve:

# +
uh = Function(V)

# Set a monitor, solve linear system, and dispay the solver
# Set a monitor, solve linear system, and display the solver
# configuration
solver.setMonitor(lambda _, its, rnorm: print(f"Iteration: {its}, rel. residual: {rnorm}"))
solver.solve(b, uh.vector)
solver.view()

# Scatter forward the solution vector to udpate ghost values
# Scatter forward the solution vector to update ghost values
uh.x.scatter_forward()
# -

Expand Down
1 change: 0 additions & 1 deletion python/demo/demo_gmsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@

msh = create_mesh(MPI.COMM_SELF, cells, x, ufl_mesh_from_gmsh(element_types[0], x.shape[1]))

# with XDMFFile(MPI.COMM_SELF, f"out_gmsh/mesh_rank_{MPI.COMM_WORLD.rank}.xdmf", "w") as file:
with XDMFFile(MPI.COMM_SELF, f"out_gmsh/mesh_rank_{MPI.COMM_WORLD.rank}.xdmf", "w") as file:
file.write_mesh(msh)
# -
Expand Down
8 changes: 4 additions & 4 deletions python/demo/demo_helmholtz.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@
problem = LinearProblem(a, L, u=uh, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
problem.solve()

# Save solution in XDMF format (to be viewed in Paraview, for example)
# Save solution in XDMF format (to be viewed in ParaView, for example)
with XDMFFile(MPI.COMM_WORLD, "out_helmholtz/plane_wave.xdmf", "w", encoding=XDMFFile.Encoding.HDF5) as file:
file.write_mesh(msh)
file.write_function(uh)
# -

# Calculate L2 and H1 errors of FEM solution and best approximation.
# This demonstrates the error bounds given in Ihlenburg. Pollution errors
# are evident for high wavenumbers.
# Calculate $L_2$ and $H^1$ errors of FEM solution and best approximation.
# This demonstrates the error bounds given in Ihlenburg. Pollution errors are
# evident for high wavenumbers.

# +
# Function space for exact solution - need it to be higher than deg
Expand Down
4 changes: 2 additions & 2 deletions python/demo/demo_interpolation-io.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#
# # Interpolation and IO
#
# This demo show the interpolation of functions into vector-element
# (H(curl)) finite element spaces, and the interpolation of these
# This demo shows the interpolation of functions into vector-element
# $H(\mathrm{curl})$ finite element spaces, and the interpolation of these
# special finite elements in discontinuous Lagrange spaces for
# artifact-free visualisation.

Expand Down
6 changes: 3 additions & 3 deletions python/demo/demo_lagrange_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,14 @@ def saw_tooth(x):
# interpolation being less accurate when using the equispaced variant of
# Lagrange compared to the GLL variant.
#
# To quantify the error, we compute the interpolation error in the L2
# To quantify the error, we compute the interpolation error in the $L_2$
# norm,
#
# $$\left\|u - u_h\right\|_2 = \left(\int_0^1 (u - u_h)^2\right)^{\frac{1}{2}},$$
#
# where $u$ is the function and $u_h$ is its interpolation in the finite
# element space. The following code uses UFL to compute the L2 error for
# the equispaced and GLL variants. The L2 error for the GLL variant is
# element space. The following code uses UFL to compute the $L_2$ error for
# the equispaced and GLL variants. The $L_2$ error for the GLL variant is
# considerably smaller than the error for the equispaced variant.

# +
Expand Down
2 changes: 1 addition & 1 deletion python/demo/demo_poisson.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
# -

# The solution can be written to a {py:class}`XDMFFile
# <dolfinx.io.XDMFFile>` file visualization with ParaView ot VisIt
# <dolfinx.io.XDMFFile>` file visualization with ParaView or VisIt

# +
with io.XDMFFile(msh.comm, "out_poisson/poisson.xdmf", "w") as file:
Expand Down
16 changes: 8 additions & 8 deletions python/demo/demo_pyvista.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def plot_scalar():
u.interpolate(lambda x: np.sin(np.pi * x[0]) * np.sin(2 * x[1] * np.pi))

# As we want to visualize the function u, we have to create a grid to
# attached the dof values to We do this by creating a topology and
# attached the DoF values to We do this by creating a topology and
# geometry based on the function space V
cells, types, x = plot.create_vtk_mesh(V)
grid = pyvista.UnstructuredGrid(cells, types, x)
Expand Down Expand Up @@ -90,7 +90,7 @@ def plot_scalar():
subplotter.show()


# ## MeshTags and using subplots
# ## Mesh tags and using subplots

def plot_meshtags():

Expand All @@ -103,7 +103,7 @@ def in_circle(x):
"""True for points inside circle with radius 2"""
return np.array((x.T[0] - 0.5)**2 + (x.T[1] - 0.5)**2 < 0.2**2, dtype=np.int32)

# Create a dolfinx.MeshTag for all cells. If midpoint is inside the
# Create mesh tags for all cells. If midpoint is inside the
# circle, it gets value 1, otherwise 0.
num_cells = msh.topology.index_map(msh.topology.dim).size_local
midpoints = compute_midpoints(msh, msh.topology.dim, list(np.arange(num_cells, dtype=np.int32)))
Expand All @@ -112,13 +112,13 @@ def in_circle(x):
cells, types, x = plot.create_vtk_mesh(msh, msh.topology.dim)
grid = pyvista.UnstructuredGrid(cells, types, x)

# As the dolfinx.MeshTag contains a value for every cell in the
# As the mesh tags contain a value for every cell in the
# geometry, we can attach it directly to the grid
grid.cell_data["Marker"] = cell_tags.values
grid.set_active_scalars("Marker")

# We create a plotter consisting of two windows, and add a plot of the
# Meshtags to the first window.
# mesh tags to the first window.
subplotter = pyvista.Plotter(shape=(1, 2))
subplotter.subplot(0, 0)
subplotter.add_text("Mesh with markers", font_size=14, color="black", position="upper_edge")
Expand All @@ -127,7 +127,7 @@ def in_circle(x):

# We can also visualize subsets of data, by creating a smaller topology,
# only consisting of those entities that has value one in the
# dolfinx.MeshTag
# mesh tags
cells, types, x = plot.create_vtk_mesh(
msh, msh.topology.dim, cell_tags.indices[cell_tags.values == 1])

Expand Down Expand Up @@ -160,14 +160,14 @@ def in_circle(x):
"""Mark sphere with radius < sqrt(2)"""
return np.array((x.T[0] - 0.5)**2 + (x.T[1] - 0.5)**2 < 0.2**2, dtype=np.int32)

# Create a dolfinx.MeshTag for all cells. If midpoint is inside the
# Create mesh tags for all cells. If midpoint is inside the
# circle, it gets value 1, otherwise 0.
num_cells = msh.topology.index_map(msh.topology.dim).size_local
midpoints = compute_midpoints(msh, msh.topology.dim, list(np.arange(num_cells, dtype=np.int32)))
cell_tags = meshtags(msh, msh.topology.dim, np.arange(num_cells), in_circle(midpoints))

# We start by interpolating a discontinuous function into a second order
# discontinuous Lagrange space Note that we use the `cell_tags` from
# discontinuous Lagrange space. Note that we use the `cell_tags` from
# the previous section to get the cells for each of the regions
cells0 = cell_tags.indices[cell_tags.values == 0]
cells1 = cell_tags.indices[cell_tags.values == 1]
Expand Down
4 changes: 2 additions & 2 deletions python/demo/demo_static-condensation.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@


def sigma_u(u):
"""Consitutive relation for stress-strain. Assuming plane-stress in XY"""
"""Constitutive relation for stress-strain. Assuming plane-stress in XY"""
eps = 0.5 * (ufl.grad(u) + ufl.grad(u).T)
sigma = E / (1. - nu ** 2) * ((1. - nu) * eps + nu * ufl.Identity(2) * ufl.tr(eps))
return sigma
Expand Down Expand Up @@ -117,7 +117,7 @@ def sigma_u(u):

@numba.cfunc(c_signature, nopython=True)
def tabulate_condensed_tensor_A(A_, w_, c_, coords_, entity_local_index, permutation=ffi.NULL):
# Prepare target condensed local elem tensor
# Prepare target condensed local element tensor
A = numba.carray(A_, (Usize, Usize), dtype=PETSc.ScalarType)

# Tabulate all sub blocks locally
Expand Down
41 changes: 22 additions & 19 deletions python/demo/demo_stokes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# # Stokes equations with Taylor-Hood elements
#
# This demo show how to solve the Stokes problem using Taylor-Hood
# This demo shows how to solve the Stokes problem using Taylor-Hood
# elements with a range of different linear solvers.
#
# ## Equation and problem definition
Expand All @@ -23,11 +23,11 @@
# \nabla \cdot u &= 0 \quad {\rm in} \ \Omega.
# $$
#
# :::{note}
# ```{note}
# The sign of the pressure has been flipped from the classical
# definition. This is done in order to have a symmetric system
# of equations rather than a non-symmetric system of equations.
# :::
# ```
#
# A typical set of boundary conditions on the boundary $\partial
# \Omega = \Gamma_{D} \cup \Gamma_{N}$ can be:
Expand All @@ -40,7 +40,7 @@
#
# ### Weak formulation
#
# We formulate the Stokes equations mixed variational form; that is, a
# We formulate the Stokes equations' mixed variational form; that is, a
# form where the two variables, the velocity and the pressure, are
# approximated. We have the problem: find $(u, p) \in W$ such that
#
Expand All @@ -58,12 +58,12 @@
# \Omega_N} g \cdot v \, {\rm d} s.
# $$
#
# The space $W$ is mixed (product) function space $W = V
# The space $W$ is a mixed (product) function space $W = V
# \times Q$, such that $u \in V$ and $q \in Q$.
#
# ### Domain and boundary conditions
#
# We shall the lid-driven cavity problem with the following definitions
# We define the lid-driven cavity problem with the following
# domain and boundary conditions:
#
# - $\Omega = [0,1]\times[0,1]$ (a unit square)
Expand All @@ -74,7 +74,7 @@
#
# ## Implementation
#
# We first import the modules and function that the program uses:
# We first import the modules and functions that the program uses:

# +
import numpy as np
Expand All @@ -94,7 +94,10 @@
from petsc4py import PETSc
# -

# We create a Mesh and attach a coordinate map to the mesh:
# We create a {py:class}`Mesh <dolfinx.mesh.Mesh>`, define functions to
# geometrically locate subsets of its boundary and define a function
# describing the velocity to be imposed as a boundary condition in a lid
# driven cavity problem:

# +
# Create mesh
Expand Down Expand Up @@ -122,16 +125,16 @@ def lid_velocity_expression(x):
# -

# We define two {py:class}`FunctionSpace <dolfinx.fem.FunctionSpace>`
# instances with different finite elements. `P2` corresponds to
# piecewise quadratics for the velocity field and `P1` to continuous
# piecewise linears for the pressure field:
# instances with different finite elements. `P2` corresponds to a continuous
# piecewise quadratic basis for the velocity field and `P1` to a continuous
# piecewise linear basis for the pressure field:


P2 = ufl.VectorElement("Lagrange", msh.ufl_cell(), 2)
P1 = ufl.FiniteElement("Lagrange", msh.ufl_cell(), 1)
V, Q = FunctionSpace(msh, P2), FunctionSpace(msh, P1)

# We can define boundary conditions:
# We define boundary conditions:

# +
# No-slip boundary condition for velocity field (`V`) on boundaries
Expand Down Expand Up @@ -207,7 +210,7 @@ def lid_velocity_expression(x):
fem.petsc.set_bc_nest(b, bcs0)
# -

# Ths pressure field for this problem is determined only up to a
# The pressure field for this problem is determined only up to a
# constant. We can supply the vector that spans the nullspace and any
# component of the solution in this direction will be eliminated during
# the iterative linear solution process.
Expand Down Expand Up @@ -259,7 +262,7 @@ def lid_velocity_expression(x):
# -

# To compute the solution, we create finite element {py:class}`Function
# <fem.Function>` for the velocity (on the space `V`) and
# <dolfinx.fem.Function>` for the velocity (on the space `V`) and
# for the pressure (on the space `Q`). The vectors for `u` and `p` are
# combined to form a nested vector and the system is solved:

Expand All @@ -276,7 +279,7 @@ def lid_velocity_expression(x):
print("(A) Norm of pressure coefficient vector (nested, iterative): {}".format(norm_p_0))

# The solution fields can be saved to file in XDMF format for
# visualization, e.g. with ParView. Before writing to file, ghost values
# visualization, e.g. with ParaView. Before writing to file, ghost values
# are updated.

# +
Expand All @@ -303,7 +306,7 @@ def lid_velocity_expression(x):
P.assemble()
b = fem.petsc.assemble_vector_block(L, a, bcs=bcs)

# Set near null space for pressure
# Set near nullspace for pressure
null_vec = A.createVecLeft()
offset = V.dofmap.index_map.size_local * V.dofmap.index_map_bs
null_vec.array[offset:] = 1.0
Expand Down Expand Up @@ -345,7 +348,7 @@ def lid_velocity_expression(x):
ksp.setFromOptions()
# -

# We also need to create a block vector,\`\`x\`\`, to store the (full)
# We also need to create a block vector, `x`, to store the (full)
# solution, which we initialize using the block RHS form `L`.

# +
Expand All @@ -366,7 +369,7 @@ def lid_velocity_expression(x):
norm_p_1 = p.x.norm()
if MPI.COMM_WORLD.rank == 0:
print("(B) Norm of velocity coefficient vector (blocked, iterative): {}".format(norm_u_1))
print("(B) Norm of pressure coefficient vector (blocked, interative): {}".format(norm_p_1))
print("(B) Norm of pressure coefficient vector (blocked, iterative): {}".format(norm_p_1))
assert np.isclose(norm_u_1, norm_u_0)
assert np.isclose(norm_p_1, norm_p_0)

Expand All @@ -381,7 +384,7 @@ def lid_velocity_expression(x):
ksp.getPC().setType("lu")
ksp.getPC().setFactorSolverType("superlu_dist")

# We also need to create a block vector,\`\`x\`\`, to store the (full)
# We also need to create a block vector, `x`, to store the (full)
# solution, which we initialize using the block RHS form `L`.

# +
Expand Down
4 changes: 2 additions & 2 deletions python/demo/demo_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

# -

# SciPy solvers do no support MPI, so all computations are performed on
# SciPy solvers do not support MPI, so all computations are performed on
# a single MPI rank

# +
Expand All @@ -41,7 +41,7 @@
cell_type=mesh.CellType.triangle)
V = fem.FunctionSpace(msh, ("Lagrange", 1))

# Define a variartional problem
# Define a variational problem
u, v = ufl.TrialFunction(V), ufl.TestFunction(V)
x = ufl.SpatialCoordinate(msh)
fr = 10 * ufl.exp(-((x[0] - 0.5) ** 2 + (x[1] - 0.5) ** 2) / 0.02)
Expand Down