Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NB: you need to search for http://localhost:3000 to use Interactive MD, otherwise fetching is not corret

Molecular Dynamics Simulation with Real-time Data Analysis

Overview

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.

Architecture

The system consists of three main components:

  1. Client: HTML/JavaScript molecular dynamics simulation
  2. Server: Node.js/Express server for data collection
  3. Analysis: Python scripts for real-time data visualization

Component Details

1. Client (Molecular Dynamics Simulation)

Original Source: Based on Daniel V. Schroeder's "Interactive Molecular Dynamics" simulation Modifications: Extended with server communication capabilities

Key Features:

  • 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

Server Communication Extensions:

  • 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

Key Modified Functions:

// 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
}

2. Server (Node.js/Express)

The server handles two main operational modes:

Mode 1: System Data Collection (mode = 'system')

  • Creates/updates Data.csv with macroscopic properties
  • Runs system.py for data analysis
  • Listens on /system endpoint for incoming data

Mode 2: Distribution Data Collection (mode = 'all')

  • Creates/updates distribution.csv with velocity distributions
  • Runs distribution.py for velocity analysis
  • Listens on /all endpoint for incoming data

Server Configuration:

const mode = 'all'; // or 'system' - controls operational mode
const N = 20; // Configuration parameter

3. Analysis Scripts (Python)

system.py - Macroscopic Properties Analysis

  • Reads Data.csv containing system-wide properties
  • Real-time plotting of energy evolution
  • Uses matplotlib animation for live updates

distribution.py - Velocity Distribution Analysis

  • Reads distribution.csv with atom velocity data
  • Creates histograms for:
    • vx (x-component velocity)
    • vy (y-component velocity)
    • v (total velocity magnitude)
  • Real-time updates of velocity distributions

Installation and Setup

Prerequisites

  • Node.js and npm
  • Python 3.x with packages: numpy, pandas, matplotlib

Installation Steps:

  1. Install Node.js dependencies:
npm install express path python-shell csv-writer
  1. Install Python dependencies:
pip install numpy pandas matplotlib
  1. Directory structure:
project/
├── server.js
├── index.html
├── system.py
├── distribution.py
├── Data.csv (generated)
└── distribution.csv (generated)

Usage Instructions

Starting the System:

  1. Start the Node.js server:
node server.js
  1. Open the client in a web browser:
http://localhost:3000
  1. Start the simulation using the "Start" button

  2. Choose data collection mode in the "Data type" dropdown:

    • System totals: Collects macroscopic properties
    • All atoms: Collects velocity distributions
  3. Start Python analysis scripts in separate terminals:

python system.py
python distribution.py

Operational Modes:

System Mode (mode = 'system')

  • Collects: Energy, Temperature, Pressure, Volume, Particle count
  • Output: Data.csv
  • Analysis: Real-time energy evolution plot

Distribution Mode (mode = 'all')

  • Collects: Velocity components (vx, vy) for all atoms
  • Output: distribution.csv
  • Analysis: Real-time velocity distribution histograms

API Endpoints

POST /system

Body: JSON with system properties

{
  "E": "Energy",
  "V": "Volume", 
  "N": "Num",
  "T": "Temp",
  "P": "Press"
}

POST /all

Body: JSON with velocity data

{
  "data": [
    {"vx": 0.123, "vy": -0.456},
    {"vx": -0.789, "vy": 0.321}
  ]
}

POST /mode

Body: JSON with operational mode

{
  "mode": "system"  // or "all"
}

Extending the System

Adding New Data Collection Modes:

  1. Extend server.js:
else if (mode == 'new_mode') {
    // Add new CSV writer configuration
    // Add new Python script execution
    // Add new endpoint handler
}
  1. Extend client data transmission:
// Add new data collection function
async function collectNewData() {
    const newData = { /* new data structure */ };
    const response = await fetch('/new_endpoint', options);
}
  1. Create new analysis script:
# new_analysis.py
# Implement real-time analysis for new data type

Adding New Analysis Visualizations:

  1. Modify existing Python scripts to add new plot types
  2. Create new Python scripts for specialized analysis
  3. Extend data collection to include additional parameters

Customizing Physics Parameters:

Modify the client-side simulation constants:

  • forceCutoff: Interaction range
  • wallStiffness: Container wall strength
  • bondStrength: Molecular bond strength
  • g: Gravitational constant

Configuration Options

Server Configuration:

const mode = 'all'; // Operational mode
const N = 20; // System parameter
const port = 3000; // Server port

Client Configuration:

  • Number of atoms (1-2500)
  • Box size and volume
  • Time step and simulation speed
  • Gravity strength
  • Data collection intervals

File Descriptions

  • server.js: Node.js server with data collection endpoints
  • index.html: Client-side molecular dynamics simulation
  • system.py: Real-time energy evolution analysis
  • distribution.py: Real-time velocity distribution analysis
  • Data.csv: Generated system property data (mode='system')
  • distribution.csv: Generated velocity distribution data (mode='all')

Troubleshooting

Common Issues:

  1. Port already in use: Change port variable in server.js
  2. Missing dependencies: Run npm install and pip install commands
  3. CSV file errors: Ensure write permissions in project directory
  4. Python script errors: Verify matplotlib backend compatibility

Performance Considerations:

  • Large atom counts (>1000) may impact browser performance
  • High data collection frequency may slow down simulation
  • Consider increasing stepsPerFrame for better performance

Acknowledgments

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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages