📄 ex5.php
字号:
<div class = "comment">All done!</div><div class ="fragment"><pre> return; }</pre></div><a name="nocomments"></a> <br><br><br> <h1> The program without comments: </h1> <pre> #include <iostream> #include <sstream> #include <algorithm> #include <math.h> #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">"linear_implicit_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.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"quadrature_rules.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">"dense_matrix.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"dense_vector.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"dof_map.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"elem.h"</FONT></B> <B><FONT COLOR="#228B22">void</FONT></B> assemble_poisson(EquationSystems& es, <B><FONT COLOR="#228B22">const</FONT></B> std::string& system_name); Real exact_solution (<B><FONT COLOR="#228B22">const</FONT></B> Real x, <B><FONT COLOR="#228B22">const</FONT></B> Real y, <B><FONT COLOR="#228B22">const</FONT></B> Real z = 0.); QuadratureType quad_type=INVALID_Q_RULE; <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); { <B><FONT COLOR="#A020F0">if</FONT></B> (argc < 3) { <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr << <B><FONT COLOR="#BC8F8F">"Usage: "</FONT></B> << argv[0] << <B><FONT COLOR="#BC8F8F">" -q n"</FONT></B> << std::endl; <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr << <B><FONT COLOR="#BC8F8F">" where n stands for:"</FONT></B> << std::endl; <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> n=0; n<QuadratureRules::num_valid_elem_rules; n++) <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr << <B><FONT COLOR="#BC8F8F">" "</FONT></B> << QuadratureRules::valid_elem_rules[n] << <B><FONT COLOR="#BC8F8F">" "</FONT></B> << QuadratureRules::name(QuadratureRules::valid_elem_rules[n]) << std::endl; <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr << 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; } quad_type = static_cast<QuadratureType>(std::atoi(argv[2])); <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> dim=3; Mesh mesh (dim); <B><FONT COLOR="#5F9EA0">MeshTools</FONT></B>::Generation::build_cube (mesh, 16, 16, 16, -1., 1., -1., 1., -1., 1., HEX8); mesh.print_info(); EquationSystems equation_systems (mesh); { equation_systems.add_system<LinearImplicitSystem> (<B><FONT COLOR="#BC8F8F">"Poisson"</FONT></B>); equation_systems.get_system(<B><FONT COLOR="#BC8F8F">"Poisson"</FONT></B>).add_variable(<B><FONT COLOR="#BC8F8F">"u"</FONT></B>, FIRST); equation_systems.get_system(<B><FONT COLOR="#BC8F8F">"Poisson"</FONT></B>).attach_assemble_function (assemble_poisson); equation_systems.init(); equation_systems.print_info(); } equation_systems.get_system(<B><FONT COLOR="#BC8F8F">"Poisson"</FONT></B>).solve(); <B><FONT COLOR="#5F9EA0">std</FONT></B>::ostringstream f_name; f_name << <B><FONT COLOR="#BC8F8F">"out_"</FONT></B> << quad_type << <B><FONT COLOR="#BC8F8F">".gmv"</FONT></B>; GMVIO(mesh).write_equation_systems (f_name.str(), equation_systems); } <B><FONT COLOR="#A020F0">return</FONT></B> libMesh::close (); } <B><FONT COLOR="#228B22">void</FONT></B> assemble_poisson(EquationSystems& es, <B><FONT COLOR="#228B22">const</FONT></B> std::string& system_name) { assert (system_name == <B><FONT COLOR="#BC8F8F">"Poisson"</FONT></B>); <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(); LinearImplicitSystem& system = es.get_system<LinearImplicitSystem>(<B><FONT COLOR="#BC8F8F">"Poisson"</FONT></B>); <B><FONT COLOR="#228B22">const</FONT></B> DofMap& dof_map = system.get_dof_map(); FEType fe_type = dof_map.variable_type(0); AutoPtr<FEBase> fe (FEBase::build(dim, fe_type)); AutoPtr<QBase> qrule(QBase::build(quad_type, dim, THIRD)); fe->attach_quadrature_rule (qrule.get()); AutoPtr<FEBase> fe_face (FEBase::build(dim, fe_type)); AutoPtr<QBase> qface (QBase::build(quad_type, dim-1, THIRD)); fe_face->attach_quadrature_rule (qface.get()); <B><FONT COLOR="#228B22">const</FONT></B> std::vector<Real>& JxW = fe->get_JxW(); <B><FONT COLOR="#228B22">const</FONT></B> std::vector<Point>& q_point = fe->get_xyz(); <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(); DenseMatrix<Number> Ke; DenseVector<Number> Fe; <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()); Fe.resize (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++) Ke(i,j) += JxW[qp]*(dphi[i][qp]*dphi[j][qp]); <B><FONT COLOR="#228B22">const</FONT></B> Real x = q_point[qp](0); <B><FONT COLOR="#228B22">const</FONT></B> Real y = q_point[qp](1); <B><FONT COLOR="#228B22">const</FONT></B> Real z = q_point[qp](2); <B><FONT COLOR="#228B22">const</FONT></B> Real eps = 1.e-3; <B><FONT COLOR="#228B22">const</FONT></B> Real uxx = (exact_solution(x-eps,y,z) + exact_solution(x+eps,y,z) + -2.*exact_solution(x,y,z))/eps/eps; <B><FONT COLOR="#228B22">const</FONT></B> Real uyy = (exact_solution(x,y-eps,z) + exact_solution(x,y+eps,z) + -2.*exact_solution(x,y,z))/eps/eps; <B><FONT COLOR="#228B22">const</FONT></B> Real uzz = (exact_solution(x,y,z-eps) + exact_solution(x,y,z+eps) + -2.*exact_solution(x,y,z))/eps/eps; <B><FONT COLOR="#228B22">const</FONT></B> Real fxy = - (uxx + uyy + ((dim==2) ? 0. : uzz)); <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++) Fe(i) += JxW[qp]*fxy*phi[i][qp]; } { <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> side=0; side<elem->n_sides(); side++) <B><FONT COLOR="#A020F0">if</FONT></B> (elem->neighbor(side) == NULL) { <B><FONT COLOR="#228B22">const</FONT></B> std::vector<std::vector<Real> >& phi_face = fe_face->get_phi(); <B><FONT COLOR="#228B22">const</FONT></B> std::vector<Real>& JxW_face = fe_face->get_JxW(); <B><FONT COLOR="#228B22">const</FONT></B> std::vector<Point >& qface_point = fe_face->get_xyz(); fe_face->reinit(elem, side); <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> qp=0; qp<qface->n_points(); qp++) { <B><FONT COLOR="#228B22">const</FONT></B> Real xf = qface_point[qp](0); <B><FONT COLOR="#228B22">const</FONT></B> Real yf = qface_point[qp](1); <B><FONT COLOR="#228B22">const</FONT></B> Real zf = qface_point[qp](2); <B><FONT COLOR="#228B22">const</FONT></B> Real penalty = 1.e10; <B><FONT COLOR="#228B22">const</FONT></B> Real value = exact_solution(xf, yf, zf); <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_face.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_face.size(); j++) Ke(i,j) += JxW_face[qp]*penalty*phi_face[i][qp]*phi_face[j][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_face.size(); i++) Fe(i) += JxW_face[qp]*penalty*value*phi_face[i][qp]; } <I><FONT COLOR="#B22222">// end face quadrature point loop </FONT></I> } <I><FONT COLOR="#B22222">// end if (elem->neighbor(side) == NULL)</FONT></I> } <I><FONT COLOR="#B22222">// end boundary condition section </FONT></I> system.matrix->add_matrix (Ke, dof_indices); system.rhs->add_vector (Fe, dof_indices); } <I><FONT COLOR="#B22222">// end of element loop</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 ./ex5-devel -q 0*************************************************************** Running ./ex5-devel -q 0 Mesh Information: mesh_dimension()=3 spatial_dimension()=3 n_nodes()=4913 n_elem()=4096 n_local_elem()=4096 n_active_elem()=4096 n_subdomains()=1 n_processors()=1 processor_id()=0 EquationSystems n_systems()=1 System "Poisson" Type "LinearImplicit" Variables="u" Finite Element Types="LAGRANGE" Approximation Orders="FIRST" n_dofs()=4913 n_local_dofs()=4913 n_constrained_dofs()=0 n_vectors()=1 **************************************************************** Done Running Example ./ex5-devel -q 0***************************************************************</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 + -