---
# System prepended metadata

title: Example for halo/boundary confusion
tags: [archive]

---

# Example for halo/boundary confusion

```python=
# --- Simple case ---
@gt.stencil
def stencil_a(field):
    if index() == iteration_domain[0]:
        left = -10
        right = field[+1]
    elif index() == iteration_domain[-1]:
        left = field[-1]
        right = -10
    else:
        left = field[-1]
        right = field[+1]
        
    return left + right

@gt.fencil
def my_simple_fencil(in_field, out_field, domain):
    out_field = stencil_a[domain](in_field)
    
in_field = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
my_simple_fencil(in_field, out_field, (0, 10))
assert out_field == [-9, 2, 4, 6, 8, 10, 12, 14, 16, -2]

# --- Conflictive case (current model) ---
@gt.fencil
def my_complex_fencil(in_field, out_field, domain):
    out_field = stencil_a[domain](in_field)
    out_field = stencil_a[domain](out_field)


in_field = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
my_complex_fencil(in_field, out_field, (0,10))
# out_field.1 = [-9, | 2, 4, 6, 8, 10, 12, 14, 16, | -2]
# out_field.2 = [-8, -5, | 8, 12, 16, 20, 24, 28, | 12, 6]
assert out_field == [-8, -5, 8, 12, 16, 20, 24, 28, 12, 6]


# --- Conflictive case (new model) ---
@gt.stencil
def stencil_a_twice(field):
    return stencil_a(stencil_a(field))
    
@gt.fencil
def my_simple_fencil(in_field, out_field, domain):
    out_field = stencil_a_twice[domain](in_field)


in_field = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
my_simple_fencil(in_field, out_field, (0, 10))
assert out_field == [-8, -5, 8, 12, 16, 20, 24, 28, 12, 6]

```

### Other approaches ??
```python=
# Fencils allows stencil lifting ??
@gt.fencil
def my_simple_fencil(in_field, out_field, domain):
    lifted_tmp = stencil_a(in_field)
    # lifted_tmp <<= stencil_a(in_field)
    out_field = stencil_a[domain](lifted_tmp)
    #out_field = stencil_a(lifted_tmp)[domain]


# Fencils allows stencil lifting ??
@gt.fencil
def my_simple_fencil(in_field, out_field, domain):
    lifted_tmp = stencil_a(in_field)
    # lifted_tmp <<= stencil_a(in_field)
    out_field = stencil_a[domain](lifted_tmp)
    #out_field = stencil_a(lifted_tmp)[domain]


# BC-independent ??
@gt.stencil(externals={'bc_function': zero_gradient})
def stencil_a(field):
    if index() < LEFT_BOUNDARY or index() >= RIGHT_BOUNDARY:
        return bc_function(field)
    else:
        return field[-1] + field[+1]


# BC-independent: even more functional ??
@gt.stencil
def stencil_a(field, bc_function):
    if index() < LEFT_BOUNDARY or index() >= RIGHT_BOUNDARY:
        return bc_function(field)
    else:
        return field[-1] + field[+1]

```


<!--
# --- Conflictive case (new model with wrong approch) ---
@gt.stencil
def new_stencil_a(field):
    if index() == iteration_domain[0]:
        left = -10
        right = field[0] + field[+2]
    elif index() == iteration_domain[1]:
        left = -10 + field[0]
        right = field[0] + field[+2]
    elif index() == iteration_domain[-2]:
        left = field[-2] + field[0]
        right = field[0] + -10
    elif index() == iteration_domain[-1]:
        left = field[0] + field[-2]
        right = -10
    else:
        left = field[-2] + field[0]
        right = field[0] + field[+2]

    return left + right


@gt.fencil
def my_new_complex_fencil(in_field, out_field, domain):
    out_field = new_stencil_a[domain](in_field)


in_field = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
my_new_complex_fencil(in_field, out_field, (0,10))
assert out_field == [-8, -5, 8, 12, 16, 20, 24, 28, 12, 6]

-->