📄 ex12.php
字号:
<?php $root=""; ?><?php require($root."navigation.php"); ?><html><head> <?php load_style($root); ?></head> <body> <?php make_navigation("ex12",$root)?> <div class="content"><a name="comments"></a> <div class = "comment"><h1>Example 12 - The <code>MeshData</code> class</h1><br><br>The previous examples covered the certainly involvedaspects of simulating multiple equation systems, and prior to this even using adaptive refinement. I.e.: cool stuff.<br><br>This example now reduces brain effort significantly,and presents some supplements concerning data I/O,especially how to handle the <code> MeshData </code> object.The <code> MeshData </code> class may be used for handling inputdata for a simulation, like actual material properties, (not indicators, like in the <code> BoundaryInfo </code> class),or mode shapes that serve as input for acoustic radiationsimulations. The use of the <code> MeshData </code> during simulationis straightforward: the <ul><li><code>Number MeshData::operator()(const Node*, int)</code>,get the i-th floating-point value associated with the node</li><li><code> bool MeshData::has_data(const Node*)</code>,verify whether a certain node has data associated</li><li><code>std::vector<Number>& MeshData::get_data (const Node* node)</code>to get read-access to the data associated with this node (bettermake sure first that this node <i>has</i> data, see <code> has_data() )</li><li>iterator for nodal data <code>MeshData::const_node_data_iterator</code>to directly iterate over the set of nodes that hold data, instead of asking through <code>has_data()</code> each time again.</li></ul>(and corresponding methods for <code> const Elem*</code>) provide access tothe floating-point data associated with nodes/elements.This example does <i>not</i> show how to use these aforementionedmethods, this is straightforward. Simply check if the current <code>Elem*</code> has a node with data. If so, add this contribution to the RHS, or whatever. Or ask the <code> MeshData </code> for each <code>Elem*</code> for its material properties...<br><br>Here, only the actual file I/O necessary to handle such nodal/element data is presented. Lean back, relax...<br><br>C++ include files that we need.</div><div class ="fragment"><pre> #include <math.h> </pre></div><div class = "comment">Functions to initialize the libraryand provide some further features (e.g. our own pi)</div><div class ="fragment"><pre> #include "libmesh.h" </pre></div><div class = "comment">Basic include files needed for the mesh and <code> MeshData </code> functionality.</div><div class ="fragment"><pre> #include "mesh.h" #include "mesh_tools.h" #include "mesh_data.h" #include "unv_io.h" #include "gmv_io.h" </pre></div><div class = "comment">The definition of a geometric vertex associated with a Mesh</div><div class ="fragment"><pre> #include "node.h" </pre></div><div class = "comment">Function prototype for creating artificial nodal datathat can be inserted into a <code>MeshData</code> object.</div><div class ="fragment"><pre> void create_artificial_data (const Mesh& mesh, std::map<const Node*, std::vector<Number> >& art_data); </pre></div><div class = "comment">The main program.</div><div class ="fragment"><pre> int main (int argc, char** argv) {</pre></div><div class = "comment">Initialize the library.</div><div class ="fragment"><pre> libMesh::init (argc, argv); </pre></div><div class = "comment">Force all our objects to have local scope. </div><div class ="fragment"><pre> { </pre></div><div class = "comment">Check for proper usage. The program is designed to be runas follows:<pre>$ ./ex12 -d 3 in_mesh.unv</pre>where in_mesh.unv should be a Universal file.</div><div class ="fragment"><pre> if (argc < 4) { std::cerr << "Usage: " << argv[0] << " -d <dim> in_mesh.unv" << std::endl; error(); } </pre></div><div class = "comment">Get the dimensionality of the mesh from argv[2]</div><div class ="fragment"><pre> const unsigned int dim = std::atoi(argv[2]); </pre></div><div class = "comment">The filename of the mesh</div><div class ="fragment"><pre> const std::string mesh_file = argv[3]; </pre></div><div class = "comment">The following example makes currently senseonly with a Universal file</div><div class ="fragment"><pre> if (mesh_file.rfind(".unv") >= mesh_file.size()) { std::cerr << "ERROR: This example works only properly with a Universal mesh file!" << std::endl; error(); } </pre></div><div class = "comment">Some notes on <code>MeshData</code>:<ul><li>The <code>MeshData</code> is <i>not</i> a mesh! Consult the <code>Mesh</code>,<code>MeshBase</code>, and <code>BoundaryMesh</code> classes for details onhandling meshes!</li><li>The <code>MeshData</code> is an object handling arbitrary floating-point data associated with mesh entities (nodes, elements), currentlymost features only available for nodal data. However,extending to element-data (does <i>anybody</i> need this?)is straightforward.</li><li>Currently std::vector<Number> is the data (associatedwith nodes/elements) that can be handled by <code>MeshData</code>,</li><li>In order to provide <i>full</i> functionality, the <code>MeshData</code><i>has</i> to be activated prior to reading in a mesh. However,there is a so-called compatibility mode available when you (intentionally) forgot to "turn on" the <code>MeshData</code>.</li><li>It is possible to provide user-defined nodal data thatmay subsequently be used or written to file.</li><li>Translate the nodal-/element-associated data to formatssuitable for visual inspection, e.g. GMV files.</li><li>Two I/O file formats are currently supported: the Universal file format with dataset 2414, and an extension of the libMesh-own xda/xdr format, named xtr, xta.Some details on this:</li><li>The xtr/xta format is simply an extension of thexdr/xda format to read/write also nodal/element-associatedfloating-point data. The xtr interface of the <code>MeshData</code>creates stand-alone files that may be binary or ASCII.You cannot append files created by the <code>MeshData</code> I/O methodsto a mesh file (xdr/xda).The xtr files may be seen as a "backdrop", especially whenbinary files are preferred. Note that unv files are <i>always</i>ASCII and may become pretty big!</li><li>The unv format is an extremely versatile text-based file formatfor arbitrary data. Its functionality is <i>large</i>, and <code>libMesh</code>supports only a small share: namely the I/O for nodes, elements andarbitrary node-/element-associated data, stored in the so-called datasets "2411", "2412", and "2414", respectively.Here, only the last dataset is covered. The two first datasets areimplemented in the Universal support for I/O of meshes. A singleunv file may hold <i>multiple</i> datasets of type "2414". Todistinguish data, each dataset has a header. The <code>libMesh</code> pendantto this header is the <code>MeshDataUnvHeader</code> class. When youread/write unv files using the <code>MeshData</code>, you <i>always</i>automatically also read/write such headers.</li></ul>Enough babble for now. Examples. </div><div class ="fragment"><pre> {</pre></div><div class = "comment">Create a mesh with the requested dimension.Below we require a 3-dim mesh, therefore assertit.</div><div class ="fragment"><pre> assert (dim == 3); Mesh mesh(dim); MeshData mesh_data(mesh); </pre></div><div class = "comment">Activate the <code>MeshData</code> of the mesh, so thatwe can remember the node and element ids usedin the file. When we do not activate the <code>MeshData</code>,then there is no chance to import node- or element-associated data.</div><div class ="fragment"><pre> mesh_data.activate(); </pre></div><div class = "comment">Now we can safely read the input mesh. Note thatthis should be an .xda/.xdr or .unv file only, otherwisewe cannot load/save associated data.</div><div class ="fragment"><pre> mesh.read (mesh_file, &mesh_data); </pre></div><div class = "comment">Print information about the mesh and the datato the screen. Obviously, there is no data(apart from node & element ids) in it, yet.</div><div class ="fragment"><pre> std::cout << std::endl << "Finished reading the mesh. MeshData is active but empty:" << std::endl << "---------------------------------------------------------" << std::endl; mesh.print_info(); mesh_data.print_info(); </pre></div><div class = "comment">Create some artificial node-associated data and storeit in the mesh's <code>MeshData</code>. Use a <code>std::map</code> for this. Let the function <code>create_artificial_data()</code> do the work.</div><div class ="fragment"><pre> { std::map<const Node*, std::vector<Number> > artificial_data; create_artificial_data (mesh, artificial_data); </pre></div><div class = "comment">Before we let the map go out of scope, insert it intothe <code>MeshData</code>. Note that by default the element-associateddata containers are closed, so that the <code>MeshData</code> isready for use.</div><div class ="fragment"><pre> mesh_data.insert_node_data(artificial_data); </pre></div><div class = "comment">Let <code>artificial_data()</code> go out of scope</div><div class ="fragment"><pre> } </pre></div><div class = "comment">Print information about the data to the screen.</div><div class ="fragment"><pre> std::cout << std::endl << "After inserting artificial data into the MeshData:" << std::endl << "--------------------------------------------------" << std::endl; mesh_data.print_info(); </pre></div><div class = "comment">Write the artificial data into a universalfile. Use a default header for this.</div><div class ="fragment"><pre> std::string first_out_data="data_first_out_with_default_header.unv"; std::cout << "Writing MeshData to: " << first_out_data << std::endl; mesh_data.write(first_out_data); </pre></div><div class = "comment">Alternatively, create your own header.</div><div class ="fragment"><pre> std::cout << std::endl << "Attach our own MeshDataUnvHeader to the MeshData:" << std::endl << "-------------------------------------------------" << std::endl << " (note the warning: the number of values per node in" << std::endl << " my_header is not correct)" << std::endl << std::endl; MeshDataUnvHeader my_header; </pre></div><div class = "comment">Specify an int for this dataset.This is particularly helpfulwhen there are multiple datasets ina file and you want to find a specific one.For this, use MeshDataUnvHeader::which_dataset(int),which is not covered here.</div><div class ="fragment"><pre> my_header.dataset_label = 3; </pre></div><div class = "comment">Specify some text that helps the <i>user</i> identify the data. This text is <i>not</i> used forfinding a specific dataset. Leave default forthe remaining 2 lines.</div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -