Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
ProjectToSphere.cpp
Go to the documentation of this file.
1 /*
2  * proj1.cpp
3  *
4  * project on a sphere of radius R, delete sets if needed, and delete edges between parts
5  * (created by resolve shared ents)
6  */
7 
8 #include "moab/Core.hpp"
9 #include "moab/Interface.hpp"
10 #include "moab/ProgOptions.hpp"
11 #include <iostream>
12 #include <cmath>
13 
15 #include <cassert>
16 using namespace moab;
17 
18 double radius = 1.; // in m: 6371220.
19 
20 int main( int argc, char** argv )
21 {
22  std::string file1;
23  std::string file2;
24  ProgOptions opts;
25 
26  opts.addOpt< std::string >( "input,i", "input file", &file1 );
27  opts.addOpt< std::string >( "output,o", "output file", &file2 );
28 
29  double radius = 1.0;
30  opts.addOpt< double >( std::string( "radius,R" ), std::string( "project to radius" ), &radius );
31 
32  opts.addOpt< void >( "deletePartitionSets,D", "delete partition sets from output file" );
33  opts.addOpt< void >( "deleteEdges,E", "delete edges from output file" );
34 
35  opts.parseCommandLine( argc, argv );
36 
37  bool delete_partition_sets = opts.numOptSet( "deletePartitionSets" ) > 0;
38  bool delete_edges = opts.numOptSet( "deleteEdges" ) > 0;
39 
40  Core moab;
41  Interface& mb = moab;
42 
43  ErrorCode rval = mb.load_mesh( file1.c_str() );MB_CHK_SET_ERR( rval, "can't read input file" );
44 
45  std::cout << "project to radius " << radius << " this input: " << file1 << " to output: " << file2 << "\n";
46 
47  Range verts;
48  rval = mb.get_entities_by_dimension( 0, 0, verts );MB_CHK_SET_ERR( rval, "can't get vertices" );
49 
50  double *x_ptr, *y_ptr, *z_ptr;
51  int count;
52  rval = mb.coords_iterate( verts.begin(), verts.end(), x_ptr, y_ptr, z_ptr, count );MB_CHK_SET_ERR( rval, "can't coords iterate" );
53 
54  assert( count == (int)verts.size() ); // should end up with just one contiguous chunk of vertices
55 
56  for( int v = 0; v < count; v++ )
57  {
58  // EntityHandle v = verts[v];
59  CartVect pos( x_ptr[v], y_ptr[v], z_ptr[v] );
60  pos = pos / pos.length();
61  pos = radius * pos;
62  x_ptr[v] = pos[0];
63  y_ptr[v] = pos[1];
64  z_ptr[v] = pos[2];
65  }
66 
67  if( delete_edges )
68  {
69  Range edges;
70  rval = mb.get_entities_by_dimension( 0, 1, edges );
71  if( MB_SUCCESS != rval ) return 1;
72  // write edges to a new set, and after that, write the set, delete the edges and the set
73  EntityHandle sf1;
74  rval = mb.create_meshset( MESHSET_SET, sf1 );MB_CHK_SET_ERR( rval, "can't create edges set" );
75  rval = mb.add_entities( sf1, edges );MB_CHK_SET_ERR( rval, "can't add edges to new set" );
76  rval = mb.write_mesh( "edgesOnly.h5m", &sf1, 1 );MB_CHK_SET_ERR( rval, "can't write edges only set" );
77  rval = mb.delete_entities( &sf1, 1 );MB_CHK_SET_ERR( rval, "can't delete edge set from database" );
78  mb.delete_entities( edges );
79  }
80 
81  if( delete_partition_sets )
82  {
83  Tag par_tag;
84  rval = mb.tag_get_handle( "PARALLEL_PARTITION", par_tag );
85  if( MB_SUCCESS == rval )
86 
87  {
88  Range par_sets;
89  rval =
90  mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &par_tag, NULL, 1, par_sets, moab::Interface::UNION );
91  if( !par_sets.empty() ) mb.delete_entities( par_sets );
92  mb.tag_delete( par_tag );
93  }
94  }
95 
96  mb.write_file( file2.c_str() );
97 
98  return 0;
99 }