From 460c62856c2f09fc07a855c62d1e5cfe7d838d19 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carsten=20Gr=C3=A4ser?= <graeser@mi.fu-berlin.de>
Date: Tue, 8 Sep 2009 20:48:47 +0000
Subject: [PATCH] Moved genericvectortools.hh to dune-solvers

[[Imported from SVN: r2811]]
---
 dune-solvers/Makefile.am                     |   2 +-
 dune-solvers/genericvectortools.hh           | 116 +++++++++++++++++++
 dune-solvers/iterationsteps/multigridstep.cc |   2 +-
 dune-solvers/solvers/iterativesolver.cc      |   2 +-
 4 files changed, 119 insertions(+), 3 deletions(-)
 create mode 100644 dune-solvers/genericvectortools.hh

diff --git a/dune-solvers/Makefile.am b/dune-solvers/Makefile.am
index 1246917f..5031b12e 100644
--- a/dune-solvers/Makefile.am
+++ b/dune-solvers/Makefile.am
@@ -2,6 +2,6 @@
 SUBDIRS = common iterationsteps norms solvers transferoperators
 
 dune_solversdir = $(includedir)/dune/dune-solvers
-dune_solvers_HEADERS = boxconstraint.hh numproc.hh computeenergy.hh
+dune_solvers_HEADERS = boxconstraint.hh numproc.hh computeenergy.hh genericvectortools.hh
 
 include $(top_srcdir)/am/global-rules
diff --git a/dune-solvers/genericvectortools.hh b/dune-solvers/genericvectortools.hh
new file mode 100644
index 00000000..ac76030b
--- /dev/null
+++ b/dune-solvers/genericvectortools.hh
@@ -0,0 +1,116 @@
+#ifndef GENERIC_VECTOR_TOOL_HH
+#define GENERIC_VECTOR_TOOL_HH
+
+/** \file
+    \brief Various tools for working with istl vectors of arbitrary nesting depth
+*/
+
+#include<iostream>
+
+#include "dune/common/fvector.hh"
+#include <dune/istl/bcrsmatrix.hh>
+
+/** \brief Various tools for working with istl vectors of arbitrary nesting depth
+*/
+struct GenericVector
+{
+
+    //! Write vector to given stream
+    template <class VectorType>
+    static void writeBinary(std::ostream& s, const VectorType& v)
+    {
+        typename VectorType::const_iterator it = v.begin();
+        typename VectorType::const_iterator end = v.end();
+        for(; it!=end; ++it)
+            GenericVector::writeBinary(s, *it);
+    }
+
+    template <class field_type, int n>
+    static void writeBinary(std::ostream& s, const Dune::FieldVector<field_type,n>& v)
+    {
+        typedef typename Dune::FieldVector<field_type,n> VectorType;
+        typename VectorType::const_iterator it = v.begin();
+        typename VectorType::const_iterator end = v.end();
+        for(; it!=end; ++it)
+            s.write(reinterpret_cast<const char*>(&(*it)), sizeof(field_type));
+    }
+
+    //! Read vector from a given stream
+    template <class VectorType>
+    static void readBinary(std::istream& s, VectorType& v)
+    {
+        typename VectorType::iterator it = v.begin();
+        typename VectorType::iterator end = v.end();
+        for(; it!=end; ++it)
+            GenericVector::readBinary(s, *it);
+    }
+
+    template <class field_type, int n>
+    static void readBinary(std::istream& s, Dune::FieldVector<field_type,n>& v)
+    {
+        typedef typename Dune::FieldVector<field_type,n> VectorType;
+        typename VectorType::iterator it = v.begin();
+        typename VectorType::iterator end = v.end();
+        for(; it!=end; ++it)
+            s.read(reinterpret_cast<char*>(&(*it)), sizeof(field_type));
+    }
+
+
+
+    //! Resize vector recursivly to size of given vector/matrix
+    template <class VectorTypeA, class VectorTypeB>
+    static void resize(VectorTypeA& a, const VectorTypeB& b)
+    {
+        a.resize(b.size());
+        typename VectorTypeB::const_iterator it = b.begin();
+        typename VectorTypeB::const_iterator end = b.end();
+        for(; it!=end; ++it)
+            GenericVector::resize(a[it.index()], *it);
+    }
+
+    template <class VectorTypeA, class T, class A>
+    static void resize(VectorTypeA& a, const Dune::BCRSMatrix<T,A>& b)
+    {
+        a.resize(b.N());
+        typedef typename Dune::BCRSMatrix<T,A> VectorTypeB;
+        typename VectorTypeB::const_iterator it = b.begin();
+        typename VectorTypeB::const_iterator end = b.end();
+        for(; it!=end; ++it)
+            GenericVector::resize(a[it.index()], *(it->begin()));
+    }
+
+    template <class field_type, int n, class VectorTypeB>
+    static void resize(Dune::FieldVector<field_type,n>& a, const VectorTypeB& b)
+    {}
+
+    template <int n, class VectorTypeB>
+    static void resize(std::bitset<n>& a, const VectorTypeB& b)
+    {}
+
+
+
+    //! Set vector to zero at indices that are true in bitvector recursivly
+    template <class VectorType, class BitVectorType>
+    static void truncate(VectorType& v, const BitVectorType& tr)
+    {
+        typename VectorType::iterator it = v.begin();
+        typename VectorType::iterator end = v.end();
+        for(; it!=end; ++it)
+            GenericVector::truncate(*it, tr[it.index()]);
+    }
+
+    template <class field_type, int n, class BitVectorType>
+    static void truncate(Dune::FieldVector<field_type,n>& v, const BitVectorType& tr)
+    {
+        typedef typename Dune::FieldVector<field_type,n> VectorType;
+        typename VectorType::iterator it = v.begin();
+        typename VectorType::iterator end = v.end();
+        for(; it!=end; ++it)
+            if (tr[it.index()])
+                *it = 0;
+    }
+
+};
+
+#endif
+
diff --git a/dune-solvers/iterationsteps/multigridstep.cc b/dune-solvers/iterationsteps/multigridstep.cc
index 0a395103..906ed2c7 100644
--- a/dune-solvers/iterationsteps/multigridstep.cc
+++ b/dune-solvers/iterationsteps/multigridstep.cc
@@ -2,8 +2,8 @@
 
 #include <dune-solvers/transferoperators/multigridtransfer.hh>
 #include <dune-solvers/solvers/loopsolver.hh>
+#include <dune-solvers/genericvectortools.hh>
 #include "blockgsstep.hh"
-#include "dune/ag-common/genericvectortools.hh"
 
 #ifdef HAVE_IPOPT
 #include <dune-solvers/solvers/quadraticipopt.hh>
diff --git a/dune-solvers/solvers/iterativesolver.cc b/dune-solvers/solvers/iterativesolver.cc
index d7ba9e16..1837dde0 100644
--- a/dune-solvers/solvers/iterativesolver.cc
+++ b/dune-solvers/solvers/iterativesolver.cc
@@ -4,7 +4,7 @@
 #include <iomanip>
 #include <fstream>
 
-#include "dune/ag-common/genericvectortools.hh"
+#include <dune-solvers/genericvectortools.hh>
 
 template <class VectorType, class BitVectorType>
 void IterativeSolver<VectorType, BitVectorType>::check() const
-- 
GitLab