/* Monitor timesteps and use interpolation to output at integer multiples of 0.1 */ static PetscErrorCode Monitor(TS ts,PetscInt step,PetscReal t,Vec X,void *ctx) { PetscErrorCode ierr; const PetscScalar *x; PetscReal tfinal, dt; User user = (User)ctx; Vec interpolatedX; PetscFunctionBeginUser; ierr = TSGetTimeStep(ts,&dt);CHKERRQ(ierr); ierr = TSGetDuration(ts,NULL,&tfinal);CHKERRQ(ierr); while (user->next_output <= t && user->next_output <= tfinal) { ierr = VecDuplicate(X,&interpolatedX);CHKERRQ(ierr); ierr = TSInterpolate(ts,user->next_output,interpolatedX);CHKERRQ(ierr); ierr = VecGetArrayRead(interpolatedX,&x);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"[%.1f] %D TS %.6f (dt = %.6f) X % 12.6e % 12.6e\n", user->next_output,step,t,dt,(double)PetscRealPart(x[0]), (double)PetscRealPart(x[1]));CHKERRQ(ierr); ierr = VecRestoreArrayRead(interpolatedX,&x);CHKERRQ(ierr); ierr = VecDestroy(&interpolatedX);CHKERRQ(ierr); user->next_output += 0.1; } PetscFunctionReturn(0); }
PetscErrorCode Monitor(TS ts,PetscInt step,PetscReal time,Vec global,void *ctx) { VecScatter scatter; IS from,to; PetscInt i,n,*idx,nsteps,maxsteps; Vec tmp_vec; PetscErrorCode ierr; PetscScalar *tmp; PetscReal maxtime; Data *data = (Data*)ctx; PetscReal tfinal = data->tfinal; PetscFunctionBeginUser; if (time > tfinal) PetscFunctionReturn(0); ierr = TSGetTimeStepNumber(ts,&nsteps);CHKERRQ(ierr); /* display output at selected time steps */ ierr = TSGetDuration(ts, &maxsteps, &maxtime);CHKERRQ(ierr); if (nsteps % 10 != 0 && time < maxtime) PetscFunctionReturn(0); /* Get the size of the vector */ ierr = VecGetSize(global,&n);CHKERRQ(ierr); /* Set the index sets */ ierr = PetscMalloc1(n,&idx);CHKERRQ(ierr); for (i=0; i<n; i++) idx[i]=i; /* Create local sequential vectors */ ierr = VecCreateSeq(PETSC_COMM_SELF,n,&tmp_vec);CHKERRQ(ierr); /* Create scatter context */ ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idx,PETSC_COPY_VALUES,&from);CHKERRQ(ierr); ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idx,PETSC_COPY_VALUES,&to);CHKERRQ(ierr); ierr = VecScatterCreate(global,from,tmp_vec,to,&scatter);CHKERRQ(ierr); ierr = VecScatterBegin(scatter,global,tmp_vec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterEnd(scatter,global,tmp_vec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecGetArray(tmp_vec,&tmp);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"At t[%D] =%14.2e u= %14.2e at the center \n",nsteps,(double)time,(double)PetscRealPart(tmp[n/2]));CHKERRQ(ierr); ierr = VecRestoreArray(tmp_vec,&tmp);CHKERRQ(ierr); ierr = PetscFree(idx);CHKERRQ(ierr); ierr = ISDestroy(&from);CHKERRQ(ierr); ierr = ISDestroy(&to);CHKERRQ(ierr); ierr = VecScatterDestroy(&scatter);CHKERRQ(ierr); ierr = VecDestroy(&tmp_vec);CHKERRQ(ierr); PetscFunctionReturn(0); }
/* Monitor timesteps and use interpolation to output at integer multiples of 0.1 */ static PetscErrorCode Monitor(TS ts,PetscInt step,PetscReal t,Vec X,void *ctx) { PetscErrorCode ierr; const PetscScalar *x; PetscReal tfinal, dt, tprev; User user = (User)ctx; PetscFunctionBeginUser; ierr = TSGetTimeStep(ts,&dt);CHKERRQ(ierr); ierr = TSGetDuration(ts,NULL,&tfinal);CHKERRQ(ierr); ierr = TSGetPrevTime(ts,&tprev);CHKERRQ(ierr); ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"[%.1f] %D TS %.6f (dt = %.6f) X % 12.6e % 12.6e\n",(double)user->next_output,step,(double)t,(double)dt,(double)PetscRealPart(x[0]),(double)PetscRealPart(x[1]));CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"t %.6f (tprev = %.6f) \n",(double)t,(double)tprev);CHKERRQ(ierr); ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr); PetscFunctionReturn(0); }
void PETSC_STDCALL tsgetduration_(TS ts,PetscInt *maxsteps,PetscReal *maxtime, int *__ierr ){ *__ierr = TSGetDuration( (TS)PetscToPointer((ts) ),maxsteps,maxtime); }