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

📄 ex7.php

📁 一个用来实现偏微分方程中网格的计算库
💻 PHP
📖 第 1 页 / 共 4 页
字号:
<div class = "comment">The matrices from which values are added to another matrixhave to be closed.  The add() method does take care of that, but let us do it explicitly.</div><div class ="fragment"><pre>          stiffness.close ();          damping.close   ();          mass.close      ();                  STOP_LOG("init phase","add_M_C_K_helmholtz");                  START_LOG("global matrix & vector additions","add_M_C_K_helmholtz");          </pre></div><div class = "comment">add the stiffness and mass with the proper frequency to theoverall system.  For this to work properly, matrix hasto be not only initialized, but filled with the identicalsparsity structure as the matrix added to it, otherwisesolver packages like PETSc crash.<br><br>Note that we have to add the mass and stiffness contributionsone at a time; otherwise, the real part of matrix wouldbe fine, but the imaginary part cluttered with unwanted products.</div><div class ="fragment"><pre>          matrix.add (scale_stiffness, stiffness);          matrix.add (scale_mass,      mass);          matrix.add (scale_damping,   damping);          rhs.add    (scale_rhs,       freq_indep_rhs);                  STOP_LOG("global matrix & vector additions","add_M_C_K_helmholtz");          </pre></div><div class = "comment">The "matrix" and "rhs" are now ready for solution   </div><div class ="fragment"><pre>        #endif        }        </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;stdio.h&gt;    #include <B><FONT COLOR="#BC8F8F">&quot;libmesh.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;libmesh_logging.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;equation_systems.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;elem.h&quot;</FONT></B>    #include <B><FONT COLOR="#BC8F8F">&quot;frequency_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;dense_matrix.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;dense_vector.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;dof_map.h&quot;</FONT></B>    #include <B><FONT COLOR="#BC8F8F">&quot;mesh_data.h&quot;</FONT></B>    <B><FONT COLOR="#228B22">void</FONT></B> assemble_helmholtz(EquationSystems&amp; es,  			<B><FONT COLOR="#228B22">const</FONT></B> std::string&amp; system_name);    <B><FONT COLOR="#228B22">void</FONT></B> add_M_C_K_helmholtz(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 USE_COMPLEX_NUMBERS      <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;ERROR: This example is intended for &quot;</FONT></B> &lt;&lt; std::endl  	    &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot; use with complex numbers.&quot;</FONT></B> &lt;&lt; std::endl;    here();      <B><FONT COLOR="#A020F0">return</FONT></B> 0;    #<B><FONT COLOR="#A020F0">else</FONT></B>        {      <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; -f [frequency]&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">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> Real frequency_in = atof(argv[2]);      <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> n_frequencies = 3;                    Mesh mesh (dim);        MeshData mesh_data(mesh);      mesh_data.activate();        mesh.read(<B><FONT COLOR="#BC8F8F">&quot;lshape.unv&quot;</FONT></B>, &amp;mesh_data);        mesh.print_info();            mesh_data.read(<B><FONT COLOR="#BC8F8F">&quot;lshape_data.unv&quot;</FONT></B>);        mesh_data.print_info();        EquationSystems equation_systems (mesh, &amp;mesh_data);            FrequencySystem &amp; f_system =              equation_systems.add_system&lt;FrequencySystem&gt; (<B><FONT COLOR="#BC8F8F">&quot;Helmholtz&quot;</FONT></B>);            f_system.add_variable(<B><FONT COLOR="#BC8F8F">&quot;p&quot;</FONT></B>, SECOND);            f_system.attach_assemble_function (assemble_helmholtz);      f_system.attach_solve_function    (add_M_C_K_helmholtz);            f_system.add_matrix (<B><FONT COLOR="#BC8F8F">&quot;stiffness&quot;</FONT></B>);      f_system.add_matrix (<B><FONT COLOR="#BC8F8F">&quot;damping&quot;</FONT></B>);      f_system.add_matrix (<B><FONT COLOR="#BC8F8F">&quot;mass&quot;</FONT></B>);      f_system.add_vector (<B><FONT COLOR="#BC8F8F">&quot;rhs&quot;</FONT></B>);            f_system.set_frequencies_by_steps (frequency_in/n_frequencies,  				       frequency_in,  				       n_frequencies);            equation_systems.parameters.set&lt;Real&gt; (<B><FONT COLOR="#BC8F8F">&quot;wave speed&quot;</FONT></B>) = 1.;      equation_systems.parameters.set&lt;Real&gt; (<B><FONT COLOR="#BC8F8F">&quot;rho&quot;</FONT></B>)        = 1.;            equation_systems.init ();            equation_systems.print_info ();        <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; n_frequencies; n++)        {  	f_system.solve (n,n);  	  	<B><FONT COLOR="#228B22">char</FONT></B> buf[14];  	sprintf (buf, <B><FONT COLOR="#BC8F8F">&quot;out%04d.gmv&quot;</FONT></B>, n);  	GMVIO(mesh).write_equation_systems (buf,  					    equation_systems);        }            equation_systems.write (<B><FONT COLOR="#BC8F8F">&quot;eqn_sys.dat&quot;</FONT></B>, libMeshEnums::WRITE);    }        <B><FONT COLOR="#A020F0">return</FONT></B> libMesh::close ();    #endif   }      <B><FONT COLOR="#228B22">void</FONT></B> assemble_helmholtz(EquationSystems&amp; es,  			<B><FONT COLOR="#228B22">const</FONT></B> std::string&amp; system_name)  {  #ifdef USE_COMPLEX_NUMBERS          assert (system_name == <B><FONT COLOR="#BC8F8F">&quot;Helmholtz&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();        FrequencySystem &amp; f_system =      es.get_system&lt;FrequencySystem&gt; (system_name);        <B><FONT COLOR="#228B22">const</FONT></B> DofMap&amp; dof_map = f_system.get_dof_map();        <B><FONT COLOR="#228B22">const</FONT></B> FEType&amp; fe_type = dof_map.variable_type(0);      <B><FONT COLOR="#228B22">const</FONT></B> Real rho = es.parameters.get&lt;Real&gt;(<B><FONT COLOR="#BC8F8F">&quot;rho&quot;</FONT></B>);        SparseMatrix&lt;Number&gt;&amp;   stiffness      = f_system.get_matrix(<B><FONT COLOR="#BC8F8F">&quot;stiffness&quot;</FONT></B>);    SparseMatrix&lt;Number&gt;&amp;   damping        = f_system.get_matrix(<B><FONT COLOR="#BC8F8F">&quot;damping&quot;</FONT></B>);    SparseMatrix&lt;Number&gt;&amp;   mass           = f_system.get_matrix(<B><FONT COLOR="#BC8F8F">&quot;mass&quot;</FONT></B>);    NumericVector&lt;Number&gt;&amp;  freq_indep_rhs = f_system.get_vector(<B><FONT COLOR="#BC8F8F">&quot;rhs&quot;</FONT></B>);        SparseMatrix&lt;Number&gt;&amp;  matrix           = *f_system.matrix;        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, Ce, Me, zero_matrix;    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.local_elements_begin();    <B><FONT COLOR="#228B22">const</FONT></B> MeshBase::const_element_iterator end_el = mesh.local_elements_end();        <B><FONT COLOR="#A020F0">for</FONT></B> ( ; el != end_el; ++el)      {        START_LOG(<B><FONT COLOR="#BC8F8F">&quot;elem init&quot;</FONT></B>,<B><FONT COLOR="#BC8F8F">&quot;assemble_helmholtz&quot;</FONT></B>);                <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">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> n_dof_indices = dof_indices.size();    	Ke.resize          (n_dof_indices, n_dof_indices);  	Ce.resize          (n_dof_indices, n_dof_indices);  	Me.resize          (n_dof_indices, n_dof_indices);  	zero_matrix.resize (n_dof_indices, n_dof_indices);  	Fe.resize          (n_dof_indices);        }                STOP_LOG(<B><FONT COLOR="#BC8F8F">&quot;elem init&quot;</FONT></B>,<B><FONT COLOR="#BC8F8F">&quot;assemble_helmholtz&quot;</FONT></B>);          START_LOG(<B><FONT COLOR="#BC8F8F">&quot;stiffness &amp; mass&quot;</FONT></B>,<B><FONT COLOR="#BC8F8F">&quot;assemble_helmholtz&quot;</FONT></B>);          <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++)  	    <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]);  		Me(i,j) += JxW[qp]*(phi[i][qp]*phi[j][qp]);  	      }	    	}          STOP_LOG(<B><FONT COLOR="#BC8F8F">&quot;stiffness &amp; mass&quot;</FONT></B>,<B><FONT COLOR="#BC8F8F">&quot;assemble_helmholtz&quot;</FONT></B>);    	         <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)  	  {  	    START_LOG(<B><FONT COLOR="#BC8F8F">&quot;damping&quot;</FONT></B>,<B><FONT COLOR="#BC8F8F">&quot;assemble_helmholtz&quot;</FONT></B>);  	        	    AutoPtr&lt;FEBase&gt; fe_face (FEBase::build(dim, fe_type));  	        	    QGauss qface(dim-1, SECOND);  	        	    fe_face-&gt;attach_quadrature_rule (&amp;qface);  	        	    <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();  	        	    fe_face-&gt;reinit(elem, side);  	        	    <B><FONT COLOR="#228B22">const</FONT></B> Real an_value = 1.;  	        	    <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.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_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++)  		    Ce(i,j) += rho*an_value*JxW_face[qp]*phi_face[i][qp]*phi_face[j][qp];  	      }    	    STOP_LOG(<B><FONT COLOR="#BC8F8F">&quot;damping&quot;</FONT></B>,<B><FONT COLOR="#BC8F8F">&quot;assemble_helmholtz&quot;</FONT></B>);  	  }                 stiffness.add_matrix      (Ke, dof_indices);        damping.add_matrix        (Ce, dof_indices);        mass.add_matrix           (Me, dof_indices);                matrix.add_matrix(zero_matrix, dof_indices);        } <I><FONT COLOR="#B22222">// loop el</FONT></I>        {      START_LOG(<B><FONT COLOR="#BC8F8F">&quot;rhs&quot;</FONT></B>,<B><FONT COLOR="#BC8F8F">&quot;assemble_helmholtz&quot;</FONT></B>);            <B><FONT COLOR="#228B22">const</FONT></B> MeshData&amp; mesh_data = es.get_mesh_data();        assert(mesh_data.n_val_per_node() == 1);        <B><FONT COLOR="#5F9EA0">MeshBase</FONT></B>::const_node_iterator       node_it  = mesh.nodes_begin();      <B><FONT COLOR="#228B22">const</FONT></B> MeshBase::const_node_iterator node_end = mesh.nodes_end();            <B><FONT COLOR="#A020F0">for</FONT></B> ( ; node_it != node_end; ++node_it)        {  	<B><FONT COLOR="#228B22">const</FONT></B> Node* node = *node_it;    	<B><FONT COLOR="#A020F0">if</FONT></B> (mesh_data.has_data(node))  	  <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> comp=0; comp&lt;node-&gt;n_comp(0,0); comp++)  	    {  	      <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> dn = node-&gt;dof_number(0,0,comp);    	      freq_indep_rhs.set(dn, mesh_data.get_data(node)[0]);  	    }        }           STOP_LOG(<B><FONT COLOR="#BC8F8F">&quot;rhs&quot;</FONT></B>,<B><FONT COLOR="#BC8F8F">&quot;assemble_helmholtz&quot;</FONT></B>);    }    #endif  }      <B><FONT COLOR="#228B22">void</FONT></B> add_M_C_K_helmholtz(EquationSystems&amp; es,  			 <B><FONT COLOR="#228B22">const</FONT></B> std::string&amp; system_name)  {  #ifdef USE_COMPLEX_NUMBERS      START_LOG(<B><FONT COLOR="#BC8F8F">&quot;init phase&quot;</FONT></B>,<B><FONT COLOR="#BC8F8F">&quot;add_M_C_K_helmholtz&quot;</FONT></B>);        assert (system_name == <B><FONT COLOR="#BC8F8F">&quot;Helmholtz&quot;</FONT></B>);        FrequencySystem &amp; f_system =      es.get_system&lt;FrequencySystem&gt; (system_name);        <B><FONT COLOR="#228B22">const</FONT></B> Real frequency = es.parameters.get&lt;Real&gt; (<B><FONT COLOR="#BC8F8F">&quot;current frequency&quot;</FONT></B>);    <B><FONT COLOR="#228B22">const</FONT></B> Real rho       = es.parameters.get&lt;Real&gt; (<B><FONT COLOR="#BC8F8F">&quot;rho&quot;</FONT></B>);    <B><FONT COLOR="#228B22">const</FONT></B> Real speed     = es.parameters.get&lt;Real&gt; (<B><FONT COLOR="#BC8F8F">&quot;wave speed&quot;</FONT></B>);        <B><FONT COLOR="#228B22">const</FONT></B> Real omega = 2.0*libMesh::pi*frequency;    <B><FONT COLOR="#228B22">const</FONT></B> Real k     = omega / speed;        SparseMatrix&lt;Number&gt;&amp;  matrix          = *f_system.matrix;    NumericVector&lt;Number&gt;&amp; rhs             = *f_system.rhs;        SparseMatrix&lt;Number&gt;&amp;   stiffness      = f_system.get_matrix(<B><FONT COLOR="#BC8F8F">&quot;stiffness&quot;</FONT></B>);    SparseMatrix&lt;Number&gt;&amp;   damping        = f_system.get_matrix(<B><FONT COLOR="#BC8F8F">&quot;damping&quot;</FONT></B>);    SparseMatrix&lt;Number&gt;&amp;   mass           = f_system.get_matrix(<B><FONT COLOR="#BC8F8F">&quot;mass&quot;</FONT></B>);    NumericVector&lt;Number&gt;&amp;  freq_indep_rhs = f_system.get_vector(<B><FONT COLOR="#BC8F8F">&quot;rhs&quot;</FONT></B>);        <B><FONT COLOR="#228B22">const</FONT></B> Number scale_stiffness (  1., 0.   );    <B><FONT COLOR="#228B22">const</FONT></B> Number scale_damping   (  0., omega);    <B><FONT COLOR="#228B22">const</FONT></B> Number scale_mass      (-k*k, 0.   );    <B><FONT COLOR="#228B22">const</FONT></B> Number scale_rhs       (  0., -(rho*omega));        matrix.close(); matrix.zero ();    rhs.close();    rhs.zero    ();        stiffness.close ();    damping.close   ();    mass.close      ();      STOP_LOG(<B><FONT COLOR="#BC8F8F">&quot;init phase&quot;</FONT></B>,<B><FONT COLOR="#BC8F8F">&quot;add_M_C_K_helmholtz&quot;</FONT></B>);      START_LOG(<B><FONT COLOR="#BC8F8F">&quot;global matrix &amp; vector additions&quot;</FONT></B>,<B><FONT COLOR="#BC8F8F">&quot;add_M_C_K_helmholtz&quot;</FONT></B>);        matrix.add (scale_stiffness, stiffness);    matrix.add (scale_mass,      mass);    matrix.add (scale_damping,   damping);    rhs.add    (scale_rhs,       freq_indep_rhs);      STOP_LOG(<B><FONT COLOR="#BC8F8F">&quot;global matrix &amp; vector additions&quot;</FONT></B>,<B><FONT COLOR="#BC8F8F">&quot;add_M_C_K_helmholtz&quot;</FONT></B>);      #endif  }  </pre> <a name="output"></a> <br><br><br> <h1> The console output of the program: </h1> <pre>****************************************************************** Skipping Example  ./ex7-devel , only good with --enable-complex***************************************************************</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 + -