Skip to content

Commit

Permalink
Merge pull request #126 from pynbody/mapping-tool
Browse files Browse the repository at this point in the history
Provide a separate particle mapping tool
  • Loading branch information
apontzen authored Oct 21, 2024
2 parents 3d0c258 + 4d701be commit b3622eb
Show file tree
Hide file tree
Showing 31 changed files with 514 additions and 329 deletions.
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project(IC)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/genetIC)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -g ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -g ")

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -DOPENMP -fopenmp -DDEBUG_INFO")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fopenmp -O3 -DOPENMP")
Expand All @@ -24,7 +24,6 @@ set(SOURCE_FILES
genetIC/src/io/gadgethdf.hpp
genetIC/src/io/input.hpp
genetIC/src/io/tipsy.hpp
genetIC/src/main.cpp
genetIC/src/simulation/coordinate.hpp
genetIC/src/cosmology/camb.hpp
genetIC/src/simulation/particles/mapper/mapper.hpp
Expand Down Expand Up @@ -64,7 +63,9 @@ set(SOURCE_FILES
genetIC/src/simulation/multilevelgrid/mask.hpp
genetIC/src/tools/memmap.hpp
genetIC/src/tools/numerics/tricubic.hpp genetIC/src/tools/logging.hpp genetIC/src/tools/logging.cpp genetIC/src/simulation/modifications/splice.hpp genetIC/src/tools/lru_cache.hpp
genetIC/src/io/swift.hpp)
genetIC/src/io/swift.hpp
genetIC/src/bindings.hpp
)

include_directories(/opt/homebrew/include)
link_directories(/opt/homebrew/lib)
Expand Down Expand Up @@ -123,4 +124,5 @@ add_definitions(-DCUBIC_INTERPOLATION
-DZELDOVICH_GRADIENT_FOURIER_SPACE
)
add_compile_options(-Wextra)
add_executable(genetIC ${SOURCE_FILES})
add_executable(genetIC ${SOURCE_FILES} genetIC/src/main.cpp)
add_executable(genetIC_mapper ${SOURCE_FILES} genetIC/src/main_mapper.cpp)
15 changes: 9 additions & 6 deletions genetIC/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Reasonably sensible defaults
CFLAGS ?= -Wall -g -lpthread -O3 -fopenmp -std=c++17 -fdiagnostics-color=auto -I`pwd` -I`pwd`/HighFive/include
CFLAGS ?= -Wall -g -lpthread -O3 -fopenmp -std=c++20 -fdiagnostics-color=auto -I`pwd` -I`pwd`/HighFive/include

# Here are code options. Probably don't fiddle with these unless you know what you're doing.
#
Expand Down Expand Up @@ -72,7 +72,7 @@ ifeq ($(HOST3), pfe)
CXX = /nasa/pkgsrc/2016Q2/gcc5/bin/g++
CPATH = /u/apontzen/genetIC/genetIC:/nasa/gsl/1.14/include/:/nasa/intel/Compiler/2016.2.181/compilers_and_libraries_2016.2.181/linux/mkl/include/fftw
LPATH = /nasa/gsl/1.14/lib/ -L/nasa/intel/Compiler/2016.2.181/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64/
CFLAGS = -Wall -g -O3 -fopenmp -std=c++17 -fdiagnostics-color=auto -I`pwd` -I`pwd`/HighFive/include -DOPENMP -DDOUBLEPRECISION
CFLAGS = -Wall -g -O3 -fopenmp -std=c++20 -fdiagnostics-color=auto -I`pwd` -I`pwd`/HighFive/include -DOPENMP -DDOUBLEPRECISION
FFTWLIB = -ldl -lpthread -lm -lstdc++ -lgfortran -lmkl_intel_ilp64 -lmkl_gnu_thread -lmkl_core
FFTW = -DFFTW3 -DFFTW_THREADS
GSLFLAGS = /nasa/gsl/1.14/lib/libgsl.a /nasa/gsl/1.14/lib/libgslcblas.a
Expand All @@ -81,14 +81,14 @@ endif
ifeq ($(HOST), Snowd)
CPATH = /opt/local/include/
LPATH = /opt/local/lib/
CFLAGS = -O3 -fopenmp -std=c++17 -fdiagnostics-color=auto -I`pwd` -I`pwd`/HighFive/include -DOPENMP -DDOUBLEPRECISION
CFLAGS = -O3 -fopenmp -std=c++20 -fdiagnostics-color=auto -I`pwd` -I`pwd`/HighFive/include -DOPENMP -DDOUBLEPRECISION
CXX = /opt/local/bin/clang++-mp-6.0
endif

ifeq ($(HOST), clema)
CPATH = /opt/local/include/
LPATH = /opt/local/lib/
CFLAGS = -O2 -g -fopenmp -std=c++17 -fdiagnostics-color=auto -I`pwd` -I`pwd`/HighFive/include -DOPENMP -DDOUBLEPRECISION
CFLAGS = -O2 -g -fopenmp -std=c++20 -fdiagnostics-color=auto -I`pwd` -I`pwd`/HighFive/include -DOPENMP -DDOUBLEPRECISION
CXX = /opt/local/bin/clang++-mp-6.0
endif

Expand All @@ -101,7 +101,7 @@ endif

ifeq ($(HOST), jasmi)
CXX = /opt/local/bin/g++-mp-13 -L/opt/local/lib/
CFLAGS = -O3 -g -fopenmp -std=c++17 -I`pwd` -I`pwd`/HighFive/include -DOPENMP -DDOUBLEPRECISION
CFLAGS = -O3 -g -fopenmp -std=c++20 -I`pwd` -I`pwd`/HighFive/include -DOPENMP -DDOUBLEPRECISION
endif

ifeq ($(HOST), Rhodo)
Expand Down Expand Up @@ -143,13 +143,16 @@ ifeq ($(HOST3), hyp)
CFLAGS += -Wextra
endif

all: genetIC
all: genetIC genetIC_mapper

%.o: %.cpp ; $(CXX) $(CFLAGS) $(CODEOPTIONS) $(GIT_VARIABLES) -I$(CPATH) $(FFTW) -c $< -o $@

genetIC: src/main.o src/tools/filesystem.o src/tools/progress/progress.o src/tools/logging.o
$(CXX) $(CFLAGS) -o genetIC $(GIT_VARIABLES) -I$(CPATH) $(FFTW) src/main.o src/tools/filesystem.o src/tools/progress/progress.o src/tools/logging.o -L$(LPATH) $(GSLFLAGS) -lm $(FFTWLIB) $(HDFLIB)

genetIC_mapper: src/main_mapper.o src/tools/filesystem.o src/tools/progress/progress.o src/tools/logging.o
$(CXX) $(CFLAGS) -o genetIC_mapper $(GIT_VARIABLES) -I$(CPATH) $(FFTW) src/main_mapper.o src/tools/filesystem.o src/tools/progress/progress.o src/tools/logging.o -L$(LPATH) $(GSLFLAGS) -lm $(FFTWLIB) $(HDFLIB)

clean:
rm -f genetIC
rm -f src/*.o
Expand Down
189 changes: 189 additions & 0 deletions genetIC/src/bindings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#ifndef _BINDINGS_HPP_INCLUDED
#define _BINDINGS_HPP_INCLUDED

#include "tools/parser.hpp"
#include "ic.hpp"

template<typename ICType>
void setup_parser(tools::ClassDispatch<ICType, void> &dispatch) {

using FloatType = typename ICType::T;

// Link command names used in parameter file to methods

// Set the cosmology
dispatch.add_class_route("TCMB", &ICType::setTCMB);
dispatch.add_class_route("Om", &ICType::setOmegaM0);
dispatch.add_class_route("Ob", &ICType::setOmegaB0);
dispatch.add_class_route("Ol", &ICType::setOmegaLambda0);
dispatch.add_class_route("hubble", &ICType::setHubble);
dispatch.add_class_route("s8", &ICType::setSigma8);
dispatch.add_class_route("ns", &ICType::setns);
dispatch.add_class_route("zin", &ICType::setZ0);
dispatch.add_class_route("z", &ICType::setZ0);

// Set seeds for random draws
// Static casts here needed to differentiate between overloaded versions of setSeed,
// now that we have both DM and baryon fields to seed.
// DEPRECATED forms:
dispatch.add_deprecated_class_route("seed", "random_seed_real_space", static_cast<void (ICType::*)(int)>(&ICType::setSeed));
dispatch.add_deprecated_class_route("seedfourier", "random_seed_serial", static_cast<void (ICType::*)(int)>(&ICType::setSeedFourier));
dispatch.add_class_route("seedfourier_reverse", static_cast<void (ICType::*)(int)>(&ICType::setSeedFourierReverseOrder));
dispatch.add_deprecated_class_route("seedfourier_parallel", "random_seed", static_cast<void (ICType::*)(int)>(&ICType::setSeedFourierParallel));

// NEW FORMS, May 2020, ahead of release (to steer people towards a sensible default)
dispatch.add_class_route("random_seed", static_cast<void (ICType::*)(int)>(&ICType::setSeedFourierParallel));
dispatch.add_class_route("random_seed_serial", static_cast<void (ICType::*)(int)>(&ICType::setSeedFourier));
dispatch.add_class_route("random_seed_real_space", static_cast<void (ICType::*)(int)>(&ICType::setSeed));

// Optional computational properties
dispatch.add_deprecated_class_route("exact_power_spectrum_enforcement", "fix_power", &ICType::setExactPowerSpectrumEnforcement);
dispatch.add_class_route("fix_power", &ICType::setExactPowerSpectrumEnforcement);

dispatch.add_class_route("strays_on", &ICType::setStraysOn);
dispatch.add_class_route("supersample", &ICType::setSupersample);
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);
dispatch.add_deprecated_class_route("center_grafic_output", "center_output",
&ICType::setCenteringOnRegion);


dispatch.add_class_route("centre_output", &ICType::setCenteringOnRegion);
// US variant:
dispatch.add_class_route("center_output", &ICType::setCenteringOnRegion);

// Gadget options
dispatch.add_class_route("gadget_particle_type", &ICType::setGadgetParticleType);
dispatch.add_class_route("gadget_flagged_particle_type", &ICType::setFlaggedGadgetParticleType);
dispatch.add_class_route("gadget_num_files", &ICType::setGadgetNumFiles);

// Define input files
dispatch.add_class_route("mapper_relative_to", &ICType::setInputMapper);
dispatch.add_class_route("camb", &ICType::setCambDat);
dispatch.add_class_route("powerlaw_amplitude", &ICType::setPowerLawAmplitude);

// Set output paths and format
dispatch.add_class_route("outdir", &ICType::setOutDir);
dispatch.add_class_route("outname", &ICType::setOutName);
dispatch.add_class_route("outformat", &ICType::setOutputFormat);

// Define grid structure - OLD NAMES
dispatch.add_deprecated_class_route("basegrid", "base_grid", &ICType::initBaseGrid);
dispatch.add_deprecated_class_route("zoomgrid", "zoom_grid", &ICType::initZoomGrid);
dispatch.add_deprecated_class_route("zoomgrid_with_origin_at", "zoom_grid_with_origin_at", &ICType::initZoomGridWithLowLeftCornerAt);

// Define grid structure - NEW NAMES May 2020
dispatch.add_class_route("base_grid", &ICType::initBaseGrid);
dispatch.add_class_route("zoom_grid", &ICType::initZoomGrid);
dispatch.add_class_route("zoom_grid_with_origin_at", &ICType::initZoomGridWithLowLeftCornerAt);


// Input Output of flagged particles - OLD NAMES
dispatch.add_deprecated_class_route("IDfile", "id_file", &ICType::loadID);
dispatch.add_deprecated_class_route("append_IDfile", "merge_id_file", &ICType::appendID);
dispatch.add_deprecated_class_route("dump_IDfile","dump_id_file", &ICType::dumpID);

// I/O of flagged particles - NEW NAMES May 2020
dispatch.add_class_route("id_file", &ICType::loadID);
dispatch.add_class_route("merge_id_file", &ICType::appendID);
dispatch.add_class_route("dump_id_file", &ICType::dumpID);

// Point to specific coordinates:
dispatch.add_class_route("centre_on", &ICType::centreParticle);
dispatch.add_class_route("centre", &ICType::setCentre);

// US variants:
dispatch.add_class_route("center_on", &ICType::centreParticle);
dispatch.add_class_route("center", &ICType::setCentre);

// Flag cells
dispatch.add_class_route("select_sphere", &ICType::selectSphere);
dispatch.add_class_route("select_ellipse", &ICType::selectEllipse);
dispatch.add_class_route("select_cube", &ICType::selectCube);
dispatch.add_class_route("select_nearest", &ICType::selectNearest);
dispatch.add_class_route("expand_flagged_region", &ICType::expandFlaggedRegion);
dispatch.add_class_route("adapt_mask", &ICType::adaptMask);
dispatch.add_class_route("autopad", &ICType::setAutopad);

// Deal with modifications
dispatch.add_class_route("modify", &ICType::modify);
dispatch.add_class_route("calculate", static_cast<void (ICType::*)(std::string)>(&ICType::calculate));
dispatch.add_class_route("filtering_scale", &ICType::setVarianceFilteringScale);
dispatch.add_class_route("clear_modifications", static_cast<void (ICType::*)()>(&ICType::clearModifications));
dispatch.add_class_route("done", &ICType::done);
dispatch.add_class_route("apply_modifications", static_cast<void (ICType::*)()>(&ICType::applyModifications));
dispatch.add_class_route("chi2", static_cast<void (ICType::*)()>(&ICType::getFieldChi2));

dispatch.add_class_route("reverse", static_cast<void (ICType::*)()>(&ICType::reverse));
dispatch.add_class_route("reverse_small_k", static_cast<void (ICType::*)(FloatType)>(&ICType::reverseSmallK));
dispatch.add_class_route("splice", &ICType::splice);

// Write objects to files
// dispatch.add_class_route("dump_grid", &ICType::dumpGrid);
dispatch.add_class_route("dump_grid", static_cast<void (ICType::*)(size_t)>(&ICType::dumpGrid));
dispatch.add_class_route("dump_vx", static_cast<void (ICType::*)(size_t)>(&ICType::dumpVelocityX));
dispatch.add_class_route("dump_grid_for_field", static_cast<void (ICType::*)(size_t, particle::species)>(&ICType::dumpGrid));
dispatch.add_class_route("dump_grid_fourier", static_cast<void (ICType::*)(size_t)>(&ICType::dumpGridFourier));
dispatch.add_class_route("dump_grid_fourier_for_field",
static_cast<void (ICType::*)(size_t, particle::species)>(&ICType::dumpGridFourier));
dispatch.add_class_route("dump_ps", static_cast<void (ICType::*)(size_t)>(&ICType::dumpPS));
dispatch.add_class_route("dump_ps_field", static_cast<void (ICType::*)(size_t, particle::species)>(&ICType::dumpPS));
dispatch.add_class_route("dump_tipsy", static_cast<void (ICType::*)(std::string)>(&ICType::saveTipsyArray));
dispatch.add_class_route("dump_tipsy_field", static_cast<void (ICType::*)(std::string, size_t)>(&ICType::saveTipsyArray));
dispatch.add_class_route("dump_mask", &ICType::dumpMask);

// Load existing random field instead of generating
dispatch.add_class_route("import_level", static_cast<void (ICType::*)(size_t, std::string)>(&ICType::importLevel));

// Extra commands related to the transfer functions:
dispatch.add_class_route("baryon_tf_on", &ICType::setUsingBaryons);
dispatch.add_class_route("baryons_all_levels", &ICType::setBaryonsOnAllLevels);

// To debug
dispatch.add_deprecated_class_route("zeroLevel", "zero_level", static_cast<void (ICType::*)(
size_t)>(&ICType::zeroLevel)); // Retained for backwards compatibility.
dispatch.add_class_route("zero_level", static_cast<void (ICType::*)(size_t)>(&ICType::zeroLevel)); // Use this instead.
dispatch.add_class_route("zero_level_field", static_cast<void (ICType::*)(size_t, size_t)>(&ICType::zeroLevel));

// Set an overall velocity offset for the entire box (useful for testing AMR sensitivity to flows)
dispatch.add_class_route("velocity_offset", &ICType::setOffset);

}

template<typename T>
void runInterpreter(T& target, const std::string &fname, std::ofstream &outf) {
runInterpreter<T>(target, fname, &outf);
}

template<typename T>
void runInterpreter(T& target, const std::string &fname, std::ofstream *outf = nullptr) {
tools::ClassDispatch<T, void> dispatch_generator;
setup_parser<T>(dispatch_generator);

auto dispatch = dispatch_generator.specify_instance(target);

std::ifstream inf;
inf.open(fname);

if (!inf.is_open())
throw std::runtime_error("Cannot open IC paramfile "+fname);

tools::ChangeCwdWhileInScope temporaryCwd(tools::getDirectoryName(fname));

if(outf != nullptr) {
dispatch.run_loop(inf, *outf);
} else {
dispatch.run_loop(inf);
}

}



#endif
2 changes: 1 addition & 1 deletion genetIC/src/cosmology/parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <functional>
#include <climits>

#include <iomanip>

namespace cosmology {

Expand Down
Loading

0 comments on commit b3622eb

Please sign in to comment.