46 if (!oomph_vec.
built())
48 std::ostringstream error_message;
49 error_message <<
"The oomph-lib vector (oomph_v) must be setup.";
51 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
79 double* v_pt =
const_cast<double*
>(oomph_vec.
values_pt());
80 Epetra_Vector* epetra_vec_pt =
81 new Epetra_Vector(Copy, *epetra_map_pt, v_pt + offset);
105 if (!dist_pt->
built())
107 std::ostringstream error_message;
108 error_message <<
"The LinearAlgebraDistribution dist_pt must be setup.";
110 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
128 Epetra_Vector* epetra_vec_pt =
new Epetra_Vector(*epetra_map_pt,
false);
131 delete epetra_map_pt;
132 delete target_dist_pt;
135 return epetra_vec_pt;
151 if (!oomph_vec.
built())
153 std::ostringstream error_message;
154 error_message <<
"The oomph-lib vector (oomph_v) must be setup.";
156 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
165 Epetra_Vector* epetra_vec_pt =
166 new Epetra_Vector(View, *epetra_map_pt, v_pt);
169 delete epetra_map_pt;
172 return epetra_vec_pt;
181 const Epetra_Vector* epetra_vec_pt,
DoubleVector& oomph_vec)
185 if (!oomph_vec.
built())
187 std::ostringstream error_message;
188 error_message <<
"The oomph-lib vector (oomph_v) must be setup.";
190 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
199 epetra_vec_pt->ExtractView(&v_values);
203 for (
unsigned i = 0;
i < nrow_local;
i++)
205 oomph_vec[
i] = v_values[
i];
214 int nproc = epetra_vec_pt->Map().Comm().NumProc();
217 epetra_vec_pt->ExtractCopy(oomph_vec.
values_pt());
222 double* local_values;
223 epetra_vec_pt->ExtractView(&local_values);
226 int my_rank = epetra_vec_pt->Map().Comm().MyPID();
230 nrow_local[my_rank] = epetra_vec_pt->MyLength();
233 int my_nrow_local_copy = nrow_local[my_rank];
245 first_row[my_rank] = epetra_vec_pt->Map().MyGlobalElements()[0];
248 int my_first_row = first_row[my_rank];
270 epetra_vec_pt->ExtractCopy(oomph_vec.
values_pt());
294 if (!oomph_matrix_pt->
built())
296 std::ostringstream error_message;
297 error_message <<
"The oomph-lib matrix must be built.";
299 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
301 if (!oomph_matrix_pt->
built())
303 std::ostringstream error_message;
304 error_message <<
"The oomph-lib matrix must be built.";
306 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
308 if (!oomph_matrix_pt->
built())
310 std::ostringstream error_message;
311 error_message <<
"The oomph-lib matrix must be built.";
313 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
320 int* column =
const_cast<int*
>(oomph_matrix_pt->
column_index());
321 double* value =
const_cast<double*
>(oomph_matrix_pt->
value());
322 int* row_start =
const_cast<int*
>(oomph_matrix_pt->
row_start());
335 oomph_matrix_pt->
nrow(),
349 unsigned nrow_local = target_dist_pt->
nrow_local();
350 unsigned first_row = target_dist_pt->
first_row();
353 int* nnz_per_row =
new int[nrow_local];
354 for (
unsigned row = 0; row < nrow_local; row++)
356 nnz_per_row[row] = row_start[row + offset + 1] - row_start[offset + row];
360 Epetra_CrsMatrix* epetra_matrix_pt =
361 new Epetra_CrsMatrix(Copy, *epetra_map_pt, nnz_per_row,
true);
364 for (
unsigned row = 0; row < nrow_local; row++)
367 int ptr = row_start[row + offset];
372 epetra_matrix_pt->InsertGlobalValues(
373 first_row + row, nnz_per_row[row], value + ptr, column + ptr);
377 std::ostringstream error_message;
379 <<
"Epetra Matrix Insert Global Values : epetra_error_flag = " << err;
381 OOMPH_CURRENT_FUNCTION,
382 OOMPH_EXCEPTION_LOCATION);
403 epetra_matrix_pt->FillComplete(*epetra_domain_map_pt, *epetra_map_pt);
407 std::ostringstream error_message;
409 <<
"Epetra Matrix Fill Complete Error : epetra_error_flag = " << err;
411 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
416 delete[] nnz_per_row;
417 delete epetra_map_pt;
418 delete epetra_domain_map_pt;
419 delete target_dist_pt;
420 delete target_col_dist_pt;
423 return epetra_matrix_pt;
478 if (!oomph_matrix_pt->
built())
480 std::ostringstream error_message;
481 error_message <<
"The oomph-lib matrix must be built.";
483 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
490 int* column =
const_cast<int*
>(oomph_matrix_pt->
column_index());
491 double* value =
const_cast<double*
>(oomph_matrix_pt->
value());
492 int* row_start =
const_cast<int*
>(oomph_matrix_pt->
row_start());
505 oomph_matrix_pt->
nrow(),
513 int first_col = oomph_matrix_pt->
first_row();
514 int ncol_local = oomph_matrix_pt->
nrow_local();
517 Epetra_Map* epetra_col_map_pt = 0;
520 std::vector<int> col_index_vector;
521 col_index_vector.reserve(oomph_matrix_pt->
nnz() + ncol_local);
522 col_index_vector.resize(ncol_local);
525 for (
int c = 0; c < ncol_local; ++c)
527 col_index_vector[c] = c + first_col;
531 std::vector<int>::iterator mid = col_index_vector.end();
534 col_index_vector.insert(mid, column, column + oomph_matrix_pt->
nnz());
538 std::vector<int>::iterator end =
540 col_index_vector.end(),
547 end = std::unique(mid, end);
550 epetra_col_map_pt =
new Epetra_Map(
552 end - col_index_vector.begin(),
553 &col_index_vector[0],
559 std::vector<int>().swap(col_index_vector);
564 int ncol = oomph_matrix_pt->
ncol();
565 Epetra_Map* epetra_col_map_pt =
566 new Epetra_LocalMap(ncol, 0, Epetra_SerialComm());
579 unsigned nrow_local = target_dist_pt->
nrow_local();
580 unsigned first_row = target_dist_pt->
first_row();
583 int* nnz_per_row =
new int[nrow_local];
584 for (
unsigned row = 0; row < nrow_local; ++row)
586 nnz_per_row[row] = row_start[row + offset + 1] - row_start[offset + row];
590 Epetra_CrsMatrix* epetra_matrix_pt =
new Epetra_CrsMatrix(
591 Copy, *epetra_map_pt, *epetra_col_map_pt, nnz_per_row,
true);
594 for (
unsigned row = 0; row < nrow_local; row++)
597 int ptr = row_start[row + offset];
602 epetra_matrix_pt->InsertGlobalValues(
603 first_row + row, nnz_per_row[row], value + ptr, column + ptr);
607 std::ostringstream error_message;
609 <<
"Epetra Matrix Insert Global Values : epetra_error_flag = " << err;
611 OOMPH_CURRENT_FUNCTION,
612 OOMPH_EXCEPTION_LOCATION);
622 epetra_matrix_pt->FillComplete();
627 std::ostringstream error_message;
629 <<
"Epetra Matrix Fill Complete Error : epetra_error_flag = " << err;
631 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
636 delete[] nnz_per_row;
637 delete epetra_map_pt;
638 delete epetra_col_map_pt;
639 delete target_dist_pt;
642 return epetra_matrix_pt;
663 if (!oomph_matrix_pt->
built())
665 std::ostringstream error_message_stream;
666 error_message_stream <<
"This matrix has not been built";
668 OOMPH_CURRENT_FUNCTION,
669 OOMPH_EXCEPTION_LOCATION);
677 std::ostringstream error_message_stream;
679 <<
"The soln vector and this matrix must have the same distribution.";
681 OOMPH_CURRENT_FUNCTION,
682 OOMPH_EXCEPTION_LOCATION);
687 if (!oomph_x.
built())
689 std::ostringstream error_message_stream;
690 error_message_stream <<
"The x vector must be setup";
692 OOMPH_CURRENT_FUNCTION,
693 OOMPH_EXCEPTION_LOCATION);
716 int epetra_error_flag = 0;
719 epetra_matrix_pt->Multiply(
false, *epetra_x_pt, *epetra_y_pt);
726 if (epetra_error_flag != 0)
728 std::ostringstream error_message;
730 <<
"Epetra Matrix Vector Multiply Error : epetra_error_flag = "
731 << epetra_error_flag;
733 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
738 delete epetra_matrix_pt;
760 if (!matrix_1.
built())
762 std::ostringstream error_message_stream;
763 error_message_stream <<
"This matrix matrix_1 has not been built";
765 OOMPH_CURRENT_FUNCTION,
766 OOMPH_EXCEPTION_LOCATION);
769 if (!matrix_2.
built())
771 std::ostringstream error_message_stream;
772 error_message_stream <<
"This matrix matrix_2 has not been built";
774 OOMPH_CURRENT_FUNCTION,
775 OOMPH_EXCEPTION_LOCATION);
778 if (matrix_1.
ncol() != matrix_2.
nrow())
780 std::ostringstream error_message;
782 <<
"Matrix dimensions incompatible for matrix-matrix multiplication"
783 <<
"ncol() for first matrix: " << matrix_1.
ncol()
784 <<
"nrow() for second matrix: " << matrix_2.
nrow();
786 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
793 std::ostringstream error_message;
795 <<
"Matrix dimensions incompatible for matrix-matrix multiplication"
796 <<
"ncol() for first matrix: " << matrix_1.
ncol()
797 <<
"nrow() for second matrix: " << matrix_2.
nrow();
799 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
806 std::ostringstream error_message_stream;
807 error_message_stream <<
"The solution matrix and matrix_1 must have "
808 "the same distribution.";
810 OOMPH_CURRENT_FUNCTION,
811 OOMPH_EXCEPTION_LOCATION);
825 bool temp_use_ml =
false;
827 (matrix_1.
ncol() == matrix_2.
ncol()))
829 temp_use_ml = use_ml;
833 Epetra_CrsMatrix* epetra_matrix_1_pt =
839 Epetra_CrsMatrix* epetra_matrix_2_pt =
844 Epetra_CrsMatrix* solution_pt;
860 solution_pt = Epetra_MatrixMult(epetra_matrix_1_pt, epetra_matrix_2_pt);
865 solution_pt =
new Epetra_CrsMatrix(Copy, epetra_matrix_1_pt->RowMap(), 0);
867 int epetra_error_flag = 0;
870 EpetraExt::MatrixMatrix::Multiply(
871 *epetra_matrix_1_pt,
false, *epetra_matrix_2_pt,
false, *solution_pt);
873 if (epetra_error_flag != 0)
875 std::ostringstream error_message;
876 error_message <<
"error flag from Multiply(): " << epetra_error_flag
877 <<
" from TrilinosHelpers::multiply" << std::endl;
879 OOMPH_CURRENT_FUNCTION,
880 OOMPH_EXCEPTION_LOCATION);
889 int nnz_local = solution_pt->NumMyNonzeros();
895 if ((
int)matrix_1.
nrow() != solution_pt->NumGlobalRows())
897 std::ostringstream error_message;
898 error_message <<
"Incorrect number of global rows in solution matrix. "
899 <<
"nrow() for first input matrix: " << matrix_1.
nrow()
900 <<
" nrow() for solution: " << solution_pt->NumGlobalRows();
902 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
906 if (
static_cast<int>(matrix_1.
nrow_local()) != solution_pt->NumMyRows())
908 std::ostringstream error_message;
909 error_message <<
"Incorrect number of local rows in solution matrix. "
910 <<
"nrow_local() for first input matrix: "
911 << matrix_1.
nrow_local() <<
" nrow_local() for solution: "
912 << solution_pt->NumMyRows();
914 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
918 if ((
int)matrix_2.
ncol() != solution_pt->NumGlobalCols())
920 std::ostringstream error_message;
921 error_message <<
"Incorrect number of global columns in solution matrix. "
922 <<
"ncol() for second input matrix: " << matrix_2.
ncol()
923 <<
" ncol() for solution: " << solution_pt->NumGlobalCols();
925 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
929 if (
static_cast<int>(matrix_1.
first_row()) != solution_pt->GRID(0))
931 std::ostringstream error_message;
933 <<
"Incorrect global index for first row of solution matrix. "
934 <<
"first_row() for first input matrix : " << matrix_1.
first_row()
935 <<
" first_row() for solution: " << solution_pt->GRID(0);
937 error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
942 double* value =
new double[nnz_local];
943 int* column_index =
new int[nnz_local];
944 int* row_start =
new int[nrow_local + 1];
949 for (
int row = first; row < last; row++)
951 row_start[row - first] = ptr;
952 solution_pt->ExtractGlobalRowCopy(
953 row, nnz_local, num_entries, value + ptr, column_index + ptr);
956 row_start[nrow_local] = ptr;
959 delete epetra_matrix_1_pt;
960 delete epetra_matrix_2_pt;
966 matrix_2.
ncol(), nnz_local, value, column_index, row_start);
983 unsigned first_row = dist_pt->
first_row();
985 int* my_global_rows =
new int[nrow_local];
986 for (
unsigned i = 0;
i < nrow_local; ++
i)
988 my_global_rows[
i] = first_row +
i;
990 Epetra_Map* epetra_map_pt =
991 new Epetra_Map(dist_pt->
nrow(),
996 delete[] my_global_rows;
997 return epetra_map_pt;
1001 return new Epetra_LocalMap(
1002 int(dist_pt->
nrow()),
1007 return new Epetra_LocalMap(
1008 int(dist_pt->
nrow()),
int(0), Epetra_SerialComm());
A class for compressed row matrices. This is a distributable object.
int * column_index()
Access to C-style column index array.
int * row_start()
Access to C-style row_start array.
unsigned long ncol() const
Return the number of columns of the matrix.
unsigned long nnz() const
Return the number of nonzero entries (the local nnz)
bool built() const
access function to the Built flag - indicates whether the matrix has been build - i....
void build_without_copy(const unsigned &ncol, const unsigned &nnz, double *value, int *column_index, int *row_start)
keeps the existing distribution and just matrix that is stored without copying the matrix data
double * value()
Access to C-style value array.
unsigned long nrow() const
Return the number of rows of the matrix.
void build(const LinearAlgebraDistribution *distribution_pt, const unsigned &ncol, const Vector< double > &value, const Vector< int > &column_index, const Vector< int > &row_start)
build method: vector of values, vector of column indices, vector of row starts and number of rows and...
bool distributed() const
distribution is serial or distributed
LinearAlgebraDistribution * distribution_pt() const
access to the LinearAlgebraDistribution
unsigned nrow() const
access function to the number of global rows.
unsigned nrow_local() const
access function for the num of local rows on this processor.
unsigned first_row() const
access function for the first row on this processor
Class to allow sorting of column indices in conversion to epetra matrix.
DistributionPredicate(const int &first_col, const int &ncol_local)
Constructor: Pass number of first column and the number of local columns.
bool operator()(const int &col)
Comparison operator: is column col in the range between (including) First_col and Last_col.
int First_col
First column held locally.
int Last_col
Last colum held locally.
A vector in the mathematical sense, initially developed for linear algebra type applications....
void build(const DoubleVector &old_vector)
Just copys the argument DoubleVector.
double * values_pt()
access function to the underlying values
Describes the distribution of a distributable linear algebra type object. Typically this is a contain...
bool distributed() const
access function to the distributed - indicates whether the distribution is serial or distributed
unsigned first_row() const
access function for the first row on this processor. If not distributed then this is just zero.
OomphCommunicator * communicator_pt() const
const access to the communicator pointer
bool built() const
if the communicator_pt is null then the distribution is not setup then false is returned,...
unsigned nrow() const
access function to the number of global rows.
unsigned nrow_local() const
access function for the num of local rows on this processor. If no MPI then Nrow is returned.
An oomph-lib wrapper to the MPI_Comm communicator object. Just contains an MPI_Comm object (which is ...
An OomphLibError object which should be thrown when an run-time error is encountered....
Epetra_CrsMatrix * create_distributed_epetra_matrix(const CRDoubleMatrix *oomph_matrix_pt, const LinearAlgebraDistribution *dist_pt)
create an Epetra_CrsMatrix from an oomph-lib CRDoubleMatrix. If oomph_matrix_pt is NOT distributed (i...
Epetra_CrsMatrix * create_distributed_epetra_matrix_for_aztecoo(CRDoubleMatrix *oomph_matrix_pt)
create and Epetra_CrsMatrix from an oomph-lib CRDoubleMatrix. Specialisation for Trilinos AztecOO....
void copy_to_oomphlib_vector(const Epetra_Vector *epetra_vec_pt, DoubleVector &oomph_vec)
Helper function to copy the contents of a Trilinos vector to an oomph-lib distributed vector....
void multiply(const CRDoubleMatrix *matrix, const DoubleVector &x, DoubleVector &soln)
Function to perform a matrix-vector multiplication on a oomph-lib matrix and vector using Trilinos fu...
Epetra_Map * create_epetra_map(const LinearAlgebraDistribution *const dist)
create an Epetra_Map corresponding to the LinearAlgebraDistribution
Epetra_Vector * create_distributed_epetra_vector(const DoubleVector &oomph_vec)
create an Epetra_Vector from an oomph-lib DoubleVector. If oomph_vec is NOT distributed (i....
Epetra_Vector * create_epetra_vector_view_data(DoubleVector &oomph_vec)
create an Epetra_Vector equivalent of DoubleVector The argument DoubleVector must be built....
//////////////////////////////////////////////////////////////////// ////////////////////////////////...