Salome HOME
merge V5_1_4
[modules/smesh.git] / src / SMDS / SMDS_UnstructuredGrid.cxx
index 99bd56fb6611af1fde43220ea2295bb289eac848..35690ac539bf75a6664538c48b4979bb3c2efb8e 100644 (file)
+#define CHRONODEF
+#include "SMDS_UnstructuredGrid.hxx"
+#include "SMDS_Mesh.hxx"
+#include "SMDS_MeshInfo.hxx"
+#include "SMDS_Downward.hxx"
 
+#include "utilities.h"
 
-#include "SMDS_UnstructuredGrid.hxx"
+#include <vtkCellArray.h>
+#include <vtkCellLinks.h>
+#include <vtkIdTypeArray.h>
+#include <vtkUnsignedCharArray.h>
+
+#include <list>
 
 using namespace std;
 
-vtkCellLinks::Link* SMDS_CellLinks::AdjustSize(vtkIdType sz)
+SMDS_UnstructuredGrid* SMDS_UnstructuredGrid::New()
 {
-  vtkIdType i;
-  vtkCellLinks::Link *newArray;
-  vtkIdType newSize = sz;
-  vtkCellLinks::Link linkInit = {0,NULL};
-
-  newArray = new vtkCellLinks::Link[newSize];
-
-  for (i=0; i<sz && i<this->Size; i++)
-    {
-    newArray[i] = this->Array[i];
-    }
+  MESSAGE("SMDS_UnstructuredGrid::New");
+  return new SMDS_UnstructuredGrid();
+}
 
-  for (i=this->Size; i < newSize ; i++)
-    {
-    newArray[i] = linkInit;
-    }
+SMDS_UnstructuredGrid::SMDS_UnstructuredGrid() :
+  vtkUnstructuredGrid()
+{
+  _cellIdToDownId.clear();
+  _downTypes.clear();
+  _downArray.clear();
+  _mesh = 0;
+  _counters = new counters(100);
+}
 
-  this->Size = newSize;
-  delete [] this->Array;
-  this->Array = newArray;
+SMDS_UnstructuredGrid::~SMDS_UnstructuredGrid()
+{
+}
 
-  return this->Array;
+unsigned long SMDS_UnstructuredGrid::GetMTime()
+{
+  unsigned long mtime = vtkUnstructuredGrid::GetMTime();
+  MESSAGE("vtkUnstructuredGrid::GetMTime: " << mtime);
+  return mtime;
 }
 
-SMDS_CellLinks* SMDS_CellLinks::New()
+void SMDS_UnstructuredGrid::Update()
 {
-  return new SMDS_CellLinks();
+  MESSAGE("SMDS_UnstructuredGrid::Update");
+  return vtkUnstructuredGrid::Update();
 }
 
-SMDS_CellLinks::SMDS_CellLinks() : vtkCellLinks()
+void SMDS_UnstructuredGrid::UpdateInformation()
 {
+  MESSAGE("SMDS_UnstructuredGrid::UpdateInformation");
+  return vtkUnstructuredGrid::UpdateInformation();
 }
 
-SMDS_CellLinks::~SMDS_CellLinks()
+void SMDS_UnstructuredGrid::setSMDS_mesh(SMDS_Mesh *mesh)
 {
+  _mesh = mesh;
 }
 
-void SMDS_UnstructuredGrid::BuildLinks()
+void SMDS_UnstructuredGrid::compactGrid(std::vector<int>& idNodesOldToNew, int newNodeSize,
+                                        std::vector<int>& idCellsOldToNew, int newCellSize)
 {
-  // Remove the old links if they are already built
-  if (this->Links)
+  MESSAGE("------------------------- SMDS_UnstructuredGrid::compactGrid " << newNodeSize << " " << newCellSize);CHRONO(1);
+  int startHole = 0;
+  int endHole = 0;
+  int startBloc = 0;
+  int endBloc = 0;
+  int alreadyCopied = 0;
+  int holes = 0;
+
+  typedef enum
+  {
+    lookHoleStart, lookHoleEnd, lookBlocEnd
+  } enumState;
+  enumState compactState = lookHoleStart;
+
+  //   if (this->Links)
+  //   {
+  //           this->Links->UnRegister(this);
+  //           this->Links = 0;
+  //   }
+
+  // --- if newNodeSize, create a new compacted vtkPoints
+
+  vtkPoints *newPoints = 0;
+  if (newNodeSize)
     {
-    this->Links->UnRegister(this);
+      MESSAGE("-------------- compactGrid, newNodeSize " << newNodeSize);
+      newPoints = vtkPoints::New();
+      newPoints->Initialize();
+      newPoints->Allocate(newNodeSize);
+      newPoints->SetNumberOfPoints(newNodeSize);
+      int oldNodeSize = idNodesOldToNew.size();
+
+      for (int i = 0; i < oldNodeSize; i++)
+        {
+          switch (compactState)
+          {
+            case lookHoleStart:
+              if (idNodesOldToNew[i] < 0)
+                {
+                  MESSAGE("-------------- newNodeSize, startHole " << i << " " << oldNodeSize);
+                  startHole = i;
+                  if (!alreadyCopied) // copy the first bloc
+                    {
+                      MESSAGE("--------- copy first nodes before hole " << i << " " << oldNodeSize);
+                      copyNodes(newPoints, idNodesOldToNew, alreadyCopied, 0, startHole);
+                    }
+                  compactState = lookHoleEnd;
+                }
+              break;
+            case lookHoleEnd:
+              if (idNodesOldToNew[i] >= 0)
+                {
+                  MESSAGE("-------------- newNodeSize, endHole " << i << " " << oldNodeSize);
+                  endHole = i;
+                  startBloc = i;
+                  compactState = lookBlocEnd;
+                }
+              break;
+            case lookBlocEnd:
+              if (idNodesOldToNew[i] < 0)
+                endBloc = i; // see nbPoints below
+              else if (i == (oldNodeSize - 1))
+                endBloc = i + 1;
+              if (endBloc)
+                {
+                  MESSAGE("-------------- newNodeSize, endbloc " << endBloc << " " << oldNodeSize);
+                  copyNodes(newPoints, idNodesOldToNew, alreadyCopied, startBloc, endBloc);
+                  compactState = lookHoleStart;
+                  startHole = i;
+                  endHole = 0;
+                  startBloc = 0;
+                  endBloc = 0;
+                }
+              break;
+          }
+        }
+      if (!alreadyCopied) // no hole, but shorter, no need to modify idNodesOldToNew
+        {
+          MESSAGE("------------- newNodeSize, shorter " << oldNodeSize);
+          copyNodes(newPoints, idNodesOldToNew, alreadyCopied, 0, newNodeSize);
+        }
+      newPoints->Squeeze();
     }
 
-  this->Links = SMDS_CellLinks::New();
-  this->Links->Allocate(this->GetNumberOfPoints());
-  this->Links->Register(this);
-  this->Links->BuildLinks(this, this->Connectivity);
-  this->Links->Delete();
+  // --- create new compacted Connectivity, Locations and Types
+
+  int oldCellSize = this->Types->GetNumberOfTuples();
+
+  vtkCellArray *newConnectivity = vtkCellArray::New();
+  newConnectivity->Initialize();
+  int oldCellDataSize = this->Connectivity->GetData()->GetSize();
+  newConnectivity->Allocate(oldCellDataSize);
+  MESSAGE("oldCellSize="<< oldCellSize << " oldCellDataSize=" << oldCellDataSize);
+
+  vtkUnsignedCharArray *newTypes = vtkUnsignedCharArray::New();
+  newTypes->Initialize();
+  //newTypes->Allocate(oldCellSize);
+  newTypes->SetNumberOfValues(newCellSize);
+
+  vtkIdTypeArray *newLocations = vtkIdTypeArray::New();
+  newLocations->Initialize();
+  //newLocations->Allocate(oldCellSize);
+  newLocations->SetNumberOfValues(newCellSize);
+
+  startHole = 0;
+  endHole = 0;
+  startBloc = 0;
+  endBloc = 0;
+  alreadyCopied = 0;
+  holes = 0;
+  compactState = lookHoleStart;
+
+  vtkIdType tmpid[50];
+  vtkIdType *pointsCell = &tmpid[0]; // --- points id to fill a new cell
+
+  for (int i = 0; i < oldCellSize; i++)
+    {
+      switch (compactState)
+      {
+        case lookHoleStart:
+          if (this->Types->GetValue(i) == VTK_EMPTY_CELL)
+            {
+              MESSAGE(" -------- newCellSize, startHole " << i << " " << oldCellSize);
+              startHole = i;
+              compactState = lookHoleEnd;
+              if (!alreadyCopied) // copy the first bloc
+                {
+                  MESSAGE("--------- copy first bloc before hole " << i << " " << oldCellSize);
+                  copyBloc(newTypes, idCellsOldToNew, idNodesOldToNew, newConnectivity, newLocations, pointsCell,
+                           alreadyCopied, 0, startHole);
+                }
+            }
+          break;
+        case lookHoleEnd:
+          if (this->Types->GetValue(i) != VTK_EMPTY_CELL)
+            {
+              MESSAGE(" -------- newCellSize, EndHole " << i << " " << oldCellSize);
+              endHole = i;
+              startBloc = i;
+              compactState = lookBlocEnd;
+              holes += endHole - startHole;
+              //alreadyCopied = startBloc -holes;
+            }
+          break;
+        case lookBlocEnd:
+          endBloc = 0;
+          if (this->Types->GetValue(i) == VTK_EMPTY_CELL)
+            endBloc = i;
+          else if (i == (oldCellSize - 1))
+            endBloc = i + 1;
+          if (endBloc)
+            {
+              MESSAGE(" -------- newCellSize, endBloc " << endBloc << " " << oldCellSize);
+              copyBloc(newTypes, idCellsOldToNew, idNodesOldToNew, newConnectivity, newLocations, pointsCell,
+                       alreadyCopied, startBloc, endBloc);
+              compactState = lookHoleStart;
+            }
+          break;
+      }
+    }
+  if (!alreadyCopied) // no hole, but shorter
+    {
+      MESSAGE(" -------- newCellSize, shorter " << oldCellSize);
+      copyBloc(newTypes, idCellsOldToNew, idNodesOldToNew, newConnectivity, newLocations, pointsCell, alreadyCopied, 0,
+               oldCellSize);
+    }
+
+  newConnectivity->Squeeze();
+  //newTypes->Squeeze();
+  //newLocations->Squeeze();
+
+  if (newNodeSize)
+    {
+      MESSAGE("------- newNodeSize, setPoints");
+      this->SetPoints(newPoints);
+      MESSAGE("NumberOfPoints: " << this->GetNumberOfPoints());
+    }
+  this->SetCells(newTypes, newLocations, newConnectivity);
+  this->BuildLinks();
 }
 
-SMDS_CellLinks* SMDS_UnstructuredGrid::GetCellLinks()
+void SMDS_UnstructuredGrid::copyNodes(vtkPoints *newPoints, std::vector<int>& idNodesOldToNew, int& alreadyCopied,
+                                      int start, int end)
 {
-  return static_cast<SMDS_CellLinks*>(this->Links);
+  MESSAGE("copyNodes " << alreadyCopied << " " << start << " " << end << " size: " << end - start << " total: " << alreadyCopied + end - start);
+  void *target = newPoints->GetVoidPointer(3 * alreadyCopied);
+  void *source = this->Points->GetVoidPointer(3 * start);
+  int nbPoints = end - start;
+  if (nbPoints > 0)
+    {
+      memcpy(target, source, 3 * sizeof(float) * nbPoints);
+      for (int j = start; j < end; j++)
+        idNodesOldToNew[j] = alreadyCopied++;
+    }
 }
 
-SMDS_UnstructuredGrid* SMDS_UnstructuredGrid::New()
+void SMDS_UnstructuredGrid::copyBloc(vtkUnsignedCharArray *newTypes, std::vector<int>& idCellsOldToNew,
+                                     std::vector<int>& idNodesOldToNew, vtkCellArray* newConnectivity,
+                                     vtkIdTypeArray* newLocations, vtkIdType* pointsCell, int& alreadyCopied,
+                                     int start, int end)
 {
-  return new SMDS_UnstructuredGrid();
+  MESSAGE("copyBloc " << alreadyCopied << " " << start << " " << end << " size: " << end - start << " total: " << alreadyCopied + end - start);
+  for (int j = start; j < end; j++)
+    {
+      newTypes->SetValue(alreadyCopied, this->Types->GetValue(j));
+      idCellsOldToNew[j] = alreadyCopied;
+      vtkIdType oldLoc = this->Locations->GetValue(j);
+      vtkIdType nbpts;
+      vtkIdType *oldPtsCell = 0;
+      this->Connectivity->GetCell(oldLoc, nbpts, oldPtsCell);
+      //MESSAGE(j << " " << alreadyCopied << " " << (int)this->Types->GetValue(j) << " " << oldLoc << " " << nbpts );
+      for (int l = 0; l < nbpts; l++)
+        {
+          int oldval = oldPtsCell[l];
+          pointsCell[l] = idNodesOldToNew[oldval];
+          //MESSAGE("   " << oldval << " " << pointsCell[l]);
+        }
+      int newcnt = newConnectivity->InsertNextCell(nbpts, pointsCell);
+      int newLoc = newConnectivity->GetInsertLocation(nbpts);
+      //MESSAGE(newcnt << " " << newLoc);
+      newLocations->SetValue(alreadyCopied, newLoc);
+      alreadyCopied++;
+    }
 }
 
-SMDS_UnstructuredGrid::SMDS_UnstructuredGrid() : vtkUnstructuredGrid()
+int SMDS_UnstructuredGrid::CellIdToDownId(int vtkCellId)
 {
+  // ASSERT((vtkCellId >= 0) && (vtkCellId < _cellIdToDownId.size()));
+  return _cellIdToDownId[vtkCellId];
 }
 
-SMDS_UnstructuredGrid::~SMDS_UnstructuredGrid()
+void SMDS_UnstructuredGrid::setCellIdToDownId(int vtkCellId, int downId)
 {
+  // ASSERT((vtkCellId >= 0) && (vtkCellId < _cellIdToDownId.size()));
+  _cellIdToDownId[vtkCellId] = downId;
 }
 
+/*! Build downward connectivity: to do only when needed because heavy memory load.
+ *  Downward connectivity is no more valid if vtkUnstructuredGrid is modified.
+ *
+ */
+void SMDS_UnstructuredGrid::BuildDownwardConnectivity()
+{
+  MESSAGE("SMDS_UnstructuredGrid::BuildDownwardConnectivity");CHRONO(2);
+
+  // --- erase previous data if any
+
+  for (int i = 0; i < _downArray.size(); i++)
+    {
+      if (_downArray[i])
+        delete _downArray[i];
+      _downArray[i] = 0;
+    }
+  _cellIdToDownId.clear();
+
+  // --- create SMDS_Downward structures (in _downArray vector[vtkCellType])
+
+  _downArray.resize(VTK_QUADRATIC_PYRAMID + 1, 0); // --- max. type value = VTK_QUADRATIC_PYRAMID
+
+  _downArray[VTK_LINE] = new SMDS_DownEdge(this);
+  _downArray[VTK_QUADRATIC_EDGE] = new SMDS_DownQuadEdge(this);
+  _downArray[VTK_TRIANGLE] = new SMDS_DownTriangle(this);
+  _downArray[VTK_QUADRATIC_TRIANGLE] = new SMDS_DownQuadTriangle(this);
+  _downArray[VTK_QUAD] = new SMDS_DownQuadrangle(this);
+  _downArray[VTK_QUADRATIC_QUAD] = new SMDS_DownQuadQuadrangle(this);
+  _downArray[VTK_TETRA] = new SMDS_DownTetra(this);
+  _downArray[VTK_QUADRATIC_TETRA] = new SMDS_DownQuadTetra(this);
+  _downArray[VTK_PYRAMID] = new SMDS_DownPyramid(this);
+  _downArray[VTK_QUADRATIC_PYRAMID] = new SMDS_DownQuadPyramid(this);
+  _downArray[VTK_WEDGE] = new SMDS_DownPenta(this);
+  _downArray[VTK_QUADRATIC_WEDGE] = new SMDS_DownQuadPenta(this);
+  _downArray[VTK_HEXAHEDRON] = new SMDS_DownHexa(this);
+  _downArray[VTK_QUADRATIC_HEXAHEDRON] = new SMDS_DownQuadHexa(this);
 
+  // --- get detailed info of number of cells of each type, allocate SMDS_downward structures
+
+  const SMDS_MeshInfo &meshInfo = _mesh->GetMeshInfo();
+
+  int nbLinTetra = meshInfo.NbTetras(ORDER_LINEAR);
+  int nbQuadTetra = meshInfo.NbTetras(ORDER_QUADRATIC);
+  int nbLinPyra = meshInfo.NbPyramids(ORDER_LINEAR);
+  int nbQuadPyra = meshInfo.NbPyramids(ORDER_QUADRATIC);
+  int nbLinPrism = meshInfo.NbPrisms(ORDER_LINEAR);
+  int nbQuadPrism = meshInfo.NbPrisms(ORDER_QUADRATIC);
+  int nbLinHexa = meshInfo.NbHexas(ORDER_LINEAR);
+  int nbQuadHexa = meshInfo.NbHexas(ORDER_QUADRATIC);
+
+  int nbLineGuess = int((4.0 / 3.0) * nbLinTetra + 2 * nbLinPrism + 2.5 * nbLinPyra + 3 * nbLinHexa);
+  int nbQuadEdgeGuess = int((4.0 / 3.0) * nbQuadTetra + 2 * nbQuadPrism + 2.5 * nbQuadPyra + 3 * nbQuadHexa);
+  int nbLinTriaGuess = 2 * nbLinTetra + nbLinPrism + 2 * nbLinPyra;
+  int nbQuadTriaGuess = 2 * nbQuadTetra + nbQuadPrism + 2 * nbQuadPyra;
+  int nbLinQuadGuess = int((2.0 / 3.0) * nbLinPrism + (1.0 / 2.0) * nbLinPyra + 3 * nbLinHexa);
+  int nbQuadQuadGuess = int((2.0 / 3.0) * nbQuadPrism + (1.0 / 2.0) * nbQuadPyra + 3 * nbQuadHexa);
+
+  int GuessSize[VTK_QUADRATIC_TETRA];
+  GuessSize[VTK_LINE] = nbLineGuess;
+  GuessSize[VTK_QUADRATIC_EDGE] = nbQuadEdgeGuess;
+  GuessSize[VTK_TRIANGLE] = nbLinTriaGuess;
+  GuessSize[VTK_QUADRATIC_TRIANGLE] = nbQuadTriaGuess;
+  GuessSize[VTK_QUAD] = nbLinQuadGuess;
+  GuessSize[VTK_QUADRATIC_QUAD] = nbQuadQuadGuess;
+  GuessSize[VTK_TETRA] = nbLinTetra;
+  GuessSize[VTK_QUADRATIC_TETRA] = nbQuadTetra;
+  GuessSize[VTK_PYRAMID] = nbLinPyra;
+  GuessSize[VTK_QUADRATIC_PYRAMID] = nbQuadPyra;
+  GuessSize[VTK_WEDGE] = nbLinPrism;
+  GuessSize[VTK_QUADRATIC_WEDGE] = nbQuadPrism;
+  GuessSize[VTK_HEXAHEDRON] = nbLinHexa;
+  GuessSize[VTK_QUADRATIC_HEXAHEDRON] = nbQuadHexa;
+
+  _downArray[VTK_LINE]->allocate(nbLineGuess);
+  _downArray[VTK_QUADRATIC_EDGE]->allocate(nbQuadEdgeGuess);
+  _downArray[VTK_TRIANGLE]->allocate(nbLinTriaGuess);
+  _downArray[VTK_QUADRATIC_TRIANGLE]->allocate(nbQuadTriaGuess);
+  _downArray[VTK_QUAD]->allocate(nbLinQuadGuess);
+  _downArray[VTK_QUADRATIC_QUAD]->allocate(nbQuadQuadGuess);
+  _downArray[VTK_TETRA]->allocate(nbLinTetra);
+  _downArray[VTK_QUADRATIC_TETRA]->allocate(nbQuadTetra);
+  _downArray[VTK_PYRAMID]->allocate(nbLinPyra);
+  _downArray[VTK_QUADRATIC_PYRAMID]->allocate(nbQuadPyra);
+  _downArray[VTK_WEDGE]->allocate(nbLinPrism);
+  _downArray[VTK_QUADRATIC_WEDGE]->allocate(nbQuadPrism);
+  _downArray[VTK_HEXAHEDRON]->allocate(nbLinHexa);
+  _downArray[VTK_QUADRATIC_HEXAHEDRON]->allocate(nbQuadHexa);
+
+  // --- iteration on vtkUnstructuredGrid cells, only faces
+  //     for each vtk face:
+  //       create a downward face entry with its downward id.
+  //       compute vtk volumes, create downward volumes entry.
+  //       mark face in downward volumes
+  //       mark volumes in downward face
+
+  MESSAGE("--- iteration on vtkUnstructuredGrid cells, only faces");CHRONO(20);
+  int cellSize = this->Types->GetNumberOfTuples();
+  _cellIdToDownId.resize(cellSize, -1);
+
+  for (int i = 0; i < cellSize; i++)
+    {
+      int vtkFaceType = this->GetCellType(i);
+      if (SMDS_Downward::getCellDimension(vtkFaceType) == 2)
+        {
+          int vtkFaceId = i;
+          //ASSERT(_downArray[vtkFaceType]);
+          int connFaceId = _downArray[vtkFaceType]->addCell(vtkFaceId);
+          SMDS_Down2D* downFace = static_cast<SMDS_Down2D*> (_downArray[vtkFaceType]);
+          downFace->setTempNodes(connFaceId, vtkFaceId);
+          int vols[2] = { -1, -1 };
+          int nbVolumes = downFace->computeVolumeIds(vtkFaceId, vols);
+          //MESSAGE("nbVolumes="<< nbVolumes);
+          for (int ivol = 0; ivol < nbVolumes; ivol++)
+            {
+              int vtkVolId = vols[ivol];
+              int vtkVolType = this->GetCellType(vtkVolId);
+              //ASSERT(_downArray[vtkVolType]);
+              int connVolId = _downArray[vtkVolType]->addCell(vtkVolId);
+              _downArray[vtkVolType]->addDownCell(connVolId, connFaceId, vtkFaceType);
+              _downArray[vtkFaceType]->addUpCell(connFaceId, connVolId, vtkVolType);
+              // MESSAGE("Face " << vtkFaceId << " belongs to volume " << vtkVolId);
+            }
+        }
+    }
+
+  // --- iteration on vtkUnstructuredGrid cells, only volumes
+  //     for each vtk volume:
+  //       create downward volumes entry if not already done
+  //       build a temporary list of faces described with their nodes
+  //       for each face
+  //         compute the vtk volumes containing this face
+  //         check if the face is already listed in the volumes (comparison of ordered list of nodes)
+  //         if not, create a downward face entry (resizing of structure required)
+  //         (the downward faces store a temporary list of nodes to ease the comparison)
+  //         create downward volumes entry if not already done
+  //         mark volumes in downward face
+  //         mark face in downward volumes
+
+  CHRONOSTOP(20);
+  MESSAGE("--- iteration on vtkUnstructuredGrid cells, only volumes");CHRONO(21);
+
+  for (int i = 0; i < cellSize; i++)
+    {
+      int vtkType = this->GetCellType(i);
+      if (SMDS_Downward::getCellDimension(vtkType) == 3)
+        {
+          //CHRONO(31);
+          int vtkVolId = i;
+          // MESSAGE("vtk volume " << vtkVolId);
+          //ASSERT(_downArray[vtkType]);
+          int connVolId = _downArray[vtkType]->addCell(vtkVolId);
+
+          // --- find all the faces of the volume, describe the faces by their nodes
+
+          SMDS_Down3D* downVol = static_cast<SMDS_Down3D*> (_downArray[vtkType]);
+          ListElemByNodesType facesWithNodes;
+          downVol->computeFacesWithNodes(vtkVolId, facesWithNodes);
+          // MESSAGE("vtk volume " << vtkVolId << " contains " << facesWithNodes.nbElems << " faces");
+          //CHRONOSTOP(31);
+          for (int iface = 0; iface < facesWithNodes.nbElems; iface++)
+            {
+              // --- find the volumes containing the face
+
+              //CHRONO(32);
+              int vtkFaceType = facesWithNodes.elems[iface].vtkType;
+              SMDS_Down2D* downFace = static_cast<SMDS_Down2D*> (_downArray[vtkFaceType]);
+              int vols[2] = { -1, -1 };
+              int *nodes = &facesWithNodes.elems[iface].nodeIds[0];
+              int lg = facesWithNodes.elems[iface].nbNodes;
+              int nbVolumes = downFace->computeVolumeIdsFromNodesFace(nodes, lg, vols);
+              // MESSAGE("vtk volume " << vtkVolId << " face " << iface << " belongs to " << nbVolumes << " volumes");
+
+              // --- check if face is registered in the volumes
+              //CHRONOSTOP(32);
+
+              //CHRONO(33);
+              int connFaceId = -1;
+              for (int ivol = 0; ivol < nbVolumes; ivol++)
+                {
+                  int vtkVolId2 = vols[ivol];
+                  int vtkVolType = this->GetCellType(vtkVolId2);
+                  //ASSERT(_downArray[vtkVolType]);
+                  int connVolId2 = _downArray[vtkVolType]->addCell(vtkVolId2);
+                  SMDS_Down3D* downVol2 = static_cast<SMDS_Down3D*> (_downArray[vtkVolType]);
+                  connFaceId = downVol2->FindFaceByNodes(connVolId2, facesWithNodes.elems[iface]);
+                  if (connFaceId >= 0)
+                    break; // --- face already created
+                }//CHRONOSTOP(33);
+
+              // --- if face is not registered in the volumes, create face
+
+              //CHRONO(34);
+              if (connFaceId < 0)
+                {
+                  connFaceId = _downArray[vtkFaceType]->addCell();
+                  downFace->setTempNodes(connFaceId, facesWithNodes.elems[iface]);
+                }//CHRONOSTOP(34);
+
+              // --- mark volumes in downward face and mark face in downward volumes
+
+              //CHRONO(35);
+              for (int ivol = 0; ivol < nbVolumes; ivol++)
+                {
+                  int vtkVolId2 = vols[ivol];
+                  int vtkVolType = this->GetCellType(vtkVolId2);
+                  //ASSERT(_downArray[vtkVolType]);
+                  int connVolId2 = _downArray[vtkVolType]->addCell(vtkVolId2);
+                  _downArray[vtkVolType]->addDownCell(connVolId2, connFaceId, vtkFaceType);
+                  _downArray[vtkFaceType]->addUpCell(connFaceId, connVolId2, vtkVolType);
+                  // MESSAGE(" From volume " << vtkVolId << " face " << connFaceId << " belongs to volume " << vtkVolId2);
+                }//CHRONOSTOP(35);
+            }
+        }
+    }
+
+  // --- iteration on vtkUnstructuredGrid cells, only edges
+  //     for each vtk edge:
+  //       create downward edge entry
+  //       store the nodes id's in downward edge (redundant with vtkUnstructuredGrid)
+  //       find downward faces
+  //       (from vtk faces or volumes, get downward faces, they have a temporary list of nodes)
+  //       mark edge in downward faces
+  //       mark faces in downward edge
+
+  CHRONOSTOP(21);
+  MESSAGE("--- iteration on vtkUnstructuredGrid cells, only edges");CHRONO(22);
+
+  for (int i = 0; i < cellSize; i++)
+    {
+      int vtkEdgeType = this->GetCellType(i);
+      if (SMDS_Downward::getCellDimension(vtkEdgeType) == 1)
+        {
+          int vtkEdgeId = i;
+          //ASSERT(_downArray[vtkEdgeType]);
+          int connEdgeId = _downArray[vtkEdgeType]->addCell(vtkEdgeId);
+          SMDS_Down1D* downEdge = static_cast<SMDS_Down1D*> (_downArray[vtkEdgeType]);
+          downEdge->setNodes(connEdgeId, vtkEdgeId);
+          vector<int> vtkIds;
+          int nbVtkCells = downEdge->computeVtkCells(connEdgeId, vtkIds);
+          int downFaces[1000];
+          unsigned char downTypes[1000];
+          int nbDownFaces = downEdge->computeFaces(connEdgeId, &vtkIds[0], nbVtkCells, downFaces, downTypes);
+          for (int n = 0; n < nbDownFaces; n++)
+            {
+              _downArray[downTypes[n]]->addDownCell(downFaces[n], connEdgeId, vtkEdgeType);
+              _downArray[vtkEdgeType]->addUpCell(connEdgeId, downFaces[n], downTypes[n]);
+            }
+        }
+    }
+
+  // --- iteration on downward faces (they are all listed now)
+  //     for each downward face:
+  //       build a temporary list of edges with their ordered list of nodes
+  //       for each edge:
+  //         find all the vtk cells containing this edge
+  //         then identify all the downward faces containing the edge, from the vtk cells
+  //         check if the edge is already listed in the faces (comparison of ordered list of nodes)
+  //         if not, create a downward edge entry with the node id's
+  //         mark edge in downward faces
+  //         mark downward faces in edge (size of list unknown, to be allocated)
+
+  CHRONOSTOP(22);CHRONO(23);
+
+  for (int vtkFaceType = 0; vtkFaceType < VTK_QUADRATIC_PYRAMID; vtkFaceType++)
+    {
+      if (SMDS_Downward::getCellDimension(vtkFaceType) != 2)
+        continue;
+
+      // --- find all the edges of the face, describe the edges by their nodes
+
+      SMDS_Down2D* downFace = static_cast<SMDS_Down2D*> (_downArray[vtkFaceType]);
+      int maxId = downFace->getMaxId();
+      for (int faceId = 0; faceId < maxId; faceId++)
+        {
+          //CHRONO(40);
+          ListElemByNodesType edgesWithNodes;
+          downFace->computeEdgesWithNodes(faceId, edgesWithNodes);
+          // MESSAGE("downward face type " << vtkFaceType << " num " << faceId << " contains " << edgesWithNodes.nbElems << " edges");
+
+          //CHRONOSTOP(40);
+          for (int iedge = 0; iedge < edgesWithNodes.nbElems; iedge++)
+            {
+
+              // --- check if the edge is already registered by exploration of the faces
+
+              //CHRONO(41);
+              vector<int> vtkIds;
+              unsigned char vtkEdgeType = edgesWithNodes.elems[iedge].vtkType;
+              int *pts = &edgesWithNodes.elems[iedge].nodeIds[0];
+              SMDS_Down1D* downEdge = static_cast<SMDS_Down1D*> (_downArray[vtkEdgeType]);
+              int nbVtkCells = downEdge->computeVtkCells(pts, vtkIds);
+              //CHRONOSTOP(41);CHRONO(42);
+              int downFaces[1000];
+              unsigned char downTypes[1000];
+              int nbDownFaces = downEdge->computeFaces(pts, &vtkIds[0], nbVtkCells, downFaces, downTypes);
+              //CHRONOSTOP(42);
+
+              //CHRONO(43);
+              int connEdgeId = -1;
+              for (int idf = 0; idf < nbDownFaces; idf++)
+                {
+                  int faceId2 = downFaces[idf];
+                  int faceType = downTypes[idf];
+                  //ASSERT(_downArray[faceType]);
+                  SMDS_Down2D* downFace2 = static_cast<SMDS_Down2D*> (_downArray[faceType]);
+                  connEdgeId = downFace2->FindEdgeByNodes(faceId2, edgesWithNodes.elems[iedge]);
+                  if (connEdgeId >= 0)
+                    break; // --- edge already created
+                }//CHRONOSTOP(43);
+
+              // --- if edge is not registered in the faces, create edge
+
+              if (connEdgeId < 0)
+                {
+                  //CHRONO(44);
+                  connEdgeId = _downArray[vtkEdgeType]->addCell();
+                  downEdge->setNodes(connEdgeId, edgesWithNodes.elems[iedge].nodeIds);
+                  //CHRONOSTOP(44);
+                }
+
+              // --- mark faces in downward edge and mark edge in downward faces
+
+              //CHRONO(45);
+              for (int idf = 0; idf < nbDownFaces; idf++)
+                {
+                  int faceId2 = downFaces[idf];
+                  int faceType = downTypes[idf];
+                  //ASSERT(_downArray[faceType]);
+                  SMDS_Down2D* downFace2 = static_cast<SMDS_Down2D*> (_downArray[faceType]);
+                  _downArray[vtkEdgeType]->addUpCell(connEdgeId, faceId2, faceType);
+                  _downArray[faceType]->addDownCell(faceId2, connEdgeId, vtkEdgeType);
+                  // MESSAGE(" From face t:" << vtkFaceType << " " << faceId <<
+                  //  " edge " << connEdgeId << " belongs to face t:" << faceType << " " << faceId2);
+                }//CHRONOSTOP(45);
+            }
+        }
+    }
+
+  CHRONOSTOP(23);CHRONO(24);
+
+  // compact downward connectivity structure: adjust downward arrays size, replace vector<vector int>> by a single vector<int>
+  // 3D first then 2D and last 1D to release memory before edge upCells reorganization, (temporary memory use)
+
+  for (int vtkType = VTK_QUADRATIC_PYRAMID; vtkType >= 0; vtkType--)
+    {
+      if (SMDS_Downward *down = _downArray[vtkType])
+        {
+          down->compactStorage();
+        }
+    }
+
+  // --- Statistics
+
+  for (int vtkType = 0; vtkType <= VTK_QUADRATIC_PYRAMID; vtkType++)
+    {
+      if (SMDS_Downward *down = _downArray[vtkType])
+        {
+          if (down->getMaxId())
+            {
+              MESSAGE("Cells of Type " << vtkType << " : number of entities, est: "
+                  << GuessSize[vtkType] << " real: " << down->getMaxId());
+            }
+        }
+    }CHRONOSTOP(24);CHRONOSTOP(2);
+  _counters->stats();
+}