本文整理汇总了C++中TSDestroy函数的典型用法代码示例。如果您正苦于以下问题:C++ TSDestroy函数的具体用法?C++ TSDestroy怎么用?C++ TSDestroy使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了TSDestroy函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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(NULL,"-time",&time_steps,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,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,NULL,NULL);CHKERRQ(ierr);
dt = 0.1;
/*
The user provides the RHS and Jacobian
*/
ierr = TSSetRHSFunction(ts,NULL,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 = MatSetUp(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 = TSSolve(ts,global);CHKERRQ(ierr);
ierr = TSGetSolveTime(ts,&ftime);CHKERRQ(ierr);
ierr = TSGetTimeStepNumber(ts,&steps);CHKERRQ(ierr);
/* free the memories */
ierr = TSDestroy(&ts);CHKERRQ(ierr);
ierr = VecDestroy(&global);CHKERRQ(ierr);
ierr = MatDestroy(&A);CHKERRQ(ierr);
ierr = PetscFinalize();
return 0;
}
示例2: main
int main(int argc,char **argv)
{
PetscErrorCode ierr;
AppCtx ctx;
TS ts;
Vec tsrhs,U;
ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr);
ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr);
ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
ierr = VecCreateMPI(PETSC_COMM_WORLD,PETSC_DECIDE,1,&tsrhs);CHKERRQ(ierr);
ierr = VecCreateMPI(PETSC_COMM_WORLD,PETSC_DECIDE,1,&U);CHKERRQ(ierr);
ierr = TSSetRHSFunction(ts,tsrhs,TSFunction,&ctx);CHKERRQ(ierr);
ctx.f = f;
ierr = SNESCreate(PETSC_COMM_WORLD,&ctx.snes);CHKERRQ(ierr);
ierr = SNESSetFromOptions(ctx.snes);CHKERRQ(ierr);
ierr = SNESSetFunction(ctx.snes,NULL,SNESFunction,&ctx);CHKERRQ(ierr);
ierr = SNESSetJacobian(ctx.snes,NULL,NULL,SNESComputeJacobianDefault,&ctx);CHKERRQ(ierr);
ctx.F = F;
ierr = VecCreateMPI(PETSC_COMM_WORLD,PETSC_DECIDE,1,&ctx.V);CHKERRQ(ierr);
ierr = VecSet(U,1.0);CHKERRQ(ierr);
ierr = TSSolve(ts,U);CHKERRQ(ierr);
ierr = VecDestroy(&ctx.V);CHKERRQ(ierr);
ierr = VecDestroy(&tsrhs);CHKERRQ(ierr);
ierr = VecDestroy(&U);CHKERRQ(ierr);
ierr = SNESDestroy(&ctx.snes);CHKERRQ(ierr);
ierr = TSDestroy(&ts);CHKERRQ(ierr);
ierr = PetscFinalize();
return ierr;
}
示例3: PETScExternalSolverDestroy
PetscErrorCode
PETScExternalSolverDestroy(TS ts)
{
PetscErrorCode ierr;
PetscFunctionBeginUser;
ierr = TSDestroy(&ts);
CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例4: TaoSetObjectiveAndGradientRoutine
/*
FormFunctionGradient - Evaluates the function and corresponding gradient.
Input Parameters:
tao - the Tao context
X - the input vector
ptr - optional user-defined context, as set by TaoSetObjectiveAndGradientRoutine()
Output Parameters:
f - the newly evaluated function
G - the newly evaluated gradient
*/
PetscErrorCode FormFunctionGradient(Tao tao,Vec IC,PetscReal *f,Vec G,void *ctx)
{
User user_ptr = (User)ctx;
TS ts;
PetscScalar *x_ptr,*y_ptr;
PetscErrorCode ierr;
ierr = VecCopy(IC,user_ptr->x);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create timestepping solver context
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr);
ierr = TSSetType(ts,TSCN);CHKERRQ(ierr);
ierr = TSSetIFunction(ts,NULL,IFunction,user_ptr);CHKERRQ(ierr);
ierr = TSSetIJacobian(ts,user_ptr->A,user_ptr->A,IJacobian,user_ptr);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Set time
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = TSSetTime(ts,0.0);CHKERRQ(ierr);
ierr = TSSetDuration(ts,PETSC_DEFAULT,0.5);CHKERRQ(ierr);
ierr = TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Save trajectory of solution so that TSAdjointSolve() may be used
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Set runtime options
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
ierr = TSSolve(ts,user_ptr->x);CHKERRQ(ierr);
ierr = VecGetArray(user_ptr->x,&x_ptr);CHKERRQ(ierr);
*f = (x_ptr[0]-user_ptr->x_ob[0])*(x_ptr[0]-user_ptr->x_ob[0])+(x_ptr[1]-user_ptr->x_ob[1])*(x_ptr[1]-user_ptr->x_ob[1]);
ierr = PetscPrintf(PETSC_COMM_WORLD,"Observed value y_ob=[%f; %f], ODE solution y=[%f;%f], Cost function f=%f\n",(double)user_ptr->x_ob[0],(double)user_ptr->x_ob[1],(double)x_ptr[0],(double)x_ptr[1],(double)(*f));CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Adjoint model starts here
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Redet initial conditions for the adjoint integration */
ierr = VecGetArray(user_ptr->lambda[0],&y_ptr);CHKERRQ(ierr);
y_ptr[0] = 2.*(x_ptr[0]-user_ptr->x_ob[0]);
y_ptr[1] = 2.*(x_ptr[1]-user_ptr->x_ob[1]);
ierr = VecRestoreArray(user_ptr->lambda[0],&y_ptr);CHKERRQ(ierr);
ierr = TSSetCostGradients(ts,1,user_ptr->lambda,NULL);CHKERRQ(ierr);
ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
ierr = VecCopy(user_ptr->lambda[0],G);
ierr = TSDestroy(&ts);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例5: RunTest
PetscErrorCode RunTest(int nx, int ny, int nz, int loops, double *wt)
{
Vec x,f;
TS ts;
AppCtx _app,*app=&_app;
double t1,t2;
PetscErrorCode ierr;
PetscFunctionBegin;
app->nx = nx; app->h[0] = 1./(nx-1);
app->ny = ny; app->h[1] = 1./(ny-1);
app->nz = nz; app->h[2] = 1./(nz-1);
ierr = VecCreate(PETSC_COMM_SELF,&x);CHKERRQ(ierr);
ierr = VecSetSizes(x,nx*ny*nz,nx*ny*nz);CHKERRQ(ierr);
ierr = VecSetUp(x);CHKERRQ(ierr);
ierr = VecDuplicate(x,&f);CHKERRQ(ierr);
ierr = TSCreate(PETSC_COMM_SELF,&ts);CHKERRQ(ierr);
ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr);
ierr = TSSetType(ts,TSTHETA);CHKERRQ(ierr);
ierr = TSThetaSetTheta(ts,1.0);CHKERRQ(ierr);
ierr = TSSetTimeStep(ts,0.01);CHKERRQ(ierr);
ierr = TSSetTime(ts,0.0);CHKERRQ(ierr);
ierr = TSSetDuration(ts,10,1.0);CHKERRQ(ierr);
ierr = TSSetSolution(ts,x);CHKERRQ(ierr);
ierr = TSSetIFunction(ts,f,FormFunction,app);CHKERRQ(ierr);
ierr = PetscOptionsSetValue("-snes_mf","1");CHKERRQ(ierr);
{
SNES snes;
KSP ksp;
ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
ierr = KSPSetType(ksp,KSPCG);CHKERRQ(ierr);
}
ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
ierr = TSSetUp(ts);CHKERRQ(ierr);
*wt = 1e300;
while (loops-- > 0) {
ierr = FormInitial(0.0,x,app);CHKERRQ(ierr);
ierr = PetscGetTime(&t1);CHKERRQ(ierr);
ierr = TSSolve(ts,x,PETSC_NULL);CHKERRQ(ierr);
ierr = PetscGetTime(&t2);CHKERRQ(ierr);
*wt = PetscMin(*wt,t2-t1);
}
ierr = VecDestroy(&x);CHKERRQ(ierr);
ierr = VecDestroy(&f);CHKERRQ(ierr);
ierr = TSDestroy(&ts);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例6: main
int main(int argc,char **argv)
{
PetscErrorCode ierr;
AppCtx ctx;
TS ts;
Vec tsrhs,U;
IS is;
PetscInt I;
PetscMPIInt rank;
ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr);
ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr);
ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
ierr = VecCreateMPI(PETSC_COMM_WORLD,1,PETSC_DETERMINE,&tsrhs);CHKERRQ(ierr);
ierr = VecCreateMPI(PETSC_COMM_WORLD,1,PETSC_DETERMINE,&U);CHKERRQ(ierr);
ierr = TSSetRHSFunction(ts,tsrhs,TSFunction,&ctx);CHKERRQ(ierr);
ctx.f = f;
ierr = SNESCreate(PETSC_COMM_WORLD,&ctx.snes);CHKERRQ(ierr);
ierr = SNESSetFromOptions(ctx.snes);CHKERRQ(ierr);
ierr = SNESSetFunction(ctx.snes,NULL,SNESFunction,&ctx);CHKERRQ(ierr);
ierr = SNESSetJacobian(ctx.snes,NULL,NULL,SNESComputeJacobianDefault,&ctx);CHKERRQ(ierr);
ctx.F = F;
ierr = VecCreateMPI(PETSC_COMM_WORLD,1,PETSC_DETERMINE,&ctx.V);CHKERRQ(ierr);
/* Create scatters to move between separate U and V representation and UV representation of solution */
ierr = VecCreateMPI(PETSC_COMM_WORLD,2,PETSC_DETERMINE,&ctx.UV);CHKERRQ(ierr);
I = 2*rank;
ierr = ISCreateGeneral(PETSC_COMM_WORLD,1,&I,PETSC_COPY_VALUES,&is);CHKERRQ(ierr);
ierr = VecScatterCreateWithData(U,NULL,ctx.UV,is,&ctx.scatterU);CHKERRQ(ierr);
ierr = ISDestroy(&is);CHKERRQ(ierr);
I = 2*rank + 1;
ierr = ISCreateGeneral(PETSC_COMM_WORLD,1,&I,PETSC_COPY_VALUES,&is);CHKERRQ(ierr);
ierr = VecScatterCreateWithData(ctx.V,NULL,ctx.UV,is,&ctx.scatterV);CHKERRQ(ierr);
ierr = ISDestroy(&is);CHKERRQ(ierr);
ierr = VecSet(U,1.0);CHKERRQ(ierr);
ierr = TSSolve(ts,U);CHKERRQ(ierr);
ierr = VecDestroy(&ctx.V);CHKERRQ(ierr);
ierr = VecDestroy(&ctx.UV);CHKERRQ(ierr);
ierr = VecScatterDestroy(&ctx.scatterU);CHKERRQ(ierr);
ierr = VecScatterDestroy(&ctx.scatterV);CHKERRQ(ierr);
ierr = VecDestroy(&tsrhs);CHKERRQ(ierr);
ierr = VecDestroy(&U);CHKERRQ(ierr);
ierr = SNESDestroy(&ctx.snes);CHKERRQ(ierr);
ierr = TSDestroy(&ts);CHKERRQ(ierr);
ierr = PetscFinalize();
return ierr;
}
示例7: main
int main(int argc, char **argv)
{
PetscErrorCode ierr;
Vec x; /* Solution vector */
TS ts; /* Time-stepping context */
AppCtx user; /* Application context */
Mat J;
PetscViewer viewer;
PetscInitialize(&argc,&argv,"petscopt_ex6", help);
/* Get physics and time parameters */
ierr = Parameter_settings(&user);CHKERRQ(ierr);
/* Create a 2D DA with dof = 1 */
ierr = DMDACreate2d(PETSC_COMM_WORLD,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_STENCIL_STAR,-4,-4,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&user.da);CHKERRQ(ierr);
/* Set x and y coordinates */
ierr = DMDASetUniformCoordinates(user.da,user.xmin,user.xmax,user.ymin,user.ymax,NULL,NULL);CHKERRQ(ierr);
/* Get global vector x from DM */
ierr = DMCreateGlobalVector(user.da,&x);CHKERRQ(ierr);
ierr = ini_bou(x,&user);CHKERRQ(ierr);
ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"ini_x",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
ierr = VecView(x,viewer);CHKERRQ(ierr);
ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
/* Get Jacobian matrix structure from the da */
ierr = DMSetMatType(user.da,MATAIJ);CHKERRQ(ierr);
ierr = DMCreateMatrix(user.da,&J);CHKERRQ(ierr);
ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr);
ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr);
ierr = TSSetIFunction(ts,NULL,IFunction,&user);CHKERRQ(ierr);
ierr = TSSetIJacobian(ts,J,J,IJacobian,&user);CHKERRQ(ierr);
ierr = TSSetApplicationContext(ts,&user);CHKERRQ(ierr);
ierr = TSSetDuration(ts,PETSC_DEFAULT,user.tmax);CHKERRQ(ierr);
ierr = TSSetInitialTimeStep(ts,user.t0,.005);CHKERRQ(ierr);
ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
ierr = TSSetPostStep(ts,PostStep);CHKERRQ(ierr);
ierr = TSSolve(ts,x);CHKERRQ(ierr);
ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"fin_x",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
ierr = VecView(x,viewer);CHKERRQ(ierr);
ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
ierr = VecDestroy(&x);CHKERRQ(ierr);
ierr = MatDestroy(&J);CHKERRQ(ierr);
ierr = DMDestroy(&user.da);CHKERRQ(ierr);
ierr = TSDestroy(&ts);CHKERRQ(ierr);
PetscFinalize();
return 0;
}
示例8: main
int main(int argc,char **argv)
{
PetscErrorCode ierr;
AppCtx ctx;
TS ts;
Vec tsrhs,UV;
IS is;
PetscInt I;
PetscMPIInt rank;
PetscInitialize(&argc,&argv,(char*)0,help);
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr);
ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr);
ierr = TSSetType(ts,TSROSW);CHKERRQ(ierr);
ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
ierr = VecCreateMPI(PETSC_COMM_WORLD,2,PETSC_DETERMINE,&tsrhs);CHKERRQ(ierr);
ierr = VecCreateMPI(PETSC_COMM_WORLD,2,PETSC_DETERMINE,&UV);CHKERRQ(ierr);
ierr = TSSetRHSFunction(ts,tsrhs,TSFunctionRHS,&ctx);CHKERRQ(ierr);
ierr = TSSetIFunction(ts,NULL,TSFunctionI,&ctx);CHKERRQ(ierr);
ctx.f = f;
ctx.F = F;
ierr = VecCreateMPI(PETSC_COMM_WORLD,1,PETSC_DETERMINE,&ctx.U);CHKERRQ(ierr);
ierr = VecCreateMPI(PETSC_COMM_WORLD,1,PETSC_DETERMINE,&ctx.V);CHKERRQ(ierr);
ierr = VecCreateMPI(PETSC_COMM_WORLD,1,PETSC_DETERMINE,&ctx.UF);CHKERRQ(ierr);
ierr = VecCreateMPI(PETSC_COMM_WORLD,1,PETSC_DETERMINE,&ctx.VF);CHKERRQ(ierr);
I = 2*rank;
ierr = ISCreateGeneral(PETSC_COMM_WORLD,1,&I,PETSC_COPY_VALUES,&is);CHKERRQ(ierr);
ierr = VecScatterCreate(ctx.U,NULL,UV,is,&ctx.scatterU);CHKERRQ(ierr);
ierr = ISDestroy(&is);CHKERRQ(ierr);
I = 2*rank + 1;
ierr = ISCreateGeneral(PETSC_COMM_WORLD,1,&I,PETSC_COPY_VALUES,&is);CHKERRQ(ierr);
ierr = VecScatterCreate(ctx.V,NULL,UV,is,&ctx.scatterV);CHKERRQ(ierr);
ierr = ISDestroy(&is);CHKERRQ(ierr);
ierr = VecSet(UV,1.0);CHKERRQ(ierr);
ierr = TSSolve(ts,UV);CHKERRQ(ierr);
ierr = VecDestroy(&tsrhs);CHKERRQ(ierr);
ierr = VecDestroy(&UV);CHKERRQ(ierr);
ierr = VecDestroy(&ctx.U);CHKERRQ(ierr);
ierr = VecDestroy(&ctx.V);CHKERRQ(ierr);
ierr = VecDestroy(&ctx.UF);CHKERRQ(ierr);
ierr = VecDestroy(&ctx.VF);CHKERRQ(ierr);
ierr = VecScatterDestroy(&ctx.scatterU);CHKERRQ(ierr);
ierr = VecScatterDestroy(&ctx.scatterV);CHKERRQ(ierr);
ierr = TSDestroy(&ts);CHKERRQ(ierr);
PetscFinalize();
return 0;
}
示例9: main
int main(int argc, char **argv)
{
PetscErrorCode ierr;
Vec x; /* Solution vector */
TS ts; /* Time-stepping context */
AppCtx user; /* Application context */
PetscViewer viewer;
PetscInitialize(&argc,&argv,"petscopt_ex7", help);
/* Get physics and time parameters */
ierr = Parameter_settings(&user);CHKERRQ(ierr);
/* Create a 2D DA with dof = 1 */
ierr = DMDACreate2d(PETSC_COMM_WORLD,user.bx,user.by,DMDA_STENCIL_STAR,-4,-4,PETSC_DECIDE,PETSC_DECIDE,1,user.st_width,NULL,NULL,&user.da);CHKERRQ(ierr);
/* Set x and y coordinates */
ierr = DMDASetUniformCoordinates(user.da,user.xmin,user.xmax,user.ymin,user.ymax,0,0);CHKERRQ(ierr);
ierr = DMDASetCoordinateName(user.da,0,"X - the angle");
ierr = DMDASetCoordinateName(user.da,1,"Y - the speed");
/* Get global vector x from DM */
ierr = DMCreateGlobalVector(user.da,&x);CHKERRQ(ierr);
ierr = ini_bou(x,&user);CHKERRQ(ierr);
ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"ini_x",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
ierr = VecView(x,viewer);CHKERRQ(ierr);
ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr);
ierr = TSSetDM(ts,user.da);CHKERRQ(ierr);
ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr);
ierr = TSSetType(ts,TSARKIMEX);CHKERRQ(ierr);
ierr = TSSetIFunction(ts,NULL,IFunction,&user);CHKERRQ(ierr);
/* ierr = TSSetIJacobian(ts,NULL,NULL,IJacobian,&user);CHKERRQ(ierr); */
ierr = TSSetApplicationContext(ts,&user);CHKERRQ(ierr);
ierr = TSSetInitialTimeStep(ts,0.0,.005);CHKERRQ(ierr);
ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
ierr = TSSetPostStep(ts,PostStep);CHKERRQ(ierr);
ierr = TSSolve(ts,x);CHKERRQ(ierr);
ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"fin_x",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
ierr = VecView(x,viewer);CHKERRQ(ierr);
ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
ierr = VecDestroy(&x);CHKERRQ(ierr);
ierr = DMDestroy(&user.da);CHKERRQ(ierr);
ierr = TSDestroy(&ts);CHKERRQ(ierr);
PetscFinalize();
return 0;
}
示例10: main
int main(int argc,char **argv)
{
TS ts;
SNES snes;
SNESLineSearch linesearch;
Vec x;
AppCtx ctx;
PetscErrorCode ierr;
DM da;
ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
ierr = SetFromOptions(&ctx);CHKERRQ(ierr);
ierr = TSCreate(PETSC_COMM_WORLD, &ts);CHKERRQ(ierr);
ierr = TSSetType(ts,TSCN);CHKERRQ(ierr);
ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr);
ierr = TSSetIFunction(ts, NULL, FormIFunction, &ctx);CHKERRQ(ierr);
ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,-4,-4,PETSC_DECIDE,PETSC_DECIDE,N_SPECIES,1,NULL,NULL,&da);CHKERRQ(ierr);
ierr = DMDASetUniformCoordinates(da, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0);CHKERRQ(ierr);
ierr = DMDASetFieldName(da,0,"species A");CHKERRQ(ierr);
ierr = DMDASetFieldName(da,1,"species B");CHKERRQ(ierr);
ierr = DMDASetFieldName(da,2,"species C");CHKERRQ(ierr);
ierr = DMSetApplicationContext(da,&ctx);CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&x);CHKERRQ(ierr);
ierr = FormInitialGuess(da, &ctx, x);CHKERRQ(ierr);
ierr = TSSetDM(ts, da);CHKERRQ(ierr);
ierr = TSSetDuration(ts,10000,1000.0);CHKERRQ(ierr);
ierr = TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER);CHKERRQ(ierr);
ierr = TSSetInitialTimeStep(ts,0.0,1.0);CHKERRQ(ierr);
ierr = TSSetSolution(ts,x);CHKERRQ(ierr);
ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
ierr = SNESGetLineSearch(snes,&linesearch);CHKERRQ(ierr);
ierr = SNESLineSearchSetPostCheck(linesearch, ReactingFlowPostCheck, (void*)&ctx);CHKERRQ(ierr);
ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
ierr = TSSolve(ts,x);CHKERRQ(ierr);
ierr = VecDestroy(&x);CHKERRQ(ierr);
ierr = TSDestroy(&ts);CHKERRQ(ierr);
ierr = DMDestroy(&da);CHKERRQ(ierr);
ierr = PetscFinalize();
PetscFunctionReturn(0);
}
示例11: main
int main(int argc,char **argv)
{
TS ts;
Vec x,c;
PetscErrorCode ierr;
DM da;
PetscInitialize(&argc,&argv,(char *)0,help);
ierr = TSCreate(PETSC_COMM_WORLD, &ts);CHKERRQ(ierr);
ierr = TSSetType(ts,TSCN);CHKERRQ(ierr);
ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr);
ierr = DMDACreate2d(PETSC_COMM_WORLD, DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_NONE,DMDA_STENCIL_STAR,-4,-4,PETSC_DECIDE,PETSC_DECIDE,1,1,PETSC_NULL,PETSC_NULL,&da);CHKERRQ(ierr);
ierr = DMDASetUniformCoordinates(da, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0);CHKERRQ(ierr);
ierr = DMDASetFieldName(da,0,"Heat");CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&x);CHKERRQ(ierr);
ierr = FormInitialGuess(da,PETSC_NULL,x);CHKERRQ(ierr);
ierr = DMDATSSetIFunctionLocal(da,INSERT_VALUES,(PetscErrorCode (*)(DMDALocalInfo*,PetscReal,void*,void*,void*,void*))FormIFunctionLocal,PETSC_NULL);CHKERRQ(ierr);
/* set up the coefficient */
ierr = DMGetNamedGlobalVector(da,"coefficient",&c);CHKERRQ(ierr);
ierr = FormDiffusionCoefficient(da,PETSC_NULL,c);CHKERRQ(ierr);
ierr = DMRestoreNamedGlobalVector(da,"coefficient",&c);CHKERRQ(ierr);
ierr = DMCoarsenHookAdd(da,PETSC_NULL,CoefficientRestrictHook,ts);CHKERRQ(ierr);
ierr = DMSubDomainHookAdd(da,PETSC_NULL,CoefficientSubDomainRestrictHook,ts);CHKERRQ(ierr);
ierr = TSSetDM(ts, da);CHKERRQ(ierr);
ierr = TSSetDuration(ts,10000,1000.0);CHKERRQ(ierr);
ierr = TSSetInitialTimeStep(ts,0.0,0.05);CHKERRQ(ierr);
ierr = TSSetSolution(ts,x);CHKERRQ(ierr);
ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
ierr = TSSolve(ts,x);CHKERRQ(ierr);
ierr = VecDestroy(&x);CHKERRQ(ierr);
ierr = TSDestroy(&ts);CHKERRQ(ierr);
ierr = DMDestroy(&da);CHKERRQ(ierr);
ierr = PetscFinalize();
PetscFunctionReturn(0);
}
示例12: main
int main(int argc,char **argv)
{
TS ts; /* timestepping context */
PetscErrorCode ierr;
PetscViewer viewer;
ierr = PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;
ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,"advection-diffusion-reaction/ex1");CHKERRQ(ierr);
ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr);
ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"advection-diffusion-reaction/binaryoutput",FILE_MODE_READ,&viewer);CHKERRQ(ierr);
ierr = TSLoad(ts,viewer);CHKERRQ(ierr);
ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
/* ierr = PetscFPTView(0);CHKERRQ(ierr); */
ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
ierr = TSSetUp(ts);CHKERRQ(ierr);
ierr = TSView(ts,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
ierr = TSSolve(ts,NULL);CHKERRQ(ierr);
ierr = TSDestroy(&ts);CHKERRQ(ierr);
ierr = PetscFinalize();
return ierr;
}
示例13: main
int main(int argc, char **argv)
{
AppCtx ctx;
DM dm;
TS ts;
Vec u, r;
PetscReal t = 0.0;
PetscReal L2error = 0.0;
PetscErrorCode ierr;
ierr = PetscInitialize(&argc, &argv, NULL, help);
CHKERRQ(ierr);
ierr = ProcessOptions(PETSC_COMM_WORLD, &ctx);
CHKERRQ(ierr);
ierr = CreateMesh(PETSC_COMM_WORLD, &dm, &ctx);
CHKERRQ(ierr);
ierr = DMSetApplicationContext(dm, &ctx);
CHKERRQ(ierr);
ierr = PetscMalloc1(1, &ctx.exactFuncs);
CHKERRQ(ierr);
ierr = SetupDiscretization(dm, &ctx);
CHKERRQ(ierr);
ierr = DMCreateGlobalVector(dm, &u);
CHKERRQ(ierr);
ierr = PetscObjectSetName((PetscObject) u, "temperature");
CHKERRQ(ierr);
ierr = VecDuplicate(u, &r);
CHKERRQ(ierr);
ierr = TSCreate(PETSC_COMM_WORLD, &ts);
CHKERRQ(ierr);
ierr = TSSetDM(ts, dm);
CHKERRQ(ierr);
ierr = DMTSSetBoundaryLocal(dm, DMPlexTSComputeBoundary, &ctx);
CHKERRQ(ierr);
ierr = DMTSSetIFunctionLocal(dm, DMPlexTSComputeIFunctionFEM, &ctx);
CHKERRQ(ierr);
ierr = DMTSSetIJacobianLocal(dm, DMPlexTSComputeIJacobianFEM, &ctx);
CHKERRQ(ierr);
ierr = TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER);
CHKERRQ(ierr);
ierr = TSSetFromOptions(ts);
CHKERRQ(ierr);
ierr = DMProjectFunction(dm, t, ctx.exactFuncs, NULL, INSERT_ALL_VALUES, u);
CHKERRQ(ierr);
ierr = TSSolve(ts, u);
CHKERRQ(ierr);
ierr = TSGetTime(ts, &t);
CHKERRQ(ierr);
ierr = DMComputeL2Diff(dm, t, ctx.exactFuncs, NULL, u, &L2error);
CHKERRQ(ierr);
if (L2error < 1.0e-11) {
ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: < 1.0e-11\n");
CHKERRQ(ierr);
}
else {
ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: %g\n", L2error);
CHKERRQ(ierr);
}
ierr = VecViewFromOptions(u, NULL, "-sol_vec_view");
CHKERRQ(ierr);
ierr = VecDestroy(&u);
CHKERRQ(ierr);
ierr = VecDestroy(&r);
CHKERRQ(ierr);
ierr = TSDestroy(&ts);
CHKERRQ(ierr);
ierr = DMDestroy(&dm);
CHKERRQ(ierr);
ierr = PetscFree(ctx.exactFuncs);
CHKERRQ(ierr);
ierr = PetscFinalize();
return ierr;
}
示例14: main
//.........这里部分代码省略.........
ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr);
ierr = TSSetType(ts,TSRK);CHKERRQ(ierr);
ierr = TSSetRHSFunction(ts,NULL,(TSRHSFunction)RHSFunction,&ctx);CHKERRQ(ierr);
ierr = TSSetRHSJacobian(ts,A,A,(TSRHSJacobian)RHSJacobian,&ctx);CHKERRQ(ierr);
ierr = TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Set initial conditions
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = TSSetSolution(ts,U);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Save trajectory of solution so that TSAdjointSolve() may be used
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
ierr = MatCreateVecs(A,&lambda[0],NULL);CHKERRQ(ierr);
/* Set initial conditions for the adjoint integration */
ierr = VecGetArray(lambda[0],&y_ptr);CHKERRQ(ierr);
y_ptr[0] = 0.0; y_ptr[1] = 0.0;
ierr = VecRestoreArray(lambda[0],&y_ptr);CHKERRQ(ierr);
ierr = MatCreateVecs(Jacp,&mu[0],NULL);CHKERRQ(ierr);
ierr = VecGetArray(mu[0],&x_ptr);CHKERRQ(ierr);
x_ptr[0] = -1.0;
ierr = VecRestoreArray(mu[0],&x_ptr);CHKERRQ(ierr);
ierr = TSSetCostGradients(ts,1,lambda,mu);CHKERRQ(ierr);
ierr = TSSetCostIntegrand(ts,1,(PetscErrorCode (*)(TS,PetscReal,Vec,Vec,void*))CostIntegrand,
(PetscErrorCode (*)(TS,PetscReal,Vec,Vec*,void*))DRDYFunction,
(PetscErrorCode (*)(TS,PetscReal,Vec,Vec*,void*))DRDPFunction,PETSC_TRUE,&ctx);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Set solver options
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = TSSetDuration(ts,PETSC_DEFAULT,10.0);CHKERRQ(ierr);
ierr = TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER);CHKERRQ(ierr);
ierr = TSSetInitialTimeStep(ts,0.0,.01);CHKERRQ(ierr);
ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Solve nonlinear system
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
if (ensemble) {
for (du[1] = -2.5; du[1] <= .01; du[1] += .1) {
ierr = VecGetArray(U,&u);CHKERRQ(ierr);
u[0] = PetscAsinScalar(ctx.Pm/ctx.Pmax);
u[1] = ctx.omega_s;
u[0] += du[0];
u[1] += du[1];
ierr = VecRestoreArray(U,&u);CHKERRQ(ierr);
ierr = TSSetInitialTimeStep(ts,0.0,.01);CHKERRQ(ierr);
ierr = TSSolve(ts,U);CHKERRQ(ierr);
}
} else {
ierr = TSSolve(ts,U);CHKERRQ(ierr);
}
ierr = VecView(U,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
ierr = TSGetSolveTime(ts,&ftime);CHKERRQ(ierr);
ierr = TSGetTimeStepNumber(ts,&steps);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Adjoint model starts here
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Set initial conditions for the adjoint integration */
ierr = VecGetArray(lambda[0],&y_ptr);CHKERRQ(ierr);
y_ptr[0] = 0.0; y_ptr[1] = 0.0;
ierr = VecRestoreArray(lambda[0],&y_ptr);CHKERRQ(ierr);
ierr = VecGetArray(mu[0],&x_ptr);CHKERRQ(ierr);
x_ptr[0] = -1.0;
ierr = VecRestoreArray(mu[0],&x_ptr);CHKERRQ(ierr);
/* Set RHS JacobianP */
ierr = TSAdjointSetRHSJacobian(ts,Jacp,RHSJacobianP,&ctx);CHKERRQ(ierr);
ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD,"\n sensitivity wrt initial conditions: d[Psi(tf)]/d[phi0] d[Psi(tf)]/d[omega0]\n");CHKERRQ(ierr);
ierr = VecView(lambda[0],PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
ierr = VecView(mu[0],PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
ierr = TSGetCostIntegral(ts,&q);CHKERRQ(ierr);
ierr = VecView(q,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
ierr = VecGetArray(q,&x_ptr);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD,"\n cost function=%g\n",(double)(x_ptr[0]-ctx.Pm));CHKERRQ(ierr);
ierr = VecRestoreArray(q,&x_ptr);CHKERRQ(ierr);
ierr = ComputeSensiP(lambda[0],mu[0],&ctx);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Free work space. All PETSc objects should be destroyed when they are no longer needed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = MatDestroy(&A);CHKERRQ(ierr);
ierr = MatDestroy(&Jacp);CHKERRQ(ierr);
ierr = VecDestroy(&U);CHKERRQ(ierr);
ierr = VecDestroy(&lambda[0]);CHKERRQ(ierr);
ierr = VecDestroy(&mu[0]);CHKERRQ(ierr);
ierr = TSDestroy(&ts);CHKERRQ(ierr);
ierr = PetscFinalize();
return(0);
}
示例15: main
//.........这里部分代码省略.........
ierr = OutputVTK(user->dm, fileName, &viewer);CHKERRQ(ierr);
ierr = VecView(algebra->solution, viewer);CHKERRQ(ierr);
ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
nplot++;
}
user->current_step++;
}
ierr = VecDestroy(&algebra->fn);CHKERRQ(ierr);
}else{
PetscReal ftime;
TS ts;
TSConvergedReason reason;
PetscInt nsteps;
ierr = PetscPrintf(PETSC_COMM_WORLD,"Using the fully explicit method based on the PETSC TS routing\n");CHKERRQ(ierr);
ierr = DMCreateGlobalVector(user->dm, &algebra->solution);CHKERRQ(ierr);
ierr = PetscObjectSetName((PetscObject) algebra->solution, "solution");CHKERRQ(ierr);
ierr = SetInitialCondition(user->dm, algebra->solution, user);CHKERRQ(ierr);
ierr = TSCreate(comm, &ts);CHKERRQ(ierr);
ierr = TSSetType(ts, TSEULER);CHKERRQ(ierr);
ierr = TSSetDM(ts, user->dm);CHKERRQ(ierr);
ierr = TSMonitorSet(ts,TSMonitorFunctionError,&user,NULL);CHKERRQ(ierr);
ierr = TSSetRHSFunction(ts, NULL, MyRHSFunction, user);CHKERRQ(ierr);
ierr = TSSetDuration(ts, 1000, user->final_time);CHKERRQ(ierr);
ierr = TSSetInitialTimeStep(ts, user->initial_time, user->dt);CHKERRQ(ierr);
ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
ierr = TSSolve(ts, algebra->solution);CHKERRQ(ierr);
ierr = TSGetSolveTime(ts, &ftime);CHKERRQ(ierr);
ierr = TSGetTimeStepNumber(ts, &nsteps);CHKERRQ(ierr);
ierr = TSGetConvergedReason(ts, &reason);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD,"%s at time %g after %D steps\n",TSConvergedReasons[reason],ftime,nsteps);CHKERRQ(ierr);
ierr = TSDestroy(&ts);CHKERRQ(ierr);
}
if(user->benchmark_couette) {
ierr = DMCreateGlobalVector(user->dm, &algebra->exactsolution);CHKERRQ(ierr);
ierr = ComputeExactSolution(user->dm, user->final_time, algebra->exactsolution, user);CHKERRQ(ierr);
}
if (user->output_solution){
PetscViewer viewer;
ierr = OutputVTK(user->dm, "solution.vtk", &viewer);CHKERRQ(ierr);
ierr = VecView(algebra->solution, viewer);CHKERRQ(ierr);
ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
}
if(user->benchmark_couette) {
PetscViewer viewer;
PetscReal norm;
ierr = OutputVTK(user->dm, "exact_solution.vtk", &viewer);CHKERRQ(ierr);
ierr = VecView(algebra->exactsolution, viewer);CHKERRQ(ierr);
ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
ierr = VecAXPY(algebra->exactsolution, -1, algebra->solution);CHKERRQ(ierr);
ierr = VecNorm(algebra->exactsolution,NORM_INFINITY,&norm);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD,"Final time at %f, Error: ||u_k-u|| = %g \n", user->final_time, norm);CHKERRQ(ierr);
ierr = OutputVTK(user->dm, "Error.vtk", &viewer);CHKERRQ(ierr);
ierr = VecView(algebra->exactsolution, viewer);CHKERRQ(ierr);
ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
}
ierr = VecDestroy(&algebra->solution);CHKERRQ(ierr);