// MFEM Example 9 - Parallel Version // SUNDIALS Modification // // Compile with: make ex9p // // Sample runs: // mpirun -np 4 ex9p -m ../../data/periodic-segment.mesh -p 1 -rp 1 -s 7 -dt 0.0025 // mpirun -np 4 ex9p -m ../../data/periodic-square.mesh -p 1 -rp 1 -s 8 -dt 0.0025 -tf 9 // mpirun -np 4 ex9p -m ../../data/periodic-hexagon.mesh -p 0 -rp 1 -s 7 -dt 0.0009 -vs 25 // mpirun -np 4 ex9p -m ../../data/periodic-hexagon.mesh -p 0 -rp 1 -s 9 -dt 0.005 -vs 15 // mpirun -np 4 ex9p -m ../../data/amr-quad.mesh -p 1 -rp 1 -s 9 -dt 0.001 -tf 9 // mpirun -np 4 ex9p -m ../../data/star-q3.mesh -p 1 -rp 1 -s 9 -dt 0.0025 -tf 9 // mpirun -np 4 ex9p -m ../../data/disc-nurbs.mesh -p 1 -rp 2 -s 7 -dt 0.0025 -tf 9 // mpirun -np 4 ex9p -m ../../data/periodic-cube.mesh -p 0 -rp 1 -s 8 -dt 0.01 -tf 8 -o 2 // // Description: This example code solves the time-dependent advection equation // du/dt + v.grad(u) = 0, where v is a given fluid velocity, and // u0(x)=u(0,x) is a given initial condition. // // The example demonstrates the use of Discontinuous Galerkin (DG) // bilinear forms in MFEM (face integrators), the use of explicit // ODE time integrators, the definition of periodic boundary // conditions through periodic meshes, as well as the use of GLVis // for persistent visualization of a time-evolving solution. The // saving of time-dependent data files for external visualization // with VisIt (visit.llnl.gov) is also illustrated. #include "mfem.hpp" #include #include #ifndef MFEM_USE_SUNDIALS #error This example requires that MFEM is built with MFEM_USE_SUNDIALS=YES #endif using namespace std; using namespace mfem; // Choice for the problem setup. The fluid velocity, initial condition and // inflow boundary condition are chosen based on this parameter. int problem; // Velocity coefficient void velocity_function(const Vector &x, Vector &v); // Initial condition double u0_function(const Vector &x); // Inflow boundary condition double inflow_function(const Vector &x); // Mesh bounding box Vector bb_min, bb_max; /** A time-dependent operator for the right-hand side of the ODE. The DG weak form of du/dt = -v.grad(u) is M du/dt = K u + b, where M and K are the mass and advection matrices, and b describes the flow on the boundary. This can be written as a general ODE, du/dt = M^{-1} (K u + b), and this class is used to evaluate the right-hand side. */ class FE_Evolution : public TimeDependentOperator { private: HypreParMatrix &M, &K; const Vector &b; HypreSmoother M_prec; CGSolver M_solver; mutable Vector z; public: FE_Evolution(HypreParMatrix &_M, HypreParMatrix &_K, const Vector &_b); virtual void Mult(const Vector &x, Vector &y) const; virtual ~FE_Evolution() { } }; int main(int argc, char *argv[]) { // 1. Initialize MPI. int num_procs, myid; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &num_procs); MPI_Comm_rank(MPI_COMM_WORLD, &myid); // 2. Parse command-line options. problem = 0; const char *mesh_file = "../../data/periodic-hexagon.mesh"; int ser_ref_levels = 2; int par_ref_levels = 0; int order = 3; int ode_solver_type = 4; double t_final = 10.0; double dt = 0.01; bool visualization = true; bool visit = false; bool binary = false; int vis_steps = 5; // Relative and absolute tolerances for CVODE and ARKODE. const double reltol = 1e-2, abstol = 1e-2; int precision = 8; cout.precision(precision); OptionsParser args(argc, argv); args.AddOption(&mesh_file, "-m", "--mesh", "Mesh file to use."); args.AddOption(&problem, "-p", "--problem", "Problem setup to use. See options in velocity_function()."); args.AddOption(&ser_ref_levels, "-rs", "--refine-serial", "Number of times to refine the mesh uniformly in serial."); args.AddOption(&par_ref_levels, "-rp", "--refine-parallel", "Number of times to refine the mesh uniformly in parallel."); args.AddOption(&order, "-o", "--order", "Order (degree) of the finite elements."); args.AddOption(&ode_solver_type, "-s", "--ode-solver", "ODE solver:\n\t" "1 - Forward Euler,\n\t" "2 - RK2 SSP,\n\t" "3 - RK3 SSP,\n\t" "4 - RK4,\n\t" "6 - RK6,\n\t" "7 - CVODE (adaptive order implicit Adams),\n\t" "8 - ARKODE default (4th order) explicit,\n\t" "9 - ARKODE RK8."); args.AddOption(&t_final, "-tf", "--t-final", "Final time; start time is 0."); args.AddOption(&dt, "-dt", "--time-step", "Time step."); args.AddOption(&visualization, "-vis", "--visualization", "-no-vis", "--no-visualization", "Enable or disable GLVis visualization."); args.AddOption(&visit, "-visit", "--visit-datafiles", "-no-visit", "--no-visit-datafiles", "Save data files for VisIt (visit.llnl.gov) visualization."); args.AddOption(&binary, "-binary", "--binary-datafiles", "-ascii", "--ascii-datafiles", "Use binary (Sidre) or ascii format for VisIt data files."); args.AddOption(&vis_steps, "-vs", "--visualization-steps", "Visualize every n-th timestep."); args.Parse(); if (!args.Good()) { if (myid == 0) { args.PrintUsage(cout); } MPI_Finalize(); return 1; } if (myid == 0) { args.PrintOptions(cout); } // check for vaild ODE solver option if (ode_solver_type < 1 || ode_solver_type > 9) { if (myid == 0) { cout << "Unknown ODE solver type: " << ode_solver_type << '\n'; } MPI_Finalize(); return 3; } // 3. Read the serial mesh from the given mesh file on all processors. We can // handle geometrically periodic meshes in this code. Mesh *mesh = new Mesh(mesh_file, 1, 1); int dim = mesh->Dimension(); // 4. Refine the mesh in serial to increase the resolution. In this example // we do 'ser_ref_levels' of uniform refinement, where 'ser_ref_levels' is // a command-line parameter. If the mesh is of NURBS type, we convert it // to a (piecewise-polynomial) high-order mesh. for (int lev = 0; lev < ser_ref_levels; lev++) { mesh->UniformRefinement(); } if (mesh->NURBSext) { mesh->SetCurvature(max(order, 1)); } mesh->GetBoundingBox(bb_min, bb_max, max(order, 1)); // 5. Define the parallel mesh by a partitioning of the serial mesh. Refine // this mesh further in parallel to increase the resolution. Once the // parallel mesh is defined, the serial mesh can be deleted. ParMesh *pmesh = new ParMesh(MPI_COMM_WORLD, *mesh); delete mesh; for (int lev = 0; lev < par_ref_levels; lev++) { pmesh->UniformRefinement(); } // 6. Define the parallel discontinuous DG finite element space on the // parallel refined mesh of the given polynomial order. DG_FECollection fec(order, dim); ParFiniteElementSpace *fes = new ParFiniteElementSpace(pmesh, &fec); HYPRE_Int global_vSize = fes->GlobalTrueVSize(); if (myid == 0) { cout << "Number of unknowns: " << global_vSize << endl; } // 7. Set up and assemble the parallel bilinear and linear forms (and the // parallel hypre matrices) corresponding to the DG discretization. The // DGTraceIntegrator involves integrals over mesh interior faces. VectorFunctionCoefficient velocity(dim, velocity_function); FunctionCoefficient inflow(inflow_function); FunctionCoefficient u0(u0_function); ParBilinearForm *m = new ParBilinearForm(fes); m->AddDomainIntegrator(new MassIntegrator); ParBilinearForm *k = new ParBilinearForm(fes); k->AddDomainIntegrator(new ConvectionIntegrator(velocity, -1.0)); k->AddInteriorFaceIntegrator( new TransposeIntegrator(new DGTraceIntegrator(velocity, 1.0, -0.5))); k->AddBdrFaceIntegrator( new TransposeIntegrator(new DGTraceIntegrator(velocity, 1.0, -0.5))); ParLinearForm *b = new ParLinearForm(fes); b->AddBdrFaceIntegrator( new BoundaryFlowIntegrator(inflow, velocity, -1.0, -0.5)); m->Assemble(); m->Finalize(); int skip_zeros = 0; k->Assemble(skip_zeros); k->Finalize(skip_zeros); b->Assemble(); HypreParMatrix *M = m->ParallelAssemble(); HypreParMatrix *K = k->ParallelAssemble(); HypreParVector *B = b->ParallelAssemble(); // 8. Define the initial conditions, save the corresponding grid function to // a file and (optionally) save data in the VisIt format and initialize // GLVis visualization. ParGridFunction *u = new ParGridFunction(fes); u->ProjectCoefficient(u0); HypreParVector *U = u->GetTrueDofs(); { ostringstream mesh_name, sol_name; mesh_name << "ex9-mesh." << setfill('0') << setw(6) << myid; sol_name << "ex9-init." << setfill('0') << setw(6) << myid; ofstream omesh(mesh_name.str().c_str()); omesh.precision(precision); pmesh->Print(omesh); ofstream osol(sol_name.str().c_str()); osol.precision(precision); u->Save(osol); } // Create data collection for solution output: either VisItDataCollection for // ascii data files, or SidreDataCollection for binary data files. DataCollection *dc = NULL; if (visit) { if (binary) { #ifdef MFEM_USE_SIDRE dc = new SidreDataCollection("Example9-Parallel", pmesh); #else MFEM_ABORT("Must build with MFEM_USE_SIDRE=YES for binary output."); #endif } else { dc = new VisItDataCollection("Example9-Parallel", pmesh); dc->SetPrecision(precision); } dc->RegisterField("solution", u); dc->SetCycle(0); dc->SetTime(0.0); dc->Save(); } socketstream sout; if (visualization) { char vishost[] = "localhost"; int visport = 19916; sout.open(vishost, visport); if (!sout) { if (myid == 0) cout << "Unable to connect to GLVis server at " << vishost << ':' << visport << endl; visualization = false; if (myid == 0) { cout << "GLVis visualization disabled.\n"; } } else { sout << "parallel " << num_procs << " " << myid << "\n"; sout.precision(precision); sout << "solution\n" << *pmesh << *u; sout << "pause\n"; sout << flush; if (myid == 0) cout << "GLVis visualization paused." << " Press space (in the GLVis window) to resume it.\n"; } } // 9. Define the time-dependent evolution operator describing the ODE // right-hand side, and define the ODE solver used for time integration. FE_Evolution adv(*M, *K, *B); double t = 0.0; adv.SetTime(t); // Create the time integrator ODESolver *ode_solver = NULL; CVODESolver *cvode = NULL; ARKStepSolver *arkode = NULL; switch (ode_solver_type) { case 1: ode_solver = new ForwardEulerSolver; break; case 2: ode_solver = new RK2Solver(1.0); break; case 3: ode_solver = new RK3SSPSolver; break; case 4: ode_solver = new RK4Solver; break; case 6: ode_solver = new RK6Solver; break; case 7: cvode = new CVODESolver(MPI_COMM_WORLD, CV_ADAMS); cvode->Init(adv); cvode->SetSStolerances(reltol, abstol); cvode->SetMaxStep(dt); cvode->UseSundialsLinearSolver(); ode_solver = cvode; break; case 8: case 9: arkode = new ARKStepSolver(MPI_COMM_WORLD, ARKStepSolver::EXPLICIT); arkode->Init(adv); arkode->SetSStolerances(reltol, abstol); arkode->SetMaxStep(dt); if (ode_solver_type == 9) { arkode->SetERKTableNum(FEHLBERG_13_7_8); } ode_solver = arkode; break; } // Initialize MFEM integrators, SUNDIALS integrators are initialized above if (ode_solver_type < 7) { ode_solver->Init(adv); } // 10. Perform time-integration (looping over the time iterations, ti, // with a time-step dt). bool done = false; for (int ti = 0; !done; ) { double dt_real = min(dt, t_final - t); ode_solver->Step(*U, t, dt_real); ti++; done = (t >= t_final - 1e-8*dt); if (done || ti % vis_steps == 0) { if (myid == 0) { cout << "time step: " << ti << ", time: " << t << endl; if (cvode) { cvode->PrintInfo(); } if (arkode) { arkode->PrintInfo(); } } // 11. Extract the parallel grid function corresponding to the finite // element approximation U (the local solution on each processor). *u = *U; if (visualization) { sout << "parallel " << num_procs << " " << myid << "\n"; sout << "solution\n" << *pmesh << *u << flush; } if (visit) { dc->SetCycle(ti); dc->SetTime(t); dc->Save(); } } } // 12. Save the final solution in parallel. This output can be viewed later // using GLVis: "glvis -np -m ex9-mesh -g ex9-final". { *u = *U; ostringstream sol_name; sol_name << "ex9-final." << setfill('0') << setw(6) << myid; ofstream osol(sol_name.str().c_str()); osol.precision(precision); u->Save(osol); } // 13. Free the used memory. delete U; delete u; delete B; delete b; delete K; delete k; delete M; delete m; delete fes; delete pmesh; delete ode_solver; delete dc; MPI_Finalize(); return 0; } // Implementation of class FE_Evolution FE_Evolution::FE_Evolution(HypreParMatrix &_M, HypreParMatrix &_K, const Vector &_b) : TimeDependentOperator(_M.Height()), M(_M), K(_K), b(_b), M_solver(M.GetComm()), z(_M.Height()) { M_prec.SetType(HypreSmoother::Jacobi); M_solver.SetPreconditioner(M_prec); M_solver.SetOperator(M); M_solver.iterative_mode = false; M_solver.SetRelTol(1e-9); M_solver.SetAbsTol(0.0); M_solver.SetMaxIter(100); M_solver.SetPrintLevel(0); } void FE_Evolution::Mult(const Vector &x, Vector &y) const { // y = M^{-1} (K x + b) K.Mult(x, z); z += b; M_solver.Mult(z, y); } // Velocity coefficient void velocity_function(const Vector &x, Vector &v) { int dim = x.Size(); // map to the reference [-1,1] domain Vector X(dim); for (int i = 0; i < dim; i++) { double center = (bb_min[i] + bb_max[i]) * 0.5; X(i) = 2 * (x(i) - center) / (bb_max[i] - bb_min[i]); } switch (problem) { case 0: { // Translations in 1D, 2D, and 3D switch (dim) { case 1: v(0) = 1.0; break; case 2: v(0) = sqrt(2./3.); v(1) = sqrt(1./3.); break; case 3: v(0) = sqrt(3./6.); v(1) = sqrt(2./6.); v(2) = sqrt(1./6.); break; } break; } case 1: case 2: { // Clockwise rotation in 2D around the origin const double w = M_PI/2; switch (dim) { case 1: v(0) = 1.0; break; case 2: v(0) = w*X(1); v(1) = -w*X(0); break; case 3: v(0) = w*X(1); v(1) = -w*X(0); v(2) = 0.0; break; } break; } case 3: { // Clockwise twisting rotation in 2D around the origin const double w = M_PI/2; double d = max((X(0)+1.)*(1.-X(0)),0.) * max((X(1)+1.)*(1.-X(1)),0.); d = d*d; switch (dim) { case 1: v(0) = 1.0; break; case 2: v(0) = d*w*X(1); v(1) = -d*w*X(0); break; case 3: v(0) = d*w*X(1); v(1) = -d*w*X(0); v(2) = 0.0; break; } break; } } } // Initial condition double u0_function(const Vector &x) { int dim = x.Size(); // map to the reference [-1,1] domain Vector X(dim); for (int i = 0; i < dim; i++) { double center = (bb_min[i] + bb_max[i]) * 0.5; X(i) = 2 * (x(i) - center) / (bb_max[i] - bb_min[i]); } switch (problem) { case 0: case 1: { switch (dim) { case 1: return exp(-40.*pow(X(0)-0.5,2)); case 2: case 3: { double rx = 0.45, ry = 0.25, cx = 0., cy = -0.2, w = 10.; if (dim == 3) { const double s = (1. + 0.25*cos(2*M_PI*X(2))); rx *= s; ry *= s; } return ( erfc(w*(X(0)-cx-rx))*erfc(-w*(X(0)-cx+rx)) * erfc(w*(X(1)-cy-ry))*erfc(-w*(X(1)-cy+ry)) )/16; } } } case 2: { double x_ = X(0), y_ = X(1), rho, phi; rho = hypot(x_, y_); phi = atan2(y_, x_); return pow(sin(M_PI*rho),2)*sin(3*phi); } case 3: { const double f = M_PI; return sin(f*X(0))*sin(f*X(1)); } } return 0.0; } // Inflow boundary condition (zero for the problems considered in this example) double inflow_function(const Vector &x) { switch (problem) { case 0: case 1: case 2: case 3: return 0.0; } return 0.0; }