Debian Codegraph @

include/deal.II/base/function.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 1998 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_function_templates_h
16 #define dealii_function_templates_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/function.h>
22 #include <deal.II/base/point.h>
23 #include <deal.II/base/tensor_function.h>
24
25 #include <deal.II/lac/vector.h>
26
27 #include <vector>
28
29 DEAL_II_NAMESPACE_OPEN
30
31
32 template <int dim, typename RangeNumberType>
33 const unsigned int Function<dim, RangeNumberType>::dimension;
34
35
36 template <int dim, typename RangeNumberType>
37 Function<dim, RangeNumberType>::Function(
38 const unsigned int n_components,
39 const typename Function<dim, RangeNumberType>::time_type initial_time)
40 : FunctionTime<typename Function<dim, RangeNumberType>::time_type>(
41 initial_time)
42 , n_components(n_components)
43 {
44 // avoid the construction of function objects that don't return any
45 // values. This doesn't make much sense in the first place, but will lead
46 // to odd errors later on (happened to me in fact :-)
47 Assert(n_components > 0, ExcZero());
48 }
49
50
51
52 template <int dim, typename RangeNumberType>
53 Function<dim, RangeNumberType> &
54 Function<dim, RangeNumberType>::operator=(const Function &f)
55 {
56 (void)f;
57 AssertDimension(n_components, f.n_components);
58 return *this;
59 }
60
61
62 template <int dim, typename RangeNumberType>
63 RangeNumberType
64 Function<dim, RangeNumberType>::value(const Point<dim> &,
65 const unsigned int) const
66 {
67 Assert(false, ExcPureFunctionCalled());
68 return 0;
69 }
70
71
72 template <int dim, typename RangeNumberType>
73 void
74 Function<dim, RangeNumberType>::vector_value(const Point<dim> &p,
75 Vector<RangeNumberType> &v) const
76 {
77 AssertDimension(v.size(), this->n_components);
78 for (unsigned int i = 0; i < this->n_components; ++i)
79 v(i) = value(p, i);
80 }
81
82
83 template <int dim, typename RangeNumberType>
84 void
85 Function<dim, RangeNumberType>::value_list(
86 const std::vector<Point<dim>> &points,
87 std::vector<RangeNumberType> &values,
88 const unsigned int component) const
89 {
90 // check whether component is in the valid range is up to the derived
91 // class
92 Assert(values.size() == points.size(),
93 ExcDimensionMismatch(values.size(), points.size()));
94
95 for (unsigned int i = 0; i < points.size(); ++i)
96 values[i] = this->value(points[i], component);
97 }
98
99
100 template <int dim, typename RangeNumberType>
101 void
102 Function<dim, RangeNumberType>::vector_value_list(
103 const std::vector<Point<dim>> &points,
104 std::vector<Vector<RangeNumberType>> &values) const
105 {
106 // check whether component is in the valid range is up to the derived
107 // class
108 Assert(values.size() == points.size(),
109 ExcDimensionMismatch(values.size(), points.size()));
110
111 for (unsigned int i = 0; i < points.size(); ++i)
112 this->vector_value(points[i], values[i]);
113 }
114
115
116 template <int dim, typename RangeNumberType>
117 void
118 Function<dim, RangeNumberType>::vector_values(
119 const std::vector<Point<dim>> &points,
120 std::vector<std::vector<RangeNumberType>> &values) const
121 {
122 const unsigned int n = this->n_components;
123 AssertDimension(values.size(), n);
124 for (unsigned int i = 0; i < n; ++i)
125 value_list(points, values[i], i);
126 }
127
128
129 template <int dim, typename RangeNumberType>
130 Tensor<1, dim, RangeNumberType>
131 Function<dim, RangeNumberType>::gradient(const Point<dim> &,
132 const unsigned int) const
133 {
134 Assert(false, ExcPureFunctionCalled());
135 return Tensor<1, dim, RangeNumberType>();
136 }
137
138
139 template <int dim, typename RangeNumberType>
140 void
141 Function<dim, RangeNumberType>::vector_gradient(
142 const Point<dim> &p,
143 std::vector<Tensor<1, dim, RangeNumberType>> &v) const
144 {
145 AssertDimension(v.size(), this->n_components);
146 for (unsigned int i = 0; i < this->n_components; ++i)
147 v[i] = gradient(p, i);
148 }
149
150
151 template <int dim, typename RangeNumberType>
152 void
153 Function<dim, RangeNumberType>::gradient_list(
154 const std::vector<Point<dim>> &points,
155 std::vector<Tensor<1, dim, RangeNumberType>> &gradients,
156 const unsigned int component) const
157 {
158 Assert(gradients.size() == points.size(),
159 ExcDimensionMismatch(gradients.size(), points.size()));
160
161 for (unsigned int i = 0; i < points.size(); ++i)
162 gradients[i] = gradient(points[i], component);
163 }
164
165
166 template <int dim, typename RangeNumberType>
167 void
168 Function<dim, RangeNumberType>::vector_gradient_list(
169 const std::vector<Point<dim>> &points,
170 std::vector<std::vector<Tensor<1, dim, RangeNumberType>>> &gradients) const
171 {
172 Assert(gradients.size() == points.size(),
173 ExcDimensionMismatch(gradients.size(), points.size()));
174
175 for (unsigned int i = 0; i < points.size(); ++i)
176 {
177 Assert(gradients[i].size() == n_components,
178 ExcDimensionMismatch(gradients[i].size(), n_components));
179 vector_gradient(points[i], gradients[i]);
180 }
181 }
182
183
184 template <int dim, typename RangeNumberType>
185 void
186 Function<dim, RangeNumberType>::vector_gradients(
187 const std::vector<Point<dim>> &points,
188 std::vector<std::vector<Tensor<1, dim, RangeNumberType>>> &values) const
189 {
190 const unsigned int n = this->n_components;
191 AssertDimension(values.size(), n);
192 for (unsigned int i = 0; i < n; ++i)
193 gradient_list(points, values[i], i);
194 }
195
196
197
198 template <int dim, typename RangeNumberType>
199 RangeNumberType
200 Function<dim, RangeNumberType>::laplacian(const Point<dim> &,
201 const unsigned int) const
202 {
203 Assert(false, ExcPureFunctionCalled());
204 return 0;
205 }
206
207
208 template <int dim, typename RangeNumberType>
209 void
210 Function<dim, RangeNumberType>::vector_laplacian(
211 const Point<dim> &,
212 Vector<RangeNumberType> &) const
213 {
214 Assert(false, ExcPureFunctionCalled());
215 }
216
217
218
219 template <int dim, typename RangeNumberType>
220 void
221 Function<dim, RangeNumberType>::laplacian_list(
222 const std::vector<Point<dim>> &points,
223 std::vector<RangeNumberType> &laplacians,
224 const unsigned int component) const
225 {
226 // check whether component is in the valid range is up to the derived
227 // class
228 Assert(laplacians.size() == points.size(),
229 ExcDimensionMismatch(laplacians.size(), points.size()));
230
231 for (unsigned int i = 0; i < points.size(); ++i)
232 laplacians[i] = this->laplacian(points[i], component);
233 }
234
235
236 template <int dim, typename RangeNumberType>
237 void
238 Function<dim, RangeNumberType>::vector_laplacian_list(
239 const std::vector<Point<dim>> &points,
240 std::vector<Vector<RangeNumberType>> &laplacians) const
241 {
242 // check whether component is in the valid range is up to the derived
243 // class
244 Assert(laplacians.size() == points.size(),
245 ExcDimensionMismatch(laplacians.size(), points.size()));
246
247 for (unsigned int i = 0; i < points.size(); ++i)
248 this->vector_laplacian(points[i], laplacians[i]);
249 }
250
251
252 template <int dim, typename RangeNumberType>
253 SymmetricTensor<2, dim, RangeNumberType>
254 Function<dim, RangeNumberType>::hessian(const Point<dim> &,
255 const unsigned int) const
256 {
257 Assert(false, ExcPureFunctionCalled());
258 return SymmetricTensor<2, dim, RangeNumberType>();
259 }
260
261
262 template <int dim, typename RangeNumberType>
263 void
264 Function<dim, RangeNumberType>::vector_hessian(
265 const Point<dim> &p,
266 std::vector<SymmetricTensor<2, dim, RangeNumberType>> &v) const
267 {
268 AssertDimension(v.size(), this->n_components);
269 for (unsigned int i = 0; i < this->n_components; ++i)
270 v[i] = hessian(p, i);
271 }
272
273
274 template <int dim, typename RangeNumberType>
275 void
276 Function<dim, RangeNumberType>::hessian_list(
277 const std::vector<Point<dim>> &points,
278 std::vector<SymmetricTensor<2, dim, RangeNumberType>> &hessians,
279 const unsigned int component) const
280 {
281 Assert(hessians.size() == points.size(),
282 ExcDimensionMismatch(hessians.size(), points.size()));
283
284 for (unsigned int i = 0; i < points.size(); ++i)
285 hessians[i] = hessian(points[i], component);
286 }
287
288
289 template <int dim, typename RangeNumberType>
290 void
291 Function<dim, RangeNumberType>::vector_hessian_list(
292 const std::vector<Point<dim>> &points,
293 std::vector<std::vector<SymmetricTensor<2, dim, RangeNumberType>>> &hessians)
294 const
295 {
296 Assert(hessians.size() == points.size(),
297 ExcDimensionMismatch(hessians.size(), points.size()));
298
299 for (unsigned int i = 0; i < points.size(); ++i)
300 {
301 Assert(hessians[i].size() == n_components,
302 ExcDimensionMismatch(hessians[i].size(), n_components));
303 vector_hessian(points[i], hessians[i]);
304 }
305 }
306
307
308
309 template <int dim, typename RangeNumberType>
310 std::size_t
311 Function<dim, RangeNumberType>::memory_consumption() const
312 {
313 // only simple data elements, so use sizeof operator
314 return sizeof(*this);
315 }
316
317
318
319 //---------------------------------------------------------------------------
320
321 namespace Functions
322 {
323 template <int dim, typename RangeNumberType>
324 ConstantFunction<dim, RangeNumberType>::ConstantFunction(
325 const RangeNumberType value,
326 const unsigned int n_components)
327 : Function<dim, RangeNumberType>(n_components)
328 , function_value_vector(n_components, value)
329 {}
330
331
332
333 template <int dim, typename RangeNumberType>
334 ConstantFunction<dim, RangeNumberType>::ConstantFunction(
335 const std::vector<RangeNumberType> &values)
336 : Function<dim, RangeNumberType>(values.size())
337 , function_value_vector(values)
338 {}
339
340
341
342 template <int dim, typename RangeNumberType>
343 ConstantFunction<dim, RangeNumberType>::ConstantFunction(
344 const Vector<RangeNumberType> &values)
345 : Function<dim, RangeNumberType>(values.size())
346 , function_value_vector(values.size())
347 {
348 Assert(values.size() == function_value_vector.size(),
349 ExcDimensionMismatch(values.size(), function_value_vector.size()));
350 std::copy(values.begin(), values.end(), function_value_vector.begin());
351 }
352
353
354
355 template <int dim, typename RangeNumberType>
356 ConstantFunction<dim, RangeNumberType>::ConstantFunction(
357 const RangeNumberType *begin_ptr,
358 const unsigned int n_components)
359 : Function<dim, RangeNumberType>(n_components)
360 , function_value_vector(n_components)
361 {
362 Assert(begin_ptr != nullptr, ExcMessage("Null pointer encountered!"));
363 std::copy(begin_ptr,
364 begin_ptr + n_components,
365 function_value_vector.begin());
366 }
367
368
369
370 template <int dim, typename RangeNumberType>
371 RangeNumberType
372 ConstantFunction<dim, RangeNumberType>::value(
373 const Point<dim> &,
374 const unsigned int component) const
375 {
376 AssertIndexRange(component, this->n_components);
377 return function_value_vector[component];
378 }
379
380
381
382 template <int dim, typename RangeNumberType>
383 void
384 ConstantFunction<dim, RangeNumberType>::vector_value(
385 const Point<dim> &,
386 Vector<RangeNumberType> &return_value) const
387 {
388 Assert(return_value.size() == this->n_components,
389 ExcDimensionMismatch(return_value.size(), this->n_components));
390
391 std::copy(function_value_vector.begin(),
392 function_value_vector.end(),
393 return_value.begin());
394 }
395
396
397
398 template <int dim, typename RangeNumberType>
399 void
400 ConstantFunction<dim, RangeNumberType>::value_list(
401 const std::vector<Point<dim>> &points,
402 std::vector<RangeNumberType> &return_values,
403 const unsigned int component) const
404 {
405 // To avoid warning of unused parameter
406 (void)points;
407 AssertIndexRange(component, this->n_components);
408 Assert(return_values.size() == points.size(),
409 ExcDimensionMismatch(return_values.size(), points.size()));
410
411 std::fill(return_values.begin(),
412 return_values.end(),
413 function_value_vector[component]);
414 }
415
416
417
418 template <int dim, typename RangeNumberType>
419 void
420 ConstantFunction<dim, RangeNumberType>::vector_value_list(
421 const std::vector<Point<dim>> &points,
422 std::vector<Vector<RangeNumberType>> &return_values) const
423 {
424 Assert(return_values.size() == points.size(),
425 ExcDimensionMismatch(return_values.size(), points.size()));
426
427 for (unsigned int i = 0; i < points.size(); ++i)
428 {
429 Assert(return_values[i].size() == this->n_components,
430 ExcDimensionMismatch(return_values[i].size(),
431 this->n_components));
432 std::copy(function_value_vector.begin(),
433 function_value_vector.end(),
434 return_values[i].begin());
435 };
436 }
437
438
439
440 template <int dim, typename RangeNumberType>
441 std::size_t
442 ConstantFunction<dim, RangeNumberType>::memory_consumption() const
443 {
444 // Here we assume RangeNumberType is a simple type.
445 return (sizeof(*this) + this->n_components * sizeof(RangeNumberType));
446 }
447
448
449
450 template <int dim, typename RangeNumberType>
451 Tensor<1, dim, RangeNumberType>
452 ConstantFunction<dim, RangeNumberType>::gradient(const Point<dim> &,
453 const unsigned int) const
454 {
455 return Tensor<1, dim, RangeNumberType>();
456 }
457
458
459
460 template <int dim, typename RangeNumberType>
461 void
462 ConstantFunction<dim, RangeNumberType>::vector_gradient(
463 const Point<dim> &,
464 std::vector<Tensor<1, dim, RangeNumberType>> &gradients) const
465 {
466 Assert(gradients.size() == this->n_components,
467 ExcDimensionMismatch(gradients.size(), this->n_components));
468
469 for (unsigned int c = 0; c < this->n_components; ++c)
470 gradients[c].clear();
471 }
472
473
474
475 template <int dim, typename RangeNumberType>
476 void
477 ConstantFunction<dim, RangeNumberType>::gradient_list(
478 const std::vector<Point<dim>> &points,
479 std::vector<Tensor<1, dim, RangeNumberType>> &gradients,
480 const unsigned int /*component*/) const
481 {
482 Assert(gradients.size() == points.size(),
483 ExcDimensionMismatch(gradients.size(), points.size()));
484
485 for (unsigned int i = 0; i < points.size(); ++i)
486 gradients[i].clear();
487 }
488
489
490
491 template <int dim, typename RangeNumberType>
492 void
493 ConstantFunction<dim, RangeNumberType>::vector_gradient_list(
494 const std::vector<Point<dim>> &points,
495 std::vector<std::vector<Tensor<1, dim, RangeNumberType>>> &gradients) const
496 {
497 Assert(gradients.size() == points.size(),
498 ExcDimensionMismatch(gradients.size(), points.size()));
499 for (unsigned int i = 0; i < points.size(); ++i)
500 {
501 Assert(gradients[i].size() == this->n_components,
502 ExcDimensionMismatch(gradients[i].size(), this->n_components));
503 for (unsigned int c = 0; c < this->n_components; ++c)
504 gradients[i][c].clear();
505 };
506 }
507
508
509
510 template <int dim, typename RangeNumberType>
511 SymmetricTensor<2, dim, RangeNumberType>
512 ConstantFunction<dim, RangeNumberType>::hessian(const Point<dim> &,
513 const unsigned int) const
514 {
515 return SymmetricTensor<2, dim, RangeNumberType>();
516 }
517
518
519
520 template <int dim, typename RangeNumberType>
521 RangeNumberType
522 ConstantFunction<dim, RangeNumberType>::laplacian(const Point<dim> &,
523 const unsigned int) const
524 {
525 return 0;
526 }
527
528
529
530 template <int dim, typename RangeNumberType>
531 ZeroFunction<dim, RangeNumberType>::ZeroFunction(
532 const unsigned int n_components)
533 : ConstantFunction<dim, RangeNumberType>(RangeNumberType(), n_components)
534 {}
535
536
537
538 template <int dim, typename RangeNumberType>
539 IdentityFunction<dim, RangeNumberType>::IdentityFunction()
540 : Function<dim, RangeNumberType>(dim)
541 {}
542
543
544
545 template <int dim, typename RangeNumberType>
546 RangeNumberType
547 IdentityFunction<dim, RangeNumberType>::value(
548 const Point<dim> &p,
549 const unsigned int component) const
550 {
551 AssertIndexRange(component, this->n_components);
552 return p[component];
553 }
554
555
556
557 template <int dim, typename RangeNumberType>
558 Tensor<1, dim, RangeNumberType>
559 IdentityFunction<dim, RangeNumberType>::gradient(
560 const Point<dim> &,
561 const unsigned int component) const
562 {
563 AssertIndexRange(component, this->n_components);
564 Tensor<1, dim, RangeNumberType> result;
565 result[component] = RangeNumberType(1);
566 return result;
567 }
568
569
570
571 template <int dim, typename RangeNumberType>
572 SymmetricTensor<2, dim, RangeNumberType>
573 IdentityFunction<dim, RangeNumberType>::hessian(const Point<dim> &,
574 const unsigned int) const
575 {
576 return SymmetricTensor<2, dim, RangeNumberType>();
577 }
578
579
580
581 template <int dim, typename RangeNumberType>
582 RangeNumberType
583 IdentityFunction<dim, RangeNumberType>::laplacian(const Point<dim> &,
584 const unsigned int) const
585 {
586 return 0;
587 }
588 } // namespace Functions
589
590 //---------------------------------------------------------------------------
591
592 template <int dim, typename RangeNumberType>
593 ComponentSelectFunction<dim, RangeNumberType>::ComponentSelectFunction(
594 const unsigned int selected,
595 const RangeNumberType value,
596 const unsigned int n_components)
597 : Functions::ConstantFunction<dim, RangeNumberType>(value, n_components)
598 , selected_components(std::make_pair(selected, selected + 1))
599 {}
600
601
602
603 template <int dim, typename RangeNumberType>
604 ComponentSelectFunction<dim, RangeNumberType>::ComponentSelectFunction(
605 const unsigned int selected,
606 const unsigned int n_components)
607 : Functions::ConstantFunction<dim, RangeNumberType>(1., n_components)
608 , selected_components(std::make_pair(selected, selected + 1))
609 {
610 AssertIndexRange(selected, n_components);
611 }
612
613
614
615 template <int dim, typename RangeNumberType>
616 ComponentSelectFunction<dim, RangeNumberType>::ComponentSelectFunction(
617 const std::pair<unsigned int, unsigned int> &selected,
618 const unsigned int n_components)
619 : Functions::ConstantFunction<dim, RangeNumberType>(1., n_components)
620 , selected_components(selected)
621 {
622 Assert(selected_components.first < selected_components.second,
623 ExcMessage("The upper bound of the interval must be larger than "
624 "the lower bound"));
625 Assert(selected_components.second <= n_components,
626 ExcMessage("The upper bound of the interval must be less than "
627 "or equal to the total number of vector components"));
628 }
629
630
631
632 template <int dim, typename RangeNumberType>
633 void
634 ComponentSelectFunction<dim, RangeNumberType>::substitute_function_value_with(
635 const Functions::ConstantFunction<dim, RangeNumberType> &f)
636 {
637 Point<dim> p;
638 for (unsigned int i = 0; i < this->function_value_vector.size(); ++i)
639 this->function_value_vector[i] = f.value(p, i);
640 }
641
642
643
644 template <int dim, typename RangeNumberType>
645 void
646 ComponentSelectFunction<dim, RangeNumberType>::vector_value(
647 const Point<dim> &,
648 Vector<RangeNumberType> &return_value) const
649 {
650 Assert(return_value.size() == this->n_components,
651 ExcDimensionMismatch(return_value.size(), this->n_components));
652
653 return_value = 0;
654 std::copy(this->function_value_vector.begin() + selected_components.first,
655 this->function_value_vector.begin() + selected_components.second,
656 return_value.begin() + selected_components.first);
657 }
658
659
660
661 template <int dim, typename RangeNumberType>
662 void
663 ComponentSelectFunction<dim, RangeNumberType>::vector_value_list(
664 const std::vector<Point<dim>> &points,
665 std::vector<Vector<RangeNumberType>> &values) const
666 {
667 Assert(values.size() == points.size(),
668 ExcDimensionMismatch(values.size(), points.size()));
669
670 for (unsigned int i = 0; i < points.size(); ++i)
671 ComponentSelectFunction<dim, RangeNumberType>::vector_value(points[i],
672 values[i]);
673 }
674
675
676
677 template <int dim, typename RangeNumberType>
678 std::size_t
679 ComponentSelectFunction<dim, RangeNumberType>::memory_consumption() const
680 {
681 // No new complex data structure is introduced here, just evaluate how much
682 // more memory is used *inside* the class via sizeof() and add that value to
683 // parent class's memory_consumption()
684 return (
685 sizeof(*this) - sizeof(Functions::ConstantFunction<dim, RangeNumberType>) +
686 Functions::ConstantFunction<dim, RangeNumberType>::memory_consumption());
687 }
688
689 //---------------------------------------------------------------------------
690
691 template <int dim, typename RangeNumberType>
692 ScalarFunctionFromFunctionObject<dim, RangeNumberType>::
693 ScalarFunctionFromFunctionObject(
694 const std::function<RangeNumberType(const Point<dim> &)> &fu)
695 : ScalarFunctionFromFunctionObject<dim, RangeNumberType>(
696 [fu](const double t, const Point<dim> &x) {
697 (void)t; // we got a function object that only takes 'x', so ignore 't'
698 return fu(x);
699 })
700 {}
701
702
703
704 template <int dim, typename RangeNumberType>
705 ScalarFunctionFromFunctionObject<dim, RangeNumberType>::
706 ScalarFunctionFromFunctionObject(
707 const std::function<RangeNumberType(const double, const Point<dim> &)>
708 &function_object)
709 : Function<dim, RangeNumberType>(1)
710 , function_object(function_object)
711 {}
712
713
714
715 template <int dim, typename RangeNumberType>
716 RangeNumberType
717 ScalarFunctionFromFunctionObject<dim, RangeNumberType>::value(
718 const Point<dim> &p,
719 const unsigned int component) const
720 {
721 (void)component;
722 Assert(component == 0,
723 ExcMessage("This object represents only scalar functions"));
724
725 return function_object(this->get_time(), p);
726 }
727
728
729
730 template <int dim, typename RangeNumberType>
731 VectorFunctionFromScalarFunctionObject<dim, RangeNumberType>::
732 VectorFunctionFromScalarFunctionObject(
733 const std::function<RangeNumberType(const Point<dim> &)> &function_object,
734 const unsigned int selected_component,
735 const unsigned int n_components)
736 : Function<dim, RangeNumberType>(n_components)
737 , function_object(function_object)
738 , selected_component(selected_component)
739 {
740 AssertIndexRange(selected_component, this->n_components);
741 }
742
743
744
745 template <int dim, typename RangeNumberType>
746 RangeNumberType
747 VectorFunctionFromScalarFunctionObject<dim, RangeNumberType>::value(
748 const Point<dim> &p,
749 const unsigned int component) const
750 {
751 AssertIndexRange(component, this->n_components);
752
753 if (component == selected_component)
754 return function_object(p);
755 else
756 return 0;
757 }
758
759
760
761 template <int dim, typename RangeNumberType>
762 void
763 VectorFunctionFromScalarFunctionObject<dim, RangeNumberType>::vector_value(
764 const Point<dim> &p,
765 Vector<RangeNumberType> &values) const
766 {
767 AssertDimension(values.size(), this->n_components);
768
769 // set everything to zero, and then the right component to its correct
770 // value
771 values = 0;
772 values(selected_component) = function_object(p);
773 }
774
775
776
777 /**
778 * The constructor for <tt>VectorFunctionFromTensorFunction</tt> which
779 * initiates the return vector to be size <tt>n_components</tt>.
780 */
781 template <int dim, typename RangeNumberType>
782 VectorFunctionFromTensorFunction<dim, RangeNumberType>::
783 VectorFunctionFromTensorFunction(
784 const TensorFunction<1, dim, RangeNumberType> &tensor_function,
785 const unsigned int selected_component,
786 const unsigned int n_components)
787 : Function<dim, RangeNumberType>(n_components)
788 , tensor_function(tensor_function)
789 , selected_component(selected_component)
790 {
791 // Verify that the Tensor<1,dim,RangeNumberType> will fit in the given length
792 // selected_components and not hang over the end of the vector.
793 AssertIndexRange(selected_component + dim - 1, this->n_components);
794 }
795
796
797
798 template <int dim, typename RangeNumberType>
799 inline RangeNumberType
800 VectorFunctionFromTensorFunction<dim, RangeNumberType>::value(
801 const Point<dim> &p,
802 const unsigned int component) const
803 {
804 AssertIndexRange(component, this->n_components);
805
806 // if the requested component is out of the range selected, then we can
807 // return early
808 if ((component < selected_component) ||
809 (component >= selected_component + dim))
810 return 0;
811
812 // otherwise retrieve the values from the <tt>tensor_function</tt> to be
813 // placed at the <tt>selected_component</tt> to
814 // <tt>selected_component + dim - 1</tt> elements of the <tt>Vector</tt>
815 // values and pick the correct one
816 const Tensor<1, dim, RangeNumberType> tensor_value = tensor_function.value(p);
817
818 return tensor_value[component - selected_component];
819 }
820
821
822 template <int dim, typename RangeNumberType>
823 inline void
824 VectorFunctionFromTensorFunction<dim, RangeNumberType>::vector_value(
825 const Point<dim> &p,
826 Vector<RangeNumberType> &values) const
827 {
828 Assert(values.size() == this->n_components,
829 ExcDimensionMismatch(values.size(), this->n_components));
830
831 // Retrieve the values from the <tt>tensor_function</tt> to be placed at
832 // the <tt>selected_component</tt> to
833 // <tt>selected_component + dim - 1</tt> elements of the <tt>Vector</tt>
834 // values.
835 const Tensor<1, dim, RangeNumberType> tensor_value = tensor_function.value(p);
836
837 // First we make all elements of values = 0
838 values = 0;
839
840 // Second we adjust the desired components to take on the values in
841 // <tt>tensor_value</tt>.
842 for (unsigned int i = 0; i < dim; ++i)
843 values(i + selected_component) = tensor_value[i];
844 }
845
846
847 /**
848 * Member function <tt>vector_value_list </tt> is the interface for giving a
849 * list of points (<code>vector<Point<dim> ></code>) of which to evaluate
850 * using the <tt>vector_value</tt> member function. Again, this function is
851 * written so as to not replicate the function definition but passes each
852 * point on to <tt>vector_value</tt> to be evaluated.
853 */
854 template <int dim, typename RangeNumberType>
855 void
856 VectorFunctionFromTensorFunction<dim, RangeNumberType>::vector_value_list(
857 const std::vector<Point<dim>> &points,
858 std::vector<Vector<RangeNumberType>> &value_list) const
859 {
860 Assert(value_list.size() == points.size(),
861 ExcDimensionMismatch(value_list.size(), points.size()));
862
863 const unsigned int n_points = points.size();
864
865 for (unsigned int p = 0; p < n_points; ++p)
866 VectorFunctionFromTensorFunction<dim, RangeNumberType>::vector_value(
867 points[p], value_list[p]);
868 }
869
870
871
872 template <int dim, typename RangeNumberType>
873 inline Tensor<1, dim, RangeNumberType>
874 VectorFunctionFromTensorFunction<dim, RangeNumberType>::gradient(
875 const Point<dim> &p,
876 const unsigned int component) const
877 {
878 AssertIndexRange(component, this->n_components);
879
880 // if the requested component is out of the range selected, then we can
881 // return early
882 if ((component < selected_component) ||
883 (component >= selected_component + dim))
884 return Tensor<1, dim>();
885
886 // // otherwise retrieve the values from the <tt>tensor_function</tt> to be
887 // // placed at the <tt>selected_component</tt> to
888 // // <tt>selected_component + dim - 1</tt> elements of the <tt>Vector</tt>
889 // // values and pick the correct one
890 const Tensor<2, dim, RangeNumberType> tensor_gradient =
891 tensor_function.gradient(p);
892
893 return tensor_gradient[component - selected_component];
894 }
895
896
897
898 template <int dim, typename RangeNumberType>
899 void
900 VectorFunctionFromTensorFunction<dim, RangeNumberType>::vector_gradient(
901 const Point<dim> &p,
902 std::vector<Tensor<1, dim, RangeNumberType>> &gradients) const
903 {
904 AssertDimension(gradients.size(), this->n_components);
905
906
907 for (unsigned int i = 0; i < this->n_components; ++i)
908 gradients[i] = gradient(p, i);
909 }
910
911
912
913 template <int dim, typename RangeNumberType>
914 void
915 VectorFunctionFromTensorFunction<dim, RangeNumberType>::gradient_list(
916 const std::vector<Point<dim>> &points,
917 std::vector<Tensor<1, dim, RangeNumberType>> &gradients,
918 const unsigned int component) const
919 {
920 Assert(gradients.size() == points.size(),
921 ExcDimensionMismatch(gradients.size(), points.size()));
922
923
924 for (unsigned int i = 0; i < points.size(); ++i)
925 gradients[i] = gradient(points[i], component);
926 }
927
928
929
930 template <int dim, typename RangeNumberType>
931 void
932 VectorFunctionFromTensorFunction<dim, RangeNumberType>::vector_gradient_list(
933 const std::vector<Point<dim>> &points,
934 std::vector<std::vector<Tensor<1, dim, RangeNumberType>>> &gradients) const
935 {
936 Assert(gradients.size() == points.size(),
937 ExcDimensionMismatch(gradients.size(), points.size()));
938
939
940 for (unsigned int i = 0; i < points.size(); ++i)
941 {
942 Assert(gradients[i].size() == this->n_components,
943 ExcDimensionMismatch(gradients[i].size(), this->n_components));
944 vector_gradient(points[i], gradients[i]);
945 }
946 }
947
948
949
950 template <int dim, typename RangeNumberType>
951 void
952 VectorFunctionFromTensorFunction<dim, RangeNumberType>::vector_gradients(
953 const std::vector<Point<dim>> &points,
954 std::vector<std::vector<Tensor<1, dim, RangeNumberType>>> &gradients) const
955 {
956 const unsigned int n = this->n_components;
957 AssertDimension(gradients.size(), n);
958 for (unsigned int i = 0; i < n; ++i)
959 gradient_list(points, gradients[i], i);
960 }
961
962
963
964 template <int dim, typename RangeNumberType>
965 FunctionFromFunctionObjects<dim, RangeNumberType>::FunctionFromFunctionObjects(
966 const unsigned int n_components,
967 const double initial_time)
968 : Function<dim, RangeNumberType>(n_components, initial_time)
969 {}
970
971
972
973 template <int dim, typename RangeNumberType>
974 FunctionFromFunctionObjects<dim, RangeNumberType>::FunctionFromFunctionObjects(
975 const std::vector<std::function<RangeNumberType(const Point<dim> &)>> &values,
976 const double initial_time)
977 : Function<dim, RangeNumberType>(values.size(), initial_time)
978 {
979 this->set_function_values(values);
980 }
981
982
983
984 template <int dim, typename RangeNumberType>
985 FunctionFromFunctionObjects<dim, RangeNumberType>::FunctionFromFunctionObjects(
986 const std::function<RangeNumberType(const Point<dim> &, const unsigned int)>
987 &values,
988 const unsigned int n_components,
989 const double initial_time)
990 : Function<dim, RangeNumberType>(n_components, initial_time)
991 , function_values(values)
992 {}
993
994
995
996 template <int dim, typename RangeNumberType>
997 FunctionFromFunctionObjects<dim, RangeNumberType>::FunctionFromFunctionObjects(
998 const std::vector<std::function<RangeNumberType(const Point<dim> &)>> &values,
999 const std::vector<
1000 std::function<Tensor<1, dim, RangeNumberType>(const Point<dim> &)>>
1001 &gradients,
1002 const double initial_time)
1003 : Function<dim, RangeNumberType>(values.size(), initial_time)
1004 {
1005 this->set_function_values(values);
1006 this->set_function_gradients(gradients);
1007 }
1008
1009
1010
1011 template <int dim, typename RangeNumberType>
1012 RangeNumberType
1013 FunctionFromFunctionObjects<dim, RangeNumberType>::value(
1014 const Point<dim> &p,
1015 const unsigned int component) const
1016 {
1017 AssertIndexRange(component, this->n_components);
1018 Assert(function_values,
1019 ExcMessage("Accessing value() in FunctionFromFunctionObjects requires "
1020 "setting the std::function objects for the value"));
1021 return function_values(p, component);
1022 }
1023
1024
1025
1026 template <int dim, typename RangeNumberType>
1027 Tensor<1, dim, RangeNumberType>
1028 FunctionFromFunctionObjects<dim, RangeNumberType>::gradient(
1029 const Point<dim> &p,
1030 const unsigned int component) const
1031 {
1032 AssertIndexRange(component, this->n_components);
1033 Assert(function_gradients,
1034 ExcMessage(
1035 "Accessing gradient() in FunctionFromFunctionObjects "
1036 "requires setting the std::function objects for the gradient"));
1037 return function_gradients(p, component);
1038 }
1039
1040
1041
1042 template <int dim, typename RangeNumberType>
1043 void
1044 FunctionFromFunctionObjects<dim, RangeNumberType>::set_function_values(
1045 const std::vector<std::function<RangeNumberType(const Point<dim> &)>> &values)
1046 {
1047 AssertDimension(this->n_components, values.size());
1048 function_values = [values](const auto &p, const auto c) {
1049 AssertIndexRange(c, values.size());
1050 return values[c](p);
1051 };
1052 }
1053
1054
1055
1056 template <int dim, typename RangeNumberType>
1057 void
1058 FunctionFromFunctionObjects<dim, RangeNumberType>::set_function_gradients(
1059 const std::vector<
1060 std::function<Tensor<1, dim, RangeNumberType>(const Point<dim> &)>>
1061 &gradients)
1062 {
1063 AssertDimension(this->n_components, gradients.size());
1064 function_gradients = [gradients](const auto &p, const auto c) {
1065 AssertIndexRange(c, gradients.size());
1066 return gradients[c](p);
1067 };
1068 }
1069
1070
1071
1072 template <int dim, typename RangeNumberType>
1073 VectorFunctionFromTensorFunctionObject<dim, RangeNumberType>::
1074 VectorFunctionFromTensorFunctionObject(
1075 const std::function<Tensor<1, dim, RangeNumberType>(const Point<dim> &)>
1076 &tensor_function_object,
1077 const unsigned int selected_component,
1078 const unsigned int n_components)
1079 : Function<dim, RangeNumberType>(n_components)
1080 , tensor_function_object(tensor_function_object)
1081 , selected_component(selected_component)
1082 {
1083 // Verify that the Tensor<1,dim,RangeNumberType> will fit in the given length
1084 // selected_components and not hang over the end of the vector.
1085 AssertIndexRange(selected_component + dim - 1, this->n_components);
1086 }
1087
1088
1089
1090 template <int dim, typename RangeNumberType>
1091 inline RangeNumberType
1092 VectorFunctionFromTensorFunctionObject<dim, RangeNumberType>::value(
1093 const Point<dim> &p,
1094 const unsigned int component) const
1095 {
1096 AssertIndexRange(component, this->n_components);
1097
1098 // if the requested component is out of the range selected, then we can
1099 // return early
1100 if ((component < selected_component) ||
1101 (component >= selected_component + dim))
1102 return 0;
1103
1104 // otherwise retrieve the values from the <tt>tensor_function</tt> to be
1105 // placed at the <tt>selected_component</tt> to
1106 // <tt>selected_component + dim - 1</tt> elements of the <tt>Vector</tt>
1107 // values and pick the correct one
1108 const Tensor<1, dim, RangeNumberType> tensor_value =
1109 tensor_function_object(p);
1110
1111 return tensor_value[component - selected_component];
1112 }
1113
1114
1115 template <int dim, typename RangeNumberType>
1116 inline void
1117 VectorFunctionFromTensorFunctionObject<dim, RangeNumberType>::vector_value(
1118 const Point<dim> &p,
1119 Vector<RangeNumberType> &values) const
1120 {
1121 Assert(values.size() == this->n_components,
1122 ExcDimensionMismatch(values.size(), this->n_components));
1123
1124 // Retrieve the values from the <tt>tensor_function</tt> to be placed at
1125 // the <tt>selected_component</tt> to
1126 // <tt>selected_component + dim - 1</tt> elements of the <tt>Vector</tt>
1127 // values.
1128 const Tensor<1, dim, RangeNumberType> tensor_value =
1129 tensor_function_object(p);
1130
1131 // First we make all elements of values = 0
1132 values = 0;
1133
1134 // Second we adjust the desired components to take on the values in
1135 // <tt>tensor_value</tt>.
1136 for (unsigned int i = 0; i < dim; ++i)
1137 values(i + selected_component) = tensor_value[i];
1138 }
1139
1140
1141 /**
1142 * Member function <tt>vector_value_list </tt> is the interface for giving a
1143 * list of points (<code>vector<Point<dim> ></code>) of which to evaluate
1144 * using the <tt>vector_value</tt> member function. Again, this function is
1145 * written so as to not replicate the function definition but passes each
1146 * point on to <tt>vector_value</tt> to be evaluated.
1147 */
1148 template <int dim, typename RangeNumberType>
1149 void
1150 VectorFunctionFromTensorFunctionObject<dim, RangeNumberType>::vector_value_list(
1151 const std::vector<Point<dim>> &points,
1152 std::vector<Vector<RangeNumberType>> &value_list) const
1153 {
1154 Assert(value_list.size() == points.size(),
1155 ExcDimensionMismatch(value_list.size(), points.size()));
1156
1157 const unsigned int n_points = points.size();
1158
1159 for (unsigned int p = 0; p < n_points; ++p)
1160 VectorFunctionFromTensorFunctionObject<dim, RangeNumberType>::vector_value(
1161 points[p], value_list[p]);
1162 }
1163
1164
1165 DEAL_II_NAMESPACE_CLOSE
1166
1167 #endif /* dealii_function_templates_h */