-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_alignment_phase.py
More file actions
73 lines (63 loc) · 2.79 KB
/
run_alignment_phase.py
File metadata and controls
73 lines (63 loc) · 2.79 KB
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
import torch
from particle_dynamics import ParticleSimulation
# Set device to GPU if available, otherwise CPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
dtype = torch.float32
# Example configurations for different dynamics
example_configs = {
"collapse_1D": {
"V": torch.tensor([[1, 0, 0], [0, -1, 0], [0, 0, 2]], device=device, dtype=dtype),
"QtK": torch.tensor([[-1, 0, 0], [0, -1, 0], [0, 0, 1]], device=device, dtype=dtype),
},
"collapse_2D": {
"V": torch.tensor([[1, 0, 0], [0, 1, 0], [0, 0, 1]], device=device, dtype=dtype),
"QtK": torch.tensor([[1, 0, 0], [0, 1, 0], [0, 0, -2]], device=device, dtype=dtype).T,
},
"collapse_rotation": {
"description": "Particles collapse towards the Z-axis while rotating around it.",
"V": torch.tensor([[1, 0, 0], [0, 1, 0], [0, 0, 1]], device=device, dtype=dtype),
"QtK": torch.tensor([[0, 1, 0], [-1, 0, 0], [0, 0, -2]], device=device, dtype=dtype).T,
},
"random_matrices": {
"description": "Complex dynamics generated from random interaction matrices.",
"V": torch.randn((3, 3), device=device, dtype=dtype),
"QtK": torch.randn((3, 3), device=device, dtype=dtype),
}
}
def main():
"""
Sets up and runs a particle dynamics simulation, then saves the result as a GIF.
"""
print(f"Using device: {'cuda' if torch.cuda.is_available() else 'cpu'}")
# --- Simulation Parameters ---
# Feel free to change these values to explore different dynamics
example_name = "collapse_1D" # Choose from example_configs keys
simulation_config = {
"N": 10**4, # Number of particles
"d": 3, # Number of dimensions (must be 3 for GIF)
"T": 5.0, # Total simulation time
"dt": 1e-2, # Simulation time step
"beta": 30.0, # Temperature
"dtype": "single", # Use 'double' for higher precision
"original_transformer": True, # Use attention mechanism
"QtK": example_configs[example_name]["QtK"],
"V": example_configs[example_name]["V"]
}
# Initialize the simulation with the specified configuration
sim = ParticleSimulation(**simulation_config)
# --- Run Simulation ---
print("\nStarting simulation...")
sim.run()
print("Simulation complete.")
# --- Generate and Save Animation ---
if sim.d == 3:
print("\nGenerating 3D animation...")
sim.save_simulation_as_gif(
filename=f'results/simulation_{example_name}.gif',
frames=150, # Number of frames in the final GIF
interval=40 # Milliseconds per frame
)
else:
print(f"\nSimulation was in {sim.d}D. GIF generation is only available for 3D.")
if __name__ == "__main__":
main()