Skip to content

Commit 9fcf7dc

Browse files
committed
compute overlap
1 parent 32a51c3 commit 9fcf7dc

File tree

3 files changed

+106
-21
lines changed

3 files changed

+106
-21
lines changed

pyphare/pyphare/pharesee/geometry.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,84 @@ def periodicity_shifts(domain_box):
149149
return shifts
150150

151151

152+
def possible_periodic_shifts(box, domain_box):
153+
from pyphare.core.box import shift
154+
155+
boxes = []
156+
dim = domain_box.ndim
157+
158+
if dim == 1:
159+
for ishift in (-1, 0, 1):
160+
ioffset = ishift * domain_box.shape[0]
161+
offset = ioffset
162+
163+
shifted = shift(box, offset)
164+
boxes += [(offset, shifted)]
165+
if dim == 2:
166+
for ishift in (-1, 0, 1):
167+
ioffset = ishift * domain_box.shape[0]
168+
for jshift in (-1, 0, 1):
169+
joffset = jshift * domain_box.shape[1]
170+
offset = [ioffset, joffset]
171+
shifted = shift(box, offset)
172+
boxes += [(offset, shifted)]
173+
if dim == 3:
174+
for ishift in (-1, 0, 1):
175+
ioffset = ishift * domain_box.shape[0]
176+
for jshift in (-1, 0, 1):
177+
joffset = jshift * domain_box.shape[1]
178+
for kshift in (-1, 0, 1):
179+
koffset = kshift * domain_box.shape[2]
180+
181+
offset = [ioffset, joffset, koffset]
182+
shifted = shift(box, offset)
183+
boxes += [(offset, shifted)]
184+
return boxes
185+
186+
152187
def compute_overlaps(patches, domain_box):
188+
overlaps = []
189+
zero_offset = [0] * domain_box.ndim if domain_box.ndim > 1 else 0
190+
191+
for ip, refPatch in enumerate(patches):
192+
for cmpPatch in patches[ip:]:
193+
194+
for ref_pdname, ref_pd in refPatch.patch_datas.items():
195+
cmp_pd = cmpPatch.patch_datas[ref_pdname]
196+
197+
gb_ref = ref_pd.ghost_box
198+
gb_cmp = cmp_pd.ghost_box
199+
200+
for offset, shifted_cmp in possible_periodic_shifts(gb_cmp, domain_box):
201+
overlap = gb_ref * shifted_cmp
202+
if overlap is not None and not np.all(
203+
overlap.shape == gb_ref.shape
204+
):
205+
if ref_pd.quantity == "field":
206+
overlap = toFieldBox(overlap, ref_pd)
207+
208+
overlaps.append(
209+
{
210+
"pdatas": (ref_pd, cmp_pd),
211+
"patches": (refPatch, cmpPatch),
212+
"box": overlap,
213+
"offset": (zero_offset, offset),
214+
}
215+
)
216+
if offset != zero_offset:
217+
overlaps.append(
218+
{
219+
"pdatas": (ref_pd, cmp_pd),
220+
"patches": (refPatch, cmpPatch),
221+
"box": overlap,
222+
"offset": (zero_offset, offset),
223+
}
224+
)
225+
226+
return overlaps
227+
228+
229+
def compute_overlaps_(patches, domain_box):
153230
"""
154231
returns a list of overlaps for all patch datas in given patches
155232
and for a domain box. An overlap is defined as an intersection of

pyphare/pyphare_tests/test_pharesee/test_geometry.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def setup_hierarchy(self, dim, interp_order, nbr_cells, refinement_boxes, **kwar
2727
domain_size=domain_size,
2828
cell_width=domain_size / nbr_cells,
2929
refinement_boxes=refinement_boxes,
30-
**kwargs
30+
**kwargs,
3131
)
3232

3333
# used for tests without ddt hierarchy overrides
@@ -78,18 +78,20 @@ def test_overlaps(self):
7878
}
7979

8080
overlaps = hierarchy_overlaps(hierarchy)
81+
tested_overlaps = {
82+
ilvl: [
83+
{"box": overlap["box"], "offset": overlap["offset"]}
84+
for overlap in overlaps[ilvl]
85+
]
86+
for ilvl in range(len(hierarchy.patch_levels))
87+
}
8188

8289
for ilvl, lvl in enumerate(hierarchy.patch_levels):
8390
self.assertEqual(len(expected[ilvl]), len(overlaps[ilvl]))
8491

85-
for exp, actual in zip(expected[ilvl], overlaps[ilvl]):
86-
act_box = actual["box"]
87-
act_offset = actual["offset"]
88-
exp_box = exp["box"]
89-
exp_offset = exp["offset"]
90-
91-
self.assertEqual(act_box, exp_box)
92-
self.assertEqual(act_offset, exp_offset)
92+
for actual in tested_overlaps[ilvl]:
93+
if actual not in expected[ilvl]:
94+
raise AssertionError(f"Unexpected overlap: {actual}")
9395

9496
def test_touch_border(self):
9597
hierarchy = self.basic_hierarchy()

pyphare/pyphare_tests/test_pharesee/test_geometry_2d.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,19 @@ def test_overlaps(self, refinement_boxes, expected):
114114
)
115115

116116
level_overlaps = hierarchy_overlaps(hierarchy)
117+
tested_overlaps = {
118+
ilvl: [
119+
{"box": overlap["box"], "offset": overlap["offset"]}
120+
for overlap in level_overlaps[ilvl]
121+
]
122+
for ilvl in expected.keys()
123+
}
117124
for ilvl, lvl in enumerate(hierarchy.levels().items()):
118125
if ilvl not in expected:
119126
continue
120127
self.assertEqual(len(expected[ilvl]), len(level_overlaps[ilvl]))
121-
for exp, actual in zip(expected[ilvl], level_overlaps[ilvl]):
122-
self.assertEqual(actual["box"], exp["box"])
123-
self.assertTrue(
124-
(np.asarray(actual["offset"]) == np.asarray(exp["offset"])).all()
125-
)
128+
for actual in tested_overlaps[ilvl]:
129+
self.assertTrue(actual in expected[ilvl])
126130

127131
if 1 in level_overlaps:
128132
fig = hierarchy.plot_2d_patches(
@@ -163,14 +167,16 @@ def test_large_patchoverlaps(self, expected):
163167
level_overlaps = hierarchy_overlaps(hierarchy)
164168
ilvl = 0
165169
overlap_boxes = []
170+
tested_overlaps = {
171+
0: [
172+
{"box": overlap["box"], "offset": overlap["offset"]}
173+
for overlap in level_overlaps[ilvl]
174+
]
175+
}
166176

167-
self.assertEqual(len(expected), len(level_overlaps[ilvl]))
168-
for exp, actual in zip(expected, level_overlaps[ilvl]):
169-
self.assertEqual(actual["box"], exp["box"])
170-
self.assertTrue(
171-
(np.asarray(actual["offset"]) == np.asarray(exp["offset"])).all()
172-
)
173-
overlap_boxes += [actual["box"]]
177+
self.assertEqual(len(expected), len(tested_overlaps[ilvl]))
178+
for actual in tested_overlaps[ilvl]:
179+
self.assertTrue(actual in expected)
174180

175181
fig = hierarchy.plot_2d_patches(
176182
ilvl,

0 commit comments

Comments
 (0)