FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
nonlinear_functional.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8#include <kernel/global/gate.hpp>
9#include <kernel/global/vector.hpp>
10
11namespace FEAT
12{
13 namespace Global
14 {
23 template<typename LocalNonlinearFunctional_, typename RowMirrorType_, typename ColMirrorType_>
25 {
26 public:
28 typedef typename LocalNonlinearFunctional_::DataType DataType;
30 typedef typename LocalNonlinearFunctional_::IndexType IndexType;
31
33 typedef typename LocalNonlinearFunctional_::VectorTypeL LocalVectorTypeL;
35 typedef typename LocalNonlinearFunctional_::VectorTypeR LocalVectorTypeR;
37 typedef typename LocalNonlinearFunctional_::FilterType LocalFilterType;
38
45
50
53
55 static constexpr int BlockHeight = LocalNonlinearFunctional_::BlockHeight;
57 static constexpr int BlockWidth = LocalNonlinearFunctional_::BlockWidth;
58
59 protected:
65 LocalNonlinearFunctional_& _nonlinear_functional;
66
67 public:
81 explicit NonlinearFunctional(GateRowType* row_gate, GateColType* col_gate,
82 LocalNonlinearFunctional_& nonlinear_functional) :
83 _row_gate(row_gate),
84 _col_gate(col_gate),
85 _nonlinear_functional(nonlinear_functional)
86 {
87 }
88
93
96 {
97 }
98
104 LocalNonlinearFunctional_& local()
105 {
107 }
108
114 const LocalNonlinearFunctional_& local() const
115 {
117 }
118
122 const Dist::Comm* get_comm() const
123 {
124 return _row_gate != nullptr ? _row_gate->get_comm() : nullptr;
125 }
126
133 {
134 return _nonlinear_functional.get_num_func_evals();
135 }
136
143 {
144 return _nonlinear_functional.get_num_grad_evals();
145 }
146
153 {
154 return _nonlinear_functional.get_num_hess_evals();
155 }
156
162 {
163 _nonlinear_functional.reset_num_evals();
164 }
165
172 {
173 return VectorTypeL(_row_gate, _nonlinear_functional.create_vector_l());
174 }
175
182 {
183 return VectorTypeR(_col_gate, _nonlinear_functional.create_vector_r());
184 }
185
189 void init()
190 {
191 _nonlinear_functional.init_pre_sync();
192 // Synchronize the necessary parts
193 while(!_nonlinear_functional.sync_scalars.empty())
194 {
195 auto it = _nonlinear_functional.sync_scalars.begin();
196 if(_col_gate != nullptr)
197 {
198 *((*it).second) = _col_gate->sum(*((*it).second));
199 }
200 _nonlinear_functional.sync_scalars.erase(it);
201 }
202
203 if(_col_gate != nullptr)
204 {
205 for(auto* it:_nonlinear_functional.sync_vecs)
206 {
207 _col_gate->sync_0(*it);
208 }
209 }
210 _nonlinear_functional.init_post_sync();
211 }
212
223 void prepare(const VectorTypeR& vec_state, FilterType& filter)
224 {
225
226 // Prepare the patch local nonlinear functional, computing everything that does need synchronising
227 _nonlinear_functional.prepare_pre_sync(vec_state.local(), filter.local());
228
229 // Synchronize the necessary parts
230 while(!_nonlinear_functional.sync_scalars.empty())
231 {
232 auto it = _nonlinear_functional.sync_scalars.begin();
233 if(_col_gate != nullptr)
234 {
235 *((*it).second) = _col_gate->sum(*((*it).second));
236 }
237 _nonlinear_functional.sync_scalars.erase(it);
238 }
239
240 if(_col_gate != nullptr)
241 {
242 for(auto* it:_nonlinear_functional.sync_vecs)
243 {
244 _col_gate->sync_0(*it);
245 }
246 }
247
248 // Prepare the rest of the functional
249 _nonlinear_functional.prepare_post_sync(vec_state.local(), filter.local());
250
251 // Sync the filter vector in the SlipFilter
252 if(_col_gate != nullptr )
253 {
254 // For all slip filters...
255 for(auto& it : filter.local().template at<0>())
256 {
257 // get the filter vector
258 auto& slip_filter_vector = it.second.get_filter_vector();
259
260 if(slip_filter_vector.used_elements() > 0)
261 {
262 // Temporary DenseVector for syncing
263 LocalVectorTypeR tmp(slip_filter_vector.size(), DataType(0));
264
265 auto* tmp_elements = tmp.template elements<LAFEM::Perspective::native>();
266 auto* sfv_elements = slip_filter_vector.template elements<LAFEM::Perspective::native>();
267
268 // Copy sparse filter vector contents to DenseVector
269 for(Index isparse(0); isparse < slip_filter_vector.used_elements(); ++isparse)
270 {
271 Index idense(slip_filter_vector.indices()[isparse]);
272 tmp_elements[idense] = sfv_elements[isparse];
273 }
274
275 _col_gate->sync_0(tmp);
276 // Copy sparse filter vector contents to DenseVector
277 for(Index isparse(0); isparse < slip_filter_vector.used_elements(); ++isparse)
278 {
279 Index idense(slip_filter_vector.indices()[isparse]);
280 tmp_elements[idense].normalize();
281 sfv_elements[isparse] = tmp_elements[idense];
282
283 }
284 }
285
286 else
287 {
288 // Temporary DenseVector for syncing
289 LocalVectorTypeR tmp(slip_filter_vector.size(), DataType(0));
290 _col_gate->sync_0(tmp);
291 }
292 }
293 } // col_gate
294 } // prepare()
295
304 {
305 // Compute total number of rows and columns
306 auto vec_r = create_vector_r();
307 vec_r.format(DataType(1));
308
309 return Index(vec_r.norm2sqr());
310 }
311
320 {
321 // Compute total number of rows and rows
322 auto vec_l = create_vector_l();
323 vec_l.format(DataType(1));
324
325 return Index(vec_l.norm2sqr());
326 }
327
339 {
340 // As the penalty term is quadratic, we have to compute the local functional values separately for summing
341 // them up over all ranks
342 const bool add_penalty_fval(false);
343 _nonlinear_functional.eval_fval_grad(fval, grad.local(), add_penalty_fval);
344
345 // Sum up over all patches
346 if(_row_gate != nullptr)
347 fval = _row_gate->sum(fval);
348
349 // Add the penalty term
350 if(get_penalty_param() > DataType(0))
351 {
352 DataType constraint(get_constraint());
353 fval += get_penalty_param()*DataType(0.5)*Math::sqr(constraint);
354 }
355
356 // Synchronize the gradient vector
357 grad.sync_0();
358 }
359
366 {
367 return _nonlinear_functional.get_penalty_param();
368 }
369
374 {
375 _nonlinear_functional.set_penalty_param(fac);
376 }
377
384 {
385 return _nonlinear_functional.get_constraint();
386 }
387
394 {
395 return _nonlinear_functional.compute_constraint();
396 }
397
398 };
399 } // namespace Global
400} // namespace FEAT
Communicator class.
Definition: dist.hpp:1349
Global Filter wrapper class template.
Definition: filter.hpp:21
Global gate implementation.
Definition: gate.hpp:51
void sync_0(LocalVector_ &vector) const
Synchronizes a type-0 vector, resulting in a type-1 vector.
Definition: gate.hpp:408
const Dist::Comm * get_comm() const
Returns a const pointer to the underlying communicator.
Definition: gate.hpp:138
DataType sum(DataType x) const
Computes a reduced sum over all processes.
Definition: gate.hpp:553
Global NonlinearFunctional wrapper class template.
Gate< LocalVectorTypeR, ColMirrorType_ > GateColType
Global Gate for right-vectors.
void set_penalty_param(const DataType fac)
Sets the local functional's penalty parameter.
VectorTypeL create_vector_l() const
Creates an empty left-vector of the correct size.
virtual ~NonlinearFunctional()
Empty virtual destructor.
LocalNonlinearFunctional_::FilterType LocalFilterType
The local functionals filter type.
LocalNonlinearFunctional_ & local()
Gets the local nonlinear functional.
Vector< LocalVectorTypeR, ColMirrorType_ > VectorTypeR
The associated global right-vector type.
DataType get_penalty_param() const
Get the penalty parameter from the local nonlinear functional.
void reset_num_evals()
Resets the evaluation counters.
LocalNonlinearFunctional_::IndexType IndexType
Index type of the local functional.
Index columns()
Gets the number of columns.
Index get_num_func_evals() const
Get the number of times compute_func() was called.
LocalNonlinearFunctional_ & _nonlinear_functional
The underlying local nonlinear functional.
LocalNonlinearFunctional_::VectorTypeL LocalVectorTypeL
The local functionals left-vector type.
NonlinearFunctional(const NonlinearFunctional &)=delete
Explicitly delete copy constructor.
GateColType * _col_gate
Gate for syncing column vectors.
Index rows()
Gets the number of rows.
const LocalNonlinearFunctional_ & local() const
Gets the local nonlinear functional, const version.
Vector< LocalVectorTypeL, RowMirrorType_ > VectorTypeL
The associated global left-vector type.
LocalNonlinearFunctional_::DataType DataType
Floating point data type of the local functional.
void init()
Functionality that cannot be done in the constructor.
void prepare(const VectorTypeR &vec_state, FilterType &filter)
Prepares the operator for evaluation by setting the current state.
VectorTypeR create_vector_r() const
Creates an empty right-vector of the correct size.
void eval_fval_grad(DataType &fval, VectorTypeL &grad)
Computes the functional's value and gradient at the current state.
Filter< LocalFilterType, RowMirrorType_ > FilterType
The associated global filter type.
GateRowType * _row_gate
Gate for syncing row vectors.
const Dist::Comm * get_comm() const
Returns the functional's communicator.
static constexpr int BlockWidth
The global nonlinear functional's Blockwidth ist the same as the local nonlinear functional's.
Index get_num_grad_evals() const
Get the number of times compute_grad() was called.
NonlinearFunctional(GateRowType *row_gate, GateColType *col_gate, LocalNonlinearFunctional_ &nonlinear_functional)
Constructor.
NonlinearFunctional()=delete
Explicitly delete default constructor.
Index get_num_hess_evals() const
Get the number of times compute_hess() was called.
Gate< LocalVectorTypeL, RowMirrorType_ > GateRowType
Global Gate for left-vectors.
LocalNonlinearFunctional_::VectorTypeR LocalVectorTypeR
The local functionals right-vector type.
VectorTypeL GradientType
The a gradient vector is the output of the operators compute_grad() function and thus a left-vector.
DataType get_constraint()
Get the constraint from the local nonlinear functional.
static constexpr int BlockHeight
The global nonlinear functional's Blockheight ist the same as the local nonlinear functional's.
DataType compute_constraint()
Computes the constraint and returns it.
Global vector wrapper class template.
Definition: vector.hpp:68
LocalVector_ & local()
Returns a reference to the internal local LAFEM vector object.
Definition: vector.hpp:121
T_ sqr(T_ x)
Returns the square of a value.
Definition: math.hpp:95
FEAT namespace.
Definition: adjactor.hpp:12
@ grad
specifies whether the space should supply basis function gradients
std::uint64_t Index
Index data type.