Project: Heat Diffusion

Given an insulated cylindrical metal rod with a heat source at each end, the temperature of the rod will eventually stabilize so that it varies linearly between the temperatures of the two heat sources. The physics and mathematics describing this process are beyond the scope of this course (see, e.g., [2]), but we will be able to simplify the mathematics and use Python lists to simulate the diffusion of heat in the rod.

The key idea in translating from the physical situation to a program is to discretize the problem, meaning, in this case, that we break up the continuous rod into a discrete set of short pieces. That allows us to consider all of the heat transfer happening at the boundaries between the pieces. If we keep track of the temperature at each of these boundaries, we have a list. For example, if the rod begins at 0°C, the ends are heated to 30°C and 50°C, and we imagine ten pieces, then the list

 temps = [30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50]

represents the initial state of the bar at the 11 boundaries. Our program will update these temperatures over time to simulate the diffusion of heat.

The program will also discretize time, breaking it into small pieces of length dt. Remarkably, if we choose dt carefully, a reasonably accurate algorithm to simulate diffusion is simple: to calculate the temperature of a boundary at the next time step, we just average the temperatures to its left and right.

Listing 14.2: Heat Diffusion (Algorithm)

 Set initial temps.
 Repeat:
  for each internal boundary temp (not the ends):
  new temp = average old temp to left and old temp to right
  copy new temps to old

⇒ Caution: It is important to calculate all the new temperatures based on the old temperatures. That is why there is what seems like an extra step of copying the new temperatures to the old locations. Remember that a full slice ([:]) will copy a list.

We calculate dt from the width h of each piece:

dt=12h2

Exercises

  1. Write a function init(left, right, inittemp, n) that returns a list of the initial boundary temperatures, given the fixed left and right temperatures, the initial temperature of the rod itself, and the number of segments n.
  2. Write a program diffusion.py to perform the simulation. Because it is helpful to see the values of the temperatures at each step of the simulation, write the rest of your code in main(). (We generally try to avoid print statements in any functions other than main().) Use these values, in addition to the initial temperature conditions given above:

    Rod length

    5 cm

    Number of segments

    10

    Duration of the simulation

    1 sec

    Determine the temperature at the center of the bar at the end of the simulation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.137.76