CoDiPack  3.0.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
expressionInterface.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2025 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <iostream>
38
39#include "../config.h"
40#include "../misc/macros.hpp"
41#include "../traits/expressionTraits.hpp"
42#include "../traits/realTraits.hpp"
43#include "expressionMemberOperations.hpp"
44#include "logic/nodeInterface.hpp"
45
47namespace codi {
48
59 template<typename T_Real, typename T_Impl>
60 struct ExpressionInterface : public NodeInterface<T_Impl>, public ExpressionMemberOperations<T_Real, T_Impl> {
61 public:
62
63 using Real = CODI_DD(T_Real, double);
64 using Impl = CODI_DD(T_Impl, ExpressionInterface);
65
68
71
74
76 CODI_INLINE Impl const& cast() const {
77 return static_cast<Impl const&>(*this);
78 }
79
80#if CODI_ImplicitConversion
82 CODI_INLINE operator const Real() const {
84
85 return cast().getValue();
86 }
87#endif
88
89 /*******************************************************************************/
92
94
96 CODI_INLINE Real const getValue() const;
97
102 * Does not need to be implemented for expressions with \c NodeInterface::LinkCount = 0 \c .
103 *
104 * @return The type of the result or a compatible vector type. E.g. Real or Direction<Real>.
105 *
106 * @tparam Tangent The type is the Real type of the selected argument or a compatible vector type. E.g. for
107 * \c Real f(complex<Real>, Real) \c the type with \c argNumber=0 \c is \c complex<Real> \c or
108 * \c Direction<complex<Real>> \c , with \c argNumber=1 \c it is \c Real \c or \c Direction<Real> \c .
109 */
110 template<size_t argNumber, typename Tangent>
111 CODI_INLINE auto applyTangent(Tangent const& tangent) const;
112
125 template<size_t argNumber, typename Adjoint>
126 CODI_INLINE auto applyAdjoint(Adjoint const& adjoint) const;
127
129
130 private:
131 ExpressionInterface& operator=(ExpressionInterface const&) = delete;
132 };
133
134#ifndef DOXYGEN_DISABLE
135 template<typename T_Type>
136 struct RealTraits::TraitsImplementation<T_Type, ExpressionTraits::EnableIfExpression<T_Type>> {
137 public:
138
140 using Real = typename Type::Real;
141
143
144 static int constexpr MaxDerivativeOrder = 1 + RealTraits::MaxDerivativeOrder<Real>();
145
147 return RealTraits::getPassiveValue(v.getValue());
148 }
149 };
150#endif
151
153 template<typename Expr>
155 out << v.getValue();
156
157 return out;
158 }
159}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:96
#define CODI_UNDEFINED
Used in interface declarations for types that have to be defined in the specializations.
Definition macros.hpp:119
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:116
Traits for everything that can be an expression e.g. codi::RealReverse, a + b, etc....
Definition expressionTraits.hpp:65
typename std::enable_if< IsExpression< Expr >::value, T >::type EnableIfExpression
Enable if wrapper for IsExpression.
Definition expressionTraits.hpp:160
inlinePassiveReal< Type > getPassiveValue(Type const &v)
Get the basic primal value of the type.
Definition realTraits.hpp:133
inlinesize_t constexpr MaxDerivativeOrder()
CoDiPack derivative order of the type.
Definition realTraits.hpp:127
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type that was used in the application.
Definition realTraits.hpp:123
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
ExpressionTraits::EnableIfExpression< Expr, std::ostream > & operator<<(std::ostream &out, Expr const &v)
Write the primal value to the stream.
Definition expressionInterface.hpp:154
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:60
inlineReal const getValue() const
Compute the primal value that is usually evaluated by the statement/expression.
T_Real Real
See ExpressionInterface.
Definition expressionInterface.hpp:63
ExpressionInterface StoreAs
Defines how this expression is stored in an expression tree.
Definition expressionInterface.hpp:93
ExpressionInterface(ExpressionInterface const &)=default
Constructor.
T_Impl Impl
See ExpressionInterface.
Definition expressionInterface.hpp:64
ExpressionInterface()=default
Constructor.
inlineauto applyAdjoint(Adjoint const &adjoint) const
inlineauto applyTangent(Tangent const &tangent) const
char ADLogic
AD logic that governs the expression. Needs to be the same for all inputs of the expression.
Definition expressionInterface.hpp:67
inlineImpl const & cast() const
Cast to the implementation.
Definition expressionInterface.hpp:76
Definition expressionMemberOperations.hpp:65
Node side interface for the traversal of expressions.
Definition nodeInterface.hpp:56
Common traits for all types used as real values.
Definition realTraits.hpp:66
static int constexpr MaxDerivativeOrder
CoDiPack derivative order of the type.
Definition realTraits.hpp:74
T_Type Type
See TraitsImplementation.
Definition realTraits.hpp:69
static inlinePassiveReal getPassiveValue(Type const &v)
Get the basic primal value of the type.
Definition realTraits.hpp:77
Type PassiveReal
The original computation type that was used in the application.
Definition realTraits.hpp:72
static void implicitCast()
Show a warning about an implicit cast of an active real type.
Definition exceptions.hpp:129