laplace2d.f90 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ! Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  2. !
  3. ! Redistribution and use in source and binary forms, with or without
  4. ! modification, are permitted provided that the following conditions
  5. ! are met:
  6. ! * Redistributions of source code must retain the above copyright
  7. ! notice, this list of conditions and the following disclaimer.
  8. ! * Redistributions in binary form must reproduce the above copyright
  9. ! notice, this list of conditions and the following disclaimer in the
  10. ! documentation and/or other materials provided with the distribution.
  11. ! * Neither the name of NVIDIA CORPORATION nor the names of its
  12. ! contributors may be used to endorse or promote products derived
  13. ! from this software without specific prior written permission.
  14. !
  15. ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  16. ! EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. ! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. ! PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. ! CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. ! EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. ! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. ! PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. ! OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. ! (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. ! OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. module laplace2d
  27. public :: initialize
  28. public :: calcNext
  29. public :: swap
  30. public :: dealloc
  31. contains
  32. subroutine initialize(A, Anew, m, n)
  33. integer, parameter :: fp_kind=kind(1.0d0)
  34. real(fp_kind),allocatable,intent(out) :: A(:,:)
  35. real(fp_kind),allocatable,intent(out) :: Anew(:,:)
  36. integer,intent(in) :: m, n
  37. allocate ( A(0:n-1,0:m-1), Anew(0:n-1,0:m-1) )
  38. A = 0.0_fp_kind
  39. Anew = 0.0_fp_kind
  40. A(0,:) = 1.0_fp_kind
  41. Anew(0,:) = 1.0_fp_kind
  42. end subroutine initialize
  43. function calcNext(A, Anew, m, n)
  44. integer, parameter :: fp_kind=kind(1.0d0)
  45. real(fp_kind),intent(inout) :: A(0:n-1,0:m-1)
  46. real(fp_kind),intent(inout) :: Anew(0:n-1,0:m-1)
  47. integer,intent(in) :: m, n
  48. integer :: i, j
  49. real(fp_kind) :: error
  50. error=0.0_fp_kind
  51. do j=1,m-2
  52. do i=1,n-2
  53. Anew(i,j) = 0.25_fp_kind * ( A(i+1,j ) + A(i-1,j ) + &
  54. A(i ,j-1) + A(i ,j+1) )
  55. error = max( error, abs(Anew(i,j)-A(i,j)) )
  56. end do
  57. end do
  58. calcNext = error
  59. end function calcNext
  60. subroutine swap(A, Anew, m, n)
  61. integer, parameter :: fp_kind=kind(1.0d0)
  62. real(fp_kind),intent(out) :: A(0:n-1,0:m-1)
  63. real(fp_kind),intent(in) :: Anew(0:n-1,0:m-1)
  64. integer,intent(in) :: m, n
  65. integer :: i, j
  66. do j=1,m-2
  67. do i=1,n-2
  68. A(i,j) = Anew(i,j)
  69. end do
  70. end do
  71. end subroutine swap
  72. subroutine dealloc(A, Anew)
  73. integer, parameter :: fp_kind=kind(1.0d0)
  74. real(fp_kind),allocatable,intent(in) :: A
  75. real(fp_kind),allocatable,intent(in) :: Anew
  76. deallocate (A,Anew)
  77. end subroutine
  78. end module laplace2d