11from sympy import Eq
22from devito .symbolics import retrieve_indexed , retrieve_dimensions
33from devito .petsc .types .equation import ConstrainBC
4- from devito .types .dimension import CustomBoundSubDimension , SpaceDimension , CustomBoundSpaceDimension
4+ from devito .types .dimension import CustomBoundSubDimension , SpaceDimension , CustomBoundSpaceDimension , CustomDimension
55from devito import Min , Max
66
77
@@ -13,6 +13,81 @@ def lower_exprs_petsc(expressions, **kwargs):
1313
1414
1515
16+ # def constrain_essential_bcs(expressions, **kwargs):
17+ # """TODO: improve docs ..Modify the subdims used in ConstrainEssentialBC equations ... to locally
18+ # constrain nodes (including non owned halo nodes) ....."""
19+
20+ # mapper = {}
21+ # new_exprs = []
22+
23+ # # build mapper
24+ # for e in expressions:
25+ # if not isinstance(e, ConstrainBC):
26+ # new_exprs.append(e)
27+ # continue
28+
29+
30+ # # this needs to be applied to all space dimensions and all subdims that are "MIDDLE"
31+
32+ # indexeds = retrieve_indexed(e)
33+ # dims = retrieve_dimensions([i for j in indexeds for i in j.indices], mode='unique')
34+ # # implicit_dims = set(e.implicit_dims)
35+ # dims.update(e.implicit_dims)
36+ # # from IPython import embed; embed()
37+ # dims = [d for d in dims if d.is_Sub and not d.local]
38+
39+
40+ # if not dims:
41+ # new_exprs.append(e)
42+ # continue
43+
44+ # for d in dims:
45+ # # replace the dim with a new one that has a different symbolic_min and symbolic_max
46+
47+ # # obvs shouldn't be obtained from indexeds[0], but how should it be obtained?
48+ # # USE e.lhs function -> the one that the BC is being applied to
49+ # # from IPython import embed; embed()
50+ # # f._size_nodomain.left
51+ # # from IPython import embed; embed()
52+ # # halo_size_left = indexeds[0].function._size_halo[d].left
53+ # # halo_size_right = indexeds[0].function._size_halo[d].right
54+
55+ # halo_size_left = 2
56+ # halo_size_right = 2
57+
58+ # from devito.petsc.types.dimension import SubDimMax, SubDimMin
59+
60+ # # TODO: change name..
61+
62+ # # in theory this class shoulod just take in d
63+ # # TODO: use unique name
64+ # sregistry = kwargs.get('sregistry')
65+ # subdim_max = SubDimMax(sregistry.make_name(prefix=d.name + '_max'), subdim=d, thickness=d.thickness)
66+ # subdim_min = SubDimMin(sregistry.make_name(prefix=d.name + '_min'), subdim=d, thickness=d.thickness)
67+
68+ # # unique_name
69+ # new_dim = CustomBoundSubDimension(
70+ # name=d.name,
71+ # parent=d.parent,
72+ # thickness=d.thickness,
73+ # local=d.local,
74+ # custom_left=Max(subdim_min, d.parent.symbolic_min - halo_size_left),
75+ # custom_right=Min(subdim_max, d.parent.symbolic_max + halo_size_right)
76+ # )
77+ # mapper[d] = new_dim
78+
79+ # new_e = e.subs(mapper)
80+ # if e.implicit_dims:
81+ # # from devito.symbolics import uxreplace
82+ # implicit_dims_new = tuple(mapper.get(d, d) for d in e.implicit_dims)
83+ # # from IPython import embed; embed()
84+ # new_e = new_e._rebuild(implicit_dims=implicit_dims_new)
85+ # new_exprs.append(new_e)
86+
87+ # return new_exprs
88+
89+
90+
1691def constrain_essential_bcs (expressions , ** kwargs ):
1792 """TODO: improve docs ..Modify the subdims used in ConstrainEssentialBC equations ... to locally
1893 constrain nodes (including non owned halo nodes) ....."""
@@ -23,28 +98,37 @@ def constrain_essential_bcs(expressions, **kwargs):
2398 # build mapper
2499 for e in expressions :
25100 if not isinstance (e , ConstrainBC ):
26- new_exprs .append (e )
101+ # new_exprs.append(e)
27102 continue
28-
29-
30103 # this needs to be applied to all space dimensions and all subdims that are "MIDDLE"
31104
32105 indexeds = retrieve_indexed (e )
33106 dims = retrieve_dimensions ([i for j in indexeds for i in j .indices ], mode = 'unique' )
107+ # from IPython import embed; embed()
34108 # implicit_dims = set(e.implicit_dims)
35109 dims .update (e .implicit_dims )
36110 # from IPython import embed; embed()
37- dims = [d for d in dims if d .is_Sub and not d .local ]
38111
39-
40- if not dims :
41- new_exprs .append (e )
112+ # Collect non-local subdims (which would be generated from a "middle" subdomain)
113+ # "local" subdims are generated from 'left' and 'right" subdomains and do not need to be considered
114+ # because by construction, left and right subdimensions cannot cross ranks
115+ subdims = [d for d in dims if d .is_Sub and not d .local ]
116+
117+ space_dims = [d for d in dims if isinstance (d , SpaceDimension )]
118+
119+ relevant_dims = subdims + space_dims
120+ # relevant_dims = subdims
121+
122+
123+ if not relevant_dims :
42124 continue
125+
43126
44- for d in dims :
127+ # first map the subdims
128+ for d in subdims :
45129 # replace the dim with a new one that has a different symbolic_min and symbolic_max
46130
47- # obvs shouldn't be obtained from indexeds[0], but how should it be obtained?
131+ # obvs shouldn't be obtained from indexeds[0]
48132 # USE e.lhs function -> the one that the BC is being applied to
49133 # from IPython import embed; embed()
50134 # f._size_nodomain.left
@@ -62,12 +146,13 @@ def constrain_essential_bcs(expressions, **kwargs):
62146 # in theory this class shoulod just take in d
63147 # TODO: use unique name
64148 sregistry = kwargs .get ('sregistry' )
65- subdim_max = SubDimMax (sregistry .make_name (prefix = d .name + '_max' ), subdim = d , thickness = d . thickness )
66- subdim_min = SubDimMin (sregistry .make_name (prefix = d .name + '_min' ), subdim = d , thickness = d . thickness )
67-
149+ subdim_max = SubDimMax (sregistry .make_name (prefix = d .name + '_max' ), subdim = d )
150+ subdim_min = SubDimMin (sregistry .make_name (prefix = d .name + '_min' ), subdim = d )
151+ # from IPython import embed; embed()
68152 # unique_name
153+ # TODO: should this just be CustomDimension?
69154 new_dim = CustomBoundSubDimension (
70- name = d .name ,
155+ name = f' { d .name } _new' ,
71156 parent = d .parent ,
72157 thickness = d .thickness ,
73158 local = d .local ,
@@ -76,145 +161,64 @@ def constrain_essential_bcs(expressions, **kwargs):
76161 )
77162 mapper [d ] = new_dim
78163
79- new_e = e .subs (mapper )
80- if e .implicit_dims :
81- # from devito.symbolics import uxreplace
82- implicit_dims_new = tuple (mapper .get (d , d ) for d in e .implicit_dims )
83- # from IPython import embed; embed()
84- new_e = new_e ._rebuild (implicit_dims = implicit_dims_new )
85- new_exprs .append (new_e )
86-
87- return new_exprs
88-
89-
164+ # then tackle space dims
165+ # for d in space_dims:
166+ # mapper[d] = d
90167
91- # def constrain_essential_bcs(expressions, **kwargs):
92- # """TODO: improve docs ..Modify the subdims used in ConstrainEssentialBC equations ... to locally
93- # constrain nodes (including non owned halo nodes) ....."""
94-
95- # mapper = {}
96- # new_exprs = []
97-
98- # # build mapper
99- # for e in expressions:
100- # if not isinstance(e, ConstrainBC):
101- # # new_exprs.append(e)
102- # continue
103- # # this needs to be applied to all space dimensions and all subdims that are "MIDDLE"
104-
105- # indexeds = retrieve_indexed(e)
106- # dims = retrieve_dimensions([i for j in indexeds for i in j.indices], mode='unique')
107- # # from IPython import embed; embed()
108- # # implicit_dims = set(e.implicit_dims)
109- # dims.update(e.implicit_dims)
110- # # from IPython import embed; embed()
111-
112- # # Collect non-local subdims (which would be generated from a "middle" subdomain)
113- # # "local" subdims are generated from 'left' and 'right" subdomains and do not need to be considered
114- # # because by construction, left and right subdimensions cannot cross ranks
115- # subdims = [d for d in dims if d.is_Sub and not d.local]
116-
117- # space_dims = [d for d in dims if isinstance(d, SpaceDimension)]
118-
119- # relevant_dims = subdims + space_dims
120- # # relevant_dims = subdims
121-
122-
123- # if not relevant_dims:
124- # continue
125-
126-
127- # # first map the subdims
128- # for d in subdims:
129- # # replace the dim with a new one that has a different symbolic_min and symbolic_max
130-
131- # # obvs shouldn't be obtained from indexeds[0]
132- # # USE e.lhs function -> the one that the BC is being applied to
133- # # from IPython import embed; embed()
134- # # f._size_nodomain.left
135- # # from IPython import embed; embed()
136- # # halo_size_left = indexeds[0].function._size_halo[d].left
137- # # halo_size_right = indexeds[0].function._size_halo[d].right
138-
139- # halo_size_left = 2
140- # halo_size_right = 2
141-
142- # from devito.petsc.types.dimension import SubDimMax, SubDimMin
143-
144- # # TODO: change name..
145-
146- # # in theory this class shoulod just take in d
147- # # TODO: use unique name
148- # sregistry = kwargs.get('sregistry')
149- # subdim_max = SubDimMax(sregistry.make_name(prefix=d.name + '_max'), subdim=d)
150- # subdim_min = SubDimMin(sregistry.make_name(prefix=d.name + '_min'), subdim=d)
151- # # from IPython import embed; embed()
152- # # unique_name
153- # new_dim = CustomBoundSubDimension(
154- # name=d.name,
155- # parent=d.parent,
156- # thickness=d.thickness,
157- # local=d.local,
158- # custom_left=Max(subdim_min, d.parent.symbolic_min - halo_size_left),
159- # custom_right=Min(subdim_max, d.parent.symbolic_max + halo_size_right)
160- # )
161- # mapper[d] = new_dim
168+ for d in space_dims :
169+ halo_size_left = 2
170+ halo_size_right = 2
162171
163- # # then tackle space dims
164- # for d in space_dims:
165- # mapper[d] = d
166- # # for d in space_dims:
167- # # halo_size_left = 2
168- # # halo_size_right = 2
172+ from devito .petsc .types .dimension import SpaceDimMax , SpaceDimMin
169173
170- # # from devito.petsc.types.dimension import SpaceDimMax, SpaceDimMin
174+ # TODO: change name..
171175
172- # # # TODO: change name..
176+ # in theory this class shoulod just take in d
177+ # TODO: use unique name
178+ sregistry = kwargs .get ('sregistry' )
179+ space_dim_max = SpaceDimMax (sregistry .make_name (prefix = d .name + '_max' ), space_dim = d )
180+ space_dim_min = SpaceDimMin (sregistry .make_name (prefix = d .name + '_min' ), space_dim = d )
173181
174- # # # in theory this class shoulod just take in d
175- # # # TODO: use unique name
176- # # sregistry = kwargs.get('sregistry')
177- # # space_dim_max = SpaceDimMax(sregistry.make_name(prefix=d.name + '_max'), space_dim=d)
178- # # space_dim_min = SpaceDimMin(sregistry.make_name(prefix=d.name + '_min'), space_dim=d)
182+ # unique_name
183+ new_dim = CustomDimension (
184+ name = sregistry .make_name (prefix = d .name + '_zoe' ),
185+ symbolic_min = Max (space_dim_min , d .symbolic_min - halo_size_left ),
186+ symbolic_max = Min (space_dim_max , d .symbolic_max + halo_size_right )
187+ )
188+ mapper [d ] = new_dim
189+ # # from IPython import embed; embed()
179190
180- # # # unique_name
181- # # new_dim = CustomBoundSpaceDimension(
182- # # name=d.name,
183- # # custom_left=Max(space_dim_min, d.symbolic_min - halo_size_left),
184- # # custom_right=Min(space_dim_max, d.symbolic_max + halo_size_right)
185- # # )
186- # # mapper[d] = new_dim
187- # # # from IPython import embed; embed()
188191
192+ # from IPython import embed; embed()
189193
190- # # from IPython import embed; embed()
191194
195+ for e in expressions :
192196
193- # for e in expressions:
197+ if not isinstance (e , ConstrainBC ):
198+ new_exprs .append (e )
199+ continue
194200
195- # if not isinstance(e, ConstrainBC):
196- # new_exprs.append(e)
197- # continue
201+ indexeds = retrieve_indexed (e )
202+ dims = retrieve_dimensions ([i for j in indexeds for i in j .indices ], mode = 'unique' )
203+ # implicit_dims = set(e.implicit_dims)
204+ dims .update (e .implicit_dims )
205+ # from IPython import embed; embed()
206+ subdims = [d for d in dims if d .is_Sub and not d .local ]
198207
199- # indexeds = retrieve_indexed(e)
200- # dims = retrieve_dimensions([i for j in indexeds for i in j.indices], mode='unique')
201- # # implicit_dims = set(e.implicit_dims)
202- # dims.update(e.implicit_dims)
203- # # from IPython import embed; embed()
204- # subdims = [d for d in dims if d.is_Sub and not d.local]
208+ space_dims = [d for d in dims if isinstance (d , SpaceDimension )]
205209
206- # space_dims = [d for d in dims if isinstance(d, SpaceDimension)]
210+ relevant_dims = subdims + space_dims
207211
208- # relevant_dims = subdims + space_dims
212+ if not relevant_dims :
213+ new_exprs .append (e )
214+ continue
209215
210- # if not relevant_dims:
211- # new_exprs.append(e)
212- # continue
216+ local_mapper = {k : v for k , v in mapper .items () if k in relevant_dims }
213217
214- # new_e = e.subs(mapper )
215- # if e.implicit_dims:
216- # implicit_dims_new = tuple(mapper .get(d, d) for d in e.implicit_dims)
217- # new_e = new_e._rebuild(implicit_dims=implicit_dims_new)
218- # new_exprs.append(new_e)
218+ new_e = e .subs (local_mapper )
219+ if e .implicit_dims :
220+ implicit_dims_new = tuple (local_mapper .get (d , d ) for d in e .implicit_dims )
221+ new_e = new_e ._rebuild (implicit_dims = implicit_dims_new )
222+ new_exprs .append (new_e )
219223
220- # return new_exprs
224+ return new_exprs
0 commit comments