File: normalsAccumulate.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 (149 lines) | stat: -rw-r--r-- 5,013 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
/***********************************************/
/**
* @file normalsAccumulate.cpp
*
* @brief Accumulate normal equations and write it to file.
*
* @author Andreas Kvas
* @date 2013-10-26
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
This program accumulates normal equations and writes the total combined system to
\configFile{outputfileNormalequation}{normalEquation}.
The \configFile{inputfileNormalEquation}{normalEquation}s must have all the same size and the same block structure.
This program is the simplified and fast version of the more general program \program{NormalsBuild}.
)";

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

#include "programs/program.h"
#include "files/fileMatrix.h"
#include "files/fileNormalEquation.h"

/***** CLASS ***********************************/

/** @brief Accumulate normal equations and write it to file.
* @ingroup programsGroup */
class NormalsAccumulate
{
public:
  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(NormalsAccumulate, SINGLEPROCESS, "accumulate normal equations and write to file", NormalEquation)

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

void NormalsAccumulate::run(Config &config, Parallel::CommunicatorPtr /*comm*/)
{
  try
  {
    FileName fileNameOut;
    std::vector<FileName> fileNameInAll;

    renameDeprecatedConfig(config, "outputfileNormalequation", "outputfileNormalEquation", date2time(2020, 6, 3));
    renameDeprecatedConfig(config, "inputfileNormalequation",  "inputfileNormalEquation",  date2time(2020, 6, 3));

    readConfig(config, "outputfileNormalEquation", fileNameOut,   Config::MUSTSET,  "", "");
    readConfig(config, "inputfileNormalEquation",  fileNameInAll, Config::MUSTSET,  "", "");
    if(isCreateSchema(config)) return;

    // ==================================

    logStatus<<"read normal equations info"<<Log::endl;
    Matrix nOut;
    NormalEquationInfo infoOut;
    std::vector<FileName> fileNameIn;
    std::vector<Matrix>   usedBlocksIn;
    for(UInt i=0; i<fileNameInAll.size(); i++)
    {
      Matrix nIn;
      NormalEquationInfo infoIn;
      try
      {
        readFileNormalEquation(fileNameInAll.at(i), infoIn, nIn);
      }
      catch(std::exception &e)
      {
        logWarning<<e.what()<<" continue..."<<Log::endl;
        continue;
      }

      fileNameIn.push_back(fileNameInAll.at(i));
      usedBlocksIn.push_back(infoIn.usedBlocks);
      if(!infoOut.blockIndex.size())
      {
        infoOut = infoIn;
        nOut    = nIn;
        continue;
      }

      if(infoOut.blockIndex.size() != infoIn.blockIndex.size())
        throw(Exception("normals must have the same size and block structure"));
      for(UInt z=0; z<infoOut.blockIndex.size(); z++)
        if(infoOut.blockIndex.at(z) != infoIn.blockIndex.at(z))
          throw(Exception("normals must have the same size and block structure"));
      for(UInt z=0; z<infoOut.blockIndex.size()-1; z++)
        for(UInt s=z; s<infoOut.blockIndex.size()-1; s++)
          if(infoIn.usedBlocks(z,s))
            infoOut.usedBlocks(z,s) = 1;
      for(UInt i=0; i<infoOut.parameterName.size(); i++)
        if(!infoOut.parameterName.at(i).combine(infoIn.parameterName.at(i)))
          logWarning << "Parameter names do not match at index " << i << ": '" << infoOut.parameterName.at(i).str() << "' != '" << infoIn.parameterName.at(i).str() << "'" << Log::endl;

      nOut += nIn;
      infoOut.lPl              += infoIn.lPl;
      infoOut.observationCount += infoIn.observationCount;
    }

    // ==================================

    logStatus<<"read and write block normals"<<Log::endl;
    Log::Timer timer(infoOut.blockIndex.size()*(infoOut.blockIndex.size()-1)/2);
    UInt idx = 0;
    for(UInt z=0; z<infoOut.blockIndex.size()-1; z++)
      for(UInt s=z; s<infoOut.blockIndex.size()-1; s++)
      {
        timer.loopStep(idx++);

        if(infoOut.usedBlocks(z,s) == 0)
          continue;

        std::string ext;
        if(infoOut.blockIndex.size()-1 > 1)
          ext = "."+z%"%02i-"s+s%"%02i"s;

        Matrix N;
        for(UInt i=0; i<fileNameIn.size(); i++)
          if(usedBlocksIn.at(i)(z,s))
          {
            logStatus<<"read matrix <"<<fileNameIn.at(i).appendBaseName(ext)<<">"<<Log::endl;
            Matrix N2;
            readFileMatrix(fileNameIn.at(i).appendBaseName(ext), N2);
            if(N.size())
              N += N2;
            else
              N = N2;
          }

        logStatus<<"write matrix <"<<fileNameOut.appendBaseName(ext)<<">"<<Log::endl;
        writeFileMatrix(fileNameOut.appendBaseName(ext), N);
      }
    timer.loopEnd();

    // ==================================

    logStatus<<"write normal equations to <"<<fileNameOut<<">"<<Log::endl;
    writeFileNormalEquation(fileNameOut, infoOut, nOut);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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