Skip to content
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

Add modern poisson2d solver & rename optimized.f90 #23

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
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
83 changes: 83 additions & 0 deletions poisson2d/modern.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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., 0., all([x,y]>0.2_dp .and. [x,y]<0.4_dp))
rouson marked this conversation as resolved.
Show resolved Hide resolved
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
rouson marked this conversation as resolved.
Show resolved Hide resolved

phi = 0.
rouson marked this conversation as resolved.
Show resolved Hide resolved

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
do concurrent(i=2:M-1, j=2:M-1) ! Compute updated solution estimate at internal points.
phi_prime(i,j) = (phi(i+1,j) + phi(i-1,j) + phi(i,j+1) + phi(i,j-1))/4._dp &
+ (dx/2._dp)*(dy/2._dp)/epsilon0*rho_sampled(i,j)
Copy link
Member

@milancurcic milancurcic Jun 29, 2021

Choose a reason for hiding this comment

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

The _dp suffix for a few literal constants here is unnecessary (let the compiler coerce to correct precision). And, if you wanted to switch to single precision, you'd only need to change it in the declaration of phi and other arrays, and not fiddle with literal constants' precisions here.

Suggested change
phi_prime(i,j) = (phi(i+1,j) + phi(i-1,j) + phi(i,j+1) + phi(i,j-1))/4._dp &
+ (dx/2._dp)*(dy/2._dp)/epsilon0*rho_sampled(i,j)
phi_prime(i,j) = (phi(i+1,j) + phi(i-1,j) + phi(i,j+1) + phi(i,j-1))/4 &
+ (dx/2)*(dy/2)/epsilon0*rho_sampled(i,j)

Copy link
Author

Choose a reason for hiding this comment

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

I don't know the rules on expression evaluation well. I was guessing that the _dp is required to avoid getting a default-precision result for the division. I'm fine with changing it. but GitHub is marking this review comment as outdated so I think I'll have to make the change locally and push it.

Copy link
Member

Choose a reason for hiding this comment

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

I am a fan of concise code, so I agree with Milan. :)

Copy link

Choose a reason for hiding this comment

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

In the above cases where 2 is an integer it doesn't matter. However when you are dealing with 0.213535 then things gets interesting since it first gets interpreted as a real(single), then gets cast to a real(double) which is not what you want. :)

But here 2 is sufficient int -> double is perfect representation.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I recommend to always write floating point numbers as 0.5_dp, whether or not they can be represented exactly. That way one cannot make a mistake and it's a simple rule to follow. Floating point divided by an integer is guaranteed to be a floating point, so there I prefer to just use an integer 2 instead of 2.0_dp.

end do
rouson marked this conversation as resolved.
Show resolved Hide resolved
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