renamed src to source

This commit is contained in:
Paul M. Sutter 2024-05-22 16:15:14 -04:00
parent 4dcaf3959b
commit 4d9c5ab2c1
71 changed files with 7 additions and 3 deletions

View file

@ -0,0 +1,22 @@
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/loaders)
IF(SDF_SUPPORT)
add_definitions(-DSDF_SUPPORT)
ENDIF(SDF_SUPPORT)
SET(prepSimulation_SRCS prepSimulation.cpp )
add_genopt(prepSimulation_SRCS prepSimulation.ggo prepSimulation_conf STRUCTNAME prepSimulation_info)
add_executable(prepSimulation ${prepSimulation_SRCS})
target_link_libraries(prepSimulation simu_loaders ${ZOB_LIBS} ${LIBSDF_LIBRARY} ${HDF5_CXX_LIBRARIES} ${HDF5_HL_LIBRARIES} ${HDF5_LIBRARIES} ${DL_LIBRARY})
SET(prepObservation_SRCS prepObservation.cpp)
add_genopt(prepObservation_SRCS prepObservation.ggo prepObservation_conf STRUCTNAME prepObservation_info)
add_executable(prepObservation ${prepObservation_SRCS})
target_link_libraries(prepObservation ${ZOB_LIBS} ${HDF5_CXX_LIBRARIES} ${HDF5_HL_LIBRARIES} ${HDF5_LIBRARIES} ${HEALPIX_LIBRARIES} ${DL_LIBRARY})
set_target_properties(prepObservation PROPERTIES COMPILE_FLAGS ${OpenMP_CXX_FLAGS} LINK_FLAGS ${OpenMP_CXX_FLAGS})
subdirs(loaders)
install(TARGETS prepSimulation prepObservation DESTINATION ${VIDE_BIN})

View file

@ -0,0 +1,68 @@
/*+
VIDE -- Void IDentification and Examination -- ./c_tools/mock/generateTestMock.cpp
Copyright (C) 2010-2014 Guilhem Lavaux
Copyright (C) 2011-2014 P. M. Sutter
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#include <iostream>
#include <cstdlib>
#include <CosmoTool/fortran.hpp>
using namespace CosmoTool;
using namespace std;
#define LX 1.0
#define LY 1.0
#define LZ 1.0
#define NUMPART (16*16*16)
int main(int argc, char **argv)
{
UnformattedWrite f("particles.bin");
f.beginCheckpoint();
f.writeInt32(NUMPART);
f.endCheckpoint();
cout << "Writing X components..." << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < NUMPART; i++)
{
f.writeReal32(drand48()*LX);
}
f.endCheckpoint();
cout << "Writing Y components..." << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < NUMPART; i++)
{
f.writeReal32(drand48()*LY);
}
f.endCheckpoint();
cout << "Writing Z components..." << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < NUMPART; i++)
{
f.writeReal32(drand48()*LZ);
}
f.endCheckpoint();
return 0;
}

View file

@ -0,0 +1,11 @@
SET(SIMU_LOADERS_SRCS
simulation_loader.cpp ramses_loader.cpp
gadget_loader.cpp flash_loader.cpp)
IF (SDF_SUPPORT)
add_definitions(-DSDF_SUPPORT)
SET(SIMU_LOADERS_SRCS ${SIMU_LOADERS_SRCS} sdf_loader.cpp multidark_loader.cpp)
ENDIF (SDF_SUPPORT)
add_library(simu_loaders ${SIMU_LOADERS_SRCS})

View file

@ -0,0 +1,149 @@
/*+
VIDE -- Void IDentification and Examination -- ./c_tools/mock/loaders/basic_loader.cpp
Copyright (C) 2010-2014 Guilhem Lavaux
Copyright (C) 2011-2014 P. M. Sutter
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#include <cassert>
#include <string>
#include <iostream>
#include <boost/format.hpp>
#include <fstream>
#include <CosmoTool/yorick.hpp>
#include "simulation_loader.hpp"
using namespace std;
using namespace CosmoTool;
using boost::format;
using boost::str;
class BasicGroupLoader: public SimulationLoader
{
private:
string storage;
SimuData *header;
int flags, numFiles;
public:
BasicGroupLoader(const string& storage_path, SimuData *header, int flags, int numfiles)
{
this->header = header;
this->storage = storage_path;
this->flags = flags;
this->numFiles = numfiles;
}
SimuData *getHeader() {
return header;
}
int num_files() {
return numFiles;
}
SimuData *loadFile(int id) {
if (id < 0 || id >= numFiles)
return 0;
SimuData *simu = new SimuData;
uint32_t *dimlist, dimrank;
string fname;
simu->time = header->time;
simu->TotalNumPart = header->NumPart;
simu->Omega_M = header->Omega_M;
simu->Omega_Lambda = header->Omega_Lambda;
simu->NumPart = -1;
if (flags & NEED_POSITION)
{
loadArray(str(format("%s/x_%d.nc") % storage % id),
simu->Pos[0], dimlist, dimrank);
assert(dimrank == 1);
simu->NumPart = dimlist[0];
loadArray(str(format("%s/y_%d.nc") % storage % id),
simu->Pos[1], dimlist, dimrank);
assert(dimrank == 1);
assert(simu->NumPart == dimlist[0]);
loadArray(str(format("%s/z_%d.nc") % storage % id),
simu->Pos[2], dimlist, dimrank);
assert(dimrank == 1);
assert(simu->NumPart == dimlist[0]);
}
if (flags & NEED_VELOCITY)
{
loadArray(str(format("%s/vx_%d.nc") % storage % id),
simu->Vel[0], dimlist, dimrank);
assert(dimrank == 1);
if (simu->NumPart < 0)
simu->NumPart = dimlist[0];
assert(simu->NumPart == dimlist[0]);
loadArray(str(format("%s/vy_%d.nc") % storage % id),
simu->Vel[0], dimlist, dimrank);
assert(dimrank == 1);
assert(simu->NumPart == dimlist[0]);
loadArray(str(format("%s/vz_%d.nc") % storage % id),
simu->Vel[2], dimlist, dimrank);
assert(dimrank == 1);
assert(simu->NumPart == dimlist[0]);
}
if (flags & NEED_GADGET_ID)
{
loadArray(str(format("%s/id_%d.nc") % storage % id),
simu->Id, dimlist, dimrank);
assert(dimrank == 1);
if (simu->NumPart < 0)
simu->NumPart = dimlist[0];
assert(simu->NumPart == dimlist[0]);
}
return simu;
}
~BasicGroupLoader()
{
delete header;
}
};
SimulationLoader *basicGroupLoader(const std::string& simupath,
int flags)
{
SimuData *header;
ifstream f;
string header_path = simupath + "/header.txt";
int numFiles;
header = new SimuData;
f.open(header_path.c_str());
f >> header->time
>> header->Omega_M
>> header->Omega_Lambda
>> header->NumPart
>> numFiles;
return new BasicGroupLoader(simupath, header, flags, numFiles);
}

View file

@ -0,0 +1,134 @@
/*+
VIDE -- Void IDentification and Examination -- ./c_tools/mock/loaders/flash_loader.cpp
Copyright (C) 2010-2014 Guilhem Lavaux
Copyright (C) 2011-2014 P. M. Sutter
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#include <cassert>
#include <string>
#include <CosmoTool/loadFlash.hpp>
#include <CosmoTool/fortran.hpp>
#include "simulation_loader.hpp"
using namespace std;
using namespace CosmoTool;
class FlashLoader: public SimulationLoader
{
private:
int load_flags;
bool onefile;
int _num_files;
SimuData *gadget_header;
string snapshot_name;
SimulationPreprocessor *preproc;
public:
FlashLoader(const string& basename, SimuData *header, int flags, bool singleFile, int _num, SimulationPreprocessor *p)
: snapshot_name(basename), load_flags(flags), onefile(singleFile), _num_files(_num), gadget_header(header), preproc(p)
{
}
~FlashLoader()
{
delete gadget_header;
}
SimuData *getHeader() {
return gadget_header;
}
int num_files() {
return _num_files;
}
SimuData *loadFile(int id) {
SimuData *d;
if (onefile && id > 0)
return 0;
if (id >= _num_files)
return 0;
if (onefile)
d = loadFlashMulti(snapshot_name.c_str(), -1, load_flags);
else
d = loadFlashMulti(snapshot_name.c_str(), id, load_flags);
if (d->Id != 0)
{
long *uniqueID = new long[d->NumPart];
for (long i = 0; i < d->NumPart; i++)
{
uniqueID[i] = d->Id[i];
}
d->new_attribute("uniqueID", uniqueID, delete_adaptor<long>);
}
applyTransformations(d);
basicPreprocessing(d, preproc);
return d;
}
};
SimulationLoader *flashLoader(const std::string& snapshot, int flags, SimulationPreprocessor *p)
{
bool singleFile;
int num_files;
SimuData *d;
try
{
d = loadFlashMulti(snapshot.c_str(), -1, 0);
singleFile = true;
num_files = 1;
}
catch (const NoSuchFileException& e)
{
try
{
d = loadFlashMulti(snapshot.c_str(), 0, 0);
num_files = 0;
}
catch(const NoSuchFileException& e)
{
return 0;
}
}
assert(d != 0);
SimuData *header = d;
if (!singleFile)
{
try
{
while ((d = loadFlashMulti(snapshot.c_str(), num_files, 0)) != 0)
{
num_files++;
delete d;
}
}
catch(const NoSuchFileException& e)
{
}
}
return new FlashLoader(snapshot, header, flags, singleFile, num_files, p);
}

View file

@ -0,0 +1,151 @@
/*+
VIDE -- Void IDentification and Examination -- ./c_tools/mock/loaders/gadget_loader.cpp
Copyright (C) 2010-2014 Guilhem Lavaux
Copyright (C) 2011-2014 P. M. Sutter
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#include <vector>
#include <cassert>
#include <string>
#include <CosmoTool/loadGadget.hpp>
#include <CosmoTool/fortran.hpp>
#include "simulation_loader.hpp"
using namespace std;
using namespace CosmoTool;
class GadgetLoader: public SimulationLoader
{
private:
int load_flags;
bool onefile;
int _num_files;
double unitMpc;
int gadgetFormat;
SimuData *gadget_header;
string snapshot_name;
SimulationPreprocessor *preproc;
public:
GadgetLoader(const string& basename, SimuData *header, int flags, bool singleFile, int _num, double unit, int gadgetFormat, SimulationPreprocessor *p)
: snapshot_name(basename), load_flags(flags), onefile(singleFile), _num_files(_num), unitMpc(1/unit), gadget_header(header), gadgetFormat(gadgetFormat), preproc(p)
{
}
~GadgetLoader()
{
delete gadget_header;
}
SimuData *getHeader() {
return gadget_header;
}
int num_files() {
return _num_files;
}
SimuData *loadFile(int id) {
SimuData *d;
if (onefile && id > 0)
return 0;
if (id >= _num_files)
return 0;
if (onefile)
d = loadGadgetMulti(snapshot_name.c_str(), -1, load_flags, gadgetFormat);
else
d = loadGadgetMulti(snapshot_name.c_str(), id, load_flags, gadgetFormat);
if (d->Id != 0)
{
long *uniqueID = new long[d->NumPart];
for (long i = 0; i < d->NumPart; i++)
{
uniqueID[i] = d->Id[i];
}
d->new_attribute("uniqueID", uniqueID, delete_adaptor<long>);
}
for (int k = 0; k < 3; k++)
{
if (d->Pos[k] != 0)
{
for (long i = 0; i < d->NumPart; i++)
d->Pos[k][i] *= unitMpc;
}
}
d->BoxSize *= unitMpc;
applyTransformations(d);
basicPreprocessing(d, preproc);
return d;
}
};
SimulationLoader *gadgetLoader(const std::string& snapshot, double Mpc_unitLength, int flags, int gadgetFormat, SimulationPreprocessor *p)
{
bool singleFile = false;
int num_files;
SimuData *d;
cout << " File to load is:" << snapshot.c_str() << endl;
try
{
d = loadGadgetMulti(snapshot.c_str(), -1, 0, gadgetFormat);
singleFile = true;
num_files = 1;
}
catch (const NoSuchFileException& e)
{
try
{
d = loadGadgetMulti(snapshot.c_str(), 0, 0, gadgetFormat);
num_files = 0;
}
catch(const NoSuchFileException& e)
{
return 0;
}
}
assert(d != 0);
SimuData *header = d;
header->BoxSize /= Mpc_unitLength;
if (!singleFile)
{
try
{
while ((d = loadGadgetMulti(snapshot.c_str(), num_files, 0, gadgetFormat)) != 0)
{
num_files++;
delete d;
}
}
catch(const NoSuchFileException& e)
{
}
}
return new GadgetLoader(snapshot, header, flags, singleFile, num_files, Mpc_unitLength, gadgetFormat, p);
}

View file

@ -0,0 +1,177 @@
/*+
VIDE -- Void IDentification and Examination -- ./c_tools/mock/loaders/multidark_loader.cpp
Copyright (C) 2010-2014 Guilhem Lavaux
Copyright (C) 2011-2014 P. M. Sutter
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#include <cassert>
#include <boost/format.hpp>
#include <string>
#include <fstream>
#include <iostream>
#include <CosmoTool/loadRamses.hpp>
#include <CosmoTool/fortran.hpp>
#include "simulation_loader.hpp"
using boost::format;
using namespace std;
using namespace CosmoTool;
static const double one_kpc = 3.08567802e16; /* km */
static const double one_Gyr = 3.1558149984e16; /* sec */
class MultiDarkLoader: public SimulationLoader
{
protected:
SimuData *header;
string darkname;
SimulationPreprocessor *preproc;
public:
MultiDarkLoader(const std::string& name, SimuData *h, SimulationPreprocessor *p)
: preproc(p), darkname(name), header(h)
{
}
~MultiDarkLoader()
{
delete header;
}
int num_files()
{
return 1;
}
SimuData *getHeader()
{
return header;
}
SimuData *loadFile(int id)
{
if (id != 0)
return 0;
ifstream fp(darkname.c_str());
SimuData *simu = new SimuData;
fp >> simu->BoxSize >> simu->Omega_M >> simu->Hubble >> simu->time >> simu->NumPart;
simu->time = 1./(1.+simu->time); // convert to scale factor
simu->TotalNumPart = simu->NumPart;
simu->Omega_Lambda = 1.0 - simu->Omega_M;
long estimated = (preproc == 0) ? simu->NumPart : preproc->getEstimatedPostprocessed(simu->NumPart);
long allocated = estimated;
for (int k = 0; k < 3; k++) {
simu->Pos[k] = new float[allocated];
simu->Vel[k] = new float[allocated];
}
simu->Id = new int64_t[allocated];
long *uniqueID = new long[allocated];
long *index = new long[allocated];
double tempData;
double shift = 0.5*simu->BoxSize;
double rescale_position = simu->Hubble*1.e-5*100./simu->time;
double rescale_velocity = one_kpc/one_Gyr;
#define INFINITY std::numeric_limits<float>::max()
float min_pos[3] = {INFINITY,INFINITY, INFINITY}, max_pos[3] = {-INFINITY,-INFINITY,-INFINITY};
cout << "loading multidark particles" << endl;
long actualNumPart = 0;
for (long i = 0; i < simu->NumPart; i++) {
SingleParticle p;
fp >> p.ID >> p.Pos[0] >> p.Pos[1]
>> p.Pos[2] >> p.Vel[2] >> p.Vel[1] >> p.Vel[0] >> tempData;
if (p.ID == -99 &&
p.Pos[0] == -99 && p.Pos[1] == -99 &&
p.Pos[2] == -99 && p.Vel[2] == -99)
break;
//p.Pos[0] = p.Pos[0]*rescale_position + shift;
//p.Pos[1] = p.Pos[1]*rescale_position + shift;
//p.Pos[2] = p.Pos[2]*rescale_position + shift;
//p.Vel[2] = p.Vel[2]*rescale_velocity;
// enforce box size in case of roundoff error
for (int k = 0; k < 3; k++) {
if (p.Pos[k] < 0) p.Pos[k] += simu->BoxSize;
if (p.Pos[k] >= simu->BoxSize) p.Pos[k] -= simu->BoxSize;
}
if (preproc != 0 && !preproc->accept(p))
continue;
copyParticleToSimu(p, simu, actualNumPart);
uniqueID[actualNumPart]= p.ID;
index[actualNumPart] = i;
actualNumPart++;
if (actualNumPart == allocated)
{
allocated += (estimated+9)/10;
reallocSimu(simu, allocated);
reallocArray(uniqueID, allocated, actualNumPart);
reallocArray(index, allocated, actualNumPart);
}
for (int k = 0; k < 3; k++) {
min_pos[k] = std::min(min_pos[k], p.Pos[k]);
max_pos[k] = std::max(max_pos[k], p.Pos[k]);
}
}
for (int k = 0; k < 3; k++) cout << boost::format("min[%d] = %g, max[%d] = %g") % k % min_pos[k] % k %max_pos[k] << endl;
applyTransformations(simu);
simu->NumPart = actualNumPart;
simu->TotalNumPart = actualNumPart;
simu->new_attribute("uniqueID", uniqueID, delete_adaptor<long>);
simu->new_attribute("index", index, delete_adaptor<long>);
return simu;
}
};
SimulationLoader *multidarkLoader(const string& multidarkname, SimulationPreprocessor *p)
{
SimuData *header;
int actualNumPart;
ifstream fp(multidarkname.c_str());
cout << "opening multidark file " << multidarkname << endl;
if (!fp)
{
cout << "could not open file!" << endl;
return 0;
}
header = new SimuData();
fp >> header->BoxSize >> header->Omega_M >> header->Hubble >> header->time >> header->NumPart;
header->time = 1./(1.+header->time); // convert to scale factor
header->TotalNumPart = header->NumPart;
header->Omega_Lambda = 1.0 - header->Omega_M;
return new MultiDarkLoader(multidarkname, header, p);
}

View file

@ -0,0 +1,183 @@
/*+
VIDE -- Void IDentification and Examination -- ./c_tools/mock/loaders/ramses_loader.cpp
Copyright (C) 2010-2014 Guilhem Lavaux
Copyright (C) 2011-2014 P. M. Sutter
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#include <cassert>
#include <string>
#include <CosmoTool/loadRamses.hpp>
#include <CosmoTool/fortran.hpp>
#include "simulation_loader.hpp"
// ben edit
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <regex.h>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <map>
// end ben edit
using namespace std;
using namespace CosmoTool;
class RamsesLoader: public SimulationLoader
{
private:
int load_flags;
int _num_files;
int baseid;
bool double_precision;
SimuData *ramses_header;
string snapshot_name;
SimulationPreprocessor *preproc;
public:
RamsesLoader(const string& basename, int baseid, bool dp, SimuData *header, int flags, int _num, SimulationPreprocessor *p)
: snapshot_name(basename), load_flags(flags), _num_files(_num), double_precision(dp),
ramses_header(header), preproc(p)
{
}
~RamsesLoader()
{
delete ramses_header;
}
SimuData *getHeader() {
return ramses_header;
}
int num_files() {
return _num_files;
}
SimuData *loadFile(int id) {
SimuData *d;
// cludgy hack until I can get baseid working in this function... the problem is with SimuData *loadFile(int id) ... just need to learn a bit more C++ ~ Ben
string baseidstr = snapshot_name.c_str();
unsigned found = baseidstr.find_last_of("/");
baseidstr = baseidstr.substr(found-5,found);
baseidstr = baseidstr.substr(0,5); // remove trailing slash
baseid = atoi(baseidstr.c_str());
if (id >= _num_files)
return 0;
d = loadRamsesSimu(snapshot_name.c_str(), baseid, id, double_precision, load_flags);
assert(d != 0);
if (d->Id != 0)
{
long *uniqueID = new long[d->NumPart];
for (long i = 0; i < d->NumPart; i++)
{
uniqueID[i] = d->Id[i];
}
d->new_attribute("uniqueID", uniqueID, delete_adaptor<long>);
}
applyTransformations(d);
basicPreprocessing(d, preproc);
return d;
}
};
SimulationLoader *ramsesLoader(const std::string& snapshot, int baseid, bool double_precision, int flags, SimulationPreprocessor *p)
{
SimuData *d, *header;
int num_files = 0; // how many particle files are there?
header = loadRamsesSimu(snapshot.c_str(), baseid, 0, double_precision, 0);
if (header == 0)
return 0;
// count number of CPU's for this output... kinda copy/paste of loadRamses.cpp in cosmotool/src
ostringstream ss_fname;
ss_fname << snapshot.c_str() << "/info_" << setfill('0') << setw(5) << baseid << ".txt";
cout << "Opening info file " << ss_fname.str() << " to find cpu number" << endl;
ifstream infile(ss_fname.str().c_str());
if (!infile)
return 0;
int err;
regex_t unit_l_rx;
// const char *pattern = "^unit_l[ ]*=[ ]*([0-9\\.E+\\-]+)";
const char *pattern = "^([A-Za-z_]+)[ ]*=[ ]*([0-9\\.E+\\-]+)";
err = regcomp (&unit_l_rx, pattern, REG_EXTENDED);
cout << unit_l_rx.re_nsub << endl;
if (err)
{
char errString[255];
regerror(err, &unit_l_rx, errString, sizeof(errString));
cout << errString << endl;
abort();
}
map<string,double> infoMap;
string line;
while (getline(infile, line))
{
regmatch_t allMatch[4];
if (!regexec(&unit_l_rx, line.c_str(), 4, allMatch, 0))
{
uint32_t start0 = allMatch[1].rm_so, end0 = allMatch[1].rm_eo;
uint32_t start1 = allMatch[2].rm_so, end1 = allMatch[2].rm_eo;
string keyword = line.substr(start0, end0-start0);
istringstream iss(line.substr(start1, end1-start1));
double unitLength;
iss >> unitLength;
infoMap[keyword] = unitLength;
}
}
regfree(&unit_l_rx);
num_files = infoMap["ncpu"];
return new RamsesLoader(snapshot, baseid, double_precision, header, flags, num_files, p);
}

View file

@ -0,0 +1,308 @@
/*+
VIDE -- Void IDentification and Examination -- ./c_tools/mock/loaders/sdf_loader.cpp
Copyright (C) 2010-2014 Guilhem Lavaux
Copyright (C) 2011-2014 P. M. Sutter
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#include <iostream>
#include <boost/format.hpp>
#include <vector>
#include <cassert>
#include <string>
#include "sdfloader_internal.hpp"
#include "simulation_loader.hpp"
#include <libSDF/SDF.h>
#undef SDFgetfloatOrDie
#undef SDFgetdoubleOrDie
#undef SDFgetintOrDie
using boost::format;
using namespace std;
using namespace CosmoTool;
static const double one_kpc = 3.08567802e16; /* km */
static const double one_Gyr = 3.1558149984e16; /* sec */
static void SDFgetfloatOrDie(SDF *sdfp, const char *name, float *v)
{
if( SDFgetfloat(sdfp, name, v) ) {
cerr << format("SDFgetfloat(%s) failed") % name << endl;
abort();
}
}
static void SDFgetdoubleOrDie(SDF *sdfp, const char *name, double *v)
{
if( SDFgetdouble(sdfp, name, v) ) {
cerr << format("SDFgetdouble(%s) failed") % name << endl;
abort();
}
}
static void SDFgetintOrDie(SDF *sdfp, const char *name, int *v)
{
if( SDFgetint(sdfp, name, v) ) {
cerr << format("SDFgetint(%s) failed") % name << endl;
abort();
}
}
class SDFLoader: public SimulationLoader
{
private:
int load_flags;
bool onefile;
int _num_files;
double unitMpc;
SimuData *sdf_header;
SDF *sdfp;
SimulationPreprocessor *preproc;
int num_splitting;
public:
SDFLoader(SDF *sdf_file, SimuData *header, int flags, int num_splitting,
SimulationPreprocessor *p)
: sdfp(sdf_file), load_flags(flags),
sdf_header(header), preproc(p)
{
this->num_splitting = num_splitting;
}
~SDFLoader()
{
delete sdf_header;
}
SimuData *getHeader() {
return sdf_header;
}
int num_files() {
return num_splitting;
}
int64_t getStart(int id)
{
return sdf_header->TotalNumPart * int64_t(id) / num_splitting;
}
int64_t getNumberInSplit(int id)
{
return getStart(id+1)-getStart(id);
}
void rescaleParticles(SimuData *d,
double rescale_position,
double rescale_velocity)
{
#define INFINITY std::numeric_limits<float>::max()
float shift = 0.5*d->BoxSize;
rescale_position /= d->time;
float min_pos[3] = {INFINITY,INFINITY, INFINITY}, max_pos[3] = {-INFINITY,-INFINITY,-INFINITY};
if (d->Pos[0] != 0)
{
for (int k = 0; k < 3; k++)
{
for (int64_t i = 0; i < d->NumPart; i++)
{
d->Pos[k][i] = (d->Pos[k][i]*rescale_position + shift);
min_pos[k] = std::min(min_pos[k], d->Pos[k][i]);
max_pos[k] = std::max(max_pos[k], d->Pos[k][i]);
}
cout << boost::format("min[%d] = %g, max[%d] = %g") % k % min_pos[k] % k %max_pos[k] << endl;
}
}
if (d->Vel[0] != 0)
{
for (int k = 0; k < 3; k++)
{
for (int64_t i = 0; i < d->NumPart; i++)
{
d->Vel[k][i] *= rescale_velocity;
}
}
}
}
void enforceBoxSize(SimuData *d)
{
for (int k = 0; k < 3; k++)
{
for (int64_t i = 0; i < d->NumPart; i++)
{
while (d->Pos[k][i]<0)
d->Pos[k][i] += d->BoxSize;
while (d->Pos[k][i]>=d->BoxSize)
d->Pos[k][i] -= d->BoxSize;
}
}
}
SimuData *loadFile(int id) {
SimuData *d;
int64_t starts[7];
int nobjs[7];
int strides[7];
void *addrs[7];
const char *names[7];
int ret;
if (id >= num_splitting)
return 0;
d = new SimuData;
d->BoxSize = sdf_header->BoxSize;
d->time = sdf_header->time;
d->Hubble = sdf_header->Hubble;
d->Omega_M = sdf_header->Omega_M;
d->Omega_Lambda = sdf_header->Omega_Lambda;
d->NumPart = getNumberInSplit(id);
int64_t numPartToLoad = getNumberInSplit(id);
int64_t start = getStart(id);
int k = 0;
#define SETUP_READ(name, vec) \
names[k] = name, starts[k] = start, nobjs[k] = numPartToLoad, strides[k] = sizeof(vec[0]), addrs[k] = vec, k++;
if (load_flags & NEED_POSITION)
{
const char *name_template[3] = { "x", "y", "z" };
for (int q = 0; q < 3; q++)
{
d->Pos[q] = new float[numPartToLoad];
SETUP_READ(name_template[q], d->Pos[q]);
}
}
if (load_flags & NEED_VELOCITY)
{
const char *name_template[3] = { "vx", "vy", "vz" };
for (int q = 0; q < 3; q++)
{
d->Vel[q] = new float[numPartToLoad];
SETUP_READ(name_template[q], d->Vel[q]);
}
}
if (load_flags & NEED_GADGET_ID)
{
d->Id = new int64_t[numPartToLoad];
SETUP_READ("ident", d->Id);
}
#undef SETUP_READ
ret = SDFseekrdvecsarr(sdfp, k, (char**)names, starts, nobjs, addrs, strides);
if (ret != 0)
{
cerr << format("Splitting %d/%d, SDF read failure: %s") % id % num_splitting % SDFerrstring << endl;
abort();
}
if (load_flags & (NEED_POSITION | NEED_VELOCITY))
rescaleParticles(d, d->Hubble*1e-5, one_kpc/one_Gyr);
enforceBoxSize(d);
applyTransformations(d);
basicPreprocessing(d, preproc);
return d;
}
};
SimulationLoader *sdfLoader(const std::string& snapshot, int flags,
int num_splitting,
SimulationPreprocessor *p)
{
SDF *sdfp;
int fileversion;
SimuData *hdr;
int64_t gnobj;
sdfp = SDFopen(0, snapshot.c_str());
if (sdfp == 0)
{
return 0;
}
cout << "Loading SDF with artificial splitting " << num_splitting << endl;
hdr = new SimuData;
SDFgetintOrDefault(sdfp, "version", &fileversion, 1);
double h0;
if (fileversion == 1)
{
SDFgetfloatOrDie(sdfp, "Omega0", &hdr->Omega_M);
SDFgetfloatOrDie(sdfp, "Lambda_prime", &hdr->Omega_Lambda);
SDFgetfloatOrDie(sdfp, "H0", &hdr->Hubble);
hdr->Hubble *= 1000.*(one_kpc/one_Gyr);
h0 = hdr->Hubble / 100.;
}
else
{
float Or, Of;
SDFgetfloatOrDie(sdfp, "Omega0_m", &hdr->Omega_M);
SDFgetfloatOrDie(sdfp, "Omega0_lambda", &hdr->Omega_Lambda);
SDFgetfloatOrDie(sdfp, "Omega0_r", &Or);
SDFgetfloatOrDie(sdfp, "Omega0_fld", &Of);
hdr->Omega_M += Or;
hdr->Omega_Lambda += Of;
SDFgetfloatOrDie(sdfp, "h_100", &hdr->Hubble);
hdr->Hubble *= 100.;
h0 = hdr->Hubble / 100.;
}
if (SDFhasname("R0", sdfp))
{
double R0;
SDFgetdoubleOrDie(sdfp, "R0", &R0);
hdr->BoxSize = 2.0*0.001*R0*h0;
}
if (SDFhasname("Rx", sdfp))
{
double R0;
SDFgetdoubleOrDie(sdfp, "Rx", &R0);
hdr->BoxSize = 2.0*0.001*R0*h0;
}
if (SDFhasname("redshift", sdfp))
{
double redshift;
SDFgetdoubleOrDie(sdfp, "redshift", &redshift);
hdr->time = 1/(1+redshift);
}
if (SDFgetint64(sdfp, "npart", &gnobj))
{
gnobj = SDFnrecs("x", sdfp);
cerr << format("[Warning] No 'npart' found in SDF file '%s', guessing npart=%ld from SDFnrecs") % snapshot % gnobj << endl;
}
hdr->NumPart = hdr->TotalNumPart = gnobj;
return new SDFLoader(sdfp, hdr, flags, num_splitting, p);
}
void sdfLoaderInit(int& argc, char **& argv)
{
}

View file

@ -0,0 +1,27 @@
/*+
VIDE -- Void IDentification and Examination -- ./c_tools/mock/loaders/sdfloader_internal.hpp
Copyright (C) 2010-2014 Guilhem Lavaux
Copyright (C) 2011-2014 P. M. Sutter
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#ifndef __VOID_SDF_LOADER_INTERNAL_HPP
#define __VOID_SDF_LOADER_INTERNAL_HPP
void sdfLoaderInit(int& argc, char **&argv);
#endif

View file

@ -0,0 +1,110 @@
/*+
VIDE -- Void IDentification and Examination -- ./c_tools/mock/loaders/simulation_loader.cpp
Copyright (C) 2010-2014 Guilhem Lavaux
Copyright (C) 2011-2014 P. M. Sutter
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#include <cmath>
#include <CosmoTool/loadSimu.hpp>
#include "simulation_loader.hpp"
#ifdef SDF_SUPPORT
#include "sdfloader_internal.hpp"
#endif
using std::min;
using namespace CosmoTool;
template<typename T> void reallocArray(T *& a, long newSize, long toCopy)
{
T *b = new T[newSize];
if (a != 0)
{
memcpy(b, a, sizeof(T)*toCopy);
delete[] a;
}
a = b;
}
void SimulationLoader::reallocSimu(SimuData *s, long newNumPart)
{
long to_copy = min(newNumPart, s->NumPart);
for (int j = 0; j < 3; j++)
{
reallocArray(s->Pos[j], newNumPart, to_copy);
reallocArray(s->Vel[j], newNumPart, to_copy);
}
reallocArray(s->Id, newNumPart, to_copy);
reallocArray(s->type, newNumPart, to_copy);
}
void SimulationLoader::applyTransformations(SimuData *s)
{
float redshift_gravity = do_redshift ? 1.0 : 0.0;
for (int i = 0; i < s->NumPart; i++)
{
s->Pos[redshift_axis][i] +=
redshift_gravity*s->Vel[redshift_axis][i]/100.;
}
}
void SimulationLoader::basicPreprocessing(SimuData *d,
SimulationPreprocessor *preproc)
{
if (preproc == 0)
return;
long numAccepted = 0;
bool *accepted = new bool[d->NumPart];
for (long i = 0; i < d->NumPart; i++)
{
SingleParticle p;
for (int k = 0; k < 3; k++)
{
p.Pos[k] = (d->Pos[k]) ? 0 : d->Pos[k][i];
p.Vel[k] = (d->Vel[k]) ? 0 : d->Vel[k][i];
}
p.ID = (d->Id) ? 0 : d->Id[i];
accepted[i] = preproc->accept(p);
numAccepted += accepted[i];
}
for (int k = 0; k < 3; k++)
{
filteredCopy(d->Pos[k], accepted, d->NumPart);
filteredCopy(d->Vel[k], accepted, d->NumPart);
}
filteredCopy(d->Id, accepted, d->NumPart);
filteredCopy(d->type, accepted, d->NumPart);
filterAttribute<long>(d, "uniqueID", accepted, d->NumPart);
d->NumPart = numAccepted;
delete[] accepted;
}
void simulationLoadersInit(int& argc, char **& argv)
{
#ifdef SDF_SUPPORT
sdfLoaderInit(argc, argv);
#endif
}

View file

@ -0,0 +1,156 @@
/*+
VIDE -- Void IDentification and Examination -- ./c_tools/mock/loaders/simulation_loader.hpp
Copyright (C) 2010-2014 Guilhem Lavaux
Copyright (C) 2011-2014 P. M. Sutter
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#ifndef _MOCK_SIMULATION_LOADER_HPP
#define _MOCK_SIMULATION_LOADER_HPP
#include <string>
#include <algorithm>
#include <CosmoTool/loadSimu.hpp>
struct SingleParticle
{
float Pos[3];
float Vel[3];
long Index;
long ID;
};
class SimulationPreprocessor
{
public:
SimulationPreprocessor() {}
virtual ~SimulationPreprocessor() {}
virtual long getEstimatedPostprocessed(long numParticles) { return numParticles; };
virtual bool accept(const SingleParticle& p) { return true; }
virtual void reset() {}
};
class SimulationLoader
{
protected:
bool do_redshift;
int redshift_axis;
SimulationLoader()
{
do_redshift = false;
redshift_axis = 2;
}
template<typename T> void reallocArray(T *& a, long newSize, long toCopy)
{
T *b = new T[newSize];
if (a != 0)
{
std::copy(a, a+toCopy, b);
delete[] a;
}
a = b;
}
void reallocSimu(CosmoTool::SimuData *s, long newNumPart);
void basicPreprocessing(CosmoTool::SimuData *d, SimulationPreprocessor *preproc);
void applyTransformations(CosmoTool::SimuData *s);
void copyParticleToSimu(const SingleParticle& p, CosmoTool::SimuData *s, long index)
{
s->Pos[0][index] = p.Pos[0];
s->Pos[1][index] = p.Pos[1];
s->Pos[2][index] = p.Pos[2];
if (s->Vel[0])
s->Vel[0][index] = p.Vel[0];
if (s->Vel[1])
s->Vel[1][index] = p.Vel[1];
if (s->Vel[2])
s->Vel[2][index] = p.Vel[2];
s->Id[index] = p.ID;
}
public:
virtual ~SimulationLoader() {}
void doRedshift(bool set = true) { do_redshift = set; }
void setVelAxis(int axis) { redshift_axis = axis; }
virtual CosmoTool::SimuData *getHeader() = 0;
virtual int num_files() = 0;
virtual CosmoTool::SimuData* loadFile(int id) = 0;
template<typename T>
void filteredCopy(T *a, bool *accepted, long N)
{
long i = 0, j = 0;
if (a == 0)
return;
while (i < N)
{
if (!accepted[i])
{
i++;
continue;
}
a[j] = a[i];
j++;
i++;
}
}
template<typename T>
void filterAttribute(CosmoTool::SimuData *d, const std::string& attrname, bool *accepted, long NumPart)
{
if (d->attributes.find(attrname) == d->attributes.end())
return;
long *l = d->as<T>(attrname);
filteredCopy<T>(l, accepted, NumPart);
}
};
template<typename T>
void delete_adaptor(void *ptr)
{
T *ptr_T = reinterpret_cast<T *>(ptr);
delete[] ptr_T;
}
// Unit length is the size of one Mpc in the simulation units
SimulationLoader *gadgetLoader(const std::string& snapshot, double Mpc_unitLength, int flags, int gadgetFormat, SimulationPreprocessor *p);
SimulationLoader *flashLoader(const std::string& snapshot, int flags, SimulationPreprocessor *p);
SimulationLoader *multidarkLoader(const std::string& snapshot, SimulationPreprocessor *p);
SimulationLoader *ramsesLoader(const std::string& snapshot, int baseid, bool double_precision, int flags, SimulationPreprocessor *p);
SimulationLoader *sdfLoader(const std::string& snapshot, int flags, int num_splitting, SimulationPreprocessor *p);
/* This is required for some parallel I/O handler (thus MPI beneath it) */
void simulationLoadersInit(int& argc, char **& argv);
#endif

View file

@ -0,0 +1,651 @@
/*+
VIDE -- Void IDentification and Examination -- ./c_tools/mock/prepObservation.cpp
Copyright (C) 2010-2014 Guilhem Lavaux
Copyright (C) 2011-2014 P. M. Sutter
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#include <healpix_map.h>
#include <healpix_map_fitsio.h>
#include <boost/format.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "prepObservation_conf.h"
#include "contour_pixels.hpp"
#include <netcdf>
#include <CosmoTool/fortran.hpp>
#include <gsl/gsl_interp.h>
#include <gsl/gsl_integration.h>
#define LIGHT_SPEED 299792.458
using namespace std;
using boost::format;
using namespace CosmoTool;
using namespace netCDF;
struct NYU_Data
{
int index;
int sector;
int region;
double ra, dec;
double cz;
double fgotten;
double phi_z;
long uniqueID;
};
struct Position
{
double xyz[3];
};
struct ParticleData
{
vector<int> id_gal;
vector<double> ra;
vector<double> dec;
vector<double> redshift;
vector<double> catalogID;
vector<long> uniqueID;
int id_mask;
// PMS
int mask_index;
// END PMS
vector<Position> pos;
double box[3][2];
double Lmax;
};
typedef vector<NYU_Data> NYU_VData;
// this defines the expansion function that we will integrate
// Laveaux & Wandelt (2012) Eq. 24
struct my_expan_params { double Om; double w0; double wa; };
double expanFun (double z, void * p) {
struct my_expan_params * params = (struct my_expan_params *)p;
double Om = (params->Om);
double w0 = (params->w0);
double wa = (params->wa);
//const double h0 = 1.0;
const double h0 = 0.71;
double ez;
double wz = w0 + wa*z/(1+z);
ez = Om*pow(1+z,3) + (1.-Om);
//ez = Om*pow(1+z,3) + pow(h0,2) * (1.-Om)*pow(1+z,3+3*wz);
ez = sqrt(ez);
//ez = sqrt(ez)/h0;
ez = 1./ez;
return ez;
}
void loadData(const string& fname, NYU_VData & data)
{
ifstream f(fname.c_str());
while (!f.eof())
{
NYU_Data d;
f >> d.index >> d.sector >> d.region >> d.ra >> d.dec >> d.cz >> d.fgotten >> d.phi_z;
// Maubert - avoid double counting of last particle in array if EOF is after a blank line
if (!f)
{
continue;
}
// End Maubert
d.uniqueID = d.index;
data.push_back(d);
}
}
void placeGalaxiesInCube(NYU_VData& data, ParticleData& output_data,
bool useComoving, double omegaM)
{
double d2r = M_PI/180;
gsl_function expanF;
expanF.function = &expanFun;
struct my_expan_params expanParams;
double maxZ = 2.0, z, result, error, *dL, *redshifts;
int numZ = 1000, iZ;
size_t nEval;
expanParams.Om = omegaM;
expanParams.w0 = -1.0;
expanParams.wa = 0.0;
expanF.params = &expanParams;
dL = (double *) malloc(numZ * sizeof(double));
redshifts = (double *) malloc(numZ * sizeof(double));
for (iZ = 0; iZ < numZ; iZ++) {
z = iZ * maxZ/numZ;
//gsl_integration_qng(&expanF, 0.0, z, 1.e-6, 1.e-6, &result, &error, &nEval);
dL[iZ] = result;
redshifts[iZ] = z;
}
gsl_interp *interp = gsl_interp_alloc(gsl_interp_linear, numZ);
gsl_interp_init(interp, redshifts, dL, numZ);
gsl_interp_accel *acc = gsl_interp_accel_alloc();
output_data.pos.resize(data.size());
output_data.id_gal.resize(data.size());
output_data.ra.resize(data.size());
output_data.dec.resize(data.size());
output_data.redshift.resize(data.size());
output_data.uniqueID.resize(data.size());
for (int j = 0; j < 3; j++)
{
output_data.box[j][0] = -INFINITY;
output_data.box[j][1] = INFINITY;
}
for (int i = 0; i < data.size(); i++)
{
double ra = data[i].ra*d2r, dec = data[i].dec*d2r;
Position& p = output_data.pos[i];
if (useComoving) {
//double pos = gsl_interp_eval(interp, redshifts, dL, data[i].cz, acc);
// Maubert - Lower boundary in redshift set to 0 to be consistent with pruneVoids (was 1.e-6 before).
gsl_integration_qng(&expanF, 0.0, data[i].cz/LIGHT_SPEED,
1.e-6,
1.e-6, &result, &error, &nEval);
double Dc = result*LIGHT_SPEED;
p.xyz[0] = Dc*cos(ra)*cos(dec);
p.xyz[1] = Dc*sin(ra)*cos(dec);
p.xyz[2] = Dc*sin(dec);
} else {
p.xyz[0] = data[i].cz*cos(ra)*cos(dec);
p.xyz[1] = data[i].cz*sin(ra)*cos(dec);
p.xyz[2] = data[i].cz*sin(dec);
}
//printf("CREATE %e %e\n", data[i].cz, sqrt(p.xyz[0]*p.xyz[0] + p.xyz[1]*p.xyz[1] + p.xyz[2]*p.xyz[2]));
output_data.id_gal[i] = data[i].index;
output_data.ra[i] = ra;
output_data.dec[i] = dec;
output_data.redshift[i] = data[i].cz;
output_data.uniqueID[i] = data[i].uniqueID;
for (int j = 0; j < 3; j++)
{
if (p.xyz[j] > output_data.box[j][0])
output_data.box[j][0] = p.xyz[j];
if (p.xyz[j] < output_data.box[j][1])
output_data.box[j][1] = p.xyz[j];
}
//printf("INSERT GAL %d %e %e %e\n", output_data.id_gal[i], p.xyz[0], p.xyz[1], p.xyz[2]);
}
// normalize box
float left = 1.e99;
float right = -1.e99;
for (int j = 0; j < 3; j++) {
if (output_data.box[j][1] < left) left = output_data.box[j][1];
if (output_data.box[j][0] > right) right = output_data.box[j][0];
}
for (int j = 0; j < 3; j++) {
output_data.box[j][1] = left;
output_data.box[j][0] = right;
}
cout << format("Galaxy position generated: %d galaxies") % output_data.pos.size() << endl;
cout << format("box is %g < x < %g; %g < y < %g; %g < z < %g")
% (1e-2*output_data.box[0][1]) % (1e-2*output_data.box[0][0])
% (1e-2*output_data.box[1][1]) % (1e-2*output_data.box[1][0])
% (1e-2*output_data.box[2][1]) % (1e-2*output_data.box[2][0]) << endl;
gsl_interp_free(interp);
}
void generateSurfaceMask(prepObservation_info& args ,
Healpix_Map<float>& mask,
vector<int>& pixel_list,
vector<int>& full_mask_list,
NYU_VData& data,
ParticleData& output_data,
bool useComoving,
double omegaM)
{
//Maubert - Needed for comobile distance in mock_sphere
gsl_function expanF;
expanF.function = &expanFun;
struct my_expan_params expanParams;
expanParams.Om = omegaM;
expanParams.w0 = -1.0;
expanParams.wa = 0.0;
expanF.params = &expanParams;
double result, error ;
size_t nEval;
//End Maubert - Needed for comobile distance in mock_sphere
// Find the first free index
int idx = -1;
int insertion = 0;
double volume = pixel_list.size()*1.0/mask.Npix()*4*M_PI;
int numToInsert;
for (int i = 0; i < output_data.id_gal.size(); i++)
{
if (idx < output_data.id_gal[i])
idx = output_data.id_gal[i]+1;
}
output_data.id_mask = idx;
// PMS
output_data.mask_index = output_data.id_gal.size();
// END PMS
cout << "Generate surface mask..." << endl;
double Rmax = -1;
for (int j = 0; j < 3; j++)
{
Rmax = max(Rmax, max(output_data.box[j][0], -output_data.box[j][1]));
}
output_data.Lmax = Rmax;
// PMS - write a small text file with galaxy position
FILE *fp;
fp = fopen("galaxies.txt", "w");
for (int i = 0; i < data.size(); i++) {
Position& p = output_data.pos[i];
fprintf(fp, "%e %e %e\n",
(p.xyz[0]),
(p.xyz[1]),
(p.xyz[2]));
}
fclose(fp);
// END PMS
cout << format("Rmax is %g, surface volume is %g") % (Rmax/100) % (volume/(4*M_PI)) << endl;
volume *= Rmax*Rmax*Rmax/3/1e6;
numToInsert = (int)floor(volume*args.density_fake_arg);
cout << format("3d volume to fill: %g (Mpc/h)^3") % volume << endl;
cout << format("Will insert %d particles") % numToInsert << endl;
fp = fopen("mock_galaxies.txt", "w");
double pct = 0;
for (int i = 0; i < numToInsert; i++) {
double new_pct = i*100./numToInsert;
if (new_pct-pct > 5.) {
pct = new_pct;
cout << format(" .. %3.0f %%") % pct << endl;
}
Position p;
bool stop_here;
do {
int p0 = (int)floor(drand48()*pixel_list.size());
vec3 v = mask.pix2vec(pixel_list[p0]);
double r = Rmax*pow(drand48(),1./3);
p.xyz[0] = v.x * r;
p.xyz[1] = v.y * r;
p.xyz[2] = v.z * r;
stop_here = true;
for (int j = 0; j < 3; j++) {
if (p.xyz[j] > output_data.box[j][0] ||
p.xyz[j] < output_data.box[j][1])
stop_here = false;
}
}
while (!stop_here);
// PMS : write mock galaxies to a small file for plotting
fprintf(fp, "%e %e %e\n",
(p.xyz[0]),
(p.xyz[1]),
(p.xyz[2]));
// END PMS
output_data.pos.push_back(p);
output_data.id_gal.push_back(idx);
output_data.ra.push_back(-1);
output_data.dec.push_back(-1);
output_data.redshift.push_back(-1);
output_data.uniqueID.push_back(-1);
//printf("INSERT MOCK %d %e %e %e\n", idx, p.xyz[0], p.xyz[1], p.xyz[2]);
insertion++;
}
fclose(fp);
// PMS
// TEST - insert mock galaxies along box edge
fp = fopen("mock_boundary.txt", "w");
double dx[3];
dx[0] = output_data.box[0][1] - output_data.box[0][0];
dx[1] = output_data.box[1][1] - output_data.box[1][0];
dx[2] = output_data.box[2][1] - output_data.box[2][0];
int nPart = 100;
// TEST
for (int iDir = 0; iDir < 0; iDir++) {
for (int iFace = 0; iFace < 0; iFace++) {
//for (int iDir = 0; iDir < 3; iDir++) {
//for (int iFace = 0; iFace < 2; iFace++) {
int iy = (iDir + 1) % 3;
int iz = (iDir + 2) % 3;
for (int i = 0; i < nPart; i++) {
for (int j = 0; j < nPart; j++) {
Position p;
p.xyz[iDir] = output_data.box[iDir][iFace];
p.xyz[iy] = i * dx[iy]/nPart + output_data.box[iy][0];
p.xyz[iz] = j * dx[iz]/nPart + output_data.box[iz][0];
output_data.pos.push_back(p);
output_data.id_gal.push_back(idx);
output_data.ra.push_back(-1);
output_data.dec.push_back(-1);
output_data.redshift.push_back(-1);
output_data.uniqueID.push_back(-1);
insertion++;
fprintf(fp, "%e %e %e\n",
(p.xyz[0]),
(p.xyz[1]),
(p.xyz[2]));
}
}
}
}
fclose(fp);
// END PMS
// PMS
// TEST - insert mock galaxies along spheres of survey redshift boundaries
fp = fopen("mock_sphere.txt", "w");
//Maubert - insert mock galaxies according to useComoving specification
// Added & compute rmin & rmax out of loop
double rmin ;
double rmax ;
if (useComoving) {
gsl_integration_qng(&expanF, 0.0, args.zMin_arg,
1.e-6,
1.e-6, &result, &error, &nEval);
rmin = result* LIGHT_SPEED;
gsl_integration_qng(&expanF, 0.0, args.zMax_arg,
1.e-6,
1.e-6, &result, &error, &nEval);
rmax = result* LIGHT_SPEED;
} else {
rmin = args.zMin_arg * LIGHT_SPEED;
rmax = args.zMax_arg * LIGHT_SPEED;
}
//for (int q = 0; q < 0; q++) {
for (int q = 0; q < full_mask_list.size(); q++) {
vec3 v = mask.pix2vec(full_mask_list[q]);
Position p;
if (rmin > 0.) {
p.xyz[0] = v.x * rmin;
p.xyz[1] = v.y * rmin;
p.xyz[2] = v.z * rmin;
output_data.pos.push_back(p);
output_data.id_gal.push_back(idx);
output_data.ra.push_back(-1);
output_data.dec.push_back(-1);
output_data.redshift.push_back(-1);
output_data.uniqueID.push_back(-1);
insertion++;
fprintf(fp, "%e %e %e\n",
(p.xyz[0]),
(p.xyz[1]),
(p.xyz[2]));
}
p.xyz[0] = v.x * rmax;
p.xyz[1] = v.y * rmax;
p.xyz[2] = v.z * rmax;
output_data.pos.push_back(p);
output_data.id_gal.push_back(idx);
output_data.ra.push_back(-1);
output_data.dec.push_back(-1);
output_data.redshift.push_back(-1);
output_data.uniqueID.push_back(-1);
insertion++;
fprintf(fp, "%e %e %e\n",
(p.xyz[0]),
(p.xyz[1]),
(p.xyz[2]));
}
fclose(fp);
// END PMS
cout << format("Done. Inserted %d particles.") % insertion << endl;
}
void saveData(ParticleData& pdata)
{
NcFile f("particles.nc", NcFile::replace);
NcDim d = f.addDim("space", 3);
NcDim p = f.addDim("Np", pdata.pos.size());
NcVar v = f.addVar("particles", ncDouble, {d, p});
double *x = new double[pdata.pos.size()];
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < pdata.pos.size(); i++)
x[i] = pdata.pos[i].xyz[j];
v.putVar({size_t(j), 0}, {1, pdata.pos.size()}, x);
}
v = f.addVar("id_gal", ncInt, std::vector<NcDim>({p}));
v.putVar(&pdata.id_gal[0]);
delete[] x;
}
void saveForZobov(ParticleData& pdata, const string& fname, const string& paramname)
{
UnformattedWrite f(fname);
static const char axis[] = { 'X', 'Y', 'Z' };
double Lmax = pdata.Lmax;
double r2d = 180./M_PI;
f.beginCheckpoint();
f.writeInt32(pdata.pos.size());
f.endCheckpoint();
for (int j = 0; j < 3; j++)
{
cout << format("Writing %c components...") % axis[j] << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < pdata.pos.size(); i++)
{
f.writeReal32((pdata.pos[i].xyz[j]+Lmax)/(2*Lmax));
}
f.endCheckpoint();
}
cout << format("Writing RA...") << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < pdata.pos.size(); i++) {
f.writeReal32(pdata.ra[i]*r2d);
}
f.endCheckpoint();
cout << format("Writing Dec...") << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < pdata.pos.size(); i++) {
f.writeReal32(pdata.dec[i]*r2d);
}
f.endCheckpoint();
cout << format("Writing Redshift...") << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < pdata.pos.size(); i++) {
f.writeReal32(pdata.redshift[i]);
}
f.endCheckpoint();
cout << format("Writing Unique ID...") << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < pdata.pos.size(); i++) {
f.writeInt64(pdata.uniqueID[i]);
}
f.endCheckpoint();
NcFile fp(paramname.c_str(), NcFile::replace);
fp.putAtt("range_x_min", ncDouble, -Lmax/100.);
fp.putAtt("range_x_max", ncDouble, Lmax/100.);
fp.putAtt("range_y_min", ncDouble, -Lmax/100.);
fp.putAtt("range_y_max", ncDouble, Lmax/100.);
fp.putAtt("range_z_min", ncDouble, -Lmax/100.);
fp.putAtt("range_z_max", ncDouble, Lmax/100.);
fp.putAtt("mask_index", ncInt, pdata.mask_index); // PMS
fp.putAtt("is_observation", ncInt, 1); // PMS
int nOutputPart = pdata.mask_index;
//int nOutputPart = pdata.pos.size();
NcDim NumPart_dim = fp.addDim("numpart_dim", nOutputPart);
NcVar v = fp.addVar("particle_ids", ncInt, NumPart_dim);
//NcVar v2 = fp.addVar("expansion", ncDouble, NumPart_dim);
//double *expansion_fac = new double[pdata.pos.size()];
//for (int i = 0; i < pdata.pos.size(); i++)
// expansion_fac[i] = 1.0;
v.putVar({0}, {size_t(nOutputPart)}, &pdata.id_gal[0]);
//v2->put(expansion_fac, pdata.pos.size());
//delete[] expansion_fac;
/*
FILE *infoFile = fopen("sample_info.txt", "w");
fprintf(infoFile, "x_min = %f\n", -Lmax/100.);
fprintf(infoFile, "x_max = %f\n", Lmax/100.);
fprintf(infoFile, "y_min = %f\n", -Lmax/100.);
fprintf(infoFile, "y_max = %f\n", Lmax/100.);
fprintf(infoFile, "z_min = %f\n", -Lmax/100.);
fprintf(infoFile, "z_max = %f\n", Lmax/100.);
fprintf(infoFile, "mask_index = %d\n", pdata.mask_index);
fprintf(infoFile, "total_particles = %d\n", pdata.pos.size());
fclose(infoFile);
*/
}
int main(int argc, char **argv)
{
prepObservation_info args_info;
prepObservation_conf_params args_params;
prepObservation_conf_init(&args_info);
prepObservation_conf_params_init(&args_params);
args_params.check_required = 0;
if (prepObservation_conf_ext (argc, argv, &args_info, &args_params))
return 1;
if (!args_info.configFile_given)
{
if (prepObservation_conf_required (&args_info, PREPOBSERVATION_CONF_PACKAGE))
return 1;
}
else
{
args_params.check_required = 1;
args_params.initialize = 0;
if (prepObservation_conf_config_file (args_info.configFile_arg,
&args_info,
&args_params))
return 1;
}
prepObservation_conf_print_version();
cout << "Loading data " << args_info.catalog_arg << "..." << endl;
vector<NYU_Data> data;
Healpix_Map<float> o_mask;
vector<int> pixel_list;
vector<int> full_mask_list;
ParticleData output_data;
loadData(args_info.catalog_arg, data);
cout << "Loading mask..." << endl;
read_Healpix_map_from_fits(args_info.mask_arg, o_mask);
Healpix_Map<float> mask;
mask.SetNside(128, RING);
mask.Import(o_mask);
computeContourPixels(mask,pixel_list);
computeMaskPixels(mask,full_mask_list);
// We compute a cube holding all the galaxies + the survey surface mask
cout << "Placing galaxies..." << endl;
placeGalaxiesInCube(data, output_data, args_info.useComoving_flag,
args_info.omegaM_arg);
generateSurfaceMask(args_info, mask, pixel_list, full_mask_list,
data, output_data,args_info.useComoving_flag,
args_info.omegaM_arg);
saveForZobov(output_data, args_info.output_arg, args_info.params_arg);
// saveData(output_data);
// PMS
FILE *fp = fopen("mask_index.txt", "w");
fprintf(fp, "%d", output_data.mask_index);
fclose(fp);
fp = fopen("total_particles.txt", "w");
fprintf(fp, "%d", output_data.pos.size());
fclose(fp);
printf("Done!\n");
// END PMS
return 0;
}

View file

@ -0,0 +1,19 @@
package "prepObservation"
version "alpha"
option "configFile" - "Configuration filename" string optional
option "catalog" - "Input NYU-VAGC catalog" string required
option "mask" - "Healpix mask of unobserved data (in Equatorial coordinates)" string required
option "density_fake" - "Number density of boundary fake tracers (1 h^3/ Mpc^3)" double optional default="1"
option "zMin" - "Minimum redshift of data" double required
option "zMax" - "Maximum redshift of data" double required
option "output" - "Filename of particle datafile" string required
option "params" - "Output parameters of the datacube" string required
option "useComoving" - "Convert to real space using LCDM cosmology" flag off
option "omegaM" - "Omega Matter for fiducial cosmology" double optional default="0.27"

View file

@ -0,0 +1,791 @@
/*+
VIDE -- Void IDentification and Examination -- ./c_tools/mock/prepSimulation.cpp
Copyright (C) 2010-2014 Guilhem Lavaux
Copyright (C) 2011-2014 P. M. Sutter
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#include <gsl/gsl_rng.h>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/format.hpp>
#include <cmath>
#include <cassert>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <CosmoTool/loadSimu.hpp>
#include <CosmoTool/interpolate.hpp>
#include <CosmoTool/fortran.hpp>
#include <CosmoTool/algo.hpp>
#include "prepSimulation_conf.h"
#include "gslIntegrate.hpp"
#include <netcdf>
#include "simulation_loader.hpp"
using namespace std;
using namespace CosmoTool;
using boost::format;
using namespace netCDF;
#define LIGHT_SPEED 299792.458
typedef boost::function2<void, SimuData*, double*> MetricFunctor;
struct TotalExpansion
{
double Omega_M, Omega_L;
double operator()(double z)
{
return 1/sqrt(Omega_M*cube(1+z) + Omega_L);
}
};
Interpolate make_cosmological_redshift(double OM, double OL, double z0, double z1, int N = 5000)
{
TotalExpansion e_computer;
double D_tilde, Q, Qprime;
InterpolatePairs pairs;
e_computer.Omega_M = OM;
e_computer.Omega_L = OL;
pairs.resize(N);
ofstream f("comoving_distance.txt");
for (int i = 0; i < N; i++)
{
double z = z0 + (z1-z0)/N*i;
pairs[i].second = z;
pairs[i].first = gslIntegrate(e_computer, 0, z, 1e-3);
f << z << " " << pairs[i].first << endl;
}
return buildFromVector(pairs);
}
void metricTransform(SimuData *data, int axis, bool reshift, bool pecvel, double* expfact, bool cosmo_flag)
{
int x0, x1, x2;
switch (axis) {
case 0:
x0 = 1; x1 = 2; x2 = 0;
break;
case 1:
x0 = 0; x1 = 2; x2 = 1;
break;
case 2:
x0 = 0; x1 = 1; x2 = 2;
break;
default:
abort();
}
double z0 = 1/data->time - 1;
Interpolate z_vs_D =
make_cosmological_redshift(data->Omega_M, data->Omega_Lambda,
0., z0+8*data->BoxSize*100/LIGHT_SPEED);
// Redshift 2*z0 should be sufficient ? This is fragile.
//A proper solver is needed here.
double z_base = reshift ? z0 : 0;
TotalExpansion e_computer;
double baseComovingDistance;
cout << "Using base redshift z=" << z0 << " " << z0+8*data->BoxSize*100/LIGHT_SPEED << endl;
e_computer.Omega_M = data->Omega_M;
e_computer.Omega_L = data->Omega_Lambda;
baseComovingDistance = LIGHT_SPEED/100.* gslIntegrate(e_computer, 0, z0, 1e-3);
cout << "Comoving distance = " << baseComovingDistance << " Mpc/h" << endl;
if (cosmo_flag) cout << "Will place particles on a lightcone..." << endl;
float minZ = 1.e99;
float maxZ = 0;
for (uint32_t i = 0; i < data->NumPart; i++)
{
float& x = data->Pos[x0][i];
float& y = data->Pos[x1][i];
float& z = data->Pos[x2][i];
float& v = data->Vel[2][i];
float z_old = z;
double reduced_red = (z + baseComovingDistance)*100./LIGHT_SPEED;
double reduced_base = (reshift ? (baseComovingDistance*100./LIGHT_SPEED) : 0);
try
{
// Distorted redshift
if (reduced_red == 0)
z = 0;
else if (cosmo_flag)
z = (z_vs_D.compute(reduced_red)-z_base)*LIGHT_SPEED/100.;
else
z = (reduced_red-reduced_base)*LIGHT_SPEED/100.0;
if (expfact)
expfact[i] = z / z_old;
// Add peculiar velocity
if (pecvel)
z += v/(100*e_computer(z0));
}
catch(const InvalidRangeException& e) {
cout << "Trying to interpolate out of the tabulated range." << endl;
cout << "The offending value is z=" << reduced_red << endl;
abort();
}
if (z > maxZ) maxZ = z;
if (z < minZ) minZ = z;
}
printf("Range of z: %.2f - %.2f\n", minZ, maxZ);
}
// slightly perturb particle positions
void joggleParticles(SimuData *data) {
cout << "Joggling particle positions..." << endl;
gsl_rng *myRng = gsl_rng_alloc(gsl_rng_taus);
int seed = 314159;
gsl_rng_set(myRng, seed);
for (uint32_t i = 0; i < data->NumPart; i++) {
data->Pos[0][i] += 1.e-3*gsl_rng_uniform(myRng);
data->Pos[1][i] += 1.e-3*gsl_rng_uniform(myRng);
data->Pos[2][i] += 1.e-3*gsl_rng_uniform(myRng);
data->Pos[0][i] -= 1.e-3*gsl_rng_uniform(myRng);
data->Pos[1][i] -= 1.e-3*gsl_rng_uniform(myRng);
data->Pos[2][i] -= 1.e-3*gsl_rng_uniform(myRng);
}
} // end joggleParticles
void generateOutput(SimuData *data, int axis,
const std::string& fname)
{
UnformattedWrite f(fname);
cout << "Generating output particles to " << fname << endl;
int x0, x1, x2;
switch (axis) {
case 0:
x0 = 1; x1 = 2; x2 = 0;
break;
case 1:
x0 = 0; x1 = 2; x2 = 1;
break;
case 2:
x0 = 0; x1 = 1; x2 = 2;
break;
default:
abort();
}
f.beginCheckpoint();
f.writeInt32(data->NumPart);
f.endCheckpoint();
cout << "Writing X components..." << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < data->NumPart; i++)
{
f.writeReal32(data->Pos[x0][i]);
}
f.endCheckpoint();
cout << "Writing Y components..." << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < data->NumPart; i++)
{
f.writeReal32(data->Pos[x1][i]);
}
f.endCheckpoint();
cout << "Writing Z components..." << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < data->NumPart; i++)
{
f.writeReal32(data->Pos[x2][i]);
}
f.endCheckpoint();
cout << "Writing RA..." << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < data->NumPart; i++)
{
f.writeReal32(data->Pos[x0][i]);
}
f.endCheckpoint();
cout << "Writing Dec..." << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < data->NumPart; i++)
{
f.writeReal32(data->Pos[x1][i]);
}
f.endCheckpoint();
cout << "Writing redshift..." << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < data->NumPart; i++)
{
f.writeReal32(data->Pos[x2][i]*LIGHT_SPEED);
}
f.endCheckpoint();
long *uniqueID = data->as<long>("uniqueID");
if (uniqueID != 0)
{
cout << "Writing unique ID..." << endl;
f.beginCheckpoint();
for (uint32_t i = 0; i < data->NumPart; i++)
{
f.writeInt64(uniqueID[i]);
}
f.endCheckpoint();
}
}
// This function prepares the list of targets for the specified snapshot. The target list is not
// cleared. That way new particles can be appended if this is a multi-file snapshot.
void selectBox(SimuData *simu, std::vector<long>& targets, prepSimulation_info& args_info, SimulationPreprocessor *preselect)
{
double ranges[3][2] = {
{ args_info.rangeX_min_arg, args_info.rangeX_max_arg },
{ args_info.rangeY_min_arg, args_info.rangeY_max_arg },
{ args_info.rangeZ_min_arg, args_info.rangeZ_max_arg }
};
long numAccepted =0;
for (uint32_t i = 0; i < simu->NumPart; i++)
{
bool acceptance = true;
SingleParticle p;
for (int j = 0; j < 3; j++) {
acceptance =
acceptance &&
(simu->Pos[j][i] > ranges[j][0]) &&
(simu->Pos[j][i] <= ranges[j][1]);
p.Pos[j] = simu->Pos[j][i];
p.Vel[j] = (simu->Vel[j] != 0) ? simu->Vel[j][i] : 0;
}
p.ID = (simu->Id != 0) ? simu->Id[i] : -1;
if (preselect != 0)
acceptance = acceptance && preselect->accept(p);
if (acceptance) {
targets.push_back(i);
numAccepted++;
}
}
cout << "SELECTBOX: Num accepted here = " << numAccepted << " / input = " << simu->NumPart << " (after resubsampling)" << endl;
}
class PreselectParticles: public SimulationPreprocessor
{
private:
gsl_rng *rng;
double subsample;
int seed;
public:
PreselectParticles(double s, int seed_value)
: subsample(s), seed(seed_value), rng(gsl_rng_alloc(gsl_rng_default))
{
gsl_rng_set(rng, seed);
}
virtual ~PreselectParticles()
{
gsl_rng_free(rng);
}
bool accept(const SingleParticle& p)
{
return gsl_rng_uniform(rng) < subsample;
}
void reset()
{
gsl_rng_set(rng, seed);
}
};
void createBox(SimuData *simu, vector<long>& targets, vector<long>& snapshot_split, SimuData *& boxed, prepSimulation_info& args_info)
{
double *ranges = new double[6];
double *mul = new double[3];
long *simu_uniqueID = simu->as<long>("uniqueID");
ranges[0] = args_info.rangeX_min_arg;
ranges[1] = args_info.rangeX_max_arg;
ranges[2] = args_info.rangeY_min_arg;
ranges[3] = args_info.rangeY_max_arg;
ranges[4] = args_info.rangeZ_min_arg;
ranges[5] = args_info.rangeZ_max_arg;
boxed = new SimuData;
boxed->Hubble = simu->Hubble;
boxed->Omega_M = simu->Omega_M;
boxed->Omega_Lambda = simu->Omega_Lambda;
boxed->time = simu->time;
boxed->BoxSize = simu->BoxSize;
boxed->NumPart = targets.size();
for (int j = 0; j < 3; j++)
{
boxed->Pos[j] = new float[boxed->NumPart];
boxed->Vel[j] = new float[boxed->NumPart];
//boxed->Vel[j] = 0;
mul[j] = 1.0/(ranges[2*j+1] - ranges[2*j+0]);
}
cout << "Min range = " << ranges[0] << " " << ranges[2] << " " << ranges[4] << endl;
cout << "Max range = " << ranges[1] << " " << ranges[3] << " " << ranges[5] << endl;
cout << "Number of accepted particles: " << boxed->NumPart << endl;
cout << "Rescaling factors = " << mul[0] << " " << mul[1] << " " << mul[2] << endl;
// PMS
FILE *fp = fopen("mask_index.txt", "w");
fprintf(fp, "%ld", boxed->NumPart);
fclose(fp);
fp = fopen("total_particles.txt", "w");
fprintf(fp, "%ld", boxed->NumPart);
fclose(fp);
printf("Done!\n");
// END PMS
long *uniqueID = new long[boxed->NumPart];
long *particle_id = new long[boxed->NumPart];
double *expansion_fac = new double[boxed->NumPart];
long *snap_split = new long[snapshot_split.size()];
int *numsnap_info = new int[1];
copy(targets.begin(), targets.end(), particle_id);
copy(snapshot_split.begin(), snapshot_split.end(), snap_split);
*numsnap_info = snapshot_split.size();
boxed->new_attribute("particle_id", particle_id, delete_adaptor<long>);
boxed->new_attribute("expansion_fac", expansion_fac, delete_adaptor<double>);
boxed->new_attribute("uniqueID", uniqueID, delete_adaptor<long>);
boxed->new_attribute("mul", mul, delete_adaptor<double>);
boxed->new_attribute("ranges", ranges, delete_adaptor<double>);
boxed->new_attribute("snapshot_split", snap_split, delete_adaptor<long>);
boxed->new_attribute("num_snapshots", numsnap_info, delete_adaptor<int>);
}
void buildBox(SimuData *simu, long num_targets, long loaded,
SimuData *boxed, double *efac)
{
uint32_t k = 0;
long *uniqueID = boxed->as<long>("uniqueID");
long *simu_uniqueID = simu->as<long>("uniqueID");
double *expansion_fac = boxed->as<double>("expansion_fac");
double *mul = boxed->as<double>("mul");
double *ranges = boxed->as<double>("ranges");
long *particle_id = boxed->as<long>("particle_id");
for (uint32_t i = 0; i < num_targets; i++, loaded++)
{
long pid = particle_id[loaded];
//assert(pid < simu->NumPart);
assert(loaded < boxed->NumPart);
for (int j = 0; j < 3; j++)
{
boxed->Pos[j][loaded] = max(min((simu->Pos[j][pid]-ranges[j*2])*mul[j], double(1)), double(0));
boxed->Vel[j][loaded] = simu->Vel[j][pid];
assert(boxed->Pos[j][loaded] >= 0);
assert(boxed->Pos[j][loaded] <= 1);
}
uniqueID[loaded] = (simu_uniqueID != 0) ? simu_uniqueID[pid] : 0;
expansion_fac[loaded] = efac[pid];
}
}
void saveBox(SimuData *&boxed, const std::string& outbox, prepSimulation_info& args_info)
{
double *ranges = boxed->as<double>("ranges");
NcFile f(outbox.c_str(), NcFile::replace);
long *particle_id = boxed->as<long>("particle_id");
double *expansion_fac = boxed->as<double>("expansion_fac");
long *snapshot_split = boxed->as<long>("snapshot_split");
int num_snapshots = *boxed->as<int>("num_snapshots");
long *uniqueID = boxed->as<long>("uniqueID");
float *velX = boxed->Vel[0];
float *velY = boxed->Vel[1];
float *velZ = boxed->Vel[2];
f.putAtt("range_x_min", ncDouble, ranges[0]);
f.putAtt("range_x_max", ncDouble, ranges[1]);
f.putAtt("range_y_min", ncDouble, ranges[2]);
f.putAtt("range_y_max", ncDouble, ranges[3]);
f.putAtt("range_z_min", ncDouble, ranges[4]);
f.putAtt("range_z_max", ncDouble, ranges[5]);
f.putAtt("mask_index", ncInt, -1);
f.putAtt("is_observation", ncInt, 0);
f.putAtt("data_subsampling", ncInt, args_info.subsample_arg);
NcDim NumPart_dim = f.addDim("numpart_dim", boxed->NumPart);
NcDim NumSnap_dim = f.addDim("numsnap_dim", num_snapshots);
NcVar v = f.addVar("particle_ids", ncInt64, NumPart_dim);
NcVar v2 = f.addVar("expansion", ncDouble, NumPart_dim);
NcVar v3 = f.addVar("snapshot_split", ncInt64, NumSnap_dim);
v.putVar({0}, {size_t(boxed->NumPart)}, particle_id);
v2.putVar({0}, {size_t(boxed->NumPart)}, expansion_fac);
v3.putVar({0}, {size_t(num_snapshots)}, snapshot_split);
if (uniqueID != 0)
{
NcVar v4 = f.addVar("unique_ids_lsb", ncInt, NumPart_dim);
NcVar v5 = f.addVar("unique_ids_msb", ncInt, NumPart_dim);
nclong *tmp_int = new nclong[boxed->NumPart];
for (long i = 0; i < boxed->NumPart; i++)
tmp_int[i] = (nclong)(((unsigned long)uniqueID[i]) & 0xffffffff);
v4.putVar({0}, {size_t(boxed->NumPart)}, tmp_int);
for (long i = 0; i < boxed->NumPart; i++)
tmp_int[i] = (nclong)((((unsigned long)uniqueID[i]) & 0xffffffff) >> 32);
v5.putVar({0}, {size_t(boxed->NumPart)}, tmp_int);
delete[] tmp_int;
}
NcVar v6 = f.addVar("vel_x", ncFloat, NumPart_dim);
NcVar v7 = f.addVar("vel_y", ncFloat, NumPart_dim);
NcVar v8 = f.addVar("vel_z", ncFloat, NumPart_dim);
v6.putVar({0}, {size_t(boxed->NumPart)}, velX);
v7.putVar({0}, {size_t(boxed->NumPart)}, velY);
v8.putVar({0}, {size_t(boxed->NumPart)}, velZ);
}
void makeBoxFromParameter(SimuData *simu, SimuData* &boxed, prepSimulation_info& args_info)
{
NcFile f(args_info.inputParameter_arg, NcFile::read);
NcVar *v;
long *particle_id;
double *expansion_fac;
long *uniqueID;
long *snapshot_split;
int *num_snapshots = new int[1];
boxed = new SimuData;
boxed->Hubble = simu->Hubble;
boxed->Omega_M = simu->Omega_M;
boxed->Omega_Lambda = simu->Omega_Lambda;
boxed->time = simu->time;
boxed->BoxSize = simu->BoxSize;
NcGroupAtt d_sub = f.getAtt("data_subsampling");
auto checkAtt = [&args_info](NcGroupAtt a) {
if (a.isNull())
return true;
double subsampling;
a.getValues(&subsampling);
return subsampling/args_info.subsample_arg - 1 > 1e-5;
};
if (checkAtt(d_sub))
{
cerr << "Parameter file was not generated with the same simulation subsampling argument. Particles will be different. Stop here." <<endl;
exit(1);
}
NcVar v_id = f.getVar("particle_ids");
NcVar v_snap = f.getVar("snapshot_split");
double *ranges;
double *mul;
std::vector<NcDim> edges1 = v_id.getDims();
std::vector<NcDim> dim_snap = v_snap.getDims();
assert(edges1.size()==1);
assert(dim_snap.size()==1);
boxed->NumPart = edges1[0].getSize();
*num_snapshots = dim_snap[0].getSize();
particle_id = new long[boxed->NumPart];
uniqueID = new long[boxed->NumPart];
mul = new double[3];
ranges = new double[6];
snapshot_split = new long[*num_snapshots];
expansion_fac = new double[boxed->NumPart];
boxed->new_attribute("uniqueID", uniqueID, delete_adaptor<long>);
boxed->new_attribute("mul", mul, delete_adaptor<double>);
boxed->new_attribute("ranges", ranges, delete_adaptor<double>);
boxed->new_attribute("particle_id", particle_id, delete_adaptor<long>);
boxed->new_attribute("num_snapshots", num_snapshots, delete_adaptor<int>);
boxed->new_attribute("snapshot_split", snapshot_split, delete_adaptor<long>);
boxed->new_attribute("expansion_fac", expansion_fac, delete_adaptor<double>);
v_id.getVar(particle_id);
v_snap.getVar(snapshot_split);
f.getAtt("range_x_min").getValues(&ranges[0]);
f.getAtt("range_x_max").getValues(&ranges[1]);
f.getAtt("range_y_min").getValues(&ranges[2]);
f.getAtt("range_y_max").getValues(&ranges[3]);
f.getAtt("range_z_min").getValues(&ranges[4]);
f.getAtt("range_z_max").getValues(&ranges[5]);
for (int j = 0; j < 3; j++)
{
boxed->Pos[j] = new float[boxed->NumPart];
boxed->Vel[j] = new float[boxed->NumPart];
//boxed->Vel[j] = 0;
mul[j] = 1.0/(ranges[2*j+1] - ranges[2*j+0]);
}
uint32_t k = 0;
NcVar v_uniq_lsb = f.getVar("unique_ids_lsb");
NcVar v_uniq_msb = f.getVar("unique_ids_lsb");
nclong *tmp_int;
tmp_int = new nclong[boxed->NumPart];
v_uniq_lsb.getVar(tmp_int);
for (long i = 0; i < boxed->NumPart; i++)
uniqueID[i] = tmp_int[i];
v_uniq_msb.getVar(tmp_int);
for (long i = 0; i < boxed->NumPart; i++)
uniqueID[i] |= (unsigned long)(tmp_int[i]) << 32;
delete[] tmp_int;
PreselectParticles *preselect = 0;
if (args_info.resubsample_given)
{
preselect = new PreselectParticles(args_info.resubsample_arg, args_info.resubsample_seed_arg);
preselect->reset();
}
if (preselect == 0)
return;
long pid_read = 0, pid_write = 0;
for (int s_id = 0; s_id < *num_snapshots; s_id++)
{
long previous_write = pid_write;
for (long q = 0; q < snapshot_split[s_id]; q++)
{
SingleParticle p;
p.ID = -1;
assert(pid_read < boxed->NumPart);
if (preselect->accept(p))
{
particle_id[pid_write] = particle_id[pid_read];
uniqueID[pid_write] = uniqueID[pid_read];
expansion_fac[pid_write] = expansion_fac[pid_read];
pid_write++;
}
pid_read++;
}
snapshot_split[s_id] = pid_write - previous_write;
}
boxed->NumPart = pid_write;
delete preselect;
cout << "Num accepted here = " << pid_write << " / input = " << pid_read << endl;
}
void makeBoxFromSimulation(SimulationLoader *loader, SimuData* &boxed, MetricFunctor metric, prepSimulation_info& args_info)
{
vector<long> targets, split;
long previous_target_num = 0;
PreselectParticles *preselect = 0;
if (args_info.resubsample_given)
{
preselect = new PreselectParticles(args_info.resubsample_arg, args_info.resubsample_seed_arg);
preselect->reset();
}
for (int nf = 0; nf < loader->num_files(); nf++)
{
SimuData *simu;
double *expfact;
cout << format("Analyzing and selecting targets in file number %d / %d") % (nf+1) % loader->num_files() << endl;
simu = loader->loadFile(nf);
metric(simu, 0);
selectBox(simu, targets, args_info, preselect);
split.push_back(targets.size() - previous_target_num);
previous_target_num = targets.size();
delete simu;
}
createBox(loader->getHeader(), targets, split, boxed, args_info);
if (preselect)
delete preselect;
}
int main(int argc, char **argv)
{
prepSimulation_info args_info;
prepSimulation_conf_params args_params;
SimuData *simu, *simuOut;
SimulationLoader *loader;
prepSimulation_conf_init(&args_info);
prepSimulation_conf_params_init(&args_params);
args_params.check_required = 0;
if (prepSimulation_conf_ext (argc, argv, &args_info, &args_params))
return 1;
if (!args_info.configFile_given)
{
if (prepSimulation_conf_required (&args_info, PREPSIMULATION_CONF_PACKAGE))
return 1;
}
else
{
args_params.check_required = 1;
args_params.initialize = 0;
if (prepSimulation_conf_config_file (args_info.configFile_arg,
&args_info,
&args_params))
return 1;
}
prepSimulation_conf_print_version();
SimulationPreprocessor *preselector = new PreselectParticles(args_info.subsample_arg, args_info.subsample_seed_arg);
if (args_info.ramsesBase_given || args_info.ramsesId_given)
{
if (args_info.ramsesBase_given && args_info.ramsesId_given) {
loader = ramsesLoader(args_info.ramsesBase_arg,
args_info.ramsesId_arg,
true, // double precision with ramses... set this to false if you are dealing with single precision
NEED_POSITION|NEED_VELOCITY|NEED_GADGET_ID, preselector);
}
else
{
cerr << "Both ramsesBase and ramsesId are required to be able to load snapshots" << endl;
return 1;
}
}
else if (args_info.gadget_given)
{
loader = gadgetLoader(args_info.gadget_arg, 1/args_info.gadgetUnit_arg, NEED_POSITION|NEED_VELOCITY|NEED_GADGET_ID, 1, preselector);
}
else if (args_info.gadget2_given)
{
loader = gadgetLoader(args_info.gadget2_arg, 1/args_info.gadgetUnit_arg, NEED_POSITION|NEED_VELOCITY|NEED_GADGET_ID, 2, preselector);
}
else if (args_info.flash_given)
{
loader = flashLoader(args_info.flash_arg, NEED_POSITION|NEED_VELOCITY|NEED_GADGET_ID, preselector);
}
#ifdef SDF_SUPPORT
else if (args_info.multidark_given)
{
loader = multidarkLoader(args_info.multidark_arg, preselector);
}
else if (args_info.sdf_given)
{
loader = sdfLoader(args_info.sdf_arg, NEED_POSITION|NEED_VELOCITY|NEED_GADGET_ID, args_info.sdf_splitting_arg, preselector);
}
#endif
else
{
cerr << "A simulation snapshot is required to generate a mock catalog." << endl;
return 1;
}
if (loader == 0)
{
cerr << "Error while loading " << endl;
return 1;
}
simu = loader->getHeader();
{
SimuData *header = loader->getHeader();
cout << "Hubble = " << header->Hubble << endl;
cout << "Boxsize = " << header->BoxSize << endl;
cout << "Omega_M = " << header->Omega_M << endl;
cout << "Omega_Lambda = " << header->Omega_Lambda << endl;
cout << "Subsample fraction: " << (args_info.subsample_given ? args_info.subsample_arg : 1.0) << endl;
}
double *expfact;
boost::function2<void, SimuData*, double*> metricOperation=
boost::bind(metricTransform, _1, args_info.axis_arg, args_info.preReShift_flag,
args_info.peculiarVelocities_flag, _2,
args_info.cosmo_flag);
if (args_info.inputParameter_given)
makeBoxFromParameter(loader->getHeader(), simuOut, args_info);
else
makeBoxFromSimulation(loader, simuOut, metricOperation, args_info);
// Reset the random number generator
preselector->reset();
long loaded = 0;
for (int nf = 0; nf < loader->num_files(); nf++)
{
long num_targets = simuOut->as<long>("snapshot_split")[nf];
cout << format("Building box from particles in %d / %d") % (nf+1) % loader->num_files() << endl;
if (num_targets == 0)
{
cout << "No particles selected there. Skipping." << endl;
continue;
}
SimuData *simu = loader->loadFile(nf);
double *efac = new double[simu->NumPart];
metricOperation(simu, efac);
buildBox(simu, num_targets, loaded, simuOut, efac);
loaded += num_targets;
assert(loaded <= simuOut->NumPart);
delete simu;
delete[] efac;
}
if (args_info.joggleParticles_flag)
joggleParticles(simuOut);
saveBox(simuOut, args_info.outputParameter_arg, args_info);
generateOutput(simuOut, args_info.axis_arg,
args_info.output_arg);
delete preselector;
double subsample = 1.0;
if (args_info.subsample_given) subsample = args_info.subsample_arg;
if (args_info.resubsample_given) subsample = args_info.resubsample_arg;
printf("Done! %5.2e\n", subsample);
return 0;
}

View file

@ -0,0 +1,45 @@
package "prepSimulation"
version "0"
option "configFile" - "Configuration file path" string optional
# Ramses data
option "ramsesBase" - "Base directory for ramses" string optional
option "ramsesId" - "Ramses snapshot id" int optional
option "gadget" - "Base name of gadget snapshot (without parallel writing extension)" string optional
option "gadget2" - "Base name of gadget snapshot (version 2, without parallel writing extension)" string optional
option "flash" - "Base name for FLASH snapshot" string optional
option "multidark" - "Base name for multidark snapshot" string optional
option "sdf" - "SDF snapshot name" string optional
option "sdf_splitting" - "Number of artificial splitting of the SDF snapshot" int optional default="20"
option "axis" - "Redshift axis (X=0, Y=1, Z=2)" int optional default="2"
option "output" - "Output filename for particles" string required
option "outputParameter" - "Output geometry parameter file for postprocessing" string required
option "rangeX_min" - "Minimum range in X for making the box" double required
option "rangeX_max" - "Maximum range in X for making the box" double required
option "rangeY_min" - "Minimum range in Y for making the box" double required
option "rangeY_max" - "Maximum range in Y for making the box" double required
option "rangeZ_min" - "Minimum range in Z for making the box (after distortion)" double required
option "rangeZ_max" - "Maximum range in Z for making the box (after distortion)" double required
option "preReShift" - "Reshift the zero of the Z axis" flag off
option "peculiarVelocities" - "Added peculiar velocities distortion" flag off
option "cosmo" - "Apply cosmological redshift" flag off
option "subsample" - "Subsample the input simulation by the specified amount" double optional default="1.0"
option "inputParameter" - "Input geometry (optional, warning!)" string optional
option "gadgetUnit" - "Unit of length in gadget file in Mpc/h" double optional default="0.001"
option "subsample_seed" - "Seed for random number generation to select the subsample" int optional default="190524"
option "resubsample" - "Resubsampling factor compared to the subsampled simulation" double optional
option "resubsample_seed" - "Seed for resubsampling from a subsampled simulation" int optional default="20132011"
option "joggleParticles" - "Slightly joggle the input particle positions" flag off