# Run the simulation
make run
# Or launch interactive web app
make app
# See all available commands
make helppython -m src.md_simulationOr from the src directory:
cd src
python md_simulation.py- Random seed is set to 42 (for reproducibility)
- Two particles are placed at random positions in a 20×20 Å box
- Custom initial velocities for each particle (configurable)
- Minimum separation of 2×σ (6.8 Å) is enforced to avoid extreme forces
- Runs for 5000 time steps (5 picoseconds)
- Both particles move according to:
- Lennard-Jones forces between them
- Elastic collisions with box walls
- Velocity Verlet integration algorithm
Three plots are generated:
- Shows the 2D box with walls
- Blue line: Particle 1's path
- Red line: Particle 2's path
- Circles: Starting positions
- Squares: Ending positions
- Top panel: Kinetic, potential, and total energy vs time
- Bottom panel: Energy deviation (should be near zero)
- Tests simulation accuracy
- Inter-particle distance vs time
- Red dashed line: Equilibrium distance (3.81 Å)
- Shows if particles attract, repel, or orbit
random_seed = 123 # Try different values
np.random.seed(random_seed)# Set different velocities for each particle
velocity1 = np.array([0.01, 0.03]) # Particle 1
velocity2 = np.array([0.02, -0.01]) # Particle 2
# Or make both particles move with same velocity
velocity1 = np.array([0.02, 0.02])
velocity2 = np.array([0.02, 0.02])box_size = (30.0, 30.0) # Larger boxsim.run(n_steps=10000) # Run for 10 picosecondssim = TwoParticleMD(
particle1=particle1,
particle2=particle2,
potential=lj_potential,
box_size=box_size,
dt=0.1 # Smaller time step = better accuracy
)======================================================================
Two-Particle Molecular Dynamics Simulation in 2D Box
======================================================================
Random seed: 42
Particle 1 starting position: [7.99, 17.21]
Particle 2 starting position: [13.71, 11.58]
Initial separation: 8.03 Angstroms
Initial velocity (particle 1): [0.020, 0.020] A/fs
Starting 2D box simulation for 5000 steps (dt=1.0 fs)...
Total simulation time: 5000.000 fs
Box size: 20.0 x 20.0 Angstroms
Progress: 10%
...
Progress: 100%
Simulation complete!
Particle 1 wall collisions: 16
Particle 2 wall collisions: 6
Energy Statistics:
Initial total energy: 0.026494 kcal/mol
Final total energy: -0.028268 kcal/mol
Energy drift: -5.476161e-02 kcal/mol
Relative drift: 206.6942%
[WARNING] Significant energy drift. Consider smaller time step.
- Wall Collisions: How many times each particle bounced off walls
- Energy Drift: Should be < 1% for good accuracy
- If drift is large, reduce
dt(time step)
- If drift is large, reduce
- Trajectories:
- Do particles collide?
- Do they orbit each other?
- Do they escape to opposite corners?
- Distance Plot:
- Oscillations = bound system (particles orbit)
- Increasing distance = unbound (particles escape)
- Sharp dips = close encounters or collisions
# Particles moving toward each other
pos1 = np.array([5.0, 10.0])
pos2 = np.array([15.0, 10.0])
vel1 = np.array([0.02, 0.0])
vel2 = np.array([-0.02, 0.0])# Same velocity, different y-positions
pos1 = np.array([5.0, 8.0])
pos2 = np.array([5.0, 12.0])
vel = np.array([0.02, 0.0]) # Both move right# Fast particles
initial_velocity = np.array([0.05, 0.05])# Slow particles, close together
initial_velocity = np.array([0.005, 0.005])
# Start near equilibrium distanceProblem: Energy drift > 10% Solution: Reduce time step
dt=0.1 # Instead of dt=1.0Problem: Huge forces, simulation explodes Solution: Code automatically ensures minimum separation of 2×σ
Problem: Matplotlib backend issue Solution: Add at start of script:
import matplotlib
matplotlib.use('TkAgg') # or 'Qt5Agg'Problem: Taking too long Solution: Reduce steps or increase record_interval
sim.run(n_steps=1000, record_interval=10) # Record every 10 stepsProblem: UnicodeEncodeError: 'cp950' codec can't encode character
Cause: Windows console using non-UTF-8 encoding (CP950, CP936, etc.)
Solution: The code now automatically handles this! If you still see errors:
Option 1: Set Environment Variable (Recommended)
set PYTHONIOENCODING=utf-8
python -m src.md_simulationOption 2: Change Console Code Page
chcp 65001
python -m src.md_simulationOption 3: Use PowerShell Instead of CMD PowerShell has better Unicode support than Command Prompt.
Option 4: Run in Python IDE Most IDEs (VS Code, PyCharm, Jupyter) handle UTF-8 automatically.
Technical Details: The code includes automatic encoding fixes for Windows:
# Already included in src/md_simulation.py
# Only applies when running directly (not during tests)
if sys.platform == 'win32' and 'pytest' not in sys.modules:
import io
sys.stdout = io.TextIOWrapper(
sys.stdout.buffer,
encoding='utf-8',
errors='replace',
line_buffering=True
)
sys.stderr = io.TextIOWrapper(
sys.stderr.buffer,
encoding='utf-8',
errors='replace',
line_buffering=True
)This ensures all output uses UTF-8 encoding, preventing crashes from Unicode characters. The fix is automatically disabled during testing to avoid interfering with pytest.