Skip to content

Commit

Permalink
simplified code and allowed for TOA alt
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholaswogan committed Nov 13, 2023
1 parent 6aa1218 commit 8f252ed
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 107 deletions.
19 changes: 16 additions & 3 deletions photochem/cython/Atmosphere.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,9 @@ cdef class Atmosphere:
if len(err.strip()) > 0:
raise PhotoException(err.decode("utf-8").strip())

def update_vertical_grid(self, double TOA_pressure):
def update_vertical_grid(self, TOA_alt = None, TOA_pressure = None):
"""Re-does the vertical grid so that the pressure at the top of the
atmosphere is at `TOA_pressure`. If the TOA needs to be raised above the current
atmosphere is at `TOA_alt` or `TOA_pressure`. If the TOA needs to be raised above the current
TOA, then the function constantly extrapolates mixing ratios, temperature,
eddy diffusion, and particle radii.
Expand All @@ -531,6 +531,19 @@ cdef class Atmosphere:
"""
cdef char err[ERR_LEN+1]

a_pxd.atmosphere_update_vertical_grid_wrapper(&self._ptr, &TOA_pressure, err)
cdef double TOA_alt_ = 0.0
cdef bool TOA_alt_present = False
if TOA_alt != None:
TOA_alt_present = True
TOA_alt_ = TOA_alt

cdef double TOA_pressure_ = 0.0
cdef bool TOA_pressure_present = False
if TOA_pressure != None:
TOA_pressure_present = True
TOA_pressure_ = TOA_pressure

a_pxd.atmosphere_update_vertical_grid_wrapper(&self._ptr, &TOA_alt_, &TOA_alt_present,
&TOA_pressure_, &TOA_pressure_present, err)
if len(err.strip()) > 0:
raise PhotoException(err.decode("utf-8").strip())
3 changes: 2 additions & 1 deletion photochem/cython/Atmosphere_pxd.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ cdef extern void atmosphere_set_temperature_wrapper(void *ptr, int *nz, double *

cdef extern void atmosphere_set_rate_fcn_wrapper(void *ptr, char *species_c, time_dependent_rate_fcn fcn, char *err)

cdef extern void atmosphere_update_vertical_grid_wrapper(void *ptr, double *toa_pressure, char *err)
cdef extern void atmosphere_update_vertical_grid_wrapper(void *ptr, double *toa_alt, bool *toa_alt_present,
double *toa_pressure, bool *toa_pressure_present, char *err)
17 changes: 15 additions & 2 deletions photochem/fortran/Atmosphere_wrapper.f90
Original file line number Diff line number Diff line change
Expand Up @@ -503,17 +503,30 @@ subroutine atmosphere_set_rate_fcn_wrapper(ptr, species_c, fcn_c, err) bind(c)
endif
end subroutine

subroutine atmosphere_update_vertical_grid_wrapper(ptr, TOA_pressure, err) bind(c)
subroutine atmosphere_update_vertical_grid_wrapper(ptr, TOA_alt, TOA_alt_present, &
TOA_pressure, TOA_pressure_present, err) bind(c)
type(c_ptr), intent(in) :: ptr
real(c_double), intent(in) :: TOA_alt
logical(c_bool), intent(in) :: TOA_alt_present
real(c_double), intent(in) :: TOA_pressure
logical(c_bool), intent(in) :: TOA_pressure_present
character(kind=c_char), intent(out) :: err(err_len+1)

character(:), allocatable :: err_f
type(Atmosphere), pointer :: pc

call c_f_pointer(ptr, pc)

call pc%update_vertical_grid(TOA_pressure, err_f)
if (TOA_alt_present .and. .not.TOA_pressure_present) then
call pc%update_vertical_grid(TOA_alt=TOA_alt, err=err_f)
elseif (.not.TOA_alt_present .and. TOA_pressure_present) then
call pc%update_vertical_grid(TOA_pressure=TOA_pressure, err=err_f)
elseif (TOA_alt_present .and. TOA_pressure_present) then
call pc%update_vertical_grid(TOA_alt=TOA_alt, TOA_pressure=TOA_pressure, err=err_f)
elseif (.not.TOA_alt_present .and. .not.TOA_pressure_present) then
call pc%update_vertical_grid(err=err_f)
endif

err(1) = c_null_char
if (allocated(err_f)) then
call copy_string_ftoc(err_f, err)
Expand Down
7 changes: 4 additions & 3 deletions src/atmosphere/photochem_atmosphere.f90
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,13 @@ module subroutine set_rate_fcn(self, species, fcn, err)
end subroutine

!> Re-does the vertical grid so that the pressure at the top of the
!> atmosphere is at `TOA_pressure`. If the TOA needs to be raised above the current
!> atmosphere is a `TOA_alt` or `TOA_pressure`. If the TOA needs to be raised above the current
!> TOA, then the function constantly extrapolates mixing ratios, temperature,
!> eddy diffusion, and particle radii.
module subroutine update_vertical_grid(self, TOA_pressure, err)
module subroutine update_vertical_grid(self, TOA_alt, TOA_pressure, err)
class(Atmosphere), target, intent(inout) :: self
real(dp), intent(in) :: TOA_pressure !! New top of atmosphere pressure (dynes/cm^2)
real(dp), optional, intent(in) :: TOA_alt !! New top of atmosphere altitude (cm)
real(dp), optional, intent(in) :: TOA_pressure !! New top of atmosphere pressure (dynes/cm^2)
character(:), allocatable, intent(out) :: err
end subroutine

Expand Down
Loading

0 comments on commit 8f252ed

Please sign in to comment.