Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion examples/cases/KUL_LES/wind_energy_system/analysis_US.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ HPC_config:
mesh_node_number: 2
mesh_ntasks_per_node: 48
mesh_wall_time_hours: 1
run_partition: ""
mesh_partition: ""
#
wckey: ""

Expand Down
106 changes: 106 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""
Pytest configuration and fixtures for WIFA tests.

Provides:
- Pre-test cleanup of leftover output directories
- Output directory fixtures with conditional cleanup (preserved on failure)
"""

import shutil
from pathlib import Path

import pytest

# Store test outcomes for conditional cleanup
_test_outcomes = {}


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""Track test outcomes to conditionally preserve output on failure."""
outcome = yield
report = outcome.get_result()
if report.when == "call":
_test_outcomes[item.nodeid] = report.failed


def pytest_configure(config):
"""Register custom markers."""
config.addinivalue_line(
"markers", "slow: marks tests as slow (deselect with '-m \"not slow\"')"
)


@pytest.fixture(scope="session", autouse=True)
def cleanup_old_outputs():
"""Remove output directories from previous test runs at session start."""
patterns = [
"output_pywake_*",
"output_test_*",
"output",
]
for pattern in patterns:
for path in Path(".").glob(pattern):
if path.is_dir():
shutil.rmtree(path)
elif path.is_file():
path.unlink()
yield


@pytest.fixture
def output_dir(request, tmp_path):
"""
Provide a unique temporary output directory for tests.

Cleans up automatically on test success, preserves on failure for debugging.
"""
yield tmp_path

# Only clean up if test passed
test_failed = _test_outcomes.get(request.node.nodeid, False)
if not test_failed:
if tmp_path.exists():
shutil.rmtree(tmp_path)


@pytest.fixture
def named_output_dir(request):
"""
Provide a named output directory based on test name.

Use this when tests need output in the current working directory
(e.g., for Code Saturne integration).

Cleans up automatically on test success, preserves on failure.
"""
test_name = request.node.name.replace("[", "_").replace("]", "_").rstrip("_")
output_path = Path(f"output_test_{test_name}")
output_path.mkdir(parents=True, exist_ok=True)

yield output_path

# Only clean up if test passed
test_failed = _test_outcomes.get(request.node.nodeid, False)
if not test_failed:
if output_path.exists():
shutil.rmtree(output_path)


@pytest.fixture
def cleanup_output_dir(request):
"""
Cleanup fixture for tests that write to the default 'output/' directory.

Use this when tests can't control their output location (e.g., when YAML
specifies output_folder). Cleans up on success, preserves on failure.
"""
output_path = Path("output")

yield output_path

# Only clean up if test passed
test_failed = _test_outcomes.get(request.node.nodeid, False)
if not test_failed:
if output_path.exists():
shutil.rmtree(output_path)
33 changes: 14 additions & 19 deletions tests/test_cs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,46 @@


def _run_cs(wes_dir, output_dir):
"""Run Code Saturne on all system* files in the given directory."""
i = 1
for yaml_input in wes_dir.glob("system*"):
print("\nRUNNING CODE_SATURNE ON", yaml_input, "\n")
validate_yaml(yaml_input, Path("plant/wind_energy_system"))
# Pass subdirectory path - run_code_saturne will create it
sub_output_dir = output_dir / f"run_{i}"
run_code_saturne(
yaml_input,
test_mode=False,
output_dir="output_test_" + output_dir + "_" + str(i),
output_dir=str(sub_output_dir),
)
i += 1


def test_cs_KUL():
def test_cs_KUL(output_dir):
wes_dir = test_path / "../examples/cases/KUL_LES/wind_energy_system/"
_run_cs(wes_dir, "KUL")
_run_cs(wes_dir, output_dir)


def test_cs_4wts():
def test_cs_4wts(output_dir):
wes_dir = test_path / "../examples/cases/windio_4turbines/wind_energy_system/"
_run_cs(wes_dir, "4wts")
_run_cs(wes_dir, output_dir)


def test_cs_abl():
def test_cs_abl(output_dir):
wes_dir = test_path / "../examples/cases/windio_4turbines_ABL/wind_energy_system/"
_run_cs(wes_dir, "abl")
_run_cs(wes_dir, output_dir)


def test_cs_abl_stable():
def test_cs_abl_stable(output_dir):
wes_dir = (
test_path / "../examples/cases/windio_4turbines_ABL_stable/wind_energy_system/"
)
_run_cs(wes_dir, "abl_stable")
_run_cs(wes_dir, output_dir)


def test_cs_profiles():
def test_cs_profiles(output_dir):
wes_dir = (
test_path
/ "../examples/cases/windio_4turbines_profiles_stable/wind_energy_system/"
)
_run_cs(wes_dir, "profiles")


if __name__ == "__main__":
test_cs_KUL()
test_cs_4wts()
test_cs_abl()
test_cs_abl_stable()
test_cs_profiles()
_run_cs(wes_dir, output_dir)
55 changes: 21 additions & 34 deletions tests/test_foxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,82 +11,69 @@
windIO_path = Path(wiop[0])


def _run_foxes(wes_dir):
def _run_foxes(wes_dir, output_dir):
"""Run FOXES on all system.yaml files in the given directory."""
assert wes_dir.is_dir(), f"{wes_dir} is not a directory"

for yaml_input in wes_dir.glob("system.yaml"):
print("\nRUNNING FOXES ON", yaml_input, "\n")
validate_yaml(yaml_input, Path("plant/wind_energy_system"))
output_dir_name = Path("output_test_foxes")
output_dir_name.mkdir(parents=True, exist_ok=True)
run_foxes(yaml_input, output_dir=output_dir_name)
rmtree(output_dir_name)
run_foxes(yaml_input, output_dir=output_dir)


def test_foxes_KUL():
def test_foxes_KUL(output_dir):
wes_dir = test_path / "../examples/cases/KUL_LES/wind_energy_system/"
_run_foxes(wes_dir)
_run_foxes(wes_dir, output_dir)


def test_foxes_4wts():
def test_foxes_4wts(output_dir):
wes_dir = test_path / "../examples/cases/windio_4turbines/wind_energy_system/"
_run_foxes(wes_dir)
_run_foxes(wes_dir, output_dir)


def test_foxes_abl():
def test_foxes_abl(output_dir):
wes_dir = test_path / "../examples/cases/windio_4turbines_ABL/wind_energy_system/"
_run_foxes(wes_dir)
_run_foxes(wes_dir, output_dir)


def test_foxes_abl_stable():
def test_foxes_abl_stable(output_dir):
wes_dir = (
test_path / "../examples/cases/windio_4turbines_ABL_stable/wind_energy_system/"
)
_run_foxes(wes_dir)
_run_foxes(wes_dir, output_dir)


def test_foxes_profiles():
def test_foxes_profiles(output_dir):
wes_dir = (
test_path
/ "../examples/cases/windio_4turbines_profiles_stable/wind_energy_system/"
)
_run_foxes(wes_dir)
_run_foxes(wes_dir, output_dir)


def test_foxes_heterogeneous_wind_rose_at_turbines():
def test_foxes_heterogeneous_wind_rose_at_turbines(output_dir):
wes_dir = (
test_path
/ "../examples/cases/heterogeneous_wind_rose_at_turbines/wind_energy_system/"
)
_run_foxes(wes_dir)
_run_foxes(wes_dir, output_dir)


def test_foxes_heterogeneous_wind_rose_map():
def test_foxes_heterogeneous_wind_rose_map(output_dir):
wes_dir = (
test_path / "../examples/cases/heterogeneous_wind_rose_map/wind_energy_system/"
)
_run_foxes(wes_dir)
_run_foxes(wes_dir, output_dir)


def test_foxes_simple_wind_rose():
def test_foxes_simple_wind_rose(output_dir):
wes_dir = test_path / "../examples/cases/simple_wind_rose/wind_energy_system/"
_run_foxes(wes_dir)
_run_foxes(wes_dir, output_dir)


def test_foxes_timeseries_with_operating_flag():
def test_foxes_timeseries_with_operating_flag(output_dir):
wes_dir = (
test_path
/ "../examples/cases/timeseries_with_operating_flag/wind_energy_system/"
)
_run_foxes(wes_dir)


if __name__ == "__main__":
test_foxes_KUL()
test_foxes_4wts()
test_foxes_abl()
test_foxes_abl_stable()
test_foxes_profiles()
test_foxes_heterogeneous_wind_rose_at_turbines()
test_foxes_heterogeneous_wind_rose_map()
test_foxes_simple_wind_rose()
_run_foxes(wes_dir, output_dir)
25 changes: 9 additions & 16 deletions tests/test_pywake.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def four_turbine_site(config_params):
return wfm(x, y, ws=ws, wd=wd, time=True), config_name


def test_pywake_KUL():
def test_pywake_KUL(output_dir):
yaml_input = (
test_path / "../examples/cases/KUL_LES/wind_energy_system/system_pywake.yaml"
)
Expand All @@ -59,10 +59,7 @@ def test_pywake_KUL():
validate_yaml(yaml_input, Path("plant/wind_energy_system"))

# compute AEP (next step is to return a richer set of outputs)
output_dir_name = "output_pywake_4wts"
Path(output_dir_name).mkdir(parents=True, exist_ok=True)
pywake_aep = run_pywake(yaml_input, output_dir=output_dir_name)
# print(pywake_aep)
pywake_aep = run_pywake(yaml_input, output_dir=output_dir)

# Check result
pywake_aep_expected = 7515.2
Expand All @@ -87,7 +84,7 @@ def config_params(request):
return request.param


def test_pywake_4wts(four_turbine_site):
def test_pywake_4wts(four_turbine_site, output_dir):
wfm, config_name = four_turbine_site

yaml_input = (
Expand All @@ -98,17 +95,14 @@ def test_pywake_4wts(four_turbine_site):
validate_yaml(yaml_input, Path("plant/wind_energy_system"))

# compute AEP (next step is to return a richer set of outputs)
output_dir_name = "output_pywake_4wts"
Path(output_dir_name).mkdir(parents=True, exist_ok=True)
pywake_aep = run_pywake(yaml_input, output_dir=output_dir_name)
# print(pywake_aep)
pywake_aep = run_pywake(yaml_input, output_dir=output_dir)

# Check result
pywake_aep_expected = wfm.aep().sum()
npt.assert_array_almost_equal(pywake_aep, pywake_aep_expected, 0)


def test_pywake_4wts_operating_flag():
def test_pywake_4wts_operating_flag(output_dir):
x = [0, 1248.1, 2496.2, 3744.3]
y = [0, 0, 0, 0]
ws = [10.0910225, 10.233016, 8.797999, 9.662098, 9.78371, 10.307792]
Expand Down Expand Up @@ -148,10 +142,7 @@ def test_pywake_4wts_operating_flag():
validate_yaml(yaml_input, Path("plant/wind_energy_system"))

# compute AEP (next step is to return a richer set of outputs)
output_dir_name = "output_pywake_4wts"
Path(output_dir_name).mkdir(parents=True, exist_ok=True)
pywake_aep = run_pywake(yaml_input, output_dir=output_dir_name)
# print(pywake_aep)
pywake_aep = run_pywake(yaml_input, output_dir=output_dir)

# Check result
pywake_aep_expected = res.aep().sum()
Expand Down Expand Up @@ -181,7 +172,9 @@ def test_pywake_4wts_operating_flag():
# fmt: on


def test_simple_wind_rose():
def test_simple_wind_rose(cleanup_output_dir):
# Note: This test uses the output_folder from the YAML ("output/"), not a fixture.
# The cleanup_output_dir fixture handles cleanup of "output/".
_ = run_pywake(
test_path / "../examples/cases/simple_wind_rose/wind_energy_system/system.yaml"
)
Expand Down
13 changes: 4 additions & 9 deletions tests/test_wayve.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from pathlib import Path

import pytest
from windIO import __path__ as wiop
from windIO import validate as validate_yaml

Expand All @@ -10,16 +11,10 @@
windIO_path = Path(wiop[0])


# @pytest.mark.skip()
def test_wayve_4wts():
@pytest.mark.slow
def test_wayve_4wts(output_dir):
yaml_input = (
test_path / "../examples/cases/windio_4turbines/wind_energy_system/system.yaml"
)
validate_yaml(yaml_input, Path("plant/wind_energy_system"))
output_dir_name = Path("output_test_wayve")
output_dir_name.mkdir(parents=True, exist_ok=True)
run_wayve(yaml_input, output_dir=output_dir_name, debug_mode=True)


if __name__ == "__main__":
test_wayve_4wts()
run_wayve(yaml_input, output_dir=output_dir, debug_mode=True)
Loading