PolyDiM
C++ library for POLYtopal DIscretization Methods
Loading...
Searching...
No Matches
SuiteSparse_Utilities.hpp
Go to the documentation of this file.
1
// _LICENSE_HEADER_
2
//
3
// Copyright (C) 2019 - 2025.
4
// Terms register on the GPL-3.0 license.
5
//
6
// This file can be redistributed and/or modified under the license terms.
7
//
8
// See top level LICENSE file for more details.
9
//
10
// This file can be used citing references in CITATION.cff file.
11
12
#ifndef __SUITESPARSE_UTILITIES_H
13
#define __SUITESPARSE_UTILITIES_H
14
15
#include <iostream>
16
#include <string>
17
18
#include "Eigen/Eigen"
19
#include "
Eigen_Utilities.hpp
"
20
#include "Gedim_Macro.hpp"
21
22
#if ENABLE_SUITESPARSE == 1
23
#include <cholmod.h>
24
#include <klu.h>
25
#endif
26
27
namespace
Gedim
28
{
29
#if ENABLE_SUITESPARSE == 1
30
struct
SuiteSparse_Utilities
31
{
32
template
<
typename
Derived>
static
double
condest
(
const
Eigen::SparseMatrix<Derived> &
sparseA
)
33
{
34
35
const
std::vector<Eigen::Triplet<Derived>>
Aentries
= Gedim::Eigen_Utilities::SparseMatrixToTriplets<Derived>(
sparseA
);
36
const
unsigned
int
N
=
sparseA
.rows();
37
38
cholmod_common
CholCommon
, *
cc
;
39
cc
= &
CholCommon
;
40
cholmod_l_start
(
cc
);
41
42
cholmod_triplet
*
Gct
=
cholmod_l_allocate_triplet
(
N
,
43
N
,
44
Aentries
.size(),
45
0,
// stype: both upper and lower are stored
46
CHOLMOD_REAL
,
47
cc
);
48
for
(
size_t
i = 0; i <
Aentries
.size(); ++i)
49
{
50
reinterpret_cast<
long
*
>
(
Gct
->i)[i] =
Aentries
[i].
row
();
51
reinterpret_cast<
long
*
>
(
Gct
->j)[i] =
Aentries
[i].
col
();
52
reinterpret_cast<
double
*
>
(
Gct
->x)[i] =
Aentries
[i].value();
53
}
54
Gct
->nnz =
Aentries
.size();
55
56
// convert triplet matrix to sparse
57
cholmod_sparse
*
G
=
cholmod_l_triplet_to_sparse
(
Gct
,
Aentries
.size(),
cc
);
58
59
klu_l_common
Common;
60
klu_l_defaults
(&Common);
61
long
*
Ap
=
reinterpret_cast<
long
*
>
(
G
->p);
62
long
*
Ai
=
reinterpret_cast<
long
*
>
(
G
->i);
63
double
*
Ax
=
reinterpret_cast<
double
*
>
(
G
->x);
64
klu_l_symbolic
*
Symbolic
=
klu_l_analyze
(
N
,
Ap
,
Ai
, &Common);
65
klu_l_numeric
*
Numeric
=
klu_l_factor
(
Ap
,
Ai
,
Ax
,
Symbolic
, &Common);
66
67
/* Computes a reasonably accurate estimate of the 1-norm condition number, using
68
* Hager’s method, as modified by Higham and Tisseur (same method as used in
69
* MATLAB’s condest */
70
int
info
=
klu_l_condest
(
Ap
,
Ax
,
Symbolic
,
Numeric
, &Common);
71
72
if
(!
info
)
73
{
74
cholmod_l_free_sparse
(&
G
, &
CholCommon
);
75
cholmod_l_free_triplet
(&
Gct
, &
CholCommon
);
76
klu_l_free_numeric
(&
Numeric
, &Common);
77
klu_l_free_symbolic
(&
Symbolic
, &Common);
78
throw
std::runtime_error(
"Suitesparse fails."
);
79
}
80
else
81
{
82
double
condest
= Common.condest;
83
84
cholmod_free_sparse
(&
G
, &
CholCommon
);
85
cholmod_l_free_triplet
(&
Gct
, &
CholCommon
);
86
klu_l_free_numeric
(&
Numeric
, &Common);
87
klu_l_free_symbolic
(&
Symbolic
, &Common);
88
return
condest
;
89
}
90
}
91
};
92
#endif
93
}
// namespace Gedim
94
#endif
// __SUITESPARSE_UTILITIES_H
Eigen_Utilities.hpp
Gedim::Eigen_Array::Eigen_Array
Eigen_Array()
Definition
Eigen_Array.hpp:28
Gedim
Definition
Eigen_Array.cpp:22
PolyDiM
gedim
GeDiM
src
Algebra
SuiteSparse_Utilities.hpp
Generated by
1.9.8