|
SPI
SLEPc PETSc Interface is used for rapid development and intuitive matrix operations similar to MatLab or NumPy style of operations
|
Go to the documentation of this file.
23 const std::string _name
32 ierr = VecCreate(PETSC_COMM_WORLD,&
vec);CHKERRXX(
ierr);
33 ierr = VecSetSizes(
vec,PETSC_DECIDE,_rows);CHKERRXX(
ierr);
46 const std::string _name
52 ierr = VecCreate(PETSC_COMM_WORLD,&
vec);CHKERRQ(
ierr);
53 ierr = VecSetSizes(
vec,PETSC_DECIDE,_rows);CHKERRQ(
ierr);
69 VecGetOwnershipRange(
vec,&low,&high);
70 if ((low <= _row) && (_row < high)){
71 ierr = VecSetValue(
vec,_row,v,INSERT_VALUES);CHKERRQ(
ierr);
88 VecGetOwnershipRange(
vec,&low,&high);
89 if ((low <= _row) && (_row < high)){
90 ierr = VecSetValue(
vec,_row,v,ADD_VALUES);CHKERRQ(
ierr);
109 PetscScalar v,v_global=0.;
111 PetscErrorCode ierr2;
112 ierr2 = VecGetOwnershipRange(
vec,&low, &high);CHKERRXX(ierr2);
113 if ((low<=_row) && (_row<high)){
114 ierr2 = VecGetValues(
vec,1,&_row,&v);CHKERRXX(ierr2);
117 MPIU_Allreduce(&v,&v_global,1,MPIU_SCALAR,MPIU_SUM,PETSC_COMM_WORLD);
129 PetscScalar v,v_global=0.;
131 ierr = VecGetOwnershipRange(
vec,&low, &high);CHKERRXX(
ierr);
132 if ((low<=_row) && (_row<high)){
133 ierr = VecGetValues(
vec,1,&_row,&v);CHKERRXX(
ierr);
136 MPIU_Allreduce(&v,&v_global,1,MPIU_SCALAR,MPIU_SUM,PETSC_COMM_WORLD);
150 VecGetOwnershipRange(
vec,&low,&high);
151 if ((low <= _row) && (_row < high)){
152 ierr = VecSetValue(
vec,_row,v,INSERT_VALUES);CHKERRQ(
ierr);
159 ierr = (*this)(_row,(PetscScalar)(v+0.0*PETSC_i));CHKERRQ(
ierr);
164 ierr = (*this)(_row,(PetscScalar)((
double)v+0.0*PETSC_i));CHKERRQ(
ierr);
200 ierr = VecSetType(A.
vec,VECMPICUDA);CHKERRXX(
ierr);
259 ierr = VecSetType(A.
vec,VECMPICUDA);CHKERRXX(
ierr);
299 ierr = VecScale(this->
vec,(PetscScalar)(a+0.*PETSC_i));CHKERRXX(
ierr);
366 return pow(*
this,(PetscScalar)p);
372 return pow(*
this,(PetscScalar)p);
384 if(X.
rows==this->rows){
428 PetscScalar maxscalar;
430 maxscalar = (*this)(
argmax,PETSC_TRUE);
472 printf(
"\n---------------- "+this->
name+
"---start------");
476 ierr = VecView(
vec,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(
ierr);
478 printf(
"---------------- "+this->
name+
"---done-------\n");
545 const std::string filename
548 ierr = PetscObjectSetName((PetscObject)A.
vec, A.
name.c_str());CHKERRQ(ierr);
550 std::ifstream f(filename.c_str());
552 ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD, filename.c_str(), FILE_MODE_APPEND, &viewer); CHKERRQ(ierr);
555 ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD, filename.c_str(), FILE_MODE_WRITE, &viewer); CHKERRQ(ierr);
557 ierr = VecView(A.
vec,viewer); CHKERRQ(ierr);
558 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
563 std::vector<PetscScalar> A,
564 std::string variablename,
568 SPIVec Avec(n,variablename);
569 for(PetscInt i=0; i<n; i++){
577 std::vector<SPIVec> A,
578 std::string variablename,
583 for(PetscInt i=0; i<n; i++){
584 A[i].name = variablename+sep+std::to_string(i);
593 const std::string filename
595 A.
ierr = PetscObjectSetName((PetscObject)A.
vec, A.
name.c_str());CHKERRQ(A.
ierr);
598 A.
ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD, filename.c_str(), FILE_MODE_READ, &viewer); CHKERRQ(A.
ierr);
600 A.
ierr = PetscViewerDestroy(&viewer); CHKERRQ(A.
ierr);
646 const PetscScalar begin,
647 const PetscScalar end,
653 PetscScalar step = (end-begin)/((PetscScalar)(_rows-1));
654 PetscScalar value=begin;
656 for (PetscInt i=0; i<_rows; ++i){
668 const PetscScalar begin,
669 const PetscScalar end,
670 const PetscScalar stepsize
672 PetscInt _rows=ceil(PetscRealPart((end-begin)/stepsize));
675 PetscScalar value=begin;
677 for (PetscInt i=0; i<_rows; ++i){
685 const PetscScalar end
687 PetscScalar begin=0.;
698 for (PetscInt i=0; i<out.
rows; ++i){
745 for (PetscInt i=0; i<out.
rows; ++i){
746 out(i,
std::erf((
double)(PetscRealPart(out(i)))));
754 for (PetscInt i=0; i<out.
rows; ++i){
755 out(i,
std::erfc((
double)(PetscRealPart(out(i)))));
763 T (*f)(T
const&, T
const&),
768 for (PetscInt i=0; i<out.
rows; ++i){
769 out(i,(*f)(out(i),B(i)));
792 PetscScalar innerproduct;
819 PetscScalar value=0.0,d0=0.0;
820 for(PetscInt i=1; i<=N+1; ++i){
822 value=0.5*(2.0*a(i-1,PETSC_TRUE)-a(i+1,PETSC_TRUE));
825 value = 0.5/((PetscScalar)i)*(2.0*a(i-1,PETSC_TRUE));
828 value = 0.5/((PetscScalar)i)*(1.0*a(i-1,PETSC_TRUE));
831 value = 0.5/((PetscScalar)i)*(1.0*a(i-1,PETSC_TRUE) - a(i+1,PETSC_TRUE));
850 PetscScalar ai=y(0,PETSC_TRUE);
851 PetscScalar bi=y(y.
rows-1,PETSC_TRUE);
852 PetscScalar mul = (bi-ai)/2.0;
865 VecNorm((x1-x2).vec,type,&error);
876 VecNorm(x1.
vec,type,&error);
886 for (PetscInt i=0; i<x.
rows-1; ++i){
887 x0(i,x(i,PETSC_TRUE));
888 x1(i,x(i+1,PETSC_TRUE));
903 for (PetscInt i=0; i<y.
rows-1; ++i){
904 y0(i,y(i,PETSC_TRUE));
905 y1(i,y(i+1,PETSC_TRUE));
911 return sum((y1+y0)/2.);
921 for (PetscInt i=0; i<y.
rows-1; ++i){
922 y0(i,y(i,PETSC_TRUE));
923 y1(i,y(i+1,PETSC_TRUE));
929 return sum((y1+y0)/2. *
diff(x));
937 ierr = PetscViewerDrawOpen(PETSC_COMM_WORLD,NULL,x.
name.c_str(),PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,&viewer);CHKERRQ(ierr);
938 ierr = VecView(x.
vec,viewer);CHKERRQ(ierr);
939 SPI::printf(
" draw(SPIVec) with title=%s, hit Enter to continue",x.
name.c_str());
942 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
SPIVec(std::string _name="SPIVec")
constructor with no arguments (no initialization)
SPIMat sin(const SPIMat &A)
take the sin of each element in a matrix
SPIVec real(const SPIVec &A)
return the real part of the vector
SPIVec atanh(const SPIVec &A)
take the atanh of each element in a vector
SPIVec & real()
take the real part of the vector
SPIVec operator-(const PetscScalar a, const SPIVec &A)
SPIVec erf(const SPIVec &A)
take the erf of each element in a vector
PetscBool operator==(const SPIVec &x2)
== VecEqual test if this==x2
SPIVec acosh(const SPIVec &A)
take the acosh of each element in a vector
SPIVec arange(const PetscScalar begin, const PetscScalar end, const PetscScalar stepsize=1)
return a range of number of equally spaced points between begin and end by step size step
SPIVec & operator=(const SPIVec &X)
PetscInt printf(std::string msg,...)
PetscBool flag_init
flag if it has been initialized
PetscScalar sum(SPIVec x)
take the sum of a vector
SPIVec operator*(const PetscScalar a)
SPIVec conj(const SPIVec &A)
SPIVec & operator*=(const PetscScalar a)
SPIVec ones(const PetscInt rows)
PetscInt save(const SPIMat &A, const std::string filename)
save matrix to filename in binary format (see Petsc documentation for format ) Format is (from Petsc ...
SPIVec cosh(const SPIVec &A)
take the cosh of each element in a vector
PetscReal L2(SPIVec x1, const SPIVec x2, NormType type=NORM_2)
calculate the norm of the difference between and vectors.
SPIVec diff(SPIVec x1)
diff of the vector (see numpy.diff)
SPIVec operator^(const PetscScalar p)
pow operation pow(this,p)
SPIVec & operator/=(const PetscScalar a)
SPIVec log(const SPIVec &A)
take the log (natural log) of each element in a vector
SPIVec asinh(const SPIVec &A)
take the asinh of each element in a vector
SPIVec linspace(const PetscScalar begin, const PetscScalar end, const PetscInt rows)
return linspace of number of rows equally spaced points between begin and end
SPIMat _Function_on_each_element(T(*f)(T const &), const SPIMat &A)
take the function of each element in a matrix, e.g. (*f)(A(i,j)) for each i,j
std::string name
Vec name (important for hdf5 i/o)
SPIVec log10(const SPIVec &A)
take the log10 of each element in a vector
PetscInt draw(const SPIMat &A)
draw nonzero structure of matrix
SPIVec imag(const SPIVec &A)
return the imaginary part of the vector
PetscScalar dot(SPIVec x, SPIVec y)
take the inner product of the two vectors (i.e. y^H x) where ^H is the complex conjugate transpose
SPIVec exp(const SPIVec &A)
take the exp of each element in a vector
SPIMat tan(const SPIMat &A)
take the tan of each element in a matrix
PetscInt set(const PetscInt _row, const PetscScalar v)
PetscScalar trapz(const SPIVec y)
trapezoidal integration of y with unity coordinate spacing,
SPIVec operator+(const SPIVec &X)
SPIMat abs(const SPIMat &A)
take the abs of each element in a matrix
SPIVec & operator-=(const SPIVec &X)
PetscScalar integrate_coeffs(const SPIVec &a)
integrate a vector of chebyshev Coefficients on a grid
PetscErrorCode ierr
ierr for various routines and operators
SPIVec operator/(const PetscScalar a)
SPIVec & axpy(const PetscScalar a, const SPIVec &X)
SPIVec atan(const SPIVec &A)
take the atan of each element in a vector
SPIVec sqrt(const SPIVec &A)
take the atanh of each element in a vector
SPIVec erfc(const SPIVec &A)
take the erfc(z) = 1-erf(z) of each element in a vector
PetscInt load(SPIMat &A, const std::string filename)
load matrix from filename from binary format (works with save(SPIMat,std::string) function
SPIVec asin(const SPIVec &A)
take the asin of each element in a vector
SPIVec pow(const SPIVec &A, SPIVec &B)
take the pow of each element in the vectors
SPIVec & operator+=(const SPIVec &X)
SPIVec tanh(const SPIVec &A)
take the tanh of each element in a vector
SPIVec sinh(const SPIVec &A)
take the sinh of each element in a vector
SPIMat acos(const SPIMat &A)
take the arccos of each element in a matrix
PetscInt Init(const PetscInt _rows, const std::string name="SPIVec")
initialize the vector of size _rows
PetscInt rows
number of rows in vec
PetscScalar dot(SPIVec y)
take the inner product of two vectors
SPIMat operator*(const PetscScalar a, const SPIMat A)
a*A operation to be equivalent to A*a
SPIVec & imag()
take the imaginary part of the vector
SPIVec operator+(const PetscScalar a, const SPIVec &A)
SPIMat cos(const SPIMat &A)
take the cos of each element in a matrix
PetscInt add(PetscInt _row, const PetscScalar v)
SPIVec operator/(const SPIVec &b, const SPIMat &A)
Solve linear system, Ax=b using b/A notation.
SPIMat zeros(const PetscInt m, const PetscInt n)
create, form, and return zeros matrix of size mxn