-
Notifications
You must be signed in to change notification settings - Fork 0
/
boundary_conditions.py
101 lines (78 loc) · 2.98 KB
/
boundary_conditions.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import numpy as np
from dolfinx import mesh, fem
from collections.abc import Callable
from petsc4py.PETSc import ScalarType
def clamped_boundary(domain, V, parameters):
fdim = domain.topology.dim - 1
boundary_facets = mesh.locate_entities_boundary(domain, fdim, boundary_side(parameters["side_coord"],parameters["coord"]))
u_D = np.array([0,0,0], dtype=ScalarType)
bc = fem.dirichletbc(u_D, fem.locate_dofs_topological(V, fdim, boundary_facets), V)
return bc
def pinned_boundary(domain, V, parameters):
fdim = domain.topology.dim - 1
boundary_facets = mesh.locate_entities_boundary(domain, fdim, boundary_side(parameters["side_coord"],parameters["coord"]))
facet_dofs = fem.locate_dofs_topological(V, fdim, boundary_facets)
if 0 in parameters["pinned_coords"]:
dofs_x = V.sub(0).dofmap.list.array[facet_dofs]
else:
dofs_x = None
if 1 in parameters["pinned_coords"]:
dofs_y = V.sub(1).dofmap.list.array[facet_dofs]
else:
dofs_y = None
if 2 in parameters["pinned_coords"]:
dofs_z = V.sub(2).dofmap.list.array[facet_dofs]
else:
dofs_z = None
bc_dofs = np.hstack([dofs_x, dofs_y, dofs_z])
bc_dofs = bc_dofs[bc_dofs != np.array(None)]
bc_dofs = bc_dofs.astype(np.int32)
u_D = fem.Function(V)
with u_D.vector.localForm() as u_local:
u_local.set(0.0)
bc = fem.dirichletbc(u_D, bc_dofs)
return bc
def clamped_edge(domain, V, parameters):
fdim = domain.topology.dim - 2
boundary_facets = mesh.locate_entities_boundary(domain, fdim, boundary_edge(parameters["side_coord_1"],parameters["coord_1"],parameters["side_coord_2"],parameters["coord_2"]))
u_D = np.array([0,0,0], dtype=ScalarType)
bc = fem.dirichletbc(u_D, fem.locate_dofs_topological(V, fdim, boundary_facets), V)
return bc
def boundary_side(side_coord, coord):
return lambda x: np.isclose(x[coord], side_coord)
def boundary_edge(side_coord_1, coord_1, side_coord_2, coord_2):
return lambda x: np.logical_and(np.isclose(x[coord_1], side_coord_1), np.isclose(x[coord_2], side_coord_2))
def boundary_full(x):
return x
def boundary_empty(x):
return None
def point_at(coord) -> Callable:
"""Defines a point. Copied from FEniCSXConcrete.
Args:
coord: points coordinates
Returns:
function defining the boundary
"""
p = to_floats(coord)
def boundary(x):
return np.logical_and(
np.logical_and(np.isclose(x[0], p[0]), np.isclose(x[1], p[1])),
np.isclose(x[2], p[2]),
)
return boundary
def to_floats(x) -> list[float]:
"""Converts `x` to a 3d coordinate. Copied from FEniCSXConcrete.
Args:
x: point coordinates at least 1D
Returns:
point described as list with x,y,z value
"""
floats = []
try:
for v in x:
floats.append(float(v))
while len(floats) < 3:
floats.append(0.0)
except TypeError:
floats = [float(x), 0.0, 0.0]
return floats