Last active
May 19, 2018 06:31
-
-
Save sage-git/4502068b64b67bcebc2db15423e25856 to your computer and use it in GitHub Desktop.
Lifegame in fortran
This file contains 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
module lifegame | |
implicit none | |
integer :: W, H | |
integer, allocatable :: field(:, :) | |
integer, allocatable :: neighbors(:, :) | |
contains | |
subroutine init_field(width, height, start_ratio) | |
integer, intent(in) :: width, height | |
double precision, intent(in) :: start_ratio | |
double precision, allocatable :: s(:, :) | |
W = width | |
H = height | |
allocate( field(W, H) ) | |
allocate( s(W, H) ) | |
call random_number(s) | |
field = s + start_ratio | |
allocate( neighbors(W, H) ) | |
end subroutine init_field | |
subroutine print_field() | |
implicit none | |
integer :: n, i | |
do i = 1, H | |
write(*, '(*(i0,:," "))') field(:, i) | |
enddo | |
end subroutine print_field | |
subroutine one_epoch | |
implicit none | |
integer :: dx, dy, i | |
neighbors = 0 | |
do concurrent (dx = -1:1, dy = -1:1) | |
neighbors = neighbors + cshift(cshift(field, dx, 1), dy, 2) | |
enddo | |
where(neighbors == 3) | |
field = 1 | |
elsewhere(neighbors == 4) | |
field = field | |
elsewhere | |
field = 0 | |
endwhere | |
end subroutine one_epoch | |
end module lifegame | |
program flg | |
use lifegame | |
implicit none | |
integer :: epoch, width, height | |
integer :: i | |
write(*, *) "# Width?" | |
read(*, *) width | |
write(*, *) "# Height?" | |
read(*, *) height | |
write(*, *) "# Epoch?" | |
read(*, *) epoch | |
call init_field(width, height, 0.5d0) | |
call print_field | |
do i = 1, epoch | |
call one_epoch | |
write(*, *) | |
write(*, *) | |
call print_field | |
enddo | |
write(*, *) | |
write(*, *) | |
stop | |
end program flg | |
This file contains 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
reset | |
set key below | |
set nozeroaxis | |
set notics | |
unset colorbox | |
set size square | |
set term gif animate size 240,200 | |
set output "field.gif" | |
set palette grey | |
do for [n=0:200:1] { | |
p "field.log" i n mat w ima t sprintf("generation %d", n) | |
} |
This file contains 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
#!/bin/bash | |
gfortran lifegame.f90 | |
echo "64\n64\n512" | ./a.out > field.log | |
gnuplot plotter.plt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment