@@ -204,9 +204,26 @@ def index_glb_to_loc(self, *args, rel=True):
204204 >>> d.index_glb_to_loc((1, 6), rel=False)
205205 (5, 6)
206206 """
207+ # Offset the loc_abs_min, loc_abs_max, glb_min, and glb_max
208+ # In the case of a Function defined on a SubDomain, the global indices
209+ # for accessing the data associated with this Function will run from
210+ # `ltkn` to `x_M-rtkn-1`. However, the indices for accessing this array
211+ # will run from `0` to `x_M-ltkn-rtkn-1`. As such, the global minimum
212+ # (`ltkn`) should be subtracted for the purpose of indexing into the local
213+ # array.
214+ if not self .loc_empty :
215+ loc_abs_min = self .loc_abs_min - self .glb_min
216+ loc_abs_max = self .loc_abs_max - self .glb_min
217+ glb_max = self .glb_max - self .glb_min
218+ else :
219+ loc_abs_min = self .loc_abs_min
220+ loc_abs_max = self .loc_abs_max
221+ glb_max = self .glb_max
222+
223+ glb_min = 0
207224
208- base = self . loc_abs_min if rel is True else 0
209- top = self . loc_abs_max
225+ base = loc_abs_min if rel else 0
226+ top = loc_abs_max
210227
211228 if len (args ) == 1 :
212229 glb_idx = args [0 ]
@@ -217,11 +234,11 @@ def index_glb_to_loc(self, *args, rel=True):
217234 return None
218235 # -> Handle negative index
219236 if glb_idx < 0 :
220- glb_idx = self . glb_max + glb_idx + 1
237+ glb_idx = glb_max + glb_idx + 1
221238 # -> Do the actual conversion
222- if self . loc_abs_min <= glb_idx <= self . loc_abs_max :
239+ if loc_abs_min <= glb_idx <= loc_abs_max :
223240 return glb_idx - base
224- elif self . glb_min <= glb_idx <= self . glb_max :
241+ elif glb_min <= glb_idx <= glb_max :
225242 return None
226243 else :
227244 # This should raise an exception when used to access a numpy.array
@@ -239,30 +256,32 @@ def index_glb_to_loc(self, *args, rel=True):
239256 elif isinstance (glb_idx , slice ):
240257 if self .loc_empty :
241258 return slice (- 1 , - 3 )
242- if glb_idx .step >= 0 and glb_idx .stop == self . glb_min :
243- glb_idx_min = self . glb_min if glb_idx .start is None \
259+ if glb_idx .step >= 0 and glb_idx .stop == glb_min :
260+ glb_idx_min = glb_min if glb_idx .start is None \
244261 else glb_idx .start
245- glb_idx_max = self . glb_min
262+ glb_idx_max = glb_min
246263 retfunc = lambda a , b : slice (a , b , glb_idx .step )
247264 elif glb_idx .step >= 0 :
248- glb_idx_min = self . glb_min if glb_idx .start is None \
265+ glb_idx_min = glb_min if glb_idx .start is None \
249266 else glb_idx .start
250- glb_idx_max = self .glb_max if glb_idx .stop is None \
267+ glb_idx_max = glb_max \
268+ if glb_idx .stop is None \
251269 else glb_idx .stop - 1
252270 retfunc = lambda a , b : slice (a , b + 1 , glb_idx .step )
253271 else :
254- glb_idx_min = self . glb_min if glb_idx .stop is None \
272+ glb_idx_min = glb_min if glb_idx .stop is None \
255273 else glb_idx .stop + 1
256- glb_idx_max = self . glb_max if glb_idx .start is None \
274+ glb_idx_max = glb_max if glb_idx .start is None \
257275 else glb_idx .start
258276 retfunc = lambda a , b : slice (b , a - 1 , glb_idx .step )
259277 else :
260278 raise TypeError ("Cannot convert index from `%s`" % type (glb_idx ))
261279 # -> Handle negative min/max
262280 if glb_idx_min is not None and glb_idx_min < 0 :
263- glb_idx_min = self . glb_max + glb_idx_min + 1
281+ glb_idx_min = glb_max + glb_idx_min + 1
264282 if glb_idx_max is not None and glb_idx_max < 0 :
265- glb_idx_max = self .glb_max + glb_idx_max + 1
283+ glb_idx_max = glb_max + glb_idx_max + 1
284+
266285 # -> Do the actual conversion
267286 # Compute loc_min. For a slice with step > 0 this will be
268287 # used to produce slice.start and for a slice with step < 0 slice.stop.
@@ -271,19 +290,19 @@ def index_glb_to_loc(self, *args, rel=True):
271290 # coincide with loc_abs_min.
272291 if isinstance (glb_idx , slice ) and glb_idx .step is not None \
273292 and glb_idx .step > 1 :
274- if glb_idx_min > self . loc_abs_max :
293+ if glb_idx_min > loc_abs_max :
275294 return retfunc (- 1 , - 3 )
276295 elif glb_idx .start is None : # glb start is zero.
277- loc_min = self . loc_abs_min - base \
296+ loc_min = loc_abs_min - base \
278297 + np .mod (glb_idx .step - np .mod (base , glb_idx .step ),
279298 glb_idx .step )
280299 else : # glb start is given explicitly
281- loc_min = self . loc_abs_min - base \
300+ loc_min = loc_abs_min - base \
282301 + np .mod (glb_idx .step - np .mod (base - glb_idx .start ,
283302 glb_idx .step ), glb_idx .step )
284- elif glb_idx_min is None or glb_idx_min < self . loc_abs_min :
285- loc_min = self . loc_abs_min - base
286- elif glb_idx_min > self . loc_abs_max :
303+ elif glb_idx_min is None or glb_idx_min < loc_abs_min :
304+ loc_min = loc_abs_min - base
305+ elif glb_idx_min > loc_abs_max :
287306 return retfunc (- 1 , - 3 )
288307 else :
289308 loc_min = glb_idx_min - base
@@ -294,19 +313,19 @@ def index_glb_to_loc(self, *args, rel=True):
294313 # coincide with loc_abs_max.
295314 if isinstance (glb_idx , slice ) and glb_idx .step is not None \
296315 and glb_idx .step < - 1 :
297- if glb_idx_max < self . loc_abs_min :
316+ if glb_idx_max < loc_abs_min :
298317 return retfunc (- 1 , - 3 )
299318 elif glb_idx .start is None :
300319 loc_max = top - base \
301- + np .mod (glb_idx .step - np .mod (top - self . glb_max ,
320+ + np .mod (glb_idx .step - np .mod (top - glb_max ,
302321 glb_idx .step ), glb_idx .step )
303322 else :
304323 loc_max = top - base \
305324 + np .mod (glb_idx .step - np .mod (top - glb_idx .start ,
306325 glb_idx .step ), glb_idx .step )
307- elif glb_idx_max is None or glb_idx_max > self . loc_abs_max :
308- loc_max = self . loc_abs_max - base
309- elif glb_idx_max < self . loc_abs_min :
326+ elif glb_idx_max is None or glb_idx_max > loc_abs_max :
327+ loc_max = loc_abs_max - base
328+ elif glb_idx_max < loc_abs_min :
310329 return retfunc (- 1 , - 3 )
311330 else :
312331 loc_max = glb_idx_max - base
@@ -321,19 +340,19 @@ def index_glb_to_loc(self, *args, rel=True):
321340 return None
322341 abs_ofs , side = args
323342 if side == LEFT :
324- rel_ofs = self . glb_min + abs_ofs - base
343+ rel_ofs = glb_min + abs_ofs - base
325344 if abs_ofs >= base and abs_ofs <= top :
326345 return rel_ofs
327346 elif abs_ofs > top :
328347 return top + 1
329348 else :
330349 return None
331350 else :
332- rel_ofs = abs_ofs - (self . glb_max - top )
333- if abs_ofs >= self . glb_max - top and abs_ofs <= self . glb_max - base :
351+ rel_ofs = abs_ofs - (glb_max - top )
352+ if abs_ofs >= glb_max - top and abs_ofs <= glb_max - base :
334353 return rel_ofs
335- elif abs_ofs > self . glb_max - base :
336- return self . glb_max - base + 1
354+ elif abs_ofs > glb_max - base :
355+ return glb_max - base + 1
337356 else :
338357 return None
339358 else :
0 commit comments