This document provides detailed documentation for the codebase.
11×11 geometric exponential spring: force on mass uA, uB; R0 is the reference chord from GRID_SPACING).
Returns: Force on
Force law:
Older files may still define this helper using spring_force_2d_geometric for lattice_simulation_11x11.jl.
Calculate 2D linear spring force (Hooke's law).
Parameters:
pos1: Position vector of mass 1[x, y]pos2: Position vector of mass 2[x, y]k: Spring constant (N/m)
Returns: Force vector on mass 1 due to mass 2 [Fx, Fy]
Force Law:
Calculate 2D damping force between two masses.
Parameters:
vel1: Velocity vector of mass 1[vx, vy]vel2: Velocity vector of mass 2[vx, vy]c: Damping coefficient (N·s/m)
Returns: Damping force vector on mass 1 [Fx, Fy]
Force Law:
Calculate x and y components of force from magnitude and angle.
Parameters:
magnitude: Force magnitude (N)angle_degrees: Angle in degrees (0-360)- 0° = +x direction (right)
- 90° = +y direction (up)
- 180° = -x direction (left)
- 270° = -y direction (down)
Returns: Tuple (fx, fy) - Force components
Main RHS for the 11×11 system: geometric NN springs, column-based p = (material_order=..., materials=...); otherwise uses default column scaling.
State vector: positions then velocities (2 DOF per mass).
Modifies: du in-place.
Legacy name in older lattice files; not used by lattice_simulation_11x11.jl.
Calculate total kinetic energy for 2D motion.
Parameters:
vel_matrix: 2×N matrix where each column is[vx, vy]for one mass
Returns: Total kinetic energy (J)
Formula:
Total potential energy for the 11×11 model (geometric NN springs + wall penalty). pos_matrix holds displacements. Pass p to match a custom material ordering, same as the RHS.
Legacy name in older scripts.
Calculate total work done by external force.
Parameters:
sol: ODE solution from DifferentialEquations.jl
Returns: Total work (J)
Formula:
Create equilibrium positions for the lattice.
Returns: 2×N matrix of equilibrium positions
Grid: Square grid with spacing GRID_SPACING
Undirected nearest-neighbor bonds for visualization (horizontal + vertical).
Returns: Vector{Tuple{Int,Int}} of mass index pairs.
Legacy helper returning NN and diagonal lists; not defined in lattice_simulation_11x11.jl.
Main simulation function with configurable force angle and application point.
Returns: Tuple (fig, sol, total_work, final_energy)
fig: GLMakie figure objectsol: ODE solutiontotal_work: Work done by external forcefinal_energy: Final total energy
Side Effects:
- Prints simulation parameters and results
- Displays interactive animation window
Save the animation to a video file.
Parameters:
filename: Output filename (default:"lattice_anim.mp4")
Side Effects: Creates MP4 file in animations directory
Convert 2D lattice coordinates to 1D index.
Parameters:
i: Row index (1 to N)j: Column index (1 to N)
Returns: Mass index (1 to N²)
Convert 1D index to row coordinate.
Parameters:
k: Mass index (1 to N²)
Returns: Row index (1 to N)
Convert 1D index to column coordinate.
Parameters:
k: Mass index (1 to N²)
Returns: Column index (1 to N)
MASS: Mass per particle (kg)K_COUPLING: Nearest neighbor spring constantK_DIAGONAL: Diagonal spring constantALPHA_COUPLING: Exponential decay rate for nearest neighbors (m⁻¹)ALPHA_DIAGONAL: Exponential decay rate for diagonals (m⁻¹)C_DAMPING: Damping coefficient (N·s/m)
FORCE_ANGLE_DEGREES: Force direction angleFORCE_TARGET_ROW: Target mass rowFORCE_TARGET_COL: Target mass columnF_MAG: Force magnitude (N)F_ACTIVE_TIME: Force duration (s)
N: Lattice size (N×N grid)TOTAL_MASSES: Total number of massesDOF_PER_MASS: Degrees of freedom per mass (2 for 2D)TOTAL_DOF: Total degrees of freedom
T_END: Simulation end time (s)OUTPUT_INTERVAL: Time between output points (s)REL_TOL: Relative tolerance for solverABS_TOL: Absolute tolerance for solver
ANIMATION_SPEED: Playback speed multiplierNODE_SIZE: Size of lattice nodesSPRING_WIDTH: Width of spring linesDIAGONAL_SPRING_WIDTH: Width of diagonal springsGRID_SPACING: Spacing between equilibrium positions
src/lattice_simulation_11x11.jl: Main 11×11 lattice simulation with backplate, material scaling, and distributed loadDeprecated Scripts/: Older simulation versions (5×5, 10×10) for referencescripts/optimization/: Material ordering optimization scriptstests/runtests.jl: Test suite
DifferentialEquations: ODE solvingGLMakie/CairoMakie: Visualization and animationOptim,Evolutionary,Metaheuristics,BlackBoxOptim: Optimization algorithmsLinearAlgebra,Printf,Statistics,Dates,DelimitedFiles: Standard libraries
LinearAlgebraPrintf
using Pkg
Pkg.activate(".")
include("src/lattice_simulation_11x11.jl")
# The simulation runs automatically when the file is included
# Modify constants in the file as needed before runningusing Pkg
Pkg.activate(".")
include("scripts/optimization/compare_optimizers.jl")
# Or use the bash script:
# ./scripts/optimization/run_overnight.sh 50- All functions use SI units (meters, kilograms, seconds, Newtons)
- Angles are specified in degrees for user convenience
- The state vector uses a flattened representation for efficiency
- Visualization requires OpenGL support