Skip to content

Commit 10c590c

Browse files
committed
broken loops in 2d but working jitback for 2d ed bueler example
1 parent 154a4dc commit 10c590c

5 files changed

Lines changed: 810 additions & 6 deletions

File tree

devito/petsc/iet/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def _create_dmda_calls(self, dmda):
361361
)
362362

363363
set_point_bcs = petsc_call(
364-
self.callback_builder._point_bc_efunc.name, [dmda, Byref(sobjs['numBC'])]
364+
self.callback_builder._point_bc_efunc.name, [dmda, sobjs['numBC']]
365365
)
366366

367367
get_local_section = petsc_call('DMGetLocalSection', [dmda, Byref(sobjs['lsection'])])

devito/petsc/types/equation.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,23 @@ class ConstrainBC(EssentialBC):
5959
# return Inc.__new__(Inc, *args, **kwargs)
6060

6161

62-
class NoOfEssentialBC(ConstrainBC, Inc):
62+
class NoOfEssentialBC(ConstrainBC):
6363
"""Equation used count essential boundary condition nodes.
6464
This type of equation is generated inside
6565
petscsolve if the user sets `constrain_bcs=True`."""
66-
def __new__(cls, *args, **kwargs):
67-
return Inc.__new__(Inc, *args, **kwargs)
66+
# def __new__(cls, *args, **kwargs):
67+
# return Inc.__new__(Inc, *args, **kwargs)
68+
pass
69+
70+
71+
# class NoOfEssentialBC(Inc, ConstrainBC):
72+
# """
73+
# Equation used to count essential boundary condition nodes.
74+
# """
75+
76+
# def __new__(cls, *args, **kwargs):
77+
# obj = super().__new__(cls, *args, **kwargs)
78+
# return obj
6879

6980

7081
class PointEssentialBC(ConstrainBC):

devito/petsc/types/metadata.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,13 @@ def _make_increment_expr(self, expr):
760760
Make the Eq that is used to increment the number of essential
761761
boundary nodes in the generated ccode.
762762
"""
763-
# rhssymb = PetscInt(name='rhssymb')
764763
if isinstance(expr, EssentialBC):
765764
assert expr.lhs == self.target
765+
from math import prod
766+
767+
rhs = prod(expr.rhs.dimensions)
766768
return NoOfEssentialBC(
767-
TempSymb, expr.rhs,
769+
TempSymb, rhs,
768770
subdomain=expr.subdomain
769771
)
770772
else:
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)