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

Example demonstrating addition of field data from phys grid to PG2 mesh. More...

#include "moab/ProgOptions.hpp"
#include "moab/Core.hpp"
#include <iostream>
+ Include dependency graph for AddFieldtoPG2.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Detailed Description

Example demonstrating addition of field data from phys grid to PG2 mesh.

This example shows how to:

  • Load PG2 mesh and phys grid solution files
  • Extract variable data from phys grid
  • Match entities between phys grid and PG2 mesh using global IDs
  • Copy variable data to PG2 mesh cells
  • Write enhanced PG2 mesh files for visualization

This tool is useful for transferring field data from phys grid solutions to PG2 mesh representations for climate model analysis.

Author
MOAB Development Team
Date
2024

Description: Add field data from phys grid to PG2 mesh for visualization and analysis

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

Definition in file AddFieldtoPG2.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 32 of file AddFieldtoPG2.cpp.

33 {
34 
35  ProgOptions opts;
36 
37  std::string inputfile, outfile( "out.h5m" ), physgridfile, variable_name;
38 
39  opts.addOpt< std::string >( "input,i", "input mesh filename", &inputfile );
40  opts.addOpt< std::string >( "output,o", "output mesh filename", &outfile );
41  opts.addOpt< std::string >( "phys,p", "phys grid solution filename", &physgridfile );
42  opts.addOpt< std::string >( "var,v", "variable to extract and add to output file", &variable_name );
43 
44  opts.parseCommandLine( argc, argv );
45 
46  if( inputfile.empty() )
47  {
48  opts.printHelp();
49  return 0;
50  }
51  ErrorCode rval;
52  Core* mb = new Core();
53 
54  EntityHandle fset1, fset2;
55  MB_CHK_SET_ERR( mb->create_meshset( MESHSET_SET, fset1 ), "can't create mesh set" );
56  MB_CHK_SET_ERR( mb->load_file( inputfile.c_str(), &fset1 ), "can't load input file" );
57 
58  cout << " opened " << inputfile << " with initial h5m data.\n";
59 
60  MB_CHK_SET_ERR( mb->create_meshset( MESHSET_SET, fset2 ), "can't create mesh set" );
61  MB_CHK_SET_ERR( mb->load_file( physgridfile.c_str(), &fset2 ), "can't load phys grid file" );
62 
63  Tag tagv;
64  MB_CHK_SET_ERR( mb->tag_get_handle( variable_name.c_str(), tagv ), "can't get tag handle" );
65 
66  Tag gitag = mb->globalId_tag();
67 
68  Range verts; // from phys grid
69  MB_CHK_SET_ERR( mb->get_entities_by_dimension( fset2, 0, verts ), "can't get vertices" );
70  std::vector< int > gids;
71  gids.resize( verts.size() );
72  MB_CHK_SET_ERR( mb->tag_get_data( gitag, verts, &gids[0] ), "can't get gi tag values" );
73  std::vector< double > valsTag;
74  valsTag.resize( verts.size() );
75  MB_CHK_SET_ERR( mb->tag_get_data( tagv, verts, &valsTag[0] ), "can't get tag vals" );
76  Range cells;
77 
78  MB_CHK_SET_ERR( mb->get_entities_by_dimension( fset1, 2, cells ), "can't get cells" );
79 
80  std::map< int, double > valsByID;
81  for( int i = 0; i < (int)gids.size(); i++ )
82  valsByID[gids[i]] = valsTag[i];
83 
84  // set now cells values
85  std::vector< int > cellsIds;
86  cellsIds.resize( cells.size() );
87  MB_CHK_SET_ERR( mb->tag_get_data( gitag, cells, &cellsIds[0] ), "can't get cells ids" );
88  for( int i = 0; i < (int)cells.size(); i++ )
89  {
90  valsTag[i] = valsByID[cellsIds[i]];
91  }
92  MB_CHK_SET_ERR( mb->tag_set_data( tagv, cells, &valsTag[0] ), "can't set cells tags" );
93 
94  MB_CHK_SET_ERR( mb->write_file( outfile.c_str(), 0, 0, &fset1, 1 ), "can't write file" );
95 
96  return 0;
97 }

References ProgOptions::addOpt(), moab::Core::create_meshset(), ErrorCode, moab::Core::get_entities_by_dimension(), moab::Core::globalId_tag(), moab::Core::load_file(), mb, MB_CHK_SET_ERR, MESHSET_SET, ProgOptions::parseCommandLine(), ProgOptions::printHelp(), moab::Range::size(), moab::Core::tag_get_data(), moab::Core::tag_get_handle(), moab::Core::tag_set_data(), and moab::Core::write_file().