charm_crd#
Module to work with the coordinates of evaluation points/cells:
defines
charm_pointandcharm_cellstructures,allocates, initializes and frees
charm_pointandcharm_cell,computes
charm_pointfor a few pre-defined grid types.
References:
Sneeuw, N. (1994) Global spherical harmonic analysis by least-squares and numerical quadrature methods in historical perspective. Geophysical Journal International 118:707-716
Driscoll, J. R., Healy, D. M. (1994) Computing Fourier transforms and convolutions on the 2-sphere. Advances in Applied Mathematics 15:202-250
Wieczorek, M. A., Meschede, M. (2018) SHTools: Tools for Working with Spherical Harmonics. Geochemistry, Geophysics, Geosystems 19:2574-2592
Note
This documentation is written for double precision version of CHarm.
Allocate, initialize and free the charm_point structure
These functions allocate, initialize and free the charm_point structure, which is designed to store evaluation points.
-
charm_point *charm_crd_point_malloc(int type, size_t nlat, size_t nlon)#
Allocates evaluation points of a given
typewithnlatlatitudes,nlonlongitudes andnlatspherical radii. IftypeisCHARM_CRD_POINT_GRID_GL,CHARM_CRD_POINT_GRID_DH1orCHARM_CRD_POINT_GRID_DH2,nlatintegration weights are allocated in addition. The memory of all array elements is uninitialized, so their values are undefined.Valid values of
typeare constantsCHARM_CRD_POINT_SCATTERED,CHARM_CRD_POINT_GRID,CHARM_CRD_POINT_GRID_GL,CHARM_CRD_POINT_GRID_DH1andCHARM_CRD_POINT_GRID_DH2.For
charm_point *pntreturned by this function withtypebeingCHARM_CRD_POINT_GRID,CHARM_CRD_POINT_GRID_GL,CHARM_CRD_POINT_GRID_DH1orCHARM_CRD_POINT_GRID_DH2, the coordinates of the grid point on thei-th latitude parallel and thej-th meridian have to be accessed aspnt->lat[i]for the grid latitude,pnt->lon[j]for the grid longitude, andpnt->r[i]for the spherical radii
with
i = 0, 1, ..., nlat - 1andj = 0, 1, ..., nlon - 1.For
typeset toCHARM_CRD_POINT_SCATTERED, the coordinates of thei-th point have to be accessed aspnt->lat[i]for the grid latitude,pnt->lon[i]for the grid longitude, andpnt->r[i]for the spherical radii
with
i = 0, 1, ..., nlat - 1,nlat == nlon.
Warning
The structure returned must be deallocated by calling
charm_crd_point_free(). The usual deallocation withfreewill lead to memory leaks.- Returns:
On success, returned is a pointer to the
charm_pointstructure. On error,NULLis returned. Errors include memory allocation failures and incorrect input arguments (e.g., unsupported value oftype).
-
charm_point *charm_crd_point_calloc(int type, size_t nlat, size_t nlon)#
The same as
charm_crd_point_malloc()but all array elements are initialized to zero.
-
charm_point *charm_crd_point_init(int type, size_t nlat, size_t nlon, double *lat, double *lon, double *r)#
For a given point
type, takesnlatspherical latitudes from the array pointed to bylat,nlonspherical longitudes fromlonandnlatspherical radii fromr(shallow copy).Valid values of
typeare constantsCHARM_CRD_POINT_SCATTERED,CHARM_CRD_POINT_GRID,CHARM_CRD_POINT_GRID_GL,CHARM_CRD_POINT_GRID_DH1andCHARM_CRD_POINT_GRID_DH2.If
typeisCHARM_CRD_POINT_SCATTERED,nlatmust be equal tonlonandeach of
lat,lonandrmust have access tonlatelements.
If
typeisCHARM_CRD_POINT_GRID,CHARM_CRD_POINT_GRID_GL,CHARM_CRD_POINT_GRID_DH1orCHARM_CRD_POINT_GRID_DH2,latmust have access tonlatelements,lonmust have access tonlonelements, andrmust have access tonlatelements.
Note
See
charm_crd_point_malloc()to find out how to access the coordinates stored in the returnedcharm_pointstructure.Note
See
charm_shc_init()for the rationale behind the shallow copies of input arrayslat,lonandr. The memory of these arrays must be treated in the same fashion as shown therein with thecandsarrays.Warning
The structure returned must be deallocated using
charm_crd_point_free(). The usual deallocation withfreewill lead to memory leaks.- Returns:
On success, returned is a pointer to the
charm_pointstructure. On error,NULLis returned. Errors include memory allocation failures and incorrect input arguments (e.g., unsupported value oftype).
-
void charm_crd_point_free(charm_point *pnt)#
Frees the memory associated with
pnt. No operation is performed ifpntisNULL.If
pnt->owneris1, the function releases all the memory that is associated withpnt, including that of its array members. Ifpnt->owneris0, the arrays are not released from the memory, because they were not allocated by CHarm.
Quadrature point grids
These functions compute the Gauss-Legendre and Driscoll-Healy quadrature grids and the associated integration weights.
-
charm_point *charm_crd_point_gl(unsigned long nmax, double r)#
Computes the Gauss-Legendre grid associated with the harmonic degree
nmax(Sneeuw, 1994) on the sphere with the radiusr. The integration weights are computed on the unit sphere.Loops are parallelized using OpenMP.
Note
The Gauss-Legendre grid is non-equiangular in latitude. The longitudinal step is constant.
Warning
The structure returned must be deallocated by calling
charm_crd_point_free(). The usual deallocation withfreewill lead to memory leaks.- Returns:
On success, returned is a pointer to the
charm_pointstructure. On error,NULLis returned. In addition to the memory allocation failure, the error may be due to the exceeded maximum number of iterations to compute the latitudes or due to the overflow problem. The latter may happen in single precision ifnmaxis larger than7200or so. The overflow check is not performed if the output fromcharm_misc_buildopt_isfinite()is zero.
-
void charm_crd_point_gl_shape(unsigned long nmax, size_t *nlat, size_t *nlon)#
Computes the number of latitudes
nlatand the number of longitudesnlonof the Gauss-Legendre grid that corresponds to the maximum harmonic degreenmax.
-
charm_point *charm_crd_point_dh1(unsigned long nmax, double r)#
Computes the non-equiangular Driscoll-Healy grid (as defined by Driscoll and Healy, 1994) associated with the maximum spherical harmonic degree
nmaxon the sphere with the radiusr. The integration weights are computed on the unit sphere.Loops are parallelized using OpenMP.
Warning
The structure returned must be deallocated by calling
charm_crd_point_free(). The usual deallocation withfreewill lead to memory leaks.- Returns:
On success, returned is a pointer to the
charm_pointstructure. On error,NULLis returned.
-
void charm_crd_point_dh1_shape(unsigned long nmax, size_t *nlat, size_t *nlon)#
Computes the number of latitudes
nlatand the number of longitudesnlonof the non-equiangular Driscoll-Healy grid that corresponds to the maximum harmonic degreenmax.
-
charm_point *charm_crd_point_dh2(unsigned long nmax, double r)#
The same as
charm_crd_point_dh1()but for the equiangular modification of the Driscoll-Healy grid after Wieczorek and Meschede (2018).
-
void charm_crd_point_dh2_shape(unsigned long nmax, size_t *nlat, size_t *nlon)#
Computes the number of latitudes
nlatand the number of longitudesnlonof the equiangular Driscoll-Healy grid that corresponds to the maximum harmonic degreenmax.
Allocate, initialize and free the charm_cell structure
These functions allocate, initialize and free the charm_cell structure, which is designed to store evaluation cells.
-
charm_cell *charm_crd_cell_malloc(int type, size_t nlat, size_t nlon)#
Allocates evaluation cells of a given
typewithnlatminimum and maximum latitudes,nlonminimum and maximum longitudes andnlatspherical radii. The memory of all array elements is uninitialized, so their values are undefined.Valid values of
typeare constantsCHARM_CRD_CELL_GRIDandCHARM_CRD_CELL_SCATTERED.For
charm_cell *cellreturned by this function withtypebeingCHARM_CRD_CELL_GRID, the coordinates of the grid cell on thei-th latitude parallel and thej-th meridian have to be accessed ascell->latmin[i]for the minimum cell latitude,cell->latmax[i]for the maximum cell latitude,cell->lonmin[j]for the minimum cell longitude,cell->lonmax[j]for the maximum cell longitude,cell->r[i]for the spherical radii
with
i = 0, 1, ..., nlat - 1andj = 0, 1, ..., nlon - 1.For
typeset toCHARM_CRD_CELL_SCATTERED, the coordinates of thei-th cell have to be accessed ascell->latmin[i]for the minimum cell latitude,cell->latmax[i]for the maximum cell latitude,cell->lonmin[i]for the minimum cell longitude,cell->lonmax[i]for the maximum cell longitude,cell->r[i]for the spherical radii
with
i = 0, 1, ..., nlat - 1,nlat == nlon.
Warning
The structure returned must be deallocated by calling
charm_crd_cell_free(). The usual deallocation withfreewill lead to memory leaks.- Returns:
On success, returned is a pointer to the
charm_cellstructure. On error,NULLis returned. Errors include memory allocation failures and incorrect input arguments (e.g., unsupported value oftype).
-
charm_cell *charm_crd_cell_calloc(int type, size_t nlat, size_t nlon)#
The same as
charm_crd_cell_malloc()but the memory of all array elements is initialized to zero.
-
charm_cell *charm_crd_cell_init(int type, size_t nlat, size_t nlon, double *latmin, double *latmax, double *lonmin, double *lonmax, double *r)#
For a given
type, takesnlatminimum and maximum spherical latitudes from the arrays pointed to bylatminandlatmax,nlonminimum and maximum spherical longitudes fromlonminandlonmaxandnlatspherical radii fromr, respectively (shallow copy).Accepted values of
typeareCHARM_CRD_CELL_GRIDandCHARM_CRD_CELL_SCATTERED.If
typeisCHARM_CRD_CELL_SCATTERED,nlatmust be equal tonlonandeach of
latmin,latmax,lonmin,lonmaxandrmust have access tonlatelements.
If
typeisCHARM_CRD_CELL_GRID,latminandlatmaxmust each have access tonlatelements,lonminandlonmaxmust each have access tonlonelements,rmust have access tonlatelements.
Note
See
charm_crd_cell_malloc()to find out how to access the coordinates stored in the returnedcharm_cellstructure.Note
See
charm_shc_init()for the rationale behind the shallow copies of input arrayslatmin,latmax,lonmin,lonmaxandr. The memory of these arrays must be treated in the same fashion as shown therein with thecandsarrays.Warning
The structure created by this function must be deallocated using
charm_crd_cell_free(). The usual deallocation withfreewill lead to memory leaks.- Returns:
On success, returned is a pointer to the
charm_cellstructure. On error,NULLis returned. Errors include memory allocation failures and incorrect input arguments (e.g., unsupported value oftype).
-
void charm_crd_cell_free(charm_cell *cell)#
Frees the memory associated with
cell. No operation is performed ifcellisNULL.If
cell->owneris1, the function releases all the memory that is associated withcell, including that of its array members. Ifcell->owneris0, the arrays are not released from the memory, because they were not allocated by CHarm.
Enums
Defines supported values of
charm_point.typetoCHARM_CRD_POINT_SCATTERED,CHARM_CRD_POINT_GRID,CHARM_CRD_POINT_GRID_GL,CHARM_CRD_POINT_GRID_DH1,CHARM_CRD_POINT_GRID_DH2. Forcharm_cell.type, valid constants areCHARM_CRD_CELL_SCATTEREDandCHARM_CRD_CELL_GRID.Values:
-
enumerator CHARM_CRD_CELL_GRID = -2#
Custom grid of cells.
-
enumerator CHARM_CRD_CELL_SCATTERED#
Scattered cells.
-
enumerator CHARM_CRD_POINT_SCATTERED = 1#
Scattered points.
-
enumerator CHARM_CRD_POINT_GRID#
Custom point grid.
-
enumerator CHARM_CRD_POINT_GRID_GL#
Gauss-Legendre point grid.
-
enumerator CHARM_CRD_POINT_GRID_DH1#
Driscoll-Healy point grid as defined by Driscoll and Healy (1994) (non-equiangular).
-
enumerator CHARM_CRD_POINT_GRID_DH2#
The equiangular modification of the Driscoll-Healy point grid after Wieczorek and Meschede (2018).
-
enumerator CHARM_CRD_CELL_GRID = -2#
-
struct charm_point#
Structure to store evaluation points.
If CHarm is compiled with the MPI support, the data of the structure can be distributed across MPI processes. This is useful if your dataset is so large that it would not fit into the RAM of a single computing node. The members of the structure are therefore divided into two groups.
Members common to both non-distributed and distributed structures. These are always accessible.
Members specific to distributed structures only. These are accessible only when CHarm was compiled with the MPI support.
Members common to non-distributed and distributed structures
-
int type#
Organization of points in the structure.
The following options are available:
CHARM_CRD_POINT_SCATTEREDfor user-defined scattered points,CHARM_CRD_POINT_GRIDfor a user-defined grid,CHARM_CRD_POINT_GRID_GLfor the Gauss-Legendre grid (Sneeuw, 1994) withnmax + 1latitudes and2 * nmax + 2longitudes (nmaxis maximum harmonic degree),CHARM_CRD_POINT_GRID_DH1for the non-equiangular Driscoll-Healy grid as defined by Driscoll and Healy (1994) with2 * nmax + 2latitudes and2 * nmax + 2longitudes, andCHARM_CRD_POINT_GRID_DH2for an equiangular modification of the Driscoll-Healy grid (Wieczorek and Meschede, 2018) with2 * nmax + 2latitudes and4 * nmax + 4longitudes.
-
size_t nlat#
Total number of points in the latitudinal direction. If
0, no points are stored in the structure.If the structure is distributed, this member represents the sum of
charm_point.local_nlatacross all MPI processes withincharm_point.comm.
-
size_t nlon#
Total number of points in the longitudinal direction. If
0, no points are stored in the structure.If the structure is distributed and
charm_point.type == CHARM_CRD_POINT_SCATTERED, then this member represents the sum ofcharm_point.local_nlonacross all MPI processes withincharm_point.comm. This means that each MPI process may hold different number of longitudes, hence scattered points.If the structure is distributed and
charm_point.typeis eitherCHARM_CRD_POINT_GRID,CHARM_CRD_POINT_GRID_GL,CHARM_CRD_POINT_GRID_DH1orCHARM_CRD_POINT_GRID_DH2, then this member has always the same value ascharm_point.local_nlon. This means that all point grids must always be distributed across MPI processes in latitudinal chunks, that is, each sub-grid must consist of the same number of longitudes, so thatcharm_point.local_nlonandcharm_point.nlonare equal.
-
size_t npoint#
Total number of points.
For scattered points (
charm_point.type == CHARM_CRD_POINT_SCATTERED), equals tocharm_point.nlatand tocharm_point.nlon.For grids (
charm_point.typeis any ofCHARM_CRD_POINT_GRID*), equals tocharm_point.nlat * charm_point.nlon.
If the structure is distributed, this member represents the sum of
charm_point.local_npointacross all MPI processes withincharm_point.comm.
-
double *lat#
Pointer to an array of latitudes in radians.
For non-distributed structures,
charm_point.nlatarray elements are associated with the pointer. For distributed structures,charm_point.local_nlatelements are associated.
-
double *lon#
Pointer to an array of longitudes in radians.
For non-distributed structures,
charm_point.nlonarray elements are associated with the pointer. For distributed structures,charm_point.local_nlonelements are associated.
-
double *r#
Pointer to an array of spherical radii in metres.
The same number of array elements is associated with this pointer as in the case of
charm_point.lat.
-
double *w#
Pointer to an array of integration weights on the unit sphere. The pointer is used (i.e. is not
NULL) only forcharm_pointstructures returned by thecharm_crd_point_gl(),charm_crd_point_dh1()andcharm_crd_point_dh2()functions, that is, ifcharm_point.typeisCHARM_CRD_POINT_GRID_GL,CHARM_CRD_POINT_GRID_DH1, orCHARM_CRD_POINT_GRID_DH2. Otherwise, the pointer isNULLand is never used.The same number of array elements is associated with this pointer as in the case of
charm_point.lat.
-
_Bool owner#
If
1, the memory associated withcharm_point.lat,charm_point.lon,charm_point.randcharm_point.wwas allocated by CHarm, socharm_crd_point_free()deallocates it.If
0, the memory associated with these arrays was allocated outside CHarm, socharm_crd_point_free()does not deallocate it (the user allocated this memory outside CHarm, so the user should free the memory outside CHarm as well).
-
_Bool distributed#
If
0, the structure is not distributed, meaning that each process (be it an OpenMP or MPI process) accesses the same data throughcharm_point.lat,charm_point.lon,charm_point.randcharm_point.w.If
1, the structure is distributed across MPI processes, that is, each MPI process accesses different data throughcharm_point.lat,charm_point.lon,charm_point.randcharm_point.w.
Note
If CHarm was compiled without the MPI support (default),
charm_point.distributedis always0.
Members available to distributed structures only
Note
The members that follow are available only when CHarm is compiled with the MPI support (
--enable-mpi, refer to charm_mpi for further details).-
size_t local_nlat#
Number of points in the latitudinal direction locally accessible to an MPI process. If
0, no points are stored locally in the structure.
-
size_t local_nlon#
Number of points in the longitudinal direction locally accessible to an MPI process. If
0, no points are stored locally in the structure.
-
size_t local_npoint#
Total number of points locally accessible to an MPI process.
It is computed in the same way as
charm_point.npoint, but usingcharm_point.local_nlatandcharm_point.local_nloninstead ofcharm_point.nlatandcharm_point.nlon.
-
size_t local_0_start#
Index, at which the local portions of
charm_point.lat,charm_point.randcharm_point.wstart in thecharm_pointstructure. Any value is accepted ifcharm_point.local_nlatis0.Assume that
charm_point *pntwithpnt->distributed == 0holds all your data points. Now assume that you distribute the same data in chunks across MPI processes, obtaining somecharm_point *pnt_distwithpnt_dist->distributed == 1. For an MPI process,pnt_dist.local_0_startis the index ofpnt_dist->lat[0]in thepnt->latarray. This means thatpnt->lat[pnt_dist->local_0_start + idx] == pnt_dist->[idx], whereidx < pnt_dist->local_nlat. If the grid is symmetric with respect to the equator, the index inpnt_dist.local_0_startis always divided by2.
-
MPI_Comm comm#
MPI communicator defining the group of MPI processes to distribute the structure across.
-
struct charm_cell#
Structure to store evaluation cells.
This structure cannot be distributed across MPI processes.
Public Members
-
int type#
Organization of cells in the structure.
The following options are available:
CHARM_CRD_CELL_GRIDfor a user-defined grid of cells,CHARM_CRD_CELL_SCATTEREDfor a user-defined set of scattered cells.
-
size_t nlat#
Total number of cells in the latitudinal direction.
-
size_t nlon#
Total number of cells in the longitudinal direction.
-
size_t ncell#
Total number of cells.
For scattered cells (
charm_cell.typeset toCHARM_CRD_CELL_SCATTERED),charm_cell.ncellequals tocharm_cell.nlatand tocharm_cell.nlon.For grids (
charm_cell.typeset toCHARM_CRD_CELL_GRID),charm_cell.ncellis given ascharm_cell.nlat * charm_cell.nlon.
-
double *latmin#
Pointer to a
charm_cell.nlatarray elements representing minimum cell latitudes in radians.
-
double *latmax#
Pointer to a
charm_cell.nlatarray elements representing maximum cell latitudes in radians.
-
double *lonmin#
Pointer to a
charm_cell.nlonarray elements representing minimum cell longitudes in radians.
-
double *lonmax#
Pointer to a
charm_cell.nlonarray elements representing maximum cell longitudes in radians.
-
double *r#
Pointer to a
charm_cell.nlatarray elements representing spherical radii in metres.
-
_Bool owner#
If
1, the memory associated withcharm_cell.latmin,charm_cell.latmax,charm_cell.lonmin,charm_cell.lonmaxandcharm_cell.rwas allocated by CHarm, socharm_crd_cell_free()deallocates it. Ifcharm_cell.owneris0, the memory associated with these arrays was allocated outside CHarm, socharm_crd_cell_free()does not deallocate it (the user allocated this memory outside CHarm, so the user should free the memory outside CHarm as well).
-
int type#