static dataVarType_t local_translateBovTypeToGridType(bovFormat_t typeInBov) { dataVarType_t type; if (typeInBov == BOV_FORMAT_INT) { type = DATAVARTYPE_INT; } else if (typeInBov == BOV_FORMAT_BYTE) { type = DATAVARTYPE_INT8; } else if (typeInBov == BOV_FORMAT_FLOAT) { if (dataVarType_isNativeFloat(DATAVARTYPE_FPV)) { type = DATAVARTYPE_FPV; } else { fprintf(stderr, "There is no float capacity in the grid :("); diediedie(EXIT_FAILURE); } } else if (typeInBov == BOV_FORMAT_DOUBLE) { type = DATAVARTYPE_DOUBLE; } else { fprintf(stderr, "Data format of bov not supported :(\n"); diediedie(EXIT_FAILURE); } return type; }
/*--- Implementations of local functions --------------------------------*/ static void local_handleFilenameChange(gridReader_t reader) { assert(reader != NULL); assert(reader->type == GRIDIO_TYPE_HDF5); const char *fileName = filename_getFullName(reader->fileName); int res = H5Fis_hdf5(fileName); if (res <= 0) { if (res == 0) { fprintf(stderr, "ERROR: %s does not seem to be an HDF5 file.\n", fileName); } else { fprintf(stderr, "ERROR: Failed to check whether %s is a HDF5 file.\n", fileName); } diediedie(EXIT_FAILURE); } hid_t file = H5Fopen(fileName, H5F_ACC_RDONLY, H5P_DEFAULT); if (file < 0) { fprintf(stderr, "ERROR: Could not open %s for reading.\n", fileName); diediedie(EXIT_FAILURE); } gridReaderHDF5_setH5File((gridReaderHDF5_t)reader, file); }
/*--- Implementations of exported functios ------------------------------*/ extern cosmoPk_t cosmoPk_newFromFile(const char *fname) { cosmoPk_t pk; FILE *f; assert(fname != NULL); pk = local_new(); f = xfopen(fname, "r"); if (fscanf(f, "# %" SCNu32 " %lf %lf \n", &(pk->numPoints), &(pk->slopeBeforeKmin), &(pk->slopeBeyondKmax)) != 3) { diediedie(EXIT_FAILURE); } local_getMem(pk, pk->numPoints); for (uint32_t i = 0; i < pk->numPoints; i++) { if (fscanf(f, " %lf %lf \n", pk->k + i, pk->P + i) != 2) { diediedie(EXIT_FAILURE); } } xfclose(&f); local_doInterpolation(pk); return pk; }
static gridWriter_t local_getWriter(parse_ini_t ini, const char *secName, gridIO_type_t type) { gridWriter_t writer; if (type == GRIDIO_TYPE_SILO) { #ifdef WITH_SILO writer = gridWriterFactory_newFromIniSilo(ini, secName); #else fprintf(stderr, "To use Silo output, run configure using --with-silo\n"); diediedie(EXIT_FAILURE); #endif } else if (type == GRIDIO_TYPE_GRAFIC) { writer = gridWriterFactory_newFromIniGrafic(ini, secName); } else if (type == GRIDIO_TYPE_HDF5) { #ifdef WITH_HDF5 writer = gridWriterFactory_newFromIniHDF5(ini, secName); #else fprintf(stderr, "To use HF5 output, run configure using --with-hdf5\n"); diediedie(EXIT_FAILURE); #endif } else { fprintf(stderr, "Cannot create writer for %s\n", gridIO_getNameFromType(type)); diediedie(EXIT_FAILURE); } return writer; }
/* * Check magic value and current state; bail if it isn't valid. * * This is designed to catch serious programming errors that violate * the libarchive API. */ void __archive_check_magic(struct archive *a, unsigned int magic, unsigned int state, const char *function) { if (a->magic != magic) { errmsg("INTERNAL ERROR: Function "); errmsg(function); errmsg(" invoked with invalid struct archive structure.\n"); diediedie(); } if (state == ARCHIVE_STATE_ANY) return; if ((a->state & state) == 0) { errmsg("INTERNAL ERROR: Function '"); errmsg(function); errmsg("' invoked with archive structure in state '"); write_all_states(a->state); errmsg("', should be in state '"); write_all_states(state); errmsg("'\n"); diediedie(); } }
static uint64_t local_actOnSingleFile(cubepm_t cubepm, uint64_t pSkip, uint64_t pAct, stai_t *data) { bool doByteswap = false; float partData[6]; if (endian_getSystemEndianess() != cubepm_getFileEndianess(cubepm)) doByteswap = true; local_seekToParticle(cubepm, pSkip); if (cubepm_getMode(cubepm) == CUBEPM_MODE_READ) { int ngrid = cubepm_getNGrid(cubepm); for (uint64_t i = UINT64_C(0); i < pAct; i++) { local_readParticle(cubepm->f, partData, doByteswap); local_applyOffsetToParticle(partData, cubepm->fileCoordOffset); local_applyPeriodicityToParticle(partData, ngrid); local_copyParticleDataToStais(partData, i, data); } } else if (cubepm_getMode(cubepm) == CUBEPM_MODE_WRITE) { for (uint64_t i = UINT64_C(0); i < pAct; i++) { local_fillParticleDataFromStais(partData, i, data); local_unapplyOffsetToParticle(partData, cubepm->fileCoordOffset); local_writeParticle(cubepm->f, partData, doByteswap); } } else { diediedie(EXIT_FAILURE); } return pAct; }
extern void art_open(art_t art, artMode_t mode, int numFile) { assert(art != NULL); assert(numFile >= 0 && numFile < art->numFiles); if (art->f != NULL) { if ((art->lastOpened != numFile) || (mode != art->mode)) xfclose(&(art->f)); else if ((art->lastOpened == numFile) && (mode == art->mode)) { rewind(art->f); return; } } art->mode = mode; if (art->mode == ART_MODE_READ) { art->f = xfopen(art->fileNamesData[numFile], "rb"); } else if (art->mode == ART_MODE_WRITE) { art->f = xfopen(art->fileNamesData[numFile], "r+b"); } else { diediedie(EXIT_FAILURE); } art->lastOpened = numFile; art->numPagesInThisFile = (numFile == art->numFiles - 1) ? art->numParticlesInLastPage : art->numParticlesInPage; art->numParticlesInThisFile = (numFile == art->numFiles - 1) ? art->numParticlesInLastFile : art->numParticlesInFile; }
extern gridWriter_t gridWriterFactory_newFromIniSilo(parse_ini_t ini, const char *sectionName) { assert(ini != NULL); assert(sectionName != NULL); gridWriterSilo_t writer = gridWriterSilo_new(); char *dbTypeStr; int dbType; int numFiles; if (parse_ini_get_string(ini, "dbType", sectionName, &dbTypeStr)) { if (strcmp(dbTypeStr, "DB_PDB") == 0) dbType = DB_PDB; else if (strcmp(dbTypeStr, "DB_HDF5") == 0) dbType = DB_HDF5; else { fprintf(stderr, "Unknown or unsupported Silo DB type: %s\n", dbTypeStr); diediedie(EXIT_FAILURE); } xfree(dbTypeStr); gridWriterSilo_setDbType(writer, dbType); } if (parse_ini_get_int32(ini, "numFiles", sectionName, &numFiles)) gridWriterSilo_setNumFiles(writer, numFiles); return (gridWriter_t)writer; }
static void local_getMem(cosmoPk_t pk, uint32_t numPoints) { if (numPoints < LOCAL_MINPOINTS) { fprintf(stderr, "P(k) needs to have at least %i points!\n", LOCAL_MINPOINTS); diediedie(EXIT_FAILURE); } if (pk->k != NULL) xfree(pk->k); pk->k = xmalloc(sizeof(double) * numPoints * 2); pk->P = pk->k + numPoints; pk->numPoints = numPoints; }
static void local_copyBufferToStai(const float *buffer, stai_t stai, int numValues) { if (stai_getSizeOfElementInBytes(stai) == sizeof(float)) { stai_setElementsMulti(stai, 0, buffer, numValues); } else if (stai_getSizeOfElementInBytes(stai) == sizeof(double)) { double d; for (int i = 0; i < numValues; i++) { d = (double)(buffer[i]); stai_setElement(stai, i, &d); } } else { diediedie(EXIT_FAILURE); } }
static void local_fillBufferFromStai(float *buffer, stai_t stai, int numValues) { if (stai_getSizeOfElementInBytes(stai) == sizeof(float)) { stai_getElementsMulti(stai, 0, buffer, numValues); } else if (stai_getSizeOfElementInBytes(stai) == sizeof(double)) { double d; for (int i = 0; i < numValues; i++) { stai_getElement(stai, i, &d); buffer[i] = (float)d; } } else { diediedie(EXIT_FAILURE); } }
/* * Check magic value and current state. * Magic value mismatches are fatal and result in calls to abort(). * State mismatches return ARCHIVE_FATAL. * Otherwise, returns ARCHIVE_OK. * * This is designed to catch serious programming errors that violate * the libarchive API. */ int __archive_check_magic(struct archive *a, unsigned int magic, unsigned int state, const char *function) { char states1[64]; char states2[64]; const char *handle_type; /* * If this isn't some form of archive handle, * then the library user has screwed up so bad that * we don't even have a reliable way to report an error. */ handle_type = archive_handle_type_name(a->magic); if (!handle_type) { errmsg("PROGRAMMER ERROR: Function "); errmsg(function); errmsg(" invoked with invalid archive handle.\n"); diediedie(); } if (a->magic != magic) { archive_set_error(a, -1, "PROGRAMMER ERROR: Function '%s' invoked" " on '%s' archive object, which is not supported.", function, handle_type); a->state = ARCHIVE_STATE_FATAL; return (ARCHIVE_FATAL); } if ((a->state & state) == 0) { /* If we're already FATAL, don't overwrite the error. */ if (a->state != ARCHIVE_STATE_FATAL) archive_set_error(a, -1, "INTERNAL ERROR: Function '%s' invoked with" " archive structure in state '%s'," " should be in state '%s'", function, write_all_states(states1, a->state), write_all_states(states2, state)); a->state = ARCHIVE_STATE_FATAL; return (ARCHIVE_FATAL); } return ARCHIVE_OK; }
extern void gridReaderHDF5_readIntoPatchForVar(gridReader_t reader, gridPatch_t patch, int idxOfVar) { assert(reader != NULL); assert(reader->type = GRIDIO_TYPE_HDF5); assert(patch != NULL); assert(idxOfVar >= 0 && idxOfVar < gridPatch_getNumVars(patch)); hid_t dataSet; hid_t dataSpaceFile, dataTypeFile; hid_t dataSpacePatch, dataTypePatch; gridPointUint32_t idxLoPatch, dimsPatch; dataVar_t var = gridPatch_getVarHandle(patch, idxOfVar); void *data = gridPatch_getVarDataHandle(patch, idxOfVar); gridPatch_getIdxLo(patch, idxLoPatch); gridPatch_getDims(patch, dimsPatch); dataSet = H5Dopen(((gridReaderHDF5_t)reader)->file, dataVar_getName(var), H5P_DEFAULT); dataTypeFile = H5Dget_type(dataSet); dataSpaceFile = H5Dget_space(dataSet); dataTypePatch = dataVar_getHDF5Datatype(var); dataSpacePatch = gridUtilHDF5_getDataSpaceFromDims(dimsPatch); gridUtilHDF5_selectHyperslab(dataSpaceFile, idxLoPatch, dimsPatch); if (H5Tequal(dataTypeFile, dataTypePatch)) { H5Dread(dataSet, dataTypeFile, dataSpacePatch, dataSpaceFile, H5P_DEFAULT, data); } else { fprintf(stderr, "ERROR: Datatype in memory differs from file.\n"); diediedie(EXIT_FAILURE); } H5Sclose(dataSpacePatch); H5Tclose(dataTypePatch); H5Sclose(dataSpaceFile); H5Tclose(dataTypeFile); H5Dclose(dataSet); } /* gridReaderHDF5_readIntoPatchForVar */
static void local_copyParticleDataToStais(const float *partData, uint64_t pos, stai_t *data) { for (int i = 0; i < 6; i++) { if (data[i] != NULL) { if (stai_getSizeOfElementInBytes(data[i]) == sizeof(float)) { stai_setElement(data[i], pos, partData + i); } else if (stai_getSizeOfElementInBytes(data[i]) == sizeof(double)) { double d = (double)(partData[i]); stai_setElement(data[i], pos, &d); } else { diediedie(EXIT_FAILURE); } } } }
extern gridWriter_t gridWriterFactory_newFromIniHDF5(parse_ini_t ini, const char *sectionName) { assert(ini != NULL); assert(sectionName != NULL); gridWriterHDF5_t writer; bool tmp, doChunking, doChecksum, doCompression; writer = gridWriterHDF5_new(); tmp = parse_ini_get_bool(ini, "doChunking", sectionName, &doChunking); if (tmp && doChunking) { int32_t *sizeFile; gridPointUint32_t sizeCode; if (!parse_ini_get_int32list(ini, "chunkSize", sectionName, NDIM, (int32_t **)&sizeFile)) { fprintf(stderr, "Could not get chunkSize from section %s.\n", sectionName); diediedie(EXIT_FAILURE); } for (int i = 0; i < NDIM; i++) sizeCode[i] = sizeFile[i]; xfree(sizeFile); gridWriterHDF5_setChunkSize(writer, sizeCode); } if (parse_ini_get_bool(ini, "doChecksum", sectionName, &doChecksum)) gridWriterHDF5_setDoChecksum(writer, doChecksum); tmp = parse_ini_get_bool(ini, "doCompression", sectionName, &doCompression); if (tmp && doCompression) { char *filterName; getFromIni(&filterName, parse_ini_get_string, ini, "filterName", sectionName); } return (gridWriter_t)writer; } /* gridWriterFactory_newFromIniHDF5 */
static bovFormat_t local_translateGridTypeToBovType(dataVarType_t type) { bovFormat_t typeAsBovType; if (type == DATAVARTYPE_INT) { typeAsBovType = BOV_FORMAT_INT; } else if (type == DATAVARTYPE_INT8) { typeAsBovType = BOV_FORMAT_BYTE; } else if (type == DATAVARTYPE_DOUBLE) { typeAsBovType = BOV_FORMAT_DOUBLE; } else if (type == DATAVARTYPE_FPV) { if (dataVarType_isNativeFloat(type)) typeAsBovType = BOV_FORMAT_FLOAT; else typeAsBovType = BOV_FORMAT_DOUBLE; } else { fprintf(stderr, "Grid type not compatible with bov type."); diediedie(EXIT_FAILURE); } return typeAsBovType; }
static void local_readHeaderActual(artHeader_t header, FILE *f) { uint32_t b1, b2; endian_t systemEndianess = endian_getSystemEndianess(); xfread(&b1, sizeof(uint32_t), 1, f); xfread(header->headerString, sizeof(char), ARTHEADER_HEADERSTRING_LENGTH, f); header->headerString[ARTHEADER_HEADERSTRING_LENGTH] = '\0'; xfread(&(header->aexpn), sizeof(float), 121, f); xfread(&b2, sizeof(uint32_t), 1, f); if (b1 != b2) { diediedie(EXIT_FAILURE); } if (systemEndianess != header->fileEndianess) local_byteswapHeader(header); local_calcFactorWeight(header); local_calcFactorPosition(header); local_calFactorVelocity(header); }
/** * @brief The main function. * * The function will set-up the environment, read an ART header file and * pretty print that on the screen. * * @param[in] argc * The number of command line arguments. * @param[in] **argv * Array of command line arguments. * * @return The function will return EXIT_SUCCESS if everything went * fine or EXIT_FAILURE in the case of errors (which should be * cleanly announced on stderr). */ int main(int argc, char **argv) { cubepm_t cubepm; double *data; stai_t dataDesc[6] = {NULL, NULL, NULL, NULL, NULL, NULL}; uint64_t actualRead; local_registerCleanUpFunctions(); local_initEnvironment(&argc, &argv); cubepm = cubepm_new(localPathToFiles, localFileNameStem, localNodesDim, localNGrid); cubepm_initHeaderValuesFromFiles(cubepm); cubepm_setBoxsizeInMpch(cubepm, localBoxsize); cubepm_setOmega0(cubepm, localOmega0); cubepm_setLambda0(cubepm, localLambda0); data = xmalloc(sizeof(double) * localPartsRead * 6); dataDesc[0] = stai_new(data + 0, sizeof(double), 6 * sizeof(double)); dataDesc[1] = stai_new(data + 1, sizeof(double), 6 * sizeof(double)); dataDesc[2] = stai_new(data + 2, sizeof(double), 6 * sizeof(double)); dataDesc[3] = stai_new(data + 3, sizeof(double), 6 * sizeof(double)); dataDesc[4] = stai_new(data + 4, sizeof(double), 6 * sizeof(double)); dataDesc[5] = stai_new(data + 5, sizeof(double), 6 * sizeof(double)); actualRead = cubepm_read(cubepm, localPartsSkip, localPartsRead, dataDesc); if (actualRead != localPartsRead) { fprintf(stderr, "Expected to read %lu particles, but got %lu\n", localPartsRead, actualRead); diediedie(EXIT_FAILURE); } if (localDoUnitConversion) { double posFac, velFac; posFac = cubepm_calcFactorFilePositionToMpch(cubepm); velFac = cubepm_calcFactorFileVelocityToKms(cubepm); #ifdef WITH_OPENMP # pragma omp parallel for #endif for (long i = 0; i < localPartsRead; i++) { data[i * 6 + 0] = data[i * 6 + 0] * posFac; data[i * 6 + 1] = data[i * 6 + 1] * posFac; data[i * 6 + 2] = data[i * 6 + 2] * posFac; data[i * 6 + 3] *= velFac; data[i * 6 + 4] *= velFac; data[i * 6 + 5] *= velFac; } } fprintf(stdout, "#position\t" " x \t y \t z \t" " vx \t vy \t vz \n"); for (long i = 0; i < localPartsRead; i++) fprintf(stdout, " %8li\t%12e\t%12e\t%12e\t%12e\t%12e\t%12e\n", i + localPartsSkip, data[i * 6 + 0], data[i * 6 + 1], data[i * 6 + 2], data[i * 6 + 3], data[i * 6 + 4], data[i * 6 + 5]); xfree(data); stai_del(&(dataDesc[0])); stai_del(&(dataDesc[1])); stai_del(&(dataDesc[2])); stai_del(&(dataDesc[3])); stai_del(&(dataDesc[4])); stai_del(&(dataDesc[5])); cubepm_del(&cubepm); return EXIT_SUCCESS; } /* main */