Created
November 28, 2018 08:05
-
-
Save komasaru/013448f69b63dc8cfc578061bd9845d6 to your computer and use it in GitHub Desktop.
Fortran 95 source code to test cellular-automaton implementation.(Ver.2)
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
!**************************************************** | |
! セル・オートマトン(C 言語との連携バージョン) | |
! | |
! 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 = 110 ! 遷移規則 (90, 30, 110, 184) | |
real(8), parameter:: density = 0.01 ! 初期状態密度 | |
integer, parameter:: W = 800, H = 750 ! 横幅,縦長さ | |
integer(1) :: a(1:W), s(1:W) ! セル状態 a, 近傍状態 s | |
integer :: i | |
call random_array(a, W, density) ! 初期状態を a にセット | |
call put_head(W, H + 1, 1) ! 出力初期化ルーチン (C) | |
call put_raw(a, W) ! 出力ルーチン (C) | |
do i = 1, H ! H 回遷移を繰り返し | |
s = cshift(a,-1)*4 + a*2 + cshift(a,1) ! 近傍状態計算 | |
a = merge(1, 0, btest(rule, s)) ! 遷移実行 | |
call put_raw(a, W) ! 出力ルーチン (C) | |
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 |
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
/* | |
* PGM 出力用 | |
*/ | |
#include <stdio.h> | |
int Width, Height, Max; | |
void put_head_(int *width, int *height, int *max){ | |
printf("P5\n%d %d\n%d\n", *width, *height, *max); | |
} | |
void put_raw_(char *a, int *width){ | |
fwrite(a, 1, *width, stdout); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment