File: normalEquationDesignVCE.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 (198 lines) | stat: -rw-r--r-- 6,653 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
/***********************************************/
/**
* @file normalEquationDesignVCE.cpp
*
* @brief Accumulate normals from observation equations.
* With individual weights of each arc by variance component estimation.
* @f[ N = A^TPA,\quad n=A^TPl @f]
* @see NormalEquation
* @see Observation
*
* @author Torsten Mayer-Guerr
* @date 2004-12-10
*
*/
/***********************************************/

#include "base/import.h"
#include "config/config.h"
#include "parallel/parallel.h"
#include "files/fileArcList.h"
#include "classes/observation/observation.h"
#include "classes/normalEquation/normalEquation.h"
#include "classes/normalEquation/normalEquationDesignVCE.h"

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

NormalEquationDesignVCE::NormalEquationDesignVCE(Config &config)
{
  try
  {
    FileName fileNameArcList;

    renameDeprecatedConfig(config, "arcList", "inputfileArcList", date2time(2020, 7, 7));

    readConfig(config, "observation",      observation,     Config::MUSTSET,  "",  "");
    readConfig(config, "startIndex",       startIndex,      Config::DEFAULT,  "0", "add this normals at index of total matrix (counting from 0)");
    readConfig(config, "inputfileArcList", fileNameArcList, Config::OPTIONAL, "", "to accelerate computation");
    if(isCreateSchema(config)) return;

    intervals = {0, observation->arcCount()};
    if(!fileNameArcList.empty())
    {
      std::vector<Time> timesInterval;
      readFileArcList(fileNameArcList, intervals, timesInterval);
    }

    iter     = 0;
    sigma2.resize(observation->arcCount(), 1.0);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

void NormalEquationDesignVCE::parameterNames(std::vector<ParameterName> &names) const
{
  try
  {
    std::vector<ParameterName> baseNames;
    observation->parameterName(baseNames);

    for(UInt i=0; i<baseNames.size(); i++)
      if(!names.at(i+startIndex).combine(baseNames.at(i)))
        logWarningOnce<<"Parameter names do not match at index "<<i+startIndex<<": '"<<names.at(i+startIndex).str()<<"' != '"<<baseNames.at(i).str()<<"'"<< Log::endl;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

void NormalEquationDesignVCE::init(MatrixDistributed &/*normals*/, UInt rhsCount)
{
  try
  {
    if(rhsCount != observation->rightSideCount())
      throw(Exception("number of right hand sides must agree ("+rhsCount%"%i != "s+observation->rightSideCount()%"%i)"s));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

Bool NormalEquationDesignVCE::addNormalEquation(UInt rhsNo, const const_MatrixSlice &x, const const_MatrixSlice &Wz,
                                                MatrixDistributed &normals, Matrix &n, Vector &lPl, UInt &obsCount)
{
  try
  {
    if(!observation->parameterCount())
      return TRUE;

    const UInt blockStart = normals.index2block(startIndex);
    const UInt blockEnd   = normals.index2block(startIndex+observation->parameterCount()-1);
    for(UInt i=blockStart; i<=blockEnd; i++)
      for(UInt k=i; k<=blockEnd; k++)
      {
        normals.setBlock(i, k);
        normals.N(i,k) = (i==k) ? Matrix(normals.blockSize(i), Matrix::SYMMETRIC) : Matrix(normals.blockSize(i), normals.blockSize(k));
      }

    // compute observation equations
    // -----------------------------
    logStatus<<"accumulate normals from observation equations"<<Log::endl;
    Vector x0  = x.slice(startIndex, rhsNo, observation->parameterCount(), 1);
    Matrix Wz0 = Wz.row(startIndex, observation->parameterCount());

    Parallel::forEachInterval(sigma2, intervals, [&](UInt arcNo) -> Double
    {
      // observation equations
      Matrix l, A, B;
      observation->observation(arcNo, l, A, B);
      if(l.rows()==0)
        return 0;

      // if equations are orthogonal transformed
      // additional residuals appended to l
      Matrix l2;
      if(l.rows()>A.rows())
      {
        l2 = l.row(A.rows(), l.rows()-A.rows());
        l  = l.row(0, A.rows());
      }

      // eliminate arc related parameters
      if(B.size())
        eliminationParameter(B,A,l);

      // Partial redundancy
      // trace(A'A*N^(-1)) = trace(A'A*W^(-1)*W^(-T))
      //                   = z'*W^(-1)*A'*A*W^(-T)*z (MonteCarlo trace estimation)
      const Double r      = l.rows() + l2.rows() - quadsum(A*Wz0)/sigma2.at(arcNo);
      const Double sigma2 = (quadsum(l.column(rhsNo)-A*x0) + quadsum(l2.column(rhsNo)))/r;

      // right hand side
      // ---------------
      matMult(1./sigma2, A.trans(),l, n);
      for(UInt i=0; i<l.columns(); i++)
        lPl(i) += (quadsum(l.column(i))+quadsum(l2.column(i)))/sigma2;
      obsCount += l.rows() + l2.rows();

      // accumulate normals
      // ------------------
      for(UInt i=blockStart; i<=blockEnd; i++)
      {
        const UInt idxN1 = (normals.blockIndex(i) < startIndex) ? (startIndex-normals.blockIndex(i)) : 0;
        const UInt idxA1 = (normals.blockIndex(i) < startIndex) ? 0 : (normals.blockIndex(i)-startIndex);
        const UInt cols1 = std::min(normals.blockSize(i)-idxN1, A.columns()-idxA1);
        rankKUpdate(1/sigma2, A.column(idxA1, cols1), normals.N(i,i).slice(idxN1, idxN1, cols1, cols1));
        for(UInt k=i+1; k<=blockEnd; k++)
        {
          const UInt idxN2 = (normals.blockIndex(k) < startIndex) ? (startIndex-normals.blockIndex(k)) : 0;
          const UInt idxA2 = (normals.blockIndex(k) < startIndex) ? 0 : (normals.blockIndex(k)-startIndex);
          const UInt cols2 = std::min(normals.blockSize(k)-idxN1, A.columns()-idxA1);
          matMult(1/sigma2, A.column(idxA1, cols1).trans(), A.column(idxA2, cols2), normals.N(i,k).slice(idxN1, idxN2, cols1, cols2));
        }
      }

      return sigma2;
    }, normals.communicator());

    normals.reduceSum(FALSE);
    Parallel::reduceSum(n,        0, normals.communicator());
    Parallel::reduceSum(lPl,      0, normals.communicator());
    Parallel::reduceSum(obsCount, 0, normals.communicator());
    Parallel::broadCast(sigma2,   0, normals.communicator());

    return (++iter >= 3); // ready after 2 iterations
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

Vector NormalEquationDesignVCE::contribution(MatrixDistributed &Cov)
{
  try
  {
    logWarningOnce<<"In NormalEquationDesignVCE: contribution is not implemented"<<Log::endl;
    return Vector(Cov.dimension());
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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