Dune-Fufem 2.11
Loading...
Searching...
No Matches
dunefunctionsfunctionalassembler.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUFEM_ASSEMBERS_DUNEFUNCTIONSFUNCTIONALASSEMBLER_HH
8#define DUNE_FUFEM_ASSEMBERS_DUNEFUNCTIONSFUNCTIONALASSEMBLER_HH
9
10#ifndef DUNE_FUFEM_DISABLE_HEADER_DEPRECATION
11#warning This header is deprecated and will be removed after 2.11. Use the global assemblers from dune/assembler/defaultglobalassembler.hh instead.
12#endif
13
24
25#include <dune/istl/bvector.hh>
26
27#include <dune/grid/common/capabilities.hh>
28
29#include <dune/functions/backends/concepts.hh>
31
34
35
36
37namespace Dune {
38namespace Fufem {
39
40
47template <class TestBasis>
48class
49[[deprecated("This class will be removed after 2.11. Use Dune::Assembler::Assembler together with Dune::Fufem::Forms based local assemblers instead.")]]
51{
52 using GridView = typename TestBasis::GridView;
53
54 // SFINAE expression check for preprocess() method of local assembler
55 template <class LocalAssembler>
56 using LocalAssemblerPreprocess = decltype(std::declval<LocalAssembler>().preprocess(std::declval<typename TestBasis::LocalView>()));
57
58public:
60 [[deprecated("This class will be removed after 2.11. Use Dune::Assembler::Assembler together with Dune::Fufem::Forms based local assemblers instead.")]]
61 DuneFunctionsFunctionalAssembler(const TestBasis& tBasis) :
62 testBasis_(tBasis)
63 {}
64
65 template<class V>
66 static decltype(auto) toVectorBackend(V& v)
67 {
68 if constexpr (Dune::models<Dune::Functions::Concept::VectorBackend<TestBasis>, decltype(v)>())
69 return v;
70 else
72 }
73
74 template <class Vector, class LocalAssembler>
75 void assembleBulkEntries(Vector&& vector, LocalAssembler&& localAssembler) const
76 {
77 auto&& vectorBackend = toVectorBackend(vector);
78
79 auto testLocalView = testBasis_.localView();
80
81 if constexpr(Dune::Std::is_detected_v<LocalAssemblerPreprocess, LocalAssembler>)
82 localAssembler.preprocess(testLocalView);
83
84 using Field = std::decay_t<decltype(vectorBackend[testLocalView.index(0)])>;
86
87 auto localVector = LocalVector(testLocalView.maxSize());
88
89 for (const auto& element : elements(testBasis_.gridView()))
90 {
91 testLocalView.bind(element);
92
93 for (size_t i=0; i<testLocalView.tree().size(); ++i)
94 {
95 auto localRow = testLocalView.tree().localIndex(i);
96 localVector[localRow] = 0;
97 }
98
99 localAssembler(element, localVector, testLocalView);
100
101 // Add element stiffness matrix onto the global stiffness matrix
102 for (size_t i=0; i<testLocalView.tree().size(); ++i)
103 {
104 auto localRow = testLocalView.tree().localIndex(i);
105 auto row = testLocalView.index(localRow);
106 vectorBackend[row] += localVector[localRow];
107 }
108 }
109 }
110
111 template <class Vector, class LocalAssembler, class ElementPartition>
112 void assembleBulkEntries(Vector&& vector, LocalAssembler&& localAssembler, const ElementPartition& elementPartition, std::size_t threadCount) const
113 {
114 static_assert(Dune::Capabilities::viewThreadSafe<typename GridView::Grid>::v, "Trying to use thread parallel assembler but grid is not viewThreadSafe.");
115
116 auto&& vectorBackend = toVectorBackend(vector);
117
118 parallelAlgorithm(threadCount, [&, localAssembler](auto parallel) mutable {
119 auto testLocalView = testBasis_.localView();
120
121 if constexpr(Dune::Std::is_detected_v<LocalAssemblerPreprocess, LocalAssembler>)
122 localAssembler.preprocess(testLocalView);
123
124 using Field = std::decay_t<decltype(vectorBackend[testLocalView.index(0)])>;
126
127 auto localVector = LocalVector(testLocalView.maxSize());
128
129 parallel.coloredForEach(elementPartition, [&](const auto& element) {
130 testLocalView.bind(element);
131
132 for (size_t i=0; i<testLocalView.tree().size(); ++i)
133 {
134 auto localRow = testLocalView.tree().localIndex(i);
135 localVector[localRow] = 0;
136 }
137
138 localAssembler(element, localVector, testLocalView);
139
140 // Add element stiffness matrix onto the global stiffness matrix
141 for (size_t i=0; i<testLocalView.tree().size(); ++i)
142 {
143 auto localRow = testLocalView.tree().localIndex(i);
144 auto row = testLocalView.index(localRow);
145 vectorBackend[row] += Dune::Fufem::asNumber(localVector[localRow]);
146 }
147 });
148 });
149 }
150
151
152 template <class Vector, class LocalAssembler>
153 void assembleBulk(Vector&& vector, LocalAssembler&& localAssembler) const
154 {
155 auto&& vectorBackend = toVectorBackend(vector);
156
157 vectorBackend.resize(testBasis_);
158 vectorBackend = 0.0;
159 assembleBulkEntries(vectorBackend, std::forward<LocalAssembler>(localAssembler));
160 }
161
162 template <class Vector, class LocalAssembler, class ElementPartition>
163 void assembleBulk(Vector&& vector, LocalAssembler&& localAssembler, const ElementPartition& elementPartition, std::size_t threadCount) const
164 {
165 auto&& vectorBackend = toVectorBackend(vector);
166
167 vectorBackend.resize(testBasis_);
168 vectorBackend = 0.0;
169 assembleBulkEntries(vectorBackend, std::forward<LocalAssembler>(localAssembler), elementPartition, threadCount);
170 }
171
172
173protected:
174 const TestBasis& testBasis_;
175};
176
177
185template <class TestBasis>
186[[deprecated("This function will be removed after 2.11. Use Dune::Assembler::Assembler together with Dune::Fufem::Forms based local assemblers instead.")]]
187auto duneFunctionsFunctionalAssembler(const TestBasis& testBasis)
188{
192}
193
194
195
196} // namespace Fufem
197} // namespace Dune
198
199
200#endif // DUNE_FUFEM_ASSEMBERS_DUNEFUNCTIONSFUNCTIONALASSEMBLER_HH
201
auto istlVectorBackend(Vector &v)
int size() const
#define DUNE_NO_DEPRECATED_END
#define DUNE_NO_DEPRECATED_BEGIN
Generic global assembler for functionals on a gridview.
Definition dunefunctionsfunctionalassembler.hh:51
void assembleBulkEntries(Vector &&vector, LocalAssembler &&localAssembler) const
Definition dunefunctionsfunctionalassembler.hh:75
const TestBasis & testBasis_
Definition dunefunctionsfunctionalassembler.hh:174
static decltype(auto) toVectorBackend(V &v)
Definition dunefunctionsfunctionalassembler.hh:66
DuneFunctionsFunctionalAssembler(const TestBasis &tBasis)
create assembler for grid
Definition dunefunctionsfunctionalassembler.hh:61
void assembleBulk(Vector &&vector, LocalAssembler &&localAssembler, const ElementPartition &elementPartition, std::size_t threadCount) const
Definition dunefunctionsfunctionalassembler.hh:163
void assembleBulkEntries(Vector &&vector, LocalAssembler &&localAssembler, const ElementPartition &elementPartition, std::size_t threadCount) const
Definition dunefunctionsfunctionalassembler.hh:112
void assembleBulk(Vector &&vector, LocalAssembler &&localAssembler) const
Definition dunefunctionsfunctionalassembler.hh:153
T forward(T... args)
void parallelAlgorithm(std::size_t threadCount, F &&threadImp)
Utility for implementing parallel algorithms based on colored entity partitions.
Definition parallelalgorithm.hh:147
auto duneFunctionsFunctionalAssembler(const TestBasis &testBasis)
Create DuneFunctionsFunctionalAssembler.
Definition dunefunctionsfunctionalassembler.hh:187
const T & asNumber(const T &t)
Forward given value as number.
Definition asnumber.hh:23