24 constexpr
double kPi = 3.14159265358979323846;
29 const double r = std::sqrt( x * x + y * y + z * z );
38 if( lon < 0.0 ) lon += 360.0;
56 "Failed to gather 2-D cells for Domain write" );
57 if( allCells.
empty() )
MB_SET_ERR( MB_FAILURE,
"No 2-D cells in file set; cannot write Domain grid" );
68 "Failed to filter owned cells for Domain write" );
76 int localMaxCorners = 0;
82 localMaxCorners = numConn;
88 _writeNC->myPcomm->proc_config().proc_comm() );
108 if( mGlobalIdTag ) mbImpl->
tag_get_data( mGlobalIdTag, &( *cit ), 1, &gid );
115 double cx = 0.0, cy = 0.0, cz = 0.0;
116 double firstLat = 0.0, firstLon = 0.0;
117 for(
int k = 0; k < numConn; ++k )
119 double vc[3] = { 0.0, 0.0, 0.0 };
124 double vlat = 0.0, vlon = 0.0;
185 MPI_Allreduce( &localN, &
mGlobalCells, 1, MPI_LONG, MPI_SUM,
186 _writeNC->myPcomm->proc_config().proc_comm() );
195 dbgOut.
tprintf( 1,
" Domain write: local_cells=%ld global_cells=%ld ni=%ld nj=%ld max_corners=%d\n",
mLocalCells,
202 std::vector< std::string >& ,
206 MB_SET_ERR( MB_FAILURE,
"Failed to define n dim" );
208 MB_SET_ERR( MB_FAILURE,
"Failed to define ni dim" );
210 MB_SET_ERR( MB_FAILURE,
"Failed to define nj dim" );
212 MB_SET_ERR( MB_FAILURE,
"Failed to define nv dim" );
219 MB_SET_ERR( MB_FAILURE,
"Failed to define xc var" );
221 MB_SET_ERR( MB_FAILURE,
"Failed to define yc var" );
223 MB_SET_ERR( MB_FAILURE,
"Failed to define xv var" );
225 MB_SET_ERR( MB_FAILURE,
"Failed to define yv var" );
227 MB_SET_ERR( MB_FAILURE,
"Failed to define mask var" );
229 MB_SET_ERR( MB_FAILURE,
"Failed to define area var" );
231 MB_SET_ERR( MB_FAILURE,
"Failed to define frac var" );
234 const char* deg_e =
"degrees_east";
235 const char* deg_n =
"degrees_north";
236 const char* rad2 =
"radian2";
237 const char* xv_name =
"longitude of grid cell verticies";
238 const char* yv_name =
"latitude of grid cell verticies";
239 const char* xc_name =
"longitude of grid cell center";
240 const char* yc_name =
"latitude of grid cell center";
241 const char* m_name =
"domain mask";
242 const char* a_name =
"area of grid cell in radians squared";
243 const char* f_name =
"fraction of grid cell that is active";
272 const char* title =
"MOAB:NCWriteDomain generated CESM-style domain file";
273 NCFUNC( put_att_text )(
_fileId, NC_GLOBAL,
"title", std::strlen( title ), title );
274 const char* conv =
"CF-1.0";
275 NCFUNC( put_att_text )(
_fileId, NC_GLOBAL,
"Conventions", std::strlen( conv ), conv );
283 std::vector< int >& )
287 auto writeAll = [&](
const std::vector< int >& allGids,
const std::vector< double >& allXc,
288 const std::vector< double >& allYc,
const std::vector< double >& allXv,
289 const std::vector< double >& allYv,
const std::vector< int >& allMask,
290 const std::vector< double >& allAreas,
const std::vector< double >& allFrac ) ->
ErrorCode {
291 const long N =
static_cast< long >( allGids.size() );
295 std::vector< long > perm( N );
296 for(
long i = 0; i < N; ++i )
298 std::sort( perm.begin(), perm.end(), [&](
long a,
long b ) { return allGids[a] < allGids[b]; } );
300 std::vector< double > sXc( N ), sYc( N ), sXv( N * ncpc ), sYv( N * ncpc );
301 std::vector< int > sMask( N );
302 std::vector< double > sArea( N, 0.0 ), sFrac( N, 1.0 );
303 const bool haveArea = !allAreas.empty();
304 for(
long i = 0; i < N; ++i )
306 const long src = perm[i];
309 sMask[i] = allMask[src];
310 if( haveArea ) sArea[i] = allAreas[src];
311 if( !allFrac.empty() ) sFrac[i] = allFrac[src];
312 for(
int k = 0; k < ncpc; ++k )
314 sXv[i * ncpc + k] = allXv[src * ncpc + k];
315 sYv[i * ncpc + k] = allYv[src * ncpc + k];
321 const size_t s2[2] = { 0, 0 };
322 const size_t c2[2] = { 1,
static_cast< size_t >( N ) };
323 const size_t s3[3] = { 0, 0, 0 };
324 const size_t c3[3] = { 1,
static_cast< size_t >( N ),
static_cast< size_t >( ncpc ) };
327 MB_SET_ERR( MB_FAILURE,
"Failed to write xc" );
329 MB_SET_ERR( MB_FAILURE,
"Failed to write yc" );
331 MB_SET_ERR( MB_FAILURE,
"Failed to write xv" );
333 MB_SET_ERR( MB_FAILURE,
"Failed to write yv" );
335 MB_SET_ERR( MB_FAILURE,
"Failed to write mask" );
337 MB_SET_ERR( MB_FAILURE,
"Failed to write area" );
339 MB_SET_ERR( MB_FAILURE,
"Failed to write frac" );
346 MPI_Comm comm =
_writeNC->myPcomm->proc_config().proc_comm();
347 const int rank =
_writeNC->myPcomm->proc_config().proc_rank();
348 const int size =
_writeNC->myPcomm->proc_config().proc_size();
352 std::vector< int > counts( size, 0 ), displs( size, 0 );
353 MPI_Gather( &myN, 1, MPI_INT, counts.data(), 1, MPI_INT, 0, comm );
357 for(
int i = 0; i < size; ++i )
364 std::vector< int > allGids;
365 std::vector< double > allXc, allYc, allXv, allYv;
366 std::vector< int > allMask;
367 std::vector< double > allAreas, allFrac;
373 allXv.resize(
static_cast< size_t >(
mGlobalCells ) * ncpc );
374 allYv.resize(
static_cast< size_t >(
mGlobalCells ) * ncpc );
380 MPI_Gatherv(
mLocalGids.data(), myN, MPI_INT, allGids.data(), counts.data(), displs.data(), MPI_INT, 0, comm );
381 MPI_Gatherv(
mXc.data(), myN, MPI_DOUBLE, allXc.data(), counts.data(), displs.data(), MPI_DOUBLE, 0, comm );
382 MPI_Gatherv(
mYc.data(), myN, MPI_DOUBLE, allYc.data(), counts.data(), displs.data(), MPI_DOUBLE, 0, comm );
383 MPI_Gatherv(
mMask.data(), myN, MPI_INT, allMask.data(), counts.data(), displs.data(), MPI_INT, 0, comm );
385 MPI_Gatherv(
mAreas.data(), myN, MPI_DOUBLE, allAreas.data(), counts.data(), displs.data(), MPI_DOUBLE, 0,
387 MPI_Gatherv(
mFrac.data(), myN, MPI_DOUBLE, allFrac.data(), counts.data(), displs.data(), MPI_DOUBLE, 0, comm );
389 std::vector< int > countsC( size, 0 ), displsC( size, 0 );
393 for(
int i = 0; i < size; ++i )
395 countsC[i] = counts[i] * ncpc;
400 MPI_Gatherv(
mXv.data(), myN * ncpc, MPI_DOUBLE, allXv.data(), countsC.data(), displsC.data(), MPI_DOUBLE, 0,
402 MPI_Gatherv(
mYv.data(), myN * ncpc, MPI_DOUBLE, allYv.data(), countsC.data(), displsC.data(), MPI_DOUBLE, 0,
406 if( rank == 0 ) rc = writeAll( allGids, allXc, allYc, allXv, allYv, allMask, allAreas, allFrac );
407 int rcInt =
static_cast< int >( rc );
408 MPI_Bcast( &rcInt, 1, MPI_INT, 0, comm );
411 dbgOut.
tprintf( 1,
" Domain write: gathered+wrote %ld cells from %d ranks\n",
mGlobalCells, size );
420 std::vector< int >& )