7 #include <dune/common/classname.hh> 8 #include <dune/common/deprecated.hh> 9 #include <dune/common/hybridutilities.hh> 10 #include <dune/common/rangeutilities.hh> 11 #include <dune/common/shared_ptr.hh> 12 #include <dune/common/std/type_traits.hh> 13 #include <dune/functions/functionspacebases/concepts.hh> 15 #include <amdis/Output.hpp> 16 #include <amdis/algorithm/ForEach.hpp> 17 #include <amdis/algorithm/InnerProduct.hpp> 18 #include <amdis/algorithm/Transform.hpp> 19 #include <amdis/common/Concepts.hpp> 20 #include <amdis/common/ConceptsBase.hpp> 21 #include <amdis/common/FakeContainer.hpp> 22 #include <amdis/common/TypeTraits.hpp> 23 #include <amdis/functions/NodeIndices.hpp> 24 #include <amdis/operations/Assigner.hpp> 25 #include <amdis/typetree/MultiIndex.hpp> 37 template <
class T,
template <
class>
class VectorImpl>
41 using Impl = VectorImpl<T>;
45 enum class VectorState
54 using VectorStateOf_t = std::conditional_t<std::is_same_v<A,Assigner::plus_assign>,
55 std::integral_constant<VectorState, VectorState::add_values>,
56 std::integral_constant<VectorState, VectorState::insert_values>>;
59 using size_type =
typename Impl::size_type;
60 using value_type =
typename Impl::value_type;
65 class = std::void_t<decltype(std::declval<GlobalBasis>().indexDistribution())> >
78 using HasLocalSize = decltype(std::declval<V>().
localSize());
81 using HasGlobalSize = decltype(std::declval<V>().
globalSize());
86 if constexpr (Dune::Std::is_detected<HasLocalSize,Impl>::value)
87 return impl_.localSize();
95 if constexpr (Dune::Std::is_detected<HasGlobalSize,Impl>::value)
96 return impl_.globalSize();
102 template <
class Basis>
109 template <
class Basis>
117 template <
class Basis>
118 void init(Basis
const& basis,
bool clear)
120 impl_.init(basis, clear);
121 state_ = clear ? VectorState::synchronized : VectorState::unknown;
128 state_ = VectorState::unknown;
143 REQUIRES(Concepts::MultiIndex<Index>)>
144 void insert(Index
const& idx,
typename Impl::value_type
const& value, Assign assign = {})
146 test_exit_dbg(state_ == VectorStateOf_t<Assign>::value ||
147 state_ == VectorState::unknown ||
148 state_ == VectorState::synchronized,
149 "Vector in invalid state {} for insertion by {}.", to_string(state_), Dune::className<Assign>());
151 impl_.insert(idx, value, assign);
154 state_ = VectorStateOf_t<Assign>::value;
158 template <
class Index,
159 REQUIRES(Concepts::MultiIndex<Index>)>
160 void set(Index
const& idx,
typename Impl::value_type
const& value)
166 template <
class Index,
167 REQUIRES(Concepts::MultiIndex<Index>)>
168 void add(Index
const& idx,
typename Impl::value_type
const& value)
174 template <
class Index,
175 REQUIRES(Concepts::MultiIndex<Index>)>
176 typename Impl::value_type
get(Index
const& idx)
const 178 const_cast<Self*
>(
this)->synchronize();
179 return impl_.at(idx);
197 template <
class LocalView,
class Node,
class Buffer,
198 REQUIRES(Concepts::LocalView<LocalView>),
199 REQUIRES(Concepts::BasisNode<Node>)>
202 test_exit(state_ == VectorState::unknown ||
203 state_ == VectorState::synchronized,
204 "Vector in invalid state {} for gather operations.", to_string(state_));
206 const_cast<Self*
>(
this)->synchronize();
208 buffer.
resize(node.size());
209 impl_.gather(nodeIndices(localView, node), buffer.begin());
214 REQUIRES(Concepts::LocalView<LocalView>)>
217 gather(localView, localView.tree(), buffer);
221 template <
class Index,
class Buffer,
222 REQUIRES(Concepts::MultiIndex<Index>)>
223 void gather(std::vector<Index>
const& indices, Buffer& buffer)
225 test_exit(state_ == VectorState::unknown ||
226 state_ == VectorState::synchronized,
227 "Vector in invalid state {} for gather operations.", to_string(state_));
229 const_cast<Self*
>(
this)->synchronize();
231 buffer.
resize(indices.size());
232 impl_.gather(indices, buffer.begin());
255 template <
class LocalView,
class Node,
class NodeVector,
class MaskRange,
class Assign,
256 REQUIRES(Concepts::LocalView<LocalView>),
257 REQUIRES(Concepts::BasisNode<Node>)>
259 MaskRange
const& mask, Assign assign)
261 test_exit(state_ == VectorStateOf_t<Assign>::value ||
262 state_ == VectorState::unknown ||
263 state_ == VectorState::synchronized,
264 "Vector in invalid state {} for insertion by {}.", to_string(state_), Dune::className<Assign>());
266 assert(localVector.size() == node.size());
269 impl_.scatter(nodeIndices(localView, node), localVector, mask, assign);
272 state_ = VectorStateOf_t<Assign>::value;
278 template <
class LocalView,
class Node,
class NodeVector,
class Assign,
279 REQUIRES(Concepts::LocalView<LocalView>),
280 REQUIRES(Concepts::BasisNode<Node>)>
281 void scatter(
LocalView const& localView, Node
const& node, NodeVector
const& localVector, Assign assign)
288 template <
class LocalView,
class LocalVector,
class Assign,
289 REQUIRES(Concepts::LocalView<LocalView>)>
290 void scatter(LocalView
const& localView, LocalVector
const& localVector, Assign assign)
292 scatter(localView, localView.tree(), localVector, assign);
296 template <
class Index,
class Buffer,
297 REQUIRES(Concepts::MultiIndex<Index>)>
298 void scatter(std::vector<Index>
const& indices, Buffer
const& values)
303 state_ = VectorStateOf_t<Assigner::assign>::value;
313 template <
class LocalInd,
class Func>
314 void forEach(LocalInd
const& localInd, Func&& func)
317 impl_.forEach(localInd, FWD(func));
327 template <
class LocalInd,
class Func>
328 void forEach(LocalInd
const& localInd, Func&& func)
const 330 const_cast<Self*
>(
this)->synchronize();
331 impl_.
forEach(localInd, FWD(func));
338 if (state_ != VectorState::synchronized)
341 state_ = VectorState::synchronized;
345 static std::string to_string(VectorState state)
348 case VectorState::synchronized:
return "synchronized";
349 case VectorState::insert_values:
return "insert_values";
350 case VectorState::add_values:
return "add_values";
351 default:
return "unknown";
361 VectorState state_ = VectorState::unknown;
367 template <
class T,
template <
class>
class Impl>
370 template <
class Vec,
class UnaryFunction>
371 static void impl (Vec&& vec, UnaryFunction f)
374 Recursive::forEach(vec.impl(), f);
378 template <
class T,
template <
class>
class Impl>
381 template <
class Operation,
class... Ts>
385 Recursive::transform(vecOut.
impl(), op, vecIn.
impl()...);
389 template <
class S,
template <
class>
class Impl>
392 template <
class In1,
class In2,
class T,
class BinOp1,
class BinOp2>
393 static T
impl (In1
const& in1, In2
const& in2, T
init, BinOp1 op1, BinOp2 op2)
396 return Recursive::innerProduct(in1.impl(), in2.impl(), std::move(init), op1, op2);
405 template <
class T,
class Facade>
406 struct VectorTypeImpl;
408 template <
class T,
class S,
template <
class>
class Impl>
409 struct VectorTypeImpl<T,VectorFacade<S,Impl>>
417 template <
class T,
class Facade>
418 using VectorType_t =
typename Impl::VectorTypeImpl<T,Facade>::type;
std::size_t localSize() const
Return the number of entries in the local part of the vector.
Definition: VectorFacade.hpp:84
void gather(LocalView const &localView, Node const &node, Buffer &buffer) const
Extract values from the vector referring to the given local indices and store it into a buffer...
Definition: VectorFacade.hpp:200
void finish()
Finish the insertion of values, see init()
Definition: VectorFacade.hpp:125
The basic container that stores a base vector and a corresponding basis.
Definition: VectorFacade.hpp:38
std::size_t globalSize() const
Return the number of entries in the global vector.
Definition: VectorFacade.hpp:93
Definition: Assigner.hpp:16
Global basis defined on a pre-basis.
Definition: GlobalBasis.hpp:44
Definition: ForEach.hpp:17
void scatter(LocalView const &localView, Node const &node, NodeVector const &localVector, MaskRange const &mask, Assign assign)
Insert a block of values into the vector (add or overwrite to existing values)
Definition: VectorFacade.hpp:258
void forEach(LocalInd const &localInd, Func &&func)
Apply func to each value at given indices localInd.
Definition: VectorFacade.hpp:314
VectorFacade(GlobalBasis const &basis)
Definition: VectorFacade.hpp:66
Definition: AdaptBase.hpp:6
constexpr bool LocalView
A Dune::Functions::LocalView type.
Definition: Concepts.hpp:181
The restriction of a finite element basis to a single element.
Definition: LocalView.hpp:14
General implementation of recursive inner-product.
Definition: InnerProduct.hpp:16
void init(Basis const &basis, bool clear)
Definition: VectorFacade.hpp:118
void add(Index const &idx, typename Impl::value_type const &value)
See insert for assignment operation Assigner::plus_assign.
Definition: VectorFacade.hpp:168
void forEach(LocalInd const &localInd, Func &&func) const
Apply func to each value at given indices localInd.
Definition: VectorFacade.hpp:328
A container-like data-structure not storing anything and with empty implementations in many container...
Definition: FakeContainer.hpp:34
void insert(Index const &idx, typename Impl::value_type const &value, Assign assign={})
Insert a single value into the matrix (add or overwrite to existing value)
Definition: VectorFacade.hpp:144
void resize(Basis const &basis)
Resize the vector to the size of the basis.
Definition: VectorFacade.hpp:103
IndexDist const & indexDistribution() const
Return the index distribution.
Definition: GlobalBasis.hpp:148
Impl const & impl() const
Return the underlying linear algebra backend.
Definition: VectorFacade.hpp:73
void resizeZero(Basis const &basis)
Resize the vector to the size of the basis and set to zero.
Definition: VectorFacade.hpp:110
void scatter(LocalView const &localView, Node const &node, NodeVector const &localVector, Assign assign)
Call scatter with MaskRange set to FakeContainer.
Definition: VectorFacade.hpp:281
constexpr bool GlobalBasis
A Dune::Functions::GlobalBasis type.
Definition: Concepts.hpp:189
void scatter(LocalView const &localView, LocalVector const &localVector, Assign assign)
Call scatter with Node given by the tree of the localView.
Definition: VectorFacade.hpp:290
void scatter(std::vector< Index > const &indices, Buffer const &values)
Call scatter the values associated to the indices into the vector.
Definition: VectorFacade.hpp:298
void gather(std::vector< Index > const &indices, Buffer &buffer)
Call gather the values associated to the indices into the buffer.
Definition: VectorFacade.hpp:223
Definition: Assigner.hpp:7