-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
30 lines (26 loc) · 1.01 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import numpy as np
from dolfinx.fem import (assemble_scalar, form, functionspace,
Function)
from mpi4py import MPI
from ufl import dx, inner
from ufl.core.expr import Expr
from dolfinx.io import VTXWriter
def save_function(v, filename):
"""Save a function v to file. The function is interpolated into a
discontinuous Lagrange space so that functions in Nedelec and
Raviart-Thomas spaces can be visualised exactly"""
mesh = v.function_space.mesh
k = v.function_space.ufl_element().degree
# NOTE: Alternatively could pass this into function so it doesn't need
# to be created each time
W = functionspace(mesh, ("Discontinuous Lagrange", k, (mesh.geometry.dim,)))
w = Function(W)
w.name = v.name
w.interpolate(v)
with VTXWriter(mesh.comm, filename, [w]) as file:
file.write(0.0)
def L2_norm(v: Expr):
"""Computes the L2-norm of v
"""
return np.sqrt(MPI.COMM_WORLD.allreduce(
assemble_scalar(form(inner(v, v) * dx)), op=MPI.SUM))