3#ifndef DUNE_PDELAB_BACKEND_SIMPLE_VECTOR_HH
4#define DUNE_PDELAB_BACKEND_SIMPLE_VECTOR_HH
15#include <dune/pdelab/gridfunctionspace/gridfunctionspace.hh>
16#include <dune/pdelab/gridfunctionspace/lfsindexcache.hh>
18#include <dune/pdelab/backend/interface.hh>
19#include <dune/pdelab/backend/common/uncachedvectorview.hh>
20#include <dune/pdelab/backend/simple/descriptors.hh>
34 typename FieldTraits<K>::real_type operator()(
const K& k)
const
36 return Dune::fvmeta::abs2(k);
42 template<
typename GFS,
typename C>
44 :
public Backend::impl::Wrapper<C>
47 friend Backend::impl::Wrapper<C>;
51 typedef typename Container::value_type ElementType;
52 typedef ElementType E;
55 typedef ElementType field_type;
57 typedef GFS GridFunctionSpace;
58 typedef typename Container::size_type size_type;
60 typedef typename GFS::Ordering::Traits::ContainerIndex ContainerIndex;
62 typedef typename Container::iterator iterator;
63 typedef typename Container::const_iterator const_iterator;
65 template<
typename LFSCache>
66 using LocalView = UncachedVectorView<VectorContainer,LFSCache>;
68 template<
typename LFSCache>
69 using ConstLocalView = ConstUncachedVectorView<const VectorContainer,LFSCache>;
72 VectorContainer(
const VectorContainer& rhs)
74 , _container(
std::make_shared<Container>(*(rhs._container)))
77 VectorContainer (
const GFS& gfs, Backend::attached_container = Backend::attached_container())
79 , _container(
std::make_shared<Container>(gfs.ordering().blockCount()))
83 VectorContainer(
const GFS& gfs, Backend::unattached_container)
92 VectorContainer (
const GFS& gfs, Container& container)
96 _container->resize(gfs.ordering().blockCount());
99 VectorContainer (
const GFS& gfs,
const E& e)
101 , _container(
std::make_shared<Container>(gfs.ordering().blockCount(),e))
109 void attach(std::shared_ptr<Container> container)
111 _container = container;
114 bool attached()
const
116 return bool(_container);
119 const std::shared_ptr<Container>& storage()
const
126 return _container->size();
129 VectorContainer& operator=(
const VectorContainer& r)
135 (*_container) = (*r._container);
139 _container = std::make_shared<Container>(*(r._container));
144 VectorContainer& operator=(
const E& e)
146 std::fill(_container->begin(),_container->end(),e);
150 VectorContainer& operator*=(
const E& e)
152 std::transform(_container->begin(),_container->end(),_container->begin(),
158 VectorContainer& operator+=(
const E& e)
160 std::transform(_container->begin(),_container->end(),_container->begin(),
165 VectorContainer& operator+=(
const VectorContainer& y)
167 std::transform(_container->begin(),_container->end(),y._container->begin(),
168 _container->begin(),std::plus<E>());
172 VectorContainer& operator-= (
const VectorContainer& y)
174 std::transform(_container->begin(),_container->end(),y._container->begin(),
175 _container->begin(),std::minus<E>());
179 E& operator[](
const ContainerIndex& ci)
181 return (*_container)[ci[0]];
184 const E& operator[](
const ContainerIndex& ci)
const
186 return (*_container)[ci[0]];
189 typename Dune::template FieldTraits<E>::real_type two_norm()
const
191 using namespace std::placeholders;
193 typedef typename Dune::template FieldTraits<E>::real_type Real;
194 return sqrt(
std::accumulate(_container->begin(),_container->end(),Real(0),std::bind(std::plus<Real>(),
_1,std::bind(abs2<E>(),
_2))));
197 typename Dune::template FieldTraits<E>::real_type one_norm()
const
199 typedef typename Dune::template FieldTraits<E>::real_type Real;
201 [](
const auto& n,
const auto& e) {
207 typename Dune::template FieldTraits<E>::real_type infinity_norm()
const
209 if (_container->size() == 0)
212 return abs(*std::max_element(_container->begin(),_container->end(),
213 [](
const auto& a,
const auto& b) {
215 return abs(a) < abs(b);
219 E operator*(
const VectorContainer& y)
const
221 return std::inner_product(_container->begin(),_container->end(),y._container->begin(),E(0));
224 E
dot(
const VectorContainer& y)
const
226 return std::inner_product(_container->begin(),_container->end(),y._container->begin(),E(0),std::plus<E>(),Dune::dot<E,E>);
229 VectorContainer& axpy(
const E& a,
const VectorContainer& y)
231 using namespace std::placeholders;
232 std::transform(_container->begin(),_container->end(),y._container->begin(),
233 _container->begin(),std::bind(std::plus<E>(),
_1,std::bind(std::multiplies<E>(),a,
_2)));
243 const Container& base ()
const
255 const Container& native ()
const
264 return _container->begin();
267 const_iterator begin()
const
269 return _container->begin();
274 return _container->end();
277 const_iterator end()
const
279 return _container->end();
282 size_t flatsize()
const
284 return _container->size();
287 const GFS& gridFunctionSpace()
const
294 std::shared_ptr<Container> _container;
303 template<
typename GFS,
typename E>
304 struct SimpleVectorSelectorHelper
307 using vector_type =
typename GFS::Traits::Backend::template vector_type<E>;
309 using Type = Simple::VectorContainer<GFS,vector_type>;
316 template<
template<
typename>
class Container,
typename GFS,
typename E>
317 struct BackendVectorSelectorHelper<Simple::VectorBackend<Container>, GFS, E>
318 :
public SimpleVectorSelectorHelper<GFS,E>
This file implements a vector space as a tensor product of a given vector space. The number of compon...
Implements a vector constructed from a given type representing a field and a compile-time given size.
auto dot(const A &a, const B &b) -> typename std::enable_if<!IsVector< A >::value &&!std::is_same< typename FieldTraits< A >::field_type, typename FieldTraits< A >::real_type > ::value, decltype(conj(a) *b)>::type
computes the dot product for fundamental data types according to Petsc's VectDot function: dot(a,...
Definition: dotproduct.hh:40
constexpr index_constant< 1 > _1
Compile time index with value 1.
Definition: indices.hh:54
constexpr index_constant< 2 > _2
Compile time index with value 2.
Definition: indices.hh:57
constexpr T accumulate(Range &&range, T value, F &&f)
Accumulate values.
Definition: hybridutilities.hh:289
Dune namespace.
Definition: alignedallocator.hh:11
std::shared_ptr< T > stackobject_to_shared_ptr(T &t)
Create a shared_ptr for a stack-allocated object.
Definition: shared_ptr.hh:70
This file implements several utilities related to std::shared_ptr.