Skip to content

Packaged code for PyPI by mamoyn #24

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 9 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
34 changes: 28 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
# diffusion2D

## Instructions for students
## Project description

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).
This package provides a solution for the 2D diffusion equation over a square domain using the Finite Difference Method. The domain starts at a uniform temperature, with a circular disc at the center at a higher temperature. Users can modify thermal diffusivity and initial conditions to simulate various scenarios. The output includes four plots showing the diffusion process at different timepoints.

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/).
## Installing the package

## Project description
To install the package, use:

## Installing the package
```bash
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple mamoyn_diffusion2d

### Using pip3 to install from PyPI
```

### Required dependencies

- Python (>=3.6)
- numpy
- matplotlib

## Running this package

To run the simulation with default parameters, execute:

```bash
solve
```

For custom parameters, import and call the `solve` function:

```python
from mamoyn_diffusion2d import diffusion2d
diffusion2d.solve(dx=0.05, dy=0.05, D=2.0)
```

## Citing

If using this package, please cite:

> Yonatan Mamo. *mamoyn_diffusion2d: A Python Package for Solving 2D Diffusion Equations.* Version 0.0.1, 2024. Available at [https://test.pypi.org/project/mamoyn_diffusion2d/](https://test.pypi.org/project/mamoyn_diffusion2d/).
81 changes: 0 additions & 81 deletions diffusion2d.py

This file was deleted.

Empty file added mamoyn_diffusion2d/__init__.py
Empty file.
69 changes: 69 additions & 0 deletions mamoyn_diffusion2d/diffusion2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
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 # Import pyplot AFTER setting the backend
from .output import create_plot, output_plots

# solve the 2D diffusion equation
def solve(dx=0.1, dy=0.1, D=4.0):
# plate size, mm
w = h = 10.
# Thermal diffusivity of steel, mm^2/s
T_cold = 300 # Initial cold temperature of square domain
T_hot = 700 # Initial hot temperature of circular disc at the center

# 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

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

# Create figure
if n in n_output:
fig_counter += 1
im = create_plot(fig, u.copy(), n, dt, T_cold, T_hot, fig_counter)

# Plot output figures
output_plots(fig, im, T_cold, T_hot)

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

# function to create a plot for a single timestamp
def create_plot(fig, data, n, dt, T_cold, T_hot, fig_counter):
ax = fig.add_subplot(220 + fig_counter)
im = ax.imshow(data, cmap=plt.get_cmap('hot'), vmin=T_cold, vmax=T_hot)
ax.set_axis_off()
ax.set_title('{:.1f} ms'.format(n * dt * 1000))
return im

# function to output all plots as one figure
def output_plots(fig, im, T_cold, T_hot):
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()
28 changes: 28 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[metadata]
name = mamoyn_diffusion2d
version = 0.0.2
author = Yonatan Mamo
author_email = girmayonatan86@gmail.com
description = A Python package to solve the 2D diffusion equation, designed for Simulation Software Engineering.
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/Simulation-Software-Engineering/diffusion2D
classifiers =
Programming Language :: Python :: 3
License :: OSI Approved :: MIT License
Operating System :: OS Independent
Topic :: Scientific/Engineering
Intended Audience :: Education
Intended Audience :: Developers
Environment :: Console

[options]
packages = find:
python_requires = >=3.6
install_requires =
numpy
matplotlib

[options.entry_points]
console_scripts =
solve = mamoyn_diffusion2d.diffusion2d:solve
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()