Skip to main content

轻松生成伊辛模型动画的模块

项目描述

ising_animate

一个 Python 包,可使用Metropolis 算法轻松生成Ising 模型的动画,Metropolis 算法是计算该系统估计值的最常用的马尔可夫链蒙特卡罗方法。

ising_2021-10-12_15-14-51

安装

请务必在您的机器上安装 Python 3.6 或更新版本。然后,您可以简单地使用 pip 安装包及其依赖项。

pip install ising_animate

用法

命令行工具

这个包可以用作命令行工具来生成所需的动画。例如,上面的动画是使用命令创建的

python -m ising_animate

您可以使用一些命令行选项指定所需的大小、初始状态、温度或字段。例如,命令

python -m ising_animate --size 256 --temp 1.5 --field 1.0 --init-state "down" --time-series

产量 ising_2021-10-12_15-26-03

有关所有可用选项的完整描述,请输入python -m ising_animate --help

进口

导入后,有四类对象可用于创建自定义动画:

  • Ising : 只是 Ising Model 的核心实现,没有动画;
  • AnimatedIsing:Ising 模型的动画,温度和外部磁场都保持在固定值;
  • CoolingAnimatedIsing:Ising 模型的动画,温度以给定速率指数下降(或上升)到目标值;
  • DynamicAnimatedIsing:Ising 模型的动画,其温度和外部磁场均由给定的时间函数描述。

它们的用法都非常相似。您只需要使用所需的参数创建一个实例...

from ising_animate import DynamicAnimatedIsing
from math import sin

frames = 100

dynamic = DynamicAnimatedIsing(
    shape=(256, 256),                # the shape of the lattice
    temp=lambda t: 1.0 + 0.3 * t,    # temperature as a function of time
    field=lambda t: sin(t),          # external magnetic field as a function of time
    time_series=True,                # plot evolution of physical quantities over time
    interval=100,                    # interval of each frame
    frames=frames,                   # amount of frames in the animation
)

并且动画本身现在由存储在的 matplotlib FuncAnimation 对象给出dynamic.animation。这个对象有一个save 方法,其中最重要的参数是输出文件名和动画的 fps。fps 可以随意选择,但自然值是 1000(一秒内的毫秒数)除以每帧的间隔。所以,在这种情况下,fps 是 10。

dynamic.animation.save("outfile.gif", fps=10)

输出文件 save 方法的另一个有用特性是可以传递一个进度回调函数,该函数将在绘制每一帧后调用,其中包含已绘制帧的计数和动画上的总帧数。这使得使用progressbar2 之类的包很容易在进度条中显示进度。

import progressbar

with progressbar.ProgressBar(max_value=frames) as bar:
    dynamic.animation.save(
        "outfile.gif",
        fps=10,
        progress_callback=lambda i, n: bar.update(i),
    )

要安装 progressbar2,请在终端中使用以下命令。

pip install progressbar2

要查看每个类的完整描述,请查看完整文档

例子

有一个ising_animate.examples包含该库使用示例的子包。上面的例子就是其中之一,可以使用

python -m ising_animate.examples.dynamic

另一个有趣的可以运行

python -m ising_animate.examples.heating

这产生了两个图,其中平均能量和比热密度与温度的相关性。这个例子很好地说明了在 2.27 的温度下发生的相变,以能量为单位。

供暖_2021-10-26_17-01

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

ising_animate-0.0.2.tar.gz (14.9 kB 查看哈希)

已上传 source

内置分布

ising_animate-0.0.2-py3-none-any.whl (15.9 kB 查看哈希)

已上传 py3