#include "Sundance.h"

int main(int argc, void** argv)
{
  try
    {
      Sundance::init(&argc, &argv);

      /* create a simple mesh on the rectangle */
      int nx = 32;
      int ny = 32;
      MeshGenerator mesher = new RectangleMesher(-1.0, 1.0, nx, -1.0, 1.0, ny);
      Mesh mesh = mesher.getMesh();

      /* define coordinate functions for x and y coordinates */
      Expr x = new CoordExpr(0);
      Expr y = new CoordExpr(1);

      /* define cells sets for each of the four sides of the rectangle */
      CellSet boundary = new BoundaryCellSet();
      CellSet left = boundary.subset( x == -1.0 );
      CellSet right = boundary.subset( x == 1.0 );
      CellSet bottom = boundary.subset( y == -1.0 );
      CellSet top = boundary.subset( y == 1.0 );

      /* define variations and unknowns for U, V, and P
       * using the same basis functions for velocity and pressure */
      Expr delU = new TestFunction(new Lagrange(1), "delU");
      Expr U = new UnknownFunction(new Lagrange(1), "U");
      Expr delV = new TestFunction(new Lagrange(1), "delV");
      Expr V = new UnknownFunction(new Lagrange(1), "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);
      Expr dy = new Derivative(1);
      Expr grad = List(dx, dy);
			
      /* momentum continuity equation */
			
      Expr momentumEqn  = -(grad*U)*(grad*delU) - (grad*V)*(grad*delV)
        + P*(dx*delU + dy*delV);

      /* incompressibility constraint equation with stabilization */

      Expr beta = new ParameterExpr(0.02);
      Expr h = 1.0/((double) nx);

      Expr continuityEqn = -delP*(dx*U + dy*V) 
        - beta*h*h*(grad*P)*(grad*delP);

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

      /*
       * Boundary conditions: 
       * v=0 on top, bottom, and left.
       * u=0 on top and bottom.
       * u=1/2 (1-y^2) on left.
       * Natural BCs on right.
       */

      Expr ULeft = 0.5*(1.0-y*y);
			
      EssentialBC bc = EssentialBC(top, delU*U + delV*V)
        && EssentialBC(bottom, delU*U + delV*V)
        && EssentialBC(left, delU*(U-ULeft));

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

      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];

      /* We've now solved the problem for the primitive variables.
       * For visualization we next compute the streamfunction */
      Expr delPsi = new TestFunction(new Lagrange(1));
      Expr psi = new UnknownFunction(new Lagrange(1));

      Expr vorticity = dx*v0 - dy*u0;
      Integral streamfunctionEqn(-(grad*delPsi)*(grad*psi) - delPsi*vorticity);

      /* streamfunction is zero along entire boundary */
      EssentialBC streamfunctionBC = EssentialBC(bottom, delPsi*psi)
        && EssentialBC(bottom, delPsi*(psi-2.0/3.0));
			
      StaticLinearProblem streamfunctionProb(mesh, streamfunctionEqn, 
                                             streamfunctionBC,
                                             delPsi, psi, 
                                             new PetraVectorType());

      Expr psi0 = streamfunctionProb.solve(solver);

      /*
        write the streamfunction in a form readable by matlab
      */
      FieldWriter psiWriter = new MatlabWriter("psi.dat");
      psiWriter.writeField(psi0);
			
			

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







