Skip to content

Commit

Permalink
fprettify styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramy-Badr-Ahmed committed Sep 24, 2024
1 parent fdce495 commit 12016aa
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions examples/maths/numerical_integration/gaussian_legendre.f90
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ program example_gaussian_quadrature
! Call Gaussian quadrature to compute the integral
call gauss_legendre_quadrature(integral_result, a, b, n, func)

write(*, '(A, F12.6)') "Gaussian Quadrature result: ", integral_result !! ≈ 0.858574
write (*, '(A, F12.6)') "Gaussian Quadrature result: ", integral_result !! ≈ 0.858574

contains

Expand All @@ -31,7 +31,7 @@ function func(x) result(fx)
real(dp), intent(in) :: x
real(dp) :: fx

fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate
fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate
end function func

end program example_gaussian_quadrature
4 changes: 2 additions & 2 deletions examples/maths/numerical_integration/midpoint.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ program example_midpoint
! Call the midpoint rule subroutine with the function passed as an argument
call midpoint(integral_result, a, b, n, func)

write(*, '(A, F12.6)') "Midpoint rule yields: ", integral_result !! ≈ 0.858196
write (*, '(A, F12.6)') "Midpoint rule yields: ", integral_result !! ≈ 0.858196

contains

Expand All @@ -30,7 +30,7 @@ function func(x) result(fx)
real(dp), intent(in) :: x
real(dp) :: fx

fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate
fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate
end function func

end program example_midpoint
2 changes: 1 addition & 1 deletion examples/maths/numerical_integration/monte_carlo.f90
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ program example_monte_carlo
! Call Monte Carlo integration
call monte_carlo(integral_result, error_estimate, a, b, n, func)

write(*, '(A, F12.6, A, F12.6)') "Monte Carlo result: ", integral_result, " +- ", error_estimate !! ≈ 0.858421
write (*, '(A, F12.6, A, F12.6)') "Monte Carlo result: ", integral_result, " +- ", error_estimate !! ≈ 0.858421

contains

Expand Down
4 changes: 2 additions & 2 deletions examples/maths/numerical_integration/simpson.f90
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ program example_simpson
! Call Simpson's rule with the function passed as an argument
call simpson(integral_result, a, b, n, func)

write(*, '(A, F12.8)') "Simpson's rule yields: ", integral_result !! ≈ 0.85819555
write (*, '(A, F12.8)') "Simpson's rule yields: ", integral_result !! ≈ 0.85819555

contains

Expand All @@ -31,7 +31,7 @@ function func(x) result(fx)
real(dp), intent(in) :: x
real(dp) :: fx

fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate
fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate
end function func

end program example_simpson
4 changes: 2 additions & 2 deletions examples/maths/numerical_integration/trapezoid.f90
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ program example_tapezoid
! Call the trapezoidal rule with the function passed as an argument
call trapezoid(integral_result, a, b, n, func)

write(*, '(A, F12.6)') 'Trapezoidal rule yields: ', integral_result !! ≈ 0.858195
write (*, '(A, F12.6)') 'Trapezoidal rule yields: ', integral_result !! ≈ 0.858195

contains

Expand All @@ -31,7 +31,7 @@ function func(x) result(fx)
real(dp), intent(in) :: x
real(dp) :: fx

fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate
fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate
end function func

end program example_tapezoid
10 changes: 5 additions & 5 deletions modules/maths/numerical_integration/gaussian_legendre.f90
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ end function func
call gauss_legendre_weights(t, w, n)

! Allocate the function value array
allocate(fx(n))
allocate (fx(n))

! Transform the nodes from the reference interval [-1, 1] to [a, b]
x = (b + a) / 2.0_dp + (b - a) * t / 2.0_dp
x = (b + a)/2.0_dp + (b - a)*t/2.0_dp

! Compute function values at the transformed points
do i = 1, n
fx(i) = func(x(i))
end do

! Apply the Gaussian-Legendre quadrature formula
integral_result = sum(w * fx) * (b - a) / 2.0_dp
integral_result = sum(w*fx)*(b - a)/2.0_dp

! Deallocate fx array
deallocate(fx)
deallocate (fx)

end subroutine gauss_legendre_quadrature

Expand All @@ -74,7 +74,7 @@ subroutine gauss_legendre_weights(t, w, n)
real(dp), intent(out), dimension(n) :: t, w !! Nodes (t) and weights (w)

! Predefined nodes and weights for different values of n
select case(n)
select case (n)
case (1)
t = [0.0_dp] !! Single node at the center for n = 1
w = [2.0_dp] !! Weight of 2 for the single point
Expand Down
10 changes: 5 additions & 5 deletions modules/maths/numerical_integration/midpoint.f90
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@ end function func
end interface

! Step size
h = (b - a) / (1.0_dp*n)
h = (b - a)/(1.0_dp*n)

! Allocate array for midpoints
allocate(x(1:n), fx(1:n))
allocate (x(1:n), fx(1:n))

! Calculate midpoints
x = [(a + (i - 0.5_dp) * h, i = 1, n)]
x = [(a + (i - 0.5_dp)*h, i=1, n)]

! Apply function to each midpoint
do i = 1, n
fx(i) = func(x(i))
end do

! Final integral value
integral_result = h * sum(fx)
integral_result = h*sum(fx)

! Deallocate arrays
deallocate(x, fx)
deallocate (x, fx)

end subroutine midpoint

Expand Down
10 changes: 5 additions & 5 deletions modules/maths/numerical_integration/monte_carlo.f90
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ end function func
end interface

! Allocate arrays for random samples and function values
allocate(uniform_sample(1:n), fx(1:n))
allocate (uniform_sample(1:n), fx(1:n))

! Generate uniform random points in [a, b]
call random_number(uniform_sample)
uniform_sample = a + (b - a) * uniform_sample !! Scale to the interval [a, b]
uniform_sample = a + (b - a)*uniform_sample !! Scale to the interval [a, b]

! Evaluate the function at all random points in parallel
!$omp parallel do !! OpenMP parallelization to distribute the loop across multiple threads
Expand All @@ -61,13 +61,13 @@ end function func
sum_fx_squared = sum(fx**2)

! Compute the Monte Carlo estimate of the integral
integral_result = (b - a) * (sum_fx / real(n, dp))
integral_result = (b - a)*(sum_fx/real(n, dp))

! Estimate the error using the variance of the function values
error_estimate = sqrt((sum_fx_squared/n - (sum_fx/n)**2) / (n-1)) * (b-a)
error_estimate = sqrt((sum_fx_squared/n - (sum_fx/n)**2)/(n - 1))*(b - a)

! Deallocate arrays
deallocate(uniform_sample, fx)
deallocate (uniform_sample, fx)

end subroutine monte_carlo

Expand Down
12 changes: 6 additions & 6 deletions modules/maths/numerical_integration/simpson.f90
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,29 @@ end function func

! Check if n is even
if (mod(n, 2) /= 0) then
write(*, *) 'Error: The number of panels (n) must be even.'
write (*, *) 'Error: The number of panels (n) must be even.'
stop
end if

! Step size
h = (b - a) / (1.0_dp*n)
h = (b - a)/(1.0_dp*n)

! Allocate arrays
allocate(x(0:n), fx(0:n))
allocate (x(0:n), fx(0:n))

! Create an array of x values, contains the endpoints and the midpoints.
x = [(a + i * h, i = 0, n)]
x = [(a + i*h, i=0, n)]

! Apply the function to each x value
do i = 0, n
fx(i) = func(x(i))
end do

! Apply Simpson's rule using array slicing
integral_result = (fx(0) + fx(n) + 4.0_dp * sum(fx(1: n-1: 2)) + 2.0_dp * sum(fx(2: n-2: 2))) * (h / 3.0_dp)
integral_result = (fx(0) + fx(n) + 4.0_dp*sum(fx(1:n - 1:2)) + 2.0_dp*sum(fx(2:n - 2:2)))*(h/3.0_dp)

! Deallocate arrays
deallocate(x, fx)
deallocate (x, fx)
end subroutine simpson

end module simpson_rule
10 changes: 5 additions & 5 deletions modules/maths/numerical_integration/trapezoid.f90
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,24 @@ end function func
end interface

! Step size
h = (b - a) / (1.0_dp*n)
h = (b - a)/(1.0_dp*n)

! Allocate arrays
allocate(x(0:n), fx(0:n))
allocate (x(0:n), fx(0:n))

! Create an array of x values
x = [(a + i * h, i = 0, n)]
x = [(a + i*h, i=0, n)]

! Apply the function to each x value
do i = 0, n
fx(i) = func(x(i))
end do

! Apply trapezoidal rule using array slicing
integral_result = ((fx(0) + fx(n)) * 0.5_dp + sum(fx(1: n))) * h
integral_result = ((fx(0) + fx(n))*0.5_dp + sum(fx(1:n)))*h

! Deallocate arrays
deallocate(x, fx)
deallocate (x, fx)
end subroutine trapezoid

end module trapezoidal_rule

0 comments on commit 12016aa

Please sign in to comment.