Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
mbtempest.cpp File Reference
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <vector>
#include <string>
#include <sstream>
#include <cassert>
#include "moab/Core.hpp"
#include "moab/IntxMesh/IntxUtils.hpp"
#include "moab/Remapping/TempestRemapper.hpp"
#include "moab/Remapping/TempestOnlineMap.hpp"
#include "moab/ProgOptions.hpp"
#include "moab/CpuTimer.hpp"
#include "DebugOutput.hpp"
+ Include dependency graph for mbtempest.cpp:

Go to the source code of this file.

Classes

struct  ToolContext
 

Functions

static moab::ErrorCode CreateTempestMesh (ToolContext &, moab::TempestRemapper &remapper, Mesh *)
 
double sample_slow_harmonic (double dLon, double dLat)
 
double sample_fast_harmonic (double dLon, double dLat)
 
double sample_constant (double dLon, double dLat)
 
double sample_stationary_vortex (double dLon, double dLat)
 
std::string get_file_read_options (ToolContext &ctx, std::string filename)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ CreateTempestMesh()

static moab::ErrorCode CreateTempestMesh ( ToolContext ctx,
moab::TempestRemapper remapper,
Mesh *  tempest_mesh 
)
static

Definition at line 1047 of file mbtempest.cpp.

1048 {
1050  int err;
1051  moab::DebugOutput& outputFormatter = ctx.outputFormatter;
1052 
1053  if( !ctx.proc_id )
1054  {
1055  outputFormatter.printf( 0, "Creating TempestRemap Mesh object ...\n" );
1056  }
1057 
1059  {
1060  ctx.timer_push( "create Tempest OverlapMesh" );
1061  // For the overlap method, choose between: "fuzzy", "exact" or "mixed"
1062  err = GenerateOverlapMesh( ctx.inFilenames[0], ctx.inFilenames[1], *tempest_mesh, ctx.outFilename, "NetCDF4",
1063  "exact", true );
1064  if( err )
1065  rval = moab::MB_FAILURE;
1066  else
1067  ctx.meshes.push_back( tempest_mesh );
1068  ctx.timer_pop();
1069  }
1071  {
1072  // Load the meshes and validate
1073  ctx.meshsets.resize( 3 );
1074  ctx.meshes.resize( 3 );
1075  ctx.meshsets[0] = remapper.GetMeshSet( moab::Remapper::SourceMesh );
1076  ctx.meshsets[1] = remapper.GetMeshSet( moab::Remapper::TargetMesh );
1077  ctx.meshsets[2] = remapper.GetMeshSet( moab::Remapper::OverlapMesh );
1078 
1079  ctx.timer_push( "load MOAB Source mesh" );
1080  // First the source
1081  MB_CHK_ERR(
1083  ctx.meshes[0] = remapper.GetMesh( moab::Remapper::SourceMesh );
1084  ctx.timer_pop();
1085 
1086  ctx.timer_push( "load MOAB Target mesh" );
1087  // Next the target
1088  MB_CHK_ERR(
1090  ctx.meshes[1] = remapper.GetMesh( moab::Remapper::TargetMesh );
1091  ctx.timer_pop();
1092 
1093  ctx.timer_push( "generate TempestRemap OverlapMesh" );
1094  // Now let us construct the overlap mesh, by calling TempestRemap interface directly
1095  // For the overlap method, choose between: "fuzzy", "exact" or "mixed"
1096  err = GenerateOverlapWithMeshes( *ctx.meshes[0], *ctx.meshes[1], *tempest_mesh, "" /*ctx.outFilename*/,
1097  "NetCDF4", "exact", false );
1098  ctx.timer_pop();
1099  if( err )
1100  rval = moab::MB_FAILURE;
1101  else
1102  {
1103  remapper.SetMesh( moab::Remapper::OverlapMesh, tempest_mesh );
1104  ctx.meshes[2] = remapper.GetMesh( moab::Remapper::OverlapMesh );
1105  // ctx.meshes.push_back(*tempest_mesh);
1106  }
1107  }
1109  {
1110  ctx.meshsets.resize( 3 );
1111  ctx.meshes.resize( 3 );
1112  ctx.meshsets[0] = remapper.GetMeshSet( moab::Remapper::SourceMesh );
1113  ctx.meshsets[1] = remapper.GetMeshSet( moab::Remapper::TargetMesh );
1114  ctx.meshsets[2] = remapper.GetMeshSet( moab::Remapper::OverlapMesh );
1115 
1116  const double radius_src = 1.0 /*2.0*acos(-1.0)*/;
1117  const double radius_dest = 1.0 /*2.0*acos(-1.0)*/;
1118 
1119  std::vector< int > smetadata, tmetadata;
1120  //const char* additional_read_opts = ( ctx.n_procs > 1 ? "NO_SET_CONTAINING_PARENTS;" : "" );
1121  std::string additional_read_opts_src = get_file_read_options( ctx, ctx.inFilenames[0] );
1122 
1123  ctx.timer_push( "load MOAB Source mesh" );
1124  // Load the source mesh and validate
1125  MB_CHK_ERR( remapper.LoadNativeMesh( ctx.inFilenames[0], ctx.meshsets[0], smetadata,
1126  additional_read_opts_src.c_str() ) );
1127  if( smetadata.size() ) remapper.SetMeshType( moab::Remapper::SourceMesh, smetadata );
1128  ctx.timer_pop();
1129 
1130  ctx.timer_push( "preprocess MOAB Source mesh" );
1131  // Rescale the radius of both to compute the intersection
1132  MB_CHK_ERR( moab::IntxUtils::ScaleToRadius( ctx.mbcore, ctx.meshsets[0], radius_src ) );
1133 
1134  ctx.timer_pop();
1135 
1136  // Load the target mesh and validate
1137  std::string addititional_read_opts_tgt = get_file_read_options( ctx, ctx.inFilenames[1] );
1138  // addititional_read_opts_tgt += "PARALLEL_GHOSTS=2.0.3;PARALLEL_THIN_GHOST_LAYER;";
1139 
1140  ctx.timer_push( "load MOAB Target mesh" );
1141  MB_CHK_ERR( remapper.LoadNativeMesh( ctx.inFilenames[1], ctx.meshsets[1], tmetadata,
1142  addititional_read_opts_tgt.c_str() ) );
1143  if( tmetadata.size() ) remapper.SetMeshType( moab::Remapper::TargetMesh, tmetadata );
1144  ctx.timer_pop();
1145 
1146  ctx.timer_push( "preprocess MOAB Target mesh" );
1147  MB_CHK_ERR( moab::IntxUtils::ScaleToRadius( ctx.mbcore, ctx.meshsets[1], radius_dest ) );
1148  ctx.timer_pop();
1149 
1150  if( ctx.computeWeights )
1151  {
1152  ctx.timer_push( "convert MOAB meshes to TempestRemap meshes in memory" );
1153  // convert MOAB representation to TempestRemap's Mesh for source
1155  ctx.meshes[0] = remapper.GetMesh( moab::Remapper::SourceMesh );
1156 
1157  // convert MOAB representation to TempestRemap's Mesh for target
1159  ctx.meshes[1] = remapper.GetMesh( moab::Remapper::TargetMesh );
1160  ctx.timer_pop();
1161  }
1162  }
1163  else if( ctx.meshType == moab::TempestRemapper::ICO )
1164  {
1165  ctx.timer_push( "generate ICO mesh with TempestRemap" );
1166  err = GenerateICOMesh( *tempest_mesh, ctx.blockSize, ctx.computeDual, ctx.outFilename, "NetCDF4" );
1167  ctx.timer_pop();
1168 
1169  if( err )
1170  rval = moab::MB_FAILURE;
1171  else
1172  ctx.meshes.push_back( tempest_mesh );
1173  }
1174  else if( ctx.meshType == moab::TempestRemapper::RLL )
1175  {
1176  ctx.timer_push( "generate RLL mesh with TempestRemap" );
1177  err = GenerateRLLMesh( *tempest_mesh, // Mesh& meshOut,
1178  ctx.blockSize * 2, ctx.blockSize, // int nLongitudes, int nLatitudes,
1179  0.0, 360.0, // double dLonBegin, double dLonEnd,
1180  -90.0, 90.0, // double dLatBegin, double dLatEnd,
1181  false, false, false, // bool fGlobalCap, bool fFlipLatLon, bool fForceGlobal,
1182  "" /*ctx.inFilename*/, "",
1183  "", // std::string strInputFile, std::string strInputFileLonName, std::string
1184  // strInputFileLatName,
1185  ctx.outFilename, "NetCDF4", // std::string strOutputFile, std::string strOutputFormat
1186  true // bool fVerbose
1187  );
1188  ctx.timer_pop();
1189 
1190  if( err )
1191  rval = moab::MB_FAILURE;
1192  else
1193  ctx.meshes.push_back( tempest_mesh );
1194  }
1195  else // default
1196  {
1197  ctx.timer_push( "generate CS mesh with TempestRemap" );
1198  err = GenerateCSMesh( *tempest_mesh, ctx.blockSize, ctx.outFilename, "NetCDF4" );
1199  ctx.timer_pop();
1200  if( err )
1201  rval = moab::MB_FAILURE;
1202  else
1203  ctx.meshes.push_back( tempest_mesh );
1204  }
1205 
1206  if( ctx.meshType != moab::TempestRemapper::OVERLAP_MOAB && !tempest_mesh )
1207  {
1208  std::cout << "Tempest Mesh is not a complete object; Quitting...";
1209  exit( -1 );
1210  }
1211 
1212  return rval;
1213 }

References ToolContext::blockSize, ToolContext::computeDual, ToolContext::computeWeights, moab::TempestRemapper::ConvertMeshToTempest(), moab::TempestRemapper::DEFAULT, ErrorCode, get_file_read_options(), moab::TempestRemapper::GetMesh(), moab::TempestRemapper::GetMeshSet(), moab::TempestRemapper::ICO, ToolContext::inFilenames, moab::TempestRemapper::LoadMesh(), moab::Remapper::LoadNativeMesh(), MB_CHK_ERR, MB_SUCCESS, ToolContext::mbcore, ToolContext::meshes, ToolContext::meshsets, ToolContext::meshType, ToolContext::outFilename, ToolContext::outputFormatter, moab::TempestRemapper::OVERLAP_FILES, moab::TempestRemapper::OVERLAP_MEMORY, moab::TempestRemapper::OVERLAP_MOAB, moab::Remapper::OverlapMesh, moab::DebugOutput::printf(), ToolContext::proc_id, moab::TempestRemapper::RLL, moab::IntxUtils::ScaleToRadius(), moab::TempestRemapper::SetMesh(), moab::TempestRemapper::SetMeshType(), moab::Remapper::SourceMesh, moab::Remapper::TargetMesh, ToolContext::timer_pop(), and ToolContext::timer_push().

Referenced by main().

◆ get_file_read_options()

std::string get_file_read_options ( ToolContext ctx,
std::string  filename 
)

Definition at line 417 of file mbtempest.cpp.

418 {
419  // if running in serial, blank option may suffice in general
420  std::string opts = "";
421  if( ctx.n_procs > 1 )
422  {
423  size_t lastindex = filename.find_last_of( "." );
424  std::string extension = filename.substr( lastindex + 1, filename.size() );
425  if( extension == "h5m" ) return "PARALLEL=READ_PART;PARTITION=PARALLEL_PARTITION;PARALLEL_RESOLVE_SHARED_ENTS;";
426  // "PARALLEL_GHOSTS=2.0.3;PARALLEL_THIN_GHOST_LAYER;";
427  else if( extension == "nc" )
428  {
429  // set default set of options
430  opts = "PARALLEL=READ_PART;PARTITION_METHOD=RCBZOLTAN;";
431 
432  if( ctx.proc_id == 0 )
433  {
434  {
435  NcFile ncFile( filename.c_str(), NcFile::ReadOnly );
436  if( !ncFile.is_valid() )
437  _EXCEPTION1( "Unable to open grid file \"%s\" for reading", filename.c_str() );
438 
439  // Check for dimension names "grid_size", "grid_rank" and "grid_corners"
440  int iSCRIPFormat = 0, iMPASFormat = 0, iESMFFormat = 0;
441  for( int i = 0; i < ncFile.num_dims(); i++ )
442  {
443  std::string strDimName = ncFile.get_dim( i )->name();
444  if( strDimName == "grid_size" || strDimName == "grid_corners" || strDimName == "grid_rank" )
445  iSCRIPFormat++;
446  if( strDimName == "nodeCount" || strDimName == "elementCount" ||
447  strDimName == "maxNodePElement" )
448  iESMFFormat++;
449  if( strDimName == "nCells" || strDimName == "nEdges" || strDimName == "nVertices" ||
450  strDimName == "vertexDegree" )
451  iMPASFormat++;
452  }
453  ncFile.close();
454 
455  if( iESMFFormat == 3 ) // Input from a NetCDF ESMF file
456  {
457  opts = "PARALLEL=READ_PART;PARTITION_METHOD=RCBZOLTAN;PARALLEL_RESOLVE_SHARED_ENTS;VARIABLE=;";
458  }
459  if( iSCRIPFormat == 3 ) // Input from a NetCDF SCRIP file
460  {
461  opts = "PARALLEL=READ_PART;PARTITION_METHOD=RCBZOLTAN;";
462  }
463  if( iMPASFormat == 4 ) // Input from a NetCDF SCRIP file
464  {
465  opts = "PARALLEL=READ_PART;PARTITION_METHOD=RCBZOLTAN;"
466  "PARALLEL_RESOLVE_SHARED_ENTS;NO_EDGES;NO_MIXED_ELEMENTS;VARIABLE=;";
467  }
468  }
469  }
470 
471 #ifdef MOAB_HAVE_MPI
472  int line_size = opts.size();
473  MPI_Bcast( &line_size, 1, MPI_INT, 0, MPI_COMM_WORLD );
474  if( ctx.proc_id != 0 ) opts.resize( line_size );
475  MPI_Bcast( const_cast< char* >( opts.data() ), line_size, MPI_CHAR, 0, MPI_COMM_WORLD );
476 #endif
477  }
478  else
479  return "PARALLEL=BCAST_DELETE;PARTITION=TRIVIAL;PARALLEL_RESOLVE_SHARED_ENTS;";
480  }
481  return opts;
482 }

References ToolContext::n_procs, ncFile, and ToolContext::proc_id.

Referenced by CreateTempestMesh().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 484 of file mbtempest.cpp.

485 {
486  NcError error( NcError::verbose_nonfatal );
487  std::stringstream sstr;
488  std::string historyStr;
489 
490  int proc_id = 0, nprocs = 1;
491 #ifdef MOAB_HAVE_MPI
492  MPI_Init( &argc, &argv );
493  MPI_Comm_rank( MPI_COMM_WORLD, &proc_id );
494  MPI_Comm_size( MPI_COMM_WORLD, &nprocs );
495 #endif
496 
497  moab::Core* mbCore = new( std::nothrow ) moab::Core;
498 
499  if( nullptr == mbCore )
500  {
501  return 1;
502  }
503 
504  // Build the history string
505  for( int ia = 0; ia < argc; ++ia )
506  historyStr += std::string( argv[ia] ) + " ";
507 
508  ToolContext* runCtx;
509 #ifdef MOAB_HAVE_MPI
510  moab::ParallelComm* pcomm = new moab::ParallelComm( mbCore, MPI_COMM_WORLD, 0 );
511 
512  runCtx = new ToolContext( mbCore, pcomm );
513  const char* writeOptions = ( nprocs > 1 ? "PARALLEL=WRITE_PART" : "" );
514 #else
515  runCtx = new ToolContext( mbCore );
516  const char* writeOptions = "";
517 #endif
518  runCtx->ParseCLOptions( argc, argv );
519 
520  const double radius_src = 1.0 /*2.0*acos(-1.0)*/;
521  const double radius_dest = 1.0 /*2.0*acos(-1.0)*/;
522 
523  moab::DebugOutput& outputFormatter = runCtx->outputFormatter;
524 
525 #ifdef MOAB_HAVE_MPI
526  moab::TempestRemapper remapper( mbCore, pcomm );
527 #else
528  moab::TempestRemapper remapper( mbCore );
529 #endif
530  remapper.meshValidate = true;
531  remapper.constructEdgeMap = true;
532  remapper.initialize();
533 
534  // Default area_method = lHuiller; Options: Girard, lHuiller, GaussQuadrature (if TR is available)
536 
537  Mesh* tempest_mesh = new Mesh();
538  MB_CHK_ERR( CreateTempestMesh( *runCtx, remapper, tempest_mesh ) );
539 
541  {
542  // Compute intersections with MOAB
543  // For the overlap method, choose between: "fuzzy", "exact" or "mixed"
544  assert( runCtx->meshes.size() == 3 );
545 
546 #ifdef MOAB_HAVE_MPI
548 #endif
549 
550  // Load the meshes and validate
551  MB_CHK_ERR( remapper.ConvertTempestMesh( moab::Remapper::SourceMesh ) );
552  MB_CHK_ERR( remapper.ConvertTempestMesh( moab::Remapper::TargetMesh ) );
553  MB_CHK_ERR( remapper.ConvertTempestMesh( moab::Remapper::OverlapMesh ) );
554  if( !runCtx->skip_io )
555  {
556  MB_CHK_ERR( mbCore->write_mesh( "tempest_intersection.h5m", &runCtx->meshsets[2], 1 ) );
557  }
558 
559  // print verbosely about the problem setting
560  size_t velist[6], gvelist[6];
561  {
562  moab::Range rintxverts, rintxelems;
563  MB_CHK_ERR( mbCore->get_entities_by_dimension( runCtx->meshsets[0], 0, rintxverts ) );
564  MB_CHK_ERR( mbCore->get_entities_by_dimension( runCtx->meshsets[0], 2, rintxelems ) );
565  velist[0] = rintxverts.size();
566  velist[1] = rintxelems.size();
567 
568  moab::Range bintxverts, bintxelems;
569  MB_CHK_ERR( mbCore->get_entities_by_dimension( runCtx->meshsets[1], 0, bintxverts ) );
570  MB_CHK_ERR( mbCore->get_entities_by_dimension( runCtx->meshsets[1], 2, bintxelems ) );
571  velist[2] = bintxverts.size();
572  velist[3] = bintxelems.size();
573  }
574 
575  moab::EntityHandle intxset; // == remapper.GetMeshSet(moab::Remapper::OverlapMesh);
576 
577  // Compute intersections with MOAB
578  {
579  // Create the intersection on the sphere object
580  runCtx->timer_push( "setup the intersector" );
581 
582  moab::Intx2MeshOnSphere* mbintx = new moab::Intx2MeshOnSphere( mbCore );
583  mbintx->set_error_tolerance( runCtx->epsrel );
584  mbintx->set_box_error( runCtx->boxeps );
585  mbintx->set_radius_source_mesh( radius_src );
586  mbintx->set_radius_destination_mesh( radius_dest );
587 #ifdef MOAB_HAVE_MPI
588  mbintx->set_parallel_comm( pcomm );
589 #endif
590  MB_CHK_ERR( mbintx->FindMaxEdges( runCtx->meshsets[0], runCtx->meshsets[1] ) );
591 
592 #ifdef MOAB_HAVE_MPI
593  moab::Range local_verts;
594  MB_CHK_ERR( mbintx->build_processor_euler_boxes( runCtx->meshsets[1], local_verts ) );
595 
596  runCtx->timer_pop();
597 
598  moab::EntityHandle covering_set;
599  runCtx->timer_push( "communicate the mesh" );
600  // TODO:: needs clarification
601  // we compute just intersection here, no need for extra ghost layers anyway
602  // ghost layers are needed in coverage for bilinear map, which does not actually need intersection
603  // this will be fixed in the future, bilinear map needs just coverage, not intersection
604  // so I am not passing the ghost layer here, even though there is an option in runCtx for a ghost layer
605  MB_CHK_ERR( mbintx->construct_covering_set(
606  runCtx->meshsets[0], covering_set ) ); // lots of communication if mesh is distributed very differently
607  runCtx->timer_pop();
608 
609  // print verbosely about the problem setting
610  {
611  moab::Range cintxverts, cintxelems;
612  MB_CHK_ERR( mbCore->get_entities_by_dimension( covering_set, 0, cintxverts ) );
613  MB_CHK_ERR( mbCore->get_entities_by_dimension( covering_set, 2, cintxelems ) );
614  velist[4] = cintxverts.size();
615  velist[5] = cintxelems.size();
616  }
617 
618  MPI_Reduce( velist, gvelist, 6, MPI_UINT64_T, MPI_SUM, 0, MPI_COMM_WORLD );
619 
620 #else
621  moab::EntityHandle covering_set = runCtx->meshsets[0];
622  for( int i = 0; i < 6; i++ )
623  gvelist[i] = velist[i];
624 #endif
625 
626  if( !proc_id )
627  {
628  outputFormatter.printf( 0, "The source set contains %lu vertices and %lu elements \n", gvelist[0],
629  gvelist[0] );
630  outputFormatter.printf( 0, "The covering set contains %lu vertices and %lu elements \n", gvelist[2],
631  gvelist[2] );
632  outputFormatter.printf( 0, "The target set contains %lu vertices and %lu elements \n", gvelist[1],
633  gvelist[1] );
634  }
635 
636  // Now let's invoke the MOAB intersection algorithm in parallel with a
637  // source and target mesh set representing two different decompositions
638  runCtx->timer_push( "compute intersections with MOAB" );
639  MB_CHK_SET_ERR( mbCore->create_meshset( moab::MESHSET_SET, intxset ), "Can't create new set" );
640  MB_CHK_SET_ERR( mbintx->intersect_meshes( covering_set, runCtx->meshsets[1], intxset ),
641  "Can't compute the intersection of meshes on the sphere" );
642  runCtx->timer_pop();
643 
644  // free the memory
645  delete mbintx;
646  }
647 
648  {
649  moab::Range intxelems, intxverts;
650  MB_CHK_ERR( mbCore->get_entities_by_dimension( intxset, 2, intxelems ) );
651  MB_CHK_ERR( mbCore->get_entities_by_dimension( intxset, 0, intxverts, true ) );
652  outputFormatter.printf( 0, "The intersection set contains %lu elements and %lu vertices \n",
653  intxelems.size(), intxverts.size() );
654 
655  moab::IntxAreaUtils areaAdaptorHuiller( moab::IntxAreaUtils::lHuiller ); // lHuiller, GaussQuadrature
656  double initial_sarea =
657  areaAdaptorHuiller.area_on_sphere( mbCore, runCtx->meshsets[0],
658  radius_src ); // use the target to compute the initial area
659  double initial_tarea =
660  areaAdaptorHuiller.area_on_sphere( mbCore, runCtx->meshsets[1],
661  radius_dest ); // use the target to compute the initial area
662  double intx_area = areaAdaptorHuiller.area_on_sphere( mbCore, intxset, radius_src );
663 
664  outputFormatter.printf( 0, "mesh areas: source = %12.10f, target = %12.10f, intersection = %12.10f \n",
665  initial_sarea, initial_tarea, intx_area );
666  outputFormatter.printf( 0, "relative error w.r.t source = %12.10e, target = %12.10e \n",
667  fabs( intx_area - initial_sarea ) / initial_sarea,
668  fabs( intx_area - initial_tarea ) / initial_tarea );
669  }
670 
671  // Write out our computed intersection file
672  if( !runCtx->skip_io )
673  {
674  MB_CHK_ERR( mbCore->write_mesh( "moab_intersection.h5m", &intxset, 1 ) );
675  }
676 
677  if( runCtx->computeWeights )
678  {
679  runCtx->timer_push( "compute weights with the Tempest meshes" );
680  // Call to generate an offline map with the tempest meshes
681  OfflineMap weightMap;
682  int err = GenerateOfflineMapWithMeshes( *runCtx->meshes[0], *runCtx->meshes[1], *runCtx->meshes[2],
683  runCtx->disc_methods[0], // std::string strInputType
684  runCtx->disc_methods[1], // std::string strOutputType,
685  runCtx->mapOptions, weightMap );
686  runCtx->timer_pop();
687 
688  std::map< std::string, std::string > mapAttributes;
689  if( err )
690  {
691  MB_SET_ERR( moab::MB_FAILURE, "Generating offline map with meshes failed." );
692  }
693  else
694  {
695  if( !runCtx->skip_io ) weightMap.Write( "outWeights.nc", mapAttributes );
696  }
697  }
698  }
699  else if( runCtx->meshType == moab::TempestRemapper::OVERLAP_MOAB )
700  {
701  // Usage: mpiexec -n 2 tools/mbtempest -t 5 -l mycs_2.h5m -l myico_2.h5m -f myoverlap_2.h5m
702 #ifdef MOAB_HAVE_MPI
704 #endif
705 
706  // print verbosely about the problem setting
707  size_t velist[4] = {}, gvelist[4] = {};
708  {
709  moab::Range srcverts, srcelems;
710  MB_CHK_ERR( mbCore->get_entities_by_dimension( runCtx->meshsets[0], 0, srcverts ) );
711  MB_CHK_ERR( mbCore->get_entities_by_dimension( runCtx->meshsets[0], 2, srcelems ) );
713  if( runCtx->enforceConvexity )
714  {
715  MB_CHK_ERR( moab::IntxUtils::enforce_convexity( mbCore, runCtx->meshsets[0], proc_id ) );
716  }
717  MB_CHK_ERR( areaAdaptor.positive_orientation( mbCore, runCtx->meshsets[0], radius_src ) );
718  // if( !proc_id )
719  // outputFormatter.printf( 0, "The source set contains %lu vertices and %lu elements \n", srcverts.size(),
720  // srcelems.size() );
721  velist[0] = srcverts.size();
722  velist[1] = srcelems.size();
723 
724  moab::Range tgtverts, tgtelems;
725  MB_CHK_ERR( mbCore->get_entities_by_dimension( runCtx->meshsets[1], 0, tgtverts ) );
726  MB_CHK_ERR( mbCore->get_entities_by_dimension( runCtx->meshsets[1], 2, tgtelems ) );
728  if( runCtx->enforceConvexity )
729  {
730  MB_CHK_ERR( moab::IntxUtils::enforce_convexity( mbCore, runCtx->meshsets[1], proc_id ) );
731  }
732  MB_CHK_ERR( areaAdaptor.positive_orientation( mbCore, runCtx->meshsets[1], radius_dest ) );
733  // if( !proc_id )
734  // outputFormatter.printf( 0, "The target set contains %lu vertices and %lu elements \n", tgtverts.size(),
735  // tgtelems.size() );
736  velist[2] = tgtverts.size();
737  velist[3] = tgtelems.size();
738  }
739  //MB_CHK_ERR( mbCore->write_file( "source_mesh.h5m", nullptr, writeOptions, &runCtx->meshsets[0], 1 ) );
740  //MB_CHK_ERR( mbCore->write_file( "target_mesh.h5m", nullptr, writeOptions, &runCtx->meshsets[1], 1 ) );
741 
742  // if( runCtx->nlayers && nprocs > 1 )
743  // {
744  // remapper.ResetMeshSet( moab::Remapper::SourceMesh, runCtx->meshsets[3] );
745  // runCtx->meshes[0] = remapper.GetMesh( moab::Remapper::SourceMesh ); // ?
746  // }
747 
748  // First compute the covering set such that the target elements are fully covered by the
749  // local source grid
750  runCtx->timer_push( "construct covering set for intersection" );
751  // if ghosting, no gnomonic
752  if( runCtx->nlayers >= 1 ) runCtx->useGnomonicProjection = false;
753  MB_CHK_ERR( remapper.ConstructCoveringSet( runCtx->epsrel, 1.0, 1.0, runCtx->boxeps, runCtx->rrmGrids,
754  runCtx->useGnomonicProjection, runCtx->nlayers ) );
755  runCtx->timer_pop();
756 
757 #ifdef MOAB_HAVE_MPI
758  MPI_Reduce( velist, gvelist, 4, MPI_UINT64_T, MPI_SUM, 0, MPI_COMM_WORLD );
759 #else
760  for( int i = 0; i < 4; i++ )
761  gvelist[i] = velist[i];
762 #endif
763 
764  if( !proc_id && runCtx->print_diagnostics )
765  {
766  outputFormatter.printf( 0, "The source set contains %lu vertices and %lu elements \n", gvelist[0],
767  gvelist[1] );
768  outputFormatter.printf( 0, "The target set contains %lu vertices and %lu elements \n", gvelist[2],
769  gvelist[3] );
770  }
771 
772  // Compute intersections with MOAB with either the Kd-tree or the advancing front algorithm
773  runCtx->timer_push( "setup and compute mesh intersections" );
774  MB_CHK_ERR( remapper.ComputeOverlapMesh( runCtx->kdtreeSearch, false ) );
775  runCtx->timer_pop();
776 
777  // print some diagnostic checks to see if the overlap grid resolved the input meshes
778  // correctly
779  double dTotalOverlapArea = 0.0;
780  if( runCtx->print_diagnostics )
781  {
782  moab::IntxAreaUtils areaAdaptorHuiller(
783  moab::IntxAreaUtils::GaussQuadrature ); // lHuiller, GaussQuadrature
784  double local_areas[3],
785  global_areas[3]; // Array for Initial area, and through Method 1 and Method 2
786  // local_areas[0] = area_on_sphere_lHuiller ( mbCore, runCtx->meshsets[1], radius_src );
787  local_areas[0] = areaAdaptorHuiller.area_on_sphere( mbCore, runCtx->meshsets[0], radius_src );
788  local_areas[1] = areaAdaptorHuiller.area_on_sphere( mbCore, runCtx->meshsets[1], radius_dest );
789  local_areas[2] = areaAdaptorHuiller.area_on_sphere( mbCore, runCtx->meshsets[2], radius_src );
790 
791 #ifdef MOAB_HAVE_MPI
792  MPI_Allreduce( &local_areas[0], &global_areas[0], 3, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD );
793 #else
794  global_areas[0] = local_areas[0];
795  global_areas[1] = local_areas[1];
796  global_areas[2] = local_areas[2];
797 #endif
798  if( !proc_id )
799  {
800  outputFormatter.printf( 0,
801  "initial area: source mesh = %12.14f, target mesh = "
802  "%12.14f, overlap mesh = %12.14f\n",
803  global_areas[0], global_areas[1], global_areas[2] );
804  // outputFormatter.printf ( 0, " area with l'Huiller: %12.14f with Girard:
805  // %12.14f\n", global_areas[2], global_areas[3] ); outputFormatter.printf ( 0, "
806  // relative difference areas = %12.10e\n", fabs ( global_areas[2] - global_areas[3]
807  // ) / global_areas[2] );
808  outputFormatter.printf( 0, "relative error w.r.t source = %12.14e, and target = %12.14e\n",
809  fabs( global_areas[0] - global_areas[2] ) / global_areas[0],
810  fabs( global_areas[1] - global_areas[2] ) / global_areas[1] );
811  }
812  dTotalOverlapArea = global_areas[2];
813  }
814 
815  if( runCtx->intxFilename.size() )
816  {
817  moab::EntityHandle writableOverlapSet;
818  MB_CHK_SET_ERR( mbCore->create_meshset( moab::MESHSET_SET, writableOverlapSet ), "Can't create new set" );
819  moab::EntityHandle meshOverlapSet = remapper.GetMeshSet( moab::Remapper::OverlapMesh );
820  moab::Range ovEnts;
821  MB_CHK_SET_ERR( mbCore->get_entities_by_dimension( meshOverlapSet, 2, ovEnts ), "Can't create new set" );
822  MB_CHK_SET_ERR( mbCore->get_entities_by_dimension( meshOverlapSet, 0, ovEnts ), "Can't create new set" );
823 
824 #ifdef MOAB_HAVE_MPI
825  // Do not remove ghosted entities if we still haven't computed weights
826  // Remove ghosted entities from overlap set before writing the new mesh set to file
827  if( nprocs > 1 )
828  {
829  moab::Range ghostedEnts;
830  MB_CHK_ERR( remapper.GetOverlapAugmentedEntities( ghostedEnts ) );
831  ovEnts = moab::subtract( ovEnts, ghostedEnts );
832 #ifdef MOAB_DBG
833  if( !runCtx->skip_io )
834  {
835  std::stringstream filename;
836  filename << "aug_overlap" << runCtx->pcomm->rank() << ".h5m";
837  MB_CHK_ERR( runCtx->mbcore->write_file( filename.str().c_str(), 0, 0, &meshOverlapSet, 1 ) );
838  }
839 #endif
840  }
841 #endif
842  MB_CHK_SET_ERR( mbCore->add_entities( writableOverlapSet, ovEnts ), "adding local intx cells failed" );
843 
844 #ifdef MOAB_HAVE_MPI
845 #ifdef MOAB_DBG
846  if( nprocs > 1 && !runCtx->skip_io )
847  {
848  std::stringstream filename;
849  filename << "writable_intx_" << runCtx->pcomm->rank() << ".h5m";
850  MB_CHK_ERR( runCtx->mbcore->write_file( filename.str().c_str(), 0, 0, &writableOverlapSet, 1 ) );
851  }
852 #endif
853 #endif
854 
855  size_t lastindex = runCtx->intxFilename.find_last_of( "." );
856  sstr.str( "" );
857  sstr << runCtx->intxFilename.substr( 0, lastindex ) << ".h5m";
858  if( !runCtx->proc_id )
859  std::cout << "Writing out the MOAB intersection mesh file to " << sstr.str() << std::endl;
860 
861  // Write out our computed intersection file
862  if( !runCtx->skip_io )
863  {
864  MB_CHK_ERR( mbCore->write_file( sstr.str().c_str(), nullptr, writeOptions, &writableOverlapSet, 1 ) );
865  }
866  }
867 
868  if( runCtx->computeWeights )
869  {
870  runCtx->meshes[2] = remapper.GetMesh( moab::Remapper::OverlapMesh );
871  if( !runCtx->proc_id ) std::cout << std::endl;
872 
873  runCtx->timer_push( "setup computation of weights" );
874  // Call to generate the remapping weights with the tempest meshes
875  moab::TempestOnlineMap* weightMap = new moab::TempestOnlineMap( &remapper );
876  runCtx->timer_pop();
877 
878  runCtx->timer_push( "compute weights with TempestRemap" );
880  runCtx->disc_methods[0], // std::string strInputType
881  runCtx->disc_methods[1], // std::string strOutputType,
882  runCtx->mapOptions, // const GenerateOfflineMapAlgorithmOptions& options
883  runCtx->doftag_names[0], // const std::string& source_tag_name
884  runCtx->doftag_names[1] // const std::string& target_tag_name
885  ) );
886  runCtx->timer_pop();
887 
888  weightMap->PrintMapStatistics();
889 
890  // Invoke the CheckMap routine on the TempestRemap serial interface directly, if running
891  // on a single process
892  if( runCtx->fCheck )
893  {
894  const double dNormalTolerance = 1.0E-8;
895  const double dStrictTolerance = 1.0E-12;
896  weightMap->CheckMap( runCtx->fCheck, runCtx->fCheck, runCtx->fCheck && ( runCtx->ensureMonotonicity ),
897  dNormalTolerance, dStrictTolerance, dTotalOverlapArea );
898  }
899 
900  if( runCtx->outFilename.size() )
901  {
902  std::map< std::string, std::string > attrMap;
903  attrMap["MOABversion"] = std::string( MOAB_PACKAGE_VERSION_STRING );
904  attrMap["Title"] = "MOAB-TempestRemap (mbtempest) Offline Regridding Weight Generator";
905  attrMap["normalization"] = "ovarea";
906  attrMap["remap_options"] = runCtx->mapOptions.strMethod;
907  attrMap["domain_a"] = runCtx->inFilenames[0];
908  attrMap["domain_b"] = runCtx->inFilenames[1];
909  if( runCtx->intxFilename.size() ) attrMap["domain_aUb"] = runCtx->intxFilename;
910  attrMap["map_aPb"] = runCtx->outFilename;
911  attrMap["methodorder_a"] = runCtx->disc_methods[0] + ":" + std::to_string( runCtx->disc_orders[0] ) +
912  ":" + std::string( runCtx->doftag_names[0] );
913  attrMap["concave_a"] = runCtx->mapOptions.fSourceConcave ? "true" : "false";
914  attrMap["methodorder_b"] = runCtx->disc_methods[1] + ":" + std::to_string( runCtx->disc_orders[1] ) +
915  ":" + std::string( runCtx->doftag_names[1] );
916  attrMap["concave_b"] = runCtx->mapOptions.fTargetConcave ? "true" : "false";
917  attrMap["bubble"] = runCtx->mapOptions.fNoBubble ? "false" : "true";
918  attrMap["history"] = historyStr;
919 
920  // Write the map file to disk in parallel using either HDF5 or SCRIP interface
921  // in extra case; maybe need a better solution, just create it with the right meshset
922  // from the beginning;
923  if( !runCtx->skip_io )
924  {
925  MB_CHK_ERR( weightMap->WriteParallelMap( runCtx->outFilename.c_str(), attrMap ) );
926  }
927  }
928 
929  if( runCtx->verifyWeights )
930  {
931  // Let us pick a sampling test function for solution evaluation
932  // SH, SV, FH, USERVAR
933  bool userVariable = false;
935  if( !runCtx->variableToVerify.compare( "SH" ) )
936  testFunction = &sample_slow_harmonic;
937  else if( !runCtx->variableToVerify.compare( "FH" ) )
938  testFunction = &sample_fast_harmonic;
939  else if( !runCtx->variableToVerify.compare( "SV" ) )
940  testFunction = &sample_stationary_vortex;
941  else
942  {
943  userVariable = runCtx->variableToVerify.size() ? true : false;
944  testFunction = runCtx->variableToVerify.size() ? nullptr : sample_stationary_vortex;
945  }
946 
947  moab::Tag srcAnalyticalFunction;
948  moab::Tag tgtAnalyticalFunction;
949  moab::Tag tgtProjectedFunction;
950  if( testFunction )
951  {
952  runCtx->timer_push( "describe a solution on source grid" );
953  MB_CHK_ERR( weightMap->DefineAnalyticalSolution( srcAnalyticalFunction, "AnalyticalSolnSrcExact",
954  moab::Remapper::SourceMesh, testFunction ) );
955  runCtx->timer_pop();
956 
957  runCtx->timer_push( "describe a solution on target grid" );
958 
959  MB_CHK_ERR( weightMap->DefineAnalyticalSolution( tgtAnalyticalFunction, "AnalyticalSolnTgtExact",
960  moab::Remapper::TargetMesh, testFunction,
961  &tgtProjectedFunction, "ProjectedSolnTgt" ) );
962  // rval = mbCore->write_file ( "tgtWithSolnTag.h5m", nullptr, writeOptions,
963  // &runCtx->meshsets[1], 1 ); MB_CHK_ERR ( rval );
964  runCtx->timer_pop();
965  }
966  else
967  {
968  MB_CHK_ERR( mbCore->tag_get_handle( runCtx->variableToVerify.c_str(), srcAnalyticalFunction ) );
969 
970  MB_CHK_ERR( mbCore->tag_get_handle( "ProjectedSolnTgt", 1, moab::MB_TYPE_DOUBLE,
971  tgtProjectedFunction,
973  }
974 
975  if( !runCtx->skip_io )
976  {
977  MB_CHK_ERR(
978  mbCore->write_file( "srcWithSolnTag.h5m", nullptr, writeOptions, &runCtx->meshsets[0], 1 ) );
979  }
980 
981  runCtx->timer_push( "compute solution projection on target grid" );
982  MB_CHK_ERR(
983  weightMap->ApplyWeights( srcAnalyticalFunction, tgtProjectedFunction, false, runCtx->cassType ) );
984  runCtx->timer_pop();
985 
986  if( !runCtx->skip_io )
987  {
988  MB_CHK_ERR(
989  mbCore->write_file( "tgtWithSolnTag2.h5m", nullptr, writeOptions, &runCtx->meshsets[1], 1 ) );
990  }
991 
992  if( nprocs == 1 && runCtx->baselineFile.size() )
993  {
994  // save the field from tgtWithSolnTag2 in a text file, and global ids for cells
995  moab::Range tgtCells;
996  MB_CHK_ERR( mbCore->get_entities_by_dimension( runCtx->meshsets[1], 2, tgtCells ) );
997  std::vector< int > globIds;
998  globIds.resize( tgtCells.size() );
999  std::vector< double > vals;
1000  vals.resize( tgtCells.size() );
1001  moab::Tag projTag;
1002  MB_CHK_ERR( mbCore->tag_get_handle( "ProjectedSolnTgt", projTag ) );
1003  moab::Tag gid = mbCore->globalId_tag();
1004  MB_CHK_ERR( mbCore->tag_get_data( gid, tgtCells, &globIds[0] ) );
1005  MB_CHK_ERR( mbCore->tag_get_data( projTag, tgtCells, &vals[0] ) );
1006  std::fstream fs;
1007  fs.open( runCtx->baselineFile.c_str(), std::fstream::out );
1008  fs << std::setprecision( 15 ); // maximum precision for doubles
1009  for( size_t i = 0; i < tgtCells.size(); i++ )
1010  fs << globIds[i] << " " << vals[i] << "\n";
1011  fs.close();
1012  // for good measure, save the source file too, with the tag AnalyticalSolnSrcExact
1013  // it will be used later to test, along with a target file
1014  if( !runCtx->skip_io )
1015  {
1016  MB_CHK_ERR( mbCore->write_file( "srcWithSolnTag.h5m", nullptr, writeOptions,
1017  &runCtx->meshsets[0], 1 ) );
1018  }
1019  }
1020 
1021  // compute error metrics if it is a known analytical functional
1022  if( !userVariable )
1023  {
1024  runCtx->timer_push( "compute error metrics against analytical solution on target grid" );
1025  std::map< std::string, double > errMetrics;
1026  MB_CHK_ERR( weightMap->ComputeMetrics( moab::Remapper::TargetMesh, tgtAnalyticalFunction,
1027  tgtProjectedFunction, errMetrics, true ) );
1028  runCtx->timer_pop();
1029  }
1030  }
1031 
1032  delete weightMap;
1033  }
1034  }
1035 
1036  // Clean up
1037  remapper.clear();
1038  delete runCtx;
1039  delete mbCore;
1040 
1041 #ifdef MOAB_HAVE_MPI
1042  MPI_Finalize();
1043 #endif
1044  exit( 0 );
1045 }

References moab::Core::add_entities(), moab::TempestOnlineMap::ApplyWeights(), moab::IntxAreaUtils::area_on_sphere(), ToolContext::baselineFile, ToolContext::boxeps, ToolContext::cassType, moab::ParallelComm::check_all_shared_handles(), moab::TempestRemapper::clear(), moab::TempestOnlineMap::ComputeMetrics(), moab::TempestRemapper::ComputeOverlapMesh(), ToolContext::computeWeights, moab::TempestRemapper::ConstructCoveringSet(), moab::TempestRemapper::constructEdgeMap, moab::TempestRemapper::ConvertTempestMesh(), moab::Core::create_meshset(), CreateTempestMesh(), moab::TempestOnlineMap::DefineAnalyticalSolution(), ToolContext::disc_methods, ToolContext::disc_orders, ToolContext::doftag_names, moab::IntxUtils::enforce_convexity(), ToolContext::enforceConvexity, ToolContext::ensureMonotonicity, ToolContext::epsrel, error(), ToolContext::fCheck, moab::Intx2Mesh::FindMaxEdges(), moab::IntxUtils::fix_degenerate_quads(), moab::IntxAreaUtils::GaussQuadrature, moab::TempestOnlineMap::GenerateRemappingWeights(), moab::Core::get_entities_by_dimension(), moab::TempestRemapper::GetMesh(), moab::TempestRemapper::GetMeshSet(), moab::TempestRemapper::GetOverlapAugmentedEntities(), moab::Core::globalId_tag(), ToolContext::inFilenames, moab::TempestRemapper::initialize(), moab::Intx2Mesh::intersect_meshes(), ToolContext::intxFilename, ToolContext::kdtreeSearch, moab::IntxAreaUtils::lHuiller, ToolContext::mapOptions, MB_CHK_ERR, MB_CHK_SET_ERR, MB_SET_ERR, MB_TAG_CREAT, MB_TAG_DENSE, MB_TYPE_DOUBLE, ToolContext::mbcore, ToolContext::meshes, MESHSET_SET, ToolContext::meshsets, ToolContext::meshType, moab::TempestRemapper::meshValidate, MOAB_PACKAGE_VERSION_STRING, ToolContext::nlayers, ToolContext::outFilename, ToolContext::outputFormatter, moab::TempestRemapper::OVERLAP_MEMORY, moab::TempestRemapper::OVERLAP_MOAB, moab::Remapper::OverlapMesh, ToolContext::ParseCLOptions(), moab::IntxAreaUtils::positive_orientation(), ToolContext::print_diagnostics, moab::DebugOutput::printf(), moab::TempestOnlineMap::PrintMapStatistics(), ToolContext::proc_id, ToolContext::rrmGrids, sample_fast_harmonic(), sample_slow_harmonic(), sample_stationary_vortex(), moab::Intx2Mesh::set_box_error(), moab::Intx2Mesh::set_error_tolerance(), moab::Intx2MeshOnSphere::set_radius_destination_mesh(), moab::Intx2MeshOnSphere::set_radius_source_mesh(), moab::Range::size(), ToolContext::skip_io, moab::Remapper::SourceMesh, moab::subtract(), moab::Core::tag_get_data(), moab::Core::tag_get_handle(), moab::Remapper::TargetMesh, ToolContext::timer_pop(), ToolContext::timer_push(), ToolContext::useGnomonicProjection, ToolContext::variableToVerify, ToolContext::verifyWeights, moab::Core::write_file(), moab::Core::write_mesh(), and moab::TempestOnlineMap::WriteParallelMap().

◆ sample_constant()

double sample_constant ( double  dLon,
double  dLat 
)
inline

Definition at line 1230 of file mbtempest.cpp.

1231 {
1232  return 1.0;
1233 }

◆ sample_fast_harmonic()

double sample_fast_harmonic ( double  dLon,
double  dLat 
)
inline

Definition at line 1224 of file mbtempest.cpp.

1225 {
1226  return ( 2.0 + pow( sin( 2.0 * dLat ), 16.0 ) * cos( 16.0 * dLon ) );
1227  // return (2.0 + pow(cos(2.0 * dLat), 16.0) * cos(16.0 * dLon));
1228 }

Referenced by main().

◆ sample_slow_harmonic()

double sample_slow_harmonic ( double  dLon,
double  dLat 
)
inline

Definition at line 1219 of file mbtempest.cpp.

1220 {
1221  return ( 2.0 + cos( dLat ) * cos( dLat ) * cos( 2.0 * dLon ) );
1222 }

Referenced by main().

◆ sample_stationary_vortex()

double sample_stationary_vortex ( double  dLon,
double  dLat 
)
inline

Find the rotated longitude and latitude of a point on a sphere with pole at (dLonC, dLatC).

Definition at line 1235 of file mbtempest.cpp.

1236 {
1237  const double dLon0 = 0.0;
1238  const double dLat0 = 0.6;
1239  const double dR0 = 3.0;
1240  const double dD = 5.0;
1241  const double dT = 6.0;
1242 
1243  /// Find the rotated longitude and latitude of a point on a sphere
1244  /// with pole at (dLonC, dLatC).
1245  {
1246  double dSinC = sin( dLat0 );
1247  double dCosC = cos( dLat0 );
1248  double dCosT = cos( dLat );
1249  double dSinT = sin( dLat );
1250 
1251  double dTrm = dCosT * cos( dLon - dLon0 );
1252  double dX = dSinC * dTrm - dCosC * dSinT;
1253  double dY = dCosT * sin( dLon - dLon0 );
1254  double dZ = dSinC * dSinT + dCosC * dTrm;
1255 
1256  dLon = atan2( dY, dX );
1257  if( dLon < 0.0 )
1258  {
1259  dLon += 2.0 * M_PI;
1260  }
1261  dLat = asin( dZ );
1262  }
1263 
1264  double dRho = dR0 * cos( dLat );
1265  double dVt = 3.0 * sqrt( 3.0 ) / 2.0 / cosh( dRho ) / cosh( dRho ) * tanh( dRho );
1266 
1267  double dOmega;
1268  if( dRho == 0.0 )
1269  {
1270  dOmega = 0.0;
1271  }
1272  else
1273  {
1274  dOmega = dVt / dRho;
1275  }
1276 
1277  return ( 1.0 - tanh( dRho / dD * sin( dLon - dOmega * dT ) ) );
1278 }

Referenced by main().