当前位置: 首页 > news >正文

沙堆状态的可视化图和雪崩分布

1.自组织临界性​​:

•系统自发演化到临界状态

•雪崩大小分布呈现幂律特征

2.​​尺度不变性​​:

•不同系统尺寸下雪崩分布相似

•幂指数τ与系统尺寸无关

3.​​幂律关系​​:

•双对数坐标下呈现线性关系

•斜率即为幂指数τ

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from tqdm import tqdm
from collections import defaultdict
import scipy.stats as statsclass Sandpile:def __init__(self, size=50, critical_value=4):"""初始化沙堆模型参数:size: 网格尺寸 (size x size)critical_value: 临界值,超过此值将发生崩塌"""self.size = sizeself.critical_value = critical_valueself.grid = np.zeros((size, size), dtype=int)self.avalanche_sizes = []  # 存储每次雪崩的大小self.avalanche_durations = []  # 存储每次雪崩的持续时间self.steps = 0def add_sand(self, i=None, j=None):"""在随机位置添加一粒沙子参数:i, j: 指定添加沙子的位置 (如果为None则随机选择)"""if i is None or j is None:i, j = np.random.randint(0, self.size, 2)self.grid[i, j] += 1self.steps += 1return i, jdef topple(self):"""执行崩塌过程,直到网格稳定返回:avalanche_size: 雪崩大小 (崩塌的总次数)duration: 雪崩持续时间 (迭代次数)"""avalanche_size = 0duration = 0topple_queue = []# 找到所有需要崩塌的位置unstable = np.where(self.grid >= self.critical_value)topple_queue = list(zip(unstable[0], unstable[1]))while topple_queue:duration += 1new_topples = []for i, j in topple_queue:# 执行崩塌self.grid[i, j] -= self.critical_valueavalanche_size += 1# 向四个邻居各添加一粒沙子for di, dj in [(0, 1), (1, 0), (0, -1), (-1, 0)]:ni, nj = i + di, j + djif 0 <= ni < self.size and 0 <= nj < self.size:self.grid[ni, nj] += 1# 如果邻居达到临界值,加入下一轮崩塌if self.grid[ni, nj] >= self.critical_value:new_topples.append((ni, nj))# 更新崩塌队列topple_queue = list(set(new_topples))  # 使用set去重return avalanche_size, durationdef simulate(self, n_steps=10000, warmup=1000):"""模拟沙堆演化过程参数:n_steps: 总模拟步数warmup: 预热步数 (达到临界状态前的步数)"""# 预热阶段 (不记录雪崩)for _ in range(warmup):self.add_sand()self.topple()# 主模拟阶段 (记录雪崩)for _ in tqdm(range(n_steps), desc="Simulating sandpile"):self.add_sand()av_size, av_duration = self.topple()if av_size > 0:self.avalanche_sizes.append(av_size)self.avalanche_durations.append(av_duration)def plot_grid(self):"""可视化当前沙堆状态"""cmap = ListedColormap(['white', 'lightblue', 'blue', 'darkblue', 'black'])plt.figure(figsize=(8, 8))plt.imshow(self.grid, cmap=cmap, vmin=0, vmax=self.critical_value)plt.title(f"Sandpile State (Size: {self.size}x{self.size})")plt.colorbar(label='Number of grains')plt.show()def plot_avalanche_distribution(self):"""绘制雪崩大小分布 (双对数坐标)"""if not self.avalanche_sizes:print("No avalanche data available. Run simulation first.")return# 统计雪崩大小频率size_counts = defaultdict(int)for size in self.avalanche_sizes:size_counts[size] += 1sizes = np.array(sorted(size_counts.keys()))freqs = np.array([size_counts[s] for s in sizes])probabilities = freqs / np.sum(freqs)# 过滤掉频率为0的点mask = freqs > 0sizes = sizes[mask]probabilities = probabilities[mask]# 绘制分布图plt.figure(figsize=(10, 6))plt.scatter(sizes, probabilities, alpha=0.7,label=f"Size {self.size}x{self.size}")plt.xscale('log')plt.yscale('log')plt.xlabel('Avalanche Size (log scale)')plt.ylabel('Probability (log scale)')plt.title('Avalanche Size Distribution (Log-Log Plot)')plt.grid(True, which="both", ls="--")# 拟合幂律分布if len(sizes) > 10:slope, intercept, r_value, _, _ = stats.linregress(np.log(sizes), np.log(probabilities))power_law = lambda x: np.exp(intercept) * x ** slopeplt.plot(sizes, power_law(sizes), 'r--',label=f'Power law: τ = {-slope:.2f}\nR² = {r_value ** 2:.2f}')plt.legend()plt.show()# 在不同系统尺寸下运行模拟
sizes = [32, 64, 128]
results = {}for size in sizes:print(f"\nSimulating sandpile of size {size}x{size}")sandpile = Sandpile(size=size)sandpile.simulate(n_steps=20000, warmup=2000)results[size] = sandpile# 绘制沙堆状态sandpile.plot_grid()# 绘制雪崩分布sandpile.plot_avalanche_distribution()# 比较不同系统尺寸的雪崩分布
plt.figure(figsize=(10, 6))
for size, sandpile in results.items():# 统计雪崩大小频率size_counts = defaultdict(int)for av_size in sandpile.avalanche_sizes:size_counts[av_size] += 1sizes_arr = np.array(sorted(size_counts.keys()))freqs = np.array([size_counts[s] for s in sizes_arr])probabilities = freqs / np.sum(freqs)# 过滤掉频率为0的点mask = freqs > 0sizes_arr = sizes_arr[mask]probabilities = probabilities[mask]plt.scatter(sizes_arr, probabilities, alpha=0.7,label=f"Size {size}x{size}")# 添加幂律拟合线if len(sizes_arr) > 10:slope, intercept, _, _, _ = stats.linregress(np.log(sizes_arr), np.log(probabilities))power_law = lambda x: np.exp(intercept) * x ** slopeplt.plot(sizes_arr, power_law(sizes_arr), '--', alpha=0.7)plt.xscale('log')
plt.yscale('log')
plt.xlabel('Avalanche Size (log scale)')
plt.ylabel('Probability (log scale)')
plt.title('Avalanche Size Distribution Comparison')
plt.grid(True, which="both", ls="--")
plt.legend()
plt.show()


文章转载自:

http://fXs2o1LR.ckcjq.cn
http://utbU8Cu8.ckcjq.cn
http://FLGkAgDK.ckcjq.cn
http://RBVY6TZd.ckcjq.cn
http://h4CG1VPN.ckcjq.cn
http://tGsqX8Sw.ckcjq.cn
http://qNXzypU6.ckcjq.cn
http://evabc19j.ckcjq.cn
http://omCOcZzD.ckcjq.cn
http://mLwWdKTz.ckcjq.cn
http://B6BSnwTE.ckcjq.cn
http://YPmLMsSd.ckcjq.cn
http://NHOTlic0.ckcjq.cn
http://eOKFdXlS.ckcjq.cn
http://nAcyeAYz.ckcjq.cn
http://ThK7GnL0.ckcjq.cn
http://zuddLE9e.ckcjq.cn
http://iUGNd5j9.ckcjq.cn
http://qEphsfeP.ckcjq.cn
http://6NfP61u4.ckcjq.cn
http://EiB9bctC.ckcjq.cn
http://z2kHzAxz.ckcjq.cn
http://hnqwcx8e.ckcjq.cn
http://yTu31BoD.ckcjq.cn
http://WlPJH12C.ckcjq.cn
http://n9ZiJkNf.ckcjq.cn
http://Rdsjr7O7.ckcjq.cn
http://hRCGFmww.ckcjq.cn
http://sqFH8Fpq.ckcjq.cn
http://K9t0OR2o.ckcjq.cn
http://www.dtcms.com/a/369196.html

相关文章:

  • JavaWeb —— 异常处理
  • ppp与ip类型wan对比
  • leetcode399.除法求值
  • 电磁波成像(X射线、CT成像)原理简介
  • RikkaHub:安卓原生AI聊天新体验
  • Linux之Ubuntu桌面化操作系统的安装
  • CASToR 生成的文件进行转换
  • AI架构师的思维方式与架构设计原则
  • 软考 系统架构设计师系列知识点之杂项集萃(140)
  • 修改上次提交的Git提交日志
  • 【可信数据空间-连接器状态监控-Java代码集成】
  • C语言(长期更新)第15讲 指针详解(五):习题实战
  • 全球汽车氮化镓技术市场规模将于2031年增长至180.5亿美元,2025-2031年复合增长率达94.3%,由Infineon和Navitas驱动
  • .Net程序员就业现状以及学习路线图(四)
  • 垃圾回收算法详解
  • 【QT 5.12.12 打包-Windows 平台下】
  • 2025高教社数学建模国赛B题 - 碳化硅外延层厚度的确定(完整参考论文)
  • 【设计模式】UML 基础教程总结(软件设计师考试重点)
  • 三维聚类建模
  • Web 转发机制深度解析
  • 鸿蒙NEXT自定义能力详解:从基础使用到高级技巧
  • Coze源码分析-资源库-删除提示词-前端源码
  • leedcode 算法刷题第二七天
  • 水上乐园票务管理系统设计与开发(代码+数据库+LW)
  • 天顶围棋(PC端)新手指南:3步完成对弈设置离线围棋游戏推荐:天顶围棋(PC端)实测解析 天顶围棋(PC端)避坑指南:新手设置全攻略
  • 同分异构体
  • 半年报中的FPGA江湖:你打你的,我打我的
  • 【Leetcode】高频SQL基础题--180.连续出现的数字
  • 高级RAG策略学习(六)——Contextual Chunk Headers(CCH)技术
  • Mysql中模糊匹配常被忽略的坑