📄 ex12.php
字号:
<div class ="fragment"><pre> my_header.id_lines_1_to_5[0] = "Artificial data"; my_header.id_lines_1_to_5[1] = "sin curve in z-direction"; my_header.id_lines_1_to_5[2] = "line in x-direction"; </pre></div><div class = "comment">Also some float data, not associated with nodes,can be stored in the header.</div><div class ="fragment"><pre> my_header.record_12[0] = libMesh::pi; </pre></div><div class = "comment">Now attach this header to the <code>MeshData</code>, and writethe same file again, but with the personalized header.</div><div class ="fragment"><pre> mesh_data.set_unv_header(&my_header); </pre></div><div class = "comment">Write again to file.</div><div class ="fragment"><pre> std::string second_out_data="data_second_with_header_out.unv"; std::cout << "Writing MeshData to: " << second_out_data << std::endl; mesh_data.write(second_out_data); </pre></div><div class = "comment">Print information about the data to the screen.</div><div class ="fragment"><pre> std::cout << std::endl << "Before clearing the MeshData:" << std::endl << "-----------------------------" << std::endl; mesh_data.print_info(); </pre></div><div class = "comment">Now clear only the data associated with nodes/elements,but keep the node/element ids used in the mesh. Toclear also these ids, use MeshData::slim() (not used here).</div><div class ="fragment"><pre> mesh_data.clear(); </pre></div><div class = "comment">Print information about the data to the screen.</div><div class ="fragment"><pre> std::cout << std::endl << "After clearing the MeshData:" << std::endl << "----------------------------" << std::endl; mesh_data.print_info(); </pre></div><div class = "comment">Now the <code>MeshData</code> is open again to read data fromfile. Read the file that we created first.</div><div class ="fragment"><pre> mesh_data.read(first_out_data); std::cout << std::endl << "After re-reading the first file:" << std::endl << "--------------------------------" << std::endl; mesh_data.print_info(); </pre></div><div class = "comment">Let the mesh, the unv_header etc go out of scope, anddo another example.</div><div class ="fragment"><pre> } std::cout << std::endl << "----------------------------------------------" << std::endl << "---------- next example with MeshData --------" << std::endl << "----------------------------------------------" << std::endl; </pre></div><div class = "comment">Create a new mesh, read it again -- but this timewith de-activated <code>MeshData</code>. Then we areable to use the compatibility mode not only forhandling <code>MeshData</code> dat, but also to <i>write</i> a mesh in unv format.The libMesh-internal node and element ids are used.</div><div class ="fragment"><pre> { Mesh mesh(dim); MeshData mesh_data(mesh); </pre></div><div class = "comment">Read the input mesh, but with deactivated <code>MeshData</code>.UNVIO unvio (mesh, mesh_data);unvio.read (mesh_file);</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.</div><div class ="fragment"><pre> std::cout << std::endl << "De-activated MeshData:" << std::endl << "----------------------" << std::endl; mesh.print_info(); mesh_data.print_info(); </pre></div><div class = "comment">Write the <i>mesh</i> (not the MeshData!) as .unv file.In general, the <code>MeshBase</code> interface for .unv I/Oneeds an active <code>MeshData</code>. However, use compatibilitymode to at least write a .unv file, but with the ids from libMesh.</div><div class ="fragment"><pre> const std::string out_mesh = "mesh_with_libmesh_ids.unv"; std::cout << "Writing _Mesh_ to: " << out_mesh << std::endl << "Try 'diff " << out_mesh << " " << mesh_file << "'" << std::endl << "to see the differences in node numbers." << std::endl << "---------------------------------------" << std::endl << std::endl; mesh.write(out_mesh, &mesh_data); </pre></div><div class = "comment">Again create some artificial node-associated data,as before.</div><div class ="fragment"><pre> { std::map<const Node*, std::vector<Number> > artificial_data; create_artificial_data (mesh, artificial_data); mesh_data.insert_node_data(artificial_data); } </pre></div><div class = "comment">Note that even with (only) compatibility mode MeshDatadata can be written. But again, the ids from libMeshare used. Consult the warning messages issued inDEBUG mode. And the user <i>has</i> to specify that the<code>MeshData</code> should change to compatibility mode.</div><div class ="fragment"><pre> mesh_data.enable_compatibility_mode(); </pre></div><div class = "comment">Now that compatibility mode is used, data can be written._Without_ explicitly enabling compatibility mode, wewould get an error message and (with the following write()statement).</div><div class ="fragment"><pre> std::string mesh_data_file = "data_third_with_libmesh_ids_out.unv"; std::cout << std::endl << "Writing MeshData to: " << mesh_data_file << std::endl << "----------------------------------------------------------" << std::endl << std::endl; mesh_data.write (mesh_data_file); #ifdef HAVE_ZLIB_H </pre></div><div class = "comment">As may already seen, UNV files are text-based, so they maybecome really big. When <code>./configure</code> found <code>zlib.h</code>,then we may also <i>read</i> or <i>write</i> <code>.unv</code> files in gzip'ed format! -- Pretty cool, and also pretty fast, due to zlib.h.<br><br>Note that this works also for mesh files, not only for meshdata files.<br><br>In order to write a ".unv.gz" file instead of a ".unv" file,simply provide the full name with ".gz" appended to thewrite method; it will then figure out whether this file shouldbe gzip'ed or not.</div><div class ="fragment"><pre> std::string packed_mesh_data_file = "packed_" + mesh_data_file + ".gz"; std::cout << std::endl << "Writing gzip'ed MeshData to: " << packed_mesh_data_file << std::endl << "---------------------------------------------------------------------------" << std::endl << " To verify the integrity of the packed version, type:" << std::endl << std::endl << " gunzip " << packed_mesh_data_file << "; " << std::endl << " diff packed_" << mesh_data_file << " " << mesh_data_file << std::endl << std::endl; mesh_data.write (packed_mesh_data_file); #endif </pre></div><div class = "comment">And now a last gimmick: The <code>MeshData::translate()</code>conveniently converts the nodal- or element-associateddata (currently only nodal) to vectors that may be used for writing a mesh with "solution" vectors, where the solution vector contains the data from the <code>MeshData</code>. Particularlyuseful for <i>inspecting</i> the data contained in <code> MeshData</code>.<br><br>And even better: the user can choose the mesh for whichto export the data. E.g. not only use the <code> mesh</code>itself, but alternatively use the <code> BoundaryMesh </code>(any mesh that uses the <i>same nodes</i>, i.e. the <code> Node*</code> have to be the same. Only exception that will not work:A mesh created using <code> Mesh::create_submesh() </code>actually will <i>not</i> work with <code> MeshData::translate() </code>).<br><br>All in all not bad, hm?</div><div class ="fragment"><pre> {</pre></div><div class = "comment">have a vector for the actual values and a vectorfor the names of the data available.</div><div class ="fragment"><pre> std::vector<Number> translated_data; std::vector<std::string> data_names; </pre></div><div class = "comment">Use the <code> mesh</code> itself. Alternatively, use the <code> BoundaryMesh</code> of <code> mesh</code>.</div><div class ="fragment"><pre> mesh_data.translate (mesh, translated_data, data_names); </pre></div><div class = "comment">And write the data to a GMV file</div><div class ="fragment"><pre> const std::string gmv_file = "data_and_mesh_out.gmv"; std::cout << std::endl << "Writing the data from the MeshData to the GMV file " << gmv_file << std::endl << "------------------------------------------------------------------------" << std::endl; GMVIO(mesh).write_nodal_data (gmv_file, translated_data, data_names); </pre></div><div class = "comment">Let the vectors with translated datago out of scope.</div><div class ="fragment"><pre> } </pre></div><div class = "comment">Let the second mesh go out of scope.</div><div class ="fragment"><pre> } } </pre></div><div class = "comment">All done.</div><div class ="fragment"><pre> return libMesh::close(); } </pre></div><div class = "comment">This function creates the data to populate the <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">get the bounding box to have some sensible data</div><div class ="fragment"><pre> MeshTools::BoundingBox b_box = MeshTools::bounding_box(mesh); const Real z_min = b_box.first (2); const Real z_max = b_box.second(2); assert (fabs(z_max-z_min) > TOLERANCE); const Real x_min = b_box.first (0); const Real x_max = b_box.second(0); assert (fabs(x_max-x_min) > TOLERANCE); </pre></div><div class = "comment">const_node_iterator node_it = mesh.nodes_begin();const const_node_iterator node_end = mesh.nodes_end();<br><br></div><div class ="fragment"><pre> MeshBase::const_node_iterator node_it = mesh.nodes_begin(); const MeshBase::const_node_iterator node_end = mesh.nodes_end(); for (; node_it != node_end; ++node_it) {</pre></div><div class = "comment">All the vectors in <code> artificial</code>_data <i>have</i> to have thesame size. Here we use only two entries per node,but theoretically arbitrary size is possible.</div><div class ="fragment">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -