def rule_write ( order, header, x, w, r ): #*****************************************************************************80 # ## RULE_WRITE writes a quadrature rule to a file. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer ORDER, the order of the rule. # # Input, string HEADER, specifies the output files. # write files 'header_w.txt', 'header_x.txt', 'header_r.txt' defining # weights, abscissas, and region. # # Input, real X(ORDER), the abscissas. # # Input, real W(ORDER), the weights. # # Input, real R(2), the region. # from r8vec_write import r8vec_write filename_x = header + '_x.txt' filename_w = header + '_w.txt' filename_r = header + '_r.txt' print '' print ' Creating quadrature files.' print '' print ' Common header is "%s".' % ( header ) print '' print ' Weight file will be "%s".' % ( filename_w ) print ' Abscissa file will be "%s".' % ( filename_x ) print ' Region file will be "%s".' % ( filename_r ) r8vec_write ( filename_w, order, w ) r8vec_write ( filename_x, order, x ) r8vec_write ( filename_r, 2, r ) return
def rule_write(order, header, x, w, r): #*****************************************************************************80 # ## RULE_WRITE writes a quadrature rule to a file. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 March 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer ORDER, the order of the rule. # # Input, string HEADER, specifies the output files. # write files 'header_w.txt', 'header_x.txt', 'header_r.txt' defining # weights, abscissas, and region. # # Input, real X(ORDER), the abscissas. # # Input, real W(ORDER), the weights. # # Input, real R(2), the region. # from r8vec_write import r8vec_write filename_x = header + '_x.txt' filename_w = header + '_w.txt' filename_r = header + '_r.txt' print '' print ' Creating quadrature files.' print '' print ' Common header is "%s".' % (header) print '' print ' Weight file will be "%s".' % (filename_w) print ' Abscissa file will be "%s".' % (filename_x) print ' Region file will be "%s".' % (filename_r) r8vec_write(filename_w, order, w) r8vec_write(filename_x, order, x) r8vec_write(filename_r, 2, r) return
def fd1d_heat_implicit_test03(): #*****************************************************************************80 # ## FD1D_HEAT_IMPLICIT_TEST03 does a simple test problem. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 November 2014 # # Author: # # John Burkardt # from fd1d_heat_implicit import fd1d_heat_implicit from fd1d_heat_implicit_cfl import fd1d_heat_implicit_cfl from fd1d_heat_implicit_matrix import fd1d_heat_implicit_matrix from r8mat_write import r8mat_write from r8vec_write import r8vec_write from mpl_toolkits.mplot3d import axes3d, Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np print '' print 'FD1D_HEAT_IMPLICIT_TEST03:' print ' Compute an approximate solution to the time-dependent' print ' one dimensional heat equation:' print ' dH/dt - K * d2H/dx2 = f(x,t)' 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_implicit_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) # # Get the system matrix. # a = fd1d_heat_implicit_matrix(x_num, cfl) 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_implicit(a, 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('H(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"' print '' print 'FD1D_HEAT_IMPLICIT_TEST03:' print ' Normal end of execution.' return
def fd1d_heat_implicit_test01 ( ): #*****************************************************************************80 # ## FD1D_HEAT_IMPLICIT_TEST01 does a simple test problem. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 November 2014 # # Author: # # John Burkardt # from fd1d_heat_implicit import fd1d_heat_implicit from fd1d_heat_implicit_cfl import fd1d_heat_implicit_cfl from fd1d_heat_implicit_matrix import fd1d_heat_implicit_matrix from r8mat_write import r8mat_write from r8vec_write import r8vec_write from mpl_toolkits.mplot3d import axes3d, Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np print '' print 'FD1D_HEAT_IMPLICIT_TEST01:' print ' Compute an approximate solution to the time-dependent' print ' one dimensional heat equation:' print ' dH/dt - K * d2H/dx2 = f(x,t)' 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_implicit_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 ) # # Get the system matrix. # a = fd1d_heat_implicit_matrix ( x_num, cfl ) 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_implicit ( a, 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 = Axes3D ( fig ) surf = ax.plot_surface ( xmat, tmat, hmat ) plt.xlabel ( '<---X--->' ) plt.ylabel ( '<---T--->' ) plt.title ( 'H(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"' print '' print 'FD1D_HEAT_IMPLICIT_TEST01:' print ' Normal end of execution.' return
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 fem1d_heat_explicit_test02 ( ): #*****************************************************************************80 # ## FEM1D_HEAT_EXPLICIT_TEST02 does a problem with known solution. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 09 November 2014 # # Author: # # John Burkardt # from assemble_mass import assemble_mass from fem1d_heat_explicit import fem1d_heat_explicit from r8mat_write import r8mat_write from r8vec_write import r8vec_write import numpy as np from math import sqrt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt print '' print 'FEM1D_HEAT_EXPLICIT_TEST02:' print ' Using the finite element method,' 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)' # # Set the nodes. # 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 ) # # Set the times. # t_num = 51 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 ) # # Set finite element information. # element_num = x_num - 1 element_node = np.zeros ( ( 2, element_num ) ) for j in range ( 0, element_num ): element_node[0,j] = j element_node[1,j] = j + 1 quad_num = 3 mass = assemble_mass ( x_num, x, element_num, element_node, quad_num ) print '' print ' Number of X nodes = %d' % ( x_num ) print ' X interval = [ %f, %f ]' % ( x_min, x_max ) print ' X step size = %f' % ( dx ) print ' Number of T steps = %d' % ( t_num ) print ' T interval = [ %f, %f ]' % ( t_min, t_max ) print ' T step size = %f' % ( dt ) print ' Number of elements = %d' % ( element_num ) print ' Number of quadrature points = %d' % ( quad_num ) # # Running the code produces an array H of temperatures H(t,x), # and vectors x and t. # g_mat = np.zeros ( ( x_num, t_num ) ) h_mat = 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 = fem1d_heat_explicit ( x_num, x, t[j-1], dt, k_test02, \ rhs_test02, bc_test02, element_num, element_node, quad_num, mass, 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 / x_num ) print ' %4d %14.6g %14.6g' % ( j, t[j], e ) for i in range ( 0, x_num ): g_mat[i,j] = g[i] h_mat[i,j] = h[i] # # Make a product grid of T and X for plotting. # t_mat, x_mat = np.meshgrid ( t, x ) # # Plot the data. # fig = plt.figure ( ) ax = fig.add_subplot ( 111, projection = '3d' ) surf = ax.plot_surface ( x_mat, t_mat, h_mat ) 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, g_mat ) r8mat_write ( 'h_test02.txt', x_num, t_num, h_mat ) 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"' print '' print 'FEM1D_HEAT_EXPLICIT_TEST02:' 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 fem1d_heat_explicit_test02(): #*****************************************************************************80 # ## FEM1D_HEAT_EXPLICIT_TEST02 does a problem with known solution. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 09 November 2014 # # Author: # # John Burkardt # from assemble_mass import assemble_mass from fem1d_heat_explicit import fem1d_heat_explicit from r8mat_write import r8mat_write from r8vec_write import r8vec_write import numpy as np from math import sqrt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt print '' print 'FEM1D_HEAT_EXPLICIT_TEST02:' print ' Using the finite element method,' 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)' # # Set the nodes. # 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) # # Set the times. # t_num = 51 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) # # Set finite element information. # element_num = x_num - 1 element_node = np.zeros((2, element_num)) for j in range(0, element_num): element_node[0, j] = j element_node[1, j] = j + 1 quad_num = 3 mass = assemble_mass(x_num, x, element_num, element_node, quad_num) print '' print ' Number of X nodes = %d' % (x_num) print ' X interval = [ %f, %f ]' % (x_min, x_max) print ' X step size = %f' % (dx) print ' Number of T steps = %d' % (t_num) print ' T interval = [ %f, %f ]' % (t_min, t_max) print ' T step size = %f' % (dt) print ' Number of elements = %d' % (element_num) print ' Number of quadrature points = %d' % (quad_num) # # Running the code produces an array H of temperatures H(t,x), # and vectors x and t. # g_mat = np.zeros((x_num, t_num)) h_mat = 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 = fem1d_heat_explicit ( x_num, x, t[j-1], dt, k_test02, \ rhs_test02, bc_test02, element_num, element_node, quad_num, mass, 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 / x_num) print ' %4d %14.6g %14.6g' % (j, t[j], e) for i in range(0, x_num): g_mat[i, j] = g[i] h_mat[i, j] = h[i] # # Make a product grid of T and X for plotting. # t_mat, x_mat = np.meshgrid(t, x) # # Plot the data. # fig = plt.figure() ax = fig.add_subplot(111, projection='3d') surf = ax.plot_surface(x_mat, t_mat, h_mat) 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, g_mat) r8mat_write('h_test02.txt', x_num, t_num, h_mat) 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"' print '' print 'FEM1D_HEAT_EXPLICIT_TEST02:' 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
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 fem1d_heat_explicit_test01 ( ): #*****************************************************************************80 # ## FEM1D_HEAT_EXPLICIT_TEST01 runs a simple test. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 November 2014 # # Author: # # John Burkardt # from assemble_mass import assemble_mass from fem1d_heat_explicit import fem1d_heat_explicit from r8mat_write import r8mat_write from r8vec_write import r8vec_write import numpy as np from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt print '' print 'FEM1D_HEAT_EXPLICIT_TEST01:' print ' The time dependent 1D heat equation is' print '' print ' Ut - k * Uxx = f(x,t)' print '' print ' for space interval A <= X <= B with boundary conditions' print '' print ' U(A,t) = UA(t)' print ' U(B,t) = UB(t)' print '' print ' and time interval T0 <= T <= T1 with initial condition' print '' print ' U(X,T0) = U0(X).' print '' print ' To compute an approximate solution:' print ' the interval [A,B] is replace by a discretized mesh Xi' print ' a set of finite element functions PSI(X) are determined,' print ' the solution U is written as a weighted sum of the basis functions,' print ' the weak form of the differential equation is written,' print ' a time grid Tj is imposed, and time derivatives replaced by' print ' an explicit forward Euler first difference,' print '' print ' The continuous PDE has now been transformed into a set of algebraic' print ' equations for the coefficients C(Xi,Tj).' # # Set the nodes. # 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 ) # # Set the times. # t_num = 401 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 ) # # Set finite element information. # element_num = x_num - 1 element_node = np.zeros ( ( 2, element_num ) ) for j in range ( 0, element_num ): element_node[0,j] = j element_node[1,j] = j + 1 quad_num = 3 mass = assemble_mass ( x_num, x, element_num, element_node, quad_num ) print '' print ' Number of X nodes = %d' % ( x_num ) print ' X interval = [ %f, %f ]' % ( x_min, x_max ) print ' X step size = %f' % ( dx ) print ' Number of T steps = %d' % ( t_num ) print ' T interval = [ %f, %f ]' % ( t_min, t_max ) print ' T step size = %f' % ( dt ) print ' Number of elements = %d' % ( element_num ) print ' Number of quadrature points = %d' % ( quad_num ) u_mat = np.zeros ( ( x_num, t_num ) ) for j in range ( 0, t_num ): if ( j == 0 ): u = ic_test01 ( x_num, x, t[j] ) u = bc_test01 ( x_num, x, t[j], u ) else: u = fem1d_heat_explicit ( x_num, x, t[j-1], dt, k_test01, \ rhs_test01, bc_test01, element_num, element_node, quad_num, mass, u ) for i in range ( 0, x_num ): u_mat[i,j] = u[i] # # Make a product grid of T and X for plotting. # t_mat, x_mat = np.meshgrid ( t, x ) # # Make a mesh plot of the solution. # fig = plt.figure ( ) ax = fig.add_subplot ( 111, projection = '3d' ) surf = ax.plot_surface ( x_mat, t_mat, u_mat ) 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, u_mat ) 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"' print '' print 'FEM1D_HEAT_EXPLICIT_TEST01:' print ' Normal end of execution.' return