diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8302173 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +Dockerfile +venv +_pycache_/ +dist/ +*.egg-info/ \ No newline at end of file diff --git a/README.md b/README.md index fe54835..7c65ffb 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,38 @@ -# 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/). +# diffusion2D ## Project description +This code simulates the diffusion equation in 2D over a square domain, where the entire domain starts at a certain temperature, except for a circular disc at the center with a higher temperature. The simulation uses the Finite Difference Method to solve the diffusion equation. Users can modify the thermal diffusivity and initial conditions of the system. The code generates four plots showing the diffusion process at different time points, allowing for a clear visualization of the temperature distribution over time. + ## Installing the package ### Using pip3 to install from PyPI +```bash +pip install -i https://test.pypi.org/simple/ gilvv-diffusion2d-1==0.0.1 +``` ### Required dependencies +The required dependencies are Numpy and Matplotlib which are automatically installed. +The python version required is also >= 3.6 + ## Running this package +Use the provided `solve()` function in python: + +```python +from gilvv_diffusion2d_1.diffusion2d import solve +solve(dx = 0.1, dy = 0.1, D = 4) +``` + +It contains three parameter, which can be adjusted: + +- `dx` intervals in x-direction +- `dy` intervals in y-direction +- `D` thermal diffusivity + ## Citing + +This is a student project from Software simulation Engineering at University of Stuttgart. +[https://github.com/Simulation-Software-Engineering/diffusion2D] \ No newline at end of file diff --git a/diffusion2d.py b/diffusion2d.py deleted file mode 100644 index c0c6083..0000000 --- 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/gilvv_diffusion2d_1/diffusion2d.py b/gilvv_diffusion2d_1/diffusion2d.py new file mode 100644 index 0000000..7c35dd1 --- /dev/null +++ b/gilvv_diffusion2d_1/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: + 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, fig_counter, T_cold, T_hot, u, 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/gilvv_diffusion2d_1/output.py b/gilvv_diffusion2d_1/output.py new file mode 100644 index 0000000..0662601 --- /dev/null +++ b/gilvv_diffusion2d_1/output.py @@ -0,0 +1,16 @@ +import matplotlib.pyplot as plt + +def create_plot(fig, fig_counter, T_cold, T_hot, u, n, dt): + #fig = plt.figure() + 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 0000000..c5bc7ea --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,25 @@ +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "gilvv_diffusion2d_1" +authors = [ + {name = "Vaibhav Gil", email = "st191590@stud.uni-stuttgart.de"}, +] +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. " +readme = "README.md" +requires-python = ">=3.6" +keywords = ["one", "two"] +classifiers = [ + "Programming Language :: Python :: 3", +] +dependencies = [ + "numpy", + 'importlib-metadata; python_version>"3.6"', + "matplotlib", +] +version = "0.0.1" + +[project.urls] +repository = "https://github.com/MarcelWolkober/diffusion2D" \ No newline at end of file diff --git a/test-script.py b/test-script.py new file mode 100644 index 0000000..05ec8a2 --- /dev/null +++ b/test-script.py @@ -0,0 +1,4 @@ + +import gilvv_diffusion2d_1.diffusion2d + +gilvv_diffusion2d_1.diffusion2d.solve() \ No newline at end of file