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

📄 ex4.php

📁 一个用来实现偏微分方程中网格的计算库
💻 PHP
📖 第 1 页 / 共 4 页
字号:
<div class = "comment">Now we will build the element matrix.  This involvesa double loop to integrate the test funcions (i) againstthe trial functions (j).<br><br>We have split the numeric integration into two loopsso that we can log the matrix and right-hand-sidecomputation seperately.<br><br>Now start logging the element matrix computation</div><div class ="fragment"><pre>              perf_log.start_event ("Ke");                      for (unsigned int qp=0; qp&lt;qrule.n_points(); qp++)        	for (unsigned int i=0; i&lt;phi.size(); i++)        	  for (unsigned int j=0; j&lt;phi.size(); j++)        	    Ke(i,j) += JxW[qp]*(dphi[i][qp]*dphi[j][qp]);        	            </pre></div><div class = "comment">Stop logging the matrix computation</div><div class ="fragment"><pre>              perf_log.stop_event ("Ke");        </pre></div><div class = "comment">Now we build the element right-hand-side contribution.This involves a single loop in which we integrate the"forcing function" in the PDE against the test functions.<br><br>Start logging the right-hand-side computation</div><div class ="fragment"><pre>              perf_log.start_event ("Fe");                            for (unsigned int qp=0; qp&lt;qrule.n_points(); qp++)        	{</pre></div><div class = "comment">fxy is the forcing function for the Poisson equation.In this case we set fxy to be a finite differenceLaplacian approximation to the (known) exact solution.<br><br>We will use the second-order accurate FD Laplacianapproximation, which in 2D on a structured grid is<br><br>u_xx + u_yy = (u(i-1,j) + u(i+1,j) +u(i,j-1) + u(i,j+1) +-4*u(i,j))/h^2<br><br>Since the value of the forcing function depends onlyon the location of the quadrature point (q_point[qp])we will compute it here, outside of the i-loop	  </div><div class ="fragment"><pre>                  const Real x = q_point[qp](0);        	  const Real y = q_point[qp](1);        	  const Real z = q_point[qp](2);        	  const Real eps = 1.e-3;                	  const Real uxx = (exact_solution(x-eps,y,z) +        			    exact_solution(x+eps,y,z) +        			    -2.*exact_solution(x,y,z))/eps/eps;        	              	  const Real uyy = (exact_solution(x,y-eps,z) +        			    exact_solution(x,y+eps,z) +        			    -2.*exact_solution(x,y,z))/eps/eps;        	          	  const Real uzz = (exact_solution(x,y,z-eps) +        			    exact_solution(x,y,z+eps) +        			    -2.*exact_solution(x,y,z))/eps/eps;                          Real fxy;                  if(dim==1)                  {</pre></div><div class = "comment">In 1D, compute the rhs by differentiating theexact solution twice.</div><div class ="fragment"><pre>                    const Real pi = libMesh::pi;                    fxy = (0.25*pi*pi)*sin(.5*pi*x);                  }                  else                  {        	    fxy = - (uxx + uyy + ((dim==2) ? 0. : uzz));                  }         </pre></div><div class = "comment">Add the RHS contribution</div><div class ="fragment"><pre>                  for (unsigned int i=0; i&lt;phi.size(); i++)        	    Fe(i) += JxW[qp]*fxy*phi[i][qp];	          	}              </pre></div><div class = "comment">Stop logging the right-hand-side computation</div><div class ="fragment"><pre>              perf_log.stop_event ("Fe");        </pre></div><div class = "comment">At this point the interior element integration hasbeen completed.  However, we have not yet addressedboundary conditions.  For this example we will onlyconsider simple Dirichlet boundary conditions imposedvia the penalty method. This is discussed at length inexample 3.</div><div class ="fragment"><pre>              {        	</pre></div><div class = "comment">Start logging the boundary condition computation</div><div class ="fragment"><pre>                perf_log.start_event ("BCs");        </pre></div><div class = "comment">The following loops over the sides of the element.If the element has no neighbor on a side then thatside MUST live on a boundary of the domain.</div><div class ="fragment"><pre>                for (unsigned int side=0; side&lt;elem-&gt;n_sides(); side++)        	  if (elem-&gt;neighbor(side) == NULL)        	    {                    </pre></div><div class = "comment">The penalty value.  \frac{1}{\epsilon}in the discussion above.</div><div class ="fragment"><pre>                      const Real penalty = 1.e10;        </pre></div><div class = "comment">The value of the shape functions at the quadraturepoints.</div><div class ="fragment"><pre>                      const std::vector&lt;std::vector&lt;Real&gt; &gt;&  phi_face = fe_face-&gt;get_phi();        </pre></div><div class = "comment">The Jacobian * Quadrature Weight at the quadraturepoints on the face.</div><div class ="fragment"><pre>                      const std::vector&lt;Real&gt;& JxW_face = fe_face-&gt;get_JxW();        </pre></div><div class = "comment">The XYZ locations (in physical space) of thequadrature points on the face.  This is wherewe will interpolate the boundary value function.</div><div class ="fragment"><pre>                      const std::vector&lt;Point &gt;& qface_point = fe_face-&gt;get_xyz();        </pre></div><div class = "comment">Compute the shape function values on the elementface.</div><div class ="fragment"><pre>                      fe_face-&gt;reinit(elem, side);        </pre></div><div class = "comment">Loop over the face quadrature points for integration.</div><div class ="fragment"><pre>                      for (unsigned int qp=0; qp&lt;qface.n_points(); qp++)                      {</pre></div><div class = "comment">The location on the boundary of the currentface quadrature point.</div><div class ="fragment"><pre>                        const Real xf = qface_point[qp](0);                        const Real yf = qface_point[qp](1);                        const Real zf = qface_point[qp](2);                </pre></div><div class = "comment">The boundary value.</div><div class ="fragment"><pre>                        const Real value = exact_solution(xf, yf, zf);        </pre></div><div class = "comment">Matrix contribution of the L2 projection. </div><div class ="fragment"><pre>                        for (unsigned int i=0; i&lt;phi_face.size(); i++)                          for (unsigned int j=0; j&lt;phi_face.size(); j++)                            Ke(i,j) += JxW_face[qp]*penalty*phi_face[i][qp]*phi_face[j][qp];        </pre></div><div class = "comment">Right-hand-side contribution of the L2projection.</div><div class ="fragment"><pre>                        for (unsigned int i=0; i&lt;phi_face.size(); i++)                          Fe(i) += JxW_face[qp]*penalty*value*phi_face[i][qp];                      }                     }                            	</pre></div><div class = "comment">Stop logging the boundary condition computation</div><div class ="fragment"><pre>                perf_log.stop_event ("BCs");              }                       </pre></div><div class = "comment">The element matrix and right-hand-side are now builtfor this element.  Add them to the global matrix andright-hand-side vector.  The \p PetscMatrix::add_matrix()and \p PetscVector::add_vector() members do this for us.Start logging the insertion of the local (element)matrix and vector into the global matrix and vector</div><div class ="fragment"><pre>              perf_log.start_event ("matrix insertion");                            system.matrix-&gt;add_matrix (Ke, dof_indices);              system.rhs-&gt;add_vector    (Fe, dof_indices);        </pre></div><div class = "comment">Start logging the insertion of the local (element)matrix and vector into the global matrix and vector</div><div class ="fragment"><pre>              perf_log.stop_event ("matrix insertion");            }        </pre></div><div class = "comment">That's it.  We don't need to do anything else to thePerfLog.  When it goes out of scope (at this function return)it will print its log to the screen. Pretty easy, huh?</div><div class ="fragment"><pre>        }</pre></div><a name="nocomments"></a> <br><br><br> <h1> The program without comments: </h1> <pre>       #include &lt;iostream&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;gnuplot_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_gauss.h&quot;</FONT></B>    #include <B><FONT COLOR="#BC8F8F">&quot;dof_map.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;perf_log.h&quot;</FONT></B>    #include <B><FONT COLOR="#BC8F8F">&quot;elem.h&quot;</FONT></B>    #include <B><FONT COLOR="#BC8F8F">&quot;string_to_enum.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;getpot.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 = 0.,  		     <B><FONT COLOR="#228B22">const</FONT></B> Real z = 0.);    <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);          {      GetPot command_line (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:\n&quot;</FONT></B>  		  &lt;&lt;<B><FONT COLOR="#BC8F8F">&quot;\t &quot;</FONT></B> &lt;&lt; argv[0] &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot; -d 2(3)&quot;</FONT></B> &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot; -n 15&quot;</FONT></B>  		  &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;        }              <B><FONT COLOR="#228B22">int</FONT></B> dim = 2;      <B><FONT COLOR="#A020F0">if</FONT></B> ( command_line.search(1, <B><FONT COLOR="#BC8F8F">&quot;-d&quot;</FONT></B>) )        dim = command_line.next(dim);            <B><FONT COLOR="#228B22">int</FONT></B> ps = 15;      <B><FONT COLOR="#A020F0">if</FONT></B> ( command_line.search(1, <B><FONT COLOR="#BC8F8F">&quot;-n&quot;</FONT></B>) )        ps = command_line.next(ps);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -