Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
LoadPartial.cpp
Go to the documentation of this file.
1 /**
2  * @file LoadPartial.cpp
3  * @brief Example demonstrating partial loading of mesh files based on tag values
4  *
5  * This example shows how to:
6  * - Load only specific parts of a mesh file based on tag values
7  * - Use parallel partition tags to load specific partitions
8  * - Handle file organization in sets for selective loading
9  * - Work with HANDLEID tags for entity identification
10  * - Write partial mesh data to new files
11  *
12  * This is particularly useful for parallel applications where each
13  * processor needs to load only its assigned portion of a large mesh.
14  *
15  * @author MOAB Development Team
16  * @date 2024
17  *
18 
19  * \brief Load a part of a file \n
20  * <b>To run</b>: LoadPartial [file] [tag_name] [val1] [val2] ...\n
21  *
22  * In this example, it is shown how to load only a part of one file; the file must be organized in
23  * sets. (cherry-picking only the sets we want) The sets to load are identified by a tag name and
24  * the tag values for the sets of interest. This procedure is used when reading in parallel, as
25  * each processor will load only its part of the file, identified either by partition or by
26  * material/block sets by default, this example will load parallel partition sets with values 1, 2,
27  * and 5 from ../MeshFiles/unittest/64bricks_1khex.h5m The example will always write the output to a
28  * file name part.h5m
29  *
30  * @param argc Number of command line arguments
31  * @param argv Command line arguments array
32  * @return 0 on success, 1 on failure
33  */
34 
35 #include <iostream>
36 #include <vector>
37 
38 // Include header for MOAB instance and tag conventions for
39 #include "moab/Core.hpp"
40 #include "MBTagConventions.hpp"
41 
42 #define NTAGVALS 3
43 
44 using namespace moab;
45 using namespace std;
46 
47 // Function to parse input parameters
48 ErrorCode get_file_options( int argc, char** argv, string& filename, string& tagName, vector< int >& tagValues )
49 {
50  // Get mesh filename
51  if( argc > 1 )
52  filename = string( argv[1] );
53  else
54  filename = string( MESH_DIR ) + string( "/64bricks_512hex_256part.h5m" );
55 
56  // Get tag selection options
57  if( argc > 2 )
58  tagName = string( argv[2] );
59  else
60  tagName = "USERTAG";
61 
62  if( argc > 3 )
63  {
64  tagValues.resize( argc - 3, 0 );
65  for( int i = 3; i < argc; ++i )
66  tagValues[i - 3] = atoi( argv[i] );
67  }
68  else
69  {
70  for( unsigned i = 0; i < tagValues.size(); ++i )
71  tagValues[i] = 2 * i + 1;
72  }
73 
74  if( argc > 1 && argc < 4 ) // print usage
75  cout << " usage is " << argv[0] << " <file> \c tag_name <value1> <value2> .. \n";
76  return MB_SUCCESS;
77 }
78 
79 int main( int argc, char** argv )
80 {
81  // Get MOAB instance
82  Interface* mb = new( std::nothrow ) Core;
83  if( NULL == mb ) return 1;
84 
85  std::string filename, tagname;
86  vector< int > tagvals( NTAGVALS ); // Allocate for a maximum of 5 tag values
87  MB_CHK_ERR( get_file_options( argc, argv, filename, tagname, tagvals ) );
88 
89 #ifdef MOAB_HAVE_HDF5
90  // This file is in the mesh files directory
91  MB_CHK_SET_ERR( mb->load_file( filename.c_str(), 0, 0, PARALLEL_PARTITION_TAG_NAME, tagvals.data(),
92  (int)tagvals.size() ),
93  "Failed to read" );
94 
95  // If HANDLEID tag present, convert to long, and see what we read from file
96  Tag handleid_tag;
97  ErrorCode rval = mb->tag_get_handle( "HANDLEID", handleid_tag );
98  if( MB_SUCCESS == rval )
99  {
100  // Convert a few values for a few vertices
101  Range verts;
102  MB_CHK_SET_ERR( mb->get_entities_by_type( 0, MBVERTEX, verts ), "Failed to get vertices" );
103  vector< long > valsTag( verts.size() );
104  rval = mb->tag_get_data( handleid_tag, verts, &valsTag[0] );
105  if( MB_SUCCESS == rval ) cout << "First 2 long values recovered: " << valsTag[0] << " " << valsTag[1] << "\n";
106  }
107 
108  MB_CHK_SET_ERR( mb->write_file( "part.h5m" ), "Failed to write partial file" );
109  cout << "Wrote successfully part.h5m.\n";
110 
111 #else
112  std::cout << "Configure MOAB with HDF5 to build and use this example correctly.\n";
113 #endif
114  delete mb;
115  return 0;
116 }