<font size="2"><span style="font-family: Arial;">I got my program to run. But
the results doesn't make any sence.<br><br>At t
= 0 y
=1.000000e+00 0.000000e+00 0.000000e+00, <br>At t
= 0.01 y =9.996000e-01
4.000000e-04 0.000000e+00, <br>At t =
0.02 y =9.992002e-01 -4.720016e-02 4.800000e-02, <br>At t
= 0.03 y =7.722397e-01
-6.681768e+02 6.684045e+02, <br>At t
= 0.04 y =-4.466124e+07
-1.338934e+11 1.339381e+11, <br>At t
= 0.05 y =-1.793342e+24
-5.376439e+27 5.378233e+27, <br>At t
= 0.06 y =-2.891574e+57
-8.668938e+60 8.671830e+60, <br>At t
= 0.07 y =-7.517556e+123
-2.253763e+127 2.254515e+127, <br>At t
= 0.08 y =-5.081142e+256
-1.523326e+260 1.523834e+260, <br>At t
= 0.09 y =-inf nan inf,
<br>At t = 0.1 y =nan
nan nan, <br>At t = 0.11 y
=nan nan nan, <br>At t =
0.12 y =nan nan nan, <br>At t
= 0.13 y =nan nan nan,
<br>At t = 0.14 y =nan
nan nan, <br><br>Would you please help me check what's wrong with my
code?<br><br>#include "petscts.h"<br><br>/*The problem is from<br> *
chemical kinetics, and consists of the following three rate<br> *
equations:
<br> * dy1/dt = -.04*y1 +
1.e4*y2*y3<br> * dy2/dt = .04*y1 - 1.e4*y2*y3 -
3.e7*(y2)^2<br> * dy3/dt = 3.e7*(y2)^2<br> * on the
interval from t = 0.0 to t = 4.e10, with initial<br> * conditions: y1 =
1.0, y2 = y3 = 0.*/<br><br><br>typedef struct {<br> PetscInt
m;<br>}AppCtx;<br><br>extern PetscErrorCode
FormJacobian(TS,PetscReal,Vec,Mat*,Mat*,MatStructure*,void*),Monitor(TS
ts,PetscInt step,PetscReal time,Vec u,void*ctx),<br>
FormFunction(TS,PetscReal,Vec,Vec,void*);<br><br>int main(int argc,char
**argv)<br>{<br> TS ts;<br> Vec
u;<br> PetscScalar
*u_localptr;<br> Mat J;<br> AppCtx
user;<br> PetscInt its,m;<br> PetscReal
dt,ftime;<br> PetscErrorCode ierr;<br>
PetscInitialize(&argc,&argv,PETSC_NULL,PETSC_NULL);<br> m=3;<br> dt=0.01;<br> ierr = PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);CHKERRQ(ierr);<br> user.m=m;<br> ierr=VecCreateSeq(PETSC_COMM_SELF,m,&u);CHKERRQ(ierr);<br> ierr=VecGetArray(u,&u_localptr);CHKERRQ(ierr);<br> u_localptr[0]=1.0;<br> u_localptr[1]=0.0;<br> u_localptr[2]=0.0;<br> ierr = VecRestoreArray(u,&u_localptr);CHKERRQ(ierr); <br> ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr);<br> ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr);<br> ierr = TSMonitorSet(ts,Monitor,&user,PETSC_NULL);<br> ierr = TSSetSolution(ts,u);CHKERRQ(ierr);<br> ierr = TSSetRHSFunction(ts,FormFunction,&user);CHKERRQ(ierr);<br> ierr = MatCreate(PETSC_COMM_SELF,&J);CHKERRQ(ierr);<br> ierr = MatSetSizes(J,PETSC_DECIDE,PETSC_DECIDE,m,m);CHKERRQ(i!
err);<br> ierr = MatSetFromOptions(J);<br> ierr = TSSetRHSJacobian(ts,J,J,FormJacobian,&user);CHKERRQ(ierr);<br> ierr = TSSetInitialTimeStep(ts,0.0,dt);CHKERRQ(ierr);<br> ierr = TSSetDuration(ts,1000,8.e-1);<br> ierr = TSSetFromOptions(ts);CHKERRQ(ierr);<br><br> ierr = TSSetUp(ts);CHKERRQ(ierr);<br> ierr = TSStep(ts,&its,&ftime);CHKERRQ(ierr);<br> <br> printf("Number of timesteps = %d final time %4.2e\n",(int)its,ftime);<br> ierr = VecDestroy(u);CHKERRQ(ierr);<br> ierr = MatDestroy(J);CHKERRQ(ierr);<br> ierr = TSDestroy(ts);CHKERRQ(ierr);<br> ierr = PetscFinalize();CHKERRQ(ierr);<br><br> return 0;<br>}<br><br><br><br>PetscErrorCode FormFunction(TS ts,PetscReal t,Vec X,Vec F,void *ptr)<br>{ <br> PetscErrorCode ierr;<br> PetscScalar *x,*f;<br> PetscInt n;<br> n=3;<br> ierr = VecGetArray(X,&x);CHKERRQ(ierr);<br> ierr = VecGetArray(F,&f);CH!
KERRQ(ierr);<br> f[0!
]=-0.04*
x[0]+1.0e4*x[1]*x[2];<br> f[1]=0.04*x[0]-1.0e4*x[1]*x[2]-3*1.0e7*x[1]*x[1];<br> f[2]=3*1.0e7*x[1]*x[1];<br> ierr = VecRestoreArray(X,&x);CHKERRQ(ierr);<br> ierr = VecRestoreArray(F,&f);CHKERRQ(ierr);<br> return 0;<br> <br>}<br>PetscErrorCode FormJacobian(TS ts,PetscReal t,Vec X,Mat *J,Mat *B,MatStructure *flag,void *ptr)<br>{<br> Mat jac=*J;<br> PetscScalar v[3],*x;<br> PetscInt row,col;<br> PetscErrorCode ierr;<br> ierr = VecGetArray(X,&x);CHKERRQ(ierr);<br> v[0]=-0.04;<br> v[1]=0.04;<br> v[2]=0.0;<br> row=0;<br> col=0;<br> ierr = Ma!
tSetValues(jac,3,&row,1,&col,v,INSERT_VALUES);CHKERRQ(ierr);<br> v[0]=1.0e4*x[2];<br> v[1]=-1.0e4*x[2]-6.0e7*x[1];<br> v[2]=6.0e7*x[1];<br> col=1;<br> ierr = MatSetValues(jac,3,&row,1,&col,v,INSERT_VALUES);CHKERRQ(ierr);<br> v[0]=1.0e4*x[1];<br> v[1]=-1.0e4*x[1];<br> v[2]=0.0;<br> col=2;<br> ierr = MatSetValues(jac,3,&row,1,&col,v,INSERT_VALUES);CHKERRQ(ierr);<br> ierr = MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);<br> ierr = VecRestoreArray(X,&x);CHKERRQ(ierr);<br> ierr = MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);<br> return 0;<br><br>}<br>PetscErrorCode Monitor(TS ts!
,PetscInt step,PetscReal time,Vec u, void *ctx)<br>{<br> !
PetscSc
alar *y;<br> <br> VecGetArray(u,&y);<br> PetscPrintf(PETSC_COMM_WORLD,"At t =%11g y =%e %e %e, \n",time,y[0],y[1],y[2]);<br> VecRestoreArray(u,&y);<br> return 0;<br>}<br><br><br></span></font>