-
Notifications
You must be signed in to change notification settings - Fork 11
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
39c006a
a7ccf54
da910c0
648739c
f8768cd
5563ae2
7b63bc9
f5ec218
bd22c45
fcb0cd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||||||
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 | ||||||||||
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) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am a fan of concise code, so I agree with Milan. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the above cases where But here 2 is sufficient There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I recommend to always write floating point numbers as |
||||||||||
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 |
There was a problem hiding this comment.
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 theimplicit none
in the module not covering it?There was a problem hiding this comment.
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 exceptinterface
, where you have to repeat it.... :(There was a problem hiding this comment.
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 ;)