PetscErrorCode StokesExactSolution(Stokes *s) { PetscInt row, start, end, i, j; PetscScalar val; Vec y0,y1; PetscErrorCode ierr; PetscFunctionBeginUser; /* velocity part */ ierr = VecGetSubVector(s->y, s->isg[0], &y0);CHKERRQ(ierr); ierr = VecGetOwnershipRange(y0, &start, &end);CHKERRQ(ierr); for (row = start; row < end; row++) { ierr = StokesGetPosition(s, row,&i,&j);CHKERRQ(ierr); if (row < s->nx*s->ny) { val = StokesExactVelocityX(j*s->hy+s->hy/2); } else { val = 0; } ierr = VecSetValue(y0, row, val, INSERT_VALUES);CHKERRQ(ierr); } ierr = VecRestoreSubVector(s->y, s->isg[0], &y0);CHKERRQ(ierr); /* pressure part */ ierr = VecGetSubVector(s->y, s->isg[1], &y1);CHKERRQ(ierr); ierr = VecGetOwnershipRange(y1, &start, &end);CHKERRQ(ierr); for (row = start; row < end; row++) { ierr = StokesGetPosition(s, row, &i, &j);CHKERRQ(ierr); val = StokesExactPressure(i*s->hx+s->hx/2); ierr = VecSetValue(y1, row, val, INSERT_VALUES);CHKERRQ(ierr); } ierr = VecRestoreSubVector(s->y, s->isg[1], &y1);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode StokesRhs(Stokes *s) { PetscInt row, start, end, i, j; PetscScalar val; Vec b0,b1; PetscErrorCode ierr; PetscFunctionBeginUser; /* velocity part */ ierr = VecGetSubVector(s->b, s->isg[0], &b0);CHKERRQ(ierr); ierr = VecGetOwnershipRange(b0, &start, &end);CHKERRQ(ierr); for (row = start; row < end; row++) { ierr = StokesGetPosition(s, row, &i, &j);CHKERRQ(ierr); if (row < s->nx*s->ny) { ierr = StokesRhsMomX(s, i, j, &val);CHKERRQ(ierr); } else if (row < 2*s->nx*s->ny) { ierr = StokesRhsMomY(s, i, j, &val);CHKERRQ(ierr); } ierr = VecSetValue(b0, row, val, INSERT_VALUES);CHKERRQ(ierr); } ierr = VecRestoreSubVector(s->b, s->isg[0], &b0);CHKERRQ(ierr); /* pressure part */ ierr = VecGetSubVector(s->b, s->isg[1], &b1);CHKERRQ(ierr); ierr = VecGetOwnershipRange(b1, &start, &end);CHKERRQ(ierr); for (row = start; row < end; row++) { ierr = StokesGetPosition(s, row, &i, &j);CHKERRQ(ierr); ierr = StokesRhsMass(s, i, j, &val);CHKERRQ(ierr); ierr = VecSetValue(b1, row, val, INSERT_VALUES);CHKERRQ(ierr); } ierr = VecRestoreSubVector(s->b, s->isg[1], &b1);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode StokesSetupMatBlock01(Stokes *s) { PetscInt row, start, end, size, i, j; PetscInt cols[5]; PetscScalar vals[5]; PetscErrorCode ierr; PetscFunctionBeginUser; /* A[1] is 2N-by-N */ ierr = MatCreate(PETSC_COMM_WORLD, &s->subA[1]);CHKERRQ(ierr); ierr = MatSetOptionsPrefix(s->subA[1],"a01_"); ierr = MatSetSizes(s->subA[1],PETSC_DECIDE,PETSC_DECIDE,2*s->nx*s->ny,s->nx*s->ny);CHKERRQ(ierr); ierr = MatSetType(s->subA[1],MATMPIAIJ);CHKERRQ(ierr); ierr = MatMPIAIJSetPreallocation(s->subA[1],5,NULL,5,NULL);CHKERRQ(ierr); ierr = MatGetOwnershipRange(s->subA[1],&start,&end);CHKERRQ(ierr); ierr = MatSetOption(s->subA[1],MAT_IGNORE_ZERO_ENTRIES,PETSC_TRUE);CHKERRQ(ierr); for (row = start; row < end; row++) { ierr = StokesGetPosition(s, row, &i, &j);CHKERRQ(ierr); /* first part: rows 0 to (nx*ny-1) */ if (row < s->nx*s->ny) { ierr = StokesStencilGradientX(s, i, j, &size, cols, vals);CHKERRQ(ierr); } /* second part: rows (nx*ny) to (2*nx*ny-1) */ else { ierr = StokesStencilGradientY(s, i, j, &size, cols, vals);CHKERRQ(ierr); } ierr = MatSetValues(s->subA[1], 1, &row, size, cols, vals, INSERT_VALUES);CHKERRQ(ierr); } ierr = MatAssemblyBegin(s->subA[1], MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(s->subA[1], MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode StokesSetupMatBlock00(Stokes *s) { PetscInt row, start, end, size, i, j; PetscInt cols[5]; PetscScalar vals[5]; PetscErrorCode ierr; PetscFunctionBeginUser; /* A[0] is 2N-by-2N */ ierr = MatCreate(PETSC_COMM_WORLD,&s->subA[0]);CHKERRQ(ierr); ierr = MatSetOptionsPrefix(s->subA[0],"a00_");CHKERRQ(ierr); ierr = MatSetSizes(s->subA[0],PETSC_DECIDE,PETSC_DECIDE,2*s->nx*s->ny,2*s->nx*s->ny);CHKERRQ(ierr); ierr = MatSetType(s->subA[0],MATMPIAIJ);CHKERRQ(ierr); ierr = MatMPIAIJSetPreallocation(s->subA[0],5,NULL,5,NULL);CHKERRQ(ierr); ierr = MatGetOwnershipRange(s->subA[0], &start, &end);CHKERRQ(ierr); for (row = start; row < end; row++) { ierr = StokesGetPosition(s, row, &i, &j);CHKERRQ(ierr); /* first part: rows 0 to (nx*ny-1) */ ierr = StokesStencilLaplacian(s, i, j, &size, cols, vals);CHKERRQ(ierr); /* second part: rows (nx*ny) to (2*nx*ny-1) */ if (row >= s->nx*s->ny) { for (i = 0; i < 5; i++) cols[i] = cols[i] + s->nx*s->ny; } for (i = 0; i < 5; i++) vals[i] = -1.0*vals[i]; /* dynamic viscosity coef mu=-1 */ ierr = MatSetValues(s->subA[0], 1, &row, size, cols, vals, INSERT_VALUES);CHKERRQ(ierr); } ierr = MatAssemblyBegin(s->subA[0], MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(s->subA[0], MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode StokesSetupMatFp(Stokes *s) { PetscInt row, start, end, size, i, j; PetscInt cols[5]; PetscScalar vals[5]; PetscErrorCode ierr; PetscFunctionBeginUser; // Fp is N-by-N ierr = MatCreate(PETSC_COMM_WORLD,&s->Fp);CHKERRQ(ierr); ierr = MatSetOptionsPrefix(s->Fp,"Fp_");CHKERRQ(ierr); ierr = MatSetSizes(s->Fp,PETSC_DECIDE,PETSC_DECIDE,s->nx*s->ny,s->nx*s->ny);CHKERRQ(ierr); ierr = MatSetType(s->Fp,MATMPIAIJ);CHKERRQ(ierr); ierr = MatMPIAIJSetPreallocation(s->Fp,5,NULL,5,NULL);CHKERRQ(ierr); ierr = MatGetOwnershipRange(s->Fp, &start, &end);CHKERRQ(ierr); for (row = start; row < end; row++) { ierr = StokesGetPosition(s, row, &i, &j);CHKERRQ(ierr); ierr = StokesStencilLaplacian(s, i, j, &size, cols, vals);CHKERRQ(ierr); for (i = 0; i < 5; i++) vals[i] = -1.0*vals[i]; //* dynamic viscosity coef mu=-1 ierr = MatSetValues(s->Fp, 1, &row, size, cols, vals, INSERT_VALUES);CHKERRQ(ierr); } ierr = MatAssemblyBegin(s->Fp, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(s->Fp, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); PetscFunctionReturn(0); }