Waves in a Box
Waves travel through a box with two openings in an internal wall. Use a slice or cutaway to see inside. A simple wave model.
Published
No video preview yet
Source
use "@hyle/maths";
// A scalar wave field occupies a complete 16 x 16 x 16 volume.
// The separate acceleration and integration phases use consistent neighbor states.
property Acoustic { displacement: Float = 0.0; velocity: Float = 0.0; damping: Float = 0.998; }
property Pulse { started: Float = 0.0; }
property Solid { density: Float = 1.0; }
model Absorber : Acoustic { damping = 0.90; };
model Barrier : Solid;
model AcousticCrest : Acoustic { displacement = 0.9; };
model AcousticMedium : Acoustic;
model Trough : Acoustic { displacement = 0.9; } + Pulse;
world { dimensions = 3; neighborhood = VonNeumann; }
// Six-neighbor coupling propagates along X, Y and Z. Solid cells are excluded,
// producing reflective interfaces; the outer absorbing layer dissipates energy.
behavior AcousticAcceleration for Acoustic {
update {
let curvature = sum n in neighbors[Acoustic] { n.displacement - self.displacement };
set self.velocity = maths::clip((self.velocity + curvature * 0.12) * self.damping, -0.6, 0.6);
}
}
behavior AcousticIntegration for Acoustic {
update { set self.displacement = maths::clip(self.displacement + self.velocity, -1.0, 1.0); }
}
// Reverse this source once before wave propagation, preserving the initial
// positive display and the existing first-step excitation.
behavior ReversePulse for Acoustic + Pulse {
update when self.started < 0.5 {
set self.displacement = -self.displacement;
set self.started = 1.0;
}
}
phase excitation { run ReversePulse on Trough; }
phase acceleration {
run AcousticAcceleration on Absorber;
run AcousticAcceleration on AcousticCrest;
run AcousticAcceleration on AcousticMedium;
run AcousticAcceleration on Trough;
}
phase integration {
run AcousticIntegration on Absorber;
run AcousticIntegration on AcousticCrest;
run AcousticIntegration on AcousticMedium;
run AcousticIntegration on Trough;
}
// Cyan and coral show opposite displacement; quiet material remains translucent.
// Use View options > Single layer, or Cutaway, to reveal the interior wavefronts.
visual PressureLight for Acoustic {
color {
let positive = maths::clip(self.displacement * 5.0, 0.0, 1.0);
let negative = maths::clip(-self.displacement * 5.0, 0.0, 1.0);
rgba(0.10 + positive * 0.88, 0.18 + negative * 0.72, 0.35 + negative * 0.60, 0.14 + maths::clip(maths::abs(self.displacement) * 4.0, 0.0, 0.72))
}
}
visual ReflectorLight for Solid { color { rgba(0.62, 0.54, 0.30, 0.88) } }
visualize Absorber with PressureLight;
visualize Barrier with ReflectorLight;
visualize AcousticCrest with PressureLight;
visualize AcousticMedium with PressureLight;
visualize Trough with PressureLight;