Example #1
0
void tic()
{
   tic_toc.Clear();
   tic_toc.Start();
}
Example #2
0
int main(int argc, char *argv[])
{
    StopWatch chrono;

    // 1. Parse command-line options.
    const char *mesh_file = "../data/star.mesh";
    int order = 1;
    bool visualization = 1;

    OptionsParser args(argc, argv);
    args.AddOption(&mesh_file, "-m", "--mesh",
                   "Mesh file to use.");
    args.AddOption(&order, "-o", "--order",
                   "Finite element order (polynomial degree).");
    args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
                   "--no-visualization",
                   "Enable or disable GLVis visualization.");
    args.Parse();
    if (!args.Good())
    {
        args.PrintUsage(cout);
        return 1;
    }
    args.PrintOptions(cout);

    // 2. Read the mesh from the given mesh file. We can handle triangular,
    //    quadrilateral, tetrahedral, hexahedral, surface and volume meshes with
    //    the same code.
    Mesh *mesh;
    ifstream imesh(mesh_file);
    if (!imesh)
    {
        cerr << "\nCan not open mesh file: " << mesh_file << '\n' << endl;
        return 2;
    }
    mesh = new Mesh(imesh, 1, 1);
    imesh.close();
    int dim = mesh->Dimension();

    // 3. Refine the mesh to increase the resolution. In this example we do
    //    'ref_levels' of uniform refinement. We choose 'ref_levels' to be the
    //    largest number that gives a final mesh with no more than 10,000
    //    elements.
    {
        int ref_levels =
            (int)floor(log(10000./mesh->GetNE())/log(2.)/dim);
        for (int l = 0; l < ref_levels; l++)
        {
            mesh->UniformRefinement();
        }
    }

    // 4. Define a finite element space on the mesh. Here we use the
    //    Raviart-Thomas finite elements of the specified order.
    FiniteElementCollection *hdiv_coll(new RT_FECollection(order, dim));
    FiniteElementCollection *l2_coll(new L2_FECollection(order, dim));

    FiniteElementSpace *R_space = new FiniteElementSpace(mesh, hdiv_coll);
    FiniteElementSpace *W_space = new FiniteElementSpace(mesh, l2_coll);

    // 5. Define the BlockStructure of the problem, i.e. define the array of
    //    offsets for each variable. The last component of the Array is the sum
    //    of the dimensions of each block.
    Array<int> block_offsets(3); // number of variables + 1
    block_offsets[0] = 0;
    block_offsets[1] = R_space->GetVSize();
    block_offsets[2] = W_space->GetVSize();
    block_offsets.PartialSum();

    std::cout << "***********************************************************\n";
    std::cout << "dim(R) = " << block_offsets[1] - block_offsets[0] << "\n";
    std::cout << "dim(W) = " << block_offsets[2] - block_offsets[1] << "\n";
    std::cout << "dim(R+W) = " << block_offsets.Last() << "\n";
    std::cout << "***********************************************************\n";

    // 6. Define the coefficients, analytical solution, and rhs of the PDE.
    ConstantCoefficient k(1.0);

    VectorFunctionCoefficient fcoeff(dim, fFun);
    FunctionCoefficient fnatcoeff(f_natural);
    FunctionCoefficient gcoeff(gFun);

    VectorFunctionCoefficient ucoeff(dim, uFun_ex);
    FunctionCoefficient pcoeff(pFun_ex);

    // 7. Allocate memory (x, rhs) for the analytical solution and the right hand
    //    side.  Define the GridFunction u,p for the finite element solution and
    //    linear forms fform and gform for the right hand side.  The data
    //    allocated by x and rhs are passed as a reference to the grid functions
    //    (u,p) and the linear forms (fform, gform).
    BlockVector x(block_offsets), rhs(block_offsets);

    LinearForm *fform(new LinearForm);
    fform->Update(R_space, rhs.GetBlock(0), 0);
    fform->AddDomainIntegrator(new VectorFEDomainLFIntegrator(fcoeff));
    fform->AddBoundaryIntegrator(new VectorFEBoundaryFluxLFIntegrator(fnatcoeff));
    fform->Assemble();

    LinearForm *gform(new LinearForm);
    gform->Update(W_space, rhs.GetBlock(1), 0);
    gform->AddDomainIntegrator(new DomainLFIntegrator(gcoeff));
    gform->Assemble();

    // 8. Assemble the finite element matrices for the Darcy operator
    //
    //                            D = [ M  B^T ]
    //                                [ B   0  ]
    //     where:
    //
    //     M = \int_\Omega k u_h \cdot v_h d\Omega   u_h, v_h \in R_h
    //     B   = -\int_\Omega \div u_h q_h d\Omega   u_h \in R_h, q_h \in W_h
    BilinearForm *mVarf(new BilinearForm(R_space));
    MixedBilinearForm *bVarf(new MixedBilinearForm(R_space, W_space));

    mVarf->AddDomainIntegrator(new VectorFEMassIntegrator(k));
    mVarf->Assemble();
    mVarf->Finalize();
    SparseMatrix &M(mVarf->SpMat());

    bVarf->AddDomainIntegrator(new VectorFEDivergenceIntegrator);
    bVarf->Assemble();
    bVarf->Finalize();
    SparseMatrix & B(bVarf->SpMat());
    B *= -1.;
    SparseMatrix *BT = Transpose(B);

    BlockMatrix darcyMatrix(block_offsets);
    darcyMatrix.SetBlock(0,0, &M);
    darcyMatrix.SetBlock(0,1, BT);
    darcyMatrix.SetBlock(1,0, &B);

    // 9. Construct the operators for preconditioner
    //
    //                 P = [ diag(M)         0         ]
    //                     [  0       B diag(M)^-1 B^T ]
    //
    //     Here we use Symmetric Gauss-Seidel to approximate the inverse of the
    //     pressure Schur Complement
    SparseMatrix *MinvBt = Transpose(B);
    Vector Md(M.Height());
    M.GetDiag(Md);
    for (int i = 0; i < Md.Size(); i++)
    {
        MinvBt->ScaleRow(i, 1./Md(i));
    }
    SparseMatrix *S = Mult(B, *MinvBt);

    Solver *invM, *invS;
    invM = new DSmoother(M);
#ifndef MFEM_USE_SUITESPARSE
    invS = new GSSmoother(*S);
#else
    invS = new UMFPackSolver(*S);
#endif

    invM->iterative_mode = false;
    invS->iterative_mode = false;

    BlockDiagonalPreconditioner darcyPrec(block_offsets);
    darcyPrec.SetDiagonalBlock(0, invM);
    darcyPrec.SetDiagonalBlock(1, invS);

    // 10. Solve the linear system with MINRES.
    //     Check the norm of the unpreconditioned residual.
    int maxIter(1000);
    double rtol(1.e-6);
    double atol(1.e-10);

    chrono.Clear();
    chrono.Start();
    MINRESSolver solver;
    solver.SetAbsTol(atol);
    solver.SetRelTol(rtol);
    solver.SetMaxIter(maxIter);
    solver.SetOperator(darcyMatrix);
    solver.SetPreconditioner(darcyPrec);
    solver.SetPrintLevel(1);
    x = 0.0;
    solver.Mult(rhs, x);
    chrono.Stop();

    if (solver.GetConverged())
        std::cout << "MINRES converged in " << solver.GetNumIterations()
                  << " iterations with a residual norm of " << solver.GetFinalNorm() << ".\n";
    else
        std::cout << "MINRES did not converge in " << solver.GetNumIterations()
                  << " iterations. Residual norm is " << solver.GetFinalNorm() << ".\n";
    std::cout << "MINRES solver took " << chrono.RealTime() << "s. \n";

    // 11. Create the grid functions u and p. Compute the L2 error norms.
    GridFunction u, p;
    u.Update(R_space, x.GetBlock(0), 0);
    p.Update(W_space, x.GetBlock(1), 0);

    int order_quad = max(2, 2*order+1);
    const IntegrationRule *irs[Geometry::NumGeom];
    for (int i=0; i < Geometry::NumGeom; ++i)
    {
        irs[i] = &(IntRules.Get(i, order_quad));
    }

    double err_u  = u.ComputeL2Error(ucoeff, irs);
    double norm_u = ComputeLpNorm(2., ucoeff, *mesh, irs);
    double err_p  = p.ComputeL2Error(pcoeff, irs);
    double norm_p = ComputeLpNorm(2., pcoeff, *mesh, irs);

    std::cout << "|| u_h - u_ex || / || u_ex || = " << err_u / norm_u << "\n";
    std::cout << "|| p_h - p_ex || / || p_ex || = " << err_p / norm_p << "\n";

    // 12. Save the mesh and the solution. This output can be viewed later using
    //     GLVis: "glvis -m ex5.mesh -g sol_u.gf" or "glvis -m ex5.mesh -g
    //     sol_p.gf".
    {
        ofstream mesh_ofs("ex5.mesh");
        mesh_ofs.precision(8);
        mesh->Print(mesh_ofs);

        ofstream u_ofs("sol_u.gf");
        u_ofs.precision(8);
        u.Save(u_ofs);

        ofstream p_ofs("sol_p.gf");
        p_ofs.precision(8);
        p.Save(p_ofs);
    }

    // 13. Save data in the VisIt format
    VisItDataCollection visit_dc("Example5", mesh);
    visit_dc.RegisterField("velocity", &u);
    visit_dc.RegisterField("pressure", &p);
    visit_dc.Save();

    // 14. Send the solution by socket to a GLVis server.
    if (visualization)
    {
        char vishost[] = "localhost";
        int  visport   = 19916;
        socketstream u_sock(vishost, visport);
        u_sock.precision(8);
        u_sock << "solution\n" << *mesh << u << "window_title 'Velocity'" << endl;
        socketstream p_sock(vishost, visport);
        p_sock.precision(8);
        p_sock << "solution\n" << *mesh << p << "window_title 'Pressure'" << endl;
    }

    // 15. Free the used memory.
    delete fform;
    delete gform;
    delete invM;
    delete invS;
    delete S;
    delete MinvBt;
    delete BT;
    delete mVarf;
    delete bVarf;
    delete W_space;
    delete R_space;
    delete l2_coll;
    delete hdiv_coll;
    delete mesh;

    return 0;
}