Initial import

This commit is contained in:
Guilhem Lavaux 2023-05-29 10:41:03 +02:00
commit 56a50eead3
820 changed files with 192077 additions and 0 deletions

View file

@ -0,0 +1,56 @@
/*+
ARES/HADES/BORG Package -- ./extra/hmclet/src/hades_julia3.cpp
Copyright (C) 2014-2020 Guilhem Lavaux <guilhem.lavaux@iap.fr>
Copyright (C) 2009-2020 Jens Jasche <jens.jasche@fysik.su.se>
Additional contributions from:
Guilhem Lavaux <guilhem.lavaux@iap.fr> (2023)
+*/
#define SAMPLER_DATA_INIT "ares_init.hpp"
#define SAMPLER_BUNDLE "julia_bundle.hpp"
#define SAMPLER_BUNDLE_INIT "julia_bundle_init.hpp"
#define SAMPLER_NAME "HADES3"
#define SAMPLER_MOCK_GENERATOR "julia_mock_gen.hpp"
#include "common/sampler_base.cpp"
#include "libLSS/tools/color_mod.hpp"
using namespace LibLSS::Color;
namespace {
void init_splash() {
static string splash_str[] = {
" ",
" /\\_/\\____, "
"____________________________ ",
" ,___/\\_/\\ \\ ~ / " +
fg(RED, "HADES+JULIA3", BRIGHT) + " ",
" \\ ~ \\ ) XXX ",
" XXX / /\\_/\\___, (c) Jens Jasche 2012 - 2019",
" |---| \\o-o/-o-o/ ~ / Guilhem Lavaux 2014 - 2019",
" | ) / \\ XXX "
"____________________________ ",
" \\ / _| / \\ \\_/ ",
" --- ,-/ _ \\_/ \\ ",
" / ( /____,__| ) ",
" ( |_ ( ) \\) _| ",
" _/ _) \\ \\__/ (_ ",
" (,-(,(,(,/ \\,),),) "
"",
"Please acknowledge XXXX",
};
static const int numSplashStr = sizeof(splash_str) / sizeof(splash_str[0]);
for (int i = 0; i < numSplashStr; i++)
Console::instance().print<LOG_STD>(splash_str[i]);
}
void close_splash() {}
RegisterStaticInit reg_splash(init_splash, close_splash, 12);
} // namespace

View file

@ -0,0 +1,119 @@
/*+
ARES/HADES/BORG Package -- ./extra/hmclet/src/julia_bundle.hpp
Copyright (C) 2014-2020 Guilhem Lavaux <guilhem.lavaux@iap.fr>
Copyright (C) 2009-2020 Jens Jasche <jens.jasche@fysik.su.se>
Additional contributions from:
Guilhem Lavaux <guilhem.lavaux@iap.fr> (2023)
+*/
#ifndef _HADES_JULIA_BUNDLE_HPP
#define _HADES_JULIA_BUNDLE_HPP
#include "libLSS/samplers/hades/hades_linear_likelihood.hpp"
#include "libLSS/samplers/core/powerspec_tools.hpp"
#include "libLSS/samplers/ares/synthetic_selection.hpp"
#include "libLSS/samplers/rgen/density_sampler.hpp"
#include "libLSS/physics/forward_model.hpp"
#include "libLSS/physics/hades_log.hpp"
#include "libLSS/physics/hades_pt.hpp"
#include "hades_option.hpp"
#include "libLSS/borg_version.hpp"
#include "libLSS/physics/modified_ngp.hpp"
#include "libLSS/physics/modified_ngp_smooth.hpp"
#include "libLSS/physics/forwards/borg_lpt.hpp"
#include "libLSS/physics/forwards/borg_2lpt.hpp"
#include "libLSS/physics/forwards/borg_multi_pm.hpp"
#include "libLSS/samplers/generic/generic_sigma8.hpp"
#include "libLSS/samplers/julia/julia_likelihood.hpp"
#include <boost/algorithm/string.hpp>
namespace LibLSS {
namespace {
HMCOption::IntegratorScheme get_Scheme(const std::string &s) {
std::string scheme = boost::to_upper_copy<std::string>(s);
using namespace HMCOption;
if (scheme == "SI_2A" || scheme == "LEAP_FROG") {
return SI_2A;
} else if (scheme == "SI_2B") {
return SI_2B;
} else if (scheme == "SI_2C") {
return SI_2C;
} else if (scheme == "SI_3A") {
return SI_3A;
} else if (scheme == "SI_4B") {
return SI_4B;
} else if (scheme == "SI_4C") {
return SI_4C;
} else if (scheme == "SI_4D") {
return SI_4D;
} else if (scheme == "SI_6A") {
return SI_6A;
} else {
error_helper<ErrorBadState>(
boost::format("Invalid integration scheme %s") % scheme);
}
}
} // namespace
class DummyPowerSpectrum : public PowerSpectrumSampler_Base {
public:
DummyPowerSpectrum(MPI_Communication *comm)
: PowerSpectrumSampler_Base(comm) {}
virtual void initialize(MarkovState &state) { initialize_base(state); }
virtual void restore(MarkovState &state) { restore_base(state); }
virtual void sample(MarkovState &state) {}
};
struct SamplerBundle {
//BlockLoop foreground_block;
typedef std::list<MarkovSampler *> SamplerList;
std::function<MarkovSampler *(int, int)> foreground_sampler_generator;
DummyPowerSpectrum dummy_ps;
SamplerList foreground_samplers;
MPI_Communication *comm;
std::shared_ptr<GenericDensitySampler> density_mc;
std::shared_ptr<MarkovSampler> bias;
std::shared_ptr<JuliaDensityLikelihood> julia_likelihood;
bool delegate_ic_to_julia;
BlockLoop foreground_block;
SyntheticSelectionUpdater sel_updater;
SamplerBundle(MPI_Communication *comm)
: comm(comm), dummy_ps(comm), delegate_ic_to_julia(false) {}
void newForeground(int catalog, int fgmap) {
Console::instance().print<LOG_VERBOSE>("Adding new foreground sampler");
MarkovSampler *fgsample = foreground_sampler_generator(catalog, fgmap);
if (fgsample != 0) {
foreground_samplers.push_back(fgsample);
foreground_block << (*fgsample);
}
}
~SamplerBundle() {
Console::instance().print<LOG_VERBOSE>("Begin destroying the bundle");
for (SamplerList::iterator i = foreground_samplers.begin();
i != foreground_samplers.end(); ++i) {
delete (*i);
}
Console::instance().print<LOG_VERBOSE>("Done destroying the bundle");
}
};
} // namespace LibLSS
#endif

View file

@ -0,0 +1,256 @@
/*+
ARES/HADES/BORG Package -- ./extra/hmclet/src/julia_bundle_init.hpp
Copyright (C) 2014-2020 Guilhem Lavaux <guilhem.lavaux@iap.fr>
Copyright (C) 2009-2020 Jens Jasche <jens.jasche@fysik.su.se>
Additional contributions from:
Guilhem Lavaux <guilhem.lavaux@iap.fr> (2023)
+*/
#ifndef __HADES_JULIA_BUNDLE_INIT_HPP
#define __HADES_JULIA_BUNDLE_INIT_HPP
#include "julia_bundle.hpp"
#include "libLSS/hmclet/julia_slice.hpp"
#include "libLSS/hmclet/julia_hmclet.hpp"
#include "likelihood_info.hpp"
#include "libLSS/samplers/rgen/qnhmc/qnhmc_density_sampler.hpp"
#include "libLSS/samplers/core/generate_random_field.hpp"
#include "setup_models.hpp"
namespace LibLSS {
template <typename ptree>
void sampler_bundle_init(
MPI_Communication *mpi_world, ptree &params, SamplerBundle &bundle,
MainLoop &loop, bool resuming) {
using boost::format;
using CosmoTool::square;
using std::string;
ptree system_params = params.get_child("system");
ptree julia_params = params.get_child("julia");
auto block_loop_params = params.get_child_optional("block_loop");
auto borg_params = params.get_child("gravity");
int hades_mixing = params.template get<int>("hades.mixing", 20);
std::string lh_type =
params.template get<std::string>("hades.likelihood", "LINEAR");
std::shared_ptr<MarkovSampler> nmean, bias;
typedef GridDensityLikelihoodBase<3> grid_t;
std::shared_ptr<grid_t> likelihood;
MarkovState &state = loop.get_state();
auto &cons = Console::instance();
BorgModelElement *model = new BorgModelElement();
model->obj = 0;
loop.get_state().newElement("BORG_model", model);
loop.get_state().newScalar("BORG_version", BORG_GIT_VERSION);
BoxModel box;
box.xmin0 = state.getScalar<double>("corner0");
box.xmin1 = state.getScalar<double>("corner1");
box.xmin2 = state.getScalar<double>("corner2");
box.L0 = state.getScalar<double>("L0");
box.L1 = state.getScalar<double>("L1");
box.L2 = state.getScalar<double>("L2");
box.N0 = state.getScalar<long>("N0");
box.N1 = state.getScalar<long>("N1");
box.N2 = state.getScalar<long>("N2");
model->obj = buildModel(
MPI_Communication::instance(), state, box, params, borg_params);
string code_path = julia_params.template get<string>("likelihood_path");
string module_name = julia_params.template get<string>("likelihood_module");
string bias_sampler_type =
julia_params.template get<string>("bias_sampler_type");
// string bias_sampler = julia_params.template get<string>("bias_sampler");
LikelihoodInfo like_info;
LibLSS_prepare::setupLikelihoodInfo(
mpi_world, loop.get_state(), like_info, params, resuming);
likelihood = bundle.julia_likelihood =
std::make_shared<JuliaDensityLikelihood>(
bundle.comm, like_info, code_path, module_name);
bundle.delegate_ic_to_julia =
julia_params.template get<bool>("ic_in_julia", false);
auto burnin = julia_params.template get<size_t>("mass_burnin", 300);
auto memory = julia_params.template get<size_t>("mass_burnin_memory", 50);
if (bias_sampler_type == "hmclet") {
auto hmclet_maxEpsilon =
julia_params.template get_optional<double>("hmclet_maxEpsilon");
auto hmclet_maxNtime =
julia_params.template get_optional<int>("hmclet_maxNtime");
auto hmcMatrix =
julia_params.template get<std::string>("hmclet_matrix", "DIAGONAL");
auto massScaling =
julia_params.template get<double>("hmclet_massScale", 0.);
auto limiter =
julia_params.template get<double>("hmclet_correlationLimiter", 0.5);
auto frozen = julia_params.template get<bool>("hmclet_frozen", false);
JuliaHmclet::types::MatrixType matrixType = JuliaHmclet::types::DIAGONAL;
if (hmcMatrix == "DIAGONAL")
matrixType = JuliaHmclet::types::DIAGONAL;
else if (hmcMatrix == "DENSE")
matrixType = JuliaHmclet::types::DENSE;
else if (hmcMatrix == "QN_DIAGONAL")
matrixType = JuliaHmclet::types::QN_DIAGONAL;
else {
error_helper<ErrorBadState>(
"Invalid matrix type for HMC: " + hmcMatrix);
}
Console::instance().print<LOG_INFO>("Build hmclet");
auto julia_hmclet = std::make_shared<JuliaHmcletMeta>(
bundle.comm, bundle.julia_likelihood, module_name, matrixType, burnin,
memory, limiter, frozen);
julia_hmclet->postinit().ready(
[julia_hmclet, hmclet_maxEpsilon, hmclet_maxNtime,
massScaling]() -> void {
Console &cons = Console::instance();
cons.print<LOG_VERBOSE>(
format("Number of hmclets = %d") %
julia_hmclet->hmclets().size());
for (auto &hmc : julia_hmclet->hmclets()) {
if (hmclet_maxEpsilon) {
cons.print<LOG_VERBOSE>(
format("Setup hmclet epsilon=%g") % *hmclet_maxEpsilon);
hmc->setMaxEpsilon(*hmclet_maxEpsilon);
}
if (hmclet_maxNtime) {
cons.print<LOG_VERBOSE>(
format("Setup hmclet ntime=%d") % *hmclet_maxNtime);
hmc->setMaxNtime(*hmclet_maxNtime);
}
hmc->setMassScaling(massScaling);
}
});
bias = bundle.bias = julia_hmclet;
} else if (bias_sampler_type == "slice") {
bias = bundle.bias = std::make_shared<JuliaMetaSlice>(
bundle.comm, module_name, bundle.julia_likelihood, burnin, memory);
} else if (bias_sampler_type == "none") {
} else {
error_helper<ErrorParams>("Unknown bias sampler type");
}
// Initialize foregrounds
LibLSS_prepare::initForegrounds(
mpi_world, loop.get_state(),
[&bundle](int a, int b) { bundle.newForeground(a, b); }, params);
/* if (!system_params.template get<bool>("block_sigma8_sampler", true))
bundle.sigma8_sampler = new GenericSigma8Sampler(bundle.comm);
else
bundle.sigma8_sampler = 0;
*/
std::string algorithm_name =
params.template get<std::string>("hades.algorithm", "HMC");
if (algorithm_name == "HMC") {
// -----------------------------------
// HMC algorithm initialization
double maxEpsilon =
params.template get<double>("hades.max_epsilon", 0.02);
int maxTimeSteps = params.template get<int>("hades.max_timesteps", 100);
std::string I_scheme_s =
params.template get<std::string>("hades.scheme", "SI_2A");
HMCOption::IntegratorScheme I_scheme = get_Scheme(I_scheme_s);
auto density_mc =
std::make_unique<HMCDensitySampler>(mpi_world, likelihood);
density_mc->setIntegratorScheme(I_scheme);
density_mc->setMaxEpsilon(maxEpsilon);
density_mc->setMaxTimeSteps(maxTimeSteps);
// HMC algorithm initialization - end
// -----------------------------------
bundle.density_mc = std::move(density_mc);
} else if (algorithm_name == "QN-HMC") {
double maxEpsilon =
params.template get<double>("hades.max_epsilon", 0.02);
int maxTimeSteps = params.template get<int>("hades.max_timesteps", 100);
std::string I_scheme_s =
params.template get<std::string>("hades.scheme", "SI_2A");
HMCOption::IntegratorScheme I_scheme = get_Scheme(I_scheme_s);
auto density_mc =
std::make_unique<QNHMCDensitySampler>(mpi_world, likelihood);
density_mc->setIntegratorScheme(I_scheme);
density_mc->setMaxEpsilon(maxEpsilon);
density_mc->setMaxTimeSteps(maxTimeSteps);
bundle.density_mc = std::move(density_mc);
} else {
error_helper<ErrorBadState>(
"Invalid algorithm name: " + algorithm_name +
" (choice is HMC or QN-HMC)");
}
bool hblock = adapt_optional<bool>(
loop.get_state(), block_loop_params, "hades_sampler_blocked", false,
DO_NOT_RESTORE);
adapt_optional<bool>(
loop.get_state(), block_loop_params, "bias_sampler_blocked", false,
DO_NOT_RESTORE);
adapt_optional<bool>(
loop.get_state(), block_loop_params, "nmean_sampler_blocked", false,
DO_NOT_RESTORE);
Console::instance().print<LOG_INFO_SINGLE>(
format("Hades mixing per mcmc step is %d") % hades_mixing);
Console::instance().print<LOG_INFO_SINGLE>(
format("Hades density is blocked: %s") % (hblock ? "YES" : "NO"));
loop << bundle.dummy_ps << bundle.sel_updater;
// ==================
// MAIN LOOP PROGRAM
if (bias != 0) {
auto bias_loop = new BlockLoop(1);
if (bias != 0)
*bias_loop << *bias;
loop
<< (BlockLoop(hades_mixing)
<< *bundle.density_mc << *bias_loop
<< (BlockLoop(10) << bundle.foreground_block));
delete bias_loop;
} else {
loop << (BlockLoop(hades_mixing) << *bundle.density_mc)
<< (BlockLoop(10) << bundle.foreground_block);
}
// If active, sample sigma8
// if (bundle.sigma8_sampler != 0)
// loop << *bundle.sigma8_sampler;
}
template <typename ptree>
void
sampler_setup_ic(SamplerBundle &bundle, MainLoop &loop, ptree const &params) {
MarkovState &state = loop.get_state();
if (bundle.delegate_ic_to_julia)
bundle.julia_likelihood->generateInitialConditions(state);
else {
generateRandomField(bundle.comm, state);
double initialRandomScaling =
params.template get<double>("mcmc.init_random_scaling", 0.1);
state.get<CArrayType>("s_hat_field")->eigen() *= initialRandomScaling;
state.get<ArrayType>("s_field")->eigen() *= initialRandomScaling;
}
}
void sampler_bundle_cleanup() {}
} // namespace LibLSS
#endif

View file

@ -0,0 +1,45 @@
/*+
ARES/HADES/BORG Package -- ./extra/hmclet/src/julia_mock_gen.hpp
Copyright (C) 2014-2020 Guilhem Lavaux <guilhem.lavaux@iap.fr>
Copyright (C) 2009-2020 Jens Jasche <jens.jasche@fysik.su.se>
Additional contributions from:
Guilhem Lavaux <guilhem.lavaux@iap.fr> (2023)
+*/
#ifndef __HADES_JULIA_MOCK_GEN_HPP
#define __HADES_JULIA_MOCK_GEN_HPP
#include <CosmoTool/algo.hpp>
#include <cmath>
namespace LibLSS {
template <typename PTree>
void prepareMockData(
PTree &ptree, MPI_Communication *comm, MarkovState &state,
CosmologicalParameters &cosmo_params, SamplerBundle &bundle) {
ConsoleContext<LOG_INFO_SINGLE> ctx("prepareMockData");
using boost::format;
using CosmoTool::square;
double Rsmooth = ptree.template get<double>("system.hades_smoothing", 1.0);
createCosmologicalPowerSpectrum(state, cosmo_params);
bundle.sel_updater.sample(state);
bundle.density_mc->generateMockData(state);
{
std::shared_ptr<H5::H5File> f;
if (comm->rank() == 0)
f = std::make_shared<H5::H5File>("mock_data.h5", H5F_ACC_TRUNC);
state.mpiSaveState(f, comm, false);
}
// bundle.hmc->generateRandomField(state);
// state.get<CArrayType>("s_hat_field")->eigen() *= 0.02;
// state.get<ArrayType>("s_field")->eigen() *= 0.02;
}
} // namespace LibLSS
#endif

View file

@ -0,0 +1,23 @@
check_ares_module(BORG_PRESENT borg)
set(extra_hmclet ${CMAKE_SOURCE_DIR}/extra/hmclet/src)
SET(HADES_OPTION)
include_directories(${extra_hmclet})
IF (BUILD_JULIA)
cmessage(STATUS "Activate Hades_Julia core")
add_executable(hades_julia3 ${extra_hmclet}/hades_julia3.cpp ${extra_hmclet}/julia_mock_gen.hpp)
target_link_libraries(hades_julia3 hades borg_models LSS ${DEP_LIBS} ${JULIA_LIBRARY})
add_dependencies(hades_julia3 ${ares_DEPS})
set_property(SOURCE ${extra_hmclet}/hades_julia3.cpp APPEND PROPERTY OBJECT_DEPENDS
${extra_hmclet}/julia_mock_gen.hpp
${extra_hmclet}/julia_bundle.hpp
${extra_hmclet}/julia_bundle_init.hpp
${CMAKE_SOURCE_DIR}/src/ares_init.hpp)
ELSE()
cmessage(CWARNING "Julia missing, Hades_Julia disabled")
ENDIF()