-
Notifications
You must be signed in to change notification settings - Fork 0
/
num_ex_1.py
60 lines (48 loc) · 1.84 KB
/
num_ex_1.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import numpy as np
from hdg_stokes import Scheme, solve
from mpi4py import MPI
from utils import create_trap_mesh
from dolfinx import mesh
import ufl
def gamma_marker(x):
"A marker function to identify the domain boundary"
return np.isclose(x[0], 0.0) | np.isclose(x[0], 1.0) | \
np.isclose(x[1], 0.0) | np.isclose(x[1], 1.0)
def u_e(x, module=ufl):
"Expression for the exact velocity"
u = (module.sin(module.pi * x[0]) * module.sin(module.pi * x[1]),
module.cos(module.pi * x[0]) * module.cos(module.pi * x[1]))
if module == ufl:
return ufl.as_vector(u)
else:
assert module == np
return np.vstack(u)
def p_e(x, module=ufl):
"Expression for the exact pressure"
return module.sin(module.pi * x[0]) * module.cos(module.pi * x[1])
if __name__ == "__main__":
# Simulation parameters
n = 16 # Number of cells in each direction
k = 2 # Polynomial degree
nu = 1.0 # Kinermatic viscosity
scheme = Scheme.DRW # Numerical scheme (RW or DRW)
# Create trapezium shaped mesh
comm = MPI.COMM_WORLD
msh = create_trap_mesh(
comm, (n, n), ((0, 0), (1, 1)), offset_scale=0.25,
ghost_mode=mesh.GhostMode.none)
# Boundary conditions
boundary_ids = {"gamma": 1}
boundary_conditions = {"gamma": lambda x: u_e(x, module=np)}
boundary_facets = mesh.locate_entities_boundary(
msh, msh.topology.dim - 1, gamma_marker)
values = np.full_like(
boundary_facets, boundary_ids["gamma"], dtype=np.intc)
perm = np.argsort(boundary_facets)
mt = mesh.meshtags(
msh, msh.topology.dim - 1, boundary_facets[perm], values[perm])
# Right-hand side
x = ufl.SpatialCoordinate(msh)
f = - nu * ufl.div(ufl.grad(u_e(x))) + ufl.grad(p_e(x))
solve(k, nu, scheme, msh, mt, boundary_ids,
boundary_conditions, f, u_e, p_e)