shape-runtime 0.2.0

Bytecode compiler, builtins, and runtime infrastructure for Shape
Documentation
/// @module std::physics::simulation
/// Physics Simulations
///
/// Convenience wrappers for iterating mechanics step functions.

from std::physics::mechanics use { projectile_step, spring_mass_step, n_body_step }

/// Simulate projectile motion until t_end or y < 0
pub fn simulate_projectile(initial_state, t_end, dt, g = 9.81) {
    let mut state = initial_state;
    let mut results = [];

    while state.t <= t_end && state.y >= 0.0 {
        results.push(state);
        state = projectile_step(state, dt, g);
    }

    results
}

/// Simulate spring-mass oscillator for a fixed duration
pub fn simulate_oscillator(initial_state, k, m, t_end, dt, damping = 0.0) {
    let steps = floor(t_end / dt);
    let mut state = initial_state;
    let mut results = [];

    for i in range(0, steps + 1) {
        results.push(state);
        state = spring_mass_step(state, k, m, dt, damping);
    }

    results
}

/// Simulate n-body system for a number of steps
pub fn simulate_n_body(particles, steps, dt, G = 1.0) {
    let mut state = particles;
    let mut results = [];

    for i in range(0, steps + 1) {
        results.push(state);
        state = n_body_step(state, dt, G);
    }

    results
}