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

📄 ex9.php

📁 一个用来实现偏微分方程中网格的计算库
💻 PHP
📖 第 1 页 / 共 3 页
字号:
<div class ="fragment"><pre>          assert (system_name == "Convection-Diffusion");        </pre></div><div class = "comment">Get a reference to the Convection-Diffusion system object.</div><div class ="fragment"><pre>          TransientLinearImplicitSystem & system =            es.get_system&lt;TransientLinearImplicitSystem&gt;("Convection-Diffusion");        </pre></div><div class = "comment">Project initial conditions at time 0</div><div class ="fragment"><pre>          es.parameters.set&lt;Real&gt; ("time") = 0;                    system.project_solution(exact_value, NULL, es.parameters);        }                        </pre></div><div class = "comment">Now we define the assemble function which will be usedby the EquationSystems object at each timestep to assemblethe linear system for solution.</div><div class ="fragment"><pre>        void assemble_cd (EquationSystems& es,        		  const std::string& system_name)        {        #ifdef ENABLE_AMR</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 == "Convection-Diffusion");          </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">The dimension that we are running</div><div class ="fragment"><pre>          const unsigned int dim = mesh.mesh_dimension();          </pre></div><div class = "comment">Get a reference to the Convection-Diffusion system object.</div><div class ="fragment"><pre>          TransientLinearImplicitSystem & system =            es.get_system&lt;TransientLinearImplicitSystem&gt; ("Convection-Diffusion");          </pre></div><div class = "comment">Get a constant reference to the Finite Element typefor the first (and only) variable in the system.</div><div class ="fragment"><pre>          FEType fe_type = system.variable_type(0);          </pre></div><div class = "comment">Build a Finite Element object of the specified type.  Since the\p FEBase::build() member dynamically creates memory we willstore the object as an \p AutoPtr<FEBase>.  This can be thoughtof as a pointer that will clean up after itself.</div><div class ="fragment"><pre>          AutoPtr&lt;FEBase&gt; fe      (FEBase::build(dim, fe_type));          AutoPtr&lt;FEBase&gt; fe_face (FEBase::build(dim, fe_type));          </pre></div><div class = "comment">A Gauss quadrature rule for numerical integration.Let the \p FEType object decide what order rule is appropriate.</div><div class ="fragment"><pre>          QGauss qrule (dim,   fe_type.default_quadrature_order());          QGauss qface (dim-1, fe_type.default_quadrature_order());        </pre></div><div class = "comment">Tell the finite element object to use our quadrature rule.</div><div class ="fragment"><pre>          fe-&gt;attach_quadrature_rule      (&qrule);          fe_face-&gt;attach_quadrature_rule (&qface);        </pre></div><div class = "comment">Here we define some references to cell-specific data thatwill be used to assemble the linear system.  We will startwith the element Jacobian * quadrature weight at each integration point.   </div><div class ="fragment"><pre>          const std::vector&lt;Real&gt;& JxW      = fe-&gt;get_JxW();          const std::vector&lt;Real&gt;& JxW_face = fe_face-&gt;get_JxW();          </pre></div><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();          const std::vector&lt;std::vector&lt;Real&gt; &gt;& psi = fe_face-&gt;get_phi();        </pre></div><div class = "comment">The element shape function gradients evaluated at the quadraturepoints.</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">The XY locations of the quadrature points used for face integration</div><div class ="fragment"><pre>          const std::vector&lt;Point&gt;& qface_points = fe_face-&gt;get_xyz();          </pre></div><div class = "comment">A reference to the \p DofMap object for this system.  The \p DofMapobject handles the index translation from node and element numbersto degree of freedom numbers.  We will talk more about the \p DofMapin future examples.</div><div class ="fragment"><pre>          const DofMap& dof_map = system.get_dof_map();        </pre></div><div class = "comment">Define data structures to contain the element matrixand right-hand-side vector contribution.  Followingbasic finite element terminology we will denote these"Ke" and "Fe".</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 forthe element.  These define where in the global systemthe element degrees of freedom get mapped.</div><div class ="fragment"><pre>          std::vector&lt;unsigned int&gt; dof_indices;        </pre></div><div class = "comment">Here we extract the velocity & parameters that we put in theEquationSystems object.</div><div class ="fragment"><pre>          const RealVectorValue velocity =            es.parameters.get&lt;RealVectorValue&gt; ("velocity");                  const Real dt = es.parameters.get&lt;Real&gt;   ("dt");          const Real time = es.parameters.get&lt;Real&gt; ("time");        </pre></div><div class = "comment">Now we will loop over all the elements in the mesh thatlive on the local processor. We will compute the elementmatrix and right-hand-side contribution.  Since the meshwill be refined we want to only consider the ACTIVE elements,hence we use a variant of the \p active_elem_iterator.const_active_local_elem_iterator           el (mesh.elements_begin());const const_active_local_elem_iterator end_el (mesh.elements_end());<br><br></div><div class ="fragment"><pre>          MeshBase::const_element_iterator       el     = mesh.active_local_elements_begin();          const MeshBase::const_element_iterator end_el = mesh.active_local_elements_end();                   for ( ; el != end_el; ++el)            {    </pre></div><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 matrix and right-hand side 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 atriangle, now we are on a quadrilateral).</div><div class ="fragment"><pre>              Ke.resize (dof_indices.size(),        		 dof_indices.size());                      Fe.resize (dof_indices.size());              </pre></div><div class = "comment">Now we will build the element matrix and right-hand-side.Constructing the RHS requires the solution and itsgradient from the previous timestep.  This myst becalculated at each quadrature point by summing thesolution degree-of-freedom values by the appropriateweight functions.</div><div class ="fragment"><pre>              for (unsigned int qp=0; qp&lt;qrule.n_points(); qp++)        	{</pre></div><div class = "comment">Values to hold the old solution & its gradient.</div><div class ="fragment"><pre>                  Number   u_old = 0.;        	  Gradient grad_u_old;        	  </pre></div><div class = "comment">Compute the old solution & its gradient.</div><div class ="fragment"><pre>                  for (unsigned int l=0; l&lt;phi.size(); l++)        	    {        	      u_old      += phi[l][qp]*system.old_solution  (dof_indices[l]);        	      </pre></div><div class = "comment">This will work,grad_u_old += dphi[l][qp]*system.old_solution (dof_indices[l]);but we can do it without creating a temporary like this:</div><div class ="fragment"><pre>                      grad_u_old.add_scaled (dphi[l][qp],system.old_solution (dof_indices[l]));        	    }        	  </pre></div><div class = "comment">Now compute the element matrix and RHS contributions.</div><div class ="fragment"><pre>                  for (unsigned int i=0; i&lt;phi.size(); i++)        	    {</pre></div><div class = "comment">The RHS contribution</div><div class ="fragment"><pre>                      Fe(i) += JxW[qp]*(</pre></div><div class = "comment">Mass matrix term</div><div class ="fragment"><pre>                                        u_old*phi[i][qp] +        				-.5*dt*(</pre></div><div class = "comment">Convection term(grad_u_old may be complex, so theorder here is important!)</div><div class ="fragment"><pre>                                                (grad_u_old*velocity)*phi[i][qp] +        					</pre></div><div class = "comment">Diffusion term</div><div class ="fragment"><pre>                                                0.01*(grad_u_old*dphi[i][qp]))             				);        	              	      for (unsigned int j=0; j&lt;phi.size(); j++)        		{</pre></div><div class = "comment">The matrix contribution</div><div class ="fragment"><pre>                          Ke(i,j) += JxW[qp]*(</pre></div><div class = "comment">Mass-matrix</div><div class ="fragment"><pre>                                              phi[i][qp]*phi[j][qp] +                 				      .5*dt*(</pre></div><div class = "comment">Convection term</div><div class ="fragment"><pre>                                                     (velocity*dphi[j][qp])*phi[i][qp] +        					     </pre></div><div class = "comment">Diffusion term</div><div class ="fragment"><pre>                                                     0.01*(dphi[i][qp]*dphi[j][qp]))              				      );        		}         	    }         	}         </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. <br><br>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.

⌨️ 快捷键说明

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