Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
ReadPartFile.cpp File Reference
#include "moab/Core.hpp"
#include <iostream>
#include <fstream>
#include <memory>
+ Include dependency graph for ReadPartFile.cpp:

Go to the source code of this file.

Macros

#define MESH_DIR   "."
 

Functions

int main (int argc, char **argv)
 

Variables

string test_file_name = string( MESH_DIR ) + string( "/3k-tri-sphere.vtk" )
 
string part_file_name
 
int nparts
 

Macro Definition Documentation

◆ MESH_DIR

#define MESH_DIR   "."
Examples
ReadPartFile.cpp.

Definition at line 33 of file ReadPartFile.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)
Examples
ReadPartFile.cpp.

Definition at line 41 of file ReadPartFile.cpp.

42 {
43  if( argc < 5 )
44  {
45  std::cerr << "Usage: " << argv[0] << " <input file> <part file> <#parts> <output file>\n";
46  return 1;
47  }
48  std::string mesh_file = argv[1];
49  std::string part_file = argv[2];
50  int nparts = std::stoi( argv[3] );
51  std::string out_file = argv[4];
52 
53  auto mb = std::make_unique< Core >();
54  MB_CHK_SET_ERR( mb ? MB_SUCCESS : MB_FAILURE, "Error: Could not allocate MOAB Core instance." );
55 
56  std::ifstream inFile( part_file );
57  MB_CHK_SET_ERR( inFile.is_open() ? MB_SUCCESS : MB_FAILURE, "Unable to open file " + part_file );
58 
59  MB_CHK_SET_ERR( mb->load_mesh( mesh_file.c_str() ), "Error: Could not load mesh file '" + mesh_file + "'" );
60 
61  Range sets;
62  MB_CHK_SET_ERR( mb->get_entities_by_type( 0, MBENTITYSET, sets ), "Error: Could not get entity sets." );
63  std::cout << "Number of sets is " << sets.size() << std::endl;
64 
65  Tag tag;
66  MB_CHK_SET_ERR( mb->tag_get_handle( "PARALLEL_PARTITION", tag ), "Error: Could not get PARALLEL_PARTITION tag." );
67 
68  int num_deleted_sets = 0;
69  for( auto it = sets.begin(); it != sets.end(); ++it )
70  {
71  EntityHandle eh = *it;
72  int val = -1;
73  MB_CHK_SET_ERR( mb->tag_get_data( tag, &eh, 1, &val ), "Error: Unable to get tag data" );
74  if( val != -1 )
75  {
76  num_deleted_sets++;
77  MB_CHK_SET_ERR( mb->delete_entities( &eh, 1 ), "Error: Unable to delete entities" );
78  }
79  }
80 
81  if( num_deleted_sets )
82  std::cout << "Deleted " << num_deleted_sets << " existing partition sets, and created new ones.\n";
83 
84  Range cells;
85  MB_CHK_SET_ERR( mb->get_entities_by_dimension( 0, 2, cells ), "Error: Could not get dimension-2 entities." );
86  std::vector< EntityHandle > psets( nparts );
87  for( int i = 0; i < nparts; i++ )
88  {
89  MB_CHK_SET_ERR( mb->create_meshset( MESHSET_SET, psets[i] ), "Error: Could not create meshset." );
90  MB_CHK_SET_ERR( mb->tag_set_data( tag, &( psets[i] ), 1, &i ), "Error: Could not set tag data." );
91  }
92 
93  for( auto it = cells.begin(); it != cells.end(); ++it )
94  {
95  int part;
96  EntityHandle eh = *it;
97  inFile >> part;
98  MB_CHK_SET_ERR( mb->add_entities( psets[part], &eh, 1 ), "Error: Could not add entity to partition set." );
99  }
100 
101  MB_CHK_SET_ERR( mb->write_file( out_file.c_str() ), "Error: Could not write output mesh file '" + out_file + "'" );
102  std::cout << "Partitioned mesh written to '" << out_file << "'.\n";
103 
104  return 0;
105 }

References moab::Core::add_entities(), moab::Range::begin(), moab::Core::create_meshset(), moab::Core::delete_entities(), moab::Range::end(), moab::Core::get_entities_by_dimension(), moab::Core::get_entities_by_type(), moab::Core::load_mesh(), mb, MB_CHK_SET_ERR, MB_SUCCESS, MBENTITYSET, MESHSET_SET, nparts, out_file, moab::Range::size(), moab::Core::tag_get_data(), moab::Core::tag_get_handle(), moab::Core::tag_set_data(), and moab::Core::write_file().

Variable Documentation

◆ nparts

◆ part_file_name

string part_file_name
Examples
ReadPartFile.cpp.

Definition at line 38 of file ReadPartFile.cpp.

◆ test_file_name

string test_file_name = string( MESH_DIR ) + string( "/3k-tri-sphere.vtk" )
Examples
ReadPartFile.cpp.

Definition at line 37 of file ReadPartFile.cpp.