Re: [petsc-users] petsc-users Digest, Vol 23, Issue 32
Hi Jed, Thank you for your reply. The Matrix A and Vector x are created by MatCreateMPIAIJ and VecCreateMPI and matrix A is obtained by SNESDefaultComputeJacobian. I compared the results using the following routine: .......................................................... sprintf(filename,"A.m"); ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,filename,&view->viewer);CHKERRQ(ierr); ierr = PetscViewerSetFormat(view->viewer, PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); ierr = MatView(A, view->viewer);CHKERRQ(ierr); Vec y; ierr = VecDuplicate(x, &y);CHKERRQ(ierr); ierr = MatMult(A, x, y);CHKERRQ(ierr); sprintf(filename,"x.m"); ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,filename,&view->viewer);CHKERRQ(ierr); ierr = PetscViewerSetFormat(view->viewer, PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); ierr = VecView(x, view->viewer);CHKERRQ(ierr); sprintf(filename,"y.m"); ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,filename,&view->viewer);CHKERRQ(ierr); ierr = PetscViewerSetFormat(view->viewer, PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); ierr = VecView(y, view->viewer);CHKERRQ(ierr); ........................................................... In the Matlab, I first load the files A.m, x.m and y.m into A, x and y, and then do "error=norm(A*x - y)". The 'error' is very big(about 200). Following your suggestion, I can write the matrix and vectors into binary files, but I do not how to load these binary files into matlab matrix and vector. I do not understand the command: [A,x,y] = PetscBinaryRead('binaryoutput'), can this command load the binary files into matlab matrix and vector? Thanks. Best, Rongliang
Message: 2 Date: Sun, 28 Nov 2010 21:18:59 +0100 From: Jed Brown <[email protected]> Subject: Re: [petsc-users] Why MatMult(A, x, Ax) != A*x? To: PETSc users list <[email protected]> Message-ID: <[email protected]> Content-Type: text/plain; charset="utf-8"
On Sun, Nov 28, 2010 at 21:11, Rongliang Chen <[email protected]
wrote:
The format of the matrix A is AIJ and is obtained by function SNESDefaultComputeJacobian. I compute A*x in Matlab with the following A and x and compare it with Ax.
....................... ierr = MatMult(*A, x, Ax);CHKERRQ(ierr);
sprintf(filename,"x.m"); ierr =
PetscViewerASCIIOpen(PETSC_COMM_WORLD,filename,&view->viewer);CHKERRQ(ierr);
ierr = PetscViewerSetFormat(view->viewer, PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); ierr = VecView(x, view->viewer);CHKERRQ(ierr);
sprintf(filename,"Ax.m"); ierr =
PetscViewerASCIIOpen(PETSC_COMM_WORLD,filename,&view->viewer);CHKERRQ(ierr);
ierr = PetscViewerSetFormat(view->viewer, PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); ierr = VecView(Ax, view->viewer);CHKERRQ(ierr);
Are the vectors and matrices obtained from a DA (DACreateGlobalVector, DAGetMatrix)? How are you comparing the matrix that PETSc sees with the matrix that Matlab sees? I suggest writing the matrix and both vectors to a PETSc binary file
PetscViewer viewer = PETSC_VIEWER_BINARY(PETSC_COMM_WORLD); MatView(A,viewer); VecView(x,viewer); MatMult(A,x,y); VecView(y,viewer);
and read them into Matlab with
[A,x,y] = PetscBinaryRead('binaryoutput') norm(A*x - y) % This should be small
Jed
On Nov 29, 2010, at 8:26 PM, Rongliang Chen wrote:
Following your suggestion, I can write the matrix and vectors into binary files, but I do not how to load these binary files into matlab matrix and vector. I do not understand the command: [A,x,y] = PetscBinaryRead('binaryoutput'), can this command load the binary files into matlab matrix and vector? Thanks.
If you first view into the binary file the A, then the x vector than the y vector then yes
[A,x,y] = PetscBinaryRead('binaryoutput')
reads in the matrix and the two vectors. Barry
Best,
Rongliang
Message: 2 Date: Sun, 28 Nov 2010 21:18:59 +0100 From: Jed Brown <[email protected]> Subject: Re: [petsc-users] Why MatMult(A, x, Ax) != A*x? To: PETSc users list <[email protected]> Message-ID: <[email protected]> Content-Type: text/plain; charset="utf-8"
On Sun, Nov 28, 2010 at 21:11, Rongliang Chen <[email protected]>wrote:
The format of the matrix A is AIJ and is obtained by function SNESDefaultComputeJacobian. I compute A*x in Matlab with the following A and x and compare it with Ax.
....................... ierr = MatMult(*A, x, Ax);CHKERRQ(ierr);
sprintf(filename,"x.m"); ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,filename,&view->viewer);CHKERRQ(ierr); ierr = PetscViewerSetFormat(view->viewer, PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); ierr = VecView(x, view->viewer);CHKERRQ(ierr);
sprintf(filename,"Ax.m"); ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,filename,&view->viewer);CHKERRQ(ierr); ierr = PetscViewerSetFormat(view->viewer, PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); ierr = VecView(Ax, view->viewer);CHKERRQ(ierr);
Are the vectors and matrices obtained from a DA (DACreateGlobalVector, DAGetMatrix)? How are you comparing the matrix that PETSc sees with the matrix that Matlab sees? I suggest writing the matrix and both vectors to a PETSc binary file
PetscViewer viewer = PETSC_VIEWER_BINARY(PETSC_COMM_WORLD); MatView(A,viewer); VecView(x,viewer); MatMult(A,x,y); VecView(y,viewer);
and read them into Matlab with
[A,x,y] = PetscBinaryRead('binaryoutput') norm(A*x - y) % This should be small
Jed
On Tue, Nov 30, 2010 at 03:26, Rongliang Chen <[email protected]>wrote:
[A,x,y] = PetscBinaryRead('binaryoutput'), can this command load the binary files into matlab matrix and vector?
Yes, this reads the binary files. You will need to add $PETSC_DIR/bin/matlab to your path (see "help addpath"). You should also call PetscViewerDestroy() when you are done with each viewer. In the source you quoted, this is never called. Jed
participants (3)
-
Barry Smith -
Jed Brown -
Rongliang Chen