📄 ex7.php
字号:
<?php $root=""; ?><?php require($root."navigation.php"); ?><html><head> <?php load_style($root); ?></head> <body> <?php make_navigation("ex7",$root)?> <div class="content"><a name="comments"></a> <div class = "comment"><h1>Example 7 - Introduction to Complex Numbers and the "FrequencySystem"</h1><br><br>This is the seventh example program. It builds onthe previous example programs, introduces complexnumbers and the FrequencySystem class to solve a simple Helmholtz equation grad(p)*grad(p)+(omega/c)^2*p=0,for multiple frequencies rather efficiently.<br><br>The FrequencySystem class offers two solution styles,namely to solve large systems, or to solvemoderately-sized systems fast, for multiple frequencies.The latter approach is implemented here.<br><br>This example uses an L--shaped mesh and nodal boundary datagiven in the files lshape.un and lshape_data.unv<br><br>For this example the library has to be compiled withcomplex numbers enabled. <br><br>C++ include files that we need</div><div class ="fragment"><pre> #include <iostream> #include <algorithm> #include <stdio.h> </pre></div><div class = "comment">Basic include files needed for overall functionality.</div><div class ="fragment"><pre> #include "libmesh.h" #include "libmesh_logging.h" #include "mesh.h" #include "mesh_generation.h" #include "gmv_io.h" #include "equation_systems.h" #include "elem.h" </pre></div><div class = "comment">Include FrequencySystem. Compared to GeneralSystem,this class offers added functionality for the solution of frequency-dependent systems.</div><div class ="fragment"><pre> #include "frequency_system.h" </pre></div><div class = "comment">Define the Finite Element object.</div><div class ="fragment"><pre> #include "fe.h" </pre></div><div class = "comment">Define Gauss quadrature rules.</div><div class ="fragment"><pre> #include "quadrature_gauss.h" </pre></div><div class = "comment">Define useful datatypes for finite elementmatrix and vector components.</div><div class ="fragment"><pre> #include "dense_matrix.h" #include "dense_vector.h" </pre></div><div class = "comment">Define matrix and vector data types for the global equation system. These are base classes,from which specific implementations, likethe PETSc or LASPACK implementations, are derived.</div><div class ="fragment"><pre> #include "sparse_matrix.h" #include "numeric_vector.h" </pre></div><div class = "comment">Define the DofMap, which handles degree of freedomindexing.</div><div class ="fragment"><pre> #include "dof_map.h" </pre></div><div class = "comment">Defines the MeshData class, which allows you to storedata about the mesh when reading in files, etc.</div><div class ="fragment"><pre> #include "mesh_data.h" </pre></div><div class = "comment">Function prototype. This is the function that will assemblethe mass, damping and stiffness matrices. It will <i>not</i>form an overall system matrix ready for solution.</div><div class ="fragment"><pre> void assemble_helmholtz(EquationSystems& es, const std::string& system_name); </pre></div><div class = "comment">Function prototype. This is the function that will combinethe previously-assembled mass, damping and stiffness matricesto the overall matrix, which then renders ready for solution.</div><div class ="fragment"><pre> void add_M_C_K_helmholtz(EquationSystems& es, const std::string& system_name); </pre></div><div class = "comment">Begin the main program. Note that this example onlyworks correctly if complex numbers have been enabledin the library. In order to link against the complexPETSc libraries, you must have built PETSc with the sameC++ compiler that you used to build libMesh. This isso that the name mangling will be the same for theroutines in both libraries.</div><div class ="fragment"><pre> int main (int argc, char** argv) {</pre></div><div class = "comment">Initialize Petsc, like in example 2.</div><div class ="fragment"><pre> libMesh::init (argc, argv); </pre></div><div class = "comment">This example is designed for complex numbers. </div><div class ="fragment"><pre> #ifndef USE_COMPLEX_NUMBERS std::cerr << "ERROR: This example is intended for " << std::endl << " use with complex numbers." << std::endl; here(); return 0; #else </pre></div><div class = "comment">Braces are used to force object scope, like in example 2</div><div class ="fragment"><pre> {</pre></div><div class = "comment">Check for proper usage.</div><div class ="fragment"><pre> if (argc < 3) { std::cerr << "Usage: " << argv[0] << " -f [frequency]" << std::endl; error(); } </pre></div><div class = "comment">Tell the user what we are doing.</div><div class ="fragment"><pre> else { std::cout << "Running " << argv[0]; for (int i=1; i<argc; i++) std::cout << " " << argv[i]; std::cout << std::endl << std::endl; } </pre></div><div class = "comment">For now, restrict to dim=2, though thismay easily be changed, see example 4</div><div class ="fragment"><pre> const unsigned int dim = 2; </pre></div><div class = "comment">Get the frequency from argv[2] as a <i>float</i>,currently, solve for 1/3rd, 2/3rd and 1/1th of the given frequency</div><div class ="fragment"><pre> const Real frequency_in = atof(argv[2]); const unsigned int n_frequencies = 3; </pre></div><div class = "comment">Create a dim-dimensional mesh.</div><div class ="fragment"><pre> Mesh mesh (dim); </pre></div><div class = "comment">Create a corresponding MeshDataand activate it. For more information on this objectcf. example 12.</div><div class ="fragment"><pre> MeshData mesh_data(mesh); mesh_data.activate(); </pre></div><div class = "comment">Read the mesh file. Here the file lshape.unv containsan L--shaped domain in .unv format.</div><div class ="fragment"><pre> mesh.read("lshape.unv", &mesh_data); </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">The load on the boundary of the domain is stored inthe .unv formated mesh data file lshape_data.unv.At this, the data is given as complex valued normalvelocities.</div><div class ="fragment"><pre> mesh_data.read("lshape_data.unv"); </pre></div><div class = "comment">Print information about the mesh to the screen.</div><div class ="fragment"><pre> mesh_data.print_info(); </pre></div><div class = "comment">Create an equation systems object, which now handlesa frequency system, as opposed to previous examples.Also pass a MeshData pointer so the data can be accessed in the matrix and rhs assembly.</div><div class ="fragment"><pre> EquationSystems equation_systems (mesh, &mesh_data); </pre></div><div class = "comment">Create a FrequencySystem named "Helmholtz" & store areference to it.</div><div class ="fragment"><pre> FrequencySystem & f_system = equation_systems.add_system<FrequencySystem> ("Helmholtz"); </pre></div><div class = "comment">Add the variable "p" to "Helmholtz". "p"will be approximated using second-order approximation.</div><div class ="fragment"><pre> f_system.add_variable("p", SECOND); </pre></div><div class = "comment">Tell the frequency system about the two user-providedfunctions. In other circumstances, at least thesolve function has to be attached.</div><div class ="fragment"><pre> f_system.attach_assemble_function (assemble_helmholtz); f_system.attach_solve_function (add_M_C_K_helmholtz); </pre></div><div class = "comment">To enable the fast solution scheme, additional<i>global</i> matrices and one global vector, all appropriately sized,have to be added. The system object takes care of theappropriate size, but the user should better fill explicitlythe sparsity structure of the overall matrix, so that thefast matrix addition method can be used, as will be shown later.</div><div class ="fragment"><pre> f_system.add_matrix ("stiffness"); f_system.add_matrix ("damping"); f_system.add_matrix ("mass"); f_system.add_vector ("rhs"); </pre></div><div class = "comment">Communicates the frequencies to the system. Note thatthe frequency system stores the frequencies as parametersin the equation systems object, so that our assemble and solvefunctions may directly access them.Will solve for 1/3rd, 2/3rd and 1/1th of the given frequency</div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -