From 603f71da66665a4d152bd295b2d24f4b2797e077 Mon Sep 17 00:00:00 2001 From: loikki <loic.hausammann@protonmail.ch> Date: Mon, 4 Jan 2021 09:36:14 +0100 Subject: [PATCH] update with recent changes in SWIFT --- pyswiftsim/__init__.py | 4 ++- src/cooling_wrapper.c | 9 +++--- src/pyswiftsim_tools.h | 55 +++++++++++++++++++-------------- src/stellar_evolution_wrapper.c | 44 ++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 30 deletions(-) diff --git a/pyswiftsim/__init__.py b/pyswiftsim/__init__.py index de58517..98d45ee 100644 --- a/pyswiftsim/__init__.py +++ b/pyswiftsim/__init__.py @@ -179,7 +179,9 @@ class ParameterFile: "GEARFeedback": { "supernovae_energy_erg": 1e51, "yields_table": "chemistry-AGB+OMgSFeZnSrYBaEu-16072013.h5", - "discrete_yields": 0 + "discrete_yields": 0, + "elements": ["Fe", "Mg", "O", "S", "Zn", "Sr", "Y", "Ba", "Eu"], + "metallicity_max_first_stars": -1, }, } """ diff --git a/src/cooling_wrapper.c b/src/cooling_wrapper.c index dbe718c..3c91a9f 100644 --- a/src/cooling_wrapper.c +++ b/src/cooling_wrapper.c @@ -154,20 +154,19 @@ PyObject* pycooling_rate(PyObject* self, PyObject* args) { hydro_set_physical_internal_energy_dt(&p, &cosmo, 0); for(int j = 0; j < GEAR_CHEMISTRY_ELEMENT_COUNT; j++) { - p.chemistry_data.smoothed_metal_mass_fraction[j] = p.chemistry_data.metal_mass_fraction[j]; + p.chemistry_data.smoothed_metal_mass_fraction[j] = p.chemistry_data.metal_mass[j] / hydro_get_mass(&p); } /* compute cooling rate */ cooling_cool_part(&pconst, &us, &cosmo, &hydro_props, - &floor_props, &cooling, &p, &xp, /* Time */0, dt, dt); + &floor_props, &cooling, &p, &xp, dt, dt, /* Time */ 0); float *tmp = PyArray_GETPTR1(rate, i); *tmp = hydro_get_physical_internal_energy_dt(&p, &cosmo); - } - /* Cleanup */ - cosmology_clean(&cosmo); + /* Clean the memory */ + PYSWIFTSIM_CLEAN_STRUCTS(); cooling_clean(&cooling); return (PyObject *) rate; diff --git a/src/pyswiftsim_tools.h b/src/pyswiftsim_tools.h index 6b75075..ac074fe 100644 --- a/src/pyswiftsim_tools.h +++ b/src/pyswiftsim_tools.h @@ -44,32 +44,39 @@ enum error_code { int pytools_check_array(PyArrayObject *obj, int dim, int type, const char* name); -#define PYSWIFTSIM_INIT_STRUCTS() \ - \ - /* Initialize parser */ \ - struct swift_params params; \ - parser_read_file(filename, ¶ms); \ - \ - /* Init unit system */ \ - struct unit_system us; \ +#define PYSWIFTSIM_INIT_STRUCTS() \ + \ + /* Initialize parser */ \ + struct swift_params params; \ + parser_read_file(filename, ¶ms); \ + \ + /* Init unit system */ \ + struct unit_system us; \ units_init_from_params(&us, ¶ms, "InternalUnitSystem"); \ - \ - /* Init physical constant */ \ - struct phys_const pconst; \ - phys_const_init(&us, ¶ms, &pconst); \ - \ - /* Init cosmo */ \ - struct cosmology cosmo; \ - \ - if (with_cosmo) \ - cosmology_init(¶ms, &us, &pconst, &cosmo); \ - else \ - cosmology_init_no_cosmo(&cosmo); \ - \ - /* Init hydro prop */ \ - struct hydro_props hydro_props; \ + \ + /* Init physical constant */ \ + struct phys_const pconst; \ + phys_const_init(&us, ¶ms, &pconst); \ + \ + /* Init cosmo */ \ + struct cosmology cosmo; \ + \ + if (with_cosmo) \ + cosmology_init(¶ms, &us, &pconst, &cosmo); \ + else \ + cosmology_init_no_cosmo(&cosmo); \ + \ + /* Init hydro prop */ \ + struct hydro_props hydro_props; \ hydro_props_init(&hydro_props, &pconst, &us, ¶ms); \ - + + + +#define PYSWIFTSIM_CLEAN_STRUCTS() \ + /* No need to clean swift_params */ \ + /* No need to clean unit_system */ \ + /* No need to clean phys_const */ \ + cosmology_clean(&cosmo); \ #endif // __PYSWIFTSIM_TOOLS_H__ diff --git a/src/stellar_evolution_wrapper.c b/src/stellar_evolution_wrapper.c index 3a23c8d..528e4c0 100644 --- a/src/stellar_evolution_wrapper.c +++ b/src/stellar_evolution_wrapper.c @@ -77,6 +77,10 @@ PyObject* pyinitial_mass_function(PyObject* self, PyObject* args) { *imf_i /= pconst.const_solar_mass; } + /* Clean the memory */ + PYSWIFTSIM_CLEAN_STRUCTS(); + feedback_clean(&fp); + return (PyObject *) imf; } @@ -142,6 +146,10 @@ PyObject* pylifetime_from_mass(PyObject* self, PyObject* args) { *tau = pow(10, *tau) * pconst.const_year * 1e6; } + /* Clean the memory */ + PYSWIFTSIM_CLEAN_STRUCTS(); + feedback_clean(&fp); + return (PyObject *) lifetime; } @@ -207,6 +215,10 @@ PyObject* pymass_from_lifetime(PyObject* self, PyObject* args) { *m = pow(10, *m) * pconst.const_solar_mass; } + /* Clean the memory */ + PYSWIFTSIM_CLEAN_STRUCTS(); + feedback_clean(&fp); + return (PyObject *) mass; } @@ -289,6 +301,10 @@ PyObject* pysupernovae_ia_rate(PyObject* self, PyObject* args) { *r /= pconst.const_solar_mass * pconst.const_year * 1e6; } + /* Clean the memory */ + PYSWIFTSIM_CLEAN_STRUCTS(); + feedback_clean(&fp); + return (PyObject *) rate; } @@ -370,6 +386,10 @@ PyObject* pysupernovae_ii_rate(PyObject* self, PyObject* args) { } + /* Clean the memory */ + PYSWIFTSIM_CLEAN_STRUCTS(); + feedback_clean(&fp); + return (PyObject *) rate; } @@ -443,6 +463,10 @@ PyObject* pysupernovae_ii_yields(PyObject* self, PyObject* args) { } } + /* Clean the memory */ + PYSWIFTSIM_CLEAN_STRUCTS(); + feedback_clean(&fp); + return (PyObject *) yields_out; } @@ -510,6 +534,10 @@ PyObject* pysupernovae_ii_mass_ejected(PyObject* self, PyObject* args) { } + /* Clean the memory */ + PYSWIFTSIM_CLEAN_STRUCTS(); + feedback_clean(&fp); + return (PyObject *) mass_ejected; } @@ -577,6 +605,10 @@ PyObject* pysupernovae_ii_mass_ejected_non_processed(PyObject* self, PyObject* a } + /* Clean the memory */ + PYSWIFTSIM_CLEAN_STRUCTS(); + feedback_clean(&fp); + return (PyObject *) mass_ejected; } @@ -622,6 +654,10 @@ PyObject* pysupernovae_ia_yields(PyObject* self, PyObject* args) { *y = yields[i] / fp.stellar_model.snia.mass_white_dwarf; } + /* Clean the memory */ + PYSWIFTSIM_CLEAN_STRUCTS(); + feedback_clean(&fp); + return (PyObject *) yields_out; } @@ -663,6 +699,10 @@ PyObject* pysupernovae_ia_mass_ejected(PyObject* self, PyObject* args) { *y = supernovae_ia_get_ejected_mass_processed(&fp.stellar_model.snia) / fp.stellar_model.snia.mass_white_dwarf; + /* Clean the memory */ + PYSWIFTSIM_CLEAN_STRUCTS(); + feedback_clean(&fp); + return (PyObject *) mass_ejected; } @@ -707,6 +747,10 @@ PyObject* pystellar_get_elements(PyObject* self, PyObject* args) { } } + /* Clean the memory */ + PYSWIFTSIM_CLEAN_STRUCTS(); + feedback_clean(&fp); + return t; } -- GitLab