<font size="2"><span style="font-family: Arial;">I want to learn petsc by
solving a small time-dependent problem:<br>&nbsp;&nbsp; The problem is from
chemical kinetics, and consists of the following three rate
equations:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;
dy1/dt = -.04*y1 + 1.e4*y2*y3<br>&nbsp;&nbsp; dy2/dt = .04*y1 - 1.e4*y2*y3 -
3.e7*(y2)^2<br>&nbsp;&nbsp; dy3/dt = 3.e7*(y2)^2<br>&nbsp; on the interval from
t = 0.0 to t = 4.e10, with initial<br>&nbsp; conditions: y1 = 1.0, y2 = y3 =
0.<br><br>I passed the compiling, and generated an executable file. But I got a
lot of - Error Message - when I run the file. I appreciate your help and
suggestions! Many thanks!<br><br>My program is :<br><br>#include
"petscts.h"<br>/*The problem is from<br>&nbsp;* chemical kinetics, and consists
of the following three rate<br>&nbsp;*
equations:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<br>&nbsp;*&nbsp;&nbsp;&nbsp; dy1/dt = -.04*y1 +
1.e4*y2*y3<br>&nbsp;*&nbsp;&nbsp;&nbsp; dy2/dt = .04*y1 - 1.e4*y2*y3 -
3.e7*(y2)^2<br>&nbsp;*&nbsp;&nbsp;&nbsp; dy3/dt = 3.e7*(y2)^2<br>&nbsp;* on the
interval from t = 0.0 to t = 4.e10, with initial<br>&nbsp;* conditions: y1 =
1.0, y2 = y3 = 0.<br>&nbsp;*/<br>typedef struct {<br>&nbsp;&nbsp;&nbsp;
PetscInt m;<br>}AppCtx;<br><br>extern PetscErrorCode&nbsp;
FormJacobian(TS,PetscReal,Vec,Mat*,Mat*,MatStructure*,void*),<br>&nbsp;&nbsp;&nbsp;&nbsp; FormFunction(TS,PetscReal,Vec,Vec,void*);<br><br>int main(int argc,char **argv)<br>{<br>&nbsp;&nbsp;&nbsp; TS ts;<br>&nbsp;&nbsp;&nbsp; Vec u,r;<br>&nbsp;&nbsp;&nbsp; PetscScalar&nbsp;&nbsp;&nbsp; *u_localptr;<br>&nbsp;&nbsp;&nbsp; Mat J;<br>&nbsp;&nbsp;&nbsp; AppCtx user;<br>&nbsp;&nbsp;&nbsp; PetscInt its,m;<br>&nbsp;&nbsp;&nbsp; PetscReal dt,ftime;<br>&nbsp;&nbsp;&nbsp; PetscErrorCode ierr;<br>&nbsp;&nbsp;&nbsp; PetscInitialize(&amp;argc,&amp;argv,PETSC_NULL,PETSC_NULL);<br>&nbsp;&nbsp;&nbsp; m=3;<br>&nbsp;&nbsp;&nbsp; dt=0.1;<br>&nbsp; ierr = PetscOptionsGetInt(PETSC_NULL,"-m",&amp;m,PETSC_NULL);CHKERRQ(ierr);<br>&nbsp; user.m=m;<br>&nbsp; u_localptr[0]=1.0;<br>&nbsp; u_localptr[1]=0.0;<br>&nbsp; u_localptr[2]=0.0;<br>&nbsp; ierr = VecRestoreArray(u,&amp;u_localptr);CHKERRQ(ierr);&nbsp;&nbsp;&nbsp; <br>&nbsp; <br>&nbsp; ierr = VecCreateSeq(PETSC_COMM_SELF,m,&amp;u);CHKERRQ(ierr);<br!
 ><br>&nbsp; ierr = TSCreate(PETSC_COMM_SELF,&amp;ts);CHKERRQ(ierr);<br>&nbsp; ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr);<br>&nbsp; ierr = TSSetSolution(ts,u);CHKERRQ(ierr);<br>&nbsp; ierr = TSSetRHSFunction(ts,FormFunction,&amp;user);CHKERRQ(ierr);<br>&nbsp; ierr = TSSetRHSJacobian(ts,J,J,FormJacobian,&amp;user);CHKERRQ(ierr);<br><br>&nbsp; ierr = TSSetType(ts,TSPSEUDO);CHKERRQ(ierr);<br>&nbsp; ierr = TSSetInitialTimeStep(ts,0.0,dt);CHKERRQ(ierr);<br>&nbsp; ierr = TSPseudoSetTimeStep(ts,TSPseudoDefaultTimeStep,0);CHKERRQ(ierr);<br>&nbsp; ierr = TSSetFromOptions(ts);CHKERRQ(ierr);<br><br>&nbsp; ierr = TSSetUp(ts);CHKERRQ(ierr);<br>&nbsp; ierr = TSStep(ts,&amp;its,&amp;ftime);CHKERRQ(ierr);<br>&nbsp; <br>&nbsp; printf("Number of pseudo timesteps = %d final time %4.2e\n",(int)its,ftime);<br>&nbsp; ierr = VecDestroy(u);CHKERRQ(ierr);<br>&nbsp; ierr = VecDestroy(r);CHKERRQ(ierr);<br>&nbsp; ierr = MatDestroy(J);CHKERRQ(ierr);<br>&nbsp; ierr = TSDestroy(ts);CHKERRQ(ie!
 rr);<br>&nbsp; ierr = PetscFinalize();CHKERRQ(ierr);<br><br>&n!
 bsp; ret
urn 0;<br>}<br><br><br><br>PetscErrorCode FormFunction(TS ts,PetscReal t,Vec X,Vec F,void *ptr)<br>{<br><br>&nbsp;&nbsp;&nbsp; PetscErrorCode ierr;<br>&nbsp;&nbsp;&nbsp; PetscScalar *x,*f;<br>&nbsp;&nbsp;&nbsp; PetscInt n;<br>&nbsp;&nbsp;&nbsp; n=3;<br>&nbsp;&nbsp;&nbsp;&nbsp; ierr = VecGetArray(X,&amp;x);CHKERRQ(ierr);<br>&nbsp; ierr = VecGetArray(F,&amp;f);CHKERRQ(ierr);<br>&nbsp;&nbsp;&nbsp; f[0]=-0.04*x[0]+1.0e4*x[1]*x[2];<br>&nbsp;&nbsp;&nbsp; f[1]=0.04-1.0e4*x[1]*x[2]-3*1.0e7*x[1]*x[1];<br>&nbsp;&nbsp;&nbsp; f[2]=3*1.0e7*x[1]*x[1];<br>&nbsp; ierr = VecRestoreArray(X,&amp;x);CHKERRQ(ierr);<br>&nbsp; ierr = VecRestoreArray(F,&amp;f);CHKERRQ(ierr);<br>&nbsp; return 0;<br>&nbsp; <br>}<br>PetscErrorCode FormJacobian(TS ts,PetscReal t,Vec X,Mat *J,Mat *B,MatStructure *flag,void *ptr)<br>{<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Mat jac=*B;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PetscScalar v[3],*x;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PetscInt r!
 ow,col;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PetscErrorCode ierr;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; ierr = VecGetArray(X,&amp;x);CHKERRQ(ierr);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; v[0]=-0.04;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; v[1]=0.04;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; v[2]=0;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; row=1;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; col=1;<br>&nbsp;&nbsp;&nbsp; &nbsp; ierr = MatSetValues(jac,1,&amp;row,1,&amp;col,v,INSERT_VALUES);CHKERRQ(ierr);<br>&nbsp;&nbsp; v[0]=1.0e4*x[2];<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; v[1]=-1.0e4*x[2]-6.0e7*x[1];<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; v[2]=6.0e7*x[1];<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; col=2;<br>&nbsp;&nbsp;&nbsp; &nbsp; ierr = MatSetValues(jac,1,&amp;row,1,&amp;col,v,INSERT_VALUES);CHKERRQ(ierr);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; v[0]=1.0e4*x[1];<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; v[1]=-1.0e4*x[1];<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; v[2]=0;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; col=3;<br>&nbsp;&nbsp;&nbsp; &n!
 bsp; ierr = MatSetValues(jac,1,&amp;row,1,&amp;col,v,INSERT_VA!
 LUES);CH
KERRQ(ierr);<br>&nbsp;&nbsp;&nbsp; ierr = MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);<br>&nbsp; ierr = VecRestoreArray(X,&amp;x);CHKERRQ(ierr);<br>&nbsp; ierr = MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);<br>&nbsp; return 0;<br><br>}<br><br></span></font><br>