Skip to content

Commit

Permalink
only solution files
Browse files Browse the repository at this point in the history
  • Loading branch information
havogt committed Nov 22, 2023
1 parent 32bd37a commit 24e78e7
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 996 deletions.
65 changes: 0 additions & 65 deletions docs/user/next/exercises/1_simple_addition.md

This file was deleted.

47 changes: 24 additions & 23 deletions docs/user/next/exercises/1_simple_addition_solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,51 @@ kernelspec:
# 1. Simple Addition

```{code-cell} ipython3
from helpers import *
import gt4py.next as gtx
import numpy as np
from helpers import random_field_new
```

Next we implement the stencil and a numpy reference version, in order to verify them against each other.

```{code-cell} ipython3
def addition_numpy(
a: np.array, b: np.array,
) -> np.array:
C = gtx.Dimension("C")
n_cells = 42
```

```{code-cell} ipython3
def addition_numpy(a: np.array, b: np.array) -> np.array:
c = a + b
return c
```

```{code-cell} ipython3
@gtx.field_operator(backend=roundtrip.executor)
@gtx.field_operator
def addition(
a: gtx.Field[[C], float],
b: gtx.Field[[C], float],
a: gtx.Field[[C], float], b: gtx.Field[[C], float]
) -> gtx.Field[[C], float]:
c = a + b
return c
return a + b
```

```{code-cell} ipython3
def test_mo_nh_diffusion_stencil_06():
a = random_field((n_cells), C)
b = random_field((n_cells), C)
def test_addition():
domain = gtx.domain({C:n_cells})
c_numpy = addition_numpy(
np.asarray(a), np.asarray(b)
)
a = random_field_new(domain)
b = random_field_new(domain)
c = zero_field((n_cells), C)
c_numpy = addition_numpy(a.asnumpy(), b.asnumpy())
addition(
a, b, out=c, offset_provider={}
)
assert np.allclose(c, c_numpy)
c = gtx.zeros(domain)
addition(a, b, out=c, offset_provider={})
assert np.allclose(c.asnumpy(), c_numpy)
print("Test successful!")
```

```{code-cell} ipython3
test_mo_nh_diffusion_stencil_06()
test_addition()
```
155 changes: 0 additions & 155 deletions docs/user/next/exercises/3_divergence_exercise_solution.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def gradient_numpy(
```

```{code-cell} ipython3
@gtx.field_operator(backend=roundtrip.executor)
@gtx.field_operator
def gradient(
f: gtx.Field[[E], float],
nx: gtx.Field[[E], float],
Expand All @@ -58,8 +58,8 @@ def gradient(
A: gtx.Field[[C], float],
edge_orientation: gtx.Field[[C, C2EDim], float],
) -> gtx.tuple[gtx.Field[[C], float], gtx.Field[[C], float]]:
f_x = A
f_y = A
f_x = neighbor_sum(f(C2E) * nx(C2E) * L(C2E) * edge_orientation, axis=C2EDim) / A
f_y = neighbor_sum(f(C2E) * ny(C2E) * L(C2E) * edge_orientation, axis=C2EDim) / A
return f_x, f_y
```

Expand All @@ -82,7 +82,7 @@ def test_gradient():
edge_orientation.asnumpy(),
)
c2e_connectivity = gtx.NeighborTableOffsetProvider(c2e_table, C, E, 3)
c2e_connectivity = gtx.NeighborTableOffsetProvider(c2e_table, C, E, 3, has_skip_values=False)
gradient_gt4py_x = zero_field((n_cells), C)
gradient_gt4py_y = zero_field((n_cells), C)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ def curl_numpy(
dualA: np.array,
edge_orientation: np.array,
) -> np.array:
uv_curl = np.sum((u[v2e]*nx[v2e] + v[v2e]*ny[v2e]) * dualL[v2e] * edge_orientation, axis=1) / dualA
uv_curl = (
np.sum(
(u[v2e] * nx[v2e] + v[v2e] * ny[v2e]) * dualL[v2e] * edge_orientation,
axis=1,
)
/ dualA
)
return uv_curl
```

```{code-cell} ipython3
@gtx.field_operator(backend=roundtrip.executor)
@gtx.field_operator
def curl(
u: gtx.Field[[E], float],
v: gtx.Field[[E], float],
Expand All @@ -63,8 +69,14 @@ def curl(
dualA: gtx.Field[[V], float],
edge_orientation: gtx.Field[[V, V2EDim], float],
) -> gtx.Field[[V], float]:
uv_curl = dualA
uv_curl = (
neighbor_sum(
(u(V2E) * nx(V2E) + v(V2E) * ny(V2E)) * dualL(V2E) * edge_orientation,
axis=V2EDim,
)
/ dualA
)
return uv_curl
```

Expand All @@ -89,7 +101,7 @@ def test_curl():
edge_orientation.asnumpy(),
)
v2e_connectivity = gtx.NeighborTableOffsetProvider(v2e_table, V, E, 6)
v2e_connectivity = gtx.NeighborTableOffsetProvider(v2e_table, V, E, 6, has_skip_values=False)
curl_gt4py = zero_field((n_vertices), V)
Expand Down
Loading

0 comments on commit 24e78e7

Please sign in to comment.