ug4
lapack_invert.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2014: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG__CPU_ALGEBRA__LAPACK_INVERT_H__
34 #define __H__UG__CPU_ALGEBRA__LAPACK_INVERT_H__
35 
36 #include "../small_matrix/densematrix.h"
37 #include "../small_matrix/densevector.h"
38 #include "../small_matrix/block_dense.h"
39 #include "lapack.h"
40 #include <vector>
41 
42 namespace ug{
43 
44 
45 
46 template<typename T>
48 {
49  std::vector<lapack_int> interchange(mat.num_rows());
50 
51  int info = getrf(mat.num_rows(), mat.num_cols(), &mat(0,0), mat.num_rows(), &interchange[0]);
52  //UG_ASSERT(info == 0, "info is " << info << ( info > 0 ? ": SparseMatrix singular in U(i,i)" : ": i-th argument had an illegal value"));
53  if(info != 0) return false;
54 
55  // calc work size
56  double worksize; int iWorksize = -1;
57  info = getri(mat.num_rows(), &mat(0,0), mat.num_rows(), &interchange[0], &worksize, iWorksize);
58  //UG_ASSERT(info == 0, "");
59  iWorksize = (int)worksize;
60 
61  std::vector<double> work(iWorksize);
62 
63  info = getri(mat.num_rows(), &mat(0,0), mat.num_rows(), &interchange[0], &work[0], iWorksize);
64  //UG_ASSERT(info == 0, "");
65  if(info != 0) return false;
66 
67  return true;
68 }
69 
70 template<typename T, size_t TUnknowns>
72 {
73  lapack_int interchange[TUnknowns];
74 
75  int info = getrf(mat.num_rows(), mat.num_cols(), &mat(0,0), mat.num_rows(), interchange);
76  UG_COND_THROW(info != 0, "info is " << info << ( info > 0 ? ": Matrix singular in mat(i,i)" : ": i-th argument had an illegal value"));
77  if(info != 0) return false;
78 
79  // calc work size
80  // todo: make this static
81  double worksize; int iWorksize = -1;
82  info = getri(mat.num_rows(), &mat(0,0), mat.num_rows(), interchange, &worksize, iWorksize);
83  UG_ASSERT(info == 0, "");
84  iWorksize = (int)worksize;
85 
86  std::vector<double> work;
87  work.resize(iWorksize);
88 
89  info = getri(mat.num_rows(), &mat(0,0), mat.num_rows(), interchange, &work[0], iWorksize);
90  UG_ASSERT(info == 0, "");
91  if(info != 0) return false;
92 
93  return true;
94 }
95 
96 
97 }
98 
99 #endif
Definition: densematrix.h:57
Definition: fixed_array.h:135
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition: error.h:61
the ug namespace
bool Invert(T &m)
lapack_int getri(lapack_int n, lapack_float *pColMajorMatrix, lapack_int lda, const int *pPivot, lapack_float *pWork, lapack_int worksize)
Definition: lapack.h:306
lapack_int getrf(lapack_int rows, lapack_int cols, lapack_float *pColMajorMatrix, lapack_int lda, lapack_int *pPivot)
Definition: lapack.h:161
bool InvertNdyn(DenseMatrix< T > &mat)
Definition: lapack_invert.h:47
int lapack_int
Definition: lapack.h:71