24#ifndef DUAL_FUNCTORS_HH
25#define DUAL_FUNCTORS_HH
39template <
typename T,
typename DESCRIPTOR>
48 this->
getName() =
"dPhysDissipationDf";
51template <
typename T,
typename DESCRIPTOR>
54 const T omega = _converter.getLatticeRelaxationFrequency();
55 const T dt = _converter.getConversionFactorTime();
58 auto cell = this->_blockLattice.get(latticeR);
59 cell.computeAllMomenta(rho, u, pi);
61 for (
int jPop=0; jPop < DESCRIPTOR::q; ++jPop) {
65 for (
int iAlpha=0; iAlpha < DESCRIPTOR::d; ++iAlpha) {
66 for (
int iBeta=0; iBeta < DESCRIPTOR::d; ++iBeta) {
68 dDdf[jPop] += pi[iPi] * dpidf[iPi] - pi[iPi] * pi[iPi] / rho;
77template <
typename T,
typename DESCRIPTOR>
82 this->
getName() =
"dPhysDissipationDf";
84 this->_blockF.emplace_back(
94template <
typename T,
typename DESCRIPTOR>
99 int nDim,
int extractDim)
100 :
BlockLatticeF3D<T,DESCRIPTOR>(blockLattice,DESCRIPTOR::q*DESCRIPTOR::d),
102 _converter(converter),
103 _nDim(nDim), _extractDim(extractDim)
105 this->
getName() =
"dPhysVelocityDf";
108template <
typename T,
typename DESCRIPTOR>
112 this->_blockLattice.get(latticeR).computeRhoU(rho, u);
114 for (
int jPop=0; jPop < DESCRIPTOR::q; ++jPop) {
115 for (
int iDim=0; iDim < DESCRIPTOR::d; ++iDim) {
116 if (iDim != _extractDim && _extractDim != -1) {
117 dVelocityDf[jPop*DESCRIPTOR::d + iDim] = T(0);
120 dVelocityDf[jPop*DESCRIPTOR::d + iDim] = (
descriptors::c<DESCRIPTOR>(jPop,iDim)-u[iDim])/rho * this->_converter.getConversionFactorVelocity();
127template <
typename T,
typename DESCRIPTOR>
132 this->
getName() =
"dPhysVelocityDf";
144template <
typename T,
typename DESCRIPTOR>
151 this->
getName() =
"dPhysVelocityDf";
164template <
typename T,
typename DESCRIPTOR>
172 using namespace functor_dsl;
174 auto wantedLatticeF = restrictF(wantedF.toShared(), sLattice);
175 auto differenceF =
norm<2>(f.toShared() - wantedLatticeF, indicatorF.toShared());
177 return util::pow(differenceF, 2) / T(2.0);
180 this->
getName() =
"differenceObjective";
183template<
typename T,
typename DESCRIPTOR>
189 f->getSuperLattice(),
190 std::static_pointer_cast<
SuperF3D<T,T>>(f.toShared()),
191 std::forward<decltype(wantedF)>(wantedF),
192 std::forward<decltype(indicatorF)>(indicatorF))
195template<
typename T,
typename DESCRIPTOR>
204 std::forward<decltype(f)>(f),
205 std::forward<decltype(wantedF)>(wantedF),
206 geometry.getMaterialIndicator(material))
210template <
typename T,
typename DESCRIPTOR>
222 _indicatorF(indicatorF),
225 this->
getName() =
"BlockDdifferenceObjectiveDf";
228template <
typename T,
typename DESCRIPTOR>
231 for (
int i=0; i < DESCRIPTOR::q; i++) {
235 if (_indicatorF(latticeR)) {
236 const int nDim = _f.getTargetDim();
238 std::vector<T> dJdD(nDim,T());
244 _wantedF(wantedF, latticeR);
246 T dFdF[nDim*DESCRIPTOR::q];
247 _dFdF(dFdF,latticeR);
249 for (
int iDim=0; iDim<nDim; iDim++) {
250 dJdD[iDim] = (f[iDim] - wantedF[iDim]) * _weight;
252 for (
int jPop=0; jPop < DESCRIPTOR::q; ++jPop) {
253 dJdF[jPop] += dJdD[iDim] * dFdF[jPop*nDim + iDim];
261template <
typename T,
typename DESCRIPTOR>
267 :
SuperLatticeF3D<T,DESCRIPTOR>(f->getSuperLattice(), f->getTargetDim()),
269 _dFdF(std::move(dFdF)),
270 _wantedF(std::forward<decltype(wantedF)>(wantedF), f->getSuperLattice()),
271 _indicatorF(std::move(indicatorF))
273 this->
getName() =
"dDifferenceObjectiveDf";
275 const T weight =
util::pow(_f->getSuperStructure().getCuboidDecomposition().getDeltaR(), 3);
277 for (
int iC = 0; iC < this->
getSuperLattice().getLoadBalancer().size(); ++iC) {
282 _dFdF->getBlockF(iC),
283 _wantedF.getBlockF(iC),
284 _indicatorF->getBlockIndicatorF(iC),
290template <
typename T,
typename DESCRIPTOR>
293 for (
int i=0; i < DESCRIPTOR::q; ++i) {
297 auto& load = this->getSuperLattice().getLoadBalancer();
299 if (load.isLocal(latticeR[0])) {
300 return this->getBlockF(load.loc(latticeR[0]))(dJdF, &latticeR[1]);
308template<
typename T,
typename DESCRIPTOR>
316 using namespace functor_dsl;
318 auto wantedLatticeF = restrictF(wantedF.toShared(), sLattice);
319 auto relativeDifferenceF =
norm<2>(f.toShared() - wantedLatticeF, indicatorF.toShared()) /
320 norm<2>(wantedLatticeF, indicatorF.toShared());
322 return pow(relativeDifferenceF, 2) / T(2.0);
325 this->
getName() =
"relativeDifferenceObjective";
328template<
typename T,
typename DESCRIPTOR>
334 f->getSuperLattice(),
335 std::static_pointer_cast<
SuperF3D<T,T>>(f.toShared()),
336 std::forward<decltype(wantedF)>(wantedF),
337 std::forward<decltype(indicatorF)>(indicatorF))
340template<
typename T,
typename DESCRIPTOR>
346 std::vector<int> materials)
349 std::forward<decltype(f)>(f),
350 std::forward<decltype(wantedF)>(wantedF),
351 geometry.getMaterialIndicator(materials))
354template<
typename T,
typename DESCRIPTOR>
363 std::forward<decltype(f)>(f),
364 std::forward<decltype(wantedF)>(wantedF),
365 geometry.getMaterialIndicator(material))
368template<
typename T,
typename DESCRIPTOR>
376 using namespace functor_dsl;
379 auto relativeDifferenceF =
norm<2>(f.toShared() - wantedF.toShared(), indicatorF.toShared()) /
380 norm<2>(wantedF.toShared(), indicatorF.toShared());
382 return pow(relativeDifferenceF, 2) / T(2.0);
385 this->
getName() =
"relativeDifferenceObjective";
389template <
typename T,
typename DESCRIPTOR>
398 BlockF3D<T>(blockStructure, DESCRIPTOR::q),
399 _f(f), _dFdF(dFdF), _wantedF(wantedF), _indicatorF(indicatorF),
400 _globalValue(globalValue), _weight(weight)
402 this->
getName() =
"BlockDrelativeDifferenceObjectiveDf";
405template <
typename T,
typename DESCRIPTOR>
408 for (
int jPop=0; jPop < DESCRIPTOR::q; ++jPop) {
412 if (_indicatorF(latticeR)) {
413 const int nDim = DESCRIPTOR::d;
421 _wantedF(wantedF, latticeR);
423 T dFdF[nDim*DESCRIPTOR::q] {};
424 _dFdF(dFdF, latticeR);
426 for (
int iDim=0; iDim < nDim; ++iDim) {
427 dJdD[iDim] = (f[iDim] - wantedF[iDim]) / _globalValue * _weight;
429 for (
int jPop=0; jPop < DESCRIPTOR::q; ++jPop) {
430 dJdF[jPop] += dJdD[iDim] * dFdF[jPop*nDim + iDim];
438template <
typename T,
typename DESCRIPTOR>
445 :
SuperF3D<T,T>(sLattice, DESCRIPTOR::q),
447 _dFdF(std::move(dFdF)),
448 _wantedF(std::move(wantedF)),
449 _indicatorF(std::move(indicatorF))
451 this->
getName() =
"dRelativeDifferenceObjectiveDf_Lattice";
455 wantedFlp(output,
nullptr);
457 const T globalValue =
util::pow(output[0], 2);
466 _dFdF->getBlockF(iC),
467 _wantedF->getBlockF(iC),
468 _indicatorF->getBlockIndicatorF(iC),
475template <
typename T,
typename DESCRIPTOR>
483 std::forward<decltype(f)>(f),
484 std::forward<decltype(dFdF)>(dFdF),
487 std::forward<decltype(wantedF)>(wantedF), sLattice)),
488 std::forward<decltype(indicatorF)>(indicatorF))
491template <
typename T,
typename DESCRIPTOR>
498 std::vector<int> materials)
500 std::forward<decltype(f)>(f),
501 std::forward<decltype(dFdF)>(dFdF),
502 std::forward<decltype(wantedF)>(wantedF),
503 geometry.getMaterialIndicator(materials))
506template <
typename T,
typename DESCRIPTOR>
515 std::forward<decltype(f)>(f),
516 std::forward<decltype(dFdF)>(dFdF),
517 std::forward<decltype(wantedF)>(wantedF),
518 geometry.getMaterialIndicator(material))
521template <
typename T,
typename DESCRIPTOR>
524 for (
int i=0; i < DESCRIPTOR::q; ++i) {
530 if (load.isLocal(latticeR[0])) {
531 return this->getBlockF(load.loc(latticeR[0]))(dJdF, &latticeR[1]);
539template <
typename T,
typename DESCRIPTOR>
548 BlockF3D<T>(blockStructure, DESCRIPTOR::q),
549 _f(f), _dFdF(dFdF), _wantedF(wantedF), _indicatorF(indicatorF),
550 _extractDim(f.getExtractDim()), _globalValue(globalValue), _weight(weight)
552 this->
getName() =
"BlockDrelativeDifferenceObjectiveComponentDf";
555template <
typename T,
typename DESCRIPTOR>
558 for (
int jPop=0; jPop < DESCRIPTOR::q; ++jPop) {
562 if (_indicatorF(latticeR)) {
563 const int nDim = DESCRIPTOR::d;
571 _wantedF(wantedF, latticeR);
573 T dFdF[nDim*DESCRIPTOR::q] {};
574 _dFdF(dFdF, latticeR);
576 for (
int iDim=0; iDim < nDim; ++iDim) {
577 if (iDim == _extractDim) {
580 dJdD[iDim] = (f[0] - wantedF[0]) / _globalValue * _weight;
583 for (
int jPop=0; jPop < DESCRIPTOR::q; ++jPop) {
584 dJdF[jPop] += dJdD[iDim] * dFdF[jPop*nDim + iDim];
592template <
typename T,
typename DESCRIPTOR>
599 :
SuperF3D<T,T>(sLattice, DESCRIPTOR::q),
600 _f(std::forward<decltype(f)>(f), extractDim),
601 _dFdF(std::move(dFdF)),
602 _wantedF(std::forward<decltype(wantedF)>(wantedF), sLattice),
603 _indicatorF(std::move(indicatorF))
605 this->
getName() =
"dRelativeDifferenceObjectiveDf";
609 wantedFlp(output,
nullptr);
611 const T globalValue =
util::pow(output[0], 2);
619 _dFdF->getBlockF(iC),
620 _wantedF.getBlockF(iC),
621 _indicatorF->getBlockIndicatorF(iC),
628template <
typename T,
typename DESCRIPTOR>
631 for (
int i=0; i < DESCRIPTOR::q; ++i) {
635 auto& load = _f.getSuperStructure().getLoadBalancer();
637 if (load.isLocal(latticeR[0])) {
638 return this->getBlockF(load.loc(latticeR[0]))(dJdF, &latticeR[1]);
AnalyticalF are applications from DD to XD, where X is set by the constructor.
represents all functors that operate on a cuboid in general, mother class of BlockLatticeF,...
Base block indicator functor.
represents all functors that operate on a DESCRIPTOR in general, e.g. getVelocity(),...
Smart pointer for managing the various ways of passing functors around.
represents all functors that operate on a SuperStructure<T,3> in general
SuperStructure< T, 3 > & getSuperStructure()
BlockF3D< W > & getBlockF(int iCloc)
std::vector< std::unique_ptr< BlockF3D< W > > > _blockF
Representation of a statistic for a parallel 2D geometry.
identity functor for memory management
Base indicator functor (discrete)
represents all functors that operate on a SuperLattice in general, e.g. getVelocity(),...
SuperLattice< T, DESCRIPTOR > & getSuperLattice()
Functor used to convert analytical functions to lattice functions.
represents all functors that operate on a DESCRIPTOR with output in Phys, e.g. physVelocity(),...
Super class maintaining block lattices for a cuboid decomposition.
BlockLattice< T, DESCRIPTOR > & getBlock(int locC)
Return BlockLattice with local index locC.
Functor that returns the Lp norm over omega of the the euklid norm of the input functor.
int getOverlap()
Read and write access to the overlap.
LoadBalancer< T > & getLoadBalancer()
Read and write access to the load balancer.
CuboidDecomposition< T, D > & getCuboidDecomposition()
Read and write access to cuboid geometry.
Conversion between physical and lattice units, as well as discretization.
functor to compute 0.5*(f-f_wanted)^2 on a lattice
BlockDdifferenceObjectiveDf3D(BlockLattice< T, DESCRIPTOR > &blockLattice, BlockF3D< T > &f, BlockF3D< T > &dFdF, BlockF3D< T > &wantedF, BlockIndicatorF3D< T > &indicatorF, T weight)
bool operator()(T output[], const int input[])
has to be implemented for 'every' derived class
functor to compute 0.5*(f[extractDim]-f_wanted[0])^2/f_wanted^2 on a lattice
bool operator()(T output[], const int input[])
has to be implemented for 'every' derived class
BlockDrelativeDifferenceObjectiveComponentDf3D(BlockStructureD< 3 > &blockStructure, BlockExtractComponentF3D< T > &f, BlockF3D< T > &dFdF, BlockF3D< T > &wantedF, BlockIndicatorF3D< T > &indicatorF, T globalValue, T weight)
functor to compute 0.5(f-f_wanted)^2/f_wanted^2 on a lattice
bool operator()(T output[], const int input[])
has to be implemented for 'every' derived class
BlockDrelativeDifferenceObjectiveDf3D(BlockStructureD< 3 > &blockStructure, BlockF3D< T > &f, BlockF3D< T > &dFdF, BlockF3D< T > &wantedF, BlockIndicatorF3D< T > &indicatorF, T globalValue, T weight)
functor to get the pointwise dual dissipation density on local lattices, if globIC is not on the loca...
bool operator()(T output[], const int input[])
BlockLatticeDphysDissipationDf(BlockLattice< T, DESCRIPTOR > &blockLattice, int overlap, const UnitConverter< T, DESCRIPTOR > &converter)
functor to get pointwise dual velocity density on local lattices, if globIC is not on the local proce...
BlockLatticeDphysVelocityDf3D(BlockLattice< T, DESCRIPTOR > &blockLattice, int overlap, const UnitConverter< T, DESCRIPTOR > &converter, int nDim, int extractDim)
bool operator()(T output[], const int input[])
has to be implemented for 'every' derived class
bool operator()(T output[], const int input[])
DdifferenceObjectiveDf3D(FunctorPtr< SuperLatticePhysF3D< T, DESCRIPTOR > > &&f, FunctorPtr< SuperLatticePhysF3D< T, DESCRIPTOR > > &&dFdF, FunctorPtr< AnalyticalF3D< T, T > > &&wantedF, FunctorPtr< SuperIndicatorF3D< T > > &&indicatorF)
functor to compute 0.5*L2Norm(f-f_wanted)^2 on a lattice
DifferenceObjective3D(SuperLattice< T, DESCRIPTOR > &sLattice, FunctorPtr< SuperF3D< T, T > > &&f, FunctorPtr< AnalyticalF3D< T, T > > &&wantedF, FunctorPtr< SuperIndicatorF3D< T > > &&indicatorF)
DrelativeDifferenceObjectiveComponentDf3D(SuperLattice< T, DESCRIPTOR > &sLattice, FunctorPtr< SuperF3D< T, T > > &&f, int extractDim, FunctorPtr< SuperF3D< T, T > > &&dFdF, FunctorPtr< AnalyticalF3D< T, T > > &&wantedF, FunctorPtr< SuperIndicatorF3D< T > > &&indicatorF)
bool operator()(T output[], const int input[])
functor to compute 0.5(f-f_wanted)^2/f_wanted^2 on a lattice
DrelativeDifferenceObjectiveDf3D(SuperLattice< T, DESCRIPTOR > &sLattice, FunctorPtr< SuperF3D< T, T > > &&f, FunctorPtr< SuperF3D< T, T > > &&dFdF, FunctorPtr< SuperF3D< T, T > > &&wantedF, FunctorPtr< SuperIndicatorF3D< T > > &&indicatorF)
bool operator()(T output[], const int input[])
functor to compute 0.5*L2Norm(f-f_wanted)^2/L2Norm(f_wanted)^2 on a lattice
RelativeDifferenceObjective3D(SuperLattice< T, DESCRIPTOR > &sLattice, FunctorPtr< SuperF3D< T, T > > &&f, FunctorPtr< AnalyticalF3D< T, T > > &&wantedF, FunctorPtr< SuperIndicatorF3D< T > > &&indicatorF)
SuperLatticeDphysDissipationDf(SuperLattice< T, DESCRIPTOR > &sLattice, const UnitConverter< T, DESCRIPTOR > &converter)
SuperLatticeDphysVelocityDf3D(SuperLattice< T, DESCRIPTOR > &sLattice, const UnitConverter< T, DESCRIPTOR > &converter)
Helper functions for the implementation of LB dynamics.
constexpr T invCs2() any_platform
constexpr int c(unsigned iPop, unsigned iDim) any_platform
Expr pow(Expr base, Expr exp)
unsigned serialSymmetricTensorIndex(unsigned i, unsigned j) any_platform
Compute serial index of symmetric tensor.
Top level namespace for all of OpenLB.
std::conditional_t< DESCRIPTOR::d==2, BlockLatticeF2D< T, DESCRIPTOR >, BlockLatticeF3D< T, DESCRIPTOR > > BlockLatticeF
std::conditional_t< DESCRIPTOR::d==2, SuperLatticePhysF2D< T, DESCRIPTOR >, SuperLatticePhysF3D< T, DESCRIPTOR > > SuperLatticePhysF
std::string getName(OperatorScope scope)
Returns human-readable name of scope.
constexpr T norm(const ScalarVector< T, D, IMPL > &a) any_platform
Euclidean vector norm.
static void dPiDf(CELL &cell, DPIDF &dpidf, int jPop) any_platform
derive stress pi by population component
Compute number of elements of a symmetric d-dimensional tensor.