void NumProcEnergyCalc :: Do(LocalHeap & lh) { double result = 0; double potential = 0; double conversion = 1.602176565e-19 * 6.02214129e23 * 0.5 / 1000.0; const BilinearFormIntegrator & bfi = (bfa) ? *bfa->GetIntegrator(0) : *gfu->GetFESpace().GetIntegrator(); const BilinearFormIntegrator & bfi2 = (bfa) ? *bfa->GetIntegrator(0) : *gfu->GetFESpace().GetIntegrator(); cout.precision(dbl::digits10); for(unsigned int kk=0; kk<molecule.size(); kk++) { Atom currentAtom = molecule.at(kk); point(0) = currentAtom.x; point(1) = currentAtom.y; point(2) = currentAtom.z; FlatVector<double> pflux(bfi.DimFlux(), lh); CalcPointFlux (ma, *gfu, point, domains, pflux, bfi, applyd, lh, component); result = pflux(0); if (showsteps) cout << "(" << pflux(0)*conversion; CalcPointFlux (ma, *gfu0, point, domains, pflux, bfi2, applyd, lh, component); result -= pflux(0); if (showsteps) cout << " - " << pflux(0)*conversion << ")*" << currentAtom.charge << " = " << currentAtom.charge * result*conversion << endl; potential += currentAtom.charge * result; } double energy; energy = (1.602176565e-19 * 6.02214129e23 * 0.5 * potential)/1000; if (MyMPI_GetNTasks() == 1 || MyMPI_GetId() != 0){ ofstream resultFile; resultFile.open("result.out"); resultFile << "The energy difference is " << energy << " [kJ/mol].\n"; resultFile.close(); cout << "The energy difference is " << setprecision(12) << energy << " [kJ/mol].\n"; } pde.GetVariable(variablename,true) = result; }
/*virtual*/ Operand* InnerProductScalar::Simplify( const SimplificationContext& simplificationContext ) { if( exponent == 0 ) { NumericScalar* numericScalar = new NumericScalar(); numericScalar->SetReal( 1.0 ); return numericScalar; } BilinearForm* bilinearForm = BilinearForm::Get(); if( !bilinearForm ) return 0; double result; if( !bilinearForm->Evaluate( nameA, nameB, result ) ) return 0; NumericScalar* numericScalar = new NumericScalar(); numericScalar->SetReal( result ); return numericScalar; }
void UpdateProblem(Mesh &mesh, FiniteElementSpace &fespace, GridFunction &x, BilinearForm &a, LinearForm &b) { // Update the space: recalculate the number of DOFs and construct a matrix // that will adjust any GridFunctions to the new mesh state. fespace.Update(); // Interpolate the solution on the new mesh by applying the transformation // matrix computed in the finite element space. Multiple GridFunctions could // be updated here. x.Update(); // Free any transformation matrices to save memory. fespace.UpdatesFinished(); // Inform the linear and bilinear forms that the space has changed. a.Update(); b.Update(); }
int main (int argc, char *argv[]) { Mesh *mesh; SightInit(argc, argv, "ex1", "dbg.MFEM.ex2"); if (argc == 1) { cout << "\nUsage: ex2 <mesh_file>\n" << endl; return 1; } // 1. Read the mesh from the given mesh file. We can handle triangular, // quadrilateral, tetrahedral or hexahedral elements with the same code. ifstream imesh(argv[1]); if (!imesh) { cerr << "\nCan not open mesh file: " << argv[1] << '\n' << endl; return 2; } mesh = new Mesh(imesh, 1, 1); imesh.close(); if (mesh->attributes.Max() < 2 || mesh->bdr_attributes.Max() < 2) { cerr << "\nInput mesh should have at least two materials and " << "two boundary attributes! (See schematic in ex2.cpp)\n" << endl; return 3; } int dim = mesh->Dimension(); // 2. Select the order of the finite element discretization space. For NURBS // meshes, we increase the order by degree elevation. int p; if(argc==3) p = atoi(argv[2]); else { cout << "Enter finite element space order --> " << flush; cin >> p; } if (mesh->NURBSext && p > mesh->NURBSext->GetOrder()) mesh->DegreeElevate(p - mesh->NURBSext->GetOrder()); // 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 5,000 // elements. { int ref_levels = (int)floor(log(5000./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 vector finite // elements, i.e. dim copies of a scalar finite element space. The vector // dimension is specified by the last argument of the FiniteElementSpace // constructor. For NURBS meshes, we use the (degree elevated) NURBS space // associated with the mesh nodes. FiniteElementCollection *fec; FiniteElementSpace *fespace; if (mesh->NURBSext) { fec = NULL; fespace = mesh->GetNodes()->FESpace(); } else { fec = new H1_FECollection(p, dim); fespace = new FiniteElementSpace(mesh, fec, dim); } dbg << "Number of unknowns: " << fespace->GetVSize() << endl << "Assembling: " << flush; // 5. Set up the linear form b(.) which corresponds to the right-hand side of // the FEM linear system. In this case, b_i equals the boundary integral // of f*phi_i where f represents a "pull down" force on the Neumann part // of the boundary and phi_i are the basis functions in the finite element // fespace. The force is defined by the VectorArrayCoefficient object f, // which is a vector of Coefficient objects. The fact that f is non-zero // on boundary attribute 2 is indicated by the use of piece-wise constants // coefficient for its last component. VectorArrayCoefficient f(dim); for (int i = 0; i < dim-1; i++) f.Set(i, new ConstantCoefficient(0.0)); { Vector pull_force(mesh->bdr_attributes.Max()); pull_force = 0.0; pull_force(1) = -1.0e-2; f.Set(dim-1, new PWConstCoefficient(pull_force)); } LinearForm *b = new LinearForm(fespace); b->AddBoundaryIntegrator(new VectorBoundaryLFIntegrator(f)); dbg << "r.h.s. ... " << flush; b->Assemble(); // 6. Define the solution vector x as a finite element grid function // corresponding to fespace. Initialize x with initial guess of zero, // which satisfies the boundary conditions. GridFunction x(fespace); x = 0.0; // 7. Set up the bilinear form a(.,.) on the finite element space // corresponding to the linear elasticity integrator with piece-wise // constants coefficient lambda and mu. The boundary conditions are // implemented by marking only boundary attribute 1 as essential. After // assembly and finalizing we extract the corresponding sparse matrix A. Vector lambda(mesh->attributes.Max()); lambda = 1.0; lambda(0) = lambda(1)*50; PWConstCoefficient lambda_func(lambda); Vector mu(mesh->attributes.Max()); mu = 1.0; mu(0) = mu(1)*50; PWConstCoefficient mu_func(mu); BilinearForm *a = new BilinearForm(fespace); a->AddDomainIntegrator(new ElasticityIntegrator(lambda_func,mu_func)); dbg << "matrix ... " << flush; a->Assemble(); Array<int> ess_bdr(mesh->bdr_attributes.Max()); ess_bdr = 0; ess_bdr[0] = 1; a->EliminateEssentialBC(ess_bdr, x, *b); a->Finalize(); dbg << "done." << endl; const SparseMatrix &A = a->SpMat(); // 8. Define a simple symmetric Gauss-Seidel preconditioner and use it to // solve the system Ax=b with PCG. GSSmoother M(A); PCG(A, M, *b, x, 1, 500, 1e-8, 0.0); // 9. For non-NURBS meshes, make the mesh curved based on the finite element // space. This means that we define the mesh elements through a fespace // based transformation of the reference element. This allows us to save // the displaced mesh as a curved mesh when using high-order finite // element displacement field. We assume that the initial mesh (read from // the file) is not higher order curved mesh compared to the chosen FE // space. if (!mesh->NURBSext) mesh->SetNodalFESpace(fespace); // 10. Save the displaced mesh and the inverted solution (which gives the // backward displacements to the original grid). This output can be // viewed later using GLVis: "glvis -m displaced.mesh -g sol.gf". { GridFunction *nodes = mesh->GetNodes(); *nodes += x; x *= -1; ofstream mesh_ofs("displaced.mesh"); mesh_ofs.precision(8); mesh->Print(mesh_ofs); ofstream sol_ofs("sol.gf"); sol_ofs.precision(8); x.Save(sol_ofs); } // 11. (Optional) Send the above data by socket to a GLVis server. Use the // "n" and "b" keys in GLVis to visualize the displacements. /*char vishost[] = "localhost"; int visport = 19916; osockstream sol_sock(visport, vishost); sol_sock << "solution\n"; sol_sock.precision(8); mesh->Print(sol_sock); x.Save(sol_sock); sol_sock.send();*/ #if defined(VNC_ENABLED) mfem::emitMesh(mesh, &x); #endif // 12. Free the used memory. delete a; delete b; if (fec) { delete fespace; delete fec; } delete mesh; return 0; }
int main (int argc, char *argv[]) { SightInit(argc, argv, "ex3", "dbg.MFEM.ex3"); Mesh *mesh; if (argc == 1) { cerr << "\nUsage: ex3 <mesh_file>\n" << endl; return 1; } // 1. Read the mesh from the given mesh file. In this 3D example, we can // handle tetrahedral or hexahedral meshes with the same code. ifstream imesh(argv[1]); if (!imesh) { cerr << "\nCan not open mesh file: " << argv[1] << '\n' << endl; return 2; } mesh = new Mesh(imesh, 1, 1); imesh.close(); if (mesh -> Dimension() != 3) { cerr << "\nThis example requires a 3D mesh\n" << endl; return 3; } // 2. 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 50,000 // elements. { int ref_levels = (int)floor(log(50000./mesh->GetNE())/log(2.)/mesh->Dimension()); for (int l = 0; l < ref_levels; l++) mesh->UniformRefinement(); } // 3. Define a finite element space on the mesh. Here we use the lowest order // Nedelec finite elements, but we can easily swich to higher-order spaces // by changing the value of p. int p = 1; FiniteElementCollection *fec = new ND_FECollection(p, mesh -> Dimension()); FiniteElementSpace *fespace = new FiniteElementSpace(mesh, fec); dbg << "Number of unknowns: " << fespace->GetVSize() << endl; // 4. Set up the linear form b(.) which corresponds to the right-hand side // of the FEM linear system, which in this case is (f,phi_i) where f is // given by the function f_exact and phi_i are the basis functions in the // finite element fespace. VectorFunctionCoefficient f(3, f_exact); LinearForm *b = new LinearForm(fespace); b->AddDomainIntegrator(new VectorFEDomainLFIntegrator(f)); b->Assemble(); // 5. Define the solution vector x as a finite element grid function // corresponding to fespace. Initialize x by projecting the exact // solution. Note that only values from the boundary edges will be used // when eliminating the non-homogenious boundary condition to modify the // r.h.s. vector b. GridFunction x(fespace); VectorFunctionCoefficient E(3, E_exact); x.ProjectCoefficient(E); // 6. Set up the bilinear form corresponding to the EM diffusion operator // curl muinv curl + sigma I, by adding the curl-curl and the mass domain // integrators and finally imposing the non-homogeneous Dirichlet boundary // conditions. The boundary conditions are implemented by marking all the // boundary attributes from the mesh as essential (Dirichlet). After // assembly and finalizing we extract the corresponding sparse matrix A. Coefficient *muinv = new ConstantCoefficient(1.0); Coefficient *sigma = new ConstantCoefficient(1.0); BilinearForm *a = new BilinearForm(fespace); a->AddDomainIntegrator(new CurlCurlIntegrator(*muinv)); a->AddDomainIntegrator(new VectorFEMassIntegrator(*sigma)); a->Assemble(); Array<int> ess_bdr(mesh->bdr_attributes.Max()); ess_bdr = 1; a->EliminateEssentialBC(ess_bdr, x, *b); a->Finalize(); const SparseMatrix &A = a->SpMat(); // 7. Define a simple symmetric Gauss-Seidel preconditioner and use it to // solve the system Ax=b with PCG. GSSmoother M(A); x = 0.0; PCG(A, M, *b, x, 1, 500, 1e-12, 0.0); // 8. Compute and print the L^2 norm of the error. dbg << "\n|| E_h - E ||_{L^2} = " << x.ComputeL2Error(E) << '\n' << endl; // 9. Save the refined mesh and the solution. This output can be viewed // later using GLVis: "glvis -m refined.mesh -g sol.gf". { ofstream mesh_ofs("refined.mesh"); mesh_ofs.precision(8); mesh->Print(mesh_ofs); ofstream sol_ofs("sol.gf"); sol_ofs.precision(8); x.Save(sol_ofs); } // 10. (Optional) Send the solution by socket to a GLVis server. /*char vishost[] = "localhost"; int visport = 19916; osockstream sol_sock(visport, vishost); sol_sock << "solution\n"; sol_sock.precision(8); mesh->Print(sol_sock); x.Save(sol_sock); sol_sock.send();*/ #if defined(VNC_ENABLED) mfem::emitMesh(mesh, &x); #endif // 11. Free the used memory. delete a; delete sigma; delete muinv; delete b; delete fespace; delete fec; delete mesh; return 0; }
int main(int argc, char *argv[]) { // 1. Parse command-line options. const char *mesh_file = "../data/beam-tri.mesh"; int order = 1; bool static_cond = false; 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(&static_cond, "-sc", "--static-condensation", "-no-sc", "--no-static-condensation", "Enable static condensation."); 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 or hexahedral elements with the same code. Mesh *mesh = new Mesh(mesh_file, 1, 1); int dim = mesh->Dimension(); if (mesh->attributes.Max() < 2 || mesh->bdr_attributes.Max() < 2) { cerr << "\nInput mesh should have at least two materials and " << "two boundary attributes! (See schematic in ex2.cpp)\n" << endl; return 3; } // 3. Select the order of the finite element discretization space. For NURBS // meshes, we increase the order by degree elevation. if (mesh->NURBSext) { mesh->DegreeElevate(order, order); } // 4. 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 5,000 // elements. { int ref_levels = (int)floor(log(5000./mesh->GetNE())/log(2.)/dim); for (int l = 0; l < ref_levels; l++) { mesh->UniformRefinement(); } } // 5. Define a finite element space on the mesh. Here we use vector finite // elements, i.e. dim copies of a scalar finite element space. The vector // dimension is specified by the last argument of the FiniteElementSpace // constructor. For NURBS meshes, we use the (degree elevated) NURBS space // associated with the mesh nodes. FiniteElementCollection *fec; FiniteElementSpace *fespace; if (mesh->NURBSext) { fec = NULL; fespace = mesh->GetNodes()->FESpace(); } else { fec = new H1_FECollection(order, dim); fespace = new FiniteElementSpace(mesh, fec, dim); } cout << "Number of finite element unknowns: " << fespace->GetTrueVSize() << endl << "Assembling: " << flush; // 6. Determine the list of true (i.e. conforming) essential boundary dofs. // In this example, the boundary conditions are defined by marking only // boundary attribute 1 from the mesh as essential and converting it to a // list of true dofs. Array<int> ess_tdof_list, ess_bdr(mesh->bdr_attributes.Max()); ess_bdr = 0; ess_bdr[0] = 1; fespace->GetEssentialTrueDofs(ess_bdr, ess_tdof_list); // 7. Set up the linear form b(.) which corresponds to the right-hand side of // the FEM linear system. In this case, b_i equals the boundary integral // of f*phi_i where f represents a "pull down" force on the Neumann part // of the boundary and phi_i are the basis functions in the finite element // fespace. The force is defined by the VectorArrayCoefficient object f, // which is a vector of Coefficient objects. The fact that f is non-zero // on boundary attribute 2 is indicated by the use of piece-wise constants // coefficient for its last component. VectorArrayCoefficient f(dim); for (int i = 0; i < dim-1; i++) { f.Set(i, new ConstantCoefficient(0.0)); } { Vector pull_force(mesh->bdr_attributes.Max()); pull_force = 0.0; pull_force(1) = -1.0e-2; f.Set(dim-1, new PWConstCoefficient(pull_force)); } LinearForm *b = new LinearForm(fespace); b->AddBoundaryIntegrator(new VectorBoundaryLFIntegrator(f)); cout << "r.h.s. ... " << flush; b->Assemble(); // 8. Define the solution vector x as a finite element grid function // corresponding to fespace. Initialize x with initial guess of zero, // which satisfies the boundary conditions. GridFunction x(fespace); x = 0.0; // 9. Set up the bilinear form a(.,.) on the finite element space // corresponding to the linear elasticity integrator with piece-wise // constants coefficient lambda and mu. Vector lambda(mesh->attributes.Max()); lambda = 1.0; lambda(0) = lambda(1)*50; PWConstCoefficient lambda_func(lambda); Vector mu(mesh->attributes.Max()); mu = 1.0; mu(0) = mu(1)*50; PWConstCoefficient mu_func(mu); BilinearForm *a = new BilinearForm(fespace); a->AddDomainIntegrator(new ElasticityIntegrator(lambda_func,mu_func)); // 10. Assemble the bilinear form and the corresponding linear system, // applying any necessary transformations such as: eliminating boundary // conditions, applying conforming constraints for non-conforming AMR, // static condensation, etc. cout << "matrix ... " << flush; if (static_cond) { a->EnableStaticCondensation(); } a->Assemble(); SparseMatrix A; Vector B, X; a->FormLinearSystem(ess_tdof_list, x, *b, A, X, B); cout << "done." << endl; cout << "Size of linear system: " << A.Height() << endl; #ifndef MFEM_USE_SUITESPARSE // 11. Define a simple symmetric Gauss-Seidel preconditioner and use it to // solve the system Ax=b with PCG. GSSmoother M(A); PCG(A, M, B, X, 1, 500, 1e-8, 0.0); #else // 11. If MFEM was compiled with SuiteSparse, use UMFPACK to solve the system. UMFPackSolver umf_solver; umf_solver.Control[UMFPACK_ORDERING] = UMFPACK_ORDERING_METIS; umf_solver.SetOperator(A); umf_solver.Mult(B, X); #endif // 12. Recover the solution as a finite element grid function. a->RecoverFEMSolution(X, *b, x); // 13. For non-NURBS meshes, make the mesh curved based on the finite element // space. This means that we define the mesh elements through a fespace // based transformation of the reference element. This allows us to save // the displaced mesh as a curved mesh when using high-order finite // element displacement field. We assume that the initial mesh (read from // the file) is not higher order curved mesh compared to the chosen FE // space. if (!mesh->NURBSext) { mesh->SetNodalFESpace(fespace); } // 14. Save the displaced mesh and the inverted solution (which gives the // backward displacements to the original grid). This output can be // viewed later using GLVis: "glvis -m displaced.mesh -g sol.gf". { GridFunction *nodes = mesh->GetNodes(); *nodes += x; x *= -1; ofstream mesh_ofs("displaced.mesh"); mesh_ofs.precision(8); mesh->Print(mesh_ofs); ofstream sol_ofs("sol.gf"); sol_ofs.precision(8); x.Save(sol_ofs); } // 15. Send the above data by socket to a GLVis server. Use the "n" and "b" // keys in GLVis to visualize the displacements. if (visualization) { char vishost[] = "localhost"; int visport = 19916; socketstream sol_sock(vishost, visport); sol_sock.precision(8); sol_sock << "solution\n" << *mesh << x << flush; } // 16. Free the used memory. delete a; delete b; if (fec) { delete fespace; delete fec; } delete mesh; return 0; }
int main(int argc, char *argv[]) { // 1. Parse command-line options. int elem_type = 1; int ref_levels = 2; int amr = 0; int order = 2; bool always_snap = false; bool visualization = 1; OptionsParser args(argc, argv); args.AddOption(&elem_type, "-e", "--elem", "Type of elements to use: 0 - triangles, 1 - quads."); args.AddOption(&order, "-o", "--order", "Finite element order (polynomial degree)."); args.AddOption(&ref_levels, "-r", "--refine", "Number of times to refine the mesh uniformly."); args.AddOption(&amr, "-amr", "--refine-locally", "Additional local (non-conforming) refinement:" " 1 = refine around north pole, 2 = refine randomly."); args.AddOption(&visualization, "-vis", "--visualization", "-no-vis", "--no-visualization", "Enable or disable GLVis visualization."); args.AddOption(&always_snap, "-snap", "--always-snap", "-no-snap", "--snap-at-the-end", "If true, snap nodes to the sphere initially and after each refinement " "otherwise, snap only after the last refinement"); args.Parse(); if (!args.Good()) { args.PrintUsage(cout); return 1; } args.PrintOptions(cout); // 2. Generate an initial high-order (surface) mesh on the unit sphere. The // Mesh object represents a 2D mesh in 3 spatial dimensions. We first add // the elements and the vertices of the mesh, and then make it high-order // by specifying a finite element space for its nodes. int Nvert = 8, Nelem = 6; if (elem_type == 0) { Nvert = 6; Nelem = 8; } Mesh *mesh = new Mesh(2, Nvert, Nelem, 0, 3); if (elem_type == 0) // inscribed octahedron { const double tri_v[6][3] = { { 1, 0, 0}, { 0, 1, 0}, {-1, 0, 0}, { 0, -1, 0}, { 0, 0, 1}, { 0, 0, -1} }; const int tri_e[8][3] = { {0, 1, 4}, {1, 2, 4}, {2, 3, 4}, {3, 0, 4}, {1, 0, 5}, {2, 1, 5}, {3, 2, 5}, {0, 3, 5} }; for (int j = 0; j < Nvert; j++) { mesh->AddVertex(tri_v[j]); } for (int j = 0; j < Nelem; j++) { int attribute = j + 1; mesh->AddTriangle(tri_e[j], attribute); } mesh->FinalizeTriMesh(1, 1, true); } else // inscribed cube { const double quad_v[8][3] = { {-1, -1, -1}, {+1, -1, -1}, {+1, +1, -1}, {-1, +1, -1}, {-1, -1, +1}, {+1, -1, +1}, {+1, +1, +1}, {-1, +1, +1} }; const int quad_e[6][4] = { {3, 2, 1, 0}, {0, 1, 5, 4}, {1, 2, 6, 5}, {2, 3, 7, 6}, {3, 0, 4, 7}, {4, 5, 6, 7} }; for (int j = 0; j < Nvert; j++) { mesh->AddVertex(quad_v[j]); } for (int j = 0; j < Nelem; j++) { int attribute = j + 1; mesh->AddQuad(quad_e[j], attribute); } mesh->FinalizeQuadMesh(1, 1, true); } // Set the space for the high-order mesh nodes. H1_FECollection fec(order, mesh->Dimension()); FiniteElementSpace nodal_fes(mesh, &fec, mesh->SpaceDimension()); mesh->SetNodalFESpace(&nodal_fes); // 3. Refine the mesh while snapping nodes to the sphere. for (int l = 0; l <= ref_levels; l++) { if (l > 0) // for l == 0 just perform snapping { mesh->UniformRefinement(); } // Snap the nodes of the refined mesh back to sphere surface. if (always_snap || l == ref_levels) { SnapNodes(*mesh); } } if (amr == 1) { for (int l = 0; l < 5; l++) { mesh->RefineAtVertex(Vertex(0, 0, 1)); } SnapNodes(*mesh); } else if (amr == 2) { for (int l = 0; l < 4; l++) { mesh->RandomRefinement(0.5); // 50% probability } SnapNodes(*mesh); } // 4. Define a finite element space on the mesh. Here we use isoparametric // finite elements -- the same as the mesh nodes. FiniteElementSpace *fespace = new FiniteElementSpace(mesh, &fec); cout << "Number of unknowns: " << fespace->GetTrueVSize() << endl; // 5. Set up the linear form b(.) which corresponds to the right-hand side of // the FEM linear system, which in this case is (1,phi_i) where phi_i are // the basis functions in the finite element fespace. LinearForm *b = new LinearForm(fespace); ConstantCoefficient one(1.0); FunctionCoefficient rhs_coef (analytic_rhs); FunctionCoefficient sol_coef (analytic_solution); b->AddDomainIntegrator(new DomainLFIntegrator(rhs_coef)); b->Assemble(); // 6. Define the solution vector x as a finite element grid function // corresponding to fespace. Initialize x with initial guess of zero. GridFunction x(fespace); x = 0.0; // 7. Set up the bilinear form a(.,.) on the finite element space // corresponding to the Laplacian operator -Delta, by adding the Diffusion // and Mass domain integrators. BilinearForm *a = new BilinearForm(fespace); a->AddDomainIntegrator(new DiffusionIntegrator(one)); a->AddDomainIntegrator(new MassIntegrator(one)); // 8. Assemble the linear system, apply conforming constraints, etc. a->Assemble(); SparseMatrix A; Vector B, X; Array<int> empty_tdof_list; a->FormLinearSystem(empty_tdof_list, x, *b, A, X, B); #ifndef MFEM_USE_SUITESPARSE // 9. Define a simple symmetric Gauss-Seidel preconditioner and use it to // solve the system AX=B with PCG. GSSmoother M(A); PCG(A, M, B, X, 1, 200, 1e-12, 0.0); #else // 9. If MFEM was compiled with SuiteSparse, use UMFPACK to solve the system. UMFPackSolver umf_solver; umf_solver.Control[UMFPACK_ORDERING] = UMFPACK_ORDERING_METIS; umf_solver.SetOperator(A); umf_solver.Mult(B, X); #endif // 10. Recover the solution as a finite element grid function. a->RecoverFEMSolution(X, *b, x); // 11. Compute and print the L^2 norm of the error. cout<<"\nL2 norm of error: " << x.ComputeL2Error(sol_coef) << endl; // 12. Save the refined mesh and the solution. This output can be viewed // later using GLVis: "glvis -m sphere_refined.mesh -g sol.gf". { ofstream mesh_ofs("sphere_refined.mesh"); mesh_ofs.precision(8); mesh->Print(mesh_ofs); ofstream sol_ofs("sol.gf"); sol_ofs.precision(8); x.Save(sol_ofs); } // 13. Send the solution by socket to a GLVis server. if (visualization) { char vishost[] = "localhost"; int visport = 19916; socketstream sol_sock(vishost, visport); sol_sock.precision(8); sol_sock << "solution\n" << *mesh << x << flush; } // 14. Free the used memory. delete a; delete b; delete fespace; delete mesh; return 0; }
int main(int argc, char *argv[]) { // 1. Parse command-line options. const char *mesh_file = "../../data/fichera.mesh"; int order = sol_p; bool static_cond = false; bool visualization = 1; bool perf = true; OptionsParser args(argc, argv); args.AddOption(&mesh_file, "-m", "--mesh", "Mesh file to use."); args.AddOption(&order, "-o", "--order", "Finite element order (polynomial degree) or -1 for" " isoparametric space."); args.AddOption(&perf, "-perf", "--hpc-version", "-std", "--standard-version", "Enable high-performance, tensor-based, assembly."); args.AddOption(&static_cond, "-sc", "--static-condensation", "-no-sc", "--no-static-condensation", "Enable static condensation."); 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 = new Mesh(mesh_file, 1, 1); int dim = mesh->Dimension(); // 3. Check if the optimized version matches the given mesh if (perf) { cout << "High-performance version using integration rule with " << int_rule_t::qpts << " points ..." << endl; if (!mesh_t::MatchesGeometry(*mesh)) { cout << "The given mesh does not match the optimized 'geom' parameter.\n" << "Recompile with suitable 'geom' value." << endl; delete mesh; return 3; } else if (!mesh_t::MatchesNodes(*mesh)) { cout << "Switching the mesh curvature to match the " << "optimized value (order " << mesh_p << ") ..." << endl; mesh->SetCurvature(mesh_p, false, -1, Ordering::byNODES); } } // 4. 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 50,000 // elements. { int ref_levels = (int)floor(log(50000./mesh->GetNE())/log(2.)/dim); for (int l = 0; l < ref_levels; l++) { mesh->UniformRefinement(); } } // 5. Define a finite element space on the mesh. Here we use continuous // Lagrange finite elements of the specified order. If order < 1, we // instead use an isoparametric/isogeometric space. FiniteElementCollection *fec; if (order > 0) { fec = new H1_FECollection(order, dim); } else if (mesh->GetNodes()) { fec = mesh->GetNodes()->OwnFEC(); cout << "Using isoparametric FEs: " << fec->Name() << endl; } else { fec = new H1_FECollection(order = 1, dim); } FiniteElementSpace *fespace = new FiniteElementSpace(mesh, fec); cout << "Number of finite element unknowns: " << fespace->GetTrueVSize() << endl; // 6. Check if the optimized version matches the given space if (perf && !sol_fes_t::Matches(*fespace)) { cout << "The given order does not match the optimized parameter.\n" << "Recompile with suitable 'sol_p' value." << endl; delete fespace; delete fec; delete mesh; return 4; } // 7. Determine the list of true (i.e. conforming) essential boundary dofs. // In this example, the boundary conditions are defined by marking all // the boundary attributes from the mesh as essential (Dirichlet) and // converting them to a list of true dofs. Array<int> ess_tdof_list; if (mesh->bdr_attributes.Size()) { Array<int> ess_bdr(mesh->bdr_attributes.Max()); ess_bdr = 1; fespace->GetEssentialTrueDofs(ess_bdr, ess_tdof_list); } // 8. Set up the linear form b(.) which corresponds to the right-hand side of // the FEM linear system, which in this case is (1,phi_i) where phi_i are // the basis functions in the finite element fespace. LinearForm *b = new LinearForm(fespace); ConstantCoefficient one(1.0); b->AddDomainIntegrator(new DomainLFIntegrator(one)); b->Assemble(); // 9. Define the solution vector x as a finite element grid function // corresponding to fespace. Initialize x with initial guess of zero, // which satisfies the boundary conditions. GridFunction x(fespace); x = 0.0; // 10. Set up the bilinear form a(.,.) on the finite element space that will // hold the matrix corresponding to the Laplacian operator -Delta. BilinearForm *a = new BilinearForm(fespace); // 11. Assemble the bilinear form and the corresponding linear system, // applying any necessary transformations such as: eliminating boundary // conditions, applying conforming constraints for non-conforming AMR, // static condensation, etc. if (static_cond) { a->EnableStaticCondensation(); } cout << "Assembling the matrix ..." << flush; tic_toc.Clear(); tic_toc.Start(); // Pre-allocate sparsity assuming dense element matrices a->UsePrecomputedSparsity(); if (!perf) { // Standard assembly using a diffusion domain integrator a->AddDomainIntegrator(new DiffusionIntegrator(one)); a->Assemble(); } else { // High-performance assembly using the templated operator type oper_t a_oper(integ_t(coeff_t(1.0)), *fespace); a_oper.AssembleBilinearForm(*a); } tic_toc.Stop(); cout << " done, " << tic_toc.RealTime() << "s." << endl; SparseMatrix A; Vector B, X; a->FormLinearSystem(ess_tdof_list, x, *b, A, X, B); cout << "Size of linear system: " << A.Height() << endl; #ifndef MFEM_USE_SUITESPARSE // 12. Define a simple symmetric Gauss-Seidel preconditioner and use it to // solve the system A X = B with PCG. GSSmoother M(A); PCG(A, M, B, X, 1, 500, 1e-12, 0.0); #else // 12. If MFEM was compiled with SuiteSparse, use UMFPACK to solve the system. UMFPackSolver umf_solver; umf_solver.Control[UMFPACK_ORDERING] = UMFPACK_ORDERING_METIS; umf_solver.SetOperator(A); umf_solver.Mult(B, X); #endif // 13. Recover the solution as a finite element grid function. a->RecoverFEMSolution(X, *b, x); // 14. Save the refined mesh and the solution. This output can be viewed later // using GLVis: "glvis -m refined.mesh -g sol.gf". ofstream mesh_ofs("refined.mesh"); mesh_ofs.precision(8); mesh->Print(mesh_ofs); ofstream sol_ofs("sol.gf"); sol_ofs.precision(8); x.Save(sol_ofs); // 15. Send the solution by socket to a GLVis server. if (visualization) { char vishost[] = "localhost"; int visport = 19916; socketstream sol_sock(vishost, visport); sol_sock.precision(8); sol_sock << "solution\n" << *mesh << x << flush; } // 16. Free the used memory. delete a; delete b; delete fespace; if (order > 0) { delete fec; } delete mesh; return 0; }