()
| 12 | } |
| 13 | |
| 14 | fn acoustic_wave_simulation() { |
| 15 | // Speed of sound |
| 16 | let c: f32 = 0.1; |
| 17 | // Distance step |
| 18 | let dx: f32 = 0.5; |
| 19 | // Time step |
| 20 | let dt: f32 = 1.0; |
| 21 | |
| 22 | // Grid size. |
| 23 | let nx: u64 = 1500; |
| 24 | let ny: u64 = 1500; |
| 25 | |
| 26 | // Grid dimensions. |
| 27 | let dims = Dim4::new(&[nx, ny, 1, 1]); |
| 28 | |
| 29 | // Pressure field |
| 30 | let mut p = constant::<f32>(0.0, dims); |
| 31 | // d(pressure)/dt field |
| 32 | let mut p_dot = p.clone(); |
| 33 | |
| 34 | // Laplacian (Del^2) convolution kernel. |
| 35 | let laplacian_values: [f32; 9] = [0.0, 1.0, 0.0, 1.0, -4.0, 1.0, 0.0, 1.0, 0.0]; |
| 36 | let laplacian_kernel = Array::new(&laplacian_values, Dim4::new(&[3, 3, 1, 1])) / (dx * dx); |
| 37 | |
| 38 | // Create a window to show the waves. |
| 39 | let mut win = Window::new(1000, 1000, "Waves".to_string()); |
| 40 | |
| 41 | // Hann windowed pulse. |
| 42 | let pulse_time: f32 = 100.0; |
| 43 | let centre_freq: f32 = 0.05; |
| 44 | let twopi = PI as f32 * 2.0; |
| 45 | |
| 46 | // Number of samples in pulse. |
| 47 | let pulse_n = (pulse_time / dt).floor() as u64; |
| 48 | |
| 49 | let i = range::<f32>(Dim4::new(&[pulse_n, 1, 1, 1]), 0); |
| 50 | let t = i.clone() * dt; |
| 51 | let hmg_wnd = cos(&(i * (twopi / pulse_n as f32))) * -0.46f32 + 0.54f32; |
| 52 | let wave = sin(&(&t * centre_freq * twopi)); |
| 53 | let pulse = wave * hmg_wnd; |
| 54 | |
| 55 | // Iteration count. |
| 56 | let mut it = 0; |
| 57 | |
| 58 | while !win.is_closed() { |
| 59 | // Convole with laplacian to get spacial second derivative. |
| 60 | let lap_p = convolve2( |
| 61 | &p, |
| 62 | &laplacian_kernel, |
| 63 | ConvMode::DEFAULT, |
| 64 | ConvDomain::SPATIAL, |
| 65 | ); |
| 66 | // Calculate the updated pressure and d(pressure)/dt fields. |
| 67 | p_dot += lap_p * (c * dt); |
| 68 | p += &p_dot * dt; |
| 69 | |
| 70 | if it < pulse_n { |
| 71 | // Location of the source. |
no test coverage detected