-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
87 lines (77 loc) · 2.55 KB
/
run.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
84
85
86
87
"""Script for running an Ising model MC simulation."""
import argparse
import time
from pathlib import Path
import numpy as np
from updown.initialize import random
from updown.mc import run, run_temp_sequence
from updown.visualize import animate_run
def parse_args() -> argparse.ArgumentParser:
"""Parse command line arguments to control the script behavoir.
Returns:
The parsed command line arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"-W",
"--width",
type=int,
help="The width (in number of spins) of the lattice to run.",
)
parser.add_argument(
"-H",
"--height",
type=int,
help="The height (in number of spins) of the lattice to run.",
)
parser.add_argument(
"-N", "--ncycles", type=int, help="The number of cycles to run per temperature."
)
parser.add_argument(
"-O",
"--output",
type=Path,
default=Path("run_animation.gif"),
help="The output to write to. Defaults to run_animation.gif",
)
parser.add_argument(
"--start-temp",
type=float,
default=1.0,
help="The starting temperature. Defaults to 1.0.",
)
parser.add_argument(
"--end-temp",
type=float,
default=1.0,
help=(
"The ending temperature. Defaults to 1.0. If different from the starting"
" temperature a simulation is run for a linear sequence of temperatures"
" starting at --start-temp and ending at --end-temp. The number of"
" temperatures run is controlled by the --ntemps argument."
),
)
parser.add_argument(
"--ntemps",
type=int,
default=20,
help=(
"The number of temperatures to run. Only accessed if --start-temp does not"
" equal --end-temp. Defaults to 20."
),
)
return parser
def main() -> None:
"""Run an Ising model MC simulation according to command line arguments."""
args = parse_args().parse_args()
start_time = time.time()
lattice = random(args.height, args.width)
if args.start_temp != args.end_temp:
temps = np.linspace(args.start_temp, args.end_temp, args.ntemps)
frames = run_temp_sequence(lattice, temps, args.ncycles)
else:
frames = run(lattice, args.ncycles, temp=args.start_temp)
animate_run(frames, file=args.output, size=(args.height, args.width))
print("--- %s seconds ---" % (time.time() - start_time))
if __name__ == "__main__":
main()