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

📄 ex9.php

📁 一个用来实现偏微分方程中网格的计算库
💻 PHP
📖 第 1 页 / 共 3 页
字号:
<?php $root=""; ?><?php require($root."navigation.php"); ?><html><head>  <?php load_style($root); ?></head> <body> <?php make_navigation("ex9",$root)?> <div class="content"><a name="comments"></a> <div class = "comment"><h1>Example 9 - Solving a Transient Linear System in Parallel</h1><br><br>This example shows how a simple, linear transientsystem can be solved in parallel.  The system is simplescalar convection-diffusion with a specified externalvelocity.  The initial condition is given, and thesolution is advanced in time with a standard Crank-Nicholsontime-stepping strategy.<br><br>C++ include files that we need</div><div class ="fragment"><pre>        #include &lt;iostream&gt;        #include &lt;algorithm&gt;        #include &lt;math.h&gt;        </pre></div><div class = "comment">Basic include file needed for the mesh functionality.</div><div class ="fragment"><pre>        #include "libmesh.h"        #include "mesh.h"        #include "mesh_refinement.h"        #include "gmv_io.h"        #include "equation_systems.h"        #include "fe.h"        #include "quadrature_gauss.h"        #include "dof_map.h"        #include "sparse_matrix.h"        #include "numeric_vector.h"        #include "dense_matrix.h"        #include "dense_vector.h"        </pre></div><div class = "comment">Some (older) compilers do not offer full stream functionality, OStringStream works around this.</div><div class ="fragment"><pre>        #include "o_string_stream.h"        </pre></div><div class = "comment">This example will solve a linear transient system,so we need to include the \p TransientLinearImplicitSystem definition.</div><div class ="fragment"><pre>        #include "linear_implicit_system.h"        #include "transient_system.h"        #include "vector_value.h"        </pre></div><div class = "comment">The definition of a geometric element</div><div class ="fragment"><pre>        #include "elem.h"        </pre></div><div class = "comment">Function prototype.  This function will assemble the systemmatrix and right-hand-side at each time step.  Note thatsince the system is linear we technically do not need toassmeble the matrix at each time step, but we will anyway.In subsequent examples we will employ adaptive mesh refinement,and with a changing mesh it will be necessary to rebuild thesystem matrix.</div><div class ="fragment"><pre>        void assemble_cd (EquationSystems& es,        		  const std::string& system_name);        </pre></div><div class = "comment">Function prototype.  This function will initialize the system.Initialization functions are optional for systems.  They allowyou to specify the initial values of the solution.  If aninitialization function is not provided then the default (0)solution is provided.</div><div class ="fragment"><pre>        void init_cd (EquationSystems& es,        	      const std::string& system_name);        </pre></div><div class = "comment">Exact solution function prototype.  This gives the exactsolution as a function of space and time.  In this case theinitial condition will be taken as the exact solution at time 0,as will the Dirichlet boundary conditions at time t.</div><div class ="fragment"><pre>        Real exact_solution (const Real x,        		     const Real y,        		     const Real t);                Number exact_value (const Point& p,        		    const Parameters& parameters,        		    const std::string&,        		    const std::string&)        {          return exact_solution(p(0), p(1), parameters.get&lt;Real&gt; ("time"));        }                        </pre></div><div class = "comment">We can now begin the main program.  Note that thisexample will fail if you are using complex numberssince it was designed to be run only with real numbers.</div><div class ="fragment"><pre>        int main (int argc, char** argv)        {</pre></div><div class = "comment">Initialize libMesh.</div><div class ="fragment"><pre>          libMesh::init (argc, argv);                #ifndef ENABLE_AMR          std::cerr &lt;&lt; "ERROR: This example requires libMesh to be\n"                    &lt;&lt; "compiled with AMR support!"                    &lt;&lt; std::endl;          return 0;        #else                  {    </pre></div><div class = "comment">Create a two-dimensional mesh.</div><div class ="fragment"><pre>            Mesh mesh (2);                </pre></div><div class = "comment">Read the mesh from file.  This is the coarse mesh that will be usedin example 10 to demonstrate adaptive mesh refinement.  Here we willsimply read it in and uniformly refine it 5 times before we computewith it.</div><div class ="fragment"><pre>            mesh.read ("../ex10/mesh.xda");            </pre></div><div class = "comment">Create a MeshRefinement object to handle refinement of our mesh.This class handles all the details of mesh refinement and coarsening.</div><div class ="fragment"><pre>            MeshRefinement mesh_refinement (mesh);            </pre></div><div class = "comment">Uniformly refine the mesh 5 times.  This is thefirst time we use the mesh refinement capabilitiesof the library.</div><div class ="fragment"><pre>            mesh_refinement.uniformly_refine (5);            </pre></div><div class = "comment">Print information about the mesh to the screen.</div><div class ="fragment"><pre>            mesh.print_info();            </pre></div><div class = "comment">Create an equation systems object.</div><div class ="fragment"><pre>            EquationSystems equation_systems (mesh);            </pre></div><div class = "comment">Add a transient system to the EquationSystemsobject named "Convection-Diffusion".</div><div class ="fragment"><pre>            TransientLinearImplicitSystem & system =               equation_systems.add_system&lt;TransientLinearImplicitSystem&gt; ("Convection-Diffusion");              </pre></div><div class = "comment">Adds the variable "u" to "Convection-Diffusion".  "u"will be approximated using first-order approximation.</div><div class ="fragment"><pre>            system.add_variable ("u", FIRST);              </pre></div><div class = "comment">Give the system a pointer to the matrix assemblyand initialization functions.</div><div class ="fragment"><pre>            system.attach_assemble_function (assemble_cd);            system.attach_init_function (init_cd);              </pre></div><div class = "comment">Initialize the data structures for the equation system.</div><div class ="fragment"><pre>            equation_systems.init ();              </pre></div><div class = "comment">Prints information about the system to the screen.</div><div class ="fragment"><pre>            equation_systems.print_info();              </pre></div><div class = "comment">Write out the initial conditions.</div><div class ="fragment"><pre>            GMVIO(mesh).write_equation_systems ("out_000.gmv",        					equation_systems);            </pre></div><div class = "comment">The Convection-Diffusion system requires that we specifythe flow velocity.  We will specify it as a RealVectorValuedata type and then use the Parameters object to pass it tothe assemble function.</div><div class ="fragment"><pre>            equation_systems.parameters.set&lt;RealVectorValue&gt;("velocity") =               RealVectorValue (0.8, 0.8);            </pre></div><div class = "comment">Solve the system "Convection-Diffusion".  This will be done bylooping over the specified time interval and calling thesolve() member at each time step.  This will assemble thesystem and call the linear solver.</div><div class ="fragment"><pre>            const Real dt = 0.025;            Real time     = 0.;                        for (unsigned int t_step = 0; t_step &lt; 50; t_step++)              {</pre></div><div class = "comment">Incremenet the time counter, set the time and thetime step size as parameters in the EquationSystem.</div><div class ="fragment"><pre>                time += dt;                	equation_systems.parameters.set&lt;Real&gt; ("time") = time;        	equation_systems.parameters.set&lt;Real&gt; ("dt")   = dt;        </pre></div><div class = "comment">A pretty update message</div><div class ="fragment"><pre>                std::cout &lt;&lt; " Solving time step ";        	</pre></div><div class = "comment">Since some compilers fail to offer full streamfunctionality, libMesh offers a string streamto work around this.  Note that for other compilers,this is just a set of preprocessor macros and thereforeshould cost nothing (compared to a hand-coded string stream).We use additional curly braces here simply to enforce datalocality.</div><div class ="fragment"><pre>                {        	  OStringStream out;                	  OSSInt(out,2,t_step);        	  out &lt;&lt; ", time=";        	  OSSRealzeroleft(out,6,3,time);        	  out &lt;&lt;  "...";        	  std::cout &lt;&lt; out.str() &lt;&lt; std::endl;        	}        	</pre></div><div class = "comment">At this point we need to update the oldsolution vector.  The old solution vectorwill be the current solution vector from theprevious time step.  We will do this by extracting thesystem from the \p EquationSystems object and usingvector assignment.  Since only \p TransientSystems(and systems derived from them) contain old solutionswe need to specify the system type when we ask for it.</div><div class ="fragment"><pre>                TransientLinearImplicitSystem&  system =        	  equation_systems.get_system&lt;TransientLinearImplicitSystem&gt;("Convection-Diffusion");                	*system.old_local_solution = *system.current_local_solution;        	</pre></div><div class = "comment">Assemble & solve the linear system</div><div class ="fragment"><pre>                equation_systems.get_system("Convection-Diffusion").solve();        	</pre></div><div class = "comment">Output evey 10 timesteps to file.</div><div class ="fragment"><pre>                if ( (t_step+1)%10 == 0)        	  {        	    OStringStream file_name;                	    file_name &lt;&lt; "out_";        	    OSSRealzeroright(file_name,3,0,t_step+1);        	    file_name &lt;&lt; ".gmv";                	    GMVIO(mesh).write_equation_systems (file_name.str(),        						equation_systems);        	  }              }          }        #endif // #ifdef ENABLE_AMR        </pre></div><div class = "comment">All done.  </div><div class ="fragment"><pre>          return libMesh::close ();        }        </pre></div><div class = "comment">We now define the function which provides theinitialization routines for the "Convection-Diffusion"system.  This handles things like setting initialconditions and boundary conditions.</div><div class ="fragment"><pre>        void init_cd (EquationSystems& es,        	      const std::string& system_name)        {</pre></div><div class = "comment">It is a good idea to make sure we are initializingthe proper system.</div>

⌨️ 快捷键说明

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