|
| 1 | +from devito.symbolics import retrieve_indexed, retrieve_dimensions |
| 2 | +from devito.petsc import EssentialBC |
| 3 | +from devito.types.dimension import CustomBoundSubDimension |
| 4 | +from devito import Min, Max |
| 5 | + |
| 6 | + |
| 7 | +def lower_exprs_petsc(expressions, **kwargs): |
| 8 | + mapper = {} |
| 9 | + |
| 10 | + additional_exprs = [] |
| 11 | + |
| 12 | + # build mapper |
| 13 | + for e in expressions: |
| 14 | + if not isinstance(e, EssentialBC): |
| 15 | + continue |
| 16 | + indexeds = retrieve_indexed(e) |
| 17 | + dims = retrieve_dimensions([i for j in indexeds for i in j.indices], mode='unique') |
| 18 | + |
| 19 | + dims = [d for d in dims if d.is_Sub and not d.local] |
| 20 | + |
| 21 | + for d in dims: |
| 22 | + # replace the dim with a new one that has a different symbolic_min and symbolic_max |
| 23 | + |
| 24 | + # obvs shouldn't be obtained from indexeds[0], but how should it be obtained? |
| 25 | + # USE e.lhs function -> the one that the BC is being applied to |
| 26 | + halo_size_left = indexeds[0].function._size_halo[d].left |
| 27 | + halo_size_right = indexeds[0].function._size_halo[d].right |
| 28 | + |
| 29 | + grid = indexeds[0].function.grid |
| 30 | + |
| 31 | + from devito.petsc.types.dimension import SubDimMax |
| 32 | + |
| 33 | + # TODO: change name.. |
| 34 | + |
| 35 | + # global_rtkn = kwargs.get(d.rtkn.name, d.rtkn.value) |
| 36 | + # in theory this class shoulod just take in d directly |
| 37 | + subdim_max = SubDimMax(d.name + '_max', subdim=d, thickness=d.thickness) |
| 38 | + |
| 39 | + # from IPython import embed; embed() |
| 40 | + |
| 41 | + new_dim = CustomBoundSubDimension( |
| 42 | + name=d.name, |
| 43 | + parent=d.parent, |
| 44 | + thickness=d.thickness, |
| 45 | + local=d.local, |
| 46 | + custom_left=Max(d.ltkn, d.parent.symbolic_min - halo_size_left), |
| 47 | + custom_right=Min(subdim_max, d.parent.symbolic_max + halo_size_right) |
| 48 | + ) |
| 49 | + mapper[d] = new_dim |
| 50 | + |
| 51 | + # from IPython import embed; embed() |
| 52 | + |
| 53 | + # build new expressions |
| 54 | + for e in expressions: |
| 55 | + if not isinstance(e, EssentialBC): |
| 56 | + continue |
| 57 | + |
| 58 | + # build new expression |
| 59 | + new_e = e.subs(mapper) |
| 60 | + |
| 61 | + additional_exprs.append(new_e) |
| 62 | + |
| 63 | + return expressions + additional_exprs |
| 64 | + |
| 65 | + |
| 66 | + |
0 commit comments