diff --git a/dune-solvers/Makefile.am b/dune-solvers/Makefile.am index 1246917fb73553f177ac97c0e4fc949662ce2904..5031b12ecd926c750e2e6ca9194712f5987e6f69 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 0000000000000000000000000000000000000000..ac76030b79dbbe1413aa91a01e0da7992eab814d --- /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 0a395103b2614b2897a4949aa147c072ade097cf..906ed2c7f756d2aa6da73be5cdbce80b01ddc718 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 d7ba9e16856309ca05a31ad4981d443b696c0e56..1837dde0fa95dfb9578bfa7202b4284e87e436ed 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