Skip to content

Packaged code for PyPI by vangasa #19

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 2 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
venv
__pycache__/
dist/
*.egg-info/
40 changes: 33 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
# 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
This code solves the diffusion equation in 2D over a square domain which is at a certain temperature and a circular disc at the center which is at a higher temperature. This code solves the diffusion equation using the Finite Difference Method. The thermal diffusivity and initial conditions of the system can be changed by the user. The code produces four plots at various timepoints of the simulation. The diffusion process can be clearly observed in these plots.

## Installing the package
To install the `diffusion2D` package from TestPyPI and PyPI, you can use the following `pip` commands:

### Using pip3 to install from TestPyPI
```bash
pip3 install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple <username>_diffusion2d
```
### Using pip3 to install from PyPI
```bash
pip3 install <username>_diffusion2d
```

### Required dependencies
The following dependencies are required to run the code:
- python >= 3.6
- numpy: For numerical computations.
- matplotlib: For plotting the diffusion process.
You can install them using:
```bash
pip install numpy matplotlib
```
Alternatively, if you are installing the package directly, the dependencies will be installed automatically.

## Running this package
Once the package is installed, you can use the solve() function to simulate the 2D diffusion process. You can run the code either interactively in a Python shell or in a Python script. Simple way is by using Python Shell. Fo that, open Python Shell and run the following command.
```python
>>> from vangasa_diffusion2d import diffusion2d
Copy link
Member

Choose a reason for hiding this comment

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

On running this command I got:

>>> from vangasa_diffusion2d import diffusion2d
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/dist-packages/vangasa_diffusion2d/diffusion2d.py", line 9, in <module>
    from output import create_plot, output_plots
ModuleNotFoundError: No module named 'output'

perhaps a relative import of output into diffusion2d.py works.

>>> diffusion2d.solve()
```

Or, to customize the parameters (dx, dy, and D), you can pass them like this:
```python
>>> from vangasa_diffusion2d import diffusion2d
>>> diffusion2d.solve(dx=0.05, dy=0.05, D=2.0)
```

## Citing
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).

If you are interested in the theoretical background of the code, please have a look in Chapter 7 of the book ["Learning Scientific Programming with Python"](https://scipython.com/book/chapter-7-matplotlib/examples/the-two-dimensional-diffusion-equation/)
81 changes: 0 additions & 81 deletions diffusion2d.py

This file was deleted.

Binary file added diffusion2d_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[build-system]
requires = ["setuptools","wheel","build"]

[project]
name = "vangasa_diffusion2d"
description = "A Python package for 2D heat diffusion simulation"
version = "0.0.2"
authors = [
{name="Saranya Vanga", email="vangasaranya1289@gmail.com"}
]
readme = "README.md"
requires-python = ">=3.6"
keywords = ["solver" ,"diffusion2D"]
classifiers = [
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
]
dependencies = [
"numpy",
"matplotlib",
]
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()
Empty file added vangasa_diffusion2d/__init__.py
Empty file.
78 changes: 78 additions & 0 deletions vangasa_diffusion2d/diffusion2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
Solving the two-dimensional diffusion equation

Example acquired from https://scipython.com/book/chapter-7-matplotlib/examples/the-two-dimensional-diffusion-equation/
"""

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

def solve(dx=0.1, dy=0.1, D=4.0):
# plate size, mm
w = h = 10

# Initial cold temperature of square domain
T_cold = 300

# Initial hot temperature of circular disc at the center
T_hot = 700

# Number of discrete mesh points in X and Y directions
nx, ny = int(w / dx), int(h / dy)

# Computing a stable time step
dx2, dy2 = dx * dx, dy * dy
dt = dx2 * dy2 / (2 * D * (dx2 + dy2))

print("dt = {}".format(dt))

u0 = T_cold * np.ones((nx, ny))
u = u0.copy()

# Initial conditions - circle of radius r centred at (cx,cy) (mm)
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


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
im = create_plot(fig,fig_counter,T_cold,T_hot,u,n,dt)

# Finalize and show plots
output_plots(fig, im)






17 changes: 17 additions & 0 deletions vangasa_diffusion2d/output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import matplotlib.pyplot as plt

def create_plot(fig, fig_counter, T_cold, T_hot,u,n,dt):
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))
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()