#include #include #include #include #include #include #include #include #include #include #include "loadRamses.hpp" #include "load_data.hpp" #include "fortran.hpp" #include using namespace std; CosmoTool::GadgetData *CosmoTool::loadRamses(const char *name) { GadgetData *gd = (GadgetData *)malloc(sizeof(GadgetData)); int id = 1; uint32_t totPart = 0; cout << "Detecting number of files and particles..." << endl; while (1) { ostringstream ss_fname; ss_fname << name << setfill('0') << setw(5) << id; string fname = ss_fname.str(); cout << " ... " << fname << endl; try { UnformattedRead infile(fname); int nCpu, ndim, nPar; infile.beginCheckpoint(); nCpu = infile.readInt32(); infile.endCheckpoint(); infile.beginCheckpoint(); ndim = infile.readInt32(); infile.endCheckpoint(); infile.beginCheckpoint(); nPar = infile.readInt32(); infile.endCheckpoint(); cout << " NUMCPU=" << nCpu << " NUMDIM=" << ndim << " NumPart=" << nPar << endl; totPart += nPar; id++; } catch (const NoSuchFileException& e) { break; } } assert (totPart <= ((~(size_t)0)/sizeof(ParticleState))); size_t memSize = sizeof(ParticleState)*(size_t)totPart; cout << " Needing " << memSize / 1024 << " kBytes" << endl; gd->particles = (ParticleState *)malloc(memSize); assert(gd->particles != 0); gd->NumPart = totPart; gd->ntot_withmasses = totPart; gd->header.npart[0] = totPart; for (int k = 1; k < 6; k++) { gd->header.npart[k] = 0; gd->header.mass[k] = 0; } gd->header.num_files = id; gd->header.BoxSize = 200.0 * 1000; // kPc gd->header.Omega0 = 0.30; // ???? gd->header.OmegaLambda = 0.70; // ???? gd->header.HubbleParam = 0.70; // ???? cout << " Total number part=" << totPart << endl << "Loading particles ..." << endl; uint32_t curPos = 0; id = 1; while (1) { ostringstream ss_fname; ss_fname << name << setfill('0') << setw(5) << id; string fname = ss_fname.str(); cout << " ... " << id; cout.flush(); try { UnformattedRead infile(fname); int nCpu, ndim, nPar; infile.beginCheckpoint(); nCpu = infile.readInt32(); infile.endCheckpoint(); infile.beginCheckpoint(); ndim = infile.readInt32(); infile.endCheckpoint(); infile.beginCheckpoint(); nPar = infile.readInt32(); infile.endCheckpoint(); ParticleState *s = &gd->particles[curPos]; for (uint32_t i = nPar; i > 0; i--,s++) s->Mass = 1.0; s = &gd->particles[curPos]; infile.beginCheckpoint(); for (uint32_t i = nPar; i > 0; i--) { s->Pos[0] = infile.readReal32(); s->Pos[0] *= gd->header.BoxSize; s++; } infile.endCheckpoint(); s = &gd->particles[curPos]; infile.beginCheckpoint(); for (uint32_t i = nPar; i > 0; i--) { s->Pos[1] = infile.readReal32(); s->Pos[1] *= gd->header.BoxSize; s++; } infile.endCheckpoint(); s = &gd->particles[curPos]; infile.beginCheckpoint(); for (uint32_t i = nPar; i > 0; i--) { s->Pos[2] = infile.readReal32(); s->Pos[2] *= gd->header.BoxSize; s++; } infile.endCheckpoint(); infile.beginCheckpoint(); for (uint32_t i = 0; i < nPar; i++) { gd->particles[curPos+i].Vel[0] = infile.readReal32(); } infile.endCheckpoint(); infile.beginCheckpoint(); for (uint32_t i = 0; i < nPar; i++) { gd->particles[curPos+i].Vel[1] = infile.readReal32(); } infile.endCheckpoint(); infile.beginCheckpoint(); for (uint32_t i = 0; i < nPar; i++) { gd->particles[curPos+i].Vel[2] = infile.readReal32(); } infile.endCheckpoint(); curPos += nPar; } catch (const NoSuchFileException& e) { break; } id++; } cout << endl; return gd; } 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 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; } CosmoTool::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 << basename << "/part_" << setfill('0') << setw(5) << outputId << ".out" << setfill('0') << setw(5) << id; string fname = ss_fname.str(); cout << " ... " << fname << endl; try { UnformattedRead infile(fname); int nCpu, ndim, nPar; infile.beginCheckpoint(); nCpu = infile.readInt32(); infile.endCheckpoint(); infile.beginCheckpoint(); ndim = infile.readInt32(); infile.endCheckpoint(); infile.beginCheckpoint(); nPar = infile.readInt32(); infile.endCheckpoint(); cout << " NUMCPU=" << nCpu << " NUMDIM=" << ndim << " NumPart=" << nPar << endl; totPart += nPar; id++; } catch (const NoSuchFileException& e) { break; } } assert (totPart <= ((~(size_t)0)/sizeof(FCoordinates))); size_t memSize = sizeof(FCoordinates)*(size_t)(totPart+totPart/nCpu); cout << " Needing " << memSize / 1024 << " kBytes" << endl; gd->pos = (FCoordinates *)malloc(sizeof(FCoordinates)*totPart); assert(gd->pos != 0); gd->NumPart = totPart; gd->BoxSize = L0*1000; cout << " Total number part=" << totPart << endl << "Loading particles ..." << endl; uint32_t curPos = 0; id = 1; while (1) { ostringstream ss_fname; ss_fname << basename << "/part_" << setfill('0') << setw(5) << outputId << ".out" << setfill('0') << setw(5) << id; string fname = ss_fname.str(); int *idP; cout << " ... " << id; cout.flush(); try { UnformattedRead infile(fname); int nCpu, ndim, nPar; infile.beginCheckpoint(); nCpu = infile.readInt32(); infile.endCheckpoint(); infile.beginCheckpoint(); ndim = infile.readInt32(); infile.endCheckpoint(); infile.beginCheckpoint(); nPar = infile.readInt32(); infile.endCheckpoint(); FCoordinates *s = new FCoordinates[nPar]; for (int k = 0; k < 3; k++) { infile.beginCheckpoint(); for (uint32_t i = 0; i < nPar; i++) { s[i][k] = infile.readReal32(); s[i][k] *= gd->BoxSize; } infile.endCheckpoint(); } // SKIP VELOCITIES for (int k = 0; k < 3; k++) { infile.beginCheckpoint(); for (uint32_t i = nPar; i > 0; i--) { infile.readReal32(); } infile.endCheckpoint(); } float minMass = INFINITY; infile.beginCheckpoint(); for (uint32_t i = nPar; i > 0; i--) { float dummyF = infile.readReal32(); if (dummyF < minMass) minMass = dummyF; } infile.endCheckpoint(); infile.beginCheckpoint(); for (uint32_t i = 0; i < nPar; i++) { int id = infile.readInt32(); id--; assert(id >= 0); assert(id < totPart); memcpy(&gd->pos[id], &s[i], sizeof(FCoordinates)); } infile.endCheckpoint(); delete[] s; curPos += nPar; } catch (const NoSuchFileException& e) { break; } id++; } cout << endl; return gd; } #if 0 int main(int argc, char **argv) { GadgetData *gd = loadRamses(argv[1]); } #endif #if 0 int main(int argc, char **argv) { GadgetData *gd = loadRamses(argv[1]); } #endif