Commit cc77fafd authored by Loic Hausammann's avatar Loic Hausammann

Add output_list test

parent 15939e28
......@@ -28,7 +28,7 @@ TESTS = testGreetings testMaths testReading.sh testSingle testKernel testSymmetr
testVoronoi1D testVoronoi2D testVoronoi3D testGravityDerivatives \
testPeriodicBC.sh testPeriodicBCPerturbed.sh testPotentialSelf \
testPotentialPair testEOS testUtilities testSelectOutput.sh \
testCbrt testCosmology
testCbrt testCosmology testOutputList
# List of test programs to compile
check_PROGRAMS = testGreetings testReading testSingle testTimeIntegration \
......@@ -39,7 +39,7 @@ check_PROGRAMS = testGreetings testReading testSingle testTimeIntegration \
testRiemannHLLC testMatrixInversion testDump testLogger \
testVoronoi1D testVoronoi2D testVoronoi3D testPeriodicBC \
testGravityDerivatives testPotentialSelf testPotentialPair testEOS testUtilities \
testSelectOutput testCbrt testCosmology
testSelectOutput testCbrt testCosmology testOutputList
# Rebuild tests when SWIFT is updated.
$(check_PROGRAMS): ../src/.libs/libswiftsim.a
......@@ -55,6 +55,8 @@ testSelectOutput_SOURCES = testSelectOutput.c
testCosmology_SOURCES = testCosmology.c
testOutputList_SOURCES = testOutputList.c
testSymmetry_SOURCES = testSymmetry.c
# Added because of issues using memcmp on clang 4.x
......
# Cosmological parameters
Cosmology:
h: 0.6777 # Reduced Hubble constant
a_begin: 1e-2 # Initial scale-factor of the simulation
a_end: 1.0 # Final scale factor of the simulation
Omega_m: 0.307 # Matter density parameter
Omega_lambda: 0.693 # Dark-energy density parameter
Omega_b: 0.0455 # Baryon density parameter
# Define the system of units to use internally.
Units:
UnitMass_in_cgs: 1.48e43 # 10^10 M_sun in grams
UnitLength_in_cgs: 3.08e21 # kpc in centimeters
UnitVelocity_in_cgs: 1e5 # km/s in centimeters per second
UnitCurrent_in_cgs: 1 # Amperes
UnitTemp_in_cgs: 1 # Kelvin
Redshift:
output_list_on: 1 # (Optional) Enable the output list
output_list: output_list_redshift.txt # (Optional) File containing the output times (see documentation in "Parameter File" section)
Time:
output_list_on: 1 # (Optional) Enable the output list
output_list: output_list_time.txt # (Optional) File containing the output times (see documentation in "Parameter File" section)
ScaleFactor:
output_list_on: 1 # (Optional) Enable the output list
output_list: output_list_scale_factor.txt # (Optional) File containing the output times (see documentation in "Parameter File" section)
# Scale Factor
0.01
0.1
0.5
/*******************************************************************************
* 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/>.
*
******************************************************************************/
#include <math.h>
#include <stdio.h>
#include <string.h>
#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] = {
0., 10., 12.,
};
/* Expected values from file */
const double a_values[Ntest] = {
0.5, 0.1, 0.01,
};
void test_no_cosmo(struct engine *e, char *name, int with_assert) {
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;
e->policy = !engine_policy_cosmology;
/* initialize output_list */
output_list_init(&list, e, name, &delta_time, &output_time);
output_list_print(list);
for(int i = 0; i < Ntest; i++) {
/* Test last value */
if (with_assert) {
assert(abs(output_time - time_values[i]) < tol);
}
/* 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);
output_time = (double) (ti_next * e->time_base) + e->time_begin;
}
output_list_clean(list);
};
void test_cosmo(struct engine *e, char *name, int with_assert) {
message("Testing output time for %s with cosmology", name);
struct output_list *list;
double delta_time = 0;
double output_time = 0;
/* Test Time */
e->time_base = log(e->time_end / e->cosmology->a_begin) / max_nr_timesteps;
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);
for(int i = 0; i < Ntest; i++) {
/* Test last value */
if (with_assert) {
assert(abs(output_time - a_values[i]) < tol);
}
/* Set current time */
e->ti_current = log(output_time / e->cosmology->a_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);
output_time = (double) exp(ti_next * e->time_base) * e->cosmology->a_begin;
}
output_list_clean(list);
};
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);
/* Test with cosmo */
test_cosmo(&e, "Redshift", with_assert);
test_cosmo(&e, "ScaleFactor", with_assert);
test_cosmo(&e, "Time", without_assert);
/* Write message and leave */
message("Test done");
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment