📄 ex17.php
字号:
</div><div class ="fragment"><pre> const std::vector<Real>& JxW = fe->get_JxW(); </pre></div><div class = "comment">The element shape functions evaluated at the quadrature points.</div><div class ="fragment"><pre> const std::vector<std::vector<Real> >& phi = fe->get_phi(); </pre></div><div class = "comment">The element shape function gradients evaluated at the quadraturepoints.</div><div class ="fragment"><pre> const std::vector<std::vector<RealGradient> >& dphi = fe->get_dphi(); </pre></div><div class = "comment">A reference to the \p DofMap object for this system. The \p DofMapobject handles the index translation from node and element numbersto degree of freedom numbers.</div><div class ="fragment"><pre> const DofMap& dof_map = eigen_system.get_dof_map(); </pre></div><div class = "comment">The element mass and stiffness matrices.</div><div class ="fragment"><pre> DenseMatrix<Number> Me; DenseMatrix<Number> Ke; </pre></div><div class = "comment">This vector will hold the degree of freedom indices forthe element. These define where in the global systemthe element degrees of freedom get mapped.</div><div class ="fragment"><pre> std::vector<unsigned int> dof_indices; </pre></div><div class = "comment">Now we will loop over all the elements in the mesh.We will compute the element matrix contribution.<br><br></div><div class ="fragment"><pre> MeshBase::const_element_iterator el = mesh.elements_begin(); const MeshBase::const_element_iterator end_el = mesh.elements_end(); for ( ; el != end_el; ++el) {</pre></div><div class = "comment">Store a pointer to the element we are currentlyworking on. This allows for nicer syntax later.</div><div class ="fragment"><pre> const Elem* elem = *el; </pre></div><div class = "comment">Get the degree of freedom indices for thecurrent element. These define where in the globalmatrix and right-hand-side this element willcontribute to.</div><div class ="fragment"><pre> dof_map.dof_indices (elem, dof_indices); </pre></div><div class = "comment">Compute the element-specific data for the currentelement. This involves computing the location of thequadrature points (q_point) and the shape functions(phi, dphi) for the current element.</div><div class ="fragment"><pre> fe->reinit (elem); </pre></div><div class = "comment">Zero the element matrices beforesumming them. We use the resize member here becausethe number of degrees of freedom might have changed fromthe last element. Note that this will be the case if theelement type is different (i.e. the last element was atriangle, now we are on a quadrilateral).</div><div class ="fragment"><pre> Ke.resize (dof_indices.size(), dof_indices.size()); Me.resize (dof_indices.size(), dof_indices.size()); </pre></div><div class = "comment">Now loop over the quadrature points. This handlesthe numeric integration.<br><br>We will build the element matrix. This involvesa double loop to integrate the test funcions (i) againstthe trial functions (j).</div><div class ="fragment"><pre> for (unsigned int qp=0; qp<qrule.n_points(); qp++) for (unsigned int i=0; i<phi.size(); i++) for (unsigned int j=0; j<phi.size(); j++) { Me(i,j) += JxW[qp]*phi[i][qp]*phi[j][qp]; Ke(i,j) += JxW[qp]*(dphi[i][qp]*dphi[j][qp]); } </pre></div><div class = "comment">Finally, simply add the element contribution to theoverall matrices A and B.</div><div class ="fragment"><pre> matrix_A.add_matrix (Ke, dof_indices); matrix_B.add_matrix (Me, dof_indices); } // end of element loop #endif // HAVE_SLEPC /** * All done! */ return; } </pre></div><a name="nocomments"></a> <br><br><br> <h1> The program without comments: </h1> <pre> #include <B><FONT COLOR="#BC8F8F">"libmesh.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"mesh.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"mesh_generation.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"gmv_io.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"eigen_system.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"equation_systems.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"fe.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"quadrature_gauss.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"dense_matrix.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"sparse_matrix.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"numeric_vector.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"dof_map.h"</FONT></B> <B><FONT COLOR="#228B22">void</FONT></B> assemble_mass(EquationSystems& es, <B><FONT COLOR="#228B22">const</FONT></B> std::string& system_name); <B><FONT COLOR="#228B22">int</FONT></B> main (<B><FONT COLOR="#228B22">int</FONT></B> argc, <B><FONT COLOR="#228B22">char</FONT></B>** argv) { <B><FONT COLOR="#5F9EA0">libMesh</FONT></B>::init (argc, argv); #ifndef HAVE_SLEPC <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr << <B><FONT COLOR="#BC8F8F">"ERROR: This example requires libMesh to be\n"</FONT></B> << <B><FONT COLOR="#BC8F8F">"compiled with SLEPc eigen solvers support!"</FONT></B> << std::endl; <B><FONT COLOR="#A020F0">return</FONT></B> 0; #<B><FONT COLOR="#A020F0">else</FONT></B> { <B><FONT COLOR="#A020F0">if</FONT></B> (argc < 3) { <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr << <B><FONT COLOR="#BC8F8F">"\nUsage: "</FONT></B> << argv[0] << <B><FONT COLOR="#BC8F8F">" -n <number of eigen values>"</FONT></B> << std::endl; error(); } <B><FONT COLOR="#A020F0">else</FONT></B> { <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << <B><FONT COLOR="#BC8F8F">"Running "</FONT></B> << argv[0]; <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i=1; i<argc; i++) <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << <B><FONT COLOR="#BC8F8F">" "</FONT></B> << argv[i]; <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << std::endl << std::endl; } <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> dim = 2; <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> nev = std::atoi(argv[2]); Mesh mesh (dim); <B><FONT COLOR="#5F9EA0">MeshTools</FONT></B>::Generation::build_square (mesh, 20, 20, -1., 1., -1., 1., QUAD4); mesh.print_info(); EquationSystems equation_systems (mesh); EigenSystem & eigen_system = equation_systems.add_system<EigenSystem> (<B><FONT COLOR="#BC8F8F">"Eigensystem"</FONT></B>); { eigen_system.add_variable(<B><FONT COLOR="#BC8F8F">"p"</FONT></B>, FIRST); eigen_system.attach_assemble_function (assemble_mass); equation_systems.parameters.set<<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B>>(<B><FONT COLOR="#BC8F8F">"eigenpairs"</FONT></B>) = nev; equation_systems.parameters.set<<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B>>(<B><FONT COLOR="#BC8F8F">"basis vectors"</FONT></B>) = nev*3; eigen_system.eigen_solver->set_eigensolver_type(ARNOLDI); equation_systems.parameters.set<Real>(<B><FONT COLOR="#BC8F8F">"linear solver tolerance"</FONT></B>) = pow(TOLERANCE, 5./3.); equation_systems.parameters.set<<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B>> (<B><FONT COLOR="#BC8F8F">"linear solver maximum iterations"</FONT></B>) = 1000; eigen_system.set_eigenproblem_type(GHEP); equation_systems.init(); equation_systems.print_info(); } eigen_system.solve(); <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> nconv = eigen_system.get_n_converged(); <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << <B><FONT COLOR="#BC8F8F">"Number of converged eigenpairs: "</FONT></B> << nconv << <B><FONT COLOR="#BC8F8F">"\n"</FONT></B> << std::endl; <B><FONT COLOR="#A020F0">if</FONT></B> (nconv != 0) { eigen_system.get_eigenpair(nconv-1); <B><FONT COLOR="#228B22">char</FONT></B> buf[14]; sprintf (buf, <B><FONT COLOR="#BC8F8F">"out.gmv"</FONT></B>); GMVIO (mesh).write_equation_systems (buf, equation_systems); } <B><FONT COLOR="#A020F0">else</FONT></B> { <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << <B><FONT COLOR="#BC8F8F">"WARNING: Solver did not converge!\n"</FONT></B> << nconv << std::endl; } } #endif <I><FONT COLOR="#B22222">// HAVE_SLEPC</FONT></I> <B><FONT COLOR="#A020F0">return</FONT></B> libMesh::close (); } <B><FONT COLOR="#228B22">void</FONT></B> assemble_mass(EquationSystems& es, <B><FONT COLOR="#228B22">const</FONT></B> std::string& system_name) { assert (system_name == <B><FONT COLOR="#BC8F8F">"Eigensystem"</FONT></B>); #ifdef HAVE_SLEPC <B><FONT COLOR="#228B22">const</FONT></B> Mesh& mesh = es.get_mesh(); <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> dim = mesh.mesh_dimension(); EigenSystem & eigen_system = es.get_system<EigenSystem> (system_name); FEType fe_type = eigen_system.get_dof_map().variable_type(0); SparseMatrix<Number>& matrix_A = *eigen_system.matrix_A; SparseMatrix<Number>& matrix_B = *eigen_system.matrix_B; AutoPtr<FEBase> fe (FEBase::build(dim, fe_type)); QGauss qrule (dim, fe_type.default_quadrature_order()); fe->attach_quadrature_rule (&qrule); <B><FONT COLOR="#228B22">const</FONT></B> std::vector<Real>& JxW = fe->get_JxW(); <B><FONT COLOR="#228B22">const</FONT></B> std::vector<std::vector<Real> >& phi = fe->get_phi(); <B><FONT COLOR="#228B22">const</FONT></B> std::vector<std::vector<RealGradient> >& dphi = fe->get_dphi(); <B><FONT COLOR="#228B22">const</FONT></B> DofMap& dof_map = eigen_system.get_dof_map(); DenseMatrix<Number> Me; DenseMatrix<Number> Ke; <B><FONT COLOR="#5F9EA0">std</FONT></B>::vector<<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B>> dof_indices; <B><FONT COLOR="#5F9EA0">MeshBase</FONT></B>::const_element_iterator el = mesh.elements_begin(); <B><FONT COLOR="#228B22">const</FONT></B> MeshBase::const_element_iterator end_el = mesh.elements_end(); <B><FONT COLOR="#A020F0">for</FONT></B> ( ; el != end_el; ++el) { <B><FONT COLOR="#228B22">const</FONT></B> Elem* elem = *el; dof_map.dof_indices (elem, dof_indices); fe->reinit (elem); Ke.resize (dof_indices.size(), dof_indices.size()); Me.resize (dof_indices.size(), dof_indices.size()); <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> qp=0; qp<qrule.n_points(); qp++) <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> i=0; i<phi.size(); i++) <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> j=0; j<phi.size(); j++) { Me(i,j) += JxW[qp]*phi[i][qp]*phi[j][qp]; Ke(i,j) += JxW[qp]*(dphi[i][qp]*dphi[j][qp]); } matrix_A.add_matrix (Ke, dof_indices); matrix_B.add_matrix (Me, dof_indices); } <I><FONT COLOR="#B22222">// end of element loop</FONT></I> #endif <I><FONT COLOR="#B22222">// HAVE_SLEPC</FONT></I> <I><FONT COLOR="#B22222">/** * All done! */</FONT></I> <B><FONT COLOR="#A020F0">return</FONT></B>; } </pre> <a name="output"></a> <br><br><br> <h1> The console output of the program: </h1> <pre>**************************************************************** Running Example ./ex17-devel -n 5***************************************************************Running ./ex17-devel -n 5 Mesh Information: mesh_dimension()=2 spatial_dimension()=3 n_nodes()=441 n_elem()=400 n_local_elem()=400 n_active_elem()=400 n_subdomains()=1 n_processors()=1 processor_id()=0 EquationSystems n_systems()=1 System "Eigensystem" Type "Eigen" Variables="p" Finite Element Types="LAGRANGE" Approximation Orders="FIRST" n_dofs()=441 n_local_dofs()=441 n_constrained_dofs()=0 n_vectors()=0Number of converged eigenpairs: 5**************************************************************** Done Running Example ./ex17-devel -n 5***************************************************************</pre></div><?php make_footer() ?></body></html><?php if (0) { ?>\#Local Variables:\#mode: html\#End:<?php } ?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -