diff --git a/examples/CosmologicalBox/cosmo.yml b/examples/CosmologicalBox/cosmo.yml new file mode 100644 index 0000000000000000000000000000000000000000..b0fbfaf3fa696d9dd3531369e1811c22cc1f4af4 --- /dev/null +++ b/examples/CosmologicalBox/cosmo.yml @@ -0,0 +1,50 @@ +# Define the system of units to use internally. +InternalUnitSystem: + UnitMass_in_cgs: 1.989e43 # 10^10 M_sun in grams + UnitLength_in_cgs: 3.085678e24 # Mpc in centimeters + UnitVelocity_in_cgs: 1e0 # km/s in centimeters per second + UnitCurrent_in_cgs: 1 # Amperes + UnitTemp_in_cgs: 1 # Kelvin + +# UnitMass_in_cgs: 1 # Grams +# UnitLength_in_cgs: 1 # Centimeters +# UnitVelocity_in_cgs: 1 # Centimeters per second +# UnitCurrent_in_cgs: 1 # Amperes +# UnitTemp_in_cgs: 1 # Kelvin + +# Parameters governing the time integration +TimeIntegration: + time_begin: 0. # The starting time of the simulation (in internal units). + time_end: 1. # The end time of the simulation (in internal units). + dt_min: 1e-6 # The minimal time-step size of the simulation (in internal units). + dt_max: 1e-2 # The maximal time-step size of the simulation (in internal units). + +# Parameters governing the snapshots +Snapshots: + basename: uniformBox # Common part of the name of output files + time_first: 0. # Time of the first output (in internal units) + delta_time: 0.01 # Time difference between consecutive outputs (in internal units) + +# Parameters governing the conserved quantities statistics +Statistics: + delta_time: 1e-2 # Time between statistics output + +# Parameters for the hydrodynamics scheme +SPH: + resolution_eta: 1.2348 # Target smoothing length in units of the mean inter-particle separation (1.2348 == 48Ngbs with the cubic spline kernel). + CFL_condition: 0.1 # Courant-Friedrich-Levy condition for time integration. + +# Parameters related to the initial conditions +InitialConditions: + file_name: ./uniformBox.hdf5 # The file to read + +Cosmology: + Omega_m: 0.307 + Omega_r: 0.000 + Omega_lambda: 0.693 + Omega_b: 0.0455 + h: 0.6777 + a_begin: 0.0078125 + a_end: 1.0 + + diff --git a/examples/CosmologicalBox/makeIC.py b/examples/CosmologicalBox/makeIC.py new file mode 100644 index 0000000000000000000000000000000000000000..01e37c67b6e2eec2984d62f4ffd503b23b5bd9ec --- /dev/null +++ b/examples/CosmologicalBox/makeIC.py @@ -0,0 +1,109 @@ +############################################################################### + # This file is part of SWIFT. + # Copyright (c) 2013 Pedro Gonnet (pedro.gonnet@durham.ac.uk), + # 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/>. + # + ############################################################################## + +import h5py +import sys +from numpy import * + +# Generates a swift IC file containing a cartesian distribution of particles +# at a constant density and pressure in a cubic box + +# Parameters +periodic= 1 # 1 For periodic box +boxSize = 1. +L = int(sys.argv[1]) # Number of particles along one axis +rho = 2. # Density +P = 1. # Pressure +gamma = 5./3. # Gas adiabatic index +eta = 1.2349 # 48 ngbs with cubic spline kernel +fileName = "uniformBox.hdf5" + +#--------------------------------------------------- +numPart = L**3 +mass = boxSize**3 * rho / numPart +internalEnergy = P / ((gamma - 1.)*rho) + +#-------------------------------------------------- + +#File +file = h5py.File(fileName, 'w') + +# Header +grp = file.create_group("/Header") +grp.attrs["BoxSize"] = boxSize +grp.attrs["NumPart_Total"] = [numPart, 0, 0, 0, 0, 0] +grp.attrs["NumPart_Total_HighWord"] = [0, 0, 0, 0, 0, 0] +grp.attrs["NumPart_ThisFile"] = [numPart, 0, 0, 0, 0, 0] +grp.attrs["Time"] = 0.0 +grp.attrs["NumFilesPerSnapshot"] = 1 +grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] +grp.attrs["Flag_Entropy_ICs"] = 0 +grp.attrs["Dimension"] = 3 + +#Runtime parameters +grp = file.create_group("/RuntimePars") +grp.attrs["PeriodicBoundariesOn"] = periodic + +#Units +grp = file.create_group("/Units") +grp.attrs["Unit length in cgs (U_L)"] = 1. +grp.attrs["Unit mass in cgs (U_M)"] = 1. +grp.attrs["Unit time in cgs (U_t)"] = 1. +grp.attrs["Unit current in cgs (U_I)"] = 1. +grp.attrs["Unit temperature in cgs (U_T)"] = 1. + +#Particle group +grp = file.create_group("/PartType0") + +v = zeros((numPart, 3)) +ds = grp.create_dataset('Velocities', (numPart, 3), 'f') +ds[()] = v +v = zeros(1) + +m = full((numPart, 1), mass) +ds = grp.create_dataset('Masses', (numPart,1), 'f') +ds[()] = m +m = zeros(1) + +h = full((numPart, 1), eta * boxSize / L) +ds = grp.create_dataset('SmoothingLength', (numPart,1), 'f') +ds[()] = h +h = zeros(1) + +u = full((numPart, 1), internalEnergy) +ds = grp.create_dataset('InternalEnergy', (numPart,1), 'f') +ds[()] = u +u = zeros(1) + + +ids = linspace(0, numPart, numPart, endpoint=False).reshape((numPart,1)) +ds = grp.create_dataset('ParticleIDs', (numPart, 1), 'L') +ds[()] = ids + 1 +x = ids % L; +y = ((ids - x) / L) % L; +z = (ids - x - L * y) / L**2; +coords = zeros((numPart, 3)) +coords[:,0] = z[:,0] * boxSize / L + boxSize / (2*L) +coords[:,1] = y[:,0] * boxSize / L + boxSize / (2*L) +coords[:,2] = x[:,0] * boxSize / L + boxSize / (2*L) +ds = grp.create_dataset('Coordinates', (numPart, 3), 'd') +ds[()] = coords + +file.close() diff --git a/examples/main.c b/examples/main.c index 6cd5f9fa6eb9b48fef4c040f6b3a90b909fd0d3b..a69ee02096eacc8952b4520d5df3b570ba623f57 100644 --- a/examples/main.c +++ b/examples/main.c @@ -472,6 +472,11 @@ int main(int argc, char *argv[]) { phys_const_print(&prog_const); } + /* Initialise the cosmology */ + struct cosmology cosmo; + if(with_cosmology) cosmology_init(params, &us, &prog_const, &cosmo); + if(with_cosmology) cosmology_print(&cosmo); + /* Initialise the hydro properties */ struct hydro_props hydro_properties; if (with_hydro) hydro_props_init(&hydro_properties, params); diff --git a/src/Makefile.am b/src/Makefile.am index 690c5a0589e7cbd71ec40c2dd2bcdd53d848bd4b..6978f1c5b186a70e019072821f503e4451a35012 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -47,7 +47,7 @@ include_HEADERS = space.h runner.h queue.h task.h lock.h cell.h part.h const.h \ sourceterms_struct.h statistics.h memswap.h cache.h runner_doiact_vec.h profiler.h \ dump.h logger.h active.h timeline.h xmf.h gravity_properties.h gravity_derivatives.h \ gravity_softened_derivatives.h vector_power.h collectgroup.h hydro_space.h sort_part.h \ - chemistry.h chemistry_io.h chemistry_struct.h + chemistry.h chemistry_io.h chemistry_struct.h cosmology.h # Common source files AM_SOURCES = space.c runner.c queue.c task.c cell.c engine.c \ @@ -59,7 +59,7 @@ AM_SOURCES = space.c runner.c queue.c task.c cell.c engine.c \ statistics.c runner_doiact_vec.c profiler.c dump.c logger.c \ part_type.c xmf.c gravity_properties.c gravity.c \ collectgroup.c hydro_space.c equation_of_state.c \ - chemistry.c + chemistry.c cosmology.c # Include files for distribution, not installation. diff --git a/src/cosmology.c b/src/cosmology.c new file mode 100644 index 0000000000000000000000000000000000000000..2eb83a5c428a76239bcf3c91f4bff2312a5e32af --- /dev/null +++ b/src/cosmology.c @@ -0,0 +1,106 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2017 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/>. + * + ******************************************************************************/ + +/** + * @file cosmology.c + * @brief Functions relating cosmological parameters + */ + +/* This object's header. */ +#include "cosmology.h" + +/* Some standard headers */ +#include <math.h> + +/** + * @brief Update the cosmological parameters to the current simulation time. + * + * @param c The #cosmology struct. + * @param e The #engine containing information about the simulation time. + */ +void cosmology_update(struct cosmology *c, const struct engine *e) { + + /* Get scale factor */ + const double a = c->a_begin * exp(e->ti_current * e->timeBase); + const double a_inv = 1. / a; + c->a = a; + c->a_inv = a_inv; + + /* Redshift */ + c->z = a_inv - 1.; + + /* E(z) */ + const double Omega_r = c->Omega_r; + const double Omega_m = c->Omega_m; + const double Omega_l = c->Omega_lambda; + const double Omega_k = c->Omega_k; + + const double E_z2 = Omega_r * a_inv * a_inv * a_inv * a_inv + + Omega_m * a_inv * a_inv * a_inv + + Omega_k * a_inv * a_inv + + Omega_l; + + const double E_z = sqrt(E_z2); + + /* H(z) */ + c->H = c->H0 * E_z; +} + + +void cosmology_init(const struct swift_params* params, + const struct unit_system* us, + const struct phys_const* phys_const, + struct cosmology *c) { + + /* Read in the cosmological parameters */ + c->Omega_m = parser_get_param_double(params, "Cosmology:Omega_m"); + c->Omega_r = parser_get_param_double(params, "Cosmology:Omega_r"); + c->Omega_lambda = parser_get_param_double(params, "Cosmology:Omega_lambda"); + c->Omega_b = parser_get_param_double(params, "Cosmology:Omega_b"); + c->h = parser_get_param_double(params, "Cosmology:h"); + c->a_begin = parser_get_param_double(params, "Cosmology:a_begin"); + c->a_end = parser_get_param_double(params, "Cosmology:a_end"); + + /* Construct derived quantities */ + c->Omega_k = 1. - (c->Omega_m + c->Omega_r + c->Omega_lambda); + const double km = 1.e5 / units_cgs_conversion_factor(us, UNIT_CONV_LENGTH); + const double s = 1. / units_cgs_conversion_factor(us, UNIT_CONV_TIME); + message("km=%e", km); + const double H0_cgs = 100. * c->h * km / s / (1.e6 * phys_const->const_parsec); /* s^-1 */ + message("s=%e", s); + c->H0 = H0_cgs; + + /* Set remaining variables to invalid values */ + c->H = -1.; + c->a = -1.; + c->a_inv = -1; + c->z = -1.; +} + + +void cosmology_print(const struct cosmology *c) { + + message("Density parameters: [O_m, O_l, O_b, O_k, O_r] = [%f, %f, %f, %f, %f]", + c->Omega_m, c->Omega_lambda, c->Omega_b, c->Omega_k, c->Omega_r); + message("Hubble constant: h = %f, H_0 = %e U_t^(-1) (internal units)", + c->h, c->H0); + +} + + diff --git a/src/cosmology.h b/src/cosmology.h new file mode 100644 index 0000000000000000000000000000000000000000..7ee19ced7fd575b6eacf9a1a63c2f6bf7db6684a --- /dev/null +++ b/src/cosmology.h @@ -0,0 +1,84 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2017 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/>. + * + ******************************************************************************/ +#ifndef SWIFT_COSMOLOGY_H +#define SWIFT_COSMOLOGY_H + +/* Config parameters. */ +#include "../config.h" + +#include "engine.h" +#include "parser.h" + +/** + * @brief Cosmological parameters + */ +struct cosmology{ + + /*! Current expansion factor of the Universe */ + double a; + + /*! Inverse of the current expansion factor of the Universe */ + double a_inv; + + /*! Current redshift */ + double z; + + /*! Hubble constant at the current redshift (in internal units) */ + double H; + + /*! Starting expansion factor */ + double a_begin; + + /*! Ending expansion factor */ + double a_end; + + /*! Reduced Hubble constant (H0 / (100km/s/Mpc)) */ + double h; + + /*! Hubble constant at z = 0 (in internal units) */ + double H0; + + /*! Matter density parameter */ + double Omega_m; + + /*! Baryon density parameter */ + double Omega_b; + + /*! Radiation constant density parameter */ + double Omega_lambda; + + /*! Cosmological constant density parameter */ + double Omega_r; + + /*! Curvature density parameter */ + double Omega_k; +}; + +void cosmology_update(struct cosmology *c, const struct engine *e); + +void cosmology_init(const struct swift_params* params, + const struct unit_system* us, + const struct phys_const* phys_const, + struct cosmology *c); + +void cosmology_print(const struct cosmology *c); + + +#endif /* SWIFT_COSMOLOGY_H */ + diff --git a/src/swift.h b/src/swift.h index eda566b19697ceeb1d66b266b44f4769be227585..ed7260ff4303154249339e84b8a501e52e08e60f 100644 --- a/src/swift.h +++ b/src/swift.h @@ -31,6 +31,7 @@ #include "clocks.h" #include "const.h" #include "cooling.h" +#include "cosmology.h" #include "cycle.h" #include "debug.h" #include "dump.h"