Commit 11cc3475 authored by lhausamm's avatar lhausamm
Browse files

Init python wrapper

parent 7bcc6455
#!/usr/bin/env python3
__author__ = "loic hausammann"
__copyright__ = "GPLv3"
descr = "Swift is a cosmological hydrodynamical code: SPH With Inter-dependent Fine-grained Tasking"
# import
from setuptools import setup, find_packages, Extension
from Cython.Build import cythonize
import numpy
import os
makefile = "../src/Makefile"
def getValueFromMakefile(f, value):
test = value + " = "
ret = None
for line in f:
start = line[:len(test)]
if test == start:
ret = line[len(test):]
if ret is None:
raise Exception("Unable to find value %s" % value)
# remove last character "\n"
return ret[:-1]
# python requirement
install_requires = []
# include
with open(makefile, "r") as f:
# need to remove \n
hdf5_include = getValueFromMakefile(f, "H5CC") + "/include"
cc = getValueFromMakefile(f, "CC")
include = [
# libraries
lib = ["m", "hdf5", "-L../src/.libs/", "swiftsim"]
# Extension object required by setup
ext = []
# cooling wrapper
tmp = Extension("cooling",
tmp = cythonize(tmp)
# scripts
scripts = []
# data to copy
data = ["../src/.libs/"]
old_cc = ""
if "CC" in os.environ:
old_cc = os.environ['CC']
os.environ["CC"] = cc
name = "pyswiftsim",
version = "0.6.0",
author = "Hausammann Loic",
author_email = "",
description = descr,
license = "GPLv3",
keywords = "swift hpc cosmology",
url = "",
packages = find_packages(),
data_files = data,
scripts = scripts,
install_requires = install_requires,
ext_modules = ext,
os.environ["CC"] = old_cc
cdef extern from "clocks.h":
void clocks_set_cpufreq(unsigned long long freq);
cdef extern from "parser.h":
struct swift_params:
void parser_read_file(const char *file_name, swift_params *params);
void parser_print_params(const swift_params *params);
cdef extern from "units.h":
struct unit_system:
void units_init(unit_system* us, const swift_params* params,
const char* category)
cdef extern from "physical_constants.h":
struct phys_const:
void phys_const_init(unit_system* us, phys_const* internal_const);
cdef extern from "cooling_struct.h":
struct cooling_function_data:
cdef extern from "part.h":
struct part:
struct xpart:
cdef extern from "hydro_space.h":
struct hydro_space:
cdef extern from "hydro.h":
void hydro_init_part(
part *p, const hydro_space *hs)
cdef extern from "debug.h":
void printParticle(const part *parts, const xpart *xparts,
long long int id, size_t N);
cdef extern from "cooling.h":
void cooling_init(const swift_params* parameter_file,
const unit_system* us,
const phys_const* phys_const,
cooling_function_data* cooling);
void cooling_print_backend(
const cooling_function_data* cooling)
void cooling_cool_part(
const phys_const* phys_const,
const unit_system* us,
const cooling_function_data* cooling,
part* p, xpart* xp, float dt)
clocks_set_cpufreq(0); # estimate automatically cpufreq and init time
# pointer types
cdef str _SWIFT_PARAMS = "swift_params"
cdef str _UNIT_SYSTEM = "unit_system"
cdef str _PHYS_CONST = "phys_const"
cdef str _COOLING_FUNCTION_DATA = "cooling_function_data"
# wrapper around C pointers
cdef class Pointer:
cdef void *_data
cdef str _data_type
def __cinit__(self):
self._data = NULL
cdef _setup(self, void *params, str data_type):
self._data = params
self._data_type = data_type
return self
def __str__(self):
return self._data_type
cdef pointer_create(void *params, str data_type):
return Pointer()._setup(params, data_type)
cdef check_pointer(Pointer ptr, str data_type):
if (ptr._data_type != data_type):
raise Exception("Expecting pointer of type %s, got %s" % (data_type, ptr._data_type))
if (ptr._data == NULL):
raise Exception("Value not set for pointer type %s" % ptr._data_type)
def read_params(str filename):
tmp = filename.encode(u"ascii")
cdef char *cfilename = tmp
if cfilename == NULL:
raise Exception("Unable to convert filename to char*")
# init params
cdef swift_params params;
parser_read_file(cfilename, &params)
return pointer_create(&params, _SWIFT_PARAMS)
def init_units(Pointer p_params):
# deal with inputs
check_pointer(p_params, _SWIFT_PARAMS)
cdef swift_params *params = <swift_params*> p_params._data
# init units
cdef unit_system units;
cdef const char* category = "InternalUnitSystem"
units_init(&units, params, category)
# init constants
cdef phys_const constants;
phys_const_init(&units, &constants)
# return
d = {}
d["constants"] = pointer_create(&constants, _PHYS_CONST)
d["units"] = pointer_create(&units, _UNIT_SYSTEM)
return d
def pycooling_init(Pointer p_params,
Pointer p_units,
Pointer p_constants):
# deal with inputs
check_pointer(p_params, _SWIFT_PARAMS)
cdef swift_params *params = <swift_params*> p_params._data
check_pointer(p_units, _UNIT_SYSTEM)
cdef unit_system *units = <unit_system*> p_units._data
check_pointer(p_constants, _PHYS_CONST)
cdef phys_const *constants = <phys_const*> p_constants._data
# init cooling
cdef cooling_function_data cooling;
cooling_init(params, units, constants, &cooling)
# print results
# store and return results
return pointer_create(&cooling, _COOLING_FUNCTION_DATA)
def pycooling_rate(Pointer p_params):
# deal with inputs
check_pointer(p_params, _SWIFT_PARAMS)
cdef swift_params *params = <swift_params*> p_params._data
cdef part p
cdef xpart xp
cdef hydro_space hs # shadowfax stuff => no need to initialize
hydro_init_part(&p, &hs)
printParticle(&p, &xp, 0, 1)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment