12 #ifndef CD_LeastSquaresImplem_H
13 #define CD_LeastSquaresImplem_H
18 #include <CD_MultiIndex.H>
19 #include <CD_NamespaceHeader.H>
24 Vector<Real> ret(a_displacements.size(), 1.0);
27 for (
int i = 0; i < a_displacements.size(); i++) {
28 const RealVect& d = a_displacements[i];
29 const Real len = d.vectorLength();
30 const Real w = std::pow(len, -a_pow);
40 std::map<IntVect, std::pair<VoFStencil, VoFStencil>>
42 const IntVectSet& a_knownTerms,
43 const Vector<VolIndex>& a_fineVofs,
44 const Vector<VolIndex>& a_coarVofs,
45 const Vector<RealVect>& a_fineDisplacements,
46 const Vector<RealVect>& a_coarDisplacements,
53 return LeastSquares::computeDualLevelStencils<T>(a_derivs,
65 std::map<IntVect, std::pair<VoFStencil, VoFStencil>>
67 const IntVectSet& a_knownTerms,
68 const Vector<VolIndex>& a_fineVofs,
69 const Vector<VolIndex>& a_coarVofs,
70 const Vector<RealVect>& a_fineDisplacements,
71 const Vector<RealVect>& a_coarDisplacements,
72 const Vector<Real>& a_fineWeights,
73 const Vector<Real>& a_coarWeights,
88 CH_assert(a_order > 0);
89 CH_assert(a_fineVofs.size() == a_fineDisplacements.size());
90 CH_assert(a_coarVofs.size() == a_coarDisplacements.size());
91 CH_assert(a_fineVofs.size() == a_fineWeights.size());
92 CH_assert(a_coarVofs.size() == a_coarWeights.size());
95 std::map<IntVect, std::pair<VoFStencil, VoFStencil>> ret;
96 for (IVSIterator ivsIt(a_derivs); ivsIt.ok(); ++ivsIt) {
97 ret.emplace(ivsIt(), std::make_pair(VoFStencil(), VoFStencil()));
100 if (a_derivs.numPts() > 0) {
104 const int Kfine = a_fineDisplacements.size();
105 const int Kcoar = a_coarDisplacements.size();
106 const int K = Kfine + Kcoar;
108 const IntVectSet isect = a_derivs & a_knownTerms;
111 MayDay::Abort(
"LeastSquares::computeDualLevelStencils -- not enough equations to achieve desired order!");
113 if (!isect.isEmpty()) {
115 "LeastSquares::computeDualLevelStencils - you have specified the same terms as both unknown and known");
139 Vector<T> linA(K * M, 0.0);
140 Vector<T> linAplus(M * K, 0.0);
144 if (!a_knownTerms.contains(mi.getCurrentIndex())) {
147 for (
int k = 0; k < K; k++) {
149 linA[i] = a_fineWeights[k] * mi.pow(a_fineDisplacements[k]) / mi.factorial();
152 linA[i] = a_coarWeights[k - Kfine] * mi.pow(a_coarDisplacements[k - Kfine]) / mi.factorial();
175 std::map<IntVect, int> rowMap;
180 if (!a_knownTerms.contains(mi.getCurrentIndex())) {
181 rowMap.emplace(mi.getCurrentIndex(), row);
187 for (IVSIterator ivsIt(a_derivs); ivsIt.ok(); ++ivsIt) {
188 const IntVect deriv = ivsIt();
190 if (rowMap.find(ivsIt()) != rowMap.end()) {
191 row = rowMap.at(ivsIt());
194 MayDay::Error(
"LeastSquares::computeDualLevelStencils -- map is out of range but this shouldn't happen!");
197 std::pair<VoFStencil, VoFStencil>& sten = ret.at(deriv);
204 for (
int k = 0; k < K; k++) {
205 const int idx = row + k * M;
207 sten.first.add(a_fineVofs[k], a_fineWeights[k] * linAplus[idx]);
210 sten.second.add(a_coarVofs[k - Kfine], a_coarWeights[k - Kfine] * linAplus[idx]);
216 MayDay::Warning(
"LeastSquares::computeDualLevelStencils - could not perform singular value decomposition");
223 #include <CD_NamespaceFooter.H>
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:41
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
bool ok() const
Check that multi-index is ok.
Definition: CD_MultiIndex.cpp:93
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