File: kernel.cpp

package info (click to toggle)
groops 0%2Bgit20250907%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 11,140 kB
  • sloc: cpp: 135,607; fortran: 1,603; makefile: 20
file content (401 lines) | stat: -rw-r--r-- 13,555 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/***********************************************/
/**
* @file kernel.cpp
*
* @brief Isotropic harmonic integral kernels.
*
* @author Torsten Mayer-Guerr
* @date 2003-09-03
*
*/
/***********************************************/

#define DOCSTRING_Kernel

#include "base/import.h"
#include "base/legendrePolynomial.h"
#include "config/configRegister.h"
#include "classes/gravityfield/gravityfield.h"
#include "classes/kernel/kernelPoisson.h"
#include "classes/kernel/kernelStokes.h"
#include "classes/kernel/kernelHotine.h"
#include "classes/kernel/kernelSingleLayer.h"
#include "classes/kernel/kernelWaterHeight.h"
#include "classes/kernel/kernelDeformation.h"
#include "classes/kernel/kernelCoefficients.h"
#include "classes/kernel/kernelGeoid.h"
#include "classes/kernel/kernelRadialGradient.h"
#include "classes/kernel/kernelFilterGauss.h"
#include "classes/kernel/kernelSelenoid.h"
#include "classes/kernel/kernelBottomPressure.h"
#include "classes/kernel/kernelBlackmanLowPass.h"
#include "classes/kernel/kernelTruncation.h"
#include "classes/kernel/kernel.h"

/***********************************************/

GROOPS_REGISTER_CLASS(Kernel, "kernelType",
                      KernelGeoid,
                      KernelStokes,
                      KernelHotine,
                      KernelPoisson,
                      KernelSingleLayer,
                      KernelWaterHeight,
                      KernelBottomPressure,
                      KernelDeformation,
                      KernelRadialGradient,
                      KernelCoefficients,
                      KernelFilterGauss,
                      KernelBlackmanLowPass,
                      KernelTruncation,
                      KernelSelenoid)

GROOPS_READCONFIG_CLASS(Kernel, "kernelType")

/***********************************************/

KernelPtr Kernel::create(Config &config, const std::string &name)
{
  try
  {
    KernelPtr   kernel;
    std::string type;
    readConfigChoice(config, name, type, Config::MUSTSET, "", "harmonic and isotropic integral kernels");

    if(readConfigChoiceElement(config, "geoidHeight",    type, "geoid = potential/normalgravity"))
      kernel = KernelPtr(new KernelGeoid(config));
    if(readConfigChoiceElement(config, "anomalies",      type, "gravity anomalies, Stokes kernel"))
      kernel = KernelPtr(new KernelStokes(config));
    if(readConfigChoiceElement(config, "disturbance",    type, "gravity disturbance, Hotine kernel"))
      kernel = KernelPtr(new KernelHotine(config));
    if(readConfigChoiceElement(config, "potential",      type, "Abel-Poisson kernel"))
      kernel = KernelPtr(new KernelPoisson(config));
    if(readConfigChoiceElement(config, "density",        type, "mass on a single layer (1/l kernel)"))
      kernel = KernelPtr(new KernelSingleLayer(config));
    if(readConfigChoiceElement(config, "waterHeight",    type, "equivalent water columns, accounts the loading"))
      kernel = KernelPtr(new KernelWaterHeight(config));
    if(readConfigChoiceElement(config, "bottomPressure", type, "ocean bottom pressure in Pascal, accounts for loading"))
      kernel = KernelPtr(new KernelBottomPressure(config));
    if(readConfigChoiceElement(config, "deformation",    type, "radial deformation by loading"))
      kernel = KernelPtr(new KernelDeformation(config));
    if(readConfigChoiceElement(config, "radialGradient", type, "radial gravity gradient"))
      kernel = KernelPtr(new KernelRadialGradient(config));
    if(readConfigChoiceElement(config, "coefficients",   type, "construct kernel by a legendre-polynomial expansion"))
      kernel = KernelPtr(new KernelCoefficients(config));
    if(readConfigChoiceElement(config, "filterGauss",    type, "smoothing by a gauss filter"))
      kernel = KernelPtr(new KernelFilterGauss(config));
    if(readConfigChoiceElement(config, "blackmanLowPass",type, "Blackman low-pass filter"))
      kernel = KernelPtr(new KernelBlackmanLowPass(config));
    if(readConfigChoiceElement(config, "truncation",     type, "truncate kernel at specific degree"))
      kernel = KernelPtr(new KernelTruncation(config));
    if(readConfigChoiceElement(config, "selenoidHeight", type, "selenoid = potential/normalgravity"))
      kernel = KernelPtr(new KernelSelenoid(config));
    endChoice(config);

    return kernel;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

Double Kernel::kernel(Vector3d const &p, Vector3d const &q) const
{
  try
  {
    return kernel(p, q, coefficients(p, maxDegree()));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

Double Kernel::radialDerivative(Vector3d const &p, Vector3d const &q) const
{
  try
  {
    return radialDerivative(p, q, coefficients(p, maxDegree()));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

Vector3d Kernel::gradient(Vector3d const &p, Vector3d const &q) const
{
  try
  {
    return gradient(p, q, coefficients(p, maxDegree()));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

Tensor3d Kernel::gradientGradient(Vector3d const &p, Vector3d const &q) const
{
  try
  {
    return gradientGradient(p, q, coefficients(p, maxDegree()));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

Double Kernel::inverseKernel(Vector3d const &p, Vector3d const &q, const Kernel &kernel2) const
{
  try
  {
    const Double r = p.r();
    const Double R = q.r();
    const Double t = inner(p, q)/r/R; // t = cos(psi)

    const Vector k2     = kernel2.coefficients(p, kernel2.maxDegree());
    const Vector k1     = inverseCoefficients (p, k2.size()-1);
    const UInt   degree = std::min(k1.rows(), k2.rows())-1;
    Double       *p1 = k1.field();
    const Double *p2 = k2.field();
    Double  f1 = R/r;
    const Double  f2 = R/r;

    for(UInt n=0; n<=degree; n++)
    {
      // k1(n) *= (R/r)^(n+1) * sqrt(2n+1) * k2(n));
      *p1++ *= f1 * sqrt(2*n+1.0) * *p2++;
      f1  *= f2;
    }

    return LegendrePolynomial::sum(t, k1, degree);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

Double Kernel::inverseKernel(const Time &time, const Vector3d &p, const GravityfieldBase &field) const
{
  try
  {
    SphericalHarmonics harmonics = field.sphericalHarmonics(time, maxDegree());
    Vector kn = inverseCoefficients(p, harmonics.maxDegree(), harmonics.isInterior());
    // Convolution with the kernel
    return inner(kn, harmonics.Yn(p));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/
/***********************************************/

Double Kernel::kernel(Vector3d const &p, Vector3d const &q, const Vector &kn) const
{
  const Double r = p.r();
  const Double R = q.r();
  const Double t = inner(p, q)/r/R; // t = cos(psi)
  // Factors: radial = sqrt(2n+1)*(R/r)^(n+1) * k_n
  const Vector radial = computeFactors(r, R, kn);
  // K = sum_n sqrt(2n+1)*(R/r)^(n+1) * k_n * P_n(t)
  return LegendrePolynomial::sum(t, radial, kn.size()-1);
}

/***********************************************/

Double Kernel::radialDerivative(Vector3d const &p, Vector3d const &q, const Vector &kn) const
{
  const Double r = p.r();
  const Double R = q.r();
  const Double t = inner(p, q)/r/R; // t = cos(psi)

  // radial_n = -(n+1)/r*(R/r)^(n+1) * sqrt(2n+1) * k_n
  const Vector radial = computeFactorsRadialDerivative(r, R, kn);
  // K = sum_n -(n+1)/r*(R/r)^(n+1) * sqrt(2n+1) * k_n * P_n(t)
  return LegendrePolynomial::sum(t, radial, kn.size()-1);
}

/***********************************************/

Vector3d Kernel::gradient(Vector3d const &p, Vector3d const &q, const Vector &kn) const
{
  const Double r  = p.r();
  const Double r2 = r*r;
  const Double R  = q.r();
  const Double t  = inner(p, q)/r/R; // t = cos(psi)
  const UInt   degree = kn.size()-1;

  const Vector radial           = computeFactors(r, R, kn);
  const Vector radialDerivative = computeFactorsRadialDerivative(r, R, kn);

  // derivatives of r with respect to x,y,z
  const Double dr_dx = p.x()/r;
  const Double dr_dy = p.y()/r;
  const Double dr_dz = p.z()/r;

  // derivatives of the kernel with respect to t,r
  const Double dK_dr    = LegendrePolynomial::sum(t, radialDerivative, degree);
  const Double dK_dt    = LegendrePolynomial::sumDerivative(t, radial, degree);

  // derivatives of t with respect to x,y,z
  const Double dt_dx = q.x()/r/R-p.x()*t/r2;
  const Double dt_dy = q.y()/r/R-p.y()*t/r2;
  const Double dt_dz = q.z()/r/R-p.z()*t/r2;

  // chain rule
  return Vector3d(dK_dr*dr_dx + dK_dt*dt_dx,
                  dK_dr*dr_dy + dK_dt*dt_dy,
                  dK_dr*dr_dz + dK_dt*dt_dz);
}

/***********************************************/

Tensor3d Kernel::gradientGradient(Vector3d const &p, Vector3d const &q, const Vector &kn) const
{
  const Double r  = p.r();
  const Double r2 = r*r;
  const Double r3 = r2*r;
  const Double r4 = r3*r;
  const Double R  = q.r();
  const Double t  = inner(p, q)/r/R; // t = cos(psi)
  const UInt   degree = kn.size()-1;

  const Vector radial              = computeFactors                   (r, R, kn);
  const Vector radialDerivative    = computeFactorsRadialDerivative   (r, R, kn);
  const Vector radialDerivative2nd = computeFactorsRadialDerivative2nd(r, R, kn);

  // derivatives of r with respect to x,y,z
  const Double dr_dx = p.x()/r;
  const Double dr_dy = p.y()/r;
  const Double dr_dz = p.z()/r;

  // 2nd derivatives of r with respect to x,y,z
  const Double d2r_dx2 = 1/r-p.x()*p.x()/r3;
  const Double d2r_dy2 = 1/r-p.y()*p.y()/r3;
  const Double d2r_dz2 = 1/r-p.z()*p.z()/r3;
  const Double d2r_dxdy =   -p.x()*p.y()/r3;
  const Double d2r_dxdz =   -p.x()*p.z()/r3;
  const Double d2r_dydz =   -p.y()*p.z()/r3;

  // derivatives of t with respect to x,y,z
  const Double dt_dx = q.x()/(r*R)-p.x()*t/r2;
  const Double dt_dy = q.y()/(r*R)-p.y()*t/r2;
  const Double dt_dz = q.z()/(r*R)-p.z()*t/r2;

  // 2nd derivatives of t with respect to x,y,z
  const Double d2t_dx2  = -t/r2 - 2*q.x()*p.x()/r3/R + 3*p.x()*p.x()*t/r4;
  const Double d2t_dy2  = -t/r2 - 2*q.y()*p.y()/r3/R + 3*p.y()*p.y()*t/r4;
  const Double d2t_dz2  = -t/r2 - 2*q.z()*p.z()/r3/R + 3*p.z()*p.z()*t/r4;
  const Double d2t_dxdy = -(q.x()*p.y()+q.y()*p.x())/r3/R+3*p.x()*p.y()*t/r4;
  const Double d2t_dxdz = -(q.x()*p.z()+q.z()*p.x())/r3/R+3*p.x()*p.z()*t/r4;
  const Double d2t_dydz = -(q.y()*p.z()+q.z()*p.y())/r3/R+3*p.y()*p.z()*t/r4;

  // derivatives of the kernel with respect to t,r
  // K = sum B_n*(R/r)^n+1 * P_n
  const Double dK_dt    = LegendrePolynomial::sumDerivative(t, radial, degree);
  const Double d2K_drdt = LegendrePolynomial::sumDerivative(t, radialDerivative, degree);
  const Double dK_dr    = LegendrePolynomial::sum          (t, radialDerivative, degree);
  const Double d2K_dr2  = LegendrePolynomial::sum          (t, radialDerivative2nd, degree);
  const Double d2K_dt2  = LegendrePolynomial::sumDerivative2nd(t, radial, degree);

  // chain rule
  Tensor3d tns;
  tns.xx() = dK_dr*d2r_dx2  + dK_dt*d2t_dx2
           + d2K_dr2*dr_dx*dr_dx + 2*d2K_drdt*dr_dx*dt_dx + d2K_dt2*dt_dx*dt_dx;

  tns.xy() = dK_dr*d2r_dxdy + dK_dt*d2t_dxdy
           + d2K_dr2*dr_dx*dr_dy + d2K_drdt*dr_dx*dt_dy + d2K_drdt*dr_dy*dt_dx + d2K_dt2*dt_dx*dt_dy;

  tns.xz() = dK_dr*d2r_dxdz + dK_dt*d2t_dxdz
           + d2K_dr2*dr_dx*dr_dz + d2K_drdt*dr_dx*dt_dz + d2K_drdt*dr_dz*dt_dx + d2K_dt2*dt_dx*dt_dz;

  tns.yy() = dK_dr*d2r_dy2  + dK_dt*d2t_dy2
           + d2K_dr2*dr_dy*dr_dy + 2*d2K_drdt*dr_dy*dt_dy + d2K_dt2*dt_dy*dt_dy;

  tns.yz() = dK_dr*d2r_dydz + dK_dt*d2t_dydz
           + d2K_dr2*dr_dy*dr_dz + d2K_drdt*dr_dy*dt_dz + d2K_drdt*dr_dz*dt_dy + d2K_dt2*dt_dy*dt_dz;

  tns.zz() = dK_dr*d2r_dz2  + dK_dt*d2t_dz2
           + d2K_dr2*dr_dz*dr_dz + 2*d2K_drdt*dr_dz*dt_dz + d2K_dt2*dt_dz*dt_dz;

  return tns;
}

/***********************************************/
/***********************************************/

// computes sqrt(2n+1)*(R/r)^(n+1) * k_n
Vector Kernel::computeFactors(Double r, Double R, const Vector &kn) const
{
  Vector  radial(kn.size());
  Double  f1   = R/r;
  Double  f2   = R/r;
  Double *rptr = radial.field();
  const Double *kptr = kn.field();

  for(UInt n=0; n<kn.size(); n++)
  {
    *rptr++ = sqrt(2.*n+1.) * f1 * *kptr++;
    f1 *= f2;
  }
  return radial;
}

/***********************************************/

// computes dK/dr = -(n+1)/r*(R/r)^(n+1) * sqrt(2n+1) * k_n
Vector Kernel::computeFactorsRadialDerivative(Double r, Double R, const Vector &kn) const
{
  Vector  radial(kn.size());
  Double  f1   = R/(r*r);
  Double  f2   = R/r;
  Double *rptr = radial.field();
  const Double *kptr = kn.field();

  for(UInt n=0; n<kn.size(); n++)
  {
    *rptr++ = - sqrt(2.*n+1.) * (n+1.) * f1 * *kptr++;
    f1 *= f2;
  }
  return radial;
}

/***********************************************/

// computes d2K/dr2 = (n+1)*(n+2)/r^2*(R/r)^(n+1) * sqrt(2n+1) * k_n
Vector Kernel::computeFactorsRadialDerivative2nd(Double r, Double R, const Vector &kn) const
{
  Vector  radial(kn.size());
  Double  f1   = R/(r*r*r);
  Double  f2   = R/r;
  Double *rptr = radial.field();
  const Double *kptr = kn.field();

  for(UInt n=0; n<kn.size(); n++)
  {
    *rptr++ = sqrt(2.*n+1.) * (n+1.)*(n+2.) * f1 * *kptr++;
    f1 *= f2;
  }
  return radial;
}

/***********************************************/