NB: you need to search for http://localhost:3000 to use Interactive MD, otherwise fetching is not corret
This project combines a client-side molecular dynamics simulation with a server-side data collection and analysis system. The simulation runs in a web browser and sends real-time data to a Node.js server, which then processes and analyzes the data using Python scripts.
The system consists of three main components:
- Client: HTML/JavaScript molecular dynamics simulation
- Server: Node.js/Express server for data collection
- Analysis: Python scripts for real-time data visualization
Original Source: Based on Daniel V. Schroeder's "Interactive Molecular Dynamics" simulation Modifications: Extended with server communication capabilities
- 2D molecular dynamics simulation using Lennard-Jones potential
- Real-time physics calculations with Verlet integration
- Interactive controls for manipulating atoms
- Multiple data collection modes
- Real-time server communication
- Mode Selection: Sends current data collection mode to server
- System Data: Sends macroscopic properties (energy, temperature, pressure)
- Distribution Data: Sends velocity distributions of all atoms
// Mode selection handler
async function SendMode(mode) {
const data = { mode: mode };
// Sends current data collection mode to server
}
// System data transmission
async function writeStatsToJson() {
// Sends energy, volume, particle count, temperature, pressure
}
// Distribution data transmission
async function writeAllToJson() {
// Sends velocity components (vx, vy) for all atoms
}The server handles two main operational modes:
- Creates/updates
Data.csvwith macroscopic properties - Runs
system.pyfor data analysis - Listens on
/systemendpoint for incoming data
- Creates/updates
distribution.csvwith velocity distributions - Runs
distribution.pyfor velocity analysis - Listens on
/allendpoint for incoming data
const mode = 'all'; // or 'system' - controls operational mode
const N = 20; // Configuration parameter- Reads
Data.csvcontaining system-wide properties - Real-time plotting of energy evolution
- Uses matplotlib animation for live updates
- Reads
distribution.csvwith atom velocity data - Creates histograms for:
- vx (x-component velocity)
- vy (y-component velocity)
- v (total velocity magnitude)
- Real-time updates of velocity distributions
- Node.js and npm
- Python 3.x with packages: numpy, pandas, matplotlib
- Install Node.js dependencies:
npm install express path python-shell csv-writer- Install Python dependencies:
pip install numpy pandas matplotlib- Directory structure:
project/
├── server.js
├── index.html
├── system.py
├── distribution.py
├── Data.csv (generated)
└── distribution.csv (generated)
- Start the Node.js server:
node server.js- Open the client in a web browser:
http://localhost:3000
-
Start the simulation using the "Start" button
-
Choose data collection mode in the "Data type" dropdown:
- System totals: Collects macroscopic properties
- All atoms: Collects velocity distributions
-
Start Python analysis scripts in separate terminals:
python system.py
python distribution.py- Collects: Energy, Temperature, Pressure, Volume, Particle count
- Output:
Data.csv - Analysis: Real-time energy evolution plot
- Collects: Velocity components (vx, vy) for all atoms
- Output:
distribution.csv - Analysis: Real-time velocity distribution histograms
Body: JSON with system properties
{
"E": "Energy",
"V": "Volume",
"N": "Num",
"T": "Temp",
"P": "Press"
}Body: JSON with velocity data
{
"data": [
{"vx": 0.123, "vy": -0.456},
{"vx": -0.789, "vy": 0.321}
]
}Body: JSON with operational mode
{
"mode": "system" // or "all"
}- Extend server.js:
else if (mode == 'new_mode') {
// Add new CSV writer configuration
// Add new Python script execution
// Add new endpoint handler
}- Extend client data transmission:
// Add new data collection function
async function collectNewData() {
const newData = { /* new data structure */ };
const response = await fetch('/new_endpoint', options);
}- Create new analysis script:
# new_analysis.py
# Implement real-time analysis for new data type- Modify existing Python scripts to add new plot types
- Create new Python scripts for specialized analysis
- Extend data collection to include additional parameters
Modify the client-side simulation constants:
forceCutoff: Interaction rangewallStiffness: Container wall strengthbondStrength: Molecular bond strengthg: Gravitational constant
const mode = 'all'; // Operational mode
const N = 20; // System parameter
const port = 3000; // Server port- Number of atoms (1-2500)
- Box size and volume
- Time step and simulation speed
- Gravity strength
- Data collection intervals
server.js: Node.js server with data collection endpointsindex.html: Client-side molecular dynamics simulationsystem.py: Real-time energy evolution analysisdistribution.py: Real-time velocity distribution analysisData.csv: Generated system property data (mode='system')distribution.csv: Generated velocity distribution data (mode='all')
- Port already in use: Change
portvariable in server.js - Missing dependencies: Run npm install and pip install commands
- CSV file errors: Ensure write permissions in project directory
- Python script errors: Verify matplotlib backend compatibility
- Large atom counts (>1000) may impact browser performance
- High data collection frequency may slow down simulation
- Consider increasing
stepsPerFramefor better performance
Original Simulation: Daniel V. Schroeder, "Interactive Molecular Dynamics"
Client Modifications: Extended with server communication capabilities
Server & Analysis: Custom implementation for real-time data processing
This system provides a complete pipeline from molecular dynamics simulation to real-time data analysis, making it ideal for educational demonstrations and computational physics experiments.