From ec5abd62424b9855db4c5da92e3bcb5f89dc383c Mon Sep 17 00:00:00 2001
From: Tomas Oberhuber <tomas.oberhuber@fjfi.cvut.cz>
Date: Sun, 16 Aug 2015 22:57:32 +0200
Subject: [PATCH] Implementing superentity initializer.

---
 TODO                                          |   3 +-
 src/mesh/config/tnlMeshConfigBase.h           |   6 +-
 src/mesh/layers/tnlMeshStorageLayer.h         |  70 ++++++++++--
 src/mesh/layers/tnlMeshSuperentityAccess.h    | 107 ++++++++++++++++++
 .../layers/tnlMeshSuperentityStorageLayer.h   |  28 ++++-
 src/mesh/tnlMesh.h                            |  60 ++++++----
 src/mesh/tnlMeshEntity.h                      | 100 +++++++++-------
 src/mesh/tnlMeshEntityInitializer.h           |  20 ++--
 src/mesh/tnlMeshInitializer.h                 |  73 ++++++++++--
 src/mesh/tnlMeshSuperentityInitializerLayer.h |  95 ++++++++++------
 src/mesh/traits/tnlMeshConfigTraits.h         |  11 +-
 src/mesh/traits/tnlMeshSuperentitiesTraits.h  |   6 +-
 tests/unit-tests/mesh/tnlMeshEntityTester.h   |  12 +-
 13 files changed, 443 insertions(+), 148 deletions(-)
 create mode 100644 src/mesh/layers/tnlMeshSuperentityAccess.h

diff --git a/TODO b/TODO
index d885b32df7..a6e199989e 100644
--- a/TODO
+++ b/TODO
@@ -1,6 +1,7 @@
 TODO: Mesh
  * vsechny traits zkusit presunout do jednotneho MeshTraits, tj. temer MeshConfigTraits ale pojmenovat jako MeshTraits
- * zrusit tnlDimesnionsTag
+ * omezit tnlDimesnionsTag - asi to ale nepujde
+   - ale pozor na konstrukce jako BaseType::superentityIdsArray< SuperDimensionTag >( DimensionsTag ); ( v tnlMesh.h)
  * prejmenovat Tagy v topologies na Topology zrejme
  * zrusit tnlStorageTraits
 
diff --git a/src/mesh/config/tnlMeshConfigBase.h b/src/mesh/config/tnlMeshConfigBase.h
index d27d5183fb..5f03b1de77 100644
--- a/src/mesh/config/tnlMeshConfigBase.h
+++ b/src/mesh/config/tnlMeshConfigBase.h
@@ -54,7 +54,8 @@ struct tnlMeshConfigBase
       /****
        *  Vertices and cells must always be stored
        */ 
-		return ( dimensions == 0 || dimensions == cellDimensions );
+      return true;
+		//return ( dimensions == 0 || dimensions == cellDimensions );
 	}
    
    /****
@@ -66,7 +67,8 @@ struct tnlMeshConfigBase
       /****
        *  Vertices must always be stored
        */
-		return ( SubentityDimensions == 0 );
+      return true;
+		//return ( SubentityDimensions == 0 );
 	}
 
 	/****
diff --git a/src/mesh/layers/tnlMeshStorageLayer.h b/src/mesh/layers/tnlMeshStorageLayer.h
index f09ba30a1a..2884bfa622 100644
--- a/src/mesh/layers/tnlMeshStorageLayer.h
+++ b/src/mesh/layers/tnlMeshStorageLayer.h
@@ -22,6 +22,10 @@
 #include <mesh/traits/tnlMeshTraits.h>
 #include <mesh/traits/tnlMeshEntitiesTraits.h>
 #include <mesh/traits/tnlStorageTraits.h>
+#include <mesh/traits/tnlMeshConfigTraits.h>
+
+template< typename MeshConfig >
+class tnlMesh;
 
 template< typename DimensionsTag,
           typename Device,
@@ -41,7 +45,8 @@ template< typename ConfigTag >
 class tnlMeshStorageLayers
    : public tnlMeshStorageLayer< ConfigTag,
                                  typename tnlMeshTraits< ConfigTag >::DimensionsTag >
-{};
+{
+};
 
 
 template< typename ConfigTag,
@@ -49,18 +54,23 @@ template< typename ConfigTag,
 class tnlMeshStorageLayer< ConfigTag,
                            DimensionsTag,
                            tnlStorageTraits< true > >
-   : public tnlMeshStorageLayer< ConfigTag, typename DimensionsTag::Decrement >
+   : public tnlMeshStorageLayer< ConfigTag, typename DimensionsTag::Decrement >,
+     public tnlMeshSuperentityStorageLayers< ConfigTag, 
+                                             typename tnlMeshConfigTraits< ConfigTag >::template EntityTraits< DimensionsTag >::Tag >
 {
+   public:
+
    typedef tnlMeshStorageLayer< ConfigTag,
                                 typename DimensionsTag::Decrement >   BaseType;
-
+   typedef tnlMeshSuperentityStorageLayers< ConfigTag, 
+                                            typename tnlMeshConfigTraits< ConfigTag >::template EntityTraits< DimensionsTag >::Tag > SuperentityStorageBaseType;
+   
    typedef tnlMeshEntitiesTraits< ConfigTag, DimensionsTag >         Tag;
    typedef typename Tag::ContainerType                                  ContainerType;
    typedef typename Tag::SharedContainerType                            SharedContainerType;
    typedef typename ContainerType::IndexType                            GlobalIndexType;
    typedef typename ContainerType::ElementType                          EntityType;
 
-   protected:
 
    using BaseType::setNumberOfEntities;
    using BaseType::getNumberOfEntities;
@@ -164,9 +174,27 @@ class tnlMeshStorageLayer< ConfigTag,
 
 
    protected:
-   ContainerType entities;
+      ContainerType entities;
 
-   SharedContainerType sharedEntities;
+      SharedContainerType sharedEntities;
+   
+   // TODO: this is only for the mesh initializer - fix it
+   public:
+
+      using BaseType::entitiesArray;
+      
+      typename tnlMeshConfigTraits< ConfigTag >::template EntityTraits< DimensionsTag >::ContainerType& entitiesArray( DimensionsTag )
+      {
+         return entities; 
+      }
+              
+      using BaseType::superentityIdsArray;
+	
+      template< typename SuperDimensionsTag >
+      typename tnlMeshConfigTraits< ConfigTag >::GlobalIdArrayType& superentityIdsArray( DimensionsTag )
+      {
+         return SuperentityStorageBaseType::superentityIdsArray( SuperDimensionsTag() );
+      }
 };
 
 template< typename ConfigTag,
@@ -182,9 +210,18 @@ class tnlMeshStorageLayer< ConfigTag,
 template< typename ConfigTag >
 class tnlMeshStorageLayer< ConfigTag,
                            tnlDimensionsTag< 0 >,
-                           tnlStorageTraits< true > >
+                           tnlStorageTraits< true > > :
+   public tnlMeshSuperentityStorageLayers< ConfigTag, 
+                                           tnlMeshVertexTag >
+
 {
+   public:
+
    typedef tnlDimensionsTag< 0 >                        DimensionsTag;
+   
+   typedef tnlMeshSuperentityStorageLayers< ConfigTag, 
+                                            tnlMeshVertexTag > SuperentityStorageBaseType;
+
 
    typedef tnlMeshEntitiesTraits< ConfigTag,
                                   DimensionsTag >       Tag;
@@ -194,7 +231,6 @@ class tnlMeshStorageLayer< ConfigTag,
    typedef typename ContainerType::ElementType             VertexType;
    typedef typename VertexType::PointType                  PointType;
 
-   protected:
 
    tnlMeshStorageLayer()
    {
@@ -329,6 +365,22 @@ class tnlMeshStorageLayer< ConfigTag,
    ContainerType vertices;
 
    SharedContainerType sharedVertices;
+   
+   // TODO: this is only for the mesh initializer - fix it
+   public:
+      
+      typename tnlMeshConfigTraits< ConfigTag >::template EntityTraits< DimensionsTag >::ContainerType& entitiesArray( DimensionsTag )
+      {
+         return vertices; 
+      }
+
+      
+      template< typename SuperDimensionsTag >
+      typename tnlMeshConfigTraits< ConfigTag >::GlobalIdArrayType& superentityIdsArray( DimensionsTag )
+      {
+         return SuperentityStorageBaseType::superentityIdsArray( SuperDimensionsTag() );
+      }
+
 };
 
 /****
@@ -341,7 +393,7 @@ class tnlMeshStorageLayer< ConfigTag,
 {
    protected:
 
-   void setNumberOfEntities();
+   void setNumberOfEntities();   
 };
 
 
diff --git a/src/mesh/layers/tnlMeshSuperentityAccess.h b/src/mesh/layers/tnlMeshSuperentityAccess.h
new file mode 100644
index 0000000000..1a90d875c7
--- /dev/null
+++ b/src/mesh/layers/tnlMeshSuperentityAccess.h
@@ -0,0 +1,107 @@
+/***************************************************************************
+                          tnlMeshSuperentityAccess.h  -  description
+                             -------------------
+    begin                : Aug 15, 2015
+    copyright            : (C) 2015 by Tomas Oberhuber et al.
+    email                : tomas.oberhuber@fjfi.cvut.cz
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef TNLSUPERENTITYACCESS_H
+#define	TNLSUPERENTITYACCESS_H
+
+#include <mesh/traits/tnlStorageTraits.h>
+#include <mesh/traits/tnlMeshConfigTraits.h>
+
+
+template< typename MeshConfig,
+          typename MeshEntity,
+          typename Dimensions,
+          typename SuperentityStorage = 
+             tnlStorageTraits< tnlMeshConfigTraits< MeshConfig >::template SuperentityTraits< MeshEntity, Dimensions>::storageEnabled > >
+class tnlMeshSuperentityAccessLayer;
+
+
+template< typename MeshConfig,
+          typename MeshEntity >
+class tnlMeshSuperentityAccess :
+   public tnlMeshSuperentityAccessLayer< MeshConfig, 
+                                         MeshEntity,
+                                         tnlDimensionsTag< tnlMeshConfigTraits< MeshConfig >::meshDimensions > >
+{
+   public:
+      bool operator == ( const tnlMeshSuperentityAccess< MeshConfig, MeshEntity>& a ) const { return true; } // TODO: fix
+      
+      void print( ostream& str ) const{};
+
+};
+
+template< typename MeshConfig,
+          typename MeshEntity,
+          typename Dimensions >
+class tnlMeshSuperentityAccessLayer< MeshConfig,
+                                     MeshEntity,
+                                     Dimensions,
+                                     tnlStorageTraits< true > > :
+   public tnlMeshSuperentityAccessLayer< MeshConfig, MeshEntity, typename Dimensions::Decrement >
+{
+	typedef tnlMeshSuperentityAccessLayer< MeshConfig, MeshEntity, typename Dimensions::Decrement > BaseType;
+
+   public:
+	   typedef typename tnlMeshConfigTraits< MeshConfig >::IdArrayAccessorType    IdArrayAccessorType;
+
+	   using BaseType::superentityIds;
+	   IdArrayAccessorType superentityIds( Dimensions ) const { return m_superentityIndices; }
+
+	   using BaseType::superentityIdsArray;
+	   IdArrayAccessorType &superentityIdsArray( Dimensions ) { return m_superentityIndices; }
+      
+      //bool operator == ( const tnlMeshSuperentityAccessLayer< MeshConfig, MeshEntity, Dimensions, tnlStorageTraits< true > >& l ) { return true; } // TODO: fix
+
+   private:
+	   IdArrayAccessorType m_superentityIndices;
+};
+
+template< typename MeshConfig,
+          typename MeshEntity,
+          typename Dimensions >
+class tnlMeshSuperentityAccessLayer< MeshConfig, MeshEntity, Dimensions, tnlStorageTraits< false > > :
+   public tnlMeshSuperentityAccessLayer< MeshConfig, MeshEntity, typename Dimensions::Decrement >
+{
+};
+
+template< typename MeshConfig,
+          typename MeshEntity >
+class tnlMeshSuperentityAccessLayer< MeshConfig, MeshEntity, tnlDimensionsTag< MeshEntity::dimensions >, tnlStorageTraits< false > >
+{
+   protected:
+	   /***
+       * Necessary because of 'using TBase::...;' in the derived classes
+       */
+	   void superentityIds()      {}
+	   void superentityIdsArray() {}
+};
+
+template< typename MeshConfig,
+          typename MeshEntity >
+class tnlMeshSuperentityAccessLayer< MeshConfig, MeshEntity, tnlDimensionsTag< MeshEntity::dimensions >, tnlStorageTraits< true > >
+{
+   protected:
+	   /***
+       * Necessary because of 'using TBase::...;' in the derived classes
+       */
+	   void superentityIds()      {}
+	   void superentityIdsArray() {}
+};
+
+
+#endif	/* TNLSUPERENTITYACCESS_H */
+
diff --git a/src/mesh/layers/tnlMeshSuperentityStorageLayer.h b/src/mesh/layers/tnlMeshSuperentityStorageLayer.h
index 7a9046a4af..d8a297a277 100644
--- a/src/mesh/layers/tnlMeshSuperentityStorageLayer.h
+++ b/src/mesh/layers/tnlMeshSuperentityStorageLayer.h
@@ -22,6 +22,7 @@
 #include <mesh/tnlDimensionsTag.h>
 #include <mesh/traits/tnlStorageTraits.h>
 #include <mesh/traits/tnlMeshTraits.h>
+#include <mesh/traits/tnlMeshConfigTraits.h>
 #include <mesh/traits/tnlMeshSuperentitiesTraits.h>
 
 template< typename ConfigTag,
@@ -173,11 +174,20 @@ class tnlMeshSuperentityStorageLayer< ConfigTag,
                 superentitiesIndices == layer.superentitiesIndices );
     }
 
-    private:
+    private:              
 
     ContainerType superentitiesIndices;
 
     SharedContainerType sharedSuperentitiesIndices;
+    
+   // TODO: this is only for the mesh initializer - fix it
+   public:
+              
+      using BaseType::superentityIdsArray;               
+      typename tnlMeshConfigTraits< ConfigTag >::GlobalIdArrayType& superentityIdsArray( DimensionsTag )
+      {
+         return this->superentitiesIndices;
+      }
 };
 
 template< typename ConfigTag,
@@ -251,7 +261,13 @@ class tnlMeshSuperentityStorageLayer< ConfigTag,
    {
       return true;
    }
-
+   
+   template< typename SuperDimensionsTag >
+   typename tnlMeshConfigTraits< ConfigTag >::GlobalIdArrayType& superentityIdsArray( DimensionsTag )
+   {
+      tnlAssert( false, );
+      //return this->superentitiesIndices;
+   }
 };
 
 template< typename ConfigTag,
@@ -309,6 +325,14 @@ class tnlMeshSuperentityStorageLayer< ConfigTag,
    {
       return true;
    }
+   
+   template< typename SuperDimensionsTag >
+   typename tnlMeshConfigTraits< ConfigTag >::GlobalIdArrayType& superentityIdsArray( DimensionsTag )
+   {
+      tnlAssert( false, );
+      //return this->superentitiesIndices;
+   }
+
 };
 
 #endif /* TNLMESHSUPERENTITYSTORAGELAYER_H_ */
diff --git a/src/mesh/tnlMesh.h b/src/mesh/tnlMesh.h
index 449fc8f24a..ab2c12817f 100644
--- a/src/mesh/tnlMesh.h
+++ b/src/mesh/tnlMesh.h
@@ -27,8 +27,6 @@ template< typename MeshConfig >
 class tnlMesh : public tnlObject,
                 public tnlMeshStorageLayers< MeshConfig >
 {
-   typedef tnlMeshStorageLayers< MeshConfig >                BaseType;
-
    public:
    typedef MeshConfig                                        Config;
    typedef typename tnlMeshTraits< MeshConfig >::PointType   PointType;
@@ -55,7 +53,7 @@ class tnlMesh : public tnlObject,
    bool save( tnlFile& file ) const
    {
       if( ! tnlObject::save( file ) ||
-          ! BaseType::save( file ) )
+          ! entitiesStorage.save( file ) )
       {
          cerr << "Mesh saving failed." << endl;
          return false;
@@ -66,7 +64,7 @@ class tnlMesh : public tnlObject,
    bool load( tnlFile& file )
    {
       if( ! tnlObject::load( file ) ||
-          ! BaseType::load( file ) )
+          ! entitiesStorage.load( file ) )
       {
          cerr << "Mesh loading failed." << endl;
          return false;
@@ -88,10 +86,10 @@ class tnlMesh : public tnlObject,
    };
    typedef EntitiesTraits< dimensions > CellTraits;
 
-   using BaseType::setNumberOfVertices;
+   /*using BaseType::setNumberOfVertices;
    using BaseType::getNumberOfVertices;
    using BaseType::setVertex;
-   using BaseType::getVertex;
+   using BaseType::getVertex;*/
 
    template< int Dimensions >
    bool entitiesAvalable() const
@@ -102,91 +100,107 @@ class tnlMesh : public tnlObject,
    template< int Dimensions >
    bool setNumberOfEntities( typename EntitiesTraits< Dimensions >::GlobalIndexType size )
    {
-      return BaseType::setNumberOfEntities( tnlDimensionsTag< Dimensions >(), size );
+      return entitiesStorage.setNumberOfEntities( tnlDimensionsTag< Dimensions >(), size );
    }
 
    template< int Dimensions >
    typename EntitiesTraits< Dimensions >::GlobalIndexType getNumberOfEntities() const
    {
-      return BaseType::getNumberOfEntities( tnlDimensionsTag< Dimensions >() );
+      return entitiesStorage.getNumberOfEntities( tnlDimensionsTag< Dimensions >() );
    }
 
    bool setNumberOfCells( typename EntitiesTraits< dimensions >::GlobalIndexType size )
    {
-      return BaseType::setNumberOfEntities( tnlDimensionsTag< dimensions >(), size );
+      return entitiesStorage.setNumberOfEntities( tnlDimensionsTag< dimensions >(), size );
    }
 
    typename EntitiesTraits< dimensions >::GlobalIndexType getNumberOfCells() const
    {
-      return BaseType::getNumberOfEntities( tnlDimensionsTag< dimensions >() );
+      return entitiesStorage.getNumberOfEntities( tnlDimensionsTag< dimensions >() );
    }
 
    template< int Dimensions >
       typename EntitiesTraits< Dimensions >::EntityType&
          getEntity( const typename EntitiesTraits< Dimensions >::GlobalIndexType entityIndex )
    {
-      return BaseType::getEntity( tnlDimensionsTag< Dimensions >(), entityIndex );
+      return entitiesStorage.getEntity( tnlDimensionsTag< Dimensions >(), entityIndex );
    }
 
    template< int Dimensions >
       const typename EntitiesTraits< Dimensions >::EntityType&
          getEntity( const typename EntitiesTraits< Dimensions >::GlobalIndexType entityIndex ) const
    {
-      return BaseType::getEntity( tnlDimensionsTag< Dimensions >(), entityIndex );
+      return entitiesStorage.getEntity( tnlDimensionsTag< Dimensions >(), entityIndex );
    }
 
    template< int Dimensions >
       void setEntity( const typename EntitiesTraits< Dimensions >::GlobalIndexType entityIndex,
                       const typename EntitiesTraits< Dimensions >::EntityType& entity )
    {
-      BaseType::setEntity( tnlDimensionsTag< Dimensions >(), entityIndex, entity );
+      entitiesStorage.setEntity( tnlDimensionsTag< Dimensions >(), entityIndex, entity );
    }
 
    template< int Dimensions >
    typename EntitiesTraits< Dimensions >::SharedContainerType& getEntities()
    {
-      return BaseType::getEntities( tnlDimensionsTag< Dimensions >() );
+      return entitiesStorage.getEntities( tnlDimensionsTag< Dimensions >() );
    }
 
    template< int Dimensions >
    const typename EntitiesTraits< Dimensions >::SharedContainerType& getEntities() const
    {
-      return BaseType::getEntities( tnlDimensionsTag< Dimensions >() );
+      return entitiesStorage.getEntities( tnlDimensionsTag< Dimensions >() );
    }
 
    typename EntitiesTraits< dimensions >::EntityType&
       getCell( const typename EntitiesTraits< dimensions >::GlobalIndexType entityIndex )
    {
-      return BaseType::getEntity( tnlDimensionsTag< dimensions >(), entityIndex );
+      return entitiesStorage.getEntity( tnlDimensionsTag< dimensions >(), entityIndex );
    }
 
    const typename EntitiesTraits< dimensions >::EntityType&
       getCell( const typename EntitiesTraits< dimensions >::GlobalIndexType entityIndex ) const
    {
-      return BaseType::getEntity( tnlDimensionsTag< dimensions >(), entityIndex );
+      return entitiesStorage.getEntity( tnlDimensionsTag< dimensions >(), entityIndex );
    }
 
    void setCell( const typename EntitiesTraits< dimensions >::GlobalIndexType entityIndex,
                  const typename EntitiesTraits< dimensions >::EntityType& entity )
    {
-      BaseType::setEntity( tnlDimensionsTag< dimensions >(), entityIndex, entity );
+      entitiesStorage.setEntity( tnlDimensionsTag< dimensions >(), entityIndex, entity );
    }
 
    void print( ostream& str ) const
    {
-      BaseType::print( str );
+      entitiesStorage.print( str );
    }
 
    bool operator==( const tnlMesh& mesh ) const
    {
-      return BaseType::operator==( mesh );
+      return entitiesStorage.operator==( mesh.entitiesStorage );
    }
 
-   private:
+   // TODO: this is only for mesh intializer - remove it if possible
+   template< typename DimensionsTag >
+	typename tnlMeshConfigTraits< MeshConfig >::template EntityTraits< DimensionsTag >::ContainerType& entitiesArray()
+   {
+      return entitiesStorage.entitiesArray( DimensionsTag() ); 
+   }
+  
+   template< typename DimensionsTag, typename SuperDimensionsTag >
+	typename tnlMeshConfigTraits< MeshConfig >::GlobalIdArrayType& superentityIdsArray()
+   {
+      return entitiesStorage.template superentityIdsArray< SuperDimensionsTag >( DimensionsTag() ); 
+   }
+   
+   protected:
+      
+      void init();
+      
+      tnlMeshStorageLayers< MeshConfig > entitiesStorage;
 
-   void init();
 
-   tnlMeshConfigValidator< MeshConfig > configValidator;
+      tnlMeshConfigValidator< MeshConfig > configValidator;
 };
 
 
diff --git a/src/mesh/tnlMeshEntity.h b/src/mesh/tnlMeshEntity.h
index 6a42ce9c6b..4ee0090aa2 100644
--- a/src/mesh/tnlMeshEntity.h
+++ b/src/mesh/tnlMeshEntity.h
@@ -26,12 +26,13 @@
 #include <mesh/topologies/tnlMeshVertexTag.h>
 #include <mesh/layers/tnlMeshSubentityStorageLayer.h>
 #include <mesh/layers/tnlMeshSuperentityStorageLayer.h>
+#include <mesh/layers/tnlMeshSuperentityAccess.h>
 
 template< typename ConfigTag,
           typename EntityTag >
 class tnlMeshEntity
-   : public tnlMeshSubentityStorageLayers< ConfigTag, EntityTag >,
-     public tnlMeshSuperentityStorageLayers< ConfigTag, EntityTag >,
+   : public tnlMeshSubentityStorageLayers< ConfigTag, EntityTag >,     
+     public tnlMeshSuperentityAccess< ConfigTag, EntityTag >,
      public tnlMeshEntityId< typename ConfigTag::IdType,
                              typename ConfigTag::GlobalIndexType >
 {
@@ -61,16 +62,16 @@ class tnlMeshEntity
 
    bool save( tnlFile& file ) const
    {
-      if( ! tnlMeshSubentityStorageLayers< ConfigTag, EntityTag >::save( file ) ||
-          ! tnlMeshSuperentityStorageLayers< ConfigTag, EntityTag >::save( file ) )
+      if( ! tnlMeshSubentityStorageLayers< ConfigTag, EntityTag >::save( file ) /*||
+          ! tnlMeshSuperentityStorageLayers< ConfigTag, EntityTag >::save( file )*/ )
          return false;
       return true;
    }
 
    bool load( tnlFile& file )
    {
-      if( ! tnlMeshSubentityStorageLayers< ConfigTag, EntityTag >::load( file ) ||
-          ! tnlMeshSuperentityStorageLayers< ConfigTag, EntityTag >::load( file ) )
+      if( ! tnlMeshSubentityStorageLayers< ConfigTag, EntityTag >::load( file ) /*||
+          ! tnlMeshSuperentityStorageLayers< ConfigTag, EntityTag >::load( file ) */ )
          return false;
       return true;
    }
@@ -79,13 +80,13 @@ class tnlMeshEntity
    {
       str << "\t Mesh entity dimensions: " << EntityTag::dimensions << endl;
       tnlMeshSubentityStorageLayers< ConfigTag, EntityTag >::print( str );
-      tnlMeshSuperentityStorageLayers< ConfigTag, EntityTag >::print( str );
+      tnlMeshSuperentityAccess< ConfigTag, EntityTag >::print( str );
    }
 
    bool operator==( const tnlMeshEntity& entity ) const
    {
       return ( tnlMeshSubentityStorageLayers< ConfigTag, EntityTag >::operator==( entity ) &&
-               tnlMeshSuperentityStorageLayers< ConfigTag, EntityTag >::operator==( entity ) &&
+               tnlMeshSuperentityAccess< ConfigTag, EntityTag >::operator==( entity ) &&
                tnlMeshEntityId< typename ConfigTag::IdType,
                                 typename ConfigTag::GlobalIndexType >::operator==( entity ) );
    }
@@ -202,13 +203,10 @@ class tnlMeshEntity
       typedef int                                               LocalIndexType;
       // TODO: make this as:
       // typedef typename Type::IndexType   LocalIndexType
-      /*enum { available = tnlMeshSuperentityStorage< ConfigTag,
-                                                    EntityTag,
-                                                    Dimensions >::enabled };*/
       static const bool available = ConfigTag::template superentityStorage( EntityTag(), Dimensions );
    };
 
-   template< int Dimensions >
+   /*template< int Dimensions >
    bool setNumberOfSuperentities( const typename SuperentitiesTraits< Dimensions >::LocalIndexType size )
    {
       static_assert( SuperentitiesTraits< Dimensions >::available, "You try to set number of superentities which are not configured for storage." );
@@ -217,13 +215,13 @@ class tnlMeshEntity
       typedef tnlMeshSuperentityStorageLayers< ConfigTag, EntityTag >  SuperentityBaseType;
       return SuperentityBaseType::setNumberOfSuperentities( tnlDimensionsTag< Dimensions >(),
                                                             size );
-   }
+   }*/
 
    template< int Dimensions >
    typename SuperentitiesTraits< Dimensions >::LocalIndexType getNumberOfSuperentities() const
    {
       static_assert( SuperentitiesTraits< Dimensions >::available, "You try to get number of superentities which are not configured for storage." );
-      typedef tnlMeshSuperentityStorageLayers< ConfigTag, EntityTag >  SuperentityBaseType;
+      typedef tnlMeshSuperentityAccess< ConfigTag, EntityTag >  SuperentityBaseType;
       return SuperentityBaseType::getNumberOfSuperentities( tnlDimensionsTag< Dimensions >() );
    }
 
@@ -235,7 +233,7 @@ class tnlMeshEntity
       tnlAssert( localIndex < this->getNumberOfSuperentities< Dimensions >(),
                  cerr << " localIndex = " << localIndex
                       << " this->getNumberOfSuperentities< Dimensions >() = " << this->getNumberOfSuperentities< Dimensions >() << endl; );
-      typedef tnlMeshSuperentityStorageLayers< ConfigTag, EntityTag >  SuperentityBaseType;
+      typedef tnlMeshSuperentityAccess< ConfigTag, EntityTag >  SuperentityBaseType;
       SuperentityBaseType::setSuperentityIndex( tnlDimensionsTag< Dimensions >(),
                                                 localIndex,
                                                 globalIndex );
@@ -249,7 +247,7 @@ class tnlMeshEntity
       tnlAssert( localIndex < this->getNumberOfSuperentities< Dimensions >(),
                  cerr << " localIndex = " << localIndex
                       << " this->getNumberOfSuperentities< Dimensions >() = " << this->getNumberOfSuperentities< Dimensions >() << endl; );
-      typedef tnlMeshSuperentityStorageLayers< ConfigTag, EntityTag >  SuperentityBaseType;
+      typedef tnlMeshSuperentityAccess< ConfigTag, EntityTag >  SuperentityBaseType;
       return SuperentityBaseType::getSuperentityIndex( tnlDimensionsTag< Dimensions >(),
                                                        localIndex );
    }
@@ -258,15 +256,15 @@ class tnlMeshEntity
       typename SuperentitiesTraits< Dimensions >::SharedContainerType& getSuperentitiesIndices()
    {
       static_assert( SuperentitiesTraits< Dimensions >::available, "You try to get superentities which are not configured for storage." );
-      typedef tnlMeshSuperentityStorageLayers< ConfigTag, EntityTag >  SuperentityBaseType;
-      return SuperentityBaseType::getSuperentitiesIndices( tnlDimensionsTag< Dimensions >() );
+      typedef tnlMeshSuperentityAccess< ConfigTag, EntityTag >  SuperentityBaseType;
+      //return SuperentityBaseType::getSuperentitiesIndices( tnlDimensionsTag< Dimensions >() );
    }
 
    template< int Dimensions >
       const typename SuperentitiesTraits< Dimensions >::SharedContainerType& getSuperentitiesIndices() const
    {
       static_assert( SuperentitiesTraits< Dimensions >::available, "You try to get superentities which are not configured for storage." );
-      typedef tnlMeshSuperentityStorageLayers< ConfigTag, EntityTag >  SuperentityBaseType;
+      typedef tnlMeshSuperentityAccess< ConfigTag, EntityTag >  SuperentityBaseType;
       return SuperentityBaseType::getSubentitiesIndices( tnlDimensionsTag< Dimensions >() );
    }
 
@@ -304,11 +302,22 @@ class tnlMeshEntity
    {
       return this->getSubentitiesIndices< 0 >();
    }
+   
+   
+   // TODO: This is only for the mesh initializer, fix this
+   typedef tnlMeshSuperentityAccess< ConfigTag, EntityTag >                     SuperentityAccessBase;
+   typedef typename tnlMeshConfigTraits< ConfigTag>::IdArrayAccessorType        IdArrayAccessorType;
+   
+   template<typename DimensionsTag >
+	IdArrayAccessorType& superentityIdsArray()
+	{
+		return SuperentityAccessBase::superentityIdsArray( DimensionsTag());
+	}
 };
 
 template< typename ConfigTag >
 class tnlMeshEntity< ConfigTag, tnlMeshVertexTag >
-   : public tnlMeshSuperentityStorageLayers< ConfigTag, tnlMeshVertexTag >,
+   : public tnlMeshSuperentityAccess< ConfigTag, tnlMeshVertexTag >,
      public tnlMeshEntityId< typename ConfigTag::IdType,
                              typename ConfigTag::GlobalIndexType >
 {
@@ -348,7 +357,7 @@ class tnlMeshEntity< ConfigTag, tnlMeshVertexTag >
 
    bool save( tnlFile& file ) const
    {
-      if( ! tnlMeshSuperentityStorageLayers< ConfigTag, tnlMeshVertexTag >::save( file ) ||
+      if( //! tnlMeshSuperentityStorageLayers< ConfigTag, tnlMeshVertexTag >::save( file ) ||
           ! point.save( file ) )
          return false;
       return true;
@@ -356,7 +365,7 @@ class tnlMeshEntity< ConfigTag, tnlMeshVertexTag >
 
    bool load( tnlFile& file )
    {
-      if( ! tnlMeshSuperentityStorageLayers< ConfigTag, tnlMeshVertexTag >::load( file ) ||
+      if( //! tnlMeshSuperentityStorageLayers< ConfigTag, tnlMeshVertexTag >::load( file ) ||
           ! point.load( file ) )
          return false;
       return true;
@@ -366,12 +375,12 @@ class tnlMeshEntity< ConfigTag, tnlMeshVertexTag >
    {
       str << "\t Mesh entity dimensions: " << tnlMeshVertexTag::dimensions << endl;
       str << "\t Coordinates = ( " << point << " )";
-      tnlMeshSuperentityStorageLayers< ConfigTag, tnlMeshVertexTag >::print( str );
+      tnlMeshSuperentityAccess< ConfigTag, tnlMeshVertexTag >::print( str );
    }
 
    bool operator==( const tnlMeshEntity& entity ) const
    {
-      return ( tnlMeshSuperentityStorageLayers< ConfigTag, tnlMeshVertexTag >::operator==( entity ) &&
+      return ( tnlMeshSuperentityAccess< ConfigTag, tnlMeshVertexTag >::operator==( entity ) &&
                tnlMeshEntityId< typename ConfigTag::IdType,
                                 typename ConfigTag::GlobalIndexType >::operator==( entity ) &&
                point == entity.point );
@@ -399,7 +408,8 @@ class tnlMeshEntity< ConfigTag, tnlMeshVertexTag >
                                                     Dimensions >::enabled };*/
       static const bool available = ConfigTag::template superentityStorage< tnlMeshVertexTag >( Dimensions );
    };
-   template< int Dimensions >
+   
+   /*template< int Dimensions >
    bool setNumberOfSuperentities( const typename SuperentitiesTraits< Dimensions >::LocalIndexType size )
    {
       tnlAssert( size >= 0,
@@ -407,41 +417,41 @@ class tnlMeshEntity< ConfigTag, tnlMeshVertexTag >
       typedef tnlMeshSuperentityStorageLayers< ConfigTag, tnlMeshVertexTag >  SuperentityBaseType;
       return SuperentityBaseType::setNumberOfSuperentities( tnlDimensionsTag< Dimensions >(),
                                                             size );
-   }
+   }*/
 
    template< int Dimensions >
    typename SuperentitiesTraits< Dimensions >::LocalIndexType getNumberOfSuperentities() const
    {
-      typedef tnlMeshSuperentityStorageLayers< ConfigTag, tnlMeshVertexTag >  SuperentityBaseType;
-      return SuperentityBaseType::getNumberOfSuperentities( tnlDimensionsTag< Dimensions >() );
+      typedef tnlMeshSuperentityAccess< ConfigTag, tnlMeshVertexTag >  SuperentityBaseType;
+      //return SuperentityBaseType::getNumberOfSuperentities( tnlDimensionsTag< Dimensions >() );
    }
 
    template< int Dimensions >
       typename SuperentitiesTraits< Dimensions >::SharedContainerType& getSuperentitiesIndices()
    {
-      typedef tnlMeshSuperentityStorageLayers< ConfigTag, tnlMeshVertexTag >  SuperentityBaseType;
-      return SuperentityBaseType::getSuperentitiesIndices( tnlDimensionsTag< Dimensions >() );
+      typedef tnlMeshSuperentityAccess< ConfigTag, tnlMeshVertexTag >  SuperentityBaseType;
+      //return SuperentityBaseType::getSuperentitiesIndices( tnlDimensionsTag< Dimensions >() );
    }
 
    template< int Dimensions >
       const typename SuperentitiesTraits< Dimensions >::SharedContainerType& getSuperentitiesIndeces() const
    {
-      typedef tnlMeshSuperentityStorageLayers< ConfigTag, tnlMeshVertexTag >  SuperentityBaseType;
-      return SuperentityBaseType::getSubentitiesIndices( tnlDimensionsTag< Dimensions >() );
+      typedef tnlMeshSuperentityAccess< ConfigTag, tnlMeshVertexTag >  SuperentityBaseType;
+      //return SuperentityBaseType::getSubentitiesIndices( tnlDimensionsTag< Dimensions >() );
    }
 
-   template< int Dimensions >
+   /*template< int Dimensions >
    void setSuperentityIndex( const typename SuperentitiesTraits< Dimensions >::LocalIndexType localIndex,
                              const typename SuperentitiesTraits< Dimensions >::GlobalIndexType globalIndex )
    {
       tnlAssert( localIndex < this->getNumberOfSuperentities< Dimensions >(),
                  cerr << " localIndex = " << localIndex
                       << " this->getNumberOfSuperentities< Dimensions >() = " << this->getNumberOfSuperentities< Dimensions >() << endl; );
-      typedef tnlMeshSuperentityStorageLayers< ConfigTag, tnlMeshVertexTag >  SuperentityBaseType;
+      typedef tnlMeshSuperentityAccess< ConfigTag, tnlMeshVertexTag >  SuperentityBaseType;
       SuperentityBaseType::setSuperentityIndex( tnlDimensionsTag< Dimensions >(),
                                                 localIndex,
                                                 globalIndex );
-   }
+   }*/
 
    template< int Dimensions >
    typename SuperentitiesTraits< Dimensions >::GlobalIndexType
@@ -450,9 +460,9 @@ class tnlMeshEntity< ConfigTag, tnlMeshVertexTag >
       tnlAssert( localIndex < this->getNumberOfSuperentities< Dimensions >(),
                  cerr << " localIndex = " << localIndex
                       << " this->getNumberOfSuperentities< Dimensions >() = " << this->getNumberOfSuperentities< Dimensions >() << endl; );
-      typedef tnlMeshSuperentityStorageLayers< ConfigTag, tnlMeshVertexTag >  SuperentityBaseType;
-      return SuperentityBaseType::getSuperentityIndex( tnlDimensionsTag< Dimensions >(),
-                                                       localIndex );
+      typedef tnlMeshSuperentityAccess< ConfigTag, tnlMeshVertexTag >  SuperentityBaseType;
+      /*return SuperentityBaseType::getSuperentityIndex( tnlDimensionsTag< Dimensions >(),
+                                                       localIndex );*/
    }
 
    /****
@@ -465,6 +475,19 @@ class tnlMeshEntity< ConfigTag, tnlMeshVertexTag >
    protected:
 
    PointType point;
+   
+   
+   // TODO: This is only for the mesh initializer, fix this
+   public:
+   typedef typename tnlMeshConfigTraits< ConfigTag>::IdArrayAccessorType        IdArrayAccessorType;
+   typedef tnlMeshSuperentityAccess< ConfigTag, tnlMeshVertexTag > SuperentityAccessBase;
+   
+   template<typename DimensionsTag >
+	IdArrayAccessorType& superentityIdsArray()
+	{
+		return SuperentityAccessBase::superentityIdsArray( DimensionsTag());
+	}
+
 };
 
 template< typename ConfigTag,
@@ -486,5 +509,4 @@ struct tnlDynamicTypeTag< tnlMeshEntity< ConfigTag, EntityTag > >
    enum { value = true };
 };
 
-
 #endif /* TNLMESHENTITY_H_ */
diff --git a/src/mesh/tnlMeshEntityInitializer.h b/src/mesh/tnlMeshEntityInitializer.h
index 4e3daa5f96..eb47aea620 100644
--- a/src/mesh/tnlMeshEntityInitializer.h
+++ b/src/mesh/tnlMeshEntityInitializer.h
@@ -42,10 +42,10 @@ template< typename ConfigTag,
 class tnlMeshEntityInitializer
    : public tnlMeshEntityInitializerLayer< ConfigTag,
                                            EntityTag, 
-                                           tnlDimensionsTag< EntityTag::dimensions - 1 > >,
+                                           tnlDimensionsTag< EntityTag::dimensions - 1 > >/*,
      public tnlMeshSuperentityInitializerLayer< ConfigTag,
                                                 EntityTag,
-                                                typename tnlMeshTraits< ConfigTag >::DimensionsTag >
+                                                typename tnlMeshTraits< ConfigTag >::DimensionsTag >*/
 {
    typedef tnlDimensionsTag< EntityTag::dimensions >                                 DimensionsTag;
    private:
@@ -118,11 +118,11 @@ class tnlMeshEntityInitializer
       return this->entity->template getSubentitiesIndices< SubentityDimensionTag::value >();
    }
    
-   template< typename SuperentityDimensionTag >
+   /*template< typename SuperentityDimensionTag >
    bool setNumberOfSuperentities( SuperentityDimensionTag, typename tnlMeshSuperentitiesTraits< ConfigTag, EntityTag, SuperentityDimensionTag >::ContainerType::IndexType size )
    {
       return this->entity->template setNumberOfSuperentities< SuperentityDimensionTag::value >( size );
-   }
+   }*/
 
    // TODO: check if we need the following two methods
    /*template< typename SuperentityDimensionTag >
@@ -157,7 +157,7 @@ class tnlMeshEntityInitializer
    void initSuperentities()
    {
       //cout << "   Initiating superentities..." << endl;
-      SuperentityBaseType::initSuperentities( *this) ;
+      //SuperentityBaseType::initSuperentities( *this) ;
    }
 
    template< typename SubentityDimensionTag >
@@ -224,10 +224,10 @@ class tnlMeshEntityInitializer
 };
 
 template< typename ConfigTag >
-class tnlMeshEntityInitializer< ConfigTag, tnlMeshVertexTag >
+class tnlMeshEntityInitializer< ConfigTag, tnlMeshVertexTag >/*
    : public tnlMeshSuperentityInitializerLayer< ConfigTag,
                                                 tnlMeshVertexTag,
-                                                typename tnlMeshTraits< ConfigTag >::DimensionsTag >
+                                                typename tnlMeshTraits< ConfigTag >::DimensionsTag >*/
 {
    typedef tnlDimensionsTag< 0 >                                                                     DimensionsTag;
 
@@ -283,7 +283,7 @@ class tnlMeshEntityInitializer< ConfigTag, tnlMeshVertexTag >
    void initSuperentities()
    {
       //cout << "    Initiating superentities of vertex ..." << endl;
-      SuperentityBaseType::initSuperentities(*this);
+      //SuperentityBaseType::initSuperentities(*this);
    }
 };
 
@@ -333,7 +333,7 @@ class tnlMeshEntityInitializerLayer< ConfigTag,
          GlobalIndexType superentityIndex = entityInitializer.getEntityIndex();
          subentityContainer[ i ] = subentityIndex;
          //cout << "       Setting " << i << "-th subentity to " << subentityContainer[ i ] << endl;
-         meshInitializer.getEntityInitializer( DimensionsTag(), subentityIndex ).addSuperentity( EntityDimensionsTag(), superentityIndex );
+         //meshInitializer.getEntityInitializer( DimensionsTag(), subentityIndex ).addSuperentity( EntityDimensionsTag(), superentityIndex );
       }
 
       BaseType::initSubentities( entityInitializer, meshInitializer );
@@ -482,7 +482,7 @@ class tnlMeshEntityInitializerLayer< ConfigTag,
                    cerr << " subentityContainer = " << subentityContainer << endl; );
          GlobalIndexType superentityIndex = entityInitializer.getEntityIndex();
          tnlAssert( superentityIndex >= 0, );
-         meshInitializer.getEntityInitializer( DimensionsTag(), subentityIndex).addSuperentity( EntityDimensionsTag(), superentityIndex );
+         //meshInitializer.getEntityInitializer( DimensionsTag(), subentityIndex).addSuperentity( EntityDimensionsTag(), superentityIndex );
       }
    }
 };
diff --git a/src/mesh/tnlMeshInitializer.h b/src/mesh/tnlMeshInitializer.h
index 1f91e8458c..2a1c8a2d80 100644
--- a/src/mesh/tnlMeshInitializer.h
+++ b/src/mesh/tnlMeshInitializer.h
@@ -47,7 +47,7 @@ class tnlMeshInitializer
    public:
 
    tnlMeshInitializer()
-   : verbose( false )
+   : verbose( false ), mesh( 0 )
    {}
 
    void setVerbose( bool verbose )
@@ -58,6 +58,7 @@ class tnlMeshInitializer
    bool initMesh( MeshType& mesh )
    {
       //cout << "======= Starting mesh initiation ========" << endl;
+      this->mesh = &mesh;
       this->setMesh( mesh );
       if( ! this->checkCells() )
          return false;
@@ -70,11 +71,34 @@ class tnlMeshInitializer
       return true;
    }
 
+   template< typename SuperDimensionsTag, typename MeshEntity>
+	static typename tnlMeshConfigTraits< ConfigTag >::IdArrayAccessorType& superentityIdsArray( MeshEntity& entity )
+	{
+		return entity.template superentityIdsArray< SuperDimensionsTag >();
+	}
+   
+   template< typename DimensionsTag >
+	typename tnlMeshConfigTraits< ConfigTag >::template EntityTraits< DimensionsTag>::ContainerType& meshEntitiesArray()
+   {
+      return mesh->template entitiesArray< DimensionsTag >();
+   }
+   
+   template< typename DimensionsTag, typename SuperDimensionsTag >
+	typename tnlMeshConfigTraits< ConfigTag >::GlobalIdArrayType& meshSuperentityIdsArray()
+   {
+      return mesh->template superentityIdsArray< DimensionsTag, SuperDimensionsTag >();
+   }
+   
    protected:
 
    bool verbose;
+   
+   MeshType* mesh;
 };
 
+/****
+ * Mesh initializer layer for cells
+ */
 template< typename ConfigTag >
 class tnlMeshInitializerLayer< ConfigTag,
                                typename tnlMeshTraits< ConfigTag >::DimensionsTag,
@@ -155,7 +179,7 @@ class tnlMeshInitializerLayer< ConfigTag,
 
    void initEntities( InitializerType& meshInitializer )
    {
-      //cout << " Initiating entities with " << DimensionsTag::value << " dimensions..." << endl;
+      cout << " Initiating cells..." << endl;
       for( typename CellInitializerContainerType::IndexType i = 0;
            i < cellInitializerContainer.getSize();
            i++ )
@@ -164,14 +188,23 @@ class tnlMeshInitializerLayer< ConfigTag,
          cellInitializerContainer[ i ].initEntity( meshInitializer );
       }
       cellInitializerContainer.reset();
+      //cout << "Initiating superentities ...." << endl;
+      //superentityInitializer.initSuperentities( meshInitializer );
       BaseType::initEntities( meshInitializer );
    }
 
    private:
-   CellInitializerContainerType cellInitializerContainer;
-};
+      typedef  tnlMeshSuperentityInitializerLayer< ConfigTag,
+                                                   typename tnlMeshConfigTraits< ConfigTag >::CellType,
+                                                   typename tnlMeshTraits< ConfigTag >::DimensionsTag >  SuperentityInitializer;
 
+      CellInitializerContainerType cellInitializerContainer;
+      SuperentityInitializer superentityInitializer;
+};
 
+/****
+ * Mesh initializer layer for other mesh entities than cells
+ */
 template< typename ConfigTag,
           typename DimensionsTag >
 class tnlMeshInitializerLayer< ConfigTag,
@@ -245,7 +278,7 @@ class tnlMeshInitializerLayer< ConfigTag,
 
    void initEntities( InitializerType &meshInitializer )
    {
-      //cout << " Initiating entities with " << DimensionsTag::value << " dimensions..." << endl;
+      cout << " Initiating entities with " << DimensionsTag::value << " dimensions..." << endl;
       //cout << " Container with entities with " << DimensionsTag::value << " dimensions has: " << endl << this->uniqueContainer << endl;
       const GlobalIndexType numberOfEntities = uniqueContainer.getSize();
       this->getMesh().template setNumberOfEntities< DimensionsTag::value >( numberOfEntities );
@@ -266,15 +299,24 @@ class tnlMeshInitializerLayer< ConfigTag,
       }
 
       entityInitializerContainer.reset();
-
+      cout << "Initiating superentities..." << endl;
+      superentityInitializer.initSuperentities( meshInitializer );
       BaseType::initEntities( meshInitializer );
    }
 
    private:
-   UniqueContainerType uniqueContainer;
-   EntityInitializerContainerType entityInitializerContainer;
+      typedef  tnlMeshSuperentityInitializerLayer< ConfigTag,
+                                                   EntityTag,
+                                                   typename tnlMeshTraits< ConfigTag >::DimensionsTag >  SuperentityInitializer;
+      
+      SuperentityInitializer superentityInitializer;
+      UniqueContainerType uniqueContainer;
+      EntityInitializerContainerType entityInitializerContainer;
 };
 
+/****
+ * Mesh initializer layer for entities not being stored
+ */
 template< typename ConfigTag,
           typename DimensionsTag >
 class tnlMeshInitializerLayer< ConfigTag,
@@ -284,7 +326,9 @@ class tnlMeshInitializerLayer< ConfigTag,
                                      typename DimensionsTag::Decrement >
 {};
 
-
+/****
+ * Mesh initializer layer for vertices
+ */
 template< typename ConfigTag >
 class tnlMeshInitializerLayer< ConfigTag,
                                tnlDimensionsTag< 0 >,
@@ -350,13 +394,20 @@ class tnlMeshInitializerLayer< ConfigTag,
          vertexInitializer.init( vertexContainer[ i ], i );
          vertexInitializer.initEntity( meshInitializer );
       }
+      superentityInitializer.initSuperentities( meshInitializer );
       vertexInitializerContainer.reset();
    }
 
    private:
-   VertexInitializerContainerType vertexInitializerContainer;
+      typedef  tnlMeshSuperentityInitializerLayer< ConfigTag,
+                                                   typename tnlMeshConfigTraits< ConfigTag >::CellType,
+                                                   typename tnlMeshTraits< ConfigTag >::DimensionsTag >  SuperentityInitializer;
+      
+      SuperentityInitializer superentityInitializer;
 
-   MeshType* mesh;
+      VertexInitializerContainerType vertexInitializerContainer;
+
+      MeshType* mesh;
 };
 
 
diff --git a/src/mesh/tnlMeshSuperentityInitializerLayer.h b/src/mesh/tnlMeshSuperentityInitializerLayer.h
index 0232ac4214..902be58b79 100644
--- a/src/mesh/tnlMeshSuperentityInitializerLayer.h
+++ b/src/mesh/tnlMeshSuperentityInitializerLayer.h
@@ -20,10 +20,10 @@
 
 #include <mesh/traits/tnlStorageTraits.h>
 #include <mesh/tnlDimensionsTag.h>
+#include <algorithm>
 
-template< typename ConfigTag,
-          typename EntityTag >
-class tnlMeshEntityInitializer;
+template< typename ConfigTag >
+class tnlMeshInitializer;
 
 template< typename ConfigTag,
           typename EntityTag,
@@ -46,34 +46,59 @@ class tnlMeshSuperentityInitializerLayer< ConfigTag,
                                                EntityTag,
                                                typename DimensionsTag::Decrement >      BaseType;
 
-   typedef typename tnlMeshSuperentitiesTraits< ConfigTag,
-                                                EntityTag,
-                                                DimensionsTag >::GrowableContainerType GrowableContainerType;
-   typedef typename GrowableContainerType::ElementType                                 GlobalIndexType;
-
-   typedef tnlMeshEntityInitializer< ConfigTag, EntityTag >                            EntityInitializerType;
-
-   public:
-
-   using BaseType::addSuperentity;
-   void addSuperentity( DimensionsTag, GlobalIndexType entityIndex )
-   {
-      superentityContainer.Append( entityIndex );
-   }
-
-   protected:
-   void initSuperentities( EntityInitializerType& entityInitializer )
-   {
-      entityInitializer.setNumberOfSuperentities( DimensionsTag(), 
-                                                  superentityContainer.getSize() );
-      superentityContainer.toArray( entityInitializer.getSuperentityContainer( DimensionsTag()) );
-      superentityContainer.reset();
-
-      BaseType::initSuperentities( entityInitializer );
-   }
+   typedef tnlDimensionsTag< EntityTag::dimensions >                                    EntityDimensions;
+	
+   typedef typename tnlMeshConfigTraits< ConfigTag >::GlobalIdArrayType                 GlobalIdArrayType;
+
+      
+   typedef typename tnlMeshConfigTraits< ConfigTag >::GlobalIndexType                   GlobalIndexType;
+   typedef tnlMeshInitializer< ConfigTag >                                              MeshInitializer;
+
+   public:      
+      using BaseType::addSuperentity;
+	   
+      void addSuperentity( DimensionsTag, GlobalIndexType entityIndex, GlobalIndexType superentityIndex)
+      {
+           indexPairs.push_back( IndexPair{ entityIndex, superentityIndex } );
+      }
+
+      void initSuperentities( MeshInitializer& meshInitializer )
+      {
+         std::sort( indexPairs.begin(),
+                    indexPairs.end(),
+                    []( IndexPair pair0, IndexPair pair1 ){ return ( pair0.entityIndex < pair1.entityIndex ); } );
+
+         GlobalIdArrayType &superentityIdsArray = meshInitializer.template meshSuperentityIdsArray< EntityDimensions, DimensionsTag >();
+         superentityIdsArray.setSize( static_cast< GlobalIndexType >( indexPairs.size() )  );
+         GlobalIndexType currentBegin = 0;
+         GlobalIndexType lastEntityIndex = 0;
+         for( GlobalIndexType i = 0; i < superentityIdsArray.getSize(); i++)
+         {
+            superentityIdsArray[ i ] = indexPairs[i].superentityIndex;
+
+            if( indexPairs[ i ].entityIndex != lastEntityIndex )
+            {
+               meshInitializer.template superentityIdsArray< DimensionsTag >( meshInitializer.template meshEntitiesArray< EntityDimensions >()[ lastEntityIndex ] ).bind( superentityIdsArray, currentBegin, i - currentBegin );
+               currentBegin = i;
+               lastEntityIndex = indexPairs[ i ].entityIndex;
+            }
+         }
+
+         meshInitializer.template superentityIdsArray< DimensionsTag >( meshInitializer.template meshEntitiesArray< EntityDimensions >()[ lastEntityIndex ] ).bind( superentityIdsArray, currentBegin, superentityIdsArray.getSize() - currentBegin );
+         indexPairs.clear();
+
+         BaseType::initSuperentities( meshInitializer );
+      }
 
    private:
-   GrowableContainerType superentityContainer;
+      struct IndexPair
+      {
+         GlobalIndexType entityIndex;
+         GlobalIndexType superentityIndex;
+      };
+
+      std::vector< IndexPair > indexPairs;
+   
 };
 
 template< typename ConfigTag,
@@ -96,11 +121,11 @@ class tnlMeshSuperentityInitializerLayer< ConfigTag,
                                           tnlDimensionsTag< EntityTag::dimensions >,
                                           tnlStorageTraits< true > >
 {
-   typedef tnlMeshEntityInitializer< ConfigTag, EntityTag > EntityInitializerType;
+   typedef tnlMeshInitializer< ConfigTag >                                      MeshInitializerType;
    
-   protected:
+   public:
    void addSuperentity()                           {} // This method is due to 'using BaseType::...;' in the derived classes.
-   void initSuperentities( EntityInitializerType& ) {}
+   void initSuperentities( MeshInitializerType& ) {}
 };
 
 template< typename ConfigTag,
@@ -110,11 +135,11 @@ class tnlMeshSuperentityInitializerLayer< ConfigTag,
                                           tnlDimensionsTag< EntityTag::dimensions >,
                                           tnlStorageTraits< false > >
 {
-   typedef tnlMeshEntityInitializer< ConfigTag, EntityTag > EntityInitializerType;
+   typedef tnlMeshInitializer< ConfigTag >                                      MeshInitializerType;
 
-   protected:
+   public:
    void addSuperentity()                           {} // This method is due to 'using BaseType::...;' in the derived classes.
-   void initSuperentities( EntityInitializerType& ) {}
+   void initSuperentities( MeshInitializerType& ) {}
 };
 
 
diff --git a/src/mesh/traits/tnlMeshConfigTraits.h b/src/mesh/traits/tnlMeshConfigTraits.h
index 7f2525ef1f..6f1fa0eba7 100644
--- a/src/mesh/traits/tnlMeshConfigTraits.h
+++ b/src/mesh/traits/tnlMeshConfigTraits.h
@@ -18,6 +18,7 @@
 #ifndef TNLMESHCONFIGTRAITS_H
 #define	TNLMESHCONFIGTRAITS_H
 
+#include <core/tnlHost.h>
 #include <mesh/traits/tnlMeshEntitiesTraits.h>
 #include <mesh/traits/tnlMeshSubentitiesTraits.h>
 #include <mesh/traits/tnlMeshSuperentitiesTraits.h>
@@ -43,10 +44,10 @@ class tnlMeshConfigTraits
       typedef EntitySeed<TConfig, TCellTopology>                                TCellSeed;*/
 
       // TODO: to asi presunout do implementace, stejne to bude vazane na TNL
-      typedef tnlArrayArray< GlobalIndexType, GlobalIndexType >                 GlobalIdArrayType;
-      typedef tnlSharedArray< GlobalIndexType, LocalIndexType >                 IdArrayAccessorType;
-      typedef tnlSharedArray< LocalIndexType, LocalIndexType >                  IdPermutationArrayAccessorType;
-      typedef tnlArray< Point, GlobalIndexType>                                 PointArrayType;
+      typedef tnlArray< GlobalIndexType, tnlHost, GlobalIndexType >             GlobalIdArrayType;
+      typedef tnlSharedArray< GlobalIndexType, tnlHost, LocalIndexType >        IdArrayAccessorType;
+      typedef tnlSharedArray< LocalIndexType, tnlHost, LocalIndexType >         IdPermutationArrayAccessorType;
+      typedef tnlArray< Point, tnlHost, GlobalIndexType>                        PointArrayType;
       //typedef tnlArray<TCellSeed, TGlobalIndex>                         TCellSeedArray;
 
       template< typename Dimensions > using EntityTraits = 
@@ -55,7 +56,7 @@ class tnlMeshConfigTraits
       template< typename MeshEntity, typename SubDimensions > using SubentityTraits =
          tnlMeshSubentitiesTraits< MeshConfig, MeshEntity, SubDimensions >;
       
-      template< typename MeshEntityy, typename SuperDimensions > using SuperentityTraits =
+      template< typename MeshEntity, typename SuperDimensions > using SuperentityTraits =
          tnlMeshSuperentitiesTraits< MeshConfig, MeshEntity, SuperDimensions >;
 };
 
diff --git a/src/mesh/traits/tnlMeshSuperentitiesTraits.h b/src/mesh/traits/tnlMeshSuperentitiesTraits.h
index c07e9ca2f0..131991d48c 100644
--- a/src/mesh/traits/tnlMeshSuperentitiesTraits.h
+++ b/src/mesh/traits/tnlMeshSuperentitiesTraits.h
@@ -31,16 +31,12 @@ template< typename ConfigTag,
           typename DimensionsTag >
 class tnlMeshSuperentitiesTraits
 {
-   /*enum { storageEnabled = tnlMeshSuperentityStorage< ConfigTag,
-                                                      EntityTag,
-                                                      DimensionsTag::value >::enabled };*/
-   static const bool storageEnabled = ConfigTag::template superentityStorage< EntityTag >( EntityTag(), DimensionsTag::value );
-
    typedef typename ConfigTag::GlobalIndexType                              GlobalIndexType;
    typedef typename ConfigTag::LocalIndexType                               LocalIndexType;
 
    public:
 
+   static const bool storageEnabled = ConfigTag::template superentityStorage< EntityTag >( EntityTag(), DimensionsTag::value );
    typedef tnlMeshEntity< ConfigTag, EntityTag >                            EntityType;
    typedef typename
       tnlMeshEntitiesTraits< ConfigTag,
diff --git a/tests/unit-tests/mesh/tnlMeshEntityTester.h b/tests/unit-tests/mesh/tnlMeshEntityTester.h
index bc0f709b84..95eaa75bc0 100644
--- a/tests/unit-tests/mesh/tnlMeshEntityTester.h
+++ b/tests/unit-tests/mesh/tnlMeshEntityTester.h
@@ -456,7 +456,7 @@ class tnlMeshEntityTester : public CppUnit :: TestCase
        CPPUNIT_ASSERT( triangleEntities[ 1 ].template getSubentityIndex< 1 >( 1 ) == 3 );
        CPPUNIT_ASSERT( triangleEntities[ 1 ].template getSubentityIndex< 1 >( 2 ) == 4 );
          
-       vertexEntities[ 0 ].template setNumberOfSuperentities< 1 >( 2 );
+       /*vertexEntities[ 0 ].template setNumberOfSuperentities< 1 >( 2 );
        vertexEntities[ 0 ].template setSuperentityIndex< 1 >( 0, 2 );
        vertexEntities[ 0 ].template setSuperentityIndex< 1 >( 1, 1 );
 
@@ -467,7 +467,7 @@ class tnlMeshEntityTester : public CppUnit :: TestCase
 
        vertexEntities[ 1 ].template setNumberOfSuperentities< 2 >( 2 );
        vertexEntities[ 1 ].template setSuperentityIndex< 2 >( 0, 0 );
-       vertexEntities[ 1 ].template setSuperentityIndex< 2 >( 1, 1 );
+       vertexEntities[ 1 ].template setSuperentityIndex< 2 >( 1, 1 );*/
 
        CPPUNIT_ASSERT( vertexEntities[ 0 ].template getNumberOfSuperentities< 1 >() == 2 );
        CPPUNIT_ASSERT( vertexEntities[ 0 ].template getSuperentityIndex< 1 >( 0 ) == 2 );
@@ -482,12 +482,12 @@ class tnlMeshEntityTester : public CppUnit :: TestCase
        CPPUNIT_ASSERT( vertexEntities[ 1 ].template getSuperentityIndex< 2 >( 0 ) == 0 );
        CPPUNIT_ASSERT( vertexEntities[ 1 ].template getSuperentityIndex< 2 >( 1 ) == 1 );
 
-       edgeEntities[ 0 ].template setNumberOfSuperentities< 2 >( 2 );
+       /*edgeEntities[ 0 ].template setNumberOfSuperentities< 2 >( 2 );
        edgeEntities[ 0 ].template setSuperentityIndex< 2 >( 0, 0 );
-       edgeEntities[ 0 ].template setSuperentityIndex< 2 >( 1, 1 );
+       edgeEntities[ 0 ].template setSuperentityIndex< 2 >( 1, 1 );*/
 
-       CPPUNIT_ASSERT( edgeEntities[ 0 ].template getNumberOfSuperentities< 2 >() == 2  );
-       CPPUNIT_ASSERT( edgeEntities[ 0 ].template getSuperentityIndex< 2 >( 0 ) == 0 );
+       /*CPPUNIT_ASSERT( edgeEntities[ 0 ].template getNumberOfSuperentities< 2 >() == 2  );
+       CPPUNIT_ASSERT( edgeEntities[ 0 ].template getSuperentityIndex< 2 >( 0 ) == 0 );*/
     };
 
 };
-- 
GitLab