From dd439574e8bb325821fe9e344c9745c73354a03d Mon Sep 17 00:00:00 2001 From: Pinku-Neko Date: Wed, 13 Nov 2024 15:15:03 +0100 Subject: [PATCH 1/3] refactor, relocation of code --- diffusion2d.py | 81 -------------------------------------- diffusion2d/__init__.py | 0 diffusion2d/diffusion2d.py | 76 +++++++++++++++++++++++++++++++++++ diffusion2d/output.py | 15 +++++++ 4 files changed, 91 insertions(+), 81 deletions(-) delete mode 100644 diffusion2d.py create mode 100644 diffusion2d/__init__.py create mode 100644 diffusion2d/diffusion2d.py create mode 100644 diffusion2d/output.py diff --git a/diffusion2d.py b/diffusion2d.py deleted file mode 100644 index c0c6083a..00000000 --- a/diffusion2d.py +++ /dev/null @@ -1,81 +0,0 @@ -""" -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 - -# plate size, mm -w = h = 10. -# intervals in x-, y- directions, mm -dx = dy = 0.1 -# Thermal diffusivity of steel, mm^2/s -D = 4. - -# 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 - 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() diff --git a/diffusion2d/__init__.py b/diffusion2d/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/diffusion2d/diffusion2d.py b/diffusion2d/diffusion2d.py new file mode 100644 index 00000000..92ea0c81 --- /dev/null +++ b/diffusion2d/diffusion2d.py @@ -0,0 +1,76 @@ +""" +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 diffusion2d.output import create_plot, output_plots + +def solve(): + # plate size, mm + w = h = 10. + # intervals in x-, y- directions, mm + dx = dy = 0.1 + # Thermal diffusivity of steel, mm^2/s + D = 4. + + # 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(T_cold, T_hot, dt, u, fig_counter, fig, n) + + # Plot output figures + output_plots(fig, im) \ No newline at end of file diff --git a/diffusion2d/output.py b/diffusion2d/output.py new file mode 100644 index 00000000..786deab1 --- /dev/null +++ b/diffusion2d/output.py @@ -0,0 +1,15 @@ +import matplotlib.pyplot as plt + +def create_plot(T_cold, T_hot, dt, u, fig_counter, fig, n): + 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() From 0fd0f5e00b9372e6e1406bd60954dfd770615a3b Mon Sep 17 00:00:00 2001 From: Pinku-Neko Date: Wed, 13 Nov 2024 15:15:35 +0100 Subject: [PATCH 2/3] packaging --- pyproject.toml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 pyproject.toml diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..57f59ca6 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,27 @@ +[build-system] +requires = ["setuptools", "wheel"] + + +[tool.setuptools.packages] +find = {where = ["."], include = ["diffusion2d"]} + +[project] +name = "caihy_diffusion2d" +description = "diffusion FDM" +readme = "README.md" +keywords = ["toy", "simulation"] +authors = [ + {name = "Henry Cai"} +] +classifiers = [ + "Programming Language :: Python :: 3" +] +dependencies = [ + "numpy", + "matplotlib" +] +version = "0.0.2" +[project.urls] +Homepage = "https://github.com/Simulation-Software-Engineering/diffusion2D" +[project.scripts] +solver = "diffusion2d.diffusion2d:solve" \ No newline at end of file From 4f050ca6df3e150f6d36b23df74424b938aecf5d Mon Sep 17 00:00:00 2001 From: Pinku-Neko Date: Wed, 13 Nov 2024 15:15:46 +0100 Subject: [PATCH 3/3] modify readme --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index c18ce7d1..1dffd690 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,27 @@ The code used in this exercise is based on [Chapter 7 of the book "Learning Scie ## Project description +This package contains a Python implementation of the 2D diffusion equation. The diffusion equation is a partial +differential equation that describes how a quantity (e.g., heat, particles, etc.) diffuses +through a medium. + ## Installing the package +pip install dist\caihy_diffusion2d-0.0.2-py3-none-any.whl + ### Using pip3 to install from PyPI +pip install -i https://test.pypi.org/simple/ caihy-diffusion2d --extra-index-url https://pypi.org/simple + ### Required dependencies +numpy +matplotlib + ## Running this package +? + ## Citing + +Cat? \ No newline at end of file