Skip to content
Snippets Groups Projects
Commit c147c232 authored by Loic Hausammann's avatar Loic Hausammann
Browse files

Merge branch 'grackle3' into 'master'

Grackle3

See merge request !3
parents 95331319 7c68829c
No related branches found
No related tags found
1 merge request!3Grackle3
...@@ -283,28 +283,45 @@ class UnitSystem(SwiftStruct): ...@@ -283,28 +283,45 @@ class UnitSystem(SwiftStruct):
super().__init__(self.struct_format, data, parent) super().__init__(self.struct_format, data, parent)
######################################################################
# #
# chemistry_part_data #
# #
######################################################################
class ChemistryPartData(SwiftStruct):
_format = "f"
_name = [
"he_density"
]
def __init__(self, data, parent=None):
super().__init__(self.struct_format, data, parent)
###################################################################### ######################################################################
# # # #
# Part # # Part #
# # # #
###################################################################### ######################################################################
class Part(SwiftStruct): class Part(SwiftStruct):
_format = "qP3d3f3ffffffN7f4c" _format = "qP3d3f3ffffffN7f4{chemistry}sc".format(
chemistry=struct.calcsize(ChemistryPartData._format)
)
_name = [ _name = [
"id", "id",
"gpart", "gpart",
"pos", "pos",
"vel", "vel",
"a_hydro", "a_hydro",
"h", "h",
"mass", "mass",
"rho", "rho",
"entropy", "entropy",
"entropy_dt", "entropy_dt",
"last_offset", "last_offset",
"density", "density",
"time_bin" "chemistry_data",
] "time_bin"
]
def __init__(self, data, parent=None): def __init__(self, data, parent=None):
super().__init__(self.struct_format, data, parent) super().__init__(self.struct_format, data, parent)
...@@ -416,7 +433,7 @@ class PhysConst(SwiftStruct): ...@@ -416,7 +433,7 @@ class PhysConst(SwiftStruct):
super().__init__(self.struct_format, data, parent) super().__init__(self.struct_format, data, parent)
class GrackleUnits(SwiftStruct): class GrackleCodeUnits(SwiftStruct):
_format = "idddddd" _format = "idddddd"
_name = [ _name = [
"comoving_coordinates", "comoving_coordinates",
...@@ -493,17 +510,17 @@ class CoolingFunctionData(SwiftStruct): ...@@ -493,17 +510,17 @@ class CoolingFunctionData(SwiftStruct):
"cooling_tstep_mult" "cooling_tstep_mult"
] ]
elif cooling_type == "grackle": elif cooling_type == "grackle":
_format = "200cidd{units}s{chemistry}s".format( _format = "200cidd{code_units}s{chemistry}s".format(
units=struct.calcsize(GrackleUnits._format), code_units=struct.calcsize(GrackleCodeUnits._format),
chemistry=struct.calcsize(GrackleChemistryData._format) chemistry=struct.calcsize(GrackleChemistryData._format)
) )
_name = [ _name = [
"cloudy_table", "GrackleCloudyTable",
"uv_background", "UVbackground",
"redshift", "GrackleRedshift",
"density_self_shielding", "GrackleHSShieldingDensityThreshold",
"units", "code_units",
"chemistry" "chemistry",
] ]
@property @property
...@@ -514,7 +531,7 @@ class CoolingFunctionData(SwiftStruct): ...@@ -514,7 +531,7 @@ class CoolingFunctionData(SwiftStruct):
} }
units = { units = {
"class": GrackleUnits, "class": GrackleCodeUnits,
"size": 1 "size": 1
} }
return { return {
......
...@@ -12,6 +12,17 @@ import numpy ...@@ -12,6 +12,17 @@ import numpy
os.environ["CC"] = "mpicc" os.environ["CC"] = "mpicc"
cflags = ["-Werror",
"-Wall",
"-Wextra",
# disables some warnings due to python
"-Wno-unused-parameter",
"-Wno-strict-prototypes",
"-Wno-unused-function",
"-Wno-incompatible-pointer-types",
"-Wno-missing-field-initializers",
]
# deal with arguments # deal with arguments
def parseCmdLine(arg, store=False): def parseCmdLine(arg, store=False):
ret = False ret = False
...@@ -96,7 +107,8 @@ ext_modules = Extension("pyswiftsim.wrapper", ...@@ -96,7 +107,8 @@ ext_modules = Extension("pyswiftsim.wrapper",
c_src, c_src,
include_dirs=include, include_dirs=include,
libraries=lib, libraries=lib,
library_dirs=lib_dir) library_dirs=lib_dir,
extra_compile_args=cflags)
ext_modules = [ext_modules] ext_modules = [ext_modules]
......
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
#include "pyswiftsim_tools.h" #include "pyswiftsim_tools.h"
#include <config.h>
/** /**
* @brief give the cooling type name * @brief give the cooling type name
* *
......
#include "pyswiftsim_tools.h" #include "pyswiftsim_tools.h"
#include "cooling_wrapper.h" #include "cooling_wrapper.h"
#include <cooling.h>
#include <cooling_struct.h>
#include <equation_of_state.h>
#include <numpy/arrayobject.h>
PyObject* pycooling_init(PyObject* self, PyObject* args) { PyObject* pycooling_init(PyObject* self, PyObject* args) {
PyObject *pyparams; PyObject *pyparams;
...@@ -43,7 +36,7 @@ PyObject* pycooling_init(PyObject* self, PyObject* args) { ...@@ -43,7 +36,7 @@ PyObject* pycooling_init(PyObject* self, PyObject* args) {
} }
PyArrayObject* pycooling_rate(PyObject* self, PyObject* args) { PyArrayObject* pycooling_rate(PyObject* self, PyObject* args) {
IMPORT_ARRAY(); import_array();
PyObject *pycooling; PyObject *pycooling;
PyObject *pyus; PyObject *pyus;
...@@ -98,8 +91,10 @@ PyArrayObject* pycooling_rate(PyObject* self, PyObject* args) { ...@@ -98,8 +91,10 @@ PyArrayObject* pycooling_rate(PyObject* self, PyObject* args) {
return NULL; return NULL;
struct part p; struct part p;
struct xpart xp;
#ifdef COOLING_GRACKLE #ifdef COOLING_GRACKLE
/* set grackle_data */
grackle_data = &cooling->chemistry; grackle_data = &cooling->chemistry;
#endif #endif
...@@ -114,12 +109,14 @@ PyArrayObject* pycooling_rate(PyObject* self, PyObject* args) { ...@@ -114,12 +109,14 @@ PyArrayObject* pycooling_rate(PyObject* self, PyObject* args) {
float u = *(float*) PyArray_GETPTR1(energy, i); float u = *(float*) PyArray_GETPTR1(energy, i);
p.entropy = gas_entropy_from_internal_energy(p.rho, u); p.entropy = gas_entropy_from_internal_energy(p.rho, u);
cooling_first_init_part(&p, &xp, cooling);
/* compute cooling rate */ /* compute cooling rate */
float *tmp = PyArray_GETPTR1(rate, i); float *tmp = PyArray_GETPTR1(rate, i);
#ifdef COOLING_GRACKLE #ifdef COOLING_GRACKLE
*tmp = cooling_rate(pconst, us, cooling, &p, dt); *tmp = cooling_rate(pconst, us, cooling, &p, &xp, dt);
#else #else
*tmp = cooling_rate(pconst, us, cooling, &p); *tmp = cooling_rate(pconst, us, cooling, &p, &xp);
#endif #endif
} }
......
#include "parser_wrapper.h" #include "parser_wrapper.h"
#include "pyswiftsim_tools.h" #include "pyswiftsim_tools.h"
#include <parser.h>
PyObject* pyparser_read_file(PyObject *self, PyObject *args) PyObject* pyparser_read_file(PyObject *self, PyObject *args)
{ {
......
#include "pyswiftsim_tools.h" #include "pyswiftsim_tools.h"
#include <part.h>
#include <hydro.h>
#include <Python.h> #include <Python.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
#define __PYSWIFTSIM_PART_H__ #define __PYSWIFTSIM_PART_H__
#include <Python.h> #include <Python.h>
#include <part.h>
/** /**
* @brief Test function for the struct * @brief Test function for the struct
......
#include "pyswiftsim_tools.h" #include "pyswiftsim_tools.h"
/* include swift */
#include <part.h>
#include <units.h>
#include <parser.h>
#include <physical_constants.h>
#include <cooling_struct.h>
#include <Python.h>
#include <numpy/arrayobject.h>
const size_t class_size[class_count] = { const size_t class_size[class_count] = {
sizeof(struct unit_system), sizeof(struct unit_system),
...@@ -19,7 +9,7 @@ const size_t class_size[class_count] = { ...@@ -19,7 +9,7 @@ const size_t class_size[class_count] = {
sizeof(struct cooling_function_data) sizeof(struct cooling_function_data)
}; };
const char *class_name[class_count] = { char *class_name[class_count] = {
"UnitSystem", "UnitSystem",
"Part", "Part",
"SwiftParams", "SwiftParams",
...@@ -69,7 +59,6 @@ PyObject* pytools_return(void *p, int class) ...@@ -69,7 +59,6 @@ PyObject* pytools_return(void *p, int class)
size_t nber_bytes; size_t nber_bytes;
char module_name[STRING_SIZE] = "pyswiftsim.structure"; char module_name[STRING_SIZE] = "pyswiftsim.structure";
char *class_pyname;
/* check class */ /* check class */
if (class >= class_count) if (class >= class_count)
...@@ -77,7 +66,7 @@ PyObject* pytools_return(void *p, int class) ...@@ -77,7 +66,7 @@ PyObject* pytools_return(void *p, int class)
/* get class information */ /* get class information */
nber_bytes = class_size[class]; nber_bytes = class_size[class];
class_pyname = class_name[class]; char *class_pyname = class_name[class];
/* import python class */ /* import python class */
python_class = pytools_import(module_name, class_pyname); python_class = pytools_import(module_name, class_pyname);
...@@ -126,8 +115,8 @@ char* pytools_get_type_name(PyObject *obj) ...@@ -126,8 +115,8 @@ char* pytools_get_type_name(PyObject *obj)
} }
/* transform to C */ /* transform to C */
size_t size; Py_ssize_t size;
char *name = PyUnicode_AsUTF8AndSize(recv, size); char *name = PyUnicode_AsUTF8AndSize(recv, &size);
Py_DECREF(recv); Py_DECREF(recv);
if (name == NULL) if (name == NULL)
...@@ -155,9 +144,9 @@ char* pytools_construct(PyObject* obj, int class) ...@@ -155,9 +144,9 @@ char* pytools_construct(PyObject* obj, int class)
PyObject *pyclass = pytools_import(module_name, class_pyname); PyObject *pyclass = pytools_import(module_name, class_pyname);
/* check if classes correspond */ /* check if classes correspond */
int test = !PyObject_IsInstance(obj, pyclass); int test = PyObject_IsInstance(obj, pyclass);
Py_DECREF(pyclass); Py_DECREF(pyclass);
if (test) if (!test)
{ {
char *recv = pytools_get_type_name(obj); char *recv = pytools_get_type_name(obj);
if (recv == NULL) if (recv == NULL)
...@@ -181,8 +170,7 @@ char* pytools_construct(PyObject* obj, int class) ...@@ -181,8 +170,7 @@ char* pytools_construct(PyObject* obj, int class)
int pytools_check_array(PyArrayObject *obj, int dim, int type) int pytools_check_array(PyArrayObject *obj, int dim, int type)
{ {
/* ensure to have numpy arrays */ IMPORT_ARRAY1(1);
IMPORT_ARRAY();
/* check if array */ /* check if array */
if (!PyArray_Check(obj)) if (!PyArray_Check(obj))
......
...@@ -6,18 +6,12 @@ ...@@ -6,18 +6,12 @@
#include <Python.h> #include <Python.h>
#include <numpy/arrayobject.h> #include <numpy/arrayobject.h>
#undef assert
#include <swift.h>
#define DIM 3 #define DIM 3
#define STRING_SIZE 200 #define STRING_SIZE 200
/* Check if arrays are imported and if not import them */
#define IMPORT_ARRAY() \
({ \
if (PyArray_API == NULL) \
{ \
import_array(); \
} \
})
/* Set the error message for python (still need to return NULL) */ /* Set the error message for python (still need to return NULL) */
#define pyerror(s, ...) \ #define pyerror(s, ...) \
({ \ ({ \
...@@ -37,6 +31,17 @@ ...@@ -37,6 +31,17 @@
__FUNCTION__, __LINE__, ##__VA_ARGS__); \ __FUNCTION__, __LINE__, ##__VA_ARGS__); \
}) })
#define IMPORT_ARRAY1(ret) \
{ \
if (PyArray_API == NULL) \
{ \
import_array1(ret); \
} \
}
#define IMPORT_ARRAY() IMPORT_ARRAY1(NUMPY_IMPORT_ARRAY_RETVAL);
/* Enum swift classes */ /* Enum swift classes */
enum swift_class { enum swift_class {
class_unit_system, class_unit_system,
...@@ -50,7 +55,7 @@ enum swift_class { ...@@ -50,7 +55,7 @@ enum swift_class {
/* size of each structure in enum swift_class */ /* size of each structure in enum swift_class */
extern const size_t class_size[]; extern const size_t class_size[];
/* name of each Python class representing a swift class */ /* name of each Python class representing a swift class */
extern const char *class_name[]; extern char *class_name[];
/* error code in pyswiftsim */ /* error code in pyswiftsim */
enum error_code { enum error_code {
......
#include "pyswiftsim_tools.h" #include "pyswiftsim_tools.h"
#include <units.h>
#include <parser.h>
#include <physical_constants.h>
#include <Python.h> #include <Python.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -38,7 +34,7 @@ PyObject* pyunit_system_init(PyObject *self, PyObject *args) ...@@ -38,7 +34,7 @@ PyObject* pyunit_system_init(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "O", &parser)) if (!PyArg_ParseTuple(args, "O", &parser))
return NULL; return NULL;
struct swift_params *params = pytools_construct(parser, class_swift_params); struct swift_params *params = (struct swift_params*) pytools_construct(parser, class_swift_params);
if (params == NULL) if (params == NULL)
return NULL; return NULL;
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#include "pyswiftsim_tools.h" #include "pyswiftsim_tools.h"
#include "config_wrapper.h" #include "config_wrapper.h"
#include <Python.h> #include <Python.h>
#include <math.h> #include <math.h>
#include <numpy/arrayobject.h> #include <numpy/arrayobject.h>
......
...@@ -19,8 +19,8 @@ gamma = 5. / 3. ...@@ -19,8 +19,8 @@ gamma = 5. / 3.
filename = "test/cooling.yml" filename = "test/cooling.yml"
# number of points # number of points
N_rho = 300 N_rho = 1
N_T = 300 N_T = 100
# density in atom / cm3 # density in atom / cm3
if N_rho == 1: if N_rho == 1:
...@@ -49,10 +49,13 @@ T = T.reshape(-1) ...@@ -49,10 +49,13 @@ T = T.reshape(-1)
# #
# parse swift params # parse swift params
print("Reading parameters")
params = wrapper.parserReadFile(filename) params = wrapper.parserReadFile(filename)
# init units # init units
print("Initialization of the unit system")
us, pconst = wrapper.unitSystemInit(params) us, pconst = wrapper.unitSystemInit(params)
# init cooling # init cooling
print("Initialization of the cooling")
cooling = wrapper.coolingInit(params, us, pconst) cooling = wrapper.coolingInit(params, us, pconst)
...@@ -74,10 +77,12 @@ dt /= us.UnitTime_in_cgs ...@@ -74,10 +77,12 @@ dt /= us.UnitTime_in_cgs
# #
# du / dt # du / dt
print("Computing cooling...")
rate = wrapper.coolingRate(pconst, us, cooling, rate = wrapper.coolingRate(pconst, us, cooling,
rho.astype(np.float32), rho.astype(np.float32),
energy.astype(np.float32), energy.astype(np.float32),
dt) dt)
print("Computing done")
# #
# plot # plot
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment