Skip to content

Commit 91564c0

Browse files
committed
compiler: Fix constrain_essential_bcs with implicit_dims
1 parent 10c590c commit 91564c0

5 files changed

Lines changed: 46 additions & 54 deletions

File tree

devito/ir/equations/equation.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,14 @@ def detect(cls, expr):
107107
ReduceMin: OpMin,
108108
PetscEq: OpPetsc
109109
}
110-
try:
111-
return reduction_mapper[type(expr)]
112-
except KeyError:
113-
pass
110+
# try:
111+
# return reduction_mapper[type(expr)]
112+
# except KeyError:
113+
# pass
114+
115+
for expr_type, op in reduction_mapper.items():
116+
if isinstance(expr, expr_type):
117+
return op
114118

115119
# NOTE: in the future we might want to track down other kinds
116120
# of operations here (e.g., memcpy). However, we don't care for

devito/petsc/equations.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,34 @@ def constrain_essential_bcs(expressions, **kwargs):
2222

2323
# build mapper
2424
for e in expressions:
25-
# from IPython import embed; embed()
2625
if not isinstance(e, ConstrainBC):
26+
new_exprs.append(e)
2727
continue
2828

2929
indexeds = retrieve_indexed(e)
3030
dims = retrieve_dimensions([i for j in indexeds for i in j.indices], mode='unique')
31-
31+
# implicit_dims = set(e.implicit_dims)
32+
dims.update(e.implicit_dims)
33+
# from IPython import embed; embed()
3234
dims = [d for d in dims if d.is_Sub and not d.local]
3335

36+
if not dims:
37+
new_exprs.append(e)
38+
continue
39+
3440
for d in dims:
3541
# replace the dim with a new one that has a different symbolic_min and symbolic_max
3642

3743
# obvs shouldn't be obtained from indexeds[0], but how should it be obtained?
3844
# USE e.lhs function -> the one that the BC is being applied to
3945
# from IPython import embed; embed()
4046
# f._size_nodomain.left
41-
halo_size_left = indexeds[0].function._size_halo[d].left
42-
halo_size_right = indexeds[0].function._size_halo[d].right
47+
# from IPython import embed; embed()
48+
# halo_size_left = indexeds[0].function._size_halo[d].left
49+
# halo_size_right = indexeds[0].function._size_halo[d].right
4350

51+
halo_size_left = 2
52+
halo_size_right = 2
4453

4554
from devito.petsc.types.dimension import SubDimMax, SubDimMin
4655

@@ -52,7 +61,7 @@ def constrain_essential_bcs(expressions, **kwargs):
5261
subdim_max = SubDimMax(sregistry.make_name(prefix=d.name + '_max'), subdim=d, thickness=d.thickness)
5362
subdim_min = SubDimMin(sregistry.make_name(prefix=d.name + '_min'), subdim=d, thickness=d.thickness)
5463

55-
# from IPython import embed; embed()
64+
# unique_name
5665
new_dim = CustomBoundSubDimension(
5766
name=d.name,
5867
parent=d.parent,
@@ -63,13 +72,12 @@ def constrain_essential_bcs(expressions, **kwargs):
6372
)
6473
mapper[d] = new_dim
6574

66-
# build new expressions
67-
for e in expressions:
68-
if isinstance(e, ConstrainBC):
69-
new_e = e.subs(mapper)
70-
new_exprs.append(new_e)
71-
72-
else:
73-
new_exprs.append(e)
74-
75+
new_e = e.subs(mapper)
76+
if e.implicit_dims:
77+
# from devito.symbolics import uxreplace
78+
implicit_dims_new = tuple(mapper.get(d, d) for d in e.implicit_dims)
79+
# from IPython import embed; embed()
80+
new_e = new_e._rebuild(implicit_dims=implicit_dims_new)
81+
new_exprs.append(new_e)
82+
7583
return new_exprs

devito/petsc/types/equation.py

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ class EssentialBC(Eq):
88
"""
99
Represents an essential boundary condition for use with `petscsolve`.
1010
11-
Due to ongoing work on PetscSection and DMDA integration (WIP),
12-
these conditions are imposed as trivial equations. The compiler
13-
will automatically zero the corresponding rows/columns in the Jacobian
14-
and lift the boundary terms into the residual RHS.
11+
The compiler will automatically zero the corresponding rows/columns in the Jacobian
12+
and lift the boundary terms into the residual RHS, unless the user
13+
specifies `constrain_bcs=True` to `petscsolve`.
1514
1615
Note:
1716
- To define an essential boundary condition, use:
@@ -47,37 +46,14 @@ class ZeroColumn(EssentialBC):
4746

4847
class ConstrainBC(EssentialBC):
4948
pass
50-
51-
52-
# class NoOfEssentialBC(ConstrainBC, Inc):
53-
# """
54-
# Equation used count essential boundary condition nodes.
55-
# This type of equation is generated inside
56-
# petscsolve if the user sets `constrain_bcs=True`.
57-
# """
58-
# def __new__(cls, *args, **kwargs):
59-
# return Inc.__new__(Inc, *args, **kwargs)
6049

6150

62-
class NoOfEssentialBC(ConstrainBC):
51+
class NoOfEssentialBC(Inc, ConstrainBC):
6352
"""Equation used count essential boundary condition nodes.
6453
This type of equation is generated inside
6554
petscsolve if the user sets `constrain_bcs=True`."""
66-
# def __new__(cls, *args, **kwargs):
67-
# return Inc.__new__(Inc, *args, **kwargs)
6855
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
7956

8057

8158
class PointEssentialBC(ConstrainBC):
8259
pass
83-

devito/petsc/types/metadata.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -762,12 +762,14 @@ def _make_increment_expr(self, expr):
762762
"""
763763
if isinstance(expr, EssentialBC):
764764
assert expr.lhs == self.target
765-
from math import prod
766-
767-
rhs = prod(expr.rhs.dimensions)
765+
# return NoOfEssentialBC(
766+
# TempSymb, expr.rhs,
767+
# subdomain=expr.subdomain,
768+
# )
768769
return NoOfEssentialBC(
769-
TempSymb, rhs,
770-
subdomain=expr.subdomain
770+
TempSymb, 1,
771+
subdomain=expr.subdomain,
772+
implicit_dims=expr.subdomain.dimensions
771773
)
772774
else:
773775
return None
@@ -791,7 +793,7 @@ def _make_point_bc_expr(self, expr):
791793
numBC = PetscInt(name='numBC2')
792794
if isinstance(expr, EssentialBC):
793795
assert expr.lhs == self.target
794-
return NoOfEssentialBC(
796+
return PointEssentialBC(
795797
TempSymb, expr.rhs,
796798
subdomain=expr.subdomain
797799
)

examples/petsc/Poisson/ed_bueler_2d.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def exact(x, y):
9999
bcs += [EssentialBC(u, bc, subdomain=sub3)]
100100
bcs += [EssentialBC(u, bc, subdomain=sub4)]
101101

102+
103+
102104
exprs = [eqn] + bcs
103105
petsc = petscsolve(
104106
exprs, target=u,
@@ -109,11 +111,11 @@ def exact(x, y):
109111

110112
with switchconfig(log_level='DEBUG'):
111113
op = Operator(petsc, language='petsc')
112-
summary = op.apply()
114+
# summary = op.apply()
113115
# print(op.arguments())
114116

115117

116-
# print(op.ccode)
118+
print(op.ccode)
117119
# iters = summary.petsc[('section0', 'poisson_2d')].KSPGetIterationNumber
118120

119121
u_exact = Function(name='u_exact', grid=grid, space_order=2)

0 commit comments

Comments
 (0)