我是编码世界的新手,所以在这里放轻松吧。我正在尝试创建一个方形平底锅的数字模拟,正好在一个点加热,即用电线加热。不幸的是,我的功能一直在使我的doc.tests失败,我不明白为什么。任何帮助将不胜感激
import sys
sys.path.append('/home/courses/python')
from logic import *
from numpy import *
def create_plate(size, plate_temperature):
plate_temperatures = empty([size, size])
plate_temperatures[:,:] = plate_temperature
return plate_temperatures
def simulate_plate(plate_temperatures,hot_x,hot_y, hot_temp,air_temp, htc,hcc,ahtc):
""" Simulate one time step on the plate, given array of temperatures, etc. """
precondition()
plate_temperatures[hot_x,hot_y] = hot_temp
#number rows = len(input)
#number cols = len(input[0])
# First, calculate heat flow _into_ every 1cmx1cm square
heat_in = zeros_like(plate_temperatures)
# The "length" of an array is the number of rows; the length of a row is the number of columns.
#because the plate is always a square precondition range(len(plate_temperatures)) ==range(len(plate_temperatures[row_place]))
for row_place in range(len(plate_temperatures)): # is this accurate?
for column_place in range(len(plate_temperatures[row_place])):
plate_temperatures[hot_x,hot_y] = hot_temp
faces = 2 # Number of faces exposed to the air, top and bottom of plate
if(column_place!=0): #square is not on left edge of plate
from_left = (plate_temperatures[row_place,column_place-1] - plate_temperatures[row_place,column_place]) * htc # heat from left
else:#square is on left edge; nothing to the left of it
from_left = 0
if(column_place<(len(plate_temperatures[row_place])-1)): #square is not on the right edge of plate
from_right = (plate_temperatures[row_place,column_place+1] - plate_temperatures[row_place,column_place]) * htc # heat from right
else: #square is on the right edge; nothing to the right of it
from_right = 0
if row_place !=0: #square is not on the top edge of plate
from_above =(plate_temperatures[row_place+1,column_place] - plate_temperatures[row_place,column_place]) * htc # heat from above
else: # square is on the top edge of plate
from_above = 0
if row_place< (len(plate_temperatures)-1):#square is not on the bottom edge of the plate
from_below = (plate_temperatures[row_place-1,column_place] - plate_temperatures[row_place,column_place]) * htc # heat from below
else:#square is on the bottom edge of the plate
from_below = 0
from_air = (air_temp - plate_temperatures[row_place,column_place]) * faces * ahtc # heat from air
heat_in[row_place,column_place] = from_left + from_right + from_above + from_below + from_air
# Now, add the heat we found above to the old temperatures
plate_temperatures[row_place, column_place] += (heat_in[row_place,column_place] * hcc)
# restore the temperature of the heated spot
plate_temperatures[hot_x,hot_y] = hot_temp
def simulate_plate_N_steps(plate_temperatures,hot_x,hot_y, hot_temp,air_temp, htc,hcc,ahtc, N):
""" Just call the one-time-step function N times... """
all_averages = [] # log of temperatures at end of time steps
for t in range(N):
simulate_plate(plate_temperatures,hot_x,hot_y, hot_temp,air_temp, htc,hcc,ahtc)
all_averages.append(average_temp(plate_temperatures))
print "Completed", N, "simulated time steps; end-step average temps were:", all_averages
# copied from http://docs.python.org/lib/module-doctest.html
def _test():
import doctest
result = doctest.testmod()
if result[0] == 0:
print "Wahoo! Passed all", result[1], __file__.split('/')[-1], "tests!"
else:
print "Rats!"
if __name__ == "__main__": _test()