Work on bug with test script - calling API in for loop.

这个提交包含在:
Craig Warren
2019-12-09 17:11:11 +00:00
父节点 342613a781
当前提交 e5e747ac35
共有 2 个文件被更改,包括 27 次插入105 次删除

查看文件

@@ -1,85 +0,0 @@
import sys
import unittest
# http://stackoverflow.com/a/17981937/1942837
from contextlib import contextmanager
from io import StringIO
@contextmanager
def captured_output():
new_out, new_err = StringIO(), StringIO()
old_out, old_err = sys.stdout, sys.stderr
try:
sys.stdout, sys.stderr = new_out, new_err
yield sys.stdout, sys.stderr
finally:
sys.stdout, sys.stderr = old_out, old_err
# end stack copy
from gprMax.input_cmd_funcs import *
class My_input_cmd_funcs_test(unittest.TestCase):
def assert_output(self, out, expected_out):
"""helper function"""
output = out.getvalue().strip()
self.assertEqual(output, expected_out)
def test_rx(self):
with captured_output() as (out, err):
rx(0, 0, 0)
self.assert_output(out, '#rx: 0 0 0')
def test_rx2(self):
with captured_output() as (out, err):
rx(0, 1, 2, 'id')
self.assert_output(out, '#rx: 0 1 2 id')
def test_rx3(self):
with captured_output() as (out, err):
rx(2, 1, 0, 'idd', ['Ex'])
self.assert_output(out, '#rx: 2 1 0 idd Ex')
def test_rx4(self):
with captured_output() as (out, err):
rx(2, 1, 0, 'id', ['Ex', 'Ez'])
self.assert_output(out, '#rx: 2 1 0 id Ex Ez')
def test_rx_rotate_exception(self):
with self.assertRaises(ValueError):
rx(2, 1, 0, 'id', ['Ex', 'Ez'], polarisation='x', rotate90origin=(1, 1)) # no dxdy given
def test_rx_rotate_success(self):
with captured_output() as (out, err):
rx(2, 1, 0, 'id', ['Ex', 'Ez'], polarisation='x', rotate90origin=(1, 1), dxdy=(0, 0))
self.assert_output(out, '#rx: 1 2 0 id Ex Ez') # note: x, y swapped
def test_rx_rotate_success2(self):
with captured_output() as (out, err):
rx(2, 1, 0, 'id', ['Ex', 'Ez'], polarisation='y', rotate90origin=(1, 1), dxdy=(0, 0))
self.assert_output(out, '#rx: 1 2 0 id Ex Ez') # note: x, y swapped
def test_src_steps(self):
with captured_output() as (out, err):
src_steps()
self.assert_output(out, '#src_steps: 0 0 0')
def test_src_steps2(self):
with captured_output() as (out, err):
src_steps(42, 43, 44.2)
self.assert_output(out, '#src_steps: 42 43 44.2')
def test_rx_steps(self):
with captured_output() as (out, err):
rx_steps()
self.assert_output(out, '#rx_steps: 0 0 0')
def test_rx_steps2(self):
with captured_output() as (out, err):
rx_steps(42, 43, 44.2)
self.assert_output(out, '#rx_steps: 42 43 44.2')
if __name__ == '__main__':
unittest.main()

查看文件

@@ -16,7 +16,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with gprMax. If not, see <http://www.gnu.org/licenses/>. # along with gprMax. If not, see <http://www.gnu.org/licenses/>.
import os from pathlib import Path
import sys import sys
from colorama import init, Fore, Style from colorama import init, Fore, Style
@@ -28,7 +28,7 @@ import matplotlib.pyplot as plt
if sys.platform == 'linux': if sys.platform == 'linux':
plt.switch_backend('agg') plt.switch_backend('agg')
from gprMax.gprMax import api import gprMax
from gprMax.exceptions import GeneralError from gprMax.exceptions import GeneralError
from tests.analytical_solutions import hertzian_dipole_fs from tests.analytical_solutions import hertzian_dipole_fs
@@ -39,13 +39,17 @@ from tests.analytical_solutions import hertzian_dipole_fs
python -m tests.test_models python -m tests.test_models
""" """
basepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'models_') # Specify directoty with set of models to test
basepath += 'basic' modelset = 'models_basic'
# basepath += 'advanced' # modelset += 'models_advanced'
# basepath += 'pmls' # modelset += 'models_pmls'
basepath = Path(__file__).parents[0] / 'tests' / modelset
# List of available basic test models # List of available basic test models
testmodels = ['hertzian_dipole_fs_analytical', '2D_ExHyHz', '2D_EyHxHz', '2D_EzHxHy', 'cylinder_Ascan_2D', 'hertzian_dipole_fs', 'hertzian_dipole_hs', 'hertzian_dipole_dispersive', 'magnetic_dipole_fs'] # testmodels = ['hertzian_dipole_fs_analytical', '2D_ExHyHz', '2D_EyHxHz', '2D_EzHxHy', 'cylinder_Ascan_2D', 'hertzian_dipole_fs', 'hertzian_dipole_hs', 'hertzian_dipole_dispersive', 'magnetic_dipole_fs']
testmodels = ['2D_ExHyHz', '2D_EyHxHz', '2D_EzHxHy']
# List of available advanced test models # List of available advanced test models
# testmodels = ['antenna_GSSI_1500_fs', 'antenna_MALA_1200_fs'] # testmodels = ['antenna_GSSI_1500_fs', 'antenna_MALA_1200_fs']
@@ -67,13 +71,13 @@ for i, model in enumerate(testmodels):
testresults[model] = {} testresults[model] = {}
# Run model # Run model
inputfile = os.path.join(basepath, model + os.path.sep + model + '.in') file = basepath / model / model
api(inputfile, gpu=None) gprMax.run(inputfile=file.with_suffix('.in'), gpu=None)
# Special case for analytical comparison # Special case for analytical comparison
if model == 'hertzian_dipole_fs_analytical': if model == 'hertzian_dipole_fs_analytical':
# Get output for model file # Get output for model file
filetest = h5py.File(os.path.join(basepath, model + os.path.sep + model + '.out'), 'r') filetest = h5py.File(file.with_suffix('.out'), 'r')
testresults[model]['Test version'] = filetest.attrs['gprMax'] testresults[model]['Test version'] = filetest.attrs['gprMax']
# Get available field output component names # Get available field output component names
@@ -103,8 +107,10 @@ for i, model in enumerate(testmodels):
else: else:
# Get output for model and reference files # Get output for model and reference files
fileref = h5py.File(os.path.join(basepath, model + os.path.sep + model + '_ref.out'), 'r') fileref = file.stem + '_ref'
filetest = h5py.File(os.path.join(basepath, model + os.path.sep + model + '.out'), 'r') fileref = file.parent / Path(fileref)
fileref = h5py.File(fileref.with_suffix('.out'), 'r')
filetest = h5py.File(file.with_suffix('.out'), 'r')
testresults[model]['Ref version'] = fileref.attrs['gprMax'] testresults[model]['Ref version'] = fileref.attrs['gprMax']
testresults[model]['Test version'] = filetest.attrs['gprMax'] testresults[model]['Test version'] = filetest.attrs['gprMax']
@@ -116,7 +122,7 @@ for i, model in enumerate(testmodels):
# Check that type of float used to store fields matches # Check that type of float used to store fields matches
if filetest[path + outputstest[0]].dtype != fileref[path + outputsref[0]].dtype: if filetest[path + outputstest[0]].dtype != fileref[path + outputsref[0]].dtype:
print(Fore.RED + 'WARNING: Type of floating point number in test model ({}) does not match type in reference solution ({})\n'.format(filetest[path + outputstest[0]].dtype, fileref[path + outputsref[0]].dtype) + Style.RESET_ALL) print(Fore.RED + f'WARNING: Type of floating point number in test model ({filetest[path + outputstest[0]].dtype}) does not match type in reference solution ({fileref[path + outputsref[0]].dtype})\n' + Style.RESET_ALL)
float_or_doubleref = fileref[path + outputsref[0]].dtype float_or_doubleref = fileref[path + outputsref[0]].dtype
float_or_doubletest = filetest[path + outputstest[0]].dtype float_or_doubletest = filetest[path + outputstest[0]].dtype
@@ -191,15 +197,16 @@ for i, model in enumerate(testmodels):
ax.grid() ax.grid()
# Save a PDF/PNG of the figure # Save a PDF/PNG of the figure
savename = os.path.join(basepath, model + os.path.sep + model) filediffs = file.stem + '_diffs'
# fig1.savefig(savename + '.pdf', dpi=None, format='pdf', bbox_inches='tight', pad_inches=0.1) filediffs = file.parent / Path(filediffs)
# fig2.savefig(savename + '_diffs.pdf', dpi=None, format='pdf', bbox_inches='tight', pad_inches=0.1) # fig1.savefig(file.with_suffix('.pdf'), dpi=None, format='pdf', bbox_inches='tight', pad_inches=0.1)
fig1.savefig(savename + '.png', dpi=150, format='png', bbox_inches='tight', pad_inches=0.1) # fig2.savefig(savediffs.with_suffix('.pdf'), dpi=None, format='pdf', bbox_inches='tight', pad_inches=0.1)
fig2.savefig(savename + '_diffs.png', dpi=150, format='png', bbox_inches='tight', pad_inches=0.1) # fig1.savefig(file.with_suffix('.png'), dpi=150, format='png', bbox_inches='tight', pad_inches=0.1)
# fig2.savefig(filediffs.with_suffix('.png'), dpi=150, format='png', bbox_inches='tight', pad_inches=0.1)
# Summary of results # Summary of results
for name, data in sorted(testresults.items()): for name, data in sorted(testresults.items()):
if 'analytical' in name: if 'analytical' in name:
print(Fore.CYAN + "Test '{}.in' using v.{} compared to analytical solution. Max difference {:.2f}dB.".format(name, data['Test version'], data['Max diff']) + Style.RESET_ALL) print(Fore.CYAN + f"Test '{name}.in' using v.{data['Test version']} compared to analytical solution. Max difference {data['Max diff']:.2f}dB." + Style.RESET_ALL)
else: else:
print(Fore.CYAN + "Test '{}.in' using v.{} compared to reference solution using v.{}. Max difference {:.2f}dB.".format(name, data['Test version'], data['Ref version'], data['Max diff']) + Style.RESET_ALL) print(Fore.CYAN + f"Test '{name}.in' using v.{data['Test version']} compared to reference solution using v.{data['Ref version']}. Max difference {data['Max diff']:.2f}dB." + Style.RESET_ALL)