From e646cf658cb8c2a9a6b68dece751ee04864fc088 Mon Sep 17 00:00:00 2001 From: Andrew Pontzen Date: Thu, 13 Jun 2024 11:53:40 +0100 Subject: [PATCH 1/4] Provide internal energy and smoothing output for GadgetHDF + baryon runs --- genetIC/src/cosmology/camb.hpp | 2 ++ genetIC/src/cosmology/parameters.hpp | 21 +++++++++++++++++++ genetIC/src/ic.hpp | 11 ++++++++++ genetIC/src/io/gadgethdf.hpp | 15 +++++++++++-- genetIC/src/io/swift.hpp | 4 ++++ genetIC/src/io/tipsy.hpp | 2 +- genetIC/src/main.cpp | 2 ++ .../src/simulation/particles/generator.hpp | 3 +++ .../particles/mapper/mapperiterator.hpp | 17 ++++++++++++--- .../simulation/particles/offsetgenerator.hpp | 4 ++++ .../src/simulation/particles/zeldovich.hpp | 5 +++++ .../paramfile.txt | 2 +- 12 files changed, 81 insertions(+), 7 deletions(-) diff --git a/genetIC/src/cosmology/camb.hpp b/genetIC/src/cosmology/camb.hpp index 90f52220..183bbd6d 100644 --- a/genetIC/src/cosmology/camb.hpp +++ b/genetIC/src/cosmology/camb.hpp @@ -65,6 +65,8 @@ namespace cosmology { public: + virtual ~PowerSpectrum() = default; + //! \brief Evaluate power spectrum for a given species at wavenumber k (Mpc/h), including the normalisation //! transferType specifies the transfer function to use (currently dm or baryon) virtual CoordinateType operator()(CoordinateType k, particle::species transferType) const = 0; diff --git a/genetIC/src/cosmology/parameters.hpp b/genetIC/src/cosmology/parameters.hpp index 7f781e6d..7ecca06c 100644 --- a/genetIC/src/cosmology/parameters.hpp +++ b/genetIC/src/cosmology/parameters.hpp @@ -28,8 +28,29 @@ namespace cosmology { FloatType sigma8; //!< Sigma8 parameter FloatType ns; //!< Scalar spectral index FloatType TCMB; //!< CMB temperature today, in K + + FloatType smoothNorm; //!< Smoothing scale for the initial conditions, as multiple of grid spacing }; + //! Estimate the temperature of the gas in the initial conditions, in K + template + FloatType getTemperature(const CosmologicalParameters &cosmo) { + if(cosmo.scalefactor > cosmo.scalefactorAtDecoupling) + return cosmo.TCMB * cosmo.scalefactorAtDecoupling / (cosmo.scalefactor * cosmo.scalefactor); + else + return cosmo.TCMB / cosmo.scalefactor; + } + + //! Estimate the specific internal energy of gas in the initial conditions, in km^2 s^-2 a [gadget units] + template + FloatType getInternalEnergy(const CosmologicalParameters &cosmo) { + FloatType H_mass_fraction = 0.76; // Hydrogen mass fraction + FloatType mean_molecular_weight = 4.0 / (1.0 + 3.0 * H_mass_fraction); + FloatType boltzmann_constant = 1.380649e-29; // Boltzmann constant in km^2 kg s^-2 K^-1 + FloatType proton_mass = 1.6726219e-27; // Proton mass in kg + return 1.5 * getTemperature(cosmo) * boltzmann_constant / ( proton_mass * mean_molecular_weight) / cosmo.scalefactor; + } + //! Computes an estimate of the linear growth factor. template FloatType growthFactor(const CosmologicalParameters &cosmology) { diff --git a/genetIC/src/ic.hpp b/genetIC/src/ic.hpp index 939ecc27..73a90960 100644 --- a/genetIC/src/ic.hpp +++ b/genetIC/src/ic.hpp @@ -205,6 +205,7 @@ class ICGenerator { cosmology.TCMB = 2.725; cosmology.scalefactorAtDecoupling = 1./150.; // NB - this is NOT photon decoupling, it's baryon decoupling cosmology.OmegaM0 = 0.3; + setNumNeighbours(64); outputFolder = "./"; // only an approximate value is needed to initialise a gas temperature @@ -1259,6 +1260,16 @@ class ICGenerator { } } + //! \brief Set the number of neighbours to use for smoothing + /*! + Note that this is only used for output smoothing length estimates, not by any calculations within + genetIC itself. + */ + void setNumNeighbours(unsigned int n) { + // density of particles is 1, so we want (4 pi smoothMultiple^3/3) = n + cosmology.smoothNorm = pow(3.0 * n / (4.0 * M_PI), 1.0 / 3.0); + } + //! Outputs the ICs in the defined format, creating appropriate particle generators if required virtual void write() { diff --git a/genetIC/src/io/gadgethdf.hpp b/genetIC/src/io/gadgethdf.hpp index 333597cd..58500e0e 100644 --- a/genetIC/src/io/gadgethdf.hpp +++ b/genetIC/src/io/gadgethdf.hpp @@ -219,12 +219,23 @@ namespace io { }, "Masses"); } + if (this->cosmology.OmegaBaryons0 > 0) { + OutputFloatType internalEnergy = cosmology::getInternalEnergy(this->cosmology); + saveBlock( + particle::species::baryon, + [internalEnergy](auto &localIterator) { + return internalEnergy; + }, "InternalEnergy"); - + saveBlock( + particle::species::baryon, + [](auto &localIterator) { + return localIterator.getSmoothingScale(); + }, "SmoothingLength"); + } } - }; diff --git a/genetIC/src/io/swift.hpp b/genetIC/src/io/swift.hpp index 34dfad7d..eb8dd5db 100644 --- a/genetIC/src/io/swift.hpp +++ b/genetIC/src/io/swift.hpp @@ -39,6 +39,10 @@ namespace io { HighFive::SilenceHDF5 silence; // suppresses HDF5 warnings while in scope SwiftHDFOutput output(Boxlength, mapper, generators, cosmology, nFiles); output(name); + logging::entry() << "Swift output saved."; + logging::entry() << " Note that Swift output follows the GadgetHDF format and unit conventions. You should"; + logging::entry() << " ensure that cleanup_h_factors and cleanup_velocity_factors are both enabled within your"; + logging::entry() << " Swift parameter file."; } diff --git a/genetIC/src/io/tipsy.hpp b/genetIC/src/io/tipsy.hpp index 9af987d9..65623c3d 100644 --- a/genetIC/src/io/tipsy.hpp +++ b/genetIC/src/io/tipsy.hpp @@ -79,7 +79,7 @@ namespace io { //! \brief Initialise baryonic particles: template void initialise(gas &p, const cosmology::CosmologicalParameters &cosmo) { - p.temp = cosmo.TCMB * cosmo.scalefactorAtDecoupling / (cosmo.scalefactor * cosmo.scalefactor); + p.temp = cosmology::getTemperature(cosmo); p.metals = 0.0; p.rho = 0.0; } diff --git a/genetIC/src/main.cpp b/genetIC/src/main.cpp index a115c7f6..f5ef40fd 100644 --- a/genetIC/src/main.cpp +++ b/genetIC/src/main.cpp @@ -60,6 +60,8 @@ void setup_parser(tools::ClassDispatch &dispatch) { dispatch.add_class_route("supersample_gas", &ICType::setSupersampleGas); dispatch.add_class_route("subsample", &ICType::setSubsample); dispatch.add_class_route("eps_norm", &ICType::setEpsNorm); + dispatch.add_class_route("num_neighbors", &ICType::setNumNeighbours); + dispatch.add_class_route("num_neighbours", &ICType::setNumNeighbours); // Grafic options dispatch.add_class_route("pvar", &ICType::setpvarValue); diff --git a/genetIC/src/simulation/particles/generator.hpp b/genetIC/src/simulation/particles/generator.hpp index 93028b10..3eed1050 100644 --- a/genetIC/src/simulation/particles/generator.hpp +++ b/genetIC/src/simulation/particles/generator.hpp @@ -34,6 +34,9 @@ namespace particle { //! Gets the cell softening scale of the grid for this evaluator virtual T getEps() const = 0; + //! Gets the smoothing scale for this evaluator + virtual T getSmoothingScale() const = 0; + //! Returns the particle at index id on the grid, without offset virtual Particle getParticleNoOffset(size_t id) const = 0; diff --git a/genetIC/src/simulation/particles/mapper/mapperiterator.hpp b/genetIC/src/simulation/particles/mapper/mapperiterator.hpp index bf09621f..59028f64 100644 --- a/genetIC/src/simulation/particles/mapper/mapperiterator.hpp +++ b/genetIC/src/simulation/particles/mapper/mapperiterator.hpp @@ -243,12 +243,23 @@ namespace particle { } - //! Gets the mass in the cell currently pointed at. - T getMass() const { + + template + inline auto getParticleProperty(Function func) const { EvaluatorPtrType pEval; size_t id; std::tie(pEval, id) = getParticleEvaluatorAndIndex(); - return pEval->getMass(); + return (pEval.get()->*func)(); + } + + //! Gets the mass in the cell currently pointed at. + T getMass() const { + return getParticleProperty(&ParticleEvaluator::getMass); + } + + //! Gets the smoothing scale in the cell currently pointed at. + T getSmoothingScale() const { + return getParticleProperty(&ParticleEvaluator::getSmoothingScale); } //! Returns a pointer to the pair obtained by dereferencing the iterator at its current position diff --git a/genetIC/src/simulation/particles/offsetgenerator.hpp b/genetIC/src/simulation/particles/offsetgenerator.hpp index 95a44956..51be341c 100644 --- a/genetIC/src/simulation/particles/offsetgenerator.hpp +++ b/genetIC/src/simulation/particles/offsetgenerator.hpp @@ -53,6 +53,10 @@ namespace particle { GridDataType getEps() const override { return underlying->getEps(); } + + GridDataType getSmoothingScale() const override { + return underlying->getSmoothingScale(); + } }; /*! \class OffsetMultiLevelParticleGenerator diff --git a/genetIC/src/simulation/particles/zeldovich.hpp b/genetIC/src/simulation/particles/zeldovich.hpp index 3906ede0..1359e13e 100644 --- a/genetIC/src/simulation/particles/zeldovich.hpp +++ b/genetIC/src/simulation/particles/zeldovich.hpp @@ -118,6 +118,11 @@ namespace particle { return onGrid->cellSize * onGrid->cellSofteningScale * epsNorm; } + //! Gets an SPH smoothing scale estimate for a single particle + virtual T getSmoothingScale() const override { + return onGrid->cellSize * cosmology.smoothNorm; + } + }; diff --git a/genetIC/tests/test_26a_gadgethdf_multifile_baryons/paramfile.txt b/genetIC/tests/test_26a_gadgethdf_multifile_baryons/paramfile.txt index 687a5144..87bc48ee 100644 --- a/genetIC/tests/test_26a_gadgethdf_multifile_baryons/paramfile.txt +++ b/genetIC/tests/test_26a_gadgethdf_multifile_baryons/paramfile.txt @@ -1,4 +1,4 @@ -# Based on test 10c, but use multi-file gadget output +# Multi-file gadgethdf output with baryons # output parameters From 5ac5204634a565fe191bc0b2491c6c86ae2a96cc Mon Sep 17 00:00:00 2001 From: Andrew Pontzen Date: Thu, 13 Jun 2024 14:26:03 +0100 Subject: [PATCH 2/4] Try to fix issue with macOS builds on CI --- genetIC/Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/genetIC/Makefile b/genetIC/Makefile index dac0c361..92e723b3 100644 --- a/genetIC/Makefile +++ b/genetIC/Makefile @@ -39,10 +39,10 @@ FFTW = -DFFTW3 -DFFTW_THREADS FFTWLIB = -lfftw3 -lfftw3f -lfftw3_threads # if pkg-config is installed, add $(pkg-config --libs-only-L hdf5) to HDFLIB: -HDFLIB = $(shell pkg-config --libs-only-L hdf5) -lhdf5 +HDFLIB = $(shell pkg-config --libs-only-L hdf5 fftw3) -lhdf5 # add also $(pkg-config --cflags hdf5) to CFLAGS: -CFLAGS += $(shell pkg-config --cflags hdf5) +CFLAGS += $(shell pkg-config --cflags hdf5 fftw3) # Provide information on the git repository, to be printed out on each run @@ -93,7 +93,7 @@ ifeq ($(HOST), clema) endif ifeq ($(HOST), butte) - CXX = g++-13 + CXX = g++-14 CPATH = /opt/homebrew/include/ LPATH = /opt/homebrew/lib/ CFLAGS += -Wl,-ld_classic # new macOS linker doesn't like C++, see XCode bug FB13097713 From 1fe7e9c70d3687e0f376e5b5192b2ec4c1c32db9 Mon Sep 17 00:00:00 2001 From: Andrew Pontzen Date: Thu, 13 Jun 2024 14:33:06 +0100 Subject: [PATCH 3/4] Another attempt to fix issue with macOS builds on CI --- genetIC/Makefile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/genetIC/Makefile b/genetIC/Makefile index 92e723b3..f5c2bcd9 100644 --- a/genetIC/Makefile +++ b/genetIC/Makefile @@ -24,7 +24,7 @@ CODEOPTIONS = -DDOUBLEPRECISION -DOUTPUT_IN_DOUBLEPRECISION -DCUBIC_INTERPOLATI CPATH ?= /opt/local/include/ LPATH ?= /opt/local/lib -GSLFLAGS = -lgsl -lgslcblas +GSLFLAGS = $(shell pkg-config --libs-only-L gsl) -lgsl -lgslcblas # Use -DFFTW3_THREADS=n to use threads. This will use n threads # UNLESS you are compiling with openmp, in which case you do not need to set n @@ -36,13 +36,13 @@ GSLFLAGS = -lgsl -lgslcblas # Note that genetIC no longer supports the use of FFTW2 FFTW = -DFFTW3 -DFFTW_THREADS -FFTWLIB = -lfftw3 -lfftw3f -lfftw3_threads +FFTWLIB = $(shell pkg-config --libs-only-L fftw3) -lfftw3 -lfftw3f -lfftw3_threads # if pkg-config is installed, add $(pkg-config --libs-only-L hdf5) to HDFLIB: -HDFLIB = $(shell pkg-config --libs-only-L hdf5 fftw3) -lhdf5 +HDFLIB = $(shell pkg-config --libs-only-L hdf5) -lhdf5 -# add also $(pkg-config --cflags hdf5) to CFLAGS: -CFLAGS += $(shell pkg-config --cflags hdf5 fftw3) +# add hdf5, fftw3, and gsl include paths to CFLAGS +CFLAGS += $(shell pkg-config --cflags hdf5 fftw3 gsl) # Provide information on the git repository, to be printed out on each run From fca1a3b7294b10c4f854bc4a4f8398ef54c4a0c9 Mon Sep 17 00:00:00 2001 From: Andrew Pontzen Date: Thu, 13 Jun 2024 14:37:45 +0100 Subject: [PATCH 4/4] Fix docker installation --- genetIC/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/genetIC/Dockerfile b/genetIC/Dockerfile index 4047072a..96fc9cf3 100644 --- a/genetIC/Dockerfile +++ b/genetIC/Dockerfile @@ -1,8 +1,8 @@ -FROM ubuntu:20.04 +FROM ubuntu:24.04 COPY ./ /genetIC RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y g++-12 libgsl-dev libfftw3-dev python3-pip pkg-config libhdf5-serial-dev -RUN pip3 install numpy pynbody +RUN pip3 install pynbody --break-system-packages # should use a venv but this is simpler for now RUN cd /genetIC && make clean && make ENTRYPOINT ["/genetIC/genetIC"]