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

📄 ex10.php

📁 一个用来实现偏微分方程中网格的计算库
💻 PHP
📖 第 1 页 / 共 4 页
字号:
      <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> init_timestep = 0;            <B><FONT COLOR="#A020F0">if</FONT></B>(command_line.search(<B><FONT COLOR="#BC8F8F">&quot;-init_timestep&quot;</FONT></B>))        init_timestep = command_line.next(0);      <B><FONT COLOR="#A020F0">else</FONT></B>      {        <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;ERROR: Initial timestep not specified\n&quot;</FONT></B> &lt;&lt; std::endl;          error();      }            <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> n_timesteps = 0;        <B><FONT COLOR="#A020F0">if</FONT></B>(command_line.search(<B><FONT COLOR="#BC8F8F">&quot;-n_timesteps&quot;</FONT></B>))        n_timesteps = command_line.next(0);      <B><FONT COLOR="#A020F0">else</FONT></B>      {        <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;ERROR: Number of timesteps not specified\n&quot;</FONT></B> &lt;&lt; std::endl;        error();      }          Mesh mesh (2);        EquationSystems equation_systems (mesh);      MeshRefinement mesh_refinement (mesh);        <B><FONT COLOR="#A020F0">if</FONT></B>(!read_solution)      {        mesh.read (<B><FONT COLOR="#BC8F8F">&quot;mesh.xda&quot;</FONT></B>);          <B><FONT COLOR="#A020F0">if</FONT></B>(!read_solution)          mesh_refinement.uniformly_refine (5);          mesh.print_info();                        TransientLinearImplicitSystem &amp; system =           equation_systems.add_system&lt;TransientLinearImplicitSystem&gt;           (<B><FONT COLOR="#BC8F8F">&quot;Convection-Diffusion&quot;</FONT></B>);          system.add_variable (<B><FONT COLOR="#BC8F8F">&quot;u&quot;</FONT></B>, FIRST);          system.attach_assemble_function (assemble_cd);        system.attach_init_function (init_cd);          equation_systems.init ();      }        <B><FONT COLOR="#A020F0">else</FONT></B>       {        mesh.read(<B><FONT COLOR="#BC8F8F">&quot;saved_mesh.xda&quot;</FONT></B>);          mesh.print_info();          equation_systems.read(<B><FONT COLOR="#BC8F8F">&quot;saved_solution.xda&quot;</FONT></B>, libMeshEnums::READ);          TransientLinearImplicitSystem &amp; system =           equation_systems.get_system&lt;TransientLinearImplicitSystem&gt;           (<B><FONT COLOR="#BC8F8F">&quot;Convection-Diffusion&quot;</FONT></B>);          system.update();          system.attach_assemble_function (assemble_cd);      }        equation_systems.print_info();              equation_systems.parameters.set&lt;<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B>&gt;        (<B><FONT COLOR="#BC8F8F">&quot;linear solver maximum iterations&quot;</FONT></B>) = 250;      equation_systems.parameters.set&lt;Real&gt;        (<B><FONT COLOR="#BC8F8F">&quot;linear solver tolerance&quot;</FONT></B>) = TOLERANCE;              <B><FONT COLOR="#A020F0">if</FONT></B>(!read_solution)        GMVIO(mesh).write_equation_systems (<B><FONT COLOR="#BC8F8F">&quot;out.gmv.000&quot;</FONT></B>,  					  equation_systems);      <B><FONT COLOR="#A020F0">else</FONT></B>        GMVIO(mesh).write_equation_systems (<B><FONT COLOR="#BC8F8F">&quot;solution_read_in.gmv&quot;</FONT></B>,  					  equation_systems);                      equation_systems.parameters.set&lt;RealVectorValue&gt;(<B><FONT COLOR="#BC8F8F">&quot;velocity&quot;</FONT></B>) =         RealVectorValue (0.8, 0.8);            <B><FONT COLOR="#228B22">const</FONT></B> Real dt = 0.025;      Real time     = init_timestep*dt;            <B><FONT COLOR="#A020F0">for</FONT></B>(<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> t_step=init_timestep;                        t_step&lt;(init_timestep+n_timesteps);                        t_step++)        {  	time += dt;    	equation_systems.parameters.set&lt;Real&gt; (<B><FONT COLOR="#BC8F8F">&quot;time&quot;</FONT></B>) = time;  	equation_systems.parameters.set&lt;Real&gt; (<B><FONT COLOR="#BC8F8F">&quot;dt&quot;</FONT></B>)   = dt;    	<B><FONT COLOR="#5F9EA0">std</FONT></B>::cout &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot; Solving time step &quot;</FONT></B>;  	  	{  	  OStringStream out;    	  OSSInt(out,2,t_step);  	  out &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;, time=&quot;</FONT></B>;  	  OSSRealzeroleft(out,6,3,time);  	  out &lt;&lt;  <B><FONT COLOR="#BC8F8F">&quot;...&quot;</FONT></B>;  	  <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout &lt;&lt; out.str() &lt;&lt; std::endl;  	}  	  	TransientLinearImplicitSystem &amp;  system =  	  equation_systems.get_system&lt;TransientLinearImplicitSystem&gt;(<B><FONT COLOR="#BC8F8F">&quot;Convection-Diffusion&quot;</FONT></B>);    	*system.old_local_solution = *system.current_local_solution;  	  	<B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> max_r_steps = 2;  	  	<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++)  	  {  	    system.solve();  	      	    <B><FONT COLOR="#A020F0">if</FONT></B> (r_step+1 != max_r_steps)  	      {  		<B><FONT COLOR="#5F9EA0">std</FONT></B>::cout &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;  Refining the mesh...&quot;</FONT></B> &lt;&lt; std::endl;    		ErrorVector error;    		KellyErrorEstimator error_estimator;  		  		error_estimator.estimate_error (system,  						error);  		  		mesh_refinement.refine_fraction() = 0.80;  		mesh_refinement.coarsen_fraction() = 0.07;  		mesh_refinement.max_h_level() = 5;  		mesh_refinement.flag_elements_by_error_fraction (error);  		  		mesh_refinement.refine_and_coarsen_elements();  		  		equation_systems.reinit ();  	      }	      	  }  	  	<B><FONT COLOR="#A020F0">if</FONT></B> ( (t_step+1)%10 == 0)  	  {  	    OStringStream file_name;    	    file_name &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;out.gmv.&quot;</FONT></B>;  	    OSSRealzeroright(file_name,3,0,t_step+1);    	    GMVIO(mesh).write_equation_systems (file_name.str(),  						equation_systems);  	  }        }          <B><FONT COLOR="#A020F0">if</FONT></B>(!read_solution)        {          mesh.write(<B><FONT COLOR="#BC8F8F">&quot;saved_mesh.xda&quot;</FONT></B>);          equation_systems.write(<B><FONT COLOR="#BC8F8F">&quot;saved_solution.xda&quot;</FONT></B>, libMeshEnums::WRITE);        }    }  #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> init_cd (EquationSystems&amp; es,  	      <B><FONT COLOR="#228B22">const</FONT></B> std::string&amp; system_name)  {    assert (system_name == <B><FONT COLOR="#BC8F8F">&quot;Convection-Diffusion&quot;</FONT></B>);      TransientLinearImplicitSystem &amp; system =      es.get_system&lt;TransientLinearImplicitSystem&gt;(<B><FONT COLOR="#BC8F8F">&quot;Convection-Diffusion&quot;</FONT></B>);      es.parameters.set&lt;Real&gt; (<B><FONT COLOR="#BC8F8F">&quot;time&quot;</FONT></B>) = 0;        system.project_solution(exact_value, NULL, es.parameters);  }        <B><FONT COLOR="#228B22">void</FONT></B> assemble_cd (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;Convection-Diffusion&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();        TransientLinearImplicitSystem &amp; system =      es.get_system&lt;TransientLinearImplicitSystem&gt; (<B><FONT COLOR="#BC8F8F">&quot;Convection-Diffusion&quot;</FONT></B>);        FEType fe_type = system.variable_type(0);        AutoPtr&lt;FEBase&gt; fe      (FEBase::build(dim, fe_type));    AutoPtr&lt;FEBase&gt; fe_face (FEBase::build(dim, fe_type));        QGauss qrule (dim,   fe_type.default_quadrature_order());    QGauss qface (dim-1, fe_type.default_quadrature_order());      fe-&gt;attach_quadrature_rule      (&amp;qrule);    fe_face-&gt;attach_quadrature_rule (&amp;qface);      <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;Real&gt;&amp; JxW_face = fe_face-&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;Real&gt; &gt;&amp; psi = fe_face-&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();      <B><FONT COLOR="#228B22">const</FONT></B> std::vector&lt;Point&gt;&amp; qface_points = fe_face-&gt;get_xyz();          <B><FONT COLOR="#228B22">const</FONT></B> DofMap&amp; dof_map = system.get_dof_map();        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="#228B22">const</FONT></B> RealVectorValue velocity =      es.parameters.get&lt;RealVectorValue&gt; (<B><FONT COLOR="#BC8F8F">&quot;velocity&quot;</FONT></B>);      <B><FONT COLOR="#228B22">const</FONT></B> Real dt = es.parameters.get&lt;Real&gt;   (<B><FONT COLOR="#BC8F8F">&quot;dt&quot;</FONT></B>);    <B><FONT COLOR="#228B22">const</FONT></B> Real time = es.parameters.get&lt;Real&gt; (<B><FONT COLOR="#BC8F8F">&quot;time&quot;</FONT></B>);      <B><FONT COLOR="#5F9EA0">MeshBase</FONT></B>::const_element_iterator       el     = mesh.active_local_elements_begin();    <B><FONT COLOR="#228B22">const</FONT></B> MeshBase::const_element_iterator end_el = mesh.active_local_elements_end();         <B><FONT COLOR="#A020F0">for</FONT></B> ( ; el != end_el; ++el)      {            <B><FONT COLOR="#228B22">const</FONT></B> Elem* elem = *el;                dof_map.dof_indices (elem, dof_indices);          fe-&gt;reinit (elem);                Ke.resize (dof_indices.size(),  		 dof_indices.size());          Fe.resize (dof_indices.size());                <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++)  	{  	  Number   u_old = 0.;  	  Gradient grad_u_old;  	    	  <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> l=0; l&lt;phi.size(); l++)  	    {  	      u_old      += phi[l][qp]*system.old_solution  (dof_indices[l]);  	        	      grad_u_old.add_scaled (dphi[l][qp],system.old_solution (dof_indices[l]));  	    }  	    	  <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]*(  				u_old*phi[i][qp] +   				-.5*dt*(  					(grad_u_old*velocity)*phi[i][qp] +  					  					0.01*(grad_u_old*dphi[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]*(  				      phi[i][qp]*phi[j][qp] +   				      .5*dt*(  					     (velocity*dphi[j][qp])*phi[i][qp] +  					     0.01*(dphi[i][qp]*dphi[j][qp]))        				      );  		}  	    }   	}           {  	<B><FONT COLOR="#228B22">const</FONT></B> Real 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)  	    {  	      fe_face-&gt;reinit(elem,s);  	        	      <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="#228B22">const</FONT></B> Number value = exact_solution (qface_points[qp](0),  						       qface_points[qp](1),  						       time);  						         		  <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;psi.size(); i++)  		    Fe(i) += penalty*JxW_face[qp]*value*psi[i][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;psi.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;psi.size(); j++)  		      Ke(i,j) += penalty*JxW_face[qp]*psi[i][qp]*psi[j][qp];  		}  	    }         }                   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>**************************************************************** Running Example  ./ex10-devel*************************************************************** Usage:	 ./ex10-devel -init_timestep 0OR	 ./ex10-devel -read_solution -init_timestep 26Running: ./ex10-devel -n_timesteps 25 -init_timestep 0 Mesh Information:  mesh_dimension()=2  spatial_dimension()=3  n_nodes()=6273  n_elem()=13650   n_local_elem()=13650   n_active_elem()=10240  n_subdomains()=1  n_processors()=1  processor_id()=0 EquationSystems  n_systems()=1   System "Convection-Diffusion"    Type "TransientLinearImplicit"    Variables="u"     Finite Element Types="LAGRANGE"     Approximation Orders="FIRST"     n_dofs()=6273    n_local_dofs()=6273    n_constrained_dofs()=0    n_vectors()=3 Solving time step  0, time=0.0250...  Refining the mesh... Solving time step  1, time=0.0500...  Refining the mesh... Solving time step  2, time=0.0750...  Refining the mesh... Solving time step  3, time=0.1000...  Refining the mesh... Solving time step  4, time=0.1250...  Refining the mesh... Solving time step  5, time=0.1500...  Refining the mesh... Solving time step  6, time=0.1750...  Refining the mesh... Solving time step  7, time=0.2000...  Refining the mesh... Solving time step  8, time=0.2250...  Refining the mesh... Solving time step  9, time=0.2500...  Refining the mesh... Solving time step 10, time=0.2750...  Refining the mesh... Solving time step 11, time=0.3000...  Refining the mesh... Solving time step 12, time=0.3250...  Refining the mesh... Solving time step 13, time=0.3500...  Refining the mesh... Solving time step 14, time=0.3750...  Refining the mesh... Solving time step 15, time=0.4000...  Refining the mesh... Solving time step 16, time=0.4250...  Refining the mesh... Solving time step 17, time=0.4500...  Refining the mesh... Solving time step 18, time=0.4750...  Refining the mesh... Solving time step 19, time=0.5000...  Refining the mesh... Solving time step 20, time=0.5250...  Refining the mesh... Solving time step 21, time=0.5500...  Refining the mesh... Solving time step 22, time=0.5750...  Refining the mesh... Solving time step 23, time=0.6000...  Refining the mesh... Solving time step 24, time=0.6250...  Refining the mesh... ***** Finished first 25 steps, now read in saved solution and continue ***** Usage:	 ./ex10-devel -init_timestep 0OR	 ./ex10-devel -read_solution -init_timestep 26Running: ./ex10-devel -read_solution -n_timesteps 25 -init_timestep 25 Mesh Information:  mesh_dimension()=2  spatial_dimension()=3  n_nodes()=713  n_elem()=1018   n_local_elem()=1018   n_active_elem()=766  n_subdomains()=1  n_processors()=1  processor_id()=0 EquationSystems  n_systems()=1   System "Convection-Diffusion"    Type "TransientLinearImplicit"    Variables="u"     Finite Element Types="LAGRANGE"     Approximation Orders="FIRST"     n_dofs()=713    n_local_dofs()=713    n_constrained_dofs()=122    n_vectors()=3 Solving time step 25, time=0.6500...  Refining the mesh... Solving time step 26, time=0.6750...  Refining the mesh... Solving time step 27, time=0.7000...  Refining the mesh... Solving time step 28, time=0.7250...  Refining the mesh... Solving time step 29, time=0.7500...  Refining the mesh... Solving time step 30, time=0.7750...  Refining the mesh... Solving time step 31, time=0.8000...  Refining the mesh... Solving time step 32, time=0.8250...  Refining the mesh... Solving time step 33, time=0.8500...  Refining the mesh... Solving time step 34, time=0.8750...  Refining the mesh... Solving time step 35, time=0.9000...  Refining the mesh... Solving time step 36, time=0.9250...  Refining the mesh... Solving time step 37, time=0.9500...  Refining the mesh... Solving time step 38, time=0.9750...  Refining the mesh... Solving time step 39, time=1.0000...  Refining the mesh... Solving time step 40, time=1.0300...  Refining the mesh... Solving time step 41, time=1.0500...  Refining the mesh... Solving time step 42, time=1.0700...  Refining the mesh... Solving time step 43, time=1.1000...  Refining the mesh... Solving time step 44, time=1.1200...  Refining the mesh... Solving time step 45, time=1.1500...  Refining the mesh... Solving time step 46, time=1.1700...  Refining the mesh... Solving time step 47, time=1.2000...  Refining the mesh... Solving time step 48, time=1.2200...  Refining the mesh... Solving time step 49, time=1.2500...  Refining the mesh... **************************************************************** Done Running Example  ./ex10-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 + -