<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman">Hi, everyone,</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman">I am new to PETSc, and I have just begun to use it together with slepc. It is really fantastic and I like it!</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> </font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman">It was not from me but one of my colleges who wanted to solve an inverse of a matrix, which had 400 rows and columns.</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> </font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3" face="Times New Roman">I know that it is not good to design a algorithm that has a process for solving the inverse of a matrix. I just wanted to give it a try. However, things turned out wired. I followed the instructions on the FAQ web page of PETSc, the one using MatLUFractor() and MatMatSolve(). After I finished the coding, I tried the program. The result I got was a matrix which only has its first column but nothing else. I did not know what's happened. The following is the codes I used. It is kind of badly organized and the gmail web page ignores my 'Tab's, forgive me for that.</font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3" face="Times New Roman"> </font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman">Thanks ahead!</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> </font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman">============Code Begins=============</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman">/*<br> * MI.cpp<br> *<br> * Created on: Nov 22, 2011<br> * Author: huyaoyu<br> */</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman">static char help[] = "Give the inverse of a matrix.\n\n";</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman">#include <slepceps.h></font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman">#include <iostream><br>#include <fstream></font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman">#undef __FUNCT__<br>#define __FUNCT__ "main"</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman">#define DIMENSION 2</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman">int main(int argc,char **args)<br>{<br> PetscErrorCode ierr;<br> PetscMPIInt size;<br> Mat A,CA;<br> Mat BB,XX;<br>
IS is_row;<br> IS is_col;<br> MatFactorInfo mfinfo;<br> PetscMPIInt n;<br> PetscScalar* array_scalar = new PetscScalar[DIMENSION*DIMENSION];</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> for(int i=0;i<DIMENSION*DIMENSION;i++)<br> {<br> array_scalar[i] = 0.0;<br> }<br> for(int i=0;i<DIMENSION;i++)<br>
{<br> array_scalar[i*DIMENSION + i] = 1.0;<br> }</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> int idxm[DIMENSION];<br> for(int i=0;i<DIMENSION;i++)<br> {<br> idxm[i] = i;<br> }</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> PetscInitialize(&argc,&args,(char *)0,help);<br> ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);<br>
if (size != 1) SETERRQ(PETSC_COMM_WORLD,1,"This is a uniprocessor example only!");<br> ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr);</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> // B & X<br> ierr = MatCreateSeqDense(PETSC_COMM_WORLD,DIMENSION,DIMENSION,PETSC_NULL,&BB); CHKERRQ(ierr);<br>
ierr = MatCreateSeqDense(PETSC_COMM_WORLD,DIMENSION,DIMENSION,PETSC_NULL,&XX); CHKERRQ(ierr);</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> ierr = MatSetValues(BB,DIMENSION,idxm,DIMENSION,idxm,array_scalar,INSERT_VALUES); CHKERRQ(ierr);<br> ierr = MatAssemblyBegin(BB,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);<br>
ierr = MatAssemblyEnd(BB,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> ierr = ISCreateStride(MPI_COMM_WORLD,DIMENSION,0,1,&is_row); CHKERRQ(ierr);<br> ierr = ISCreateStride(MPI_COMM_WORLD,DIMENSION,0,1,&is_col); CHKERRQ(ierr);<br>
ierr = MatFactorInfoInitialize(&mfinfo); CHKERRQ(ierr);</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> // matrix A<br> std::fstream in_file;<br> double temp_scalar;<br> in_file.open("./A.txt",std::ifstream::in);<br>
if(!in_file.good())<br> {<br> ierr = PetscPrintf(PETSC_COMM_SELF,"File open failed!\n"); CHKERRQ(ierr);<br> return 1;<br> }</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> for(int i=0;i<DIMENSION;i++)<br> {<br> for(int j=0;j<DIMENSION;j++)<br> {<br> in_file>>temp_scalar;</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> array_scalar[i*DIMENSION + j] = temp_scalar;<br> }<br> }<br> in_file.close();</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> ierr = MatCreateSeqDense(PETSC_COMM_WORLD,DIMENSION,DIMENSION,PETSC_NULL,&A); CHKERRQ(ierr);<br> ierr = MatCreateSeqDense(PETSC_COMM_WORLD,DIMENSION,DIMENSION,PETSC_NULL,&CA); CHKERRQ(ierr);<br>
ierr = MatSetValues(A,DIMENSION,idxm,DIMENSION,idxm,array_scalar,INSERT_VALUES); CHKERRQ(ierr);</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);<br> ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> ierr = MatDuplicate(A,MAT_COPY_VALUES,&CA); CHKERRQ(ierr);</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> ierr = MatLUFactor(A,is_row,is_col,&mfinfo); CHKERRQ(ierr);<br> ierr = MatMatSolve(A,BB,XX); CHKERRQ(ierr);<br>
ierr = PetscPrintf(PETSC_COMM_SELF,"The inverse of A matrix is:\n"); CHKERRQ(ierr);<br> ierr = MatView(XX,PETSC_VIEWER_STDOUT_SELF); CHKERRQ(ierr);</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> ierr = MatMatMult(CA,XX,MAT_REUSE_MATRIX,PETSC_DEFAULT,&BB);<br> ierr = MatView(BB,PETSC_VIEWER_STDOUT_SELF); CHKERRQ(ierr);</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> // destroy<br> ierr = MatDestroy(&A); CHKERRQ(ierr);<br> ierr = MatDestroy(&BB); CHKERRQ(ierr);<br>
ierr = MatDestroy(&XX); CHKERRQ(ierr);<br> ierr = MatDestroy(&CA); CHKERRQ(ierr);</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> ierr = PetscFinalize();</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> delete[] array_scalar;</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman"> return 0;<br>}</font></font></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span lang="EN-US"><font size="3"><font face="Times New Roman">============Code Ends==============</font></font></span></p>