Read info file in Ramses. loadRamsesPosition by snapshot id
This commit is contained in:
parent
1b54cb0953
commit
a41907f4a3
@ -1,3 +1,4 @@
|
||||
#include <regex.h>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
@ -9,6 +10,7 @@
|
||||
#include <fstream>
|
||||
#include "loadRamses.hpp"
|
||||
#include "load_data.h"
|
||||
#include <map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -221,18 +223,90 @@ GadgetData *CosmoTool::loadRamses(const char *name)
|
||||
return gd;
|
||||
}
|
||||
|
||||
PurePositionData *CosmoTool::loadRamsesPosition(const char *name)
|
||||
typedef struct
|
||||
{
|
||||
double unitLength;
|
||||
double aexp;
|
||||
double boxSize;
|
||||
} InfoData;
|
||||
|
||||
int readInfoFile(const char *basename, int outputId, InfoData& info)
|
||||
{
|
||||
ostringstream ss_fname;
|
||||
ss_fname << basename << "/info_" << setfill('0') << setw(5) << outputId << ".txt";
|
||||
|
||||
cout << "Opening info file " << ss_fname.str() << 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);
|
||||
|
||||
info.unitLength = infoMap["unit_l"];
|
||||
info.aexp = infoMap["aexp"];
|
||||
info.boxSize = infoMap["boxlen"];
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
PurePositionData *CosmoTool::loadRamsesPosition(const char *basename, int outputId)
|
||||
{
|
||||
PurePositionData *gd = (PurePositionData *)malloc(sizeof(PurePositionData));
|
||||
int id = 1;
|
||||
uint32_t totPart = 0;
|
||||
int nCpu = 0;
|
||||
InfoData info;
|
||||
|
||||
static const double CM_IN_MPC = 3.08e24;
|
||||
|
||||
if (!readInfoFile(basename, outputId, info))
|
||||
return 0;
|
||||
|
||||
double L0 = info.boxSize*info.unitLength/CM_IN_MPC/info.aexp;
|
||||
cout << "L0=" << L0 << " Mpc" << endl;
|
||||
|
||||
cout << "Detecting number of files and particles..." << endl;
|
||||
while (1)
|
||||
{
|
||||
ostringstream ss_fname;
|
||||
ss_fname << name << setfill('0') << setw(5) << id;
|
||||
ss_fname << basename << "/part_" << setfill('0') << setw(5) << outputId << ".out" << setfill('0') << setw(5) << id;
|
||||
string fname = ss_fname.str();
|
||||
|
||||
cout << " ... " << fname << endl;
|
||||
@ -267,7 +341,7 @@ PurePositionData *CosmoTool::loadRamsesPosition(const char *name)
|
||||
gd->pos = (FCoordinates *)malloc(sizeof(FCoordinates)*totPart);
|
||||
assert(gd->pos != 0);
|
||||
gd->NumPart = totPart;
|
||||
gd->BoxSize = 1000.0;
|
||||
gd->BoxSize = L0*1000;
|
||||
|
||||
cout << " Total number part=" << totPart << endl
|
||||
<< "Loading particles ..." << endl;
|
||||
@ -277,7 +351,8 @@ PurePositionData *CosmoTool::loadRamsesPosition(const char *name)
|
||||
while (1)
|
||||
{
|
||||
ostringstream ss_fname;
|
||||
ss_fname << name << setfill('0') << setw(5) << id;
|
||||
ss_fname << basename << "/part_" << setfill('0') << setw(5) << outputId << ".out" << setfill('0') << setw(5) << id;
|
||||
|
||||
string fname = ss_fname.str();
|
||||
int *idP;
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
namespace CosmoTool {
|
||||
|
||||
GadgetData *loadRamses(const char *name);
|
||||
PurePositionData *loadRamsesPosition(const char *fname);
|
||||
PurePositionData *loadRamsesPosition(const char *fname, int id);
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user