Skip to content

Commit

Permalink
Merge pull request #26 from nextsimhub/transpose-mask
Browse files Browse the repository at this point in the history
Transpose input to match changing on indices in nextsimdg
  • Loading branch information
TomMelt authored Jan 22, 2024
2 parents b5bb992 + 1655c42 commit 369fec2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
-DTrilinos_ENABLE_ALL_PACKAGES:BOOL=OFF \
-DTrilinos_ENABLE_Zoltan:BOOL=ON \
-DTrilinos_ENABLE_Fortran:BOOL=OFF \
-DZoltan_ENABLE_EXAMPLES:BOOL=ON \
-DZoltan_ENABLE_TESTS:BOOL=ON \
-DZoltan_ENABLE_EXAMPLES:BOOL=OFF \
-DZoltan_ENABLE_TESTS:BOOL=OFF \
-DBUILD_SHARED_LIBS=ON \
-Bbuild -S.
sudo cmake --build build --config Release --target install
Expand All @@ -35,7 +35,7 @@ jobs:
cd PnetCDF
autoreconf -i
CXX=mpicxx CC=mpicc ./configure --disable-fortran --enable-shared
make -j2
make
sudo make install
cd ..
git clone -b v4.8.1 https://github.com/Unidata/netcdf-c.git
Expand All @@ -45,6 +45,8 @@ jobs:
-DCMAKE_C_COMPILER=mpicc \
-DCMAKE_CXX_COMPILER=mpicxx \
-DCMAKE_BUILD_TYPE=Release \
-DENABLE_TESTS=OFF \
-DENABLE_PARALLEL_TESTS=OFF \
-Bbuild -S.
sudo cmake --build build --config Release --target install
cd ..
Expand All @@ -69,7 +71,7 @@ jobs:
- name: Install dependencies
run: |
brew install cmake
brew extract --version=2.13.9 catch2 homebrew/cask
brew extract --version=2.13.9 catch2 homebrew/cask-versions
brew install catch2@2.13.9
brew install boost
brew install hdf5-mpi
Expand All @@ -81,8 +83,8 @@ jobs:
-DTrilinos_ENABLE_ALL_PACKAGES:BOOL=OFF \
-DTrilinos_ENABLE_Zoltan:BOOL=ON \
-DTrilinos_ENABLE_Fortran:BOOL=OFF \
-DZoltan_ENABLE_EXAMPLES:BOOL=ON \
-DZoltan_ENABLE_TESTS:BOOL=ON \
-DZoltan_ENABLE_EXAMPLES:BOOL=OFF \
-DZoltan_ENABLE_TESTS:BOOL=OFF \
-DBUILD_SHARED_LIBS=ON \
-Bbuild -S.
sudo cmake --build build --config Release --target install
Expand All @@ -94,6 +96,8 @@ jobs:
-DCMAKE_C_COMPILER=mpicc \
-DCMAKE_CXX_COMPILER=mpicxx \
-DCMAKE_BUILD_TYPE=Release \
-DENABLE_TESTS=OFF \
-DENABLE_PARALLEL_TESTS=OFF \
-Bbuild -S.
sudo cmake --build build --config Release --target install
cd ..
Expand Down
40 changes: 30 additions & 10 deletions Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,21 @@ Grid::Grid(MPI_Comm comm, const std::string& filename,
if (ret != NC_NOERR)
data_nc_id = nc_id;
int dim0_nc_id, dim1_nc_id;
NC_CHECK(nc_inq_dimid(data_nc_id, dim0_name.c_str(), &dim0_nc_id));
NC_CHECK(nc_inq_dimid(data_nc_id, dim1_name.c_str(), &dim1_nc_id));
// I have switched the order of the dimensions here to reflect the change of
// indices in nextsim-dg
NC_CHECK(nc_inq_dimid(data_nc_id, dim1_name.c_str(), &dim0_nc_id));
NC_CHECK(nc_inq_dimid(data_nc_id, dim0_name.c_str(), &dim1_nc_id));

CHECK_MPI(MPI_Comm_rank(comm, &_rank));

// Retrieve the extent of each dimension of interest. The dimensions of
// interest are the spatial dimensions of the grid. These are named "x" and
// "y" by default.
// I have switched the order of the dimensions here to reflect the change of
// indices in nextsim-dg
size_t tmp_0, tmp_1;
NC_CHECK(nc_inq_dimlen(data_nc_id, dim0_nc_id, &tmp_0));
NC_CHECK(nc_inq_dimlen(data_nc_id, dim1_nc_id, &tmp_1));
NC_CHECK(nc_inq_dimlen(data_nc_id, dim1_nc_id, &tmp_0));
NC_CHECK(nc_inq_dimlen(data_nc_id, dim0_nc_id, &tmp_1));
_global_ext_0 = static_cast<int>(tmp_0);
_global_ext_1 = static_cast<int>(tmp_1);
_global_ext_blk_0 = ceil(static_cast<float>(_global_ext_0) / _blk_factor_0);
Expand Down Expand Up @@ -138,24 +142,40 @@ Grid::Grid(MPI_Comm comm, const std::string& filename,
int dim_id[NDIMS];
char dim_name[NDIMS][128];
NC_CHECK(nc_inq_vardimid(data_nc_id, mask_nc_id, &dim_id[0]));
NC_CHECK(nc_inq_dimname(data_nc_id, dim_id[0], &dim_name[0][0]));
NC_CHECK(nc_inq_dimname(data_nc_id, dim_id[1], &dim_name[1][0]));
// I have switched the order of the dimensions here to reflect the change of
// indices in nextsim-dg
NC_CHECK(nc_inq_dimname(data_nc_id, dim_id[1], &dim_name[0][0]));
NC_CHECK(nc_inq_dimname(data_nc_id, dim_id[0], &dim_name[1][0]));
if (dim_name[0] != dim0_name || dim_name[1] != dim1_name) {
throw std::runtime_error("Dimension ordering provided does not match "
"ordering in netCDF grid file");
}

_land_mask.resize(_num_objects);
size_t start[NDIMS], count[NDIMS];
// I have switched the order of the dimensions here to reflect the change of
// indices in nextsim-dg
// Coordinate of first element
start[0] = _global_0;
start[1] = _global_1;
start[1] = _global_0;
start[0] = _global_1;
// Number of elements in every extension
count[0] = _local_ext_0;
count[1] = _local_ext_1;
count[1] = _local_ext_0;
count[0] = _local_ext_1;
NC_CHECK(nc_get_vara_int(data_nc_id, mask_nc_id, start, count,
_land_mask.data()));

// create copy of land mask ready to transpose
std::vector<int> _land_mask_copy(_land_mask);

// transpose _land_mask to reflect the change of indices in nextsim-dg
int index = 0;
for (size_t j = 0; j < count[1]; j++) {
for (size_t i = 0; i < count[0]; i++) {
_land_mask[index] = _land_mask_copy[i*count[1]+j];
index++;
}
}

// Apply land mask
if (_blk_factor_0 == 1 && _blk_factor_1 == 1) {
for (int i = 0; i < _num_objects; i++) {
Expand Down
8 changes: 7 additions & 1 deletion Grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,20 @@ class LIB_EXPORT Grid
* dim1) dimensions of the Grid class, where dim1 is defined as the fastest
* increasing dimension. For example, if the dimensions of interest in the
* netCDF file are named x and y and the variables are dimensioned as (y, x),
* then the y dimension will be mapped to dim0 and x to dim1 of the Grid
* then the y dimension will be mapped to dim1 and x to dim0 of the Grid
* class, since the netCDF C/C++ convention is that the last dimension in the
* CDL notation is the fastest increasing dimension. The default dimension
* names for the netCDF file are "x" for dim0 and "y" for dim1, and the
* default name for the land mask variable is "mask". the We assume that all
* variables defined in the netCDF file follow the same convention in terms of
* dimension ordering.
*
* The code was originally written with the assumption that nextsim-dg would
* store arrays in (x,y) order. However, the indices have since been swapped
* and now nextsim-dg uses (y,x) order. To account for the switch of indices
* the grid input netcdf file is now transposed when it is read-in and the x
* and y dims are swapped in Grid.cpp.
*
* @param comm MPI communicator.
* @param filename Grid file in netCDF format.
* @param dim0_name Name of 1st grid dimension in netCDF file (optional)
Expand Down
Binary file modified test/test_0.nc
Binary file not shown.
Binary file modified test/test_1.nc
Binary file not shown.
Binary file modified test/test_2.nc
Binary file not shown.

0 comments on commit 369fec2

Please sign in to comment.