# 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:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
    #> 1 "et <- calc_earthtide(utc = tm…    1.63s    1.63s     0.614     127MB     4.91
    #> 2 "et_irregular <- calc_earthtid… 529.27ms 529.27ms     1.89      108MB    15.1

# 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:t> <bch:t>     <dbl> <bch:byt>    <dbl>
    #> 1 "et <- calc_earthtide(utc = tms,…   1.77s   1.77s     0.566   108.4MB     4.53
    #> 2 "et_catalog <- calc_earthtide(ut… 683.5ms 683.5ms     1.46     61.1MB     4.39

# 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:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
    #> 1 "et <- calc_earthtide(utc = tm…    3.07s    3.07s     0.325   108.9MB     1.95
    #> 2 "et_cutoff <- calc_earthtide(u… 107.39ms 107.39ms     9.31     17.6MB     9.31

# 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.72s    1.72s     0.581     108MB     3.48
    #> 2 "et_astro <- calc_earthtide(ut… 583.13ms 583.13ms     1.71      108MB    10.3

# 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:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
    #> 1 "et <- calc_earthtide(utc = tm…    1.41s    1.41s     0.708     108MB     5.66
    #> 2 "et_threads <- calc_earthtide(… 465.92ms 465.92ms     2.15      108MB    12.9

# 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:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
    #> 1 "et <- calc_earthtide(utc = tm…    1.43s    1.43s     0.697     108MB     4.18
    #> 2 "et_interp <- calc_earthtide(u… 347.11ms 347.11ms     2.88      108MB    20.2

# 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…    2.14s    2.14s     0.467     153MB     3.27
    #> 2 "et_interp_threads <- calc_ear… 354.64ms 354.64ms     2.82      112MB    19.7