simulate and animate ice being heated using heat enthalpy in python

To simulate and animate the heating of ice using heat enthalpy in python, we can use the following steps:

  1. Define the constants needed for the simulation, such as the specific heat capacity of ice (c), the latent heat of fusion of ice (Lf), and the density of ice (rho).
main.py
c = 2.108     # specific heat capacity of ice in J/(g*K)
Lf = 333.55   # latent heat of fusion of ice in J/g
rho = 0.9167  # density of ice in g/cm^3
150 chars
4 lines
  1. Set up the initial conditions for the simulation, such as the mass of ice (m), the initial temperature of the ice (T_i), the final temperature of the system (T_f), the time step (dt), and the total simulation time (t_max).
main.py
m = 100.0     # mass of ice in g
T_i = -20.0   # initial temperature of ice in °C
T_f = 100.0   # final temperature of water in °C
dt = 0.1      # time step in s
t_max = 300.0 # total simulation time in s
205 chars
6 lines
  1. Calculate the total amount of heat needed to melt the ice and then heat the resulting water to the final temperature.
main.py
Q = m * Lf + m * c * (T_f - 0.0)  # total heat required in J
61 chars
2 lines
  1. Set up the simulation loop, where we iterate over time steps and calculate the temperature of the ice/water at each time step.
main.py
import numpy as np
import matplotlib.pyplot as plt

# arrays to store data
t_arr = np.arange(0, t_max+dt, dt)
T_arr = np.zeros_like(t_arr)

# set initial conditions
T_arr[0] = T_i

for i in range(1, len(t_arr)):
    # Calculate the rate of temperature change based on the current temperature
    T = T_arr[i-1]   # current temperature
    dT_dt = Q / (m * c) * rho * dt / Lf
    
    # Calculate the new temperature
    T += dT_dt
    
    # Record the new temperature
    T_arr[i] = T
486 chars
21 lines
  1. Plot the results of the simulation to animate the heating of the ice.
main.py
# Set up the animation
fig, ax = plt.subplots()

def animate(i):
    ax.clear()
    ax.set_xlim(0, t_max)
    ax.set_ylim(T_i, T_f)
    ax.plot(t_arr[:i], T_arr[:i])
    ax.set_xlabel('Time (s)')
    ax.set_ylabel('Temperature (°C)')
    ax.set_title('Heating of Ice Simulation')

ani = animation.FuncAnimation(fig, animate, interval=10, frames=len(t_arr), repeat=False)
plt.show()
382 chars
15 lines

This will generate an animation of the temperature of the ice/water as it heats up over time due to the input heat energy.

gistlibby LogSnag