Skip to content

Commit

Permalink
moved function pointers to uintptr_t
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholaswogan committed Jun 12, 2023
1 parent 79425a9 commit a03cbca
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
46 changes: 30 additions & 16 deletions photochem/cython/Atmosphere.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,23 @@ cdef class Atmosphere:
fcn : function
A Numba cfunc that describes the time-dependent photon flux
"""
cdef uintptr_t fcn_l
cdef a_pxd.time_dependent_flux_fcn fcn_c

argtypes = (ct.c_double, ct.c_int32, ct.POINTER(ct.c_double))
restype = None
if not fcn.ctypes.argtypes == argtypes:
raise PhotoException("The callback function has the wrong argument types.")
if not fcn.ctypes.restype == restype:
raise PhotoException("The callback function has the wrong return type.")
if fcn is None:
fcn_l = 0
fcn_c = NULL
else:
argtypes = (ct.c_double, ct.c_int32, ct.POINTER(ct.c_double))
restype = None
if not fcn.ctypes.argtypes == argtypes:
raise PhotoException("The callback function has the wrong argument types.")
if not fcn.ctypes.restype == restype:
raise PhotoException("The callback function has the wrong return type.")

fcn_l = fcn.address
fcn_c = <a_pxd.time_dependent_flux_fcn> fcn_l

cdef unsigned long long int fcn_l = <unsigned long long int> fcn.address
cdef a_pxd.time_dependent_flux_fcn fcn_c = <a_pxd.time_dependent_flux_fcn> fcn_l
a_pxd.atmosphere_set_photon_flux_fcn_wrapper(&self._ptr, fcn_c)

def set_rate_fcn(self, str species, object fcn):
Expand All @@ -486,16 +493,23 @@ cdef class Atmosphere:
cdef bytes species_b = pystring2cstring(species)
cdef char *species_c = species_b
cdef char err[ERR_LEN+1]
cdef uintptr_t fcn_l
cdef a_pxd.time_dependent_rate_fcn fcn_c

argtypes = (ct.c_double, ct.c_int32, ct.POINTER(ct.c_double))
restype = None
if not fcn.ctypes.argtypes == argtypes:
raise PhotoException("The callback function has the wrong argument types.")
if not fcn.ctypes.restype == restype:
raise PhotoException("The callback function has the wrong return type.")
if fcn is None:
fcn_l = 0
fcn_c = NULL
else:
argtypes = (ct.c_double, ct.c_int32, ct.POINTER(ct.c_double))
restype = None
if not fcn.ctypes.argtypes == argtypes:
raise PhotoException("The callback function has the wrong argument types.")
if not fcn.ctypes.restype == restype:
raise PhotoException("The callback function has the wrong return type.")

cdef unsigned long long int fcn_l = <unsigned long long int> fcn.address
cdef a_pxd.time_dependent_rate_fcn fcn_c = <a_pxd.time_dependent_rate_fcn> fcn_l
fcn_l = fcn.address
fcn_c = <a_pxd.time_dependent_rate_fcn> fcn_l

a_pxd.atmosphere_set_rate_fcn_wrapper(&self._ptr, species_c, fcn_c, err)
if len(err.strip()) > 0:
raise PhotoException(err.decode("utf-8").strip())
24 changes: 16 additions & 8 deletions photochem/cython/EvoAtmosphere.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,23 @@ cdef class EvoAtmosphere:
fcn : function
A Numba cfunc that describes the temperature dependent surface albedo
"""
argtypes = (ct.c_double,)
restype = ct.c_double
if not fcn.ctypes.argtypes == argtypes:
raise PhotoException("The callback function has the wrong argument types.")
if not fcn.ctypes.restype == restype:
raise PhotoException("The callback function has the wrong return type.")
cdef uintptr_t fcn_l
cdef ea_pxd.temp_dependent_albedo_fcn fcn_c

cdef unsigned long long int fcn_l = <unsigned long long int> fcn.address
cdef ea_pxd.temp_dependent_albedo_fcn fcn_c = <ea_pxd.temp_dependent_albedo_fcn> fcn_l
if fcn is None:
fcn_l = 0
fcn_c = NULL
else:
argtypes = (ct.c_double,)
restype = ct.c_double
if not fcn.ctypes.argtypes == argtypes:
raise PhotoException("The callback function has the wrong argument types.")
if not fcn.ctypes.restype == restype:
raise PhotoException("The callback function has the wrong return type.")

fcn_l = fcn.address
fcn_c = <ea_pxd.temp_dependent_albedo_fcn> fcn_l

ea_pxd.evoatmosphere_set_albedo_fcn_wrapper(&self._ptr, fcn_c)

property T_surf:
Expand Down
1 change: 1 addition & 0 deletions photochem/cython/_photochem.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from numpy cimport ndarray
from libcpp cimport bool
from libc.stdint cimport uintptr_t
import numpy as np
import ctypes as ct
import os
Expand Down

0 comments on commit a03cbca

Please sign in to comment.