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

Go to the source code of this file.

Classes

struct  anonymous_namespace{ReadWriteTest.cpp}::Config
 

Namespaces

 anonymous_namespace{ReadWriteTest.cpp}
 

Functions

void anonymous_namespace{ReadWriteTest.cpp}::print_usage (const char *program_name)
 
Config anonymous_namespace{ReadWriteTest.cpp}::parse_arguments (int argc, char **argv)
 
void anonymous_namespace{ReadWriteTest.cpp}::print_config (const Config &config, int rank, int nprocs)
 
int main (int argc, char **argv)
 

Function Documentation

◆ main()

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

Definition at line 141 of file ReadWriteTest.cpp.

142 {
143  // Initialize MPI
144  int mpi_initialized = 0;
145  MPI_Initialized( &mpi_initialized );
146  if( !mpi_initialized )
147  {
148  MPI_Init( &argc, &argv );
149  }
150 
151  int rank = 0;
152  int nprocs = 1;
153  MPI_Comm_rank( MPI_COMM_WORLD, &rank );
154  MPI_Comm_size( MPI_COMM_WORLD, &nprocs );
155 
156  try
157  {
158  // Parse command line arguments
159  Config config = parse_arguments( argc, argv );
160  if( rank == 0 )
161  {
162  print_config( config, rank, nprocs );
163  }
164 
165  // Create MOAB instance with smart pointer for automatic cleanup
166  auto moab = std::make_unique< Core >();
167  if( !moab )
168  {
169  std::cerr << "Error: Failed to create MOAB instance" << std::endl;
170  MPI_Abort( MPI_COMM_WORLD, 1 );
171  }
172 
173  // Create parallel communication interface
174  auto pcomm = std::make_unique< ParallelComm >( moab.get(), MPI_COMM_WORLD );
175 
176  // Create a mesh set to store the loaded mesh
177  EntityHandle mesh_set;
178  MB_CHK_ERR( moab->create_meshset( moab::MESHSET_SET, mesh_set ) );
179 
180  // Time the read operation
181  auto read_start = std::chrono::high_resolution_clock::now();
182 
183  if( rank == 0 )
184  {
185  std::cout << "Reading mesh file..." << std::endl;
186  }
187 
188  // Read the mesh file
189  MB_CHK_SET_ERR( moab->load_file( config.input_file.c_str(), &mesh_set, config.read_opts.c_str() ),
190  "Failed to read input file: " << config.input_file );
191 
192  auto read_end = std::chrono::high_resolution_clock::now();
193  std::chrono::duration< double > read_elapsed = read_end - read_start;
194 
195  if( rank == 0 )
196  {
197  std::cout << "Read completed in " << read_elapsed.count() << " seconds" << std::endl;
198  std::cout << "Writing mesh file..." << std::endl;
199  }
200 
201  // Time the write operation
202  auto write_start = std::chrono::high_resolution_clock::now();
203 
204  // Write the mesh file
205  MB_CHK_SET_ERR( moab->write_file( config.output_file.c_str(), nullptr, config.write_opts.c_str(), &mesh_set,
206  1 ),
207  "Failed to write output file: " << config.output_file );
208 
209  auto write_end = std::chrono::high_resolution_clock::now();
210  std::chrono::duration< double > write_elapsed = write_end - write_start;
211 
212  if( rank == 0 )
213  {
214  std::cout << "Write completed in " << write_elapsed.count() << " seconds" << std::endl;
215  std::cout << "\n=== Test completed successfully ===" << std::endl;
216  }
217 
218  // Clean up
219  pcomm.reset();
220  moab.reset();
221 
222  // Only finalize MPI if we initialized it
223  if( !mpi_initialized )
224  {
225  MPI_Finalize();
226  }
227 
228  return 0;
229  }
230  catch( const std::exception& e )
231  {
232  std::cerr << "Error on rank " << rank << ": " << e.what() << std::endl;
233  MPI_Abort( MPI_COMM_WORLD, 1 );
234  return 1;
235  }
236  catch( ... )
237  {
238  std::cerr << "Unknown error occurred on rank " << rank << std::endl;
239  MPI_Abort( MPI_COMM_WORLD, 1 );
240  return 1;
241  }
242 }

References MB_CHK_ERR, MB_CHK_SET_ERR, MESHSET_SET, anonymous_namespace{ReadWriteTest.cpp}::parse_arguments(), and anonymous_namespace{ReadWriteTest.cpp}::print_config().