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

📄 ex8.php

📁 一个用来实现偏微分方程中网格的计算库
💻 PHP
📖 第 1 页 / 共 4 页
字号:
<div class = "comment">Store a pointer to the element we are currentlyworking on.  This allows for nicer syntax later.</div><div class ="fragment"><pre>              const Elem* elem = *el;        </pre></div><div class = "comment">Get the degree of freedom indices for thecurrent element.  These define where in the globalmatrix and right-hand-side this element willcontribute 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 currentelement.  This involves computing the location of thequadrature 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">Zero the element matrices and rhs beforesumming them.  We use the resize member here becausethe number of degrees of freedom might have changed fromthe last element.  Note that this will be the case if theelement type is different (i.e. the last element was HEX8and now have a PRISM6).</div><div class ="fragment"><pre>              {                const unsigned int 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);              }        </pre></div><div class = "comment">Now loop over the quadrature points.  This handlesthe numeric integration.</div><div class ="fragment"><pre>              for (unsigned int qp=0; qp&lt;qrule.n_points(); qp++)        	{</pre></div><div class = "comment">Now we will build the element matrix.  This involvesa double loop to integrate the test funcions (i) againstthe trial functions (j).</div><div class ="fragment"><pre>                  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]);        		Me(i,j) += JxW[qp]*phi[i][qp]*phi[j][qp]        		           *1./(speed*speed);        	      } // end of the matrix summation loop	          	} // end of quadrature point loop        </pre></div><div class = "comment">Now compute the contribution to the element matrix and theright-hand-side vector if the current element lies on theboundary. </div><div class ="fragment"><pre>              {</pre></div><div class = "comment">In this example no natural boundary conditions willbe considered.  The code is left here so it can easilybe extended.<br><br>don't do this for any side</div><div class ="fragment"><pre>                for (unsigned int side=0; side&lt;elem-&gt;n_sides(); side++)        	  if (!true)</pre></div><div class = "comment">if (elem->neighbor(side) == NULL)</div><div class ="fragment"><pre>                    {</pre></div><div class = "comment">Declare a special finite element object forboundary integration.</div><div class ="fragment"><pre>                      AutoPtr&lt;FEBase&gt; fe_face (FEBase::build(dim, fe_type));        	      </pre></div><div class = "comment">Boundary integration requires one quadraure rule,with dimensionality one less than the dimensionalityof the element.</div><div class ="fragment"><pre>                      QGauss qface(dim-1, SECOND);        	      </pre></div><div class = "comment">Tell the finte element object to use ourquadrature rule.</div><div class ="fragment"><pre>                      fe_face-&gt;attach_quadrature_rule (&qface);        	      </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">Compute the shape function values on the elementface.</div><div class ="fragment"><pre>                      fe_face-&gt;reinit(elem, side);        </pre></div><div class = "comment">Here we consider a normal acceleration acc_n=1 applied tothe whole boundary of our mesh.</div><div class ="fragment"><pre>                      const Real acc_n_value = 1.0;        	      </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">Right-hand-side contribution due to prescribednormal acceleration.</div><div class ="fragment"><pre>                          for (unsigned int i=0; i&lt;phi_face.size(); i++)        		    {        		      Fe(i) += acc_n_value*rho        			*phi_face[i][qp]*JxW_face[qp];        		    }        		} // end face quadrature point loop	          	    } // end if (elem-&gt;neighbor(side) == NULL)        	</pre></div><div class = "comment">In this example the Dirichlet boundary conditions will be imposed via panalty method after thesystem is assembled.	<br><br></div><div class ="fragment"><pre>              } // end boundary condition section                  </pre></div><div class = "comment">Finally, simply add the contributions to the additionalmatrices and vector.</div><div class ="fragment"><pre>              stiffness.add_matrix (Ke, dof_indices);              damping.add_matrix   (Ce, dof_indices);              mass.add_matrix      (Me, dof_indices);                            force.add_vector     (Fe, dof_indices);            </pre></div><div class = "comment">For the overall matrix, explicitly zero the entries wherewe added values in the other ones, so that we have identical sparsity footprints.</div><div class ="fragment"><pre>              matrix.add_matrix(zero_matrix, dof_indices);                        } // end of element loop          </pre></div><div class = "comment">All done!</div><div class ="fragment"><pre>          return;        }        </pre></div><div class = "comment">This function applies the initial conditions</div><div class ="fragment"><pre>        void apply_initial(EquationSystems& es,        		   const std::string& system_name)        {</pre></div><div class = "comment">Get a reference to our system, as before</div><div class ="fragment"><pre>          NewmarkSystem & t_system = es.get_system&lt;NewmarkSystem&gt; (system_name);          </pre></div><div class = "comment">Numeric vectors for the pressure, velocity and accelerationvalues.</div><div class ="fragment"><pre>          NumericVector&lt;Number&gt;&  pres_vec       = t_system.get_vector("displacement");          NumericVector&lt;Number&gt;&  vel_vec        = t_system.get_vector("velocity");          NumericVector&lt;Number&gt;&  acc_vec        = t_system.get_vector("acceleration");        </pre></div><div class = "comment">Assume our fluid to be at rest, which wouldalso be the default conditions in class NewmarkSystem,but let us do it explicetly here.</div><div class ="fragment"><pre>          pres_vec.zero();          vel_vec.zero();          acc_vec.zero();        }        </pre></div><div class = "comment">This function applies the Dirichlet boundary conditions</div><div class ="fragment"><pre>        void fill_dirichlet_bc(EquationSystems& es,        		       const std::string& system_name)        {</pre></div><div class = "comment">It is a good idea to make sure we are assemblingthe proper system.</div><div class ="fragment"><pre>          assert (system_name == "Wave");        </pre></div><div class = "comment">Get a reference to our system, as before.</div><div class ="fragment"><pre>          NewmarkSystem & t_system = es.get_system&lt;NewmarkSystem&gt; (system_name);        </pre></div><div class = "comment">Get writable references to the overall matrix and vector.</div><div class ="fragment"><pre>          SparseMatrix&lt;Number&gt;&  matrix = *t_system.matrix;          NumericVector&lt;Number&gt;& rhs    = *t_system.rhs;        </pre></div><div class = "comment">Get a constant reference to the mesh object.</div><div class ="fragment"><pre>          const Mesh& mesh = es.get_mesh();        </pre></div><div class = "comment">Get \p libMesh's  \f$ \pi \f$</div><div class ="fragment"><pre>          const Real pi = libMesh::pi;        </pre></div><div class = "comment">Ask the \p EquationSystems flag whetherwe should do this also for the matrix</div><div class ="fragment"><pre>          const bool do_for_matrix =            es.parameters.get&lt;bool&gt;("Newmark set BC for Matrix");        </pre></div><div class = "comment">Ge the current time from \p EquationSystems</div><div class ="fragment"><pre>          const Real current_time = es.parameters.get&lt;Real&gt;("time");        </pre></div><div class = "comment">Number of nodes in the mesh.</div><div class ="fragment"><pre>          unsigned int n_nodes = mesh.n_nodes();                  for (unsigned int n_cnt=0; n_cnt&lt;n_nodes; n_cnt++)            {</pre></div><div class = "comment">Get a reference to the current node.</div><div class ="fragment"><pre>              const Node& curr_node = mesh.node(n_cnt);        </pre></div><div class = "comment">Check if Dirichlet BCs should be applied to this node.Use the \p TOLERANCE from \p mesh_common.h as tolerance.Here a pressure value is applied if the z-coord.is equal to 4, which corresponds to one end of thepipe-mesh in this directory.</div><div class ="fragment"><pre>              const Real z_coo = 4.;                      if (fabs(curr_node(2)-z_coo) &lt; TOLERANCE)        	{</pre></div><div class = "comment">The global number of the respective degree of freedom.</div><div class ="fragment"><pre>                  unsigned int dn = curr_node.dof_number(0,0,0);        </pre></div><div class = "comment">

⌨️ 快捷键说明

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