-
Notifications
You must be signed in to change notification settings - Fork 22
[mullamm] Adding tests #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mahek-mulla
wants to merge
2
commits into
Simulation-Software-Engineering:main
Choose a base branch
from
mahek-mulla:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
numpy>=1.26.1 | ||
matplotlib>=3.8.4 |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
""" | ||
Tests for functions in class SolveDiffusion2D | ||
""" | ||
|
||
from diffusion2d import SolveDiffusion2D | ||
import unittest | ||
|
||
class TestDiffusion2D(unittest.TestCase): | ||
def setUp(self): | ||
"""Initialize the solver with default values for tests.""" | ||
self.solver = SolveDiffusion2D() | ||
|
||
# Set up the domain and physical parameters for the tests | ||
self.solver.w = 20. # Width of the domain | ||
self.solver.h = 30. # Height of the domain | ||
self.solver.dx = 0.2 # Grid step size in x direction | ||
self.solver.dy = 0.4 # Grid step size in y direction | ||
self.solver.T_cold = 100. # Initial cold temperature | ||
self.solver.T_hot = 400. # Initial hot temperature | ||
self.solver.initialize_domain(self.solver.w, self.solver.h, self.solver.dx, self.solver.dy) | ||
|
||
def test_initialize_domain(self): | ||
""" | ||
Check function SolveDiffusion2D.initialize_domain | ||
""" | ||
self.solver.initialize_domain(w=20., h=30., dx=0.2, dy=0.4) | ||
|
||
# nx and ny should be based on w/dx and h/dy | ||
self.assertEqual(self.solver.nx, 100, f"Expected nx=100, but got {self.solver.nx}") | ||
self.assertEqual(self.solver.ny, 75, f"Expected ny=75, but got {self.solver.ny}") | ||
|
||
|
||
|
||
|
||
def test_initialize_physical_parameters(self): | ||
""" | ||
Checks function SolveDiffusion2D.initialize_domain | ||
""" | ||
self.solver.initialize_physical_parameters(d=2., T_cold=200., T_hot=500.) | ||
|
||
# Check the calculated dt value (assert with some tolerance) | ||
expected_dt = 0.008 | ||
self.assertAlmostEqual(self.solver.dt, expected_dt, places=4, msg=f"Expected dt={expected_dt}, but got {self.solver.dt}") | ||
|
||
|
||
|
||
def test_set_initial_condition(self): | ||
""" | ||
Checks function SolveDiffusion2D.get_initial_function | ||
""" | ||
# Initialize physical parameters for this test | ||
self.solver.nx = 100 | ||
self.solver.ny = 150 | ||
self.solver.T_cold = 100. | ||
self.solver.T_hot = 400. | ||
u = self.solver.set_initial_condition() | ||
|
||
# Check the shape of the output matrix u (it should match nx, ny) | ||
self.assertEqual(u.shape, (self.solver.nx, self.solver.ny),f"Expected u.shape = ({self.solver.nx}, {self.solver.ny}), but got {u.shape}") | ||
|
||
# Check that corners are cold (boundary conditions) | ||
self.assertEqual(u[0, 0], self.solver.T_cold, "Top-left corner should be cold.") | ||
self.assertEqual(u[0, -1], self.solver.T_cold, "Top-right corner should be cold.") | ||
self.assertEqual(u[-1, 0], self.solver.T_cold, "Bottom-left corner should be cold.") | ||
self.assertEqual(u[-1, -1], self.solver.T_cold, "Bottom-right corner should be cold.") | ||
|
||
# Check if some of the center values are hot (inside the circle, should be equal to T_hot) | ||
# Based on the implementation, we know that the center will have T_hot values. | ||
r, cx, cy = 2, 5, 5 # Circle parameters used in set_initial_condition | ||
for i in range(self.solver.nx): | ||
for j in range(self.solver.ny): | ||
# Calculate distance from the center (cx, cy) | ||
dist = (i * self.solver.dx - cx) ** 2 + (j * self.solver.dy - cy) ** 2 | ||
if dist < r**2: | ||
# The points within the radius of the circle should have T_hot | ||
self.assertEqual(u[i, j], self.solver.T_hot,f"Expected T_hot={self.solver.T_hot} inside the circle, but got {u[i,j]} at ({i},{j})") | ||
else: | ||
# Points outside the circle should remain at T_cold | ||
self.assertEqual(u[i, j], self.solver.T_cold, f"Expected T_cold={self.solver.T_cold} outside the circle, but got {u[i,j]} at ({i},{j})") | ||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
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_unit.py"]] |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having this function call in
setUp
would mean it is called before every test. This means that the unit test gets a state of the code where a function call has been made, which makes it a unit test. The purpose of a unit test is to test a function in isolation.