Skip to content
Snippets Groups Projects
Commit a946cf04 authored by Lasse Hinrichsen's avatar Lasse Hinrichsen
Browse files

clean up

parent c2bd24db
No related branches found
No related tags found
1 merge request!25WIP: Semilinear functional
...@@ -237,14 +237,6 @@ public: ...@@ -237,14 +237,6 @@ public:
return Semilinearfunctional(f.weights_, new_origin, f.phi_); return Semilinearfunctional(f.weights_, new_origin, f.phi_);
} }
friend auto derivative(const Semilinearfunctional& f)
{
// TODO: is this too much to ask for?
// auto phi_prime = derivative(f.phi_);
DUNE_THROW(Dune::NotImplemented, "Derivative not yet implemented");
return 42;
}
friend auto directionalRestriction(const Semilinearfunctional& f, friend auto directionalRestriction(const Semilinearfunctional& f,
const Vector& origin, const Vector& origin,
const Vector& direction) const Vector& direction)
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <dune/common/hybridutilities.hh> #include <dune/common/hybridutilities.hh>
#include <dune/common/typetraits.hh> #include <dune/common/typetraits.hh>
#include <dune/matrix-vector/algorithm.hh> #include <dune/matrix-vector/algorithm.hh>
#include <dune/tnnmg/functionals/semilinearfunctional.hh>
#include <type_traits> #include <type_traits>
namespace Dune::TNNMG { namespace Dune::TNNMG {
...@@ -12,24 +13,6 @@ actually could be applied in other contexts, too. Some are even part of open MR ...@@ -12,24 +13,6 @@ actually could be applied in other contexts, too. Some are even part of open MR
that gathered dust by now. that gathered dust by now.
*/ */
/** \brief For two vectors of equal size (and blocking), apply a scalar function
* componentswise such that u_i = f(v_i).
*/
template<typename F, typename V>
void
applyComponentwise(F&& f, V& u, const V& v)
{
namespace H = Dune::Hybrid;
H::ifElse(
Dune::IsNumber<std::decay_t<V>>(),
[&](auto&& id) { u = f(id(v)); },
[&](auto id) {
H::forEach(H::integralRange(H::size(id(v))), [&](auto i) {
applyComponentwise(f, H::elementAt(u, i), H::elementAt(v, i));
});
});
}
/** Multiply the i-th component of u by the i-th component of v, i.e. /** Multiply the i-th component of u by the i-th component of v, i.e.
* u_i <- u_i*v_i * u_i <- u_i*v_i
*/ */
...@@ -38,14 +21,13 @@ void ...@@ -38,14 +21,13 @@ void
multiplyComponentwise(V& u, const V& v) multiplyComponentwise(V& u, const V& v)
{ {
namespace H = Dune::Hybrid; namespace H = Dune::Hybrid;
H::ifElse( if constexpr (Dune::IsNumber<std::decay_t<V>>()) {
Dune::IsNumber<std::decay_t<V>>(), u *= v;
[&](auto&& id) { u *= id(v); }, } else {
[&](auto id) { H::forEach(H::integralRange(H::size(v)), [&](auto i) {
H::forEach(H::integralRange(H::size(id(v))), [&](auto i) { multiplyComponentwise(H::elementAt(u, i), H::elementAt(v, i));
multiplyComponentwise(H::elementAt(u, i), H::elementAt(v, i));
});
}); });
}
} }
template<class M, class V> template<class M, class V>
...@@ -53,15 +35,15 @@ void ...@@ -53,15 +35,15 @@ void
writeDiagonal(M& m, V&& v) writeDiagonal(M& m, V&& v)
{ {
namespace H = Dune::Hybrid; namespace H = Dune::Hybrid;
H::ifElse( if constexpr (Dune::IsNumber<std::decay_t<V>>()) {
Dune::IsNumber<std::decay_t<V>>(), m = v;
[&](auto&& id) { m = id(v); }, } else {
[&](auto&& id) { H::forEach(H::integralRange(H::size(v)), [&](auto i) {
H::forEach(H::integralRange(H::size(id(v))), [&](auto i) { writeDiagonal(H::elementAt(H::elementAt(m, i), i), H::elementAt(v, i));
writeDiagonal(H::elementAt(H::elementAt(m, i), i), H::elementAt(v, i));
});
}); });
}
} }
template<class NV, class NBV, class F> template<class NV, class NBV, class F>
void void
determineTruncation(const NV& x, NBV&& truncationFlags, const F& function) determineTruncation(const NV& x, NBV&& truncationFlags, const F& function)
...@@ -72,7 +54,8 @@ determineTruncation(const NV& x, NBV&& truncationFlags, const F& function) ...@@ -72,7 +54,8 @@ determineTruncation(const NV& x, NBV&& truncationFlags, const F& function)
} else { } else {
assert(x.size() == truncationFlags.size()); assert(x.size() == truncationFlags.size());
H::forEach(H::integralRange(H::size(x)), [&](auto&& i) { H::forEach(H::integralRange(H::size(x)), [&](auto&& i) {
determineTruncation(x[i], truncationFlags[i], function); // todo element_at determineTruncation(
H::elementAt(x, i), H::elementAt(truncationFlags, i), function);
}); });
} }
} }
...@@ -86,8 +69,9 @@ truncateVector(NV& x, const NBV& truncationFlags) ...@@ -86,8 +69,9 @@ truncateVector(NV& x, const NBV& truncationFlags)
if (truncationFlags) if (truncationFlags)
x = 0; x = 0;
} else { } else {
H::forEach(H::integralRange(H::size(x)), H::forEach(H::integralRange(H::size(x)), [&](auto&& i) {
[&](auto&& i) { truncateVector(x[i], truncationFlags[i]); }); truncateVector(H::elementAt(x, i), H::elementAt(truncationFlags, i));
});
} }
} }
...@@ -104,9 +88,11 @@ truncateMatrix(NM& A, ...@@ -104,9 +88,11 @@ truncateMatrix(NM& A,
A = 0; A = 0;
} else { } else {
H::forEach(H::integralRange(H::size(rowTruncationFlags)), [&](auto&& i) { H::forEach(H::integralRange(H::size(rowTruncationFlags)), [&](auto&& i) {
auto&& Ai = A[i]; auto&& Ai = H::elementAt(A, i);
sparseRangeFor(Ai, [&](auto&& Aij, auto&& j) { sparseRangeFor(Ai, [&](auto&& Aij, auto&& j) {
truncateMatrix(Aij, rowTruncationFlags[i], colTruncationFlags[j]); truncateMatrix(Aij,
H::elementAt(rowTruncationFlags, i),
H::elementAt(colTruncationFlags, i));
}); });
}); });
} }
...@@ -158,14 +144,14 @@ public: ...@@ -158,14 +144,14 @@ public:
// Assuming that the supplied function does not explode if it is evaluated // Assuming that the supplied function does not explode if it is evaluated
// in a point where it's actually not defined. // in a point where it's actually not defined.
Helper::applyComponentwise(derivative(f_.phi()), ngrad_, x); Impl::applyComponentwise(derivative(f_.phi()), ngrad_, x);
Helper::multiplyComponentwise(ngrad_, f_.weights()); Helper::multiplyComponentwise(ngrad_, f_.weights());
ngrad_ *= -1; ngrad_ *= -1;
mat_ = 0; mat_ = 0;
// compute diagonal entries // compute diagonal entries
auto diag = x; auto diag = x;
Helper::applyComponentwise(derivative(derivative(f_.phi())), diag, x); Impl::applyComponentwise(derivative(derivative(f_.phi())), diag, x);
Helper::multiplyComponentwise(diag, f_.weights()); Helper::multiplyComponentwise(diag, f_.weights());
// now, write them into the diagonal of the Hessian // now, write them into the diagonal of the Hessian
Helper::writeDiagonal(mat_, diag); Helper::writeDiagonal(mat_, diag);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment