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