-
Notifications
You must be signed in to change notification settings - Fork 34
Packaged code by murugapy #23
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
Closed
prasithbasky
wants to merge
2
commits into
Simulation-Software-Engineering:main
from
prasithbasky:main
Closed
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
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 |
|---|---|---|
| @@ -1,16 +1,34 @@ | ||
| # diffusion2d | ||
| \# murugapy\_diffusion2d | ||
|
|
||
| ## Instructions for students | ||
|
|
||
| Please follow the instructions in [pypi_exercise.md](https://github.com/Simulation-Software-Engineering/Lecture-Material/blob/main/03_building_and_packaging/pypi_exercise.md). | ||
|
|
||
| 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/). | ||
| 2D diffusion equation solver and snapshot plotter. Refactored from the SciPython example. | ||
|
|
||
| ## Description | ||
|
|
||
| ## Installing the package | ||
|
|
||
| ## Running this package | ||
| \## Overview | ||
|
|
||
| This package solves the two-dimensional diffusion equation on a square plate with a hot circular disc in the center. It uses finite difference discretization and plots snapshots of the temperature field at selected times. | ||
|
|
||
|
|
||
|
|
||
| \## Usage | ||
|
|
||
| ```python | ||
|
|
||
| from murugapy\_diffusion2d import solve | ||
|
|
||
|
|
||
|
|
||
| \# Use defaults | ||
|
|
||
| solve() | ||
|
|
||
|
|
||
|
|
||
| \# Custom parameters | ||
|
|
||
| solve(dx=0.05, dy=0.05, D=4.0) | ||
|
|
||
|
|
||
| ## Citing | ||
|
|
||
This file was deleted.
Oops, something went wrong.
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,3 @@ | ||
| from .diffusion2d import solve | ||
|
|
||
| __all__=["solve"] |
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,67 @@ | ||
| import numpy as np | ||
| import matplotlib.pyplot as plt | ||
|
|
||
| from .output import create_plot, output_plots | ||
|
|
||
|
|
||
| def do_timestep(u_nm1, u, D, dt, dx2, dy2): | ||
| u[1:-1, 1:-1] = ( | ||
| u_nm1[1:-1, 1:-1] + | ||
| D * dt * ( | ||
| (u_nm1[2:, 1:-1] - 2*u_nm1[1:-1, 1:-1] + u_nm1[:-2, 1:-1]) / dx2 + | ||
| (u_nm1[1:-1, 2:] - 2*u_nm1[1:-1, 1:-1] + u_nm1[1:-1, :-2]) / dy2 | ||
| ) | ||
| ) | ||
| return u.copy(), u | ||
|
|
||
|
|
||
| def solve(dx=0.1, dy=0.1, D=4.0): | ||
| # Plate size | ||
| w = h = 10.0 | ||
|
|
||
| # Temperatures | ||
| T_cold = 300 | ||
| T_hot = 700 | ||
|
|
||
| # Mesh points | ||
| nx, ny = int(w / dx), int(h / dy) | ||
|
|
||
| # Time step (stable) | ||
| dx2, dy2 = dx*dx, dy*dy | ||
| dt = dx2 * dy2 / (2 * D * (dx2 + dy2)) | ||
| print("dt =", dt) | ||
|
|
||
| # Initial fields | ||
| u0 = T_cold * np.ones((nx, ny)) | ||
| u = u0.copy() | ||
|
|
||
| # Hot circular region | ||
| r = min(h, w) / 4 | ||
| cx, cy = w/2, h/2 | ||
| r2 = r*r | ||
|
|
||
| for i in range(nx): | ||
| for j in range(ny): | ||
| p2 = (i*dx - cx)**2 + (j*dy - cy)**2 | ||
| if p2 < r2: | ||
| u0[i, j] = T_hot | ||
|
|
||
| nsteps = 101 | ||
| output_steps = [0, 10, 50, 100] | ||
|
|
||
| fig = plt.figure() | ||
| fig_counter = 0 | ||
| last_im = None # for colorbar reference | ||
|
|
||
| for n in range(nsteps): | ||
| u0, u = do_timestep(u0, u, D, dt, dx2, dy2) | ||
|
|
||
| if n in output_steps: | ||
| fig_counter += 1 | ||
| last_im = create_plot(fig, u, n, dt, T_cold, T_hot, fig_counter) | ||
|
|
||
| output_plots(fig, last_im) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| solve() |
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,18 @@ | ||
| import matplotlib.pyplot as plt | ||
|
|
||
| def create_plot(fig, u, timestep, dt, T_cold, T_hot, fig_counter): | ||
| ax = fig.add_subplot(220 + fig_counter) | ||
| im = ax.imshow(u.copy(), | ||
| cmap=plt.get_cmap('hot'), | ||
| vmin=T_cold, vmax=T_hot) | ||
| ax.set_axis_off() | ||
| ax.set_title('{:.1f} ms'.format(timestep * dt * 1000)) | ||
| return im | ||
|
|
||
|
|
||
| def output_plots(fig, im): | ||
| fig.subplots_adjust(right=0.85) | ||
| cbar_ax = fig.add_axes([0.9, 0.15, 0.03, 0.7]) | ||
| cbar_ax.set_xlabel('$T$ / K', labelpad=20) | ||
| fig.colorbar(im, cax=cbar_ax) | ||
| plt.show() |
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,20 @@ | ||
| [build-system] | ||
| requires = ["setuptools>=61.0", "wheel"] | ||
| build-backend = 'setuptools.build_meta' | ||
|
|
||
| [project] | ||
| name = "murugapy_diffusion2d" | ||
| version = "0.0.1" | ||
| description = "2D diffusion PDE solver and plotting (from scipython example)" | ||
| readme = "README.md" | ||
| requires-python = ">=3.6" | ||
| authors = [ | ||
| { name = "Prasith Basky M", email = "[email protected]" } | ||
| ] | ||
| license = { file = "LICENSE" } | ||
|
|
||
| dependencies = [ | ||
| "numpy", | ||
| "matplotlib" | ||
| ] | ||
| urls = { "Repository" = "https://github.com/prasithbasky/diffusion2d" } |
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.
This header syntax is problematic. Only # is used. Also the same below.