All files / src/components HelpModal.jsx

87.23% Statements 41/47
84.09% Branches 37/44
83.33% Functions 15/18
90.24% Lines 37/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400                        1x                                                                                                                                         1x                                 85x 85x 85x     85x 85x 85x 4x     4x       85x 32x 1x         85x 65x   34x 1x 1x 1x     1x           34x 34x     85x 19x 18x   1x       85x 1x 1x       85x   85x                                                                                   4x           5x                     405x     1x                                                                         20x     32x                                                                                                                                                                                                                                                                                            
/**
 * @fileoverview Help Modal Component
 * @description Interactive tutorial and help documentation for the simulation
 * @module components/HelpModal
 */
 
import { useState, useEffect } from 'react';
import './HelpModal.css';
 
/**
 * Tutorial steps for guided walkthrough
 */
const TUTORIAL_STEPS = [
  {
    id: 1,
    title: 'Welcome!',
    icon: '👋',
    content: `Welcome to the Molecular Dynamics Simulation! This tutorial will guide you through 
    the basics of using the application. Click "Next" to continue.`,
  },
  {
    id: 2,
    title: 'Adding Atoms',
    icon: '⚛️',
    content: `Use the atom selector buttons (H, C, O, N) to choose an atom type, then click 
    on the canvas to add atoms. Each atom type has different properties like mass and radius.`,
  },
  {
    id: 3,
    title: 'Loading Molecules',
    icon: '🧬',
    content: `Click on preset molecules (Water, Methane, CO₂, etc.) to instantly add complete 
    molecular structures. Molecules are placed at the center of the simulation area.`,
  },
  {
    id: 4,
    title: 'Controlling the Simulation',
    icon: '⏯️',
    content: `Use the Play/Pause button to start or stop the physics simulation. 
    The Reset button clears all atoms and restarts with a water molecule.`,
  },
  {
    id: 5,
    title: 'Player Control',
    icon: '🎮',
    content: `The first atom (highlighted with a ring) is the "player atom". Use the 
    arrow keys to apply forces and move it around. This is great for exploring interactions!`,
  },
  {
    id: 6,
    title: 'Energy Monitoring',
    icon: '📊',
    content: `Watch the Energy Monitor panel to see kinetic, potential, and total energy 
    in real-time. Expand the graph for a visual history of energy changes.`,
  },
  {
    id: 7,
    title: 'Settings & Customization',
    icon: '⚙️',
    content: `Click Settings in the header to adjust simulation parameters like time step, 
    wall bounce strength, and display options. Experiment to see how they affect behavior!`,
  },
  {
    id: 8,
    title: 'ReaxFF Parameters',
    icon: '📂',
    content: `Use the File Uploader to load ReaxFF force field parameter files. These 
    contain the physics parameters for reactive molecular dynamics.`,
  },
  {
    id: 9,
    title: 'You\'re Ready!',
    icon: '🚀',
    content: `That's the basics! Explore, experiment, and have fun watching atoms interact. 
    Remember: smaller time steps = more accurate but slower simulation.`,
  },
];
 
/**
 * Quick reference keyboard shortcuts
 */
const KEYBOARD_SHORTCUTS = [
  { keys: ['↑', '↓', '←', '→'], description: 'Move player atom' },
  { keys: ['Space'], description: 'Pause/Resume simulation' },
  { keys: ['R'], description: 'Reset simulation' },
  { keys: ['B'], description: 'Toggle bond display' },
  { keys: ['P'], description: 'Toggle physics' },
];
 
/**
 * Help Modal Component
 * Provides interactive tutorial and quick reference
 * @param {Object} props - Component props
 * @param {boolean} [props.isOpenExternal] - External open state control
 * @param {Function} [props.onCloseExternal] - External close handler
 * @returns {JSX.Element} Help modal with tutorial
 */
function HelpModal({ isOpenExternal, onCloseExternal }) {
  const [isOpenInternal, setIsOpenInternal] = useState(false);
  const [activeTab, setActiveTab] = useState('tutorial');
  const [currentStep, setCurrentStep] = useState(0);
 
  // Support both internal button and external hamburger menu
  const isOpen = isOpenExternal !== undefined ? isOpenExternal : isOpenInternal;
  const handleOpen = () => setIsOpenInternal(true);
  const handleClose = () => {
    Iif (onCloseExternal) {
      onCloseExternal();
    } else {
      setIsOpenInternal(false);
    }
  };
 
  const handleBackdropClick = (e) => {
    if (e.target.classList.contains('help-modal-backdrop')) {
      handleClose();
    }
  };
 
  // Keyboard navigation
  useEffect(() => {
    if (!isOpen) return;
    
    const handleKeyDown = (e) => {
      Eif (e.key === 'Escape') handleClose();
      Eif (activeTab === 'tutorial') {
        Iif (e.key === 'ArrowRight' || e.key === 'Enter') {
          setCurrentStep(prev => Math.min(prev + 1, TUTORIAL_STEPS.length - 1));
        }
        Iif (e.key === 'ArrowLeft') {
          setCurrentStep(prev => Math.max(prev - 1, 0));
        }
      }
    };
    
    window.addEventListener('keydown', handleKeyDown);
    return () => window.removeEventListener('keydown', handleKeyDown);
  }, [isOpen, activeTab]);
 
  const nextStep = () => {
    if (currentStep < TUTORIAL_STEPS.length - 1) {
      setCurrentStep(currentStep + 1);
    } else {
      handleClose();
    }
  };
 
  const prevStep = () => {
    Eif (currentStep > 0) {
      setCurrentStep(currentStep - 1);
    }
  };
 
  const step = TUTORIAL_STEPS[currentStep];
 
  return (
    <>
      {isOpenExternal === undefined && (
        <button 
          className="help-button" 
          onClick={handleOpen}
          title="Help & Tutorial"
          aria-label="Help"
        >
          ❓ Help
        </button>
      )}
 
      {isOpen && (
        <div 
          className="help-modal-backdrop" 
          onClick={handleBackdropClick}
          role="dialog"
          aria-modal="true"
          aria-labelledby="help-title"
        >
          <div className="help-modal">
            <div className="help-header">
              <h2 id="help-title">Help & Tutorial</h2>
              <button 
                className="help-modal-close" 
                onClick={handleClose}
                aria-label="Close"
              >
                ✕
              </button>
            </div>
 
            <div className="help-tabs">
              <button
                className={`help-tab ${activeTab === 'tutorial' ? 'active' : ''}`}
                onClick={() => setActiveTab('tutorial')}
              >
                📚 Tutorial
              </button>
              <button
                className={`help-tab ${activeTab === 'reference' ? 'active' : ''}`}
                onClick={() => setActiveTab('reference')}
              >
                ⌨️ Quick Reference
              </button>
              <button
                className={`help-tab ${activeTab === 'physics' ? 'active' : ''}`}
                onClick={() => setActiveTab('physics')}
              >
                🔬 Physics Info
              </button>
            </div>
 
            <div className="help-content">
              {activeTab === 'tutorial' && (
                <div className="tutorial-view">
                  <div className="tutorial-progress">
                    {TUTORIAL_STEPS.map((_, index) => (
                      <div
                        key={index}
                        className={`progress-dot ${index === currentStep ? 'active' : ''} ${index < currentStep ? 'completed' : ''}`}
                        onClick={() => setCurrentStep(index)}
                      />
                    ))}
                  </div>
 
                  <div className="tutorial-step">
                    <div className="step-icon">{step.icon}</div>
                    <h3 className="step-title">{step.title}</h3>
                    <p className="step-content">{step.content}</p>
                    <div className="step-counter">
                      Step {currentStep + 1} of {TUTORIAL_STEPS.length}
                    </div>
                  </div>
 
                  <div className="tutorial-nav">
                    <button
                      className="tutorial-btn secondary"
                      onClick={prevStep}
                      disabled={currentStep === 0}
                    >
                      ← Previous
                    </button>
                    <button
                      className="tutorial-btn primary"
                      onClick={nextStep}
                    >
                      {currentStep === TUTORIAL_STEPS.length - 1 ? 'Finish' : 'Next →'}
                    </button>
                  </div>
                </div>
              )}
 
              {activeTab === 'reference' && (
                <div className="reference-view">
                  <h3>Keyboard Shortcuts</h3>
                  <div className="shortcuts-list">
                    {KEYBOARD_SHORTCUTS.map((shortcut, index) => (
                      <div key={index} className="shortcut-item">
                        <div className="shortcut-keys">
                          {shortcut.keys.map((key, i) => (
                            <kbd key={i}>{key}</kbd>
                          ))}
                        </div>
                        <span className="shortcut-desc">{shortcut.description}</span>
                      </div>
                    ))}
                  </div>
 
                  <h3>Mouse Controls</h3>
                  <div className="shortcuts-list">
                    <div className="shortcut-item">
                      <span className="mouse-action">Click</span>
                      <span className="shortcut-desc">Add selected atom type</span>
                    </div>
                    <div className="shortcut-item">
                      <span className="mouse-action">Scroll</span>
                      <span className="shortcut-desc">Zoom in/out (when enabled)</span>
                    </div>
                  </div>
                </div>
              )}
 
              {activeTab === 'physics' && (
                <div className="physics-view">
                  <h3>Physics Formulas & Forces</h3>
                  
                  <div className="physics-section">
                    <h4>🔴 Active Forces in Simulation</h4>
                    <ul className="forces-list">
                      <li><strong>Lennard-Jones Force:</strong> Attractive/repulsive van der Waals interactions between all atom pairs</li>
                      <li><strong>Wall Force:</strong> Harmonic spring force at boundaries (reflective mode)</li>
                      <li><strong>Coulomb Force:</strong> Electrostatic interactions (when enabled)</li>
                      <li><strong>Player Force:</strong> External force applied via arrow keys</li>
                      <li><strong>Thermostat Force:</strong> Berendsen velocity rescaling (when enabled)</li>
                    </ul>
                  </div>
 
                  <div className="physics-section">
                    <h4>📐 Lennard-Jones Potential</h4>
                    <p>Models van der Waals forces (attraction at long range, repulsion at short range):</p>
                    <div className="formula">V(r) = 4ε[(σ/r)¹² - (σ/r)⁶]</div>
                    <div className="formula-desc">
                      <span>ε (epsilon) = well depth (0.185 kcal/mol)</span>
                      <span>σ (sigma) = equilibrium distance (1.91 Å)</span>
                      <span>r = distance between atoms</span>
                    </div>
                    <p>The force is the negative gradient: <strong>F = -dV/dr</strong></p>
                  </div>
 
                  <div className="physics-section">
                    <h4>⚡ Coulomb Potential</h4>
                    <p>Electrostatic interaction between charged particles:</p>
                    <div className="formula">V(r) = k × q₁q₂ / r</div>
                    <div className="formula-desc">
                      <span>k = 332.06 kcal·Å/(mol·e²)</span>
                      <span>q₁, q₂ = partial charges</span>
                    </div>
                  </div>
 
                  <div className="physics-section">
                    <h4>🌡️ Temperature & Kinetic Energy</h4>
                    <p>Temperature from equipartition theorem:</p>
                    <div className="formula">T = (2/3) × KE / (N × kB)</div>
                    <div className="formula-desc">
                      <span>KE = Σ(½mv²) = total kinetic energy</span>
                      <span>kB = 0.001987 kcal/(mol·K)</span>
                      <span>N = number of atoms</span>
                    </div>
                    <div className="formula">KE = ½mv² (per atom)</div>
                  </div>
 
                  <div className="physics-section">
                    <h4>🔄 Berendsen Thermostat</h4>
                    <p>Velocity rescaling to target temperature:</p>
                    <div className="formula">λ = √[1 + (Δt/τ)(T₀/T - 1)]</div>
                    <div className="formula-desc">
                      <span>τ = coupling constant (0.5 ps)</span>
                      <span>T₀ = target temperature</span>
                      <span>T = current temperature</span>
                      <span>v_new = λ × v_old</span>
                    </div>
                  </div>
 
                  <div className="physics-section">
                    <h4>⚙️ Integration Method</h4>
                    <p>Velocity Verlet algorithm for time evolution:</p>
                    <div className="formula">x(t+Δt) = x(t) + v(t)Δt + ½a(t)Δt²</div>
                    <div className="formula">v(t+Δt) = v(t) + ½[a(t) + a(t+Δt)]Δt</div>
                    <div className="formula-desc">
                      <span>Δt = time step (0.05 fs default)</span>
                      <span>a = F/m (Newton's 2nd law)</span>
                    </div>
                  </div>
 
                  <div className="physics-section">
                    <h4>🔗 Bond Order</h4>
                    <p>Calculated from interatomic distance:</p>
                    <div className="formula">BO = exp[-β(r - r₀)]</div>
                    <div className="formula-desc">
                      <span>r₀ = equilibrium bond length</span>
                      <span>β = decay parameter</span>
                      <span>Single: BO ≈ 1, Double: BO ≈ 2, Triple: BO ≈ 3</span>
                    </div>
                  </div>
 
                  <div className="physics-section">
                    <h4>📊 Mean Square Displacement (MSD)</h4>
                    <p>Measures diffusion of atoms over time:</p>
                    <div className="formula">MSD = ⟨|r(t) - r(0)|²⟩</div>
                    <div className="formula-desc">
                      <span>Related to diffusion: MSD = 6Dt (3D)</span>
                      <span>D = diffusion coefficient</span>
                    </div>
                  </div>
 
                  <div className="physics-section">
                    <h4>📈 Radial Distribution Function g(r)</h4>
                    <p>Probability of finding atoms at distance r:</p>
                    <div className="formula">g(r) = n(r) / (ρ × 4πr²dr)</div>
                    <div className="formula-desc">
                      <span>n(r) = atom count in shell [r, r+dr]</span>
                      <span>ρ = number density</span>
                      <span>Peaks indicate coordination shells</span>
                    </div>
                  </div>
 
                  <div className="physics-section">
                    <h4>⚖️ Energy Conservation</h4>
                    <div className="formula">E_total = KE + PE = constant</div>
                    <p>In an isolated system (no thermostat), total energy should be conserved. Deviations indicate numerical error or external forces.</p>
                  </div>
                </div>
              )}
            </div>
          </div>
        </div>
      )}
    </>
  );
}
 
export default HelpModal;