33 static Epetra_SerialComm seq_comm;
35 template<
typename Scalar>
36 EpetraMatrix<Scalar>::EpetraMatrix()
44 this->row_storage =
true;
45 this->col_storage =
false;
48 template<
typename Scalar>
49 EpetraMatrix<Scalar>::EpetraMatrix(Epetra_RowMatrix &op)
51 this->mat =
dynamic_cast<Epetra_CrsMatrix *
>(&op);
53 this->grph = (Epetra_CrsGraph *) &this->mat->Graph();
54 this->std_map = (Epetra_BlockMap *) &this->grph->Map();
57 this->row_storage =
true;
58 this->col_storage =
false;
61 template<
typename Scalar>
62 EpetraMatrix<Scalar>::~EpetraMatrix()
67 template<
typename Scalar>
68 void EpetraMatrix<Scalar>::prealloc(
unsigned int n)
72 std_map =
new Epetra_Map(n, 0, seq_comm);
73 grph =
new Epetra_CrsGraph(Copy, *std_map, 0);
76 template<
typename Scalar>
77 void EpetraMatrix<Scalar>::pre_add_ij(
unsigned int row,
unsigned int col)
79 int col_to_pass = col;
80 grph->InsertGlobalIndices(row, 1, &col_to_pass);
84 void EpetraMatrix<double>::finish()
90 void EpetraMatrix<std::complex<double> >::finish()
93 mat_im->FillComplete();
97 void EpetraMatrix<double>::alloc()
101 mat =
new Epetra_CrsMatrix(Copy, *grph);
105 void EpetraMatrix<std::complex<double> >::alloc()
107 grph->FillComplete();
109 mat =
new Epetra_CrsMatrix(Copy, *grph);
110 mat_im =
new Epetra_CrsMatrix(Copy, *grph);
114 void EpetraMatrix<double>::free()
118 delete mat; mat = NULL;
119 delete grph; grph = NULL;
120 delete std_map; std_map = NULL;
125 void EpetraMatrix<std::complex<double> >::free()
129 delete mat; mat = NULL;
130 delete mat_im; mat_im = NULL;
131 delete grph; grph = NULL;
132 delete std_map; std_map = NULL;
136 template<
typename Scalar>
137 Scalar EpetraMatrix<Scalar>::get(
unsigned int m,
unsigned int n)
139 int n_entries = mat->NumGlobalEntries(m);
142 mat->ExtractGlobalRowCopy(m, n_entries, n_entries, &vals[0], &idxs[0]);
143 for (
int i = 0; i < n_entries; i++)
144 if(idxs[i] == (
int)n)
149 template<
typename Scalar>
150 int EpetraMatrix<Scalar>::get_num_row_entries(
unsigned int row)
152 return mat->NumGlobalEntries(row);
155 template<
typename Scalar>
156 void EpetraMatrix<Scalar>::extract_row_copy(
unsigned int row,
unsigned int len,
unsigned int &n_entries,
double *vals,
unsigned int *idxs)
158 int* idxs_to_pass =
new int[len];
159 for(
unsigned int i = 0; i < len; i++)
160 idxs_to_pass[i] = idxs[i];
161 int n_entries_to_pass = n_entries;
162 mat->ExtractGlobalRowCopy(row, len, n_entries_to_pass, vals, idxs_to_pass);
163 delete [] idxs_to_pass;
167 void EpetraMatrix<double>::zero()
173 void EpetraMatrix<std::complex<double> >::zero()
176 mat_im->PutScalar(0.0);
180 void EpetraMatrix<double>::add(
unsigned int m,
unsigned int n,
double v)
185 int ierr = mat->SumIntoGlobalValues(m, 1, &v, &n_to_pass);
191 void EpetraMatrix<std::complex<double> >::add(
unsigned int m,
unsigned int n, std::complex<double> v)
195 double v_r = std::real<double>(v);
197 int ierr = mat->SumIntoGlobalValues(m, 1, &v_r, &n_to_pass);
200 double v_i = std::imag<double>(v);
201 ierr = mat_im->SumIntoGlobalValues(m, 1, &v_i, &n_to_pass);
207 template<
typename Scalar>
208 void EpetraMatrix<Scalar>::add_to_diagonal(Scalar v)
210 for (
unsigned int i = 0; i < this->size; i++)
216 template<
typename Scalar>
217 void EpetraMatrix<Scalar>::add_to_diagonal_blocks(
int num_stages, EpetraMatrix<Scalar>* mat_block)
219 int ndof = mat_block->get_size();
220 if(this->get_size() != (
unsigned int) num_stages * ndof)
223 for (
int i = 0; i < num_stages; i++)
225 this->add_as_block(ndof*i, ndof*i, mat_block);
229 template<
typename Scalar>
230 void EpetraMatrix<Scalar>::add_sparse_to_diagonal_blocks(
int num_stages, SparseMatrix<Scalar>* mat)
232 add_to_diagonal_blocks(num_stages,
dynamic_cast<EpetraMatrix<Scalar>*
>(mat));
235 template<
typename Scalar>
236 void EpetraMatrix<Scalar>::add_as_block(
unsigned int i,
unsigned int j, EpetraMatrix<Scalar>* mat)
238 if((this->get_size() < i + mat->get_size() )||(this->get_size() < j + mat->get_size() ))
240 unsigned int block_size = mat->get_size();
241 for (
unsigned int r = 0; r<block_size; r++)
243 for (
unsigned int c = 0; c<block_size; c++)
245 this->add(i + r, j + c, mat->get(r, c));
250 template<
typename Scalar>
251 void EpetraMatrix<Scalar>::multiply_with_vector(Scalar* vector_in, Scalar* vector_out)
253 for (
unsigned int i = 0; i<this->size; i++)
256 for (
unsigned int j = 0; j<this->size; j++)
258 vector_out[i] +=vector_in[j]*
get(i, j);
263 template<
typename Scalar>
264 void EpetraMatrix<Scalar>::add(
unsigned int m,
unsigned int n, Scalar **mat,
int *rows,
int *cols)
266 for (
unsigned int i = 0; i < m; i++)
267 for (
unsigned int j = 0; j < n; j++)
268 if(rows[i] >= 0 && cols[j] >= 0)
269 add(rows[i], cols[j], mat[i][j]);
272 template<
typename Scalar>
273 bool EpetraMatrix<Scalar>::dump(FILE *file,
const char *var_name,
EMatrixDumpFormat fmt,
char* number_format)
278 template<
typename Scalar>
279 unsigned int EpetraMatrix<Scalar>::get_matrix_size()
const
284 template<
typename Scalar>
285 double EpetraMatrix<Scalar>::get_fill_in()
const
287 return mat->NumGlobalNonzeros() / ((double)this->size*this->size);
290 template<
typename Scalar>
291 unsigned int EpetraMatrix<Scalar>::get_nnz()
const
293 return mat->NumGlobalNonzeros();
296 template<
typename Scalar>
297 EpetraVector<Scalar>::EpetraVector()
299 this->std_map = NULL;
306 template<
typename Scalar>
307 EpetraVector<Scalar>::EpetraVector(
const Epetra_Vector &v)
309 this->vec = (Epetra_Vector *) &v;
311 this->std_map = (Epetra_BlockMap *) &v.Map();
312 this->size = v.MyLength();
316 template<
typename Scalar>
317 EpetraVector<Scalar>::~EpetraVector()
322 template<
typename Scalar>
323 void EpetraVector<Scalar>::alloc(
unsigned int n)
327 std_map =
new Epetra_Map(this->size, 0, seq_comm);
328 vec =
new Epetra_Vector(*std_map);
329 vec_im =
new Epetra_Vector(*std_map);
333 template<
typename Scalar>
334 void EpetraVector<Scalar>::zero()
336 for (
unsigned int i = 0; i < this->size; i++) (*vec)[i] = 0.0;
338 for (
unsigned int i = 0; i < this->size; i++) (*vec_im)[i] = 0.0;
341 template<
typename Scalar>
342 void EpetraVector<Scalar>::change_sign()
344 for (
unsigned int i = 0; i < this->size; i++) (*vec)[i] *= -1.;
345 for (
unsigned int i = 0; i < this->size; i++) (*vec_im)[i] *= -1.;
348 template<
typename Scalar>
349 void EpetraVector<Scalar>::free()
353 delete std_map; std_map = NULL;
354 delete vec; vec = NULL;
355 delete vec_im; vec_im = NULL;
361 void EpetraVector<double>::set(
unsigned int idx,
double y)
367 void EpetraVector<std::complex<double> >::set(
unsigned int idx, std::complex<double> y)
369 (*vec)[idx] = std::real(y);
370 (*vec_im)[idx] = std::imag(y);
374 void EpetraVector<double>::add(
unsigned int idx,
double y)
380 void EpetraVector<std::complex<double> >::add(
unsigned int idx, std::complex<double> y)
382 (*vec)[idx] += std::real(y);
383 (*vec_im)[idx] += std::imag(y);
386 template<
typename Scalar>
387 void EpetraVector<Scalar>::add(
unsigned int n,
unsigned int *idx, Scalar *y)
389 for (
unsigned int i = 0; i < n; i++)
393 template<
typename Scalar>
394 Scalar EpetraVector<Scalar>::get(
unsigned int idx)
399 template<
typename Scalar>
400 void EpetraVector<Scalar>::extract(Scalar *v)
const
402 vec->ExtractCopy((
double *)v);
405 template<
typename Scalar>
406 void EpetraVector<Scalar>::add_vector(Vector<Scalar>* vec)
408 assert(this->length() == vec->length());
409 for (
unsigned int i = 0; i < this->length(); i++) this->add(i, vec->get(i));
412 template<
typename Scalar>
413 void EpetraVector<Scalar>::add_vector(Scalar* vec)
415 for (
unsigned int i = 0; i < this->length(); i++) this->add(i, vec[i]);
418 template<
typename Scalar>
419 bool EpetraVector<Scalar>::dump(FILE *file,
const char *var_name,
EMatrixDumpFormat fmt,
char* number_format)
424 template class HERMES_API EpetraMatrix<double>;
425 template class HERMES_API EpetraMatrix<std::complex<double> >;
426 template class HERMES_API EpetraVector<double>;
427 template class HERMES_API EpetraVector<std::complex<double> >;