From 7caa07de9833e59fa4e783904b932d4ce217a19d Mon Sep 17 00:00:00 2001
From: Wenqing Wang <wenqing.wang@ufz.de>
Date: Fri, 9 Jun 2017 14:00:27 +0200
Subject: [PATCH] [PETSc] Some minor changes according to the comments by Diam
 and Tom

---
 MathLib/LinAlg/LinAlg.h                      |  2 +-
 MathLib/LinAlg/PETSc/PETScVector.cpp         | 24 +++++++++++---------
 MathLib/LinAlg/PETSc/PETScVector.h           |  6 ++---
 NumLib/DOF/MeshComponentMap.cpp              | 10 ++++----
 ProcessLib/GlobalVectorFromNamedFunction.cpp |  1 -
 5 files changed, 21 insertions(+), 22 deletions(-)

diff --git a/MathLib/LinAlg/LinAlg.h b/MathLib/LinAlg/LinAlg.h
index 154d8b45a80..8577adf1d4e 100644
--- a/MathLib/LinAlg/LinAlg.h
+++ b/MathLib/LinAlg/LinAlg.h
@@ -213,7 +213,7 @@ namespace LinAlg
 /**
     Set local accessible vector in order to get entries. Call this
     before call operator[] or get(...) of x.
-    The function only has computation if DDC is appied,
+    The function only has computation if DDC is enabled,
     e.g. parallel computing. Up to now, Eigen vector is not used for
     global vectors in parallel computing. Therefore this function is
     empty in the current status.
diff --git a/MathLib/LinAlg/PETSc/PETScVector.cpp b/MathLib/LinAlg/PETSc/PETScVector.cpp
index 0ef9641355c..12e1a2b10e1 100644
--- a/MathLib/LinAlg/PETSc/PETScVector.cpp
+++ b/MathLib/LinAlg/PETSc/PETScVector.cpp
@@ -66,8 +66,8 @@ PETScVector::PETScVector(const PetscInt vec_size,
     config();
 
     for (PetscInt i=0; i<nghosts; i++)
-        _global_ids2local_ids_ghost.insert
-        (std::make_pair(ghost_ids[i], _size_loc + i));
+        _global_ids2local_ids_ghost.emplace(ghost_ids[i],
+                                            _size_loc + i);
 }
 
 PETScVector::PETScVector(const PETScVector &existing_vec, const bool deep_copy)
@@ -146,10 +146,12 @@ void PETScVector::getGlobalVector(std::vector<PetscScalar>& u) const
     PetscMemoryGetCurrentUsage(&mem1);
 #endif
 
+    assert(u.size() == _size);
+
     PetscScalar *xp = nullptr;
     VecGetArray(_v, &xp);
 
-    gatherLocalVectors(xp, &u[0]);
+    gatherLocalVectors(xp, u.data());
 
     //This following line may be needed late on
     //  for a communication load balance:
@@ -166,17 +168,17 @@ void PETScVector::getGlobalVector(std::vector<PetscScalar>& u) const
 
 void PETScVector::setLocalAccessibleVector() const
 {
-    if (_entry_array.size() == 0)
+    if (_entry_array.empty())
     {
         const PetscInt array_size
             = _global_ids2local_ids_ghost.size() > 0 ?
-              _size_loc + _size_ghosts: _size;
+                      _size_loc + _size_ghosts: _size;
         _entry_array.resize(array_size);
     }
 
-    if (_global_ids2local_ids_ghost.size() > 0)
+    if ( !_global_ids2local_ids_ghost.empty() )
     {
-        double* loc_x = getLocalVector();
+        PetscScalar* loc_x = getLocalVector();
         std::copy_n(loc_x, _size_loc + _size_ghosts,
                     _entry_array.begin());
         restoreArray(loc_x);
@@ -185,11 +187,11 @@ void PETScVector::setLocalAccessibleVector() const
         getGlobalVector(_entry_array);
 }
 
-void PETScVector::copyValues(std::vector<double>& u) const
+void PETScVector::copyValues(std::vector<PetscScalar>& u) const
 {
     assert(u.size() == (std::size_t) (getLocalSize() + getGhostSize()));
 
-    double* loc_x = getLocalVector();
+    PetscScalar* loc_x = getLocalVector();
     std::copy_n(loc_x, getLocalSize() + getGhostSize(), u.begin());
     restoreArray(loc_x);
 }
@@ -207,9 +209,9 @@ PetscScalar PETScVector::get(const PetscInt idx) const
 }
 
 
-std::vector<double> PETScVector::get(std::vector<IndexType> const& indices) const
+std::vector<PetscScalar> PETScVector::get(std::vector<IndexType> const& indices) const
 {
-    std::vector<double> local_x(indices.size());
+    std::vector<PetscScalar> local_x(indices.size());
     // If VecGetValues can get values from different processors,
     // use VecGetValues(_v, indices.size(), indices.data(),
     //                    local_x.data());
diff --git a/MathLib/LinAlg/PETSc/PETScVector.h b/MathLib/LinAlg/PETSc/PETScVector.h
index 706377cfa4d..bb5193109d5 100644
--- a/MathLib/LinAlg/PETSc/PETScVector.h
+++ b/MathLib/LinAlg/PETSc/PETScVector.h
@@ -170,11 +170,11 @@ class PETScVector
 
         /// Get several entries. setLocalAccessibleVector() must be
         /// called beforehand.
-        std::vector<double> get(std::vector<IndexType> const& indices) const;
+        std::vector<PetscScalar> get(std::vector<IndexType> const& indices) const;
 
         /// Get the value of an entry by [] operator.
         /// setLocalAccessibleVector() must be called beforehand.
-        double operator[] (PetscInt idx) const
+        PetscScalar operator[] (PetscInt idx) const
         {
             return get(idx);
         }
@@ -207,7 +207,7 @@ class PETScVector
            Copy local entries including ghost ones to an array
            \param u Preallocated vector for the values of local entries.
         */
-        void copyValues(std::vector<double>& u) const;
+        void copyValues(std::vector<PetscScalar>& u) const;
 
         /*! View the global vector for test purpose. Do not use it for output a big vector.
             \param file_name  File name for output
diff --git a/NumLib/DOF/MeshComponentMap.cpp b/NumLib/DOF/MeshComponentMap.cpp
index c9be0069205..a365bffd7bf 100644
--- a/NumLib/DOF/MeshComponentMap.cpp
+++ b/NumLib/DOF/MeshComponentMap.cpp
@@ -71,12 +71,7 @@ MeshComponentMap::MeshComponentMap(
             for (std::size_t j = 0; j < mesh_subset.getNumberOfNodes(); j++)
             {
                 GlobalIndexType global_id = 0;
-                if (order == ComponentOrder::BY_LOCATION)
-                {
-                    global_id = static_cast<GlobalIndexType>(
-                        components.size() * mesh.getGlobalNodeID(j) + comp_id);
-                }
-                else
+                if (order != ComponentOrder::BY_LOCATION)
                 {
                     // Deactivated since this case is not suitable to
                     // arrange non ghost entries of a partition within
@@ -85,6 +80,9 @@ MeshComponentMap::MeshComponentMap(
                               " can only be numbered by the oder type"
                               " of ComponentOrder::BY_LOCATION");
                 }
+                global_id = static_cast<GlobalIndexType>(
+                    components.size() * mesh.getGlobalNodeID(j)
+                        + comp_id);
                 const bool is_ghost =
                     mesh.isGhostNode(mesh.getNode(j)->getID());
                 if (is_ghost)
diff --git a/ProcessLib/GlobalVectorFromNamedFunction.cpp b/ProcessLib/GlobalVectorFromNamedFunction.cpp
index a0d3519e5b3..82020a384e3 100644
--- a/ProcessLib/GlobalVectorFromNamedFunction.cpp
+++ b/ProcessLib/GlobalVectorFromNamedFunction.cpp
@@ -54,7 +54,6 @@ GlobalVector const& GlobalVectorFromNamedFunction::call(
         _context.index = node_id;
         auto const value = _function_caller.call(args);
 
-        // Problems with PETSc also uses global index.
         result->set(node_id, value);
     }
 
-- 
GitLab