#include "TSFDefs.h"
#include "Sundance.h"
#include "LAPACKGeneralMatrix.h"
#include "DenseSerialVectorType.h"
#include "TSFTimer.h"

using namespace Sundance;

using namespace TSF;


int main(int argc, void** argv)
{
  try
    {
      MPIComm::init(&argc, &argv);
      Timer::start();

      int nx = 40;
      int ny = 40;
      Mesh mesh = rectMesh(-1.0, 1.0, nx, -1.0, 1.0, ny);

      CellSet left(1, "left");
      CellSet right(1, "right");
      CellSet top(1, "top");
      CellSet bottom(1, "bottom");

      // define variations and unknowns for U, V, and P.
      /* use Taylor-Hood discretization */
      Expr delU = new TestFunction(new Lagrange(2), "delU");
      Expr U = new UnknownFunction(new Lagrange(2), "U");
      Expr delV = new TestFunction(new Lagrange(2), "delV");
      Expr V = new UnknownFunction(new Lagrange(2), "V");
      Expr delP = new TestFunction(new Lagrange(1), "delP");
      Expr P = new UnknownFunction(new Lagrange(1), "P");
			
      Expr delVelocity = List(delU, delV);
      Expr velocity = List(U, V);

      // create differential operators for x and y directions, and
      // then form gradient operator.
      Expr dx = new Derivative(0,1);
      Expr dy = new Derivative(1,1);
      Expr grad = List(dx, dy);
			
      // coordinate functions
      Expr x = new CoordExpr(0, "x");
      Expr y = new CoordExpr(1, "y");

      // momentum continuity 
			
      Expr momentumEqn  = -(grad*U)*(grad*delU) - (grad*V)*(grad*delV)
        - P*(dx*delU + dy*delV);

      // incompressibility

      Expr continuityEqn = delP*(dx*U + dy*V);

      Expr eqn = Integral(momentumEqn) + Integral(continuityEqn);




      /*
       * Boundary conditions: 
       * v=0 on all sides
       * u=1 on top 
       * u=0 elsewehere
       */

      EssentialBC bc = EssentialBC(top, delU*(U-1) + delV*V)
        && EssentialBC(left, delU*(U) + delV*V)
        && EssentialBC(right, delU*(U) + delV*V)
        && EssentialBC(bottom, delU*(U) + delV*V);


      /*
        Create a solver object: stablized biconjugate gradient solver
      */
      TSFPreconditionerFactory prec = new ILUKPreconditionerFactory(2);
      TSFLinearSolver solver = new BICGSTABSolver(prec, 1.0e-12, 400);
      //TSFLinearSolver solver = new DirectSolver();

      StaticLinearProblem prob(mesh, eqn, bc, List(delU, delV, delP),
                               List(U, V, P), new PetraVectorType());

      Expr soln = prob.solve(solver);

      Expr U0 = soln[0];
      Expr V0 = soln[1];
      Expr P0 = soln[2];

      ofstream ofu("u.dat");

      U0.matlabDump(ofu);
      ofstream ofv("v.dat");
      V0.matlabDump(ofv);
      ofstream ofp("p.dat");
      P0.matlabDump(ofp);

      Expr delPsi = new TestFunction(new Lagrange(1), "delPsi");
      Expr psi = new UnknownFunction(new Lagrange(1), "psi");

			

    }
  catch(exception& e)
    {
      TSFOut::println(e.what());
      Testing::crash(__FILE__);
      Testing::timeStamp(__FILE__, __DATE__, __TIME__);
    }
  Timer::report();
  MPIComm::finalize();
}







