36#include "PetscVecTools.hpp"
38#include <petscviewer.h>
40#include "DistributedVectorFactory.hpp"
41#include "DistributedVector.hpp"
42#include "PetscException.hpp"
50 VecAssemblyBegin(vector);
51 VecAssemblyEnd(vector);
59 if (row >= lo && row < hi)
61 VecSetValues(vector, 1, &row, &value, INSERT_VALUES);
70 if (row >= lo && row < hi)
72 VecSetValues(vector, 1, &row, &value, ADD_VALUES);
79#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 7)
80 PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_MATLAB);
82 PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_MATLAB);
84 VecView(vector, PETSC_VIEWER_STDOUT_WORLD);
85#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 7)
86 PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
92#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
93 PetscScalar zero = 0.0;
94 VecSet(&zero, vector);
96 VecZeroEntries(vector);
103 VecGetSize(vector, &size);
104 return (
unsigned) size;
109 VecGetOwnershipRange(vector, &lo, &hi);
116 assert(lo <= row && row < hi);
120 VecGetArray(vector, &p_vector);
121 double answer = p_vector[local_index];
122 VecRestoreArray(vector, &p_vector);
129#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
130 VecAXPY(&scaleFactor, x, y);
132 VecAXPY(y, scaleFactor, x);
138#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
139 PETSCEXCEPT( VecScale(&scaleFactor, vector) );
141 PETSCEXCEPT( VecScale(vector, scaleFactor) );
147#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
148 PETSCEXCEPT( VecWAXPY(&a, x, y, w) );
150 PETSCEXCEPT( VecWAXPY(w, a, x, y) );
158 VecGetSize(interleavedVec, &num_rows);
159 VecGetLocalSize(interleavedVec, &num_local_rows);
161 IS A11_rows, A22_rows;
162 ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 0, 2, &A11_rows);
163 ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 1, 2, &A22_rows);
166 ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 0, 1, &all_vector);
168 unsigned subvector_num_rows = num_rows/2;
169 unsigned subvector_local_rows = num_local_rows/2;
173 VecScatterCreate(interleavedVec, A11_rows, x1_subvector, all_vector, &rFirstVariableScatterContext);
174 VecScatterCreate(interleavedVec, A22_rows, x2_subvector, all_vector, &rSecondVariableScatterContext);
186 PetscScalar *p_interleaved_vec;
187 PetscScalar *p_1st_variable_vec;
188 PetscScalar *p_2nd_variable_vec;
194#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2)
196 VecGetArrayRead(interleavedVec, (
const PetscScalar**)&p_interleaved_vec);
198 VecGetArray(interleavedVec, &p_interleaved_vec);
200 VecGetArray(firstVariableVec, &p_1st_variable_vec);
201 VecGetArray(secondVariableVec, &p_2nd_variable_vec);
204 VecGetLocalSize(interleavedVec, &vec_local_size);
205 assert(vec_local_size%2 == 0);
207 for (
PetscInt local_index=0; local_index<vec_local_size/2; local_index++)
209 p_1st_variable_vec[local_index] = p_interleaved_vec[2*local_index];
210 p_2nd_variable_vec[local_index] = p_interleaved_vec[2*local_index+1];
213 VecRestoreArray(interleavedVec, &p_interleaved_vec);
214 VecRestoreArray(firstVariableVec, &p_1st_variable_vec);
215 VecRestoreArray(secondVariableVec, &p_2nd_variable_vec);
256 PetscScalar *p_interleaved_vec;
257 PetscScalar *p_1st_variable_vec;
258 PetscScalar *p_2nd_variable_vec;
260 VecGetArray(interleavedVec, &p_interleaved_vec);
261 VecGetArray(firstVariableVec, &p_1st_variable_vec);
262 VecGetArray(secondVariableVec, &p_2nd_variable_vec);
265 VecGetLocalSize(interleavedVec, &vec_local_size);
266 assert(vec_local_size%2 == 0);
268 for (
PetscInt local_index=0; local_index<vec_local_size/2; local_index++)
270 p_interleaved_vec[2*local_index] = p_1st_variable_vec[local_index];
271 p_interleaved_vec[2*local_index+1] = p_2nd_variable_vec[local_index];
274 VecRestoreArray(interleavedVec, &p_interleaved_vec);
275 VecRestoreArray(firstVariableVec, &p_1st_variable_vec);
276 VecRestoreArray(secondVariableVec, &p_2nd_variable_vec);