Nektar++
GlobalLinSysXxtFull.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: GlobalLinSysXxtFull.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: GlobalLinSysXxtFull definition
32//
33///////////////////////////////////////////////////////////////////////////////
34
38
39using namespace std;
40
41namespace Nektar
42{
43namespace MultiRegions
44{
45/**
46 * @class GlobalLinSysXxtFull
47 */
48
49/**
50 * Registers the class with the Factory.
51 */
54 "XxtFull", GlobalLinSysXxtFull::create, "Xxt Full Matrix.");
55
56/// Constructor for full direct matrix solve.
58 const GlobalLinSysKey &pLinSysKey, const std::weak_ptr<ExpList> &pExp,
59 const std::shared_ptr<AssemblyMap> &pLocToGloMap)
60 : GlobalLinSys(pLinSysKey, pExp, pLocToGloMap),
61 GlobalLinSysXxt(pLinSysKey, pExp, pLocToGloMap)
62{
63
65 "This routine should only be used when using a Full XXT"
66 " matrix solve");
67
68 AssembleMatrixArrays(pLocToGloMap);
69}
70
72{
73}
74
75/**
76 * Solve the linear system using a full global matrix system.
77 */
79 const Array<OneD, const NekDouble> &pLocInput,
80 Array<OneD, NekDouble> &pLocOutput,
81 const AssemblyMapSharedPtr &pLocToGloMap,
82 const Array<OneD, const NekDouble> &pDirForcing)
83{
84 bool dirForcCalculated = (bool)pDirForcing.size();
85 int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
86 int nGlobDofs = pLocToGloMap->GetNumGlobalCoeffs();
87 int nLocDofs = pLocToGloMap->GetNumLocalCoeffs();
88
89 Array<OneD, NekDouble> tmp(nLocDofs);
90 Array<OneD, NekDouble> tmp1(nLocDofs);
91 Array<OneD, NekDouble> global(nGlobDofs, 0.0);
92
93 if (nDirDofs)
94 {
95 // calculate the dirichlet forcing
96 if (dirForcCalculated)
97 {
98 // assume pDirForcing is in local space
100 pDirForcing.size() >= nLocDofs,
101 "DirForcing is not of sufficient size. Is it in local space?");
102 Vmath::Vsub(nLocDofs, pLocInput, 1, pDirForcing, 1, tmp1, 1);
103 }
104 else
105 {
106 // Calculate the dirichlet forcing and substract it
107 // from the rhs
108 m_expList.lock()->GeneralMatrixOp(m_linSysKey, pLocOutput, tmp);
109
110 Vmath::Vsub(nLocDofs, pLocInput, 1, tmp, 1, tmp1, 1);
111 }
112
113 SolveLinearSystem(pLocToGloMap->GetNumLocalCoeffs(), tmp1, tmp,
114 pLocToGloMap);
115
116 // Add back initial and boundary condition
117 Vmath::Vadd(nLocDofs, tmp, 1, pLocOutput, 1, pLocOutput, 1);
118 }
119 else
120 {
121 SolveLinearSystem(pLocToGloMap->GetNumLocalCoeffs(), pLocInput,
122 pLocOutput, pLocToGloMap);
123 }
124}
125
126/**
127 * Construct the local matrix row index, column index and value index
128 * arrays and initialize the XXT data structure with this information.
129 * @param locToGloMap Local to global mapping information.
130 */
132 const std::shared_ptr<AssemblyMap> &pLocToGloMap)
133{
134 ExpListSharedPtr vExp = m_expList.lock();
135 unsigned int nElmt = vExp->GetNumElmts();
136 DNekScalMatSharedPtr loc_mat;
137 unsigned int iCount = 0;
138 unsigned int rCount = 0;
139 unsigned int nRows = 0;
140 unsigned int nEntries = 0;
141 unsigned int numDirBnd = pLocToGloMap->GetNumGlobalDirBndCoeffs();
142 unsigned int nLocal = pLocToGloMap->GetNumLocalCoeffs();
143 const Array<OneD, NekDouble> &vMapSign =
144 pLocToGloMap->GetLocalToGlobalSign();
145 bool doSign = pLocToGloMap->GetSignChange();
146 unsigned int i = 0, j = 0, k = 0, n = 0;
147 int gid1;
148 Array<OneD, unsigned int> vSizes(nElmt);
149
150 // First construct a map of the number of local DOFs in each block
151 // and the number of matrix entries for each block
152
153 // Dimension of matrix is just the linear vertex space
156 {
157 for (n = 0; n < nElmt; ++n)
158 {
159 vSizes[n] = vExp->GetExp(n)->GetNverts();
160 nEntries += vSizes[n] * vSizes[n];
161 }
162 }
163 else
164 {
165 for (n = 0; n < nElmt; ++n)
166 {
167 vSizes[n] = vExp->GetExp(n)->GetNcoeffs();
168 nEntries += vSizes[n] * vSizes[n];
169 }
170 }
171
172 // Set up i-index, j-index and value arrays
175 m_Ar = Array<OneD, double>(nEntries, 0.0);
176
177 // Set up the universal ID array for XXT
178 Array<OneD, unsigned long> vId(nLocal);
179
180 // Loop over each elemental block, extract matrix indices and value
181 // and set the universal ID array
182 for (n = iCount = 0; n < nElmt; ++n)
183 {
184 loc_mat = GetBlock(n);
185 nRows = loc_mat->GetRows();
186
187 for (i = 0; i < nRows; ++i)
188 {
189 gid1 = pLocToGloMap->GetLocalToGlobalMap(iCount + i);
190 for (j = 0; j < nRows; ++j)
191 {
192 k = rCount + i * vSizes[n] + j;
193 m_Ai[k] = iCount + i;
194 m_Aj[k] = iCount + j;
195 m_Ar[k] = (*loc_mat)(i, j);
196 if (doSign)
197 {
198 m_Ar[k] *= vMapSign[iCount + i] * vMapSign[iCount + j];
199 }
200 }
201
202 // Dirichlet DOFs are not included in the solve, so we set
203 // these to the special XXT id=0.
204 if (gid1 < numDirBnd)
205 {
206 vId[iCount + i] = 0;
207 }
208 else
209 {
210 vId[iCount + i] = pLocToGloMap->GetGlobalToUniversalMap(gid1);
211 }
212 }
213 iCount += vSizes[n];
214 rCount += vSizes[n] * vSizes[n];
215 }
216
217 // Set up XXT and output some stats
218 LibUtilities::CommSharedPtr vComm = pLocToGloMap->GetComm();
219 m_crsData = Xxt::Init(nLocal, vId, m_Ai, m_Aj, m_Ar, vComm);
220 if (m_verbose)
221 {
223 }
224}
225
226/// Solve the linear system for given input and output vectors.
228 const int pNumRows, const Array<OneD, const NekDouble> &pInput,
229 Array<OneD, NekDouble> &pOutput, const AssemblyMapSharedPtr &pLocToGloMap,
230 const int pNumDir)
231{
232 boost::ignore_unused(pNumRows, pNumDir);
233
234 int nLocal = pNumRows;
235
236 Vmath::Zero(nLocal, pOutput, 1);
237
238 // Set Output into correct sign
239 if (pLocToGloMap->GetSignChange())
240 {
241 Array<OneD, NekDouble> vlocal(nLocal);
242 Vmath::Vmul(nLocal, pLocToGloMap->GetLocalToGlobalSign(), 1, pInput, 1,
243 vlocal, 1);
244
245 Xxt::Solve(pOutput, m_crsData, vlocal);
246
247 Vmath::Vmul(nLocal, pLocToGloMap->GetLocalToGlobalSign(), 1, pOutput, 1,
248 pOutput, 1);
249 }
250 else
251 {
252 Xxt::Solve(pOutput, m_crsData, pInput);
253 }
254}
255
256} // namespace MultiRegions
257} // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:249
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:198
A global linear system.
Definition: GlobalLinSys.h:72
const std::weak_ptr< ExpList > m_expList
Local Matrix System.
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:192
const GlobalLinSysKey m_linSysKey
Key associated with this linear system.
Definition: GlobalLinSys.h:122
DNekScalMatSharedPtr GetBlock(unsigned int n)
Definition: GlobalLinSys.h:211
GlobalSysSolnType GetGlobalSysSolnType() const
Return the associated solution type.
virtual void v_Solve(const Array< OneD, const NekDouble > &in, Array< OneD, NekDouble > &out, 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.
virtual void v_SolveLinearSystem(const int pNumRows, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const AssemblyMapSharedPtr &locToGloMap, const int pNumDir=0) override
Solve the linear system for given input and output vectors.
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 AssembleMatrixArrays(const std::shared_ptr< AssemblyMap > &pLocToGloMap)
GlobalLinSysXxtFull(const GlobalLinSysKey &pLinSysKey, const std::weak_ptr< ExpList > &pExpList, const std::shared_ptr< AssemblyMap > &pLocToGloMap)
Constructor for full direct matrix solve.
static std::string className
Name of class.
Array< OneD, unsigned int > m_Aj
Array< OneD, unsigned int > m_Ai
StdRegions::MatrixType GetMatrixType() const
Return the matrix type.
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:57
GlobalLinSysFactory & GetGlobalLinSysFactory()
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
std::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:52
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
std::shared_ptr< DNekScalMat > DNekScalMatSharedPtr
void Vmul(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x*y.
Definition: Vmath.cpp:207
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.cpp:354
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:487
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.cpp:414
void nektar_crs_stats(struct crs_data *data)
static struct crs_data * Init(unsigned int pRank, const Nektar::Array< OneD, unsigned long > pId, const Nektar::Array< OneD, unsigned int > pAi, const Nektar::Array< OneD, unsigned int > pAj, const Nektar::Array< OneD, NekDouble > pAr, const LibUtilities::CommSharedPtr &pComm)
Initialise the matrix-solve.
Definition: Xxt.hpp:158
static void Solve(Nektar::Array< OneD, NekDouble > pX, struct crs_data *pCrs, Nektar::Array< OneD, NekDouble > pB)
Solve the matrix system for a given input vector b.
Definition: Xxt.hpp:186