|
| 1 | +import os |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from devito import (Grid, Function, Eq, Operator, switchconfig, |
| 5 | + configuration, SubDomain, norm, mmax) |
| 6 | + |
| 7 | +from devito.petsc import petscsolve, EssentialBC |
| 8 | +from devito.petsc.initialize import PetscInitialize |
| 9 | + |
| 10 | +import matplotlib.pyplot as plt |
| 11 | + |
| 12 | +configuration['compiler'] = 'custom' |
| 13 | +os.environ['CC'] = 'mpicc' |
| 14 | + |
| 15 | + |
| 16 | +# 2D test |
| 17 | +# Solving -u_xx - u_yy = f(x,y) |
| 18 | +# Dirichlet BCs: u(0,y) = 0, u(1,y)=-e^y, u(x,0) = -x, u(x,1)=-xe |
| 19 | +# Manufactured solution: u(x,y) = -xe^(y), with corresponding RHS f(x,y) = xe^(y) |
| 20 | +# ref - https://github.com/bueler/p4pdes/blob/master/c/ch6/fish.c |
| 21 | + |
| 22 | +PetscInitialize() |
| 23 | + |
| 24 | +# Subdomains to implement BCs |
| 25 | +class SubTop(SubDomain): |
| 26 | + name = 'subtop' |
| 27 | + |
| 28 | + def define(self, dimensions): |
| 29 | + x, y = dimensions |
| 30 | + return {x: ('middle', 1, 1), y: ('right', 1)} |
| 31 | + |
| 32 | + |
| 33 | +class SubBottom(SubDomain): |
| 34 | + name = 'subbottom' |
| 35 | + |
| 36 | + def define(self, dimensions): |
| 37 | + x, y = dimensions |
| 38 | + return {x: ('middle', 1, 1), y: ('left', 1)} |
| 39 | + |
| 40 | + |
| 41 | +class SubLeft(SubDomain): |
| 42 | + name = 'subleft' |
| 43 | + |
| 44 | + def define(self, dimensions): |
| 45 | + x, y = dimensions |
| 46 | + return {x: ('left', 1), y: y} |
| 47 | + |
| 48 | + |
| 49 | +class SubRight(SubDomain): |
| 50 | + name = 'subright' |
| 51 | + |
| 52 | + def define(self, dimensions): |
| 53 | + x, y = dimensions |
| 54 | + return {x: ('right', 1), y: y} |
| 55 | + |
| 56 | + |
| 57 | +sub1 = SubTop() |
| 58 | +sub2 = SubBottom() |
| 59 | +sub3 = SubLeft() |
| 60 | +sub4 = SubRight() |
| 61 | + |
| 62 | +subdomains = (sub1, sub2, sub3, sub4) |
| 63 | + |
| 64 | +def exact(x, y): |
| 65 | + return -x*np.float64(np.exp(y)) |
| 66 | + |
| 67 | +Lx = np.float64(1.) |
| 68 | +Ly = np.float64(1.) |
| 69 | + |
| 70 | +n = 17 |
| 71 | +h = Lx/(n-1) |
| 72 | + |
| 73 | + |
| 74 | +grid = Grid( |
| 75 | + shape=(n, n), extent=(Lx, Ly), subdomains=subdomains, dtype=np.float64 |
| 76 | +) |
| 77 | + |
| 78 | +u = Function(name='u', grid=grid, space_order=2) |
| 79 | +f = Function(name='f', grid=grid, space_order=2) |
| 80 | +bc = Function(name='bc', grid=grid, space_order=2) |
| 81 | + |
| 82 | +eqn = Eq(-u.laplace, f, subdomain=grid.interior) |
| 83 | + |
| 84 | +tmpx = np.linspace(0, Lx, n).astype(np.float64) |
| 85 | +tmpy = np.linspace(0, Ly, n).astype(np.float64) |
| 86 | + |
| 87 | +Y, X = np.meshgrid(tmpx, tmpy) |
| 88 | + |
| 89 | +f.data[:] = X*np.float64(np.exp(Y)) |
| 90 | + |
| 91 | +bc.data[0, :] = 0. |
| 92 | +bc.data[-1, :] = -np.exp(tmpy) |
| 93 | +bc.data[:, 0] = -tmpx |
| 94 | +bc.data[:, -1] = -tmpx*np.exp(1) |
| 95 | + |
| 96 | +# # Create boundary condition expressions using subdomains |
| 97 | +bcs = [EssentialBC(u, bc, subdomain=sub1)] |
| 98 | +bcs += [EssentialBC(u, bc, subdomain=sub2)] |
| 99 | +bcs += [EssentialBC(u, bc, subdomain=sub3)] |
| 100 | +bcs += [EssentialBC(u, bc, subdomain=sub4)] |
| 101 | + |
| 102 | +exprs = [eqn] + bcs |
| 103 | +petsc = petscsolve( |
| 104 | + exprs, target=u, |
| 105 | + solver_parameters={'ksp_rtol': 1e-12, 'ksp_type': 'cg', 'pc_type': 'none'}, |
| 106 | + options_prefix='poisson_2d', |
| 107 | + constrain_bcs=True |
| 108 | +) |
| 109 | + |
| 110 | +with switchconfig(log_level='DEBUG'): |
| 111 | + op = Operator(petsc, language='petsc') |
| 112 | + summary = op.apply() |
| 113 | + # print(op.arguments()) |
| 114 | + |
| 115 | + |
| 116 | +# print(op.ccode) |
| 117 | +# iters = summary.petsc[('section0', 'poisson_2d')].KSPGetIterationNumber |
| 118 | + |
| 119 | +u_exact = Function(name='u_exact', grid=grid, space_order=2) |
| 120 | +u_exact.data[:] = exact(X, Y) |
| 121 | +print(u_exact) |
| 122 | + |
| 123 | +diff = Function(name='diff', grid=grid, space_order=2) |
| 124 | +diff.data[:] = u_exact.data[:] - u.data[:] |
| 125 | + |
| 126 | +# # Compute infinity norm using numpy |
| 127 | +# # TODO: Figure out how to compute the infinity norm using Devito |
| 128 | +infinity_norm = np.linalg.norm(diff.data[:].ravel(), ord=np.inf) |
| 129 | +print(f"Infinity Norm={infinity_norm}") |
| 130 | + |
| 131 | +# # Compute discrete L2 norm (RMS error) |
| 132 | +n_interior = np.prod([s - 1 for s in grid.shape]) |
| 133 | +discrete_l2_norm = norm(diff) / np.sqrt(n_interior) |
| 134 | +print(f"Discrete L2 Norm={discrete_l2_norm}") |
0 commit comments