|
| 1 | +import os |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from devito import (Grid, Function, Eq, Operator, SubDomain, |
| 5 | + configuration, switchconfig) |
| 6 | +from devito.petsc import PETScSolve, EssentialBC |
| 7 | +from devito.petsc.initialize import PetscInitialize |
| 8 | +configuration['compiler'] = 'custom' |
| 9 | +os.environ['CC'] = 'mpicc' |
| 10 | + |
| 11 | +PetscInitialize() |
| 12 | + |
| 13 | +# Laplace equation, solving phi.laplace = 0 |
| 14 | + |
| 15 | +# Constant Dirichlet BCs: |
| 16 | +# phi(x, 0) = 0 |
| 17 | +# phi(0, y) = 0 |
| 18 | +# phi(1, y) = 0 |
| 19 | +# phi(x, 1) = f(x) = sin(pi*x) |
| 20 | + |
| 21 | +# The analytical solution is: |
| 22 | +# phi(x, y) = sinh(pi*y)*sin(pi*x)/sinh(pi) |
| 23 | + |
| 24 | + |
| 25 | +# Subdomains to implement BCs |
| 26 | +class SubTop(SubDomain): |
| 27 | + name = 'subtop' |
| 28 | + |
| 29 | + def define(self, dimensions): |
| 30 | + x, y = dimensions |
| 31 | + return {x: x, y: ('right', 1)} |
| 32 | + |
| 33 | + |
| 34 | +class SubBottom(SubDomain): |
| 35 | + name = 'subbottom' |
| 36 | + |
| 37 | + def define(self, dimensions): |
| 38 | + x, y = dimensions |
| 39 | + return {x: x, y: ('left', 1)} |
| 40 | + |
| 41 | + |
| 42 | +class SubLeft(SubDomain): |
| 43 | + name = 'subleft' |
| 44 | + |
| 45 | + def define(self, dimensions): |
| 46 | + x, y = dimensions |
| 47 | + return {x: ('left', 1), y: y} |
| 48 | + |
| 49 | + |
| 50 | +class SubRight(SubDomain): |
| 51 | + name = 'subright' |
| 52 | + |
| 53 | + def define(self, dimensions): |
| 54 | + x, y = dimensions |
| 55 | + return {x: ('right', 1), y: y} |
| 56 | + |
| 57 | + |
| 58 | +sub1 = SubTop() |
| 59 | +sub2 = SubBottom() |
| 60 | +sub3 = SubLeft() |
| 61 | +sub4 = SubRight() |
| 62 | + |
| 63 | +subdomains = (sub1, sub2, sub3, sub4) |
| 64 | + |
| 65 | + |
| 66 | +Lx = np.float64(1.) |
| 67 | +Ly = np.float64(1.) |
| 68 | + |
| 69 | + |
| 70 | +def analytical(x, y, Lx, Ly): |
| 71 | + tmp = np.float64(np.pi)/Lx |
| 72 | + numerator = np.float64(np.sinh(tmp*y)) * np.float64(np.sin(tmp*x)) |
| 73 | + return numerator / np.float64(np.sinh(tmp*Ly)) |
| 74 | + |
| 75 | + |
| 76 | +n_values = list(range(13, 174, 10)) |
| 77 | +dx = np.array([Lx/(n-1) for n in n_values]) |
| 78 | +errors = [] |
| 79 | + |
| 80 | + |
| 81 | +for n in n_values: |
| 82 | + |
| 83 | + grid = Grid( |
| 84 | + shape=(n, n), extent=(Lx, Ly), subdomains=subdomains, dtype=np.float64 |
| 85 | + ) |
| 86 | + |
| 87 | + phi = Function(name='phi', grid=grid, space_order=2, dtype=np.float64) |
| 88 | + rhs = Function(name='rhs', grid=grid, space_order=2, dtype=np.float64) |
| 89 | + |
| 90 | + phi.data[:] = np.float64(0.0) |
| 91 | + rhs.data[:] = np.float64(0.0) |
| 92 | + |
| 93 | + eqn = Eq(rhs, phi.laplace, subdomain=grid.interior) |
| 94 | + |
| 95 | + tmpx = np.linspace(0, Lx, n).astype(np.float64) |
| 96 | + tmpy = np.linspace(0, Ly, n).astype(np.float64) |
| 97 | + Y, X = np.meshgrid(tmpx, tmpy) |
| 98 | + |
| 99 | + # Create boundary condition expressions using subdomains |
| 100 | + bc_func = Function(name='bcs', grid=grid, space_order=2, dtype=np.float64) |
| 101 | + bc_func.data[:] = np.float64(0.0) |
| 102 | + bc_func.data[:, -1] = np.float64(np.sin(tmpx*np.pi)) |
| 103 | + |
| 104 | + bcs = [EssentialBC(phi, bc_func, subdomain=sub1)] # top |
| 105 | + bcs += [EssentialBC(phi, bc_func, subdomain=sub2)] # bottom |
| 106 | + bcs += [EssentialBC(phi, bc_func, subdomain=sub3)] # left |
| 107 | + bcs += [EssentialBC(phi, bc_func, subdomain=sub4)] # right |
| 108 | + |
| 109 | + exprs = [eqn] + bcs |
| 110 | + petsc = PETScSolve(exprs, target=phi, solver_parameters={'ksp_rtol': 1e-8}) |
| 111 | + |
| 112 | + with switchconfig(language='petsc'): |
| 113 | + op = Operator(petsc) |
| 114 | + op.apply() |
| 115 | + |
| 116 | + phi_analytical = analytical(X, Y, Lx, Ly) |
| 117 | + |
| 118 | + diff = phi_analytical[1:-1, 1:-1] - phi.data[1:-1, 1:-1] |
| 119 | + error = np.linalg.norm(diff) / np.linalg.norm(phi_analytical[1:-1, 1:-1]) |
| 120 | + errors.append(error) |
| 121 | + |
| 122 | +slope, _ = np.polyfit(np.log(dx), np.log(errors), 1) |
| 123 | + |
| 124 | +assert slope > 1.9 |
| 125 | +assert slope < 2.1 |
0 commit comments