cosmotool/src/loadGadget.cpp

384 lines
8.9 KiB
C++
Raw Normal View History

2013-03-03 00:42:56 +01:00
/*+
2014-05-22 09:38:59 +02:00
This is CosmoTool (./src/loadGadget.cpp) -- Copyright (C) Guilhem Lavaux (2007-2014)
2013-03-03 00:42:56 +01:00
guilhem.lavaux@gmail.com
This software is a computer program whose purpose is to provide a toolbox for cosmological
data analysis (e.g. filters, generalized Fourier transforms, power spectra, ...)
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty and the software's author, the holder of the
economic rights, and the successive licensors have only limited
liability.
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
+*/
2013-03-06 04:39:33 +01:00
#include <cmath>
2010-09-12 21:09:39 +02:00
#include <iostream>
2010-04-23 09:29:10 +02:00
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "load_data.hpp"
#include "loadGadget.hpp"
#include "fortran.hpp"
using namespace CosmoTool;
2010-09-12 21:09:39 +02:00
using namespace std;
2010-04-23 09:29:10 +02:00
void loadGadgetHeader(UnformattedRead *f, GadgetHeader& h, SimuData *data, int id)
2010-04-23 09:29:10 +02:00
{
f->beginCheckpoint();
2010-04-23 09:29:10 +02:00
for (int i = 0; i < 6; i++)
h.npart[i] = f->readInt32();
2010-04-23 09:29:10 +02:00
for (int i = 0; i < 6; i++)
h.mass[i] = f->readReal64();
data->time = h.time = f->readReal64();
h.redshift = f->readReal64();
h.flag_sfr = f->readInt32();
h.flag_feedback = f->readInt32();
2010-04-23 09:29:10 +02:00
for (int i = 0; i < 6; i++)
h.npartTotal[i] = f->readInt32();
h.flag_cooling = f->readInt32();
h.num_files = f->readInt32();
data->BoxSize = h.BoxSize = f->readReal64();
data->Omega_M = h.Omega0 = f->readReal64();
data->Omega_Lambda = h.OmegaLambda = f->readReal64();
data->Hubble = h.HubbleParam = f->readReal64();
f->endCheckpoint(true);
2010-04-23 09:29:10 +02:00
long NumPart = 0, NumPartTotal = 0;
for(int k=0; k<6; k++)
{
NumPart += h.npart[k];
NumPartTotal += (id < 0) ? h.npart[k] : h.npartTotal[k];
2010-04-23 09:29:10 +02:00
}
data->NumPart = NumPart;
data->TotalNumPart = NumPartTotal;
2010-04-23 09:29:10 +02:00
}
SimuData *CosmoTool::loadGadgetMulti(const char *fname, int id,
int loadflags, int GadgetFormat,
SimuFilter filter)
{
SimuData *data;
int p, n;
UnformattedRead *f;
GadgetHeader h;
float velmul;
if (id >= 0) {
2010-08-03 17:24:40 +02:00
int k = snprintf(0, 0, "%s.%d", fname, id)+1;
char *out_fname = new char[k];
snprintf(out_fname, k, "%s.%d", fname, id);
2011-06-06 15:43:38 +02:00
f = new UnformattedRead(out_fname);
if (f == 0)
return 0;
delete[] out_fname;
} else {
f = new UnformattedRead(fname);
if (f == 0)
return 0;
}
data = new SimuData;
if (data == 0) {
delete f;
return 0;
}
long NumPart = 0, NumPartTotal = 0;
2010-09-12 21:09:39 +02:00
try
{
loadGadgetHeader(f, h, data, id);
if (GadgetFormat == 1)
velmul = sqrt(h.time);
else if (GadgetFormat == 2)
velmul = 1/(h.time);
else {
cerr << "unknown gadget format" << endl;
abort();
}
NumPart = data->NumPart;
NumPartTotal = data->TotalNumPart;
2010-09-12 21:09:39 +02:00
}
catch (const InvalidUnformattedAccess& e)
{
cerr << "Invalid format while reading header" << endl;
delete data;
delete f;
return 0;
}
if (loadflags & NEED_TYPE)
{
2010-09-12 21:09:39 +02:00
int p = 0;
data->type = new int[data->NumPart];
for (int k = 0; k < 6; k++)
for (int n = 0; n < h.npart[k]; n++,p++)
data->type[p] = k;
}
if (loadflags & NEED_POSITION) {
for (int i = 0; i < 3; i++) {
data->Pos[i] = new float[data->NumPart];
if (data->Pos[i] == 0) {
delete data;
return 0;
}
}
2010-09-12 21:09:39 +02:00
try
{
f->beginCheckpoint();
for(int k = 0, p = 0; k < 6; k++) {
for(int n = 0; n < h.npart[k]; n++) {
data->Pos[0][p] = f->readReal32();
data->Pos[1][p] = f->readReal32();
data->Pos[2][p] = f->readReal32();
p++;
}
}
f->endCheckpoint();
}
catch (const InvalidUnformattedAccess& e)
{
cerr << "Invalid format while reading positions" << endl;
delete f;
delete data;
return 0;
}
} else {
// Skip positions
f->skip(NumPart * 3 * sizeof(float) + 2*4);
}
if (loadflags & NEED_VELOCITY) {
for (int i = 0; i < 3; i++)
{
data->Vel[i] = new float[data->NumPart];
if (data->Vel[i] == 0)
{
2010-09-12 21:09:39 +02:00
delete f;
delete data;
return 0;
}
}
2010-09-12 21:09:39 +02:00
try
{
f->beginCheckpoint();
for(int k = 0, p = 0; k < 6; k++) {
for(int n = 0; n < h.npart[k]; n++) {
// THIS IS GADGET 1
data->Vel[0][p] = f->readReal32()*velmul;
data->Vel[1][p] = f->readReal32()*velmul;
data->Vel[2][p] = f->readReal32()*velmul;
2010-09-12 21:09:39 +02:00
p++;
}
}
f->endCheckpoint();
}
catch (const InvalidUnformattedAccess& e)
{
cerr << "Invalid format while reading velocities" << endl;
delete f;
delete data;
return 0;
}
// THE VELOCITIES ARE IN PHYSICAL COORDINATES
/// // TODO: FIX THE UNITS OF THESE FUNKY VELOCITIES !!!
} else {
// Skip velocities
f->skip(NumPart*3*sizeof(float)+2*4);
}
// Skip ids
if (loadflags & NEED_GADGET_ID) {
2010-09-12 21:09:39 +02:00
try
{
2010-09-12 21:09:39 +02:00
f->beginCheckpoint();
data->Id = new long[data->NumPart];
2010-09-12 21:09:39 +02:00
if (data->Id == 0)
{
2010-09-12 21:09:39 +02:00
delete f;
delete data;
return 0;
}
2010-09-12 21:09:39 +02:00
for(int k = 0, p = 0; k < 6; k++)
{
for(int n = 0; n < h.npart[k]; n++)
{
data->Id[p] = f->readInt32();
p++;
}
}
f->endCheckpoint();
}
catch (const InvalidUnformattedAccess& e)
{
2012-11-10 14:59:10 +01:00
cerr << "Invalid unformatted access while reading ID" << endl;
2010-09-12 21:09:39 +02:00
delete f;
delete data;
return 0;
}
} else {
f->skip(2*4);
for (int k = 0; k < 6; k++)
f->skip(h.npart[k]*4);
}
2014-07-05 22:05:04 +02:00
if (loadflags & NEED_MASS) {
try
{
long l = 0;
f->beginCheckpoint();
data->Mass = new float[NumPart];
for (int k = 0; k < 6; k++)
{
if (h.mass[k] == 0) {
for(int n = 0; n < h.npart[k]; n++)
{
data->Mass[l++] = f->readReal32();
}
} else {
for(int n = 0; n < h.npart[k]; n++)
{
data->Mass[l++] = h.mass[k];
}
}
}
f->endCheckpoint();
}
catch (const InvalidUnformattedAccess& e)
{
cerr << "Invalid unformatted access while reading ID" << endl;
delete f;
delete data;
return 0;
}
} else {
f->skip(2*4);
for (int k = 0; k < 6; k++)
if (h.mass[k] == 0)
f->skip(h.npart[k]*4);
}
delete f;
return data;
}
2012-03-30 17:47:48 +02:00
void CosmoTool::writeGadget(const std::string& fname, SimuData *data, int GadgetFormat)
2012-03-30 17:47:48 +02:00
{
UnformattedWrite *f;
int npart[6], npartTotal[6];
2012-03-30 17:47:48 +02:00
float mass[6];
if (data->Pos[0] == 0 || data->Vel[0] == 0 || data->Id == 0) {
cerr << "Invalid simulation data array" << endl;
2012-03-30 17:47:48 +02:00
return;
}
2012-03-30 17:47:48 +02:00
f = new UnformattedWrite(fname);
if (f == 0) {
cerr << "Cannot create file" << endl;
2012-03-30 17:47:48 +02:00
return;
}
2012-03-30 17:47:48 +02:00
for (int i = 0; i < 6; i++)
{
npart[i] = npartTotal[i] = 0;
2012-03-30 17:47:48 +02:00
mass[i] = 0;
}
2012-04-02 00:49:19 +02:00
mass[1] = 1.0;
2012-03-30 17:47:48 +02:00
npart[1] = data->NumPart;
npartTotal[1] = data->TotalNumPart;
2012-03-30 17:47:48 +02:00
f->beginCheckpoint();
for (int i = 0; i < 6; i++)
f->writeInt32(npart[i]);
for (int i = 0; i < 6; i++)
f->writeReal64(mass[i]);
f->writeReal64(data->time);
f->writeReal64(1/data->time-1);
f->writeInt32(0);
f->writeInt32(0);
for (int i = 0; i < 6; i++)
f->writeInt32(npartTotal[i]);
2012-03-30 17:47:48 +02:00
f->writeInt32(0);
f->writeInt32(1);
f->writeReal64(data->BoxSize);
f->writeReal64(data->Omega_M);
f->writeReal64(data->Omega_Lambda);
f->writeReal64(data->Hubble);
2012-04-02 00:49:19 +02:00
char buf[100] = { 0, };
f->writeOrderedBuffer(buf, 96);
2012-03-30 17:47:48 +02:00
f->endCheckpoint();
f->beginCheckpoint();
for(int n = 0; n < data->NumPart; n++) {
for (int k = 0; k < 3; k++)
f->writeReal32(data->Pos[k][n]);
}
f->endCheckpoint();
float velmul = 1.0;
if (GadgetFormat == 1)
velmul = sqrt(data->time);
f->beginCheckpoint();
for(int n = 0; n < data->NumPart; n++) {
for (int k = 0; k < 3; k++)
f->writeReal32(data->Vel[k][n]/velmul);
}
f->endCheckpoint();
f->beginCheckpoint();
for(int n = 0; n < data->NumPart; n++)
{
2012-04-02 00:49:19 +02:00
f->writeInt32(data->Id[n]);
2012-03-30 17:47:48 +02:00
}
f->endCheckpoint();
2012-04-02 00:49:19 +02:00
delete f;
2012-03-30 17:47:48 +02:00
}