-
Notifications
You must be signed in to change notification settings - Fork 10
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
rouson
wants to merge
10
commits into
fortran-lang:main
Choose a base branch
from
rouson:modern-poisson2d
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
39c006a
reduce poisson2d optimized.f90 problem size
a7ccf54
feat: add modernized poisson2d solver
da910c0
rename optimized.f90 to archaic.f90
648739c
feat: make dy immutable via expression-association
f8768cd
Update poisson2d/modern.f90
5563ae2
Update poisson2d/modern.f90
7b63bc9
Update poisson2d/modern.f90
f5ec218
Update poisson2d/modern.f90
bd22c45
Update poisson2d/modern.f90
fcb0cd0
Update poisson2d/modern.f90
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 ;)