diff --git a/README.md b/README.md index da66993c..d3190f63 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,440 @@ Please follow the instructions in [python_testing_exercise.md](https://github.co ### pytest log +I am working on a Windows machine, so initially pytest was not able to detect the class correctly and giving the following output: + +```sh +PS C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425> pytest .\tests\unit\test_diffusion2d_functions.py +======================================================================== test session starts ======================================================================== +platform win32 -- Python 3.12.0, pytest-8.3.3, pluggy-1.5.0 +rootdir: C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425 +collected 0 items / 1 error + +============================================================================== ERRORS =============================================================================== +_____________________________________________________ ERROR collecting tests/unit/test_diffusion2d_functions.py _____________________________________________________ +ImportError while importing test module 'C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425\tests\unit\test_diffusion2d_functions.py'. +Hint: make sure your test modules/packages have valid Python names. +Traceback: +C:\Program Files\Python312\Lib\importlib\__init__.py:90: in import_module + return _bootstrap._gcd_import(name[level:], package, level) +tests\unit\test_diffusion2d_functions.py:5: in + from diffusion2d import SolveDiffusion2D +E ModuleNotFoundError: No module named 'diffusion2d' +====================================================================== short test summary info ====================================================================== +ERROR tests/unit/test_diffusion2d_functions.py +!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +========================================================================= 1 error in 0.16s ========================================================================== +``` + +**Solution**: Introduced `__init__.py` files in all directories and ran pytest as a module using: `python -m pytest` + + +#### Changing `self.nx = int(w / dx)` to `self.nx = int(h / dx)` in initialize_domain + +```sh +PS C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425> python -m pytest .\tests\unit\test_diffusion2d_functions.py +========================================================================= test session starts ========================================================================= +platform win32 -- Python 3.12.0, pytest-8.3.3, pluggy-1.5.0 +rootdir: C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425 +collected 3 items + +tests\unit\test_diffusion2d_functions.py F.. [100%] + +============================================================================== FAILURES =============================================================================== +_______________________________________________________________________ test_initialize_domain ________________________________________________________________________ + + def test_initialize_domain(): + """ + Check function SolveDiffusion2D.initialize_domain + """ + solver = SolveDiffusion2D() + solver.initialize_domain(w=20.,h=30.,dx=0.2,dy=0.2) + +> assert solver.nx == 100 +E assert 150 == 100 +E + where 150 = .nx + +tests\unit\test_diffusion2d_functions.py:15: AssertionError +========================================================================== warnings summary =========================================================================== +..\..\..\..\..\..\..\..\AppData\Roaming\Python\Python312\site-packages\dateutil\tz\tz.py:37 + C:\Users\Vedant\AppData\Roaming\Python\Python312\site-packages\dateutil\tz\tz.py:37: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC). + EPOCH = datetime.datetime.utcfromtimestamp(0) + +-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html +======================================================================= short test summary info ======================================================================= +FAILED tests/unit/test_diffusion2d_functions.py::test_initialize_domain - assert 150 == 100 +=============================================================== 1 failed, 2 passed, 1 warning in 0.94s ================================================================ +``` + + +#### Changing to `dx2, dy2 = self.dx * self.dy, self.dx * self.dy` in initialize_physical_parameters + +```sh +PS C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425> python -m pytest .\tests\unit\test_diffusion2d_functions.py +========================================================================= test session starts ========================================================================= +platform win32 -- Python 3.12.0, pytest-8.3.3, pluggy-1.5.0 +rootdir: C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425 +collected 3 items + +tests\unit\test_diffusion2d_functions.py .F. [100%] + +============================================================================== FAILURES =============================================================================== +_________________________________________________________________ test_initialize_physical_parameters _________________________________________________________________ + + def test_initialize_physical_parameters(): + """ + Checks function SolveDiffusion2D.initialize_domain + """ + solver = SolveDiffusion2D() + solver.dx = 0.2 + solver.dy = 0.4 + solver.initialize_physical_parameters(d=2.,T_cold=200.,T_hot=500.) +> assert round(solver.dt, 3) == 0.008 +E assert 0.01 == 0.008 +E + where 0.01 = round(0.010000000000000002, 3) +E + where 0.010000000000000002 = .dt + +tests\unit\test_diffusion2d_functions.py:27: AssertionError +------------------------------------------------------------------------ Captured stdout call ------------------------------------------------------------------------- +dt = 0.010000000000000002 +========================================================================== warnings summary =========================================================================== +..\..\..\..\..\..\..\..\AppData\Roaming\Python\Python312\site-packages\dateutil\tz\tz.py:37 + C:\Users\Vedant\AppData\Roaming\Python\Python312\site-packages\dateutil\tz\tz.py:37: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC). + EPOCH = datetime.datetime.utcfromtimestamp(0) + +-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html +======================================================================= short test summary info ======================================================================= +FAILED tests/unit/test_diffusion2d_functions.py::test_initialize_physical_parameters - assert 0.01 == 0.008 +=============================================================== 1 failed, 2 passed, 1 warning in 0.97s ================================================================ +``` + +#### Changing to `u = self.T_hot * np.ones((self.nx, self.ny))` in set_initial_condition + +```sh +PS C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425> python -m pytest .\tests\unit\test_diffusion2d_functions.py +========================================================================= test session starts ========================================================================= +platform win32 -- Python 3.12.0, pytest-8.3.3, pluggy-1.5.0 +rootdir: C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425 +collected 3 items + +tests\unit\test_diffusion2d_functions.py ..F [100%] + +============================================================================== FAILURES =============================================================================== +_____________________________________________________________________ test_set_initial_condition ______________________________________________________________________ + + def test_set_initial_condition(): + """ + Checks function SolveDiffusion2D.get_initial_function + """ + solver = SolveDiffusion2D() + solver.nx = 100 + solver.ny = 150 + solver.T_cold = 100. + solver.dx = 0.2 + solver.dy = 0.2 + solver.T_hot = 400. + u = solver.set_initial_condition() + + assert u.shape[0] == solver.nx # 100 + assert u.shape[1] == solver.ny # 150 + +> assert u[0, 0] == solver.T_cold +E assert 400.0 == 100.0 +E + where 100.0 = .T_cold + +tests\unit\test_diffusion2d_functions.py:46: AssertionError +========================================================================== warnings summary =========================================================================== +..\..\..\..\..\..\..\..\AppData\Roaming\Python\Python312\site-packages\dateutil\tz\tz.py:37 + C:\Users\Vedant\AppData\Roaming\Python\Python312\site-packages\dateutil\tz\tz.py:37: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC). + EPOCH = datetime.datetime.utcfromtimestamp(0) + +-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html +======================================================================= short test summary info ======================================================================= +FAILED tests/unit/test_diffusion2d_functions.py::test_set_initial_condition - assert 400.0 == 100.0 +=============================================================== 1 failed, 2 passed, 1 warning in 0.90s ================================================================ +``` + ### unittest log +#### Changing `self.nx = int(w / dx)` to `self.nx = int(h / dx)` in initialize_domain + +```sh +PS C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425> python -m unittest .\tests\unit\test_diffusion2d_functions.py +Fdt = 0.008000000000000002 +.. +====================================================================== +FAIL: test_initialize_domain (tests.unit.test_diffusion2d_functions.TestDiffusion2D.test_initialize_domain) +Check function SolveDiffusion2D.initialize_domain +---------------------------------------------------------------------- +Traceback (most recent call last): + File "C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425\tests\unit\test_diffusion2d_functions.py", line 21, in test_initialize_domain + self.assertEqual(self.solver.nx, 100) +AssertionError: 150 != 100 + +---------------------------------------------------------------------- +Ran 3 tests in 0.006s + +FAILED (failures=1) +``` + +#### Changing to `dx2, dy2 = self.dx * self.dy, self.dx * self.dy` in initialize_physical_parameters + +```sh +PS C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425> python -m unittest .\tests\unit\test_diffusion2d_functions.py +.dt = 0.010000000000000002 +F. +====================================================================== +FAIL: test_initialize_physical_parameters (tests.unit.test_diffusion2d_functions.TestDiffusion2D.test_initialize_physical_parameters) +Checks function SolveDiffusion2D.initialize_domain +---------------------------------------------------------------------- +Traceback (most recent call last): + File "C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425\tests\unit\test_diffusion2d_functions.py", line 34, in test_initialize_physical_parameters + self.assertAlmostEqual(self.solver.dt, 0.008) +AssertionError: 0.010000000000000002 != 0.008 within 7 places (0.0020000000000000018 difference) + +---------------------------------------------------------------------- +Ran 3 tests in 0.006s + +FAILED (failures=1) +``` + +#### Changing to `u = self.T_hot * np.ones((self.nx, self.ny))` in set_initial_condition + +```sh +PS C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425> python -m unittest .\tests\unit\test_diffusion2d_functions.py +.dt = 0.008000000000000002 +.F +====================================================================== +FAIL: test_set_initial_condition (tests.unit.test_diffusion2d_functions.TestDiffusion2D.test_set_initial_condition) +Checks function SolveDiffusion2D.get_initial_function +---------------------------------------------------------------------- +Traceback (most recent call last): + File "C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425\tests\unit\test_diffusion2d_functions.py", line 52, in test_set_initial_condition + self.assertEqual(u[0, 0], self.solver.T_cold) +AssertionError: 400.0 != 100.0 + +---------------------------------------------------------------------- +Ran 3 tests in 0.005s + +FAILED (failures=1) +``` + +### Integration test log + +#### Changing `self.nx = int(w / dx)` to `self.nx = int(h / dx)` in initialize_domain + +This only affects the second test as the first one is independent of the values of nx and ny. + +```sh +PS C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425> python -m pytest .\tests\integration\test_diffusion2d.py +========================================================================= test session starts ========================================================================= +platform win32 -- Python 3.12.0, pytest-8.3.3, pluggy-1.5.0 +rootdir: C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425 +collected 2 items + +tests\integration\test_diffusion2d.py .F [100%] + +============================================================================== FAILURES =============================================================================== +_____________________________________________________________________ test_set_initial_condition ______________________________________________________________________ + + def test_set_initial_condition(): + """ + Checks function SolveDiffusion2D.get_initial_function + """ + solver = SolveDiffusion2D() + w = 3. + h = 2.5 + dx = 1. + dy = 0.5 + d = 2. + T_cold = 200. + T_hot = 500. + solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot) + u = solver.set_initial_condition() + u_expected = np.array([ + [200., 200., 200., 200., 200.], + [200., 200., 200., 200., 200.], + [200., 200., 200., 200., 200.] + ]) +> np.testing.assert_array_equal(u, u_expected, "Expected array:\n{}\nGot:\n{}\n".format(u_expected, u)) + +tests\integration\test_diffusion2d.py:45: +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + +args = (, array([[200., 200., 200., 200., 200.], + [200., 200., 200., 200., 200.]]), array([[200., 200., 200., 200., 200.], + [200., 200., 200., 200., 200.], + [200., 200., 200., 200., 200.]])) +kwds = {'err_msg': 'Expected array:\n[[200. 200. 200. 200. 200.]\n [200. 200. 200. 200. 200.]\n [200. 200. 200. 200. 200.]]\n.... 200. 200. 200.]\n [200. 200. 200. 200. 200.]]\n', 'header': 'Arrays are not equal', 'strict': False, 'verbose': True} + + @wraps(func) + def inner(*args, **kwds): + with self._recreate_cm(): +> return func(*args, **kwds) +E AssertionError: +E Arrays are not equal +E Expected array: +E [[200. 200. 200. 200. 200.] +E [200. 200. 200. 200. 200.] +E [200. 200. 200. 200. 200.]] +E Got: +E [[200. 200. 200. 200. 200.] +E [200. 200. 200. 200. 200.]] +E +E (shapes (2, 5), (3, 5) mismatch) +E x: array([[200., 200., 200., 200., 200.], +E [200., 200., 200., 200., 200.]]) +E y: array([[200., 200., 200., 200., 200.], +E [200., 200., 200., 200., 200.], +E [200., 200., 200., 200., 200.]]) + +C:\Program Files\Python312\Lib\contextlib.py:81: AssertionError +------------------------------------------------------------------------ Captured stdout call ------------------------------------------------------------------------- +dt = 0.05 +========================================================================== warnings summary =========================================================================== +..\..\..\..\..\..\..\..\AppData\Roaming\Python\Python312\site-packages\dateutil\tz\tz.py:37 + C:\Users\Vedant\AppData\Roaming\Python\Python312\site-packages\dateutil\tz\tz.py:37: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC). + EPOCH = datetime.datetime.utcfromtimestamp(0) + +-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html +======================================================================= short test summary info ======================================================================= +FAILED tests/integration/test_diffusion2d.py::test_set_initial_condition - AssertionError: +=============================================================== 1 failed, 1 passed, 1 warning in 1.11s ================================================================ +``` + +#### Changing to `dx2, dy2 = self.dx * self.dy, self.dx * self.dy` in initialize_physical_parameters + +```sh +PS C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425> python -m pytest .\tests\integration\test_diffusion2d.py +========================================================================= test session starts ========================================================================= +platform win32 -- Python 3.12.0, pytest-8.3.3, pluggy-1.5.0 +rootdir: C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425 +collected 2 items + +tests\integration\test_diffusion2d.py F. [100%] + +============================================================================== FAILURES =============================================================================== +_________________________________________________________________ test_initialize_physical_parameters _________________________________________________________________ + + def test_initialize_physical_parameters(): + """ + Checks function SolveDiffusion2D.initialize_domain + """ + solver = SolveDiffusion2D() + w = 20. + h = 30. + dx = 0.2 + dy = 0.4 + d = 2. + T_cold = 200. + T_hot = 500. + solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot) +> assert round(solver.dt, 3) == 0.008 +E assert 0.01 == 0.008 +E + where 0.01 = round(0.010000000000000002, 3) +E + where 0.010000000000000002 = .dt + +tests\integration\test_diffusion2d.py:22: AssertionError +------------------------------------------------------------------------ Captured stdout call ------------------------------------------------------------------------- +dt = 0.010000000000000002 +========================================================================== warnings summary =========================================================================== +..\..\..\..\..\..\..\..\AppData\Roaming\Python\Python312\site-packages\dateutil\tz\tz.py:37 + C:\Users\Vedant\AppData\Roaming\Python\Python312\site-packages\dateutil\tz\tz.py:37: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC). + EPOCH = datetime.datetime.utcfromtimestamp(0) + +-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html +======================================================================= short test summary info ======================================================================= +FAILED tests/integration/test_diffusion2d.py::test_initialize_physical_parameters - assert 0.01 == 0.008 +=============================================================== 1 failed, 1 passed, 1 warning in 0.92s ================================================================ +``` + +#### Changing to `u = self.T_hot * np.ones((self.nx, self.ny))` in set_initial_condition + +```sh +PS C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425> python -m pytest .\tests\integration\test_diffusion2d.py +========================================================================= test session starts ========================================================================= +platform win32 -- Python 3.12.0, pytest-8.3.3, pluggy-1.5.0 +rootdir: C:\Users\Vedant\Documents\Uni\Subjects\Winter2425\SimTech\Exercises\Exercise7\testing-python-exercise-wt2425 +collected 2 items + +tests\integration\test_diffusion2d.py .F [100%] + +============================================================================== FAILURES =============================================================================== +_____________________________________________________________________ test_set_initial_condition ______________________________________________________________________ + + def test_set_initial_condition(): + """ + Checks function SolveDiffusion2D.get_initial_function + """ + solver = SolveDiffusion2D() + w = 3. + h = 2.5 + dx = 1. + dy = 0.5 + d = 2. + T_cold = 200. + T_hot = 500. + solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot) + u = solver.set_initial_condition() + u_expected = np.array([ + [200., 200., 200., 200., 200.], + [200., 200., 200., 200., 200.], + [200., 200., 200., 200., 200.] + ]) +> np.testing.assert_array_equal(u, u_expected, "Expected array:\n{}\nGot:\n{}\n".format(u_expected, u)) + +tests\integration\test_diffusion2d.py:45: +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + +args = (, array([[500., 500., 500., 500., 500.], + [500., 500., 500., 500., 500.], + [500., 5... array([[200., 200., 200., 200., 200.], + [200., 200., 200., 200., 200.], + [200., 200., 200., 200., 200.]])) +kwds = {'err_msg': 'Expected array:\n[[200. 200. 200. 200. 200.]\n [200. 200. 200. 200. 200.]\n [200. 200. 200. 200. 200.]]\n.... 500. 500. 500.]\n [500. 500. 500. 500. 500.]]\n', 'header': 'Arrays are not equal', 'strict': False, 'verbose': True} + + @wraps(func) + def inner(*args, **kwds): + with self._recreate_cm(): +> return func(*args, **kwds) +E AssertionError: +E Arrays are not equal +E Expected array: +E [[200. 200. 200. 200. 200.] +E [200. 200. 200. 200. 200.] +E [200. 200. 200. 200. 200.]] +E Got: +E [[500. 500. 500. 500. 500.] +E [500. 500. 500. 500. 500.] +E [500. 500. 500. 500. 500.]] +E +E Mismatched elements: 15 / 15 (100%) +E Max absolute difference: 300. +E Max relative difference: 1.5 +E x: array([[500., 500., 500., 500., 500.], +E [500., 500., 500., 500., 500.], +E [500., 500., 500., 500., 500.]]) +E y: array([[200., 200., 200., 200., 200.], +E [200., 200., 200., 200., 200.], +E [200., 200., 200., 200., 200.]]) + +C:\Program Files\Python312\Lib\contextlib.py:81: AssertionError +------------------------------------------------------------------------ Captured stdout call ------------------------------------------------------------------------- +dt = 0.05 +========================================================================== warnings summary =========================================================================== +..\..\..\..\..\..\..\..\AppData\Roaming\Python\Python312\site-packages\dateutil\tz\tz.py:37 + C:\Users\Vedant\AppData\Roaming\Python\Python312\site-packages\dateutil\tz\tz.py:37: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC). + EPOCH = datetime.datetime.utcfromtimestamp(0) + +-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html +======================================================================= short test summary info ======================================================================= +FAILED tests/integration/test_diffusion2d.py::test_set_initial_condition - AssertionError: +=============================================================== 1 failed, 1 passed, 1 warning in 0.97s ================================================================ +``` + ## Citing The code used in this exercise is based on [Chapter 7 of the book "Learning Scientific Programming with Python"](https://scipython.com/book/chapter-7-matplotlib/examples/the-two-dimensional-diffusion-equation/). diff --git a/__init__.py b/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/coverage-report.pdf b/coverage-report.pdf new file mode 100644 index 00000000..b7e6f2a5 Binary files /dev/null and b/coverage-report.pdf differ diff --git a/diffusion2d.py b/diffusion2d.py index 51a07f2d..64181edc 100644 --- a/diffusion2d.py +++ b/diffusion2d.py @@ -38,6 +38,10 @@ def __init__(self): self.dt = None def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1): + assert isinstance(w, float), "w must be float" + assert isinstance(h, float), "h must be float" + assert isinstance(dx, float), "dx must be float" + assert isinstance(dy, float), "dy must be float" self.w = w self.h = h self.dx = dx @@ -45,7 +49,10 @@ def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1): self.nx = int(w / dx) self.ny = int(h / dy) - def initialize_physical_parameters(self, d=4., T_cold=300, T_hot=700): + def initialize_physical_parameters(self, d=4., T_cold=300., T_hot=700.): + assert isinstance(d, float), "d must be float" + assert isinstance(T_cold, float), "T_cold must be float" + assert isinstance(T_hot, float), "T_hot must be float" self.D = d self.T_cold = T_cold self.T_hot = T_hot diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..070be7e6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +numpy>=1.26.1 +matplotlib>=3.8.4 \ No newline at end of file diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/integration/test_diffusion2d.py b/tests/integration/test_diffusion2d.py index fd026b40..bd583a92 100644 --- a/tests/integration/test_diffusion2d.py +++ b/tests/integration/test_diffusion2d.py @@ -3,13 +3,23 @@ """ from diffusion2d import SolveDiffusion2D - +import numpy as np def test_initialize_physical_parameters(): """ Checks function SolveDiffusion2D.initialize_domain """ solver = SolveDiffusion2D() + w = 20. + h = 30. + dx = 0.2 + dy = 0.4 + d = 2. + T_cold = 200. + T_hot = 500. + solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot) + assert round(solver.dt, 3) == 0.008 def test_set_initial_condition(): @@ -17,3 +27,19 @@ def test_set_initial_condition(): Checks function SolveDiffusion2D.get_initial_function """ solver = SolveDiffusion2D() + w = 3. + h = 2.5 + dx = 1. + dy = 0.5 + d = 2. + T_cold = 200. + T_hot = 500. + solver.initialize_domain(w=w, h=h, dx=dx, dy=dy) + solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot) + u = solver.set_initial_condition() + u_expected = np.array([ + [200., 200., 200., 200., 200.], + [200., 200., 200., 200., 200.], + [200., 200., 200., 200., 200.] + ]) + np.testing.assert_array_equal(u, u_expected, "Expected array:\n{}\nGot:\n{}\n".format(u_expected, u)) diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/test_diffusion2d_functions.py b/tests/unit/test_diffusion2d_functions.py index c4277ffd..744cfe7c 100644 --- a/tests/unit/test_diffusion2d_functions.py +++ b/tests/unit/test_diffusion2d_functions.py @@ -3,24 +3,59 @@ """ from diffusion2d import SolveDiffusion2D +import unittest +class TestDiffusion2D(unittest.TestCase): -def test_initialize_domain(): - """ - Check function SolveDiffusion2D.initialize_domain - """ - solver = SolveDiffusion2D() + def setUp(self): + self.solver = SolveDiffusion2D() -def test_initialize_physical_parameters(): - """ - Checks function SolveDiffusion2D.initialize_domain - """ - solver = SolveDiffusion2D() + def test_initialize_domain(self): + """ + Check function SolveDiffusion2D.initialize_domain + """ + self.solver.initialize_domain(w=20.,h=30.,dx=0.2,dy=0.4) + # Check that nx and nx are correctly computed based on w, dx and h, dy resp. + self.assertEqual(self.solver.nx, 100) + self.assertEqual(self.solver.ny, 75) -def test_set_initial_condition(): - """ - Checks function SolveDiffusion2D.get_initial_function - """ - solver = SolveDiffusion2D() + + def test_initialize_physical_parameters(self): + """ + Checks function SolveDiffusion2D.initialize_domain + """ + self.solver.dx = 0.2 + self.solver.dy = 0.4 + self.solver.initialize_physical_parameters(d=2.,T_cold=200.,T_hot=500.) + + # Check that value of dt is correctly computed based on input settings + self.assertAlmostEqual(self.solver.dt, 0.008) + + + def test_set_initial_condition(self): + """ + Checks function SolveDiffusion2D.get_initial_function + """ + self.solver.nx = 100 + self.solver.ny = 150 + self.solver.T_cold = 100. + self.solver.dx = 0.2 + self.solver.dy = 0.2 + self.solver.T_hot = 400. + u = self.solver.set_initial_condition() + + # Check shape is appropriate + self.assertEqual(u.shape, (self.solver.nx, self.solver.ny)) + + # Check that corners are cold + self.assertEqual(u[0, 0], self.solver.T_cold) + self.assertEqual(u[0, -1], self.solver.T_cold) + self.assertEqual(u[-1, 0], self.solver.T_cold) + self.assertEqual(u[-1, -1], self.solver.T_cold) + + # checking that values in the circle center are hot cannot be tested as it depends on the values + # of r, cx, cy. These values are not parameters of the class and hardcoding them here would bind + # the test to the settings of the actual function thus losing generality. This would have been + # testable if there was a choice to set these values by the user. \ No newline at end of file diff --git a/tox.toml b/tox.toml new file mode 100644 index 00000000..9b033fa0 --- /dev/null +++ b/tox.toml @@ -0,0 +1,12 @@ +requires = ["tox>=4"] +env_list = ["pytest", "unittest"] + +[env.pytest] +description = "pytest" +deps = ["pytest", "-r requirements.txt"] +commands = [["python", "-m", "pytest", "tests/integration/test_diffusion2d.py"]] + +[env.unittest] +description = "unittest" +deps = ["-r requirements.txt"] +commands = [["python", "-m", "unittest", "tests/unit/test_diffusion2d_functions.py"]] \ No newline at end of file diff --git a/tox_output.png b/tox_output.png new file mode 100644 index 00000000..d43d020a Binary files /dev/null and b/tox_output.png differ