Ice Sheet System Model  4.18
Code documentation
PetscVec.cpp
Go to the documentation of this file.
1 
5 /*Headers:*/
6 /*{{{*/
7 #ifdef HAVE_CONFIG_H
8  #include <config.h>
9 #else
10 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
11 #endif
12 
13 #include <stdio.h>
14 #include <string.h>
15 #include "../petscincludes.h"
16 #include "../../../shared/shared.h"
17 
18 /*}}}*/
19 
20 /*PetscVec constructors and destructor*/
22  this->vector=NULL;
23  #ifdef _HAVE_AD_
24  this->avector=NULL;
25  #endif
26 }
27 /*}}}*/
28 PetscVec::PetscVec(int M,bool fromlocalsize){/*{{{*/
29 
30  this->vector=NewVec(M,IssmComm::GetComm(),fromlocalsize);
31 
32 }
33 /*}}}*/
34 PetscVec::PetscVec(int m,int M){/*{{{*/
35 
36  VecCreate(IssmComm::GetComm(),&this->vector);
37  VecSetSizes(this->vector,m,M);
38  VecSetFromOptions(this->vector);
39  VecSet(this->vector,0.0);
40 
41 }
42 /*}}}*/
43 PetscVec::PetscVec(Vec petsc_vec){/*{{{*/
44 
45  if(petsc_vec==NULL){
46  this->vector=NewVec(0,IssmComm::GetComm());
47  }
48  else{
49  /*copy vector*/
50  VecDuplicate(petsc_vec,&this->vector);
51  VecCopy(petsc_vec,this->vector);
52  }
53 
54 }
55 /*}}}*/
56 PetscVec::PetscVec(IssmDouble* serial_vec,int M){/*{{{*/
57 
58  int* idxm=NULL;
59  if(M)idxm=xNew<int>(M);
60  for(int i=0;i<M;i++) idxm[i]=i;
61 
62  this->vector=NewVec(M,IssmComm::GetComm());
63  VecSetValues(this->vector,M,idxm,serial_vec,INSERT_VALUES);
64  VecAssemblyBegin(this->vector);
65  VecAssemblyEnd(this->vector);
66 
67  xDelete<int>(idxm);
68 }
69 /*}}}*/
71  VecFree(&this->vector);
72 }
73 /*}}}*/
74 
75 /*PetscVec specific routines: */
76 void PetscVec::Echo(void){/*{{{*/
77 
78  _assert_(this->vector);
79  VecView(this->vector,PETSC_VIEWER_STDOUT_WORLD);
80 }
81 /*}}}*/
82 void PetscVec::Assemble(void){/*{{{*/
83 
84  _assert_(this->vector);
85  VecAssemblyBegin(this->vector);
86  VecAssemblyEnd(this->vector);
87 
88 }
89 /*}}}*/
90 void PetscVec::SetValues(int ssize, int* list, IssmDouble* values, InsMode mode){/*{{{*/
91 
92  _assert_(this->vector);
93  VecSetValues(this->vector,ssize,list,values,ISSMToPetscInsertMode(mode));
94 
95 }
96 /*}}}*/
97 void PetscVec::SetValue(int dof, IssmDouble value, InsMode mode){/*{{{*/
98 
99  _assert_(this->vector);
100  VecSetValues(this->vector,1,&dof,&value,ISSMToPetscInsertMode(mode));
101 
102 }
103 /*}}}*/
104 void PetscVec::GetValue(IssmDouble* pvalue,int dof){/*{{{*/
105 
106  _assert_(this->vector);
107  VecGetValues(this->vector,1,&dof,pvalue);
108 
109 }
110 /*}}}*/
111 void PetscVec::GetSize(int* pM){/*{{{*/
112 
113  _assert_(this->vector);
114  VecGetSize(this->vector,pM);
115 }
116 /*}}}*/
117 void PetscVec::GetLocalSize(int* pm){/*{{{*/
118 
119  _assert_(this->vector);
120  VecGetLocalSize(this->vector,pm);
121 }
122 /*}}}*/
123 void PetscVec::GetLocalVector(IssmDouble** pvector,int** pindices){/*{{{*/
124 
125  _assert_(this->vector);
126 
127  /*First, check that vector size is not 0*/
128  int vector_size;
129  this->GetSize(&vector_size);
130  if(vector_size==0){
131  *pvector=NULL;
132  *pindices=NULL;
133  return;
134  }
135 
136  /*Get Ownership range*/
137  PetscInt lower_row,upper_row;
138  VecGetOwnershipRange(this->vector,&lower_row,&upper_row);
139  int range=upper_row-lower_row;
140 
141  /*return NULL if no range*/
142  if(range==0){
143  *pvector=NULL;
144  *pindices=NULL;
145  return;
146  }
147 
148  /*Build indices*/
149  int* indices=xNew<int>(range);
150  for(int i=0;i<range;i++) indices[i]=lower_row+i;
151  /*Get vector*/
152  IssmDouble* values =xNew<IssmDouble>(range);
153  VecGetValues(this->vector,range,indices,values);
154 
155  *pvector = values;
156  *pindices = indices;
157 } /*}}}*/
159 
160  _assert_(this->vector);
161  Vec vec_output=NULL;
162  VecDuplicate(this->vector,&vec_output);
163  PetscVec* output=new PetscVec(vec_output);
164  VecFree(&vec_output);
165 
166  return output;
167 }
168 /*}}}*/
169 void PetscVec::Set(IssmDouble value){/*{{{*/
170 
171  _assert_(this->vector);
172  VecSet(this->vector,value);
173 
174 }
175 /*}}}*/
176 void PetscVec::AXPY(PetscVec* X, IssmDouble a){/*{{{*/
177 
178  _assert_(this->vector);
179  VecAXPY(this->vector,a,X->vector);
180 
181 }
182 /*}}}*/
183 void PetscVec::AYPX(PetscVec* X, IssmDouble a){/*{{{*/
184 
185  _assert_(this->vector);
186  VecAYPX(this->vector,a,X->vector);
187 
188 }
189 /*}}}*/
191 
192  IssmDouble* vec_serial=NULL;
193  VecToMPISerial(&vec_serial, this->vector,IssmComm::GetComm(),true);
194  return vec_serial;
195 
196 }
197 /*}}}*/
199 
200  IssmDouble* vec_serial=NULL;
201  VecToMPISerial(&vec_serial, this->vector,IssmComm::GetComm(),false);
202  return vec_serial;
203 
204 }
205 /*}}}*/
206 void PetscVec::Shift(IssmDouble shift){/*{{{*/
207 
208  if(this->vector) VecShift(this->vector,shift);
209 
210 }
211 /*}}}*/
212 void PetscVec::Copy(PetscVec* to){/*{{{*/
213 
214  if(this->vector) VecCopy(this->vector,to->vector);
215 
216 }
217 /*}}}*/
219 
220  _assert_(this->vector);
221 
222  IssmDouble max;
223  VecMax(this->vector,NULL,&max);
224  return max;
225 
226 }
227 /*}}}*/
229 
230  IssmDouble norm=0;
231  _assert_(this->vector);
232  VecNorm(this->vector,ISSMToPetscNormMode(mode),&norm);
233  return norm;
234 
235 }
236 /*}}}*/
237 void PetscVec::Scale(IssmDouble scale_factor){/*{{{*/
238 
239  _assert_(this->vector);
240  VecScale(this->vector,scale_factor);
241 
242 }
243 /*}}}*/
244 void PetscVec::Pow(IssmDouble scale_factor){/*{{{*/
245 
246  _assert_(this->vector);
247  VecPow(this->vector,scale_factor);
248 
249 }
250 /*}}}*/
252 
253  IssmDouble dot;
254  _assert_(this->vector);
255  VecDot(this->vector,input->vector,&dot);
256  return dot;
257 
258 }
259 /*}}}*/
261 
262  _assert_(this->vector);
263  VecPointwiseDivide(this->vector,x->vector,y->vector);
264 
265 }
266 /*}}}*/
268 
269  _assert_(this->vector);
270  VecPointwiseMult(this->vector,x->vector,y->vector);
271 
272 }
273 /*}}}*/
PetscVec::AYPX
void AYPX(PetscVec *X, IssmDouble a)
Definition: PetscVec.cpp:183
PetscVec::SetValues
void SetValues(int ssize, int *list, IssmDouble *values, InsMode mode)
Definition: PetscVec.cpp:90
_assert_
#define _assert_(ignore)
Definition: exceptions.h:37
IssmDouble
double IssmDouble
Definition: types.h:37
PetscVec::Max
IssmDouble Max(void)
Definition: PetscVec.cpp:218
PetscVec::PetscVec
PetscVec()
Definition: PetscVec.cpp:21
IssmComm::GetComm
static ISSM_MPI_Comm GetComm(void)
Definition: IssmComm.cpp:30
PetscVec::GetLocalSize
void GetLocalSize(int *pM)
Definition: PetscVec.cpp:117
PetscVec::PointwiseMult
void PointwiseMult(PetscVec *x, PetscVec *y)
Definition: PetscVec.cpp:267
PetscVec::GetValue
void GetValue(IssmDouble *pvalue, int dof)
Definition: PetscVec.cpp:104
PetscVec::Dot
IssmDouble Dot(PetscVec *vector)
Definition: PetscVec.cpp:251
PetscVec::vector
Vec vector
Definition: PetscVec.h:25
PetscVec::Scale
void Scale(IssmDouble scale_factor)
Definition: PetscVec.cpp:237
VecFree
void VecFree(Vec *pvec)
Definition: VecFree.cpp:16
PetscVec::Shift
void Shift(IssmDouble shift)
Definition: PetscVec.cpp:206
ISSMToPetscNormMode
NormType ISSMToPetscNormMode(NormMode mode)
Definition: ISSMToPetscNormMode.cpp:20
PetscVec::~PetscVec
~PetscVec()
Definition: PetscVec.cpp:70
NewVec
Vec NewVec(int size, ISSM_MPI_Comm comm, bool fromlocalsize)
Definition: NewVec.cpp:19
PetscVec::Assemble
void Assemble(void)
Definition: PetscVec.cpp:82
VecToMPISerial
int VecToMPISerial(double **pgathered_vector, Vec vector, ISSM_MPI_Comm comm, bool broadcast=true)
Definition: VecToMPISerial.cpp:14
PetscVec::ToMPISerial0
IssmDouble * ToMPISerial0(void)
Definition: PetscVec.cpp:198
PetscVec::GetSize
void GetSize(int *pM)
Definition: PetscVec.cpp:111
PetscVec::Duplicate
PetscVec * Duplicate(void)
Definition: PetscVec.cpp:158
PetscVec::GetLocalVector
void GetLocalVector(IssmDouble **pvector, int **pindices)
Definition: PetscVec.cpp:123
PetscVec::Norm
IssmDouble Norm(NormMode norm_type)
Definition: PetscVec.cpp:228
NormMode
NormMode
Definition: toolkitsenums.h:15
PetscVec::PointwiseDivide
void PointwiseDivide(PetscVec *x, PetscVec *y)
Definition: PetscVec.cpp:260
PetscVec::AXPY
void AXPY(PetscVec *X, IssmDouble a)
Definition: PetscVec.cpp:176
ISSMToPetscInsertMode
InsertMode ISSMToPetscInsertMode(InsMode mode)
Definition: ISSMToPetscInsertMode.cpp:20
PetscVec::Copy
void Copy(PetscVec *to)
Definition: PetscVec.cpp:212
PetscVec
Definition: PetscVec.h:22
InsMode
InsMode
Definition: toolkitsenums.h:14
PetscVec::Pow
void Pow(IssmDouble scale_factor)
Definition: PetscVec.cpp:244
PetscVec::Echo
void Echo(void)
Definition: PetscVec.cpp:76
PetscVec::ToMPISerial
IssmDouble * ToMPISerial(void)
Definition: PetscVec.cpp:190
PetscVec::Set
void Set(IssmDouble value)
Definition: PetscVec.cpp:169
max
IssmDouble max(IssmDouble a, IssmDouble b)
Definition: extrema.cpp:24
PetscVec::SetValue
void SetValue(int dof, IssmDouble value, InsMode mode)
Definition: PetscVec.cpp:97