|
| 1 | +module m_finite_differences |
| 2 | + |
| 3 | + use m_global_parameters |
| 4 | + |
| 5 | + implicit none |
| 6 | + |
| 7 | +contains |
| 8 | + |
| 9 | + subroutine s_compute_fd_divergence(div, fields, ix_s, iy_s, iz_s) |
| 10 | + |
| 11 | + type(scalar_field), intent(INOUT) :: div |
| 12 | + type(scalar_field), intent(IN) :: fields(1:3) |
| 13 | + type(int_bounds_info), intent(IN) :: ix_s, iy_s, iz_s |
| 14 | + |
| 15 | + integer :: x, y, z !< Generic loop iterators |
| 16 | + |
| 17 | + real(kind(0d0)) :: divergence |
| 18 | + |
| 19 | + !$acc parallel loop collapse(3) private(divergence) |
| 20 | + do x = ix_s%beg, ix_s%end |
| 21 | + do y = iy_s%beg, iy_s%end |
| 22 | + do z = iz_s%beg, iz_s%end |
| 23 | + |
| 24 | + if (x == ix_s%beg) then |
| 25 | + divergence = (-3d0*fields(1)%sf(x, y, z) + 4d0*fields(1)%sf(x + 1, y, z) - fields(1)%sf(x + 2, y, z))/(x_cc(x + 2) - x_cc(x)) |
| 26 | + else if (x == ix_s%end) then |
| 27 | + divergence = (+3d0*fields(1)%sf(x, y, z) - 4d0*fields(1)%sf(x - 1, y, z) + fields(1)%sf(x - 2, y, z))/(x_cc(x) - x_cc(x - 2)) |
| 28 | + else |
| 29 | + divergence = (fields(1)%sf(x + 1, y, z) - fields(1)%sf(x - 1, y, z))/(x_cc(x + 1) - x_cc(x - 1)) |
| 30 | + end if |
| 31 | + |
| 32 | + if (n > 0) then |
| 33 | + if (y == iy_s%beg) then |
| 34 | + divergence = divergence + (-3d0*fields(2)%sf(x, y, z) + 4d0*fields(2)%sf(x, y + 1, z) - fields(2)%sf(x, y + 2, z))/(y_cc(y + 2) - y_cc(y)) |
| 35 | + else if (y == iy_s%end) then |
| 36 | + divergence = divergence + (+3d0*fields(2)%sf(x, y, z) - 4d0*fields(2)%sf(x, y - 1, z) + fields(2)%sf(x, y - 2, z))/(y_cc(y) - y_cc(y - 2)) |
| 37 | + else |
| 38 | + divergence = divergence + (fields(2)%sf(x, y + 1, z) - fields(2)%sf(x, y - 1, z))/(y_cc(y + 1) - y_cc(y - 1)) |
| 39 | + end if |
| 40 | + end if |
| 41 | + |
| 42 | + if (p > 0) then |
| 43 | + if (z == iz_s%beg) then |
| 44 | + divergence = divergence + (-3d0*fields(3)%sf(x, y, z) + 4d0*fields(3)%sf(x, y, z + 1) - fields(3)%sf(x, y, z + 2))/(z_cc(z + 2) - z_cc(z)) |
| 45 | + else if (z == iz_s%end) then |
| 46 | + divergence = divergence + (+3d0*fields(3)%sf(x, y, z) - 4d0*fields(3)%sf(x, y, z - 1) + fields(2)%sf(x, y, z - 2))/(z_cc(z) - z_cc(z - 2)) |
| 47 | + else |
| 48 | + divergence = divergence + (fields(3)%sf(x, y, z + 1) - fields(3)%sf(x, y, z - 1))/(z_cc(z + 1) - z_cc(z - 1)) |
| 49 | + end if |
| 50 | + end if |
| 51 | + |
| 52 | + div%sf(x, y, z) = div%sf(x, y, z) + divergence |
| 53 | + |
| 54 | + end do |
| 55 | + end do |
| 56 | + end do |
| 57 | + |
| 58 | + end subroutine s_compute_fd_divergence |
| 59 | + |
| 60 | + !> The purpose of this subroutine is to compute the finite- |
| 61 | + !! difference coefficients for the centered schemes utilized |
| 62 | + !! in computations of first order spatial derivatives in the |
| 63 | + !! s-coordinate direction. The s-coordinate direction refers |
| 64 | + !! to the x-, y- or z-coordinate direction, depending on the |
| 65 | + !! subroutine's inputs. Note that coefficients of up to 4th |
| 66 | + !! order accuracy are available. |
| 67 | + !! @param q Number of cells in the s-coordinate direction |
| 68 | + !! @param s_cc Locations of the cell-centers in the s-coordinate direction |
| 69 | + !! @param fd_coeff_s Finite-diff. coefficients in the s-coordinate direction |
| 70 | + subroutine s_compute_finite_difference_coefficients(q, s_cc, fd_coeff_s, buff_size, & |
| 71 | + fd_number_in, fd_order_in, offset_s) |
| 72 | +
|
| 73 | + integer :: lB, lE !< loop bounds |
| 74 | + integer, intent(IN) :: q |
| 75 | + integer, intent(IN) :: buff_size, fd_number_in, fd_order_in |
| 76 | + type(int_bounds_info), optional, intent(IN) :: offset_s |
| 77 | + real(kind(0d0)), allocatable, dimension(:, :), intent(INOUT) :: fd_coeff_s |
| 78 | +
|
| 79 | + real(kind(0d0)), & |
| 80 | + dimension(-buff_size:q + buff_size), & |
| 81 | + intent(IN) :: s_cc |
| 82 | +
|
| 83 | + integer :: i !< Generic loop iterator |
| 84 | +
|
| 85 | + if (present(offset_s)) then |
| 86 | + lB = -offset_s%beg |
| 87 | + lE = q + offset_s%end |
| 88 | + else |
| 89 | + lB = 0 |
| 90 | + lE = q |
| 91 | + end if |
| 92 | +
|
| 93 | + if (allocated(fd_coeff_s)) deallocate (fd_coeff_s) |
| 94 | + allocate (fd_coeff_s(-fd_number_in:fd_number_in, lb:lE)) |
| 95 | +
|
| 96 | + ! Computing the 1st order finite-difference coefficients |
| 97 | + if (fd_order_in == 1) then |
| 98 | + do i = lB, lE |
| 99 | + fd_coeff_s(-1, i) = 0d0 |
| 100 | + fd_coeff_s(0, i) = -1d0/(s_cc(i + 1) - s_cc(i)) |
| 101 | + fd_coeff_s(1, i) = -fd_coeff_s(0, i) |
| 102 | + end do |
| 103 | +
|
| 104 | + ! Computing the 2nd order finite-difference coefficients |
| 105 | + elseif (fd_order_in == 2) then |
| 106 | + do i = lB, lE |
| 107 | + fd_coeff_s(-1, i) = -1d0/(s_cc(i + 1) - s_cc(i - 1)) |
| 108 | + fd_coeff_s(0, i) = 0d0 |
| 109 | + fd_coeff_s(1, i) = -fd_coeff_s(-1, i) |
| 110 | + end do |
| 111 | +
|
| 112 | + ! Computing the 4th order finite-difference coefficients |
| 113 | + else |
| 114 | + do i = lB, lE |
| 115 | + fd_coeff_s(-2, i) = 1d0/(s_cc(i - 2) - 8d0*s_cc(i - 1) - s_cc(i + 2) + 8d0*s_cc(i + 1)) |
| 116 | + fd_coeff_s(-1, i) = -8d0*fd_coeff_s(-2, i) |
| 117 | + fd_coeff_s(0, i) = 0d0 |
| 118 | + fd_coeff_s(1, i) = -fd_coeff_s(-1, i) |
| 119 | + fd_coeff_s(2, i) = -fd_coeff_s(-2, i) |
| 120 | + end do |
| 121 | +
|
| 122 | + end if |
| 123 | +
|
| 124 | + end subroutine s_compute_finite_difference_coefficients ! -------------- |
| 125 | +
|
| 126 | +end module m_finite_differences |
| 127 | +
|
0 commit comments