1 // libCEED + MFEM Example: BP3 2 // 3 // This example illustrates a simple usage of libCEED with the MFEM (mfem.org) 4 // finite element library. 5 // 6 // The example reads a mesh from a file and solves a linear system with a 7 // diffusion stiffness matrix (with a prescribed analytic solution, provided by 8 // the function 'solution'). The diffusion matrix is expressed as a new class, 9 // CeedDiffusionOperator, derived from mfem::Operator. Internally, 10 // CeedDiffusionOperator uses a CeedOperator object constructed based on an 11 // mfem::FiniteElementSpace. All libCEED objects use a Ceed logical device 12 // object constructed based on a command line argument. (-ceed). 13 // 14 // The linear system is inverted using the conjugate gradients algorithm 15 // corresponding to CEED BP3, see http://ceed.exascaleproject.org/bps. Arbitrary 16 // mesh and solution orders in 1D, 2D and 3D are supported from the same code. 17 // 18 // Build with: 19 // 20 // make bp3 [MFEM_DIR=</path/to/mfem>] [CEED_DIR=</path/to/libceed>] 21 // 22 // Sample runs: 23 // 24 // bp3 25 // bp3 -ceed /cpu/self 26 // bp3 -m ../../../mfem/data/fichera.mesh -o 4 27 // bp3 -m ../../../mfem/data/square-disc-nurbs.mesh -o 6 28 // bp3 -m ../../../mfem/data/inline-segment.mesh -o 8 29 30 #include <ceed.h> 31 #include <mfem.hpp> 32 #include <bp3.hpp> 33 34 /// Exact solution 35 double solution(const mfem::Vector &pt) { 36 static const double x[3] = { -0.32, 0.15, 0.24 }; 37 static const double k[3] = { 1.21, 1.45, 1.37 }; 38 double val = sin(M_PI*(x[0]+k[0]*pt(0))); 39 for (int d = 1; d < pt.Size(); d++) 40 val *= sin(M_PI*(x[d]+k[d]*pt(d))); 41 return val; 42 } 43 44 /// Right-hand side 45 double rhs(const mfem::Vector &pt) { 46 static const double x[3] = { -0.32, 0.15, 0.24 }; 47 static const double k[3] = { 1.21, 1.45, 1.37 }; 48 double f[3], l[3], val, lap; 49 f[0] = sin(M_PI*(x[0]+k[0]*pt(0))); 50 l[0] = M_PI*M_PI*k[0]*k[0]*f[0]; 51 val = f[0]; 52 lap = l[0]; 53 for (int d = 1; d < pt.Size(); d++) { 54 f[d] = sin(M_PI*(x[d]+k[d]*pt(d))); 55 l[d] = M_PI*M_PI*k[d]*k[d]*f[d]; 56 lap = lap*f[d] + val*l[d]; 57 val = val*f[d]; 58 } 59 return lap; 60 } 61 62 63 int main(int argc, char *argv[]) { 64 // 1. Parse command-line options. 65 const char *ceed_spec = "/cpu/self"; 66 const char *mesh_file = "../../../mfem/data/star.mesh"; 67 int order = 2; 68 bool visualization = true; 69 70 mfem::OptionsParser args(argc, argv); 71 args.AddOption(&ceed_spec, "-c", "-ceed", "Ceed specification."); 72 args.AddOption(&mesh_file, "-m", "--mesh", "Mesh file to use."); 73 args.AddOption(&order, "-o", "--order", 74 "Finite element order (polynomial degree)."); 75 args.AddOption(&visualization, "-vis", "--visualization", "-no-vis", 76 "--no-visualization", 77 "Enable or disable GLVis visualization."); 78 args.Parse(); 79 if (!args.Good()) { 80 args.PrintUsage(std::cout); 81 return 1; 82 } 83 args.PrintOptions(std::cout); 84 85 // 2. Initialize a Ceed device object using the given Ceed specification. 86 Ceed ceed; 87 CeedInit(ceed_spec, &ceed); 88 89 // 3. Read the mesh from the given mesh file. 90 mfem::Mesh *mesh = new mfem::Mesh(mesh_file, 1, 1); 91 int dim = mesh->Dimension(); 92 93 // 4. Refine the mesh to increase the resolution. In this example we do 94 // 'ref_levels' of uniform refinement. We choose 'ref_levels' to be the 95 // largest number that gives a final system with no more than 50,000 (1,000 96 // in 1D) unknowns, approximately. 97 { 98 double max_dofs = (dim > 1) ? 50000 : 1000; 99 int ref_levels = 100 (int)floor((log(max_dofs/mesh->GetNE())-dim*log(order))/log(2.)/dim); 101 for (int l = 0; l < ref_levels; l++) { 102 mesh->UniformRefinement(); 103 } 104 } 105 if (mesh->GetNodalFESpace() == NULL) { 106 mesh->SetCurvature(1, false, -1, mfem::Ordering::byNODES); 107 } 108 if (mesh->NURBSext) { 109 mesh->SetCurvature(order, false, -1, mfem::Ordering::byNODES); 110 } 111 112 // 5. Define a finite element space on the mesh. Here we use continuous 113 // Lagrange finite elements of the specified order. 114 MFEM_VERIFY(order > 0, "invalid order"); 115 mfem::FiniteElementCollection *fec = new mfem::H1_FECollection(order, dim); 116 mfem::FiniteElementSpace *fespace = new mfem::FiniteElementSpace(mesh, fec); 117 std::cout << "Number of finite element unknowns: " 118 << fespace->GetTrueVSize() << std::endl; 119 120 mfem::FunctionCoefficient sol_coeff(solution); 121 mfem::Array<int> ess_tdof_list; 122 mfem::GridFunction sol(fespace); 123 if (mesh->bdr_attributes.Size()) { 124 mfem::Array<int> ess_bdr(mesh->bdr_attributes.Max()); 125 ess_bdr = 1; 126 fespace->GetEssentialTrueDofs(ess_bdr, ess_tdof_list); 127 sol.ProjectBdrCoefficient(sol_coeff, ess_bdr); 128 } 129 130 // 6. Construct a rhs vector using the linear form f(v) = (rhs, v), where 131 // v is a test function. 132 mfem::LinearForm b(fespace); 133 mfem::FunctionCoefficient rhs_coeff(rhs); 134 b.AddDomainIntegrator(new mfem::DomainLFIntegrator(rhs_coeff)); 135 b.Assemble(); 136 137 // 7. Construct a CeedDiffusionOperator utilizing the 'ceed' device and using 138 // the 'fespace' object to extract data needed by the Ceed objects. 139 CeedDiffusionOperator diff(ceed, fespace); 140 141 mfem::Operator *D; 142 mfem::Vector X, B; 143 diff.FormLinearSystem(ess_tdof_list, sol, b, D, X, B); 144 145 // 8. Solve the discrete system using the conjugate gradients (CG) method. 146 mfem::CGSolver cg; 147 cg.SetRelTol(1e-6); 148 cg.SetMaxIter(1000); 149 cg.SetPrintLevel(3); 150 cg.SetOperator(*D); 151 152 cg.Mult(B, X); 153 154 // 9. Compute and print the L2 norm of the error. 155 std::cout << "L2 norm of the error: " << sol.ComputeL2Error(sol_coeff) 156 << std::endl; 157 158 // 10. Open a socket connection to GLVis and send the mesh and solution for 159 // visualization. 160 if (visualization) { 161 char vishost[] = "localhost"; 162 int visport = 19916; 163 mfem::socketstream sol_sock(vishost, visport); 164 sol_sock.precision(8); 165 sol_sock << "solution\n" << *mesh << sol << std::flush; 166 } 167 168 // 11. Free memory and exit. 169 delete fespace; 170 delete fec; 171 delete mesh; 172 CeedDestroy(&ceed); 173 return 0; 174 } 175