Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
LoadPartial.cpp File Reference

Example demonstrating partial loading of mesh files based on tag values. More...

#include <iostream>
#include <vector>
#include "moab/Core.hpp"
#include "MBTagConventions.hpp"
+ Include dependency graph for LoadPartial.cpp:

Go to the source code of this file.

Macros

#define NTAGVALS   3
 

Functions

ErrorCode get_file_options (int argc, char **argv, string &filename, string &tagName, vector< int > &tagValues)
 
int main (int argc, char **argv)
 

Detailed Description

Example demonstrating partial loading of mesh files based on tag values.

This example shows how to:

  • Load only specific parts of a mesh file based on tag values
  • Use parallel partition tags to load specific partitions
  • Handle file organization in sets for selective loading
  • Work with HANDLEID tags for entity identification
  • Write partial mesh data to new files

This is particularly useful for parallel applications where each processor needs to load only its assigned portion of a large mesh.

Author
MOAB Development Team
Date
2024

Load a part of a file
To run: LoadPartial [file] [tag_name] [val1] [val2] ...
In this example, it is shown how to load only a part of one file; the file must be organized in sets. (cherry-picking only the sets we want) The sets to load are identified by a tag name and the tag values for the sets of interest. This procedure is used when reading in parallel, as each processor will load only its part of the file, identified either by partition or by material/block sets by default, this example will load parallel partition sets with values 1, 2, and 5 from ../MeshFiles/unittest/64bricks_1khex.h5m The example will always write the output to a file name part.h5m

Parameters
argcNumber of command line arguments
argvCommand line arguments array
Returns
0 on success, 1 on failure

Definition in file LoadPartial.cpp.

Macro Definition Documentation

◆ NTAGVALS

#define NTAGVALS   3

Definition at line 42 of file LoadPartial.cpp.

Function Documentation

◆ get_file_options()

ErrorCode get_file_options ( int  argc,
char **  argv,
string &  filename,
string &  tagName,
vector< int > &  tagValues 
)

Definition at line 48 of file LoadPartial.cpp.

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 }

References MB_SUCCESS, and MESH_DIR.

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 79 of file LoadPartial.cpp.

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 }

References ErrorCode, moab::Core::get_entities_by_type(), get_file_options(), moab::Core::load_file(), mb, MB_CHK_ERR, MB_CHK_SET_ERR, MB_SUCCESS, MBVERTEX, NTAGVALS, PARALLEL_PARTITION_TAG_NAME, moab::Range::size(), moab::Core::tag_get_data(), moab::Core::tag_get_handle(), and moab::Core::write_file().