【javascript】「自己複製反応(グレイスコットモデル)」のコード

<head>
<title>グレイスコットモデル</title>

<script type=”text/javascript”>
var ctx;
var i;
var j;
var dt=1;
var h=2.5/256;
var du=2.0E-5;
var dv=1.0E-5;
var tmp;
var F=0.024;
var k=0.06;
var imax=100;

var u = [];
for(var i=0; i<100; i++){
u[i] = [];
for(var j=0; j<100; j++){
u[i][j] = 0;
}
}

var v = [];
for(var i=0; i<100; i++){
v[i] = [];
for(var j=0; j<100; j++){
v[i][j] = 0;
}
}

for ( i = 0; i < 100; i++ ){
for ( j = 0; j < 100; j++ ){
if(i<=10 && j<=10){
u[i][j]=0.5;
v[i][j]=0.25;
}
else{u[i][j]=1;v[i][j]=0;}
}
}

function init(){
F=0.024;
var canvas = document.getElementById(‘screen1’);
if (canvas.getContext){
ctx = canvas.getContext(‘2d’);
setInterval(draw, 10);
}
}

function change(){
F=0.04;

var canvas = document.getElementById(‘screen1’);
if (canvas.getContext){
ctx = canvas.getContext(‘2d’);
setInterval(draw, 10);

}
}

function draw(){

ctx.clearRect ( 0 , 0 , 300 , 300 );

for ( i = 1; i < imax-1; i++ ){
for ( j = 1; j < imax-1; j++ ){
tmp = (u[i-1][j]+u[i+1][j]+u[i][j-1]+u[i][j+1]-4*u[i][j])/(h*h);
u[i][j] = u[i][j]+dt*(du*tmp-u[i][j]*v[i][j]*v[i][j]+F*(1-u[i][j]));
tmp = (v[i-1][j]+v[i+1][j]+v[i][j-1]+v[i][j+1]-4*v[i][j])/(h*h);
v[i][j] = v[i][j]+dt*(dv*tmp+u[i][j]*v[i][j]*v[i][j]-(F+k)*v[i][j]);
}
}

i = 0;
for ( j = 1; j < imax-1; j++ ){
tmp = (2*u[i+1][j]+u[i][j-1]+u[i][j+1]-4*u[i][j])/(h*h);
u[i][j] = u[i][j]+dt*(du*tmp-u[i][j]*v[i][j]*v[i][j]+F*(1-u[i][j]));
tmp = (2*v[i+1][j]+v[i][j-1]+v[i][j+1]-4*v[i][j])/(h*h);
v[i][j] = v[i][j]+dt*(dv*tmp+u[i][j]*v[i][j]*v[i][j]-(F+k)*v[i][j]);
}

i = imax-1;
for ( j = 1; j < imax-1; j++ ){
tmp = (2*u[i-1][j]+u[i][j-1]+u[i][j+1]-4*u[i][j])/(h*h);
u[i][j] = u[i][j]+dt*(du*tmp-u[i][j]*v[i][j]*v[i][j]+F*(1-u[i][j]));
tmp = (2*v[i-1][j]+v[i][j-1]+v[i][j+1]-4*v[i][j])/(h*h);
v[i][j] = v[i][j]+dt*(dv*tmp+u[i][j]*v[i][j]*v[i][j]-(F+k)*v[i][j]);
}

j = 0;
for ( i = 1; i < imax-1; i++ ){
tmp = (u[i-1][j]+u[i+1][j]+2*u[i][j+1]-4*u[i][j])/(h*h);
u[i][j] = u[i][j]+dt*(du*tmp-u[i][j]*v[i][j]*v[i][j]+F*(1-u[i][j]));
tmp = (v[i-1][j]+v[i+1][j]+2*v[i][j+1]-4*v[i][j])/(h*h);
v[i][j] = v[i][j]+dt*(dv*tmp+u[i][j]*v[i][j]*v[i][j]-(F+k)*v[i][j]);
}

j = imax-1;
for ( i = 1; i < imax-1; i++ ){
tmp = (u[i-1][j]+u[i+1][j]+2*u[i][j-1]-4*u[i][j])/(h*h);
u[i][j] = u[i][j]+dt*(du*tmp-u[i][j]*v[i][j]*v[i][j]+F*(1-u[i][j]));
tmp = (v[i-1][j]+v[i+1][j]+2*v[i][j-1]-4*v[i][j])/(h*h);
v[i][j] = v[i][j]+dt*(dv*tmp+u[i][j]*v[i][j]*v[i][j]-(F+k)*v[i][j]);
}

i = 0; j = 0;
tmp = (2*u[i+1][j]+2*u[i][j+1]-4*u[i][j])/(h*h);
u[i][j] = u[i][j]+dt*(du*tmp-u[i][j]*v[i][j]*v[i][j]+F*(1-u[i][j]));
tmp = (2*v[i+1][j]+2*v[i][j+1]-4*v[i][j])/(h*h);
v[i][j] = v[i][j]+dt*(dv*tmp+u[i][j]*v[i][j]*v[i][j]-(F+k)*v[i][j]);

i = 0; j = imax-1;
tmp = (2*u[i+1][j]+2*u[i][j-1]-4*u[i][j])/(h*h);
u[i][j] = u[i][j]+dt*(du*tmp-u[i][j]*v[i][j]*v[i][j]+F*(1-u[i][j]));
tmp = (2*v[i+1][j]+2*v[i][j-1]-4*v[i][j])/(h*h);
v[i][j] = v[i][j]+dt*(dv*tmp+u[i][j]*v[i][j]*v[i][j]-(F+k)*v[i][j]);

i = imax-1; j = 0;
tmp = (2*u[i-1][j]+2*u[i][j+1]-4*u[i][j])/(h*h);
u[i][j] = u[i][j]+dt*(du*tmp-u[i][j]*v[i][j]*v[i][j]+F*(1-u[i][j]));
tmp = (2*v[i-1][j]+2*v[i][j+1]-4*v[i][j])/(h*h);
v[i][j] = v[i][j]+dt*(dv*tmp+u[i][j]*v[i][j]*v[i][j]-(F+k)*v[i][j]);

i = imax-1; j = imax-1;
tmp = (2*u[i-1][j]+2*u[i][j-1]-4*u[i][j])/(h*h);
u[i][j] = u[i][j]+dt*(du*tmp-u[i][j]*v[i][j]*v[i][j]+F*(1-u[i][j]));
tmp = (2*v[i-1][j]+2*v[i][j-1]-4*v[i][j])/(h*h);
v[i][j] = v[i][j]+dt*(dv*tmp+u[i][j]*v[i][j]*v[i][j]-(F+k)*v[i][j]);

for ( i = 0; i < imax; i++ ){
for ( j = 0; j < imax; j++ ){

if(u[i][j]<0) {u[i][j]=0;}
else if(u[i][j]>1){u[i][j]=1;}
else {u[i][j]=u[i][j];}
if(v[i][j]<0) {v[i][j]=0;}
else if(v[i][j]>1){v[i][j]=1;}
else {v[i][j]=v[i][j];}
}
}

for ( i = 0; i < 100; i++ ){
for ( j = 0; j < 100; j++ ){

if(v[i][j]<0) {ctx.fillStyle = ‘rgb(00,00,00)’;}
else if(0<=v[i][j] & v[i][j]<0.1) {ctx.fillStyle = ‘rgb(255,00,00)’;}
else if(0.1<=v[i][j] & v[i][j]<0.2) {ctx.fillStyle = ‘rgb(255,255,00)’;}
else if(0.2<=v[i][j] & v[i][j]<0.25) {ctx.fillStyle = ‘rgb(00,255,00)’;}
else {ctx.fillStyle = ‘rgb(00,00,255)’;}

ctx.fillRect(i*3,j*3,3,3); //四角形
ctx.fill(); //色指定

}
}

}
</script>

</head>

<body>

<canvas id=”screen1″ width=”300″ height=”300″></canvas>

<input type=”button” value=”dot” onclick=”init()”>
<input type=”button” value=”striped” onclick=”change()”>

</body>

</html>

  • このエントリーをはてなブックマークに追加

コメントをどうぞ

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください