Last active
November 28, 2018 08:04
-
-
Save komasaru/8145f797c53fd17abe25743323d716e6 to your computer and use it in GitHub Desktop.
Fortran 95 source code to test cellular-automaton implementation.
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
!**************************************************** | |
! セル・オートマトン | |
! | |
! date name version | |
! 2018.09.14 mk-mode.com 1.00 新規作成 | |
! | |
! Copyright(C) 2018 mk-mode.com All Rights Reserved. | |
!**************************************************** | |
! | |
program CellAtm | |
implicit none | |
integer, parameter:: rule = 90 ! 遷移規則 (90, 30, 110, 184) | |
real(8), parameter:: density = 0.04 ! 初期状態密度 | |
integer, parameter:: W = 78, H = 20 ! 横幅,縦長さ | |
integer(1) :: a(1:W), s(1:W) ! セル状態 a, 近傍状態 s | |
integer :: i | |
call random_array(a, W, density) ! 初期状態を a にセット | |
print *, merge('X', ' ', a/=0) ! 表示 1 -> X | |
do i = 1, H ! H 回遷移を繰り返し | |
s(1) = a(W) * 4 + a(1) * 2 + a(2) ! 左端の近傍状態 | |
s(W) = a(W-1) * 4 + a(W) * 2 + a(1) ! 右端の近傍状態 | |
s(2:W-1) = a(1:W-2) * 4 + a(2:W-1) * 2 + a(3:W) ! 他の近傍状態 | |
a = merge(1, 0, btest(rule, s)) ! 遷移実行 | |
print *, merge('X', ' ', a/=0) ! 表示 1 -> X | |
end do | |
stop | |
contains | |
subroutine random_array(a, n, densty ) | |
integer :: n | |
integer(1) :: a(1:n) | |
real(8) :: densty | |
integer :: ck, sz, i | |
real(8) :: rnd(1:n) | |
call system_clock(ck) ! クロック値 ( 毎回違う ) を取得 | |
call random_seed(size=sz) ! 乱数シードの数を取得 | |
call random_seed(put=(/(ck+i, i=1,sz)/)) ! 乱数初期値変更 | |
call random_number(rnd) ! rnd に n 個の乱数をセット | |
a = merge(1, 0, rnd < densty) ! a に密度 densty の 1 をセット | |
end subroutine random_array | |
end program CellAtm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment