void particles::pit::myfunctions::RepresentationChange::ascend(
  particles::pit::Cell * const    fineGridCells,
  particles::pit::Vertex * const  fineGridVertices,
  const peano::grid::VertexEnumerator&          fineGridVerticesEnumerator,
  particles::pit::Vertex * const  coarseGridVertices,
  const peano::grid::VertexEnumerator&          coarseGridVerticesEnumerator,
  particles::pit::Cell&           coarseGridCell
) {
  particles::pit::myfunctions::CoordinatesRepresentationChange::ascend(
    fineGridCells,
    fineGridVertices,
    fineGridVerticesEnumerator,
    coarseGridVertices,
    coarseGridVerticesEnumerator,
    coarseGridCell);

  dfor3(k)
    particles::pit::Cell fineGridCell = fineGridCells[ fineGridVerticesEnumerator.cell(k) ];
    const tarch::la::Vector<DIMENSIONS,double> cellOffset     = fineGridVerticesEnumerator.getVertexPosition(k);
    const tarch::la::Vector<DIMENSIONS,double> meanVelocity = fineGridCell.getMeanVelocity();
    bool isLeaf = fineGridCell.isLeaf();
    const int cellIndex = fineGridCell.getCellIndex();
    const int NumberOfParticles = ParticleHeap::getInstance().getData(cellIndex).size();

    if( isLeaf && NumberOfParticles>1 ) {
      // Compute Max-Norm
      double maxRelativeError = computeMaxRelativeError( fineGridCell );
      // Save maximal maxRelativeError in _globalMaxRelativeError
      if (_globalMaxRelativeError < maxRelativeError) {
        _globalMaxRelativeError = maxRelativeError;
      }
      double maxError = computeMaxError( fineGridCell );
      if(_global_max_error < maxError) {
          _global_max_error = maxError;
      }
      double maxOffset = computeMaxOffset( fineGridCell );
      double minOffset = computeMinOffset( fineGridCell );
      if(_globalMaxOffset < maxOffset) {
        _globalMaxOffset = maxOffset;
      }
      // Compute RMSD
      tarch::la::Vector<DIMENSIONS,double> rmsd = computeRMSD( fineGridCell );
      // Computer L2-Norm
      tarch::la::Vector<DIMENSIONS,double>
        l2ErrorNorm = computeL2ErrorNorm( fineGridCell );
      tarch::la::Vector<DIMENSIONS,double>
        l2Norm = computeL2Norm( fineGridCell );
      //std::cout << "ascend() l2ErrorNorm: " << l2ErrorNorm << std::endl;


      // Save maximal l2ErrorNorm in _globalMaxL2ErrorNorm
      for(int d = 0; d<DIMENSIONS; d++) {
        if(_globalMaxL2ErrorNorm < l2ErrorNorm[d]) {
          _globalMaxL2ErrorNorm = l2ErrorNorm[d];
        }
      }
      // Add l2ErrorNorm to _globalL2ErrorNorm
      _globalL2ErrorNorm += l2ErrorNorm;
      // Add l2Norm to _globalL2OffsetNorm
      _globalL2OffsetNorm += l2Norm;
      // Don't forget to increment _globalNormAdditions to divide _globalL2Norm
      //by it at the end of iteration before writing it in the file!
      ++_globalNormAdditions;


      // Output for checking
      if(VERBOSE) {
          printParticlesInfo( fineGridCell, "l2ErrorNorm", l2ErrorNorm );
      }

      /* All computations put in output */
      _maxRelativeErrorOut << maxRelativeError << " ";
      _maxErrorOut << maxError << " ";
      _maxOffsetOut << maxOffset << " ";
      _minOffsetOut << minOffset << " ";

      // Histogram process
      l2_error_norm_histogram_->processHistogram(l2ErrorNorm);
      max_error_norm_histogram_->processHistogram(maxError);
      max_offset_norm_histogram_->processHistogram(maxOffset);

      for(int d=0; d<DIMENSIONS; d++) {
        _RMSDOut << rmsd[d] << " ";
        _L2ErrorNormOut << l2ErrorNorm[d] << " ";
        _L2NormOut << l2Norm[d] << " ";
        _meanVelocityOut << meanVelocity[d] << " ";
      }
      /* Write coordinates of each cell near the value of the Norm(offset) */
      for(int d=0; d<DIMENSIONS; d++) {
        _maxRelativeErrorOut << cellOffset[d] << " ";
    	_maxErrorOut << cellOffset[d] << " ";
        _maxOffsetOut << cellOffset[d] << " ";
        _minOffsetOut << cellOffset[d] << " ";
        _RMSDOut << cellOffset[d] << " ";
        _L2ErrorNormOut << cellOffset[d] << " ";
        _L2NormOut << cellOffset[d] << " ";
        _meanVelocityOut << cellOffset[d] << " ";
      }
      /* Put the new line character to have one cell per line */
      _maxRelativeErrorOut << std::endl;
      _maxErrorOut << std::endl;
      _maxOffsetOut << std::endl;
      _minOffsetOut << std::endl;
      _RMSDOut << std::endl;
      _L2ErrorNormOut << std::endl;
      _L2NormOut << std::endl;
      _meanVelocityOut << std::endl;
    }
  enddforx
}
void particles::pit::myfunctions::CoordinatesRepresentationChange::ascend(
  particles::pit::Cell * const    fineGridCells,
  particles::pit::Vertex * const  fineGridVertices,
  const peano::grid::VertexEnumerator&          fineGridVerticesEnumerator,
  particles::pit::Vertex * const  coarseGridVertices,
  const peano::grid::VertexEnumerator&          coarseGridVerticesEnumerator,
  particles::pit::Cell&           coarseGridCell
) {

  dfor3(k)
	particles::pit::Cell fineGridCell = fineGridCells[ fineGridVerticesEnumerator.cell(k) ];
    const tarch::la::Vector<DIMENSIONS,double> cellOffset     = fineGridVerticesEnumerator.getVertexPosition(k);
    const tarch::la::Vector<DIMENSIONS,double> MeanCoordinate = fineGridCell.getMeanCoordinate();
    bool isLeaf = fineGridCell.isLeaf();
    const int cellIndex = fineGridCell.getCellIndex();
    const int NumberOfParticles = ParticleHeap::getInstance().getData(cellIndex).size();

    if( isLeaf && NumberOfParticles>1 ) {
      // Compute Max-Norm
      double maxRelativeError = computeMaxRelativeError( fineGridCell );
      // Save maximal maxRelativeError in _globalMaxRelativeError
      if (_globalMaxRelativeError < maxRelativeError) {
        _globalMaxRelativeError = maxRelativeError;
      }
      double maxError = computeMaxError( fineGridCell );
      if(_global_max_error < maxError) {
          _global_max_error = maxError;
      }
      double maxOffset = computeMaxOffset( fineGridCell );
      // Compute RMSD
      tarch::la::Vector<DIMENSIONS,double> rmsd = computeRMSD( fineGridCell );
      // Computer L2-Norm
      tarch::la::Vector<DIMENSIONS,double> l2ErrorNorm = computeL2ErrorNorm( fineGridCell );
      tarch::la::Vector<DIMENSIONS,double> l2Norm = computeL2Norm( fineGridCell );

      // Save maximal l2ErrorNorm in _globalMaxL2ErrorNorm
      for(int d = 0; d<DIMENSIONS; d++) {
        if(_globalMaxL2ErrorNorm < l2ErrorNorm[d]) {
          _globalMaxL2ErrorNorm = l2ErrorNorm[d];
        }
      }
      // Add l2Norm to _globalL2Norm
      _globalL2ErrorNorm += l2ErrorNorm;
      // Don't forget to increment _globalNormAdditions to divide _globalL2Norm by it
      // at the end of iteration before writing it in the file!
      ++_globalNormAdditions;
      //std::cout << "_globalNormAdditions: " << _globalNormAdditions << std::endl;

      // Output for checking
      //printParticlesInfo( fineGridCell, "maxError", maxError );

      _maxRelativeErrorOut << maxRelativeError << " ";
      _maxErrorOut << maxError << " ";
      _maxOffsetOut << maxOffset << " ";

      // Histogram process
      l2_error_norm_histogram_->processHistogram(l2ErrorNorm);
      max_error_norm_histogram_->processHistogram(maxError);
      max_offset_norm_histogram_->processHistogram(maxOffset);

      for(int d=0; d<DIMENSIONS; d++) {
        _RMSDOut << rmsd[d] << " ";
        _L2ErrorNormOut << l2ErrorNorm[d] << " ";
        _L2NormOut << l2Norm[d] << " ";
        _MeanCoordinateOut << MeanCoordinate[d] << " ";
      }
      for(int d=0; d<DIMENSIONS; d++) {
        _maxRelativeErrorOut << cellOffset[d] << " ";
    	_maxErrorOut << cellOffset[d] << " ";
        _maxOffsetOut << cellOffset[d] << " ";
        _RMSDOut << cellOffset[d] << " ";
        _L2ErrorNormOut << cellOffset[d] << " ";
        _L2NormOut << cellOffset[d] << " ";
        _MeanCoordinateOut << cellOffset[d] << " ";
      }
      _maxRelativeErrorOut << std::endl;
      _maxErrorOut << std::endl;
      _maxOffsetOut << std::endl;
      _RMSDOut << std::endl;
      _L2ErrorNormOut << std::endl;
      _L2NormOut << std::endl;
      _MeanCoordinateOut << std::endl;
    }
  enddforx
}
void RenderArea::updateImageOutput()
{
    if (imageOutput)
    {
        if (output)
        {
            columnPoints *p;
            int iR, iG, iB;

            for (int i = 0; i < outputPoints->count(); i++)
            {
                p = outputPoints->at(i);

                for (int o = 0; o < p->count(); o++)
                {
                    iR = p->at(o)->intensity;
                    iG = p->at(o)->intensity;
                    iB = p->at(o)->intensity;

                    if (iR == -1) // mask pixel
                    {
                        iR = 150;
                        iG = 0;
                        iB = 0;
                    }

                    imageOutput->setPixel(i,p->count() - 1 - o,qRgb(iR,iG,iB));
                }
            }
        }
        else
        {
            columnPoints *ip;
            columnPoints *op;

            for (int i = 0; i < outputPoints->count(); i++)
            {
                op = outputPoints->at(i);
                ip = inputPoints->at(i);

                for (int o = 0; o < ip->count(); o++)
                {
                    if (ip->at(o)->intensity == -1)
                    {
                        imageOutput->setPixel(i,op->count() - 1 - o,qRgb(op->at(o)->intensity,op->at(o)->intensity,op->at(o)->intensity));

                        //if (i == outputPoints->count() - 1)
                        //{
                        //    qDebug() << "output - intensity" << op->count() - 1 - o << " - " << op->at(o)->intensity;
                        //}
                    }
                    else
                    {
                        imageOutput->setPixel(i,ip->count() - 1 - o,qRgb(ip->at(o)->intensity,ip->at(o)->intensity,ip->at(o)->intensity));

                        //if (i == outputPoints->count() - 1)
                        //{
                        //    qDebug() << "output - intensity" << op->count() - 1 - o << " - " << ip->at(o)->intensity;
                        //}
                    }
                }
            }
        }

        computeRMSD();
        update();
    }
}