Spaces:
Sleeping
Sleeping
4kasha
commited on
Commit
·
74e96d7
1
Parent(s):
a35c33f
init
Browse files- README.md +2 -2
- app.py +133 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
---
|
| 2 |
title: Percolation
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.6.0
|
|
|
|
| 1 |
---
|
| 2 |
title: Percolation
|
| 3 |
+
emoji: 🧱
|
| 4 |
+
colorFrom: blue
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.6.0
|
app.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import time
|
| 5 |
+
from scipy.ndimage import label
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def create_initial_plot(grid_size):
|
| 9 |
+
grid = np.zeros((grid_size, grid_size))
|
| 10 |
+
fig = plt.figure(figsize=(15, 6))
|
| 11 |
+
grid_ax = fig.add_subplot(121)
|
| 12 |
+
graph_ax = fig.add_subplot(122)
|
| 13 |
+
|
| 14 |
+
grid_ax.imshow(grid, cmap='Greys', alpha=0.3)
|
| 15 |
+
grid_ax.set_title(
|
| 16 |
+
f'Site Occupation Probability p = 0.00\n'
|
| 17 |
+
f'Largest Cluster Ratio = 0.000'
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
graph_ax.plot([0], [0], '-b', label='Largest Cluster Size')
|
| 21 |
+
graph_ax.set_xlabel('Occupation Probability p')
|
| 22 |
+
graph_ax.set_ylabel('Largest Cluster Size Ratio')
|
| 23 |
+
graph_ax.set_title('Phase Transition in 2D Percolation')
|
| 24 |
+
graph_ax.grid(True)
|
| 25 |
+
graph_ax.legend()
|
| 26 |
+
graph_ax.set_xlim(0, 1)
|
| 27 |
+
graph_ax.set_ylim(0, 1)
|
| 28 |
+
|
| 29 |
+
plt.tight_layout()
|
| 30 |
+
return fig
|
| 31 |
+
|
| 32 |
+
def get_largest_cluster(grid):
|
| 33 |
+
labeled_array, num_features = label(grid)
|
| 34 |
+
if num_features == 0:
|
| 35 |
+
return np.zeros_like(grid), 0
|
| 36 |
+
|
| 37 |
+
sizes = [np.sum(labeled_array == i) for i in range(1, num_features + 1)]
|
| 38 |
+
if not sizes:
|
| 39 |
+
return np.zeros_like(grid), 0
|
| 40 |
+
|
| 41 |
+
max_cluster_index = np.argmax(sizes) + 1
|
| 42 |
+
max_cluster_mask = labeled_array == max_cluster_index
|
| 43 |
+
max_cluster_size = sizes[max_cluster_index - 1]
|
| 44 |
+
|
| 45 |
+
return max_cluster_mask, max_cluster_size
|
| 46 |
+
|
| 47 |
+
def run_percolation_simulation(grid_size):
|
| 48 |
+
p_step = 0.02
|
| 49 |
+
fine_step = 0.01
|
| 50 |
+
|
| 51 |
+
p1 = np.arange(0, 0.56, p_step)
|
| 52 |
+
p2 = np.arange(0.56, 0.60 + fine_step, fine_step)
|
| 53 |
+
p3 = np.arange(0.60 + p_step, 1.0 + p_step, p_step)
|
| 54 |
+
steps = np.concatenate((p1, p2, p3))
|
| 55 |
+
|
| 56 |
+
p_values = []
|
| 57 |
+
largest_clusters = []
|
| 58 |
+
|
| 59 |
+
fig = plt.figure(figsize=(15, 6))
|
| 60 |
+
grid_ax = fig.add_subplot(121)
|
| 61 |
+
graph_ax = fig.add_subplot(122)
|
| 62 |
+
|
| 63 |
+
np.random.seed(3407)
|
| 64 |
+
for p in steps:
|
| 65 |
+
grid = np.random.random((grid_size, grid_size)) < p
|
| 66 |
+
largest_cluster_mask, largest_cluster_size = get_largest_cluster(grid)
|
| 67 |
+
largest_cluster_ratio = largest_cluster_size / (grid_size * grid_size)
|
| 68 |
+
|
| 69 |
+
p_values.append(p)
|
| 70 |
+
largest_clusters.append(largest_cluster_ratio)
|
| 71 |
+
|
| 72 |
+
grid_ax.clear()
|
| 73 |
+
graph_ax.clear()
|
| 74 |
+
|
| 75 |
+
grid_ax.imshow(grid, cmap='Greys', alpha=0.3)
|
| 76 |
+
grid_ax.imshow(largest_cluster_mask, cmap='Blues', alpha=0.7)
|
| 77 |
+
|
| 78 |
+
grid_ax.set_title(
|
| 79 |
+
f'Site Occupation Probability p = {p:.2f}\n'
|
| 80 |
+
f'Largest Cluster Ratio = {largest_cluster_ratio:.3f}'
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
graph_ax.plot(p_values, largest_clusters, '-b', label='Largest Cluster Size')
|
| 84 |
+
graph_ax.set_xlabel('Occupation Probability p')
|
| 85 |
+
graph_ax.set_ylabel('Largest Cluster Size Ratio')
|
| 86 |
+
graph_ax.set_title('Phase Transition in 2D Percolation')
|
| 87 |
+
graph_ax.grid(True)
|
| 88 |
+
graph_ax.legend()
|
| 89 |
+
graph_ax.set_xlim(0, 1)
|
| 90 |
+
graph_ax.set_ylim(0, 1)
|
| 91 |
+
|
| 92 |
+
plt.tight_layout()
|
| 93 |
+
|
| 94 |
+
yield fig
|
| 95 |
+
|
| 96 |
+
time.sleep(0.25)
|
| 97 |
+
|
| 98 |
+
with gr.Blocks() as demo:
|
| 99 |
+
gr.Markdown("# 2D Site Percolation Simulation")
|
| 100 |
+
gr.Markdown("""
|
| 101 |
+
This simulation shows the formation of clusters in a 2D percolation system as the occupation probability increases.
|
| 102 |
+
Watch how the system undergoes a phase transition around p ≈ 0.593 (critical point).
|
| 103 |
+
|
| 104 |
+
- Gray dots: Occupied sites
|
| 105 |
+
- Blue region: Largest connected cluster
|
| 106 |
+
""")
|
| 107 |
+
|
| 108 |
+
with gr.Row():
|
| 109 |
+
plot = gr.Plot(value=create_initial_plot(50))
|
| 110 |
+
with gr.Row():
|
| 111 |
+
grid_size = gr.Slider(
|
| 112 |
+
minimum=20, maximum=200, step=10, value=150,
|
| 113 |
+
label="Grid Size", info="Size of the simulation grid"
|
| 114 |
+
)
|
| 115 |
+
with gr.Row():
|
| 116 |
+
start_btn = gr.Button("Start Simulation")
|
| 117 |
+
|
| 118 |
+
start_btn.click(
|
| 119 |
+
fn=run_percolation_simulation,
|
| 120 |
+
inputs=[grid_size],
|
| 121 |
+
outputs=plot,
|
| 122 |
+
show_progress=False
|
| 123 |
+
)
|
| 124 |
+
grid_size.change(
|
| 125 |
+
fn=create_initial_plot,
|
| 126 |
+
inputs=[grid_size],
|
| 127 |
+
outputs=plot
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
if __name__ == "__main__":
|
| 131 |
+
demo.queue().launch(
|
| 132 |
+
debug=True
|
| 133 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
matplotlib==3.8.0
|
| 2 |
+
numpy==1.26.4
|
| 3 |
+
scipy==1.13.1
|