Chi-Tech
petsc_utils_04_vecmanip.cc
Go to the documentation of this file.
1#include "petsc_utils.h"
2
4
5#include "chi_log.h"
6
7// ###################################################################
8/**Copies a PETSc vector to a STL vector. Only the local portion is
9 * copied.*/
11 std::vector<double>& data,
12 size_t N,
13 bool resize_STL /*=true*/)
14{
15 if (resize_STL)
16 {
17 data.clear();
18 data.assign(N, 0.0);
19 }
20 else
21 ChiLogicalErrorIf(data.size() < N,
22 "data.size() < N, " + std::to_string(data.size()) +
23 " < " + std::to_string(N));
24
25 const double* x_ref;
26 VecGetArrayRead(x, &x_ref);
27
28 std::copy(x_ref, x_ref + N, data.begin());
29
30 VecRestoreArrayRead(x, &x_ref);
31}
32
33// ###################################################################
34/**Copies a PETSc vector to a STL vector. Only the local portion is
35 * copied.*/
37 Vec x, std::vector<double>& data, size_t N, bool resize_STL /*=true*/)
38{
39 if (resize_STL)
40 {
41 data.clear();
42 data.assign(N, 0.0);
43 }
44 else
45 ChiLogicalErrorIf(data.size() != N,
46 "data.size() != N, " + std::to_string(data.size()) +
47 " < " + std::to_string(N));
48
49 auto info = GetGhostVectorLocalViewRead(x);
50 const double* x_ref = info.x_localized_raw;
51
52 std::copy(x_ref, x_ref + N, data.begin());
53
55}
56
57void chi_math::PETScUtils::CopySTLvectorToVec(const std::vector<double>& data,
58 Vec x,
59 size_t N)
60{
61 double* x_ref;
62 VecGetArray(x, &x_ref);
63
64 std::copy(data.begin(), data.end(), x_ref);
65
66 VecRestoreArray(x, &x_ref);
67}
68
70 Vec x)
71{
72 const double* y_data = y.Data();
73 double* x_data;
74 VecGetArray(x, &x_data);
75 std::copy(y_data, y_data + y.LocalSize(), x_data);
76 VecRestoreArray(x, &x_data);
77}
78
79// ###################################################################
80/**Copies global values from a PETSc vector to a STL vector.*/
82 Vec x, const std::vector<int64_t>& global_indices, std::vector<double>& data)
83{
84 //=================================== Populating local indices
85 size_t N = global_indices.size();
86 std::vector<int64_t> local_indices(N, 0);
87 size_t counter = 0;
88 for (auto val : global_indices)
89 {
90 local_indices[counter] = counter;
91 ++counter;
92 }
93
94 //=================================== Creating PETSc vector
95 Vec local_vec;
96 VecCreateSeq(PETSC_COMM_SELF, global_indices.size() + 1, &local_vec);
97 VecSet(local_vec, 0.0);
98
99 //=================================== Create and transfer index sets
100 IS global_set;
101 IS local_set;
102 ISCreateGeneral(
103 PETSC_COMM_SELF, N, global_indices.data(), PETSC_COPY_VALUES, &global_set);
104 ISCreateGeneral(
105 PETSC_COMM_SELF, N, local_indices.data(), PETSC_COPY_VALUES, &local_set);
106 VecScatter scat;
107 VecScatterCreate(x, global_set, local_vec, local_set, &scat);
108 VecScatterBegin(scat, x, local_vec, INSERT_VALUES, SCATTER_FORWARD);
109 VecScatterEnd(scat, x, local_vec, INSERT_VALUES, SCATTER_FORWARD);
110
111 //=================================== Copy to STL
112 data.clear();
113 data.resize(N, 0.0);
114 const double* x_ref;
115 VecGetArrayRead(local_vec, &x_ref);
116
117 std::copy(x_ref, x_ref + N, data.begin());
118
119 VecRestoreArrayRead(x, &x_ref);
120
121 //=================================== Cleanup
122 ISDestroy(&global_set);
123 ISDestroy(&local_set);
124
125 VecDestroy(&local_vec);
126}
127
128// ###################################################################
129/**Communicates ghost entries of a ghost vector. This operation
130 * is suitable when only a single vector is communicated. When
131 * more than vector is communicated it would be more efficient
132 * to "Begin" all the vectors followed by and "End" of each
133 * vector.*/
135{
136 VecGhostUpdateBegin(x, INSERT_VALUES, SCATTER_FORWARD);
137 VecGhostUpdateEnd(x, INSERT_VALUES, SCATTER_FORWARD);
138}
139
140// ###################################################################
141/**Gets a local raw view of a ghost vector.*/
144{
145 Vec x_localized;
146 VecGhostGetLocalForm(x, &x_localized);
147 const double* x_localized_raw;
148
149 VecGetArrayRead(x_localized, &x_localized_raw);
150
151 GhostVecLocalRaw local_data;
152 local_data.x_localized = x_localized;
153 local_data.x_localized_raw = (double*)x_localized_raw;
154
155 return local_data;
156}
157
158// ###################################################################
159/**Gets a local raw view of a ghost vector.*/
161 Vec x, GhostVecLocalRaw& local_data)
162{
163 VecRestoreArrayRead(local_data.x_localized,
164 (const double**)&local_data.x_localized_raw);
165 VecGhostRestoreLocalForm(x, &local_data.x_localized);
166}
#define ChiLogicalErrorIf(condition, message)
virtual double * Data()=0
uint64_t LocalSize() const
Return the size of the locally owned portion of the parallel vector.
void RestoreGhostVectorLocalViewRead(Vec x, GhostVecLocalRaw &local_data)
GhostVecLocalRaw GetGhostVectorLocalViewRead(Vec x)
void CopyVecToSTLvectorWithGhosts(Vec x, std::vector< double > &data, size_t N, bool resize_STL=true)
void CopyGlobalVecToSTLvector(Vec x, const std::vector< int64_t > &global_indices, std::vector< double > &data)
void CopyParallelVectorToVec(const ParallelVector &y, Vec x)
void CopyVecToSTLvector(Vec x, std::vector< double > &data, size_t N, bool resize_STL=true)
void CopySTLvectorToVec(const std::vector< double > &data, Vec x, size_t N)
struct _p_Vec * Vec