Actual source code: ex18.c


  2: static char help[] = "Computes the integral of 2*x/(1+x^2) from x=0..1 \nThis is equal to the ln(2).\n\n";

  4: /*T
  5:    Concepts: vectors^assembling vectors;
  6:    Processors: n

  8:    Contributed by Mike McCourt <mccomic@iit.edu> and Nathan Johnston <johnnat@iit.edu>
  9: T*/

 11: /*
 12:   Include "petscvec.h" so that we can use vectors.  Note that this file
 13:   automatically includes:
 14:      petscsys.h       - base PETSc routines   petscis.h     - index sets
 15:      petscviewer.h - viewers
 16: */
 17: #include <petscvec.h>

 19: PetscScalar func(PetscScalar a)
 20: {
 21:   return (PetscScalar)2.*a/((PetscScalar)1.+a*a);
 22: }

 24: int main(int argc,char **argv)
 25: {
 26:   PetscMPIInt    rank,size;
 27:   PetscInt       rstart,rend,i,k,N,numPoints=1000000;
 28:   PetscScalar    dummy,result=0,h=1.0/numPoints,*xarray;
 29:   Vec            x,xend;

 31:   PetscInitialize(&argc,&argv,(char*)0,help);
 32:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 33:   MPI_Comm_size(PETSC_COMM_WORLD,&size);

 35:   /*
 36:      Create a parallel vector.
 37:        Here we set up our x vector which will be given values below.
 38:        The xend vector is a dummy vector to find the value of the
 39:          elements at the endpoints for use in the trapezoid rule.
 40:   */
 41:   VecCreate(PETSC_COMM_WORLD,&x);
 42:   VecSetSizes(x,PETSC_DECIDE,numPoints);
 43:   VecSetFromOptions(x);
 44:   VecGetSize(x,&N);
 45:   VecSet(x,result);
 46:   VecDuplicate(x,&xend);
 47:   result = 0.5;
 48:   if (rank == 0) {
 49:     i    = 0;
 50:     VecSetValues(xend,1,&i,&result,INSERT_VALUES);
 51:   }
 52:   if (rank == size-1) {
 53:     i    = N-1;
 54:     VecSetValues(xend,1,&i,&result,INSERT_VALUES);
 55:   }
 56:   /*
 57:      Assemble vector, using the 2-step process:
 58:        VecAssemblyBegin(), VecAssemblyEnd()
 59:      Computations can be done while messages are in transition
 60:      by placing code between these two statements.
 61:   */
 62:   VecAssemblyBegin(xend);
 63:   VecAssemblyEnd(xend);

 65:   /*
 66:      Set the x vector elements.
 67:       i*h will return 0 for i=0 and 1 for i=N-1.
 68:       The function evaluated (2x/(1+x^2)) is defined above.
 69:       Each evaluation is put into the local array of the vector without message passing.
 70:   */
 71:   VecGetOwnershipRange(x,&rstart,&rend);
 72:   VecGetArray(x,&xarray);
 73:   k    = 0;
 74:   for (i=rstart; i<rend; i++) {
 75:     xarray[k] = (PetscScalar)i*h;
 76:     xarray[k] = func(xarray[k]);
 77:     k++;
 78:   }
 79:   VecRestoreArray(x,&xarray);

 81:   /*
 82:      Evaluates the integral.  First the sum of all the points is taken.
 83:      That result is multiplied by the step size for the trapezoid rule.
 84:      Then half the value at each endpoint is subtracted,
 85:      this is part of the composite trapezoid rule.
 86:   */
 87:   VecSum(x,&result);
 88:   result = result*h;
 89:   VecDot(x,xend,&dummy);
 90:   result = result-h*dummy;

 92:   /*
 93:       Return the value of the integral.
 94:   */
 95:   PetscPrintf(PETSC_COMM_WORLD,"ln(2) is %g\n",(double)PetscRealPart(result));
 96:   VecDestroy(&x);
 97:   VecDestroy(&xend);

 99:   PetscFinalize();
100:   return 0;
101: }

103: /*TEST

105:      test:
106:        nsize: 1

108:      test:
109:        nsize: 2
110:        suffix: 2
111:        output_file: output/ex18_1.out

113: TEST*/