?? ex12.c
字號:
<< "-------------------------------------------------" << 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; // Specify an int for this dataset. // This is particularly helpful // when there are multiple datasets in // a file and you want to find a specific one. // For this, use MeshDataUnvHeader::which_dataset(int), // which is not covered here. my_header.dataset_label = 3; // Specify some text that helps the <i>user</i> // identify the data. This text is <i>not</i> used for // finding a specific dataset. Leave default for // the remaining 2 lines. 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"; // Also some float data, not associated with nodes, // can be stored in the header. my_header.record_12[0] = libMesh::pi; // Now attach this header to the <code>MeshData</code>, and write // the same file again, but with the personalized header. mesh_data.set_unv_header(&my_header); // Write again to file. 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); // Print information about the data to the screen. std::cout << std::endl << "Before clearing the MeshData:" << std::endl << "-----------------------------" << std::endl; mesh_data.print_info(); // Now clear only the data associated with nodes/elements, // but keep the node/element ids used in the mesh. To // clear also these ids, use MeshData::slim() (not used here). mesh_data.clear(); // Print information about the data to the screen. std::cout << std::endl << "After clearing the MeshData:" << std::endl << "----------------------------" << std::endl; mesh_data.print_info(); // Now the <code>MeshData</code> is open again to read data from // file. Read the file that we created first. mesh_data.read(first_out_data); std::cout << std::endl << "After re-reading the first file:" << std::endl << "--------------------------------" << std::endl; mesh_data.print_info(); // Let the mesh, the unv_header etc go out of scope, and // do another example. } std::cout << std::endl << "----------------------------------------------" << std::endl << "---------- next example with MeshData --------" << std::endl << "----------------------------------------------" << std::endl; // Create a new mesh, read it again -- but this time // with de-activated <code>MeshData</code>. Then we are // able to use the compatibility mode not only for // handling <code>MeshData</code> dat, but also to <i>write</i> // a mesh in unv format. // The libMesh-internal node and element ids are used. { Mesh mesh(dim); MeshData mesh_data(mesh); // Read the input mesh, but with deactivated <code>MeshData</code>. mesh.read(mesh_file, &mesh_data); // Print information about the mesh and the data // to the screen. std::cout << std::endl << "De-activated MeshData:" << std::endl << "----------------------" << std::endl; mesh.print_info(); mesh_data.print_info(); // Write the <i>mesh</i> (not the MeshData!) as .unv file. // In general, the <code>MeshBase</code> interface for .unv I/O // needs an active <code>MeshData</code>. However, use compatibility // mode to at least write a .unv file, but with the // ids from libMesh. 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); // Again create some artificial node-associated data, // as before. { std::map<const Node*, std::vector<Number> > artificial_data; create_artificial_data (mesh, artificial_data); mesh_data.insert_node_data(artificial_data); } // Note that even with (only) compatibility mode MeshData // data can be written. But again, the ids from libMesh // are used. Consult the warning messages issued in // DEBUG mode. And the user <i>has</i> to specify that the // <code>MeshData</code> should change to compatibility mode. mesh_data.enable_compatibility_mode(); // Now that compatibility mode is used, data can be written. // _Without_ explicitly enabling compatibility mode, we // would get an error message and (with the following write() // statement). 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 // As may already seen, UNV files are text-based, so they may // become 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. // // Note that this works also for mesh files, not only for // meshdata files. // // In order to write a ".unv.gz" file instead of a ".unv" file, // simply provide the full name with ".gz" appended to the // write method; it will then figure out whether this file should // be gzip'ed or not. 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 // And now a last gimmick: The <code>MeshData::translate()</code> // conveniently converts the nodal- or element-associated // data (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>. Particularly // useful for <i>inspecting</i> the data contained in <code> MeshData</code>. // // And even better: the user can choose the mesh for which // to 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>). // // All in all not bad, hm? { // have a vector for the actual values and a vector // for the names of the data available. std::vector<Number> translated_data; std::vector<std::string> data_names; // Use the <code> mesh</code> itself. Alternatively, use the // <code> BoundaryMesh</code> of <code> mesh</code>. mesh_data.translate (mesh, translated_data, data_names); // And write the data to a GMV file 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); // Let the vectors with translated data // go out of scope. } // Let the second mesh go out of scope. } // All done. return 0;}// This function creates the data to populate the <code> MeshData</code> objectvoid create_artificial_data (const Mesh& mesh, std::map<const Node*, std::vector<Number> >& art_data){ // get the bounding box to have some sensible data MeshTools::BoundingBox b_box = MeshTools::bounding_box(mesh); const Real z_min = b_box.first (2); const Real z_max = b_box.second(2); libmesh_assert (fabs(z_max-z_min) > TOLERANCE); const Real x_min = b_box.first (0); const Real x_max = b_box.second(0); libmesh_assert (fabs(x_max-x_min) > TOLERANCE); // const_node_iterator node_it = mesh.nodes_begin();// const const_node_iterator node_end = mesh.nodes_end(); 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) { // All the vectors in <code> artificial</code>_data <i>have</i> to have the // same size. Here we use only two entries per node, // but theoretically arbitrary size is possible. std::vector<Number> node_data; node_data.resize(2); // Use a sin curve in z-direction and a linear // increase in x-direction 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; // Insert the current data together with a pointer to // the current node in the map. art_data.insert (std::make_pair(*node_it,node_data)); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -