/* Laplacian in 3D. Use for testing MatSolve routines. Modeled by the partial differential equation - Laplacian u = 1,0 < x,y,z < 1, with boundary conditions u = 1 for x = 0, x = 1, y = 0, y = 1, z = 0, z = 1. */ static char help[] = "This example is for testing different MatSolve routines :MatSolve(), MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), and MatMatSolve().\n\ Example usage: ./ex129 -mat_type aij -dof 2\n\n"; #include #include extern PetscErrorCode ComputeMatrix(DM,Mat); extern PetscErrorCode ComputeRHS(DM,Vec); extern PetscErrorCode ComputeRHSMatrix(PetscInt,PetscInt,Mat*); int main(int argc,char **args) { PetscMPIInt size; Vec x,b,y,b1; DM da; Mat A,F,RHS,X,C1; MatFactorInfo info; IS perm,iperm; PetscInt dof =1,M=8,m,n,nrhs; PetscScalar one = 1.0; PetscReal norm,tol = 1000*PETSC_MACHINE_EPSILON; PetscBool InplaceLU=PETSC_FALSE; PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); PetscCheck(size == 1,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"This is a uniprocessor example only"); PetscCall(PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL)); PetscCall(PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL)); PetscCall(DMDACreate(PETSC_COMM_WORLD,&da)); PetscCall(DMSetDimension(da,3)); PetscCall(DMDASetBoundaryType(da,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE)); PetscCall(DMDASetStencilType(da,DMDA_STENCIL_STAR)); PetscCall(DMDASetSizes(da,M,M,M)); PetscCall(DMDASetNumProcs(da,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE)); PetscCall(DMDASetDof(da,dof)); PetscCall(DMDASetStencilWidth(da,1)); PetscCall(DMDASetOwnershipRanges(da,NULL,NULL,NULL)); PetscCall(DMSetMatType(da,MATBAIJ)); PetscCall(DMSetFromOptions(da)); PetscCall(DMSetUp(da)); PetscCall(DMCreateGlobalVector(da,&x)); PetscCall(DMCreateGlobalVector(da,&b)); PetscCall(VecDuplicate(b,&y)); PetscCall(ComputeRHS(da,b)); PetscCall(VecSet(y,one)); PetscCall(DMCreateMatrix(da,&A)); PetscCall(ComputeMatrix(da,A)); PetscCall(MatGetSize(A,&m,&n)); nrhs = 2; PetscCall(PetscOptionsGetInt(NULL,NULL,"-nrhs",&nrhs,NULL)); PetscCall(ComputeRHSMatrix(m,nrhs,&RHS)); PetscCall(MatDuplicate(RHS,MAT_DO_NOT_COPY_VALUES,&X)); PetscCall(MatGetOrdering(A,MATORDERINGND,&perm,&iperm)); PetscCall(PetscOptionsGetBool(NULL,NULL,"-inplacelu",&InplaceLU,NULL)); PetscCall(MatFactorInfoInitialize(&info)); if (!InplaceLU) { PetscCall(MatGetFactor(A,MATSOLVERPETSC,MAT_FACTOR_LU,&F)); info.fill = 5.0; PetscCall(MatLUFactorSymbolic(F,A,perm,iperm,&info)); PetscCall(MatLUFactorNumeric(F,A,&info)); } else { /* Test inplace factorization */ PetscCall(MatDuplicate(A,MAT_COPY_VALUES,&F)); PetscCall(MatLUFactor(F,perm,iperm,&info)); } PetscCall(VecDuplicate(y,&b1)); /* MatSolve */ PetscCall(MatSolve(F,b,x)); PetscCall(MatMult(A,x,b1)); PetscCall(VecAXPY(b1,-1.0,b)); PetscCall(VecNorm(b1,NORM_2,&norm)); if (norm > tol) { PetscCall(PetscPrintf(PETSC_COMM_WORLD,"MatSolve : Error of norm %g\n",(double)norm)); } /* MatSolveTranspose */ PetscCall(MatSolveTranspose(F,b,x)); PetscCall(MatMultTranspose(A,x,b1)); PetscCall(VecAXPY(b1,-1.0,b)); PetscCall(VecNorm(b1,NORM_2,&norm)); if (norm > tol) { PetscCall(PetscPrintf(PETSC_COMM_WORLD,"MatSolveTranspose : Error of norm %g\n",(double)norm)); } /* MatSolveAdd */ PetscCall(MatSolveAdd(F,b,y,x)); PetscCall(MatMult(A,y,b1)); PetscCall(VecScale(b1,-1.0)); PetscCall(MatMultAdd(A,x,b1,b1)); PetscCall(VecAXPY(b1,-1.0,b)); PetscCall(VecNorm(b1,NORM_2,&norm)); if (norm > tol) { PetscCall(PetscPrintf(PETSC_COMM_WORLD,"MatSolveAdd : Error of norm %g\n",(double)norm)); } /* MatSolveTransposeAdd */ PetscCall(MatSolveTransposeAdd(F,b,y,x)); PetscCall(MatMultTranspose(A,y,b1)); PetscCall(VecScale(b1,-1.0)); PetscCall(MatMultTransposeAdd(A,x,b1,b1)); PetscCall(VecAXPY(b1,-1.0,b)); PetscCall(VecNorm(b1,NORM_2,&norm)); if (norm > tol) { PetscCall(PetscPrintf(PETSC_COMM_WORLD,"MatSolveTransposeAdd : Error of norm %g\n",(double)norm)); } /* MatMatSolve */ PetscCall(MatMatSolve(F,RHS,X)); PetscCall(MatMatMult(A,X,MAT_INITIAL_MATRIX,2.0,&C1)); PetscCall(MatAXPY(C1,-1.0,RHS,SAME_NONZERO_PATTERN)); PetscCall(MatNorm(C1,NORM_FROBENIUS,&norm)); if (norm > tol) { PetscCall(PetscPrintf(PETSC_COMM_WORLD,"MatMatSolve : Error of norm %g\n",(double)norm)); } PetscCall(VecDestroy(&x)); PetscCall(VecDestroy(&b)); PetscCall(VecDestroy(&b1)); PetscCall(VecDestroy(&y)); PetscCall(MatDestroy(&A)); PetscCall(MatDestroy(&F)); PetscCall(MatDestroy(&RHS)); PetscCall(MatDestroy(&C1)); PetscCall(MatDestroy(&X)); PetscCall(ISDestroy(&perm)); PetscCall(ISDestroy(&iperm)); PetscCall(DMDestroy(&da)); PetscCall(PetscFinalize()); return 0; } PetscErrorCode ComputeRHS(DM da,Vec b) { PetscInt mx,my,mz; PetscScalar h; PetscFunctionBegin; PetscCall(DMDAGetInfo(da,0,&mx,&my,&mz,0,0,0,0,0,0,0,0,0)); h = 1.0/((mx-1)*(my-1)*(mz-1)); PetscCall(VecSet(b,h)); PetscFunctionReturn(0); } PetscErrorCode ComputeRHSMatrix(PetscInt m,PetscInt nrhs,Mat *C) { PetscRandom rand; Mat RHS; PetscScalar *array,rval; PetscInt i,k; PetscFunctionBegin; PetscCall(MatCreate(PETSC_COMM_WORLD,&RHS)); PetscCall(MatSetSizes(RHS,m,PETSC_DECIDE,PETSC_DECIDE,nrhs)); PetscCall(MatSetType(RHS,MATSEQDENSE)); PetscCall(MatSetUp(RHS)); PetscCall(PetscRandomCreate(PETSC_COMM_WORLD,&rand)); PetscCall(PetscRandomSetFromOptions(rand)); PetscCall(MatDenseGetArray(RHS,&array)); for (i=0; i 1) { for (k=1; k