Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
mbaddchunk.cpp
Go to the documentation of this file.
1 /*
2  * addncdata.cpp
3  * this tool will take an existing h5m file and add data from some chunk description files
4  * generated from e3sm runs;
5  * will support mainly showing the chunks in ViSit
6  *
7  * example of usage:
8  * ./mbaddchunk -i penta3d.h5m -n chunks_on_proc.txt -o penta3d_ch.h5m
9  *
10  *
11  * Basically, will output a new h5m file (penta3d_ch.h5m), which has 2 extra tags, corresponding to
12  * the chunks number and processors it sits on
13  *
14  *
15  * file penta3d.h5m is obtained from the pentagons file, with a python script
16  *
17  */
18 
19 #include "moab/ProgOptions.hpp"
20 #include "moab/Core.hpp"
21 
22 #include <cmath>
23 #include <sstream>
24 #include <iostream>
25 #include <iomanip>
26 #include <fstream>
27 
28 using namespace moab;
29 using namespace std;
30 
31 int main( int argc, char* argv[] )
32 {
33  ProgOptions opts;
34 
35  std::string inputfile( "penta3d.h5m" ), outfile( "penta3d_ch.h5m" ), chunkfile_name, gsmapfile;
36  opts.addOpt< std::string >( "input,i", "input mesh filename", &inputfile );
37  opts.addOpt< std::string >( "chunkFile,n", "chunk file from cam run", &chunkfile_name );
38  opts.addOpt< std::string >( "gsMAPfile,g", "gsmap file", &gsmapfile );
39  opts.addOpt< std::string >( "output,o", "output mesh filename", &outfile );
40 
41  opts.parseCommandLine( argc, argv );
42 
43  Core* mb = new Core();
44 
45  // open the netcdf file, and see if it has that variable we are looking for
46  MB_CHK_SET_ERR( mb->load_file( inputfile.c_str() ), "can't load input file" );
47  std::cout << " opened " << inputfile << " with initial h5m data.\n";
48 
49  Range nodes;
50  MB_CHK_SET_ERR( mb->get_entities_by_dimension( 0, 0, nodes ), "can't get nodes" );
51 
52  Range edges;
53  MB_CHK_SET_ERR( mb->get_entities_by_dimension( 0, 1, edges ), "can't get edges" );
54 
55  Range cells;
56  MB_CHK_SET_ERR( mb->get_entities_by_dimension( 0, 2, cells ), "can't get cells" );
57 
58  std::cout << " it has " << nodes.size() << " vertices " << edges.size() << " edges " << cells.size() << " cells\n";
59 
60  // construct maps between global id and handles
61  std::map< int, EntityHandle > vGidHandle;
62  std::map< int, EntityHandle > eGidHandle;
63  std::map< int, EntityHandle > cGidHandle;
64  std::vector< int > gids;
65  Tag gid = mb->globalId_tag();
66 
67  gids.resize( nodes.size() );
68  MB_CHK_SET_ERR( mb->tag_get_data( gid, nodes, &gids[0] ), "can't get global id on vertices" );
69  int i = 0;
70  for( Range::iterator vit = nodes.begin(); vit != nodes.end(); vit++ )
71  {
72  vGidHandle[gids[i++]] = *vit;
73  }
74 
75  gids.resize( edges.size() );
76  MB_CHK_SET_ERR( mb->tag_get_data( gid, edges, &gids[0] ), "can't get global id on edges" );
77  i = 0;
78  for( Range::iterator vit = edges.begin(); vit != edges.end(); vit++ )
79  {
80  eGidHandle[gids[i++]] = *vit;
81  }
82 
83  gids.resize( cells.size() );
84  MB_CHK_SET_ERR( mb->tag_get_data( gid, cells, &gids[0] ), "can't get global id on cells" );
85  i = 0;
86  for( Range::iterator vit = cells.begin(); vit != cells.end(); vit++ )
87  {
88  cGidHandle[gids[i++]] = *vit;
89  }
90 
91  if( chunkfile_name.length() > 0 )
92  {
93  // Open chunk file
94  ifstream inFile;
95  inFile.open( chunkfile_name.c_str() );
96  if( !inFile )
97  {
98  cout << "Unable to open chunk file";
99  exit( 1 ); // terminate with error
100  }
101 
102  Tag pTag, cTag;
103  int def_val = -1;
104  MB_CHK_SET_ERR( mb->tag_get_handle( "ProcID", 1, MB_TYPE_INTEGER, pTag, MB_TAG_CREAT | MB_TAG_DENSE, &def_val ),
105  "can't define processor tag" );
107  &def_val ),
108  "can't define chunk tag" );
109 
110  int proc, lcid, ncols;
111  while( inFile >> proc )
112  {
113  inFile >> lcid >> ncols;
114  int Gid;
115  for( i = 0; i < ncols; i++ )
116  {
117  inFile >> Gid;
118  EntityHandle cell = cGidHandle[Gid];
119  MB_CHK_SET_ERR( mb->tag_set_data( pTag, &cell, 1, &proc ), "can't set proc tag" );
120  MB_CHK_SET_ERR( mb->tag_set_data( cTag, &cell, 1, &lcid ), "can't set chunk tag" );
121  }
122  }
123 
124  inFile.close();
125  }
126 
127  if( gsmapfile.length() > 0 )
128  {
129  // Open chunk file
130  ifstream inFile;
131 
132  inFile.open( gsmapfile.c_str() );
133  if( !inFile )
134  {
135  cout << "Unable to open gsmap file";
136  exit( 1 ); // terminate with error
137  }
138 
139  Tag pTag, cTag;
140  int def_val = -1;
141  std::string procTagName = gsmapfile + "_proc";
142  MB_CHK_SET_ERR( mb->tag_get_handle( procTagName.c_str(), 1, MB_TYPE_INTEGER, pTag, MB_TAG_CREAT | MB_TAG_DENSE,
143  &def_val ),
144  "can't define processor tag" );
145  std::string segTagName = gsmapfile + "_seg";
146  MB_CHK_SET_ERR( mb->tag_get_handle( segTagName.c_str(), 1, MB_TYPE_INTEGER, cTag, MB_TAG_CREAT | MB_TAG_DENSE,
147  &def_val ),
148  "can't define segment tag" );
149 
150  int compid, ngseg, gsize;
151  inFile >> compid >> ngseg >> gsize;
152  for( i = 1; i <= ngseg; i++ )
153  {
154  int start, len, pe;
155  inFile >> start >> len >> pe;
156  int Gid;
157  for( int j = 0; j < len; j++ )
158  {
159  Gid = start + j;
160  EntityHandle cell = cGidHandle[Gid];
161  MB_CHK_SET_ERR( mb->tag_set_data( pTag, &cell, 1, &pe ), "can't set proc tag" );
162  MB_CHK_SET_ERR( mb->tag_set_data( cTag, &cell, 1, &i ), "can't set segment tag" );
163  }
164  }
165 
166  inFile.close();
167  }
168 
169  MB_CHK_SET_ERR( mb->write_file( outfile.c_str() ), "can't write file" );
170  std::cout << " wrote file " << outfile << "\n";
171  return 0;
172 }
173