diff --git a/examples/acoustics_2d_homogeneous/test_2d_acoustics.py b/examples/acoustics_2d_homogeneous/test_2d_acoustics.py index 96d4f9a1..096ffb9a 100644 --- a/examples/acoustics_2d_homogeneous/test_2d_acoustics.py +++ b/examples/acoustics_2d_homogeneous/test_2d_acoustics.py @@ -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 diff --git a/examples/acoustics_2d_mapped/test_acoustics_2d_mapped.py b/examples/acoustics_2d_mapped/test_acoustics_2d_mapped.py index b9487717..12b9c501 100644 --- a/examples/acoustics_2d_mapped/test_acoustics_2d_mapped.py +++ b/examples/acoustics_2d_mapped/test_acoustics_2d_mapped.py @@ -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 diff --git a/examples/acoustics_2d_variable/test_acoustics_2d_variable.py b/examples/acoustics_2d_variable/test_acoustics_2d_variable.py index 9e7f298c..67c6d3f0 100644 --- a/examples/acoustics_2d_variable/test_acoustics_2d_variable.py +++ b/examples/acoustics_2d_variable/test_acoustics_2d_variable.py @@ -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 diff --git a/examples/acoustics_2d_variable/test_acoustics_2d_variable_io.py b/examples/acoustics_2d_variable/test_acoustics_2d_variable_io.py index 6a2ebe62..d627e97b 100644 --- a/examples/acoustics_2d_variable/test_acoustics_2d_variable_io.py +++ b/examples/acoustics_2d_variable/test_acoustics_2d_variable_io.py @@ -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) diff --git a/examples/acoustics_3d_variable/test_3d_acoustics.py b/examples/acoustics_3d_variable/test_3d_acoustics.py index 643435e2..da3d7b40 100644 --- a/examples/acoustics_3d_variable/test_3d_acoustics.py +++ b/examples/acoustics_3d_variable/test_3d_acoustics.py @@ -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 diff --git a/examples/advection_1d/advection_1d.py b/examples/advection_1d/advection_1d.py index 188cb41e..84765d21 100755 --- a/examples/advection_1d/advection_1d.py +++ b/examples/advection_1d/advection_1d.py @@ -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: diff --git a/examples/advection_1d/test_advection.py b/examples/advection_1d/test_advection.py index b87b6d2f..e739e18e 100644 --- a/examples/advection_1d/test_advection.py +++ b/examples/advection_1d/test_advection.py @@ -1,50 +1,46 @@ -def test_1d_advection(): - """test_1d_advection +from . import advection_1d +import numpy as np +from clawpack.pyclaw.util import check_diff +import os - tests against expected classic, sharpclaw, and high-order weno results """ +def error(**kwargs): + """ + Compute difference between initial and final solutions. + This should vanish due to the periodic boundary conditions + """ - from . import advection_1d + claw = advection_1d.setup(outdir=None,**kwargs) + claw.run() - def verify_expected(expected): - """ given an expected value, returns a verification function """ - def advection_verify(claw): - from clawpack.pyclaw.util import check_diff - import numpy as np + # tests are done across the entire domain of q normally + q0 = claw.frames[0].state.get_q_global() + qfinal = claw.frames[claw.num_output_times].state.get_q_global() - q0=claw.frames[0].state.get_q_global() - qfinal=claw.frames[claw.num_output_times].state.get_q_global() + q0 = q0.reshape([-1]) + qfinal = qfinal.reshape([-1]) + dx = claw.solution.domain.grid.delta[0] + diff = dx*np.sum(np.abs(qfinal-q0)) + return diff - if q0 is not None and qfinal is not None: - dx=claw.solution.domain.grid.delta[0] - test = dx*np.linalg.norm(qfinal-q0,1) - return check_diff(expected, test, reltol=1e-4) - else: - return - return advection_verify - from clawpack.pyclaw.util import gen_variants +class TestAdvection1D: + def test_python_classic(self): + assert abs(error(kernel_language='Python',solver_type='classic')-3.203924e-04)<1e-4 - classic_tests = gen_variants(advection_1d.setup, verify_expected(3.203924e-04), - kernel_languages=('Python','Fortran'), - solver_type='classic', outdir=None) + def test_fortran_classic(self): + assert abs(error(kernel_language='Fortran',solver_type='classic')-3.203924e-04)<1e-4 - sharp_tests_rk = gen_variants(advection_1d.setup, verify_expected(1.163605e-05), - kernel_languages=('Python','Fortran'), - solver_type='sharpclaw',time_integrator='SSP104', outdir=None) + def test_python_sharpclaw(self): + assert abs(error(kernel_language='Python',solver_type='sharpclaw')-1.163605e-05)<1e-4 - sharp_tests_lmm = gen_variants(advection_1d.setup, verify_expected(1.500727e-05), - kernel_languages=('Python','Fortran'), - solver_type='sharpclaw',time_integrator='SSPLMMk3', outdir=None) + def test_fortran_sharpclaw(self): + assert abs(error(kernel_language='Fortran',solver_type='sharpclaw')-1.163605e-05)<1e-4 - weno_tests = gen_variants(advection_1d.setup, verify_expected(7.489618e-06), - kernel_languages=('Fortran',), solver_type='sharpclaw', - time_integrator='SSP104', weno_order=17, - outdir=None) - - from itertools import chain - for test in chain(classic_tests, sharp_tests_rk, sharp_tests_lmm, weno_tests): - yield test + def test_sharpclaw_multistep(self): + assert abs(error(kernel_language='Fortran',solver_type='sharpclaw',time_integrator='SSPLMMk3')-1.500727e-05)<1e-4 + def test_weno17(self): + assert abs(error(kernel_language='Fortran',solver_type='sharpclaw',weno_order=17)-7.489618e-06)<1e-4 if __name__=="__main__": import nose