testSelectOutput.c 5.87 KB
Newer Older
lhausamm's avatar
lhausamm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (C) 2015 Matthieu Schaller (matthieu.schaller@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/>.
 *
 ******************************************************************************/

/* Some standard headers. */
21
#include "../config.h"
lhausamm's avatar
lhausamm committed
22 23 24 25 26

/* Includes. */
#include "swift.h"

void select_output_engine_init(struct engine *e, struct space *s,
lhausamm's avatar
lhausamm committed
27 28
                               struct cosmology *cosmo,
                               struct swift_params *params,
29
                               struct output_options *output,
lhausamm's avatar
lhausamm committed
30 31
                               struct cooling_function_data *cooling,
                               struct hydro_props *hydro_properties) {
lhausamm's avatar
lhausamm committed
32 33 34 35
  /* set structures */
  e->s = s;
  e->cooling_func = cooling;
  e->parameter_file = params;
36
  e->output_options = output;
lhausamm's avatar
lhausamm committed
37 38 39 40 41 42 43 44 45 46
  e->cosmology = cosmo;
  e->policy = engine_policy_hydro;
  e->hydro_properties = hydro_properties;

  /* initialization of threadpool */
  threadpool_init(&e->threadpool, 1);

  /* set parameters */
  e->verbose = 1;
  e->time = 0;
47 48
  e->snapshot_output_count = 0;
  e->snapshot_compression = 0;
lhausamm's avatar
lhausamm committed
49 50 51
};

void select_output_space_init(struct space *s, double *dim, int periodic,
lhausamm's avatar
lhausamm committed
52 53 54
                              size_t Ngas, size_t Nspart, size_t Ngpart,
                              struct part *parts, struct spart *sparts,
                              struct gpart *gparts) {
lhausamm's avatar
lhausamm committed
55
  s->periodic = periodic;
lhausamm's avatar
lhausamm committed
56
  for (int i = 0; i < 3; i++) {
lhausamm's avatar
lhausamm committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70
    s->dim[i] = dim[i];
  }

  /* init space particles */
  s->nr_parts = Ngas;
  s->nr_sparts = Nspart;
  s->nr_gparts = Ngpart;

  s->parts = parts;
  s->gparts = gparts;
  s->sparts = sparts;

  /* Allocate the extra parts array for the gas particles. */
  if (posix_memalign((void **)&s->xparts, xpart_align,
lhausamm's avatar
lhausamm committed
71
                     Ngas * sizeof(struct xpart)) != 0)
lhausamm's avatar
lhausamm committed
72 73 74 75
    error("Failed to allocate xparts.");
  bzero(s->xparts, Ngas * sizeof(struct xpart));
};

lhausamm's avatar
lhausamm committed
76
void select_output_space_clean(struct space *s) { free(s->xparts); };
lhausamm's avatar
lhausamm committed
77 78

void select_output_engine_clean(struct engine *e) {
lhausamm's avatar
lhausamm committed
79
  threadpool_clean(&e->threadpool);
lhausamm's avatar
lhausamm committed
80 81
}

82 83 84 85 86
int main(int argc, char *argv[]) {

  /* Initialize CPU frequency, this also starts time. */
  unsigned long long cpufreq = 0;
  clocks_set_cpufreq(cpufreq);
lhausamm's avatar
lhausamm committed
87

88
  // const char *base_name = "testSelectOutput";
Loic Hausammann's avatar
Loic Hausammann committed
89 90
  size_t Ngas = 0, Ngpart = 0, Ngpart_background = 0, Nspart = 0, Nbpart = 0,
         Nsink = 0;
lhausamm's avatar
lhausamm committed
91
  int flag_entropy_ICs = -1;
92
  int periodic = 1;
lhausamm's avatar
lhausamm committed
93 94 95 96
  double dim[3];
  struct part *parts = NULL;
  struct gpart *gparts = NULL;
  struct spart *sparts = NULL;
97
  struct bpart *bparts = NULL;
Loic Hausammann's avatar
Loic Hausammann committed
98
  struct sink *sinks = NULL;
lhausamm's avatar
lhausamm committed
99 100 101 102

  /* parse parameters */
  message("Reading parameters.");
  struct swift_params param_file;
103
  const char *input_file = "selectOutputParameters.yml";
lhausamm's avatar
lhausamm committed
104 105
  parser_read_file(input_file, &param_file);

106 107 108
  struct output_options output_options;
  output_options_init(&param_file, 0, &output_options);

lhausamm's avatar
lhausamm committed
109 110 111 112 113 114 115 116 117 118 119 120
  /* Default unit system */
  message("Initialization of the unit system.");
  struct unit_system us;
  units_init_cgs(&us);

  /* Default physical constants */
  message("Initialization of the physical constants.");
  struct phys_const prog_const;
  phys_const_init(&us, &param_file, &prog_const);

  /* Read data */
  message("Reading initial conditions.");
121 122 123 124 125 126 127 128 129 130 131 132
  read_ic_single(
      "input.hdf5", &us, dim, &parts, &gparts, &sinks, &sparts, &bparts, &Ngas,
      &Ngpart, &Ngpart_background, &Nsink, &Nspart, &Nbpart, &flag_entropy_ICs,
      /*with_hydro=*/1,
      /*with_gravity=*/0,
      /*with_sink=*/0,
      /*with_stars=*/0,
      /*with_black_holes=*/0,
      /*with_cosmology=*/0,
      /*cleanup_h=*/0,
      /*cleanup_sqrt_a=*/0,
      /*h=*/1., /*a=*/1., /*n_threads=*/1, /*dry_run=*/0, /*remap_ids=*/0);
lhausamm's avatar
lhausamm committed
133 134 135 136

  /* pseudo initialization of the space */
  message("Initialization of the space.");
  struct space s;
lhausamm's avatar
lhausamm committed
137 138
  select_output_space_init(&s, dim, periodic, Ngas, Nspart, Ngpart, parts,
                           sparts, gparts);
lhausamm's avatar
lhausamm committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

  /* initialization of cosmology */
  message("Initialization of the cosmology.");
  struct cosmology cosmo;
  cosmology_init_no_cosmo(&cosmo);

  /* pseudo initialization of cooling */
  message("Initialization of the cooling.");
  struct cooling_function_data cooling;

  /* pseudo initialization of hydro */
  message("Initialization of the hydro.");
  struct hydro_props hydro_properties;
  hydro_props_init(&hydro_properties, &prog_const, &us, &param_file);

  /* pseudo initialization of the engine */
  message("Initialization of the engine.");
  struct engine e;
157 158
  e.physical_constants = &prog_const;
  sprintf(e.snapshot_base_name, "testSelectOutput");
159
  sprintf(e.run_name, "Select Output Test");
160 161
  select_output_engine_init(&e, &s, &cosmo, &param_file, &output_options,
                            &cooling, &hydro_properties);
lhausamm's avatar
lhausamm committed
162 163 164

  /* check output selection */
  message("Checking output parameters.");
165 166 167
  io_prepare_output_fields(&output_options, /*with_cosmology=*/0,
                           /*with_fof=*/0,
                           /*with_structure_finding=*/0);
lhausamm's avatar
lhausamm committed
168 169 170

  /* write output file */
  message("Writing output.");
171
  write_output_single(&e, &us, &us);
lhausamm's avatar
lhausamm committed
172 173 174 175 176 177 178 179 180 181 182

  /* Clean-up */
  message("Cleaning memory.");
  select_output_engine_clean(&e);
  select_output_space_clean(&s);
  cosmology_clean(&cosmo);
  free(parts);
  free(gparts);

  return 0;
}