Skip to content

Commit

Permalink
feat: add main w terminal execution of lattice size and optional visu…
Browse files Browse the repository at this point in the history
…alisation via --plot
  • Loading branch information
San-WierPa committed Jun 2, 2024
1 parent f2a5d67 commit 78156b6
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import numpy as np
import argparse
from src.py_ising_2d import Config, mc_metro_random, initial_lattice, calc_magnetisation, plot_results

def run_simulation(n, plot):
config = Config(n=n)

for t_index, temp in enumerate(config.T):
M1 = 0
M2 = 0
spin_config = initial_lattice(config.n)
beta = 1.0 / temp

# Equilibrate the system
for _ in range(config.eq_steps):
mc_metro_random(spin_config, config.n, beta)

# Perform Monte Carlo steps
for _ in range(config.mc_steps):
mc_metro_random(spin_config, config.n, beta)
mag = calc_magnetisation(spin_config)

M1 += mag
M2 += mag * mag

# Store results
config.M[t_index] = config.norm_1 * M1
config.Chi[t_index] = (config.norm_1 * M2 - config.norm_2 * M1 * M1) * beta

# Output results for the current lattice size
print(f"Lattice size {n} completed.")
print("\nMagnetizations:")
print(np.array2string(config.M, formatter={'float_kind':lambda x: "%.4f" % x}))
print("\nSusceptibilities:")
print(np.array2string(config.Chi, formatter={'float_kind':lambda x: "%.4f" % x}))

# Plot the results if the plot flag is set
if plot:
plot_results(config.T, config.M, config.Chi, n)

def main():
parser = argparse.ArgumentParser(description="Monte Carlo Simulation using the Metropolis algorithm.")
parser.add_argument(
"lattice_size",
type=int,
help="Size of the lattice (e.g., 4, 8, 16, 32)"
)
parser.add_argument(
"--plot",
action='store_true',
help="Plot the results after simulation"
)

args = parser.parse_args()
run_simulation(args.lattice_size, args.plot)

if __name__ == "__main__":
main()

0 comments on commit 78156b6

Please sign in to comment.