Percolation / app.py
4kasha
init
74e96d7
raw
history blame
3.95 kB
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
import time
from scipy.ndimage import label
def create_initial_plot(grid_size):
grid = np.zeros((grid_size, grid_size))
fig = plt.figure(figsize=(15, 6))
grid_ax = fig.add_subplot(121)
graph_ax = fig.add_subplot(122)
grid_ax.imshow(grid, cmap='Greys', alpha=0.3)
grid_ax.set_title(
f'Site Occupation Probability p = 0.00\n'
f'Largest Cluster Ratio = 0.000'
)
graph_ax.plot([0], [0], '-b', label='Largest Cluster Size')
graph_ax.set_xlabel('Occupation Probability p')
graph_ax.set_ylabel('Largest Cluster Size Ratio')
graph_ax.set_title('Phase Transition in 2D Percolation')
graph_ax.grid(True)
graph_ax.legend()
graph_ax.set_xlim(0, 1)
graph_ax.set_ylim(0, 1)
plt.tight_layout()
return fig
def get_largest_cluster(grid):
labeled_array, num_features = label(grid)
if num_features == 0:
return np.zeros_like(grid), 0
sizes = [np.sum(labeled_array == i) for i in range(1, num_features + 1)]
if not sizes:
return np.zeros_like(grid), 0
max_cluster_index = np.argmax(sizes) + 1
max_cluster_mask = labeled_array == max_cluster_index
max_cluster_size = sizes[max_cluster_index - 1]
return max_cluster_mask, max_cluster_size
def run_percolation_simulation(grid_size):
p_step = 0.02
fine_step = 0.01
p1 = np.arange(0, 0.56, p_step)
p2 = np.arange(0.56, 0.60 + fine_step, fine_step)
p3 = np.arange(0.60 + p_step, 1.0 + p_step, p_step)
steps = np.concatenate((p1, p2, p3))
p_values = []
largest_clusters = []
fig = plt.figure(figsize=(15, 6))
grid_ax = fig.add_subplot(121)
graph_ax = fig.add_subplot(122)
np.random.seed(3407)
for p in steps:
grid = np.random.random((grid_size, grid_size)) < p
largest_cluster_mask, largest_cluster_size = get_largest_cluster(grid)
largest_cluster_ratio = largest_cluster_size / (grid_size * grid_size)
p_values.append(p)
largest_clusters.append(largest_cluster_ratio)
grid_ax.clear()
graph_ax.clear()
grid_ax.imshow(grid, cmap='Greys', alpha=0.3)
grid_ax.imshow(largest_cluster_mask, cmap='Blues', alpha=0.7)
grid_ax.set_title(
f'Site Occupation Probability p = {p:.2f}\n'
f'Largest Cluster Ratio = {largest_cluster_ratio:.3f}'
)
graph_ax.plot(p_values, largest_clusters, '-b', label='Largest Cluster Size')
graph_ax.set_xlabel('Occupation Probability p')
graph_ax.set_ylabel('Largest Cluster Size Ratio')
graph_ax.set_title('Phase Transition in 2D Percolation')
graph_ax.grid(True)
graph_ax.legend()
graph_ax.set_xlim(0, 1)
graph_ax.set_ylim(0, 1)
plt.tight_layout()
yield fig
time.sleep(0.25)
with gr.Blocks() as demo:
gr.Markdown("# 2D Site Percolation Simulation")
gr.Markdown("""
This simulation shows the formation of clusters in a 2D percolation system as the occupation probability increases.
Watch how the system undergoes a phase transition around p ≈ 0.593 (critical point).
- Gray dots: Occupied sites
- Blue region: Largest connected cluster
""")
with gr.Row():
plot = gr.Plot(value=create_initial_plot(50))
with gr.Row():
grid_size = gr.Slider(
minimum=20, maximum=200, step=10, value=150,
label="Grid Size", info="Size of the simulation grid"
)
with gr.Row():
start_btn = gr.Button("Start Simulation")
start_btn.click(
fn=run_percolation_simulation,
inputs=[grid_size],
outputs=plot,
show_progress=False
)
grid_size.change(
fn=create_initial_plot,
inputs=[grid_size],
outputs=plot
)
if __name__ == "__main__":
demo.queue().launch(
debug=True
)