-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffusion_solver_example.py
More file actions
45 lines (36 loc) · 1.53 KB
/
Copy pathdiffusion_solver_example.py
File metadata and controls
45 lines (36 loc) · 1.53 KB
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
"""Advance the same manufactured diffusion problem with FD and PU solvers."""
import numpy as np
from kernelpack import solvers
from _common import build_square_domain, exact_solution, unit_dirichlet, zero_neumann
def run_solver(solver, domain) -> np.ndarray:
dt = 0.02
diffusivity = 0.25
points = domain.get_int_bdry_nodes()
forcing = lambda nu, time, x: (
-np.exp(-time) * (x[:, 0] ** 2 + x[:, 1] ** 2)
- 4.0 * nu * np.exp(-time)
)
boundary = lambda alpha, beta, normals, time, xb: exact_solution(time, xb)
solver.init(domain, 3, dt, diffusivity)
solver.set_initial_state(exact_solution(0.0, points))
solver.bdf1_step(dt, forcing, zero_neumann, unit_dirichlet, boundary)
solver.bdf2_step(2.0 * dt, forcing, zero_neumann, unit_dirichlet, boundary)
return solver.bdf3_step(3.0 * dt, forcing, zero_neumann, unit_dirichlet, boundary)
def main() -> None:
domain = build_square_domain()
points = domain.get_int_bdry_nodes()
fd_solution = run_solver(
solvers.DiffusionSolver(
lap_assembler="fd",
bc_assembler="fd",
lap_stencil="wls",
bc_stencil="wls",
),
domain,
)
pu_solution = run_solver(solvers.PUDiffusionSolver(), domain)
truth = exact_solution(0.06, points)
print(f"FD relative l2 error: {np.linalg.norm(fd_solution - truth) / np.linalg.norm(truth):.6e}")
print(f"PU relative l2 error: {np.linalg.norm(pu_solution - truth) / np.linalg.norm(truth):.6e}")
if __name__ == "__main__":
main()