PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
IcebergRemover.cc
Go to the documentation of this file.
1 /* Copyright (C) 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2023 PISM Authors
2  *
3  * This file is part of PISM.
4  *
5  * PISM is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 3 of the License, or (at your option) any later
8  * version.
9  *
10  * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with PISM; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include "pism/frontretreat/util/IcebergRemover.hh"
21 #include "pism/util/connected_components.hh"
22 #include "pism/util/Mask.hh"
23 #include "pism/util/error_handling.hh"
24 #include "pism/util/Grid.hh"
25 #include "pism/util/array/CellType.hh"
26 #include "pism/util/petscwrappers/Vec.hh"
27 
28 namespace pism {
29 namespace calving {
30 
31 IcebergRemover::IcebergRemover(std::shared_ptr<const Grid> g)
32  : Component(g),
33  m_iceberg_mask(m_grid, "iceberg_mask"){
34 
36 }
37 
38 /**
39  * Use PISM's ice cover mask to update ice thickness, removing "icebergs".
40  *
41  * @param[in,out] pism_mask PISM's ice cover mask
42  * @param[in,out] ice_thickness ice thickness
43  */
45  array::CellType1 &cell_type,
46  array::Scalar &ice_thickness) {
47  update_impl(bc_mask, cell_type, ice_thickness);
48 }
49 
51  array::CellType1 &cell_type,
52  array::Scalar &ice_thickness) {
53  const int
54  mask_grounded_ice = 1,
55  mask_floating_ice = 2;
56 
57  // prepare the mask that will be handed to the connected component
58  // labeling code:
59  {
60  m_iceberg_mask.set(0.0);
61 
62  array::AccessScope list{&cell_type, &m_iceberg_mask, &bc_mask};
63 
64  for (auto p = m_grid->points(); p; p.next()) {
65  const int i = p.i(), j = p.j();
66 
67  if (cell_type.grounded_ice(i,j)) {
68  m_iceberg_mask(i,j) = mask_grounded_ice;
69  } else if (cell_type.floating_ice(i,j)) {
70  m_iceberg_mask(i,j) = mask_floating_ice;
71  }
72  }
73 
74  // Mark icy Dirichlet B.C. cells as "grounded" because we don't want them removed.
75  for (auto p = m_grid->points(); p; p.next()) {
76  const int i = p.i(), j = p.j();
77 
78  if (bc_mask(i, j) > 0.5 and cell_type.icy(i, j)) {
79  m_iceberg_mask(i, j) = mask_grounded_ice;
80  }
81  }
82  }
83 
84  // identify icebergs using serial code on processor 0:
85  {
87 
88  ParallelSection rank0(m_grid->com);
89  try {
90  if (m_grid->rank() == 0) {
91  petsc::VecArray mask_p0(*m_mask_p0);
92  label_connected_components(mask_p0.get(), m_grid->My(), m_grid->Mx(), true, mask_grounded_ice);
93  }
94  } catch (...) {
95  rank0.failed();
96  }
97  rank0.check();
98 
100  }
101 
102  // correct ice thickness and the cell type mask using the resulting
103  // "iceberg" mask:
104  {
105  array::AccessScope list{&ice_thickness, &cell_type, &m_iceberg_mask, &bc_mask};
106 
107  for (auto p = m_grid->points(); p; p.next()) {
108  const int i = p.i(), j = p.j();
109 
110  if (m_iceberg_mask(i,j) > 0.5 && bc_mask(i,j) < 0.5) {
111  ice_thickness(i,j) = 0.0;
112  cell_type(i,j) = MASK_ICE_FREE_OCEAN;
113  }
114  }
115  }
116 
117  // update ghosts of the cell_type and the ice thickness (then surface
118  // elevation can be updated redundantly)
119  cell_type.update_ghosts();
120  ice_thickness.update_ghosts();
121 }
122 
123 } // end of namespace calving
124 } // end of namespace pism
const std::shared_ptr< const Grid > m_grid
grid used by this component
Definition: Component.hh:156
A class defining a common interface for most PISM sub-models.
Definition: Component.hh:118
void failed()
Indicates a failure of a parallel section.
Makes sure that we call begin_access() and end_access() for all accessed array::Arrays.
Definition: Array.hh:65
void get_from_proc0(petsc::Vec &onp0)
Gets a local Array2 from processor 0.
Definition: Array.cc:1095
void set(double c)
Result: v[j] <- c for all j.
Definition: Array.cc:707
std::shared_ptr< petsc::Vec > allocate_proc0_copy() const
Definition: Array.cc:963
void put_on_proc0(petsc::Vec &onp0) const
Puts a local array::Scalar on processor 0.
Definition: Array.cc:1052
void update_ghosts()
Updates ghost points.
Definition: Array.cc:693
bool floating_ice(int i, int j) const
Definition: CellType.hh:50
bool grounded_ice(int i, int j) const
Definition: CellType.hh:46
bool icy(int i, int j) const
Definition: CellType.hh:42
std::shared_ptr< petsc::Vec > m_mask_p0
virtual void update_impl(const array::Scalar &bc_mask, array::CellType1 &cell_type, array::Scalar &ice_thickness)
void update(const array::Scalar &bc_mask, array::CellType1 &cell_type, array::Scalar &ice_thickness)
IcebergRemover(std::shared_ptr< const Grid > g)
double * get()
Definition: Vec.cc:54
Wrapper around VecGetArray and VecRestoreArray.
Definition: Vec.hh:44
void label_connected_components(double *image, int nrows, int ncols, bool identify_icebergs, int mask_grounded, int first_label)
static const double g
Definition: exactTestP.cc:36
@ MASK_ICE_FREE_OCEAN
Definition: Mask.hh:35