def sphere_fibonacci_grid_points_test(): #*****************************************************************************80 # #% SPHERE_FIBONACCI_GRID_POINTS_TEST tests SPHERE_FIBONACCI_GRID_POINTS. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 May 2015 # # Author: # # John Burkardt # import platform from r8mat_print_some import r8mat_print_some from r8mat_write import r8mat_write print('') print('SPHERE_FIBONACCI_GRID_POINTS_TEST') print(' Python version: %s' % (platform.python_version())) print(' SPHERE_FIBONACCI_GRID_POINTS computes points on a sphere') print(' that lie on a Fibonacci spiral.') ng = 1000 print('') print(' Number of points NG = %d' % (ng)) xg = sphere_fibonacci_grid_points(ng) r8mat_print_some(ng, 3, xg, 0, 0, 19, 2, ' Part of the grid array:') # # Write the nodes to a file. # filename = 'sphere_fibonacci_grid_points.xyz' r8mat_write(filename, ng, 3, xg) # # Terminate. # print('') print('SPHERE_FIBONACCI_GRID_POINTS_TEST:') print(' Normal end of execution.') return
def sphere_fibonacci_grid_points_test ( ): #*****************************************************************************80 # #% SPHERE_FIBONACCI_GRID_POINTS_TEST tests SPHERE_FIBONACCI_GRID_POINTS. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 May 2015 # # Author: # # John Burkardt # from r8mat_print_some import r8mat_print_some from r8mat_write import r8mat_write print '' print 'SPHERE_FIBONACCI_GRID_POINTS_TEST' print ' SPHERE_FIBONACCI_GRID_POINTS computes points on a sphere' print ' that lie on a Fibonacci spiral.' ng = 1000 print '' print ' Number of points NG = %d' % ( ng ) xg = sphere_fibonacci_grid_points ( ng ) r8mat_print_some ( ng, 3, xg, 0, 0, 19, 2, ' Part of the grid array:' ) # # Write the nodes to a file. # filename = 'sphere_fibonacci_grid_points.xyz' r8mat_write ( filename, ng, 3, xg ) # # Terminate. # print '' print 'SPHERE_FIBONACCI_GRID_POINTS_TEST:' print ' Normal end of execution.' return
def polygon_grid_points_test01 ( ): #*****************************************************************************80 # ## POLYGON_GRID_POINTS_TEST01 tests POLYGON_GRID_POINTS # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 10 May 2015 # # Author: # # John Burkardt # import numpy as np from polygon_grid_count import polygon_grid_count from polygon_grid_display import polygon_grid_display from r8mat_print import r8mat_print from r8mat_write import r8mat_write print '' print 'POLYGON_GRID_POINTS_TEST01:' print ' POLYGON_GRID_POINTS returns grid points for a polygon' print ' of NV vertices, with N+1 points on a side' print '' print ' For this test, the polygon is a triangle.' # # Define the polygon. # nv = 3 v = np.array ( [ \ [ 0.0, 0.0 ], \ [ 1.0, 0.0 ], \ [ 0.5, 0.86602540378443860 ] ] ) r8mat_print ( nv, 2, v, ' Polygon vertices:' ) # # Count the grid points. # n = 5 ng = polygon_grid_count ( n, nv ) print '' print ' N = %d' % ( n ) print ' Number of grid points will be NG = %d' % ( ng ) # # Compute the grid points. # xg = polygon_grid_points ( n, nv, v, ng ) r8mat_print ( ng, 2, xg, ' The grid point array:' ) # # Display the points. # filename = 'triangle.png' polygon_grid_display ( n, nv, v, ng, xg, filename ) # # Write the points to a file. # filename = 'triangle.xy' r8mat_write ( filename, ng, 2, xg ) print '' print ' Data written to the file "%s"' % ( filename ) # # Terminate. # print '' print 'POLYGON_GRID_POINTS_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 polygon_grid_points_test01(): #*****************************************************************************80 # ## POLYGON_GRID_POINTS_TEST01 tests POLYGON_GRID_POINTS # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 10 May 2015 # # Author: # # John Burkardt # import numpy as np from polygon_grid_count import polygon_grid_count from polygon_grid_display import polygon_grid_display from r8mat_print import r8mat_print from r8mat_write import r8mat_write print '' print 'POLYGON_GRID_POINTS_TEST01:' print ' POLYGON_GRID_POINTS returns grid points for a polygon' print ' of NV vertices, with N+1 points on a side' print '' print ' For this test, the polygon is a triangle.' # # Define the polygon. # nv = 3 v = np.array ( [ \ [ 0.0, 0.0 ], \ [ 1.0, 0.0 ], \ [ 0.5, 0.86602540378443860 ] ] ) r8mat_print(nv, 2, v, ' Polygon vertices:') # # Count the grid points. # n = 5 ng = polygon_grid_count(n, nv) print '' print ' N = %d' % (n) print ' Number of grid points will be NG = %d' % (ng) # # Compute the grid points. # xg = polygon_grid_points(n, nv, v, ng) r8mat_print(ng, 2, xg, ' The grid point array:') # # Display the points. # filename = 'triangle.png' polygon_grid_display(n, nv, v, ng, xg, filename) # # Write the points to a file. # filename = 'triangle.xy' r8mat_write(filename, ng, 2, xg) print '' print ' Data written to the file "%s"' % (filename) # # Terminate. # print '' print 'POLYGON_GRID_POINTS_TEST01:' print ' Normal end of execution.' 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_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 triangle_grid_points_test ( n ): #*****************************************************************************80 # ## TRIANGLE_GRID_POINTS_TEST tests TRIANGLE_GRID_POINTS. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 09 April 2015 # # Author: # # John Burkardt # import numpy as np from r82col_print_part import r82col_print_part from r8mat_print import r8mat_print from r8mat_write import r8mat_write from triangle_grid_count import triangle_grid_count from triangle_grid_display import triangle_grid_display print '' print 'TRIANGLE_GRID_POINTS_TEST:' print ' TRIANGLE_GRID_POINTS defines a triangular grid' print ' with N+1 points on a side, based on any triangle.' print '' print ' Input value of N is %d' % ( n ) ng = triangle_grid_count ( n ) print ' Number of grid points will be %d' % ( ng ) t = np.array ( [ \ [ 0.0, 0.0 ], \ [ 1.0, 0.0 ], \ [ 0.5, 0.86602540378443860 ] ] ) r8mat_print ( 3, 2, t, ' Triangle vertices:' ) tg = triangle_grid_points ( n, t ) r82col_print_part ( ng, tg, 20, ' Part of the grid point array:' ); # # Write the data to a file. # filename = 'triangle_grid_points.xy' r8mat_write ( filename, ng, 2, tg ) # # Plot the data. # print '' print ' Data written to the file "%s".' % ( filename ) filename = 'triangle_grid_points.png' triangle_grid_display ( t, ng, tg, filename ) print '' print 'TRIANGLE_GRID_POINTS_TEST:' print ' Normal end of execution.' return
def burgers_viscous_time_exact1_test02(): #*****************************************************************************80 # ## BURGERS_VISCOUS_TIME_EXACT1_TEST02 tests sets up a finer test case. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 September 2015 # # Author: # # John Burkardt # import numpy as np import platform from r8mat_print import r8mat_print from r8mat_write import r8mat_write from r8vec_print import r8vec_print vtn = 41 vxn = 41 nu = 0.01 / np.pi print('') print('BURGERS_VISCOUS_TIME_EXACT1_TEST02') print(' Python version: %s' % (platform.python_version())) print(' BURGERS_VISCOUS_TIME_EXACT1 computes solution #1') print(' to the Burgers equation.') print('') print(' Viscosity NU = %g' % (nu)) print(' NX = %d' % (vxn)) print(' NT = %d' % (vtn)) xlo = -1.0 xhi = +1.0 vx = np.linspace(xlo, xhi, vxn) r8vec_print(vxn, vx, ' X grid points:') tlo = 0.0 thi = 3.0 / np.pi vt = np.linspace(tlo, thi, vtn) r8vec_print(vtn, vt, ' T grid points:') vu = burgers_viscous_time_exact1(nu, vxn, vx, vtn, vt) filename = 'burgers_solution_test02.txt' r8mat_write(filename, vxn, vtn, vu) print('') print(' Data written to file "%s"' % (filename)) # # Terminate # print('') print('BURGERS_VISCOUS_TIME_EXACT1_TEST02') 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 square_grid_points_test ( m, n ): #*****************************************************************************80 # ## SQUARE_GRID_POINTS_TEST tests SQUARE_GRID_POINTS. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 13 April 2015 # # Author: # # John Burkardt # import numpy as np from r82col_print_part import r82col_print_part from r8mat_print import r8mat_print from r8mat_write import r8mat_write from square_grid_count import square_grid_count from square_grid_display import square_grid_display print '' print 'SQUARE_GRID_POINTS_TEST:' print ' SQUARE_GRID_POINTS defines a grid' print ' with M*N points on a side, based on a quadrilateral.' print '' print ' Input value of M is %d' % ( m ) print ' Input value of N is %d' % ( n ) ns = np.zeros ( 2, dtype = np.int32 ); ns[0] = m ns[1] = n ng = square_grid_count ( ns ) print '' print ' Number of grid points will be %d' % ( ng ) xv = np.array ( [ \ [ 0.0, 1.0 ], \ [ 3.0, 2.0 ], \ [ 4.0, 5.0 ], \ [ 1.0, 3.0 ] ] ) r8mat_print ( 4, 2, xv, ' Quadrilateral vertices:' ) xg = square_grid_points ( ns, xv, ng ) r82col_print_part ( ng, xg, 20, ' Part of the grid point array:' ); # # Write the data to a file. # filename = 'square_grid_points.xy' r8mat_write ( filename, ng, 2, xg ) print '' print ' Data written to the file "%s".' % ( filename ) # # Plot the data. # filename = 'square_grid_points.png' square_grid_display ( xv, ng, xg, filename ) # # Terminate. # print '' print 'SQUARE_GRID_POINTS_TEST:' print ' Normal end of execution.' return
def ball_grid_points_test(): #*****************************************************************************80 # #% BALL_GRID_POINTS_TEST tests BALL_GRID_POINTS. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 11 April 2015 # # Author: # # John Burkardt # import numpy as np from ball_grid_count import ball_grid_count from ball_grid_display import ball_grid_display from r83col_print_part import r83col_print_part from r8mat_write import r8mat_write print '' print 'BALL_GRID_POINTS_TEST:' print ' BALL_GRID_POINTS can define a grid of points' print ' with N+1 points on a horizontal or vertical radius,' print ' based on any ball.' n = 4 r = 2.0 c = np.array([1.0, 5.0, 2.0]) print '' print ' We use N = %d' % (n) print ' Radius R = %g' % (r) print ' Center C = (%g,%g,%g)' % (c[0], c[1], c[2]) ng = ball_grid_count(n, r, c) print '' print ' Number of grid points will be %d' % (ng) xg = ball_grid_points(n, r, c, ng) r83col_print_part(ng, xg, 20, ' Part of the grid point array:') filename = 'ball_grid_points.xyz' r8mat_write(filename, ng, 3, xg) print '' print ' Data written to the file "%s".' % (filename) # # Plot the grid. # filename = 'ball_grid_points.png' ball_grid_display(r, c, ng, xg, filename) print '' print ' Plot written to the file "%s".' % (filename) # # Terminate. # print '' print 'BALL_GRID_POINTS_TEST:' print ' Normal end of execution.' 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
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_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_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 square_grid_points_test(m, n): #*****************************************************************************80 # ## SQUARE_GRID_POINTS_TEST tests SQUARE_GRID_POINTS. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 13 April 2015 # # Author: # # John Burkardt # import numpy as np from r82col_print_part import r82col_print_part from r8mat_print import r8mat_print from r8mat_write import r8mat_write from square_grid_count import square_grid_count from square_grid_display import square_grid_display print '' print 'SQUARE_GRID_POINTS_TEST:' print ' SQUARE_GRID_POINTS defines a grid' print ' with M*N points on a side, based on a quadrilateral.' print '' print ' Input value of M is %d' % (m) print ' Input value of N is %d' % (n) ns = np.zeros(2, dtype=np.int32) ns[0] = m ns[1] = n ng = square_grid_count(ns) print '' print ' Number of grid points will be %d' % (ng) xv = np.array ( [ \ [ 0.0, 1.0 ], \ [ 3.0, 2.0 ], \ [ 4.0, 5.0 ], \ [ 1.0, 3.0 ] ] ) r8mat_print(4, 2, xv, ' Quadrilateral vertices:') xg = square_grid_points(ns, xv, ng) r82col_print_part(ng, xg, 20, ' Part of the grid point array:') # # Write the data to a file. # filename = 'square_grid_points.xy' r8mat_write(filename, ng, 2, xg) print '' print ' Data written to the file "%s".' % (filename) # # Plot the data. # filename = 'square_grid_points.png' square_grid_display(xv, ng, xg, filename) # # Terminate. # print '' print 'SQUARE_GRID_POINTS_TEST:' print ' Normal end of execution.' return
def ball_grid_points_test ( ): #*****************************************************************************80 # #% BALL_GRID_POINTS_TEST tests BALL_GRID_POINTS. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 11 April 2015 # # Author: # # John Burkardt # import numpy as np from ball_grid_count import ball_grid_count from ball_grid_display import ball_grid_display from r83col_print_part import r83col_print_part from r8mat_write import r8mat_write print '' print 'BALL_GRID_POINTS_TEST:' print ' BALL_GRID_POINTS can define a grid of points' print ' with N+1 points on a horizontal or vertical radius,' print ' based on any ball.' n = 4 r = 2.0 c = np.array ( [ 1.0, 5.0, 2.0 ] ) print '' print ' We use N = %d' % ( n ) print ' Radius R = %g' % ( r ) print ' Center C = (%g,%g,%g)' % ( c[0], c[1], c[2] ) ng = ball_grid_count ( n, r, c ) print '' print ' Number of grid points will be %d' % ( ng ) xg = ball_grid_points ( n, r, c, ng ) r83col_print_part ( ng, xg, 20, ' Part of the grid point array:' ) filename = 'ball_grid_points.xyz' r8mat_write ( filename, ng, 3, xg ) print '' print ' Data written to the file "%s".' % ( filename ) # # Plot the grid. # filename = 'ball_grid_points.png' ball_grid_display ( r, c, ng, xg, filename ) print '' print ' Plot written to the file "%s".' % ( filename ) # # Terminate. # print '' print 'BALL_GRID_POINTS_TEST:' print ' Normal end of execution.' return
def circle_arc_grid_points_test(): #*****************************************************************************80 # ## CIRCLE_ARC_GRID_POINTS_TEST demonstrates the use of CIRCLE_ARC_GRID. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 April 2015 # # Author: # # John Burkardt # import numpy as np from circle_arc_grid_display import circle_arc_grid_display from r82col_print_part import r82col_print_part from r8mat_write import r8mat_write print '' print 'CIRCLE_ARC_GRID_POINTS_TEST' print ' CIRCLE_ARC_GRID_POINTS returns points along a circular arc.' print ' In this example, we compute points on a 90 degree arc.' r = 2.0 c = np.array([5.0, 5.0]) a = np.array([0.0, 90.0]) n = 20 # # Echo the input. # print '' print ' Radius = %g' % (r) print ' Center = %g %g\n' % (c[0], c[1]) print ' Angle 1 = %g' % (a[0]) print ' Angle 2 = %g' % (a[1]) print ' Number of points = %d' % (n) # # Compute the data. # xy = circle_arc_grid_points(r, c, a, n) # # Print a little of the data. # r82col_print_part(n, xy, 10, ' Some points on the arc:') # # Write the data. # filename = 'circle_arc_grid_points.xy' r8mat_write(filename, n, 2, xy) print '' print ' Data written to "%s"' % (filename) # # Plot the data. # filename = 'circle_arc_grid_points.png' circle_arc_grid_display(r, c, a, n, xy, filename) print '' print ' Plot saved in file "%s"' % (filename) # # Terminate. # print '' print 'CIRCLE_ARC_GRID_POINTS_TEST:' 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 circle_arc_grid_points_test ( ): #*****************************************************************************80 # ## CIRCLE_ARC_GRID_POINTS_TEST demonstrates the use of CIRCLE_ARC_GRID. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 April 2015 # # Author: # # John Burkardt # import numpy as np from circle_arc_grid_display import circle_arc_grid_display from r82col_print_part import r82col_print_part from r8mat_write import r8mat_write print '' print 'CIRCLE_ARC_GRID_POINTS_TEST' print ' CIRCLE_ARC_GRID_POINTS returns points along a circular arc.' print ' In this example, we compute points on a 90 degree arc.' r = 2.0 c = np.array ( [ 5.0, 5.0 ] ) a = np.array ( [ 0.0, 90.0 ] ) n = 20; # # Echo the input. # print '' print ' Radius = %g' % ( r ) print ' Center = %g %g\n' % ( c[0], c[1] ) print ' Angle 1 = %g' % ( a[0] ) print ' Angle 2 = %g' % ( a[1] ) print ' Number of points = %d' % ( n ) # # Compute the data. # xy = circle_arc_grid_points ( r, c, a, n ) # # Print a little of the data. # r82col_print_part ( n, xy, 10, ' Some points on the arc:' ) # # Write the data. # filename = 'circle_arc_grid_points.xy' r8mat_write ( filename, n, 2, xy ) print '' print ' Data written to "%s"' % ( filename ) # # Plot the data. # filename = 'circle_arc_grid_points.png' circle_arc_grid_display ( r, c, a, n, xy, filename ) print '' print ' Plot saved in file "%s"' % ( filename ) # # Terminate. # print '' print 'CIRCLE_ARC_GRID_POINTS_TEST:' print ' Normal end of execution.' return
def triangle_grid_points_test(n): #*****************************************************************************80 # ## TRIANGLE_GRID_POINTS_TEST tests TRIANGLE_GRID_POINTS. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 09 April 2015 # # Author: # # John Burkardt # import numpy as np from r82col_print_part import r82col_print_part from r8mat_print import r8mat_print from r8mat_write import r8mat_write from triangle_grid_count import triangle_grid_count from triangle_grid_display import triangle_grid_display print '' print 'TRIANGLE_GRID_POINTS_TEST:' print ' TRIANGLE_GRID_POINTS defines a triangular grid' print ' with N+1 points on a side, based on any triangle.' print '' print ' Input value of N is %d' % (n) ng = triangle_grid_count(n) print ' Number of grid points will be %d' % (ng) t = np.array ( [ \ [ 0.0, 0.0 ], \ [ 1.0, 0.0 ], \ [ 0.5, 0.86602540378443860 ] ] ) r8mat_print(3, 2, t, ' Triangle vertices:') tg = triangle_grid_points(n, t) r82col_print_part(ng, tg, 20, ' Part of the grid point array:') # # Write the data to a file. # filename = 'triangle_grid_points.xy' r8mat_write(filename, ng, 2, tg) # # Plot the data. # print '' print ' Data written to the file "%s".' % (filename) filename = 'triangle_grid_points.png' triangle_grid_display(t, ng, tg, filename) print '' print 'TRIANGLE_GRID_POINTS_TEST:' print ' Normal end of execution.' return