HermesCommon  3.0
matrix_solver.cpp
1 // This file is part of HermesCommon
2 //
3 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
4 // Email: hpfem-group@unr.edu, home page: http://www.hpfem.org/.
5 //
6 // Hermes2D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the License,
9 // or (at your option) any later version.
10 //
11 // Hermes2D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Hermes2D; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20 #include "matrix_solver.h"
21 #ifdef WITH_UMFPACK
23 #endif
24 
25 namespace Hermes
26 {
27  namespace Solvers
28  {
29  template<typename Scalar>
30  MatrixSolver<Scalar>::MatrixSolver(bool force_use_direct_solver) : Hermes::Mixins::Loggable(true)
31  {
33  SparseMatrix<Scalar>* A = create_matrix<Scalar>(force_use_direct_solver);
34  Vector<Scalar>* b = create_vector<Scalar>(force_use_direct_solver);
35  this->linear_matrix_solver = create_linear_solver<Scalar>(A, b, force_use_direct_solver);
36 
37  this->constant_jacobian = false;
38 
39  this->jacobian_reusable = false;
40 
41  this->problem_size = -1;
42 
43 #ifdef WITH_UMFPACK
44  this->do_UMFPACK_reporting = false;
45 #endif
46 
47  this->sln_vector = nullptr;
48  }
49 
50  template<typename Scalar>
52  {
53  SparseMatrix<Scalar>* temp_matrix = linear_matrix_solver->get_matrix();
54  Vector<Scalar>* temp_rhs = linear_matrix_solver->get_rhs();
55 
56  delete this->linear_matrix_solver;
57 
58  if (temp_matrix)
59  delete temp_matrix;
60  if (temp_rhs)
61  delete temp_rhs;
62  }
63 
64  template<typename Scalar>
66  {
68  this->linear_matrix_solver->set_verbose_output(to_set);
69  }
70 
71  template<typename Scalar>
73  {
74  return this->linear_matrix_solver;
75  }
76 
77  template<typename Scalar>
79  {
80  return this->linear_matrix_solver->get_matrix();
81  }
82 
83  template<typename Scalar>
85  {
86  return this->linear_matrix_solver->get_rhs();
87  }
88 
89  template<typename Scalar>
91  {
92  return sln_vector;
93  }
94 
95  template<typename Scalar>
97  {
98  this->constant_jacobian = to_set;
99  if (!to_set)
100  this->jacobian_reusable = false;
101  }
102 
103 #ifdef WITH_UMFPACK
104  template<typename Scalar>
105  double MatrixSolver<Scalar>::get_UMFPACK_reporting_data(UMFPACK_reporting_data_value data_value)
106  {
107  return this->UMFPACK_reporting_data[data_value];
108  }
109 
110  template<typename Scalar>
111  void MatrixSolver<Scalar>::set_UMFPACK_output(bool to_set, bool with_output)
112  {
113  if (!dynamic_cast<UMFPackLinearMatrixSolver<Scalar>*>(this->linear_matrix_solver))
114  {
115  this->warn("A different MatrixSolver than UMFPACK is used, ignoring the call to set_UMFPACK_reporting().");
116  return;
117  }
118 
119  if (with_output)
120  ((UMFPackLinearMatrixSolver<Scalar>*)this->linear_matrix_solver)->set_output_level(2);
121  else
122  ((UMFPackLinearMatrixSolver<Scalar>*)this->linear_matrix_solver)->set_output_level(0);
123 
124  this->do_UMFPACK_reporting = to_set;
125  }
126 
127  template<typename Scalar>
129  {
130  if (this->do_UMFPACK_reporting)
131  {
132  UMFPackLinearMatrixSolver<Scalar>* umfpack_matrix_solver = (UMFPackLinearMatrixSolver<Scalar>*)this->linear_matrix_solver;
133  if (this->linear_matrix_solver->get_used_reuse_scheme() != HERMES_REUSE_MATRIX_STRUCTURE_COMPLETELY)
134  {
135  this->UMFPACK_reporting_data[this->FactorizationSize] = umfpack_matrix_solver->Info[UMFPACK_NUMERIC_SIZE] * umfpack_matrix_solver->Info[UMFPACK_SIZE_OF_UNIT];
136  this->UMFPACK_reporting_data[this->PeakMemoryUsage] = umfpack_matrix_solver->Info[UMFPACK_PEAK_MEMORY] * umfpack_matrix_solver->Info[UMFPACK_SIZE_OF_UNIT];
137  this->UMFPACK_reporting_data[this->Flops] = umfpack_matrix_solver->Info[UMFPACK_FLOPS];
138  }
139  else
140  memset(this->UMFPACK_reporting_data, 0, 3 * sizeof(double));
141  }
142  }
143 #endif
144  template class HERMES_API MatrixSolver < double > ;
145  template class HERMES_API MatrixSolver < std::complex<double> > ;
146  }
147 }
General namespace for the Hermes library.
General (linear/nonlinear) matrix solver functionality.
virtual void set_verbose_output(bool to_set)
Verbose output.
General (abstract) vector representation in Hermes.
General (abstract) sparse matrix representation in Hermes.
MatrixSolver(bool force_use_direct_solver=false)
bool jacobian_reusable
Jacobian is ready to be reused if desirable.
Definition: matrix_solver.h:86
SparseMatrix< Scalar > * get_jacobian()
Only a shortcut for algebraic solver (->) get_matrix().
double Info[UMFPACK_INFO]
For reporting.
void handle_UMFPACK_reports()
This is not used now.
interface for both linear and nonlinear algebraic solvers.
Definition: matrix_solver.h:35
virtual double get_UMFPACK_reporting_data(UMFPACK_reporting_data_value data_value)
void set_jacobian_constant(bool to_set=true)
Sets the jacobian to be constant, i.e. reused whenever possible.
UMFPACK solver interface.
Abstract class for defining solver interface.
Scalar * sln_vector
The solution vector.
Definition: matrix_solver.h:76
Encapsulation of UMFPACK linear solver.
Vector< Scalar > * get_residual()
Only a shortcut for algebraic solver (->) get_rhs().
Hermes::Solvers::LinearMatrixSolver< Scalar > * linear_matrix_solver
Linear solver.
Definition: matrix_solver.h:80
virtual void set_verbose_output(bool to_set)
Definition: mixins.cpp:552
int problem_size
Number of equations.
Definition: matrix_solver.h:89
virtual Scalar * get_sln_vector()
Return the solution vector.
bool constant_jacobian
Jacobian can be reused if possible.
Definition: matrix_solver.h:83
virtual void set_UMFPACK_output(bool to_set=true, bool with_output=false)