Skip to content

Cjc/vp demo #4320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
929eea3
Vlassov test
dham May 14, 2025
a14704f
advanced_tut.rst
colinjcotter May 14, 2025
38e70ca
spacing
dham May 14, 2025
3f83a4c
add in some gross hashes to deal with indentation
colinjcotter May 14, 2025
d966afe
add to demos file
dham May 14, 2025
2a12fc3
update demo links
dham May 14, 2025
6060de5
merge from release
colinjcotter May 14, 2025
7b7a8ba
tests
colinjcotter May 15, 2025
eba304d
add this demo
colinjcotter May 15, 2025
7fd9392
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
f2c8723
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
373f504
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
fe5d9a8
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
c8387f8
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
1be29b4
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
d8c50e4
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
cfb6121
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
4b46efd
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
9cb6fde
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
accc271
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
03397e8
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
fa1c630
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
b56a910
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
40581de
Update demos/vlasov_poisson_1d/vp1d.py.rst
colinjcotter May 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions demos/test_demos_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import glob
import importlib
import os
import subprocess
import sys
from collections import namedtuple
from pathlib import Path
from os.path import abspath, basename, dirname, join, splitext

import pyadjoint
import pytest


Demo = namedtuple("Demo", ["loc", "requirements"])


CWD = abspath(dirname(__file__))
DEMO_DIR = join(CWD, "..", "..", "..", "demos")

SERIAL_DEMOS = [
Demo(("benney_luke", "benney_luke"), ["vtk"]),
Demo(("boussinesq", "boussinesq"), []),
Demo(("burgers", "burgers"), ["vtk"]),
Demo(("camassa-holm", "camassaholm"), ["vtk"]),
Demo(("DG_advection", "DG_advection"), ["matplotlib"]),
Demo(("eigenvalues_QG_basinmodes", "qgbasinmodes"), ["matplotlib", "slepc", "vtk"]),
Demo(("extruded_continuity", "extruded_continuity"), []),
Demo(("helmholtz", "helmholtz"), ["vtk"]),
Demo(("higher_order_mass_lumping", "higher_order_mass_lumping"), ["vtk"]),
Demo(("immersed_fem", "immersed_fem"), []),
Demo(("linear_fluid_structure_interaction", "linear_fluid_structure_interaction"), ["vtk"]),
Demo(("linear-wave-equation", "linear_wave_equation"), ["vtk"]),
Demo(("ma-demo", "ma-demo"), ["vtk"]),
Demo(("matrix_free", "navier_stokes"), ["mumps", "vtk"]),
Demo(("matrix_free", "poisson"), []),
Demo(("matrix_free", "rayleigh-benard"), ["hypre", "mumps", "vtk"]),
Demo(("matrix_free", "stokes"), ["hypre", "mumps", "vtk"]),
Demo(("multigrid", "geometric_multigrid"), ["vtk"]),
Demo(("netgen", "netgen_mesh"), ["mumps", "netgen", "slepc", "vtk"]),
Demo(("nonlinear_QG_winddrivengyre", "qg_winddrivengyre"), ["vtk"]),
Demo(("parallel-printing", "parprint"), []),
Demo(("poisson", "poisson_mixed"), ["vtk"]),
Demo(("quasigeostrophy_1layer", "qg_1layer_wave"), ["hypre", "vtk"]),
Demo(("saddle_point_pc", "saddle_point_systems"), ["hypre", "mumps"]),
Demo(("vlasov_poisson_1d", "vp1d"), [])
]
PARALLEL_DEMOS = [
Demo(("full_waveform_inversion", "full_waveform_inversion"), ["adjoint"]),
]


@pytest.fixture
def env():
env = os.environ.copy()
env["MPLBACKEND"] = "pdf"
return env


def test_no_missing_demos():
all_demo_locs = {
demo.loc
for demos in [SERIAL_DEMOS, PARALLEL_DEMOS]
for demo in demos
}
for rst_file in glob.glob(f"{DEMO_DIR}/*/*.py.rst"):
rst_path = Path(rst_file)
demo_dir = rst_path.parent.name
demo_name, _, _ = rst_path.name.split(".")
demo_loc = (demo_dir, demo_name)
assert demo_loc in all_demo_locs
all_demo_locs.remove(demo_loc)
assert not all_demo_locs, "Unrecognised demos listed"


def _maybe_skip_demo(demo, skip_dependency):
skip_dep, dependency_skip_markers_and_reasons = skip_dependency
# Add pytest skips for missing imports or packages

for dep, _, reason in dependency_skip_markers_and_reasons:
if dep in demo.requirements and skip_dep(dep):
pytest.skip(reason)


def _prepare_demo(demo, monkeypatch, tmpdir):
# Change to the temporary directory (monkeypatch ensures that this
# is undone when the fixture usage disappears)
monkeypatch.chdir(tmpdir)

demo_dir, demo_name = demo.loc
rst_file = f"{DEMO_DIR}/{demo_dir}/{demo_name}.py.rst"

# Check if we need to generate any meshes
geos = glob.glob("%s/*.geo" % dirname(rst_file))
for geo in geos:
name = "%s.msh" % splitext(basename(geo))[0]
if os.path.exists(name):
# No need to generate if it's already there
continue
try:
subprocess.check_call(["gmsh", geo, "-format", "msh2", "-3", "-o", str(tmpdir.join(name))])
except (subprocess.CalledProcessError, OSError):
# Skip if unable to make mesh
pytest.skip("Unable to generate mesh file, skipping test")

# Get the name of the python file that pylit will make
name = splitext(basename(rst_file))[0]
py_file = str(tmpdir.join(name))
# Convert rst demo to runnable python file
subprocess.check_call(["pylit", rst_file, py_file])
return Path(py_file)


def _exec_file(py_file):
# To execute a file we import it. We therefore need to modify sys.path so the
# tempdir can be found.
sys.path.insert(0, str(py_file.parent))
importlib.import_module(py_file.with_suffix("").name)
sys.path.pop(0) # cleanup


@pytest.mark.skipcomplex
@pytest.mark.parametrize("demo", SERIAL_DEMOS, ids=["/".join(d.loc) for d in SERIAL_DEMOS])
def test_serial_demo(demo, env, monkeypatch, tmpdir, skip_dependency):
_maybe_skip_demo(demo, skip_dependency)
py_file = _prepare_demo(demo, monkeypatch, tmpdir)
_exec_file(py_file)

if "adjoint" in demo.requirements:
pyadjoint.pause_annotation()
pyadjoint.get_working_tape().clear_tape()


@pytest.mark.parallel(2)
@pytest.mark.skipcomplex
@pytest.mark.parametrize("demo", PARALLEL_DEMOS, ids=["/".join(d.loc) for d in PARALLEL_DEMOS])
def test_parallel_demo(demo, env, monkeypatch, tmpdir, skip_dependency):
_maybe_skip_demo(demo, skip_dependency)
py_file = _prepare_demo(demo, monkeypatch, tmpdir)
_exec_file(py_file)

if "adjoint" in demo.requirements:
pyadjoint.pause_annotation()
pyadjoint.get_working_tape().clear_tape()
Binary file added demos/vlasov_poisson_1d/vlasov_0s_HR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demos/vlasov_poisson_1d/vlasov_0s_LR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demos/vlasov_poisson_1d/vlasov_15_HR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demos/vlasov_poisson_1d/vlasov_15_LR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading