📄 ex12.php
字号:
<pre> std::vector<Number> node_data; node_data.resize(2); </pre></div><div class = "comment">Use a sin curve in z-direction and a linearincrease in x-direction</div><div class ="fragment"><pre> const Point& p = **node_it; const Real z_normalized = (p(2)-z_min)/(z_max-z_min); const Real x_normalized = (p(0)-x_min)/(x_max-x_min); node_data[0] = sin(2*libMesh::pi*z_normalized); node_data[1] = x_normalized; </pre></div><div class = "comment">Insert the current data together with a pointer tothe current node in the map.</div><div class ="fragment"><pre> art_data.insert (std::make_pair(*node_it,node_data)); } }</pre></div><a name="nocomments"></a> <br><br><br> <h1> The program without comments: </h1> <pre> #include <math.h> #include <B><FONT COLOR="#BC8F8F">"libmesh.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"mesh.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"mesh_tools.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"mesh_data.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"unv_io.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"gmv_io.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"node.h"</FONT></B> <B><FONT COLOR="#228B22">void</FONT></B> create_artificial_data (<B><FONT COLOR="#228B22">const</FONT></B> Mesh& mesh, <B><FONT COLOR="#5F9EA0">std</FONT></B>::map<<B><FONT COLOR="#228B22">const</FONT></B> Node*, std::vector<Number> >& art_data); <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="#A020F0">if</FONT></B> (argc < 4) { <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr << <B><FONT COLOR="#BC8F8F">"Usage: "</FONT></B> << argv[0] << <B><FONT COLOR="#BC8F8F">" -d <dim> in_mesh.unv"</FONT></B> << std::endl; error(); } <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> dim = std::atoi(argv[2]); <B><FONT COLOR="#228B22">const</FONT></B> std::string mesh_file = argv[3]; <B><FONT COLOR="#A020F0">if</FONT></B> (mesh_file.rfind(<B><FONT COLOR="#BC8F8F">".unv"</FONT></B>) >= mesh_file.size()) { <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr << <B><FONT COLOR="#BC8F8F">"ERROR: This example works only properly with a Universal mesh file!"</FONT></B> << std::endl; error(); } { assert (dim == 3); Mesh mesh(dim); MeshData mesh_data(mesh); mesh_data.activate(); mesh.read (mesh_file, &mesh_data); <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << std::endl << <B><FONT COLOR="#BC8F8F">"Finished reading the mesh. MeshData is active but empty:"</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">"---------------------------------------------------------"</FONT></B> << std::endl; mesh.print_info(); mesh_data.print_info(); { <B><FONT COLOR="#5F9EA0">std</FONT></B>::map<<B><FONT COLOR="#228B22">const</FONT></B> Node*, std::vector<Number> > artificial_data; create_artificial_data (mesh, artificial_data); mesh_data.insert_node_data(artificial_data); } <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << std::endl << <B><FONT COLOR="#BC8F8F">"After inserting artificial data into the MeshData:"</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">"--------------------------------------------------"</FONT></B> << std::endl; mesh_data.print_info(); <B><FONT COLOR="#5F9EA0">std</FONT></B>::string first_out_data=<B><FONT COLOR="#BC8F8F">"data_first_out_with_default_header.unv"</FONT></B>; <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << <B><FONT COLOR="#BC8F8F">"Writing MeshData to: "</FONT></B> << first_out_data << std::endl; mesh_data.write(first_out_data); <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << std::endl << <B><FONT COLOR="#BC8F8F">"Attach our own MeshDataUnvHeader to the MeshData:"</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">"-------------------------------------------------"</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">" (note the warning: the number of values per node in"</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">" my_header is not correct)"</FONT></B> << std::endl << std::endl; MeshDataUnvHeader my_header; my_header.dataset_label = 3; my_header.id_lines_1_to_5[0] = <B><FONT COLOR="#BC8F8F">"Artificial data"</FONT></B>; my_header.id_lines_1_to_5[1] = <B><FONT COLOR="#BC8F8F">"sin curve in z-direction"</FONT></B>; my_header.id_lines_1_to_5[2] = <B><FONT COLOR="#BC8F8F">"line in x-direction"</FONT></B>; my_header.record_12[0] = libMesh::pi; mesh_data.set_unv_header(&my_header); <B><FONT COLOR="#5F9EA0">std</FONT></B>::string second_out_data=<B><FONT COLOR="#BC8F8F">"data_second_with_header_out.unv"</FONT></B>; <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << <B><FONT COLOR="#BC8F8F">"Writing MeshData to: "</FONT></B> << second_out_data << std::endl; mesh_data.write(second_out_data); <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << std::endl << <B><FONT COLOR="#BC8F8F">"Before clearing the MeshData:"</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">"-----------------------------"</FONT></B> << std::endl; mesh_data.print_info(); mesh_data.clear(); <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << std::endl << <B><FONT COLOR="#BC8F8F">"After clearing the MeshData:"</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">"----------------------------"</FONT></B> << std::endl; mesh_data.print_info(); mesh_data.read(first_out_data); <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << std::endl << <B><FONT COLOR="#BC8F8F">"After re-reading the first file:"</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">"--------------------------------"</FONT></B> << std::endl; mesh_data.print_info(); } <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << std::endl << <B><FONT COLOR="#BC8F8F">"----------------------------------------------"</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">"---------- next example with MeshData --------"</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">"----------------------------------------------"</FONT></B> << std::endl; { Mesh mesh(dim); MeshData mesh_data(mesh); mesh.read(mesh_file, &mesh_data); <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << std::endl << <B><FONT COLOR="#BC8F8F">"De-activated MeshData:"</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">"----------------------"</FONT></B> << std::endl; mesh.print_info(); mesh_data.print_info(); <B><FONT COLOR="#228B22">const</FONT></B> std::string out_mesh = <B><FONT COLOR="#BC8F8F">"mesh_with_libmesh_ids.unv"</FONT></B>; <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << <B><FONT COLOR="#BC8F8F">"Writing _Mesh_ to: "</FONT></B> << out_mesh << std::endl << <B><FONT COLOR="#BC8F8F">"Try 'diff "</FONT></B> << out_mesh << <B><FONT COLOR="#BC8F8F">" "</FONT></B> << mesh_file << <B><FONT COLOR="#BC8F8F">"'"</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">"to see the differences in node numbers."</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">"---------------------------------------"</FONT></B> << std::endl << std::endl; mesh.write(out_mesh, &mesh_data); { <B><FONT COLOR="#5F9EA0">std</FONT></B>::map<<B><FONT COLOR="#228B22">const</FONT></B> Node*, std::vector<Number> > artificial_data; create_artificial_data (mesh, artificial_data); mesh_data.insert_node_data(artificial_data); } mesh_data.enable_compatibility_mode(); <B><FONT COLOR="#5F9EA0">std</FONT></B>::string mesh_data_file = <B><FONT COLOR="#BC8F8F">"data_third_with_libmesh_ids_out.unv"</FONT></B>; <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << std::endl << <B><FONT COLOR="#BC8F8F">"Writing MeshData to: "</FONT></B> << mesh_data_file << std::endl << <B><FONT COLOR="#BC8F8F">"----------------------------------------------------------"</FONT></B> << std::endl << std::endl; mesh_data.write (mesh_data_file); #ifdef HAVE_ZLIB_H <B><FONT COLOR="#5F9EA0">std</FONT></B>::string packed_mesh_data_file = <B><FONT COLOR="#BC8F8F">"packed_"</FONT></B> + mesh_data_file + <B><FONT COLOR="#BC8F8F">".gz"</FONT></B>; <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << std::endl << <B><FONT COLOR="#BC8F8F">"Writing gzip'ed MeshData to: "</FONT></B> << packed_mesh_data_file << std::endl << <B><FONT COLOR="#BC8F8F">"---------------------------------------------------------------------------"</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">" To verify the integrity of the packed version, type:"</FONT></B> << std::endl << std::endl << <B><FONT COLOR="#BC8F8F">" gunzip "</FONT></B> << packed_mesh_data_file << <B><FONT COLOR="#BC8F8F">"; "</FONT></B> << std::endl << <B><FONT COLOR="#BC8F8F">" diff packed_"</FONT></B> << mesh_data_file << <B><FONT COLOR="#BC8F8F">" "</FONT></B> << mesh_data_file << std::endl << std::endl; mesh_data.write (packed_mesh_data_file); #endif { <B><FONT COLOR="#5F9EA0">std</FONT></B>::vector<Number> translated_data; <B><FONT COLOR="#5F9EA0">std</FONT></B>::vector<std::string> data_names; mesh_data.translate (mesh, translated_data, data_names); <B><FONT COLOR="#228B22">const</FONT></B> std::string gmv_file = <B><FONT COLOR="#BC8F8F">"data_and_mesh_out.gmv"</FONT></B>; <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << std::endl << <B><FONT COLOR="#BC8F8F">"Writing the data from the MeshData to the GMV file "</FONT></B> << gmv_file << std::endl << <B><FONT COLOR="#BC8F8F">"------------------------------------------------------------------------"</FONT></B> << std::endl; GMVIO(mesh).write_nodal_data (gmv_file, translated_data, data_names); } } } <B><FONT COLOR="#A020F0">return</FONT></B> libMesh::close(); } <B><FONT COLOR="#228B22">void</FONT></B> create_artificial_data (<B><FONT COLOR="#228B22">const</FONT></B> Mesh& mesh, <B><FONT COLOR="#5F9EA0">std</FONT></B>::map<<B><FONT COLOR="#228B22">const</FONT></B> Node*, std::vector<Number> >& art_data) { <B><FONT COLOR="#5F9EA0">MeshTools</FONT></B>::BoundingBox b_box = MeshTools::bounding_box(mesh); <B><FONT COLOR="#228B22">const</FONT></B> Real z_min = b_box.first (2); <B><FONT COLOR="#228B22">const</FONT></B> Real z_max = b_box.second(2); assert (fabs(z_max-z_min) > TOLERANCE); <B><FONT COLOR="#228B22">const</FONT></B> Real x_min = b_box.first (0); <B><FONT COLOR="#228B22">const</FONT></B> Real x_max = b_box.second(0); assert (fabs(x_max-x_min) > TOLERANCE); <B><FONT COLOR="#5F9EA0">MeshBase</FONT></B>::const_node_iterator node_it = mesh.nodes_begin(); <B><FONT COLOR="#228B22">const</FONT></B> MeshBase::const_node_iterator node_end = mesh.nodes_end(); <B><FONT COLOR="#A020F0">for</FONT></B> (; node_it != node_end; ++node_it) { <B><FONT COLOR="#5F9EA0">std</FONT></B>::vector<Number> node_data; node_data.resize(2); <B><FONT COLOR="#228B22">const</FONT></B> Point& p = **node_it; <B><FONT COLOR="#228B22">const</FONT></B> Real z_normalized = (p(2)-z_min)/(z_max-z_min); <B><FONT COLOR="#228B22">const</FONT></B> Real x_normalized = (p(0)-x_min)/(x_max-x_min); node_data[0] = sin(2*libMesh::pi*z_normalized); node_data[1] = x_normalized; art_data.insert (std::make_pair(*node_it,node_data)); } }</pre> <a name="output"></a> <br><br><br> <h1> The console output of the program: </h1> <pre>**************************************************************** Running Example ./ex12-devel -d 3 ../../examples/ex8/pipe-mesh.unv*************************************************************** Finished reading the mesh. MeshData is active but empty:--------------------------------------------------------- Mesh Information: mesh_dimension()=3 spatial_dimension()=3 n_nodes()=3977 n_elem()=3520 n_local_elem()=3520 n_active_elem()=3520 n_subdomains()=1 n_processors()=1 processor_id()=0 MeshData Information: object activated.After inserting artificial data into the MeshData:-------------------------------------------------- MeshData Information: object activated. Element associated data initialized. n_val_per_elem()=0 n_elem_data()=0 Node associated data initialized. n_val_per_node()=2 n_node_data()=3977Writing MeshData to: data_first_out_with_default_header.unvAttach our own MeshDataUnvHeader to the MeshData:------------------------------------------------- (note the warning: the number of values per node in my_header is not correct)Writing MeshData to: data_second_with_header_out.unvWARNING: nvaldc=3 of attached MeshDataUnvHeader object not valid! re-set nvaldc to 2Before clearing the MeshData:----------------------------- MeshData Information: object activated. Element associated data initialized. n_val_per_elem()=0 n_elem_data()=0 Node associated data initialized. n_val_per_node()=2 n_node_data()=3977After clearing the MeshData:---------------------------- MeshData Information: object activated.After re-reading the first file:-------------------------------- MeshData Information: object activated. Element associated data initialized. n_val_per_elem()=0 n_elem_data()=0 Node associated data initialized. n_val_per_node()=2 n_node_data()=3977-------------------------------------------------------- next example with MeshData ------------------------------------------------------De-activated MeshData:---------------------- Mesh Information: mesh_dimension()=3 spatial_dimension()=3 n_nodes()=3977 n_elem()=3520 n_local_elem()=3520 n_active_elem()=3520 n_subdomains()=1 n_processors()=1 processor_id()=0 MeshData neither active nor in compatibility mode.Writing _Mesh_ to: mesh_with_libmesh_ids.unvTry 'diff mesh_with_libmesh_ids.unv ../../examples/ex8/pipe-mesh.unv'to see the differences in node numbers.---------------------------------------************************************************************************** WARNING: MeshData neither active nor in compatibility mode. ** Enable compatibility mode for MeshData. Use this Universal ** file with caution: libMesh node and element ids are used. **************************************************************************Writing MeshData to: data_third_with_libmesh_ids_out.unv----------------------------------------------------------Writing gzip'ed MeshData to: packed_data_third_with_libmesh_ids_out.unv.gz--------------------------------------------------------------------------- To verify the integrity of the packed version, type: gunzip packed_data_third_with_libmesh_ids_out.unv.gz; diff packed_data_third_with_libmesh_ids_out.unv data_third_with_libmesh_ids_out.unvWriting the data from the MeshData to the GMV file data_and_mesh_out.gmv------------------------------------------------------------------------ **************************************************************** Done Running Example ./ex12-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 + -