Skip to content

Commit

Permalink
python wrapper for custom binary diffusion fcn
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholaswogan committed Nov 10, 2023
1 parent 02bf66b commit 6d41eb0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions photochem/cython/PhotochemVars.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ cdef class PhotochemVars:
cdef ndarray arr = np.empty(dim1, np.double)
var_pxd.photochemvars_edd_get(&self._ptr, &dim1, <double *>arr.data)
return arr

property custom_binary_diffusion_fcn:
"A function for specifying a custom binary diffusion parameter (b_ij)"
def __set__(self, object fcn):
cdef uintptr_t fcn_l
cdef var_pxd.binary_diffusion_fcn fcn_c
if fcn is None:
fcn_l = 0
fcn_c = NULL
else:
argtypes = (ct.c_double, ct.c_double, 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 = <var_pxd.binary_diffusion_fcn> fcn_l

var_pxd.photochemvars_custom_binary_diffusion_fcn_set(&self._ptr, fcn_c)

property photon_flux:
"ndarray[double,dim=1], shape (nw). photon/cm^2/s in each wavelength bin hitting planet."
Expand Down
3 changes: 3 additions & 0 deletions photochem/cython/PhotochemVars_pxd.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cdef extern from "<stdbool.h>":

# callback signatures
ctypedef void (*time_dependent_flux_fcn)(double tn, int nw, double *photon_flux)
ctypedef double (*binary_diffusion_fcn)(double mu_i, double mubar, double T)

cdef extern void allocate_photochemvars(void *ptr)
cdef extern void deallocate_photochemvars(void *ptr)
Expand All @@ -27,6 +28,8 @@ cdef extern void photochemvars_temperature_get(void *ptr, int *dim1, double *tem
cdef extern void photochemvars_edd_get_size(void *ptr, int *dim1)
cdef extern void photochemvars_edd_get(void *ptr, int *dim1, double *arr)

cdef extern void photochemvars_custom_binary_diffusion_fcn_set(void *ptr, binary_diffusion_fcn fcn)

cdef extern void photochemvars_photon_flux_get_size(void *ptr, int *dim1)
cdef extern void photochemvars_photon_flux_get(void *ptr, int *dim1, double *arr)

Expand Down
14 changes: 14 additions & 0 deletions photochem/fortran/PhotochemVars_wrapper.f90
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,20 @@ subroutine photochemvars_edd_get(ptr, dim1, arr) bind(c)
call c_f_pointer(ptr, var)
arr = var%edd
end subroutine

subroutine photochemvars_custom_binary_diffusion_fcn_set(ptr, fcn_c) bind(c)
use photochem_types, only: binary_diffusion_fcn
type(c_ptr), intent(in) :: ptr
type(c_funptr), value, intent(in) :: fcn_c

procedure(binary_diffusion_fcn), pointer :: fcn_f
type(PhotochemVars), pointer :: var

call c_f_pointer(ptr, var)
call c_f_procpointer(fcn_c, fcn_f)
var%custom_binary_diffusion_fcn => fcn_f

end subroutine

subroutine photochemvars_photon_flux_get_size(ptr, dim1) bind(c)
type(c_ptr), intent(in) :: ptr
Expand Down

0 comments on commit 6d41eb0

Please sign in to comment.