Skip to content

Packaged code for PyPI by mullamm #14

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 1 commit 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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,40 @@ Please follow the instructions in [pypi_exercise.md](https://github.com/Simulati
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
This project solves the two-dimensional diffusion equation using finite difference methods. The diffusion equation is an important partial differential equation that describes the distribution of a quantity (such as temperature) over space and time. This package includes functions to simulate the diffusion process on a 2D grid and visualize the results.

## Installing the package

### Using pip3 to install from PyPI
To install the package directly from PyPI, you can use pip3:

```sh
pip install -i https://test.pypi.org/simple/ mullamm_diffusion2d==0.0.1
```

### Required dependencies

The following dependencies are required to run this package:
• numpy
• matplotlib

These dependencies will be installed automatically when you install the package using pip3.

## Running this package
## Running this package

To run the diffusion simulation, you can use the `solve` function provided in the package. Below is an example script that demonstrates how to use the package:

```python
from mullamm_diffusion2d import diffusion2d
diffusion2d.solve()
```

# Run the simulation with default parameters
solve()

# Run the simulation with custom parameters
solve(dx=0.05, dy=0.05, D=2.0)

## Citing
Mahek Mulla (2024). mullamm_diffusion2d: A Python package for solving the 2D diffusion equation. Available at: https://github.com/mahek-mulla/diffusion2D.git
Binary file added dist/mullamm_diffusion2d-0.0.2-py3-none-any.whl
Binary file not shown.
Binary file added dist/mullamm_diffusion2d-0.0.2.tar.gz
Binary file not shown.
61 changes: 61 additions & 0 deletions mullamm_diffusion2d.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Metadata-Version: 2.1
Name: mullamm_diffusion2d
Version: 0.0.2
Summary: A 2D diffusion solver example package
Home-page: https://github.com/mahek-mulla/diffusion2D.git
Author: Mahek Mulla
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: matplotlib

# 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
This project solves the two-dimensional diffusion equation using finite difference methods. The diffusion equation is an important partial differential equation that describes the distribution of a quantity (such as temperature) over space and time. This package includes functions to simulate the diffusion process on a 2D grid and visualize the results.

## Installing the package

### Using pip3 to install from PyPI
To install the package directly from PyPI, you can use pip3:

```sh
pip install -i https://test.pypi.org/simple/ mullamm_diffusion2d==0.0.1
```

### Required dependencies

The following dependencies are required to run this package:
• numpy
• matplotlib

These dependencies will be installed automatically when you install the package using pip3.

## Running this package
## Running this package

To run the diffusion simulation, you can use the `solve` function provided in the package. Below is an example script that demonstrates how to use the package:

```python
from mullamm_diffusion2d import diffusion2d
diffusion2d.solve()
```

# Run the simulation with default parameters
solve()

# Run the simulation with custom parameters
solve(dx=0.05, dy=0.05, D=2.0)

## Citing
Mahek Mulla (2024). mullamm_diffusion2d: A Python package for solving the 2D diffusion equation. Available at: https://github.com/mahek-mulla/diffusion2D.git
12 changes: 12 additions & 0 deletions mullamm_diffusion2d.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
LICENSE
README.md
setup.cfg
setup.py
mullamm_diffusion2d/__init__.py
mullamm_diffusion2d/diffusion2d.py
mullamm_diffusion2d/output.py
mullamm_diffusion2d.egg-info/PKG-INFO
mullamm_diffusion2d.egg-info/SOURCES.txt
mullamm_diffusion2d.egg-info/dependency_links.txt
mullamm_diffusion2d.egg-info/requires.txt
mullamm_diffusion2d.egg-info/top_level.txt
1 change: 1 addition & 0 deletions mullamm_diffusion2d.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 2 additions & 0 deletions mullamm_diffusion2d.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy
matplotlib
1 change: 1 addition & 0 deletions mullamm_diffusion2d.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mullamm_diffusion2d
Empty file added mullamm_diffusion2d/__init__.py
Empty file.
78 changes: 78 additions & 0 deletions mullamm_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)


# Plot output figures
output_plots(fig, im)





16 changes: 16 additions & 0 deletions mullamm_diffusion2d/output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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()
19 changes: 19 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[metadata]
name = mullamm_diffusion2d
version = 0.0.2
author = Mahek Mulla
description = A 2D diffusion solver example package
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/mahek-mulla/diffusion2D.git
classifiers =
Programming Language :: Python :: 3
License :: OSI Approved :: MIT License
Operating System :: OS Independent

[options]
packages = find:
python_requires = >=3.6
install_requires =
numpy
matplotlib
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# setup.py

from setuptools import setup

if __name__ == "__main__":
setup()