LifeV
Assembly.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief File containing method to insert local contributions in the global system.
30 
31  @author Alessandro Veneziani
32  @date 31-01-2001
33 
34  @contributor Samuel Quinodoz <samuel.quinodoz@epfl.ch>
35  @mantainer Samuel Quinodoz <samuel.quinodoz@epfl.ch>
36 
37  To delete:
38  extract_vec
39  assemb_mat
40  assemb_vec
41 
42  To create:
43  namespace Assemble
44 
45  To change:
46  template parameter
47  DOF -> globalMatrix and localMatrix
48  */
49 
50 
51 #ifndef _ASSEMBLY
52 #define _ASSEMBLY
53 
54 #include <lifev/core/LifeV.hpp>
55 
56 #include <lifev/core/array/MatrixElemental.hpp>
57 #include <lifev/core/array/VectorElemental.hpp>
58 #include <lifev/core/array/MatrixEpetra.hpp>
59 #include <lifev/core/array/VectorEpetra.hpp>
60 
61 #include <lifev/core/fem/CurrentFE.hpp>
62 #include <lifev/core/fem/DOFLocalPattern.hpp>
63 
64 
65 namespace LifeV
66 {
67 
68 
69 
70 //! Assembly procedure for vectors
71 /*!
72  This function allows the user to transfer
73  a local contribution to a global vector.
74  */
75 template <typename DofType>
76 void
77 assembleVector ( VectorEpetra& globalVector,
78  VectorElemental& localVector,
79  const CurrentFE& currentFE,
80  const DofType dof,
81  Int block,
82  Int offset = 0 )
83 
84 {
85  UInt elementID = currentFE.currentLocalId();
86  assembleVector (globalVector, elementID, localVector, currentFE.nbFEDof(), dof, block, offset);
87 }
88 
89 //! Assembly procedure for vectors
90 /*!
91  This function can transfer a local
92  contribution to a global vector.
93  */
94 template <typename DofType>
95 void
96 assembleVector ( VectorEpetra& globalVector,
97  const UInt& elementID,
98  VectorElemental& localVector,
99  const UInt& feNbDof,
100  const DofType& dof,
101  Int block,
102  Int offset = 0)
103 
104 {
105  VectorElemental::vector_view localView = localVector.block ( block );
106 
107  UInt iGlobalID;
108 
109  for ( UInt i (0) ; i < feNbDof ; ++i )
110  {
111  iGlobalID = dof.localToGlobalMap ( elementID, i ) + offset;
112  globalVector.sumIntoGlobalValues ( iGlobalID, localView ( i ) );
113  }
114 }
115 
116 //! Assembly procedure for the matrix
117 /*!
118  This method allows to transfer local contributions
119  to a global matrix.
120  */
121 template <typename DofType>
122 void
123 assembleMatrix ( MatrixEpetra<Real>& globalMatrix,
124  const UInt& elementID,
125  MatrixElemental& localMatrix,
126  const UInt& feNbDof,
127  const DofType& dof,
128  Int iblock,
129  Int jblock,
130  Int iOffset,
131  Int jOffset)
132 
133 {
134 
135  MatrixElemental::matrix_view localView = localMatrix.block ( iblock, jblock );
136 
137  assembleMatrix ( globalMatrix,
138  elementID,
139  elementID,
140  localView,
141  feNbDof,
142  feNbDof,
143  dof,
144  dof, iOffset, jOffset);
145 }
146 
147 //! Assembly procedure for the matrix
148 /*!
149  This method allows to transfer local contributions
150  to a global matrix.
151  */
152 template <typename DofType>
153 void
154 assembleMatrix ( MatrixEpetra<Real>& globalMatrix,
155  MatrixElemental& localMatrix,
156  const CurrentFE& currentFE,
157  const DofType& dof,
158  Int iblock,
159  Int jblock,
160  Int iOffset,
161  Int jOffset)
162 
163 {
164  return assembleMatrix ( globalMatrix, localMatrix, currentFE, currentFE, dof, dof,
165  iblock, jblock, iOffset, jOffset);
166 }
167 
168 //! Assembly procedure for the matrix
169 /*!
170  This method allows to transfer local contributions
171  to a global matrix.
172  */
173 template <typename DofType1, typename DofType2, typename LocalMatrixType>
174 void
175 assembleMatrix ( MatrixEpetra<Real>& globalMatrix,
176  UInt const& elementID1,
177  UInt const& elementID2,
178  LocalMatrixType& localMatrix,
179  const CurrentFE& currentFE1,
180  const CurrentFE& currentFE2,
181  const DofType1& dof1,
182  const DofType2& dof2,
183  Int iOffset,
184  Int jOffset)
185 
186 {
187  assembleMatrix (globalMatrix, elementID1, elementID2, localMatrix, currentFE1.nbFEDof(), currentFE2.nbFEDof(),
188  dof1, dof2, iOffset, jOffset);
189 
190 }
191 
192 //! Assembly procedure for the matrix
193 /*!
194  This method allows to transfer local contributions
195  to a global matrix.
196  */
197 template <typename DofType1, typename DofType2, typename LocalMatrixType>
198 void
199 assembleMatrix ( MatrixEpetra<Real>& globalMatrix,
200  UInt const& elementID1,
201  UInt const& elementID2,
202  LocalMatrixType& localMatrix,
203  const UInt& fe1NbDof,
204  const UInt& fe2NbDof,
205  const DofType1& dof1,
206  const DofType2& dof2,
207  Int iOffset,
208  Int jOffset )
209 
210 {
211  // Global ID of the dofs
212  std::vector<Int> iList (fe1NbDof);
213  std::vector<Int> jList (fe2NbDof);
214 
215  // Raw data to insert in the matrix
216  std::vector<Real*> matPtr (fe2NbDof);
217 
218 
219  for ( UInt k1 (0) ; k1 < fe1NbDof ; k1++ )
220  {
221  iList[k1] = dof1.localToGlobalMap ( elementID1, k1 ) + iOffset ;
222  }
223 
224  for ( UInt k2 (0) ; k2 < fe2NbDof ; k2++ )
225  {
226  jList[k2] = dof2.localToGlobalMap ( elementID2, k2 ) + jOffset ;
227  matPtr[k2] = & (localMatrix (static_cast<UInt> (0), k2) );
228  }
229 
230  assert (localMatrix.indexij ( Int (1), Int (0) ) == 1);
231 
232  globalMatrix.addToCoefficients ( fe1NbDof, fe2NbDof, iList, jList, &matPtr[0], Epetra_FECrsMatrix::COLUMN_MAJOR );
233 }
234 
235 //! Assembly procedure for the matrix
236 /*!
237  This method allows to transfer local contributions
238  to a global matrix.
239  */
240 template <typename DofType1, typename DofType2>
241 void
242 assembleMatrix ( MatrixEpetra<Real>& globalMatrix,
243  MatrixElemental& localMatrix,
244  const CurrentFE& currentFE1,
245  const CurrentFE& currentFE2,
246  const DofType1& dof1,
247  const DofType2& dof2,
248  Int iblock,
249  Int jblock,
250  Int iOffset,
251  Int jOffset )
252 
253 {
254  MatrixElemental::matrix_view localView = localMatrix.block ( iblock, jblock );
255 
256  UInt elementID1 = currentFE1.currentLocalId();
257  UInt elementID2 = currentFE2.currentLocalId();
258 
259  assembleMatrix ( globalMatrix, elementID1, elementID2, localView,
260  currentFE1, currentFE2, dof1, dof2, iOffset, jOffset );
261 
262  return;
263 
264 }
265 
266 //! Assembly procedure for the transposed matrix
267 /*!
268  This method allows to transfer local contributions
269  to a transposed global matrix.
270 
271  The coefficient is used to multiply the values.
272  */
273 template <typename DofType1, typename DofType2>
274 void
275 assembleTransposeMatrix ( MatrixEpetra<Real>& globalMatrix,
276  Real coefficient,
277  MatrixElemental& localMatrix,
278  const CurrentFE& currentFE1,
279  const CurrentFE& currentFE2,
280  const DofType1& dof1,
281  const DofType2& dof2,
282  Int iblock,
283  Int jblock,
284  Int iOffset ,
285  Int jOffset )
286 
287 {
288  MatrixElemental::matrix_type localView (localMatrix.block ( jblock, iblock ) );
289  localView *= coefficient;
290 
291  Int i, j;
292  UInt k1, k2;
293 
294  UInt elementID1 = currentFE1.currentLocalId();
295  UInt elementID2 = currentFE2.currentLocalId();
296 
297  std::vector<Int> ilist (currentFE1.nbFEDof() );
298  std::vector<Int> jlist (currentFE2.nbFEDof() );
299 
300  std::vector<Real*> matPtr (currentFE1.nbFEDof() );
301 
302  for ( k1 = 0 ; k1 < currentFE1.nbFEDof() ; k1++ )
303  {
304  i = k1;
305  ilist[k1] = dof1.localToGlobalMap ( elementID1, i ) + iOffset ;
306  matPtr[k1] = & (localView (0, i) );
307  }
308 
309  for ( k2 = 0 ; k2 < currentFE2.nbFEDof() ; k2++ )
310  {
311  j = k2;
312  jlist[k2] = dof2.localToGlobalMap ( elementID2, j ) + jOffset ;
313  }
314 
315  assert (localView.indexij ( Int (1), Int (0) ) == 1);
316 
317  globalMatrix.addToCoefficients ( currentFE1.nbFEDof(), currentFE2.nbFEDof(),
318  ilist, jlist, &matPtr[0], Epetra_FECrsMatrix::ROW_MAJOR );
319 
320 }
321 
322 template <typename DOF, typename VectorElemental>
323 void
324 extract_vec ( const VectorEpetra& V,
325  VectorElemental& elvec,
326  const DOFLocalPattern& fe,
327  const DOF& dof,
328  const UInt feId,
329  const UInt elvecBlock )
330 {
331  typename VectorElemental::vector_view vec = elvec.block ( elvecBlock );
332 
333  for ( UInt i (0) ; i < fe.nbLocalDof() ; ++i )
334  {
335  const UInt ig = dof.localToGlobalMap ( feId, i );
336  vec ( i ) = V [ ig ];
337  }
338 }
339 
340 }
341 
342 #endif
VectorEpetra - The Epetra Vector format Wrapper.
void importFromHDF5(std::string const &fileName, std::string const &matrixName="matrix")
Read a matrix from a HDF5 (.h5) file.
int32_type Int
Generic integer data.
Definition: LifeV.hpp:188
const data_type & operator[](const UInt row) const
Access operators.
void updateInverseJacobian(const UInt &iQuadPt)
DOFLocalPattern - A class to store the "couplings" between the basis functions.
double Real
Generic real data.
Definition: LifeV.hpp:175
CurrentFE - A primordial class for the assembly of the local matrices/vectors retaining the values on...
Definition: CurrentFE.hpp:243
UInt nbFEDof() const
Getter for the number of nodes.
Definition: CurrentFE.hpp:377
const UInt & nbLocalDof() const
Return the number of local degrees of freedom.
UInt currentLocalId() const
Getter for the local ID of the current cell.
Definition: CurrentFE.hpp:359
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191