~ubuntu-branches/ubuntu/warty/petsc/warty

« back to all changes in this revision

Viewing changes to src/ksp/pc/examples/tests/ex3.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam C. Powell, IV
  • Date: 2004-06-07 13:41:43 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040607134143-92p586zrauvie0le
Tags: 2.2.0-2
* Upstream patch level 2.
* New PETSC_BOPT_EXTRA option for different BOPT and lib names, with _c++
  symlinks only for plain and single (closes: #249617).
* New DEBIAN_DIST=contrib option to link with hypre, parmetis (closes:
  #249619).
* Combined petsc-c and petsc-fortran substvars into petsc-compilers.
* Extra quote in -dev prerm eliminates "too many arguments" problem.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*$Id: ex3.c,v 1.53 2001/08/07 03:03:43 balay Exp $*/
 
2
 
 
3
static char help[] = "Demonstrates the use of fast Richardson for SOR. And\n\
 
4
also tests the MatRelax() routines.  Input parameters are:\n\
 
5
 -n <n> : problem dimension\n\n";
 
6
 
 
7
#include "petscksp.h"
 
8
#include "petscpc.h"
 
9
 
 
10
#undef __FUNCT__
 
11
#define __FUNCT__ "main"
 
12
int main(int argc,char **args)
 
13
{
 
14
  Mat     mat;          /* matrix */
 
15
  Vec     b,ustar,u;  /* vectors (RHS, exact solution, approx solution) */
 
16
  PC      pc;           /* PC context */
 
17
  KSP     ksp;          /* KSP context */
 
18
  int     ierr,n = 10,i,its,col[3];
 
19
  PetscScalar  value[3],one = 1.0,zero = 0.0;
 
20
  KSPType kspname;
 
21
  PCType  pcname;
 
22
 
 
23
  PetscInitialize(&argc,&args,(char *)0,help);
 
24
  ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr);
 
25
 
 
26
  /* Create and initialize vectors */
 
27
  ierr = VecCreateSeq(PETSC_COMM_SELF,n,&b);    CHKERRQ(ierr);
 
28
  ierr = VecCreateSeq(PETSC_COMM_SELF,n,&ustar);CHKERRQ(ierr);
 
29
  ierr = VecCreateSeq(PETSC_COMM_SELF,n,&u);    CHKERRQ(ierr);
 
30
  ierr = VecSet(&one,ustar);CHKERRQ(ierr);
 
31
  ierr = VecSet(&zero,u);CHKERRQ(ierr);
 
32
 
 
33
  /* Create and assemble matrix */
 
34
  ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,n,n,3,PETSC_NULL,&mat);CHKERRQ(ierr);
 
35
  value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;
 
36
  for (i=1; i<n-1; i++) {
 
37
    col[0] = i-1; col[1] = i; col[2] = i+1;
 
38
    ierr = MatSetValues(mat,1,&i,3,col,value,INSERT_VALUES);CHKERRQ(ierr);
 
39
  }
 
40
  i = n - 1; col[0] = n - 2; col[1] = n - 1;
 
41
  ierr = MatSetValues(mat,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr);
 
42
  i = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;
 
43
  ierr = MatSetValues(mat,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr);
 
44
  ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
 
45
  ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
 
46
 
 
47
  /* Compute right-hand-side vector */
 
48
  ierr = MatMult(mat,ustar,b);CHKERRQ(ierr);
 
49
 
 
50
  /* Create PC context and set up data structures */
 
51
  ierr = PCCreate(PETSC_COMM_WORLD,&pc);CHKERRQ(ierr);
 
52
  ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
 
53
  ierr = PCSetFromOptions(pc);CHKERRQ(ierr);
 
54
  ierr = PCSetOperators(pc,mat,mat,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
 
55
  ierr = PCSetVector(pc,u);  CHKERRQ(ierr);
 
56
  ierr = PCSetUp(pc);CHKERRQ(ierr);
 
57
 
 
58
  /* Create KSP context and set up data structures */
 
59
  ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
 
60
  ierr = KSPSetType(ksp,KSPRICHARDSON);CHKERRQ(ierr);
 
61
  ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
 
62
  ierr = KSPSetSolution(ksp,u);CHKERRQ(ierr);
 
63
  ierr = KSPSetRhs(ksp,b);CHKERRQ(ierr);
 
64
  ierr = PCSetOperators(pc,mat,mat,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
 
65
  ierr = KSPSetPC(ksp,pc);CHKERRQ(ierr);
 
66
  ierr = KSPSetUp(ksp);CHKERRQ(ierr);
 
67
 
 
68
  /* Solve the problem */
 
69
  ierr = KSPGetType(ksp,&kspname);CHKERRQ(ierr);
 
70
  ierr = PCGetType(pc,&pcname);CHKERRQ(ierr);
 
71
  ierr = PetscPrintf(PETSC_COMM_SELF,"Running %s with %s preconditioning\n",kspname,pcname);CHKERRQ(ierr);
 
72
  ierr = KSPSolve(ksp);CHKERRQ(ierr);
 
73
  ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr);
 
74
  ierr = PetscPrintf(PETSC_COMM_WORLD,"Number of iterations %d\n",its);
 
75
 
 
76
  /* Free data structures */
 
77
  ierr = KSPDestroy(ksp);CHKERRQ(ierr);
 
78
  ierr = VecDestroy(u);CHKERRQ(ierr);
 
79
  ierr = VecDestroy(ustar);CHKERRQ(ierr);
 
80
  ierr = VecDestroy(b);CHKERRQ(ierr);
 
81
  ierr = MatDestroy(mat);CHKERRQ(ierr);
 
82
  ierr = PCDestroy(pc);CHKERRQ(ierr);
 
83
  ierr = PetscFinalize();CHKERRQ(ierr);
 
84
  return 0;
 
85
}
 
86
    
 
87
 
 
88