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