Skip to content

Add modern poisson2d solver & rename optimized.f90 #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion poisson2d/optimized.f90 → poisson2d/archaic.f90
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pure real(dp) function rho(x,y)
program poisson
use rhofunc, only: rho
implicit none
integer, parameter :: dp=kind(0.d0), M=300
integer, parameter :: dp=kind(0.d0), M=150
integer :: i,j, iter
real(dp),parameter :: epsilon0=8.85E-12_dp, target=1E-6_dp, a=0.01
real(dp) :: delta, b, e, phiprime(M,M), phi(M,M), a2, rhoarr(M,M)
Expand Down
81 changes: 81 additions & 0 deletions poisson2d/modern.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module rho_interface
!! Define a scalar function over 2-space.
implicit none

private
public :: rho, dp

integer, parameter :: precision = 15, range = 307
integer, parameter :: dp = selected_real_kind(precision, range)

interface
pure real(dp) module function rho(x,y)
!! Poisson equation inhomogeneous term.
implicit none
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question for my knowledge: why is there an implicit none here? Is the implicit none in the module not covering it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is a common mistake that I also learned the hard way --- implicit none in a module propagates and applies to everything except interface, where you have to repeat it.... :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @certik for your answer. I will have to check all my codes ;)

real(dp), intent(in) :: x,y
end function
end interface

end module

submodule(rho_interface) rho_definition
implicit none
contains
module procedure rho
!! To Do: give meaningful names to the magic numbers.
!! See https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants.
if (all([x,y]>0.6_dp .and. [x,y]<0.8_dp)) then
rho = 1._dp
else
rho = merge(-1._dp, 0._dp, all([x,y]>0.2_dp .and. [x,y]<0.4_dp))
end if
end procedure
end submodule

program poisson
!! Solve the 2D Poisson equation using a smoothing operation to iterate.
use rho_interface, only: rho, dp
implicit none

integer i,j
integer, parameter :: M=150
real(dp), parameter :: dx=0.01_dp
real(dp) :: t_start, rho_sampled(M,M)

call cpu_time(t_start)

associate( dy => (dx) ) ! Associating with an expression provides immutable state so dy cannot be inadvertently redefined.

do concurrent(i=1:M, j=1:M)
rho_sampled(i,j) = rho(i*dx,j*dy)
end do

block ! Tighten the scoping to declutter the code above.
real(dp) :: delta_phi, t_end
real(dp), parameter :: epsilon0=8.85E-12_dp, tolerance=1E-6_dp
real(dp), dimension(M,M) :: phi_prime, phi
integer :: iteration

phi = 0

phi_prime([1,M], 2:M-1) = phi([1,M], 2:M-1) ! Initialize only boundary values except corners. (Internal values will
phi_prime(2:M-1, [1,M]) = phi(2:M-1, [1,M]) ! be overwritten in the first iteration. Corners will never be used.)

delta_phi = tolerance + epsilon(tolerance) ! Ensure at least 1 iteration.
iteration = 0
do while (delta_phi > tolerance )
iteration = iteration + 1
phi_prime(2:M-1,2:M-1) = (phi(3:,2:M-1) + phi(:M-2,2:M-1) + phi(2:M-1,3:) + phi(2:M-1,1:M-2))/4._dp &
+ (dx/2._dp)*(dy/2._dp)/epsilon0*rho_sampled(2:M-1,2:M-1)
delta_phi = maxval(abs(phi_prime - phi))
phi(2:M-1, 2:M-1) = phi_prime(2:M-1, 2:M-1) ! Update internal values.
end do

call cpu_time(t_end)
print *, t_end-t_start, iteration

end block

end associate

end program