From 6b076a7cb8349063fec84d5c58b02f723951b94e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20Bj=C3=B8rndal?= Date: Wed, 25 Jan 2017 11:59:41 +0100 Subject: [PATCH 1/3] bugfix, to_save_str = ' '.join(to_save) fails if to_save is Noen (which is the default!) --- gprMax/input_cmd_funcs.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gprMax/input_cmd_funcs.py b/gprMax/input_cmd_funcs.py index 387db89a..21f9aec8 100644 --- a/gprMax/input_cmd_funcs.py +++ b/gprMax/input_cmd_funcs.py @@ -621,7 +621,11 @@ def rx(x, y, z, identifier=None, to_save=None, polarisation=None, dxdy=None, rot x, y, xf, yf = rotate90_edge(x, y, xf, yf, polarisation, rotate90origin) c = Coordinate(x, y, z) - command('rx', str(c), identifier, ' '.join(to_save)) + to_save_str = '' + if to_save is not None: + to_save_str = ' '.join(to_save) + + command('rx', str(c), identifier, to_save_str) return c From c1bff26ce8af30e0c0f321ab28a75644cf324166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20Bj=C3=B8rndal?= Date: Wed, 25 Jan 2017 13:24:08 +0100 Subject: [PATCH 2/3] user-friendly exception message when using polarization in rx and not passing in dxdy --- gprMax/input_cmd_funcs.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/gprMax/input_cmd_funcs.py b/gprMax/input_cmd_funcs.py index 21f9aec8..c7419735 100644 --- a/gprMax/input_cmd_funcs.py +++ b/gprMax/input_cmd_funcs.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with gprMax. If not, see . +import sys from collections import namedtuple """This module contains functional forms of some of the most commonly used gprMax commands. It can be useful to use these within Python scripting in an input file. @@ -613,11 +614,18 @@ def rx(x, y, z, identifier=None, to_save=None, polarisation=None, dxdy=None, rot if rotate90origin: if polarisation == 'x': - xf = x + dxdy[0] + try: + xf = x + dxdy[0] + except Exception as e: + raise ValueError('With polarization = x, a dxdy[0] float values is required, got dxdy=%s' % dxdy) from e yf = y elif polarisation == 'y': xf = x - yf = y + dxdy[1] + try: + yf = y + dxdy[1] + except Exception as e: + raise ValueError('With polarization = y, a dxdy[1] float values is required, got dxdy=%s' % dxdy) from e + x, y, xf, yf = rotate90_edge(x, y, xf, yf, polarisation, rotate90origin) c = Coordinate(x, y, z) @@ -659,3 +667,4 @@ def rx_steps(dx=0, dy=0, dz=0): c = Coordinate(dx, dy, dz) command('rx_steps', str(c)) return c + From dfa547e19278752123db8d6efb7c6174a377fe75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20Bj=C3=B8rndal?= Date: Wed, 25 Jan 2017 13:25:00 +0100 Subject: [PATCH 3/3] added an initial basic unit-test for input_cmd_funcs. Feel free to move this file to the test/ directory --- gprMax/input_cmd_funcs_test.py | 79 ++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 gprMax/input_cmd_funcs_test.py diff --git a/gprMax/input_cmd_funcs_test.py b/gprMax/input_cmd_funcs_test.py new file mode 100644 index 00000000..8ff52abf --- /dev/null +++ b/gprMax/input_cmd_funcs_test.py @@ -0,0 +1,79 @@ +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 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_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()