OpenLB 1.7
Loading...
Searching...
No Matches
optiCaseAD.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2*
3* Copyright (C) 2012, 2021 Mathias J. Krause, Julius Jeßberger
4* E-mail contact: info@openlb.net
5* The most recent release of OpenLB can be downloaded at
6* <http://www.openlb.net/>
7*
8* This program is free software; you can redistribute it and/or
9* modify it under the terms of the GNU General Public License
10* as published by the Free Software Foundation; either version 2
11* of the License, or (at your option) any later version.
12*
13* This program is distributed in the hope that it will be useful,
14* but WITHOUT ANY WARRANTY; without even the implied warranty of
15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16* GNU General Public License for more details.
17*
18* You should have received a copy of the GNU General Public
19* License along with this program; if not, write to the Free
20* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21* Boston, MA 02110-1301, USA.
22*/
23
29#ifndef OPTI_CASE_AD_H
30#define OPTI_CASE_AD_H
31
32
33#include "utilities/aDiff.h"
34#include "optiCase.h"
36
37
38namespace olb {
39
40namespace opti {
41
49template<typename S, std::size_t n,
50 template<typename> typename C = util::StdVector>
51class OptiCaseAD : public OptiCase<S,C<S>> {
52
53protected:
55 std::function<S (const C<S>&)> _functionHelp { [](const C<S>&){ return S{}; } };
56 std::function<T (const C<T>&)> _adFunctionHelp { [](const C<T>&){ return T{}; } };
57 std::function<S (const C<S>&, unsigned)> _function;
58 std::function<T (const C<T>&, unsigned)> _adFunction;
59
60public:
61 explicit OptiCaseAD() = default;
62
64 std::function<S (const C<S>&, unsigned)> function,
65 std::function<T (const C<T>&, unsigned)> adFunction,
66 std::function<void (void)> postEvaluation = [](){})
67 : OptiCase<S,C<S>>(postEvaluation), _function(function), _adFunction(adFunction)
68 { }
69
70 // If function with (only) one argument is passed, use plain wrappers
72 std::function<S (const C<S>&)> function,
73 std::function<T (const C<T>&)> adFunction,
74 std::function<void (void)> postEvaluation = [](){})
75 : OptiCase<S,C<S>>(postEvaluation),
76 _functionHelp(function), _adFunctionHelp(adFunction), // store original functions for memory reasons
77 _function ([&](const C<S>& arg, unsigned){ return _functionHelp(arg); }),
78 _adFunction ([&](const C<T>& arg, unsigned){ return _adFunctionHelp(arg); })
79 { }
80
82 const C<S>& control, unsigned optiStep=0) override
83 {
84 return _function(control, optiStep);
85 }
86
87 void computeDerivatives(const C<S>& control,
88 C<S>& derivatives, unsigned optiStep=0) override
89 {
90 assert((control.size() == derivatives.size()));
91
92 auto adControl = util::iniAD<n,S,C>(control);
93
94 const T adResult = _adFunction(adControl, optiStep);
95
96 for(std::size_t it = 0; it < control.size(); ++it){
97 derivatives[it] = adResult.d(it);
98 }
99 }
100};
101
102
106template<typename S, std::size_t n, template<typename> typename SOLVER,
107 template<typename> typename C = util::StdVector>
108class OptiCaseAdForSolver : public OptiCaseAD<S,n,C> {
109
110public:
111 using typename OptiCaseAD<S,n,C>::T;
112 std::shared_ptr<SOLVER<S>> _solver;
113 std::shared_ptr<SOLVER<T>> _adSolver;
114
115 OptiCaseAdForSolver(std::shared_ptr<SOLVER<S>> solver,
116 std::shared_ptr<SOLVER<T>> adSolver)
117 : OptiCaseAD<S,n,C>(getCallable<S>(solver), getCallable<T>(adSolver),
118 std::bind(&SOLVER<S>::postProcess, solver)),
119 _solver(solver), _adSolver(adSolver)
120 { }
121
124 createLbSolver<SOLVER<S>> (xml),
125 createLbSolver<SOLVER<T>> (xml))
126 { }
127};
128
129template<typename S, typename T, template<typename> typename SOLVER>
130OptiCaseAdForSolver(std::shared_ptr<SOLVER<S>>,std::shared_ptr<SOLVER<T>>)
132
133}
134
135}
136
137#endif
The description of a algoritmic differentiation data type using the forward method – header file.
Derivatives are computed with automatic differentiation.
Definition optiCaseAD.h:51
std::function< S(const C< S > &, unsigned) _function)
Definition optiCaseAD.h:57
std::function< T(const C< T > &) _adFunctionHelp)
Definition optiCaseAD.h:56
S evaluateObjective(const C< S > &control, unsigned optiStep=0) override
Definition optiCaseAD.h:81
std::function< S(const C< S > &) _functionHelp)
Definition optiCaseAD.h:55
std::function< T(const C< T > &, unsigned) _adFunction)
Definition optiCaseAD.h:58
void computeDerivatives(const C< S > &control, C< S > &derivatives, unsigned optiStep=0) override
Definition optiCaseAD.h:87
OptiCaseAD(std::function< S(const C< S > &, unsigned)> function, std::function< T(const C< T > &, unsigned)> adFunction, std::function< void(void)> postEvaluation=[](){})
Definition optiCaseAD.h:63
OptiCaseAD(std::function< S(const C< S > &)> function, std::function< T(const C< T > &)> adFunction, std::function< void(void)> postEvaluation=[](){})
Definition optiCaseAD.h:71
util::ADf< S, n > T
Definition optiCaseAD.h:54
Interface for OptiCaseAD that performs Lattice-Boltzmann-Solver construction itself (from xml file)
Definition optiCaseAD.h:108
OptiCaseAdForSolver(XMLreader const &xml)
Definition optiCaseAD.h:122
OptiCaseAdForSolver(std::shared_ptr< SOLVER< S > > solver, std::shared_ptr< SOLVER< T > > adSolver)
Definition optiCaseAD.h:115
std::shared_ptr< SOLVER< T > > _adSolver
Definition optiCaseAD.h:113
std::shared_ptr< SOLVER< S > > _solver
Definition optiCaseAD.h:112
Abstract base class for optimization tasks.
Definition optiCase.h:40
void postEvaluation()
Definition optiCase.h:56
Definition of a description of a algoritmic differentiation data type using the forward method.
Definition aDiff.h:64
constexpr T & d(unsigned i)
Definition aDiff.h:252
OptiCaseAdForSolver(std::shared_ptr< SOLVER< S > >, std::shared_ptr< SOLVER< T > >) -> OptiCaseAdForSolver< S, T::dim, SOLVER, util::StdVector >
std::vector< S, std::allocator< S > > StdVector
Top level namespace for all of OpenLB.
std::shared_ptr< SOLVER > createLbSolver(XMLreader const &xml)
Definition lbSolver.h:338
std::function< T(const std::vector< T > &, unsigned) getCallable)(std::shared_ptr< SOLVER > solver)
Returns a function that encapsulates the solving process.
Definition lbSolver.h:317
Optimization Code.