Skip to content

Commit

Permalink
Get more tests working with pytest.
Browse files Browse the repository at this point in the history
  • Loading branch information
ketch committed May 20, 2024
1 parent 7cdbfaf commit 60c872c
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 249 deletions.
63 changes: 22 additions & 41 deletions examples/acoustics_2d_homogeneous/test_2d_acoustics.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,30 @@
def test_2d_acoustics():
"""test_2d_acoustics"""
from . import acoustics_2d
import numpy as np
from clawpack.pyclaw.util import check_diff
import os

def verify_data(data_filename):
def verify(claw):
""" verifies 2d homogeneous acoustics from a previously verified run """
import os
import numpy as np
from clawpack.pyclaw.util import check_diff
def check_error(data_filename,**kwargs):

claw = acoustics_2d.setup(disable_output=True,**kwargs)
claw.run()

#grabs parallel results to process 0, None to other processes
test_q=claw.solution.state.get_q_global()
test_pressure = claw.frames[-1].q[0,:,:]
thisdir = os.path.dirname(__file__)
expected_pressure = np.loadtxt(os.path.join(thisdir,data_filename))
return check_diff(expected_pressure, test_pressure, reltol=1e-3,
delta=claw.solution.grid.delta)

if test_q is not None:
test_pressure = test_q[0,:,:]
thisdir = os.path.dirname(__file__)
expected_pressure = np.loadtxt(os.path.join(thisdir,data_filename))
return check_diff(expected_pressure, test_pressure, reltol=1e-3,
delta=claw.solution.grid.delta)
else:
return
return verify

from clawpack.pyclaw.util import gen_variants
from . import acoustics_2d
class TestAcoustics2D:
def test_classic(self):
assert check_error('verify_classic.txt',solver_type='classic')==None

classic_tests = gen_variants(acoustics_2d.setup, verify_data('verify_classic.txt'),
kernel_languages=('Fortran',), solver_type='classic', disable_output=True)
def test_classic_ptwise(self):
assert check_error('verify_classic.txt',solver_type='classic',ptwise=True)==None

ptwise_tests = gen_variants(acoustics_2d.setup, verify_data('verify_classic.txt'),
kernel_languages=('Fortran',), ptwise=True, solver_type='classic', disable_output=True)
def test_sharpclaw(self):
assert check_error('verify_sharpclaw.txt',solver_type='sharpclaw')==None

sharp_tests_rk = gen_variants(acoustics_2d.setup, verify_data('verify_sharpclaw.txt'),
kernel_languages=('Fortran',), solver_type='sharpclaw',
time_integrator='SSP104', disable_output=True)

sharp_tests_lmm = gen_variants(acoustics_2d.setup, verify_data('verify_sharpclaw_lmm.txt'),
kernel_languages=('Fortran',), solver_type='sharpclaw',
time_integrator='SSPLMMk2', disable_output=True)

from itertools import chain
for test in chain(classic_tests, ptwise_tests, sharp_tests_rk, sharp_tests_lmm):
yield test


if __name__=="__main__":
import nose
nose.main()
def test_sharpclaw_multistep(self):
assert check_error('verify_sharpclaw_lmm.txt',solver_type='sharpclaw',
time_integrator='SSPLMMk2')==None
51 changes: 16 additions & 35 deletions examples/acoustics_2d_mapped/test_acoustics_2d_mapped.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,21 @@
def test_acoustics_2d_variable():
"""Test variable-coefficient 2D acoustics on mapped grids"""
from . import acoustics_2d_inclusions
import numpy as np
from clawpack.pyclaw.util import check_diff
import os

import acoustics_2d_inclusions
def check_error(data_filename,**kwargs):

def verify_acoustics_inclusions(controller, solver_type='classic'):
""" Regression test against data from a previous run."""
import os
from clawpack.pyclaw.util import check_diff
import numpy as np
claw = acoustics_2d_inclusions.setup(disable_output=True,**kwargs)
claw.run()
test_pressure = claw.frames[-1].q[0,:,:]

state = controller.frames[controller.num_output_times].state
dx, dy = controller.solution.domain.grid.delta
test_q = state.get_q_global()
thisdir = os.path.dirname(__file__)
expected_pressure = np.load(os.path.join(thisdir, 'pressure.npz'))['arr_0']
test_err = np.max(np.abs(expected_pressure[:].reshape(-1) -
test_pressure[:].reshape(-1)))
return check_diff(0, test_err, abstol=1e-7)

if test_q is not None:
thisdir = os.path.dirname(__file__)
expected_pressure = np.load(os.path.join(thisdir,
'pressure.npz'))['arr_0']
test_pressure = test_q[0,:,:]
test_err = np.max(np.abs(expected_pressure[:].reshape(-1) -
test_pressure[:].reshape(-1)))
return check_diff(0, test_err, abstol=1e-7)


from clawpack.pyclaw.util import gen_variants

verify_func = lambda controller: verify_acoustics_inclusions(controller, solver_type='classic')
classic_tests = gen_variants(acoustics_2d_inclusions.setup, verify_func,
solver_type='classic', disable_output=True,
num_cells=100,num_output_times=1)

from itertools import chain
for test in chain(classic_tests):
yield test


if __name__=="__main__":
import nose
nose.main()
def test_classic():
assert check_error('verify_classic.txt',solver_type='classic',
num_cells=100,num_output_times=1)==None
71 changes: 23 additions & 48 deletions examples/acoustics_2d_variable/test_acoustics_2d_variable.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,30 @@
def test_acoustics_2d_variable():
"""Test variable-coefficient 2D acoustics"""
from . import acoustics_2d_interface
import numpy as np
from clawpack.pyclaw.util import check_diff
import os

from . import acoustics_2d_interface
def check_error(**kwargs):

def verify_acoustics(controller, solver_type='classic'):
""" Verifies 2d variable-coefficient acoustics from a previously verified classic run """
import os
from clawpack.pyclaw.util import check_diff
import numpy as np
solver_type = kwargs['solver_type']
claw = acoustics_2d_interface.setup(disable_output=True,**kwargs)
claw.run()
test_pressure = claw.frames[-1].q[0,:,:]

state = controller.frames[controller.num_output_times].state
dx, dy = controller.solution.domain.grid.delta
test_q = state.get_q_global()
thisdir = os.path.dirname(__file__)
expected_pressure = np.loadtxt(os.path.join(thisdir, 'pressure_%s.txt' % solver_type))
test_err = np.max(np.abs(expected_pressure[:].reshape(-1) -
test_pressure[:].reshape(-1)))
print(test_err)
return check_diff(0, test_err, abstol=1e-1)

if test_q is not None:
thisdir = os.path.dirname(__file__)
expected_pressure = np.loadtxt(os.path.join(thisdir,
'pressure_%s.txt' % solver_type))
test_pressure = test_q[0,:,:]
#test_err = dx*dy*np.linalg.norm(expected_pressure-test_pressure)
test_err = np.max(np.abs(expected_pressure[:].reshape(-1) -
test_pressure[:].reshape(-1)))
return check_diff(0, test_err, abstol=1e-1)

class TestAcoustics2D:
def test_classic(self):
assert check_error(solver_type='classic',num_cells=(50,50))==None

from clawpack.pyclaw.util import gen_variants
def test_sharpclaw(self):
assert check_error(solver_type='sharpclaw',num_cells=(50,50))==None

verify_func = lambda controller: verify_acoustics(controller, solver_type='classic')
classic_tests = gen_variants(acoustics_2d_interface.setup, verify_func,
solver_type='classic', disable_output=True,
num_cells=(50, 50))

verify_func = lambda controller: verify_acoustics(controller, solver_type='sharpclaw')
sharp_tests_rk = gen_variants(acoustics_2d_interface.setup,
verify_func,
solver_type='sharpclaw',
time_integrator='SSP104',
disable_output=True, num_cells=(50, 50))

sharp_tests_lmm = gen_variants(acoustics_2d_interface.setup,
verify_func, lim_type=1,
solver_type='sharpclaw',
time_integrator='SSPLMMk2',
disable_output=True,
num_cells=(50, 50))

from itertools import chain
for test in chain(classic_tests, sharp_tests_rk, sharp_tests_lmm):
yield test


if __name__=="__main__":
import nose
nose.main()
def test_sharpclaw_multistep(self):
assert check_error(solver_type='sharpclaw',num_cells=(50,50),
time_integrator='SSPLMMk2')==None
26 changes: 3 additions & 23 deletions examples/acoustics_2d_variable/test_acoustics_2d_variable_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,7 @@ def verify_acoustics_io(controller):
else:
return


from clawpack.pyclaw.util import gen_variants
tempdir = './_io_test_results'
classic_tests = gen_variants(acoustics_2d_interface.setup,
verify_acoustics_io, solver_type='classic',
outdir=tempdir, num_cells=(50, 50), disable_petsc=True)

import shutil
from itertools import chain
try:
for test in chain(classic_tests):
yield test
finally:
ERROR_STR= """Error removing %(path)s, %(error)s """
try:
shutil.rmtree(tempdir )
except OSError as xxx_todo_changeme:
(errno, strerror) = xxx_todo_changeme.args
print(ERROR_STR % {'path' : tempdir, 'error': strerror })


if __name__=="__main__":
import nose
nose.main()
claw = acoustics_2d_interface.setup(outdir=tempdir,num_cells=(50,50))
claw.run()
verify_acoustics_io(claw)
77 changes: 12 additions & 65 deletions examples/acoustics_3d_variable/test_3d_acoustics.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,23 @@
import os
from itertools import chain

import numpy as np

from clawpack.pyclaw.util import gen_variants
from clawpack.pyclaw.util import check_diff

from . import acoustics_3d_interface

def check_error(**kwargs):

def test_3d_acoustics():
""" Tests for homogeneous and heterogeneous 3D acoustics"""

def acoustics_verify_homogeneous(claw):
""" Regression test for 3D homogeneous acoustics equations.
"""

pinitial = claw.frames[0].state.get_q_global()
pfinal = claw.frames[claw.num_output_times].state.get_q_global()

if pinitial is not None:
pinitial = pinitial[0, :, :, :].reshape(-1)
pfinal = pfinal[0, :, :, :].reshape(-1)
grid = claw.solution.state.grid
final_difference = np.prod(grid.delta)*np.linalg.norm(pfinal-pinitial, ord=1)
return check_diff(0., final_difference, abstol=1e-1)
else:
# In parallel, we check values only for the rank 0 process
return

def acoustics_verify_heterogeneous(claw):
""" Regression test for 3D heterogeneous acoustics equations
"""

pinitial = claw.frames[0].state.get_q_global()
pfinal = claw.frames[claw.num_output_times].state.get_q_global()

if pinitial is not None:
pfinal = pfinal[0, :, :, :].reshape(-1)
thisdir = os.path.dirname(__file__)
verify_pfinal = np.loadtxt(os.path.join(thisdir, 'verify_classic_heterogeneous.txt'))
norm_err = np.linalg.norm(pfinal-verify_pfinal)
return check_diff(0, norm_err, abstol=10.)
else:
# In parallel, we check values only for the rank 0 process
return

classic_homogeneous_tests = gen_variants(acoustics_3d_interface.setup, acoustics_verify_homogeneous,
kernel_languages=('Fortran',),
solver_type='classic', problem='homogeneous',
disable_output=True,mx=128,my=4,mz=4)

classic_heterogeneous_tests = gen_variants(acoustics_3d_interface.setup, acoustics_verify_heterogeneous,
kernel_languages=('Fortran',),
solver_type='classic', problem='heterogeneous',
disable_output=True,mx=15,my=15,mz=15)

sharp_homogeneous_tests = gen_variants(acoustics_3d_interface.setup, acoustics_verify_homogeneous,
kernel_languages=('Fortran',),
solver_type='sharpclaw', problem='homogeneous',
disable_output=True,mx=128,my=4,mz=4)
claw = acoustics_3d_interface.setup(problem='homogeneous',mx=128,my=4,mz=4,disable_output=True,**kwargs)
claw.run()

sharp_heterogeneous_tests = gen_variants(acoustics_3d_interface.setup, acoustics_verify_heterogeneous,
kernel_languages=('Fortran',),
solver_type='sharpclaw', problem='heterogeneous',
disable_output=True,mx=15,my=15,mz=15)
test_pressure = claw.frames[-1].q[0,:,:,:]
expected_pressure = claw.frames[0].q[0,:,:,:]
return check_diff(expected_pressure, test_pressure, reltol=1e-1,
delta=claw.solution.grid.delta)

for test in chain(classic_homogeneous_tests, classic_heterogeneous_tests, sharp_homogeneous_tests,
sharp_heterogeneous_tests):
yield test

class TestAcoustics3D:
def test_classic(self):
assert check_error(solver_type='classic')==None

if __name__=="__main__":
import nose
nose.main()
def test_sharpclaw(self):
assert check_error(solver_type='sharpclaw')==None
2 changes: 1 addition & 1 deletion examples/advection_1d/advection_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import numpy as np
from clawpack import riemann

def setup(nx=100, kernel_language='Python', use_petsc=False, solver_type='classic', weno_order=5,
def setup(nx=400, kernel_language='Python', use_petsc=False, solver_type='classic', weno_order=5,
time_integrator='SSP104', outdir='./_output'):

if use_petsc:
Expand Down
Loading

0 comments on commit 60c872c

Please sign in to comment.