File: kernelSingleLayer.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 (247 lines) | stat: -rw-r--r-- 6,200 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
/***********************************************/
/**
* @file kernelSingleLayer.cpp
*
* @brief Single layer density.
* @see Kernel
*
* @author Torsten Mayer-Guerr
* @date 2003-09-20
*
*/
/***********************************************/

#include "base/import.h"
#include "base/legendrePolynomial.h"
#include "files/fileMatrix.h"
#include "classes/gravityfield/gravityfield.h"
#include "classes/kernel/kernel.h"
#include "classes/kernel/kernelSingleLayer.h"

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

KernelSingleLayer::KernelSingleLayer(Config &config)
{
  try
  {
    FileName loveNumberName;

    readConfig(config, "inputfileLoadingLoveNumber", loveNumberName, Config::OPTIONAL,  "{groopsDataDir}/loading/loadLoveNumbers_Gegout97.txt", "");
    if(isCreateSchema(config)) return;

    if(!loveNumberName.empty())
      readFileMatrix(loveNumberName, kn);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

Vector KernelSingleLayer::coefficients(Vector3d const &q, UInt degree) const
{
  try
  {
    if((degree==INFINITYDEGREE) && (kn.rows()>0))
      degree = kn.rows()-1;
    if(degree==INFINITYDEGREE)
      throw(Exception("INFINITYDEGREE requested"));

    Vector  coeff(degree+1);
    Double *cp = coeff.field();
    Double  factor = 4*PI*GRAVITATIONALCONSTANT*q.r();
    for(UInt n=0; n<std::min(degree+1, kn.size()); n++)
      *cp++ = factor * (1.+kn(n)) / (2.*n+1.);
    for(UInt n=std::min(degree+1, kn.size()); n<=degree; n++)
      *cp++ = factor / (2.*n+1.);

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

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

Vector KernelSingleLayer::inverseCoefficients(Vector3d const &p, UInt degree, Bool interior) const
{
  try
  {
    if((degree==INFINITYDEGREE) && (kn.rows()>0))
      degree = kn.rows()-1;
    if(degree==INFINITYDEGREE)
      throw(Exception("INFINITYDEGREE requested"));

    if(interior)
      throw(Exception("interior not implemented"));

    Vector  coeff(degree+1);
    Double *cp = coeff.field();
    Double  factor = 1./(4.*PI*GRAVITATIONALCONSTANT*p.r());
    for(UInt n=0; n<std::min(degree+1, kn.size()); n++)
      *cp++ = factor * (2.*n+1.) / (1.+kn(n));
    for(UInt n=std::min(degree+1, kn.size()); n<=degree; n++)
      *cp++ = factor * (2.*n+1.);

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

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

Double KernelSingleLayer::kernel(Vector3d const &p, Vector3d const &q) const
{
  try
  {
    Double K = 4*PI*GRAVITATIONALCONSTANT/(p-q).r();

    // add love load numbers
    if(kn.size())
    {
      Vector  coeff(kn.rows());
      Double *cp = coeff.field();
      const Double factor = 4*PI*GRAVITATIONALCONSTANT/q.r();
      for(UInt n=0; n<kn.rows(); n++)
        *cp++ = factor*kn(n)/(2.*n+1.);
      K += Kernel::kernel(p, q, coeff);
    }

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

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

Double KernelSingleLayer::radialDerivative(Vector3d const &p, Vector3d const &q) const
{
  try
  {
    const Double r = p.r();
    const Double R = q.r();
    const Double l = (p-q).r();
    const Double cos_psi = (R*R+r*r-l*l)/(2*R*r);
    Double dKdr = -4*PI*GRAVITATIONALCONSTANT/(l*l*l) * (r-R*cos_psi);

    if(kn.rows())
    {
      Vector  coeff(kn.rows());
      Double *cp = coeff.field();
      const Double factor = 4*PI*GRAVITATIONALCONSTANT/q.r();
      for(UInt n=0; n<kn.rows(); n++)
        *cp++ = factor*kn(n)/(2.*n+1.);
      dKdr += Kernel::radialDerivative(p, q, coeff);
    }

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

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

Vector3d KernelSingleLayer::gradient(Vector3d const &p, Vector3d const &q) const
{
  try
  {
    const Double l = (p-q).r();
    Vector3d g = -4*PI*GRAVITATIONALCONSTANT/(l*l*l) * (p-q);

    if(kn.rows())
    {
      Vector  coeff(kn.rows());
      Double *cp = coeff.field();
      const Double factor = 4*PI*GRAVITATIONALCONSTANT/q.r();
      for(UInt n=0; n<kn.rows(); n++)
        *cp++ = factor*kn(n)/(2.*n+1.);
      g += Kernel::gradient(p, q, coeff);
    }

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

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

Double KernelSingleLayer::inverseKernel(Vector3d const &p, Vector3d const &q, const Kernel &kernel) const
{
  try
  {
    Double f = -1./(4.*PI*GRAVITATIONALCONSTANT) * (2*kernel.radialDerivative(p,q) + kernel.kernel(p,q)/p.r());

    if(kn.rows())
    {
      Double r  = p.r();
      Double R  = q.r();
      Double t  = inner(p, q)/r/R; // t = cos(psi)
      Vector k2 = kernel.coefficients(p, kn.rows()-1);
      Vector k1(kn.rows());
      Double f1 = R/r;
      Double f2 = R/r;
      Double factor = 1./(4.*PI*GRAVITATIONALCONSTANT*p.r());
      Double       *p1 = k1.field();
      const Double *p2 = k2.field();
      for(UInt n=0; n<kn.rows(); n++)
      {
        // k1(n) *= (R/r)^(n+1) * sqrt(2n+1) * k2(n));
        *p1++ = f1 * factor * (2.*n+1.) * (1/(1.+kn(n))-1) * sqrt(2.*n+1.) * *p2++;
        f1  *= f2;
      }
      f += LegendrePolynomial::sum(t, k1, kn.rows()-1);
    }

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

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

Double KernelSingleLayer::inverseKernel(const Time &time, const Vector3d &p, const GravityfieldBase &field) const
{
  try
  {
    Double f = -1./(4.*PI*GRAVITATIONALCONSTANT) * (2*field.radialGradient(time,p) + field.potential(time,p)/p.r());

    if(kn.rows())
    {
      Vector  coeff(kn.rows());
      Double *cp = coeff.field();
      Double  factor = 1./(4.*PI*GRAVITATIONALCONSTANT*p.r());
      for(UInt n=0; n<kn.rows(); n++)
        *cp++ = factor * (2.*n+1.) * (1/(1.+kn(n))-1);
      // Convolution with the kernel
      SphericalHarmonics harmonics = field.sphericalHarmonics(time, coeff.rows()-1);
      f += inner(coeff, harmonics.Yn(p));
    }

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

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