Skip to content

Commit

Permalink
python wrapper. clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholaswogan committed Feb 21, 2024
1 parent c0396a6 commit ab8e44f
Show file tree
Hide file tree
Showing 7 changed files with 475 additions and 49 deletions.
147 changes: 137 additions & 10 deletions clima/cython/AdiabatClimate.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cdef class AdiabatClimate:
cdef void *_ptr

def __init__(self, species_file = None, settings_file = None,
flux_file = None, data_dir = None):
flux_file = None, data_dir = None, cbool double_radiative_grid = True):
"""Initializes `AdiabatClimate`
Parameters
Expand Down Expand Up @@ -47,8 +47,8 @@ cdef class AdiabatClimate:

# Initialize
wa_pxd.adiabatclimate_create_wrapper(&self._ptr, species_file_c,
settings_file_c, flux_file_c, data_dir_c,
err)
settings_file_c, flux_file_c, data_dir_c, &double_radiative_grid,
err)
if len(err.strip()) > 0:
raise ClimaException(err.decode("utf-8").strip())

Expand Down Expand Up @@ -316,14 +316,55 @@ cdef class AdiabatClimate:
raise ClimaException(err.decode("utf-8").strip())
return T_surf

def RCE(self, ndarray[double, ndim=1] P_i_surf, double T_surf_guess, ndarray[double, ndim=1] T_guess,
convecting_with_below = None):
"""Compute full radiative-convective equilibrium.
Parameters
----------
P_i_surf : ndarray[double,ndim=1]
Array of surface pressures of each species (dynes/cm^2)
T_surf_guess : float
A guess for the surface temperature (K)
T_guess : ndarray[double,ndim=1]
A guess for the temperature in each atmospheric layer (K)
convecting_with_below : ndarray[bool,ndim=1], optional
An array describing a guess for the radiative vs. convective
regions of the atmosphere
Returns
-------
bool
Whether the routine converged or not.
"""
cdef int ng = P_i_surf.shape[0]
cdef int dim_T_guess = T_guess.shape[0]
cdef ndarray[cbool, ndim=1] convecting_with_below_ = np.array([False],dtype=np.bool_)
cdef cbool convecting_with_below_present
if convecting_with_below is None:
convecting_with_below_present = False
else:
convecting_with_below_present = True
convecting_with_below_ = convecting_with_below
cdef int dim_convecting_with_below = convecting_with_below_.shape[0]
cdef cbool converged
cdef char err[ERR_LEN+1]

wa_pxd.adiabatclimate_rce_wrapper(&self._ptr, &ng, <double *>P_i_surf.data, &T_surf_guess,
&dim_T_guess, <double *>T_guess.data, &convecting_with_below_present,
&dim_convecting_with_below, <cbool *>convecting_with_below_.data, &converged, err)
if len(err.strip()) > 0:
raise ClimaException(err.decode("utf-8").strip())
return converged

def to_regular_grid(self):
"Re-grids atmosphere so that each grid cell is equally spaced in altitude."
cdef char err[ERR_LEN+1]
wa_pxd.adiabatclimate_to_regular_grid_wrapper(&self._ptr, err)
if len(err.strip()) > 0:
raise ClimaException(err.decode("utf-8").strip())

def out2atmosphere_txt(self, filename, ndarray[double, ndim=1] eddy, bool overwrite = False, bool clip = True):
def out2atmosphere_txt(self, filename, ndarray[double, ndim=1] eddy, cbool overwrite = False, cbool clip = True):
"""Saves state of the atmosphere to a file.
Parameters
Expand Down Expand Up @@ -390,10 +431,10 @@ cdef class AdiabatClimate:
use the initial guess in `self.make_column_P_guess`
"""
def __get__(self):
cdef bool val
cdef cbool val
wa_pxd.adiabatclimate_use_make_column_p_guess_get(&self._ptr, &val)
return val
def __set__(self, bool val):
def __set__(self, cbool val):
wa_pxd.adiabatclimate_use_make_column_p_guess_set(&self._ptr, &val)

property make_column_P_guess:
Expand All @@ -418,10 +459,10 @@ cdef class AdiabatClimate:
it matches the skin temperature. The initial guess will always be self.T_trop.
"""
def __get__(self):
cdef bool val
cdef cbool val
wa_pxd.adiabatclimate_solve_for_t_trop_get(&self._ptr, &val)
return val
def __set__(self, bool val):
def __set__(self, cbool val):
wa_pxd.adiabatclimate_solve_for_t_trop_set(&self._ptr, &val)

property albedo_fcn:
Expand Down Expand Up @@ -478,10 +519,10 @@ cdef class AdiabatClimate:
observed dayside temperature of a tidally locked planet.
"""
def __get__(self):
cdef bool val
cdef cbool val
wa_pxd.adiabatclimate_tidally_locked_dayside_get(&self._ptr, &val)
return val
def __set__(self, bool val):
def __set__(self, cbool val):
wa_pxd.adiabatclimate_tidally_locked_dayside_set(&self._ptr, &val)

property L:
Expand Down Expand Up @@ -546,7 +587,46 @@ cdef class AdiabatClimate:
var = Radtran()
var._ptr = ptr1
return var

property convecting_with_below:
"""ndarray[bool,ndim=1], shape (nz). If True, then the layer below
is convecting with the current layer. Index 1 determines if the
first atomspheric layer is convecting with the ground.
"""
def __get__(self):
cdef int dim1
wa_pxd.adiabatclimate_convecting_with_below_get_size(&self._ptr, &dim1)
cdef ndarray[cbool, ndim=1] arr = np.empty(dim1, bool)
wa_pxd.adiabatclimate_convecting_with_below_get(&self._ptr, &dim1, <cbool *>arr.data)
return arr

property lapse_rate:
"ndarray[double,ndim=1], shape (nz). The true lapse rate (dlnT/dlnP)."
def __get__(self):
cdef int dim1
wa_pxd.adiabatclimate_lapse_rate_get_size(&self._ptr, &dim1)
cdef ndarray arr = np.empty(dim1, np.double)
wa_pxd.adiabatclimate_lapse_rate_get(&self._ptr, &dim1, <double *>arr.data)
return arr

property lapse_rate_intended:
"ndarray[double,ndim=1], shape (nz). The computed lapse rate (dlnT/dlnP)."
def __get__(self):
cdef int dim1
wa_pxd.adiabatclimate_lapse_rate_intended_get_size(&self._ptr, &dim1)
cdef ndarray arr = np.empty(dim1, np.double)
wa_pxd.adiabatclimate_lapse_rate_intended_get(&self._ptr, &dim1, <double *>arr.data)
return arr

property convective_newton_step_size:
"float. The size of the newton step."
def __get__(self):
cdef double val
wa_pxd.adiabatclimate_convective_newton_step_size_get(&self._ptr, &val)
return val
def __set__(self, double val):
wa_pxd.adiabatclimate_convective_newton_step_size_set(&self._ptr, &val)

property rtol:
"float. Relative tolerance of integration."
def __get__(self):
Expand All @@ -573,6 +653,53 @@ cdef class AdiabatClimate:
return val
def __set__(self, double val):
wa_pxd.adiabatclimate_tol_make_column_set(&self._ptr, &val)

property epsj:
"float. Perturbation for the jacobian"
def __get__(self):
cdef double val
wa_pxd.adiabatclimate_epsj_get(&self._ptr, &val)
return val
def __set__(self, double val):
wa_pxd.adiabatclimate_epsj_set(&self._ptr, &val)

property xtol_rc:
"float. xtol for RC equilibrium"
def __get__(self):
cdef double val
wa_pxd.adiabatclimate_xtol_rc_get(&self._ptr, &val)
return val
def __set__(self, double val):
wa_pxd.adiabatclimate_xtol_rc_set(&self._ptr, &val)

property max_rc_iters:
"int. Max number of iterations in the RCE routine"
def __get__(self):
cdef int val
wa_pxd.adiabatclimate_max_rc_iters_get(&self._ptr, &val)
return val
def __set__(self, int val):
wa_pxd.adiabatclimate_max_rc_iters_set(&self._ptr, &val)

property max_rc_iters_convection:
"""int. Max number of iterations for which convective layers can
be converged to radiative layers in the RCE routine
"""
def __get__(self):
cdef int val
wa_pxd.adiabatclimate_max_rc_iters_convection_get(&self._ptr, &val)
return val
def __set__(self, int val):
wa_pxd.adiabatclimate_max_rc_iters_convection_set(&self._ptr, &val)

property verbose:
"bool. verbosity"
def __get__(self):
cdef cbool val
wa_pxd.adiabatclimate_verbose_get(&self._ptr, &val)
return val
def __set__(self, cbool val):
wa_pxd.adiabatclimate_verbose_set(&self._ptr, &val)

property P_surf:
"float. Surface pressure (dynes/cm^2)"
Expand Down
33 changes: 32 additions & 1 deletion clima/cython/AdiabatClimate_pxd.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cdef extern void deallocate_adiabatclimate(void *ptr);

# wrappers for functions
cdef extern void adiabatclimate_create_wrapper(void *ptr, char *species_file,
char *settings_file, char *flux_file, char *data_dir, char *err);
char *settings_file, char *flux_file, char *data_dir, bool *double_radiative_grid, char *err);

cdef extern void adiabatclimate_make_profile_wrapper(void *ptr, double *T_surf, int *ng,
double *P_i_surf, char *err)
Expand Down Expand Up @@ -43,6 +43,10 @@ cdef extern void adiabatclimate_surface_temperature_bg_gas_wrapper(void *ptr, in
double *P_i_surf, double *P_surf, char *bg_gas,
double *T_guess, double *T_surf, char *err)

cdef extern void adiabatclimate_rce_wrapper(void *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, bool *converged, char *err)

cdef extern void adiabatclimate_set_ocean_solubility_fcn_wrapper(void *ptr, char *species_c, ocean_solubility_fcn fcn, char *err)

cdef extern void adiabatclimate_to_regular_grid_wrapper(void *ptr, char *err)
Expand Down Expand Up @@ -99,6 +103,18 @@ cdef extern void adiabatclimate_species_names_get(void *ptr, int *dim1, char* sp

cdef extern void adiabatclimate_rad_get(void *ptr, void *ptr1)

cdef extern void adiabatclimate_convecting_with_below_get_size(void *ptr, int *dim1)
cdef extern void adiabatclimate_convecting_with_below_get(void *ptr, int *dim1, bool *arr)

cdef extern void adiabatclimate_lapse_rate_get_size(void *ptr, int *dim1)
cdef extern void adiabatclimate_lapse_rate_get(void *ptr, int *dim1, double *arr)

cdef extern void adiabatclimate_lapse_rate_intended_get_size(void *ptr, int *dim1)
cdef extern void adiabatclimate_lapse_rate_intended_get(void *ptr, int *dim1, double *arr)

cdef extern void adiabatclimate_convective_newton_step_size_get(void *ptr, double *val)
cdef extern void adiabatclimate_convective_newton_step_size_set(void *ptr, double *val)

cdef extern void adiabatclimate_rtol_get(void *ptr, double *val)
cdef extern void adiabatclimate_rtol_set(void *ptr, double *val)

Expand All @@ -108,6 +124,21 @@ cdef extern void adiabatclimate_atol_set(void *ptr, double *val)
cdef extern void adiabatclimate_tol_make_column_get(void *ptr, double *val)
cdef extern void adiabatclimate_tol_make_column_set(void *ptr, double *val)

cdef extern void adiabatclimate_epsj_get(void *ptr, double *val)
cdef extern void adiabatclimate_epsj_set(void *ptr, double *val)

cdef extern void adiabatclimate_xtol_rc_get(void *ptr, double *val)
cdef extern void adiabatclimate_xtol_rc_set(void *ptr, double *val)

cdef extern void adiabatclimate_max_rc_iters_get(void *ptr, int *val)
cdef extern void adiabatclimate_max_rc_iters_set(void *ptr, int *val)

cdef extern void adiabatclimate_max_rc_iters_convection_get(void *ptr, int *val)
cdef extern void adiabatclimate_max_rc_iters_convection_set(void *ptr, int *val)

cdef extern void adiabatclimate_verbose_get(void *ptr, bool *val)
cdef extern void adiabatclimate_verbose_set(void *ptr, bool *val)

cdef extern void adiabatclimate_p_surf_get(void *ptr, double *val)

cdef extern void adiabatclimate_p_trop_get(void *ptr, double *val)
Expand Down
4 changes: 2 additions & 2 deletions clima/cython/_clima.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from numpy cimport ndarray
from numpy cimport ndarray, uint8_t
from libc.stdint cimport uintptr_t
from libcpp cimport bool
from libcpp cimport bool as cbool
import numpy as np
import ctypes as ct
import os
Expand Down
Loading

0 comments on commit ab8e44f

Please sign in to comment.