<commit_before>from firedrake import norm, VectorFunctionSpace, as_vector
from gusto import *
import pytest


def run(state, advection_scheme, tmax, f_end):
    timestepper = PrescribedAdvection(state, advection_scheme)
    timestepper.run(0, tmax)
    return norm(state.fields("f") - f_end)


@pytest.mark.parametrize("equation_form", ["advective", "continuity"])
@pytest.mark.parametrize("scheme", ["ssprk", "implicit_midpoint"])
@pytest.mark.parametrize("space", ["CG", "theta"])
def test_supg_advection_scalar(tmpdir, equation_form, scheme, space,
                               tracer_setup):

    setup = tracer_setup(tmpdir, geometry="slice")
    state = setup.state

    if space == "CG":
        V = state.spaces("CG1", "CG", 1)
        ibp = IntegrateByParts.NEVER
    else:
        V = state.spaces("theta", degree=1)
        ibp = IntegrateByParts.TWICE

    opts = SUPGOptions()

    if equation_form == "advective":
        eqn = AdvectionEquation(state, V, "f", ufamily=setup.family,
                                udegree=setup.degree, ibp=ibp)
    else:
        eqn = ContinuityEquation(state, V, "f", ufamily=setup.family,
                                 udegree=setup.degree, ibp=ibp)
    state.fields("f").interpolate(setup.f_init)
    state.fields("u").project(setup.uexpr)
    if scheme == "ssprk":
        advection_scheme = [(eqn, SSPRK3(state, options=opts))]
    elif scheme == "implicit_midpoint":
        advection_scheme = [(eqn, ImplicitMidpoint(state, options=opts))]

    assert run(state, advection_scheme, setup.tmax, setup.f_end) < setup.tol


@pytest.mark.parametrize("equation_form", ["advective", "continuity"])
@pytest.mark.parametrize("scheme", ["ssprk", "implicit_midpoint"])
@pytest.mark.parametrize("space", ["CG", "HDiv"])
def test_supg_advection_vector(tmpdir, equation_form, scheme, space,
                               tracer_setup):

    setup = tracer_setup(tmpdir, geometry="slice")
    state = setup.state

    gdim = state.mesh.geometric_dimension()
    f_init = as_vector((setup.f_init, *[0.]*(gdim-1)))
    if space == "CG":
        V = VectorFunctionSpace(state.mesh, "CG", 1)
        ibp = IntegrateByParts.NEVER
    else:
        V = state.spaces("HDiv", setup.family, setup.degree)
        ibp = IntegrateByParts.TWICE

    opts = SUPGOptions()

    if equation_form == "advective":
        eqn = AdvectionEquation(state, V, "f", ufamily=setup.family,
                                udegree=setup.degree, ibp=ibp)
    else:
        eqn = ContinuityEquation(state, V, "f", ufamily=setup.family,
                                 udegree=setup.degree, ibp=ibp)
    f = state.fields("f")
    try:
        f.interpolate(f_init)
    except NotImplementedError:
        f.project(f_init)
    state.fields("u").project(setup.uexpr)
    if scheme == "ssprk":
        advection_scheme = [(eqn, SSPRK3(state, options=opts))]
    elif scheme == "implicit_midpoint":
        advection_scheme = [(eqn, ImplicitMidpoint(state, options=opts))]

    f_end = as_vector((setup.f_end, *[0.]*(gdim-1)))
    assert run(state, advection_scheme, setup.tmax, f_end) < setup.tol
<commit_msg>fix initialisation for SUPG test<commit_after>from firedrake import norm, VectorFunctionSpace, as_vector
from gusto import *
import pytest


def run(state, advection_scheme, tmax, f_end):
    timestepper = PrescribedAdvection(state, advection_scheme)
    timestepper.run(0, tmax)
    return norm(state.fields("f") - f_end)


@pytest.mark.parametrize("equation_form", ["advective", "continuity"])
@pytest.mark.parametrize("scheme", ["ssprk", "implicit_midpoint"])
@pytest.mark.parametrize("space", ["CG", "theta"])
def test_supg_advection_scalar(tmpdir, equation_form, scheme, space,
                               tracer_setup):

    setup = tracer_setup(tmpdir, geometry="slice")
    state = setup.state

    if space == "CG":
        V = state.spaces("CG1", "CG", 1)
        ibp = IntegrateByParts.NEVER
    else:
        V = state.spaces("theta", degree=1)
        ibp = IntegrateByParts.TWICE

    opts = SUPGOptions()

    if equation_form == "advective":
        eqn = AdvectionEquation(state, V, "f", ufamily=setup.family,
                                udegree=setup.degree, ibp=ibp)
    else:
        eqn = ContinuityEquation(state, V, "f", ufamily=setup.family,
                                 udegree=setup.degree, ibp=ibp)
    state.fields("f").interpolate(setup.f_init)
    state.fields("u").project(setup.uexpr)
    if scheme == "ssprk":
        advection_scheme = [(eqn, SSPRK3(state, options=opts))]
    elif scheme == "implicit_midpoint":
        advection_scheme = [(eqn, ImplicitMidpoint(state, options=opts))]

    assert run(state, advection_scheme, setup.tmax, setup.f_end) < setup.tol


@pytest.mark.parametrize("equation_form", ["advective", "continuity"])
@pytest.mark.parametrize("scheme", ["ssprk", "implicit_midpoint"])
@pytest.mark.parametrize("space", ["CG", "HDiv"])
def test_supg_advection_vector(tmpdir, equation_form, scheme, space,
                               tracer_setup):

    setup = tracer_setup(tmpdir, geometry="slice")
    state = setup.state

    gdim = state.mesh.geometric_dimension()
    f_init = as_vector((setup.f_init, *[0.]*(gdim-1)))
    if space == "CG":
        V = VectorFunctionSpace(state.mesh, "CG", 1)
        ibp = IntegrateByParts.NEVER
    else:
        V = state.spaces("HDiv", setup.family, setup.degree)
        ibp = IntegrateByParts.TWICE

    opts = SUPGOptions()

    if equation_form == "advective":
        eqn = AdvectionEquation(state, V, "f", ufamily=setup.family,
                                udegree=setup.degree, ibp=ibp)
    else:
        eqn = ContinuityEquation(state, V, "f", ufamily=setup.family,
                                 udegree=setup.degree, ibp=ibp)
    f = state.fields("f")
    if space == "CG":
        f.interpolate(f_init)
    else:
        f.project(f_init)
    state.fields("u").project(setup.uexpr)
    if scheme == "ssprk":
        advection_scheme = [(eqn, SSPRK3(state, options=opts))]
    elif scheme == "implicit_midpoint":
        advection_scheme = [(eqn, ImplicitMidpoint(state, options=opts))]

    f_end = as_vector((setup.f_end, *[0.]*(gdim-1)))
    assert run(state, advection_scheme, setup.tmax, f_end) < setup.tol

{'fraction_non_alphanumeric': 0.14016602809706258, 'fraction_numerical': 0.00351213282247765, 'mean_word_length': 3.0393294648613796, 'pattern_counts': {'":': 17, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1854219', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2110, 'n_words': 391}