forked from NCAR/wrf_hydro_nwm_public
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updatedi mports in tests and travis.yml
- Loading branch information
jmills-ncar
committed
Mar 28, 2018
1 parent
ad03da0
commit cab43ee
Showing
5 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import pytest | ||
import sys | ||
import pathlib | ||
from wrfhydro import * | ||
|
||
def pytest_addoption(parser): | ||
parser.addoption('--domain_dir', action='store', help='domain directory') | ||
parser.addoption('--candidate_dir', action='store', help='candidate directory') | ||
parser.addoption('--reference_dir', action='store', help='reference directory') | ||
parser.addoption('--output_dir', action='store', help='test output directory') | ||
|
||
@pytest.fixture(scope="session") | ||
def candidate_sim(request): | ||
# Setup a candidate model | ||
domain_dir = request.config.getoption("--domain_dir") | ||
candidate_dir = request.config.getoption("--candidate_dir") | ||
|
||
# Setup a candidate model | ||
candidate_model = WrfHydroModel(candidate_dir) | ||
|
||
# Setup a domain | ||
domain = WrfHydroDomain(domain_top_dir=domain_dir, | ||
domain_config='NWM', | ||
model_version=candidate_model.version) | ||
|
||
# Setup a candidate simulation | ||
candidate_sim = WrfHydroSim(candidate_model, domain) | ||
|
||
return candidate_sim | ||
|
||
@pytest.fixture(scope="session") | ||
def reference_sim(request): | ||
# Setup a candidate model | ||
domain_dir = request.config.getoption("--domain_dir") | ||
reference_dir = request.config.getoption("--reference_dir") | ||
|
||
# Setup a candidate model | ||
reference_model = WrfHydroModel(reference_dir) | ||
|
||
# Setup a domain | ||
domain = WrfHydroDomain(domain_top_dir=domain_dir, | ||
domain_config='NWM', | ||
model_version=reference_model.version) | ||
|
||
# Setup a candidate simulation | ||
reference_sim = WrfHydroSim(reference_model, domain) | ||
|
||
return reference_sim | ||
|
||
@pytest.fixture(scope="session") | ||
def output_dir(request): | ||
output_dir = pathlib.Path(request.config.getoption("--output_dir")) | ||
if output_dir.is_dir() is False: | ||
output_dir.mkdir() | ||
return output_dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
import sys | ||
from wrfhydro import * | ||
import shutil | ||
import pickle | ||
import datetime as dt | ||
import copy | ||
import warnings | ||
import pytest | ||
|
||
#docker@da16b70279a1:~/wrf_hydro_py/wrfhydro$ pytest wrf_hydro_nwm_test_refactor --domain_dir | ||
# /home/docker/wrf_hydro_py/wrfhydro/tests/data/domain --candidate_dir /home/docker/wrf_hydro_py/wrfhydro/tests/data/wrf_hydro_nwm/source --reference_dir /home/docker/wrf_hydro_py/wrfhydro/tests/data/wrf_hydro_nwm/source --output_dir /home/docker/tests | ||
|
||
################################## | ||
####Setup the test with a domain, a candidate, and a reference | ||
# Get domain, reference, candidate, and optional output directory from command line arguments | ||
# Setup a domain | ||
|
||
|
||
################################## | ||
# Define tests | ||
|
||
###Compile questionscompiler, | ||
def test_compile_candidate(candidate_sim,output_dir): | ||
compile_dir = output_dir / 'compile_candidate' | ||
|
||
# Compile the model | ||
candidate_sim.model.compile(compiler = 'gfort', | ||
compile_dir = compile_dir, | ||
overwrite=True) | ||
|
||
# Check compilation status | ||
assert candidate_sim.model.compile_log.returncode == 0, "Candidate code did not compile correctly" | ||
|
||
|
||
def test_compile_reference(reference_sim,output_dir): | ||
compile_dir = output_dir / 'compile_reference' | ||
|
||
# Compile the model | ||
reference_sim.model.compile(compiler = 'gfort', | ||
compile_dir = compile_dir, | ||
overwrite=True) | ||
|
||
# Check compilation status | ||
assert reference_sim.model.compile_log.returncode == 0, "Reference code did not compile correctly" | ||
|
||
|
||
|
||
###Run questions | ||
def test_run_candidate(candidate_sim,output_dir): | ||
# Set simulation directory | ||
simulation_dir = output_dir / 'run_candidate' | ||
|
||
# Run the simulation | ||
candidate_run = candidate_sim.run(simulation_dir=simulation_dir, | ||
num_cores=2, | ||
mode='w') | ||
|
||
# Check subprocess and model run status | ||
assert candidate_run.run_log.returncode == 0, "Candidate code run exited with non-zero status" | ||
assert candidate_run.run_status == 0, "Candidate code run did not complete" | ||
|
||
def test_run_reference(reference_sim,output_dir): | ||
#Set simulation directory | ||
simulation_dir = output_dir / 'run_reference' | ||
|
||
# Run the simulation | ||
reference_run = reference_sim.run(simulation_dir=simulation_dir, | ||
num_cores=2, | ||
mode='w') | ||
|
||
# Check subprocess and model run status | ||
assert reference_run.run_log.returncode == 0, "Reference code run exited with non-zero status" | ||
assert reference_run.run_status == 0, "Reference code run did not complete" | ||
|
||
|
||
#Ncores question | ||
def test_ncores_candidate(candidate_sim,output_dir): | ||
|
||
# Load initial run model object | ||
candidate_run_expected = pickle.load(open(output_dir / 'run_candidate/WrfHydroRun.pkl', "rb")) | ||
# Set simulation directory | ||
simulation_dir = output_dir.joinpath('ncores_candidate') | ||
|
||
# Run the simulation | ||
candidate_ncores_run = candidate_sim.run(simulation_dir=simulation_dir, | ||
num_cores=1, | ||
mode='w') | ||
|
||
#Check against initial run | ||
ncores_restart_diffs = RestartDiffs(candidate_ncores_run,candidate_run_expected) | ||
|
||
## Check hydro restarts | ||
for diff in ncores_restart_diffs.hydro: | ||
assert diff == None, "Candidate hydro restart files do not match when run with different number of cores" | ||
|
||
## Check lsm restarts | ||
for diff in ncores_restart_diffs.lsm: | ||
assert diff == None, "Candidate lsm restart files do not match when run with different number of cores" | ||
|
||
## Check nudging restarts | ||
for diff in ncores_restart_diffs.nudging: | ||
assert diff == None, "Candidate nudging restart files do not match when run with different number of cores" | ||
|
||
|
||
#Perfect restarts question | ||
def test_perfrestart_candidate(candidate_sim,output_dir): | ||
# Load initial run model object | ||
candidate_run_expected = pickle.load(open(output_dir / 'run_candidate' / 'WrfHydroRun.pkl', | ||
"rb")) | ||
|
||
#Make deep copy since changing namelist optoins | ||
perfrestart_sim = copy.deepcopy(candidate_sim) | ||
|
||
# Set simulation directory | ||
simulation_dir = output_dir / 'restart_candidate' | ||
|
||
#Make directory so that symlinks can be placed | ||
if simulation_dir.is_dir() is True: | ||
shutil.rmtree(str(simulation_dir)) | ||
simulation_dir.mkdir(parents=True) | ||
|
||
# Symlink restarts files to new directory and modify namelistrestart files | ||
|
||
# Hydro | ||
hydro_rst = candidate_run_expected.restart_hydro[0] | ||
new_hydro_rst_path = simulation_dir.joinpath(hydro_rst.name) | ||
new_hydro_rst_path.symlink_to(hydro_rst) | ||
|
||
perfrestart_sim.hydro_namelist['hydro_nlist'].update( | ||
{'restart_file': str(new_hydro_rst_path)}) | ||
|
||
# LSM | ||
lsm_rst = candidate_run_expected.restart_lsm[0] | ||
new_lsm_rst_path = simulation_dir.joinpath(lsm_rst.name) | ||
new_lsm_rst_path.symlink_to(lsm_rst) | ||
|
||
perfrestart_sim.namelist_hrldas['noahlsm_offline'].update( | ||
{'restart_filename_requested': str(simulation_dir.joinpath(lsm_rst.name))}) | ||
|
||
# Nudging | ||
if len(candidate_run_expected.restart_nudging) > 0: | ||
nudging_rst = candidate_run_expected.restart_nudging[0] | ||
new_nudging_rst_path = simulation_dir.joinpath(nudging_rst.name) | ||
new_nudging_rst_path.symlink_to(nudging_rst) | ||
|
||
perfrestart_sim.hydro_namelist['nudging_nlist'].update( | ||
{'nudginglastobsfile': str(simulation_dir.joinpath(nudging_rst.name))}) | ||
|
||
#Move simulation start time to restart time in hydro restart file | ||
start_dt = hydro_rst.open() | ||
start_dt = dt.datetime.strptime(start_dt.Restart_Time,'%Y-%m-%d_%H:%M:%S') | ||
perfrestart_sim.namelist_hrldas['noahlsm_offline'].update( | ||
{'start_year': start_dt.year, | ||
'start_month': start_dt.month, | ||
'start_day': start_dt.day, | ||
'start_hour': start_dt.hour, | ||
'start_min': start_dt.minute}) | ||
|
||
#Adjust duration to be shorter by restart time delta in days | ||
hydro_rst_dt = perfrestart_sim.hydro_namelist['hydro_nlist']['rst_dt'] | ||
previous_duration = candidate_run_expected.simulation.namelist_hrldas['noahlsm_offline'][ | ||
'kday'] | ||
new_duration = int(previous_duration - hydro_rst_dt/60/24) | ||
perfrestart_sim.namelist_hrldas['noahlsm_offline'].update({'kday':new_duration}) | ||
|
||
# Run the simulation | ||
with warnings.catch_warnings(): | ||
warnings.simplefilter("ignore") | ||
candidate_perfrestart_run = perfrestart_sim.run(simulation_dir=simulation_dir, | ||
num_cores=2, | ||
mode='a') | ||
|
||
#Check against initial run | ||
perfstart_restart_diffs = RestartDiffs(candidate_perfrestart_run,candidate_run_expected) | ||
## Check hydro restarts | ||
for diff in perfstart_restart_diffs.hydro: | ||
assert diff == None, "Candidate hydro restart files do not match when starting from a restart" | ||
|
||
## Check lsm restarts | ||
for diff in perfstart_restart_diffs.lsm: | ||
assert diff == None, "Candidate lsm restart files do not match when starting from a restart" | ||
|
||
## Check nudging restarts | ||
for diff in perfstart_restart_diffs.nudging: | ||
assert diff == None, "Candidate nudging restart files do not match when starting from a restart" | ||
|
||
#regression question | ||
def test_regression(output_dir): | ||
candidate_run_expected = pickle.load(open(output_dir / 'run_candidate' / 'WrfHydroRun.pkl', | ||
"rb")) | ||
reference_run_expected = pickle.load(open(output_dir / 'run_reference' / 'WrfHydroRun.pkl', | ||
"rb")) | ||
#Check regression | ||
regression_diffs = RestartDiffs(candidate_run_expected,reference_run_expected) | ||
|
||
## Check hydro restarts | ||
for diff in regression_diffs.hydro: | ||
assert diff == None, "Candidate hydro restart files do not regress on reference restart files" | ||
|
||
## Check lsm restarts | ||
for diff in regression_diffs.lsm: | ||
assert diff == None, "Candidate lsm restart files do not regress on reference restart files" | ||
|
||
## Check nudging restarts | ||
for diff in regression_diffs.nudging: | ||
assert diff == None, "Candidate nudging restart files do not regress on reference restart files" |
Binary file not shown.