Skip to content
Snippets Groups Projects
Select Git revision
  • 99b8f51179e5120bdf59fc165b4fe3813013a803
  • develop default protected
  • feature/variational-hydro
  • origin/stage/bouguettaia
  • feature/gmsh-reader
  • feature/reconstruction
  • save_clemence
  • feature/kinetic-schemes
  • feature/local-dt-fsi
  • feature/composite-scheme-sources
  • feature/composite-scheme-other-fluxes
  • feature/serraille
  • feature/composite-scheme
  • hyperplastic
  • feature/polynomials
  • feature/gks
  • feature/implicit-solver-o2
  • feature/coupling_module
  • feature/implicit-solver
  • feature/merge-local-dt-fsi
  • master protected
  • v0.5.0 protected
  • v0.4.1 protected
  • v0.4.0 protected
  • v0.3.0 protected
  • v0.2.0 protected
  • v0.1.0 protected
  • Kidder
  • v0.0.4 protected
  • v0.0.3 protected
  • v0.0.2 protected
  • v0 protected
  • v0.0.1 protected
33 results

TinyMatrix.hpp

Blame
  • ItemValue.hpp 6.42 KiB
    #ifndef ITEM_VALUE_HPP
    #define ITEM_VALUE_HPP
    
    #include <PastisAssert.hpp>
    #include <PastisOStream.hpp>
    
    #include <Array.hpp>
    
    #include <ItemType.hpp>
    #include <ItemId.hpp>
    
    #include <IConnectivity.hpp>
    #include <memory>
    
    template <typename DataType,
              ItemType item_type,
              typename ConnectivityPtr = std::shared_ptr<const IConnectivity>>
    class ItemValue
    {
     public:
      static const ItemType item_t{item_type};
      using data_type = DataType;
    
      using ItemId = ItemIdT<item_type>;
      using index_type = ItemId;
    
     private:
      using ConnectivitySharedPtr = std::shared_ptr<const IConnectivity>;
      using ConnectivityWeakPtr = std::weak_ptr<const IConnectivity>;
    
      static_assert(std::is_same_v<ConnectivityPtr, ConnectivitySharedPtr> or
                    std::is_same_v<ConnectivityPtr, ConnectivityWeakPtr>);
    
      ConnectivityPtr m_connectivity_ptr;
    
      bool m_is_built{false};
    
      Array<DataType> m_values;
    
      // Allow const std:shared_ptr version to access our data
      friend ItemValue<std::add_const_t<DataType>, item_type,
                       ConnectivitySharedPtr>;
    
      // Allow const std:weak_ptr version to access our data
      friend ItemValue<std::add_const_t<DataType>, item_type,
                       ConnectivityWeakPtr>;
    
      friend PASTIS_INLINE
      ItemValue<std::remove_const_t<DataType>,item_type, ConnectivityPtr>
      copy(const ItemValue<DataType, item_type, ConnectivityPtr>& source)
      {
        ItemValue<std::remove_const_t<DataType>, item_type, ConnectivityPtr> image(source);
    
        image.m_values = copy(source.m_values);
        return image;
      }
    
     public:
      PASTIS_INLINE
      bool isBuilt() const
      {
        return m_connectivity_ptr.use_count() != 0;
      }
    
      PASTIS_INLINE
      std::shared_ptr<const IConnectivity> connectivity_ptr() const
      {
        if constexpr (std::is_same_v<ConnectivityPtr, ConnectivitySharedPtr>) {
          return m_connectivity_ptr;
        } else {
          return m_connectivity_ptr.lock();
        }
      }
    
      PASTIS_INLINE
      size_t size() const
      {
        Assert(this->isBuilt());
        return m_values.size();
      }
    
      PASTIS_INLINE
      void fill(const DataType& data) const
      {
        static_assert(not std::is_const<DataType>(),
                      "Cannot modify ItemValue of const");
        m_values.fill(data);
      }
    
      // Following Kokkos logic, these classes are view and const view does allow
      // changes in data
      PASTIS_FORCEINLINE
      DataType& operator[](const ItemId& i) const noexcept(NO_ASSERT)
      {
        Assert(this->isBuilt());
        return m_values[i];
      }
    
      template <typename IndexType>
      DataType& operator[](const IndexType& i) const noexcept(NO_ASSERT)
      {
        static_assert(std::is_same<IndexType,ItemId>(),
                      "ItemValue must be indexed by ItemId");
        static_assert(not std::is_const<DataType>(),
                      "Cannot modify ItemValue of const");
        return m_values[i];
      }
    
      PASTIS_INLINE
      size_t numberOfItems() const
      {
        Assert(this->isBuilt());
        return m_values.size();
      }
    
      template <typename DataType2>
      PASTIS_INLINE
      ItemValue&
      operator=(const Array<DataType2>& values)
      {
        // ensures that DataType is the same as source DataType2
        static_assert(std::is_same<std::remove_const_t<DataType>, std::remove_const_t<DataType2>>(),
                      "Cannot assign ItemValue of different type");
        // ensures that const is not lost through copy
        static_assert(((std::is_const<DataType2>() and std::is_const<DataType>())
                       or not std::is_const<DataType2>()),
                      "Cannot assign ItemValue of const to ItemValue of non-const");
    
        if (not this->isBuilt()) {
          perr() << "Cannot assign an array of values to a non-built ItemValue\n";
          std::terminate();
        }
    
        if (m_values.size() != values.size()) {
          perr() << "Cannot assign an array of values of a different size\n";
          std::terminate();
        }
    
        if (values.size() > 0) {
          if (not this->isBuilt()) {
            perr() << "Cannot assign array of values to a non-built ItemValue\n";
            std::terminate();
          }
    
          m_values = values;
        }
    
        return *this;
      }
    
      template <typename DataType2,
                typename ConnectivityPtr2>
      PASTIS_INLINE
      ItemValue&
      operator=(const ItemValue<DataType2, item_type, ConnectivityPtr2>& value_per_item)
      {
        // ensures that DataType is the same as source DataType2
        static_assert(std::is_same<std::remove_const_t<DataType>, std::remove_const_t<DataType2>>(),
                      "Cannot assign ItemValue of different type");
        // ensures that const is not lost through copy
        static_assert(((std::is_const<DataType2>() and std::is_const<DataType>())
                       or not std::is_const<DataType2>()),
                      "Cannot assign ItemValue of const to ItemValue of non-const");
    
        m_values = value_per_item.m_values;
        if constexpr (std::is_same_v<ConnectivityPtr, ConnectivitySharedPtr> and
                      std::is_same_v<ConnectivityPtr2, ConnectivityWeakPtr>) {
          m_connectivity_ptr = value_per_item.m_connectivity_ptr.lock();
        } else {
          m_connectivity_ptr = value_per_item.m_connectivity_ptr;
        }
    
        return *this;
      }
    
      template <typename DataType2,
                typename ConnectivityPtr2>
      PASTIS_INLINE
      ItemValue(const ItemValue<DataType2, item_type, ConnectivityPtr2>& value_per_item)
      {
        this->operator=(value_per_item);
      }
    
      PASTIS_INLINE
      ItemValue() = default;
    
      PASTIS_INLINE
      ItemValue(const IConnectivity& connectivity)
          : m_connectivity_ptr{connectivity.shared_ptr()},
            m_values{connectivity.numberOf<item_type>()}
      {
        static_assert(not std::is_const<DataType>(),
                      "Cannot allocate ItemValue of const data: only view is supported"); ;
      }
    
      PASTIS_INLINE
      ~ItemValue() = default;
    };
    
    template <typename DataType>
    using NodeValue = ItemValue<DataType, ItemType::node>;
    
    template <typename DataType>
    using EdgeValue = ItemValue<DataType, ItemType::edge>;
    
    template <typename DataType>
    using FaceValue = ItemValue<DataType, ItemType::face>;
    
    template <typename DataType>
    using CellValue = ItemValue<DataType, ItemType::cell>;
    
    template <typename DataType>
    using WeakNodeValue = ItemValue<DataType, ItemType::node, std::weak_ptr<const IConnectivity>>;
    
    template <typename DataType>
    using WeakEdgeValue = ItemValue<DataType, ItemType::edge, std::weak_ptr<const IConnectivity>>;
    
    template <typename DataType>
    using WeakFaceValue = ItemValue<DataType, ItemType::face, std::weak_ptr<const IConnectivity>>;
    
    template <typename DataType>
    using WeakCellValue = ItemValue<DataType, ItemType::cell, std::weak_ptr<const IConnectivity>>;
    
    #endif // ITEM_VALUE_HPP