-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamlit_app.py
83 lines (67 loc) · 2.65 KB
/
streamlit_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import time
import numpy
import pandas
import streamlit
from utils import (
monte_carlo_step,
plot_particle_distribution,
random_configuration,
total_potential_energy,
)
streamlit.header("Thomson problem")
streamlit.write(
"""
This app allows you to solve the
[Thomson problem](https://en.wikipedia.org/wiki/Thomson_problem) numerically by
using the [Monte Carlo method](https://en.wikipedia.org/wiki/Monte_Carlo_method).
Also we use the [metropolis algorithm](https://www.sciencedirect.com/topics/computer-science/metropolis-algorithm)
to relax the system. To look into the code, please visit [this repository](https://github.com/jdalzatec/thomson-problem-mc).
"""
)
with streamlit.form(key="form"):
col1, col2 = streamlit.beta_columns(2)
with col1:
num_particles = streamlit.number_input(
"Num. particles", min_value=2, max_value=32, value=5
)
with col2:
mcs = streamlit.number_input("MCS", min_value=1, value=500)
sigma = streamlit.slider(
"sigma", min_value=0.001, max_value=1.0, step=0.001, format="%.3f"
)
temp = streamlit.slider(
"Temperature", min_value=0.001, max_value=10.0, format="%.3f"
)
run_button = streamlit.form_submit_button("Run")
progress_bar = streamlit.progress(0)
mcs_text = streamlit.empty()
plot_section = streamlit.empty()
if run_button:
accepted_section, energy_section = streamlit.beta_columns(2)
with accepted_section:
streamlit.subheader("Accepted movements")
accepted_chart = streamlit.line_chart([])
with energy_section:
streamlit.subheader("Potential energy")
energy_chart = streamlit.line_chart([])
if run_button:
particles_distribution = random_configuration(num_particles)
data = pandas.DataFrame()
for i in range(mcs):
accepted = monte_carlo_step(particles_distribution, sigma, temp)
energy = total_potential_energy(particles_distribution)
data = data.append(
{"accepted_movements": accepted, "total_potential_energy": energy},
ignore_index=True,
)
progress_bar.progress((i + 1) / mcs)
mcs_text.write(f"Step {i + 1} of {mcs}")
plot_section.plotly_chart(plot_particle_distribution(particles_distribution))
accepted_chart.add_rows([accepted])
energy_chart.add_rows([energy])
streamlit.dataframe(data)
streamlit.subheader("Mean position")
streamlit.write(numpy.mean(particles_distribution, axis=0))
streamlit.subheader(f"Total potential energy for {num_particles} particles")
streamlit.write(total_potential_energy(particles_distribution))
streamlit.balloons()