20 #include "libmesh/libmesh_config.h"
22 #ifdef LIBMESH_TRILINOS_HAVE_EPETRA
25 #include "libmesh/trilinos_epetra_matrix.h"
26 #include "libmesh/trilinos_epetra_vector.h"
27 #include "libmesh/dof_map.h"
28 #include "libmesh/dense_matrix.h"
29 #include "libmesh/parallel.h"
30 #include "libmesh/sparsity_pattern.h"
31 #include "libmesh/int_range.h"
48 libmesh_assert(this->_dof_map);
51 (sparsity_pattern.size());
55 const numeric_index_type n_l = this->_dof_map->n_dofs_on_processor(this->processor_id());
61 libmesh_assert_equal_to (n, m);
62 libmesh_assert_equal_to (n_l, m_l);
68 this->comm().sum (summed_m_l);
69 this->comm().sum (summed_n_l);
71 libmesh_assert_equal_to (m, summed_m_l);
72 libmesh_assert_equal_to (n, summed_n_l);
77 _map =
new Epetra_Map (static_cast<int>(m),
80 Epetra_MpiComm (this->comm().
get()));
82 libmesh_assert_equal_to (static_cast<numeric_index_type>(_map->NumGlobalPoints()), m);
83 libmesh_assert_equal_to (static_cast<numeric_index_type>(_map->MaxAllGID()+1), m);
85 const std::vector<numeric_index_type> & n_nz = this->_sp->get_n_nz();
86 const std::vector<numeric_index_type> & n_oz = this->_sp->get_n_oz();
89 libmesh_assert_equal_to (n_nz.size(), n_l);
90 libmesh_assert_equal_to (n_oz.size(), n_l);
93 std::vector<int> n_nz_tot; n_nz_tot.reserve(n_nz.size());
95 for (
auto i : index_range(n_nz))
96 n_nz_tot.push_back(std::min(n_nz[i] + n_oz[i], n));
101 _graph =
new Epetra_CrsGraph(Copy, *_map, n_nz_tot.data());
106 _graph->InsertGlobalIndices(_graph->GRID(i),
107 cast_int<numeric_index_type>(sparsity_pattern[i].size()),
108 const_cast<int *>(reinterpret_cast<const int *>(sparsity_pattern[i].data())));
110 _graph->FillComplete();
120 template <
typename T>
129 if ((m==0) || (n==0))
137 libmesh_assert (!this->_mat);
138 libmesh_assert (!this->_map);
146 libmesh_assert_equal_to (n, m);
147 libmesh_assert_equal_to (n_l, m_l);
153 this->comm().sum (summed_m_l);
154 this->comm().sum (summed_n_l);
156 libmesh_assert_equal_to (m, summed_m_l);
157 libmesh_assert_equal_to (n, summed_n_l);
162 _map =
new Epetra_Map (static_cast<int>(m),
165 Epetra_MpiComm (this->comm().
get()));
167 libmesh_assert_equal_to (static_cast<numeric_index_type>(_map->NumGlobalPoints()), m);
168 libmesh_assert_equal_to (static_cast<numeric_index_type>(_map->MaxAllGID()+1), m);
170 _mat =
new Epetra_FECrsMatrix (Copy, *_map, nnz + noz);
176 template <
typename T>
179 libmesh_assert(this->_dof_map);
190 _mat =
new Epetra_FECrsMatrix (Copy, *_graph);
195 template <
typename T>
205 template <
typename T>
210 libmesh_not_implemented();
213 auto mat_copy = std::make_unique<EpetraMatrix<T>>(this->comm());
219 return std::unique_ptr<SparseMatrix<T>>(mat_copy.release());
224 template <
typename T>
229 auto mat_copy = this->zero_clone();
230 mat_copy->add(1., *
this);
234 return std::unique_ptr<SparseMatrix<T>>(mat_copy.release());
239 template <
typename T>
252 template <
typename T>
257 libmesh_assert(_mat);
259 return static_cast<Real>(_mat->NormOne());
264 template <
typename T>
270 libmesh_assert(_mat);
272 return static_cast<Real>(_mat->NormInf());
277 template <
typename T>
279 const std::vector<numeric_index_type> & rows,
280 const std::vector<numeric_index_type> & cols)
287 libmesh_assert_equal_to (rows.size(), m);
288 libmesh_assert_equal_to (cols.size(), n);
300 template <
typename T>
307 _mat->ExtractDiagonalCopy(*(epetra_dest->
vec()));
312 template <
typename T>
322 if (&epetra_dest !=
this)
323 libmesh_not_implemented();
331 template <
typename T>
334 _destroy_mat_on_exit(true),
335 _use_transpose(false)
341 template <
typename T>
343 const Parallel::Communicator & comm) :
345 _destroy_mat_on_exit(false),
346 _use_transpose(false)
355 template <
typename T>
363 template <
typename T>
366 libmesh_assert(_mat);
368 _mat->GlobalAssemble();
373 template <
typename T>
383 template <
typename T>
393 template <
typename T>
397 libmesh_assert(_map);
404 template <
typename T>
408 libmesh_assert(_map);
415 template <
typename T>
423 epetra_i =
static_cast<int>(i),
424 epetra_j = static_cast<int>(j);
426 T epetra_value = value;
429 _mat->ReplaceGlobalValues (epetra_i, 1, &epetra_value, &epetra_j);
431 _mat->InsertGlobalValues (epetra_i, 1, &epetra_value, &epetra_j);
436 template <
typename T>
444 epetra_i =
static_cast<int>(i),
445 epetra_j = static_cast<int>(j);
447 T epetra_value = value;
449 _mat->SumIntoGlobalValues (epetra_i, 1, &epetra_value, &epetra_j);
454 template <
typename T>
456 const std::vector<numeric_index_type> & dof_indices)
458 this->add_matrix (dm, dof_indices, dof_indices);
463 template <
typename T>
466 #ifdef LIBMESH_TRILINOS_HAVE_EPETRAEXT
471 libmesh_assert_equal_to (this->m(), X_in.
m());
472 libmesh_assert_equal_to (this->n(), X_in.
n());
475 cast_ptr<const EpetraMatrix<T> *> (&X_in);
477 EpetraExt::MatrixMatrix::Add (*X->_mat,
false, a_in, *_mat, 1.);
479 libmesh_error_msg(
"ERROR: EpetraExt is required for EpetraMatrix::add()!");
486 template <
typename T>
491 libmesh_assert(this->_mat);
492 libmesh_assert (this->_mat->MyGlobalRow(static_cast<int>(i)));
493 libmesh_assert_greater_equal (i, this->row_start());
494 libmesh_assert_less (i, this->row_stop());
501 _mat->ExtractMyRowView (i-this->row_start(),
508 int * index = std::lower_bound (row_indices, row_indices+row_length, j);
510 libmesh_assert_less (*index, row_length);
511 libmesh_assert_equal_to (static_cast<numeric_index_type>(row_indices[*index]), j);
515 return values[*index];
521 template <
typename T>
525 libmesh_assert(this->_mat);
527 return this->_mat->Filled();
531 template <
typename T>
534 std::swap(_mat, m.
_mat);
542 template <
typename T>
546 libmesh_assert(_mat);
560 #endif // LIBMESH_TRILINOS_HAVE_EPETRA
此类提供了对Trilinos Epetra_Vector对象的友好接口。所有重写的虚拟函数在numeric_vector.h中都有文档。
virtual ~EpetraMatrix()
析构函数
virtual std::unique_ptr< SparseMatrix< T > > clone() const override
创建矩阵克隆。
virtual numeric_index_type row_stop() const override
获取本地行索引的结束位置。
unsigned int n() const
返回矩阵的列维度。
virtual void get_transpose(SparseMatrix< T > &dest) const override
获取矩阵的转置。
bool _destroy_mat_on_exit
在接口函数中是否销毁_mat。
virtual void update_sparsity_pattern(const SparsityPattern::Graph &graph) override
更新矩阵的稀疏性模式。这将告诉底层矩阵存储方案如何映射(i,j)元素。
bool _use_transpose
手动跟踪是否手动进行了转置。
virtual std::unique_ptr< SparseMatrix< T > > zero_clone() const override
创建零克隆矩阵。
virtual void init(const numeric_index_type m, const numeric_index_type n, const numeric_index_type m_l, const numeric_index_type n_l, const numeric_index_type nnz=30, const numeric_index_type noz=10, const numeric_index_type blocksize=1) override
初始化矩阵。
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) override
添加稀疏矩阵的元素值。
virtual numeric_index_type n() const override
获取全局列数。
virtual void set(const numeric_index_type i, const numeric_index_type j, const T value) override
设置矩阵元素值。
这是一个通用的稀疏矩阵类。该类包含了必须在派生类中覆盖的纯虚拟成员。 使用一个公共的基类允许从不同的求解器包中以不同的格式统一访问稀疏矩阵。
dof_id_type numeric_index_type
bool _is_initialized
Flag that tells if init() has been called.
unsigned int m() const
返回矩阵的行维度。
bool _is_initialized
标志,指示矩阵是否已初始化。
virtual numeric_index_type m() const override
获取全局行数。
virtual numeric_index_type m() const =0
void swap(EpetraMatrix< T > &other)
交换内部数据指针,不交换实际值。
std::vector< T > & get_values()
返回对应于存储向量的引用的底层数据存储矢量。
virtual numeric_index_type row_start() const override
获取本地行索引的起始位置。
virtual void close() override
关闭矩阵,使其无法再次修改。
virtual void print_personal(std::ostream &os=libMesh::out) const override
打印矩阵的个性化信息。
EpetraMatrix(const Parallel::Communicator &comm)
构造函数; 初始化矩阵为空,没有任何结构。矩阵不可用。仅用于成员类的矩阵。 所有其他矩阵应在所有必要信息都可用的数据流的某个点创建。
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual Real l1_norm() const override
获取矩阵的L1范数。
virtual Real linfty_norm() const override
获取矩阵的L∞范数。
virtual void get_diagonal(NumericVector< T > &dest) const override
获取矩阵的对角线元素。
virtual bool closed() const override
检查矩阵是否已关闭。
virtual void zero() override
将矩阵元素设置为零。
virtual void clear() noexceptoverride
清除矩阵。
bool initialized()
Checks that library initialization has been done.
int * numeric_trilinos_cast(const numeric_index_type *p)
Epetra_FECrsMatrix * _mat
实际Epetra数据类型来保存矩阵条目。
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value) override
添加矩阵元素值。
Epetra_Vector * vec()
返回原始的 Epetra_Vector 指针。
定义用于有限元类型计算的密集矩阵。 用于在求和成全局矩阵之前存储单元刚度矩阵。所有被覆盖的虚函数都记录在dense_matrix_base.h中。
此类提供了对Epetra数据结构的并行、稀疏矩阵的友好接口。所有重写的虚拟函数在sparse_matrix.h中都有文档。
virtual T operator()(const numeric_index_type i, const numeric_index_type j) const override
获取矩阵元素值。
virtual numeric_index_type n() const =0