rust-roche 0.4.4

Rust translation of Tom Marsh's cpp-roche package for modelling Roche distorted stars/binary systems.
Documentation
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
use crate::errors::RocheError;
use crate::{Etype, Star, Vec3};
use crate::{dbrent, pot_min, x_l1, x_l1_1, x_l1_2, x_l2, x_l3};
use crate::{drpot1, drpot2, rpot_grad, rpot_val, rpot1, rpot2};
use crate::{set_earth, sphere_eclipse, sphere_eclipse_vector};
use std::f64::consts::TAU;

///
/// RocheContext is a basic struct packaging the mass ratio, q=M2/M1,
/// the star (i.e. the Primary or Secondary), the spin of that star,
/// and the x-coordinate of the L1 point together with the functions
/// that accept these values as inputs. x_l1 is calculated on initialisation
/// with RocheContext::new(q, star, spin) and saves it being recalculated
/// over and over in loops.
///
pub struct RocheContext {
    pub q: f64,
    pub star: Star,
    pub spin: f64,
    pub x_l1: f64,
}

impl RocheContext {
    pub fn new(q: f64, star: Star, spin: f64) -> Result<Self, RocheError> {
        let x_l1: f64 = match star {
            Star::Primary => x_l1_1(q, spin)?,
            Star::Secondary => x_l1_2(q, spin)?,
        };
        Ok(Self {
            q,
            star,
            spin,
            x_l1,
        })
    }

    pub fn potential(&self, earth: &Vec3, p: &Vec3, lam: f64) -> Result<f64, RocheError> {
        rpot_val(self.q, self.star, self.spin, earth, p, lam)
    }

    pub fn gradient(&self, earth: &Vec3, p: &Vec3, lam: f64) -> Result<(f64, f64), RocheError> {
        let (dp, dl) = rpot_grad(self.q, self.star, self.spin, earth, p, lam)?;
        Ok((dp, dl))
    }

    pub fn potential_grad(
        &self,
        earth: &Vec3,
        p: &Vec3,
        lam: f64,
    ) -> Result<(f64, f64, f64), RocheError> {
        let f = self.potential(earth, p, lam)?;
        let (dp, dl) = rpot_grad(self.q, self.star, self.spin, earth, p, lam)?;
        Ok((f, dp, dl))
    }

    pub fn ref_sphere(&self, ffac: f64) -> Result<(f64, f64), RocheError> {
        let tref: f64;
        let rref: f64;
        let pref: f64;
        if self.star == Star::Primary {
            tref = self.x_l1;
            rref = tref * 1.0_f64.min(1.001 * ffac);
            pref = rpot1(
                self.q,
                self.spin,
                &Vec3 {
                    x: ffac * tref,
                    y: 0.0,
                    z: 0.0,
                },
            )?;
            Ok((rref, pref))
        } else if self.star == Star::Secondary {
            tref = 1.0 - self.x_l1;
            rref = tref * 1.0_f64.min(1.001 * ffac);
            pref = rpot2(
                self.q,
                self.spin,
                &Vec3 {
                    x: 1.0 - ffac * tref,
                    y: 0.0,
                    z: 0.0,
                },
            )?;
            Ok((rref, pref))
        } else {
            let message = format!("{:?} is not and instance of Star.", self.star);
            Err(RocheError::ParameterError(message))
        }
    }

    pub fn fblink(&self, ffac: f64, acc: f64, earth: &Vec3, p: &Vec3) -> Result<bool, RocheError> {
        let (rref, pref) = self.ref_sphere(ffac)?;

        let cofm: Vec3 = match self.star {
            Star::Primary => Vec3::cofm1(),
            Star::Secondary => Vec3::cofm2(),
        };

        // First compute the multipliers cutting the reference sphere (if any)
        let mut lam1 = 0.0;
        let mut lam2 = 0.0;
        if !sphere_eclipse_vector(earth, p, &cofm, rref, &mut lam1, &mut lam2) {
            return Ok(false);
        }
        if lam1 == 0.0 {
            return Ok(true);
        }

        // Create function objects for 1D minimisation in lambda direction
        let func = |lam: f64| self.potential(earth, p, lam);

        // Now try to bracket a minimum. We just crudely compute function at regularly spaced intervals filling in the
        // gaps until the step size between the points drops below the threshold. Take every opportunity to jump out early
        // either if the potential is below the threshold or if we have bracketed a minimum.
        let mut nstep: i32 = 1;
        let mut step: f64 = lam2 - lam1;

        let mut f1: f64 = 0.0;
        let mut f2: f64 = 0.0;
        let mut flam: f64 = 1.0;
        let mut lam: f64 = lam1;

        while step > acc {
            lam = lam1 + step / 2.0;

            for _ in 0..nstep {
                flam = func(lam)?;
                if flam <= pref {
                    return Ok(true);
                }

                // Calculate these as late as possible because they may often not be needed
                if nstep == 1 {
                    f1 = func(lam1)?;
                    f2 = func(lam2)?;
                }

                if flam < f1 && flam < f2 {
                    break;
                }

                lam += step;
            }
            if flam < f1 && flam < f2 {
                break;
            }
            step /= 2.0;
            nstep *= 2;
        }

        if flam < f1 && flam < f2 {
            // OK, minimum bracketted, so finally pin it down accurately
            // Possible that multiple minima could cause problems but I have
            // never seen this in practice.
            let dfunc = |lam: f64| {
                let (_dp, dl) = self.gradient(earth, p, lam)?;
                Ok(dl)
            };

            let (_xmin, flam) =
                dbrent(lam1, lam, lam2, func, dfunc, acc, true, pref)?;

            Ok(flam < pref)
        } else {
            // Not bracketted even after a detailed search, and we have not jumped
            // out either, so assume no eclipse
            Ok(false)
        }
    }

    pub fn face(
        &self,
        direction: Vec3,
        rref: f64,
        pref: f64,
        acc: f64,
    ) -> Result<(Vec3, Vec3, f64, f64), RocheError> {
        let mut pvec: Vec3;
        let mut r: f64;

        let cofm: Vec3 = match self.star {
            Star::Primary => Vec3::cofm1(),
            Star::Secondary => Vec3::cofm2(),
        };

        let rp: fn(f64, f64, &Vec3) -> Result<f64, RocheError> = match self.star {
            Star::Primary => rpot1,
            Star::Secondary => rpot2,
        };

        let drp: fn(f64, f64, &Vec3) -> Result<Vec3, RocheError> = match self.star {
            Star::Primary => drpot1,
            Star::Secondary => drpot2,
        };

        let mut tref: f64 = rp(self.q, self.spin, &(cofm + rref * direction))?;
        if tref < pref {
            let message = format!(
                "point at reference radius {} appears to be at lower potential {} than the reference potential {}",
                rref, tref, pref
            );
            return Err(RocheError::FaceError(message));
        }

        let mut r1: f64 = rref / 2.;
        let mut r2: f64 = rref;
        tref = pref + 1.;

        const MAXSEARCH: i32 = 30;
        let mut i: i32 = 0;
        while i < MAXSEARCH && tref > pref {
            r1 = r2 / 2.;
            tref = rp(self.q, self.spin, &(cofm + r1 * direction))?;
            if tref > pref {
                r2 = r1;
            }
            i += 1;
        }
        if tref > pref {
            let message = "could not find a radius with a potential below the reference potential; probably bad inputs.";
            return Err(RocheError::FaceError(message.to_string()));
        }

        const MAXCHOP: i32 = 100;
        let mut nchop: i32 = 0;
        while r2 - r1 > acc && nchop < MAXCHOP {
            r = (r1 + r2) / 2.;
            pvec = cofm + r * direction;
            if rp(self.q, self.spin, &pvec)? < pref {
                r1 = r;
            } else {
                r2 = r;
            }
            nchop += 1;
        }
        if nchop == MAXCHOP {
            return Err(RocheError::FaceError(
                "reached maximum number of binary chops".to_string(),
            ));
        }
        r = (r1 + r2) / 2.;
        pvec = cofm + r * direction;
        let mut dvec: Vec3 = drp(self.q, self.spin, &pvec)?;
        let g = dvec.length();
        dvec /= g;
        Ok((pvec, dvec, r, g))
    }

    pub fn ingress_egress(
        &self,
        ffac: f64,
        iangle: f64,
        delta: f64,
        r: &Vec3,
        ingress: &mut f64,
        egress: &mut f64,
    ) -> Result<bool, RocheError> {
        let rref: f64;
        let pref: f64;
        (rref, pref) = self.ref_sphere(ffac)?;
        let ri: f64 = iangle.to_radians();
        let (sini, cosi) = ri.sin_cos();

        let cofm: Vec3 = match self.star {
            Star::Primary => Vec3::cofm1(),
            Star::Secondary => Vec3::cofm2(),
        };

        let mut phi1: f64 = 0.0;
        let mut phi2: f64 = 0.0;
        let mut lam1: f64 = 0.0;
        let mut lam2: f64 = 0.0;
        let mut phi: f64 = 0.0;
        let mut lam: f64 = 0.0;

        if sphere_eclipse(
            cosi, sini, r, &cofm, rref, &mut phi1, &mut phi2, &mut lam1, &mut lam2,
        ) {
            let acc: f64 = 2. * (2. * TAU * (lam2 - lam1) * delta).sqrt();

            if self.pot_min(
                cosi, sini, r, phi1, phi2, lam1, lam2, rref, pref, acc, &mut phi, &mut lam,
            )? {
                let mut pin: f64 = phi;
                let mut pout: f64 = phi1;
                let mut pmid: f64;

                while (pin - pout).abs() > delta {
                    pmid = (pin + pout) / 2.0;
                    if self
                        .fblink(ffac, acc, &set_earth(cosi, sini, pmid), r)
                        .unwrap()
                    {
                        pin = pmid;
                    } else {
                        pout = pmid;
                    }
                }
                *ingress = (pin + pout) / 2.0;
                *ingress -= ingress.floor();

                pin = phi;
                pout = phi2;
                while (pin - pout).abs() > delta {
                    pmid = (pin + pout) / 2.;
                    if self
                        .fblink(ffac, acc, &set_earth(cosi, sini, pmid), r)
                        .unwrap()
                    {
                        pin = pmid;
                    } else {
                        pout = pmid;
                    }
                }
                *egress = (pin + pout) / 2.0;
                *egress -= egress.floor();
                if *egress < *ingress {
                    *egress += 1.0;
                }
                Ok(true)
            } else {
                Ok(false)
            }
        } else {
            Ok(false)
        }
    }

    pub fn star_eclipse(
        &self,
        r: f64,
        ffac: f64,
        iangle: f64,
        posn: &Vec3,
        delta: f64,
        roche: bool,
        star: Star,
        eclipses: &mut Etype,
    ) -> Result<(), RocheError> {
        let ri = iangle.to_radians();
        let (sini, cosi) = ri.sin_cos();
        let cofm = match star {
            Star::Primary => Vec3::cofm1(),
            Star::Secondary => Vec3::cofm2(),
        };
        let mut lam1: f64 = 0.0;
        let mut lam2: f64 = 0.0;
        let mut ingress: f64 = 0.0;
        let mut egress: f64 = 0.0;
        // let mut eclipses = Etype::new();
        if (roche && self.ingress_egress(ffac, iangle, delta, posn, &mut ingress, &mut egress)?)
            || (!roche
                && sphere_eclipse(
                    cosi,
                    sini,
                    posn,
                    &cofm,
                    r,
                    &mut ingress,
                    &mut egress,
                    &mut lam1,
                    &mut lam2,
                ))
        {
            eclipses.push((ingress, egress));
        }
        Ok(())
    }

    pub fn pot_min(
        &self,
        cosi: f64,
        sini: f64,
        p: &Vec3,
        phi1: f64,
        phi2: f64,
        lam1: f64,
        lam2: f64,
        rref: f64,
        pref: f64,
        acc: f64,
        phi: &mut f64,
        lam: &mut f64,
    ) -> Result<bool, RocheError> {
        pot_min(
            self.q, self.star, self.spin, cosi, sini, p, phi1, phi2, lam1, lam2, rref, pref, acc,
            phi, lam,
        )
    }

    pub fn x_l1(&self) -> Result<f64, RocheError> {
        x_l1(self.q)
    }

    pub fn x_l1_asyncronous(&self) -> Result<f64, RocheError> {
        match self.star {
            Star::Primary => Ok(x_l1_1(self.q, self.spin)?),
            Star::Secondary => Ok(x_l1_2(self.q, self.spin)?),
        }
    }

    pub fn x_l2(&self) -> Result<f64, RocheError> {
        x_l2(self.q)
    }

    pub fn x_l3(&self) -> Result<f64, RocheError> {
        x_l3(self.q)
    }
}