Skip to content

Packaged code for PyPI by ahmedsa #11

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
dist
src/ahmedsa_diffusion2d.egg-info
31 changes: 24 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
# 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/).

## Project description
# Project description
A 2D diffusion equation solver with visualization output that used matplotlib and numpy to plot 3 graphs. The project can take arguements for values of dx, dy and D to compute and visualize the 2D diffusion and use matplotlib to plot the figures.

## Installing the package
You can use pip to directly install the package or build it from source using pip from the tar.gz/.whl package from [TestPyPi](link)

### Using pip3 to install from PyPI
To install the package, simply run:
```console
pip install -i https://test.pypi.org/simple/ ahmedsa-diffusion2d
```

### Required dependencies
The required dependencies are:
- Python (version >= 3)
- pip
- NumPy
- Matplotlib
If you plan to build from source, you also need to install the **build** pip package.

## Running this package
To run the package, you can use the following command:
```console
python3 diffusion2d.py
```
Or if you prefer to pass values of dx, dy and D (respectively), you can pass them as arguements as follows:
```console
python3 diffusion2d.py solve <dx> <dy> <D>
```
Comment on lines +24 to +31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instructions to run the packaged code are expected here. Without instructions to run the packaged code, it is hard to figure out how it works.


## Citing
- [Chapter 7 of the book "Learning Scientific Programming with Python"](https://scipython.com/book/chapter-7-matplotlib/examples/the-two-dimensional-diffusion-equation/)
- [Simulation Service Engineering Exercise](https://github.com/Simulation-Software-Engineering/diffusion2D)

20 changes: 20 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[build-system]
requires = ["setuptools", "wheel", "build"]

[project]
name = "ahmedsa_diffusion2d"
description = "A 2D diffusion equation solver with visualization output"
authors = [
{ name="Syed Mustafa Ahmed", email="[email protected]" }
]
readme = "README.md"
keywords = ["diffusion2D", "solver"]
classifiers = [
"Programming Language :: Python :: 3"
]
dependencies = [
"matplotlib",
"numpy"
]
requires-python = ">=3.0"
version = "0.0.2"
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from setuptools import setup

if __name__ == "__main__":
setup()
File renamed without changes.
Empty file added src/__init__.py
Empty file.
75 changes: 47 additions & 28 deletions diffusion2d.py → src/diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
Example acquired from https://scipython.com/book/chapter-7-matplotlib/examples/the-two-dimensional-diffusion-equation/
"""

import sys
import numpy as np
import matplotlib.pyplot as plt
from output import create_plot, output_plots

# plate size, mm
w = h = 10.
Expand Down Expand Up @@ -45,37 +47,54 @@


def do_timestep(u_nm1, u, D, dt, dx2, dy2):
# Propagate with forward-difference in time, central-difference in space
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)

u_nm1 = u.copy()
return u_nm1, u


# Number of timesteps
nsteps = 101
# Output 4 figures at these timesteps
n_output = [0, 10, 50, 100]
fig_counter = 0
fig = plt.figure()

# Time loop
for n in range(nsteps):
u0, u = do_timestep(u0, u, D, dt, dx2, dy2)

# Create figure
if n in n_output:
fig_counter += 1
ax = fig.add_subplot(220 + fig_counter)
im = ax.imshow(u.copy(), cmap=plt.get_cmap('hot'), vmin=T_cold, vmax=T_hot) # image for color bar axes
ax.set_axis_off()
ax.set_title('{:.1f} ms'.format(n * dt * 1000))

# Plot output figures
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()
# Solver function
def solve(dx=0.1, dy=0.1, D=4.0):
w = h = 10.0
T_cold = 300
T_hot = 700
nx, ny = int(w / dx), int(h / dy)
dx2, dy2 = dx * dx, dy * dy
dt = dx2 * dy2 / (2 * D * (dx2 + dy2))
u0 = T_cold * np.ones((nx, ny))
u = u0.copy()
r = min(h, w) / 4.0
cx = w / 2.0
cy = h / 2.0
r2 = r ** 2
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
n_output = [0, 10, 50, 100]
fig = plt.figure()
fig_counter = 0
im = None

for n in range(nsteps):
u0, u = do_timestep(u0, u, D, dt, dx2, dy2)
if n in n_output:
fig_counter += 1
im = create_plot(fig, u, T_cold, T_hot, dt, n, fig_counter)

output_plots(fig, im)

# Running the solve function as a test
if __name__ == "__main__":
if len(sys.argv) < 4:
print("Usage: python3 diffusion2d.py <dx> <dy> <D>")
else:
dx = float(sys.argv[2])
dy = float(sys.argv[3])
D = float(sys.argv[4])
print(f'dx={dx}, dy={dy}, D={D}')

solve(dx, dy, D)
15 changes: 15 additions & 0 deletions src/output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import matplotlib.pyplot as plt

def create_plot(fig, u, T_cold, T_hot, dt, time_step, fig_counter):
ax = fig.add_subplot(220 + fig_counter)
im = ax.imshow(u, cmap=plt.get_cmap('hot'), vmin=T_cold, vmax=T_hot)
ax.set_axis_off()
ax.set_title(f'{time_step * dt * 1000:.1f} ms')
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()