#! /usr/bin/env python # def fem1d_bvp_quadratic_test10 ( ): #*****************************************************************************80 # ## FEM1D_BVP_QUADRATIC_TEST10 tests FEM1D_BVP_QUADRATIC. # # Discussion: # # We want to compute errors and do convergence rates for the # following problem: # # - uxx + u = x for 0 < x < 1 # u(0) = u(1) = 0 # # exact = x - sinh(x) / sinh(1) # exact' = 1 - cosh(x) / sinh(1) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 13 July 2015 # # Author: # # John Burkardt # # Reference: # # Dianne O'Leary, # Scientific Computing with Case Studies, # SIAM, 2008, # ISBN13: 978-0-898716-66-5, # LC: QA401.O44. # import matplotlib.pyplot as plt import numpy as np import platform from fem1d_bvp_quadratic import fem1d_bvp_quadratic from h1s_error_quadratic import h1s_error_quadratic from l2_error_quadratic import l2_error_quadratic from max_error_quadratic import max_error_quadratic print ( '' ) print ( 'FEM1D_BVP_QUADRATIC_TEST10' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Solve -( A(x) U\'(x) )\' + C(x) U(x) = F(x)' ) print ( ' for 0 < x < 1, with U(0) = U(1) = 0.' ) print ( ' A(X) = 1.0' ) print ( ' C(X) = 1.0' ) print ( ' F(X) = X' ) print ( ' U(X) = X - SINH(X) / SINH(1)' ) print ( '' ) print ( ' log(E) E L2error H1error Maxerror' ) print ( '' ) e_log_max = 6 ne_plot = np.zeros ( e_log_max + 1 ) h_plot = np.zeros ( e_log_max + 1 ) l2_plot = np.zeros ( e_log_max + 1 ) h1_plot = np.zeros ( e_log_max + 1 ) mx_plot = np.zeros ( e_log_max + 1 ) for e_log in range ( 0, e_log_max + 1 ): ne = 2 ** ( e_log + 1 ) n = ne + 1 x_lo = 0.0 x_hi = 1.0 x = np.linspace ( x_lo, x_hi, n ) u = fem1d_bvp_quadratic ( n, a10, c10, f10, x ) ne_plot[e_log] = ne h_plot[e_log] = ( x_hi - x_lo ) / float ( ne ) l2_plot[e_log] = l2_error_quadratic ( n, x, u, exact10 ) h1_plot[e_log] = h1s_error_quadratic ( n, x, u, exactp10 ) mx_plot[e_log] = max_error_quadratic ( n, x, u, exact10 ) print ( ' %4d %4d %14g %14g %14g' \ % ( e_log + 1, ne, l2_plot[e_log], h1_plot[e_log], mx_plot[e_log] ) ) print ( '' ) print ( ' log(E1) E1 / E2 L2rate H1rate Maxrate' ) print ( '' ) for e_log in range ( 0, e_log_max ): ne1 = 2 ** ( e_log + 1 ) ne2 = 2 * ne1 ne_plot[e_log] = ne1 l2 = l2_plot[e_log] / l2_plot[e_log+1] l2 = np.log ( l2 ) / np.log ( 2.0 ) h1 = h1_plot[e_log] / h1_plot[e_log+1] h1 = np.log ( h1 ) / np.log ( 2.0 ) mx = mx_plot[e_log] / mx_plot[e_log+1] mx = np.log ( mx ) / np.log ( 2.0 ) print ( ' %4d %4d/%4d %14g %14g %14g' \ % ( e_log + 1, ne1, ne2, l2, h1, mx ) ) # # Plot the L2 error as a function of NE. # fig = plt.figure ( ) plt.plot ( ne_plot, l2_plot, 'bo-' ) plt.xlabel ( '<---NE--->' ) plt.ylabel ( '<---L2(error)--->' ) plt.title ( 'L2 error as function of number of elements' ) plt.xscale ( 'log' ) plt.yscale ( 'log' ) plt.grid ( True ) plt.axis ( 'equal' ) plt.savefig ( 'l2error_test10.png' ) plt.show ( ) # # Plot the max error as a function of NE. # fig = plt.figure ( ) plt.plot ( ne_plot, mx_plot, 'bo-' ) plt.xlabel ( '<---NE--->' ) plt.ylabel ( '<---Max(error)--->' ) plt.title ( 'Max error as function of number of elements' ) plt.xscale ( 'log' ) plt.yscale ( 'log' ) plt.grid ( True ) plt.axis ( 'equal' ) plt.savefig ( 'maxerror_test10.png' ) plt.show ( ) # # Plot the H1 error as a function of NE. # fig = plt.figure ( ) plt.plot ( ne_plot, h1_plot, 'bo-' ) plt.xlabel ( '<---NE--->' ) plt.ylabel ( '<---H1(error)--->' ) plt.title ( 'H1 error as function of number of elements' ) plt.xscale ( 'log' ) plt.yscale ( 'log' ) plt.grid ( True ) plt.axis ( 'equal' ) plt.savefig ( 'h1error_test10.png' ) plt.show ( ) # # Terminate. # print ( '' ) print ( 'FEM1D_BVP_QUADRATIC_TEST10' ) print ( ' Normal end of execution.' ) return def a10 ( x ): value = 1.0 return value def c10 ( x ): value = 1.0 return value def exact10 ( x ): import numpy as np value = x - np.sinh ( x ) / np.sinh ( 1.0 ) return value def exactp10 ( x ): import numpy as np value = 1.0 - np.cosh ( x ) / np.sinh ( 1.0 ) return value def f10 ( x ): value = x return value if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) fem1d_bvp_quadratic_test10 ( ) timestamp ( )