/* Formatted test for TS routines. Solves U_t=F(t,u) Where: [2*u1+u2 F(t,u)= [u1+2*u2+u3 [ u2+2*u3 We can compare the solutions from euler, beuler and SUNDIALS to see what is the difference. */ static char help[] = "Solves a nonlinear ODE. \n\n"; #include "petscsys.h" #include "petscts.h" #include "petscpc.h" extern PetscErrorCode RHSFunction(TS,PetscReal,Vec,Vec,void*); extern PetscErrorCode RHSJacobian(TS,PetscReal,Vec,Mat*,Mat*,MatStructure *,void*); extern PetscErrorCode Monitor(TS,PetscInt,PetscReal,Vec,void *); extern PetscErrorCode Initial(Vec,void *); extern PetscReal solx(PetscReal); extern PetscReal soly(PetscReal); extern PetscReal solz(PetscReal); #undef __FUNCT__ #define __FUNCT__ "main" int main(int argc,char **argv) { PetscErrorCode ierr; PetscInt time_steps = 100,steps; PetscMPIInt size; Vec global; PetscReal dt,ftime; TS ts; MatStructure A_structure; Mat A = 0; ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-time",&time_steps,PETSC_NULL);CHKERRQ(ierr); /* set initial conditions */ ierr = VecCreate(PETSC_COMM_WORLD,&global);CHKERRQ(ierr); ierr = VecSetSizes(global,PETSC_DECIDE,3);CHKERRQ(ierr); ierr = VecSetFromOptions(global);CHKERRQ(ierr); ierr = Initial(global,PETSC_NULL);CHKERRQ(ierr); /* make timestep context */ ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr); ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr); ierr = TSMonitorSet(ts,Monitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); dt = 0.1; /* The user provides the RHS and Jacobian */ ierr = TSSetRHSFunction(ts,RHSFunction,NULL);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,3,3);CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); ierr = RHSJacobian(ts,0.0,global,&A,&A,&A_structure,NULL);CHKERRQ(ierr); ierr = TSSetRHSJacobian(ts,A,A,RHSJacobian,NULL);CHKERRQ(ierr); ierr = TSSetFromOptions(ts);CHKERRQ(ierr); ierr = TSSetInitialTimeStep(ts,0.0,dt);CHKERRQ(ierr); ierr = TSSetDuration(ts,time_steps,1);CHKERRQ(ierr); ierr = TSSetSolution(ts,global);CHKERRQ(ierr); ierr = TSSetUp(ts);CHKERRQ(ierr); ierr = TSStep(ts,&steps,&ftime);CHKERRQ(ierr); /* free the memories */ ierr = TSDestroy(ts);CHKERRQ(ierr); ierr = VecDestroy(global);CHKERRQ(ierr); ierr= MatDestroy(A);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; } /* -------------------------------------------------------------------*/ #undef __FUNCT__ #define __FUNCT__ "Initial" /* this test problem has initial values (1,1,1). */ PetscErrorCode Initial(Vec global,void *ctx) { PetscScalar *localptr; PetscInt i,mybase,myend,locsize; PetscErrorCode ierr; /* determine starting point of each processor */ ierr = VecGetOwnershipRange(global,&mybase,&myend);CHKERRQ(ierr); ierr = VecGetLocalSize(global,&locsize);CHKERRQ(ierr); /* Initialize the array */ ierr = VecGetArray(global,&localptr);CHKERRQ(ierr); for (i=0; i