Initial import
This commit is contained in:
commit
56a50eead3
820 changed files with 192077 additions and 0 deletions
18
extra/hades/libLSS/tests/data/gen_gradient_data.py
Normal file
18
extra/hades/libLSS/tests/data/gen_gradient_data.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
#+
|
||||
# ARES/HADES/BORG Package -- ./extra/hades/libLSS/tests/data/gen_gradient_data.py
|
||||
# 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)
|
||||
#
|
||||
#+
|
||||
import h5py as h5
|
||||
import numpy as np
|
||||
|
||||
N=32
|
||||
|
||||
numbers = np.random.normal(size=(N,N,N))
|
||||
with h5.File("gradient_numbers.h5", mode="w") as f:
|
||||
f['/random'] = numbers
|
||||
f['/random_fft'] = np.fft.rfftn(numbers)
|
BIN
extra/hades/libLSS/tests/data/gradient_numbers.h5
Normal file
BIN
extra/hades/libLSS/tests/data/gradient_numbers.h5
Normal file
Binary file not shown.
288
extra/hades/libLSS/tests/generic_gradient_benchmark.cpp
Normal file
288
extra/hades/libLSS/tests/generic_gradient_benchmark.cpp
Normal file
|
@ -0,0 +1,288 @@
|
|||
/*+
|
||||
ARES/HADES/BORG Package -- ./extra/hades/libLSS/tests/generic_gradient_benchmark.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)
|
||||
|
||||
+*/
|
||||
#include "libLSS/tools/static_init.hpp"
|
||||
#include "libLSS/samplers/core/types_samplers.hpp"
|
||||
#include "libLSS/samplers/core/powerspec_tools.hpp"
|
||||
#include "libLSS/mcmc/global_state.hpp"
|
||||
#include "libLSS/samplers/rgen/gsl_random_number.hpp"
|
||||
#include "libLSS/samplers/rgen/hmc/hmc_density_sampler.hpp"
|
||||
#include "libLSS/tools/array_tools.hpp"
|
||||
#include "libLSS/physics/cosmo.hpp"
|
||||
#include "libLSS/physics/forward_model.hpp"
|
||||
#include "libLSS/tools/mpi_fftw_helper.hpp"
|
||||
#include <CosmoTool/cosmopower.hpp>
|
||||
#include <CosmoTool/algo.hpp>
|
||||
#include <boost/format.hpp>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "libLSS/tests/setup_hades_test_run.hpp"
|
||||
|
||||
using namespace LibLSS;
|
||||
using boost::format;
|
||||
using CosmoTool::square;
|
||||
using std::endl;
|
||||
using std::ios;
|
||||
using std::ofstream;
|
||||
using std::string;
|
||||
|
||||
typedef boost::multi_array_types::extent_range range;
|
||||
|
||||
typedef RandomNumberThreaded<GSL_RandomNumber> RGenType;
|
||||
|
||||
static constexpr size_t BORG_RESOLUTION = 128; //128;
|
||||
|
||||
#ifndef BORG_SUPERSAMPLING
|
||||
# define BORG_SUPERSAMPLING 1
|
||||
#endif
|
||||
|
||||
#ifndef BORG_FORCESAMPLING
|
||||
# define BORG_FORCESAMPLING 1
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
#if defined(ARES_MPI_FFTW)
|
||||
RegisterStaticInit reg0(fftw_mpi_init, fftw_mpi_cleanup, 9, "MPI/FFTW");
|
||||
#endif
|
||||
RegisterStaticInit reg1(
|
||||
CosmoTool::init_fftw_wisdom, CosmoTool::save_fftw_wisdom, 10,
|
||||
"FFTW/WISDOM");
|
||||
#if !defined(ARES_MPI_FFTW) && \
|
||||
defined( \
|
||||
_OPENMP) // Do not use MPI and Threaded FFTW at the same time for the moment.
|
||||
RegisterStaticInit
|
||||
reg2(fftw_init_threads, fftw_cleanup_threads, 11, "FFTW/THREADS");
|
||||
#endif
|
||||
}; // 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) {}
|
||||
};
|
||||
|
||||
void createCosmologicalPowerSpectrum(
|
||||
MarkovState &state, CosmologicalParameters &cosmo_params,
|
||||
double adjust = 1) {
|
||||
double h;
|
||||
CosmoTool::CosmoPower cpower;
|
||||
|
||||
h = cpower.h = cosmo_params.h;
|
||||
cpower.OMEGA_B = cosmo_params.omega_b;
|
||||
cpower.OMEGA_C = cosmo_params.omega_m - cosmo_params.omega_b;
|
||||
cpower.SIGMA8 = cosmo_params.sigma8;
|
||||
cpower.setFunction(CosmoTool::CosmoPower::HU_WIGGLES);
|
||||
cpower.updateCosmology();
|
||||
cpower.normalize();
|
||||
|
||||
ArrayType1d::ArrayType &k = *state.get<ArrayType1d>("k_modes")->array;
|
||||
ArrayType1d::ArrayType &Pk = *state.get<ArrayType1d>("powerspectrum")->array;
|
||||
for (long i = 0; i < k.num_elements(); i++) {
|
||||
Pk[i] = cpower.power(k[i] * h) * h * h * h * adjust;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef MIN_RANK
|
||||
# define MIN_RANK 1
|
||||
#endif
|
||||
|
||||
static const int INIT_RANK = MIN_RANK;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
MPI_Communication *comm_base = setupMPI(argc, argv);
|
||||
StaticInit::execute();
|
||||
Console &cons = Console::instance();
|
||||
cons.setVerboseLevel<LOG_DEBUG>();
|
||||
int rankThreshold = INIT_RANK;
|
||||
int k = 0, kmax;
|
||||
using boost::chrono::system_clock;
|
||||
|
||||
cons.outputToFile(
|
||||
boost::str(format("gradient_bench_rank_%d.txt") % comm_base->rank()));
|
||||
|
||||
for (kmax = 0; rankThreshold < comm_base->size(); kmax++)
|
||||
rankThreshold *= 2;
|
||||
kmax++;
|
||||
|
||||
rankThreshold = INIT_RANK;
|
||||
|
||||
cons.print<LOG_DEBUG>(format("kmax = %d") % kmax);
|
||||
|
||||
while (k < kmax) {
|
||||
int color = (comm_base->rank() < rankThreshold) ? 0 : 1;
|
||||
// int color;
|
||||
// int groupper = comm_base->size() / rankThreshold;
|
||||
// if (groupper > 1)
|
||||
// color =
|
||||
// ((comm_base->rank() % groupper) == 0) ? 0 : 1;
|
||||
// else
|
||||
// color = comm_base->rank() == 0 ? 0 : 1;
|
||||
cons.format<LOG_DEBUG>("Color is %d", color);
|
||||
MPI_Communication *comm = comm_base->split(color, comm_base->rank());
|
||||
boost::chrono::system_clock::time_point start_context_forward,
|
||||
start_context_adjoint;
|
||||
boost::chrono::duration<double> duration_forward, duration_adjoint;
|
||||
|
||||
if (color == 1) {
|
||||
comm_base->barrier();
|
||||
rankThreshold = std::min(comm_base->size(), 2 * rankThreshold);
|
||||
k++;
|
||||
delete comm;
|
||||
continue;
|
||||
}
|
||||
|
||||
{
|
||||
MarkovState state;
|
||||
RGenType randgen(-1);
|
||||
int M;
|
||||
BoxModel box;
|
||||
BoxModel box2;
|
||||
LikelihoodInfo info;
|
||||
|
||||
randgen.seed(2348098);
|
||||
|
||||
state.newElement(
|
||||
"random_generator", new RandomStateElement<RandomNumber>(&randgen));
|
||||
|
||||
LibLSS_test::setup_hades_test_run(comm, BORG_RESOLUTION, 600, state);
|
||||
LibLSS_test::setup_box(state, box);
|
||||
|
||||
state.newScalar<long>("Ndata0", box.N0 / DOWNGRADE_DATA);
|
||||
state.newScalar<long>("Ndata1", box.N1 / DOWNGRADE_DATA);
|
||||
state.newScalar<long>("Ndata2", box.N2 / DOWNGRADE_DATA);
|
||||
// FIXME!
|
||||
state.newScalar<long>("localNdata0", state.getScalar<long>("startN0"));
|
||||
state.newScalar<long>(
|
||||
"localNdata1",
|
||||
state.getScalar<long>("startN0") + state.getScalar<long>("localN0"));
|
||||
state.newScalar<long>("localNdata2", 0);
|
||||
state.newScalar<long>("localNdata3", box.N1 / DOWNGRADE_DATA);
|
||||
state.newScalar<long>("localNdata4", 0);
|
||||
state.newScalar<long>("localNdata5", box.N2 / DOWNGRADE_DATA);
|
||||
|
||||
LibLSS_test::setup_likelihood_info(state, info, comm);
|
||||
|
||||
box2 = box;
|
||||
box2.N0 *= 2;
|
||||
box2.N1 *= 2;
|
||||
box2.N2 *= 2;
|
||||
|
||||
DummyPowerSpectrum dummy_p(comm);
|
||||
HMCDensitySampler::Likelihood_t likelihood = makeLikelihood(info);
|
||||
|
||||
auto model = buildModel(comm, state, box, box2);
|
||||
BorgModelElement *model_element = new BorgModelElement();
|
||||
|
||||
model_element->obj = model;
|
||||
state.newElement("BORG_model", model_element);
|
||||
|
||||
HMCDensitySampler hmc(comm, likelihood);
|
||||
|
||||
// Initialize (data,s)->t sampler
|
||||
dummy_p.init_markov(state);
|
||||
hmc.init_markov(state);
|
||||
|
||||
createCosmologicalPowerSpectrum(
|
||||
state, state.getScalar<CosmologicalParameters>("cosmology"));
|
||||
|
||||
// Build some mock field
|
||||
{
|
||||
ConsoleContext<LOG_INFO> ctx("Generation performance");
|
||||
timings::set_file_pattern("timing_generation_N_" + to_string(rankThreshold) + "_%d.txt");
|
||||
timings::reset();
|
||||
hmc.generateMockData(state);
|
||||
timings::trigger_dump();
|
||||
timings::set_file_pattern("timing_stats_N_" + to_string(rankThreshold) + "_%d.txt");
|
||||
}
|
||||
|
||||
typedef FFTW_Manager<double, 3> DFT_Manager;
|
||||
std::unique_ptr<DFT_Manager> mgr_p = std::make_unique<DFT_Manager>(
|
||||
BORG_RESOLUTION, BORG_RESOLUTION, BORG_RESOLUTION, comm);
|
||||
auto &mgr = *mgr_p;
|
||||
CArrayType *s_hat_field = state.get<CArrayType>("s_hat_field");
|
||||
auto gradient_field_p = mgr.allocate_complex_array();
|
||||
auto &gradient_field = gradient_field_p.get_array();
|
||||
auto tmp_field_p = mgr.allocate_complex_array();
|
||||
auto delta_out_p = mgr.allocate_array();
|
||||
auto &delta_out = delta_out_p.get_array();
|
||||
|
||||
double volume = box.L0 * box.L1 * box.L2;
|
||||
double ai = state.getScalar<double>("borg_a_initial");
|
||||
Cosmology cosmo(state.getScalar<CosmologicalParameters>("cosmology"));
|
||||
double D_init = cosmo.d_plus(ai) /
|
||||
cosmo.d_plus(1.0); // Scale factor for initial conditions
|
||||
|
||||
array::scaleAndCopyArray3d(
|
||||
tmp_field_p.get_array(), *s_hat_field->array, D_init);
|
||||
|
||||
{
|
||||
ConsoleContext<LOG_INFO> ctx("Forward-Gradient performance");
|
||||
timings::set_file_pattern("timing_forward_N_" + to_string(rankThreshold) + "_%d.txt");
|
||||
timings::reset();
|
||||
|
||||
start_context_forward = system_clock::now();
|
||||
//model->forwardModel(tmp_field_p.get_array(), delta_out, true);
|
||||
likelihood->logLikelihood(tmp_field_p.get_array());
|
||||
duration_forward = system_clock::now() - start_context_forward;
|
||||
timings::trigger_dump();
|
||||
}
|
||||
|
||||
if (RUN_RSD_TEST) {
|
||||
{
|
||||
ConsoleContext<LOG_INFO> ctx("Forward RSD performance");
|
||||
|
||||
double vobsext[3] = {100, 100, 100};
|
||||
start_context_adjoint = system_clock::now();
|
||||
model->forwardModelRsdField(delta_out, vobsext);
|
||||
duration_adjoint = system_clock::now() - start_context_adjoint;
|
||||
}
|
||||
|
||||
{
|
||||
double vobsext[3] = {0, 0, 0};
|
||||
model->forwardModelRsdField(delta_out, vobsext);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
ConsoleContext<LOG_INFO> ctx("Gradient performance");
|
||||
timings::set_file_pattern("timing_gradient_N_" + to_string(rankThreshold) + "_%d.txt");
|
||||
timings::reset();
|
||||
start_context_adjoint = system_clock::now();
|
||||
fwrap(gradient_field) = 1.0;
|
||||
likelihood->gradientLikelihood(
|
||||
tmp_field_p.get_array(), gradient_field, false, 1.0);
|
||||
duration_adjoint = system_clock::now() - start_context_adjoint;
|
||||
timings::trigger_dump();
|
||||
}
|
||||
}
|
||||
|
||||
if (comm_base->rank() == 0) {
|
||||
ofstream f_performance("bench_performance.txt", ios::app);
|
||||
f_performance << format("%s % 5d % 5d % 5d % .5lf % .5lf") % testName %
|
||||
BORG_RESOLUTION % rankThreshold %
|
||||
smp_get_max_threads() % duration_forward.count() %
|
||||
duration_adjoint.count()
|
||||
<< endl;
|
||||
}
|
||||
comm_base->barrier();
|
||||
rankThreshold = std::min(comm_base->size(), 2 * rankThreshold);
|
||||
k++;
|
||||
delete comm;
|
||||
}
|
||||
|
||||
StaticInit::finalize();
|
||||
doneMPI();
|
||||
|
||||
return 0;
|
||||
}
|
430
extra/hades/libLSS/tests/generic_gradient_test.cpp
Normal file
430
extra/hades/libLSS/tests/generic_gradient_test.cpp
Normal file
|
@ -0,0 +1,430 @@
|
|||
/*+
|
||||
ARES/HADES/BORG Package -- ./extra/hades/libLSS/tests/generic_gradient_test.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)
|
||||
|
||||
+*/
|
||||
#include "libLSS/tools/static_init.hpp"
|
||||
#include "libLSS/samplers/core/types_samplers.hpp"
|
||||
#include "libLSS/samplers/core/powerspec_tools.hpp"
|
||||
#include "libLSS/mcmc/global_state.hpp"
|
||||
#include "libLSS/samplers/rgen/gsl_random_number.hpp"
|
||||
#include "libLSS/tools/array_tools.hpp"
|
||||
#include "libLSS/physics/cosmo.hpp"
|
||||
#include "libLSS/physics/forward_model.hpp"
|
||||
#include <CosmoTool/cosmopower.hpp>
|
||||
#include <CosmoTool/algo.hpp>
|
||||
#include <boost/format.hpp>
|
||||
#include "libLSS/physics/likelihoods/base.hpp"
|
||||
#include "libLSS/physics/hades_pt.hpp"
|
||||
#include "libLSS/physics/chain_forward_model.hpp"
|
||||
#include "libLSS/physics/hermitic.hpp"
|
||||
|
||||
using namespace LibLSS;
|
||||
using boost::format;
|
||||
using CosmoTool::square;
|
||||
using std::string;
|
||||
|
||||
typedef boost::multi_array_types::extent_range range;
|
||||
|
||||
typedef RandomNumberMPI<GSL_RandomNumber> RGenType;
|
||||
|
||||
static const int STEP_GRADIENT = 1 * 1; //2*2;//8*8;
|
||||
static const bool TEST_BORG_REDSHIFT = false;
|
||||
|
||||
#ifndef BORG_SUPERSAMPLING
|
||||
# define BORG_SUPERSAMPLING 1
|
||||
#endif
|
||||
|
||||
#ifndef BORG_FORCESAMPLING
|
||||
# define BORG_FORCESAMPLING 1
|
||||
#endif
|
||||
|
||||
#ifndef MODEL_TO_TEST
|
||||
# define MODEL_TO_TEST(model, box) \
|
||||
auto model = new HadesLinear(comm, box, 0.001)
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
#if defined(ARES_MPI_FFTW)
|
||||
RegisterStaticInit reg0(fftw_mpi_init, fftw_mpi_cleanup, 9, "MPI/FFTW");
|
||||
#endif
|
||||
RegisterStaticInit reg1(
|
||||
CosmoTool::init_fftw_wisdom, CosmoTool::save_fftw_wisdom, 10,
|
||||
"FFTW/WISDOM");
|
||||
}; // 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) {}
|
||||
};
|
||||
|
||||
void createCosmologicalPowerSpectrum(
|
||||
MarkovState &state, CosmologicalParameters &cosmo_params,
|
||||
double adjust = 1) {
|
||||
double h;
|
||||
CosmoTool::CosmoPower cpower;
|
||||
|
||||
h = cpower.h = cosmo_params.h;
|
||||
cpower.OMEGA_B = cosmo_params.omega_b;
|
||||
cpower.OMEGA_C = cosmo_params.omega_m - cosmo_params.omega_b;
|
||||
cpower.SIGMA8 = cosmo_params.sigma8;
|
||||
cpower.setFunction(CosmoTool::CosmoPower::HU_WIGGLES);
|
||||
cpower.updateCosmology();
|
||||
cpower.normalize();
|
||||
|
||||
ArrayType1d::ArrayType &k = *state.get<ArrayType1d>("k_modes")->array;
|
||||
ArrayType1d::ArrayType &Pk = *state.get<ArrayType1d>("powerspectrum")->array;
|
||||
for (long i = 0; i < k.num_elements(); i++) {
|
||||
Pk[i] = cpower.power(k[i] * h) * h * h * h * adjust;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
MPI_Communication *comm = setupMPI(argc, argv);
|
||||
StaticInit::execute();
|
||||
Console &cons = Console::instance();
|
||||
cons.setVerboseLevel<LOG_DEBUG>();
|
||||
|
||||
cons.outputToFile(
|
||||
boost::str(format("gradient_test_rank_%d.txt") % comm->rank()));
|
||||
{
|
||||
MarkovState state;
|
||||
SelArrayType *sel_data, *sel_data2;
|
||||
SLong *N0, *N1, *N2, *N2_HC, *Ncat, *fourierLocalSize, *NUM_MODES, *localN0,
|
||||
*startN0;
|
||||
SDouble *L0, *L1, *L2, *K_MIN, *K_MAX, *corner0, *corner1, *corner2,
|
||||
*borg_a_initial;
|
||||
ArrayType1d *bias0;
|
||||
ArrayType *data0, *growth;
|
||||
RGenType randgen(comm, -1);
|
||||
int M;
|
||||
BoxModel box, box2;
|
||||
|
||||
randgen.seed(2348098);
|
||||
|
||||
state.newElement(
|
||||
"random_generator", new RandomStateElement<RandomNumber>(&randgen));
|
||||
|
||||
state.newElement("N0", N0 = new SLong());
|
||||
state.newElement("N1", N1 = new SLong());
|
||||
state.newElement("N2", N2 = new SLong());
|
||||
state.newElement("N2_HC", N2_HC = new SLong());
|
||||
state.newElement("NUM_MODES", NUM_MODES = new SLong());
|
||||
state.newElement("K_MIN", K_MIN = new SDouble());
|
||||
state.newElement("K_MAX", K_MAX = new SDouble());
|
||||
|
||||
state.newElement("L0", L0 = new SDouble());
|
||||
state.newElement("L1", L1 = new SDouble());
|
||||
state.newElement("L2", L2 = new SDouble());
|
||||
|
||||
state.newElement("NCAT", Ncat = new SLong());
|
||||
state.newElement("startN0", startN0 = new SLong());
|
||||
state.newElement("localN0", localN0 = new SLong());
|
||||
state.newElement("fourierLocalSize", fourierLocalSize = new SLong());
|
||||
|
||||
state.newElement("corner0", corner0 = new SDouble());
|
||||
state.newElement("corner1", corner1 = new SDouble());
|
||||
state.newElement("corner2", corner2 = new SDouble());
|
||||
|
||||
state.newElement("borg_a_initial", borg_a_initial = new SDouble());
|
||||
|
||||
state.newScalar<double>("ares_heat", 1.0);
|
||||
state.newScalar<int>("borg_pm_nsteps", 1);
|
||||
state.newScalar<double>("borg_pm_start_z", 69.);
|
||||
state.newScalar<bool>("borg_do_rsd", TEST_BORG_REDSHIFT);
|
||||
state.newScalar<int>("borg_supersampling", BORG_SUPERSAMPLING);
|
||||
state.newScalar<int>("borg_forcesampling", BORG_FORCESAMPLING);
|
||||
|
||||
M = box.N0 = N0->value = 16;
|
||||
box.N1 = N1->value = M;
|
||||
box.N2 = N2->value = M;
|
||||
|
||||
state.newScalar<long>("Ndata0", M / DOWNGRADE_DATA);
|
||||
state.newScalar<long>("Ndata1", M / DOWNGRADE_DATA);
|
||||
state.newScalar<long>("Ndata2", M / DOWNGRADE_DATA);
|
||||
ptrdiff_t localn0, startn0, data_localn0, data_startn0;
|
||||
|
||||
#ifdef ARES_MPI_FFTW
|
||||
{
|
||||
|
||||
fourierLocalSize->value =
|
||||
MPI_FCalls::local_size_3d(M, M, M, comm->comm(), &localn0, &startn0);
|
||||
startN0->value = startn0;
|
||||
localN0->value = localn0;
|
||||
|
||||
MPI_FCalls::local_size_3d(
|
||||
M / DOWNGRADE_DATA, M / DOWNGRADE_DATA, M / DOWNGRADE_DATA,
|
||||
comm->comm(), &data_localn0, &data_startn0);
|
||||
}
|
||||
#else
|
||||
fourierLocalSize->value = M * M * (M / 2 + 1);
|
||||
startn0 = startN0->value = 0;
|
||||
localn0 = localN0->value = M;
|
||||
data_localn0 = M / DOWNGRADE_DATA;
|
||||
data_startn0 = 0;
|
||||
#endif
|
||||
|
||||
state.newScalar<long>("localNdata0", data_startn0);
|
||||
state.newScalar<long>("localNdata1", data_startn0 + data_localn0);
|
||||
state.newScalar<long>("localNdata2", 0);
|
||||
state.newScalar<long>("localNdata3", (M / DOWNGRADE_DATA));
|
||||
state.newScalar<long>("localNdata4", 0);
|
||||
state.newScalar<long>("localNdata5", (M / DOWNGRADE_DATA));
|
||||
|
||||
cons.print<LOG_INFO>(
|
||||
format("startN0 = %d, localN0 = %d") % startN0->value % localN0->value);
|
||||
|
||||
Ncat->value = 1;
|
||||
|
||||
box.xmin0 = corner0->value = -1500;
|
||||
box.xmin1 = corner1->value = -1500;
|
||||
box.xmin2 = corner2->value = -1500;
|
||||
N2_HC->value = M / 2 + 1;
|
||||
NUM_MODES->value = 300;
|
||||
box.L0 = L0->value = 3000.0;
|
||||
box.L1 = L1->value = 3000.0;
|
||||
box.L2 = L2->value = 3000.0;
|
||||
K_MIN->value = 0.;
|
||||
K_MAX->value =
|
||||
M_PI *
|
||||
sqrt(
|
||||
square(N0->value / L0->value) + square(N1->value / L1->value) +
|
||||
square(N2->value / L2->value)) *
|
||||
1.1;
|
||||
|
||||
box2 = box;
|
||||
box2.N0 *= 2;
|
||||
box2.N1 *= 2;
|
||||
box2.N2 *= 2;
|
||||
|
||||
borg_a_initial->value = 0.001;
|
||||
|
||||
state.newElement(
|
||||
"growth_factor",
|
||||
growth = new ArrayType(
|
||||
boost::extents[range(startn0, startn0 + localn0)][M][M]));
|
||||
growth->eigen().fill(1);
|
||||
growth->setRealDims(ArrayDimension(M, M, M));
|
||||
if (DOWNGRADE_DATA == 1) {
|
||||
auto ext = boost::extents[range(startn0, startn0 + localn0)][M][M];
|
||||
auto adim = ArrayDimension(M, M, M);
|
||||
state.newElement("galaxy_data_0", data0 = new ArrayType(ext));
|
||||
data0->setRealDims(adim);
|
||||
state.newElement("galaxy_sel_window_0", sel_data = new SelArrayType(ext));
|
||||
sel_data->setRealDims(adim);
|
||||
state.newElement(
|
||||
"galaxy_synthetic_sel_window_0", sel_data2 = new SelArrayType(ext));
|
||||
sel_data2->setRealDims(adim);
|
||||
} else {
|
||||
size_t startdown = data_startn0;
|
||||
size_t enddown = data_startn0 + data_localn0;
|
||||
auto ext = boost::extents[range(startdown, enddown)][M / DOWNGRADE_DATA]
|
||||
[M / DOWNGRADE_DATA];
|
||||
auto adim = ArrayDimension(
|
||||
M / DOWNGRADE_DATA, M / DOWNGRADE_DATA, M / DOWNGRADE_DATA);
|
||||
|
||||
state.newElement("galaxy_data_0", data0 = new ArrayType(ext));
|
||||
data0->setRealDims(adim);
|
||||
state.newElement("galaxy_sel_window_0", sel_data = new SelArrayType(ext));
|
||||
sel_data->setRealDims(adim);
|
||||
state.newElement(
|
||||
"galaxy_synthetic_sel_window_0", sel_data2 = new SelArrayType(ext));
|
||||
sel_data2->setRealDims(adim);
|
||||
}
|
||||
state.newElement(
|
||||
"galaxy_bias_0", bias0 = new ArrayType1d(boost::extents[0]));
|
||||
state.newScalar("galaxy_nmean_0", 20.0);
|
||||
state.newScalar("galaxy_bias_ref_0", false);
|
||||
state.newScalar("ares_heat", 1.0);
|
||||
|
||||
ScalarStateElement<CosmologicalParameters> *s_cosmo =
|
||||
new ScalarStateElement<CosmologicalParameters>();
|
||||
state.newElement("cosmology", s_cosmo);
|
||||
|
||||
CosmologicalParameters &cparams = s_cosmo->value;
|
||||
cparams.omega_r = 0.; /* negligible radiation density */
|
||||
cparams.omega_k = 0.; /* curvature - flat prior for everything! */
|
||||
cparams.omega_m = 0.3175;
|
||||
cparams.omega_b = 0.049;
|
||||
cparams.omega_q = 0.6825;
|
||||
cparams.w = -1.;
|
||||
cparams.n_s = 0.9624;
|
||||
cparams.wprime = 0.;
|
||||
cparams.sigma8 = 0.8344;
|
||||
cparams.h = 0.6711;
|
||||
cparams.beta = 1.5;
|
||||
cparams.z0 = 0.;
|
||||
cparams.a0 = 1.; /* scale factor at epoch of observation usually 1*/
|
||||
// Initialize (data,s)->t sampler
|
||||
|
||||
LikelihoodInfo info;
|
||||
|
||||
{
|
||||
namespace L = LibLSS::Likelihood;
|
||||
|
||||
info[L::MPI] = MPI_Communication::instance();
|
||||
info["ManyPower_prior_width"] = 3.5;
|
||||
|
||||
L::GridSize gs(boost::extents[3]), mpi_gs(boost::extents[6]),
|
||||
gsd(boost::extents[3]);
|
||||
L::GridLengths gl(boost::extents[6]);
|
||||
|
||||
state.getScalarArray<long, 3>("Ndata", gsd);
|
||||
|
||||
gs[0] = N0->value;
|
||||
gs[1] = N1->value;
|
||||
gs[2] = N2->value;
|
||||
mpi_gs[0] = startn0;
|
||||
mpi_gs[1] = startn0 + localn0;
|
||||
mpi_gs[2] = 0;
|
||||
mpi_gs[3] = N1->value;
|
||||
mpi_gs[4] = 0;
|
||||
mpi_gs[5] = N2->value;
|
||||
gl[0] = corner0->value;
|
||||
gl[1] = corner0->value + L0->value;
|
||||
gl[2] = corner1->value;
|
||||
gl[3] = corner1->value + L1->value;
|
||||
gl[4] = corner2->value;
|
||||
gl[5] = corner2->value + L2->value;
|
||||
|
||||
info[L::GRID] = gs;
|
||||
info[L::GRID_LENGTH] = gl;
|
||||
info[L::MPI_GRID] = mpi_gs;
|
||||
info[L::DATA_GRID] = gsd;
|
||||
info["EFT_Lambda"] = 0.07;
|
||||
|
||||
std::shared_ptr<boost::multi_array_ref<long, 3>> cmap =
|
||||
std::make_shared<boost::multi_array<long, 3>>(boost::extents[range(
|
||||
startn0, startn0 + localn0)][N1->value][N2->value]);
|
||||
|
||||
array::fill(*cmap, 0);
|
||||
|
||||
for (int i = startn0; i < startn0 + localn0; i++) {
|
||||
for (int j = 0; j < N1->value; j++) {
|
||||
for (int k = 0; k < N2->value; k++) {
|
||||
long idx = (i + j * N0->value + k * N0->value * N1->value) % 8;
|
||||
|
||||
(*cmap)[i][j][k] = idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
auto p_cmap = make_promise_pointer(cmap);
|
||||
info[L::COLOR_MAP] = p_cmap;
|
||||
|
||||
p_cmap.defer.submit_ready();
|
||||
}
|
||||
|
||||
#ifdef DATA_SETUP
|
||||
DATA_SETUP(state);
|
||||
#endif
|
||||
|
||||
DummyPowerSpectrum dummy_p(comm);
|
||||
HMCDensitySampler::Likelihood_t likelihood = makeLikelihood(info);
|
||||
HMCDensitySampler hmc(comm, likelihood);
|
||||
|
||||
dummy_p.init_markov(state);
|
||||
|
||||
auto model = makeModel(comm, state, box, box2);
|
||||
auto chain = std::make_shared<ChainForwardModel>(comm, box);
|
||||
auto fixer = std::make_shared<ForwardHermiticOperation>(comm, box);
|
||||
|
||||
BorgModelElement *model_element = new BorgModelElement();
|
||||
|
||||
chain->addModel(fixer);
|
||||
chain->addModel(model);
|
||||
|
||||
{
|
||||
ArrayType1d::ArrayType vobs(boost::extents[3]);
|
||||
vobs[0] = 1000.;
|
||||
vobs[1] = -300;
|
||||
vobs[2] = 200.;
|
||||
model->setObserver(vobs);
|
||||
}
|
||||
model_element->obj = std::shared_ptr<BORGForwardModel>(chain);
|
||||
state.newElement("BORG_model", model_element);
|
||||
|
||||
createCosmologicalPowerSpectrum(state, cparams);
|
||||
|
||||
hmc.init_markov(state);
|
||||
|
||||
likelihood->setupDefaultParameters(state, 0);
|
||||
|
||||
// Build some mock field
|
||||
|
||||
sel_data->eigen().fill(1);
|
||||
sel_data2->eigen().fill(1);
|
||||
|
||||
#if 0
|
||||
//Build spherical mask
|
||||
for (int n0 = 0; n0 < M; n0++) {
|
||||
for (int n1 = 0; n1 < M; n1++) {
|
||||
for (int n2 = 0; n2 < M; n2++) {
|
||||
|
||||
double r= sqrt((n0-M/2)*(n0-M/2) + (n1-M/2)*(n1-M/2) +(n2-M/2)*(n2-M/2));
|
||||
|
||||
if(r>M/4) (*sel_data->array)[n0][n1][n2] = 0.;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if 0
|
||||
for (int n0 = startn0; n0 < startn0 + localn0; n0++) {
|
||||
double in0 = (n0 - M / 2) * 1.0 / M;
|
||||
for (int n1 = 0; n1 < M; n1++) {
|
||||
double in1 = (n1 - M / 2) * 1.0 / M;
|
||||
for (int n2 = 0; n2 < M; n2++) {
|
||||
double in2 = (n2 - M / 2) * 1.0 / M;
|
||||
double S =
|
||||
exp(-0.5 * (in0 * in0 + in1 * in1 + in2 * in2) / (0.2 * 0.2));
|
||||
double r = sqrt(
|
||||
(n0 - M / 2) * (n0 - M / 2) + (n1 - M / 2) * (n1 - M / 2) +
|
||||
(n2 - M / 2) * (n2 - M / 2));
|
||||
|
||||
(*sel_data->array)[n0][n1][n2] = S;
|
||||
if (r > M / 4)
|
||||
(*sel_data->array)[n0][n1][n2] = 0.;
|
||||
(*sel_data2->array)[n0][n1][n2] = (*sel_data->array)[n0][n1][n2];
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
// Build some s field
|
||||
|
||||
long N2real;
|
||||
|
||||
#ifdef ARES_MPI_FFTW
|
||||
N2real = 2 * N2_HC->value;
|
||||
#else
|
||||
N2real = N2->value;
|
||||
#endif
|
||||
|
||||
hmc.generateMockData(state);
|
||||
cons.setVerboseLevel<LOG_INFO>();
|
||||
hmc.checkGradient(state, STEP_GRADIENT);
|
||||
//hmc.checkGradientReal(state, STEP_GRADIENT);
|
||||
|
||||
{
|
||||
std::shared_ptr<H5::H5File> f;
|
||||
|
||||
if (comm->rank() == 0)
|
||||
f = std::make_shared<H5::H5File>("dump.h5", H5F_ACC_TRUNC);
|
||||
|
||||
state.mpiSaveState(f, comm, false);
|
||||
}
|
||||
}
|
||||
StaticInit::finalize();
|
||||
doneMPI();
|
||||
|
||||
return 0;
|
||||
}
|
97
extra/hades/libLSS/tests/generic_mock.hpp
Normal file
97
extra/hades/libLSS/tests/generic_mock.hpp
Normal file
|
@ -0,0 +1,97 @@
|
|||
/*+
|
||||
ARES/HADES/BORG Package -- ./extra/hades/libLSS/tests/generic_mock.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 __LIBLSS_TEST_GENERIC_MOCK_HPP
|
||||
#define __LIBLSS_TEST_GENERIC_MOCK_HPP
|
||||
|
||||
#include "libLSS/mpi/generic_mpi.hpp"
|
||||
#include "libLSS/tools/static_init.hpp"
|
||||
#include "libLSS/physics/likelihoods/voxel_poisson.hpp"
|
||||
#include "libLSS/tools/fusewrapper.hpp"
|
||||
#include "libLSS/samplers/core/random_number.hpp"
|
||||
#include "libLSS/samplers/core/types_samplers.hpp"
|
||||
#include "libLSS/samplers/rgen/hmc/hmc_density_sampler.hpp"
|
||||
#include <CosmoTool/hdf5_array.hpp>
|
||||
|
||||
template <typename Likelihood>
|
||||
void generate_mock_data(
|
||||
LibLSS::MPI_Communication *comm, LibLSS::MarkovState &state, size_t const N,
|
||||
double const L) {
|
||||
using namespace LibLSS;
|
||||
typedef typename Likelihood::bias_t bias_t;
|
||||
double const nmean = state.getScalar<double>("galaxy_nmean_0");
|
||||
auto const &bias_params = *state.get<ArrayType1d>("galaxy_bias_0")->array;
|
||||
auto model = state.get<BorgModelElement>("BORG_model")->obj;
|
||||
|
||||
// Make three sinus
|
||||
double const epsilon = 0.1;
|
||||
|
||||
typedef FFTW_Manager_3d<double> FFT_Manager;
|
||||
FFT_Manager mgr(N, N, N, comm);
|
||||
FFT_Manager::ArrayReal ic_field(
|
||||
mgr.extents_real(), boost::c_storage_order(), mgr.allocator_real);
|
||||
FFT_Manager::ArrayReal final_density(
|
||||
model->out_mgr->extents_real(), boost::c_storage_order(), model->out_mgr->allocator_real);
|
||||
FFT_Manager::ArrayFourier ic_field_hat(
|
||||
mgr.extents_complex(), boost::c_storage_order(), mgr.allocator_complex);
|
||||
|
||||
auto rhom = fwrap(ic_field);
|
||||
|
||||
rhom = LibLSS::b_fused_idx<double, 3>(
|
||||
[N, epsilon](size_t const i, size_t const j, size_t const k) -> double {
|
||||
double x = double(i) / N;
|
||||
double z = double(j) / N;
|
||||
double y = double(k) / N;
|
||||
|
||||
return epsilon * sin(M_PI * x) * sin(M_PI * y) * sin(M_PI * z);
|
||||
});
|
||||
|
||||
// Compute the Fourier transform, as that is the required input for the forward
|
||||
// model.
|
||||
auto w_ic_hat = fwrap(ic_field_hat);
|
||||
FFT_Manager::plan_type aplan =
|
||||
mgr.create_r2c_plan(ic_field.data(), ic_field_hat.data());
|
||||
mgr.execute_r2c(aplan, ic_field.data(), ic_field_hat.data());
|
||||
w_ic_hat = w_ic_hat * double((L * L * L) / (N * N * N));
|
||||
fwrap(*state.get<CArrayType>("s_hat_field")->array) = ic_field_hat;
|
||||
|
||||
auto vobs = state.get<ArrayType1d>("BORG_vobs")->array;
|
||||
|
||||
typedef ScalarStateElement<CosmologicalParameters> CosmoElement;
|
||||
GenericDetails::compute_forward(
|
||||
model->lo_mgr, model, state.get<CosmoElement>("cosmology")->value,
|
||||
state.getScalar<double>("borg_a_initial"), *vobs, ModelInput<3>(model->lo_mgr, model->get_box_model(), ic_field_hat),
|
||||
ModelOutput<3>(model->out_mgr, model->get_box_model_output(), final_density), false);
|
||||
|
||||
auto data = fwrap(*state.get<ArrayType>("galaxy_data_0")->array);
|
||||
|
||||
RandomNumber &rgen = state.get<RandomGen>("random_generator")->get();
|
||||
|
||||
// Make nmean=3, data
|
||||
bias_t bias_func;
|
||||
bias_func.prepare(*model, final_density, nmean, bias_params, true);
|
||||
Console::instance().print<LOG_VERBOSE>(
|
||||
"Density field ready. Now do joint selection + sample of mock data");
|
||||
data = Likelihood::likelihood_t::sample(
|
||||
rgen, bias_func.selection_adaptor.apply(
|
||||
b_fused_idx<double, 3>(
|
||||
FuseWrapper_detail::constantFunctor<double>(1),
|
||||
boost::extents[N][N][N]),
|
||||
bias_func.compute_density(final_density)));
|
||||
Console::instance().print<LOG_VERBOSE>("Done with mock. Save now.");
|
||||
bias_func.cleanup();
|
||||
|
||||
H5::H5File f("mock.h5", H5F_ACC_TRUNC);
|
||||
CosmoTool::hdf5_write_array(f, "data", *data);
|
||||
CosmoTool::hdf5_write_array(f, "ic_field", ic_field);
|
||||
CosmoTool::hdf5_write_array(f, "ic_hat_field", ic_field_hat);
|
||||
CosmoTool::hdf5_write_array(f, "final_density", final_density);
|
||||
}
|
||||
|
||||
#endif
|
74
extra/hades/libLSS/tests/hades_gradients.py_config
Normal file
74
extra/hades/libLSS/tests/hades_gradients.py_config
Normal file
|
@ -0,0 +1,74 @@
|
|||
tests = {
|
||||
'tests': {
|
||||
'linear': {
|
||||
'includes': [
|
||||
"libLSS/samplers/hades/hades_linear_likelihood.hpp",
|
||||
"libLSS/physics/hades_pt.hpp"
|
||||
],
|
||||
'likelihood':
|
||||
"LibLSS::HadesLinearDensityLikelihood",
|
||||
'model_args':
|
||||
'comm, box, box, 0.001, 1',
|
||||
'model':
|
||||
'LibLSS::HadesLinear'
|
||||
},
|
||||
'log_linear': {
|
||||
'includes': [
|
||||
"libLSS/samplers/hades/hades_linear_likelihood.hpp",
|
||||
"libLSS/physics/hades_log.hpp"
|
||||
],
|
||||
'likelihood':
|
||||
"LibLSS::HadesLinearDensityLikelihood",
|
||||
'model':
|
||||
'LibLSS::HadesLog',
|
||||
'model_args':
|
||||
'comm, box, 0.001'
|
||||
},
|
||||
'primordial_linear': {
|
||||
'includes': [
|
||||
"libLSS/samplers/hades/hades_linear_likelihood.hpp",
|
||||
"libLSS/physics/forwards/primordial.hpp"
|
||||
],
|
||||
'likelihood':
|
||||
"LibLSS::HadesLinearDensityLikelihood",
|
||||
'model':
|
||||
'LibLSS::ForwardPrimordial',
|
||||
'model_args':
|
||||
'comm, box, 0.001'
|
||||
},
|
||||
'chain_primordial_log_linear': {
|
||||
'includes': [
|
||||
"libLSS/samplers/hades/hades_linear_likelihood.hpp",
|
||||
"libLSS/physics/chain_forward_model.hpp",
|
||||
"libLSS/physics/forwards/primordial.hpp",
|
||||
"libLSS/physics/forwards/transfer_ehu.hpp",
|
||||
"libLSS/physics/hades_log.hpp"
|
||||
],
|
||||
'likelihood':
|
||||
"LibLSS::HadesLinearDensityLikelihood",
|
||||
'model': [
|
||||
'LibLSS::ForwardPrimordial', 'LibLSS::ForwardEisensteinHu',
|
||||
'LibLSS::HadesLog'
|
||||
],
|
||||
'model_args':
|
||||
['comm, box, 0.001', 'comm, box', 'comm, box, .001']
|
||||
},
|
||||
'chain_primordial_pt_linear': {
|
||||
'includes': [
|
||||
"libLSS/samplers/hades/hades_linear_likelihood.hpp",
|
||||
"libLSS/physics/chain_forward_model.hpp",
|
||||
"libLSS/physics/forwards/primordial.hpp",
|
||||
"libLSS/physics/forwards/transfer_ehu.hpp",
|
||||
"libLSS/physics/hades_pt.hpp"
|
||||
],
|
||||
'likelihood':
|
||||
"LibLSS::HadesLinearDensityLikelihood",
|
||||
'model': [
|
||||
'LibLSS::ForwardPrimordial', 'LibLSS::ForwardEisensteinHu',
|
||||
'LibLSS::HadesLinear'
|
||||
],
|
||||
'model_args':
|
||||
['comm, box, .001', 'comm, box', 'comm, box, box, .001, 1']
|
||||
}
|
||||
}
|
||||
}
|
250
extra/hades/libLSS/tests/setup_hades_test_run.cpp
Normal file
250
extra/hades/libLSS/tests/setup_hades_test_run.cpp
Normal file
|
@ -0,0 +1,250 @@
|
|||
/*+
|
||||
ARES/HADES/BORG Package -- ./extra/hades/libLSS/tests/setup_hades_test_run.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)
|
||||
|
||||
+*/
|
||||
#include "libLSS/tools/static_init.hpp"
|
||||
#include "libLSS/samplers/core/types_samplers.hpp"
|
||||
#include "libLSS/samplers/core/powerspec_tools.hpp"
|
||||
#include "libLSS/mcmc/global_state.hpp"
|
||||
#include "libLSS/samplers/rgen/gsl_random_number.hpp"
|
||||
#include "libLSS/tools/array_tools.hpp"
|
||||
#include "libLSS/physics/cosmo.hpp"
|
||||
#include "libLSS/physics/forward_model.hpp"
|
||||
#include <CosmoTool/cosmopower.hpp>
|
||||
#include <CosmoTool/algo.hpp>
|
||||
#include <boost/format.hpp>
|
||||
#include "libLSS/tools/fusewrapper.hpp"
|
||||
#include "setup_hades_test_run.hpp"
|
||||
#include "libLSS/physics/likelihoods/base.hpp"
|
||||
|
||||
using namespace LibLSS;
|
||||
using boost::format;
|
||||
using CosmoTool::square;
|
||||
using std::string;
|
||||
|
||||
typedef boost::multi_array_types::extent_range range;
|
||||
|
||||
typedef RandomNumberThreaded<GSL_RandomNumber> RGenType;
|
||||
|
||||
namespace {
|
||||
#if defined(ARES_MPI_FFTW)
|
||||
RegisterStaticInit reg0(fftw_mpi_init, fftw_mpi_cleanup, 9, "MPI/FFTW");
|
||||
#endif
|
||||
RegisterStaticInit reg1(
|
||||
CosmoTool::init_fftw_wisdom, CosmoTool::save_fftw_wisdom, 10,
|
||||
"FFTW/WISDOM");
|
||||
|
||||
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) {}
|
||||
};
|
||||
|
||||
constexpr size_t TEST_NUM_MODES = 200;
|
||||
|
||||
}; // namespace
|
||||
|
||||
static void createCosmologicalPowerSpectrum(
|
||||
MarkovState &state, CosmologicalParameters &cosmo_params,
|
||||
double adjust = 1) {
|
||||
double h;
|
||||
CosmoTool::CosmoPower cpower;
|
||||
|
||||
h = cpower.h = cosmo_params.h;
|
||||
cpower.OMEGA_B = cosmo_params.omega_b;
|
||||
cpower.OMEGA_C = cosmo_params.omega_m - cosmo_params.omega_b;
|
||||
cpower.SIGMA8 = cosmo_params.sigma8;
|
||||
cpower.setFunction(CosmoTool::CosmoPower::HU_WIGGLES);
|
||||
cpower.updateCosmology();
|
||||
cpower.normalize();
|
||||
|
||||
ArrayType1d::ArrayType &k = *state.get<ArrayType1d>("k_modes")->array;
|
||||
ArrayType1d::ArrayType &Pk = *state.get<ArrayType1d>("powerspectrum")->array;
|
||||
for (size_t i = 0; i < k.num_elements(); i++) {
|
||||
Pk[i] = cpower.power(k[i] * h) * h * h * h * adjust;
|
||||
}
|
||||
}
|
||||
|
||||
void LibLSS_test::setup_box(MarkovState &state, 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");
|
||||
}
|
||||
|
||||
void LibLSS_test::setup_likelihood_info(
|
||||
MarkovState &state, LikelihoodInfo &info, MPI_Communication *comm) {
|
||||
namespace L = LibLSS::Likelihood;
|
||||
|
||||
info[L::MPI] = comm;
|
||||
info["ManyPower_prior_width"] = 3.5;
|
||||
|
||||
L::GridSize gs(boost::extents[3]), gsd(boost::extents[3]),
|
||||
mpi_gs(boost::extents[6]);
|
||||
L::GridLengths gl(boost::extents[6]);
|
||||
|
||||
state.getScalarArray<long, 3>("N", gs);
|
||||
mpi_gs[0] = state.getScalar<long>("startN0");
|
||||
mpi_gs[1] = mpi_gs[0] + state.getScalar<long>("localN0");
|
||||
mpi_gs[2] = 0;
|
||||
mpi_gs[3] = gs[1];
|
||||
mpi_gs[4] = 0;
|
||||
mpi_gs[5] = gs[2];
|
||||
gl[0] = state.getScalar<double>("corner0");
|
||||
gl[2] = state.getScalar<double>("corner1");
|
||||
gl[4] = state.getScalar<double>("corner2");
|
||||
gl[1] = gl[0] + state.getScalar<double>("L0");
|
||||
gl[3] = gl[2] + state.getScalar<double>("L1");
|
||||
gl[5] = gl[4] + state.getScalar<double>("L2");
|
||||
|
||||
info[L::GRID] = gs;
|
||||
info[L::GRID_LENGTH] = gl;
|
||||
info[L::MPI_GRID] = mpi_gs;
|
||||
info["EFT_Lambda"] = 0.15; // Some default, 0.15 h/Mpc
|
||||
if (state.exists("Ndata0")) {
|
||||
state.getScalarArray<long, 3>("Ndata", gsd);
|
||||
info[L::DATA_GRID] = gsd;
|
||||
}
|
||||
|
||||
std::shared_ptr<boost::multi_array_ref<long, 3>> cmap =
|
||||
std::make_shared<boost::multi_array<long, 3>>(
|
||||
boost::extents[range(mpi_gs[0], mpi_gs[1])][mpi_gs[3]][mpi_gs[5]]);
|
||||
array::fill(*cmap, 0);
|
||||
|
||||
for (int i = mpi_gs[0]; i < mpi_gs[1]; i++) {
|
||||
for (int j = 0; j < mpi_gs[3]; j++) {
|
||||
for (int k = 0; k < mpi_gs[5]; k++) {
|
||||
long idx = (i + j * gs[0] + k * gs[0] * gs[1]) % 8;
|
||||
|
||||
(*cmap)[i][j][k] = idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
auto promise_cmap = make_promise_pointer(cmap);
|
||||
info[L::COLOR_MAP] = promise_cmap;
|
||||
|
||||
promise_cmap.defer.submit_ready();
|
||||
}
|
||||
|
||||
void LibLSS_test::setup_hades_test_run(
|
||||
MPI_Communication *comm, size_t Nbase, double L, MarkovState &state,
|
||||
boost::multi_array_ref<double, 1> *bias_params) {
|
||||
Console &cons = Console::instance();
|
||||
|
||||
SelArrayType *sel_data, *s_sel_data;
|
||||
ArrayType1d *bias0;
|
||||
ArrayType *data0, *growth;
|
||||
RGenType *randgen = new RGenType(-1);
|
||||
|
||||
randgen->seed(23482098);
|
||||
|
||||
cons.print<LOG_INFO>("Setting up a mock run configuration");
|
||||
|
||||
state.newElement(
|
||||
"random_generator", new RandomStateElement<RandomNumber>(randgen, true));
|
||||
|
||||
state.newScalar<long>("N0", Nbase);
|
||||
state.newScalar<long>("N1", Nbase);
|
||||
state.newScalar<long>("N2", Nbase);
|
||||
state.newScalar<long>("N2_HC", Nbase / 2 + 1);
|
||||
state.newScalar<long>("NUM_MODES", TEST_NUM_MODES);
|
||||
state.newScalar<double>("K_MIN", 0);
|
||||
state.newScalar<double>("K_MAX", 2 * M_PI / L * Nbase * 1.1 * std::sqrt(3.0));
|
||||
|
||||
state.newScalar<double>("L0", L);
|
||||
state.newScalar<double>("L1", L);
|
||||
state.newScalar<double>("L2", L);
|
||||
|
||||
state.newScalar<long>("NCAT", 1);
|
||||
|
||||
state.newScalar<double>("ares_heat", 1.0);
|
||||
|
||||
state.newScalar<double>("corner0", -L / 2);
|
||||
state.newScalar<double>("corner1", -L / 2);
|
||||
state.newScalar<double>("corner2", -L / 2);
|
||||
|
||||
state.newScalar<double>("borg_a_initial", 0.001);
|
||||
|
||||
state.newScalar<int>("borg_pm_nsteps", 30);
|
||||
state.newScalar<double>("borg_pm_start_z", 69.);
|
||||
|
||||
FFTW_Manager_3d<double> mgr(Nbase, Nbase, Nbase, comm);
|
||||
|
||||
state.newScalar<long>("startN0", mgr.startN0);
|
||||
state.newScalar<long>("localN0", mgr.localN0);
|
||||
state.newScalar<long>("fourierLocalSize", mgr.allocator_real.minAllocSize);
|
||||
|
||||
auto local_extent =
|
||||
boost::extents[range(mgr.startN0, mgr.startN0 + mgr.localN0)][Nbase]
|
||||
[Nbase];
|
||||
auto full_extent = ArrayDimension(Nbase, Nbase, Nbase);
|
||||
|
||||
state.newElement("growth_factor", growth = new ArrayType(local_extent));
|
||||
growth->eigen().fill(1);
|
||||
growth->setRealDims(full_extent);
|
||||
state.newElement("galaxy_data_0", data0 = new ArrayType(local_extent));
|
||||
data0->setRealDims(full_extent);
|
||||
state.newElement(
|
||||
"galaxy_sel_window_0", sel_data = new SelArrayType(local_extent));
|
||||
state.newElement(
|
||||
"galaxy_synthetic_sel_window_0",
|
||||
s_sel_data = new SelArrayType(local_extent));
|
||||
sel_data->setRealDims(full_extent);
|
||||
s_sel_data->setRealDims(full_extent);
|
||||
|
||||
size_t Nb = (bias_params == 0) ? 1 : bias_params->shape()[0];
|
||||
state.newElement(
|
||||
"galaxy_bias_0", bias0 = new ArrayType1d(boost::extents[Nb]));
|
||||
|
||||
if (bias_params == 0)
|
||||
(*bias0->array)[0] = 2;
|
||||
else
|
||||
fwrap(*bias0->array) = *bias_params;
|
||||
state.newScalar<double>("galaxy_nmean_0", 20);
|
||||
state.newScalar<bool>("galaxy_bias_ref_0", true);
|
||||
|
||||
DummyPowerSpectrum dummy_p(comm);
|
||||
|
||||
dummy_p.init_markov(state);
|
||||
|
||||
ScalarStateElement<CosmologicalParameters> *s_cosmo =
|
||||
new ScalarStateElement<CosmologicalParameters>();
|
||||
state.newElement("cosmology", s_cosmo);
|
||||
|
||||
CosmologicalParameters &cparams = s_cosmo->value;
|
||||
cparams.omega_r = 0.; /* negligible radiation density */
|
||||
cparams.omega_k = 0.; /* curvature - flat prior for everything! */
|
||||
cparams.omega_m = 0.3175;
|
||||
cparams.omega_b = 0.049;
|
||||
cparams.omega_q = 0.6825;
|
||||
cparams.w = -1.;
|
||||
cparams.n_s = 0.9624;
|
||||
cparams.wprime = 0.;
|
||||
cparams.sigma8 = 0.8344;
|
||||
cparams.h = 0.6711;
|
||||
cparams.beta = 1.5;
|
||||
cparams.z0 = 0.;
|
||||
cparams.a0 = 1.; /* scale factor at epoch of observation usually 1*/
|
||||
|
||||
createCosmologicalPowerSpectrum(state, cparams);
|
||||
|
||||
// Build some mock field
|
||||
|
||||
sel_data->eigen().fill(1);
|
||||
s_sel_data->eigen().fill(1);
|
||||
}
|
29
extra/hades/libLSS/tests/setup_hades_test_run.hpp
Normal file
29
extra/hades/libLSS/tests/setup_hades_test_run.hpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*+
|
||||
ARES/HADES/BORG Package -- ./extra/hades/libLSS/tests/setup_hades_test_run.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 __LIBLSS_SETUP_HADES_TEST_HPP
|
||||
#define __LIBLSS_SETUP_HADES_TEST_HPP
|
||||
|
||||
#include "libLSS/mpi/generic_mpi.hpp"
|
||||
#include "libLSS/physics/forward_model.hpp"
|
||||
#include "libLSS/mcmc/global_state.hpp"
|
||||
#include "libLSS/physics/likelihoods/base.hpp"
|
||||
|
||||
namespace LibLSS_test {
|
||||
void setup_box(LibLSS::MarkovState &state, LibLSS::BoxModel &box);
|
||||
void setup_hades_test_run(
|
||||
LibLSS::MPI_Communication *comm, size_t Nbase, double L,
|
||||
LibLSS::MarkovState &state,
|
||||
boost::multi_array_ref<double, 1> *bias_params = 0);
|
||||
|
||||
void setup_likelihood_info(
|
||||
LibLSS::MarkovState &state, LibLSS::LikelihoodInfo &info, LibLSS::MPI_Communication *comm = LibLSS::MPI_Communication::instance());
|
||||
} // namespace LibLSS_test
|
||||
|
||||
#endif
|
59
extra/hades/libLSS/tests/test_ghost_array.cpp
Normal file
59
extra/hades/libLSS/tests/test_ghost_array.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
#include <boost/format.hpp>
|
||||
#include "libLSS/mpi/generic_mpi.hpp"
|
||||
#include "libLSS/tools/mpi/ghost_array.hpp"
|
||||
#include "libLSS/tools/static_init.hpp"
|
||||
#include "libLSS/tools/console.hpp"
|
||||
#include "libLSS/tools/string_tools.hpp"
|
||||
|
||||
using namespace LibLSS;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
MPI_Communication *comm = setupMPI(argc, argv);
|
||||
|
||||
StaticInit::execute();
|
||||
|
||||
Console::instance().outputToFile(
|
||||
boost::str(boost::format("ghost_test.txt_%d") % comm->rank()));
|
||||
;
|
||||
|
||||
GhostArray<int> ghost;
|
||||
|
||||
int Ncomm = comm->size();
|
||||
|
||||
boost::multi_array<double, 1> test_array(boost::extents[2]);
|
||||
boost::multi_array<int, 1> test_idx(boost::extents[2]);
|
||||
|
||||
if (comm->size() > 1) {
|
||||
test_idx[0] = comm->rank();
|
||||
test_idx[1] = (comm->rank() + 1) % comm->size();
|
||||
} else {
|
||||
test_idx[0] = 0;
|
||||
test_idx[1] = 1;
|
||||
}
|
||||
|
||||
test_array[0] = 0.5;
|
||||
test_array[1] = 0.5;
|
||||
|
||||
ghost.setup(comm, test_idx);
|
||||
|
||||
int rank = comm->rank();
|
||||
int rank_next = (comm->rank() + 1) % comm->size();
|
||||
ghost.synchronize(test_array, [rank, rank_next](size_t i) {
|
||||
if (i == rank)
|
||||
return 0;
|
||||
else if (i == rank_next)
|
||||
return 1;
|
||||
else {
|
||||
Console::instance().print<LOG_ERROR>("Invalid index");
|
||||
MPI_Communication::instance()->abort();
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
|
||||
Console::instance().print<LOG_VERBOSE>(
|
||||
"Result: " + LibLSS::to_string(test_array));
|
||||
|
||||
doneMPI();
|
||||
|
||||
return 0;
|
||||
}
|
99
extra/hades/libLSS/tests/test_hermiticity.cpp
Normal file
99
extra/hades/libLSS/tests/test_hermiticity.cpp
Normal file
|
@ -0,0 +1,99 @@
|
|||
/*+
|
||||
ARES/HADES/BORG Package -- ./extra/hades/libLSS/tests/test_hermiticity.cpp
|
||||
Copyright (C) 2019-2020 Guilhem Lavaux <guilhem.lavaux@iap.fr>
|
||||
|
||||
Additional contributions from:
|
||||
Guilhem Lavaux <guilhem.lavaux@iap.fr> (2023)
|
||||
|
||||
+*/
|
||||
#define BOOST_TEST_MODULE modelio
|
||||
#define BOOST_TEST_NO_MAIN
|
||||
#define BOOST_TEST_ALTERNATIVE_INIT_API
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#include <boost/test/data/test_case.hpp>
|
||||
#include <H5Cpp.h>
|
||||
#include <boost/multi_array.hpp>
|
||||
#include "libLSS/tools/static_init.hpp"
|
||||
#include "libLSS/tests/testFramework.hpp"
|
||||
#include "libLSS/tools/hdf5_error.hpp"
|
||||
#include "libLSS/tools/hermiticity_fixup.hpp"
|
||||
#include "libLSS/tools/fusewrapper.hpp"
|
||||
#include <memory>
|
||||
|
||||
using namespace LibLSS;
|
||||
using boost::extents;
|
||||
using namespace CosmoTool;
|
||||
|
||||
namespace utf = boost::unit_test;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(hermitic_forward) {
|
||||
int const N = 32;
|
||||
auto comm = MPI_Communication::instance();
|
||||
auto mgr = std::make_shared<FFTW_Manager<double, 3>>(N, N, N, comm);
|
||||
typedef boost::multi_array<std::complex<double>, 3> CArray;
|
||||
CArray input(mgr->extents_complex());
|
||||
CArray input_ref(mgr->extents_complex());
|
||||
CArray rebuilt(boost::extents[N][N][N / 2 + 1]);
|
||||
|
||||
LibLSS_tests::loadReferenceInput(N, rebuilt);
|
||||
fwrap(input) = 0;
|
||||
fwrap(input[mgr->complex_range()]) = fwrap(rebuilt[mgr->complex_range()]);
|
||||
fwrap(rebuilt) = 0;
|
||||
|
||||
Hermiticity_fixer<double, 3> fixer(mgr);
|
||||
size_t numLines = mgr->localN0;
|
||||
|
||||
fixer.forward(input);
|
||||
|
||||
|
||||
if (comm->rank() == 0) {
|
||||
long numPlanes, q = 0;
|
||||
|
||||
fwrap(rebuilt[mgr->complex_range()]) = fwrap(input[mgr->complex_range()]);
|
||||
q+= mgr->localN0;
|
||||
for (int r = 1; r < comm->size(); r++) {
|
||||
comm->recv(&numPlanes, 1, translateMPIType<long>(), r, r);
|
||||
comm->recv(
|
||||
&rebuilt[q][0][0], numPlanes * mgr->N1 * mgr->N2_HC,
|
||||
translateMPIType<std::complex<double>>(), r, r);
|
||||
q += numPlanes;
|
||||
}
|
||||
} else {
|
||||
long numPlanes = mgr->localN0;
|
||||
comm->send(&numPlanes, 1, translateMPIType<long>(), 0, comm->rank());
|
||||
comm->send(
|
||||
&input[mgr->startN0][0][0], numPlanes * mgr->N1 * mgr->N2_HC,
|
||||
translateMPIType<std::complex<double>>(), 0, comm->rank());
|
||||
H5::H5File f("dump_rank.h5_"+to_string(comm->rank()), H5F_ACC_TRUNC);
|
||||
CosmoTool::hdf5_write_array(f, "input", input);
|
||||
}
|
||||
if (comm->rank() == 0) {
|
||||
CArray input_ref_full(boost::extents[N][N][N / 2 + 1]);
|
||||
LibLSS_tests::loadReferenceInput(N, input_ref_full);
|
||||
double norm = std::abs(fwrap(input_ref_full)).sum();
|
||||
double rel_difference =
|
||||
(std::abs(fwrap(rebuilt) - fwrap(input_ref_full)).sum()) / norm;
|
||||
BOOST_CHECK_LT(rel_difference, 1e-6);
|
||||
|
||||
H5::H5File f("dump.h5", H5F_ACC_TRUNC);
|
||||
CosmoTool::hdf5_write_array(f, "rebuilt", rebuilt);
|
||||
CosmoTool::hdf5_write_array(f, "input_ref", input_ref_full);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
setupMPI(argc, argv);
|
||||
LibLSS::QUIET_CONSOLE_START = true;
|
||||
StaticInit::execute();
|
||||
LibLSS::Console::instance().setVerboseLevel<LOG_DEBUG>();
|
||||
|
||||
int ret = utf::unit_test_main(&init_unit_test, argc, argv);
|
||||
|
||||
StaticInit::finalize();
|
||||
doneMPI();
|
||||
return 0;
|
||||
}
|
||||
// ARES TAG: authors_num = 1
|
||||
// ARES TAG: name(0) = Guilhem Lavaux
|
||||
// ARES TAG: year(0) = 2019-2020
|
||||
// ARES TAG: email(0) = guilhem.lavaux@iap.fr
|
348
extra/hades/libLSS/tests/test_modelio.cpp
Normal file
348
extra/hades/libLSS/tests/test_modelio.cpp
Normal file
|
@ -0,0 +1,348 @@
|
|||
/*+
|
||||
ARES/HADES/BORG Package -- ./extra/hades/libLSS/tests/test_modelio.cpp
|
||||
Copyright (C) 2019-2020 Guilhem Lavaux <guilhem.lavaux@iap.fr>
|
||||
|
||||
Additional contributions from:
|
||||
Guilhem Lavaux <guilhem.lavaux@iap.fr> (2023)
|
||||
|
||||
+*/
|
||||
#define BOOST_TEST_MODULE modelio
|
||||
#define BOOST_TEST_NO_MAIN
|
||||
#define BOOST_TEST_ALTERNATIVE_INIT_API
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#include <boost/test/data/test_case.hpp>
|
||||
#include <H5Cpp.h>
|
||||
#include <boost/multi_array.hpp>
|
||||
#include "libLSS/tools/static_init.hpp"
|
||||
#include "libLSS/physics/forward_model.hpp"
|
||||
#include "libLSS/physics/model_io.hpp"
|
||||
#include "libLSS/physics/hades_pt.hpp"
|
||||
#include "libLSS/physics/hades_log.hpp"
|
||||
#include "libLSS/tests/testFramework.hpp"
|
||||
#include "libLSS/physics/hades_log.hpp"
|
||||
#include "libLSS/tools/hdf5_error.hpp"
|
||||
#include "libLSS/physics/chain_forward_model.hpp"
|
||||
#include "libLSS/physics/forwards/primordial.hpp"
|
||||
#include "libLSS/samplers/core/powerspec_tools.hpp"
|
||||
|
||||
using namespace LibLSS;
|
||||
using boost::extents;
|
||||
using namespace CosmoTool;
|
||||
|
||||
namespace utf = boost::unit_test;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_modelio_input) {
|
||||
BoxModel box{0, 0, 0, 100, 100, 100, 32, 32, 32};
|
||||
|
||||
BOOST_CHECK_EQUAL(box.N0, 32);
|
||||
BOOST_CHECK_EQUAL(box.N1, 32);
|
||||
BOOST_CHECK_EQUAL(box.N2, 32);
|
||||
|
||||
BOOST_TEST_CHECKPOINT("initialize manager");
|
||||
auto mgr = std::make_shared<FFTW_Manager<double, 3>>(
|
||||
box.N0, box.N1, box.N2, MPI_Communication::instance());
|
||||
|
||||
BOOST_TEST_CHECKPOINT("allocate arrays");
|
||||
auto input_array_p = mgr->allocate_array();
|
||||
auto &input_array = input_array_p.get_array();
|
||||
|
||||
BOOST_TEST_CHECKPOINT("fill up value");
|
||||
fwrap(input_array) = 1;
|
||||
|
||||
{
|
||||
BOOST_TEST_CHECKPOINT("init modelio");
|
||||
ModelInput<3> io1(mgr, box, input_array);
|
||||
|
||||
BOOST_TEST_CHECKPOINT("request output format");
|
||||
io1.setRequestedIO(PREFERRED_REAL);
|
||||
|
||||
auto const &d = io1.getReal();
|
||||
BOOST_CHECK_CLOSE(d[0][0][0], 1, 0.1);
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST_CHECKPOINT("init modelio");
|
||||
ModelInput<3> io1(mgr, box, input_array);
|
||||
|
||||
BOOST_TEST_CHECKPOINT("request output format");
|
||||
io1.setRequestedIO(PREFERRED_FOURIER);
|
||||
|
||||
BOOST_TEST_CHECKPOINT("obtain output");
|
||||
auto const &dhat = io1.getFourierConst();
|
||||
double dVol = std::pow(box.L0, 3);
|
||||
BOOST_CHECK_CLOSE(dhat[0][0][0].real(), dVol, 0.1);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_modelio_output) {
|
||||
BoxModel box{0, 0, 0, 100, 100, 100, 32, 32, 32};
|
||||
|
||||
BOOST_CHECK_EQUAL(box.N0, 32);
|
||||
BOOST_CHECK_EQUAL(box.N1, 32);
|
||||
BOOST_CHECK_EQUAL(box.N2, 32);
|
||||
|
||||
BOOST_TEST_CHECKPOINT("initialize manager");
|
||||
auto mgr = std::make_shared<FFTW_Manager<double, 3>>(
|
||||
box.N0, box.N1, box.N2, MPI_Communication::instance());
|
||||
|
||||
BOOST_TEST_CHECKPOINT("allocate arrays");
|
||||
auto output_array_p = mgr->allocate_array();
|
||||
auto &output_array = output_array_p.get_array();
|
||||
|
||||
{
|
||||
BOOST_TEST_CHECKPOINT("init modelio");
|
||||
ModelOutput<3> io1(mgr, box, output_array);
|
||||
|
||||
BOOST_TEST_CHECKPOINT("request output format");
|
||||
io1.setRequestedIO(PREFERRED_REAL);
|
||||
|
||||
auto &d = io1.getRealOutput();
|
||||
fwrap(d) = 1;
|
||||
}
|
||||
BOOST_CHECK_CLOSE(output_array[0][0][0], 1, 0.1);
|
||||
|
||||
{
|
||||
BOOST_TEST_CHECKPOINT("init modelio");
|
||||
ModelOutput<3> io1(mgr, box, output_array);
|
||||
|
||||
BOOST_TEST_CHECKPOINT("request output format");
|
||||
io1.setRequestedIO(PREFERRED_FOURIER);
|
||||
|
||||
BOOST_TEST_CHECKPOINT("obtain fourier output");
|
||||
auto &dhat = io1.getFourierOutput();
|
||||
fwrap(dhat) = 0;
|
||||
dhat[0][0][0] = std::complex<double>(1.0, 0.0);
|
||||
}
|
||||
|
||||
BOOST_CHECK_CLOSE(output_array[0][0][0], 1.0 / (100. * 100. * 100.), 0.1);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_move) {
|
||||
BoxModel box{0, 0, 0, 100, 100, 100, 32, 32, 32};
|
||||
auto mgr = std::make_shared<FFTW_Manager<double, 3>>(
|
||||
box.N0, box.N1, box.N2, MPI_Communication::instance());
|
||||
|
||||
BOOST_TEST_CHECKPOINT("allocate arrays");
|
||||
auto input_array_p = mgr->allocate_array();
|
||||
auto &input_array = input_array_p.get_array();
|
||||
|
||||
BOOST_TEST_CHECKPOINT("fill up value");
|
||||
fwrap(input_array) = 1;
|
||||
|
||||
{
|
||||
BOOST_TEST_CHECKPOINT("init modelio");
|
||||
ModelInput<3> io1(mgr, box, input_array);
|
||||
ModelInput<3> io2;
|
||||
|
||||
io2 = std::move(io1);
|
||||
|
||||
BOOST_TEST_CHECKPOINT("request output format");
|
||||
io2.setRequestedIO(PREFERRED_REAL);
|
||||
|
||||
auto const &d = io2.getReal();
|
||||
BOOST_CHECK_CLOSE(d[0][0][0], 1, 0.1);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_modelio_pt) {
|
||||
BoxModel box{0, 0, 0, 100, 100, 100, 32, 32, 32};
|
||||
CosmologicalParameters cparams;
|
||||
|
||||
cparams.a0 = 1.0;
|
||||
cparams.fnl = 0;
|
||||
cparams.h = 0.67;
|
||||
cparams.omega_b = 0.05;
|
||||
cparams.omega_k = 0.0;
|
||||
cparams.omega_m = 0.3;
|
||||
cparams.omega_q = 0.7;
|
||||
cparams.omega_r = 0.0;
|
||||
cparams.w = -1;
|
||||
cparams.wprime = 0;
|
||||
|
||||
Cosmology cosmo(cparams);
|
||||
|
||||
auto mgr = std::make_shared<FFTW_Manager<double, 3>>(
|
||||
box.N0, box.N1, box.N2, MPI_Communication::instance());
|
||||
|
||||
HadesLinear fwd(MPI_Communication::instance(), box, box, 0.1, 1.0);
|
||||
auto input_delta_p = mgr->allocate_array();
|
||||
auto input_delta_c_p = mgr->allocate_complex_array();
|
||||
auto output_delta_p = mgr->allocate_array();
|
||||
auto ag_input_p = mgr->allocate_array();
|
||||
auto ag_output_c_p = mgr->allocate_complex_array();
|
||||
auto ag_output_p = mgr->allocate_array();
|
||||
auto output_delta_c_p = mgr->allocate_complex_array();
|
||||
|
||||
auto &input_delta = input_delta_p.get_array();
|
||||
auto &input_delta_c = input_delta_c_p.get_array();
|
||||
auto &output_delta = output_delta_p.get_array();
|
||||
auto &output_delta_c = output_delta_c_p.get_array();
|
||||
auto &ag_input = ag_input_p.get_array();
|
||||
auto &ag_output_c = ag_output_c_p.get_array();
|
||||
auto &ag_output = ag_output_p.get_array();
|
||||
auto &ag_input_c = ag_output_c_p.get_array();
|
||||
|
||||
array::fill(ag_input, 1.0);
|
||||
|
||||
fwd.setCosmoParams(cparams);
|
||||
|
||||
{
|
||||
LibLSS_tests::loadReferenceInput(box.N0, input_delta);
|
||||
double ref = input_delta[0][0][0];
|
||||
|
||||
fwrap(input_delta) =
|
||||
fwrap(input_delta) * cosmo.d_plus(0.1) / cosmo.d_plus(1.0);
|
||||
|
||||
fwd.forwardModel_v2(ModelInput<3>(mgr, box, input_delta));
|
||||
fwd.getDensityFinal(ModelOutput<3>(mgr, box, output_delta));
|
||||
|
||||
BOOST_CHECK_CLOSE(output_delta[0][0][0], ref, 0.1);
|
||||
|
||||
fwd.adjointModel_v2(ModelInputAdjoint<3>(mgr, box, ag_input));
|
||||
fwd.getAdjointModelOutput(ModelOutputAdjoint<3>(mgr, box, ag_input));
|
||||
|
||||
double scale = cosmo.d_plus(0.1) / cosmo.d_plus(1.0);
|
||||
BOOST_CHECK_CLOSE(ag_input[0][0][0], 1.0 / scale, 0.1);
|
||||
}
|
||||
|
||||
{
|
||||
LibLSS_tests::loadReferenceInput(box.N0, input_delta);
|
||||
LibLSS_tests::loadReferenceInput(box.N0, input_delta_c);
|
||||
|
||||
fwrap(input_delta_c) = fwrap(input_delta_c) * ((box.L0 * box.L1 * box.L2) /
|
||||
(box.N0 * box.N1 * box.N2));
|
||||
|
||||
fwrap(input_delta) =
|
||||
fwrap(input_delta) * cosmo.d_plus(0.1) / cosmo.d_plus(1.0);
|
||||
fwd.forwardModel_v2(ModelInput<3>(mgr, box, input_delta));
|
||||
fwd.getDensityFinal(ModelOutput<3>(mgr, box, output_delta_c));
|
||||
|
||||
BOOST_CHECK_CLOSE(
|
||||
output_delta_c[0][0][0].real(), input_delta_c[0][0][0].real(), 0.1);
|
||||
BOOST_CHECK_CLOSE(
|
||||
output_delta_c[0][0][1].real(), input_delta_c[0][0][1].real(), 0.1);
|
||||
|
||||
array::fill(ag_input_c, 1.0);
|
||||
/* fwd.adjointModel_v2(ModelInputAdjoint<3>(mgr, box, ag_input_c));
|
||||
fwd.getAdjointModelOutput(ModelOutputAdjoint<3>(mgr, box, ag_output));
|
||||
auto scale = std::real(fwrap(ag_input_c)).sum() * 1.0 /
|
||||
(box.L0 * box.L1 * box.L2) * cosmo.d_plus(1.0) /
|
||||
cosmo.d_plus(0.1);
|
||||
BOOST_CHECK_CLOSE(ag_output[0][0][0], scale, 0.1);*/
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_chain_model) {
|
||||
BoxModel box{0, 0, 0, 100, 100, 100, 32, 32, 32};
|
||||
CosmologicalParameters cparams;
|
||||
|
||||
cparams.a0 = 1.0;
|
||||
cparams.fnl = 0;
|
||||
cparams.h = 0.67;
|
||||
cparams.omega_b = 0.05;
|
||||
cparams.omega_k = 0.0;
|
||||
cparams.omega_m = 0.3;
|
||||
cparams.omega_q = 0.7;
|
||||
cparams.omega_r = 0.0;
|
||||
cparams.w = -1;
|
||||
cparams.wprime = 0;
|
||||
|
||||
Cosmology cosmo(cparams);
|
||||
|
||||
auto mgr = std::make_shared<FFTW_Manager<double, 3>>(
|
||||
box.N0, box.N1, box.N2, MPI_Communication::instance());
|
||||
auto comm = MPI_Communication::instance();
|
||||
|
||||
auto fwd = std::make_shared<HadesLinear>(comm, box, box, 0.1, 1.0);
|
||||
auto fwd_log = std::make_shared<HadesLog>(comm, box, 1.0);
|
||||
|
||||
{
|
||||
ChainForwardModel chain(comm, box);
|
||||
|
||||
chain.addModel(fwd);
|
||||
chain.addModel(fwd_log);
|
||||
|
||||
auto input_delta_p = mgr->allocate_array();
|
||||
auto output_delta_p = mgr->allocate_array();
|
||||
auto output_delta_c_p = mgr->allocate_complex_array();
|
||||
|
||||
auto &input_delta = input_delta_p.get_array();
|
||||
auto &output_delta = output_delta_p.get_array();
|
||||
auto &output_delta_c = output_delta_c_p.get_array();
|
||||
|
||||
{
|
||||
LibLSS_tests::loadReferenceInput(box.N0, input_delta);
|
||||
double ref = fwrap(input_delta).sum() * (box.L0 * box.L1 * box.L2) /
|
||||
(box.N0 * box.N1 * box.N2);
|
||||
|
||||
fwrap(input_delta) =
|
||||
fwrap(input_delta) * cosmo.d_plus(0.1) / cosmo.d_plus(1.0);
|
||||
chain.forwardModel_v2(ModelInput<3>(mgr, box, input_delta));
|
||||
chain.getDensityFinal(ModelOutput<3>(mgr, box, output_delta_c));
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
ChainForwardModel chain(comm, box);
|
||||
size_t const Nmodes = 1000;
|
||||
auto primordial = std::make_shared<ForwardPrimordial>(comm, box, 1.0);
|
||||
|
||||
CosmologicalParameters cosmo_pars;
|
||||
|
||||
cosmo_pars.omega_b = 0.049;
|
||||
cosmo_pars.omega_k = 0;
|
||||
cosmo_pars.omega_m = 0.315;
|
||||
cosmo_pars.omega_q = 0.785;
|
||||
cosmo_pars.omega_r = 0;
|
||||
cosmo_pars.w = -1;
|
||||
cosmo_pars.wprime = 0;
|
||||
cosmo_pars.z0 = 0;
|
||||
cosmo_pars.sigma8 = 0.8;
|
||||
cosmo_pars.h = 0.68;
|
||||
|
||||
primordial->setCosmoParams(cosmo_pars);
|
||||
|
||||
chain.addModel(primordial);
|
||||
chain.addModel(fwd_log);
|
||||
|
||||
auto input_delta_p = mgr->allocate_array();
|
||||
auto output_delta_p = mgr->allocate_array();
|
||||
auto output_delta_c_p = mgr->allocate_complex_array();
|
||||
|
||||
auto &input_delta = input_delta_p.get_array();
|
||||
auto &output_delta = output_delta_p.get_array();
|
||||
auto &output_delta_c = output_delta_c_p.get_array();
|
||||
|
||||
{
|
||||
LibLSS_tests::loadReferenceInput(box.N0, input_delta);
|
||||
double ref = fwrap(input_delta).sum() * (box.L0 * box.L1 * box.L2) /
|
||||
(box.N0 * box.N1 * box.N2);
|
||||
|
||||
fwrap(input_delta) =
|
||||
fwrap(input_delta) * (1.0 / std::sqrt(box.N0 * box.N1 * box.N2));
|
||||
chain.forwardModel_v2(ModelInput<3>(mgr, box, input_delta));
|
||||
chain.getDensityFinal(ModelOutput<3>(mgr, box, output_delta));
|
||||
|
||||
BOOST_CHECK_CLOSE(output_delta[0][0][0], -1, 0.1);
|
||||
BOOST_CHECK_CLOSE(output_delta[0][4][0], -1, 0.1);
|
||||
BOOST_CHECK_CLOSE(output_delta[15][5][22], -0.99879686176352611, 0.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
setupMPI(argc, argv);
|
||||
LibLSS::QUIET_CONSOLE_START = true;
|
||||
StaticInit::execute();
|
||||
LibLSS::Console::instance().setVerboseLevel<LOG_STD>();
|
||||
|
||||
int ret = utf::unit_test_main(&init_unit_test, argc, argv);
|
||||
|
||||
StaticInit::finalize();
|
||||
doneMPI();
|
||||
return 0;
|
||||
}
|
||||
// ARES TAG: authors_num = 1
|
||||
// ARES TAG: name(0) = Guilhem Lavaux
|
||||
// ARES TAG: year(0) = 2019-2020
|
||||
// ARES TAG: email(0) = guilhem.lavaux@iap.fr
|
78
extra/hades/libLSS/tests/test_symplectic.cpp
Normal file
78
extra/hades/libLSS/tests/test_symplectic.cpp
Normal file
|
@ -0,0 +1,78 @@
|
|||
/*+
|
||||
ARES/HADES/BORG Package -- ./extra/hades/libLSS/tests/test_symplectic.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)
|
||||
|
||||
+*/
|
||||
#include <H5Cpp.h>
|
||||
#include <boost/multi_array.hpp>
|
||||
#include "libLSS/tools/static_init.hpp"
|
||||
#include "libLSS/tools/symplectic_integrator.hpp"
|
||||
#include <CosmoTool/algo.hpp>
|
||||
#include <CosmoTool/hdf5_array.hpp>
|
||||
#include <boost/preprocessor/cat.hpp>
|
||||
#include <boost/preprocessor/stringize.hpp>
|
||||
#include <boost/preprocessor/seq/for_each.hpp>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace LibLSS;
|
||||
using boost::extents;
|
||||
using namespace CosmoTool;
|
||||
|
||||
void force_func(
|
||||
boost::multi_array<double, 1> &x, boost::multi_array<double, 1> &grad) {
|
||||
grad[0] = x[0];
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
using namespace SymplecticOption;
|
||||
setupMPI(argc, argv);
|
||||
|
||||
LibLSS::Console &console = LibLSS::Console::instance();
|
||||
LibLSS::StaticInit::execute();
|
||||
SymplecticIntegrators F;
|
||||
boost::multi_array<double, 1> mass(extents[1]), position(extents[1]),
|
||||
momentum(extents[1]), gradient(extents[1]);
|
||||
double epsilon = 0.1;
|
||||
double Einit;
|
||||
int Ntime = 2 * M_PI / epsilon;
|
||||
boost::multi_array<double, 1> E(boost::extents[Ntime]),
|
||||
p(boost::extents[Ntime]), m(boost::extents[Ntime]);
|
||||
|
||||
#define BUILD_SCHEME(r, data, elem) \
|
||||
std::make_pair(elem, BOOST_PP_STRINGIZE(elem)),
|
||||
|
||||
std::pair<IntegratorScheme, std::string> schemes[] = {BOOST_PP_SEQ_FOR_EACH(
|
||||
BUILD_SCHEME, _,
|
||||
(SI_2A)(SI_2B)(SI_2C)(SI_3A)(SI_4B)(SI_4C)(SI_4D)(SI_6A))};
|
||||
int numSchemes = sizeof(schemes) / sizeof(schemes[0]);
|
||||
H5::H5File f("symplectic.h5", H5F_ACC_TRUNC);
|
||||
|
||||
for (int s = 0; s < numSchemes; s++) {
|
||||
|
||||
F.setIntegratorScheme(schemes[s].first);
|
||||
mass[0] = 1;
|
||||
position[0] = 0;
|
||||
momentum[0] = 1;
|
||||
|
||||
Einit = 0.5 * square(position[0]) + 0.5 * square(momentum[0]) / mass[0];
|
||||
for (int i = 0; i < Ntime; i++) {
|
||||
F.integrate(force_func, mass, epsilon, 1, position, momentum, gradient);
|
||||
p[i] = position[0];
|
||||
m[i] = momentum[0] / mass[0];
|
||||
E[i] = 0.5 * square(position[0]) + 0.5 * square(momentum[0]) / mass[0] -
|
||||
Einit;
|
||||
}
|
||||
|
||||
H5::Group g = f.createGroup(schemes[s].second);
|
||||
hdf5_write_array(g, "energy", E);
|
||||
hdf5_write_array(g, "position", p);
|
||||
hdf5_write_array(g, "velocity", m);
|
||||
}
|
||||
LibLSS::StaticInit::finalize();
|
||||
|
||||
return 0;
|
||||
}
|
13
extra/hades/libLSS/tests/tests.cmake
Normal file
13
extra/hades/libLSS/tests/tests.cmake
Normal file
|
@ -0,0 +1,13 @@
|
|||
SET(EXTRA_HADES ${CMAKE_SOURCE_DIR}/extra/hades/libLSS/tests)
|
||||
|
||||
set(TEST_LIBRARY_SOURCES ${EXTRA_HADES}/setup_hades_test_run.cpp)
|
||||
|
||||
include_directories(${EXTRA_HADES})
|
||||
|
||||
SET(TEST_hades_LIST symplectic modelio hermiticity ghost_array )
|
||||
|
||||
include(${CMAKE_SOURCE_DIR}/extra/hades/scripts/gradient_tests.cmake)
|
||||
|
||||
hades_add_gradient_test(hades_gradients ${EXTRA_HADES}/hades_gradients.py_config)
|
||||
|
||||
add_test(NAME modelio COMMAND ${CURRENT_CMAKE_BINARY_DIR}/test_modelio)
|
Loading…
Add table
Add a link
Reference in a new issue