Nektar++
Loading...
Searching...
No Matches
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{
82 "This routine should only be used when using a Full Direct"
83 " matrix solve");
84 ASSERTL1(pExp.lock()->GetComm()->GetSize() == 1,
85 "Direct full matrix solve can only be used in serial.");
86
87 AssembleFullMatrix(pLocToGloMap);
88}
89
90/**
91 * Solve the linear system using a full global matrix system.
92 */
94 const Array<OneD, const NekDouble> &pLocInput,
95 Array<OneD, NekDouble> &pLocOutput,
96 const AssemblyMapSharedPtr &pLocToGloMap,
97 const Array<OneD, const NekDouble> &pDirForcing)
98{
99 bool dirForcCalculated = (bool)pDirForcing.size();
100 int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
101 int nGlobDofs = pLocToGloMap->GetNumGlobalCoeffs();
102 int nLocDofs = pLocToGloMap->GetNumLocalCoeffs();
103
104 if (nDirDofs)
105 {
106 std::shared_ptr<MultiRegions::ExpList> expList = m_expList.lock();
107 Array<OneD, NekDouble> rhs(nLocDofs);
108
109 // Calculate the Dirichlet forcing
110 if (dirForcCalculated)
111 {
112 // Assume pDirForcing is in local space
113 ASSERTL0(
114 pDirForcing.size() >= nLocDofs,
115 "DirForcing is not of sufficient size. Is it in local space?");
116 Vmath::Vsub(nLocDofs, pLocInput, 1, pDirForcing, 1, rhs, 1);
117 }
118 else
119 {
120 // Calculate initial condition and Dirichlet forcing and subtract it
121 // from the rhs
122 expList->GeneralMatrixOp(m_linSysKey, pLocOutput, rhs);
123
124 // Iterate over all the elements computing Robin BCs where
125 // necessary
126 for (auto &r : m_robinBCInfo) // add robin mass matrix
127 {
130
131 int n = r.first;
132 int offset = expList->GetCoeff_Offset(n);
133
134 LocalRegions::ExpansionSharedPtr vExp = expList->GetExp(n);
135 // Add local matrix contribution
136 for (rBC = r.second; rBC; rBC = rBC->next)
137 {
138 vExp->AddRobinTraceContribution(
139 rBC->m_robinID, rBC->m_robinPrimitiveCoeffs,
140 pLocOutput + offset, rhsloc = rhs + offset);
141 }
142 }
143 Vmath::Vsub(nLocDofs, pLocInput, 1, rhs, 1, rhs, 1);
144 }
145
146 Array<OneD, NekDouble> diff(nLocDofs);
147
148 // Solve for perturbation from initial guess in pOutput
149 SolveLinearSystem(nGlobDofs, rhs, diff, pLocToGloMap, nDirDofs);
150
151 // Add back initial and boundary condition
152 Vmath::Vadd(nLocDofs, diff, 1, pLocOutput, 1, pLocOutput, 1);
153 }
154 else
155 {
156 SolveLinearSystem(nGlobDofs, pLocInput, pLocOutput, pLocToGloMap,
157 nDirDofs);
158 }
159}
160
161/**
162 * Assemble a full matrix from the block matrix stored in
163 * #m_blkMatrices and the given local to global mapping information.
164 * @param locToGloMap Local to global mapping information.
165 */
167 const AssemblyMapSharedPtr &pLocToGloMap)
168{
169 int i, j, n, cnt, gid1, gid2;
170 NekDouble sign1, sign2, value;
171 int totDofs = pLocToGloMap->GetNumGlobalCoeffs();
172 int NumDirBCs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
173
174 unsigned int rows = totDofs - NumDirBCs;
175 unsigned int cols = totDofs - NumDirBCs;
176 NekDouble zero = 0.0;
177
178 DNekMatSharedPtr Gmat;
179 int bwidth = pLocToGloMap->GetFullSystemBandWidth();
180 MatrixStorage matStorage = eFULL;
181
182 switch (m_linSysKey.GetMatrixType())
183 {
184 // case for all symmetric matices
189 {
190 if ((2 * (bwidth + 1)) < rows)
191 {
194 rows, cols, zero, matStorage, bwidth, bwidth);
195 }
196 else
197 {
198 matStorage = ePOSITIVE_DEFINITE_SYMMETRIC;
200 rows, cols, zero, matStorage);
201 }
202 break;
203 }
206 {
207 matStorage = eFULL;
208 Gmat = MemoryManager<DNekMat>::AllocateSharedPtr(rows, cols, zero,
209 matStorage);
210 break;
211 }
212 default:
213 {
214 NEKERROR(ErrorUtil::efatal, "Add MatrixType to switch "
215 "statement");
216 }
217 }
218
219 // fill global matrix
220 DNekScalMatSharedPtr loc_mat;
221
222 int loc_lda;
223 for (n = cnt = 0; n < m_expList.lock()->GetNumElmts(); ++n)
224 {
225 loc_mat = GetBlock(n);
226 loc_lda = loc_mat->GetRows();
227
228 for (i = 0; i < loc_lda; ++i)
229 {
230 gid1 = pLocToGloMap->GetLocalToGlobalMap(cnt + i) - NumDirBCs;
231 sign1 = pLocToGloMap->GetLocalToGlobalSign(cnt + i);
232 if (gid1 >= 0)
233 {
234 for (j = 0; j < loc_lda; ++j)
235 {
236 gid2 =
237 pLocToGloMap->GetLocalToGlobalMap(cnt + j) - NumDirBCs;
238 sign2 = pLocToGloMap->GetLocalToGlobalSign(cnt + j);
239 if (gid2 >= 0)
240 {
241 // When global matrix is symmetric,
242 // only add the value for the upper
243 // triangular part in order to avoid
244 // entries to be entered twice
245 if ((matStorage == eFULL) || (gid2 >= gid1))
246 {
247 value = Gmat->GetValue(gid1, gid2) +
248 sign1 * sign2 * (*loc_mat)(i, j);
249 Gmat->SetValue(gid1, gid2, value);
250 }
251 }
252 }
253 }
254 }
255 cnt += loc_lda;
256 }
257
258 if (rows)
259 {
262 }
263}
264
265/// Solve the linear system for given input and output vectors.
267 const int pNumRows, const Array<OneD, const NekDouble> &pInput,
268 Array<OneD, NekDouble> &pOutput, const AssemblyMapSharedPtr &pLocToGloMap,
269 const int pNumDir)
270{
271 Array<OneD, NekDouble> tmp(pNumRows);
272 Array<OneD, NekDouble> global(pNumRows, 0.0);
273
274 pLocToGloMap->Assemble(pInput, tmp);
275
276 const int nHomDofs = pNumRows - pNumDir;
277 DNekVec Vin(nHomDofs, tmp + pNumDir);
278
279 Array<OneD, NekDouble> tmp1 = global + pNumDir;
280 DNekVec Vout(nHomDofs, tmp1, eWrapper);
281
282 m_linSys->Solve(Vin, Vout);
283
284 pLocToGloMap->GlobalToLocal(global, pOutput);
285}
286
287} // namespace Nektar::MultiRegions
#define ASSERTL0(condition, msg)
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
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.
const std::weak_ptr< ExpList > m_expList
Local Matrix System.
const std::map< int, RobinBCInfoSharedPtr > m_robinBCInfo
Robin boundary info.
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.
const GlobalLinSysKey m_linSysKey
Key associated with this linear system.
DNekScalMatSharedPtr GetBlock(unsigned int n)
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::shared_ptr< DNekScalMat > DNekScalMatSharedPtr
@ ePOSITIVE_DEFINITE_SYMMETRIC_BANDED
@ ePOSITIVE_DEFINITE_SYMMETRIC
std::shared_ptr< DNekMat > DNekMatSharedPtr
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.