tVec.cc - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
HTML git clone git://src.adamsgaard.dk/pism
DIR Log
DIR Files
DIR Refs
DIR LICENSE
---
tVec.cc (3641B)
---
1 /* Copyright (C) 2015, 2016, 2017 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 "Vec.hh"
21 #include "pism/util/error_handling.hh"
22
23 namespace pism {
24 namespace petsc {
25
26 // Wrapper around Vec (calls VecDestroy)
27
28 Vec::Vec() {
29 m_value = NULL;
30 }
31
32 Vec::Vec(::Vec v) {
33 m_value = v;
34 }
35
36 Vec::~Vec() {
37 if (m_value != NULL) {
38 PetscErrorCode ierr = VecDestroy(&m_value); CHKERRCONTINUE(ierr);
39 }
40 }
41
42 // Wrapper around VecGetArray / VecRestoreArray
43
44 VecArray::VecArray(::Vec v)
45 : m_v(v), m_array(NULL) {
46 PetscErrorCode ierr = VecGetArray(m_v, &m_array);
47 PISM_CHK(ierr, "VecGetArray");
48 }
49
50 VecArray::~VecArray() {
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
72 VecArray2D::~VecArray2D() {
73 PetscErrorCode ierr = VecRestoreArray2d(m_v, m_My, m_Mx, 0, 0, &m_array); CHKERRCONTINUE(ierr);
74 }
75
76 // Wrapper around DMDAVecGetArray / DMDAVecRestoreArray
77
78 DMDAVecArray::DMDAVecArray(DM::Ptr dm, ::Vec v)
79 : m_dm(dm), m_v(v) {
80 PetscErrorCode ierr = DMDAVecGetArray(*m_dm, m_v, &m_array);
81 PISM_CHK(ierr, "DMDAVecGetArray");
82 }
83
84 DMDAVecArray::~DMDAVecArray() {
85 PetscErrorCode ierr = DMDAVecRestoreArray(*m_dm, m_v, &m_array); CHKERRCONTINUE(ierr);
86 }
87
88 void* DMDAVecArray::get() {
89 return m_array;
90 }
91
92 // Wrapper around DMDAVecGetArrayDOF / DMDAVecRestoreArrayDOF
93
94 DMDAVecArrayDOF::DMDAVecArrayDOF(DM::Ptr dm, ::Vec v)
95 : m_dm(dm), m_v(v) {
96 PetscErrorCode ierr = DMDAVecGetArrayDOF(*m_dm, m_v, &m_array);
97 PISM_CHK(ierr, "DMDAVecGetArrayDOF");
98 }
99
100 DMDAVecArrayDOF::~DMDAVecArrayDOF() {
101 PetscErrorCode ierr = DMDAVecRestoreArrayDOF(*m_dm, m_v, &m_array); CHKERRCONTINUE(ierr);
102 }
103
104 void* DMDAVecArrayDOF::get() {
105 return m_array;
106 }
107
108 // Wrapper around DMGetGlobalVector / DMRestoreGlobalVector
109
110 TemporaryGlobalVec::TemporaryGlobalVec(DM::Ptr dm) {
111 m_dm = dm;
112 PetscErrorCode ierr = DMGetGlobalVector(*m_dm, &m_value);
113 PISM_CHK(ierr, "DMGetGlobalVector");
114 }
115
116 TemporaryGlobalVec::~TemporaryGlobalVec() {
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