Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
VisTags.cpp File Reference
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
#include "moab/Core.hpp"
#include "MBTagConventions.hpp"
#include "moab/FileOptions.hpp"
+ Include dependency graph for VisTags.cpp:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

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

Definition at line 42 of file VisTags.cpp.

43 {
44 #ifdef MOAB_HAVE_NETCDF
45 
46 #ifdef MOAB_HAVE_MPI
47  MPI_Init( &argc, &argv );
48 #endif
49 
50  ErrorCode rval;
51  string file_input, file_output;
52  string read_opts, tags; // Tags to write, separated by commas; it is the name of the tag
53  if( argc < 2 )
54  {
55  file_input = string( MESH_DIR ) + string( "/io/gcrm_r3.nc" );
56  file_output = "VisTagsOut.vtk";
57  }
58  else
59  {
60  file_input = argv[1];
61  file_output = argv[2];
62  }
63  read_opts = "";
64  tags = "";
65 
66  // Instantiate
67  Interface* mb = new( std::nothrow ) Core;
68  if( NULL == mb ) return 1;
69 
70  int dimension = 2;
71  // In MOAB, it may have index after reading (T0, T1, etc)
72  char* levels = NULL; // Levels, separated by commas, no spaces (like 0, 1, 19)
73  if( argc > 3 )
74  {
75  int index = 3;
76  while( index < argc )
77  {
78  if( !strcmp( argv[index], "-O" ) ) // This is for reading options, optional
79  read_opts = argv[++index];
80  if( !strcmp( argv[index], "-t" ) ) tags = argv[++index];
81  if( !strcmp( argv[index], "-l" ) ) levels = argv[++index];
82  if( !strcmp( argv[index], "-d" ) ) dimension = atoi( argv[++index] );
83  index++;
84  }
85  }
86 
87  ostringstream opts;
88  opts << ";;TAGS=" << tags << ";LEVELS=" << levels << "\0";
89  FileOptions fo( opts.str().c_str() );
90 
91  vector< string > tagsNames;
92  vector< int > levelsArray;
93  fo.get_strs_option( "TAGS", tagsNames );
94  fo.get_ints_option( "LEVELS", levelsArray );
95 
96  // Load the input file with the specified options
97  rval = mb->load_file( file_input.c_str(), 0, read_opts.c_str() );MB_CHK_SET_ERR( rval, "not loading file" );
98 
99  Range ents;
100  rval = mb->get_entities_by_dimension( 0, dimension, ents );MB_CHK_SET_ERR( rval, "not getting ents" );
101 
102  // Now create double tags for entities of dimension
103  for( size_t i = 0; i < tagsNames.size(); i++ )
104  {
105  string tagName = tagsNames[i];
106  Tag tagh;
107  rval = mb->tag_get_handle( tagName.c_str(), tagh );
108  if( MB_SUCCESS != rval )
109  {
110  cout << "not getting tag " << tagName.c_str() << "\n";
111  continue;
112  }
113 
114  int len = 0;
115  rval = mb->tag_get_length( tagh, len );
116  if( MB_SUCCESS != rval )
117  {
118  cout << "not getting tag len " << tagName.c_str() << "\n";
119  continue;
120  }
121 
122  DataType type;
123  rval = mb->tag_get_data_type( tagh, type );
124  if( MB_SUCCESS != rval )
125  {
126  cout << "not getting tag type " << tagName.c_str() << "\n";
127  continue;
128  }
129 
130  int count;
131  void* dataptr; // Assume double tags, for simplicity
132  rval = mb->tag_iterate( tagh, ents.begin(), ents.end(), count, dataptr );
133  if( MB_SUCCESS != rval || count != (int)ents.size() )
134  {
135  cout << "not getting tag iterate right " << tagName.c_str() << "\n";
136  continue;
137  }
138 
139  // Now create a new tag, with a new name, concatenated, and copy data there , for each level
140  for( size_t j = 0; j < levelsArray.size(); j++ )
141  {
142  int level = levelsArray[j];
143  if( level >= len )
144  {
145  cout << "level too big at " << level << "\n";
146  continue;
147  }
148 
149  ostringstream newTagName;
150  newTagName << tagName << "_" << level;
151  Tag newTagh;
152  rval = mb->tag_get_handle( newTagName.str().c_str(), 1, type, newTagh, MB_TAG_DENSE | MB_TAG_CREAT );
153  if( MB_SUCCESS != rval )
154  {
155  cout << "not getting new tag " << newTagName.str() << "\n";
156  continue;
157  }
158 
159  void* newDataPtr;
160  rval = mb->tag_iterate( newTagh, ents.begin(), ents.end(), count, newDataPtr );
161  if( MB_SUCCESS != rval || count != (int)ents.size() )
162  {
163  cout << "not getting new tag iterate " << newTagName.str() << "\n";
164  continue;
165  }
166 
167  if( MB_TYPE_DOUBLE == type )
168  {
169  double* ptrD = (double*)newDataPtr;
170  double* oldData = (double*)dataptr;
171  for( int k = 0; k < count; k++, ptrD++ )
172  *ptrD = oldData[level + count * k];
173  }
174  } // for (size_t j = 0; j < levelsArray.size(); j++)
175 
176  mb->tag_delete( tagh ); // No need for the tag anymore, write it to the new file
177  } // for (size_t i = 0; i < tagsNames.size(); i++)
178 
179  rval = mb->write_file( file_output.c_str() );MB_CHK_SET_ERR( rval, "Can't write file " << file_output );
180  cout << "Successfully wrote file " << file_output << "\n";
181 
182  delete mb;
183 
184 #ifdef MOAB_HAVE_MPI
185  MPI_Finalize();
186 #endif
187 #else
188  std::cout << " configure with netcdf for this example to work\n";
189 #endif
190  return 0;
191 }

References moab::Range::begin(), moab::Range::end(), ErrorCode, moab::Core::get_entities_by_dimension(), moab::FileOptions::get_ints_option(), moab::FileOptions::get_strs_option(), moab::Core::load_file(), mb, MB_CHK_SET_ERR, MB_SUCCESS, MB_TAG_CREAT, MB_TAG_DENSE, MB_TYPE_DOUBLE, MESH_DIR, moab::Range::size(), moab::Core::tag_delete(), moab::Core::tag_get_data_type(), moab::Core::tag_get_handle(), moab::Core::tag_get_length(), moab::Core::tag_iterate(), and moab::Core::write_file().