Skip to content

Commit

Permalink
got rid of function for time dependent photons
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholaswogan committed Nov 10, 2023
1 parent 8bb2926 commit 1634881
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 90 deletions.
28 changes: 0 additions & 28 deletions photochem/cython/Atmosphere.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -450,34 +450,6 @@ cdef class Atmosphere:
if len(err.strip()) > 0:
raise PhotoException(err.decode("utf-8").strip())

def set_photon_flux_fcn(self, object fcn):
"""Sets a function describing a time-dependent photon flux.
This is useful for modeling flares.
Parameters
----------
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

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

a_pxd.atmosphere_set_photon_flux_fcn_wrapper(&self._ptr, fcn_c)

def set_rate_fcn(self, str species, object fcn):
"""Sets a function describing a custom rate for a species.
This could be useful for modeling external processes not in the
Expand Down
3 changes: 0 additions & 3 deletions photochem/cython/Atmosphere_pxd.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ cdef extern from "<stdbool.h>":
pass

# callback signatures
ctypedef void (*time_dependent_flux_fcn)(double tn, int nw, double *photon_flux)
ctypedef void (*time_dependent_rate_fcn)(double tn, int nz, double *rate)

# allocate and destroy
Expand Down Expand Up @@ -45,6 +44,4 @@ cdef extern void atmosphere_evolve_wrapper(void *ptr, char *filename,
cdef extern void atmosphere_set_temperature_wrapper(void *ptr, int *nz, double *temperature,
double *trop_alt, bool *trop_alt_present, char *err)

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

cdef extern void atmosphere_set_rate_fcn_wrapper(void *ptr, char *species_c, time_dependent_rate_fcn fcn, char *err)
22 changes: 21 additions & 1 deletion photochem/cython/PhotochemVars.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,27 @@ cdef class PhotochemVars:
cdef ndarray arr = np.empty(dim1, np.double)
var_pxd.photochemvars_z_get(&self._ptr, &dim1, <double *>arr.data)
return arr


property photon_flux_fcn:
"A function for altering the photon flux over time"
def __set__(self, object fcn):
cdef uintptr_t fcn_l
cdef var_pxd.time_dependent_flux_fcn fcn_c
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 = <var_pxd.time_dependent_flux_fcn> fcn_l

var_pxd.photochemvars_photon_flux_fcn_set(&self._ptr, fcn_c)

property temperature:
"ndarray[double,dim=1], shape (nz). The temperature of each atmospheric layer (K)"
def __get__(self):
Expand Down
5 changes: 5 additions & 0 deletions photochem/cython/PhotochemVars_pxd.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ from libcpp cimport bool
cdef extern from "<stdbool.h>":
pass

# callback signatures
ctypedef void (*time_dependent_flux_fcn)(double tn, int nw, double *photon_flux)

cdef extern void allocate_photochemvars(void *ptr)
cdef extern void deallocate_photochemvars(void *ptr)

Expand All @@ -16,6 +19,8 @@ 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_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)

Expand Down
15 changes: 0 additions & 15 deletions photochem/fortran/Atmosphere_wrapper.f90
Original file line number Diff line number Diff line change
Expand Up @@ -441,21 +441,6 @@ subroutine atmosphere_set_temperature_wrapper(ptr, nz, temperature, &

end subroutine

subroutine atmosphere_set_photon_flux_fcn_wrapper(ptr, photon_flux_fcn_c) bind(c)
use photochem_types, only: time_dependent_flux_fcn
type(c_ptr), intent(in) :: ptr
type(c_funptr), value, intent(in) :: photon_flux_fcn_c

procedure(time_dependent_flux_fcn), pointer :: photon_flux_fcn_f

type(Atmosphere), pointer :: pc

call c_f_pointer(ptr, pc)
call c_f_procpointer(photon_flux_fcn_c, photon_flux_fcn_f)
call pc%set_photon_flux_fcn(photon_flux_fcn_f)

end subroutine

subroutine atmosphere_set_rate_fcn_wrapper(ptr, species_c, fcn_c, err) bind(c)
use photochem_types, only: time_dependent_rate_fcn
type(c_ptr), intent(in) :: ptr
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 @@ -70,6 +70,20 @@ subroutine photochemvars_usol_init_get(ptr, dim1, dim2, usol_init) bind(c)
call c_f_pointer(ptr, var)
usol_init = var%usol_init
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
type(c_funptr), value, intent(in) :: photon_flux_fcn_c

procedure(time_dependent_flux_fcn), pointer :: photon_flux_fcn_f
type(PhotochemVars), pointer :: var

call c_f_pointer(ptr, var)
call c_f_procpointer(photon_flux_fcn_c, photon_flux_fcn_f)
var%photon_flux_fcn => photon_flux_fcn_f

end subroutine

subroutine photochemvars_temperature_get_size(ptr, dim1) bind(c)
type(c_ptr), intent(in) :: ptr
Expand Down
9 changes: 0 additions & 9 deletions src/atmosphere/photochem_atmosphere.f90
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ module photochem_atmosphere
procedure :: set_lower_bc
procedure :: set_upper_bc
procedure :: set_temperature
procedure :: set_photon_flux_fcn
procedure :: set_rate_fcn
end type
interface Atmosphere
Expand Down Expand Up @@ -271,14 +270,6 @@ module subroutine set_temperature(self, temperature, trop_alt, err)
character(:), allocatable, intent(out) :: err
end subroutine

!> Sets a function describing a time-dependent photon flux.
!> This is useful for modeling flares.
module subroutine set_photon_flux_fcn(self, photon_flux_fcn)
use photochem_types, only: time_dependent_flux_fcn
class(Atmosphere), target, intent(inout) :: self
procedure(time_dependent_flux_fcn), pointer :: photon_flux_fcn
end subroutine

!> Sets a function describing a custom rate for a species.
!> This could be useful for modeling external processes not in the
!> model.
Expand Down
7 changes: 0 additions & 7 deletions src/atmosphere/photochem_atmosphere_utils.f90
Original file line number Diff line number Diff line change
Expand Up @@ -605,13 +605,6 @@ module subroutine set_temperature(self, temperature, trop_alt, err)

end subroutine

module subroutine set_photon_flux_fcn(self, photon_flux_fcn)
use photochem_types, only: time_dependent_flux_fcn
class(Atmosphere), target, intent(inout) :: self
procedure(time_dependent_flux_fcn), pointer :: photon_flux_fcn
self%var%photon_flux_fcn => photon_flux_fcn
end subroutine

module subroutine set_rate_fcn(self, species, fcn, err)
use photochem_types, only: time_dependent_rate_fcn
class(Atmosphere), target, intent(inout) :: self
Expand Down
28 changes: 1 addition & 27 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,8 @@ def main():
"../templates/ModernEarth/atmosphere_ModernEarth.txt")

pc.initialize_stepper(pc.wrk.usol)
tn = 0
while tn < 1e17:
tn = pc.step()
tn = pc.step()
pc.destroy_stepper()

con = pc.redox_conservation()
print('redox conservation = '+'%.2e'%con)
assert con < 1e-4

con = pc.atom_conservation('C').factor
print('C conservation = '+'%.2e'%con)
assert con < 1e-6

con = pc.atom_conservation('S').factor
print('S conservation = '+'%.2e'%con)
assert con < 1e-6

con = pc.atom_conservation('H').factor
print('H conservation = '+'%.2e'%con)
assert con < 1e-6

con = pc.atom_conservation('O').factor
print('O conservation = '+'%.2e'%con)
assert con < 1e-6

con = pc.atom_conservation('Cl').factor
print('Cl conservation = '+'%.2e'%con)
assert con < 1e-6

if __name__ == "__main__":
main()

0 comments on commit 1634881

Please sign in to comment.