charm_crd#
Module to work with the coordinates of evaluation points/cells:
defines
charm_point
andcharm_cell
structures,allocates, initializes and frees
charm_point
andcharm_cell
,computes
charm_point
for 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
type
withnlat
latitudes,nlon
longitudes andnlat
spherical radii. Iftype
isCHARM_CRD_POINT_GRID_GL
,CHARM_CRD_POINT_GRID_DH1
orCHARM_CRD_POINT_GRID_DH2
,nlat
integration weights are allocated in addition. The memory of all array elements is uninitialized, so their values are undefined.Valid values of
type
are constantsCHARM_CRD_POINT_SCATTERED
,CHARM_CRD_POINT_GRID
,CHARM_CRD_POINT_GRID_GL
,CHARM_CRD_POINT_GRID_DH1
andCHARM_CRD_POINT_GRID_DH2
.For
charm_point *pnt
returned by this function withtype
beingCHARM_CRD_POINT_GRID
,CHARM_CRD_POINT_GRID_GL
,CHARM_CRD_POINT_GRID_DH1
orCHARM_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 - 1
andj = 0, 1, ..., nlon - 1
.For
type
set 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 withfree
will lead to memory leaks.- Returns:
On success, returned is a pointer to the
charm_point
structure. On error,NULL
is 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
, takesnlat
spherical latitudes from the array pointed to bylat
,nlon
spherical longitudes fromlon
andnlat
spherical radii fromr
(shallow copy).Valid values of
type
are constantsCHARM_CRD_POINT_SCATTERED
,CHARM_CRD_POINT_GRID
,CHARM_CRD_POINT_GRID_GL
,CHARM_CRD_POINT_GRID_DH1
andCHARM_CRD_POINT_GRID_DH2
.If
type
isCHARM_CRD_POINT_SCATTERED
,nlat
must be equal tonlon
andeach of
lat
,lon
andr
must have access tonlat
elements.
If
type
isCHARM_CRD_POINT_GRID
,CHARM_CRD_POINT_GRID_GL
,CHARM_CRD_POINT_GRID_DH1
orCHARM_CRD_POINT_GRID_DH2
,lat
must have access tonlat
elements,lon
must have access tonlon
elements, andr
must have access tonlat
elements.
Note
See
charm_crd_point_malloc()
to find out how to access the coordinates stored in the returnedcharm_point
structure.Note
See
charm_shc_init()
for the rationale behind the shallow copies of input arrayslat
,lon
andr
. The memory of these arrays must be treated in the same fashion as shown therein with thec
ands
arrays.Warning
The structure returned must be deallocated using
charm_crd_point_free()
. The usual deallocation withfree
will lead to memory leaks.- Returns:
On success, returned is a pointer to the
charm_point
structure. On error,NULL
is 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 ifpnt
isNULL
.If
pnt->owner
is1
, the function releases all the memory that is associated withpnt
, including that of its array members. Ifpnt->owner
is0
, 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 withfree
will lead to memory leaks.- Returns:
On success, returned is a pointer to the
charm_point
structure. On error,NULL
is 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 ifnmax
is larger than7200
or 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
nlat
and the number of longitudesnlon
of 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
nmax
on 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 withfree
will lead to memory leaks.- Returns:
On success, returned is a pointer to the
charm_point
structure. On error,NULL
is returned.
-
void charm_crd_point_dh1_shape(unsigned long nmax, size_t *nlat, size_t *nlon)#
Computes the number of latitudes
nlat
and the number of longitudesnlon
of 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
nlat
and the number of longitudesnlon
of 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
type
withnlat
minimum and maximum latitudes,nlon
minimum and maximum longitudes andnlat
spherical radii. The memory of all array elements is uninitialized, so their values are undefined.Valid values of
type
are constantsCHARM_CRD_CELL_GRID
andCHARM_CRD_CELL_SCATTERED
.For
charm_cell *cell
returned by this function withtype
beingCHARM_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 - 1
andj = 0, 1, ..., nlon - 1
.For
type
set 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 withfree
will lead to memory leaks.- Returns:
On success, returned is a pointer to the
charm_cell
structure. On error,NULL
is 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
, takesnlat
minimum and maximum spherical latitudes from the arrays pointed to bylatmin
andlatmax
,nlon
minimum and maximum spherical longitudes fromlonmin
andlonmax
andnlat
spherical radii fromr
, respectively (shallow copy).Accepted values of
type
areCHARM_CRD_CELL_GRID
andCHARM_CRD_CELL_SCATTERED
.If
type
isCHARM_CRD_CELL_SCATTERED
,nlat
must be equal tonlon
andeach of
latmin
,latmax
,lonmin
,lonmax
andr
must have access tonlat
elements.
If
type
isCHARM_CRD_CELL_GRID
,latmin
andlatmax
must each have access tonlat
elements,lonmin
andlonmax
must each have access tonlon
elements,r
must have access tonlat
elements.
Note
See
charm_crd_cell_malloc()
to find out how to access the coordinates stored in the returnedcharm_cell
structure.Note
See
charm_shc_init()
for the rationale behind the shallow copies of input arrayslatmin
,latmax
,lonmin
,lonmax
andr
. The memory of these arrays must be treated in the same fashion as shown therein with thec
ands
arrays.Warning
The structure created by this function must be deallocated using
charm_crd_cell_free()
. The usual deallocation withfree
will lead to memory leaks.- Returns:
On success, returned is a pointer to the
charm_cell
structure. On error,NULL
is 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 ifcell
isNULL
.If
cell->owner
is1
, the function releases all the memory that is associated withcell
, including that of its array members. Ifcell->owner
is0
, the arrays are not released from the memory, because they were not allocated by CHarm.
Enums
-
enum [anonymous]#
Defines supported values of
charm_point.type
toCHARM_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_SCATTERED
andCHARM_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_SCATTERED
for user-defined scattered points,CHARM_CRD_POINT_GRID
for a user-defined grid,CHARM_CRD_POINT_GRID_GL
for the Gauss-Legendre grid (Sneeuw, 1994) withnmax + 1
latitudes and2 * nmax + 2
longitudes (nmax
is maximum harmonic degree),CHARM_CRD_POINT_GRID_DH1
for the non-equiangular Driscoll-Healy grid as defined by Driscoll and Healy (1994) with2 * nmax + 2
latitudes and2 * nmax + 2
longitudes, andCHARM_CRD_POINT_GRID_DH2
for an equiangular modification of the Driscoll-Healy grid (Wieczorek and Meschede, 2018) with2 * nmax + 2
latitudes and4 * nmax + 4
longitudes.
-
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_nlat
across 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_nlon
across 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.type
is eitherCHARM_CRD_POINT_GRID
,CHARM_CRD_POINT_GRID_GL
,CHARM_CRD_POINT_GRID_DH1
orCHARM_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_nlon
andcharm_point.nlon
are equal.
-
size_t npoint#
Total number of points.
For scattered points (
charm_point.type == CHARM_CRD_POINT_SCATTERED
), equals tocharm_point.nlat
and tocharm_point.nlon
.For grids (
charm_point.type
is 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_npoint
across all MPI processes withincharm_point.comm
.
-
double *lat#
Pointer to an array of latitudes in radians.
For non-distributed structures,
charm_point.nlat
array elements are associated with the pointer. For distributed structures,charm_point.local_nlat
elements are associated.
-
double *lon#
Pointer to an array of longitudes in radians.
For non-distributed structures,
charm_point.nlon
array elements are associated with the pointer. For distributed structures,charm_point.local_nlon
elements 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_point
structures returned by thecharm_crd_point_gl()
,charm_crd_point_dh1()
andcharm_crd_point_dh2()
functions, that is, ifcharm_point.type
isCHARM_CRD_POINT_GRID_GL
,CHARM_CRD_POINT_GRID_DH1
, orCHARM_CRD_POINT_GRID_DH2
. Otherwise, the pointer isNULL
and 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.r
andcharm_point.w
was 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.r
andcharm_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.r
andcharm_point.w
.
Note
If CHarm was compiled without the MPI support (default),
charm_point.distributed
is 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_nlat
andcharm_point.local_nlon
instead ofcharm_point.nlat
andcharm_point.nlon
.
-
size_t local_0_start#
Index, at which the local portions of
charm_point.lat
,charm_point.r
andcharm_point.w
start in thecharm_point
structure. Any value is accepted ifcharm_point.local_nlat
is0
.Assume that
charm_point *pnt
withpnt->distributed == 0
holds all your data points. Now assume that you distribute the same data in chunks across MPI processes, obtaining somecharm_point *pnt_dist
withpnt_dist->distributed == 1
. For an MPI process,pnt_dist.local_0_start
is the index ofpnt_dist->lat[0]
in thepnt->lat
array. 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_start
is 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_GRID
for a user-defined grid of cells,CHARM_CRD_CELL_SCATTERED
for 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.type
set toCHARM_CRD_CELL_SCATTERED
),charm_cell.ncell
equals tocharm_cell.nlat
and tocharm_cell.nlon
.For grids (
charm_cell.type
set toCHARM_CRD_CELL_GRID
),charm_cell.ncell
is given ascharm_cell.nlat * charm_cell.nlon
.
-
double *latmin#
Pointer to a
charm_cell.nlat
array elements representing minimum cell latitudes in radians.
-
double *latmax#
Pointer to a
charm_cell.nlat
array elements representing maximum cell latitudes in radians.
-
double *lonmin#
Pointer to a
charm_cell.nlon
array elements representing minimum cell longitudes in radians.
-
double *lonmax#
Pointer to a
charm_cell.nlon
array elements representing maximum cell longitudes in radians.
-
double *r#
Pointer to a
charm_cell.nlat
array elements representing spherical radii in metres.
-
_Bool owner#
If
1
, the memory associated withcharm_cell.latmin
,charm_cell.latmax
,charm_cell.lonmin
,charm_cell.lonmax
andcharm_cell.r
was allocated by CHarm, socharm_crd_cell_free()
deallocates it. Ifcharm_cell.owner
is0
, 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#