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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//! Code for the analytic MWA beam.
mod error;
mod ffi;
#[cfg(any(feature = "cuda", feature = "hip"))]
mod gpu;
#[cfg(test)]
mod tests;
pub use error::AnalyticBeamError;
#[cfg(any(feature = "cuda", feature = "hip"))]
pub use gpu::AnalyticBeamGpu;
use std::f64::consts::{FRAC_PI_2, TAU};
use marlu::{c64, constants::VEL_C, rayon, AzEl, Jones};
use rayon::prelude::*;
use crate::constants::{DELAY_STEP, MWA_DPL_SEP};
#[cfg(any(feature = "cuda", feature = "hip"))]
use ndarray::prelude::*;
/// Which analytic beam code are we emulating?
#[derive(Clone, Copy, Debug)]
pub enum AnalyticType {
/// Behaviour derived from [mwa_pb](https://github.com/MWATelescope/mwa_pb).
MwaPb,
/// Behaviour derived from the RTS.
Rts,
}
impl AnalyticType {
/// Different analytic beam types use different MWA dipole heights by
/// default. This method returns the default height given a analytic beam
/// type.
pub fn get_default_dipole_height(self) -> f64 {
match self {
AnalyticType::MwaPb => 0.278,
AnalyticType::Rts => 0.30,
}
}
}
/// The main struct to be used for calculating analytic pointings.
pub struct AnalyticBeam {
/// The height of the MWA dipoles we're simulating \[metres\].
///
/// The RTS uses an old value, presumably derived from early MWA dipoles.
/// The up-to-date value is 0.278m, and is used by default.
dipole_height: f64,
/// Which analytic beam code are we emulating?
beam_type: AnalyticType,
/// The number of bowties in a row of an MWA tile. Almost all MWA tiles
/// have 4 bowties per row, for a total of 16 bowties. As of October 2023,
/// the only exception is the CRAM tile, which has 8 bowties per row, for a
/// total of 64 bowties.
pub(crate) bowties_per_row: u8,
}
impl Default for AnalyticBeam {
fn default() -> Self {
let beam_type = AnalyticType::MwaPb;
AnalyticBeam {
dipole_height: beam_type.get_default_dipole_height(),
beam_type,
bowties_per_row: 4,
}
}
}
impl AnalyticBeam {
/// Create a new [`AnalyticBeam`] struct using mwa_pb analytic beam code and
/// the [default](MWA_DPL_HGT) MWA dipole height.
pub fn new() -> AnalyticBeam {
AnalyticBeam::default()
}
/// Create a new [`AnalyticBeam`] struct using RTS analytic beam code and
/// the MWA dipole height from the [RTS](MWA_DPL_HGT_RTS).
pub fn new_rts() -> AnalyticBeam {
let beam_type = AnalyticType::Rts;
AnalyticBeam {
dipole_height: beam_type.get_default_dipole_height(),
beam_type,
bowties_per_row: 4,
}
}
/// Create a new [`AnalyticBeam`] struct with custom behaviour, MWA
/// dipole height and variable bowties per row (you want this to be 4 for
/// normal MWA tiles, 8 for the CRAM).
pub fn new_custom(
beam_type: AnalyticType,
dipole_height_metres: f64,
bowties_per_row: u8,
) -> AnalyticBeam {
if bowties_per_row == 0 {
panic!("bowties_per_row was 0, why would you do that?");
}
// We want the number of bowties to fit in a u8; complain if there are
// too many damn bowties.
if bowties_per_row >= 16 {
panic!("bowties_per_row is restricted to be less than 16");
}
AnalyticBeam {
dipole_height: dipole_height_metres,
beam_type,
bowties_per_row,
}
}
/// Calculate the beam-response Jones matrix for a given direction, pointing
/// and latitude.
///
/// `delays` and `amps` apply to each dipole in an MWA tile in the M&C
/// order; see
/// <https://wiki.mwatelescope.org/pages/viewpage.action?pageId=48005139>.
/// `delays` *must* have `bowties_per_row * bowties_per_row` elements (which
/// was declared when `AnalyticBeam` was created), whereas `amps` can have
/// this number or double elements; if the former is given, then these map
/// 1:1 with bowties. If double are given, then the *smallest* of the two
/// amps corresponding to a bowtie's dipoles is used.
///
/// e.g. A normal MWA tile has 4 bowties per row. `delays` must then have
/// 16 elements, and `amps` can have 16 or 32 elements. A CRAM tile has 8
/// bowties per row; `delays` must have 64 elements, and `amps` can have 64
/// or 128 elements.
pub fn calc_jones(
&self,
azel: AzEl,
freq_hz: u32,
delays: &[u32],
amps: &[f64],
latitude_rad: f64,
norm_to_zenith: bool,
) -> Result<Jones<f64>, AnalyticBeamError> {
self.calc_jones_pair(
azel.az,
azel.za(),
freq_hz,
delays,
amps,
latitude_rad,
norm_to_zenith,
)
}
/// Calculate the beam-response Jones matrix for a given direction and
/// pointing.
///
/// `delays` and `amps` apply to each dipole in an MWA tile in the M&C
/// order; see
/// <https://wiki.mwatelescope.org/pages/viewpage.action?pageId=48005139>.
/// `delays` *must* have `bowties_per_row * bowties_per_row` elements (which
/// was declared when `AnalyticBeam` was created), whereas `amps` can have
/// this number or double elements; if the former is given, then these map
/// 1:1 with bowties. If double are given, then the *smallest* of the two
/// amps corresponding to a bowtie's dipoles is used.
///
/// e.g. A normal MWA tile has 4 bowties per row. `delays` must then have
/// 16 elements, and `amps` can have 16 or 32 elements. A CRAM tile has 8
/// bowties per row; `delays` must have 64 elements, and `amps` can have 64
/// or 128 elements.
#[allow(clippy::too_many_arguments)]
pub fn calc_jones_pair(
&self,
az_rad: f64,
za_rad: f64,
freq_hz: u32,
delays: &[u32],
amps: &[f64],
latitude_rad: f64,
norm_to_zenith: bool,
) -> Result<Jones<f64>, AnalyticBeamError> {
if za_rad > FRAC_PI_2 {
return Err(AnalyticBeamError::BelowHorizon { za: za_rad });
}
let num_bowties = usize::from(self.bowties_per_row * self.bowties_per_row);
if delays.len() != num_bowties {
return Err(AnalyticBeamError::IncorrectDelaysLength {
got: delays.len(),
expected: num_bowties,
});
}
if amps.len() != num_bowties && amps.len() != num_bowties * 2 {
return Err(AnalyticBeamError::IncorrectAmpsLength {
got: amps.len(),
expected1: num_bowties,
expected2: num_bowties * 2,
});
}
let amps = fix_amps(amps, delays);
let (amps, delays) = if matches!(self.beam_type, AnalyticType::Rts) {
reorder_to_rts(&s, delays)
} else {
(amps.to_vec(), delay_ints_to_floats(delays))
};
let lambda_m = VEL_C / freq_hz as f64;
let (s_lat, c_lat) = latitude_rad.sin_cos();
let jones = self.calc_jones_inner(
az_rad,
za_rad,
lambda_m,
latitude_rad,
s_lat,
c_lat,
&delays,
&s,
norm_to_zenith,
);
Ok(jones)
}
/// Calculate the beam-response Jones matrices for many directions
/// given a pointing and latitude. This is basically a wrapper around
/// `calc_jones` that efficiently calculates the Jones matrices in
/// parallel. The number of parallel threads used can be controlled by
/// setting `RAYON_NUM_THREADS`.
///
/// `delays` and `amps` apply to each dipole in an MWA tile in the M&C
/// order; see
/// <https://wiki.mwatelescope.org/pages/viewpage.action?pageId=48005139>.
/// `delays` *must* have `bowties_per_row * bowties_per_row` elements (which
/// was declared when `AnalyticBeam` was created), whereas `amps` can have
/// this number or double elements; if the former is given, then these map
/// 1:1 with bowties. If double are given, then the *smallest* of the two
/// amps corresponding to a bowtie's dipoles is used.
///
/// e.g. A normal MWA tile has 4 bowties per row. `delays` must then have
/// 16 elements, and `amps` can have 16 or 32 elements. A CRAM tile has 8
/// bowties per row; `delays` must have 64 elements, and `amps` can have 64
/// or 128 elements.
pub fn calc_jones_array(
&self,
azels: &[AzEl],
freq_hz: u32,
delays: &[u32],
amps: &[f64],
latitude_rad: f64,
norm_to_zenith: bool,
) -> Result<Vec<Jones<f64>>, AnalyticBeamError> {
let mut results = vec![Jones::default(); azels.len()];
self.calc_jones_array_inner(
azels,
freq_hz,
delays,
amps,
latitude_rad,
norm_to_zenith,
&mut results,
)?;
Ok(results)
}
/// Calculate the Jones matrices for many directions given a pointing and
/// latitude. This is the same as `calc_jones_array` but uses pre-allocated
/// memory.
///
/// `delays` and `amps` apply to each dipole in an MWA tile in the M&C
/// order; see
/// <https://wiki.mwatelescope.org/pages/viewpage.action?pageId=48005139>.
/// `delays` *must* have `bowties_per_row * bowties_per_row` elements (which
/// was declared when `AnalyticBeam` was created), whereas `amps` can have
/// this number or double elements; if the former is given, then these map
/// 1:1 with bowties. If double are given, then the *smallest* of the two
/// amps corresponding to a bowtie's dipoles is used.
///
/// e.g. A normal MWA tile has 4 bowties per row. `delays` must then have
/// 16 elements, and `amps` can have 16 or 32 elements. A CRAM tile has 8
/// bowties per row; `delays` must have 64 elements, and `amps` can have 64
/// or 128 elements.
#[allow(clippy::too_many_arguments)]
pub fn calc_jones_array_inner(
&self,
azels: &[AzEl],
freq_hz: u32,
delays: &[u32],
amps: &[f64],
latitude_rad: f64,
norm_to_zenith: bool,
results: &mut [Jones<f64>],
) -> Result<(), AnalyticBeamError> {
for azel in azels {
let za = azel.za();
if za > FRAC_PI_2 {
return Err(AnalyticBeamError::BelowHorizon { za });
}
}
let num_bowties = usize::from(self.bowties_per_row * self.bowties_per_row);
if delays.len() != num_bowties {
return Err(AnalyticBeamError::IncorrectDelaysLength {
got: delays.len(),
expected: num_bowties,
});
}
if amps.len() != num_bowties && amps.len() != num_bowties * 2 {
return Err(AnalyticBeamError::IncorrectAmpsLength {
got: amps.len(),
expected1: num_bowties,
expected2: num_bowties * 2,
});
}
let amps = fix_amps(amps, delays);
let (amps, delays) = if matches!(self.beam_type, AnalyticType::Rts) {
reorder_to_rts(&s, delays)
} else {
(amps.to_vec(), delay_ints_to_floats(delays))
};
let lambda_m = VEL_C / freq_hz as f64;
let (s_lat, c_lat) = latitude_rad.sin_cos();
azels
.par_iter()
.zip(results.par_iter_mut())
.try_for_each(|(&azel, result)| {
if azel.za() > FRAC_PI_2 {
return Err(AnalyticBeamError::BelowHorizon { za: azel.za() });
}
let j = self.calc_jones_inner(
azel.az,
azel.za(),
lambda_m,
latitude_rad,
s_lat,
c_lat,
&delays,
&s,
norm_to_zenith,
);
*result = j;
Ok(())
})
}
/// Calculate the beam-response Jones matrices for many directions given a
/// pointing. This is basically a wrapper around `calc_jones` that
/// efficiently calculates the Jones matrices in parallel. The number of
/// parallel threads used can be controlled by setting `RAYON_NUM_THREADS`.
///
/// `delays` and `amps` apply to each dipole in an MWA tile in the M&C
/// order; see
/// <https://wiki.mwatelescope.org/pages/viewpage.action?pageId=48005139>.
/// `delays` *must* have `bowties_per_row * bowties_per_row` elements (which
/// was declared when `AnalyticBeam` was created), whereas `amps` can have
/// this number or double elements; if the former is given, then these map
/// 1:1 with bowties. If double are given, then the *smallest* of the two
/// amps corresponding to a bowtie's dipoles is used.
///
/// e.g. A normal MWA tile has 4 bowties per row. `delays` must then have
/// 16 elements, and `amps` can have 16 or 32 elements. A CRAM tile has 8
/// bowties per row; `delays` must have 64 elements, and `amps` can have 64
/// or 128 elements.
#[allow(clippy::too_many_arguments)]
pub fn calc_jones_array_pair(
&self,
az_rad: &[f64],
za_rad: &[f64],
freq_hz: u32,
delays: &[u32],
amps: &[f64],
latitude_rad: f64,
norm_to_zenith: bool,
) -> Result<Vec<Jones<f64>>, AnalyticBeamError> {
for &za in za_rad {
if za > FRAC_PI_2 {
return Err(AnalyticBeamError::BelowHorizon { za });
}
}
let num_bowties = usize::from(self.bowties_per_row * self.bowties_per_row);
if delays.len() != num_bowties {
return Err(AnalyticBeamError::IncorrectDelaysLength {
got: delays.len(),
expected: num_bowties,
});
}
if amps.len() != num_bowties && amps.len() != num_bowties * 2 {
return Err(AnalyticBeamError::IncorrectAmpsLength {
got: amps.len(),
expected1: num_bowties,
expected2: num_bowties * 2,
});
}
let amps = fix_amps(amps, delays);
let (amps, delays) = if matches!(self.beam_type, AnalyticType::Rts) {
reorder_to_rts(&s, delays)
} else {
(amps.to_vec(), delay_ints_to_floats(delays))
};
let lambda_m = VEL_C / freq_hz as f64;
let (s_lat, c_lat) = latitude_rad.sin_cos();
let out = az_rad
.par_iter()
.zip(za_rad.par_iter())
.map(|(&az, &za)| {
self.calc_jones_inner(
az,
za,
lambda_m,
latitude_rad,
s_lat,
c_lat,
&delays,
&s,
norm_to_zenith,
)
})
.collect();
Ok(out)
}
/// Calculate the Jones matrices for many directions given a pointing. This
/// is the same as `calc_jones_array_pair` but uses pre-allocated memory.
///
/// `delays` and `amps` apply to each dipole in an MWA tile in the M&C
/// order; see
/// <https://wiki.mwatelescope.org/pages/viewpage.action?pageId=48005139>.
/// `delays` *must* have `bowties_per_row * bowties_per_row` elements (which
/// was declared when `AnalyticBeam` was created), whereas `amps` can have
/// this number or double elements; if the former is given, then these map
/// 1:1 with bowties. If double are given, then the *smallest* of the two
/// amps corresponding to a bowtie's dipoles is used.
///
/// e.g. A normal MWA tile has 4 bowties per row. `delays` must then have
/// 16 elements, and `amps` can have 16 or 32 elements. A CRAM tile has 8
/// bowties per row; `delays` must have 64 elements, and `amps` can have 64
/// or 128 elements.
#[allow(clippy::too_many_arguments)]
pub fn calc_jones_array_pair_inner(
&self,
az_rad: &[f64],
za_rad: &[f64],
freq_hz: u32,
delays: &[u32],
amps: &[f64],
latitude_rad: f64,
norm_to_zenith: bool,
results: &mut [Jones<f64>],
) -> Result<(), AnalyticBeamError> {
for &za in za_rad {
if za > FRAC_PI_2 {
return Err(AnalyticBeamError::BelowHorizon { za });
}
}
let num_bowties = usize::from(self.bowties_per_row * self.bowties_per_row);
if delays.len() != num_bowties {
return Err(AnalyticBeamError::IncorrectDelaysLength {
got: delays.len(),
expected: num_bowties,
});
}
if amps.len() != num_bowties && amps.len() != num_bowties * 2 {
return Err(AnalyticBeamError::IncorrectAmpsLength {
got: amps.len(),
expected1: num_bowties,
expected2: num_bowties * 2,
});
}
let amps = fix_amps(amps, delays);
let (amps, delays) = if matches!(self.beam_type, AnalyticType::Rts) {
reorder_to_rts(&s, delays)
} else {
(amps.to_vec(), delay_ints_to_floats(delays))
};
let lambda_m = VEL_C / freq_hz as f64;
let (s_lat, c_lat) = latitude_rad.sin_cos();
az_rad
.par_iter()
.zip(za_rad.par_iter())
.zip(results.par_iter_mut())
.try_for_each(|((&az, &za), result)| {
if za > FRAC_PI_2 {
return Err(AnalyticBeamError::BelowHorizon { za });
}
let j = self.calc_jones_inner(
az,
za,
lambda_m,
latitude_rad,
s_lat,
c_lat,
&delays,
&s,
norm_to_zenith,
);
*result = j;
Ok(())
})
}
/// Helper function.
// The code here was derived with the help of primary_beam.py in mwa_pb,
// commit 8619797, and Jack's WODEN.
#[allow(clippy::too_many_arguments)]
fn calc_jones_inner(
&self,
az_rad: f64,
za_rad: f64,
lambda_m: f64,
latitude_rad: f64,
sin_latitude: f64,
cos_latitude: f64,
delays: &[f64],
amps: &[f64],
norm_to_zenith: bool,
) -> Jones<f64> {
// The following logic could probably be significantly cleaned up, but
// I'm out of time.
let (s_az, c_az) = az_rad.sin_cos();
let (s_za, c_za) = za_rad.sin_cos();
let mut jones = match self.beam_type {
AnalyticType::MwaPb => Jones::from([
c64::new(c_za * s_az, 0.0),
c64::new(c_az, 0.0),
c64::new(c_za * c_az, 0.0),
c64::new(-s_az, 0.0),
]),
AnalyticType::Rts => {
let hadec = AzEl::from_radians(az_rad, FRAC_PI_2 - za_rad).to_hadec(latitude_rad);
let (s_ha, c_ha) = hadec.ha.sin_cos();
let (s_dec, c_dec) = hadec.dec.sin_cos();
Jones::from([
c64::new(cos_latitude * c_dec + sin_latitude * s_dec * c_ha, 0.0),
c64::new(-sin_latitude * s_ha, 0.0),
c64::new(s_dec * s_ha, 0.0),
c64::new(c_ha, 0.0),
])
}
};
let proj_e = s_za * s_az;
let proj_n = s_za * c_az;
// The RTS code uses proj_z as below, but dip_z is always set to 0.0, so
// we don't actually need proj_z. lmao
// let proj_z = c_za;
let multiplier = -TAU / lambda_m;
// Loop over each dipole.
let mut array_factor = c64::new(0.0, 0.0);
for (k, (&delay, &)) in delays.iter().zip(amps.iter()).enumerate() {
let col = k % usize::from(self.bowties_per_row);
let row = k / usize::from(self.bowties_per_row);
let (dip_e, dip_n) = match self.beam_type {
AnalyticType::MwaPb => (
(col as f64 - 1.5) * MWA_DPL_SEP,
(row as f64 - 1.5) * MWA_DPL_SEP,
),
AnalyticType::Rts => (
(row as f64 - 1.5) * MWA_DPL_SEP,
(col as f64 - 1.5) * MWA_DPL_SEP,
),
};
// let dip_z = 0.0;
let phase = match self.beam_type {
AnalyticType::MwaPb => {
-multiplier
* (dip_e * proj_e
+ dip_n * proj_n
// + dip_z * proj_z
- delay)
}
AnalyticType::Rts => {
multiplier
* (dip_e * proj_e
+ dip_n * proj_n
// + dip_z * proj_z
- delay)
}
};
let (s_phase, c_phase) = phase.sin_cos();
array_factor += amp * c64::new(c_phase, s_phase);
}
let mut ground_plane = 2.0 * (TAU * self.dipole_height / lambda_m * c_za).sin()
/ usize::from(self.bowties_per_row).pow(2) as f64;
if norm_to_zenith {
ground_plane /= 2.0 * (TAU * self.dipole_height / lambda_m).sin();
}
jones[0] *= ground_plane * array_factor;
jones[1] *= ground_plane * array_factor;
jones[2] *= ground_plane * array_factor;
jones[3] *= ground_plane * array_factor;
// The RTS deliberately sets the imaginary parts to 0.
if matches!(self.beam_type, AnalyticType::Rts) {
for j in jones.iter_mut() {
*j = c64::new(j.re, 0.0);
}
}
jones
}
/// Prepare a compute-capable GPU device for beam-response computations
/// given the delays and amps to be used. The resulting object takes
/// directions and frequencies to compute the beam responses on the device.
///
/// `delays_array` and `amps_array` must have the same number of rows;
/// these correspond to tile configurations (i.e. each tile is allowed
/// to have distinct delays and amps). The number of elements per row of
/// `delays_array` and `amps_array` have the same restrictions as `delays`
/// and `amps` in `calc_jones`.
///
/// The code will automatically de-duplicate tile configurations so that no
/// redundant calculations are done.
///
/// # Safety
///
/// This function interfaces directly with the CUDA/HIP API. Rust errors
/// attempt to catch problems but there are no guarantees.
#[cfg(any(feature = "cuda", feature = "hip"))]
pub unsafe fn gpu_prepare(
&self,
delays: ArrayView2<u32>,
amps: ArrayView2<f64>,
) -> Result<gpu::AnalyticBeamGpu, AnalyticBeamError> {
// This function is deliberately kept thin to keep the focus of this
// module on the CPU code.
gpu::AnalyticBeamGpu::new(self, delays, amps)
}
}
/// Ensure that any delays of 32 have an amplitude (dipole gain) of 0. The
/// results are bad otherwise! Also potentially halve the number of amps (e.g.
/// if 32 are given for a 16-bowtie tile, yield 16); we use the smaller of the
/// two gains associated with a bowtie.
fn fix_amps(amps: &[f64], delays: &[u32]) -> Vec<f64> {
// The lengths of `amps` and `delays` should be checked before calling this
// functions; the asserts are a last resort guard.
assert!(amps.len() == delays.len() || amps.len() == delays.len() * 2);
let mut fixed_amps = vec![0.0; delays.len()];
fixed_amps
.iter_mut()
.zip(amps.iter())
.zip(delays.iter())
.for_each(|((fixed, &), &delay)| *fixed = if delay == 32 { 0.0 } else { amp });
if amps.len() == delays.len() * 2 {
fixed_amps
.iter_mut()
.zip(amps.iter().skip(delays.len()))
.for_each(|(fixed, &)| {
*fixed = fixed.min(amp);
});
}
fixed_amps
}
/// The RTS doesn't use the M&C order. This function takes in the M&C-ordered
/// amps and delays, and returns RTS-ordered amps and delays. It also does
/// extra... things.
// Several thousand upside down emojis.
fn reorder_to_rts(amps: &[f64], delays: &[u32]) -> (Vec<f64>, Vec<f64>) {
// Assume that the number of delays is the number of bowties.
let num_bowties = delays.len();
// Get the number of bowties per row from the number of bowties. This
// assumes that the number is a perfect square.
let bowties_per_row = (num_bowties as f64).sqrt().round() as usize;
assert_eq!(bowties_per_row * bowties_per_row, num_bowties);
let mut indices = Vec::with_capacity(num_bowties);
for i_col in 0..bowties_per_row {
for i_row in (0..bowties_per_row).rev() {
indices.push(i_row * bowties_per_row + i_col);
}
}
// Convert to "RTS order".
let mut rts_amps = vec![0.0; num_bowties];
let mut rts_delays = vec![0.0; num_bowties];
indices
.into_iter()
.zip(rts_amps.iter_mut())
.zip(rts_delays.iter_mut())
.for_each(|((i, rts_amp), rts_delay)| {
*rts_amp = amps[i];
*rts_delay = f64::from(delays[i]);
});
// Do this crazy stuff.
let delay_0 = rts_delays.iter().sum::<f64>() * VEL_C * DELAY_STEP / num_bowties as f64;
rts_delays.iter_mut().for_each(|d| {
*d = *d * VEL_C * DELAY_STEP - delay_0;
});
(rts_amps, rts_delays)
}
fn delay_ints_to_floats(delays: &[u32]) -> Vec<f64> {
delays
.iter()
.copied()
.map(|d| d as f64 * VEL_C * DELAY_STEP)
.collect()
}