Comments (1)
Colaboratory上ではPyQtが動作せず、vispyのWebGL backendはまだ完成していないようで利用できませんでしたが、matplotlibを利用して alifebook_lib.visualizer
に相当する描画部分を置き換えることでColaboratory上で動作させることができました。
# alife_book_src/chap02/gray_scott.py を編集
import sys, os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.animation as animation
from matplotlib import rc
# シミュレーションの各パラメタ
SPACE_GRID_SIZE = 256
dx = 0.01
dt = 1
VISUALIZATION_STEP = 64 # 何ステップごとに画面を更新するか。 # どうもimshowの実行が遅いので数値を大きくしている
# モデルの各パラメタ
Du = 2e-5
Dv = 1e-5
f, k = 0.04, 0.06 # amorphous
# f, k = 0.035, 0.065 # spots
# f, k = 0.012, 0.05 # wandering bubbles
# f, k = 0.025, 0.05 # waves
# f, k = 0.022, 0.051 # stripe
# 初期化
u = np.ones((SPACE_GRID_SIZE, SPACE_GRID_SIZE))
v = np.zeros((SPACE_GRID_SIZE, SPACE_GRID_SIZE))
# **にSQUARE_SIZE四方の正方形を置く
SQUARE_SIZE = 20
u[SPACE_GRID_SIZE//2-SQUARE_SIZE//2:SPACE_GRID_SIZE//2+SQUARE_SIZE//2,
SPACE_GRID_SIZE//2-SQUARE_SIZE//2:SPACE_GRID_SIZE//2+SQUARE_SIZE//2] = 0.5
v[SPACE_GRID_SIZE//2-SQUARE_SIZE//2:SPACE_GRID_SIZE//2+SQUARE_SIZE//2,
SPACE_GRID_SIZE//2-SQUARE_SIZE//2:SPACE_GRID_SIZE//2+SQUARE_SIZE//2] = 0.25
# 対称性を壊すために、少しノイズを入れる
u += np.random.rand(SPACE_GRID_SIZE, SPACE_GRID_SIZE)*0.1
v += np.random.rand(SPACE_GRID_SIZE, SPACE_GRID_SIZE)*0.1
value_range_min=0
value_range_max=1
value_range = (value_range_min, value_range_max)
def frames(*args, **kwargs):
global u, v, value_range
for i in range(VISUALIZATION_STEP):
# ラプラシアンの計算
laplacian_u = (np.roll(u, 1, axis=0) + np.roll(u, -1, axis=0) +
np.roll(u, 1, axis=1) + np.roll(u, -1, axis=1) - 4*u) / (dx*dx)
laplacian_v = (np.roll(v, 1, axis=0) + np.roll(v, -1, axis=0) +
np.roll(v, 1, axis=1) + np.roll(v, -1, axis=1) - 4*v) / (dx*dx)
# Gray-Scottモデル方程式
dudt = Du*laplacian_u - u*v*v + f*(1.0-u)
dvdt = Dv*laplacian_v + u*v*v - (f+k)*v
u += dt * dudt
v += dt * dvdt
matrix = u
matrix[matrix < value_range[0]] = value_range[0]
matrix[matrix > value_range[1]] = value_range[1]
img = ((matrix.astype(np.float64) - value_range[0]) / (value_range[1] - value_range[0]) * 255).astype(np.uint8)
return [plt.imshow(img, interpolation='none')]
fig = plt.figure()
anim = animation.FuncAnimation(fig, frames, frames=120, interval=200) # 計算し終わるまで時間がかかります
rc('animation', html='jshtml')
anim
変更がvisualizerの差し替えのみで済めば全てのサンプルをmatplotlibで(= Colaboratory上で)動かせたのですが、visualizerと matplotlib.animation
のインターフェース構造が異なっていたのでサンプルごとに書き換えないと動かなそうです 😞
from alife_book_src.
Related Issues (20)
- chap02/gray_scott_1d.pyとgray_scott_param.pyのコメントが英語 HOT 1
- Windows+Anacondaでpygame周りのエラー HOT 2
- chap07のreadme.mdの句点
- コメントは日本語で統一 HOT 4
- chap06_evolution/ant_nn_ga.py内のスペルミス
- Subsumptionで、働いているモジュールに応じて色を変える
- SwarmVisualizerを改良 HOT 1
- SCLVisualizerを改良
- 1DArrayVisualizer HOT 2
- docker anaconda3へのAlife環境インストールエラーにつきまして HOT 4
- vispy実行時のSegmentation fault (コアダンプ) HOT 1
- pymunkのインストールでエラー HOT 2
- VisPy is not yet compatible with matplotlib 2.2+ HOT 1
- gray_scott.pyが動かない HOT 12
- English translation for book and repository HOT 1
- 4.2 chap04 boids.py boidが現れない HOT 5
- scl_utils.pyのムーア近傍の取得のコードに関しまして
- simulatorをjupyter notebook上で動くようにしたい HOT 1
- get_random_2_moore_neighborhoodの実装が正しくない
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alife_book_src.