From 2dacdb31ed327cf1b94a2942278fdfd29d5d64cc Mon Sep 17 00:00:00 2001
From: Wenqing Wang <wenqing.wang@ufz.de>
Date: Mon, 24 Mar 2014 14:07:34 +0100
Subject: [PATCH] Changes with the comments by Nori, Tom and Lars

---
 MathLib/LinAlg/Dense/DenseMatrix-impl.h     |  4 +--
 MathLib/LinAlg/Dense/DenseMatrix.h          |  2 +-
 MathLib/LinAlg/PETSc/PETScMatrix.cpp        | 21 +++++--------
 MathLib/LinAlg/PETSc/PETScMatrix.h          | 34 ++++++++++-----------
 MathLib/LinAlg/PETSc/PETScMatrixOption.h    | 25 ++++++++-------
 MathLib/LinAlg/PETSc/PETScVector.h          |  4 +--
 Tests/MathLib/TestGlobalMatrixInterface.cpp | 26 ++++++++--------
 scripts/cmake/Find.cmake                    |  4 +++
 scripts/cmake/findPETSC/FindPETSc.cmake     |  6 ----
 9 files changed, 59 insertions(+), 67 deletions(-)

diff --git a/MathLib/LinAlg/Dense/DenseMatrix-impl.h b/MathLib/LinAlg/Dense/DenseMatrix-impl.h
index 2f3cabc7203..ee0f2ceee19 100644
--- a/MathLib/LinAlg/Dense/DenseMatrix-impl.h
+++ b/MathLib/LinAlg/Dense/DenseMatrix-impl.h
@@ -1,5 +1,5 @@
 /**
- * @file DenseMatrix.tpp
+ * @file DenseMatrix-imp.h
  * @author Thomas Fischer and Haibing Shao
  * @date Jun 10, 2013
  *
@@ -54,7 +54,7 @@ DenseMatrix<FP_TYPE, IDX_TYPE>::~DenseMatrix ()
 }
 
 template <typename FP_TYPE, typename IDX_TYPE>
-FP_TYPE* DenseMatrix<FP_TYPE, IDX_TYPE>::getData() const
+const FP_TYPE* DenseMatrix<FP_TYPE, IDX_TYPE>::getEntries() const
 {
     return _data;
 }
diff --git a/MathLib/LinAlg/Dense/DenseMatrix.h b/MathLib/LinAlg/Dense/DenseMatrix.h
index 98461d7a633..7aeef330429 100644
--- a/MathLib/LinAlg/Dense/DenseMatrix.h
+++ b/MathLib/LinAlg/Dense/DenseMatrix.h
@@ -55,7 +55,7 @@ public:
    /**
     * Get the raw data directly
    */
-   FP_TYPE *getData () const;
+   const FP_TYPE *getEntries() const;
 
    /**
     * Assignment operator, makes a copy of the internal data of the object.
diff --git a/MathLib/LinAlg/PETSc/PETScMatrix.cpp b/MathLib/LinAlg/PETSc/PETScMatrix.cpp
index 2a80d653c07..1d6cbaa6858 100644
--- a/MathLib/LinAlg/PETSc/PETScMatrix.cpp
+++ b/MathLib/LinAlg/PETSc/PETScMatrix.cpp
@@ -18,36 +18,31 @@
 namespace MathLib
 {
 
-PETScMatrix::PETScMatrix (const PetscInt size, const PETScMatrixOption mat_opt)
+PETScMatrix::PETScMatrix (const PetscInt size, const PETScMatrixOption &mat_opt)
+    :_size(size), _n_loc_rows(PETSC_DECIDE), _n_loc_cols(mat_opt._n_local_cols)
 {
-    _size = size;
-    _loc_rows = PETSC_DECIDE;
-    _loc_cols = mat_opt._local_cols;
-
-    if(mat_opt._is_size_local_rows)
+    if(!mat_opt._is_global_size)
     {
         _size = PETSC_DECIDE;
-        _loc_rows = size;
+        _n_loc_rows = size;
     }
 
     MatCreate(PETSC_COMM_WORLD, &_A);
-    MatSetSizes(_A, _loc_rows, _loc_cols, _size, _size);
+    MatSetSizes(_A, _n_loc_rows, _n_loc_cols, _size, _size);
 
     MatSetFromOptions(_A);
 
     // for a dense matrix: MatSeqAIJSetPreallocation(_A, d_nz, PETSC_NULL);
-    MatMPIAIJSetPreallocation(_A, mat_opt._d_nz, PETSC_NULL, mat_opt._o_nz, PETSC_NULL);
+    MatMPIAIJSetPreallocation(_A, mat_opt._d_nnz, PETSC_NULL, mat_opt._o_nnz, PETSC_NULL);
 
     MatGetOwnershipRange(_A, &_start_rank, &_end_rank);
     MatGetSize(_A, &_size,  PETSC_NULL);
-    MatGetLocalSize(_A, &_loc_rows, &_loc_cols);
+    MatGetLocalSize(_A, &_n_loc_rows, &_n_loc_cols);
 }
 
 void PETScMatrix::setRowsColumnsZero(std::vector<PetscInt> const& row_pos)
 {
-    // Each process indicates only rows it owns that are to be zeroed.
-    // If it is called, it must be called by all ranks of cores.
-
+    // Each rank (compute core) processes only the rows that belong to the rank itself.
     const PetscScalar one = 1.0;
     const PetscInt nrows = static_cast<PetscInt> (row_pos.size());
 
diff --git a/MathLib/LinAlg/PETSc/PETScMatrix.h b/MathLib/LinAlg/PETSc/PETScMatrix.h
index 2500332a552..60ca99208b3 100644
--- a/MathLib/LinAlg/PETSc/PETScMatrix.h
+++ b/MathLib/LinAlg/PETSc/PETScMatrix.h
@@ -27,8 +27,6 @@ namespace MathLib
 {
 
 /*!
-   \class PETScVector
-
    \brief Wrapper class for PETSc matrix routines
 */
 class PETScMatrix
@@ -39,7 +37,7 @@ class PETScMatrix
           \param size   The dimension of the matrix.
           \param mat_op The configuration information for creating a matrix
         */
-        PETScMatrix(const PetscInt size, const PETScMatrixOption mat_op = PETScMatrixOption() );
+        PETScMatrix(const PetscInt size, const PETScMatrixOption &mat_op = PETScMatrixOption() );
 
         ~PETScMatrix()
         {
@@ -58,7 +56,7 @@ class PETScMatrix
         }
 
         /// Get the dimension
-        PetscInt size() const
+        PetscInt getDimension() const
         {
             return _size;
         }
@@ -69,26 +67,26 @@ class PETScMatrix
             return _start_rank;
         }
 
-        /// Get the last global index of the row in the same rank
+        /// Get the end global index of the rows in the same rank
         PetscInt getRangeEnd() const
         {
             return _end_rank;
         }
 
         /// Get the number of local rows
-        PetscInt getLocalRows() const
+        PetscInt getNLocalRows() const
         {
-            return _loc_rows;
+            return _n_loc_rows;
         }
 
         /// Get the number of local columns
-        PetscInt getLocalColumns() const
+        PetscInt getNLocalColumns() const
         {
-            return _loc_cols;
+            return _n_loc_cols;
         }
 
         /// Get matrix reference
-        PETSc_Mat &getData()
+        PETSc_Mat &getRawMatrix()
         {
             return _A;
         }
@@ -100,7 +98,9 @@ class PETScMatrix
         }
 
         /*
-           \brief         Set the specified rows and columns to zero except off-diagonal entries
+           \brief Set the specified rows to zero except off-diagonal entries, i.e.
+                  \f$A(k, j) = 0, j!=k, j=1,2,\cdots, n\f$, where \f$k \in \mbox{row\_pos}\f$
+                  This fucntion must be called by all rank.
            \param row_pos The row indicies of the specified rows.
         */
         void setRowsColumnsZero(std::vector<PetscInt> const& row_pos);
@@ -111,13 +111,13 @@ class PETScMatrix
            \param vec_r The result vector, e.g. \f$ y \f$
             Both of the two arguments must be created prior to be used.
         */
-        void multVector(PETScVector &vec, PETScVector &vec_r)
+        void multiVector(const PETScVector &vec, PETScVector &vec_r)
         {
             MatMult(_A, vec.getData(), vec_r.getData() );
         }
 
         /*!
-           \brief       Insert a single entry with value.
+           \brief       Set a single entry with a value.
            \param i     The row index
            \param j     The column index
            \param value The entry value
@@ -179,12 +179,12 @@ class PETScMatrix
         /// PETSc matrix
         PETSc_Mat _A;
 
-        /// Dimension
+        /// Dimension of matrix
         PetscInt _size;
         /// Number of the local rows
-        PetscInt _loc_rows;
+        PetscInt _n_loc_rows;
         /// Number of the local columns
-        PetscInt _loc_cols;
+        PetscInt _n_loc_cols;
 
         /// Starting index in a rank
         PetscInt _start_rank;
@@ -208,7 +208,7 @@ void PETScMatrix::add(std::vector<PetscInt> const& row_pos,
     const PetscInt nrows = static_cast<PetscInt> (row_pos.size());
     const PetscInt ncols = static_cast<PetscInt> (col_pos.size());
 
-    MatSetValues(_A, nrows, &row_pos[0], ncols, &col_pos[0], sub_mat.getData(), ADD_VALUES);
+    MatSetValues(_A, nrows, &row_pos[0], ncols, &col_pos[0], sub_mat.getEntries(), ADD_VALUES);
 };
 
 /*!
diff --git a/MathLib/LinAlg/PETSc/PETScMatrixOption.h b/MathLib/LinAlg/PETSc/PETScMatrixOption.h
index 4ddcb644f01..f9add25c2a6 100644
--- a/MathLib/LinAlg/PETSc/PETScMatrixOption.h
+++ b/MathLib/LinAlg/PETSc/PETScMatrixOption.h
@@ -19,38 +19,37 @@
 namespace MathLib
 {
 /*!
-   \struct  PETScMatrixOption
    \brief This a struct data containing the configuration information to create a PETSc type matrix
 */
 struct PETScMatrixOption
 {
-    PETScMatrixOption() :  _is_size_local_rows(false), _local_cols(PETSC_DECIDE),
-        _d_nz(10), _o_nz(10)
+    PETScMatrixOption() :  _is_global_size(true), _n_local_cols(PETSC_DECIDE),
+        _d_nnz(PETSC_DECIDE), _o_nnz(PETSC_DECIDE)
     { }
 
     /*!
-     \brief Flag for the type of the first argument of
-            the constructor of class PETScMatrix, size.
-            true:  the size is the number of local rows,
-            false: the size is the number of global rows.
+     \brief Flag for the type of size, which is one of arguments of
+            the constructor of class PETScMatrix
+              true:  the size is the number of local rows,
+              false: the size is the number of global rows.
             The default is false.
      */
-    bool _is_size_local_rows;
+    bool _is_global_size;
 
     /// Number of local columns. The default is PETSC_DECIDE.
-    PetscInt _local_cols;
+    PetscInt _n_local_cols;
 
     /*!
      \brief Number of nonzeros per row in DIAGONAL portion of local submatrix
-           (same value is used for all local rows), the default is 10
+           (same value is used for all local rows), the default is PETSC_DECIDE
     */
-    PetscInt _d_nz;
+    PetscInt _d_nnz;
 
     /*!
      \brief Number of nonzeros per row in the OFF-DIAGONAL portion of local submatrix
-            (same value is used for all local rows), the default is 10
+            (same value is used for all local rows), the default is PETSC_DECIDE
     */
-    PetscInt _o_nz;
+    PetscInt _o_nnz;
 };
 
 } // end namespace
diff --git a/MathLib/LinAlg/PETSc/PETScVector.h b/MathLib/LinAlg/PETSc/PETScVector.h
index b5a72069fee..c67c3c89374 100644
--- a/MathLib/LinAlg/PETSc/PETScVector.h
+++ b/MathLib/LinAlg/PETSc/PETScVector.h
@@ -39,7 +39,7 @@ class PETScVector
         /*!
             \brief Constructor
             \param vec_size       The size of the vector, either global or local
-            \param is_global_size The flag of the type of vec_size, i.e. whether it is a global size 
+            \param is_global_size The flag of the type of vec_size, i.e. whether it is a global size
                                   or local size. The default is true.
         */
         PETScVector(const PetscInt vec_size, const bool is_global_size = true);
@@ -179,7 +179,7 @@ class PETScVector
         }
 
         /// Get PETsc vector. Use it only for test purpose
-        PETSc_Vec &getData()
+        const PETSc_Vec &getData() const
         {
             return _v;
         }
diff --git a/Tests/MathLib/TestGlobalMatrixInterface.cpp b/Tests/MathLib/TestGlobalMatrixInterface.cpp
index 4a57cbc7cd9..a775af693aa 100644
--- a/Tests/MathLib/TestGlobalMatrixInterface.cpp
+++ b/Tests/MathLib/TestGlobalMatrixInterface.cpp
@@ -42,7 +42,7 @@ void checkGlobalMatrixInterface(T_MATRIX &m)
     m.add(0, 0, 1.0);
     m.setZero();
 
-    MathLib::DenseMatrix<double, short> local_m(2, 2, 1.0);
+    MathLib::DenseMatrix<double> local_m(2, 2, 1.0);
     std::vector<std::size_t> vec_pos(2);
     vec_pos[0] = 1;
     vec_pos[1] = 3;
@@ -61,16 +61,16 @@ void checkGlobalMatrixInterfaceMPI(T_MATRIX &m, T_VECTOR &v)
     MPI_Comm_rank(PETSC_COMM_WORLD, &mrank);
 
     ASSERT_EQ(3u, msize);
-    ASSERT_EQ(6u, m.size());
-    ASSERT_EQ(m.getRangeEnd()-m.getRangeBegin(), m.getLocalRows());
+    ASSERT_EQ(6u, m.getDimension());
+    ASSERT_EQ(m.getRangeEnd()-m.getRangeBegin(), m.getNLocalRows());
 
     int gathered_cols;
-    int local_cols = m.getLocalColumns();
+    int local_cols = m.getNLocalColumns();
     MPI_Allreduce(&local_cols, &gathered_cols, 1, MPI_INT, MPI_SUM, PETSC_COMM_WORLD);
     ASSERT_EQ(6u, gathered_cols);
 
     // Add entries
-    MathLib::DenseMatrix<double, short> loc_m(2);
+    MathLib::DenseMatrix<double> loc_m(2, 2);
     loc_m(0, 0) = 1.;
     loc_m(0, 1) = 2.;
     loc_m(1, 0) = 3.;
@@ -91,7 +91,7 @@ void checkGlobalMatrixInterfaceMPI(T_MATRIX &m, T_VECTOR &v)
     v = 1.;
     const bool deep_copy = false;
     T_VECTOR y(v, deep_copy);
-    m.multVector(v, y);
+    m.multiVector(v, y);
 
     ASSERT_EQ(sqrt(3*(3*3 + 7*7)), y.getNorm());
 
@@ -102,7 +102,7 @@ void checkGlobalMatrixInterfaceMPI(T_MATRIX &m, T_VECTOR &v)
         m.add(1, 1, 5.0);
     }
     MathLib::finalizeMatrixAssembly(m);
-    m.multVector(v, y);
+    m.multiVector(v, y);
 
     ASSERT_EQ(sqrt((2*3*3 + 8*8 + 3*7*7)), y.getNorm());
 }
@@ -128,10 +128,10 @@ TEST(Math, CheckInterface_LisMatrix)
 TEST(Math, CheckInterface_PETScMatrix_Local_Size)
 {
     MathLib::PETScMatrixOption opt;
-    opt._d_nz = 2;
-    opt._o_nz = 0;
-    opt._is_size_local_rows = true;
-    opt._local_cols = 2;
+    opt._d_nnz = 2;
+    opt._o_nnz = 0;
+    opt._is_global_size = false;
+    opt._n_local_cols = 2;
     MathLib::PETScMatrix A(2, opt);
 
     const bool is_gloabal_size = false;
@@ -142,8 +142,8 @@ TEST(Math, CheckInterface_PETScMatrix_Local_Size)
 TEST(Math, CheckInterface_PETScMatrix_Global_Size)
 {
     MathLib::PETScMatrixOption opt;
-    opt._d_nz = 2;
-    opt._o_nz = 0;
+    opt._d_nnz = 2;
+    opt._o_nnz = 0;
     MathLib::PETScMatrix A(6, opt);
 
     MathLib::PETScVector x(6);
diff --git a/scripts/cmake/Find.cmake b/scripts/cmake/Find.cmake
index 865941f1ba3..06cf9d7b4dd 100644
--- a/scripts/cmake/Find.cmake
+++ b/scripts/cmake/Find.cmake
@@ -120,6 +120,10 @@ ENDIF()
 IF(OGS_USE_PETSC)
     MESSAGE (STATUS  "Configuring for PETSc" )
 
+    ##Force CMake to accept a given PETSc configuration in case the failure of MPI tests
+    ##This may cause the compilation broken.
+    SET(PETSC_EXECUTABLE_RUNS YES)
+
     SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/scripts/cmake/findPETSC")
     FIND_PACKAGE(PETSc REQUIRED)
 
diff --git a/scripts/cmake/findPETSC/FindPETSc.cmake b/scripts/cmake/findPETSC/FindPETSc.cmake
index 4b4eb3addb2..536e4546cc6 100644
--- a/scripts/cmake/findPETSC/FindPETSc.cmake
+++ b/scripts/cmake/findPETSC/FindPETSc.cmake
@@ -320,12 +320,6 @@ int main(int argc,char *argv[]) {
   mark_as_advanced (PETSC_INCLUDES PETSC_LIBRARIES PETSC_COMPILER PETSC_DEFINITIONS PETSC_MPIEXEC PETSC_EXECUTABLE_RUNS)
 endif ()
 
-# message (STATUS "dddd---${PETSC_INCLUDES}--${PETSC_LIBRARIES}-- ${petsc_includes_minimal}  ")
-# message (STATUS "dddd---${PETSC_EXECUTABLE_RUNS} ggggg  ${PETSC_DEFINITIONS}")
-
- set (PETSC_EXECUTABLE_RUNS "YES" CACHE BOOL
-"Can the system successfully run a PETSc executable? This variable can be manually set to \"YES\" to force CMake to accept a given PETSc configuration, but this will almost always result in a broken build. If you change PETSC_DIR, PETSC_ARCH, or PETSC_CURRENT you would have to reset this variable." FORCE)
-
 include (FindPackageHandleStandardArgs)
 find_package_handle_standard_args (PETSc
   "PETSc could not be found.  Be sure to set PETSC_DIR and PETSC_ARCH."
-- 
GitLab