version 0.4.4
differentiablefunctionfactory.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2025 The Ikarus Developers ikarus@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
9#pragma once
10
11#include <utility>
12
13#include <dune/functions/common/differentiablefunctionfromcallables.hh>
14
19
20namespace Ikarus {
21
23{
24 template <typename Assembler, typename... Affordances>
25 static auto op(Assembler&& as, AffordanceCollection<Affordances...> affordances,
26 DBCOption dbcOption = DBCOption::Full) {
27 auto asPtr = toSharedPointer(std::forward<Assembler>(as));
28
29 using FERequirement = typename traits::remove_pointer_t<std::remove_cvref_t<Assembler>>::FERequirement;
30
31 [[maybe_unused]] auto KFunction = [dbcOption, assembler = asPtr, affordances](const FERequirement& p) -> auto& {
32 return assembler->matrix(p, affordances.matrixAffordance(), dbcOption);
33 };
34
35 [[maybe_unused]] auto residualFunction = [dbcOption, assembler = asPtr,
36 affordances](const FERequirement& p) -> auto& {
37 return assembler->vector(p, affordances.vectorAffordance(), dbcOption);
38 };
39
40 auto reqArg = FERequirement(); // This is only created to help makeDifferentiableFunction deduce the argument type
41 // of the functions. therefore, no valid values needed
42
43 if constexpr (affordances.hasScalarAffordance) {
44 [[maybe_unused]] auto energyFunction = [assembler = asPtr, affordances](const FERequirement& p) -> auto& {
45 return assembler->scalar(p, affordances.scalarAffordance());
46 };
47
48 return makeDifferentiableFunction(functions(energyFunction, residualFunction, KFunction), reqArg);
49 } else
50 return makeDifferentiableFunction(functions(residualFunction, KFunction), reqArg);
51 }
52
53 template <typename Assembler>
54 static auto op(Assembler&& as, DBCOption dbcOption) {
55 auto asPtr = toSharedPointer(std::forward<Assembler>(as));
56 if (as->boundToAffordanceCollection())
57 return op(asPtr, as->affordanceCollection(), dbcOption);
58
59 DUNE_THROW(Dune::InvalidStateException,
60 "Assembler has to be bound to an affordance collection before you can call "
61 "this method");
62 }
63
64 template <typename Assembler>
65 static auto op(Assembler&& as) {
66 auto asPtr = toSharedPointer(std::forward<Assembler>(as));
67
68 if (asPtr->boundToAffordanceCollection() and asPtr->boundToDBCOption())
69 return op(asPtr, asPtr->affordanceCollection(), asPtr->dBCOption());
70 DUNE_THROW(Dune::InvalidStateException,
71 "Assembler has to be bound to an affordance collection and to an "
72 "DBCOption before you can call "
73 "this method");
74 }
75
76private:
77 // Convert to shared pointer if needed
78 template <typename Assembler>
79 static decltype(auto) toSharedPointer(Assembler&& as) {
80 if constexpr (std::is_pointer_v<std::remove_cvref_t<Assembler>> ||
82 return std::forward<Assembler>(as);
83 } else {
84 return std::make_shared<std::remove_cvref_t<Assembler>>(std::forward<Assembler>(as));
85 }
86 }
87};
88
89} // namespace Ikarus
Contains derivative traits for common vbalue and derivative relations used for makeDifferentiableFunc...
Provides a DifferentiableFunction class for handling differentiable Functions.
Definition of the LinearElastic class for finite element mechanics computations.
Definition: assemblermanipulatorbuildingblocks.hh:22
auto makeDifferentiableFunction(const Impl::Functions< F... > &derivativesFunctions, const Arg &parameter)
Factory method for DifferentiableFunction It is a function taking several callables and the argument ...
Definition: differentiablefunction.hh:99
DBCOption
Definition: dirichletbcenforcement.hh:8
auto functions(Args &&... args)
Creates a Functions object.
Definition: differentiablefunction.hh:44
typename remove_pointer< T >::type remove_pointer_t
Definition: traits.hh:176
Struct representing a collection of affordances.
Definition: ferequirements.hh:107
auto vectorAffordance() const
Definition: ferequirements.hh:159
static constexpr bool hasScalarAffordance
Definition: ferequirements.hh:114
auto scalarAffordance() const
Definition: ferequirements.hh:154
auto matrixAffordance() const
Definition: ferequirements.hh:164
Definition: differentiablefunctionfactory.hh:23
static auto op(Assembler &&as)
Definition: differentiablefunctionfactory.hh:65
static auto op(Assembler &&as, AffordanceCollection< Affordances... > affordances, DBCOption dbcOption=DBCOption::Full)
Definition: differentiablefunctionfactory.hh:25
static auto op(Assembler &&as, DBCOption dbcOption)
Definition: differentiablefunctionfactory.hh:54
Type trait to check if a type is a isSharedPtr.
Definition: traits.hh:136