HermesCommon  3.0
algebra_mixins.cpp
Go to the documentation of this file.
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.
22 #include "algebra_mixins.h"
23 #include "vector.h"
24 #include "util/memory_handling.h"
25 #include "matrix.h"
26 
27 namespace Hermes
28 {
29  namespace Algebra
30  {
31  namespace Mixins
32  {
33  template<typename Scalar>
34  MatrixRhsOutput<Scalar>::MatrixRhsOutput() : output_matrixOn(false), output_matrixIterations(-1), matrixFilename("Matrix"),
35  matrixVarname("A"), matrixFormat(Hermes::Algebra::EXPORT_FORMAT_PLAIN_ASCII), matrix_number_format("%lf"), output_rhsOn(false), output_rhsIterations(-1),
36  RhsFilename("Rhs"), RhsVarname("b"), RhsFormat(Hermes::Algebra::EXPORT_FORMAT_PLAIN_ASCII), rhs_number_format("%lf")
37  {
38  }
39 
40  template<typename Scalar>
42  {
43  if (matrix == nullptr)
44  return;
45 
46  if (this->output_matrixOn)
47  {
48  char* fileName = malloc_with_check<char>(this->matrixFilename.length() + 5);
49  if (this->only_lastMatrixIteration)
50  sprintf(fileName, "%s", this->matrixFilename.c_str());
51  else if (this->output_matrixIterations == -1 || this->output_matrixIterations >= iteration)
52  sprintf(fileName, "%s_%i", this->matrixFilename.c_str(), iteration);
53  else
54  {
55  free_with_check(fileName);
56  return;
57  }
58 
59  matrix->export_to_file(fileName, this->matrixVarname.c_str(), this->matrixFormat, this->matrix_number_format);
60  free_with_check(fileName);
61  }
62  }
63 
64  template<typename Scalar>
66  {
67  if (matrix == nullptr)
68  return;
69 
70  if (this->output_matrixOn)
71  {
72  char* fileName = malloc_with_check<char>(this->matrixFilename.length() + 5);
73  sprintf(fileName, "%s", this->matrixFilename.c_str());
74  matrix->export_to_file(fileName, this->matrixVarname.c_str(), this->matrixFormat, this->matrix_number_format);
75  free_with_check(fileName);
76  }
77  }
78 
79  template<typename Scalar>
81  {
82  if (rhs == nullptr)
83  return;
84 
85  if (this->output_rhsOn)
86  {
87  char* fileName = malloc_with_check<char>(this->RhsFilename.length() + 5);
88  if (this->only_lastRhsIteration)
89  sprintf(fileName, "%s", this->RhsFilename.c_str());
90  else if (this->output_rhsIterations == -1 || this->output_rhsIterations >= iteration)
91  sprintf(fileName, "%s_%i", this->RhsFilename.c_str(), iteration);
92  else
93  {
94  free_with_check(fileName);
95  return;
96  }
97 
98  rhs->export_to_file(fileName, this->RhsVarname.c_str(), this->RhsFormat, this->rhs_number_format);
99  free_with_check(fileName);
100  }
101  }
102 
103  template<typename Scalar>
105  {
106  if (rhs == nullptr)
107  return;
108 
109  if (this->output_rhsOn)
110  {
111  char* fileName = malloc_with_check<char>(this->RhsFilename.length() + 5);
112  sprintf(fileName, "%s", this->RhsFilename.c_str());
113  rhs->export_to_file(fileName, this->RhsVarname.c_str(), this->RhsFormat, this->rhs_number_format);
114  free_with_check(fileName);
115  }
116  }
117 
118  template<typename Scalar>
119  void MatrixRhsOutput<Scalar>::output_matrix(bool only_last_iteration, int firstIterations)
120  {
121  output_matrixOn = true;
122  this->only_lastMatrixIteration = only_last_iteration;
123  this->output_matrixIterations = firstIterations;
124  }
125  template<typename Scalar>
127  {
128  this->matrixFilename = name;
129  }
130 
131  template<typename Scalar>
133  {
134  this->print_matrix_zero_values = to_set;
135  }
136 
137  template<typename Scalar>
139  {
140  this->matrixVarname = name;
141  }
142  template<typename Scalar>
144  {
145  this->matrixFormat = format;
146  }
147 
148  template<typename Scalar>
150  {
151  this->matrix_number_format = number_format;
152  }
153 
154  template<typename Scalar>
155  void MatrixRhsOutput<Scalar>::output_rhs(bool only_last_iteration, int firstIterations)
156  {
157  this->output_rhsOn = true;
158  this->only_lastRhsIteration = only_last_iteration;
159  this->output_rhsIterations = firstIterations;
160  }
161  template<typename Scalar>
163  {
164  this->RhsFilename = name;
165  }
166  template<typename Scalar>
168  {
169  this->RhsVarname = name;
170  }
171  template<typename Scalar>
173  {
174  this->RhsFormat = format;
175  }
176  template<typename Scalar>
178  {
179  this->rhs_number_format = number_format;
180  }
181 
182  template<typename Scalar>
183  void MatrixRhsImportExport<Scalar>::export_to_file(std::string filename, const char *var_name, MatrixExportFormat fmt, char* number_format)
184  {
185  this->export_to_file(filename.c_str(), var_name, fmt, number_format);
186  }
187 
188  template<typename Scalar>
189  void MatrixRhsImportExport<Scalar>::export_to_file(std::string filename, std::string var_name, MatrixExportFormat fmt, char* number_format)
190  {
191  this->export_to_file(filename.c_str(), var_name.c_str(), fmt, number_format);
192  }
193 
194  template<typename Scalar>
195  void MatrixRhsImportExport<Scalar>::import_from_file(std::string filename, const char *var_name, MatrixExportFormat fmt)
196  {
197  this->import_from_file(filename.c_str(), var_name, fmt);
198  }
199 
200  template<typename Scalar>
201  void MatrixRhsImportExport<Scalar>::import_from_file(std::string filename, std::string var_name, MatrixExportFormat fmt)
202  {
203  this->import_from_file(filename.c_str(), var_name.c_str(), fmt);
204  }
205 
206  template HERMES_API class MatrixRhsOutput < double > ;
207  template HERMES_API class MatrixRhsOutput < std::complex<double> > ;
208  template HERMES_API class MatrixRhsImportExport < double > ;
209  template HERMES_API class MatrixRhsImportExport < std::complex<double> > ;
210  }
211  }
212 }
General namespace for the Hermes library.
General (abstract) vector representation in Hermes.
virtual void export_to_file(const char *filename, const char *var_name, Algebra::MatrixExportFormat fmt, char *number_format="%lf")=0
void output_rhs(bool only_last_iteration=true, int firstIterations=-1)
virtual void import_from_file(const char *filename, const char *var_name, Algebra::MatrixExportFormat fmt)
File containing common definitions, and basic global enums etc. for HermesCommon. ...
void set_matrix_export_format(Hermes::Algebra::MatrixExportFormat format)
void process_vector_output(Hermes::Algebra::Vector< Scalar > *rhs, int iteration)
Processes the matrix.
General (abstract) sparse matrix representation in Hermes.
MatrixExportFormat
Format of file matrix and vector output.
Plain ascii file lines contains row column and value.
void set_matrix_number_format(char *number_format)
Basic matrix classes and operations.
Mixins classes for algebraic purposes.
void set_print_zero_matrix_entries(bool to_set)
Sets this instance to output matrix entries even though they are zero or not.
Basic vector classes and operations.
void set_rhs_export_format(Hermes::Algebra::MatrixExportFormat format)
void process_matrix_output(Hermes::Algebra::SparseMatrix< Scalar > *matrix, int iteration)
Processes the matrix.
void output_matrix(bool only_last_iteration=true, int firstIterations=-1)
void set_rhs_number_format(char *number_format)