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_packages/__init__.py b/diffusion2d_packages/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/diffusion2d_packages/__pycache__/__init__.cpython-312.pyc b/diffusion2d_packages/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 00000000..50c2b9b1 Binary files /dev/null and b/diffusion2d_packages/__pycache__/__init__.cpython-312.pyc differ diff --git a/diffusion2d_packages/__pycache__/diffusion2d.cpython-312.pyc b/diffusion2d_packages/__pycache__/diffusion2d.cpython-312.pyc new file mode 100644 index 00000000..164f2b25 Binary files /dev/null and b/diffusion2d_packages/__pycache__/diffusion2d.cpython-312.pyc differ diff --git a/diffusion2d_packages/__pycache__/output.cpython-312.pyc b/diffusion2d_packages/__pycache__/output.cpython-312.pyc new file mode 100644 index 00000000..d94eb5a2 Binary files /dev/null and b/diffusion2d_packages/__pycache__/output.cpython-312.pyc differ diff --git a/diffusion2d_packages/diffusion2d.py b/diffusion2d_packages/diffusion2d.py new file mode 100644 index 00000000..80642c8e --- /dev/null +++ b/diffusion2d_packages/diffusion2d.py @@ -0,0 +1,79 @@ +""" +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 + + # Thermal diffusivity of steel, mm^2/s + + + # 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(u, n * dt, T_cold, T_hot, fig_counter, fig) + + # Plot output figures + output_plots(fig, im) + +if __name__ == "__main__": + solve() diff --git a/diffusion2d_packages/output.py b/diffusion2d_packages/output.py new file mode 100644 index 00000000..892a2d1c --- /dev/null +++ b/diffusion2d_packages/output.py @@ -0,0 +1,17 @@ +from matplotlib import pyplot as plt + +def create_plot(u, timestep, T_cold, T_hot, fig_counter, fig): + + 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(f'{timestep:.1f} ms') + return im # Returning im to be used for the colorbar + +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() diff --git a/dist/nagannhh_packages-0.0.1-py3-none-any.whl b/dist/nagannhh_packages-0.0.1-py3-none-any.whl new file mode 100644 index 00000000..10201c80 Binary files /dev/null and b/dist/nagannhh_packages-0.0.1-py3-none-any.whl differ diff --git a/dist/nagannhh_packages-0.0.1.tar.gz b/dist/nagannhh_packages-0.0.1.tar.gz new file mode 100644 index 00000000..c5e7335c Binary files /dev/null and b/dist/nagannhh_packages-0.0.1.tar.gz differ diff --git a/nagannhh_packages.egg-info/PKG-INFO b/nagannhh_packages.egg-info/PKG-INFO new file mode 100644 index 00000000..89259c2b --- /dev/null +++ b/nagannhh_packages.egg-info/PKG-INFO @@ -0,0 +1,10 @@ +Metadata-Version: 2.1 +Name: nagannhh_packages +Version: 0.0.1 +Summary: A package to solve 2D diffusion equation test exercise +Home-page: https://github.com/harshithn31/diffusion2D +Author: nagannhh +License-File: LICENSE +Requires-Dist: numpy>=2.1.3 +Requires-Dist: matplotlib>=3.9.2 +Requires-Dist: setuptools>=75.5.0 diff --git a/nagannhh_packages.egg-info/SOURCES.txt b/nagannhh_packages.egg-info/SOURCES.txt new file mode 100644 index 00000000..72f7e463 --- /dev/null +++ b/nagannhh_packages.egg-info/SOURCES.txt @@ -0,0 +1,13 @@ +LICENSE +README.md +setup.cfg +setup.py +diffusion2d_packages/__init__.py +diffusion2d_packages/diffusion2d.py +diffusion2d_packages/output.py +nagannhh_packages.egg-info/PKG-INFO +nagannhh_packages.egg-info/SOURCES.txt +nagannhh_packages.egg-info/dependency_links.txt +nagannhh_packages.egg-info/entry_points.txt +nagannhh_packages.egg-info/requires.txt +nagannhh_packages.egg-info/top_level.txt \ No newline at end of file diff --git a/nagannhh_packages.egg-info/dependency_links.txt b/nagannhh_packages.egg-info/dependency_links.txt new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/nagannhh_packages.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/nagannhh_packages.egg-info/entry_points.txt b/nagannhh_packages.egg-info/entry_points.txt new file mode 100644 index 00000000..c1ce363b --- /dev/null +++ b/nagannhh_packages.egg-info/entry_points.txt @@ -0,0 +1,2 @@ +[console_scripts] +diffusion2d-solve = diffusion2d_packages.diffusion2d:solve diff --git a/nagannhh_packages.egg-info/requires.txt b/nagannhh_packages.egg-info/requires.txt new file mode 100644 index 00000000..20836730 --- /dev/null +++ b/nagannhh_packages.egg-info/requires.txt @@ -0,0 +1,3 @@ +numpy>=2.1.3 +matplotlib>=3.9.2 +setuptools>=75.5.0 diff --git a/nagannhh_packages.egg-info/top_level.txt b/nagannhh_packages.egg-info/top_level.txt new file mode 100644 index 00000000..c39267a0 --- /dev/null +++ b/nagannhh_packages.egg-info/top_level.txt @@ -0,0 +1 @@ +diffusion2d_packages diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 00000000..d028b9e7 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,17 @@ +[metadata] +name = nagannhh_packages +version = 0.0.1 +author = nagannhh +url = https://github.com/harshithn31/diffusion2D +description = A package to solve 2D diffusion equation test exercise + +[options] +packages = find: +install_requires = + numpy>=2.1.3 + matplotlib>=3.9.2 + setuptools>=75.5.0 + +[options.entry_points] +console_scripts = + diffusion2d-solve = diffusion2d_packages.diffusion2d:solve \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 00000000..65147441 --- /dev/null +++ b/setup.py @@ -0,0 +1,4 @@ +from setuptools import setup + +if __name__ == "__main__": + setup() \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 00000000..b2062b11 --- /dev/null +++ b/test.py @@ -0,0 +1,2 @@ +import diffusion2d_packages.diffusion2d +diffusion2d_packages.diffusion2d.solve()