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 #ifndef MFEM_DIR 67 const char *mesh_file = "../../../mfem/data/star.mesh"; 68 #else 69 const char *mesh_file = MFEM_DIR "/data/star.mesh"; 70 #endif 71 int order = 2; 72 bool visualization = true; 73 74 mfem::OptionsParser args(argc, argv); 75 args.AddOption(&ceed_spec, "-c", "-ceed", "Ceed specification."); 76 args.AddOption(&mesh_file, "-m", "--mesh", "Mesh file to use."); 77 args.AddOption(&order, "-o", "--order", 78 "Finite element order (polynomial degree)."); 79 args.AddOption(&visualization, "-vis", "--visualization", "-no-vis", 80 "--no-visualization", 81 "Enable or disable GLVis visualization."); 82 args.Parse(); 83 if (!args.Good()) { 84 args.PrintUsage(std::cout); 85 return 1; 86 } 87 args.PrintOptions(std::cout); 88 89 // 2. Initialize a Ceed device object using the given Ceed specification. 90 Ceed ceed; 91 CeedInit(ceed_spec, &ceed); 92 93 // 3. Read the mesh from the given mesh file. 94 mfem::Mesh *mesh = new mfem::Mesh(mesh_file, 1, 1); 95 int dim = mesh->Dimension(); 96 97 // 4. Refine the mesh to increase the resolution. In this example we do 98 // 'ref_levels' of uniform refinement. We choose 'ref_levels' to be the 99 // largest number that gives a final system with no more than 50,000 (1,000 100 // in 1D) unknowns, approximately. 101 { 102 double max_dofs = (dim > 1) ? 50000 : 1000; 103 int ref_levels = 104 (int)floor((log(max_dofs/mesh->GetNE())-dim*log(order))/log(2.)/dim); 105 for (int l = 0; l < ref_levels; l++) { 106 mesh->UniformRefinement(); 107 } 108 } 109 if (mesh->GetNodalFESpace() == NULL) { 110 mesh->SetCurvature(1, false, -1, mfem::Ordering::byNODES); 111 } 112 if (mesh->NURBSext) { 113 mesh->SetCurvature(order, false, -1, mfem::Ordering::byNODES); 114 } 115 116 // 5. Define a finite element space on the mesh. Here we use continuous 117 // Lagrange finite elements of the specified order. 118 MFEM_VERIFY(order > 0, "invalid order"); 119 mfem::FiniteElementCollection *fec = new mfem::H1_FECollection(order, dim); 120 mfem::FiniteElementSpace *fespace = new mfem::FiniteElementSpace(mesh, fec); 121 std::cout << "Number of finite element unknowns: " 122 << fespace->GetTrueVSize() << std::endl; 123 124 mfem::FunctionCoefficient sol_coeff(solution); 125 mfem::Array<int> ess_tdof_list; 126 mfem::GridFunction sol(fespace); 127 if (mesh->bdr_attributes.Size()) { 128 mfem::Array<int> ess_bdr(mesh->bdr_attributes.Max()); 129 ess_bdr = 1; 130 fespace->GetEssentialTrueDofs(ess_bdr, ess_tdof_list); 131 sol.ProjectBdrCoefficient(sol_coeff, ess_bdr); 132 } 133 134 // 6. Construct a rhs vector using the linear form f(v) = (rhs, v), where 135 // v is a test function. 136 mfem::LinearForm b(fespace); 137 mfem::FunctionCoefficient rhs_coeff(rhs); 138 b.AddDomainIntegrator(new mfem::DomainLFIntegrator(rhs_coeff)); 139 b.Assemble(); 140 141 // 7. Construct a CeedDiffusionOperator utilizing the 'ceed' device and using 142 // the 'fespace' object to extract data needed by the Ceed objects. 143 CeedDiffusionOperator diff(ceed, fespace); 144 145 mfem::Operator *D; 146 mfem::Vector X, B; 147 diff.FormLinearSystem(ess_tdof_list, sol, b, D, X, B); 148 149 // 8. Solve the discrete system using the conjugate gradients (CG) method. 150 mfem::CGSolver cg; 151 cg.SetRelTol(1e-6); 152 cg.SetMaxIter(1000); 153 cg.SetPrintLevel(3); 154 cg.SetOperator(*D); 155 156 cg.Mult(B, X); 157 158 // 9. Compute and print the L2 norm of the error. 159 std::cout << "L2 norm of the error: " << sol.ComputeL2Error(sol_coeff) 160 << std::endl; 161 162 // 10. Open a socket connection to GLVis and send the mesh and solution for 163 // visualization. 164 if (visualization) { 165 char vishost[] = "localhost"; 166 int visport = 19916; 167 mfem::socketstream sol_sock(vishost, visport); 168 sol_sock.precision(8); 169 sol_sock << "solution\n" << *mesh << sol << std::flush; 170 } 171 172 // 11. Free memory and exit. 173 delete fespace; 174 delete fec; 175 delete mesh; 176 CeedDestroy(&ceed); 177 return 0; 178 } 179