32 static Epetra_SerialComm seq_comm;
34 template<
typename Scalar>
35 EpetraMatrix<Scalar>::EpetraMatrix()
38 this->mat_im =
nullptr;
40 this->std_map =
nullptr;
44 template<
typename Scalar>
45 EpetraMatrix<Scalar>::EpetraMatrix(
const EpetraMatrix<Scalar> &op) : SparseMatrix<Scalar>(op)
47 this->mat =
new Epetra_CrsMatrix(*op.mat);
49 this->mat_im =
new Epetra_CrsMatrix(*op.mat_im);
51 this->mat_im =
nullptr;
53 assert(mat !=
nullptr);
54 this->grph =
new Epetra_CrsGraph(this->mat->Graph());
55 this->std_map =
new Epetra_BlockMap(this->grph->Map());
59 template<
typename Scalar>
60 EpetraMatrix<Scalar>::EpetraMatrix(Epetra_RowMatrix &op)
62 this->mat =
dynamic_cast<Epetra_CrsMatrix*
>(&op);
63 assert(mat !=
nullptr);
65 this->size = this->mat->NumGlobalRows();
66 this->grph =
const_cast<Epetra_CrsGraph*
>(&this->mat->Graph());
67 this->std_map =
const_cast<Epetra_BlockMap*
>(&this->grph->Map());
71 template<
typename Scalar>
72 EpetraMatrix<Scalar>::~EpetraMatrix()
77 template<
typename Scalar>
82 std_map =
new Epetra_Map(n, 0, seq_comm);
83 grph =
new Epetra_CrsGraph(Copy, *std_map, 0);
84 mat =
new Epetra_CrsMatrix(Copy, *grph);
86 mat_im =
new Epetra_CrsMatrix(Copy, *grph);
89 template<
typename Scalar>
92 int col_to_pass = col;
93 grph->InsertGlobalIndices(row, 1, &col_to_pass);
106 mat_im->FillComplete();
109 template<
typename Scalar>
113 grph->FillComplete();
116 mat =
new Epetra_CrsMatrix(Copy, *grph);
122 mat_im =
new Epetra_CrsMatrix(Copy, *grph);
131 delete mat; mat =
nullptr;
132 delete grph; grph =
nullptr;
133 delete std_map; std_map =
nullptr;
142 delete mat; mat =
nullptr;
143 delete mat_im; mat_im =
nullptr;
144 delete grph; grph =
nullptr;
145 delete std_map; std_map =
nullptr;
149 template<
typename Scalar>
152 int n_entries = mat->NumGlobalEntries(m);
153 std::vector<double> vals(n_entries);
154 std::vector<int> idxs(n_entries);
155 mat->ExtractGlobalRowCopy(m, n_entries, n_entries, &vals[0], &idxs[0]);
156 for (
int i = 0; i < n_entries; i++)
157 if (idxs[i] == (
int)n)
162 template<
typename Scalar>
165 return mat->NumGlobalEntries(row);
168 template<
typename Scalar>
169 void EpetraMatrix<Scalar>::extract_row_copy(
unsigned int row,
unsigned int len,
unsigned int &n_entries,
double *vals,
unsigned int *idxs)
171 int* idxs_to_pass = malloc_with_check<EpetraMatrix<Scalar>,
int>(len,
this);
172 for (
unsigned int i = 0; i < len; i++)
173 idxs_to_pass[i] = idxs[i];
174 int n_entries_to_pass = n_entries;
175 mat->ExtractGlobalRowCopy(row, len, n_entries_to_pass, vals, idxs_to_pass);
176 free_with_check(idxs_to_pass);
189 mat_im->PutScalar(0.0);
198 #pragma omp critical (EpetraMatrixAdd)
200 int ierr = mat->SumIntoMyValues(m, 1, &v, &n_to_pass);
208 void EpetraMatrix<std::complex<double> >::add(
unsigned int m,
unsigned int n, std::complex<double> v)
212 double v_r = std::real<double>(v);
214 int ierr = mat->SumIntoMyValues(m, 1, &v_r, &n_to_pass);
217 double v_i = std::imag<double>(v);
218 ierr = mat_im->SumIntoMyValues(m, 1, &v_i, &n_to_pass);
224 void EpetraMatrix<std::complex<double> >::multiply_with_vector(std::complex<double>* vector_in, std::complex<double>*& vector_out,
bool vector_out_initialized)
const
226 SparseMatrix<std::complex<double> >::multiply_with_vector(vector_in, vector_out, vector_out_initialized);
232 Epetra_Vector x(View, mat->OperatorDomainMap(), vector_in);
233 Epetra_Vector y(mat->OperatorRangeMap());
234 mat->Multiply(
false, x, y);
235 y.ExtractCopy(vector_out);
238 template<
typename Scalar>
241 for (
unsigned int i = 0; i < m; i++)
242 for (
unsigned int j = 0; j < n; j++)
243 if (rows[i] >= 0 && cols[j] >= 0)
244 add(rows[i], cols[j], mat[i * size + j]);
254 void EpetraMatrix<std::complex<double> >::multiply_with_Scalar(std::complex<double> value)
259 template<
typename Scalar>
265 EpetraExt::MatrixMatrix::Add(*ep_mat->mat,
false, 1.0, *this->mat, 1.0);
266 if (this->mat_im && ep_mat->
mat_im)
267 EpetraExt::MatrixMatrix::Add(*ep_mat->
mat_im,
false, 1.0, *this->mat_im, 1.0);
276 case EXPORT_FORMAT_MATLAB_SIMPLE:
278 file = fopen(filename,
"w");
279 EpetraExt::RowMatrixToHandle(file, *this->mat);
292 template<
typename Scalar>
298 template<
typename Scalar>
301 return mat->NumGlobalNonzeros() / ((double)this->size*this->size);
304 template<
typename Scalar>
307 return mat->NumGlobalNonzeros();
310 template<
typename Scalar>
313 this->std_map =
nullptr;
315 this->vec_im =
nullptr;
320 template<
typename Scalar>
321 EpetraVector<Scalar>::EpetraVector(
const Epetra_Vector &v)
323 this->vec = (Epetra_Vector *)&v;
324 this->vec_im =
nullptr;
325 this->std_map = (Epetra_BlockMap *)&v.Map();
326 this->size = v.MyLength();
330 template<
typename Scalar>
331 EpetraVector<Scalar>::~EpetraVector()
336 template<
typename Scalar>
343 std_map =
new Epetra_Map(this->size, 0, seq_comm);
344 vec =
new Epetra_Vector(*std_map);
345 vec_im =
new Epetra_Vector(*std_map);
348 assert(this->get_size() == n);
352 template<
typename Scalar>
355 for (
unsigned int i = 0; i < this->size; i++) (*vec)[i] = 0.0;
357 for (
unsigned int i = 0; i < this->size; i++) (*vec_im)[i] = 0.0;
360 template<
typename Scalar>
363 for (
unsigned int i = 0; i < this->size; i++)
365 for (
unsigned int i = 0; i < this->size; i++)
370 template<
typename Scalar>
374 new_vector->
size = this->get_size();
376 new_vector->vec =
new Epetra_Vector(*this->vec);
377 new_vector->std_map =
new Epetra_BlockMap(*this->std_map);
378 new_vector->
vec_im =
new Epetra_Vector(*this->vec_im);
383 template<
typename Scalar>
388 delete std_map; std_map =
nullptr;
389 delete vec; vec =
nullptr;
390 delete vec_im; vec_im =
nullptr;
402 void EpetraVector<std::complex<double> >::set(
unsigned int idx, std::complex<double> y)
404 (*vec)[idx] = std::real(y);
405 (*vec_im)[idx] = std::imag(y);
415 void EpetraVector<std::complex<double> >::add(
unsigned int idx, std::complex<double> y)
417 (*vec)[idx] += std::real(y);
418 (*vec_im)[idx] += std::imag(y);
421 template<
typename Scalar>
424 for (
unsigned int i = 0; i < n; i++)
428 template<
typename Scalar>
434 template<
typename Scalar>
438 vec->ExtractCopy((
double *)v);
441 template<
typename Scalar>
447 EpetraExt::VectorToMatrixMarketFile(filename, *this->vec);
450 case EXPORT_FORMAT_MATLAB_SIMPLE:
452 EpetraExt::VectorToMatlabFile(filename, *this->vec);
General namespace for the Hermes library.
virtual void free()
free the memory associated with stiffness matrix
virtual void finish()
Finish manipulation with matrix (called before solving)
virtual void free()
free the memory
Exception interface Basically a std::exception, but with a constructor with string and with print_msg...
General (abstract) vector representation in Hermes.
virtual void zero()
Zero the vector.
virtual void add(unsigned int m, unsigned int n, Scalar v)
virtual Scalar get(unsigned int m, unsigned int n) const
General (abstract) sparse matrix representation in Hermes.
virtual void alloc()
allocate the memory for stiffness matrix
MatrixExportFormat
Format of file matrix and vector output.
virtual void alloc(unsigned int ndofs)
Plain ascii file lines contains row column and value.
virtual void set(unsigned int idx, Scalar y)
virtual Scalar get(unsigned int idx) const
virtual void prealloc(unsigned int n)
File containing functionality for investigating call stack.
virtual void add_sparse_matrix(SparseMatrix< Scalar > *mat)
virtual double get_fill_in() const
Get fill-in.
virtual void pre_add_ij(unsigned int row, unsigned int col)
Epetra_Vector * vec_im
Imaginary part of the vector, vec holds the real part.
virtual Vector< Scalar > * change_sign()
Multiply by minus one.
virtual void add(unsigned int idx, Scalar y)
EpetraMatrix and EpetraVector storage classes for Amesos, AztecOO, ... .
virtual void extract(Scalar *v) const
Epetra_CrsMatrix * mat_im
Imaginary part of the matrix, mat holds the real part.
Vector< Scalar > * duplicate() const
Duplicates a matrix (including allocation).
virtual void zero()
Zero the matrix.
virtual unsigned int get_nnz() const
Matrix Market which can be read by pysparse library.
virtual void multiply_with_Scalar(Scalar value)
Multiply with a Scalar.
virtual void multiply_with_vector(Scalar *vector_in, Scalar *&vector_out, bool vector_out_initialized=false) const
Multiply with a vector.
virtual void export_to_file(const char *filename, const char *var_name, MatrixExportFormat fmt, char *number_format="%lf")
Method is not overriden and should be.
unsigned int size
size of vector
virtual void export_to_file(const char *filename, const char *var_name, MatrixExportFormat fmt, char *number_format="%lf")