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

📄 ex0.php

📁 一个用来实现偏微分方程中网格的计算库
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<div class = "comment">The element shape functions evaluated at the quadrature points.</div><div class ="fragment"><pre>          const std::vector&lt;std::vector&lt;Real&gt; &gt;& phi = fe-&gt;get_phi();        </pre></div><div class = "comment">The element shape function gradients evaluated at the quadrature points.</div><div class ="fragment"><pre>          const std::vector&lt;std::vector&lt;RealGradient&gt; &gt;& dphi = fe-&gt;get_dphi();        </pre></div><div class = "comment">Declare a dense matrix and dense vector to hold the element matrixand right-hand-side contribution</div><div class ="fragment"><pre>          DenseMatrix&lt;Number&gt; Ke;          DenseVector&lt;Number&gt; Fe;        </pre></div><div class = "comment">This vector will hold the degree of freedom indices for the element.These define where in the global system the element degrees of freedomget mapped.</div><div class ="fragment"><pre>          std::vector&lt;unsigned int&gt; dof_indices;        </pre></div><div class = "comment">We now loop over all the active elements in the mesh in order to calculatethe matrix and right-hand-side contribution from each element. Use aconst_element_iterator to loop over the elements. We makeel_end const as it is used only for the stopping condition of the loop.</div><div class ="fragment"><pre>          MeshBase::const_element_iterator el     = mesh.active_elements_begin();          const MeshBase::const_element_iterator el_end = mesh.active_elements_end();        </pre></div><div class = "comment">Note that ++el is preferred to el++ when using loops with iterators</div><div class ="fragment"><pre>          for( ; el != el_end; ++el)          {</pre></div><div class = "comment">It is convenient to store a pointer to the current element</div><div class ="fragment"><pre>            const Elem* elem = *el;        </pre></div><div class = "comment">Get the degree of freedom indices for the current element. These define where in the global matrix and right-hand-side this element will contribute 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 current element. This involves computing the location of the quadrature points (q_point) and the shape functions (phi, dphi) for the current element.</div><div class ="fragment"><pre>            fe-&gt;reinit(elem);        </pre></div><div class = "comment">Store the number of local degrees of freedom contained in this element</div><div class ="fragment"><pre>            const int n_dofs = dof_indices.size();        </pre></div><div class = "comment">We resize and zero out Ke and Fe (resize() also clears the matrix andvector). In this example, all elements in the mesh are EDGE3's, so Ke will always be 3x3, and Fe will always be 3x1. If the mesh containeddifferent element types, then the size of Ke and Fe would change.</div><div class ="fragment"><pre>            Ke.resize(n_dofs, n_dofs);            Fe.resize(n_dofs);                </pre></div><div class = "comment">Now loop over quadrature points to handle numerical integration</div><div class ="fragment"><pre>            for(unsigned int qp=0; qp&lt;qrule.n_points(); qp++)            {</pre></div><div class = "comment">Now build the element matrix and right-hand-side using loops tointegrate the test functions (i) against the trial functions (j).</div><div class ="fragment"><pre>              for(unsigned int i=0; i&lt;phi.size(); i++)              {                Fe(i) += JxW[qp]*phi[i][qp];                        for(unsigned int j=0; j&lt;phi.size(); j++)                {                  Ke(i,j) += JxW[qp]*(1.e-3*dphi[i][qp]*dphi[j][qp] +                                              phi[i][qp]*phi[j][qp]);                }              }            }                </pre></div><div class = "comment">At this point we have completed the matrix and RHS summation. Thefinal step is to apply boundary conditions, which in this case aresimple Dirichlet conditions with u(0) = u(1) = 0.<br><br>Define the penalty parameter used to enforce the BC's</div><div class ="fragment"><pre>            double penalty = 1.e10;        </pre></div><div class = "comment">Loop over the sides of this element. For a 1D element, the "sides"are defined as the nodes on each edge of the element, i.e. 1D elementshave 2 sides.</div><div class ="fragment"><pre>            for(unsigned int s=0; s&lt;elem-&gt;n_sides(); s++)            {</pre></div><div class = "comment">If this element has a NULL neighbor, then it is on the edge of themesh and we need to enforce a boundary condition using the penaltymethod.</div><div class ="fragment"><pre>              if(elem-&gt;neighbor(s) == NULL)              {                Ke(s,s) += penalty;                Fe(s)   += 0*penalty;              }            }        </pre></div><div class = "comment">This is a function call that is necessary when using adaptivemesh refinement. See Example 10 for more details.</div><div class ="fragment"><pre>            dof_map.constrain_element_matrix_and_vector (Ke, Fe, dof_indices);        </pre></div><div class = "comment">Add Ke and Fe to the global matrix and right-hand-side.</div><div class ="fragment"><pre>            system.matrix-&gt;add_matrix(Ke, dof_indices);            system.rhs-&gt;add_vector(Fe, dof_indices);          }        #endif // #ifdef ENABLE_AMR        }</pre></div><a name="nocomments"></a> <br><br><br> <h1> The program without comments: </h1> <pre>     #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;edge_edge3.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;gnuplot_io.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;equation_systems.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;linear_implicit_system.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;sparse_matrix.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;dof_map.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;error_vector.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;kelly_error_estimator.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;mesh_refinement.h&quot;</FONT></B>      <B><FONT COLOR="#228B22">void</FONT></B> assemble_1D(EquationSystems&amp; es, <B><FONT COLOR="#228B22">const</FONT></B> std::string&amp; 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 ENABLE_AMR    <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;ERROR: This example requires libMesh to be\n&quot;</FONT></B>              &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;compiled with AMR support!&quot;</FONT></B>              &lt;&lt; std::endl;    <B><FONT COLOR="#A020F0">return</FONT></B> 0;  #<B><FONT COLOR="#A020F0">else</FONT></B>      {      <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> dim = 1;      Mesh mesh(dim);        <B><FONT COLOR="#5F9EA0">MeshTools</FONT></B>::Generation::build_line(mesh,4,0.,1.,EDGE3);        EquationSystems equation_systems(mesh);      LinearImplicitSystem&amp; system = equation_systems.add_system        &lt;LinearImplicitSystem&gt;(<B><FONT COLOR="#BC8F8F">&quot;1D&quot;</FONT></B>);        system.add_variable(<B><FONT COLOR="#BC8F8F">&quot;u&quot;</FONT></B>,SECOND);        system.attach_assemble_function(assemble_1D);        MeshRefinement mesh_refinement(mesh);        mesh_refinement.refine_fraction()  = 0.7;      mesh_refinement.coarsen_fraction() = 0.3;      mesh_refinement.max_h_level()      = 5;        equation_systems.init();        <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> max_r_steps = 5; <I><FONT COLOR="#B22222">// Refine the mesh 5 times</FONT></I>        <B><FONT COLOR="#A020F0">for</FONT></B>(<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> r_step=0; r_step&lt;=max_r_steps; r_step++)      {        equation_systems.get_system(<B><FONT COLOR="#BC8F8F">&quot;1D&quot;</FONT></B>).solve();          <B><FONT COLOR="#A020F0">if</FONT></B>(r_step != max_r_steps)        {          ErrorVector error;          KellyErrorEstimator error_estimator;            error_estimator.estimate_error(system, error);            mesh_refinement.flag_elements_by_error_fraction (error);            mesh_refinement.refine_and_coarsen_elements();            equation_systems.reinit();        }          }        GnuPlotIO plot(mesh,<B><FONT COLOR="#BC8F8F">&quot;Example 0&quot;</FONT></B>, GnuPlotIO::GRID_ON);        plot.write_equation_systems(<B><FONT COLOR="#BC8F8F">&quot;gnuplot_script&quot;</FONT></B>,equation_systems);    }    #endif <I><FONT COLOR="#B22222">// #ifndef ENABLE_AMR</FONT></I>        <B><FONT COLOR="#A020F0">return</FONT></B> libMesh::close();  }          <B><FONT COLOR="#228B22">void</FONT></B> assemble_1D(EquationSystems&amp; es, <B><FONT COLOR="#228B22">const</FONT></B> std::string&amp; system_name)  {    #ifdef ENABLE_AMR      assert(system_name == <B><FONT COLOR="#BC8F8F">&quot;1D&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;1D&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));      QGauss qrule(dim,FIFTH);    fe-&gt;attach_quadrature_rule(&amp;qrule);        <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;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.active_elements_begin();    <B><FONT COLOR="#228B22">const</FONT></B> MeshBase::const_element_iterator el_end = mesh.active_elements_end();      <B><FONT COLOR="#A020F0">for</FONT></B>( ; el != el_end; ++el)    {      <B><FONT COLOR="#228B22">const</FONT></B> Elem* elem = *el;        dof_map.dof_indices(elem, dof_indices);        fe-&gt;reinit(elem);        <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> n_dofs = dof_indices.size();        Ke.resize(n_dofs, n_dofs);      Fe.resize(n_dofs);          <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.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++)        {          Fe(i) += JxW[qp]*phi[i][qp];            <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]*(1.e-3*dphi[i][qp]*dphi[j][qp] +                                        phi[i][qp]*phi[j][qp]);          }        }      }            <B><FONT COLOR="#228B22">double</FONT></B> penalty = 1.e10;        <B><FONT COLOR="#A020F0">for</FONT></B>(<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> s=0; s&lt;elem-&gt;n_sides(); s++)      {        <B><FONT COLOR="#A020F0">if</FONT></B>(elem-&gt;neighbor(s) == NULL)        {          Ke(s,s) += penalty;          Fe(s)   += 0*penalty;        }      }        dof_map.constrain_element_matrix_and_vector (Ke, Fe, dof_indices);        system.matrix-&gt;add_matrix(Ke, dof_indices);      system.rhs-&gt;add_vector(Fe, dof_indices);    }  #endif <I><FONT COLOR="#B22222">// #ifdef ENABLE_AMR</FONT></I>  }</pre> <a name="output"></a> <br><br><br> <h1> The console output of the program: </h1> <pre>Compiling C++ (in development mode) ex0.C...Linking ex0-devel.../home/peterson/code/libmesh/contrib/tecplot/lib/i686-pc-linux-gnu/tecio.a(tecxxx.o): In function `tecini':tecxxx.c:(.text+0x1a7): warning: the use of `mktemp' is dangerous, better use `mkstemp'**************************************************************** Running Example  ./ex0-devel***************************************************************  **************************************************************** Done Running Example  ./ex0-devel***************************************************************</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 + -