/******************************************************************************* * This file is part of SWIFT. * Copyright (c) 2019 Matthieu Schaller (matthieu.schaller@durham.ac.uk) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * 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 Lesser General Public License * along with this program. If not, see . * ******************************************************************************/ #ifndef SWIFT_COLIBRE_FEEDBACK_TABLES_H #define SWIFT_COLIBRE_FEEDBACK_TABLES_H static const float default_maxage_Myr_HII = 50.f; static const float default_maxage_Myr_SW = 250.f; /** * @brief Allocates and reads early feedback tables, flattens and stores them in * the feedback properties. * * @param feedback_props the #feedback_props data struct to read the tables * into. */ INLINE static void read_feedback_tables(struct feedback_props *fp) { #ifdef HAVE_HDF5 hid_t dataset; herr_t status; hid_t tempfile_id = H5Fopen(fp->early_feedback_table_path, H5F_ACC_RDONLY, H5P_DEFAULT); if (tempfile_id < 0) error("unable to open file %s\n", fp->early_feedback_table_path); /* read sizes of array dimensions */ dataset = H5Dopen(tempfile_id, "Header/NumberOfBins/MetallicityBins", H5P_DEFAULT); status = H5Dread(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &fp->early_fb_nr_met_bins); if (status < 0) error("error reading number of metallicities \n"); status = H5Dclose(dataset); if (status < 0) error("error closing dataset: number of metallicities \n"); dataset = H5Dopen(tempfile_id, "Header/NumberOfBins/StellarAgeBins", H5P_DEFAULT); status = H5Dread(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &fp->early_fb_nr_age_bins); if (status < 0) error("error reading number of ages \n"); status = H5Dclose(dataset); if (status < 0) error("error closing dataset: number of ages\n"); dataset = H5Dopen(tempfile_id, "Header/NumberOfBins/DensityBins", H5P_DEFAULT); status = H5Dread(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &fp->early_fb_nr_dens_bins); if (status < 0) error("error reading number of densities \n"); status = H5Dclose(dataset); if (status < 0) error("error closing dataset: number of densities\n"); dataset = H5Dopen(tempfile_id, "Header/NumberOfBins/TemperatureBins", H5P_DEFAULT); status = H5Dread(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &fp->early_fb_nr_temp_bins); if (status < 0) error("error reading number of temperatures \n"); status = H5Dclose(dataset); if (status < 0) error("error closing dataset: number of temperatures\n"); /* allocate arrays */ if (posix_memalign((void **)&fp->early_fb_log10Z_bins, SWIFT_STRUCT_ALIGNMENT, fp->early_fb_nr_met_bins * sizeof(float)) != 0) error("Failed to allocate metallicity bins\n"); if (posix_memalign((void **)&fp->early_fb_age_bins, SWIFT_STRUCT_ALIGNMENT, fp->early_fb_nr_age_bins * sizeof(float)) != 0) error("Failed to allocate age bins\n"); if (posix_memalign((void **)&fp->early_fb_log10nH_bins, SWIFT_STRUCT_ALIGNMENT, fp->early_fb_nr_dens_bins * sizeof(float)) != 0) error("Failed to allocate density bins\n"); if (posix_memalign((void **)&fp->early_fb_log10T_bins, SWIFT_STRUCT_ALIGNMENT, fp->early_fb_nr_temp_bins * sizeof(float)) != 0) error("Failed to allocate temperature bins\n"); if (posix_memalign((void **)&fp->HII_log10_Qcum, SWIFT_STRUCT_ALIGNMENT, fp->early_fb_nr_met_bins * fp->early_fb_nr_age_bins * sizeof(float)) != 0) error("Failed to allocate Q (HII regions) array\n"); if (posix_memalign((void **)&fp->SW_log10_Pcum, SWIFT_STRUCT_ALIGNMENT, fp->early_fb_nr_met_bins * fp->early_fb_nr_age_bins * sizeof(float)) != 0) error("Failed to allocate P (stellar winds) array\n"); if (posix_memalign((void **)&fp->RP_log10_Pcum, SWIFT_STRUCT_ALIGNMENT, fp->early_fb_nr_temp_bins * fp->early_fb_nr_met_bins * fp->early_fb_nr_dens_bins * fp->early_fb_nr_age_bins * sizeof(float)) != 0) error("Failed to allocate P (radiation pressure) array\n"); /* read in the metallicity bins */ dataset = H5Dopen(tempfile_id, "Header/MetallicityBins", H5P_DEFAULT); status = H5Dread(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, fp->early_fb_log10Z_bins); if (status < 0) error("error reading metallicity bins\n"); status = H5Dclose(dataset); if (status < 0) error("error closing dataset: metallicity bins"); /* read in the solar metallicity */ dataset = H5Dopen(tempfile_id, "Header/Constants/SolarMetallicity", H5P_DEFAULT); status = H5Dread(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &fp->Zsol); if (status < 0) error("error reading solar metallcity \n"); status = H5Dclose(dataset); if (status < 0) error("error closing dataset: solar metallicity"); /* convert log10 Z/Zsol to log10 Z */ for (int i = 0; i < fp->early_fb_nr_met_bins; i++) { fp->early_fb_log10Z_bins[i] += log10(fp->Zsol); } /* read in the stellar ages bins */ dataset = H5Dopen(tempfile_id, "Header/StellarAgeBins", H5P_DEFAULT); status = H5Dread(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, fp->early_fb_age_bins); if (status < 0) error("error reading age bins\n"); status = H5Dclose(dataset); if (status < 0) error("error closing dataset: age bins"); /* read in the density bins */ dataset = H5Dopen(tempfile_id, "Header/DensityBins", H5P_DEFAULT); status = H5Dread(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, fp->early_fb_log10nH_bins); if (status < 0) error("error reading density bins\n"); status = H5Dclose(dataset); if (status < 0) error("error closing dataset: density bins"); /* read in the temperature bins */ dataset = H5Dopen(tempfile_id, "Header/TemperatureBins", H5P_DEFAULT); status = H5Dread(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, fp->early_fb_log10T_bins); if (status < 0) error("error reading temperature bins\n"); status = H5Dclose(dataset); if (status < 0) error("error closing dataset: temperature bins"); /* read in cumulative ionizing photons for HII regions */ dataset = H5Dopen(tempfile_id, "HIIregions/CumulativeIonizingPhotons", H5P_DEFAULT); status = H5Dread(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, fp->HII_log10_Qcum); if (status < 0) error("error reading Q\n"); status = H5Dclose(dataset); if (status < 0) error("error closing dataset: HIIregions/CumulativeIonizingPhotons"); /* read in cumulative momentum input from stellar winds */ dataset = H5Dopen(tempfile_id, "StellarWinds/CumulativeMomentum", H5P_DEFAULT); status = H5Dread(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, fp->SW_log10_Pcum); if (status < 0) error("error reading P\n"); status = H5Dclose(dataset); if (status < 0) error("error closing dataset: StellarWinds/CumulativeMomentum"); /* read in cumulative momentum input from radiation pressure */ dataset = H5Dopen(tempfile_id, "/RadiationPressure/Absorption/logPcumulative_Total", H5P_DEFAULT); status = H5Dread(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, fp->RP_log10_Pcum); if (status < 0) error("error reading P\n"); status = H5Dclose(dataset); if (status < 0) error( "error closing dataset: " "RadiationPressure/Absorption/logPcumulative_Total"); /* Close the file */ status = H5Fclose(tempfile_id); if (status < 0) error("error closing file"); #else error("Need HDF5 to read early feedback tables"); #endif } #endif /* SWIFT_COLIBRE_FEEDBACK_TABLES_H */