Nektar++
GlobalLinSysDirectFull.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: GlobalLinSysDirectFull.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: GlobalLinSysDirectFull definition
32//
33///////////////////////////////////////////////////////////////////////////////
34
37
38using namespace std;
39
41{
42/**
43 * @class GlobalLinSysDirect
44 *
45 * Consider a linear system
46 * \f$\boldsymbol{M\hat{u}}_g=\boldsymbol{\hat{f}}\f$
47 * to be solved, where \f$\boldsymbol{M}\f$ is a matrix of type
48 * specified by \a mkey. This function assembles the global system
49 * matrix \f$\boldsymbol{M}\f$ out of the elemental submatrices
50 * \f$\boldsymbol{M}^e\f$. This is equivalent to:
51 * \f[ \boldsymbol{M}=\boldsymbol{\mathcal{A}}^T
52 * \underline{\boldsymbol{M}}^e\boldsymbol{\mathcal{A}}.\f]
53 * where the matrix \f$\boldsymbol{\mathcal{A}}\f$ is a sparse
54 * permutation matrix of size \f$N_{\mathrm{eof}}\times
55 * N_{\mathrm{dof}}\f$. However, due to the size and sparsity of the
56 * matrix \f$\boldsymbol{\mathcal{A}}\f$, it is more efficient to
57 * assemble the global matrix using the mapping array \a
58 * map\f$[e][i]\f$ contained in the input argument \a locToGloMap.
59 * The global assembly is then evaluated as:
60 * \f[ \boldsymbol{M}\left[\mathrm{\texttt{map}}[e][i]\right]
61 * \left[\mathrm{\texttt{map}}[e][j]\right]
62 * =\mathrm{\texttt{sign}}[e][i]\cdot
63 * \mathrm{\texttt{sign}}[e][j] \cdot\boldsymbol{M}^e[i][j]\f]
64 * where the values \a sign\f$[e][i]\f$ ensure the correct connectivity.
65 */
66
67/**
68 * Registers the class with the Factory.
69 */
72 "DirectFull", GlobalLinSysDirectFull::create, "Direct Full.");
73
74/// Constructor for full direct matrix solve.
76 const GlobalLinSysKey &pLinSysKey, const std::weak_ptr<ExpList> &pExp,
77 const std::shared_ptr<AssemblyMap> &pLocToGloMap)
78 : GlobalLinSys(pLinSysKey, pExp, pLocToGloMap),
79 GlobalLinSysDirect(pLinSysKey, pExp, pLocToGloMap)
80{
81
83 "This routine should only be used when using a Full Direct"
84 " matrix solve");
85 ASSERTL1(pExp.lock()->GetComm()->GetSize() == 1,
86 "Direct full matrix solve can only be used in serial.");
87
88 AssembleFullMatrix(pLocToGloMap);
89}
90
91/**
92 * Solve the linear system using a full global matrix system.
93 */
95 const Array<OneD, const NekDouble> &pLocInput,
96 Array<OneD, NekDouble> &pLocOutput,
97 const AssemblyMapSharedPtr &pLocToGloMap,
98 const Array<OneD, const NekDouble> &pDirForcing)
99{
100 bool dirForcCalculated = (bool)pDirForcing.size();
101 int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
102 int nGlobDofs = pLocToGloMap->GetNumGlobalCoeffs();
103 int nLocDofs = pLocToGloMap->GetNumLocalCoeffs();
104
105 if (nDirDofs)
106 {
107 std::shared_ptr<MultiRegions::ExpList> expList = m_expList.lock();
108 Array<OneD, NekDouble> rhs(nLocDofs);
109
110 // Calculate the Dirichlet forcing
111 if (dirForcCalculated)
112 {
113 // Assume pDirForcing is in local space
114 ASSERTL0(
115 pDirForcing.size() >= nLocDofs,
116 "DirForcing is not of sufficient size. Is it in local space?");
117 Vmath::Vsub(nLocDofs, pLocInput, 1, pDirForcing, 1, rhs, 1);
118 }
119 else
120 {
121 // Calculate initial condition and Dirichlet forcing and subtract it
122 // from the rhs
123 expList->GeneralMatrixOp(m_linSysKey, pLocOutput, rhs);
124
125 // Iterate over all the elements computing Robin BCs where
126 // necessary
127 for (auto &r : m_robinBCInfo) // add robin mass matrix
128 {
131
132 int n = r.first;
133 int offset = expList->GetCoeff_Offset(n);
134
135 LocalRegions::ExpansionSharedPtr vExp = expList->GetExp(n);
136 // Add local matrix contribution
137 for (rBC = r.second; rBC; rBC = rBC->next)
138 {
139 vExp->AddRobinTraceContribution(
140 rBC->m_robinID, rBC->m_robinPrimitiveCoeffs,
141 pLocOutput + offset, rhsloc = rhs + offset);
142 }
143 }
144 Vmath::Vsub(nLocDofs, pLocInput, 1, rhs, 1, rhs, 1);
145 }
146
147 Array<OneD, NekDouble> diff(nLocDofs);
148
149 // Solve for perturbation from initial guess in pOutput
150 SolveLinearSystem(nGlobDofs, rhs, diff, pLocToGloMap, nDirDofs);
151
152 // Add back initial and boundary condition
153 Vmath::Vadd(nLocDofs, diff, 1, pLocOutput, 1, pLocOutput, 1);
154 }
155 else
156 {
157 SolveLinearSystem(nGlobDofs, pLocInput, pLocOutput, pLocToGloMap,
158 nDirDofs);
159 }
160}
161
162/**
163 * Assemble a full matrix from the block matrix stored in
164 * #m_blkMatrices and the given local to global mapping information.
165 * @param locToGloMap Local to global mapping information.
166 */
168 const AssemblyMapSharedPtr &pLocToGloMap)
169{
170 int i, j, n, cnt, gid1, gid2;
171 NekDouble sign1, sign2, value;
172 int totDofs = pLocToGloMap->GetNumGlobalCoeffs();
173 int NumDirBCs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
174
175 unsigned int rows = totDofs - NumDirBCs;
176 unsigned int cols = totDofs - NumDirBCs;
177 NekDouble zero = 0.0;
178
179 DNekMatSharedPtr Gmat;
180 int bwidth = pLocToGloMap->GetFullSystemBandWidth();
181 MatrixStorage matStorage = eFULL;
182
183 switch (m_linSysKey.GetMatrixType())
184 {
185 // case for all symmetric matices
190 {
191 if ((2 * (bwidth + 1)) < rows)
192 {
195 rows, cols, zero, matStorage, bwidth, bwidth);
196 }
197 else
198 {
199 matStorage = ePOSITIVE_DEFINITE_SYMMETRIC;
201 rows, cols, zero, matStorage);
202 }
203 break;
204 }
207 {
208 matStorage = eFULL;
209 Gmat = MemoryManager<DNekMat>::AllocateSharedPtr(rows, cols, zero,
210 matStorage);
211 break;
212 }
213 default:
214 {
215 NEKERROR(ErrorUtil::efatal, "Add MatrixType to switch "
216 "statement");
217 }
218 }
219
220 // fill global matrix
221 DNekScalMatSharedPtr loc_mat;
222
223 int loc_lda;
224 for (n = cnt = 0; n < m_expList.lock()->GetNumElmts(); ++n)
225 {
226 loc_mat = GetBlock(n);
227 loc_lda = loc_mat->GetRows();
228
229 for (i = 0; i < loc_lda; ++i)
230 {
231 gid1 = pLocToGloMap->GetLocalToGlobalMap(cnt + i) - NumDirBCs;
232 sign1 = pLocToGloMap->GetLocalToGlobalSign(cnt + i);
233 if (gid1 >= 0)
234 {
235 for (j = 0; j < loc_lda; ++j)
236 {
237 gid2 =
238 pLocToGloMap->GetLocalToGlobalMap(cnt + j) - NumDirBCs;
239 sign2 = pLocToGloMap->GetLocalToGlobalSign(cnt + j);
240 if (gid2 >= 0)
241 {
242 // When global matrix is symmetric,
243 // only add the value for the upper
244 // triangular part in order to avoid
245 // entries to be entered twice
246 if ((matStorage == eFULL) || (gid2 >= gid1))
247 {
248 value = Gmat->GetValue(gid1, gid2) +
249 sign1 * sign2 * (*loc_mat)(i, j);
250 Gmat->SetValue(gid1, gid2, value);
251 }
252 }
253 }
254 }
255 }
256 cnt += loc_lda;
257 }
258
259 if (rows)
260 {
263 }
264}
265
266/// Solve the linear system for given input and output vectors.
268 const int pNumRows, const Array<OneD, const NekDouble> &pInput,
269 Array<OneD, NekDouble> &pOutput, const AssemblyMapSharedPtr &pLocToGloMap,
270 const int pNumDir)
271{
272 Array<OneD, NekDouble> tmp(pNumRows);
273 Array<OneD, NekDouble> global(pNumRows, 0.0);
274
275 pLocToGloMap->Assemble(pInput, tmp);
276
277 const int nHomDofs = pNumRows - pNumDir;
278 DNekVec Vin(nHomDofs, tmp + pNumDir);
279
280 Array<OneD, NekDouble> tmp1 = global + pNumDir;
281 DNekVec Vout(nHomDofs, tmp1, eWrapper);
282
283 m_linSys->Solve(Vin, Vout);
284
285 pLocToGloMap->GlobalToLocal(global, pOutput);
286}
287
288} // namespace Nektar::MultiRegions
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:202
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:242
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
void v_Solve(const Array< OneD, const NekDouble > &pLocInput, Array< OneD, NekDouble > &pLocalOutput, const AssemblyMapSharedPtr &locToGloMap, const Array< OneD, const NekDouble > &dirForcing=NullNekDouble1DArray) override
Solve the linear system for given input and output vectors using a specified local to global map.
GlobalLinSysDirectFull(const GlobalLinSysKey &pLinSysKey, const std::weak_ptr< ExpList > &pExpList, const std::shared_ptr< AssemblyMap > &pLocToGloMap)
Constructor for full direct matrix solve.
static GlobalLinSysSharedPtr create(const GlobalLinSysKey &pLinSysKey, const std::weak_ptr< ExpList > &pExpList, const std::shared_ptr< AssemblyMap > &pLocToGloMap)
Creates an instance of this class.
void v_SolveLinearSystem(const int pNumRows, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const AssemblyMapSharedPtr &locToGloMap, const int pNumDir) override
Solve the linear system for given input and output vectors.
void AssembleFullMatrix(const std::shared_ptr< AssemblyMap > &locToGloMap)
DNekLinSysSharedPtr m_linSys
Basic linear system object.
A global linear system.
Definition: GlobalLinSys.h:70
const std::weak_ptr< ExpList > m_expList
Local Matrix System.
Definition: GlobalLinSys.h:122
const std::map< int, RobinBCInfoSharedPtr > m_robinBCInfo
Robin boundary info.
Definition: GlobalLinSys.h:124
void SolveLinearSystem(const int pNumRows, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const AssemblyMapSharedPtr &locToGloMap, const int pNumDir=0)
Solve the linear system for given input and output vectors.
Definition: GlobalLinSys.h:190
const GlobalLinSysKey m_linSysKey
Key associated with this linear system.
Definition: GlobalLinSys.h:120
DNekScalMatSharedPtr GetBlock(unsigned int n)
Definition: GlobalLinSys.h:209
GlobalSysSolnType GetGlobalSysSolnType() const
Return the associated solution type.
StdRegions::MatrixType GetMatrixType() const
Return the matrix type.
std::shared_ptr< Expansion > ExpansionSharedPtr
Definition: Expansion.h:66
std::shared_ptr< RobinBCInfo > RobinBCInfoSharedPtr
GlobalLinSysFactory & GetGlobalLinSysFactory()
std::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:50
std::vector< double > w(NPUPPER)
std::shared_ptr< DNekScalMat > DNekScalMatSharedPtr
@ ePOSITIVE_DEFINITE_SYMMETRIC_BANDED
@ ePOSITIVE_DEFINITE_SYMMETRIC
std::shared_ptr< DNekMat > DNekMatSharedPtr
Definition: NekTypeDefs.hpp:75
double NekDouble
PointerWrapper
Specifies if the pointer passed to a NekMatrix or NekVector is copied into an internal representation...
void Vadd(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Add vector z = x+y.
Definition: Vmath.hpp:180
void Vsub(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Subtract vector z = x-y.
Definition: Vmath.hpp:220
STL namespace.