Skip to main content

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 RPM. 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 and sample observables.
  • 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 RPM

# Initialize the grid with 3 colors, a grid size of 64 by 64, and a beta value of 1.7
sim = RPM(num_colors=3, grid_size=64, beta=2.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_data('example.json')

# Load the state of the simulation from a file later
sim.load_data('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, observables=[sim.get_grid], progress_bar=True)

# Create an animation using the animate method
animation = sim.animate()

# 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 steps:

import numpy as np 

# Define custom observables using built-in methods or custom functions
def custom_obs():
    return np.exp( 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(sim.grid, axis = (1,2,3))
}

# run the simulation
sim.step(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 numpy as np
import matplotlib.pyplot as plt 

# Calculate the loops for each color
loops, lengths, visits = 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= f'Color {c}', alpha=0.7)

plt.xlabel('Loop Length')
plt.ylabel('Frequency')
plt.legend()
plt.show()

# find 3 longest loops of color 0
sorted = np.sort(lengths[0])[-3:]

top_3 = []

for l in loops[0]:
    if len(l) in sorted:
        top_3.append(l)

#plot them
sim.plot_loop_overlap(top_3, color=0, alpha_loop=0.5)

Tests and Benchmark

To run a test that checks all the functionality and a benchmark , you can run the scripts test.py and benchmark.py in the PyRandomLoop.tests submodule:

from PyRandomLoop.tests import test, benchmark

test.main()
benchmark.main()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyRandomLoop-0.2.0.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

PyRandomLoop-0.2.0-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

File details

Details for the file PyRandomLoop-0.2.0.tar.gz.

File metadata

  • Download URL: PyRandomLoop-0.2.0.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.9

File hashes

Hashes for PyRandomLoop-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f0b15804c7685ee2aa0ef647378d4dbe90f0b793c9a4bd22a8812746aa0bd296
MD5 ee09dafa1e1307e601c14b5d6e055b86
BLAKE2b-256 df762585b7a51915fe72361a462dcc050e952da531b712ded279c88511fc6044

See more details on using hashes here.

File details

Details for the file PyRandomLoop-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: PyRandomLoop-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.9

File hashes

Hashes for PyRandomLoop-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5637435941a862a9c4fa7f65a4a456c2e37a39f11f78df6c7641855ad3b069f
MD5 6737f9f65f09bbd11fc9954e5f85b5ad
BLAKE2b-256 e0c19c4669e6f16175095093d1f1bc166d1ceb28a3f2531fcbde37579b444e1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page