<jupyter_start><jupyter_text>This notebook demos varios differential operators We start with our differential operators<jupyter_code>from dusk.script import *


@stencil
def gradient(f: Field[Edge], nx: Field[Edge], ny: Field[Edge], L: Field[Edge], A: Field[Cell], edge_orientation: Field[Cell > Edge], 
                f_x: Field[Cell], f_y: Field[Cell]):
  with levels_downward:
    f_x = 0.
    f_y = 0.

@stencil
def divergence(u: Field[Edge], v: Field[Edge], nx: Field[Edge], ny: Field[Edge], L: Field[Edge], A: Field[Cell], edge_orientation: Field[Cell > Edge],
                  uv_div: Field[Cell]):
  with levels_downward:
    uv_div = 0.

@stencil
def curl(u: Field[Edge], v: Field[Edge], nx: Field[Edge], ny: Field[Edge], dualL: Field[Edge], dualA: Field[Vertex], edge_orientation: Field[Vertex > Edge],
            uv_curl: Field[Vertex]):
  with levels_downward:
    uv_curl = 0.<jupyter_output><empty_output><jupyter_text>Then we can use dusk's Python API to convert the stencils to SIR. This API can also invoke dawn to compile SIR to C++:<jupyter_code>from dusk.transpile import callables_to_pyast, pyast_to_sir, sir_to_json
with open("gradient.sir", "w+") as f:
    sir = pyast_to_sir(callables_to_pyast([gradient]))
    f.write(sir_to_json(sir))
with open("divergence.sir", "w+") as f:
    sir = pyast_to_sir(callables_to_pyast([divergence]))
    f.write(sir_to_json(sir))
with open("curl.sir", "w+") as f:
    sir = pyast_to_sir(callables_to_pyast([curl]))
    f.write(sir_to_json(sir))    
!dawn-opt gradient.sir | dawn-codegen -b naive-ico -o gradient_cxx-naive.cpp
!dawn-opt divergence.sir | dawn-codegen -b naive-ico -o divergence_cxx-naive.cpp
!dawn-opt curl.sir | dawn-codegen -b naive-ico -o curl_cxx-naive.cpp<jupyter_output><empty_output><jupyter_text>The generated C++ code also requires a driver which is already setup for this demo. With the driver code we can generate an executable `runner`:<jupyter_code>!make<jupyter_output><empty_output><jupyter_text>Now, its up to you which differentail operator you want to run and check. Simply launch `runner gradient`, `runner divergence` or `runner curl`<jupyter_code>!./runner gradient<jupyter_output><empty_output><jupyter_text>Besides ensuring the error norms L1, L2 and L infinity are small (they should all be well below 0.1), you can also have a look at some test functions and their differentials. Again, you can use `check gradient`, `check divergence` or `check curl`. Please make sure that you ran the appropriate differential operator beforhand using the `runner`<jupyter_code>%run checker.py gradient<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11650114591291062, 'fraction_numerical': 0.0030557677616501145, 'mean_word_length': 5.191489361702128, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '285537', 'license': 'MIT', 'n_tokens_mistral': 931, 'n_tokens_neox': 893, 'n_words': 263, 'path': 'content/differential_ops/differential_ops_exercise.ipynb', 'repo_name': 'dawn-ico/dusk-dawn-notebook', 'size': '2618'}