Skip to content
Closed
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: 26 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
# diffusion2d
\# murugapy\_diffusion2d
Copy link
Member

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.


## 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

81 changes: 0 additions & 81 deletions diffusion2d.py

This file was deleted.

3 changes: 3 additions & 0 deletions murugapy_diffusion2d/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .diffusion2d import solve

__all__=["solve"]
67 changes: 67 additions & 0 deletions murugapy_diffusion2d/diffusion2d.py
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()
18 changes: 18 additions & 0 deletions murugapy_diffusion2d/output.py
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()
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>=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" }