diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..692350ec --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +venv +__pycache__/ +dist/ +*.egg-info/ \ No newline at end of file diff --git a/README.md b/README.md index fe548354..41526a03 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,49 @@ # 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 +## Project Description +This code solves the diffusion equation in 2D over a square domain which is at a certain temperature and a circular disc at the center which is at a higher temperature. This code solves the diffusion equation using the Finite Difference Method. The thermal diffusivity and initial conditions of the system can be changed by the user. The code produces four plots at various timepoints of the simulation. The diffusion process can be clearly observed in these plots. ## Installing the package +To install the `diffusion2D` package from TestPyPI and PyPI, you can use the following `pip` commands: +### Using pip3 to install from TestPyPI +```bash +pip3 install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple _diffusion2d +``` ### Using pip3 to install from PyPI +```bash +pip3 install _diffusion2d +``` ### Required dependencies +The following dependencies are required to run the code: +- Python version >= 3.6 and update it if it is older than 3.6. +- Install pip, build, and Twine. +- Install NumPy and Matplotlib with pip.. + +You can install them using: +```bash +pip install numpy matplotlib twine +``` +Alternatively, if you are installing the package directly, the dependencies will be installed automatically. ## Running this package +Once the package is installed, you can use the solve() function to simulate the 2D diffusion process. We can run the code either interactively in a Python shell or in a Python script. +If you choose Python Shell and run the following command. +```python +>>> from niyati_diffusion2d import diffusion2d +>>> diffusion2d.solve() +``` + +you can also build using the following command: + +You can install them using: +```python +>>> python3 -m build +``` + ## Citing +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). + +If you are interested in the theoretical background of the code, please have a look in Chapter 7 of the book ["Learning Scientific Programming with Python"](https://scipython.com/book/chapter-7-matplotlib/examples/the-two-dimensional-diffusion-equation/) \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/__pycache__/diffusion2d.cpython-312.pyc b/__pycache__/diffusion2d.cpython-312.pyc new file mode 100644 index 00000000..3eee3a59 Binary files /dev/null and b/__pycache__/diffusion2d.cpython-312.pyc differ diff --git a/__pycache__/output.cpython-312.pyc b/__pycache__/output.cpython-312.pyc new file mode 100644 index 00000000..c959cc90 Binary files /dev/null and b/__pycache__/output.cpython-312.pyc differ 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/niyati_diffusion2d-0.1-py3-none-any.whl b/dist/niyati_diffusion2d-0.1-py3-none-any.whl new file mode 100644 index 00000000..33fe9ab3 Binary files /dev/null and b/dist/niyati_diffusion2d-0.1-py3-none-any.whl differ diff --git a/dist/niyati_diffusion2d-0.1.tar.gz b/dist/niyati_diffusion2d-0.1.tar.gz new file mode 100644 index 00000000..f88ec056 Binary files /dev/null and b/dist/niyati_diffusion2d-0.1.tar.gz differ diff --git a/niyati_diffusion2d.egg-info/PKG-INFO b/niyati_diffusion2d.egg-info/PKG-INFO new file mode 100644 index 00000000..175f9183 --- /dev/null +++ b/niyati_diffusion2d.egg-info/PKG-INFO @@ -0,0 +1,34 @@ +Metadata-Version: 2.1 +Name: niyati_diffusion2d +Version: 0.1 +Summary: diffusion2D +Author-email: Niyati +License: BSD-3-Clause +Keywords: one,two +Classifier: Programming Language :: Python :: 3 +Requires-Python: >=3.6 +Description-Content-Type: text/markdown +License-File: LICENSE +Requires-Dist: importlib-metadata; python_version < "3.4" +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 + +## Installing the package + +### Using pip3 to install from PyPI + +### Required dependencies + +## Running this package + +## Citing diff --git a/niyati_diffusion2d.egg-info/SOURCES.txt b/niyati_diffusion2d.egg-info/SOURCES.txt new file mode 100644 index 00000000..c4660550 --- /dev/null +++ b/niyati_diffusion2d.egg-info/SOURCES.txt @@ -0,0 +1,9 @@ +LICENSE +README.md +pyproject.toml +niyati_diffusion2d/diffusion2d.py +niyati_diffusion2d.egg-info/PKG-INFO +niyati_diffusion2d.egg-info/SOURCES.txt +niyati_diffusion2d.egg-info/dependency_links.txt +niyati_diffusion2d.egg-info/requires.txt +niyati_diffusion2d.egg-info/top_level.txt \ No newline at end of file diff --git a/niyati_diffusion2d.egg-info/dependency_links.txt b/niyati_diffusion2d.egg-info/dependency_links.txt new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/niyati_diffusion2d.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/niyati_diffusion2d.egg-info/requires.txt b/niyati_diffusion2d.egg-info/requires.txt new file mode 100644 index 00000000..28375154 --- /dev/null +++ b/niyati_diffusion2d.egg-info/requires.txt @@ -0,0 +1,5 @@ +numpy +matplotlib + +[:python_version < "3.4"] +importlib-metadata diff --git a/niyati_diffusion2d.egg-info/top_level.txt b/niyati_diffusion2d.egg-info/top_level.txt new file mode 100644 index 00000000..2cb9d9ce --- /dev/null +++ b/niyati_diffusion2d.egg-info/top_level.txt @@ -0,0 +1 @@ +niyati_diffusion2d diff --git a/niyati_diffusion2d/__pycache__/diffusion2d.cpython-312.pyc b/niyati_diffusion2d/__pycache__/diffusion2d.cpython-312.pyc new file mode 100644 index 00000000..c002a0e1 Binary files /dev/null and b/niyati_diffusion2d/__pycache__/diffusion2d.cpython-312.pyc differ diff --git a/niyati_diffusion2d/diffusion2d.py b/niyati_diffusion2d/diffusion2d.py new file mode 100644 index 00000000..82d86233 --- /dev/null +++ b/niyati_diffusion2d/diffusion2d.py @@ -0,0 +1,86 @@ +""" +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.): + # 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: + #creating 1 plot taking parameter fig, fig_counter, T_cold, T_hot, u, n dt + 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)) + ax,im = create_plot(fig_counter, fig, u, T_cold, T_hot, n,dt) + + # 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() + output_plots(fig,im) diff --git a/output.py b/output.py new file mode 100644 index 00000000..94900653 --- /dev/null +++ b/output.py @@ -0,0 +1,17 @@ +import matplotlib.pyplot as plt + +def create_plot(fig_counter, fig, u,T_cold,T_hot, 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 ax,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() + \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..3f3f9bd5 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,24 @@ +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "niyati_diffusion2d" +authors = [ + {name = "Niyati", email = "st191812@stud.uni-stuttgart.de"}, +] +description = "diffusion2D" +readme = "README.md" +requires-python = ">=3.6" +keywords = ["one", "two"] +license = {text = "BSD-3-Clause"} +classifiers = [ + "Programming Language :: Python :: 3", +] +dependencies = [ + 'importlib-metadata; python_version<"3.4"', + 'numpy', + 'matplotlib', +] +version="0.1" + diff --git a/test-script.py b/test-script.py new file mode 100644 index 00000000..a1a62a6d --- /dev/null +++ b/test-script.py @@ -0,0 +1,4 @@ +import niyati_diffusion2d.diffusion2d + + +niyati_diffusion2d.diffusion2d.solve() \ No newline at end of file