Code Monkey home page Code Monkey logo

Comments (1)

mitsuyoshi-yamazaki avatar mitsuyoshi-yamazaki commented on July 21, 2024 3

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.