⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex5.php

📁 一个用来实现偏微分方程中网格的计算库
💻 PHP
📖 第 1 页 / 共 3 页
字号:
<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 &lt;iostream&gt;  #include &lt;sstream&gt;   #include &lt;algorithm&gt;  #include &lt;math.h&gt;    #include <B><FONT COLOR="#BC8F8F">&quot;libmesh.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;mesh.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;mesh_generation.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;gmv_io.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;linear_implicit_system.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;equation_systems.h&quot;</FONT></B>    #include <B><FONT COLOR="#BC8F8F">&quot;fe.h&quot;</FONT></B>    #include <B><FONT COLOR="#BC8F8F">&quot;quadrature.h&quot;</FONT></B>    #include <B><FONT COLOR="#BC8F8F">&quot;quadrature_rules.h&quot;</FONT></B>    #include <B><FONT COLOR="#BC8F8F">&quot;sparse_matrix.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;numeric_vector.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;dense_matrix.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;dense_vector.h&quot;</FONT></B>    #include <B><FONT COLOR="#BC8F8F">&quot;dof_map.h&quot;</FONT></B>    #include <B><FONT COLOR="#BC8F8F">&quot;elem.h&quot;</FONT></B>                <B><FONT COLOR="#228B22">void</FONT></B> assemble_poisson(EquationSystems&amp; es,                        <B><FONT COLOR="#228B22">const</FONT></B> std::string&amp; 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 &lt; 3)        {  	<B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;Usage: &quot;</FONT></B> &lt;&lt; argv[0] &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot; -q n&quot;</FONT></B>  		  &lt;&lt; std::endl;  	<B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;  where n stands for:&quot;</FONT></B> &lt;&lt; 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&lt;QuadratureRules::num_valid_elem_rules; n++)  	  <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;  &quot;</FONT></B> &lt;&lt; QuadratureRules::valid_elem_rules[n] &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;    &quot;</FONT></B>   		    &lt;&lt; QuadratureRules::name(QuadratureRules::valid_elem_rules[n])  		    &lt;&lt; std::endl;  	  	<B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr &lt;&lt; std::endl;  	  	error();        }                  <B><FONT COLOR="#A020F0">else</FONT></B>         {  	<B><FONT COLOR="#5F9EA0">std</FONT></B>::cout &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;Running &quot;</FONT></B> &lt;&lt; argv[0];  	  	<B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i=1; i&lt;argc; i++)  	  <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot; &quot;</FONT></B> &lt;&lt; argv[i];  	  	<B><FONT COLOR="#5F9EA0">std</FONT></B>::cout &lt;&lt; std::endl &lt;&lt; std::endl;        }              quad_type = static_cast&lt;QuadratureType&gt;(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&lt;LinearImplicitSystem&gt; (<B><FONT COLOR="#BC8F8F">&quot;Poisson&quot;</FONT></B>);                equation_systems.get_system(<B><FONT COLOR="#BC8F8F">&quot;Poisson&quot;</FONT></B>).add_variable(<B><FONT COLOR="#BC8F8F">&quot;u&quot;</FONT></B>, FIRST);          equation_systems.get_system(<B><FONT COLOR="#BC8F8F">&quot;Poisson&quot;</FONT></B>).attach_assemble_function (assemble_poisson);          equation_systems.init();                equation_systems.print_info();      }        equation_systems.get_system(<B><FONT COLOR="#BC8F8F">&quot;Poisson&quot;</FONT></B>).solve();        <B><FONT COLOR="#5F9EA0">std</FONT></B>::ostringstream f_name;      f_name &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;out_&quot;</FONT></B> &lt;&lt; quad_type &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;.gmv&quot;</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&amp; es,                        <B><FONT COLOR="#228B22">const</FONT></B> std::string&amp; system_name)  {    assert (system_name == <B><FONT COLOR="#BC8F8F">&quot;Poisson&quot;</FONT></B>);      <B><FONT COLOR="#228B22">const</FONT></B> Mesh&amp; 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&amp; system = es.get_system&lt;LinearImplicitSystem&gt;(<B><FONT COLOR="#BC8F8F">&quot;Poisson&quot;</FONT></B>);        <B><FONT COLOR="#228B22">const</FONT></B> DofMap&amp; dof_map = system.get_dof_map();        FEType fe_type = dof_map.variable_type(0);          AutoPtr&lt;FEBase&gt; fe (FEBase::build(dim, fe_type));            AutoPtr&lt;QBase&gt; qrule(QBase::build(quad_type, dim, THIRD));            fe-&gt;attach_quadrature_rule (qrule.get());          AutoPtr&lt;FEBase&gt; fe_face (FEBase::build(dim, fe_type));            AutoPtr&lt;QBase&gt;  qface (QBase::build(quad_type,  				      dim-1,   				      THIRD));  	              fe_face-&gt;attach_quadrature_rule (qface.get());  	                <B><FONT COLOR="#228B22">const</FONT></B> std::vector&lt;Real&gt;&amp; JxW = fe-&gt;get_JxW();        <B><FONT COLOR="#228B22">const</FONT></B> std::vector&lt;Point&gt;&amp; q_point = fe-&gt;get_xyz();        <B><FONT COLOR="#228B22">const</FONT></B> std::vector&lt;std::vector&lt;Real&gt; &gt;&amp; phi = fe-&gt;get_phi();        <B><FONT COLOR="#228B22">const</FONT></B> std::vector&lt;std::vector&lt;RealGradient&gt; &gt;&amp; dphi = fe-&gt;get_dphi();          DenseMatrix&lt;Number&gt; Ke;    DenseVector&lt;Number&gt; Fe;        <B><FONT COLOR="#5F9EA0">std</FONT></B>::vector&lt;<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B>&gt; 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-&gt;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&lt;qrule-&gt;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&lt;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&lt;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&lt;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&lt;elem-&gt;n_sides(); side++)  	  <B><FONT COLOR="#A020F0">if</FONT></B> (elem-&gt;neighbor(side) == NULL)  	    {	        	      <B><FONT COLOR="#228B22">const</FONT></B> std::vector&lt;std::vector&lt;Real&gt; &gt;&amp; phi_face    = fe_face-&gt;get_phi();  	      <B><FONT COLOR="#228B22">const</FONT></B> std::vector&lt;Real&gt;&amp;               JxW_face    = fe_face-&gt;get_JxW();	        	      <B><FONT COLOR="#228B22">const</FONT></B> std::vector&lt;Point &gt;&amp;             qface_point = fe_face-&gt;get_xyz();  	        	        	      fe_face-&gt;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&lt;qface-&gt;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&lt;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&lt;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&lt;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-&gt;neighbor(side) == NULL)</FONT></I>        } <I><FONT COLOR="#B22222">// end boundary condition section	  </FONT></I>                                                system.matrix-&gt;add_matrix (Ke, dof_indices);        system.rhs-&gt;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 + -