testOutputList.c 4.29 KB
Newer Older
Loic Hausammann's avatar
Loic Hausammann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (C) 2016 James Willis (james.s.willis@durham.ac.uk).
 *               2018 Peter W. Draper (p.w.draper@durham.ac.uk)
 *
 * 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/>.
 *
 ******************************************************************************/
20
#include "../config.h"
Loic Hausammann's avatar
Loic Hausammann committed
21

22
/* Includes. */
Loic Hausammann's avatar
Loic Hausammann committed
23 24 25 26 27 28 29 30
#include "swift.h"

#define Ntest 3
#define tol 1e-12
#define filename "output_list_params.yml"

/* Expected values from file */
const double time_values[Ntest] = {
Bert Vandenbroucke's avatar
Bert Vandenbroucke committed
31 32 33
    0.,
    10.,
    12.,
Loic Hausammann's avatar
Loic Hausammann committed
34 35 36 37
};

/* Expected values from file */
const double a_values[Ntest] = {
Bert Vandenbroucke's avatar
Bert Vandenbroucke committed
38
    0.01,
Loic Hausammann's avatar
Loic Hausammann committed
39 40
    0.1,
    0.5,
Loic Hausammann's avatar
Loic Hausammann committed
41 42
};

43
void test_no_cosmo(struct engine *e, const char *name, const int with_assert) {
Loic Hausammann's avatar
Loic Hausammann committed
44 45 46 47 48 49 50 51 52 53 54
  message("Testing output time for %s without cosmology", name);

  struct output_list *list;
  double delta_time = 0;
  double output_time = 0;

  /* Test Time */
  e->time_begin = 0;
  e->time_end = 14;
  e->time_base = (e->time_end - e->time_begin) / max_nr_timesteps;
  e->ti_current = 0;
55
  e->policy = 0;
Loic Hausammann's avatar
Loic Hausammann committed
56 57 58 59 60

  /* initialize output_list */
  output_list_init(&list, e, name, &delta_time, &output_time);
  output_list_print(list);

Bert Vandenbroucke's avatar
Bert Vandenbroucke committed
61
  for (int i = 0; i < Ntest; i++) {
Loic Hausammann's avatar
Loic Hausammann committed
62 63
    /* Test last value */
    if (with_assert) {
Loic Hausammann's avatar
Loic Hausammann committed
64
      assert(fabs(output_time - time_values[i]) < tol);
Loic Hausammann's avatar
Loic Hausammann committed
65 66 67 68 69 70 71 72 73 74
    }

    /* Set current time */
    e->ti_current = (output_time - e->time_begin) / e->time_base;
    e->ti_current += 1;

    /* Read next value */
    integertime_t ti_next;
    output_list_read_next_time(list, e, name, &ti_next);

Bert Vandenbroucke's avatar
Bert Vandenbroucke committed
75
    output_time = (double)(ti_next * e->time_base) + e->time_begin;
Loic Hausammann's avatar
Loic Hausammann committed
76 77
  }

78
  output_list_clean(&list);
Loic Hausammann's avatar
Loic Hausammann committed
79 80
};

81
void test_cosmo(struct engine *e, const char *name, const int with_assert) {
Loic Hausammann's avatar
Loic Hausammann committed
82 83 84 85 86 87 88
  message("Testing output time for %s with cosmology", name);

  struct output_list *list;
  double delta_time = 0;
  double output_time = 0;

  /* Test Time */
89
  e->time_base = log(e->time_end / e->cosmology->a_begin) / max_nr_timesteps;
Loic Hausammann's avatar
Loic Hausammann committed
90 91 92 93 94 95 96
  e->ti_current = 0;
  e->policy = engine_policy_cosmology;

  /* initialize output_list */
  output_list_init(&list, e, name, &delta_time, &output_time);
  output_list_print(list);

Bert Vandenbroucke's avatar
Bert Vandenbroucke committed
97
  for (int i = 0; i < Ntest; i++) {
Loic Hausammann's avatar
Loic Hausammann committed
98 99
    /* Test last value */
    if (with_assert) {
Loic Hausammann's avatar
Loic Hausammann committed
100
      assert(fabs(output_time - a_values[i]) < tol);
Loic Hausammann's avatar
Loic Hausammann committed
101 102 103 104
    }

    /* Set current time */
    e->ti_current = log(output_time / e->cosmology->a_begin) / e->time_base;
105
    e->ti_current += 16;
Loic Hausammann's avatar
Loic Hausammann committed
106 107 108 109 110

    /* Read next value */
    integertime_t ti_next;
    output_list_read_next_time(list, e, name, &ti_next);

Bert Vandenbroucke's avatar
Bert Vandenbroucke committed
111
    output_time = (double)exp(ti_next * e->time_base) * e->cosmology->a_begin;
Loic Hausammann's avatar
Loic Hausammann committed
112 113
  }

114
  output_list_clean(&list);
Loic Hausammann's avatar
Loic Hausammann committed
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
};

int main(int argc, char *argv[]) {
  /* Create a structure to read file into. */
  struct swift_params params;

  /* Read the parameter file. */
  parser_read_file(filename, &params);

  /* initialization of unit system */
  struct unit_system us;
  units_init_from_params(&us, &params, "Units");

  /* initialization of phys_const */
  struct phys_const phys_const;
  phys_const_init(&us, &params, &phys_const);

  /* initialization of cosmo */
  struct cosmology cosmo;
  cosmology_init(&params, &us, &phys_const, &cosmo);

  /* Pseudo initialization of engine */
  struct engine e;
  e.cosmology = &cosmo;
  e.parameter_file = &params;
  e.physical_constants = &phys_const;
  e.internal_units = &us;

  int with_assert = 1;
  int without_assert = 0;
  /* Test without cosmo */
  test_no_cosmo(&e, "Time", with_assert);
Bert Vandenbroucke's avatar
Bert Vandenbroucke committed
147

Loic Hausammann's avatar
Loic Hausammann committed
148 149 150 151 152
  /* Test with cosmo */
  test_cosmo(&e, "Redshift", with_assert);
  test_cosmo(&e, "ScaleFactor", with_assert);
  test_cosmo(&e, "Time", without_assert);

153 154
  cosmology_clean(&cosmo);

Loic Hausammann's avatar
Loic Hausammann committed
155 156 157 158
  /* Write message and leave */
  message("Test done");
  return 0;
}