Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created November 28, 2018 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/013448f69b63dc8cfc578061bd9845d6 to your computer and use it in GitHub Desktop.
Save komasaru/013448f69b63dc8cfc578061bd9845d6 to your computer and use it in GitHub Desktop.
Fortran 95 source code to test cellular-automaton implementation.(Ver.2)
!****************************************************
! セル・オートマトン(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
/*
* 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