Skip to content

Commit

Permalink
python wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholaswogan committed Nov 10, 2023
1 parent 6d41eb0 commit f9d324f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
16 changes: 16 additions & 0 deletions photochem/cython/PhotochemVars.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ cdef class PhotochemVars:
cdef ndarray arr = np.empty((dim1, dim2), np.double, order="F")
var_pxd.photochemvars_usol_init_get(&self._ptr, &dim1, &dim2, <double *>arr.data)
return arr

property relative_humidity:
"double. Relative humidity of H2O in the troposphere."
def __get__(self):
cdef double val
var_pxd.photochemvars_relative_humidity_get(&self._ptr, &val)
return val
def __set__(self, double val):
var_pxd.photochemvars_relative_humidity_set(&self._ptr, &val)

property z:
"ndarray[double,dim=1], shape (nz). The altitude of the center of each atmopsheric layer (cm)"
Expand Down Expand Up @@ -99,6 +108,13 @@ cdef class PhotochemVars:
cdef ndarray arr = np.empty(dim1, np.double)
var_pxd.photochemvars_edd_get(&self._ptr, &dim1, <double *>arr.data)
return arr
def __set__(self, ndarray[double, ndim=1] edd_new_):
cdef int dim1
var_pxd.photochemvars_edd_get_size(&self._ptr, &dim1)
cdef ndarray edd_new = np.asfortranarray(edd_new_)
if edd_new.shape[0] != dim1:
raise PhotoException("Input edd is the wrong size.")
var_pxd.photochemvars_edd_set(&self._ptr, &dim1, <double *>edd_new.data)

property custom_binary_diffusion_fcn:
"A function for specifying a custom binary diffusion parameter (b_ij)"
Expand Down
4 changes: 4 additions & 0 deletions photochem/cython/PhotochemVars_pxd.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ cdef extern void photochemvars_at_photo_equilibrium_get(void *ptr, bool *at_phot
cdef extern void photochemvars_usol_init_get_size(void *ptr, int *dim1, int *dim2)
cdef extern void photochemvars_usol_init_get(void *ptr, int *dim1, int *dim2, double *usol_init)

cdef extern void photochemvars_relative_humidity_get(void *ptr, double *val)
cdef extern void photochemvars_relative_humidity_set(void *ptr, double *val)

cdef extern void photochemvars_photon_flux_fcn_set(void *ptr, time_dependent_flux_fcn fcn)

cdef extern void photochemvars_temperature_get_size(void *ptr, int *dim1)
cdef extern void photochemvars_temperature_get(void *ptr, int *dim1, double *temperature)

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_edd_set(void *ptr, int *dim1, double *arr)

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

Expand Down
25 changes: 25 additions & 0 deletions photochem/fortran/PhotochemVars_wrapper.f90
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ subroutine photochemvars_usol_init_get(ptr, dim1, dim2, usol_init) bind(c)
usol_init = var%usol_init
end subroutine

subroutine photochemvars_relative_humidity_get(ptr, val) bind(c)
type(c_ptr), intent(in) :: ptr
real(c_double), intent(out) :: val
type(PhotochemVars), pointer :: var
call c_f_pointer(ptr, var)
val = var%relative_humidity
end subroutine

subroutine photochemvars_relative_humidity_set(ptr, val) bind(c)
type(c_ptr), intent(in) :: ptr
real(c_double), intent(in) :: val
type(PhotochemVars), pointer :: var
call c_f_pointer(ptr, var)
var%relative_humidity = val
end subroutine

subroutine photochemvars_photon_flux_fcn_set(ptr, photon_flux_fcn_c) bind(c)
use photochem_types, only: time_dependent_flux_fcn
type(c_ptr), intent(in) :: ptr
Expand Down Expand Up @@ -119,6 +135,15 @@ subroutine photochemvars_edd_get(ptr, dim1, arr) bind(c)
arr = var%edd
end subroutine

subroutine photochemvars_edd_set(ptr, dim1, arr) bind(c)
type(c_ptr), intent(in) :: ptr
integer(c_int), intent(in) :: dim1
real(c_double), intent(in) :: arr(dim1)
type(PhotochemVars), pointer :: var
call c_f_pointer(ptr, var)
var%edd = arr
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
Expand Down
3 changes: 1 addition & 2 deletions src/input/photochem_input_read.f90
Original file line number Diff line number Diff line change
Expand Up @@ -745,14 +745,13 @@ subroutine unpack_settings(infile, s, dat, var, err)

if (dat%fix_water_in_trop) then

allocate(var%relative_humidity)
read(s%relative_humidity,*,iostat = io) var%relative_humidity

if (io /= 0) then
! it isn't a float
if (trim(s%relative_humidity) == "manabe") then
var%use_manabe = .true.
deallocate(var%relative_humidity)
var%relative_humidity = -1.0
else
err = '"relative-humidity" can only be a number between 0 and 1, or "manabe". See '//trim(infile)
return
Expand Down
2 changes: 1 addition & 1 deletion src/photochem_types.f90
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ subroutine time_dependent_rate_fcn(tn, nz, rate)
real(dp) :: rainfall_rate !! relative to modern Earth's average rainfall rate of 1.1e17 molecules/cm2/s
integer :: trop_ind !! index of troposphere (only for fix_water_in_trop == true or gas_rainout == true)
logical :: use_manabe !! use manabe formula
real(dp), allocatable :: relative_humidity !! relative humidity if no manabe
real(dp) :: relative_humidity !! relative humidity if no manabe
real(dp) :: H2O_condensation_rate(3) !! H2O condesation rate parameters

! radiative transfer
Expand Down

0 comments on commit f9d324f

Please sign in to comment.