Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
AddEdgeFluxVector.cpp File Reference
#include <iostream>
#include <sstream>
#include "moab/Core.hpp"
#include "moab/Interface.hpp"
#include "moab/IntxMesh/IntxUtils.hpp"
#include "moab/ProgOptions.hpp"
+ Include dependency graph for AddEdgeFluxVector.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 18 of file AddEdgeFluxVector.cpp.

19 {
20  string filein = "source_1_angle.h5m";
21  string fileout = "source_with_vec.h5m";
22 
23  ProgOptions opts;
24  opts.addOpt< std::string >( "model,m", "input file ", &filein );
25 
26  opts.addOpt< std::string >( "output,o", "output filename", &fileout );
27 
28  opts.parseCommandLine( argc, argv );
29 
30  Core moab;
31  Interface* mb = &moab;
32 
33  ErrorCode rval = mb->load_file( filein.c_str() );MB_CHK_ERR( rval );
34 
35  // get the angle tag, and the baro tag; compute a 3d vector normal on the edge
36  // (or look at the angle) compute also the latitude, long at mid edge, draw the normal and multiply with the edge length?
37  //
38  Tag angle, bflux;
39  rval = mb->tag_get_handle( "barotropicThicknessFlux0", bflux );MB_CHK_ERR( rval );
40  rval = mb->tag_get_handle( "angleEdge", angle );MB_CHK_ERR( rval );
41  MB_CHK_ERR( rval );
42 
43  Range edges;
44  rval = mb->get_entities_by_dimension( 0, 1, edges );MB_CHK_ERR( rval );
45  std::vector< double > angles( edges.size() ), fluxes( edges.size() );
46  std::cout << " file:" << filein << " nb edges:" << edges.size() << "\n";
47 
48  rval = mb->tag_get_data( angle, edges, &angles[0] );MB_CHK_ERR( rval );
49  rval = mb->tag_get_data( bflux, edges, &fluxes[0] );MB_CHK_ERR( rval );
50 
51  Tag vectorTag;
52  rval = mb->tag_get_handle( "FluxVector", 3, MB_TYPE_DOUBLE, vectorTag, MB_TAG_CREAT | MB_TAG_DENSE );MB_CHK_SET_ERR( rval, "Couldn't get tag handle" );
53 
54  Tag vectorTagU;
55  rval = mb->tag_get_handle( "FluxVectorU", 3, MB_TYPE_DOUBLE, vectorTagU, MB_TAG_CREAT | MB_TAG_DENSE );MB_CHK_SET_ERR( rval, "Couldn't get tag handle" );
56  Tag vectorTagV;
57  rval = mb->tag_get_handle( "FluxVectorV", 3, MB_TYPE_DOUBLE, vectorTagV, MB_TAG_CREAT | MB_TAG_DENSE );MB_CHK_SET_ERR( rval, "Couldn't get tag handle" );
58 
59  Tag edgeLenTag;
60  rval = mb->tag_get_handle( "EdgeLength", 1, MB_TYPE_DOUBLE, edgeLenTag, MB_TAG_CREAT | MB_TAG_DENSE );MB_CHK_SET_ERR( rval, "Couldn't get tag handle" );
61 
62  // for each edge. get v1, v2, mid edge. compute lat, lon,
63  int i = 0;
64  for( Range::iterator eit = edges.begin(); eit != edges.end(); eit++, i++ )
65  {
66  EntityHandle eh = *eit;
67  const EntityHandle* conn;
68  int nv = 2;
69  rval = mb->get_connectivity( eh, conn, nv );MB_CHK_SET_ERR( rval, "Couldn't get edge conn" );
70  CartVect verts[2];
71  if( nv != 2 )
72  {
73  MB_CHK_SET_ERR( MB_FAILURE, "wrong number of vertices on edge" );
74  }
75  rval = mb->get_coords( conn, 2, &verts[0][0] );MB_CHK_SET_ERR( rval, "Couldn't get vertex coordinates" );
76  CartVect mid = 0.5 * ( verts[0] + verts[1] );
77  double edgeLen = ( verts[1] - verts[0] ).length();
78  rval = mb->tag_set_data( edgeLenTag, &eh, 1, &edgeLen );MB_CHK_SET_ERR( rval, "Couldn't set edge length" );
79  // utility to convert to lat/lon
80  IntxUtils::SphereCoords sph = IntxUtils::cart_to_spherical( mid );
81  double lat = sph.lat, lon = sph.lon;
82  // vectors along par latitude, and meridian:
83  CartVect u, v; // these are tangent at the mid to the sphere u = d p / d lon
84  // v = dp / d lat
85  // CartVect res;
86  // res[0] = sc.R * cos( sc.lat ) * cos( sc.lon ); // x coordinate
87  // res[1] = sc.R * cos( sc.lat ) * sin( sc.lon ); // y
88  // res[2] = sc.R * sin( sc.lat ); // z
89  u[0] = -sin( lon ); // x coordinate
90  u[1] = cos( lon );
91  u[2] = 0.; // no z component
92  v[0] = -sin( lat ) * cos( lon );
93  v[1] = -sin( lat ) * sin( lon );
94  v[2] = cos( lat );
95  CartVect flux = cos( angles[i] ) * u + sin( angles[i] ) * v; // unit vector
96  flux = fluxes[i] * flux; //
97 
98  rval = mb->tag_set_data( vectorTag, &eh, 1, &( flux[0] ) );MB_CHK_SET_ERR( rval, "Couldn't set vector tag" );
99  double projU, projV;
100  projU = flux % u; // this is dot product between vectors
101  CartVect pU = projU * u;
102  projV = flux % v;
103  CartVect pV = projV * v;
104  rval = mb->tag_set_data( vectorTagU, &eh, 1, &( pU[0] ) );MB_CHK_SET_ERR( rval, "Couldn't set vector tag" );
105  rval = mb->tag_set_data( vectorTagV, &eh, 1, &( pV[0] ) );MB_CHK_SET_ERR( rval, "Couldn't set vector tag" );
106  }
107  // after computing each edge flux, add all vectors in a cell, to see the total flux along edges
108  Range cells;
109  rval = mb->get_entities_by_dimension( 0, 2, cells );MB_CHK_ERR( rval );
110 
111  // get all edges adjacent to a cell, then multiply by edge length and compute the total flux at
112  // center of cell
113  for( Range::iterator cit = cells.begin(); cit != cells.end(); ++cit )
114  {
115  EntityHandle cell = *cit;
116  // get edges adjacent to the cell
117  std::vector< EntityHandle > adjEdges;
118  rval = mb->get_adjacencies( &cell, 1, 1, false, adjEdges, Interface::UNION );MB_CHK_ERR( rval );
119  CartVect totalFlux = CartVect( 0. );
120  for( size_t i = 0; i < adjEdges.size(); i++ )
121  {
122  EntityHandle edge = adjEdges[i];
123  CartVect flux;
124  rval = mb->tag_get_data( vectorTag, &edge, 1, &flux[0] );MB_CHK_ERR( rval );
125  double edgeLen;
126  rval = mb->tag_get_data( edgeLenTag, &edge, 1, &edgeLen );MB_CHK_ERR( rval );
127  totalFlux = totalFlux + edgeLen * flux;
128  }
129  rval = mb->tag_set_data( vectorTag, &cell, 1, &totalFlux[0] );MB_CHK_ERR( rval );
130  }
131  std::cout << " writing " << fileout << "\n";
132  rval = mb->write_file( fileout.c_str() );MB_CHK_ERR( rval );
133 
134  return 0;
135 }

References ProgOptions::addOpt(), moab::angle(), moab::Range::begin(), moab::IntxUtils::cart_to_spherical(), moab::Range::end(), ErrorCode, moab::Core::get_adjacencies(), moab::Core::get_connectivity(), moab::Core::get_coords(), moab::Core::get_entities_by_dimension(), moab::IntxUtils::SphereCoords::lat, length(), moab::Core::load_file(), moab::IntxUtils::SphereCoords::lon, mb, MB_CHK_ERR, MB_CHK_SET_ERR, MB_TAG_CREAT, MB_TAG_DENSE, MB_TYPE_DOUBLE, ProgOptions::parseCommandLine(), moab::Range::size(), moab::Core::tag_get_data(), moab::Core::tag_get_handle(), moab::Core::tag_set_data(), moab::Interface::UNION, and moab::Core::write_file().