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

📄 ex2.php

📁 一个用来实现偏微分方程中网格的计算库
💻 PHP
字号:
<?php $root=""; ?><?php require($root."navigation.php"); ?><html><head>  <?php load_style($root); ?></head> <body> <?php make_navigation("ex2",$root)?> <div class="content"><a name="comments"></a> <div class = "comment"><h1>Example 2 - Defining a Simple System</h1><br><br>This is the second example program.  It demonstrates how tocreate an equation system for a simple scalar system.  Thisexample will also introduce some of the issues involved with using Petscin your application.<br><br>This is the first example program that indirectlyuses the Petsc library.  By default equation data is storedin Petsc vectors, which may span multiple processors.  BeforePetsc is used it must be initialized via libMesh::init().  Note thatby passing argc and argv to Petsc you may specifycommand line arguments to Petsc.  For example, you mighttry running this example as:<br><br>./ex2 -log_info<br><br>to see what Petsc is doing behind the scenes or<br><br>./ex2 -log_summary<br><br>to get a summary of what Petsc did.Among other things, libMesh::init() initializes the MPIcommunications library on your system if you haven't alreadydone so.<br><br>C++ include files that we need</div><div class ="fragment"><pre>        #include &lt;iostream&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"</pre></div><div class = "comment">Include file that defines various mesh generation utilities</div><div class ="fragment"><pre>        #include "mesh_generation.h"</pre></div><div class = "comment">Include file that defines (possibly multiple) systems of equations.</div><div class ="fragment"><pre>        #include "equation_systems.h"</pre></div><div class = "comment">Include files that define a simple steady system</div><div class ="fragment"><pre>        #include "linear_implicit_system.h"        #include "transient_system.h"                int main (int argc, char** argv)        {          libMesh::init (argc, argv);</pre></div><div class = "comment">This set of braces are used to force object scope.  Thisway we can guarantee all our objects are destroyed before callinglibMesh::close() at the end of the program.</div><div class ="fragment"><pre>          {</pre></div><div class = "comment">A brief message to the user to inform her of theexact name of the program being run, and its command line.</div><div class ="fragment"><pre>            std::cout &lt;&lt; "Running " &lt;&lt; argv[0];            for (int i=1; i&lt;argc; i++)              std::cout &lt;&lt; " " &lt;&lt; argv[i];            std::cout &lt;&lt; std::endl &lt;&lt; std::endl;            </pre></div><div class = "comment">Create a 2D mesh.</div><div class ="fragment"><pre>            Mesh mesh (2);            </pre></div><div class = "comment">Use the MeshTools::Generation mesh generator to create a uniformgrid on the unit square.  By default a mesh of QUAD4elements will be created.  We instruct the mesh generatorto build a mesh of 5x5 elements.</div><div class ="fragment"><pre>            MeshTools::Generation::build_cube (mesh, 5, 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. This object cancontain multiple systems of different flavors for solving loosely coupled physics.  Each system can contain multiple variables of different approximation orders.  Here we will simply create a single system with one variable.  Later on, other flavors of systems will be introduced.  For the moment, we use the general system.The EquationSystems object needs a reference to the meshobject, so the order of construction here is important.</div><div class ="fragment"><pre>            EquationSystems equation_systems (mesh);            </pre></div><div class = "comment">Add a flag "test" that is visible for all systems.  Thishelps in inter-system communication.</div><div class ="fragment"><pre>            equation_systems.parameters.set&lt;bool&gt; ("test") = true;              </pre></div><div class = "comment">Set a simulation-specific parameter visible for all systems.This helps in inter-system-communication.</div><div class ="fragment"><pre>            equation_systems.parameters.set&lt;Real&gt; ("dummy") = 42.;              </pre></div><div class = "comment">Set another simulation-specific parameter </div><div class ="fragment"><pre>            equation_systems.parameters.set&lt;Real&gt; ("nobody") = 0.;            </pre></div><div class = "comment">Now we declare the system and its variables.We begin by adding a "SteadyStytem" to theEquationSystems object, and we give it the name"Simple System".</div><div class ="fragment"><pre>            equation_systems.add_system&lt;TransientLinearImplicitSystem&gt; ("Simple System");              </pre></div><div class = "comment">Adds the variable "u" to "Simple System".  "u"will be approximated using first-order approximation.</div><div class ="fragment"><pre>            equation_systems.get_system("Simple System").add_variable("u", FIRST);              </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 the equation system if the user specified anoutput file name.  Note that there are two possibleformats to write to.  Specifying libMeshEnums::WRITE willcreate a formatted ASCII file.  Optionally, you can specifylibMeshEnums::ENCODE and get an XDR-encoded binary file.<br><br>We will write the data, clear the object, and read the filewe just wrote.  This is simply to demonstrate capability.Note that you might use this in an application to periodicallydump the state of your simulation.  You can then restart fromthis data later.</div><div class ="fragment"><pre>            if (argc == 2)              if (argv[1][0] != '-')        	{        	  std::cout &lt;&lt; "&lt;&lt;&lt; Writing system to file " &lt;&lt; argv[1]        		    &lt;&lt; std::endl;        	  </pre></div><div class = "comment">Write the system.</div><div class ="fragment"><pre>                  equation_systems.write (argv[1], libMeshEnums::WRITE);        	  </pre></div><div class = "comment">Clear the equation systems data structure.</div><div class ="fragment"><pre>                  equation_systems.clear ();                	  std::cout &lt;&lt; "&gt;&gt;&gt; Reading system from file " &lt;&lt; argv[1]        		    &lt;&lt; std::endl &lt;&lt; std::endl;        	  </pre></div><div class = "comment">Read the file we just wrote.  This betterwork!</div><div class ="fragment"><pre>                  equation_systems.read (argv[1], libMeshEnums::READ);        </pre></div><div class = "comment">Print the information again.</div><div class ="fragment"><pre>                  equation_systems.print_info();        	}            </pre></div><div class = "comment">All our objects will be destroyed at this closing brace.That way we can safely call PetscFinalize() and be sure wedon't have any Petsc-dependent objects lurking around!</div><div class ="fragment"><pre>          }        </pre></div><div class = "comment">Call libMesh::close(), which in turn finalizes PETSc and/orMPI.</div><div class ="fragment"><pre>          return libMesh::close();        }</pre></div><a name="nocomments"></a> <br><br><br> <h1> The program without comments: </h1> <pre>     #include &lt;iostream&gt;  #include <B><FONT COLOR="#BC8F8F">&quot;libmesh.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;equation_systems.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;linear_implicit_system.h&quot;</FONT></B>  #include <B><FONT COLOR="#BC8F8F">&quot;transient_system.h&quot;</FONT></B>    <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);    {      <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;            Mesh mesh (2);            <B><FONT COLOR="#5F9EA0">MeshTools</FONT></B>::Generation::build_cube (mesh, 5, 5);            mesh.print_info();        EquationSystems equation_systems (mesh);            equation_systems.parameters.set&lt;<B><FONT COLOR="#228B22">bool</FONT></B>&gt; (<B><FONT COLOR="#BC8F8F">&quot;test&quot;</FONT></B>) = true;              equation_systems.parameters.set&lt;Real&gt; (<B><FONT COLOR="#BC8F8F">&quot;dummy&quot;</FONT></B>) = 42.;              equation_systems.parameters.set&lt;Real&gt; (<B><FONT COLOR="#BC8F8F">&quot;nobody&quot;</FONT></B>) = 0.;            equation_systems.add_system&lt;TransientLinearImplicitSystem&gt; (<B><FONT COLOR="#BC8F8F">&quot;Simple System&quot;</FONT></B>);              equation_systems.get_system(<B><FONT COLOR="#BC8F8F">&quot;Simple System&quot;</FONT></B>).add_variable(<B><FONT COLOR="#BC8F8F">&quot;u&quot;</FONT></B>, FIRST);              equation_systems.init();              equation_systems.print_info();        <B><FONT COLOR="#A020F0">if</FONT></B> (argc == 2)        <B><FONT COLOR="#A020F0">if</FONT></B> (argv[1][0] != <B><FONT COLOR="#BC8F8F">'-'</FONT></B>)  	{  	  <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;&lt;&lt;&lt; Writing system to file &quot;</FONT></B> &lt;&lt; argv[1]  		    &lt;&lt; std::endl;  	    	  equation_systems.write (argv[1], libMeshEnums::WRITE);  	    	  equation_systems.clear ();    	  <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout &lt;&lt; <B><FONT COLOR="#BC8F8F">&quot;&gt;&gt;&gt; Reading system from file &quot;</FONT></B> &lt;&lt; argv[1]  		    &lt;&lt; std::endl &lt;&lt; std::endl;  	    	  equation_systems.read (argv[1], libMeshEnums::READ);    	  equation_systems.print_info();  	}          }      <B><FONT COLOR="#A020F0">return</FONT></B> libMesh::close();  }</pre> <a name="output"></a> <br><br><br> <h1> The console output of the program: </h1> <pre>**************************************************************** Running Example  ./ex2-devel*************************************************************** Running ./ex2-devel Mesh Information:  mesh_dimension()=2  spatial_dimension()=3  n_nodes()=36  n_elem()=25   n_local_elem()=25   n_active_elem()=25  n_subdomains()=1  n_processors()=1  processor_id()=0 EquationSystems  n_systems()=1   System "Simple System"    Type "TransientLinearImplicit"    Variables="u"     Finite Element Types="LAGRANGE"     Approximation Orders="FIRST"     n_dofs()=36    n_local_dofs()=36    n_constrained_dofs()=0    n_vectors()=3 Running ./ex2-devel eqn_sys.dat Mesh Information:  mesh_dimension()=2  spatial_dimension()=3  n_nodes()=36  n_elem()=25   n_local_elem()=25   n_active_elem()=25  n_subdomains()=1  n_processors()=1  processor_id()=0 EquationSystems  n_systems()=1   System "Simple System"    Type "TransientLinearImplicit"    Variables="u"     Finite Element Types="LAGRANGE"     Approximation Orders="FIRST"     n_dofs()=36    n_local_dofs()=36    n_constrained_dofs()=0    n_vectors()=3<<< Writing system to file eqn_sys.dat>>> Reading system from file eqn_sys.dat EquationSystems  n_systems()=1   System "Simple System"    Type "TransientLinearImplicit"    Variables="u"     Finite Element Types="LAGRANGE"     Approximation Orders="FIRST"     n_dofs()=36    n_local_dofs()=36    n_constrained_dofs()=0    n_vectors()=3 **************************************************************** Done Running Example  ./ex2-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 + -