Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
AddFieldtoPG2.cpp
Go to the documentation of this file.
1 /**
2  * @file AddFieldtoPG2.cpp
3  * @brief Example demonstrating addition of field data from phys grid to PG2 mesh
4  *
5  * This example shows how to:
6  * - Load PG2 mesh and phys grid solution files
7  * - Extract variable data from phys grid
8  * - Match entities between phys grid and PG2 mesh using global IDs
9  * - Copy variable data to PG2 mesh cells
10  * - Write enhanced PG2 mesh files for visualization
11  *
12  * This tool is useful for transferring field data from phys grid
13  * solutions to PG2 mesh representations for climate model analysis.
14  *
15  * @author MOAB Development Team
16  * @date 2024
17  *
18 
19  * Description: Add field data from phys grid to PG2 mesh for visualization and analysis
20  *
21  * @param argc Number of command line arguments
22  * @param argv Command line arguments array
23  * @return 0 on success, 1 on failure
24  */
25 #include "moab/ProgOptions.hpp"
26 #include "moab/Core.hpp"
27 #include <iostream>
28 
29 using namespace moab;
30 using namespace std;
31 
32 int main( int argc, char* argv[] )
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 }