simulate ice being heated in python

To simulate ice being heated in Python, we need to compute the temperature profile of the ice block as we heat it. This can be done using a simple heat transfer model, assuming that the ice is in a one-dimensional slab with uniform thermal properties.

We can use the following equation to compute the temperature profile of the ice as a function of time:

$T(x,t) = T_{0} + (T_{i} - T_{0}) \cdot Erfc(\frac{x}{2\sqrt{\alpha t}})$

where $T_{0}$ is the initial temperature, $T_{i}$ is the temperature of the surroundings (i.e., the heating element), $Erfc$ is the complementary error function, $x$ is the distance from the surface of the ice, $\alpha$ is the thermal diffusivity of ice, and $t$ is time.

Using this equation, we can compute the temperature profile of the ice at any point in time, and then use that to compute the rate of heat transfer into the ice. We can then use a numerical integration method (e.g., the trapezoidal rule) to integrate this rate of heat transfer over time, and thus compute the total amount of energy that is transferred to the ice.

Here's some example code that implements this simulation using numpy:

main.py
import numpy as np

# Define the parameters of the simulation
L = 0.1     # Length of the ice block (m)
T0 = -5.0   # Initial temperature of the ice (C)
Ti = 20.0   # Temperature of the surroundings (C)
tmax = 60.0 # Maximum time to simulate (s)

# Define the thermal diffusivity of ice (m^2/s)
alpha = 1.43e-6

# Define the spatial grid
dx = 0.001 # Spacing between grid points (m)
x = np.arange(0, L+dx, dx)

# Initialize the temperature array
T = np.ones_like(x) * T0

# Set up a loop to simulate the heating process
t = 0.0
dt = 0.1
while t < tmax:

    # Compute the temperature profile at the current time
    T = T0 + (Ti - T0) * np.special.erfc(x / (2 * np.sqrt(alpha * t)))

    # Compute the rate of heat transfer into the ice
    q = -alpha * (T[1:] - T[:-1]) / dx

    # Integrate the rate of heat transfer over time
    Q = np.trapz(q, x[1:])

    # Update the temperature of the ice
    T0 += Q / (L * 1000 * 2.1)

    # Increment the time step
    t += dt

    # Print out the current time and temperature
    print("t = {:.1f} s, T0 = {:.1f} C".format(t, T0))
1076 chars
41 lines

This code should produce a simulation of the ice block being heated over time, with the temperature of the ice increasing until it reaches the temperature of the surroundings. Note that the parameters of the simulation can be adjusted to simulate different scenarios (e.g., different starting temperatures, different heating elements, different sizes of the ice block).

gistlibby LogSnag