diff --git a/$HOME/.pypirc b/$HOME/.pypirc new file mode 100644 index 00000000..5492f970 --- /dev/null +++ b/$HOME/.pypirc @@ -0,0 +1,3 @@ +[testpypi] + username = __token__ + password = pypi-AgENdGVzdC5weXBpLm9yZwIkMGU4ZTM2YzgtMWU1My00ZjI5LWFhYjgtMjdlNjdjYmNkMzUxAAIqWzMsIjFkNDA2ODdmLTYyZGItNDhhOC1hYTJmLTk5MGJhYTY1NmFhNSJdAAAGIGuTgrGeCqS9KP_XKI-zH8aHHbnZ1e1pUL1ejEy_VqHS \ No newline at end of file diff --git a/Figure_1.png b/Figure_1.png new file mode 100644 index 00000000..42b9491d Binary files /dev/null and b/Figure_1.png differ diff --git a/__pycache__/output.cpython-313.pyc b/__pycache__/output.cpython-313.pyc new file mode 100644 index 00000000..9cd78ef0 Binary files /dev/null and b/__pycache__/output.cpython-313.pyc differ diff --git a/adr1ja_diffusion2d.egg-info/PKG-INFO b/adr1ja_diffusion2d.egg-info/PKG-INFO new file mode 100644 index 00000000..6d6b4ba3 --- /dev/null +++ b/adr1ja_diffusion2d.egg-info/PKG-INFO @@ -0,0 +1,33 @@ +Metadata-Version: 2.1 +Name: adr1ja_diffusion2d +Version: 0.0.2 +Summary: A simulation package for solving the 2D diffusion equation using finite difference methods +Author-email: Adrija +Keywords: diffusion,simulation,finite difference,scientific computing +Classifier: Programming Language :: Python :: 3 +Classifier: Operating System :: OS Independent +Requires-Python: >=3.10 +Description-Content-Type: text/markdown +License-File: LICENSE +Requires-Dist: numpy>=2.1.0 +Requires-Dist: matplotlib>=3.9.0 + +# 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 + +## Installing the package + +### Using pip3 to install from PyPI + +### Required dependencies + +## Running this package + +## Citing diff --git a/adr1ja_diffusion2d.egg-info/SOURCES.txt b/adr1ja_diffusion2d.egg-info/SOURCES.txt new file mode 100644 index 00000000..004b21e2 --- /dev/null +++ b/adr1ja_diffusion2d.egg-info/SOURCES.txt @@ -0,0 +1,9 @@ +LICENSE +README.md +pyproject.toml +setup.py +adr1ja_diffusion2d.egg-info/PKG-INFO +adr1ja_diffusion2d.egg-info/SOURCES.txt +adr1ja_diffusion2d.egg-info/dependency_links.txt +adr1ja_diffusion2d.egg-info/requires.txt +adr1ja_diffusion2d.egg-info/top_level.txt \ No newline at end of file diff --git a/adr1ja_diffusion2d.egg-info/dependency_links.txt b/adr1ja_diffusion2d.egg-info/dependency_links.txt new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/adr1ja_diffusion2d.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/adr1ja_diffusion2d.egg-info/requires.txt b/adr1ja_diffusion2d.egg-info/requires.txt new file mode 100644 index 00000000..a14b1a78 --- /dev/null +++ b/adr1ja_diffusion2d.egg-info/requires.txt @@ -0,0 +1,2 @@ +numpy>=2.1.0 +matplotlib>=3.9.0 diff --git a/adr1ja_diffusion2d.egg-info/top_level.txt b/adr1ja_diffusion2d.egg-info/top_level.txt new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/adr1ja_diffusion2d.egg-info/top_level.txt @@ -0,0 +1 @@ + diff --git a/adr1ja_diffusion2d/__init__.py b/adr1ja_diffusion2d/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/adr1ja_diffusion2d/__pycache__/__init__.cpython-313.pyc b/adr1ja_diffusion2d/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 00000000..f19c26e8 Binary files /dev/null and b/adr1ja_diffusion2d/__pycache__/__init__.cpython-313.pyc differ diff --git a/adr1ja_diffusion2d/__pycache__/diffusion2d.cpython-313.pyc b/adr1ja_diffusion2d/__pycache__/diffusion2d.cpython-313.pyc new file mode 100644 index 00000000..46f98a73 Binary files /dev/null and b/adr1ja_diffusion2d/__pycache__/diffusion2d.cpython-313.pyc differ diff --git a/adr1ja_diffusion2d/__pycache__/output.cpython-313.pyc b/adr1ja_diffusion2d/__pycache__/output.cpython-313.pyc new file mode 100644 index 00000000..4ebf0cf9 Binary files /dev/null and b/adr1ja_diffusion2d/__pycache__/output.cpython-313.pyc differ diff --git a/adr1ja_diffusion2d/diffusion2d.py b/adr1ja_diffusion2d/diffusion2d.py new file mode 100644 index 00000000..3b6ad44e --- /dev/null +++ b/adr1ja_diffusion2d/diffusion2d.py @@ -0,0 +1,57 @@ +""" +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 adr1ja_diffusion2d.output import create_plot, output_plots + +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 + +def solve(dx=0.1, dy=0.1, D=4.0): + # Parameters + w = h = 10.0 + T_cold = 300 + T_hot = 700 + nx, ny = int(w / dx), int(h / dy) + dx2, dy2 = dx * dx, dy * dy + dt = dx2 * dy2 / (2 * D * (dx2 + dy2)) + nsteps = 101 + n_output = [0, 10, 50, 100] + + # Initial conditions + u0 = T_cold * np.ones((nx, ny)) + u = u0.copy() + r = min(h, w) / 4.0 + cx, cy = w / 2.0, h / 2.0 + r2 = r ** 2 + for i in range(nx): + for j in range(ny): + if (i * dx - cx) ** 2 + (j * dy - cy) ** 2 < r2: + u0[i, j] = T_hot + + # Time loop and plotting + fig = plt.figure() + fig_counter = 0 + for n in range(nsteps): + u0, u = do_timestep(u0, u, D, dt, dx2, dy2) + if n in n_output: + fig_counter += 1 + im = create_plot(u, T_cold, T_hot, dt, n, fig, fig_counter) + + # Output all plots together + output_plots(im, fig) + +solve() + + + diff --git a/adr1ja_diffusion2d/output.py b/adr1ja_diffusion2d/output.py new file mode 100644 index 00000000..f502953f --- /dev/null +++ b/adr1ja_diffusion2d/output.py @@ -0,0 +1,17 @@ +import matplotlib.pyplot as plt + +def create_plot(u, T_cold, T_hot, dt, n, fig, fig_counter): + """Creates a single plot for a given time step.""" + 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(n * dt * 1000)) + return im # return the image for color bar usage later + +def output_plots(im, fig): + """Combines plots into a single figure and displays the color bar.""" + 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.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/dist/adr1ja_diffusion2d-0.0.2-py3-none-any.whl b/dist/adr1ja_diffusion2d-0.0.2-py3-none-any.whl new file mode 100644 index 00000000..8fe4b242 Binary files /dev/null and b/dist/adr1ja_diffusion2d-0.0.2-py3-none-any.whl differ diff --git a/dist/adr1ja_diffusion2d-0.0.2.tar.gz b/dist/adr1ja_diffusion2d-0.0.2.tar.gz new file mode 100644 index 00000000..f6eb0836 Binary files /dev/null and b/dist/adr1ja_diffusion2d-0.0.2.tar.gz differ diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..a1a9eed4 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,20 @@ +[build-system] +requires = ["setuptools", "wheel", "build"] +build-backend = "setuptools.build_meta" + +[project] +name = "adr1ja_diffusion2d" +version = "0.0.2" +description = "A simulation package for solving the 2D diffusion equation using finite difference methods" +readme = "README.md" +requires-python = ">=3.10" +authors = [{name = "Adrija", email = "adrijasingh13@gmail.com"}] +classifiers = [ + "Programming Language :: Python :: 3", + "Operating System :: OS Independent", +] +keywords = ["diffusion", "simulation", "finite difference", "scientific computing"] +dependencies = [ + "numpy>=2.1.0", + "matplotlib>=3.9.0" +] diff --git a/setup.config b/setup.config new file mode 100644 index 00000000..e69de29b diff --git a/setup.py b/setup.py new file mode 100644 index 00000000..34590680 --- /dev/null +++ b/setup.py @@ -0,0 +1,7 @@ +from setuptools import setup, find_packages + +setup( + name="adr1ja_diffusion2d", + version="0.0.2", + packages=find_packages(where="adr1ja_diffusion2d"), +)