Commit 150e7484 authored by oliver.sander_at_tu-dresden.de's avatar oliver.sander_at_tu-dresden.de
Browse files

Move new SumFunctional class to file sumfunctional.hh

And get rid of the file newsumfunctional.hh, which was never intended to
be a permanent solution.  With this patch, the file sumfunctional.hh
contains both the new and the old implementation, and you can switch
between the two with USE_OLD_TNNMG.
parent 251a68ba
......@@ -6,7 +6,6 @@ install(FILES
boxconstrainedquadraticfunctional.hh
convexfunctional.hh
cubeindicatorfunctional.hh
newsumfunctional.hh
nonsmoothconvexfunctional.hh
normfunctional.hh
quadraticfunctional.hh
......
// -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set ts=8 sw=2 et sts=2:
#ifndef DUNE_TNNMG_FUNCTIONALS_NEWSUMFUNCTIONAL_HH
#define DUNE_TNNMG_FUNCTIONALS_NEWSUMFUNCTIONAL_HH
#include <dune/common/hybridutilities.hh>
#include <dune/common/std/apply.hh>
namespace Dune {
namespace TNNMG {
template<class... F>
class ShiftedSumFunctional;
/** \brief Sum of several functionals
*
* \tparam F List of functionals
*/
template<class... F>
class SumFunctional
{
using FunctionTuple = std::tuple<F...>;
using F0 = std::tuple_element_t<0,FunctionTuple>;
public:
using Functions = FunctionTuple;
using Vector = typename F0::Vector;
using Range = typename F0::Range;
SumFunctional(const F&... f) :
functions_(f...)
{}
Range operator()(const Vector& v) const
{
Range y{0};
Dune::Hybrid::forEach(functions_, [&](auto&& f) {
y += f(v);
});
return y;
}
const FunctionTuple& functions() const
{
return functions_;
}
friend ShiftedSumFunctional<F...>
shift(const SumFunctional& f, const Vector& origin)
{
return Dune::Std::apply([&](const auto&... args) {
return ShiftedSumFunctional<F...>(args..., origin);
}, f.functions());
}
private:
FunctionTuple functions_;
};
template<class... F, class Vector>
auto directionalRestriction(const SumFunctional<F...>& f, const Vector& origin, const Vector& direction)
{
return Dune::Std::apply([&](const auto&... args) {
return SumFunctional<decltype(directionalRestriction(args, origin, direction))...>(directionalRestriction(args, origin, direction)...);
}, f.functions());
}
/** \brief Sum of several functionals
*
* \tparam F List of functionals
*/
template<class... F>
class ShiftedSumFunctional
{
using FunctionTuple = std::tuple<F...>;
using F0 = std::tuple_element_t<0,FunctionTuple>;
public:
using Vector = typename F0::Vector;
using Range = typename F0::Range;
private:
using ShiftedFunctionTuple = std::tuple<decltype(shift(std::declval<F>(), std::declval<Vector>()))...>;
public:
using Functions = ShiftedFunctionTuple;
ShiftedSumFunctional(const F&... f, const Vector& origin) :
shiftedFunctions_(shift(f, origin)...)
{}
Range operator()(const Vector& v) const
{
Range y{0};
Dune::Hybrid::forEach(shiftedFunctions_, [&](auto&& f) {
y += f(v);
});
return y;
}
void updateOrigin()
{
Dune::Hybrid::forEach(shiftedFunctions_, [&](auto&& f) {
f.updateOrigin();
});
}
template<class Index>
void updateOrigin(Index i)
{
Dune::Hybrid::forEach(shiftedFunctions_, [&](auto&& f) {
f.updateOrigin(i);
});
}
const ShiftedFunctionTuple& functions() const
{
return shiftedFunctions_;
}
private:
ShiftedFunctionTuple shiftedFunctions_;
};
template<class... F, class Index>
auto coordinateRestriction(const ShiftedSumFunctional<F...>& f, const Index& i)
{
return Dune::Std::apply([&](const auto&... args) {
return SumFunctional<decltype(coordinateRestriction(args, i))...>(coordinateRestriction(args, i)...);
}, f.functions());
}
} // end namespace TNNMG
} // end namespace Dune
#endif // DUNE_TNNMG_FUNCTIONALS_NEWSUMFUNCTIONAL_HH
#ifndef DUNE_TNNMG_FUNCTIONALS_SUMFUNCTIONAL_HH
#define DUNE_TNNMG_FUNCTIONALS_SUMFUNCTIONAL_HH
#ifdef USE_OLD_TNNMG
#include <memory>
#include <dune/common/shared_ptr.hh>
#include "dune/tnnmg/functionals/nonsmoothconvexfunctional.hh"
#else
#include <dune/common/hybridutilities.hh>
#include <dune/common/std/apply.hh>
#endif
#ifndef USE_OLD_TNNMG
namespace Dune {
namespace TNNMG {
// Forward declaration
template<class... F>
class ShiftedSumFunctional;
/** \brief Sum of several functionals
*
* \tparam F List of functionals
*/
template<class... F>
class SumFunctional
{
using FunctionTuple = std::tuple<F...>;
using F0 = std::tuple_element_t<0,FunctionTuple>;
public:
using Functions = FunctionTuple;
using Vector = typename F0::Vector;
using Range = typename F0::Range;
SumFunctional(const F&... f) :
functions_(f...)
{}
Range operator()(const Vector& v) const
{
Range y{0};
Dune::Hybrid::forEach(functions_, [&](auto&& f) {
y += f(v);
});
return y;
}
const FunctionTuple& functions() const
{
return functions_;
}
friend ShiftedSumFunctional<F...>
shift(const SumFunctional& f, const Vector& origin)
{
return Dune::Std::apply([&](const auto&... args) {
return ShiftedSumFunctional<F...>(args..., origin);
}, f.functions());
}
private:
FunctionTuple functions_;
};
template<class... F, class Vector>
auto directionalRestriction(const SumFunctional<F...>& f, const Vector& origin, const Vector& direction)
{
return Dune::Std::apply([&](const auto&... args) {
return SumFunctional<decltype(directionalRestriction(args, origin, direction))...>(directionalRestriction(args, origin, direction)...);
}, f.functions());
}
/** \brief Sum of several functionals
*
* \tparam F List of functionals
*/
template<class... F>
class ShiftedSumFunctional
{
using FunctionTuple = std::tuple<F...>;
using F0 = std::tuple_element_t<0,FunctionTuple>;
public:
using Vector = typename F0::Vector;
using Range = typename F0::Range;
private:
using ShiftedFunctionTuple = std::tuple<decltype(shift(std::declval<F>(), std::declval<Vector>()))...>;
public:
using Functions = ShiftedFunctionTuple;
ShiftedSumFunctional(const F&... f, const Vector& origin) :
shiftedFunctions_(shift(f, origin)...)
{}
Range operator()(const Vector& v) const
{
Range y{0};
Dune::Hybrid::forEach(shiftedFunctions_, [&](auto&& f) {
y += f(v);
});
return y;
}
void updateOrigin()
{
Dune::Hybrid::forEach(shiftedFunctions_, [&](auto&& f) {
f.updateOrigin();
});
}
template<class Index>
void updateOrigin(Index i)
{
Dune::Hybrid::forEach(shiftedFunctions_, [&](auto&& f) {
f.updateOrigin(i);
});
}
const ShiftedFunctionTuple& functions() const
{
return shiftedFunctions_;
}
private:
ShiftedFunctionTuple shiftedFunctions_;
};
template<class... F, class Index>
auto coordinateRestriction(const ShiftedSumFunctional<F...>& f, const Index& i)
{
return Dune::Std::apply([&](const auto&... args) {
return SumFunctional<decltype(coordinateRestriction(args, i))...>(coordinateRestriction(args, i)...);
}, f.functions());
}
} // end namespace TNNMG
} // end namespace Dune
#else
namespace Dune {
namespace TNNMG {
......@@ -125,5 +269,6 @@ namespace Dune {
} // namespace TNNMG
} // namespace Dune
#endif
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment