int main(int argc,char **args) { PetscErrorCode ierr; PetscViewer socketviewer; PetscInt rank,size; Vec x,y; Mat A; PetscScalar *a; int vsize,portnumber = 5050; //PetscViewer_Socket *vmatlab; PetscInitialize(&argc,&args,(char *)0,help); MPI_Comm_size(PETSC_COMM_WORLD,&size); MPI_Comm_rank(PETSC_COMM_WORLD,&rank); // Rank 0 connects to socket (use PETSC_DEFAULT, not necessary to be #5050) //PetscViewerSocketOpen(PETSC_COMM_WORLD,0,portnumber,&socketviewer); PetscViewerSocketOpen(PETSC_COMM_WORLD,0,PETSC_DEFAULT,&socketviewer); // Example for receiving and sending a Scalar // It becomes a VECSEQ with size one. VecReceive(socketviewer,PETSC_NULL,&x); //VecView(x,0); // We cannot send VECSEQ type vector, so use PetscRealView, and notice the a is a pointer. VecGetArray(x,&a); PetscRealView(1,a, socketviewer); // Example for receving and sending a SEQ Vector // If the size of the receiving vector is larger than 1, it becomes a VECMPI type vector. VecReceive(socketviewer,VECSEQ,&x); VecGetArray(x,&a); VecGetSize(x,&vsize); PetscRealView(vsize,a, socketviewer); // Example for receving and sending a MPI Vector VecReceive(socketviewer,VECMPI,&y); //VecView(y,0); // Use VecView to send a VECMPI type vector VecView(y, socketviewer); // Example for receving and sending a MPI Matrix MatReceiveTranspose(socketviewer,MATMPIAIJ,&A); //MatView(A,0); // MatView(A,socketviewer); ///////////////////////////////////////////////////////////////////////////////////////// ierr = VecDestroy(x); CHKERRQ(ierr); ierr = PetscFinalize(); CHKERRQ(ierr); return 0; }
/*@ PetscDTGaussQuadrature - create Gauss quadrature Not Collective Input Arguments: + npoints - number of points . a - left end of interval (often-1) - b - right end of interval (often +1) Output Arguments: + x - quadrature points - w - quadrature weights Level: intermediate References: Golub and Welsch, Calculation of Quadrature Rules, Math. Comp. 23(106), 221--230, 1969. .seealso: PetscDTLegendreEval() @*/ PetscErrorCode PetscDTGaussQuadrature(PetscInt npoints,PetscReal a,PetscReal b,PetscReal *x,PetscReal *w) { PetscErrorCode ierr; PetscInt i; PetscReal *work; PetscScalar *Z; PetscBLASInt N,LDZ,info; PetscFunctionBegin; /* Set up the Golub-Welsch system */ for (i=0; i<npoints; i++) { x[i] = 0; /* diagonal is 0 */ if (i) w[i-1] = 0.5 / PetscSqrtReal(1 - 1./PetscSqr(2*i)); } ierr = PetscRealView(npoints-1,w,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); ierr = PetscMalloc2(npoints*npoints,PetscScalar,&Z,PetscMax(1,2*npoints-2),PetscReal,&work);CHKERRQ(ierr); ierr = PetscBLASIntCast(npoints,&N);CHKERRQ(ierr); LDZ = N; ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr); PetscStackCall("LAPACKsteqr",LAPACKsteqr_("I",&N,x,w,Z,&LDZ,work,&info)); ierr = PetscFPTrapPop();CHKERRQ(ierr); if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEQR error"); for (i=0; i<(npoints+1)/2; i++) { PetscReal y = 0.5 * (-x[i] + x[npoints-i-1]); /* enforces symmetry */ x[i] = (a+b)/2 - y*(b-a)/2; x[npoints-i-1] = (a+b)/2 + y*(b-a)/2; w[i] = w[npoints-1-i] = (b-a)*PetscSqr(0.5*PetscAbsScalar(Z[i*npoints] + Z[(npoints-i-1)*npoints])); } ierr = PetscFree2(Z,work);CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc,char **args) { Mat A; PetscErrorCode ierr; PetscReal *norms; char file[PETSC_MAX_PATH_LEN]; PetscBool flg; PetscViewer fd; PetscInt n; PetscMPIInt rank; PetscInitialize(&argc,&args,(char *)0,help); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = PetscOptionsGetString(PETSC_NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -f option"); ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); ierr = MatGetSize(A,PETSC_NULL,&n);CHKERRQ(ierr); ierr = PetscMalloc(n*sizeof(PetscReal),&norms);CHKERRQ(ierr); ierr = MatGetColumnNorms(A,NORM_2,norms);CHKERRQ(ierr); if (!rank) { ierr = PetscRealView(n,norms,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); } ierr = PetscFree(norms);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscInt degrees[1000],ndegrees,npoints,two; PetscReal points[1000],weights[1000],interval[2]; PetscBool flg; ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Discretization tools test options",NULL);CHKERRQ(ierr); { ndegrees = 1000; degrees[0] = 0; degrees[1] = 1; degrees[2] = 2; ierr = PetscOptionsIntArray("-degrees","list of degrees to evaluate","",degrees,&ndegrees,&flg);CHKERRQ(ierr); if (!flg) ndegrees = 3; npoints = 1000; points[0] = 0.0; points[1] = -0.5; points[2] = 1.0; ierr = PetscOptionsRealArray("-points","list of points at which to evaluate","",points,&npoints,&flg);CHKERRQ(ierr); if (!flg) npoints = 3; two = 2; interval[0] = -1.; interval[1] = 1.; ierr = PetscOptionsRealArray("-interval","interval on which to construct quadrature","",interval,&two,NULL);CHKERRQ(ierr); } ierr = PetscOptionsEnd();CHKERRQ(ierr); ierr = CheckPoints("User-provided points",npoints,points,ndegrees,degrees);CHKERRQ(ierr); ierr = PetscDTGaussQuadrature(npoints,interval[0],interval[1],points,weights);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Quadrature weights\n");CHKERRQ(ierr); ierr = PetscRealView(npoints,weights,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); { PetscReal a = interval[0],b = interval[1],zeroth,first,second; PetscInt i; zeroth = b - a; first = (b*b - a*a)/2; second = (b*b*b - a*a*a)/3; for (i=0; i<npoints; i++) { zeroth -= weights[i]; first -= weights[i] * points[i]; second -= weights[i] * PetscSqr(points[i]); } if (PetscAbs(zeroth) < 1e-10) zeroth = 0.; if (PetscAbs(first) < 1e-10) first = 0.; if (PetscAbs(second) < 1e-10) second = 0.; ierr = PetscPrintf(PETSC_COMM_WORLD,"Moment error: zeroth=%g, first=%g, second=%g\n",(double)(-zeroth),(double)(-first),(double)(-second));CHKERRQ(ierr); } ierr = CheckPoints("Gauss points",npoints,points,ndegrees,degrees);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscInt i,j,degrees[1000],ndegrees,nsrc_points,ntarget_points; PetscReal src_points[1000],target_points[1000],*R; PetscBool flg; ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Discretization tools test options",NULL);CHKERRQ(ierr); { ndegrees = 1000; degrees[0] = 1; degrees[1] = 2; degrees[2] = 3; ierr = PetscOptionsIntArray("-degrees","list of max degrees to evaluate","",degrees,&ndegrees,&flg);CHKERRQ(ierr); if (!flg) ndegrees = 3; nsrc_points = 1000; src_points[0] = -1.; src_points[1] = 0.; src_points[2] = 1.; ierr = PetscOptionsRealArray("-src_points","list of points defining intervals on which to integrate","",src_points,&nsrc_points,&flg);CHKERRQ(ierr); if (!flg) nsrc_points = 3; ntarget_points = 1000; target_points[0] = -1.; target_points[1] = 0.; target_points[2] = 1.; ierr = PetscOptionsRealArray("-target_points","list of points defining intervals on which to integrate","",target_points,&ntarget_points,&flg);CHKERRQ(ierr); if (!flg) ntarget_points = 3; } ierr = PetscOptionsEnd();CHKERRQ(ierr); ierr = PetscMalloc1((nsrc_points-1)*(ntarget_points-1),&R);CHKERRQ(ierr); for (i=0; i<ndegrees; i++) { ierr = PetscDTReconstructPoly(degrees[i],nsrc_points-1,src_points,ntarget_points-1,target_points,R);CHKERRQ(ierr); for (j=0; j<(ntarget_points-1)*(nsrc_points-1); j++) { /* Truncate to zero for nicer output */ if (PetscAbs(R[j]) < 10*PETSC_MACHINE_EPSILON) R[j] = 0; } for (j=0; j<ntarget_points-1; j++) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Degree %D target interval (%g,%g)\n",degrees[i],(double)target_points[j],(double)target_points[j+1]);CHKERRQ(ierr); ierr = PetscRealView(nsrc_points-1,R+j*(nsrc_points-1),PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } } ierr = PetscFree(R);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscInt n = 3,i; PetscReal *la_nodes,*la_weights,*n_nodes,*n_weights; ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; ierr = PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);CHKERRQ(ierr); ierr = PetscMalloc1(n,&la_nodes);CHKERRQ(ierr); ierr = PetscMalloc1(n,&la_weights);CHKERRQ(ierr); ierr = PetscMalloc1(n,&n_nodes);CHKERRQ(ierr); ierr = PetscMalloc1(n,&n_weights);CHKERRQ(ierr); ierr = PetscDTGaussLobattoLegendreQuadrature(n,PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA,la_nodes,la_weights);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_SELF,"Gauss-Lobatto-Legendre nodes and weights computed via linear algebra: \n");CHKERRQ(ierr); ierr = PetscRealView(n,la_nodes,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); ierr = PetscRealView(n,la_weights,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); ierr = PetscDTGaussLobattoLegendreQuadrature(n,PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON,n_nodes,n_weights);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_SELF,"Gauss-Lobatto-Legendre nodes and weights computed via Newton: \n");CHKERRQ(ierr); ierr = PetscRealView(n,n_nodes,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); ierr = PetscRealView(n,n_weights,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); for (i=0; i<n; i++) { la_nodes[i] -= n_nodes[i]; la_weights[i] -= n_weights[i]; } ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_SELF,"Difference: \n");CHKERRQ(ierr); ierr = PetscRealView(n,la_nodes,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); ierr = PetscRealView(n,la_weights,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); ierr = PetscFree(la_nodes);CHKERRQ(ierr); ierr = PetscFree(la_weights);CHKERRQ(ierr); ierr = PetscFree(n_nodes);CHKERRQ(ierr); ierr = PetscFree(n_weights);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }