testKernelLongGrav.c 4.1 KB
Newer Older
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
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (C) 2020 Matthieu Schaller (schaller@strw.leidenuniv.nl)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/

#include "../config.h"
#include "swift.h"

/* Standard includes */
#include <fenv.h>
#include <math.h>

const int num_tests = 1 << 10;

/**
 * @brief Check that a and b are consistent (up to some relative error)
 *
 * @param a First value
 * @param b Second value
 * @param s String used to identify this check in messages
35 36 37
 * @param tol Relative tolerance.
 * @param r Distance tested (for error messages).
 * @param r_s Mesh size tested (for error messages).
38 39 40 41
 */
void check_value(double a, double b, const char* s, const double tol,
                 const double r, const double r_s) {
  if (fabs(a - b) / fabs(a + b) > tol)
42 43 44 45
    error(
        "Values are inconsistent: %12.15e %12.15e rel=%e (%s for r_s=%e "
        "r/r_s=%e)!",
        a, b, fabs(a - b) / fabs(a + b), s, r_s, r / r_s);
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
}

int main(int argc, char* argv[]) {

  /* Initialize CPU frequency, this also starts time. */
  unsigned long long cpufreq = 0;
  clocks_set_cpufreq(cpufreq);

/* Choke on FPEs */
#ifdef HAVE_FE_ENABLE_EXCEPT
  feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
#endif

  /* Get some randomness going */
  const int seed = time(NULL);
  message("Seed = %d", seed);
  srand(seed);

  for (int n = 0; n < num_tests; ++n) {

    const double r_s = exp10(4. * rand() / ((double)RAND_MAX) - 2.);
    const double r_s_inv = 1.f / r_s;

    // message("Testing r_s=%e", r_s);

    /* Loop over some radii */
72
    for (double i = -4; i < 1; i += 0.001) {
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

      /* Get a radius in the relevant range */
      const double r = exp10(i) * r_s;

      if (r > 5. * r_s) break;

      /* Compute the SWIFT expressions */
      struct chi_derivatives chi_swift;
      kernel_long_grav_derivatives((float)r, (float)r_s_inv, &chi_swift);

      /* Compute the exact expressions */
      const double one_over_sqrt_pi = M_2_SQRTPI * 0.5;
      const double u = 0.5 * r / r_s;
      const double C = one_over_sqrt_pi * exp(-u * u);

      const double chi_0 = erfc(u);
      const double chi_1 = -C / r_s;
      const double chi_2 = C * 0.5 * r * pow(r_s, -3.);
      const double chi_3 = C * 0.25 * (2. * r_s * r_s - r * r) * pow(r_s, -5.);
      const double chi_4 =
          C * 0.125 * (r * r * r - 6. * r_s * r_s * r) * pow(r_s, -7.);
      const double chi_5 =
          C * 0.0625 *
          (12. * pow(r_s, 4.) - 12. * r_s * r_s * r * r + pow(r, 4.)) *
          pow(r_s, -9.);

99
      check_value(chi_swift.chi_0, chi_0, "chi_0", 3.4e-3, r, r_s);
100 101 102
      check_value(chi_swift.chi_1, chi_1, "chi_1", 1e-5, r, r_s);
      check_value(chi_swift.chi_2, chi_2, "chi_2", 1e-5, r, r_s);
      check_value(chi_swift.chi_3, chi_3, "chi_3", 1e-4, r, r_s);
103 104
      check_value(chi_swift.chi_4, chi_4, "chi_4", 4e-4, r, r_s);
      check_value(chi_swift.chi_5, chi_5, "chi_5", 4e-4, r, r_s);
105 106 107 108 109 110 111 112 113 114 115

      /* Compute the expression for individual particles */
      float swift_corr_f_lr, swift_corr_pot_lr;
      kernel_long_grav_eval(r / r_s, &swift_corr_f_lr, &swift_corr_pot_lr);

      /* And the exact ones */
      const double corr_pot = erfc(u);
      const double corr_f = erfc(u) + M_2_SQRTPI * u * exp(-u * u);

      check_value(swift_corr_pot_lr, corr_pot, "corr_pot", 3.4e-3, r, r_s);
      check_value(swift_corr_f_lr, corr_f, "corr_f", 2.4e-4, r, r_s);
116 117 118 119 120
    }
  }

  return 0;
}