# Benchmarks

    library(earthtide)
    library(bench)

    eval_chunks <- TRUE # may not want to run on CRAN because of threads and running time

This vignette describes a few ways to speed up the computation of Earth tides and in some cases reduce memory consumption. The examples below are kept small to minimize computation time for CRAN, but the methods can scale to larger problems.

The following techniques are presented below: - Irregular time steps - Change wave catalog - Change wave amplitude cutoff - Change how often astronomical parameters are updated - Use parallel computation - Interpolations

# Irregular time steps

Some times you may not need to predict at regular time steps. Irregular time steps are allowed, however, the parameter should be set to 1L if you are not using a regular time series.

    set.seed(123)
    tms <- as.POSIXct("1990-01-01", tz = "UTC") + 0:(900)
    indices <- sort(sample(0:900, 100, replace = FALSE))

    wave_groups <- data.frame(start = 0, end = 8)

    check_fun <- function(target, current) (all.equal(target, current, check.attributes = FALSE))

    bench::mark(
      et <- calc_earthtide(
        utc = tms,
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-10,
        catalog = "ksm04",
        wave_groups = wave_groups
      )[indices, ],
      et_irregular <- calc_earthtide(
        utc = tms[indices],
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-10,
        catalog = "ksm04",
        wave_groups = wave_groups
      ), check = check_fun, iterations = 1
    )
    #> Warning: Some expressions had a GC in every iteration; so filtering is
    #> disabled.
    #> # A tibble: 2 × 6
    #>   expression                             min median `itr/sec` mem_alloc `gc/sec`
    #>   <bch:expr>                           <bch> <bch:>     <dbl> <bch:byt>    <dbl>
    #> 1 "et <- calc_earthtide(utc = tms, do… 997ms  997ms      1.00     127MB     8.02
    #> 2 "et_irregular <- calc_earthtide(utc… 277ms  277ms      3.61     108MB    25.3

# Catalog

Using a catalog with fewer waves will be faster. Here we compare ksm04 and hw95s.

    tms <- as.POSIXct("1990-01-01", tz = "UTC") + 0:(900)

    wave_groups <- data.frame(start = 0, end = 8)

    bench::mark(
      et <- calc_earthtide(
        utc = tms,
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-10,
        catalog = "ksm04",
        wave_groups = wave_groups
      ),
      et_catalog <- calc_earthtide(
        utc = tms,
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-10,
        catalog = "hw95s",
        wave_groups = wave_groups
      ), check = FALSE, iterations = 1
    )
    #> Warning: Some expressions had a GC in every iteration; so filtering is
    #> disabled.
    #> # A tibble: 2 × 6
    #>   expression                             min median `itr/sec` mem_alloc `gc/sec`
    #>   <bch:expr>                           <bch> <bch:>     <dbl> <bch:byt>    <dbl>
    #> 1 "et <- calc_earthtide(utc = tms, do… 989ms  989ms      1.01   108.2MB     7.08
    #> 2 "et_catalog <- calc_earthtide(utc =… 420ms  420ms      2.38    60.9MB     4.76

# cutoff parameter

Increasing the cutoff will decrease the number of waves and thus the speed increases. Results will not be the same.

    tms <- as.POSIXct("1990-01-01", tz = "UTC") + 0:(1800)

    wave_groups <- data.frame(start = 0, end = 8)

    bench::mark(
      et <- calc_earthtide(
        utc = tms,
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-10,
        catalog = "ksm04",
        wave_groups = wave_groups
      ),
      et_cutoff <- calc_earthtide(
        utc = tms,
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-5,
        catalog = "ksm04",
        wave_groups = wave_groups
      ), check = FALSE, iterations = 1
    )
    #> Warning: Some expressions had a GC in every iteration; so filtering is
    #> disabled.
    #> # A tibble: 2 × 6
    #>   expression                            min  median `itr/sec` mem_alloc `gc/sec`
    #>   <bch:expr>                        <bch:t> <bch:t>     <dbl> <bch:byt>    <dbl>
    #> 1 "et <- calc_earthtide(utc = tms,…   1.76s   1.76s     0.567   108.7MB     3.40
    #> 2 "et_cutoff <- calc_earthtide(utc… 82.83ms 82.83ms    12.1      17.4MB    12.1

# astro_update parameter

Increasing the parameter leads to an approximation that may speed up computation. Results will not be exactly the same but can be very close as in the following example. The default is that parameters are updated for every time-step.

    tms <- as.POSIXct("1990-01-01", tz = "UTC") + 0:(900)

    wave_groups <- data.frame(start = 0, end = 8)

    bench::mark(
      et <- calc_earthtide(
        utc = tms,
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-10,
        catalog = "ksm04",
        wave_groups = wave_groups
      ),
      et_astro <- calc_earthtide(
        utc = tms,
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-10,
        catalog = "ksm04",
        wave_groups = wave_groups,
        astro_update = 30L
      ), iterations = 1
    )
    #> Warning: Some expressions had a GC in every iteration; so filtering is
    #> disabled.
    #> # A tibble: 2 × 6
    #>   expression                           min   median `itr/sec` mem_alloc `gc/sec`
    #>   <bch:expr>                      <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
    #> 1 "et <- calc_earthtide(utc = tm…    1.01s    1.01s     0.989     108MB     7.91
    #> 2 "et_astro <- calc_earthtide(ut… 333.06ms 333.06ms     3.00      108MB    21.0

# n_thread parameter

Adjust the number of threads used for parallel computation. This should result in equivalent values.

    tms <- as.POSIXct("1990-01-01", tz = "UTC") + 0:(900)

    wave_groups <- data.frame(start = 0, end = 8)

    bench::mark(
      et <- calc_earthtide(
        utc = tms,
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-10,
        catalog = "ksm04",
        wave_groups = wave_groups
      ),
      et_threads <- calc_earthtide(
        utc = tms,
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-10,
        catalog = "ksm04",
        wave_groups = wave_groups,
        n_thread = 10L
      ), iterations = 1
    )
    #> Warning: Some expressions had a GC in every iteration; so filtering is
    #> disabled.
    #> # A tibble: 2 × 6
    #>   expression                             min median `itr/sec` mem_alloc `gc/sec`
    #>   <bch:expr>                           <bch> <bch:>     <dbl> <bch:byt>    <dbl>
    #> 1 "et <- calc_earthtide(utc = tms, do… 992ms  992ms      1.01     108MB     6.05
    #> 2 "et_threads <- calc_earthtide(utc =… 297ms  297ms      3.37     108MB    23.6

# Predict and interpolate

For one second output you can predict every minute and interpolate.
Interpolation is done via which achieves good accuracy with larger approximations. The number of samples skipped may need to be adjusted depending on the size of your time step. Results will not be the exactly the same but can be very close as in the following example.

    tms <- as.POSIXct("1990-01-01", tz = "UTC") + 0:(900)
    tms_interp <- as.POSIXct("1990-01-01", tz = "UTC") + seq(0, 900, 180)

    wave_groups <- data.frame(start = 0, end = 8)

    bench::mark(
      et <- calc_earthtide(
        utc = tms,
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-10,
        catalog = "ksm04",
        wave_groups = wave_groups
      ),
      et_interp <- calc_earthtide(
        utc = tms_interp,
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-10,
        catalog = "ksm04",
        wave_groups = wave_groups,
        utc_interp = tms
      ), iterations = 1
    )
    #> Warning: Some expressions had a GC in every iteration; so filtering is
    #> disabled.
    #> # A tibble: 2 × 6
    #>   expression                             min median `itr/sec` mem_alloc `gc/sec`
    #>   <bch:expr>                           <bch> <bch:>     <dbl> <bch:byt>    <dbl>
    #> 1 "et <- calc_earthtide(utc = tms, do… 984ms  984ms      1.02     108MB     8.13
    #> 2 "et_interp <- calc_earthtide(utc = … 224ms  224ms      4.47     108MB    31.3

# Combination of the above

We will use a larger dataset to compare approximation methods. In general, interpolation will give the best speed-up to accuracy if your time-steps are small (seconds).

    tms <- as.POSIXct("1990-01-01", tz = "UTC") + 0:(86400)
    tms_interp <- as.POSIXct("1990-01-01", tz = "UTC") + seq(0, 86400, 180)

    wave_groups <- data.frame(start = 0, end = 8)

    bench::mark(
      et_astro_threads <- calc_earthtide(
        utc = tms,
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-10,
        catalog = "ksm04",
        wave_groups = wave_groups,
        astro_update = 60L,
        n_thread = 10L
      ),
      et_interp_threads <- calc_earthtide(
        utc = tms_interp,
        do_predict = TRUE,
        method = c("tidal_potential", "lod_tide", "pole_tide"),
        latitude = 52.3868,
        longitude = 9.7144,
        elevation = 110,
        gravity = 9.8127,
        cutoff = 1.0e-10,
        catalog = "ksm04",
        wave_groups = wave_groups,
        utc_interp = tms,
        n_thread = 10L
      ), iterations = 1
    )
    #> Warning: Some expressions had a GC in every iteration; so filtering is
    #> disabled.
    #> # A tibble: 2 × 6
    #>   expression                           min   median `itr/sec` mem_alloc `gc/sec`
    #>   <bch:expr>                      <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
    #> 1 "et_astro_threads <- calc_eart…    1.84s    1.84s     0.544     153MB     4.35
    #> 2 "et_interp_threads <- calc_ear… 277.44ms 277.44ms     3.60      112MB    25.2