Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
ExtractClose.cpp File Reference
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include "moab/Core.hpp"
#include "moab/Interface.hpp"
#include "moab/Range.hpp"
#include "moab/ProgOptions.hpp"
#include "moab/CartVect.hpp"
#include "moab/IntxMesh/IntxUtils.hpp"
+ Include dependency graph for ExtractClose.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 
)

Definition at line 30 of file ExtractClose.cpp.

31 {
32  ProgOptions opts;
33 
34  // vertex 25 in filt out (-0.784768, 0.594952, -0.173698)
35  double x = -0.784768, y = 0.594952, z = -0.173698;
36  double lat = 28.6551, lon = 61.0204; // from Charlie Zender's page
37  // https://acme-climate.atlassian.net/wiki/spaces/ED/pages/1347092547/Assessing+and+Addressing+Regridding+Weights+in+Map-Files
38 
39  string inputFile = test_file_name;
40  opts.addOpt< string >( "inFile,i", "Specify the input file name string ", &inputFile );
41 
42  string outFile = "out.h5m";
43  opts.addOpt< string >( "outFile,o", "Specify the output file name string ", &outFile );
44 
45  opts.addOpt< double >( string( "xpos,x" ), string( "x position" ), &x );
46  opts.addOpt< double >( string( "ypos,y" ), string( "y position" ), &y );
47  opts.addOpt< double >( string( "zpos,z" ), string( "z position" ), &z );
48 
49  opts.addOpt< double >( string( "latitude,t" ), string( "north latitude in degrees" ), &lat );
50  opts.addOpt< double >( string( "longitude,w" ), string( "east longitude in degrees" ), &lon );
51 
52  bool spherical = false;
53  opts.addOpt< void >( "spherical,s", "use spherical coords input (default false) ", &spherical );
54 
55  double distance = 0.01;
56  opts.addOpt< double >( string( "distance,d" ), string( "distance " ), &distance );
57 
58  opts.parseCommandLine( argc, argv );
59 
60  int rank = 0;
61  int numProcesses = 1;
62  std::string readopts;
63 
64  // Instantiate
65  Core mb;
66 
67  // Load the file into a new file set
68  EntityHandle fileSet;
69  ErrorCode rval = mb.create_meshset( MESHSET_SET, fileSet );MB_CHK_SET_ERR( rval, "Error creating file set" );
70  rval = mb.load_file( inputFile.c_str(), &fileSet, readopts.c_str() );MB_CHK_SET_ERR( rval, "Error loading file" );
71 
72  if( !rank )
73  {
74  cout << " reading file " << inputFile << " on " << numProcesses << " task";
75  if( numProcesses > 1 ) cout << "s";
76  cout << "\n";
77  }
78  // Get all 2d elements in the file set
79  Range elems;
80  rval = mb.get_entities_by_dimension( fileSet, 2, elems );MB_CHK_SET_ERR( rval, "Error getting 2d elements" );
81 
82  // create a meshset with close elements
83  EntityHandle outSet;
84 
85  // create meshset
86  rval = mb.create_meshset( MESHSET_SET, outSet );MB_CHK_ERR( rval );
87 
88  // double sphere radius is 1
89  CartVect point( x, y, z );
90  if( spherical )
91  {
92  std::cout << " use spherical coordinates on the sphere of radius 1.\n";
94  pos.R = 1; // always on the
95  pos.lat = lat * M_PI / 180; // defined in math.h
96  pos.lon = lon * M_PI / 180;
97  point = IntxUtils::spherical_to_cart( pos );
98  }
99 
100  Range closeByCells;
101  for( Range::iterator it = elems.begin(); it != elems.end(); it++ )
102  {
103  EntityHandle cell = *it;
105  rval = mb.get_coords( &cell, 1, &( center[0] ) );MB_CHK_SET_ERR( rval, "Can't get cell center coords" );
106  double dist = ( center - point ).length();
107  if( dist <= distance )
108  {
109  closeByCells.insert( cell );
110  }
111  }
112 
113  rval = mb.add_entities( outSet, closeByCells );MB_CHK_SET_ERR( rval, "Can't add to entity set" );
114 
115  int numCells = (int)closeByCells.size();
116 
117  if( numCells == 0 )
118  {
119  if( !rank )
120  std::cout << " no close cells to the point " << point << " at distance less than " << distance << "\n";
121  }
122  else
123  {
124  if( !rank )
125  std::cout << " write file " << outFile << " with cells closer than " << distance << " from " << point
126  << "\n";
127  string writeOpts;
128  if( numProcesses > 1 ) writeOpts = string( "PARALLEL=WRITE_PART;" );
129  rval = mb.write_file( outFile.c_str(), 0, writeOpts.c_str(), &outSet, 1 );MB_CHK_SET_ERR( rval, "Can't write file" );
130  }
131 
132 
133  return 0;
134 }

References moab::Core::add_entities(), ProgOptions::addOpt(), moab::Range::begin(), center(), moab::Core::create_meshset(), moab::Range::end(), ErrorCode, moab::Core::get_coords(), moab::Core::get_entities_by_dimension(), moab::Range::insert(), moab::IntxUtils::SphereCoords::lat, length(), moab::Core::load_file(), moab::IntxUtils::SphereCoords::lon, mb, MB_CHK_ERR, MB_CHK_SET_ERR, MESHSET_SET, ProgOptions::parseCommandLine(), moab::IntxUtils::SphereCoords::R, moab::Range::size(), moab::IntxUtils::spherical_to_cart(), test_file_name, and moab::Core::write_file().