Commit bc7eb5f7 authored by Matthieu Schaller's avatar Matthieu Schaller
Browse files

Added example of a constant box evolving with cosmology.

parent 3f4fa041
# Define the system of units to use internally.
InternalUnitSystem:
UnitMass_in_cgs: 1.98848e43 # 10^10 M_sun in grams
UnitLength_in_cgs: 3.08567758e24 # Mpc in centimeters
UnitVelocity_in_cgs: 1e5 # km/s in centimeters per second
UnitCurrent_in_cgs: 1 # Amperes
UnitTemp_in_cgs: 1 # Kelvin
Cosmology:
Omega_m: 1.
Omega_lambda: 0.
Omega_b: 1.
h: 1.
a_begin: 0.00990099
a_end: 1.0
# Parameters governing the time integration
TimeIntegration:
dt_min: 1e-7 # The minimal time-step size of the simulation (in internal units).
dt_max: 5e-3 # The maximal time-step size of the simulation (in internal units).
# Parameters governing the snapshots
Snapshots:
basename: box # Common part of the name of output files
time_first: 0. # Time of the first output (in internal units)
delta_time: 1.04 # Time difference between consecutive outputs (in internal units)
scale_factor_first: 0.00991
compression: 4
# Parameters governing the conserved quantities statistics
Statistics:
delta_time: 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
CFL_condition: 0.1 # Courant-Friedrich-Levy condition for time integration.
# Parameters related to the initial conditions
InitialConditions:
file_name: ./constantBox.hdf5 # The file to read
Scheduler:
max_top_level_cells: 8
cell_split_size: 50
Gravity:
mesh_side_length: 32
eta: 0.025
theta: 0.3
r_cut_max: 5.
comoving_softening: 0.05
max_physical_softening: 0.05
################################################################################
# This file is part of SWIFT.
# Copyright (c) 2018 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
from numpy import *
# Parameters
T_i = 100. # Initial temperature of the gas (in K)
z_i = 100. # Initial redshift
gamma = 5./3. # Gas adiabatic index
numPart_1D = 32
#glassFile = "glassCube_32.hdf5"
fileName = "constantBox.hdf5"
# Some units
Mpc_in_m = 3.08567758e22
Msol_in_kg = 1.98848e30
Gyr_in_s = 3.08567758e19
mH_in_kg = 1.6737236e-27
# Some constants
kB_in_SI = 1.38064852e-23
G_in_SI = 6.67408e-11
# Some useful variables in h-full units
H_0 = 1. / Mpc_in_m * 10**5 # h s^-1
rho_0 = 3. * H_0**2 / (8* math.pi * G_in_SI) # h^2 kg m^-3
lambda_i = 64. / H_0 * 10**5 # h^-1 m (= 64 h^-1 Mpc)
x_min = -0.5 * lambda_i
x_max = 0.5 * lambda_i
# SI system of units
unit_l_in_si = Mpc_in_m
unit_m_in_si = Msol_in_kg * 1.e10
unit_t_in_si = Gyr_in_s
unit_v_in_si = unit_l_in_si / unit_t_in_si
unit_u_in_si = unit_v_in_si**2
#---------------------------------------------------
# Read the glass file
#glass = h5py.File(glassFile, "r" )
# Read particle positions and h from the glass
#pos = glass["/PartType0/Coordinates"][:,:]
#h = glass["/PartType0/SmoothingLength"][:] * 0.3
#glass.close()
# Total number of particles
#numPart = size(h)
#if numPart != numPart_1D**3:
# print "Non-matching glass file"
numPart = numPart_1D**3
# Set box size and interparticle distance
boxSize = x_max - x_min
delta_x = boxSize / numPart_1D
# Get the particle mass
a_i = 1. / (1. + z_i)
m_i = boxSize**3 * rho_0 / numPart
# Build the arrays
#pos *= boxSize
#h *= boxSize
coords = zeros((numPart, 3))
v = zeros((numPart, 3))
ids = linspace(1, numPart, numPart)
m = zeros(numPart)
h = zeros(numPart)
u = zeros(numPart)
# Set the particles on the left
for i in range(numPart_1D):
for j in range(numPart_1D):
for k in range(numPart_1D):
index = i * numPart_1D**2 + j * numPart_1D + k
coords[index,0] = (i + 0.5) * delta_x
coords[index,1] = (j + 0.5) * delta_x
coords[index,2] = (k + 0.5) * delta_x
u[index] = kB_in_SI * T_i / (gamma - 1.) / mH_in_kg
h[index] = 1.2348 * delta_x
m[index] = m_i
v[index,0] = 0.
v[index,1] = 0.
v[index,2] = 0.
# Unit conversion
coords /= unit_l_in_si
v /= unit_v_in_si
m /= unit_m_in_si
h /= unit_l_in_si
u /= unit_u_in_si
boxSize /= unit_l_in_si
#File
file = h5py.File(fileName, 'w')
# Header
grp = file.create_group("/Header")
grp.attrs["BoxSize"] = [boxSize, 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"] = 1
#Units
grp = file.create_group("/Units")
grp.attrs["Unit length in cgs (U_L)"] = 100. * unit_l_in_si
grp.attrs["Unit mass in cgs (U_M)"] = 1000. * unit_m_in_si
grp.attrs["Unit time in cgs (U_t)"] = 1. * unit_t_in_si
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")
grp.create_dataset('Coordinates', data=coords, dtype='d', compression="gzip", shuffle=True)
grp.create_dataset('Velocities', data=v, dtype='f',compression="gzip", shuffle=True)
grp.create_dataset('Masses', data=m, dtype='f', compression="gzip", shuffle=True)
grp.create_dataset('SmoothingLength', data=h, dtype='f', compression="gzip", shuffle=True)
grp.create_dataset('InternalEnergy', data=u, dtype='f', compression="gzip", shuffle=True)
grp.create_dataset('ParticleIDs', data=ids, dtype='L', compression="gzip", shuffle=True)
file.close()
#!/bin/bash
# Generate the initial conditions if they are not present.
if [ ! -e constantBox.hdf5 ]
then
echo "Generating initial conditions for the uniform cosmo box example..."
python makeIC.py
fi
# Run SWIFT
../swift -s -c -G -t 8 constant_volume.yml 2>&1 | tee output.log
# Plot the result
python plotSolution.py $i
......@@ -146,7 +146,7 @@ grp.create_dataset('ParticleIDs', data=ids, dtype='L')
file.close()
import pylab as pl
#import pylab as pl
pl.plot(coords[:,0], v[:,0], "k.")
pl.show()
#pl.plot(coords[:,0], v[:,0], "k.")
#pl.show()
Supports Markdown
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