PISM, A Parallel Ice Sheet Model  stable v2.1.1 committed by Constantine Khrulev on 2024-12-04 13:36:58 -0900
Vec.cc
Go to the documentation of this file.
1 /* Copyright (C) 2015, 2016, 2017, 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/util/petscwrappers/Vec.hh"
21 #include "pism/util/error_handling.hh"
22 
23 namespace pism {
24 namespace petsc {
25 
26 // Wrapper around Vec (calls VecDestroy)
27 
29  m_value = NULL;
30 }
31 
32 Vec::Vec(::Vec v) {
33  m_value = v;
34 }
35 
37  if (m_value != NULL) {
38  PetscErrorCode ierr = VecDestroy(&m_value); CHKERRCONTINUE(ierr);
39  }
40 }
41 
42 // Wrapper around VecGetArray / VecRestoreArray
43 
45  : m_v(v), m_array(NULL) {
46  PetscErrorCode ierr = VecGetArray(m_v, &m_array);
47  PISM_CHK(ierr, "VecGetArray");
48 }
49 
51  PetscErrorCode ierr = VecRestoreArray(m_v, &m_array); CHKERRCONTINUE(ierr);
52 }
53 
54 double* VecArray::get() {
55  return m_array;
56 }
57 
58 // Wrapper around VecGetArray2d / VecRestoreArray2d
59 
60 VecArray2D::VecArray2D(::Vec vec, int Mx, int My)
61  : m_Mx(Mx), m_My(My), m_i_offset(0), m_j_offset(0), m_v(vec) {
62  PetscErrorCode ierr = VecGetArray2d(m_v, m_My, m_Mx, 0, 0, &m_array);
63  PISM_CHK(ierr, "VecGetArray2d");
64 }
65 
66 VecArray2D::VecArray2D(::Vec vec, int Mx, int My, int i0, int j0)
67  : m_Mx(Mx), m_My(My), m_i_offset(i0), m_j_offset(j0), m_v(vec) {
68  PetscErrorCode ierr = VecGetArray2d(m_v, m_My, m_Mx, 0, 0, &m_array);
69  PISM_CHK(ierr, "VecGetArray2d");
70 }
71 
73  PetscErrorCode ierr = VecRestoreArray2d(m_v, m_My, m_Mx, 0, 0, &m_array); CHKERRCONTINUE(ierr);
74 }
75 
76 // Wrapper around DMDAVecGetArray / DMDAVecRestoreArray
77 
79  : m_dm(dm), m_v(v) {
80  PetscErrorCode ierr = DMDAVecGetArray(*m_dm, m_v, &m_array);
81  PISM_CHK(ierr, "DMDAVecGetArray");
82 }
83 
85  PetscErrorCode ierr = DMDAVecRestoreArray(*m_dm, m_v, &m_array); CHKERRCONTINUE(ierr);
86 }
87 
89  return m_array;
90 }
91 
92 // Wrapper around DMDAVecGetArrayDOF / DMDAVecRestoreArrayDOF
93 
95  : m_dm(dm), m_v(v) {
96  PetscErrorCode ierr = DMDAVecGetArrayDOF(*m_dm, m_v, &m_array);
97  PISM_CHK(ierr, "DMDAVecGetArrayDOF");
98 }
99 
101  PetscErrorCode ierr = DMDAVecRestoreArrayDOF(*m_dm, m_v, &m_array); CHKERRCONTINUE(ierr);
102 }
103 
105  return m_array;
106 }
107 
108 // Wrapper around DMGetGlobalVector / DMRestoreGlobalVector
109 
111  m_dm = dm;
112  PetscErrorCode ierr = DMGetGlobalVector(*m_dm, &m_value);
113  PISM_CHK(ierr, "DMGetGlobalVector");
114 }
115 
117  // This takes advantage of the fact that the destructor of a derived
118  // class is called before the destructor of its base class, so we
119  // can set m_value to NULL and turn the destructor of the base class
120  // (Vec) into a no-op.
121  if (m_value != NULL) {
122  PetscErrorCode ierr = DMRestoreGlobalVector(*m_dm, &m_value); CHKERRCONTINUE(ierr);
123  m_value = NULL;
124  }
125 }
126 
127 
128 } // end of namespace petsc
129 } // end of namespace pism
std::shared_ptr< Wrapper > Ptr
Definition: Wrapper.hh:30
DMDAVecArrayDOF(DM::Ptr dm, ::Vec v)
Definition: Vec.cc:94
DMDAVecArray(DM::Ptr dm, ::Vec v)
Definition: Vec.cc:78
TemporaryGlobalVec(DM::Ptr dm)
Definition: Vec.cc:110
double ** m_array
Definition: Vec.hh:67
VecArray2D(::Vec vec, int my_Mx, int my_My)
Definition: Vec.cc:60
VecArray(::Vec v)
Definition: Vec.cc:44
double * m_array
Definition: Vec.hh:51
double * get()
Definition: Vec.cc:54
#define PISM_CHK(errcode, name)