#ifndef __ERROR_ESTIMATE_H
#define __ERROR_ESTIMATE_H

#include <dolfin.h>

namespace dolfin {
  namespace unicorn {
    class ErrorEstimate
    {
    public:
      
      /// Constructor
      ErrorEstimate(Mesh& mesh, Form* Lres, Form* Lgradphi);
      
      // Compute error (norm estimate)
      void ComputeError(real& error);
      
      // Compute error indicator
      void ComputeErrorIndicator(real t, real k, real T);
      
      // Compute largest indicators
      void ComputeLargestIndicators(std::vector<int>& cells, real percentage);
      
      // Refine based on indicators
      void AdaptiveRefinement(std::vector<int>& cells, int iter);
      
      // Comparison operator for index/value pairs
      struct less_pair : public std::binary_function<std::pair<int, real>,
	std::pair<int, real>, bool>
      {
	bool operator()(std::pair<int, real> x, std::pair<int, real> y)
	{
	  return x.second < y.second;
	}
      };
      
      Mesh& mesh;
      
      Form* Lres;
      Form* Lgradphi;
      Vector e_indx, resx, gradphix;
      Assembler assembler;
    };
    
  }}

#endif
