File: covariancePod.cpp

package info (click to toggle)
groops 0%2Bgit20240830%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 11,052 kB
  • sloc: cpp: 134,939; fortran: 1,569; makefile: 20
file content (301 lines) | stat: -rw-r--r-- 9,969 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
/***********************************************/
/**
* @file covariancePod.cpp
*
* @brief Covariance matrix of kinematic orbits.
*
* @author Torsten Mayer-Guerr
* @date 2010-07-18
*
*/
/***********************************************/

#define DOCSTRING_CovariancePod

#include "base/import.h"
#include "config/configRegister.h"
#include "files/fileMatrix.h"
#include "files/fileInstrument.h"
#include "covariancePod.h"

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

GROOPS_REGISTER_CLASS_WITHOUT_SUBS(CovariancePod, "covariancePodType")
GROOPS_READCONFIG_CLASS(CovariancePod, "covariancePodType")

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

CovariancePod::CovariancePod(Config &config, const std::string &name)
{
  try
  {
    FileName fileNameSigmaArc, fileNameSigmaEpoch, fileNameCovPodEpoch, fileNameCovFunc;

    readConfigSequence(config, name, Config::MUSTSET, "", "");
    readConfig(config, "sigma",                        sigma,               Config::DEFAULT,  "1", "general variance factor");
    readConfig(config, "inputfileSigmasPerArc",        fileNameSigmaArc,    Config::OPTIONAL, "",  "different accuaries for each arc (multplicated with sigma)");
    readConfig(config, "inputfileSigmasPerEpoch",      fileNameSigmaEpoch,  Config::OPTIONAL, "",  "different accuaries for each epoch (added)");
    readConfig(config, "inputfileCovarianceFunction",  fileNameCovFunc,     Config::OPTIONAL, "",  "covariances in time for along, cross, and radial direction");
    readConfig(config, "inputfileCovariancePodEpoch",  fileNameCovPodEpoch, Config::OPTIONAL, "",  "3x3 epoch wise covariances");
    endSequence(config);
    if(isCreateSchema(config)) return;

    if(!fileNameSigmaArc.empty())
      readFileMatrix(fileNameSigmaArc, sigmaArc);

    if(!fileNameSigmaEpoch.empty())
      fileSigmaEpoch.open(fileNameSigmaEpoch);

    if(!fileNameCovPodEpoch.empty())
      fileCovPodEpoch.open(fileNameCovPodEpoch);

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

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

void CovariancePod::testInput(const OrbitArc &pod, const ObservationSigmaArc &sigmaEpoch, const Covariance3dArc &covPod, const_MatrixSliceRef covFunction)
{
  if(sigmaEpoch.size() && (sigmaEpoch.size() != pod.size()))
    throw(Exception("sigma per epoch not compatible with this arc number"));
  if((covPod.size() != 0) && (covPod.size() != pod.size()))
    throw(Exception("orbit and CovariancePodEpoch are not compatible"));
  if(covFunction.size() && (covFunction.rows()<pod.size()))
    throw(Exception("covariance function to short for this arc"));
}

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

Matrix CovariancePod::covariance(UInt arcNo, const OrbitArc &pod)
{
  try
  {
    ObservationSigmaArc sigmaEpoch = fileSigmaEpoch.readArc(arcNo);
    Covariance3dArc     covPod     = fileCovPodEpoch.readArc(arcNo);
    testInput(pod, sigmaEpoch, covPod, covFunction);
    if(sigmaArc.size() && (arcNo>=sigmaArc.size()))
      throw(Exception("sigmasPerArc contain not enough rows for this arc number"));

    Double sigma2 = pow(this->sigma, 2);
    if(sigmaArc.size())
      sigma2 *= pow(this->sigmaArc(arcNo), 2);

    Matrix C(3*pod.size(), Matrix::TRIANGULAR);
    for(UInt i=0; i<sigmaEpoch.size(); i++)
      for(UInt k=0; k<3; k++)
        C(3*i+k,3*i+k) += pow(sigmaEpoch.at(i).sigma, 2);

    // special case 1: diagonal matrix
    // -------------------------------
    if((covPod.size()==0) && (covFunction.size()==0))
    {
      for(UInt i=0; i<C.rows(); i++)
        C(i,i) += sigma2;
      return C;
    }

    // special case 2: epoch block diagonal matrix
    // -------------------------------------------
    if(covPod.size() && (covFunction.size()==0))
    {
      for(UInt i=0; i<pod.size(); i++)
      {
        Matrix C3x3 = covPod.at(i).covariance.matrix();
        axpy(sigma2, C3x3, C.slice(3*i,3*i,3,3));
      }
      return C;
    }

    // covariance function in orbit system
    // -----------------------------------
    const Double sampling = covFunction(1,0)-covFunction(0,0);
    for(UInt z=0; z<pod.size(); z++)
      for(UInt s=z; s<pod.size(); s++)
      {
        UInt idx = static_cast<UInt>(round((pod.at(s).time-pod.at(z).time).seconds()/sampling));
        C(3*z+0, 3*s+0) += sigma2 * covFunction(idx, 1+0);
        C(3*z+1, 3*s+1) += sigma2 * covFunction(idx, 1+1);
        C(3*z+2, 3*s+2) += sigma2 * covFunction(idx, 1+2);
      }
    fillSymmetric(C);

    // rotate and decorrelate epoch wise residuals
    // -------------------------------------------
    for(UInt i=0; i<pod.size(); i++)
    {
      // orbit system: z: radial, x: along, y: cross
      Vector3d x;
      if(i==0)
        x = pod.at(i+1).position - pod.at(i).position;
      else
        x = pod.at(i).position - pod.at(i-1).position;
      Vector3d z = normalize(pod.at(i).position);
      Vector3d y = normalize(crossProduct(z, x));
      x = crossProduct(y, z);
      Matrix D = Rotary3d(x,y).matrix().trans(); // Rot from CRF to SRF

      // epoch wise decorrelation
      if(covPod.size()!=0)
      {
        Matrix V = covPod.at(i).covariance.matrix();    // 3x3 Epoch covariance
        V.setType(Matrix::SYMMETRIC, Matrix::UPPER);
        Vector eigen = eigenValueDecomposition(V);
        Matrix V1 = V;
        V1.column(0) *= std::sqrt(eigen(0));
        V1.column(1) *= std::sqrt(eigen(1));
        V1.column(2) *= std::sqrt(eigen(2));
        D = D*V1*V.trans();           // W^-T im LORF
      }

      copy(D.trans() * C.row(3*i,3), C.row(3*i,3));
      copy(C.column(3*i,3) * D,      C.column(3*i,3));
    }

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

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

void CovariancePod::decorrelate(UInt arcNo, const OrbitArc &pod, const std::list<MatrixSlice> &A)
{
  try
  {
    ObservationSigmaArc sigmaEpoch = fileSigmaEpoch.readArc(arcNo);
    Covariance3dArc     covPod     = fileCovPodEpoch.readArc(arcNo);
    testInput(pod, sigmaEpoch, covPod, covFunction);
    if(sigmaArc.size() && (arcNo>=sigmaArc.size()))
      throw(Exception("sigmasPerArc contain not enough rows for this arc number"));

    Double weight = 1./this->sigma;
    if(sigmaArc.size() != 0)
      weight *= 1./this->sigmaArc(arcNo);

    // special case 1: diagonal matrix
    // -------------------------------
    if((covPod.size()==0) && (sigmaEpoch.size()==0) && (covFunction.size()==0))
    {
      for(MatrixSliceRef WA : A)
        if(WA.size()) WA *= weight;
      return;
    }

    // special case 2: epoch block diagonal matrix
    // -------------------------------------------
    if(covPod.size() && (covFunction.size()==0))
    {
      for(UInt i=0; i<pod.size(); i++)
      {
        Matrix W = covPod.at(i).covariance.matrix();
        W.setType(Matrix::SYMMETRIC);

        if(sigmaEpoch.size())
        {
          W(0,0) += sigmaEpoch.at(i).sigma;
          W(1,1) += sigmaEpoch.at(i).sigma;
          W(2,2) += sigmaEpoch.at(i).sigma;
        }

        cholesky(W);
        for(MatrixSliceRef WA : A)
          if(WA.size())
            triangularSolve(weight, W.trans(), WA.row(3*i,3));
      }
      return;
    }

    decorrelate(pod, 1/weight, sigmaEpoch, covPod, covFunction, A);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

Matrix CovariancePod::decorrelate(const OrbitArc &pod, Double sigmaArc, const ObservationSigmaArc &sigmaEpoch,
                                  const Covariance3dArc &covPod, const_MatrixSliceRef covFunction,
                                  const std::list<MatrixSlice> &A)
{
  try
  {
    testInput(pod, sigmaEpoch, covPod, covFunction);

    // rotate and decorrelate epoch wise residuals
    // -------------------------------------------
    for(UInt i=0; i<pod.size(); i++)
    {
      // orbit system: z: radial, x: along, y: cross
      Vector3d x;
      if(i==0)
        x = pod.at(i+1).position - pod.at(i).position;
      else
        x = pod.at(i).position - pod.at(i-1).position;
      Vector3d z = normalize(pod.at(i).position);
      Vector3d y = normalize(crossProduct(z, x));
      x = crossProduct(y, z);
      Matrix D = Rotary3d(x,y).matrix().trans(); // Rot from CRF to SRF

      // epoch wise decorrelation
      if(covPod.size()!=0)
      {
        Matrix V = covPod.at(i).covariance.matrix(); // 3x3 Epoch covariance
        V.setType(Matrix::SYMMETRIC, Matrix::UPPER);
        Vector eigen = eigenValueDecomposition(V);
        Matrix V1 = V;
        V1.column(0) *= 1./sqrt(eigen(0));
        V1.column(1) *= 1./sqrt(eigen(1));
        V1.column(2) *= 1./sqrt(eigen(2));
        D = D*V1*V.trans();           // W^-T im LORF
      }

      for(MatrixSliceRef WA : A)
        if(WA.size())
          copy(D * WA.row(3*i,3), WA.row(3*i,3));
    }

    // covariance function in orbit system
    // -----------------------------------
    Matrix W(3*pod.size(), Matrix::SYMMETRIC, Matrix::UPPER);
    if(covFunction.size())
    {
      const Double sampling = covFunction(1,0)-covFunction(0,0);
      for(UInt z=0; z<pod.size(); z++)
        for(UInt s=z; s<pod.size(); s++)
        {
          UInt idx = static_cast<UInt>(round((pod.at(s).time-pod.at(z).time).seconds()/sampling));
          W(3*z+0, 3*s+0) = sigmaArc*sigmaArc * covFunction(idx, 1+0);
          W(3*z+1, 3*s+1) = sigmaArc*sigmaArc * covFunction(idx, 1+1);
          W(3*z+2, 3*s+2) = sigmaArc*sigmaArc * covFunction(idx, 1+2);
        }
    }

    for(UInt i=0; i<sigmaEpoch.size(); i++)
      for(UInt k=0; k<3; k++)
        W(3*i+k,3*i+k) += pow(sigmaEpoch.at(i).sigma, 2);

    cholesky(W);
    for(MatrixSliceRef WA : A)
      if(WA.size())
        triangularSolve(1., W.trans(), WA);

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

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