Skip to content

Commit

Permalink
particles
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholaswogan committed Jan 3, 2025
1 parent f44cbf9 commit d639246
Show file tree
Hide file tree
Showing 8 changed files with 364 additions and 19 deletions.
60 changes: 60 additions & 0 deletions clima/cython/AdiabatClimate.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,39 @@ cdef class AdiabatClimate:
raise ClimaException(err.decode("utf-8").strip())
return T_surf

def set_particle_density_and_radii(self, ndarray[double, ndim=1] P, ndarray[double, ndim=2] pdensities, ndarray[double, ndim=2] pradii):
"""Sets particle densities and radii.
Parameters
----------
P_i_surf : ndarray[double,ndim=1]
Array of pressures in dynes/cm^2
pdensities : ndarray[double,ndim=2]
Particle densities in particles/cm^3 at each pressure and for each
particle in the model. Shape (nz, np).
pradii : ndarray[double,ndim=2]
Particle radii in cm at each pressure and for each particle
in the model. Shape (nz, np).
"""
cdef int dim_P = P.shape[0]
cdef int dim1_pdensities = pdensities.shape[0]
cdef int dim2_pdensities = pdensities.shape[1]
pdensities = np.asfortranarray(pdensities)
cdef int dim1_pradii = pradii.shape[0]
cdef int dim2_pradii = pradii.shape[1]
pradii = np.asfortranarray(pradii)
cdef char err[ERR_LEN+1]

wa_pxd.adiabatclimate_set_particle_density_and_radii(
self._ptr, &dim_P, <double *> P.data,
&dim1_pdensities, &dim2_pdensities, <double *> pdensities.data,
&dim1_pradii, &dim2_pradii, <double *> pradii.data,
err
)

if len(err.strip()) > 0:
raise ClimaException(err.decode("utf-8").strip())

def RCE(self, ndarray[double, ndim=1] P_i_surf, double T_surf_guess, ndarray[double, ndim=1] T_guess,
convecting_with_below = None, custom_dry_mix = None):
"""Compute full radiative-convective equilibrium.
Expand Down Expand Up @@ -713,6 +746,15 @@ cdef class AdiabatClimate:
wa_pxd.adiabatclimate_species_names_get(self._ptr, &dim1, <char *>species_names_c.data)
return c2stringarr(species_names_c, S_STR_LEN, dim1)

property particle_names:
"List, shape (np). The name of each particle in the model"
def __get__(self):
cdef int dim1
wa_pxd.adiabatclimate_particle_names_get_size(self._ptr, &dim1)
cdef ndarray particle_names_c = np.empty(dim1*S_STR_LEN + 1, 'S1')
wa_pxd.adiabatclimate_particle_names_get(self._ptr, &dim1, <char *>particle_names_c.data)
return c2stringarr(particle_names_c, S_STR_LEN, dim1)

property rad:
"Radtran object that does radiative transfer"
def __get__(self):
Expand Down Expand Up @@ -909,6 +951,24 @@ cdef class AdiabatClimate:
cdef ndarray arr = np.empty((dim1, dim2), np.double, order="F")
wa_pxd.adiabatclimate_densities_get(self._ptr, &dim1, &dim2, <double *>arr.data)
return arr

property pdensities:
"ndarray[double,ndim=2], shape (nz,np). Particle densities in particles/cm^3."
def __get__(self):
cdef int dim1, dim2
wa_pxd.adiabatclimate_pdensities_get_size(self._ptr, &dim1, &dim2)
cdef ndarray arr = np.empty((dim1, dim2), np.double, order="F")
wa_pxd.adiabatclimate_pdensities_get(self._ptr, &dim1, &dim2, <double *>arr.data)
return arr

property pradii:
"ndarray[double,ndim=2], shape (nz,np). Particle radii in cm."
def __get__(self):
cdef int dim1, dim2
wa_pxd.adiabatclimate_pradii_get_size(self._ptr, &dim1, &dim2)
cdef ndarray arr = np.empty((dim1, dim2), np.double, order="F")
wa_pxd.adiabatclimate_pradii_get(self._ptr, &dim1, &dim2, <double *>arr.data)
return arr

property N_atmos:
"ndarray[double,ndim=1], shape (ng). Reservoir of gas in the atmosphere (mol/cm^2)"
Expand Down
16 changes: 16 additions & 0 deletions clima/cython/AdiabatClimate_pxd.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ cdef extern void adiabatclimate_surface_temperature_bg_gas_wrapper(AdiabatClimat
double *P_i_surf, double *P_surf, char *bg_gas,
double *T_guess, double *T_surf, char *err)

cdef extern void adiabatclimate_set_particle_density_and_radii(
AdiabatClimate *ptr, int *dim_P, double *P,
int *dim1_pdensities, int *dim2_pdensities, double *pdensities,
int *dim1_pradii, int *dim2_pradii, double *pradii,
char *err
)

cdef extern void adiabatclimate_rce_wrapper(
AdiabatClimate *ptr, int *ng, double *P_i_surf, double *T_surf_guess, int *dim_T_guess, double *T_guess,
bool *convecting_with_below_present, int *dim_convecting_with_below, bool *convecting_with_below,
Expand Down Expand Up @@ -127,6 +134,9 @@ cdef extern void adiabatclimate_surface_heat_flow_set(AdiabatClimate *ptr, doubl
cdef extern void adiabatclimate_species_names_get_size(AdiabatClimate *ptr, int *dim1)
cdef extern void adiabatclimate_species_names_get(AdiabatClimate *ptr, int *dim1, char* species_names)

cdef extern void adiabatclimate_particle_names_get_size(AdiabatClimate *ptr, int *dim1)
cdef extern void adiabatclimate_particle_names_get(AdiabatClimate *ptr, int *dim1, char* particle_names)

cdef extern void adiabatclimate_rad_get(AdiabatClimate *ptr, rad_pxd.Radtran **ptr1)

cdef extern void adiabatclimate_convecting_with_below_get_size(AdiabatClimate *ptr, int *dim1)
Expand Down Expand Up @@ -189,6 +199,12 @@ cdef extern void adiabatclimate_dz_get(AdiabatClimate *ptr, int *dim1, double *a
cdef extern void adiabatclimate_densities_get_size(AdiabatClimate *ptr, int *dim1, int *dim2)
cdef extern void adiabatclimate_densities_get(AdiabatClimate *ptr, int *dim1, int *dim2, double *arr)

cdef extern void adiabatclimate_pdensities_get_size(AdiabatClimate *ptr, int *dim1, int *dim2)
cdef extern void adiabatclimate_pdensities_get(AdiabatClimate *ptr, int *dim1, int *dim2, double *arr)

cdef extern void adiabatclimate_pradii_get_size(AdiabatClimate *ptr, int *dim1, int *dim2)
cdef extern void adiabatclimate_pradii_get(AdiabatClimate *ptr, int *dim1, int *dim2, double *arr)

cdef extern void adiabatclimate_n_atmos_get_size(AdiabatClimate *ptr, int *dim1)
cdef extern void adiabatclimate_n_atmos_get(AdiabatClimate *ptr, int *dim1, double *arr)

Expand Down
114 changes: 104 additions & 10 deletions clima/fortran/AdiabatClimate.f90
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,31 @@ subroutine adiabatclimate_surface_temperature_bg_gas_wrapper(ptr, ng, P_i_surf,

end subroutine

subroutine adiabatclimate_set_particle_density_and_radii(ptr, dim_P, P, dim1_pdensities, dim2_pdensities, pdensities, &
dim1_pradii, dim2_pradii, pradii, err) bind(c)
use clima, only: AdiabatClimate
type(c_ptr), value, intent(in) :: ptr
integer(c_int), intent(in) :: dim_P
real(c_double), intent(in) :: P(dim_P)
integer(c_int), intent(in) :: dim1_pdensities, dim2_pdensities
real(c_double), intent(in) :: pdensities(dim1_pdensities, dim2_pdensities)
integer(c_int), intent(in) :: dim1_pradii, dim2_pradii
real(c_double), intent(in) :: pradii(dim1_pradii, dim2_pradii)
character(c_char), intent(out) :: err(err_len+1)

character(:), allocatable :: err_f
type(AdiabatClimate), pointer :: c

call c_f_pointer(ptr, c)
call c%set_particle_density_and_radii(P, pdensities, pradii, err_f)

err(1) = c_null_char
if (allocated(err_f)) then
call copy_string_ftoc(err_f, err)
endif

end subroutine

subroutine adiabatclimate_rce_wrapper(ptr, ng, P_i_surf, T_surf_guess, dim_T_guess, T_guess, &
convecting_with_below_present, dim_convecting_with_below, convecting_with_below, &
custom_present, dim_sp_custom, sp_custom, dim_P_custom, P_custom, &
Expand Down Expand Up @@ -804,6 +829,35 @@ subroutine adiabatclimate_species_names_get(ptr, dim1, species_names) bind(c)

end subroutine

subroutine adiabatclimate_particle_names_get_size(ptr, dim1) bind(c)
use clima, only: AdiabatClimate
type(c_ptr), value, intent(in) :: ptr
integer(c_int), intent(out) :: dim1
type(AdiabatClimate), pointer :: c
call c_f_pointer(ptr, c)
dim1 = size(c%particle_names)
end subroutine

subroutine adiabatclimate_particle_names_get(ptr, dim1, particle_names) bind(c)
use clima, only: AdiabatClimate, s_str_len
type(c_ptr), value, intent(in) :: ptr
integer(c_int), intent(in) :: dim1
character(kind=c_char), intent(out) :: particle_names(dim1*s_str_len+1)
type(AdiabatClimate), pointer :: c

integer :: i, j, k

call c_f_pointer(ptr, c)
do i = 1,dim1
do j = 1,s_str_len
k = j + (i - 1) * s_str_len
particle_names(k) = c%particle_names(i)(j:j)
enddo
enddo
particle_names(dim1*s_str_len+1) = c_null_char

end subroutine

subroutine adiabatclimate_rad_get(ptr, ptr1) bind(c)
use clima, only: AdiabatClimate
type(c_ptr), value, intent(in) :: ptr
Expand Down Expand Up @@ -1165,6 +1219,56 @@ subroutine adiabatclimate_densities_get_size(ptr, dim1, dim2) bind(c)
dim2 = size(c%densities,2)
end subroutine

subroutine adiabatclimate_densities_get(ptr, dim1, dim2, arr) bind(c)
use clima, only: AdiabatClimate
type(c_ptr), value, intent(in) :: ptr
integer(c_int), intent(in) :: dim1, dim2
real(c_double), intent(out) :: arr(dim1,dim2)
type(AdiabatClimate), pointer :: c
call c_f_pointer(ptr, c)
arr = c%densities
end subroutine

subroutine adiabatclimate_pdensities_get_size(ptr, dim1, dim2) bind(c)
use clima, only: AdiabatClimate
type(c_ptr), value, intent(in) :: ptr
integer(c_int), intent(out) :: dim1, dim2
type(AdiabatClimate), pointer :: c
call c_f_pointer(ptr, c)
dim1 = size(c%pdensities,1)
dim2 = size(c%pdensities,2)
end subroutine

subroutine adiabatclimate_pdensities_get(ptr, dim1, dim2, arr) bind(c)
use clima, only: AdiabatClimate
type(c_ptr), value, intent(in) :: ptr
integer(c_int), intent(in) :: dim1, dim2
real(c_double), intent(out) :: arr(dim1,dim2)
type(AdiabatClimate), pointer :: c
call c_f_pointer(ptr, c)
arr = c%pdensities
end subroutine

subroutine adiabatclimate_pradii_get_size(ptr, dim1, dim2) bind(c)
use clima, only: AdiabatClimate
type(c_ptr), value, intent(in) :: ptr
integer(c_int), intent(out) :: dim1, dim2
type(AdiabatClimate), pointer :: c
call c_f_pointer(ptr, c)
dim1 = size(c%pradii,1)
dim2 = size(c%pradii,2)
end subroutine

subroutine adiabatclimate_pradii_get(ptr, dim1, dim2, arr) bind(c)
use clima, only: AdiabatClimate
type(c_ptr), value, intent(in) :: ptr
integer(c_int), intent(in) :: dim1, dim2
real(c_double), intent(out) :: arr(dim1,dim2)
type(AdiabatClimate), pointer :: c
call c_f_pointer(ptr, c)
arr = c%pradii
end subroutine

subroutine adiabatclimate_n_atmos_get_size(ptr, dim1) bind(c)
use clima, only: AdiabatClimate
type(c_ptr), value, intent(in) :: ptr
Expand Down Expand Up @@ -1221,14 +1325,4 @@ subroutine adiabatclimate_n_ocean_get(ptr, dim1, dim2, arr) bind(c)
type(AdiabatClimate), pointer :: c
call c_f_pointer(ptr, c)
arr = c%N_ocean
end subroutine

subroutine adiabatclimate_densities_get(ptr, dim1, dim2, arr) bind(c)
use clima, only: AdiabatClimate
type(c_ptr), value, intent(in) :: ptr
integer(c_int), intent(in) :: dim1, dim2
real(c_double), intent(out) :: arr(dim1,dim2)
type(AdiabatClimate), pointer :: c
call c_f_pointer(ptr, c)
arr = c%densities
end subroutine
Loading

0 comments on commit d639246

Please sign in to comment.