testParser.c 2.81 KB
Newer Older
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).
 *
 * 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 "parser.h"
21
22
#include <assert.h>
#include <string.h>
Matthieu Schaller's avatar
Matthieu Schaller committed
23
#include <stdio.h>
24
#include <math.h>
25
26

int main(int argc, char *argv[]) {
Matthieu Schaller's avatar
Matthieu Schaller committed
27
28
29
  const char *input_file = argv[1];

  /* Create a structure to read file into. */
30
  struct swift_params param_file;
Matthieu Schaller's avatar
Matthieu Schaller committed
31

32
  /* Read the parameter file. */
Matthieu Schaller's avatar
Matthieu Schaller committed
33
  parser_read_file(input_file, &param_file);
34

35
  /* Print the contents of the structure to stdout. */
36
  parser_print_params(&param_file);
37

38
  /* Print the contents of the structure to a file in YAML format. */
39
  parser_write_params_to_file(&param_file, "parser_output.yml");
40
41
42

  /* Retrieve parameters and store them in variables defined above.
   * Have to specify the name of the parameter as it appears in the
43
   * input file: testParserInput.yaml.*/
44
45
46
47
48
49
50
51
  const int no_of_threads =
      parser_get_param_int(&param_file, "Scheduler:no_of_threads");
  const int no_of_time_steps =
      parser_get_param_int(&param_file, "Simulation:no_of_time_steps");
  const float max_h = parser_get_param_float(&param_file, "Simulation:max_h");
  const double start_time =
      parser_get_param_double(&param_file, "Simulation:start_time");
  const int kernel = parser_get_param_int(&param_file, "kernel");
52

53
54
  const int optional = parser_get_opt_param_int(&param_file, "optional", 1);

55
56
  char ic_file[PARSER_MAX_LINE_SIZE];
  parser_get_param_string(&param_file, "IO:ic_file", ic_file);
57

58
  /* Print the variables to check their values are correct. */
59
60
  printf(
      "no_of_threads: %d, no_of_time_steps: %d, max_h: %f, start_time: %lf, "
61
62
63
      "ic_file: %s, kernel: %d optional: %d\n",
      no_of_threads, no_of_time_steps, max_h, start_time, ic_file, kernel,
      optional);
64
65
66
67

  assert(no_of_threads == 16);
  assert(no_of_time_steps == 10);
  assert(fabs(max_h - 1.1255) < 0.00001);
68
  assert(fabs(start_time - 1.23456789) < 0.00001);
69
  assert(strcmp(ic_file, "ic_file.ini") == 0); /*strcmp returns 0 if correct.*/
70
  assert(kernel == 4);
71
  assert(optional == 1);
72
73

  return 0;
74
}