Actual source code: ex12.c
1: /*$Id: ex12.c,v 1.41 2001/08/07 21:31:49 bsmith Exp $*/
3: /*
4: Simple example to show how PETSc programs can be run from Matlab.
5: See ex12.m.
6: */
8: static char help[] = "Solves the one dimensional heat equation.\n\n";
10: #include petscda.h
11: #include petscsys.h
15: int main(int argc,char **argv)
16: {
17: int rank,size,M = 14,ierr,time_steps = 20,w=1,s=1;
18: DA da;
19: PetscViewer viewer;
20: Vec local,global,copy;
21: PetscScalar *localptr,*copyptr;
22: PetscReal h,k;
23: int localsize,j,i,mybase,myend;
24:
25: PetscInitialize(&argc,&argv,(char*)0,help);
27: PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);
28: PetscOptionsGetInt(PETSC_NULL,"-time",&time_steps,PETSC_NULL);
29:
30: /* Set up the array */
31: DACreate1d(PETSC_COMM_WORLD,DA_NONPERIODIC,M,w,s,PETSC_NULL,&da);
32: DACreateGlobalVector(da,&global);
33: DACreateLocalVector(da,&local);
34: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
35: MPI_Comm_size(PETSC_COMM_WORLD,&size);
37: /* Make copy of local array for doing updates */
38: VecDuplicate(local,©);
39: VecGetArray (copy,©ptr);
41: /* Set Up Display to Show Heat Graph */
42: viewer = PETSC_VIEWER_SOCKET_WORLD;
44: /* determine starting point of each processor */
45: VecGetOwnershipRange(global,&mybase,&myend);
47: /* Initialize the Array */
48: VecGetLocalSize (local,&localsize);
49: VecGetArray (local,&localptr);
50: localptr[0] = copyptr[0] = 0.0;
51: localptr[localsize-1] = copyptr[localsize-1] = 1.0;
52: for (i=1; i<localsize-1; i++) {
53: j=(i-1)+mybase;
54: localptr[i] = sin((PETSC_PI*j*6)/((PetscReal)M)
55: + 1.2 * sin((PETSC_PI*j*2)/((PetscReal)M))) * 4+4;
56: }
58: VecRestoreArray (copy,©ptr);
59: VecRestoreArray(local,&localptr);
60: DALocalToGlobal(da,local,INSERT_VALUES,global);
62: /* Assign Parameters */
63: h= 1.0/M;
64: k= h*h/2.2;
66: for (j=0; j<time_steps; j++) {
68: /* Global to Local */
69: DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);
70: DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);
72: /*Extract local array */
73: VecGetArray(local,&localptr);
74: VecGetArray (copy,©ptr);
76: /* Update Locally - Make array of new values */
77: /* Note: I don't do anything for the first and last entry */
78: for (i=1; i< localsize-1; i++) {
79: copyptr[i] = localptr[i] + (k/(h*h)) *
80: (localptr[i+1]-2.0*localptr[i]+localptr[i-1]);
81: }
82:
83: VecRestoreArray (copy,©ptr);
84: VecRestoreArray(local,&localptr);
86: /* Local to Global */
87: DALocalToGlobal(da,copy,INSERT_VALUES,global);
88:
89: /* View Wave */
90: VecView(global,viewer);
92: }
94: VecDestroy(copy);
95: VecDestroy(local);
96: VecDestroy(global);
97: DADestroy(da);
98: PetscFinalize();
99: return 0;
100: }
101: