charm_gfm#
Module for spectral gravity forward modelling. Offers:
global and spatially limited spectral gravity forward modelling of band-limited topographic masses having constant, lateral and 3D varying mass density.
Note
This documentation is written for double precision version of CHarm.
Note
Spatially limited integration radius is available only if
--enable-mpfr is specified when calling ./configure during
the installation. See Installing for further
details.
Symbolic constants representing types of truncation coefficients
Note
The symbolic constants that follow are available only if
--enable-mpfr is specified when calling ./configure during
the installation. See Installing for further
details.
-
CHARM_GFM_Q00 0U#
Truncation coefficients \(Q^{0,0,j}_{npi}(r, \psi_0, R)\) defined by Eq. (88) of Bucha (2025).
-
CHARM_GFM_Q10 10U#
Truncation coefficients \(Q^{1, 0, j}_{npi}(r, \psi_0, R)\) defined by Eq. (88) of Bucha (2025).
-
CHARM_GFM_Q11 11U#
Truncation coefficients \(Q^{1, 1, j}_{npi}(r, \psi_0, R)\) defined by Eq. (88) of Bucha (2025).
-
CHARM_GFM_Q20 20U#
Truncation coefficients \(Q^{2, 0, j}_{npi}(r, \psi_0, R)\) defined by Eq. (88) of Bucha (2025).
-
CHARM_GFM_Q21 21U#
Truncation coefficients \(Q^{2, 1, j}_{npi}(r, \psi_0, R)\) defined by Eq. (88) of Bucha (2025).
-
CHARM_GFM_Q22 22U#
Truncation coefficients \(Q^{2, 2, j}_{npi}(r, \psi_0, R)\) defined by Eq. (88) of Bucha (2025).
-
CHARM_GFM_NEAR_ZONE 1#
Symbolic constant that can be used to forward model near-zone masses in the
charm_gfm_cap_density_*functions. Near-zone masses are defined as masses up to some spherical distance \(\psi_0\) from evaluation points.
-
CHARM_GFM_FAR_ZONE (-1)#
Symbolic constant that can be used to forward model far-zone masses in the
charm_gfm_cap_density_*functions. Far-zone masses are defined as masses beyond some spherical distance \(\psi_0\) from evaluation points.
Global spectral gravity forward modelling
These functions perform spectral gravity forward modelling using full integration radius, that is, masses all around the body are forward modelled. The density may be constant, laterally variable or 3D variable.
-
void charm_gfm_global_density_3d(const charm_shc *shape_shcs, unsigned long shape_nmax, double shape_radius_ref, charm_shc **density_shcs, unsigned long *density_nmax, unsigned density_order, double grav_const, double mass, unsigned shape_power_min, unsigned shape_power_max, unsigned long potential_shcs_nmax, const char *shape_density_shcs_path, const char *potential_shcs_path, const char *shcs_file_format, charm_shc *potential_shcs, charm_err *err)#
Performs global spectral gravity forward modelling of 3D mass densities.
Assume a body having a star-shaped surface expandable into surface spherical harmonics. Let the body’s density vary in spherical coordinates \(r', \varphi', \lambda'\) as a finite-degree polynomial in the radial direction as
\[\rho(r', \varphi', \lambda') = \sum_{i = 0}^I \rho_i(\varphi', \lambda') \, \left( r' \right)^i{,}\]where \(\rho_i(\varphi', \lambda')\) are laterally varying polynomial coefficients of the density that are expandable into surface spherical harmonics. From now on, we assume only those masses that are found between some internal sphere and the surface of the body. This function computes spherical harmonic coefficients of the gravitational potential implied by such mass distributions.
The routine has several input parameters, so it is useful to group them into a few categories:
parameters defining the shape of the gravitating body (
shape_shcs,shape_nmax,shape_radius_ref),parameters defining the density of the gravitating body (
density_shcs,density_nmax,density_order),parameters related to outputs (
shape_density_shcs_path,potential_shcs_path,shcs_file_format,potential_shcs,err),parameters related to the method (
shape_power_min,shape_power_max,grav_const,mass,potential_shcs_nmax).
Each parameter is described in detail below.
References:
Bucha B (2025) Spectral gravity forward modelling of 3D variable densities using an arbitrary integration radius with application to lunar topographic masses. Journal of Geodesy, doi: 10.1007/s00190-025-01951-9.
- Parameters:
shape_shcs – [in] Spherical harmonic coefficients defining the shape of the gravitating body (e.g., planetary topography), that is, the upper integration limit in the radial direction. It must hold that (i) surface spherical harmonic synthesis of coefficients from
shape_shcsgives the full spherical radius of surface points in metres, (ii)shape_shcs->mu == 1.0and (iii)shape_shcs->r == 1.0.shape_nmax – [in] Maximum harmonic degree to take when defining the shape of the gravitating body by the spherical harmonic coefficients in
shape_shcs. The value cannot be larger thanshape_shcs->nmax.shape_radius_ref – [in] Radius in metres defining the lower integration limit in the radial direction. Forward modelled will be the masses between the sphere of radius
shape_radius_refand the body’s surface defined byshape_shcs. The value must be positive.density_shcs – [in] Pointer to an array of pointers to
charm_shcstructures defining spherical harmonic coefficients of the polynomial density coefficients. For instance,density_shcs[0]is a pointer tocharm_shcstructure defining the spherical harmonic coefficients of the zero-order laterally varying polynomial density coefficients \(\rho_0(\varphi', \lambda')\);density_shcs[1]is a pointer tocharm_shcstructure holding the spherical harmonic coefficients of the first-order laterally varying polynomial density coefficients \(\rho_1(\varphi', \lambda')\); etc.density_nmax – [in] Pointer to an array of unsigned long integers defining the maximum harmonic degrees to be used with the respective
charm_shcstructures indensity_shcswhen defining the polynomial density coefficients. More specifically,density_nmax[i]is the maximum harmonic degree that will be used when defining the polynomial density coefficients of order \(i\) bydensity_shcs[i]; it must hold thatdensity_nmax[i] <= density_shcs[i]->nmax.density_order – [in] Maximum order of the density polynomial. Both
density_shcsanddensity_nmaxmust have an access to at leastdensity_order + 1array elements.grav_const – [in] Newton’s gravitational constant.
mass – [in] Mass of the body. The value affects the output potential coefficients
potential_shcs(see below) but do not affect the gravitational effects synthesized from the coefficients. This is because the value is also used to computepotential_shcs->mu, so it cancels out during the synthesis. Only non-zero values are accepted.shape_power_min – [in] Minimum topography power. Usually, the value is
1. Values higher than1do not produce a complete gravity field model, but are useful in specific situations. At any rate, the value must be positive.shape_power_max – [in] Maximum topography power. The value cannot be smaller than
shape_power_min. The larger is the value, the more complete is the spectral gravity forward modelling.potential_shcs_nmax – [in] Maximum harmonic degree of the output spherical harmonic coefficients of the implied gravitational potential in
potential_shcs(see below). The value cannot be larger thanpotential_shcs->nmax.shape_density_shcs_path – [in] Path and prefix of output files containing spherical harmonic coefficients of shape-density functions. For instance, the
"/tmp/shape-density"string will cause that several files (depending onshape_power_maxanddensity_order) having the prefixshape-densitywill be created in the/tmpfolder. IfNULL, coefficients of shape-density functions are not exported. The topographic-density function is defined as (see Bucha 2025) \(\left( \frac{r_{\mathrm{S}}(\varphi', \lambda') - R}{R} \right)^p \, \rho_i(\varphi', \lambda') \, R^i\), where \(r_{\mathrm{S}}\) is the radius of a surface point synthesized fromshape_shcsup to degreeshape_nmax, \(R\) isshape_radius_refand \(\rho_i(\varphi', \lambda')\) are the polynomial density coefficients of the \(i\)-th order synthesized fromdensity_shcs[i]up to degreedensity_nmax[i].potential_shcs_path – [in] Path and prefix of output files containing spherical harmonic coefficients of gravitational potential contributions due to the shape-density functions. If
NULL, the coefficients are not exported.shcs_file_format – [in] String defining the format of output files that are specified by
shape_density_shcs_pathand/orpotential_shcs_path. Accepted strings are"tbl","mtx","dov","bin". If bothshape_density_shcs_pathandpotential_shcs_pathareNULL,shcs_file_formatcan beNULL, too.potential_shcs – [out] Spherical harmonic coefficients of the output gravitational potential. In a special case, when
potential_shcsisNULL, computed and exported are only partial contributions due to the individual shape and density powers to the potential coefficients, that is, they are not summed to yield potential coefficients. The partial outputs are exported throughshape_density_shcs_pathandpotential_shcs_path. The output potential coefficients can be reproduced by summing all output files with the prefixpotential_shcs_path. This option is useful if you need to save some memory.err – [out] Error reported by the function (if any).
-
void charm_gfm_global_density_lateral(const charm_shc *shape_shcs, unsigned long shape_nmax, double shape_radius_ref, charm_shc *density_shcs, unsigned long density_nmax, double grav_const, double mass, unsigned shape_power_min, unsigned shape_power_max, unsigned long potential_shcs_nmax, const char *shape_density_shcs_path, const char *potential_shcs_path, const char *shcs_file_format, charm_shc *potential_shcs, charm_err *err)#
Performs global spectral gravity forward modelling with laterally varying mass density.
The function input parameters are the same as in
charm_gfm_global_density_3d()with two exceptions:density_shcsis now a pointer to acharm_shcstructure that holds spherical harmonic coefficients of the laterally varying mass density function,density_nmaxis anunsigned longinteger indicating the maximum harmonic degree, up to which the coefficients fromdensity_shcsare used in forward modelling.
-
void charm_gfm_global_density_const(const charm_shc *shape_shcs, unsigned long shape_nmax, double shape_radius_ref, double density, double grav_const, double mass, unsigned shape_power_min, unsigned shape_power_max, unsigned long potential_shcs_nmax, const char *shape_shcs_path, const char *potential_shcs_path, const char *shcs_file_format, charm_shc *potential_shcs, charm_err *err)#
Performs global spectral gravity forward modelling with a constant mass density after Hirt and Kuhn (2014).
The function input parameters are the same as in
charm_gfm_global_density_3d()with two exceptions.A single constant mass
densityreplaced thedensity_shcsanddensity_nmaxparameters.The
shape_shcs_pathvariable replacedshape_density_shcs_pathand is used to export spherical harmonic coefficients of the powers of the topographic height function \(\left( \frac{r_{\mathrm{S}}(\varphi', \lambda') - R}{R} \right)^p\), where \(r_{\mathrm{S}}\) is defined byshape_shcsandshape_nmaxand \(R\) isshape_radius_ref. This time, the coefficients of the exported powers of the topographic height function are therefore not multiplied by the constant density, hence the new variable name.
Note
The function is optimized in terms of memory usage. It therefore requires smaller amount of working RAM than
charm_gfm_global_density_3d()andcharm_gfm_global_density_lateral()would require if used with a constant mass density as their special case.
Cap-modified spectral gravity forward modelling
These functions perform spectral gravity forward modelling using spatially restricted integration radius. They can be used to compute the gravitational contribution of masses up to (near-zone) or beyond (far-zone) some distance from evaluation points as opposed to the global integration from the previous routines. The density may be constant, laterally variable or 3D variable.
Note
These functions are compiled only if --enable-mpfr is specified
when calling ./configure during the installation. See
Installing for further details.
-
void charm_gfm_cap_density_3d(const charm_shc *shape_shcs, unsigned long shape_nmax, double shape_radius_ref, charm_shc **density_shcs, unsigned long *density_nmax, unsigned density_order, double grav_const, double mass, unsigned shape_power_min, unsigned shape_power_max, unsigned radial_derivative_min, unsigned radial_derivative_max, double evaluation_spherical_radius, double integration_radius, unsigned u, unsigned v, int zone, mpfr_prec_t nbits, unsigned long potential_shcs_nmax, const char *shape_density_shcs_path, const char *potential_shcs_path, const char *shcs_file_format, charm_shc **potential_shcs, charm_err *err)#
Performs cap-modified spectral gravity forward modelling of 3D mass densities.
Similarly as
charm_gfm_global_density_3d(), this function forward models 3D mass distributions. The difference is that it considers only those topographic masses that are located inside or outside a spherical cap centred at evaluation points. The output spherical harmonic coefficients can thus be used to compute near-zone or far-zone effects on some gravitational field quantity.Many of the function parameters have exactly the same meaning as in
charm_gfm_global_density_3d(), so explained will be the new ones only.Tip
Use
charm_gfm_cap_q_check_prec()to determine the appropriate value of the input parameternbits.References:
Bucha B (2025) Spectral gravity forward modelling of 3D variable densities using an arbitrary integration radius with application to lunar topographic masses. Journal of Geodesy, doi: 10.1007/s00190-025-01951-9.
Note
This function is compiled only if
--enable-mpfris specified when calling./configureduring the installation. See Installing for further details.Warning
The function parameters
uandvspecify the output quantity, of which spherical harmonic coefficientspotential_shcsare computed. However, the output quantity is not necessarily some derivative of the gravitational potential in the local north-oriented reference frame (LNOF). Yet, you can get the directional potential derivatives in LNOF if you do some rather simple manipulations on your own (see Bucha, 2025). The exception is the caseu = 0andv = 0, which leads directly to the spherical harmonic coefficients of the gravitational potential.- Parameters:
radial_derivative_min – [in] Minimum radial derivative of the quantity specified by
uandv(see below). Use0for no radial derivative.radial_derivative_max – [in] The same as
radial_derivative_minbut for the maximum radial derivative.evaluation_spherical_radius – [in] Spherical radius in metres of evaluation points. The output coefficients
potential_shcsrefer to this sphere. If your goal is to compute youru,v-quantity on an irregular surface (e.g., on the Earth’s surface), use analytical continuation through radial derivatives. These can be specified byradial_derivative_minandradial_derivative_max.integration_radius – [in] Cap radius in radians defining the masses to be integrated. Masses within the cap are here called near-zone masses and masses beyond the cap are here called far-zone masses.
u – [in] Order of the potential derivative, of which spherical harmonic coefficients
potential_shcsare computed. Use0to get the gravitational potential, use1to get quantities related to the first-order potential derivatives or use2for quantities related to the second-order potential derivatives. Values larger than2are not allowed.v – [in] Order of the potential derivative with respect to the spherical distance. Valid values are
0,...,u.zone – [in] Non-negative integer to integrate near-zone masses (e.g.,
CHARM_GFM_NEAR_ZONE) or negative integer to integrate far-zone masses (e.g.,CHARM_GFM_FAR_ZONE).nbits – [in] Number of bits to represent the significand of all floating point numbers used to compute the truncation coefficients.
potential_shcs – [out] Double pointer to the
charm_shcstructure.potential_shcsmust have access toradial_derivative_max - radial_derivative_min + 1charm_shcstructures, each of which must have allocated memory for coefficients at least up to degreepotential_shcs_nmax. In a special case, whenpotential_shcsisNULL, computed and exported are only partial contributions of individual shape and density powers to the potential coefficients, that is, they are not summed. The partial outputs are exported throughshape_density_shcs_pathandpotential_shcs_path. The output potential coefficients can be reproduced using the output files with the prefixpotential_shcs_path. This option is useful if you need to save some memory.
-
void charm_gfm_cap_density_lateral(const charm_shc *shape_shcs, unsigned long shape_nmax, double shape_radius_ref, charm_shc *density_shcs, unsigned long density_nmax, double grav_const, double mass, unsigned shape_power_min, unsigned shape_power_max, unsigned radial_derivative_min, unsigned radial_derivative_max, double evaluation_spherical_radius, double integration_radius, unsigned u, unsigned v, int zone, mpfr_prec_t nbits, unsigned long potential_shcs_nmax, const char *shape_density_shcs_path, const char *potential_shcs_path, const char *shcs_file_format, charm_shc **potential_shcs, charm_err *err)#
Performs cap-modified spectral gravity forward modelling with laterally varying mass density.
The function input parameters are the same as in
charm_gfm_cap_density_3d()with two exceptions:density_shcsis now a pointer to acharm_shcstructure that holds spherical harmonic coefficients of the laterally varying mass density function,density_nmaxis anunsigned longinteger indicating the maximum harmonic degree, up to which the coefficients fromdensity_shcsare used in forward modelling.
-
void charm_gfm_cap_density_const(const charm_shc *shape_shcs, unsigned long shape_nmax, double shape_radius_ref, double density, double grav_const, double mass, unsigned shape_power_min, unsigned shape_power_max, unsigned radial_derivative_min, unsigned radial_derivative_max, double evaluation_spherical_radius, double integration_radius, unsigned u, unsigned v, int zone, mpfr_prec_t nbits, unsigned long potential_shcs_nmax, const char *shape_shcs_path, const char *potential_shcs_path, const char *shcs_file_format, charm_shc **potential_shcs, charm_err *err)#
Performs cap-modified spectral gravity forward modelling with a constant mass density after Bucha et al (2019).
The function input parameters are the same as in
charm_gfm_cap_density_3d()with two exceptions.A single constant mass
densityreplaced thedensity_shcsanddensity_nmaxparameters.The
shape_shcs_pathvariable replacedshape_density_shcs_pathand is used to export spherical harmonic coefficients of the powers of the topographic height function \(\left( \frac{r_{\mathrm{S}}(\varphi', \lambda') - R}{R} \right)^p\), where \(r_{\mathrm{S}}\) is defined byshape_shcsandshape_nmaxand \(R\) isshape_radius_ref. This time, the coefficients of the exported powers of the topographic height function are therefore not multiplied by the constant density, hence the new variable name.
Note
The function is optimized in terms of memory usage. It therefore requires smaller amount of working RAM than
charm_gfm_cap_density_3d()andcharm_gfm_cap_density_lateral()would require if used with a constant mass density as their special case.
-
void charm_gfm_cap_q(const mpfr_t shape_radius_ref, const mpfr_t evaluation_spherical_radius, const mpfr_t integration_radius, unsigned long potential_shcs_nmax, unsigned shape_power_max, unsigned radial_derivative_min, unsigned radial_derivative_max, unsigned density_order, int zone, unsigned type, mpfr_prec_t nbits, mpfr_t *qkpin, charm_err *err)#
Computes trunction coefficients
CHARM_GFM_Q00,CHARM_GFM_Q10,CHARM_GFM_Q11,CHARM_GFM_Q20,CHARM_GFM_Q21,CHARM_GFM_Q22that are used in spectral gravity forward modelling with spatially limited integration radius.Tip
Use
charm_gfm_cap_q_check_prec()to determine the appropriate value of the input parameternbits.Note
This function is compiled only if
--enable-mpfris specified when calling./configureduring the installation. See Installing for further details.- Parameters:
shape_radius_ref – [in] Radius of the reference sphere in metres.
evaluation_spherical_radius – [in] Spherical radius of evaluation points in metres.
integration_radius – [in] Integration radius in radians.
potential_shcs_nmax – [in] Maximum spherical harmonic degree of the truncation coefficients.
shape_power_max – [in] Maximum topography power of the truncation coefficients (must be larger than zero).
radial_derivative_min – [in] Minimum radial derivative of the truncation coefficients.
radial_derivative_max – [in] Maximum radial derivative of the truncation coefficients.
density_order – [in] Maximum order of the polynomial density coefficients.
zone – [in] Non-negative integer for near-zone truncation coefficients (e.g.,
CHARM_GFM_NEAR_ZONE) or negative integer for far-zone truncation coefficients (e.g.,CHARM_GFM_FAR_ZONE).type – [in] Type of the truncation coefficients (symbolic constant
CHARM_GFM_Q00,CHARM_GFM_Q10,CHARM_GFM_Q11,CHARM_GFM_Q20,CHARM_GFM_Q21orCHARM_GFM_Q22).nbits – [in] Number of bits to represent the significand of all floating point numbers used to compute the truncation coefficients.
qkpin – [out] Pointer to an array of
nqelements of thempfr_tdata type.nqcan be obtained bycharm_gfm_cap_nq()or it can be computed as follows. Letnmax = potential_shcs_nmax,pmax = shape_power_max,kmin = radial_derivative_min,kmax = radial_derivative_max,imax = density_order. The total number of elements is thennq = (kmax - kmin + 1) * pmax * (nmax + 1) * (imax + 1). The value of \(\partial^k Q^{uvj}_{npi}(r, \psi_0, R) / \partial r^k\) can be accessed asqkpin[(((k - kmin) * pmax + (p - 1)) * (imax + 1) + i) * (nmax + 1) + n].qkpincan thus be considered as a 4D-array, in which harmonic degreenvaries fastest and the order of the radial derivative varies slowest, or, more precisely, the order isn,i,p, andk.err – [out] Error reported by the function (if any).
-
size_t charm_gfm_cap_nq(unsigned long potential_shcs_nmax, unsigned shape_power_max, unsigned radial_derivative_min, unsigned radial_derivative_max, unsigned density_order, charm_err *err)#
Returns the number of truncation coefficients
qkpincomputed bycharm_gfm_cap_q().- Parameters:
potential_shcs_nmax – [in] Maximum spherical harmonic degree of the truncation coefficients.
shape_power_max – [in] Maximum topography power of the truncation coefficients (must be larger than zero).
radial_derivative_min – [in] Minimum radial derivative of the truncation coefficients.
radial_derivative_max – [in] Maximum radial derivative of the truncation coefficients.
density_order – [in] Maximum order of the polynomial density coefficients.
err – [out] Error reported by the function (if any).
- Returns:
On success, returned is the number of truncation coefficients. On error,
0is returned in addition to the error reporting througherr.
-
long charm_gfm_cap_q_check_prec(const mpfr_t shape_radius_ref, const mpfr_t evaluation_spherical_radius, const mpfr_t integration_radius, unsigned long potential_shcs_nmax, unsigned shape_power_max, unsigned radial_derivative_min, unsigned radial_derivative_max, unsigned density_order, unsigned type, mpfr_prec_t nbits, mpfr_prec_t nbits_ref, charm_err *err)#
Estimates the lowest number of correct digits of the sum of near- and far-zone truncation coefficients obtained by
charm_gfm_cap_q()when using the same input parameters. The uncertainty of the estimate is 1 digit. In case of an error, returned is a negative integer.First, near- and far-zone truncation coefficients are computed and summed using the
nbitsprecision. Then, reference coefficients from global spectral gravity forward modelling are evaluated using thenbits_refprecision. Finally, the sums are compared with respect to the reference values.Tip
Use this function before calling
charm_gfm_cap_density_*()to check whethernbitsthat you enter tocharm_gfm_cap_density_*()is large enough to ensure accurate computation of truncation coefficients. The number returned by this function should be at least7,16or34for gravity forward modelling in single, double or quadruple precision, respectively. Otherwise, increasenbitsandnbits_refuntil the desired accuracy is met. Once the value returned by this function is large enough, that particular (or higher)nbitscan safely entercharm_gfm_cap_density_*().Note
If all sums of truncation coefficients are computed accurately to the last bit, returned is a large integer (
LONG_MAXfromlimits.h). This may happen, for instance, ifnmaxis so small that all truncation coefficients of a giventypeare zero (e.g.,nmax = 0fortype = CHARM_GFM_Q20).Note
The number returned is related to the sum of near- and far-zone truncation coefficients. Usually, all near- and far-zone coefficients are more accurate than indicated by the returned value. Therefore, even a smaller value of
nbitsmay suffice to get accurate outputs fromcharm_gfm_cap_density_*().Note
This function is compiled only if
--enable-mpfris specified when calling./configureduring the installation. See Installing for further details.- Parameters:
shape_radius_ref – [in] Radius of the reference sphere in metres.
evaluation_spherical_radius – [in] Spherical radius of the evaluation point in metres.
integration_radius – [in] Integration radius in radians.
potential_shcs_nmax – [in] Maximum spherical harmonic degree of the truncation coefficients.
shape_power_max – [in] Maximum topography power of the truncation coefficients (must be larger than zero).
radial_derivative_min – [in] Minimum radial derivative of the truncation coefficients.
radial_derivative_max – [in] Maximum radial derivative of the truncation coefficients.
density_order – [in] Maximum order of the density polynomial.
type – [in] Type of the truncation coefficients (symbolic constant
CHARM_GFM_Q00,CHARM_GFM_Q10,CHARM_GFM_Q11,CHARM_GFM_Q20,CHARM_GFM_Q21orCHARM_GFM_Q22).nbits – [in] Number of bits to represent the significand of all floating point numbers used to compute the truncation coefficients.
nbits_ref – [in] Number of bits to represent the significand of all floating point numbers used to compute the reference values (cannot be smaller than
nbits).err – [out] Error reported by the function (if any).
- Returns:
An estimate of the lowest number of correct digits across all harmonic degrees up to
potential_shcs_nmax, all topography powers up toshape_power_maxand radial derivatives fromradial_derivative_mintoradial_derivative_max, respectively.