/******************************************************************************* * This file is part of SWIFT. * Copyright (c) 2016 Matthieu Schaller (matthieu.schaller@durham.ac.uk) * 2018 Folkert Nobels (nobels@strw.leidenuniv.nl) * * 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_MOSAICS_STARS_IO_H #define SWIFT_MOSAICS_STARS_IO_H #include "io_properties.h" #include "stars.h" #include "stars_part.h" /** * @brief Specifies which s-particle fields to read from a dataset * * @param sparts The s-particle array. * @param list The list of i/o properties to read. * @param num_fields The number of i/o fields to read. */ INLINE static void stars_read_particles(struct spart *sparts, struct io_props *list, int *num_fields) { /* Say how much we want to read */ *num_fields = 9; /* List what we want to read */ list[0] = io_make_input_field("Coordinates", DOUBLE, 3, COMPULSORY, UNIT_CONV_LENGTH, sparts, x); list[1] = io_make_input_field("Velocities", FLOAT, 3, COMPULSORY, UNIT_CONV_SPEED, sparts, v); list[2] = io_make_input_field("Masses", FLOAT, 1, COMPULSORY, UNIT_CONV_MASS, sparts, mass); list[3] = io_make_input_field("ParticleIDs", LONGLONG, 1, COMPULSORY, UNIT_CONV_NO_UNITS, sparts, id); list[4] = io_make_input_field("SmoothingLength", FLOAT, 1, OPTIONAL, UNIT_CONV_LENGTH, sparts, h); list[5] = io_make_input_field("Masses", FLOAT, 1, COMPULSORY, UNIT_CONV_MASS, sparts, mass_init); list[6] = io_make_input_field("StellarFormationTime", FLOAT, 1, OPTIONAL, UNIT_CONV_NO_UNITS, sparts, birth_time); list[7] = io_make_input_field("BirthDensities", FLOAT, 1, OPTIONAL, UNIT_CONV_DENSITY, sparts, birth_density); list[8] = io_make_input_field("BirthTemperatures", FLOAT, 1, OPTIONAL, UNIT_CONV_TEMPERATURE, sparts, birth_temperature); } INLINE static void convert_spart_pos(const struct engine *e, const struct spart *sp, double *ret) { const struct space *s = e->s; if (s->periodic) { ret[0] = box_wrap(sp->x[0], 0.0, s->dim[0]); ret[1] = box_wrap(sp->x[1], 0.0, s->dim[1]); ret[2] = box_wrap(sp->x[2], 0.0, s->dim[2]); } else { ret[0] = sp->x[0]; ret[1] = sp->x[1]; ret[2] = sp->x[2]; } } INLINE static void convert_spart_vel(const struct engine *e, const struct spart *sp, float *ret) { const int with_cosmology = (e->policy & engine_policy_cosmology); const struct cosmology *cosmo = e->cosmology; const integertime_t ti_current = e->ti_current; const double time_base = e->time_base; const integertime_t ti_beg = get_integer_time_begin(ti_current, sp->time_bin); const integertime_t ti_end = get_integer_time_end(ti_current, sp->time_bin); /* Get time-step since the last kick */ float dt_kick_grav; if (with_cosmology) { dt_kick_grav = cosmology_get_grav_kick_factor(cosmo, ti_beg, ti_current); dt_kick_grav -= cosmology_get_grav_kick_factor(cosmo, ti_beg, (ti_beg + ti_end) / 2); } else { dt_kick_grav = (ti_current - ((ti_beg + ti_end) / 2)) * time_base; } /* Extrapolate the velocites to the current time */ const struct gpart *gp = sp->gpart; ret[0] = gp->v_full[0] + gp->a_grav[0] * dt_kick_grav; ret[1] = gp->v_full[1] + gp->a_grav[1] * dt_kick_grav; ret[2] = gp->v_full[2] + gp->a_grav[2] * dt_kick_grav; /* Conversion from internal units to peculiar velocities */ ret[0] *= cosmo->a_inv; ret[1] *= cosmo->a_inv; ret[2] *= cosmo->a_inv; } INLINE static void convert_spart_luminosities(const struct engine *e, const struct spart *sp, float *ret) { stars_get_luminosities(sp, e->policy & engine_policy_cosmology, e->cosmology, e->time, e->physical_constants, e->stars_properties, ret); } INLINE static void convert_spart_potential(const struct engine *e, const struct spart *sp, float *ret) { if (sp->gpart != NULL) ret[0] = gravity_get_comoving_potential(sp->gpart); else ret[0] = 0.f; } INLINE static void convert_spart_ages(const struct engine *e, const struct spart *sp, float *ret) { if (e->policy & engine_policy_cosmology) ret[0] = cosmology_get_delta_time_from_scale_factors( e->cosmology, sp->birth_scale_factor, e->cosmology->a); else ret[0] = e->time - sp->birth_time; } INLINE static void convert_spart_tidal_tensors(const struct engine *e, const struct spart *sp, float *ret) { if (sp->gpart != NULL) gravity_get_comoving_tidal_tensor(sp->gpart, &ret[0], &ret[1], &ret[2], &ret[3], &ret[4], &ret[5]); else { ret[0] = 0.f; ret[1] = 0.f; ret[2] = 0.f; ret[3] = 0.f; ret[4] = 0.f; ret[5] = 0.f; } } INLINE static void convert_spart_clmass_total(const struct engine *e, const struct spart *sp, float *ret) { for (int iMP = 0; iMP < MOSAICS_MULTIPHYS; iMP++) { /* Sum of surviving cluster masses */ ret[iMP] = 0.f; for (int i = 0; i < MOSAICS_MAX_CLUSTERS; i++) { ret[iMP] += sp->cl_mass[iMP][i]; } } } /** * @brief Specifies which s-particle fields to write to a dataset * * @param sparts The s-particle array. * @param list The list of i/o properties to write. * @param num_fields The number of i/o fields to write. * @param with_cosmology Are we running a cosmological simulation? */ INLINE static void stars_write_particles(const struct spart *sparts, struct io_props *list, int *num_fields, const int with_cosmology) { /* Say how much we want to write */ *num_fields = 39; /* List what we want to write */ list[0] = io_make_output_field_convert_spart( "Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH, 1.f, sparts, convert_spart_pos, "Co-moving position of the particles"); list[1] = io_make_output_field_convert_spart( "Velocities", FLOAT, 3, UNIT_CONV_SPEED, 0.f, sparts, convert_spart_vel, "Peculiar velocities of the particles. This is a * dx/dt where x is the " "co-moving position of the particles."); list[2] = io_make_output_field("Masses", FLOAT, 1, UNIT_CONV_MASS, 0.f, sparts, mass, "Masses of the particles at the current point " "in time (i.e. after stellar losses)"); list[3] = io_make_physical_output_field( "ParticleIDs", ULONGLONG, 1, UNIT_CONV_NO_UNITS, 0.f, sparts, id, /*can convert to comoving=*/0, "Unique ID of the particles"); list[4] = io_make_output_field( "SmoothingLengths", FLOAT, 1, UNIT_CONV_LENGTH, 1.f, sparts, h, "Co-moving smoothing lengths (FWHM of the kernel) of the particles"); list[5] = io_make_output_field("InitialMasses", FLOAT, 1, UNIT_CONV_MASS, 0.f, sparts, mass_init, "Masses of the star particles at birth time"); if (with_cosmology) { list[6] = io_make_physical_output_field( "BirthScaleFactors", FLOAT, 1, UNIT_CONV_NO_UNITS, 0.f, sparts, birth_scale_factor, /*can convert to comoving=*/0, "Scale-factors at which the stars were born"); } else { list[6] = io_make_output_field("BirthTimes", FLOAT, 1, UNIT_CONV_TIME, 0.f, sparts, birth_time, "Times at which the stars were born"); } list[7] = io_make_output_field( "SNIIFeedbackEnergyFractions", FLOAT, 1, UNIT_CONV_NO_UNITS, 0.f, sparts, SNII_f_E, "Fractions of the canonical feedback energy that was used for the stars' " "SNII feedback events"); list[8] = io_make_physical_output_field( "BirthDensities", FLOAT, 1, UNIT_CONV_DENSITY, 0.f, sparts, birth_density, /*can convert to comoving=*/0, "Physical densities at the time of birth of the gas particles that " "turned into stars (note that we store the physical density at the birth " "redshift, no conversion is needed)"); list[9] = io_make_output_field("BirthTemperatures", FLOAT, 1, UNIT_CONV_TEMPERATURE, 0.f, sparts, birth_temperature, "Temperatures at the time of birth of the gas " "particles that turned into stars"); list[10] = io_make_output_field( "HIIregions_last_rebuild", FLOAT, 1, UNIT_CONV_NO_UNITS, 0.f, sparts, HIIregion_last_rebuild, "Age of star in Myr when HII region was last rebuilt"); list[11] = io_make_output_field( "HIIregions_mass_to_ionize", FLOAT, 1, UNIT_CONV_MASS, 0.f, sparts, HIIregion_mass_to_ionize, "Masses of the HII regions at the current point " "in time"); list[12] = io_make_output_field("Timestep", FLOAT, 1, UNIT_CONV_TIME, 0.f, sparts, star_timestep, "Current timestep of the star particle"); list[13] = io_make_output_field( "HIIregions_mass_in_kernel", FLOAT, 1, UNIT_CONV_MASS, 0.f, sparts, HIIregion_mass_in_kernel, "Masses in kernels at time of HII region formation"); list[14] = io_make_output_field("TimeBins", CHAR, 1, UNIT_CONV_NO_UNITS, 0.f, sparts, time_bin, "Time-bins of the particles"); list[15] = io_make_physical_output_field_convert_spart( "Luminosities", FLOAT, luminosity_bands_count, UNIT_CONV_NO_UNITS, 0.f, sparts, /*can convert to comoving=*/0, convert_spart_luminosities, "Rest-frame dust-free AB-luminosities of the star particles in the GAMA " "bands. These were computed using the BC03 (GALAXEV) models convolved " "with different filter bands and interpolated in log-log (f(log(Z), " "log(age)) = log(flux)) as used in the dust-free modelling of Trayford " "et al. (2015). The luminosities are given in dimensionless units. They " "have been divided by 3631 Jy already, i.e. they can be turned into " "absolute AB-magnitudes (rest-frame absolute maggies) directly by " "applying -2.5 log10(L) without additional corrections."); list[16] = io_make_output_field_convert_spart( "TidalTensors", FLOAT, 6, UNIT_CONV_TIDAL_TENSOR, -3.f, sparts, convert_spart_tidal_tensors, "Components of co-moving the tidal tensor (Hessian matrix of the " "gravitational potential). Since the matrix is symmetric, only 6 values " "per tensor are stored (xx, yy, zz, xy, xz, yz)"); list[17] = io_make_output_field_convert_spart( "Potentials", FLOAT, 1, UNIT_CONV_POTENTIAL, -1.f, sparts, convert_spart_potential, "Gravitational potentials of the particles"); list[18] = io_make_output_field( "SNIaRates", DOUBLE, 1, UNIT_CONV_FREQUENCY, 0.f, sparts, SNIa_rate, "SNIa rate averaged over the last enrichment timestep"); list[19] = io_make_output_field("GCs_Masses", FLOAT, MOSAICS_MULTIPHYS * MOSAICS_MAX_CLUSTERS, UNIT_CONV_MASS, 0.f, sparts, cl_mass, "Masses of clusters tagged to particle"); list[20] = io_make_output_field( "GCs_InitialMasses", FLOAT, MOSAICS_MULTIPHYS * MOSAICS_MAX_CLUSTERS, UNIT_CONV_MASS, 0.f, sparts, cl_initial_mass, "Initial masses of clusters tagged to particle"); if (with_cosmology) { list[21] = io_make_physical_output_field( "GCs_DisruptionScaleFactors", FLOAT, MOSAICS_MULTIPHYS * MOSAICS_MAX_CLUSTERS, UNIT_CONV_NO_UNITS, 0.f, sparts, cl_disruption_time, /*can convert to comoving=*/0, "Scale-factors at which clusters became disrupted (-1 if surviving)"); } else { list[21] = io_make_output_field( "GCs_DisruptionTimes", FLOAT, MOSAICS_MULTIPHYS * MOSAICS_MAX_CLUSTERS, UNIT_CONV_TIME, 0.f, sparts, cl_disruption_time, "Times at which clusters became disrupted (-1 if surviving)"); } list[22] = io_make_output_field("GCs_MassLossShocks", FLOAT, MOSAICS_MULTIPHYS * MOSAICS_MAX_CLUSTERS, UNIT_CONV_MASS, 0.f, sparts, cl_dmshock, "Cluster mass lost to tidal shocks"); list[23] = io_make_physical_output_field( "GCs_NumberOfClusters", INT, MOSAICS_MULTIPHYS, UNIT_CONV_NO_UNITS, 0.f, sparts, num_clusters, /*can convert to comoving=*/1, "Surviving number of subgrid star clusters"); list[24] = io_make_physical_output_field( "GCs_InitialNumberOfClusters", INT, MOSAICS_MULTIPHYS, UNIT_CONV_NO_UNITS, 0.f, sparts, initial_num_clusters, /*can convert to comoving=*/1, "Number of star clusters tried to form in total"); list[25] = io_make_physical_output_field( "GCs_InitialNumberOfClustersEvoLim", INT, MOSAICS_MULTIPHYS, UNIT_CONV_NO_UNITS, 0.f, sparts, initial_num_clusters_evo, /*can convert to comoving=*/1, "Number of star clusters formed above evolution mass limit"); list[26] = io_make_output_field("GCs_InitialClusterMassTotal", FLOAT, MOSAICS_MULTIPHYS, UNIT_CONV_MASS, 0.f, sparts, initial_cluster_mass_total, "Sum of initial star cluster masses"); list[27] = io_make_output_field( "GCs_InitialClusterMassEvoLim", FLOAT, MOSAICS_MULTIPHYS, UNIT_CONV_MASS, 0.f, sparts, initial_cluster_mass_evo, "Sum of initial star cluster masses above evolution mass limit"); list[28] = io_make_output_field_convert_spart( "GCs_ClusterMassTotal", FLOAT, MOSAICS_MULTIPHYS, UNIT_CONV_MASS, 0.f, sparts, convert_spart_clmass_total, "Sum of surviving star cluster masses"); list[29] = io_make_output_field( "GCs_SubgridFieldMass", FLOAT, MOSAICS_MULTIPHYS, UNIT_CONV_MASS, 0.f, sparts, field_mass, "Subgrid field mass component of star particle"); list[30] = io_make_physical_output_field( "GCs_ClusterFormationEfficiency", FLOAT, MOSAICS_MULTIPHYS, UNIT_CONV_NO_UNITS, 0.f, sparts, CFE, /*can convert to comoving=*/1, "Fraction of star formation which occurs in bound star clusters"); list[31] = io_make_output_field( "GCs_ClusterTruncationMass", FLOAT, MOSAICS_MULTIPHYS, UNIT_CONV_MASS, 0.f, sparts, Mcstar, "Exponential truncation of star cluster initial mass function (Mcstar)"); list[32] = io_make_physical_output_field( "GCs_BirthGasFraction", FLOAT, 1, UNIT_CONV_NO_UNITS, 0.f, sparts, cell_gas_fraction, /*can convert to comoving=*/1, "Local gas fraction (within cell) at time of star formation"); list[33] = io_make_physical_output_field( "GCs_BirthEpicyclicFrequency", FLOAT, 1, UNIT_CONV_FREQUENCY, 0.f, sparts, kappa_birth, /*can convert to comoving=*/0, "Epicyclic frequency at time of star formation"); list[34] = io_make_physical_output_field( "GCs_BirthCircularFrequency", FLOAT, 1, UNIT_CONV_FREQUENCY, 0.f, sparts, Omega_birth, /*can convert to comoving=*/0, "Circular frequency at time of star formation"); list[35] = io_make_output_field("GCs_BirthToomreMass", FLOAT, 1, UNIT_CONV_MASS, 0.f, sparts, Toomre_mass, "Local Toomre mass at formation"); list[36] = io_make_physical_output_field( "GCs_ToomreCollapseFraction", FLOAT, 1, UNIT_CONV_NO_UNITS, 0.f, sparts, frac_collapse, /*can convert to comoving=*/1, "Fraction of Toomre mass which can collapse to a GMC"); list[37] = io_make_physical_output_field( "GCs_FgasCellWidth", FLOAT, 1, UNIT_CONV_LENGTH, 0.f, sparts, cell_width, /*can convert to comoving=*/0, "Cell width at point of gas fraction calculation"); list[38] = io_make_physical_output_field_convert_spart( "Ages", FLOAT, 1, UNIT_CONV_TIME, 0.f, sparts, /*can convert to comoving=*/0, convert_spart_ages, "Ages of the stars."); } /** * @brief Initialize the global properties of the stars scheme. * * By default, takes the values provided by the hydro. * * @param sp The #stars_props. * @param phys_const The physical constants in the internal unit system. * @param us The internal unit system. * @param params The parsed parameters. * @param p The already read-in properties of the hydro scheme. * @param cosmo The cosmological model. */ INLINE static void stars_props_init(struct stars_props *sp, const struct phys_const *phys_const, const struct unit_system *us, struct swift_params *params, const struct hydro_props *p, const struct cosmology *cosmo) { /* Kernel properties */ sp->eta_neighbours = parser_get_opt_param_float( params, "Stars:resolution_eta", p->eta_neighbours); /* Tolerance for the smoothing length Newton-Raphson scheme */ sp->h_tolerance = parser_get_opt_param_float(params, "Stars:h_tolerance", p->h_tolerance); /* Get derived properties */ sp->target_neighbours = pow_dimension(sp->eta_neighbours) * kernel_norm; const float delta_eta = sp->eta_neighbours * (1.f + sp->h_tolerance); sp->delta_neighbours = (pow_dimension(delta_eta) - pow_dimension(sp->eta_neighbours)) * kernel_norm; /* Number of iterations to converge h */ sp->max_smoothing_iterations = parser_get_opt_param_int( params, "Stars:max_ghost_iterations", p->max_smoothing_iterations); /* Time integration properties */ const float max_volume_change = parser_get_opt_param_float(params, "Stars:max_volume_change", -1); if (max_volume_change == -1) sp->log_max_h_change = p->log_max_h_change; else sp->log_max_h_change = logf(powf(max_volume_change, hydro_dimension_inv)); /* Do we want to overwrite the stars' birth time? */ sp->overwrite_birth_time = parser_get_opt_param_int(params, "Stars:overwrite_birth_time", 0); sp->overwrite_birth_density = parser_get_opt_param_int(params, "Stars:overwrite_birth_density", 0); sp->overwrite_birth_temperature = parser_get_opt_param_int(params, "Stars:overwrite_birth_temperature", 0); /* Read birth time to set all stars in ICs */ if (sp->overwrite_birth_time) { sp->spart_first_init_birth_time = parser_get_param_float(params, "Stars:birth_time"); } /* Read birth density to set all stars in ICs */ if (sp->overwrite_birth_density) { sp->spart_first_init_birth_density = parser_get_param_float(params, "Stars:birth_density"); } /* Read birth temperature to set all stars in ICs */ if (sp->overwrite_birth_temperature) { sp->spart_first_init_birth_temperature = parser_get_param_float(params, "Stars:birth_temperature"); } /* Maximal time-step lengths */ const double Myr = 1e6 * 365.25 * 24. * 60. * 60.; const double conv_fac = units_cgs_conversion_factor(us, UNIT_CONV_TIME); const double max_time_step_young_Myr = parser_get_opt_param_float( params, "Stars:max_timestep_young_Myr", FLT_MAX); const double max_time_step_old_Myr = parser_get_opt_param_float(params, "Stars:max_timestep_old_Myr", FLT_MAX); const double age_threshold_Myr = parser_get_opt_param_float( params, "Stars:timestep_age_threshold_Myr", FLT_MAX); const double age_threshold_unlimited_Myr = parser_get_opt_param_float( params, "Stars:timestep_age_threshold_unlimited_Myr", 0.); /* Check for consistency */ if (age_threshold_unlimited_Myr != 0. && age_threshold_Myr != FLT_MAX) { if (age_threshold_unlimited_Myr < age_threshold_Myr) error( "The age threshold for unlimited stellar time-step sizes (%e Myr) is " "smaller than the transition threshold from young to old ages (%e " "Myr)", age_threshold_unlimited_Myr, age_threshold_Myr); } /* Convert to internal units */ sp->max_time_step_young = max_time_step_young_Myr * Myr / conv_fac; sp->max_time_step_old = max_time_step_old_Myr * Myr / conv_fac; sp->age_threshold = age_threshold_Myr * Myr / conv_fac; sp->age_threshold_unlimited = age_threshold_unlimited_Myr * Myr / conv_fac; /* Read photometry table filepath */ char base_dir_name[200]; parser_get_param_string(params, "Stars:luminosity_filename", base_dir_name); /* Luminosity tables */ for (int i = 0; i < (int)luminosity_bands_count; ++i) { const int count_Z = eagle_stars_lum_tables_N_Z; const int count_ages = eagle_stars_lum_tables_N_ages; const int count_L = count_Z * count_ages; sp->lum_tables_Z[i] = (float *)malloc(count_Z * sizeof(float)); sp->lum_tables_ages[i] = (float *)malloc(count_ages * sizeof(float)); sp->lum_tables_luminosities[i] = (float *)malloc(count_L * sizeof(float)); static const char *luminosity_band_names[luminosity_bands_count] = { "u", "g", "r", "i", "z", "Y", "J", "H", "K"}; char fname[256]; sprintf(fname, "%s/GAMA/%s", base_dir_name, luminosity_band_names[i]); FILE *file = fopen(fname, "r"); if (!file) error("Error opening photometry file '%s'", fname); char buffer[200]; int j = 0, k = 0; while (fgets(buffer, sizeof(buffer), file)) { double z, age, L; sscanf(buffer, "%le %le %le", &z, &age, &L); if (age == 0.) sp->lum_tables_Z[i][k++] = log10(z); if (j < count_ages) sp->lum_tables_ages[i][j] = log10(age + FLT_MIN); sp->lum_tables_luminosities[i][j] = log10(L); ++j; } fclose(file); } /* Luminosity conversion factor */ const double L_sun = 3.828e26; /* Watt */ const double pc = 3.08567758149e16; /* m */ const double A = 4. * M_PI * (10. * pc) * (10 * pc); const double to_Jansky = 1e26 * L_sun / A; const double zero_point_AB = 3631; /* Jansky */ sp->lum_tables_factor = to_Jansky / zero_point_AB; /* MOSAICS parameters ----------------------------------------------------- */ /* King 1966 density profile parameter */ sp->W0 = parser_get_opt_param_float(params, "Stars:clusters_King_W0", 5.0); /* Half-mass radii (pc) */ sp->rh = parser_get_opt_param_float(params, "Stars:clusters_rh", 4.0); /* Evaporation on by default */ sp->cluster_evap = parser_get_opt_param_int(params, "Stars:clusters_evaporation", 1); /* Tidal shocks on by default */ sp->cluster_shocks = parser_get_opt_param_int(params, "Stars:clusters_tidal_shocks", 1); /* Include constant isolation term for evaporation */ sp->spitzer_evap_term = parser_get_opt_param_int(params, "Stars:Spitzer_evap_term", 1); /* Add metallicity dependence to evaporation */ sp->met_dep_evaporation = parser_get_opt_param_int(params, "Stars:met_dep_evaporation", 1); /* Use Omega^2 = -lambda_2, otherwise sum(-lambda/3) */ sp->Omega_is_lambda2 = parser_get_opt_param_int(params, "Stars:Omega_is_lambda2", 0); /* Minimum cell width for gas fraction calculation, in kpc */ sp->min_fgas_cell_width = parser_get_opt_param_float(params, "Stars:min_fgas_cell_width", 0.1); /* Use only the subgrid turbulent component for velocity dispersion */ sp->subgrid_gas_vel_disp = parser_get_opt_param_int( params, "Stars:use_subgrid_velocity_dispersion", 0); /* Parameters of initial cluster mass function ---------------------------- */ /* Use a power-law mass function? One value for each model */ parser_get_param_int_array(params, "Stars:power_law_clMF", MOSAICS_MULTIPHYS, sp->power_law_clMF); /* Value of fixed Schechter truncation for each model (Msun) */ parser_get_param_float_array(params, "Stars:fixed_Mcstar", MOSAICS_MULTIPHYS, sp->fixed_Mcstar); /* Cluster mass function index M^(-alpha). One value for each model */ parser_get_param_float_array(params, "Stars:clMF_slope", MOSAICS_MULTIPHYS, sp->clMF_slope); /* Cluster mass function minimum (Msun) */ sp->clMF_min = parser_get_opt_param_float(params, "Stars:clMF_min", 100.0); /* Initial lowest cluster mass to evolve (Msun) */ sp->clMF_min_evolve = parser_get_opt_param_float(params, "Stars:clMF_min_evolve", 500.0); /* Cluster mass function maximum (Msun) */ sp->clMF_max = parser_get_opt_param_float(params, "Stars:clMF_max", 1.0e9); /* Integrated star formation efficiency in collapse of GMC (for Mcstar) */ sp->GMC_SFE = parser_get_opt_param_float(params, "Stars:GMC_SFE", 0.1); /* Calculate GMC mass from inverted Larson-type M-sigma relation of form * Mgmc = const * sigma^m */ sp->Mgmc_Larson_relation = parser_get_opt_param_int(params, "Stars:Mgmc_Larson_relation", 1); /* Power law index for Larson-type M-sigma relation */ sp->GMC_m_sigma_index = parser_get_opt_param_float(params, "Stars:GMC_m_sigma_index", 4.0); /* Constant for Larson-type M-sigma relation (Msun) */ sp->GMC_m_sigma_const = parser_get_opt_param_float(params, "Stars:GMC_m_sigma_const", 3000.0); /* Parameters of cluster formation efficiency ----------------------------- */ /* Value of fixed cluster formation efficiency for each model */ parser_get_param_float_array(params, "Stars:fixed_CFE", MOSAICS_MULTIPHYS, sp->fixed_CFE); /* Sound speed of cold ISM (m/s) */ sp->fixed_cs = parser_get_opt_param_float(params, "Stars:fixed_sound_speed", -1.0); /* star formation law. 0: Elmegreen 02; 1: Krumholz & McKee 05 */ sp->sflaw = parser_get_opt_param_int(params, "Stars:CFE_sflaw", 0); /*! Value of constant specific star formation rate per free-fall time */ sp->const_sfrff = parser_get_opt_param_float(params, "Stars:CFE_sfrff", 0.01); /* GMC virial parameter */ sp->qvir = parser_get_opt_param_float(params, "Stars:CFE_qvir", 1.3); /* time of first SN (Myr) */ sp->tsn = parser_get_opt_param_float(params, "Stars:CFE_tsn", 3.0); /* time of determining the CFE (Myr) */ sp->tview = parser_get_opt_param_float(params, "Stars:CFE_tview", 10.0); /* Minimum GMC surface density (Msun/pc^2) */ sp->surfGMC_min = parser_get_opt_param_float(params, "Stars:CFE_surfGMC_minimum", 0.0); /* maximum (protostellar core) SFE */ sp->ecore = parser_get_opt_param_float(params, "Stars:CFE_ecore", 0.5); /* turbulent/magnetic pressure ratio */ sp->beta0 = parser_get_opt_param_float(params, "Stars:CFE_beta0", 1.0e10); /* SN/radiative feedback mode. 0=supernova, 1=radiative, 2=both */ sp->radfb = parser_get_opt_param_int(params, "Stars:CFE_radfb", 0); /* Cruel cradle parameters */ sp->cruel_cradle = parser_get_opt_param_int(params, "Stars:CFE_cruel_cradle", 0); sp->xsurv = parser_get_opt_param_float(params, "Stars:CFE_xsurv", 0.); /* Use metallicity-dependent local bound fraction for CFE */ sp->CFE_met_dep_fbound = parser_get_opt_param_int(params, "Stars:CFE_met_dep_fbound", 1); /* Saturation value for metallicity-dependent fbound */ sp->fbound_saturation = parser_get_opt_param_float(params, "Stars:fbound_sat", 0.95); /* Scale SFE value for metallicity-dependent fbound */ sp->fbound_scale = parser_get_opt_param_float(params, "Stars:fbound_scale", 0.09); /* Shape parameter for metallicity-dependent fbound */ sp->fbound_beta = parser_get_opt_param_float(params, "Stars:fbound_beta", 1.5); /* Low SFE power law indices for metallicity-dependent fbound. Default values * based on Grudic et al. 2021 */ int errorint = parser_get_opt_param_float_array(params, "Stars:fbound_alpha", 2, sp->fbound_alpha); if (errorint == 0) { /* Default values */ sp->fbound_alpha[0] = 1.5; sp->fbound_alpha[1] = 2.2; } /* Total metal mass fraction corresponding to alpha values for * metallicity-dependent fbound */ errorint = parser_get_opt_param_float_array(params, "Stars:fbound_Z", 2, sp->fbound_Z); if (errorint == 0) { /* Default values */ sp->fbound_Z[0] = 0.0002; sp->fbound_Z[1] = 0.02; } sp->fbound_logZ[0] = log10(sp->fbound_Z[0]); sp->fbound_logZ[1] = log10(sp->fbound_Z[1]); /* Factor through the beta parameter */ sp->fbound_scale = pow(sp->fbound_scale, sp->fbound_beta); sp->fbound_alpha[0] /= sp->fbound_beta; sp->fbound_alpha[1] /= sp->fbound_beta; /* Some constants, see Kruijssen 2012, Appendix C */ sp->const_phifb = 5.28084e-16; sp->const_kappa0 = 5.01354e-08; sp->const_psi = 9.90157e-12; /* Parameters of cluster evolution ---------------------------------------- */ /* Evaporation law exponent for W0 = [5,7] King profile * Default values from Lamers, Baumgardt & Gieles 2013 */ sp->gamma_W5 = parser_get_opt_param_float(params, "Stars:gamma_W5", 0.65); sp->gamma_W7 = parser_get_opt_param_float(params, "Stars:gamma_W7", 0.80); /* Total metal mass fraction dependence of evaporation exponent * Default values from Gieles & Gnedin 2023 */ sp->gamma_max_Zdep = parser_get_opt_param_float(params, "Stars:gamma_max_Zdep", 1.33); sp->Z_gamma_max = parser_get_opt_param_float(params, "Stars:Z_gamma_max", 0.0006); sp->Z_gamma_min = parser_get_opt_param_float(params, "Stars:Z_gamma_min", 0.017); sp->logZ_gamma_max = log10(sp->Z_gamma_max); sp->logZ_gamma_min = log10(sp->Z_gamma_min); /* Disruption timescale (Myr) */ sp->fixed_t0evap = parser_get_opt_param_float(params, "Stars:fixed_evap_t0", -1.f); /* Evaporation constants (Myr) */ sp->const_t0evapsunW5 = parser_get_opt_param_float(params, "Stars:const_t0evap_W0_5", 20.9); sp->const_t0evapsunW7 = parser_get_opt_param_float(params, "Stars:const_t0evap_W0_7", 6.1); sp->const_tidesun = parser_get_opt_param_float(params, "Stars:const_tidesun", 7.005e-4); /* Set up constants for MOSAICS calculations */ const double const_Myr = 1e6 * phys_const->const_year; const double pc2 = phys_const->const_parsec * phys_const->const_parsec; const double yr3 = phys_const->const_year * phys_const->const_year * phys_const->const_year; const double kms = 1e5 / units_cgs_conversion_factor(us, UNIT_CONV_VELOCITY); /* Convert to internal units */ sp->rh *= phys_const->const_parsec; sp->min_fgas_cell_width *= 1000. * phys_const->const_parsec; for (int i = 0; i < MOSAICS_MULTIPHYS; i++) sp->fixed_Mcstar[i] *= phys_const->const_solar_mass; sp->clMF_min *= phys_const->const_solar_mass; sp->clMF_min_evolve *= phys_const->const_solar_mass; sp->clMF_max *= phys_const->const_solar_mass; sp->GMC_m_sigma_const = sp->GMC_m_sigma_const * phys_const->const_solar_mass * pow(1. / kms, sp->GMC_m_sigma_index); if (sp->fixed_cs > 0) sp->fixed_cs *= 100.0 / units_cgs_conversion_factor(us, UNIT_CONV_VELOCITY); sp->tsn *= const_Myr; sp->tview *= const_Myr; sp->surfGMC_min *= phys_const->const_solar_mass / (phys_const->const_parsec * phys_const->const_parsec); sp->const_phifb *= pc2 / yr3; sp->const_kappa0 *= pc2 / phys_const->const_solar_mass; sp->const_psi *= pc2 / yr3; if (sp->fixed_t0evap > 0) sp->fixed_t0evap *= const_Myr; sp->const_t0evapsunW5 *= const_Myr; sp->const_t0evapsunW7 *= const_Myr; sp->const_tidesun /= (const_Myr * const_Myr); } /** * @brief Print the global properties of the stars scheme. * * @param sp The #stars_props. */ INLINE static void stars_props_print(const struct stars_props *sp) { message("Stars kernel: %s with eta=%f (%.2f neighbours).", kernel_name, sp->eta_neighbours, sp->target_neighbours); message("Stars relative tolerance in h: %.5f (+/- %.4f neighbours).", sp->h_tolerance, sp->delta_neighbours); message( "Stars integration: Max change of volume: %.2f " "(max|dlog(h)/dt|=%f).", pow_dimension(expf(sp->log_max_h_change)), sp->log_max_h_change); message("Maximal iterations in ghost task set to %d", sp->max_smoothing_iterations); if (sp->overwrite_birth_time) message("Stars' birth time read from the ICs will be overwritten to %f", sp->spart_first_init_birth_time); message("Stars' age threshold for unlimited dt: %e [U_t]", sp->age_threshold_unlimited); message("Stars' young/old age threshold: %e [U_t]", sp->age_threshold); message("Max time-step size of young stars: %e [U_t]", sp->max_time_step_young); message("Max time-step size of old stars: %e [U_t]", sp->max_time_step_old); message("MOSAICS parallel formation models: %d", MOSAICS_MULTIPHYS); message("MOSAICS maximum clusters: %d", MOSAICS_MAX_CLUSTERS); } #if defined(HAVE_HDF5) INLINE static void stars_props_print_snapshot(hid_t h_grpstars, hid_t h_grp_columns, const struct stars_props *sp) { io_write_attribute_s(h_grpstars, "Kernel function", kernel_name); io_write_attribute_f(h_grpstars, "Kernel target N_ngb", sp->target_neighbours); io_write_attribute_f(h_grpstars, "Kernel delta N_ngb", sp->delta_neighbours); io_write_attribute_f(h_grpstars, "Kernel eta", sp->eta_neighbours); io_write_attribute_f(h_grpstars, "Smoothing length tolerance", sp->h_tolerance); io_write_attribute_f(h_grpstars, "Volume log(max(delta h))", sp->log_max_h_change); io_write_attribute_f(h_grpstars, "Volume max change time-step", pow_dimension(expf(sp->log_max_h_change))); io_write_attribute_i(h_grpstars, "Max ghost iterations", sp->max_smoothing_iterations); io_write_attribute_i(h_grpstars, "MOSAICS_MAX_CLUSTERS", MOSAICS_MAX_CLUSTERS); io_write_attribute_i(h_grpstars, "MOSAICS_MULTIPHYS", MOSAICS_MULTIPHYS); static const char luminosity_band_names[luminosity_bands_count][32] = { "GAMA_u", "GAMA_g", "GAMA_r", "GAMA_i", "GAMA_z", "GAMA_Y", "GAMA_J", "GAMA_H", "GAMA_K"}; /* Add to the named columns */ hsize_t dims[1] = {luminosity_bands_count}; hid_t type = H5Tcopy(H5T_C_S1); H5Tset_size(type, 32); hid_t space = H5Screate_simple(1, dims, NULL); hid_t dset = H5Dcreate(h_grp_columns, "Luminosities", type, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite(dset, type, H5S_ALL, H5S_ALL, H5P_DEFAULT, luminosity_band_names[0]); H5Dclose(dset); H5Tclose(type); H5Sclose(space); } #endif /** * @brief Free the memory allocated for the stellar properties. * * @param sp The #stars_props structure. */ INLINE static void stars_props_clean(struct stars_props *sp) { for (int i = 0; i < (int)luminosity_bands_count; ++i) { free(sp->lum_tables_Z[i]); free(sp->lum_tables_ages[i]); free(sp->lum_tables_luminosities[i]); } } /** * @brief Write a #stars_props struct to the given FILE as a stream of bytes. * * @param p the struct * @param stream the file stream */ INLINE static void stars_props_struct_dump(const struct stars_props *p, FILE *stream) { restart_write_blocks((void *)p, sizeof(struct stars_props), 1, stream, "starsprops", "stars props"); const int count_Z = eagle_stars_lum_tables_N_Z; const int count_ages = eagle_stars_lum_tables_N_ages; const int count_L = count_Z * count_ages; /* Did we allocate anything? */ if (p->lum_tables_Z[0]) { for (int i = 0; i < (int)luminosity_bands_count; ++i) { restart_write_blocks(p->lum_tables_Z[i], count_Z, sizeof(float), stream, "luminosity_Z", "stars props"); restart_write_blocks(p->lum_tables_ages[i], count_ages, sizeof(float), stream, "luminosity_ages", "stars props"); restart_write_blocks(p->lum_tables_luminosities[i], count_L, sizeof(float), stream, "luminosity_L", "stars props"); } } } /** * @brief Restore a stars_props struct from the given FILE as a stream of * bytes. * * @param p the struct * @param stream the file stream */ INLINE static void stars_props_struct_restore(struct stars_props *p, FILE *stream) { restart_read_blocks((void *)p, sizeof(struct stars_props), 1, stream, NULL, "stars props"); /* Did we allocate anything? */ if (p->lum_tables_Z[0]) { for (int i = 0; i < (int)luminosity_bands_count; ++i) { const int count_Z = eagle_stars_lum_tables_N_Z; const int count_ages = eagle_stars_lum_tables_N_ages; const int count_L = count_Z * count_ages; p->lum_tables_Z[i] = (float *)malloc(count_Z * sizeof(float)); p->lum_tables_ages[i] = (float *)malloc(count_ages * sizeof(float)); p->lum_tables_luminosities[i] = (float *)malloc(count_L * sizeof(float)); restart_read_blocks((void *)p->lum_tables_Z[i], count_Z, sizeof(float), stream, NULL, "stars props"); restart_read_blocks((void *)p->lum_tables_ages[i], count_ages, sizeof(float), stream, NULL, "stars props"); restart_read_blocks((void *)p->lum_tables_luminosities[i], count_L, sizeof(float), stream, NULL, "stars props"); } } } #endif /* SWIFT_MOSAICS_STAR_IO_H */