<commit_before>// This file is part of the dune-pymor project:
//   https://github.com/pymor/dune-pymor
// Copyright holders: <NAME>, <NAME>
// License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)

#include <dune/stuff/test/test_common.hh>

#include <utility>

#include <dune/common/typetraits.hh>

#include <dune/stuff/common/float_cmp.hh>
#include <dune/stuff/la/container/common.hh>
#include <dune/stuff/la/container/eigen.hh>

#include <dune/pymor/common/exceptions.hh>
#include <dune/pymor/parameters/base.hh>
#include <dune/pymor/parameters/functional.hh>
#include <dune/pymor/functionals/interfaces.hh>
#include <dune/pymor/functionals/default.hh>
#include <dune/pymor/functionals/affine.hh>


using namespace Dune;
using namespace Dune::Pymor;

typedef testing::Types<
                        Stuff::LA::CommonDenseVector< double >
#if HAVE_EIGEN
                      , Stuff::LA::EigenDenseVector< double >
#endif
                      > VectorTypes;


static const size_t dim = 4;


template< class VectorType >
struct VectorBasedTest
  : public ::testing::Test
{
  void check() const
  {
    // static tests
    typedef Functionals::VectorBased< VectorType >  FunctionalType;
    typedef typename FunctionalType::Traits         Traits;
    // * of the traits
    typedef typename Traits::derived_type T_derived_type;
    static_assert(std::is_same< FunctionalType, T_derived_type >::value,
                  "FunctionalType::Traits::derived_type is wrong!");
    typedef typename Traits::SourceType   T_SourceType;
    typedef typename Traits::ScalarType   T_ScalarType;
    typedef typename Traits::FrozenType   T_FrozenType;
    // * of the class itself (aka the derived type)
    typedef typename FunctionalType::SourceType     D_SourceType;
    typedef typename FunctionalType::ScalarType     D_ScalarType;
    typedef typename FunctionalType::ContainerType  D_ContainerType;
    typedef typename FunctionalType::FrozenType     D_FrozenType;
    static_assert(std::is_same< D_ContainerType, VectorType >::value,
                  "FunctionalType::ContainerType is wrong!");
    // * of the class as the interface
    typedef FunctionalInterface< Traits >       InterfaceType;
    typedef typename InterfaceType::SourceType  I_SourceType;
    typedef typename InterfaceType::ScalarType  I_ScalarType;
    typedef typename InterfaceType::FrozenType  I_FrozenType;
    // dynamic tests
    // * of the class itself (aka the derived type)
    const FunctionalType d_from_ptr(new VectorType(dim, D_ScalarType(1)));
    FunctionalType d_from_shared_ptr(std::make_shared< VectorType >(dim));
    d_from_shared_ptr = d_from_ptr;
    FunctionalType DUNE_UNUSED(d_copy_constructor)(d_from_ptr); // <- at this point, all functionals share the same vector!
    const bool d_linear = d_from_ptr.linear();
    if (!d_linear) DUNE_PYMOR_THROW(PymorException, "");
    const DUNE_STUFF_SSIZE_T d_dim_source = d_from_ptr.dim_source();
    if (d_dim_source != dim) DUNE_PYMOR_THROW(PymorException, d_dim_source);
    const VectorType source(dim, D_ScalarType(1));
    const D_ScalarType d_apply = d_from_ptr.apply(source);
    if (Stuff::Common::FloatCmp::ne(d_apply, D_ScalarType(dim))) DUNE_PYMOR_THROW(PymorException, d_apply);
    // * of the class as the interface
    const InterfaceType& i_from_ptr = static_cast< const InterfaceType& >(d_from_ptr);
    const bool i_linear = i_from_ptr.linear();
    if (!i_linear) DUNE_PYMOR_THROW(PymorException, "");
    const DUNE_STUFF_SSIZE_T i_dim_source = i_from_ptr.dim_source();
    if (i_dim_source != dim) DUNE_PYMOR_THROW(PymorException, i_dim_source);
    const I_ScalarType i_apply = i_from_ptr.apply(source);
    if (Stuff::Common::FloatCmp::ne(i_apply, I_ScalarType(dim))) DUNE_PYMOR_THROW(PymorException, i_apply);
  }
}; // struct VectorBasedTest


TYPED_TEST_CASE(VectorBasedTest, VectorTypes);
TYPED_TEST(VectorBasedTest, FUNCTIONALS) {
  this->check();
}


template< class VectorType >
struct LinearAffinelyDecomposedVectorBasedTest
  : public ::testing::Test
{
  void check() const
  {
    // static tests
    typedef Functionals::LinearAffinelyDecomposedVectorBased< VectorType >  FunctionalType;
    typedef typename FunctionalType::Traits Traits;
    // * of the traits
    typedef typename Traits::derived_type T_derived_type;
    static_assert(std::is_same< FunctionalType, T_derived_type >::value,
                  "FunctionalType::Traits::derived_type is wrong!");
    typedef typename Traits::ComponentType ComponentType;
    // * of the class itself (aka the derived type)
    typedef typename FunctionalType::ComponentType  D_ComponentType;
    typedef typename FunctionalType::FrozenType     D_FrozenType;
    typedef typename FunctionalType::ScalarType     D_ScalarType;
    typedef typename FunctionalType::AffinelyDecomposedVectorType D_AffinelyDecomposedVectorType;
    // * of the class as the interface
    typedef AffinelyDecomposedFunctionalInterface< Traits > InterfaceType;
    typedef typename InterfaceType::derived_type I_derived_type;
    static_assert(std::is_same< FunctionalType, I_derived_type >::value,
                  "InterfaceType::derived_type is wrong!");
    typedef typename InterfaceType::ComponentType I_ComponentType;
    typedef typename InterfaceType::FrozenType    I_FrozenType;
    typedef typename InterfaceType::ScalarType    I_ScalarType;
    // dynamic tests
    // * of the class itself (aka the derived type)
    D_AffinelyDecomposedVectorType affinelyDecomposedVector(new VectorType(dim, 1));
    affinelyDecomposedVector.register_component(new VectorType(dim, 1),
                                                new ParameterFunctional("diffusion", 1, "diffusion[0]"));
    affinelyDecomposedVector.register_component(new VectorType(dim, 1),
                                                new ParameterFunctional("force", 2, "force[0]"));
    const Parameter mu = {{"diffusion", "force"},
                          {{1.0}, {1.0, 1.0}}};
    if (affinelyDecomposedVector.parameter_type() != mu.type())
      DUNE_PYMOR_THROW(PymorException,
                       "\nmu.type()                                 = " << mu.type()
                       << "\naffinelyDecomposedVector.parameter_type() = "
                       << affinelyDecomposedVector.parameter_type());
    FunctionalType d_functional(affinelyDecomposedVector);
    if (!d_functional.parametric()) DUNE_PYMOR_THROW(PymorException, "");
    if (d_functional.parameter_type() != mu.type())
      DUNE_PYMOR_THROW(PymorException,
                       "\nmu.type()         = " << mu.type()
                       << "\n.parameter_type() = " << d_functional.parameter_type());
    const DUNE_STUFF_SSIZE_T d_num_components = d_functional.num_components();<commit_msg>[test.functionals] fixed scalar parameter functional usage<commit_after>// This file is part of the dune-pymor project:
//   https://github.com/pymor/dune-pymor
// Copyright holders: <NAME>, <NAME>
// License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)

#include <dune/stuff/test/test_common.hh>

#include <utility>

#include <dune/common/typetraits.hh>

#include <dune/stuff/common/float_cmp.hh>
#include <dune/stuff/la/container/common.hh>
#include <dune/stuff/la/container/eigen.hh>

#include <dune/pymor/common/exceptions.hh>
#include <dune/pymor/parameters/base.hh>
#include <dune/pymor/parameters/functional.hh>
#include <dune/pymor/functionals/interfaces.hh>
#include <dune/pymor/functionals/default.hh>
#include <dune/pymor/functionals/affine.hh>


using namespace Dune;
using namespace Dune::Pymor;

typedef testing::Types<
                        Stuff::LA::CommonDenseVector< double >
#if HAVE_EIGEN
                      , Stuff::LA::EigenDenseVector< double >
#endif
                      > VectorTypes;


static const size_t dim = 4;


template< class VectorType >
struct VectorBasedTest
  : public ::testing::Test
{
  void check() const
  {
    // static tests
    typedef Functionals::VectorBased< VectorType >  FunctionalType;
    typedef typename FunctionalType::Traits         Traits;
    // * of the traits
    typedef typename Traits::derived_type T_derived_type;
    static_assert(std::is_same< FunctionalType, T_derived_type >::value,
                  "FunctionalType::Traits::derived_type is wrong!");
    typedef typename Traits::SourceType   T_SourceType;
    typedef typename Traits::ScalarType   T_ScalarType;
    typedef typename Traits::FrozenType   T_FrozenType;
    // * of the class itself (aka the derived type)
    typedef typename FunctionalType::SourceType     D_SourceType;
    typedef typename FunctionalType::ScalarType     D_ScalarType;
    typedef typename FunctionalType::ContainerType  D_ContainerType;
    typedef typename FunctionalType::FrozenType     D_FrozenType;
    static_assert(std::is_same< D_ContainerType, VectorType >::value,
                  "FunctionalType::ContainerType is wrong!");
    // * of the class as the interface
    typedef FunctionalInterface< Traits >       InterfaceType;
    typedef typename InterfaceType::SourceType  I_SourceType;
    typedef typename InterfaceType::ScalarType  I_ScalarType;
    typedef typename InterfaceType::FrozenType  I_FrozenType;
    // dynamic tests
    // * of the class itself (aka the derived type)
    const FunctionalType d_from_ptr(new VectorType(dim, D_ScalarType(1)));
    FunctionalType d_from_shared_ptr(std::make_shared< VectorType >(dim));
    d_from_shared_ptr = d_from_ptr;
    FunctionalType DUNE_UNUSED(d_copy_constructor)(d_from_ptr); // <- at this point, all functionals share the same vector!
    const bool d_linear = d_from_ptr.linear();
    if (!d_linear) DUNE_PYMOR_THROW(PymorException, "");
    const DUNE_STUFF_SSIZE_T d_dim_source = d_from_ptr.dim_source();
    if (d_dim_source != dim) DUNE_PYMOR_THROW(PymorException, d_dim_source);
    const VectorType source(dim, D_ScalarType(1));
    const D_ScalarType d_apply = d_from_ptr.apply(source);
    if (Stuff::Common::FloatCmp::ne(d_apply, D_ScalarType(dim))) DUNE_PYMOR_THROW(PymorException, d_apply);
    // * of the class as the interface
    const InterfaceType& i_from_ptr = static_cast< const InterfaceType& >(d_from_ptr);
    const bool i_linear = i_from_ptr.linear();
    if (!i_linear) DUNE_PYMOR_THROW(PymorException, "");
    const DUNE_STUFF_SSIZE_T i_dim_source = i_from_ptr.dim_source();
    if (i_dim_source != dim) DUNE_PYMOR_THROW(PymorException, i_dim_source);
    const I_ScalarType i_apply = i_from_ptr.apply(source);
    if (Stuff::Common::FloatCmp::ne(i_apply, I_ScalarType(dim))) DUNE_PYMOR_THROW(PymorException, i_apply);
  }
}; // struct VectorBasedTest


TYPED_TEST_CASE(VectorBasedTest, VectorTypes);
TYPED_TEST(VectorBasedTest, FUNCTIONALS) {
  this->check();
}


template< class VectorType >
struct LinearAffinelyDecomposedVectorBasedTest
  : public ::testing::Test
{
  void check() const
  {
    // static tests
    typedef Functionals::LinearAffinelyDecomposedVectorBased< VectorType >  FunctionalType;
    typedef typename FunctionalType::Traits Traits;
    // * of the traits
    typedef typename Traits::derived_type T_derived_type;
    static_assert(std::is_same< FunctionalType, T_derived_type >::value,
                  "FunctionalType::Traits::derived_type is wrong!");
    typedef typename Traits::ComponentType ComponentType;
    // * of the class itself (aka the derived type)
    typedef typename FunctionalType::ComponentType  D_ComponentType;
    typedef typename FunctionalType::FrozenType     D_FrozenType;
    typedef typename FunctionalType::ScalarType     D_ScalarType;
    typedef typename FunctionalType::AffinelyDecomposedVectorType D_AffinelyDecomposedVectorType;
    // * of the class as the interface
    typedef AffinelyDecomposedFunctionalInterface< Traits > InterfaceType;
    typedef typename InterfaceType::derived_type I_derived_type;
    static_assert(std::is_same< FunctionalType, I_derived_type >::value,
                  "InterfaceType::derived_type is wrong!");
    typedef typename InterfaceType::ComponentType I_ComponentType;
    typedef typename InterfaceType::FrozenType    I_FrozenType;
    typedef typename InterfaceType::ScalarType    I_ScalarType;
    // dynamic tests
    // * of the class itself (aka the derived type)
    D_AffinelyDecomposedVectorType affinelyDecomposedVector(new VectorType(dim, 1));
    affinelyDecomposedVector.register_component(new VectorType(dim, 1),
                                                new ParameterFunctional("diffusion", 1, "diffusion"));
    affinelyDecomposedVector.register_component(new VectorType(dim, 1),
                                                new ParameterFunctional("force", 2, "force[0]"));
    const Parameter mu = {{"diffusion", "force"},
                          {{1.0}, {1.0, 1.0}}};
    if (affinelyDecomposedVector.parameter_type() != mu.type())
      DUNE_PYMOR_THROW(PymorException,
                       "\nmu.type()                                 = " << mu.type()
                       << "\naffinelyDecomposedVector.parameter_type() = "
                       << affinelyDecomposedVector.parameter_type());
    FunctionalType d_functional(affinelyDecomposedVector);
    if (!d_functional.parametric()) DUNE_PYMOR_THROW(PymorException, "");
    if (d_functional.parameter_type() != mu.type())
      DUNE_PYMOR_THROW(PymorException,
                       "\nmu.type()         = " << mu.type()
                       << "\n.parameter_type() = " << d_functional.parameter_type());
    const DUNE_STUFF_SSIZE_T d_num_components = d_functional.num_components();
{'fraction_non_alphanumeric': 0.1015015457088179, 'fraction_numerical': 0.002576181363167967, 'mean_word_length': 3.7292029237730597, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 63, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1265261', 'n_tokens_mistral': 4324, 'n_tokens_neox': 4112, 'n_words': 854}