|
| 1 | +!> Test program for the Gaussian Legendre Quadrature module |
| 2 | +!! |
| 3 | +!! Created by: Your Name (https://github.com/YourGitHub) |
| 4 | +!! in Pull Request: #32 |
| 5 | +!! https://github.com/TheAlgorithms/Fortran/pull/32 |
| 6 | +!! |
| 7 | +!! This program provides test cases to validate the gaussian_legendre_quadrature module against known integral values. |
| 8 | + |
| 9 | +program test_gaussian_legendre_quadrature |
| 10 | + use gaussian_legendre_quadrature |
| 11 | + implicit none |
| 12 | + |
| 13 | + ! Run test cases |
| 14 | + call test_integral_x_squared_0_to_1() |
| 15 | + call test_integral_e_x_0_to_1() |
| 16 | + call test_integral_sin_0_to_pi() |
| 17 | + call test_integral_cos_0_to_pi_over_2() |
| 18 | + call test_integral_1_over_x_1_to_e() |
| 19 | + call test_integral_x_cubed_0_to_1() |
| 20 | + call test_integral_sin_squared_0_to_pi() |
| 21 | + call test_integral_1_over_x_1_to_e() |
| 22 | + |
| 23 | + print *, "All tests completed." |
| 24 | + |
| 25 | +contains |
| 26 | + |
| 27 | + ! Test case 1: ∫ x^2 dx from 0 to 1 (Exact result = 1/3 ≈ 0.3333) |
| 28 | + subroutine test_integral_x_squared_0_to_1() |
| 29 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 30 | + integer :: panels_number |
| 31 | + lower_bound = 0.0_dp |
| 32 | + upper_bound = 1.0_dp |
| 33 | + panels_number = 5 ! Adjust the number of quadrature points as needed from 1 to 5 |
| 34 | + expected = 1.0_dp / 3.0_dp |
| 35 | + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, f_x_squared) |
| 36 | + call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1") |
| 37 | + end subroutine test_integral_x_squared_0_to_1 |
| 38 | + |
| 39 | + ! Test case 2: ∫ e^x dx from 0 to 1 (Exact result = e - 1 ≈ 1.7183) |
| 40 | + subroutine test_integral_e_x_0_to_1() |
| 41 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 42 | + integer :: panels_number |
| 43 | + lower_bound = 0.0_dp |
| 44 | + upper_bound = 1.0_dp |
| 45 | + panels_number = 3 |
| 46 | + expected = exp(1.0_dp) - 1.0_dp |
| 47 | + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, exp_function) |
| 48 | + call assert_test(integral_result, expected, "Test 2: ∫ e^x dx from 0 to 1") |
| 49 | + end subroutine test_integral_e_x_0_to_1 |
| 50 | + |
| 51 | + ! Test case 3: ∫ sin(x) dx from 0 to π (Exact result = 2) |
| 52 | + subroutine test_integral_sin_0_to_pi() |
| 53 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 54 | + integer :: panels_number |
| 55 | + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. |
| 56 | + lower_bound = 0.0_dp |
| 57 | + upper_bound = pi |
| 58 | + panels_number = 5 |
| 59 | + expected = 2.0_dp |
| 60 | + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, sin_function) |
| 61 | + call assert_test(integral_result, expected, "Test 3: ∫ sin(x) dx from 0 to π") |
| 62 | + end subroutine test_integral_sin_0_to_pi |
| 63 | + |
| 64 | + ! Test case 4: ∫ cos(x) dx from 0 to π/2 (Exact result = 1) |
| 65 | + subroutine test_integral_cos_0_to_pi_over_2() |
| 66 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 67 | + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. |
| 68 | + integer :: panels_number |
| 69 | + lower_bound = 0.0_dp |
| 70 | + upper_bound = pi / 2.0_dp |
| 71 | + panels_number = 5 |
| 72 | + expected = 1.0_dp |
| 73 | + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, cos_function) |
| 74 | + call assert_test(integral_result, expected, "Test 4: ∫ cos(x) dx from 0 to π/2") |
| 75 | + end subroutine test_integral_cos_0_to_pi_over_2 |
| 76 | + |
| 77 | + ! Test case 5: ∫ (1/x) dx from 1 to e (Exact result = 1) |
| 78 | + subroutine test_integral_1_over_x_1_to_e() |
| 79 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 80 | + integer :: panels_number |
| 81 | + lower_bound = 1.0_dp |
| 82 | + upper_bound = exp(1.0_dp) |
| 83 | + panels_number = 5 |
| 84 | + expected = 1.0_dp |
| 85 | + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, log_function) |
| 86 | + call assert_test(integral_result, expected, "Test 5: ∫ (1/x) dx from 1 to e") |
| 87 | + end subroutine test_integral_1_over_x_1_to_e |
| 88 | + |
| 89 | + ! Test case 6: ∫ x^3 dx from 0 to 1 (Exact result = 1/4 = 0.25) |
| 90 | + subroutine test_integral_x_cubed_0_to_1() |
| 91 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 92 | + integer :: panels_number |
| 93 | + lower_bound = 0.0_dp |
| 94 | + upper_bound = 1.0_dp |
| 95 | + panels_number = 4 |
| 96 | + expected = 0.25_dp |
| 97 | + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, f_x_cubed) |
| 98 | + call assert_test(integral_result, expected, "Test 6: ∫ x^3 dx from 0 to 1") |
| 99 | + end subroutine test_integral_x_cubed_0_to_1 |
| 100 | + |
| 101 | + ! Test case 7: ∫ sin^2(x) dx from 0 to π (Exact result = π/2 ≈ 1.5708) |
| 102 | + subroutine test_integral_sin_squared_0_to_pi() |
| 103 | + real(dp) :: lower_bound, upper_bound, integral_result, expected |
| 104 | + real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture. |
| 105 | + integer :: panels_number |
| 106 | + lower_bound = 0.0_dp |
| 107 | + upper_bound = pi |
| 108 | + panels_number = 5 |
| 109 | + expected = 1.57084_dp ! Approximate value, adjust tolerance as needed |
| 110 | + call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, panels_number, sin_squared_function) |
| 111 | + call assert_test(integral_result, expected, "Test 7: ∫ sin^2(x) dx from 0 to π") |
| 112 | + end subroutine test_integral_sin_squared_0_to_pi |
| 113 | + |
| 114 | + ! Function for x^2 |
| 115 | + real(dp) function f_x_squared(x) |
| 116 | + real(dp), intent(in) :: x |
| 117 | + f_x_squared = x**2 |
| 118 | + end function f_x_squared |
| 119 | + |
| 120 | + ! Function for e^x |
| 121 | + real(dp) function exp_function(x) |
| 122 | + real(dp), intent(in) :: x |
| 123 | + exp_function = exp(x) |
| 124 | + end function exp_function |
| 125 | + |
| 126 | + ! Function for 1/x |
| 127 | + real(dp) function log_function(x) |
| 128 | + real(dp), intent(in) :: x |
| 129 | + log_function = 1.0_dp / x |
| 130 | + end function log_function |
| 131 | + |
| 132 | + ! Function for cos(x) |
| 133 | + real(dp) function cos_function(x) |
| 134 | + real(dp), intent(in) :: x |
| 135 | + cos_function = cos(x) |
| 136 | + end function cos_function |
| 137 | + |
| 138 | + ! Function for x^3 |
| 139 | + real(dp) function f_x_cubed(x) |
| 140 | + real(dp), intent(in) :: x |
| 141 | + f_x_cubed = x**3 |
| 142 | + end function f_x_cubed |
| 143 | + |
| 144 | + ! Function for sin(x) |
| 145 | + real(dp) function sin_function(x) |
| 146 | + real(dp), intent(in) :: x |
| 147 | + sin_function = sin(x) |
| 148 | + end function sin_function |
| 149 | + |
| 150 | + ! Function for sin^2(x) |
| 151 | + real(dp) function sin_squared_function(x) |
| 152 | + real(dp), intent(in) :: x |
| 153 | + sin_squared_function = sin(x)**2 |
| 154 | + end function sin_squared_function |
| 155 | + |
| 156 | + !> Subroutine to assert the test results |
| 157 | + subroutine assert_test(actual, expected, test_name) |
| 158 | + real(dp), intent(in) :: actual, expected |
| 159 | + character(len=*), intent(in) :: test_name |
| 160 | + real(dp), parameter :: tol = 1.0e-5_dp |
| 161 | + |
| 162 | + if (abs(actual - expected) < tol) then |
| 163 | + print *, test_name, " PASSED" |
| 164 | + else |
| 165 | + print *, test_name, " FAILED" |
| 166 | + print *, " Expected: ", expected |
| 167 | + print *, " Got: ", actual |
| 168 | + stop 1 |
| 169 | + end if |
| 170 | + end subroutine assert_test |
| 171 | + |
| 172 | +end program test_gaussian_legendre_quadrature |
0 commit comments