def fd1d_heat_explicit_test02 ( ): #*****************************************************************************80 # ## FD1D_HEAT_EXPLICIT_TEST02 does a problem with known solution. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 November 2014 # # Author: # # John Burkardt # from fd1d_heat_explicit import fd1d_heat_explicit from fd1d_heat_explicit_cfl import fd1d_heat_explicit_cfl from math import sqrt from r8mat_write import r8mat_write from r8vec_write import r8vec_write from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np print '' print 'FD1D_HEAT_EXPLICIT_TEST02:' print ' Compute an approximate solution to the time-dependent' print ' one dimensional heat equation for a problem where we' print ' know the exact solution.' print '' print ' dH/dt - K * d2H/dx2 = f(x,t)' print '' print ' Run a simple test case.' # # Heat coefficient. # k = k_test02 ( ) # # X_NUM is the number of equally spaced nodes to use between 0 and 1. # x_num = 21 x_min = 0.0 x_max = 1.0 dx = ( x_max - x_min ) / ( x_num - 1 ) x = np.linspace ( x_min, x_max, x_num ) # # T_NUM is the number of equally spaced time points between 0 and 10.0. # t_num = 26 t_min = 0.0 t_max = 10.0 dt = ( t_max - t_min ) / ( t_num - 1 ) t = np.linspace ( t_min, t_max, t_num ) # # Get the CFL coefficient. # cfl = fd1d_heat_explicit_cfl ( k, t_num, t_min, t_max, x_num, x_min, x_max ) print '' print ' Number of X nodes = %d' % ( x_num ) print ' X interval is [%f,%f]' % ( x_min, x_max ) print ' X spacing is %f' % ( dx ) print ' Number of T values = %d' % ( t_num ) print ' T interval is [%f,%f]' % ( t_min, t_max ) print ' T spacing is %f' % ( dt ) print ' Constant K = %g' % ( k ) print ' CFL coefficient = %g' % ( cfl ) # # Running the code produces an array H of temperatures H(t,x), # and vectors x and t. # gmat = np.zeros ( ( x_num, t_num ) ) hmat = np.zeros ( ( x_num, t_num ) ) print '' print ' Step Time RMS Error' print '' for j in range ( 0, t_num ): if ( j == 0 ): h = ic_test02 ( x_num, x, t[j] ) h = bc_test02 ( x_num, x, t[j], h ) else: h = fd1d_heat_explicit ( x_num, x, t[j-1], dt, cfl, rhs_test02, bc_test02, h ) g = exact_test02 ( x_num, x, t[j] ) e = 0.0 for i in range ( 0, x_num ): e = e + ( h[i] - g[i] ) ** 2 e = sqrt ( e ) / sqrt ( x_num ) print ' %4d %14.6g %14.6g' % ( j, t[j], e ) for i in range ( 0, x_num ): gmat[i,j] = g[i] hmat[i,j] = h[i] # # Plot the data. # tmat, xmat = np.meshgrid ( t, x ) fig = plt.figure ( ) ax = Axes3D ( fig ) surf = ax.plot_surface ( xmat, tmat, hmat ) plt.xlabel ( '<---X--->' ) plt.ylabel ( '<---T--->' ) plt.title ( 'U(X,T)' ) plt.savefig ( 'plot_test02.png' ) plt.show ( ) # # Write the data to files. # r8mat_write ( 'g_test02.txt', x_num, t_num, gmat ) r8mat_write ( 'h_test02.txt', x_num, t_num, hmat ) r8vec_write ( 't_test02.txt', t_num, t ) r8vec_write ( 'x_test02.txt', x_num, x ) print '' print ' G(X,T) written to "g_test02.txt"' print ' H(X,T) written to "h_test02.txt"' print ' T values written to "t_test02.txt"' print ' X values written to "x_test02.txt"' return
def fd1d_heat_explicit_test03(): #*****************************************************************************80 # ## FD1D_HEAT_EXPLICIT_TEST03 does a simple test problem. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 November 2014 # # Author: # # John Burkardt # from fd1d_heat_explicit import fd1d_heat_explicit from fd1d_heat_explicit_cfl import fd1d_heat_explicit_cfl from math import sqrt from r8mat_write import r8mat_write from r8vec_write import r8vec_write from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np print '' print 'FD1D_HEAT_EXPLICIT_TEST03:' print ' Compute an approximate solution to the time-dependent' print ' one dimensional heat equation:' print '' print ' dH/dt - K * d2H/dx2 = f(x,t)' print '' print ' Run a simple test case.' # # Heat coefficient. # k = k_test03() # # X_NUM is the number of equally spaced nodes to use between 0 and 1. # x_num = 21 x_min = -5.0 x_max = +5.0 dx = (x_max - x_min) / (x_num - 1) x = np.linspace(x_min, x_max, x_num) # # T_NUM is the number of equally spaced time points between 0 and 10.0. # t_num = 81 t_min = 0.0 t_max = 4.0 dt = (t_max - t_min) / (t_num - 1) t = np.linspace(t_min, t_max, t_num) # # Get the CFL coefficient. # cfl = fd1d_heat_explicit_cfl(k, t_num, t_min, t_max, x_num, x_min, x_max) print '' print ' Number of X nodes = %d' % (x_num) print ' X interval is [%f,%f]' % (x_min, x_max) print ' X spacing is %f' % (dx) print ' Number of T values = %d' % (t_num) print ' T interval is [%f,%f]' % (t_min, t_max) print ' T spacing is %f' % (dt) print ' Constant K = %g' % (k) print ' CFL coefficient = %g' % (cfl) # # Running the code produces an array H of temperatures H(t,x), # and vectors x and t. # hmat = np.zeros((x_num, t_num)) for j in range(0, t_num): if (j == 0): h = ic_test03(x_num, x, t[j]) h = bc_test03(x_num, x, t[j], h) else: h = fd1d_heat_explicit(x_num, x, t[j - 1], dt, cfl, rhs_test03, bc_test03, h) for i in range(0, x_num): hmat[i, j] = h[i] # # Plot the data. # tmat, xmat = np.meshgrid(t, x) fig = plt.figure() ax = Axes3D(fig) surf = ax.plot_surface(xmat, tmat, hmat) plt.xlabel('<---X--->') plt.ylabel('<---T--->') plt.title('U(X,T)') plt.savefig('plot_test03.png') plt.show() # # Write the data to files. # r8mat_write('h_test03.txt', x_num, t_num, hmat) r8vec_write('t_test03.txt', t_num, t) r8vec_write('x_test03.txt', x_num, x) print '' print ' H(X,T) written to "h_test03.txt"' print ' T values written to "t_test03.txt"' print ' X values written to "x_test3.txt"' return
def fd1d_heat_explicit_test01(path,mode,weightsFile): """fd1d_heat_explicit_test01 does a simple test problem""" from fd1d_heat_explicit import fd1d_heat_explicit from fd1d_heat_explicit_cfl import fd1d_heat_explicit_cfl from r8mat_write import r8mat_write from r8vec_write import r8vec_write from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np print '' print 'FD1D_HEAT_EXPLICIT_TEST01:' print ' Compute an approximate solution to the time-dependent' print ' one dimensional heat equation:' print '' print ' dH/dt - K * d2H/dx2 = f(x,t)' print '' print ' Run a simple test case.' """Heat coefficient""" k = k_test01 ( ) # # X_NUM is the number of equally spaced nodes to use between 0 and 1. # x_num = 21 x_min = 0.0 x_max = 1.0 dx = ( x_max - x_min ) / ( x_num - 1 ) x = np.linspace ( x_min, x_max, x_num ) # # T_NUM is the number of equally spaced time points between 0 and 10.0. # t_num = 201 t_min = 0.0 t_max = 80.0 dt = ( t_max - t_min ) / ( t_num - 1 ) t = np.linspace ( t_min, t_max, t_num ) # # Get the CFL coefficient. # cfl = fd1d_heat_explicit_cfl ( k, t_num, t_min, t_max, x_num, x_min, x_max ) print '' print ' Number of X nodes = %d' % ( x_num ) print ' X interval is [%f,%f]' % ( x_min, x_max ) print ' X spacing is %f' % ( dx ) print ' Number of T values = %d' % ( t_num ) print ' T interval is [%f,%f]' % ( t_min, t_max ) print ' T spacing is %f' % ( dt ) print ' Constant K = %g' % ( k ) print ' CFL coefficient = %g' % ( cfl ) # # Running the code produces an array H of temperatures H(t,x), # and vectors x and t. # hmat = np.zeros ( ( x_num, t_num ) ) for j in range ( 0, t_num ): if ( j == 0 ): h = ic_test01 ( x_num, x, t[j] ,mode) h = bc_test01 ( x_num, x, t[j], h ,mode) else: h = fd1d_heat_explicit ( x_num, x, t[j-1], dt, cfl, rhs_test01, bc_test01, h, mode, weightsFile ) for i in range ( 0, x_num ): hmat[i,j] = h[i] # # Plot the data. # tmat, xmat = np.meshgrid ( t, x ) fig = plt.figure ( ) ax = fig.add_subplot ( 111, projection = '3d' ) ax = Axes3D ( fig ) surf = ax.plot_surface ( xmat, tmat, hmat ) plt.xlabel ( '<---X--->' ) plt.ylabel ( '<---T--->' ) plt.title ( 'U(X,T)' ) save_at = path.strip() print path, save_at filename = str(save_at) + 'plot_test_' + str(mode) + '.png' print filename plt.savefig (filename) #plt.show ( ) # # Write the data to files. # filename = str(save_at) + 'h_test01.txt' r8mat_write ( filename, x_num, t_num, hmat ) #filename = str(save_at) + 't_test01.txt' #r8vec_write ( filename, t_num, t ) #filename = str(save_at) + 'x_test01.txt' #r8vec_write ( filename, x_num, x ) print '' print ' H(X,T) written to "h_test01.txt"' print ' T values written to "t_test01.txt"' print ' X values written to "x_test01.txt"' # # Terminate. # print '' print 'FD1D_HEAT_EXPLICIT_TEST01:' print ' Normal end of execution.' return
def fd1d_heat_explicit_test01(): # *****************************************************************************80 # ## FD1D_HEAT_EXPLICIT_TEST01 does a simple test problem # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 November 2014 # # Author: # # John Burkardt # from fd1d_heat_explicit import fd1d_heat_explicit from fd1d_heat_explicit_cfl import fd1d_heat_explicit_cfl from r8mat_write import r8mat_write from r8vec_write import r8vec_write from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np print "" print "FD1D_HEAT_EXPLICIT_TEST01:" print " Compute an approximate solution to the time-dependent" print " one dimensional heat equation:" print "" print " dH/dt - K * d2H/dx2 = f(x,t)" print "" print " Run a simple test case." # # Heat coefficient. # k = k_test01() # # X_NUM is the number of equally spaced nodes to use between 0 and 1. # x_num = 21 x_min = 0.0 x_max = 1.0 dx = (x_max - x_min) / (x_num - 1) x = np.linspace(x_min, x_max, x_num) # # T_NUM is the number of equally spaced time points between 0 and 10.0. # t_num = 201 t_min = 0.0 t_max = 80.0 dt = (t_max - t_min) / (t_num - 1) t = np.linspace(t_min, t_max, t_num) # # Get the CFL coefficient. # cfl = fd1d_heat_explicit_cfl(k, t_num, t_min, t_max, x_num, x_min, x_max) print "" print " Number of X nodes = %d" % (x_num) print " X interval is [%f,%f]" % (x_min, x_max) print " X spacing is %f" % (dx) print " Number of T values = %d" % (t_num) print " T interval is [%f,%f]" % (t_min, t_max) print " T spacing is %f" % (dt) print " Constant K = %g" % (k) print " CFL coefficient = %g" % (cfl) # # Running the code produces an array H of temperatures H(t,x), # and vectors x and t. # hmat = np.zeros((x_num, t_num)) for j in range(0, t_num): if j == 0: h = ic_test01(x_num, x, t[j]) h = bc_test01(x_num, x, t[j], h) else: h = fd1d_heat_explicit(x_num, x, t[j - 1], dt, cfl, rhs_test01, bc_test01, h) for i in range(0, x_num): hmat[i, j] = h[i] # # Plot the data. # tmat, xmat = np.meshgrid(t, x) fig = plt.figure() # ax = fig.add_subplot ( 111, projection = '3d' ) ax = Axes3D(fig) surf = ax.plot_surface(xmat, tmat, hmat) plt.xlabel("<---X--->") plt.ylabel("<---T--->") plt.title("U(X,T)") plt.savefig("plot_test01.png") plt.show() # # Write the data to files. # r8mat_write("h_test01.txt", x_num, t_num, hmat) r8vec_write("t_test01.txt", t_num, t) r8vec_write("x_test01.txt", x_num, x) print "" print ' H(X,T) written to "h_test01.txt"' print ' T values written to "t_test01.txt"' print ' X values written to "x_test01.txt"' # # Terminate. # print "" print "FD1D_HEAT_EXPLICIT_TEST01:" print " Normal end of execution." return
def fd1d_heat_explicit_test02 ( ): # FD1D_HEAT_EXPLICIT_TEST03 does a simple test problem. from fd1d_heat_explicit import fd1d_heat_explicit from fd1d_heat_explicit_cfl import fd1d_heat_explicit_cfl from math import sqrt from r8mat_write import r8mat_write from r8vec_write import r8vec_write from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np print '' print 'FD1D_HEAT_EXPLICIT_TEST03:' print ' Compute an approximate solution to the time-dependent' print ' one dimensional heat equation:' print '' print ' dH/dt - K * d2H/dx2 = f(x,t)' print '' print ' Run a simple test case.' # # Heat coefficient. # k = k_test02 ( ) # # X_NUM is the number of equally spaced nodes to use between 0 and 1. # x_num = 21 x_min = -5.0 x_max = +5.0 dx = ( x_max - x_min ) / ( x_num - 1 ) x = np.linspace ( x_min, x_max, x_num ) # # T_NUM is the number of equally spaced time points between 0 and 10.0. # t_num = 81 t_min = 0.0 t_max = 4.0 dt = ( t_max - t_min ) / ( t_num - 1 ) t = np.linspace ( t_min, t_max, t_num ) # # Get the CFL coefficient. # cfl = fd1d_heat_explicit_cfl ( k, t_num, t_min, t_max, x_num, x_min, x_max ) print '' print ' Number of X nodes = %d' % ( x_num ) print ' X interval is [%f,%f]' % ( x_min, x_max ) print ' X spacing is %f' % ( dx ) print ' Number of T values = %d' % ( t_num ) print ' T interval is [%f,%f]' % ( t_min, t_max ) print ' T spacing is %f' % ( dt ) print ' Constant K = %g' % ( k ) print ' CFL coefficient = %g' % ( cfl ) # # Running the code produces an array H of temperatures H(t,x), # and vectors x and t. # hmat = np.zeros ( ( x_num, t_num ) ) for j in range ( 0, t_num ): if ( j == 0 ): h = ic_test02 ( x_num, x, t[j] ) h = bc_test02 ( x_num, x, t[j], h ) else: h = fd1d_heat_explicit ( x_num, x, t[j-1], dt, cfl, rhs_test02, bc_test02, h ) for i in range ( 0, x_num ): hmat[i,j] = h[i] # # Plot the data. # tmat, xmat = np.meshgrid ( t, x ) fig = plt.figure ( ) ax = Axes3D ( fig ) surf = ax.plot_surface ( xmat, tmat, hmat ) plt.xlabel ( '<---X--->' ) plt.ylabel ( '<---T--->' ) plt.title ( 'U(X,T)' ) plt.savefig ( 'plot_test02.png' ) plt.show ( ) # # Write the data to files. # r8mat_write ( 'h_test02.txt', x_num, t_num, hmat ) r8vec_write ( 't_test02.txt', t_num, t ) r8vec_write ( 'x_test02.txt', x_num, x ) print '' print ' H(X,T) written to "h_test02.txt"' print ' T values written to "t_test02.txt"' print ' X values written to "x_test3.txt"' return