chombo-discharge
Loading...
Searching...
No Matches
CD_LeastSquaresImplem.H
1/* chombo-discharge
2 * Copyright © 2021 SINTEF Energy Research.
3 * Please refer to Copyright.txt and LICENSE in the chombo-discharge root directory.
4 */
5
12#ifndef CD_LeastSquaresImplem_H
13#define CD_LeastSquaresImplem_H
14
15// Our includes
16#include <CD_LaPackUtils.H>
17#include <CD_LeastSquares.H>
18#include <CD_MultiIndex.H>
19#include <CD_NamespaceHeader.H>
20
21inline Vector<Real>
23{
24 Vector<Real> ret(a_displacements.size(), 1.0);
25
26 if (a_pow > 0) {
27 // Figure out the min distance, and normalize every weight with this distance. We do not use physical
28 // distances when weighting the equations :)
29 Real normDist = 0.0;
30 for (int i = 0; i < a_displacements.size(); i++) {
31 normDist = std::max(normDist, a_displacements[i].vectorLength());
32 }
33
34 // Set weightings = 1/(1 + |displacement|/normDist)^a_pow
35 for (int i = 0; i < a_displacements.size(); i++) {
36 const Real len = a_displacements[i].vectorLength() / normDist;
37 const Real w = std::pow(1 + len, -a_pow);
38
39 ret[i] = w;
40 }
41 }
42
43 return ret;
44}
45
46template <typename T>
70
71template <typename T>
81 const int a_order)
82{
83 // TLDR: This routine does a two-level least squares solve of a system of equations, each equation describing
84 // a Taylor series expansion up to specified order (a_order). This routine "solves" this overdetermined system
85 // using weighted least squares so that we can obtain terms in the Taylor series. The user will have specified
86 // the desired unknowns (a_derivs) to be returned in stencil form as well as any terms that were already
87 // known (a_knownTerms).
88 //
89 // The least squares solve is done by LaPack, and so the least squares system matrix is filled in Fortran
90 // order. The actual "solve" consists of using the Moore-Penrose pseudoinverse and once we have inverted
91 // the system we can form stencils for each of the specified terms in the Taylor series.
92 //
93 // This routine does not contain much code, but can be difficult to understand.
94
95 CH_assert(a_order > 0);
98 CH_assert(a_fineVofs.size() == a_fineWeights.size());
99 CH_assert(a_coarVofs.size() == a_coarWeights.size());
100
101 // Initialize return stuff
103 for (IVSIterator ivsIt(a_derivs); ivsIt.ok(); ++ivsIt) {
104 ret.emplace(ivsIt(), std::make_pair(VoFStencil(), VoFStencil()));
105 }
106
107 if (a_derivs.numPts() > 0) {
108
109 // This is because some unknowns (rows) can be eliminated.
111 const int Kfine = a_fineDisplacements.size();
112 const int Kcoar = a_coarDisplacements.size();
113 const int K = Kfine + Kcoar;
114
116
117 if (K < M) {
118 MayDay::Abort("LeastSquares::computeDualLevelStencils -- not enough equations to achieve desired order!");
119 }
120 if (!isect.isEmpty()) {
122 "LeastSquares::computeDualLevelStencils - you have specified the same terms as both unknown and known");
123 }
124
125 // Build the A-matrix in column major order (this is what Fortran wants) so we can use LaPackUtils::computePseudoInverse.
126 // ----------------------------------------------------------------------------------------------------------------------
127 // If we have an (unweighted) full system then our system A*x = b is
128 //
129 // A x b
130 // === === ===
131 // [1 (x-x0) 0.5(x-x0)^2 ...] [f(x) ] [f(x0)]
132 // [1 (x-x1) 0.5(x-x1)^2 ...] [df/dx ] [f(x1)]
133 // [1 (x-x2) 0.5(x-x2)^2 ...] [d^2f/dx^2](x) = [f(x2)]
134 // [: : : ] [ : ] [ : ]
135 // [: : : ] [ : ] [ : ]
136 //
137 // Extensions to 2D/3D simply use multi-index notation, and weights are simply multiplied into each row, i.e. we
138 // solve (w*A) * x = (w*b). Inverting the system gives x = [w * A^+ * w] * b where A^+ is the Moore-Penrose
139 // pseudoinverse. We put the result of [w * A^+ * w] into a stencil.
140 //
141 // Note that columns can be eliminated through knownTerms, in which case we remove unknowns (i.e., rows) from the system.
142 // This will also correspond to a modification of the right-hand side, but the required modifications are not accesible
143 // in this routine, and so the user will have to make sense of them.
144
145 int i = 0; // Exists just because we fill memory linearly.
146 Vector<T> linA(K * M, 0.0); // Equal to (w*A)
147 Vector<T> linAplus(M * K, 0.0); // Equal to (w*A)^+
148
149 // Loop over column
150 for (MultiIndex mi(a_order); mi.ok(); ++mi) {
151 if (!a_knownTerms.contains(mi.getCurrentIndex())) {
152
153 // Fill column, write the system using the fine vofs first, then the coarse vofs.
154 for (int k = 0; k < K; k++) {
155 if (k < Kfine) {
156 linA[i] = a_fineWeights[k] * mi.pow(a_fineDisplacements[k]) / mi.factorial();
157 }
158 else {
159 linA[i] = a_coarWeights[k - Kfine] * mi.pow(a_coarDisplacements[k - Kfine]) / mi.factorial();
160 }
161 i++;
162 }
163 }
164 }
165
166 // Compute the pseudo-inverse.
167 const bool foundSVD = LaPackUtils::computePseudoInverse(linAplus.stdVector(), linA.stdVector(), K, M);
168
169 if (foundSVD) {
170 // When we have eliminated rows in the linear system we can't use MultiIndex to map directly, this occurs because
171 // if we eliminated unknowns (rows) our system can be something like (if we eliminated term f(x)):
172 //
173 // [(x-x0) 0.5(x-x0)^2 ...] [df/dx ] [f(x0) - f(x)]
174 // [(x-x1) 0.5(x-x1)^2 ...] [d^2f/dx^2] [f(x1) - f(x)]
175 // [(x-x2) 0.5(x-x2)^2 ...] [ ](x) = [f(x2) - f(x)]
176 // [ : : ] [ : ] [ : ]
177 // [ : : ] [ : ] [ : ]
178 //
179 // The MultiIndex won't know about this, and will (correctly!), believe that the first row corresponds to
180 // multi-index (0,0,0). Since that is truly not the case, we map the rows in A to multi-indices and use that to
181 // identify the row in (w*A)^+ that corresponds to a specific unknown in the Taylor series. This is what happens below.
183 int row = 0;
184 for (MultiIndex mi(a_order); mi.ok(); ++mi) {
185
186 // This is the order in which A was built.
187 if (!a_knownTerms.contains(mi.getCurrentIndex())) {
188 rowMap.emplace(mi.getCurrentIndex(), row);
189 row++;
190 }
191 }
192
193 // Recall that linAplus is M*K so the stride is always M, starting at some specified row.
194 for (IVSIterator ivsIt(a_derivs); ivsIt.ok(); ++ivsIt) {
195 const IntVect deriv = ivsIt();
196
197 if (rowMap.find(ivsIt()) != rowMap.end()) {
198 row = rowMap.at(ivsIt());
199 }
200 else {
201 MayDay::Error("LeastSquares::computeDualLevelStencils -- map is out of range but this shouldn't happen!");
202 }
203
205
206 sten.first.clear();
207 sten.second.clear();
208
209 // Map the pseudoinverse into something that is usable by a stencil. Note that linAplus is (w*A)^+, but we want
210 // the term [(w*A)^+ * w], so we also need to multiply in the weights here (because the right-hand side was also weighted).
211 for (int k = 0; k < K; k++) {
212 const int idx = row + k * M;
213 if (k < Kfine) {
214 sten.first.add(a_fineVofs[k], a_fineWeights[k] * linAplus[idx]);
215 }
216 else {
217 sten.second.add(a_coarVofs[k - Kfine], a_coarWeights[k - Kfine] * linAplus[idx]);
218 }
219 }
220 }
221 }
222 else {
223 MayDay::Warning("LeastSquares::computeDualLevelStencils - could not perform singular value decomposition");
224 }
225 }
226
227 return ret;
228}
229
230#include <CD_NamespaceFooter.H>
231
232#endif
Interface to some LaPack routines.
Agglomeration of various routines that use least squares computations.
static std::map< IntVect, std::pair< VoFStencil, VoFStencil > > computeDualLevelStencils(const IntVectSet &a_derivs, const IntVectSet &a_knownTerms, const Vector< VolIndex > &a_fineVofs, const Vector< VolIndex > &a_coarVofs, const Vector< RealVect > &a_fineDisplacements, const Vector< RealVect > &a_coarDisplacements, const int a_p, const int a_order)
Compute a least squares interpolation to a specified order. This version separates the stencils into ...
Definition CD_LeastSquaresImplem.H:48
static int getTaylorExpansionSize(const int a_order)
Get the size of a Taylor expansion for a given order.
Definition CD_LeastSquares.cpp:416
static Vector< Real > makeDiagWeights(const Vector< RealVect > &a_displacements, const int a_pow)
Create a list of weights. This routine returns a list of diagonal weights for a least squares system....
Definition CD_LeastSquaresImplem.H:22
SpaceDim multi-index type for use with higher order Taylor series.
Definition CD_MultiIndex.H:30
Base class for a tracer particle solver. This solver can advance particles in a pre-defined velocity ...
Definition CD_TracerParticleSolver.H:37
TracerParticleSolver()
Default constructor.
Definition CD_TracerParticleSolverImplem.H:25
bool computePseudoInverse(std::vector< double > &a_linAplus, const std::vector< double > &a_linA, const int &a_M, const int &a_N)
Compute the pseudoinverse of matrix through singular value decomposition.
Definition CD_LaPackUtils.cpp:163