<div style="line-height:1.7;color:#000000;font-size:14px;font-family:arial">/* Program usage: mpiexec ex1 [-help] [all PETSc options] */<br><br>static char help[] = "Basic vector routines.\n\n";<br><br><br>#include "petscksp.h"<br>#include "petscvec.h"<br><br><br><br>int main(int argc,char **argv)<br>{<br> int N=16;<br> <br> int MyRank;<br> <br> Mat A; <br> PC Pc; <br> KSP ksp;<br> Vec b,x;<br> <br> PetscInitialize(&argc,&argv,(char*)0,help);<br> MPI_Comm_rank(MPI_COMM_WORLD,&MyRank);<br> <br> MatCreate(PETSC_COMM_WORLD,&A);<br> MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);<br> MatSetFromOptions(A);<br> MatMPIAIJSetPreallocation(A,5,PETSC_NULL,5,PETSC_NULL);<br> <br> VecCreate(PETSC_COMM_WORLD,&b);<br> VecSetSizes(b,PETSC_DECIDE,N);<br> VecSetFromOptions(b); <br> VecDuplicate(b,&x);<br>&n
bsp; <br> int row=0;<br> int* col3;<br> int* col4;<br> int* col5;<br> PetscMalloc(3*sizeof(PetscInt),&col3);<br> PetscMalloc(4*sizeof(PetscInt),&col4);<br> PetscMalloc(5*sizeof(PetscInt),&col5);<br> <br> col3[0]=0; col3[1]=1; col3[2]=4;<br> double value3[3]={2.2916666666666665e+00,-1.1458333333333333e+00,-1.1458333333333333e+00};<br> MatSetValues(A,1,&row,3,col3,value3,INSERT_VALUES);<br><br> row=1;<br> col4[0]=0; col4[1]=1; col4[2]=2; col4[3]=5;<br> double value4[4]={-1.1458333333333333e+00,3.8020833333333330e+00,-1.2500000000000000e+00 ,-1.4062500000000000e+00};<br> MatSetValues(A,1,&row,4,col4,value4,INSERT_VALUES);<br><br> <br> row=2;<br> col4[0]=1; col4[1]=2; col4[2]=3; col4[3]=6;<br> value4[0]=-1.2500000000000000e+00;<br> value4[1]=3.8020833333333330e+00;<br> value4[2]=-1.1458333333333333e+00 ;<br>&n
bsp;value4[3]=-1.4062500000000000e+00;<br> MatSetValues(A,1,&row,4,col4,value4,INSERT_VALUES);<br> <br> <br> row=3;<br> col3[0]=2; col3[1]=3; col3[2]=7;<br> value3[0]=-1.1458333333333333e+00;<br> value3[1]=2.2916666666666665e+00;<br> value3[2]=-1.1458333333333333e+00;<br> MatSetValues(A,1,&row,3,col3,value3,INSERT_VALUES); <br> <br> <br> row=4;<br> col4[0]=0; col4[1]=4; col4[2]=5; col4[3]=8;<br> value4[0]=-1.1458333333333333e+00;<br> value4[1]=3.8020833333333330e+00;<br> value4[2]=-1.4062500000000000e+00;<br> value4[3]=-1.2500000000000000e+00;<br> MatSetValues(A,1,&row,4,col4,value4,INSERT_VALUES); <br> <br> row=5;<br> col5[0]=1; col5[1]=4; col5[2]=5; col5[3]=6; col5[4]=9;<br> double value5[5];<br> value5[0]=-1.4062500000000000e+00;<br> value5[1]=-1.4062500000000000e+00;<br> value5[2]=5
.9375000000000000e+00;<br> value5[3]=-1.5625000000000000e+00 ;<br> value5[4]=-1.5625000000000000e+00 ;<br> MatSetValues(A,1,&row,5,col5,value5,INSERT_VALUES); <br> <br> <br> <br> row=6;<br> col5[0]=2; col5[1]=5; col5[2]=6; col5[3]=7; col5[4]=10;<br> value5[0]=-1.4062500000000000e+00;<br> value5[1]=-1.5625000000000000e+00;<br> value5[2]=5.9375000000000000e+00;<br> value5[3]=-1.4062500000000000e+00 ;<br> value5[4]=-1.5625000000000000e+00 ;<br> MatSetValues(A,1,&row,5,col5,value5,INSERT_VALUES); <br> <br> row=7;<br> col4[0]=3; col4[1]=6; col4[2]=7; col4[3]=11;<br> value4[0]=-1.1458333333333333e+00;<br> value4[1]=-1.4062500000000000e+00;<br> value4[2]=3.8020833333333330e+00;<br> value4[3]=-1.2500000000000000e+00;<br> MatSetValues(A,1,&row,4,col4,value4,INSERT_VALUES); <br> <br> row=8;<br>
col4[0]=4; col4[1]=8; col4[2]=9; col4[3]=12;<br> value4[0]=-1.2500000000000000e+00;<br> value4[1]=3.8020833333333330e+00;<br> value4[2]=-1.4062500000000000e+00;<br> value4[3]=-1.1458333333333333e+00;<br> MatSetValues(A,1,&row,4,col4,value4,INSERT_VALUES); <br> <br> <br> row=9;<br> col5[0]=5; col5[1]=8; col5[2]=9; col5[3]=10; col5[4]=13;<br> value5[0]=-1.5625000000000000e+00;<br> value5[1]=-1.4062500000000000e+00;<br> value5[2]=5.9375000000000000e+00;<br> value5[3]=-1.5625000000000000e+00 ;<br> value5[4]= -1.4062500000000000e+00;<br> MatSetValues(A,1,&row,5,col5,value5,INSERT_VALUES); <br> <br> row=10;<br> col5[0]=6; col5[1]=9; col5[2]=10; col5[3]=11; col5[4]=14;<br> value5[0]=-1.5625000000000000e+00;<br> value5[1]=-1.5625000000000000e+00;<br> value5[2]=5.93750000000000
00e+00;<br> value5[3]=-1.4062500000000000e+00 ;<br> value5[4]= -1.4062500000000000e+00;<br> MatSetValues(A,1,&row,5,col5,value5,INSERT_VALUES); <br> <br> <br> row=11;<br> col4[0]=7; col4[1]=10; col4[2]=11; col4[3]=15;<br> value4[0]=-1.2500000000000000e+00;<br> value4[1]=-1.4062500000000000e+00;<br> value4[2]=3.8020833333333330e+00;<br> value4[3]=-1.1458333333333333e+00;<br> MatSetValues(A,1,&row,4,col4,value4,INSERT_VALUES); <br> <br> <br> <br> <br> row=12;<br> col3[0]=8; col3[1]=12; col3[2]=13;<br> value3[0]=-1.1458333333333333e+00;<br> value3[1]=2.2916666666666665e+00;<br> value3[2]=-1.1458333333333333e+00;<br> MatSetValues(A,1,&row,3,col3,value3,INSERT_VALUES); <br> <br> <br> row=13;<br> col4[0]=9; col4[1]=12; col4[2]=13; col4[3]=
14;<br> value4[0]=-1.4062500000000000e+00;<br> value4[1]=-1.1458333333333333e+00;<br> value4[2]=3.8020833333333330e+00;<br> value4[3]=-1.2500000000000000e+00;<br> MatSetValues(A,1,&row,4,col4,value4,INSERT_VALUES); <br> <br> <br> row=14;<br> col4[0]=10; col4[1]=13; col4[2]=14; col4[3]=15;<br> value4[0]=-1.4062500000000000e+00;<br> value4[1]=-1.2500000000000000e+00;<br> value4[2]=3.8020833333333330e+00;<br> value4[3]=-1.1458333333333333e+00;<br> MatSetValues(A,1,&row,4,col4,value4,INSERT_VALUES); <br> <br> <br> <br> row=15;<br> col3[0]=11; col3[1]=14; col3[2]=15;<br> value3[0]=-1.1458333333333333e+00;<br> value3[1]=-1.1458333333333333e+00;<br> value3[2]=2.2916666666666665e+00;<br> MatSetValues(A,1,&row,3,col3,value3,INSERT_VALUES); <br> <br> <br>&n
bsp;<br> MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);<br> MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); <br> <br>// MatView(A,PETSC_VIEWER_STDOUT_WORLD); <br><br> <br> <br> double val[16]={-7.6233779415782715e-04,<br>-3.0457072596705860e-04,<br>3.0457072596705860e-04,<br>7.6233779415782715e-04,<br>-4.4764543813290442e-03,<br>-1.6196451741044846e-03,<br>1.6196451741044846e-03,<br>4.4764543813290442e-03,<br>-1.9333475373837013e-02,<br>-5.4815619458573189e-03,<br>5.4815619458573189e-03,<br>1.9333475373837013e-02,<br>-8.4153777598326651e-02,<br>-1.2883385353962010e-02,<br>1.2883385353962010e-02,<br>8.4153777598326651e-02};<br><br>int* col16;<br> PetscMalloc(16*sizeof(PetscInt),&col16);<br> for(int i=0;i<16;i++)<br> col16[i]=i;<br> <br>VecSetValues(b,16,col16,val,INSERT_VALUES);<br> VecAssemblyBegin(b);<br> VecAssemblyEnd(b); <br>//<br> &nbs
p; VecView(b,PETSC_VIEWER_STDOUT_WORLD); <br><br> <br> KSPCreate(PETSC_COMM_WORLD,&ksp);<br> KSPSetOperators(ksp,A,A,SAME_NONZERO_PATTERN);<br> KSPSetOperators(ksp,A,A,SAME_PRECONDITIONER);<br> KSPSetInitialGuessNonzero(ksp,PETSC_FALSE);<br> <br> KSPSetType(ksp,KSPBCGS);<br> // KSPSetType(ksp,KSPLSQR);<br> // KSPSetType(ksp,KSPFGMRES);<br> // KSPSetType(ksp,KSPDGMRES);<br>//KSPSetType(ksp,KSPTCQMR);<br> // KSPSetType(ksp,KSPPREONLY);<br> <br> //KSPGetPC(ksp,&Pc);<br> // PCSetType(Pc,PCJACOBI);<br> KSPSetFromOptions(ksp);<br> KSPSetTolerances(ksp,1.e-20,1.e-20,PETSC_DEFAULT,1000);<br> KSPSolve(ksp,b,x); <br> <br> <br> VecView(x,PETSC_VIEWER_STDOUT_WORLD); <br><br> <br> PetscFinalize();<br> return 0;<br>}<br> <br><br></div><br><br><span title="neteasefooter"><span id="netease_mail_footer"></span></s
pan>