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