20 #ifndef MOAB_HAVE_TEMPESTREMAP
21 #error Tool requires compilation with TempestRemap dependency
25 #include "OfflineMap.h"
26 #include "netcdfcpp.h"
27 #include "NetCDFUtilities.h"
28 #include "DataArray2D.h"
32 template <
typename T >
37 std::vector< T >& data )
39 int* tag_sizes =
new int[sets.
size()];
40 const void** tag_data = (
const void**)
new void*[sets.
size()];
45 for(
unsigned is = 0; is < sets.
size(); ++is )
46 out_data_size += tag_sizes[is];
48 data.resize( out_data_size );
50 for(
unsigned index = 0; index < sets.
size(); index++ )
52 T* m_vals = (T*)tag_data[index];
53 for(
int k = 0; k < tag_sizes[index]; k++ )
55 data[ioffset++] = m_vals[k];
62 void ReadFileMetaData( std::string& metaFilename, std::map< std::string, std::string >& metadataVals )
64 std::ifstream metafile;
67 metafile.open( metaFilename.c_str() );
68 metadataVals[
"Title"] =
"MOAB-TempestRemap (MBTR) Offline Regridding Weight Converter (h5mtoscrip)";
69 std::string key, value;
70 while( std::getline( metafile, line ) )
72 size_t lastindex = line.find_last_of(
"=" );
73 key = line.substr( 0, lastindex - 1 );
74 value = line.substr( lastindex + 2, line.length() );
76 metadataVals[std::string( key )] = std::string( value );
81 int main(
int argc,
char* argv[] )
85 NcError error2( NcError::verbose_nonfatal );
86 std::stringstream sstr;
88 std::string h5mfilename, scripfile;
90 bool writeXYCoords =
false;
93 MPI_Init( &argc, &argv );
96 opts.
addOpt< std::string >(
"weights,w",
"h5m remapping weights filename", &h5mfilename );
97 opts.
addOpt< std::string >(
"scrip,s",
"Output SCRIP map filename", &scripfile );
98 opts.
addOpt<
int >(
"dim,d",
"Dimension of entities to use for partitioning", &dimension );
99 opts.
addOpt<
void >(
"mesh,m",
"Only convert the mesh and exclude the remap weight details", &noMap );
100 opts.
addOpt<
void >(
"coords,c",
"Write the center and vertex coordinates in lat/lon format", &writeXYCoords );
104 if( h5mfilename.empty() || scripfile.empty() )
118 const std::string partition_set_name =
"PARALLEL_PARTITION";
119 const std::string global_id_name =
"GLOBAL_ID";
127 NcError error_temp( NcError::verbose_fatal );
130 NcFile ncMap( scripfile.c_str(), NcFile::Replace, NULL, 0, NcFile::Offset64Bits );
131 if( !ncMap.is_valid() )
133 _EXCEPTION1(
"Unable to open output map file \"%s\"", scripfile.c_str() );
138 std::map< std::string, std::string > mapAttributes;
139 size_t lastindex = h5mfilename.find_last_of(
"." );
140 std::stringstream sstr;
141 sstr << h5mfilename.substr( 0, lastindex ) <<
".meta";
142 std::string metaFilename = sstr.str();
144 mapAttributes[
"Command"] =
145 "Converted with MOAB:h5mtoscrip with --w=" + h5mfilename +
" and --s=" + scripfile;
148 std::map< std::string, std::string >::const_iterator iterAttributes = mapAttributes.begin();
149 for( ; iterAttributes != mapAttributes.end(); iterAttributes++ )
152 std::cout << iterAttributes->first <<
" -- " << iterAttributes->second << std::endl;
153 ncMap.add_att( iterAttributes->first.c_str(), iterAttributes->second.c_str() );
158 Tag globalIDTag, materialSetTag;
192 int smat_metadata_glb[13];
199 ( ( ( a ) == 0 ) ? "FV" : ( ( ( a ) == 1 ) ? "cGLL" : "dGLL" ) ) \
202 int nA = smat_metadata_glb[0];
203 int nB = smat_metadata_glb[1];
204 int nVA = smat_metadata_glb[2];
205 int nVB = smat_metadata_glb[3];
206 int nDofB = smat_metadata_glb[4];
207 int nDofA = smat_metadata_glb[5];
208 int NNZ = smat_metadata_glb[6];
209 int nOrdA = smat_metadata_glb[7];
210 int nOrdB = smat_metadata_glb[8];
211 int nBasA = smat_metadata_glb[9];
212 std::string methodA =
DTYPE( nBasA );
213 int nBasB = smat_metadata_glb[10];
214 std::string methodB =
DTYPE( nBasB );
215 int bConserved = smat_metadata_glb[11];
216 int bMonotonicity = smat_metadata_glb[12];
218 EntityHandle source_mesh = 0, target_mesh = 0, overlap_mesh = 0;
219 for(
unsigned im = 0; im < meshsets.
size(); ++im )
223 if( elems.
size() - nA == 0 && source_mesh == 0 )
224 source_mesh = meshsets[im];
225 else if( elems.
size() - nB == 0 && target_mesh == 0 )
226 target_mesh = meshsets[im];
227 else if( overlap_mesh == 0 )
228 overlap_mesh = meshsets[im];
233 Tag srcIDTag, srcAreaTag, tgtIDTag, tgtAreaTag;
238 Tag smatRowdataTag, smatColdataTag, smatValsdataTag;
242 Tag srcCenterLon, srcCenterLat, tgtCenterLon, tgtCenterLat;
247 Tag srcVertexLon, srcVertexLat, tgtVertexLon, tgtVertexLat;
259 std::vector< int > src_gids, tgt_gids;
260 std::vector< double > src_areas, tgt_areas;
261 int srcID_size, tgtID_size, srcArea_size, tgtArea_size;
267 assert( srcArea_size == srcID_size );
268 assert( tgtArea_size == tgtID_size );
270 std::vector< double > src_glob_areas( nDofA, 0.0 ), tgt_glob_areas( nDofB, 0.0 );
271 for(
int i = 0; i < srcArea_size; ++i )
275 assert( i < srcID_size );
276 assert( src_gids[i] < nDofA );
277 if( src_areas[i] > src_glob_areas[src_gids[i]] ) src_glob_areas[src_gids[i]] = src_areas[i];
279 for(
int i = 0; i < tgtArea_size; ++i )
283 assert( i < tgtID_size );
284 assert( tgt_gids[i] < nDofB );
285 if( tgt_areas[i] > tgt_glob_areas[tgt_gids[i]] ) tgt_glob_areas[tgt_gids[i]] = tgt_areas[i];
289 int nSrcGridDims = 1;
290 int nDstGridDims = 1;
292 NcDim* dimSrcGridRank = ncMap.add_dim(
"src_grid_rank", nSrcGridDims );
293 NcDim* dimDstGridRank = ncMap.add_dim(
"dst_grid_rank", nDstGridDims );
295 NcVar* varSrcGridDims = ncMap.add_var(
"src_grid_dims", ncInt, dimSrcGridRank );
296 NcVar* varDstGridDims = ncMap.add_var(
"dst_grid_dims", ncInt, dimDstGridRank );
300 varSrcGridDims->put( &nA, 1 );
301 varSrcGridDims->add_att(
"name0",
"num_elem" );
305 varSrcGridDims->put( &nDofA, 1 );
306 varSrcGridDims->add_att(
"name1",
"num_dof" );
311 varDstGridDims->put( &nB, 1 );
312 varDstGridDims->add_att(
"name0",
"num_elem" );
316 varDstGridDims->put( &nDofB, 1 );
317 varDstGridDims->add_att(
"name1",
"num_dof" );
321 NcDim* dimNA = ncMap.add_dim(
"n_a", nDofA );
322 NcDim* dimNB = ncMap.add_dim(
"n_b", nDofB );
325 const int nva = ( nA == nDofA ? nVA : 1 );
326 const int nvb = ( nB == nDofB ? nVB : 1 );
327 NcDim* dimNVA = ncMap.add_dim(
"nv_a", nva );
328 NcDim* dimNVB = ncMap.add_dim(
"nv_b", nvb );
337 NcVar* varYCA = ncMap.add_var(
"yc_a", ncDouble, dimNA );
338 NcVar* varYCB = ncMap.add_var(
"yc_b", ncDouble, dimNB );
340 NcVar* varXCA = ncMap.add_var(
"xc_a", ncDouble, dimNA );
341 NcVar* varXCB = ncMap.add_var(
"xc_b", ncDouble, dimNB );
343 NcVar* varYVA = ncMap.add_var(
"yv_a", ncDouble, dimNA , dimNVA );
344 NcVar* varYVB = ncMap.add_var(
"yv_b", ncDouble, dimNB , dimNVB );
346 NcVar* varXVA = ncMap.add_var(
"xv_a", ncDouble, dimNA , dimNVA );
347 NcVar* varXVB = ncMap.add_var(
"xv_b", ncDouble, dimNB , dimNVB );
349 varYCA->add_att(
"units",
"degrees" );
350 varYCB->add_att(
"units",
"degrees" );
352 varXCA->add_att(
"units",
"degrees" );
353 varXCB->add_att(
"units",
"degrees" );
355 varYVA->add_att(
"units",
"degrees" );
356 varYVB->add_att(
"units",
"degrees" );
358 varXVA->add_att(
"units",
"degrees" );
359 varXVB->add_att(
"units",
"degrees" );
361 std::vector< double > src_centerlat, src_centerlon;
363 rval =
get_vartag_data( mbCore, srcCenterLat, sets, srccenter_size, src_centerlat );
MB_CHK_SET_ERR( rval,
"Getting source mesh areas failed" );
364 rval =
get_vartag_data( mbCore, srcCenterLon, sets, srccenter_size, src_centerlon );
MB_CHK_SET_ERR( rval,
"Getting target mesh areas failed" );
365 std::vector< double > src_glob_centerlat( nDofA, 0.0 ), src_glob_centerlon( nDofA, 0.0 );
367 for(
int i = 0; i < srccenter_size; ++i )
369 assert( i < srcID_size );
370 assert( src_gids[i] < nDofA );
372 src_glob_centerlat[src_gids[i]] = src_centerlat[i];
373 src_glob_centerlon[src_gids[i]] = src_centerlon[i];
376 std::vector< double > tgt_centerlat, tgt_centerlon;
378 rval =
get_vartag_data( mbCore, tgtCenterLat, sets, tgtcenter_size, tgt_centerlat );
MB_CHK_SET_ERR( rval,
"Getting source mesh areas failed" );
379 rval =
get_vartag_data( mbCore, tgtCenterLon, sets, tgtcenter_size, tgt_centerlon );
MB_CHK_SET_ERR( rval,
"Getting target mesh areas failed" );
380 std::vector< double > tgt_glob_centerlat( nDofB, 0.0 ), tgt_glob_centerlon( nDofB, 0.0 );
381 for(
int i = 0; i < tgtcenter_size; ++i )
383 assert( i < tgtID_size );
384 assert( tgt_gids[i] < nDofB );
386 tgt_glob_centerlat[tgt_gids[i]] = tgt_centerlat[i];
387 tgt_glob_centerlon[tgt_gids[i]] = tgt_centerlon[i];
390 varYCA->put( &( src_glob_centerlat[0] ), nDofA );
391 varYCB->put( &( tgt_glob_centerlat[0] ), nDofB );
392 varXCA->put( &( src_glob_centerlon[0] ), nDofA );
393 varXCB->put( &( tgt_glob_centerlon[0] ), nDofB );
395 src_centerlat.clear();
396 src_centerlon.clear();
397 tgt_centerlat.clear();
398 tgt_centerlon.clear();
400 DataArray2D< double > src_glob_vertexlat( nDofA, nva ), src_glob_vertexlon( nDofA, nva );
403 std::vector< double > src_vertexlat, src_vertexlon;
405 rval =
get_vartag_data( mbCore, srcVertexLat, sets, srcvertex_size, src_vertexlat );
MB_CHK_SET_ERR( rval,
"Getting source mesh areas failed" );
406 rval =
get_vartag_data( mbCore, srcVertexLon, sets, srcvertex_size, src_vertexlon );
MB_CHK_SET_ERR( rval,
"Getting target mesh areas failed" );
408 for(
unsigned vIndex = 0; vIndex < src_gids.size(); ++vIndex )
410 for(
int vNV = 0; vNV < nva; ++vNV )
412 assert( offset < srcvertex_size );
413 src_glob_vertexlat[src_gids[vIndex]][vNV] = src_vertexlat[offset];
414 src_glob_vertexlon[src_gids[vIndex]][vNV] = src_vertexlon[offset];
420 DataArray2D< double > tgt_glob_vertexlat( nDofB, nvb ), tgt_glob_vertexlon( nDofB, nvb );
423 std::vector< double > tgt_vertexlat, tgt_vertexlon;
425 rval =
get_vartag_data( mbCore, tgtVertexLat, sets, tgtvertex_size, tgt_vertexlat );
MB_CHK_SET_ERR( rval,
"Getting source mesh areas failed" );
426 rval =
get_vartag_data( mbCore, tgtVertexLon, sets, tgtvertex_size, tgt_vertexlon );
MB_CHK_SET_ERR( rval,
"Getting target mesh areas failed" );
428 for(
unsigned vIndex = 0; vIndex < tgt_gids.size(); ++vIndex )
430 for(
int vNV = 0; vNV < nvb; ++vNV )
432 assert( offset < tgtvertex_size );
433 tgt_glob_vertexlat[tgt_gids[vIndex]][vNV] = tgt_vertexlat[offset];
434 tgt_glob_vertexlon[tgt_gids[vIndex]][vNV] = tgt_vertexlon[offset];
440 varYVA->put( &( src_glob_vertexlat[0][0] ), nDofA, nva );
441 varYVB->put( &( tgt_glob_vertexlat[0][0] ), nDofB, nvb );
443 varXVA->put( &( src_glob_vertexlon[0][0] ), nDofA, nva );
444 varXVB->put( &( tgt_glob_vertexlon[0][0] ), nDofB, nvb );
448 NcVar* varAreaA = ncMap.add_var(
"area_a", ncDouble, dimNA );
449 varAreaA->put( &( src_glob_areas[0] ), nDofA );
452 NcVar* varAreaB = ncMap.add_var(
"area_b", ncDouble, dimNB );
453 varAreaB->put( &( tgt_glob_areas[0] ), nDofB );
456 std::vector< int > mat_rows, mat_cols;
457 std::vector< double > mat_vals;
458 int row_sizes, col_sizes, val_sizes;
460 assert( row_sizes == NNZ );
462 assert( col_sizes == NNZ );
464 assert( val_sizes == NNZ );
468 SparseMatrix< double > mapMatrix;
470 for(
int innz = 0; innz < NNZ; ++innz )
473 if( fabs( mapMatrix( mat_rows[innz], mat_cols[innz] ) ) > 1e-12 )
475 printf(
"Adding to existing loc: (%d, %d) = %12.8f\n", mat_rows[innz], mat_cols[innz],
476 mapMatrix( mat_rows[innz], mat_cols[innz] ) );
479 mapMatrix( mat_rows[innz], mat_cols[innz] ) += mat_vals[innz];
483 DataArray1D< int > vecRow;
484 DataArray1D< int > vecCol;
485 DataArray1D< double > vecS;
487 mapMatrix.GetEntries( vecRow, vecCol, vecS );
489 int nS = vecS.GetRows();
497 printf(
"Primary sets: %15zu\n", sets.
size() );
498 printf(
"Original NNZ: %18d\n", NNZ );
499 printf(
"Consolidated Total NNZ: %8d\n", nS );
500 printf(
"Conservative weights ? %6d\n", ( bConserved > 0 ) );
501 printf(
"Monotone weights ? %10d\n", ( bMonotonicity > 0 ) );
503 printf(
"\n--------------------------------------------------------------\n" );
504 printf(
"%20s %21s %15s\n",
"Description",
"Source",
"Target" );
505 printf(
"--------------------------------------------------------------\n" );
507 printf(
"%25s %15d %15d\n",
"Number of elements:", nA, nB );
508 printf(
"%25s %15d %15d\n",
"Number of DoFs:", nDofA, nDofB );
509 printf(
"%25s %15d %15d\n",
"Maximum vertex/element:", nVA, nVB );
510 printf(
"%25s %15s %15s\n",
"Discretization type:", methodA.c_str(), methodB.c_str() );
511 printf(
"%25s %15d %15d\n",
"Discretization order:", nOrdA, nOrdB );
515 DataArray1D< double > dFracA( nDofA );
516 DataArray1D< double > dFracB( nDofB );
518 for(
int i = 0; i < nS; i++ )
522 dFracA[vecCol[i]] += vecS[i] / src_glob_areas[vecCol[i]] * tgt_glob_areas[vecRow[i]];
523 dFracB[vecRow[i]] += vecS[i];
526 NcVar* varFracA = ncMap.add_var(
"frac_a", ncDouble, dimNA );
527 varFracA->put( &( dFracA[0] ), nDofA );
528 varFracA->add_att(
"name",
"fraction of target coverage of source dof" );
529 varFracA->add_att(
"units",
"unitless" );
531 NcVar* varFracB = ncMap.add_var(
"frac_b", ncDouble, dimNB );
532 varFracB->put( &( dFracB[0] ), nDofB );
533 varFracB->add_att(
"name",
"fraction of source coverage of target dof" );
534 varFracB->add_att(
"units",
"unitless" );
538 NcDim* dimNS = ncMap.add_dim(
"n_s", nS );
540 NcVar* varRow = ncMap.add_var(
"row", ncInt, dimNS );
541 varRow->add_att(
"name",
"sparse matrix target dof index" );
542 varRow->add_att(
"first_index",
"1" );
544 NcVar* varCol = ncMap.add_var(
"col", ncInt, dimNS );
545 varCol->add_att(
"name",
"sparse matrix source dof index" );
546 varCol->add_att(
"first_index",
"1" );
548 NcVar* varS = ncMap.add_var(
"S", ncDouble, dimNS );
549 varS->add_att(
"name",
"sparse matrix coefficient" );
552 for(
int i = 0; i < nS; i++ )
558 varRow->set_cur( (
long)0 );
559 varRow->put( &( vecRow[0] ), nS );
561 varCol->set_cur( (
long)0 );
562 varCol->put( &( vecCol[0] ), nS );
564 varS->set_cur( (
long)0 );
565 varS->put( &( vecS[0] ), nS );
571 catch( std::exception& e )
573 std::cout <<
" exception caught during tree initialization " << e.what() << std::endl;