Provides a simulation framework for a random loop model in statistical mechanics, including initialization, simulation, and visualization capabilities.
Project description
PyRandomLoop is a Python package designed for simulating and visualizing a random loop model on a 2d grid.
The core of the simulation is the class StateSpace. It provides methods for initializing the grid, running the simulation, visualization, saving and loading the state of the simulation, and calculating various statistics.
Features
- Initialize the grid with a specified number of colors, grid size, and boundary conditions.
- Run the simulation using the Metropolis or Glauber algorithm.
- Save and load the state of the simulation to/from a file.
- Calculate and visualize various statistics about the loops formed by each color.
Examples
Initializing the Grid and Running the Simulation
To initiate the grid and run the simulation, you can use the following code:
from PyRandomLoop import StateSpace
# Initialize the grid with 3 colors, a grid size of 64 by 64, and a beta value of 1.7
sim = StateSpace(num_colors=3, grid_size=64, beta=1.7)
# Run the simulation for 1000 steps, showing a progress bar
sim.step(10_000, progress_bar=True)
# Save the state of the simulation to a file
sim.save_state('example.json')
# Load the state of the simulation from a file later
sim.load_state('example.json')
Visualizing the Grid Using the Plot Functions
To visualize the grid using the plot functions, you can use the following code:
# Plot the grid for all colors
sim.plot_grid()
# Plot the overlap of all colors in the grid
sim.plot_overlap()
Creating an Animation Using the Animate Method
To create an animation using the animate method first you need to sample the grid during the simulation:
# sample grid state every 1000 steps
sim.step(100_000, sample_rate=1_000, progress_bar=True)
# Create an animation using the animate method
animation = sim.animate(frames=100)
# Save the animation
animation.save('example.gif')
Studying Observables During the Simulation
To study observables during the simulation, you can define a list or a dictionary of callable objects, which will be called every sample_rate:
# Define custom observables using built-in methods or custom functions
def custom_obs():
return np.log( sim.avg_local_time() )
observables = {
'avg_links': sim.avg_links,
'max_links': sim.max_links,
'avg_local_time': sim.avg_local_time,
'custom_obs': custom_obs,
'links_std': lambda : np.std(m.grid, axis = (1,2,3))
}
# Create a loop builder to study observables during the simulation
loop_builder = grid.loop_builder(num_steps=100_000, sample_rate=1_000, observables=observables)
# Analyze the collected data
print(sim.data)
Study loops
After running the simulation, we can calculate the loops and their length for each color using the loop_builder method.
import matplotlib.pyplot as plt
# Initialize the grid with 3 colors, a grid size of 100, and a beta value of 1.5
sim = StateSpace(3, 64, 5)
# Run the simulation
sim.step(100_000)
# Calculate the loops for each color
loops, lengths = sim.loop_builder()
# Plot the histogram of loop lengths for each color
for c in range(sim.num_colors):
plt.hist(lengths[c], log = True, align='left', label='Color {}'.format(c))
plt.xlabel('Loop Length')
plt.ylabel('Frequency')
plt.legend()
plt.show()
# plot 3 longest loops of color 0
# find top 10 longest loops
# print top 10
sorted = np.sort(lengths[0])[-3:]
top_three = []
for l in loops[0]:
if len(l) in sorted:
top.append(l)
#plot it
m.plot_loop(0, top_three, alpha = 0.5) #, colors=colors)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file PyRandomLoop-0.1.5.tar.gz.
File metadata
- Download URL: PyRandomLoop-0.1.5.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d6bfcad5e103bf2ea0a9f8dc32aab90b489cdc725cdf53640cfe689fdc0d5ab
|
|
| MD5 |
59dff0a0eea3821bf3873d272c44da3e
|
|
| BLAKE2b-256 |
ac9acf3669183c678dbb5320e66707e6f4ba02320e2b637b57da5f327d338a4e
|