1c4762a1bSJed Brown 2c4762a1bSJed Brown static char help[] ="Solves a simple data assimilation problem with one dimensional Burger's equation using TSAdjoint\n\n"; 3c4762a1bSJed Brown 4c4762a1bSJed Brown /* 5c4762a1bSJed Brown 6c4762a1bSJed Brown Not yet tested in parallel 7c4762a1bSJed Brown 8c4762a1bSJed Brown */ 9c4762a1bSJed Brown /* 10c4762a1bSJed Brown Concepts: TS^time-dependent nonlinear problems 11c4762a1bSJed Brown Concepts: TS^Burger's equation 12c4762a1bSJed Brown Concepts: adjoints 13c4762a1bSJed Brown Processors: n 14c4762a1bSJed Brown */ 15c4762a1bSJed Brown 16c4762a1bSJed Brown /* ------------------------------------------------------------------------ 17c4762a1bSJed Brown 18c4762a1bSJed Brown This program uses the one-dimensional Burger's equation 19c4762a1bSJed Brown u_t = mu*u_xx - u u_x, 20c4762a1bSJed Brown on the domain 0 <= x <= 1, with periodic boundary conditions 21c4762a1bSJed Brown 22c4762a1bSJed Brown to demonstrate solving a data assimilation problem of finding the initial conditions 23c4762a1bSJed Brown to produce a given solution at a fixed time. 24c4762a1bSJed Brown 25c4762a1bSJed Brown The operators are discretized with the spectral element method 26c4762a1bSJed Brown 27c4762a1bSJed Brown See the paper PDE-CONSTRAINED OPTIMIZATION WITH SPECTRAL ELEMENTS USING PETSC AND TAO 28c4762a1bSJed Brown by OANA MARIN, EMIL CONSTANTINESCU, AND BARRY SMITH for details on the exact solution 29c4762a1bSJed Brown used 30c4762a1bSJed Brown 31c4762a1bSJed Brown ------------------------------------------------------------------------- */ 32c4762a1bSJed Brown 33c4762a1bSJed Brown #include <petsctao.h> 34c4762a1bSJed Brown #include <petscts.h> 35c4762a1bSJed Brown #include <petscdt.h> 36c4762a1bSJed Brown #include <petscdraw.h> 37c4762a1bSJed Brown #include <petscdmda.h> 38c4762a1bSJed Brown 39c4762a1bSJed Brown /* 40c4762a1bSJed Brown User-defined application context - contains data needed by the 41c4762a1bSJed Brown application-provided call-back routines. 42c4762a1bSJed Brown */ 43c4762a1bSJed Brown 44c4762a1bSJed Brown typedef struct { 45c4762a1bSJed Brown PetscInt n; /* number of nodes */ 46c4762a1bSJed Brown PetscReal *nodes; /* GLL nodes */ 47c4762a1bSJed Brown PetscReal *weights; /* GLL weights */ 48c4762a1bSJed Brown } PetscGLL; 49c4762a1bSJed Brown 50c4762a1bSJed Brown typedef struct { 51c4762a1bSJed Brown PetscInt N; /* grid points per elements*/ 52c4762a1bSJed Brown PetscInt E; /* number of elements */ 53c4762a1bSJed Brown PetscReal tol_L2,tol_max; /* error norms */ 54c4762a1bSJed Brown PetscInt steps; /* number of timesteps */ 55c4762a1bSJed Brown PetscReal Tend; /* endtime */ 56c4762a1bSJed Brown PetscReal mu; /* viscosity */ 57c4762a1bSJed Brown PetscReal L; /* total length of domain */ 58c4762a1bSJed Brown PetscReal Le; 59c4762a1bSJed Brown PetscReal Tadj; 60c4762a1bSJed Brown } PetscParam; 61c4762a1bSJed Brown 62c4762a1bSJed Brown typedef struct { 63c4762a1bSJed Brown Vec obj; /* desired end state */ 64c4762a1bSJed Brown Vec grid; /* total grid */ 65c4762a1bSJed Brown Vec grad; 66c4762a1bSJed Brown Vec ic; 67c4762a1bSJed Brown Vec curr_sol; 68c4762a1bSJed Brown Vec true_solution; /* actual initial conditions for the final solution */ 69c4762a1bSJed Brown } PetscData; 70c4762a1bSJed Brown 71c4762a1bSJed Brown typedef struct { 72c4762a1bSJed Brown Vec grid; /* total grid */ 73c4762a1bSJed Brown Vec mass; /* mass matrix for total integration */ 74c4762a1bSJed Brown Mat stiff; /* stifness matrix */ 75c4762a1bSJed Brown Mat keptstiff; 76c4762a1bSJed Brown Mat grad; 77c4762a1bSJed Brown PetscGLL gll; 78c4762a1bSJed Brown } PetscSEMOperators; 79c4762a1bSJed Brown 80c4762a1bSJed Brown typedef struct { 81c4762a1bSJed Brown DM da; /* distributed array data structure */ 82c4762a1bSJed Brown PetscSEMOperators SEMop; 83c4762a1bSJed Brown PetscParam param; 84c4762a1bSJed Brown PetscData dat; 85c4762a1bSJed Brown TS ts; 86c4762a1bSJed Brown PetscReal initial_dt; 87c4762a1bSJed Brown } AppCtx; 88c4762a1bSJed Brown 89c4762a1bSJed Brown /* 90c4762a1bSJed Brown User-defined routines 91c4762a1bSJed Brown */ 92c4762a1bSJed Brown extern PetscErrorCode FormFunctionGradient(Tao,Vec,PetscReal*,Vec,void*); 93c4762a1bSJed Brown extern PetscErrorCode RHSMatrixLaplaciangllDM(TS,PetscReal,Vec,Mat,Mat,void*); 94c4762a1bSJed Brown extern PetscErrorCode RHSMatrixAdvectiongllDM(TS,PetscReal,Vec,Mat,Mat,void*); 95c4762a1bSJed Brown extern PetscErrorCode InitialConditions(Vec,AppCtx*); 96c4762a1bSJed Brown extern PetscErrorCode TrueSolution(Vec,AppCtx*); 97c4762a1bSJed Brown extern PetscErrorCode ComputeObjective(PetscReal,Vec,AppCtx*); 98c4762a1bSJed Brown extern PetscErrorCode MonitorError(Tao,void*); 99c4762a1bSJed Brown extern PetscErrorCode RHSFunction(TS,PetscReal,Vec,Vec,void*); 100c4762a1bSJed Brown extern PetscErrorCode RHSJacobian(TS,PetscReal,Vec,Mat,Mat,void*); 101c4762a1bSJed Brown 102c4762a1bSJed Brown int main(int argc,char **argv) 103c4762a1bSJed Brown { 104c4762a1bSJed Brown AppCtx appctx; /* user-defined application context */ 105c4762a1bSJed Brown Tao tao; 106c4762a1bSJed Brown Vec u; /* approximate solution vector */ 107c4762a1bSJed Brown PetscInt i, xs, xm, ind, j, lenglob; 108c4762a1bSJed Brown PetscReal x, *wrk_ptr1, *wrk_ptr2; 109c4762a1bSJed Brown MatNullSpace nsp; 110c4762a1bSJed Brown PetscMPIInt size; 111c4762a1bSJed Brown 112c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 113c4762a1bSJed Brown Initialize program and set problem parameters 114c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 115c4762a1bSJed Brown PetscFunctionBegin; 116c4762a1bSJed Brown 117*9566063dSJacob Faibussowitsch PetscCall(PetscInitialize(&argc,&argv,(char*)0,help)); 118c4762a1bSJed Brown 119c4762a1bSJed Brown /*initialize parameters */ 120c4762a1bSJed Brown appctx.param.N = 10; /* order of the spectral element */ 121c4762a1bSJed Brown appctx.param.E = 10; /* number of elements */ 122c4762a1bSJed Brown appctx.param.L = 4.0; /* length of the domain */ 123c4762a1bSJed Brown appctx.param.mu = 0.01; /* diffusion coefficient */ 124c4762a1bSJed Brown appctx.initial_dt = 5e-3; 125c4762a1bSJed Brown appctx.param.steps = PETSC_MAX_INT; 126c4762a1bSJed Brown appctx.param.Tend = 4; 127c4762a1bSJed Brown 128*9566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetInt(NULL,NULL,"-N",&appctx.param.N,NULL)); 129*9566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetInt(NULL,NULL,"-E",&appctx.param.E,NULL)); 130*9566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(NULL,NULL,"-Tend",&appctx.param.Tend,NULL)); 131*9566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(NULL,NULL,"-mu",&appctx.param.mu,NULL)); 132c4762a1bSJed Brown appctx.param.Le = appctx.param.L/appctx.param.E; 133c4762a1bSJed Brown 134*9566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 1353c859ba3SBarry Smith PetscCheck((appctx.param.E % size) == 0,PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Number of elements must be divisible by number of processes"); 136c4762a1bSJed Brown 137c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 138c4762a1bSJed Brown Create GLL data structures 139c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 140*9566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(appctx.param.N,&appctx.SEMop.gll.nodes,appctx.param.N,&appctx.SEMop.gll.weights)); 141*9566063dSJacob Faibussowitsch PetscCall(PetscDTGaussLobattoLegendreQuadrature(appctx.param.N,PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA,appctx.SEMop.gll.nodes,appctx.SEMop.gll.weights)); 142c4762a1bSJed Brown appctx.SEMop.gll.n = appctx.param.N; 143c4762a1bSJed Brown lenglob = appctx.param.E*(appctx.param.N-1); 144c4762a1bSJed Brown 145c4762a1bSJed Brown /* 146c4762a1bSJed Brown Create distributed array (DMDA) to manage parallel grid and vectors 147c4762a1bSJed Brown and to set up the ghost point communication pattern. There are E*(Nl-1)+1 148c4762a1bSJed Brown total grid values spread equally among all the processors, except first and last 149c4762a1bSJed Brown */ 150c4762a1bSJed Brown 151*9566063dSJacob Faibussowitsch PetscCall(DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_PERIODIC,lenglob,1,1,NULL,&appctx.da)); 152*9566063dSJacob Faibussowitsch PetscCall(DMSetFromOptions(appctx.da)); 153*9566063dSJacob Faibussowitsch PetscCall(DMSetUp(appctx.da)); 154c4762a1bSJed Brown 155c4762a1bSJed Brown /* 156c4762a1bSJed Brown Extract global and local vectors from DMDA; we use these to store the 157c4762a1bSJed Brown approximate solution. Then duplicate these for remaining vectors that 158c4762a1bSJed Brown have the same types. 159c4762a1bSJed Brown */ 160c4762a1bSJed Brown 161*9566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(appctx.da,&u)); 162*9566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u,&appctx.dat.ic)); 163*9566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u,&appctx.dat.true_solution)); 164*9566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u,&appctx.dat.obj)); 165*9566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u,&appctx.SEMop.grid)); 166*9566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u,&appctx.SEMop.mass)); 167*9566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u,&appctx.dat.curr_sol)); 168c4762a1bSJed Brown 169*9566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(appctx.da,&xs,NULL,NULL,&xm,NULL,NULL)); 170*9566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArray(appctx.da,appctx.SEMop.grid,&wrk_ptr1)); 171*9566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArray(appctx.da,appctx.SEMop.mass,&wrk_ptr2)); 172c4762a1bSJed Brown 173c4762a1bSJed Brown /* Compute function over the locally owned part of the grid */ 174c4762a1bSJed Brown 175c4762a1bSJed Brown xs=xs/(appctx.param.N-1); 176c4762a1bSJed Brown xm=xm/(appctx.param.N-1); 177c4762a1bSJed Brown 178c4762a1bSJed Brown /* 179c4762a1bSJed Brown Build total grid and mass over entire mesh (multi-elemental) 180c4762a1bSJed Brown */ 181c4762a1bSJed Brown 182c4762a1bSJed Brown for (i=xs; i<xs+xm; i++) { 183c4762a1bSJed Brown for (j=0; j<appctx.param.N-1; j++) { 184c4762a1bSJed Brown x = (appctx.param.Le/2.0)*(appctx.SEMop.gll.nodes[j]+1.0)+appctx.param.Le*i; 185c4762a1bSJed Brown ind=i*(appctx.param.N-1)+j; 186c4762a1bSJed Brown wrk_ptr1[ind]=x; 187c4762a1bSJed Brown wrk_ptr2[ind]=.5*appctx.param.Le*appctx.SEMop.gll.weights[j]; 188c4762a1bSJed Brown if (j==0) wrk_ptr2[ind]+=.5*appctx.param.Le*appctx.SEMop.gll.weights[j]; 189c4762a1bSJed Brown } 190c4762a1bSJed Brown } 191*9566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArray(appctx.da,appctx.SEMop.grid,&wrk_ptr1)); 192*9566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArray(appctx.da,appctx.SEMop.mass,&wrk_ptr2)); 193c4762a1bSJed Brown 194c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 195c4762a1bSJed Brown Create matrix data structure; set matrix evaluation routine. 196c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 197*9566063dSJacob Faibussowitsch PetscCall(DMSetMatrixPreallocateOnly(appctx.da, PETSC_TRUE)); 198*9566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix(appctx.da,&appctx.SEMop.stiff)); 199*9566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix(appctx.da,&appctx.SEMop.grad)); 200c4762a1bSJed Brown /* 201c4762a1bSJed Brown For linear problems with a time-dependent f(u,t) in the equation 202c4762a1bSJed Brown u_t = f(u,t), the user provides the discretized right-hand-side 203c4762a1bSJed Brown as a time-dependent matrix. 204c4762a1bSJed Brown */ 205*9566063dSJacob Faibussowitsch PetscCall(RHSMatrixLaplaciangllDM(appctx.ts,0.0,u,appctx.SEMop.stiff,appctx.SEMop.stiff,&appctx)); 206*9566063dSJacob Faibussowitsch PetscCall(RHSMatrixAdvectiongllDM(appctx.ts,0.0,u,appctx.SEMop.grad,appctx.SEMop.grad,&appctx)); 207c4762a1bSJed Brown /* 208c4762a1bSJed Brown For linear problems with a time-dependent f(u,t) in the equation 209c4762a1bSJed Brown u_t = f(u,t), the user provides the discretized right-hand-side 210c4762a1bSJed Brown as a time-dependent matrix. 211c4762a1bSJed Brown */ 212c4762a1bSJed Brown 213*9566063dSJacob Faibussowitsch PetscCall(MatDuplicate(appctx.SEMop.stiff,MAT_COPY_VALUES,&appctx.SEMop.keptstiff)); 214c4762a1bSJed Brown 215c4762a1bSJed Brown /* attach the null space to the matrix, this probably is not needed but does no harm */ 216*9566063dSJacob Faibussowitsch PetscCall(MatNullSpaceCreate(PETSC_COMM_WORLD,PETSC_TRUE,0,NULL,&nsp)); 217*9566063dSJacob Faibussowitsch PetscCall(MatSetNullSpace(appctx.SEMop.stiff,nsp)); 218*9566063dSJacob Faibussowitsch PetscCall(MatSetNullSpace(appctx.SEMop.keptstiff,nsp)); 219*9566063dSJacob Faibussowitsch PetscCall(MatNullSpaceTest(nsp,appctx.SEMop.stiff,NULL)); 220*9566063dSJacob Faibussowitsch PetscCall(MatNullSpaceDestroy(&nsp)); 221c4762a1bSJed Brown /* attach the null space to the matrix, this probably is not needed but does no harm */ 222*9566063dSJacob Faibussowitsch PetscCall(MatNullSpaceCreate(PETSC_COMM_WORLD,PETSC_TRUE,0,NULL,&nsp)); 223*9566063dSJacob Faibussowitsch PetscCall(MatSetNullSpace(appctx.SEMop.grad,nsp)); 224*9566063dSJacob Faibussowitsch PetscCall(MatNullSpaceTest(nsp,appctx.SEMop.grad,NULL)); 225*9566063dSJacob Faibussowitsch PetscCall(MatNullSpaceDestroy(&nsp)); 226c4762a1bSJed Brown 227c4762a1bSJed Brown /* Create the TS solver that solves the ODE and its adjoint; set its options */ 228*9566063dSJacob Faibussowitsch PetscCall(TSCreate(PETSC_COMM_WORLD,&appctx.ts)); 229*9566063dSJacob Faibussowitsch PetscCall(TSSetProblemType(appctx.ts,TS_NONLINEAR)); 230*9566063dSJacob Faibussowitsch PetscCall(TSSetType(appctx.ts,TSRK)); 231*9566063dSJacob Faibussowitsch PetscCall(TSSetDM(appctx.ts,appctx.da)); 232*9566063dSJacob Faibussowitsch PetscCall(TSSetTime(appctx.ts,0.0)); 233*9566063dSJacob Faibussowitsch PetscCall(TSSetTimeStep(appctx.ts,appctx.initial_dt)); 234*9566063dSJacob Faibussowitsch PetscCall(TSSetMaxSteps(appctx.ts,appctx.param.steps)); 235*9566063dSJacob Faibussowitsch PetscCall(TSSetMaxTime(appctx.ts,appctx.param.Tend)); 236*9566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTime(appctx.ts,TS_EXACTFINALTIME_MATCHSTEP)); 237*9566063dSJacob Faibussowitsch PetscCall(TSSetTolerances(appctx.ts,1e-7,NULL,1e-7,NULL)); 238*9566063dSJacob Faibussowitsch PetscCall(TSSetFromOptions(appctx.ts)); 239c4762a1bSJed Brown /* Need to save initial timestep user may have set with -ts_dt so it can be reset for each new TSSolve() */ 240*9566063dSJacob Faibussowitsch PetscCall(TSGetTimeStep(appctx.ts,&appctx.initial_dt)); 241*9566063dSJacob Faibussowitsch PetscCall(TSSetRHSFunction(appctx.ts,NULL,RHSFunction,&appctx)); 242*9566063dSJacob Faibussowitsch PetscCall(TSSetRHSJacobian(appctx.ts,appctx.SEMop.stiff,appctx.SEMop.stiff,RHSJacobian,&appctx)); 243c4762a1bSJed Brown 244c4762a1bSJed Brown /* Set Objective and Initial conditions for the problem and compute Objective function (evolution of true_solution to final time */ 245*9566063dSJacob Faibussowitsch PetscCall(InitialConditions(appctx.dat.ic,&appctx)); 246*9566063dSJacob Faibussowitsch PetscCall(TrueSolution(appctx.dat.true_solution,&appctx)); 247*9566063dSJacob Faibussowitsch PetscCall(ComputeObjective(appctx.param.Tend,appctx.dat.obj,&appctx)); 248c4762a1bSJed Brown 249*9566063dSJacob Faibussowitsch PetscCall(TSSetSaveTrajectory(appctx.ts)); 250*9566063dSJacob Faibussowitsch PetscCall(TSSetFromOptions(appctx.ts)); 251f32d6360SSatish Balay 252c4762a1bSJed Brown /* Create TAO solver and set desired solution method */ 253*9566063dSJacob Faibussowitsch PetscCall(TaoCreate(PETSC_COMM_WORLD,&tao)); 254*9566063dSJacob Faibussowitsch PetscCall(TaoSetMonitor(tao,MonitorError,&appctx,NULL)); 255*9566063dSJacob Faibussowitsch PetscCall(TaoSetType(tao,TAOBQNLS)); 256*9566063dSJacob Faibussowitsch PetscCall(TaoSetSolution(tao,appctx.dat.ic)); 257c4762a1bSJed Brown /* Set routine for function and gradient evaluation */ 258*9566063dSJacob Faibussowitsch PetscCall(TaoSetObjectiveAndGradient(tao,NULL,FormFunctionGradient,(void *)&appctx)); 259c4762a1bSJed Brown /* Check for any TAO command line options */ 260*9566063dSJacob Faibussowitsch PetscCall(TaoSetTolerances(tao,1e-8,PETSC_DEFAULT,PETSC_DEFAULT)); 261*9566063dSJacob Faibussowitsch PetscCall(TaoSetFromOptions(tao)); 262*9566063dSJacob Faibussowitsch PetscCall(TaoSolve(tao)); 263c4762a1bSJed Brown 264*9566063dSJacob Faibussowitsch PetscCall(TaoDestroy(&tao)); 265*9566063dSJacob Faibussowitsch PetscCall(MatDestroy(&appctx.SEMop.stiff)); 266*9566063dSJacob Faibussowitsch PetscCall(MatDestroy(&appctx.SEMop.keptstiff)); 267*9566063dSJacob Faibussowitsch PetscCall(MatDestroy(&appctx.SEMop.grad)); 268*9566063dSJacob Faibussowitsch PetscCall(VecDestroy(&u)); 269*9566063dSJacob Faibussowitsch PetscCall(VecDestroy(&appctx.dat.ic)); 270*9566063dSJacob Faibussowitsch PetscCall(VecDestroy(&appctx.dat.true_solution)); 271*9566063dSJacob Faibussowitsch PetscCall(VecDestroy(&appctx.dat.obj)); 272*9566063dSJacob Faibussowitsch PetscCall(VecDestroy(&appctx.SEMop.grid)); 273*9566063dSJacob Faibussowitsch PetscCall(VecDestroy(&appctx.SEMop.mass)); 274*9566063dSJacob Faibussowitsch PetscCall(VecDestroy(&appctx.dat.curr_sol)); 275*9566063dSJacob Faibussowitsch PetscCall(PetscFree2(appctx.SEMop.gll.nodes,appctx.SEMop.gll.weights)); 276*9566063dSJacob Faibussowitsch PetscCall(DMDestroy(&appctx.da)); 277*9566063dSJacob Faibussowitsch PetscCall(TSDestroy(&appctx.ts)); 278c4762a1bSJed Brown 279c4762a1bSJed Brown /* 280c4762a1bSJed Brown Always call PetscFinalize() before exiting a program. This routine 281c4762a1bSJed Brown - finalizes the PETSc libraries as well as MPI 282c4762a1bSJed Brown - provides summary and diagnostic information if certain runtime 283c4762a1bSJed Brown options are chosen (e.g., -log_summary). 284c4762a1bSJed Brown */ 285*9566063dSJacob Faibussowitsch PetscCall(PetscFinalize()); 286b122ec5aSJacob Faibussowitsch return 0; 287c4762a1bSJed Brown } 288c4762a1bSJed Brown 289c4762a1bSJed Brown /* --------------------------------------------------------------------- */ 290c4762a1bSJed Brown /* 291c4762a1bSJed Brown InitialConditions - Computes the initial conditions for the Tao optimization solve (these are also initial conditions for the first TSSolve() 292c4762a1bSJed Brown 293c4762a1bSJed Brown The routine TrueSolution() computes the true solution for the Tao optimization solve which means they are the initial conditions for the objective function 294c4762a1bSJed Brown 295c4762a1bSJed Brown Input Parameter: 296c4762a1bSJed Brown u - uninitialized solution vector (global) 297c4762a1bSJed Brown appctx - user-defined application context 298c4762a1bSJed Brown 299c4762a1bSJed Brown Output Parameter: 300c4762a1bSJed Brown u - vector with solution at initial time (global) 301c4762a1bSJed Brown */ 302c4762a1bSJed Brown PetscErrorCode InitialConditions(Vec u,AppCtx *appctx) 303c4762a1bSJed Brown { 304c4762a1bSJed Brown PetscScalar *s; 305c4762a1bSJed Brown const PetscScalar *xg; 306c4762a1bSJed Brown PetscInt i,xs,xn; 307c4762a1bSJed Brown 308c4762a1bSJed Brown PetscFunctionBegin; 309*9566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArray(appctx->da,u,&s)); 310*9566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArrayRead(appctx->da,appctx->SEMop.grid,(void*)&xg)); 311*9566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(appctx->da,&xs,NULL,NULL,&xn,NULL,NULL)); 312c4762a1bSJed Brown for (i=xs; i<xs+xn; i++) { 313c4762a1bSJed Brown s[i]=2.0*appctx->param.mu*PETSC_PI*PetscSinScalar(PETSC_PI*xg[i])/(2.0+PetscCosScalar(PETSC_PI*xg[i]))+0.25*PetscExpReal(-4.0*PetscPowReal(xg[i]-2.0,2.0)); 314c4762a1bSJed Brown } 315*9566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArray(appctx->da,u,&s)); 316*9566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArrayRead(appctx->da,appctx->SEMop.grid,(void*)&xg)); 317c4762a1bSJed Brown PetscFunctionReturn(0); 318c4762a1bSJed Brown } 319c4762a1bSJed Brown 320c4762a1bSJed Brown /* 321c4762a1bSJed Brown TrueSolution() computes the true solution for the Tao optimization solve which means they are the initial conditions for the objective function. 322c4762a1bSJed Brown 323a5b23f4aSJose E. Roman InitialConditions() computes the initial conditions for the beginning of the Tao iterations 324c4762a1bSJed Brown 325c4762a1bSJed Brown Input Parameter: 326c4762a1bSJed Brown u - uninitialized solution vector (global) 327c4762a1bSJed Brown appctx - user-defined application context 328c4762a1bSJed Brown 329c4762a1bSJed Brown Output Parameter: 330c4762a1bSJed Brown u - vector with solution at initial time (global) 331c4762a1bSJed Brown */ 332c4762a1bSJed Brown PetscErrorCode TrueSolution(Vec u,AppCtx *appctx) 333c4762a1bSJed Brown { 334c4762a1bSJed Brown PetscScalar *s; 335c4762a1bSJed Brown const PetscScalar *xg; 336c4762a1bSJed Brown PetscInt i,xs,xn; 337c4762a1bSJed Brown 338c4762a1bSJed Brown PetscFunctionBegin; 339*9566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArray(appctx->da,u,&s)); 340*9566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArrayRead(appctx->da,appctx->SEMop.grid,(void*)&xg)); 341*9566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(appctx->da,&xs,NULL,NULL,&xn,NULL,NULL)); 342c4762a1bSJed Brown for (i=xs; i<xs+xn; i++) { 343c4762a1bSJed Brown s[i]=2.0*appctx->param.mu*PETSC_PI*PetscSinScalar(PETSC_PI*xg[i])/(2.0+PetscCosScalar(PETSC_PI*xg[i])); 344c4762a1bSJed Brown } 345*9566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArray(appctx->da,u,&s)); 346*9566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArrayRead(appctx->da,appctx->SEMop.grid,(void*)&xg)); 347c4762a1bSJed Brown PetscFunctionReturn(0); 348c4762a1bSJed Brown } 349c4762a1bSJed Brown /* --------------------------------------------------------------------- */ 350c4762a1bSJed Brown /* 351c4762a1bSJed Brown Sets the desired profile for the final end time 352c4762a1bSJed Brown 353c4762a1bSJed Brown Input Parameters: 354c4762a1bSJed Brown t - final time 355c4762a1bSJed Brown obj - vector storing the desired profile 356c4762a1bSJed Brown appctx - user-defined application context 357c4762a1bSJed Brown 358c4762a1bSJed Brown */ 359c4762a1bSJed Brown PetscErrorCode ComputeObjective(PetscReal t,Vec obj,AppCtx *appctx) 360c4762a1bSJed Brown { 361c4762a1bSJed Brown PetscScalar *s; 362c4762a1bSJed Brown const PetscScalar *xg; 363c4762a1bSJed Brown PetscInt i, xs,xn; 364c4762a1bSJed Brown 365c4762a1bSJed Brown PetscFunctionBegin; 366*9566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArray(appctx->da,obj,&s)); 367*9566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArrayRead(appctx->da,appctx->SEMop.grid,(void*)&xg)); 368*9566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(appctx->da,&xs,NULL,NULL,&xn,NULL,NULL)); 369c4762a1bSJed Brown for (i=xs; i<xs+xn; i++) { 370c4762a1bSJed Brown s[i]=2.0*appctx->param.mu*PETSC_PI*PetscSinScalar(PETSC_PI*xg[i])*PetscExpScalar(-PETSC_PI*PETSC_PI*t*appctx->param.mu)\ 371c4762a1bSJed Brown /(2.0+PetscExpScalar(-PETSC_PI*PETSC_PI*t*appctx->param.mu)*PetscCosScalar(PETSC_PI*xg[i])); 372c4762a1bSJed Brown } 373*9566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArray(appctx->da,obj,&s)); 374*9566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArrayRead(appctx->da,appctx->SEMop.grid,(void*)&xg)); 375c4762a1bSJed Brown PetscFunctionReturn(0); 376c4762a1bSJed Brown } 377c4762a1bSJed Brown 378c4762a1bSJed Brown PetscErrorCode RHSFunction(TS ts,PetscReal t,Vec globalin,Vec globalout,void *ctx) 379c4762a1bSJed Brown { 380c4762a1bSJed Brown AppCtx *appctx = (AppCtx*)ctx; 381c4762a1bSJed Brown 382c4762a1bSJed Brown PetscFunctionBegin; 383*9566063dSJacob Faibussowitsch PetscCall(MatMult(appctx->SEMop.grad,globalin,globalout)); /* grad u */ 384*9566063dSJacob Faibussowitsch PetscCall(VecPointwiseMult(globalout,globalin,globalout)); /* u grad u */ 385*9566063dSJacob Faibussowitsch PetscCall(VecScale(globalout, -1.0)); 386*9566063dSJacob Faibussowitsch PetscCall(MatMultAdd(appctx->SEMop.keptstiff,globalin,globalout,globalout)); 387c4762a1bSJed Brown PetscFunctionReturn(0); 388c4762a1bSJed Brown } 389c4762a1bSJed Brown 390c4762a1bSJed Brown /* 391c4762a1bSJed Brown 392c4762a1bSJed Brown K is the discretiziation of the Laplacian 393c4762a1bSJed Brown G is the discretization of the gradient 394c4762a1bSJed Brown 395c4762a1bSJed Brown Computes Jacobian of K u + diag(u) G u which is given by 396c4762a1bSJed Brown K + diag(u)G + diag(Gu) 397c4762a1bSJed Brown */ 398c4762a1bSJed Brown PetscErrorCode RHSJacobian(TS ts,PetscReal t,Vec globalin,Mat A, Mat B,void *ctx) 399c4762a1bSJed Brown { 400c4762a1bSJed Brown AppCtx *appctx = (AppCtx*)ctx; 401c4762a1bSJed Brown Vec Gglobalin; 402c4762a1bSJed Brown 403c4762a1bSJed Brown PetscFunctionBegin; 404c4762a1bSJed Brown /* A = diag(u) G */ 405c4762a1bSJed Brown 406*9566063dSJacob Faibussowitsch PetscCall(MatCopy(appctx->SEMop.grad,A,SAME_NONZERO_PATTERN)); 407*9566063dSJacob Faibussowitsch PetscCall(MatDiagonalScale(A,globalin,NULL)); 408c4762a1bSJed Brown 409c4762a1bSJed Brown /* A = A + diag(Gu) */ 410*9566063dSJacob Faibussowitsch PetscCall(VecDuplicate(globalin,&Gglobalin)); 411*9566063dSJacob Faibussowitsch PetscCall(MatMult(appctx->SEMop.grad,globalin,Gglobalin)); 412*9566063dSJacob Faibussowitsch PetscCall(MatDiagonalSet(A,Gglobalin,ADD_VALUES)); 413*9566063dSJacob Faibussowitsch PetscCall(VecDestroy(&Gglobalin)); 414c4762a1bSJed Brown 415c4762a1bSJed Brown /* A = K - A */ 416*9566063dSJacob Faibussowitsch PetscCall(MatScale(A,-1.0)); 417*9566063dSJacob Faibussowitsch PetscCall(MatAXPY(A,1.0,appctx->SEMop.keptstiff,SAME_NONZERO_PATTERN)); 418c4762a1bSJed Brown PetscFunctionReturn(0); 419c4762a1bSJed Brown } 420c4762a1bSJed Brown 421c4762a1bSJed Brown /* --------------------------------------------------------------------- */ 422c4762a1bSJed Brown 423c4762a1bSJed Brown /* 424c4762a1bSJed Brown RHSMatrixLaplacian - User-provided routine to compute the right-hand-side 425c4762a1bSJed Brown matrix for the heat equation. 426c4762a1bSJed Brown 427c4762a1bSJed Brown Input Parameters: 428c4762a1bSJed Brown ts - the TS context 429c4762a1bSJed Brown t - current time (ignored) 430c4762a1bSJed Brown X - current solution (ignored) 431c4762a1bSJed Brown dummy - optional user-defined context, as set by TSetRHSJacobian() 432c4762a1bSJed Brown 433c4762a1bSJed Brown Output Parameters: 434c4762a1bSJed Brown AA - Jacobian matrix 435c4762a1bSJed Brown BB - optionally different matrix from which the preconditioner is built 436c4762a1bSJed Brown str - flag indicating matrix structure 437c4762a1bSJed Brown 438c4762a1bSJed Brown */ 439c4762a1bSJed Brown PetscErrorCode RHSMatrixLaplaciangllDM(TS ts,PetscReal t,Vec X,Mat A,Mat BB,void *ctx) 440c4762a1bSJed Brown { 441c4762a1bSJed Brown PetscReal **temp; 442c4762a1bSJed Brown PetscReal vv; 443c4762a1bSJed Brown AppCtx *appctx = (AppCtx*)ctx; /* user-defined application context */ 444c4762a1bSJed Brown PetscInt i,xs,xn,l,j; 445c4762a1bSJed Brown PetscInt *rowsDM; 446c4762a1bSJed Brown 447c4762a1bSJed Brown PetscFunctionBegin; 448c4762a1bSJed Brown /* 449c4762a1bSJed Brown Creates the element stiffness matrix for the given gll 450c4762a1bSJed Brown */ 451*9566063dSJacob Faibussowitsch PetscCall(PetscGaussLobattoLegendreElementLaplacianCreate(appctx->SEMop.gll.n,appctx->SEMop.gll.nodes,appctx->SEMop.gll.weights,&temp)); 452a5b23f4aSJose E. Roman /* workaround for clang analyzer warning: Division by zero */ 4533c859ba3SBarry Smith PetscCheck(appctx->param.N > 1,PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Spectral element order should be > 1"); 454c4762a1bSJed Brown 455c4762a1bSJed Brown /* scale by the size of the element */ 456c4762a1bSJed Brown for (i=0; i<appctx->param.N; i++) { 457c4762a1bSJed Brown vv=-appctx->param.mu*2.0/appctx->param.Le; 458c4762a1bSJed Brown for (j=0; j<appctx->param.N; j++) temp[i][j]=temp[i][j]*vv; 459c4762a1bSJed Brown } 460c4762a1bSJed Brown 461*9566063dSJacob Faibussowitsch PetscCall(MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE)); 462*9566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(appctx->da,&xs,NULL,NULL,&xn,NULL,NULL)); 463c4762a1bSJed Brown 464c4762a1bSJed Brown xs = xs/(appctx->param.N-1); 465c4762a1bSJed Brown xn = xn/(appctx->param.N-1); 466c4762a1bSJed Brown 467*9566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(appctx->param.N,&rowsDM)); 468c4762a1bSJed Brown /* 469c4762a1bSJed Brown loop over local elements 470c4762a1bSJed Brown */ 471c4762a1bSJed Brown for (j=xs; j<xs+xn; j++) { 472c4762a1bSJed Brown for (l=0; l<appctx->param.N; l++) { 473c4762a1bSJed Brown rowsDM[l] = 1+(j-xs)*(appctx->param.N-1)+l; 474c4762a1bSJed Brown } 475*9566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(A,appctx->param.N,rowsDM,appctx->param.N,rowsDM,&temp[0][0],ADD_VALUES)); 476c4762a1bSJed Brown } 477*9566063dSJacob Faibussowitsch PetscCall(PetscFree(rowsDM)); 478*9566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 479*9566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 480*9566063dSJacob Faibussowitsch PetscCall(VecReciprocal(appctx->SEMop.mass)); 481*9566063dSJacob Faibussowitsch PetscCall(MatDiagonalScale(A,appctx->SEMop.mass,0)); 482*9566063dSJacob Faibussowitsch PetscCall(VecReciprocal(appctx->SEMop.mass)); 483c4762a1bSJed Brown 484*9566063dSJacob Faibussowitsch PetscCall(PetscGaussLobattoLegendreElementLaplacianDestroy(appctx->SEMop.gll.n,appctx->SEMop.gll.nodes,appctx->SEMop.gll.weights,&temp)); 485c4762a1bSJed Brown PetscFunctionReturn(0); 486c4762a1bSJed Brown } 487c4762a1bSJed Brown 488c4762a1bSJed Brown /* 489c4762a1bSJed Brown RHSMatrixAdvection - User-provided routine to compute the right-hand-side 490c4762a1bSJed Brown matrix for the Advection equation. 491c4762a1bSJed Brown 492c4762a1bSJed Brown Input Parameters: 493c4762a1bSJed Brown ts - the TS context 494c4762a1bSJed Brown t - current time 495c4762a1bSJed Brown global_in - global input vector 496c4762a1bSJed Brown dummy - optional user-defined context, as set by TSetRHSJacobian() 497c4762a1bSJed Brown 498c4762a1bSJed Brown Output Parameters: 499c4762a1bSJed Brown AA - Jacobian matrix 500c4762a1bSJed Brown BB - optionally different preconditioning matrix 501c4762a1bSJed Brown str - flag indicating matrix structure 502c4762a1bSJed Brown 503c4762a1bSJed Brown */ 504c4762a1bSJed Brown PetscErrorCode RHSMatrixAdvectiongllDM(TS ts,PetscReal t,Vec X,Mat A,Mat BB,void *ctx) 505c4762a1bSJed Brown { 506c4762a1bSJed Brown PetscReal **temp; 507c4762a1bSJed Brown AppCtx *appctx = (AppCtx*)ctx; /* user-defined application context */ 508c4762a1bSJed Brown PetscInt xs,xn,l,j; 509c4762a1bSJed Brown PetscInt *rowsDM; 510c4762a1bSJed Brown 511c4762a1bSJed Brown PetscFunctionBegin; 512c4762a1bSJed Brown /* 513c4762a1bSJed Brown Creates the advection matrix for the given gll 514c4762a1bSJed Brown */ 515*9566063dSJacob Faibussowitsch PetscCall(PetscGaussLobattoLegendreElementAdvectionCreate(appctx->SEMop.gll.n,appctx->SEMop.gll.nodes,appctx->SEMop.gll.weights,&temp)); 516*9566063dSJacob Faibussowitsch PetscCall(MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE)); 517c4762a1bSJed Brown 518*9566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(appctx->da,&xs,NULL,NULL,&xn,NULL,NULL)); 519c4762a1bSJed Brown 520c4762a1bSJed Brown xs = xs/(appctx->param.N-1); 521c4762a1bSJed Brown xn = xn/(appctx->param.N-1); 522c4762a1bSJed Brown 523*9566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(appctx->param.N,&rowsDM)); 524c4762a1bSJed Brown for (j=xs; j<xs+xn; j++) { 525c4762a1bSJed Brown for (l=0; l<appctx->param.N; l++) { 526c4762a1bSJed Brown rowsDM[l] = 1+(j-xs)*(appctx->param.N-1)+l; 527c4762a1bSJed Brown } 528*9566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(A,appctx->param.N,rowsDM,appctx->param.N,rowsDM,&temp[0][0],ADD_VALUES)); 529c4762a1bSJed Brown } 530*9566063dSJacob Faibussowitsch PetscCall(PetscFree(rowsDM)); 531*9566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 532*9566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 533c4762a1bSJed Brown 534*9566063dSJacob Faibussowitsch PetscCall(VecReciprocal(appctx->SEMop.mass)); 535*9566063dSJacob Faibussowitsch PetscCall(MatDiagonalScale(A,appctx->SEMop.mass,0)); 536*9566063dSJacob Faibussowitsch PetscCall(VecReciprocal(appctx->SEMop.mass)); 537*9566063dSJacob Faibussowitsch PetscCall(PetscGaussLobattoLegendreElementAdvectionDestroy(appctx->SEMop.gll.n,appctx->SEMop.gll.nodes,appctx->SEMop.gll.weights,&temp)); 538c4762a1bSJed Brown PetscFunctionReturn(0); 539c4762a1bSJed Brown } 540c4762a1bSJed Brown /* ------------------------------------------------------------------ */ 541c4762a1bSJed Brown /* 542c4762a1bSJed Brown FormFunctionGradient - Evaluates the function and corresponding gradient. 543c4762a1bSJed Brown 544c4762a1bSJed Brown Input Parameters: 545c4762a1bSJed Brown tao - the Tao context 546c4762a1bSJed Brown IC - the input vector 547a82e8c82SStefano Zampini ctx - optional user-defined context, as set when calling TaoSetObjectiveAndGradient() 548c4762a1bSJed Brown 549c4762a1bSJed Brown Output Parameters: 550c4762a1bSJed Brown f - the newly evaluated function 551c4762a1bSJed Brown G - the newly evaluated gradient 552c4762a1bSJed Brown 553c4762a1bSJed Brown Notes: 554c4762a1bSJed Brown 555c4762a1bSJed Brown The forward equation is 556c4762a1bSJed Brown M u_t = F(U) 557c4762a1bSJed Brown which is converted to 558c4762a1bSJed Brown u_t = M^{-1} F(u) 559c4762a1bSJed Brown in the user code since TS has no direct way of providing a mass matrix. The Jacobian of this is 560c4762a1bSJed Brown M^{-1} J 561c4762a1bSJed Brown where J is the Jacobian of F. Now the adjoint equation is 562c4762a1bSJed Brown M v_t = J^T v 563c4762a1bSJed Brown but TSAdjoint does not solve this since it can only solve the transposed system for the 564c4762a1bSJed Brown Jacobian the user provided. Hence TSAdjoint solves 565c4762a1bSJed Brown w_t = J^T M^{-1} w (where w = M v) 566a5b23f4aSJose E. Roman since there is no way to indicate the mass matrix as a separate entity to TS. Thus one 567c4762a1bSJed Brown must be careful in initializing the "adjoint equation" and using the result. This is 568c4762a1bSJed Brown why 569c4762a1bSJed Brown G = -2 M(u(T) - u_d) 570c4762a1bSJed Brown below (instead of -2(u(T) - u_d) and why the result is 571c4762a1bSJed Brown G = G/appctx->SEMop.mass (that is G = M^{-1}w) 572c4762a1bSJed Brown below (instead of just the result of the "adjoint solve"). 573c4762a1bSJed Brown 574c4762a1bSJed Brown */ 575c4762a1bSJed Brown PetscErrorCode FormFunctionGradient(Tao tao,Vec IC,PetscReal *f,Vec G,void *ctx) 576c4762a1bSJed Brown { 577c4762a1bSJed Brown AppCtx *appctx = (AppCtx*)ctx; /* user-defined application context */ 578c4762a1bSJed Brown Vec temp; 579c4762a1bSJed Brown PetscInt its; 580c4762a1bSJed Brown PetscReal ff, gnorm, cnorm, xdiff,errex; 581c4762a1bSJed Brown TaoConvergedReason reason; 582c4762a1bSJed Brown 583c4762a1bSJed Brown PetscFunctionBegin; 584*9566063dSJacob Faibussowitsch PetscCall(TSSetTime(appctx->ts,0.0)); 585*9566063dSJacob Faibussowitsch PetscCall(TSSetStepNumber(appctx->ts,0)); 586*9566063dSJacob Faibussowitsch PetscCall(TSSetTimeStep(appctx->ts,appctx->initial_dt)); 587*9566063dSJacob Faibussowitsch PetscCall(VecCopy(IC,appctx->dat.curr_sol)); 588c4762a1bSJed Brown 589*9566063dSJacob Faibussowitsch PetscCall(TSSolve(appctx->ts,appctx->dat.curr_sol)); 590c4762a1bSJed Brown 591*9566063dSJacob Faibussowitsch PetscCall(VecWAXPY(G,-1.0,appctx->dat.curr_sol,appctx->dat.obj)); 592c4762a1bSJed Brown 593c4762a1bSJed Brown /* 594c4762a1bSJed Brown Compute the L2-norm of the objective function, cost function is f 595c4762a1bSJed Brown */ 596*9566063dSJacob Faibussowitsch PetscCall(VecDuplicate(G,&temp)); 597*9566063dSJacob Faibussowitsch PetscCall(VecPointwiseMult(temp,G,G)); 598*9566063dSJacob Faibussowitsch PetscCall(VecDot(temp,appctx->SEMop.mass,f)); 599c4762a1bSJed Brown 600c4762a1bSJed Brown /* local error evaluation */ 601*9566063dSJacob Faibussowitsch PetscCall(VecWAXPY(temp,-1.0,appctx->dat.ic,appctx->dat.true_solution)); 602*9566063dSJacob Faibussowitsch PetscCall(VecPointwiseMult(temp,temp,temp)); 603c4762a1bSJed Brown /* for error evaluation */ 604*9566063dSJacob Faibussowitsch PetscCall(VecDot(temp,appctx->SEMop.mass,&errex)); 605*9566063dSJacob Faibussowitsch PetscCall(VecDestroy(&temp)); 606c4762a1bSJed Brown errex = PetscSqrtReal(errex); 607c4762a1bSJed Brown 608c4762a1bSJed Brown /* 609c4762a1bSJed Brown Compute initial conditions for the adjoint integration. See Notes above 610c4762a1bSJed Brown */ 611c4762a1bSJed Brown 612*9566063dSJacob Faibussowitsch PetscCall(VecScale(G, -2.0)); 613*9566063dSJacob Faibussowitsch PetscCall(VecPointwiseMult(G,G,appctx->SEMop.mass)); 614*9566063dSJacob Faibussowitsch PetscCall(TSSetCostGradients(appctx->ts,1,&G,NULL)); 615*9566063dSJacob Faibussowitsch PetscCall(TSAdjointSolve(appctx->ts)); 616*9566063dSJacob Faibussowitsch PetscCall(VecPointwiseDivide(G,G,appctx->SEMop.mass)); 617c4762a1bSJed Brown 618*9566063dSJacob Faibussowitsch PetscCall(TaoGetSolutionStatus(tao, &its, &ff, &gnorm, &cnorm, &xdiff, &reason)); 619c4762a1bSJed Brown PetscFunctionReturn(0); 620c4762a1bSJed Brown } 621c4762a1bSJed Brown 622c4762a1bSJed Brown PetscErrorCode MonitorError(Tao tao,void *ctx) 623c4762a1bSJed Brown { 624c4762a1bSJed Brown AppCtx *appctx = (AppCtx*)ctx; 625c4762a1bSJed Brown Vec temp; 626c4762a1bSJed Brown PetscReal nrm; 627c4762a1bSJed Brown 628c4762a1bSJed Brown PetscFunctionBegin; 629*9566063dSJacob Faibussowitsch PetscCall(VecDuplicate(appctx->dat.ic,&temp)); 630*9566063dSJacob Faibussowitsch PetscCall(VecWAXPY(temp,-1.0,appctx->dat.ic,appctx->dat.true_solution)); 631*9566063dSJacob Faibussowitsch PetscCall(VecPointwiseMult(temp,temp,temp)); 632*9566063dSJacob Faibussowitsch PetscCall(VecDot(temp,appctx->SEMop.mass,&nrm)); 633*9566063dSJacob Faibussowitsch PetscCall(VecDestroy(&temp)); 634c4762a1bSJed Brown nrm = PetscSqrtReal(nrm); 635*9566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Error for initial conditions %g\n",(double)nrm)); 636c4762a1bSJed Brown PetscFunctionReturn(0); 637c4762a1bSJed Brown } 638c4762a1bSJed Brown 639c4762a1bSJed Brown /*TEST 640c4762a1bSJed Brown 641c4762a1bSJed Brown build: 642c4762a1bSJed Brown requires: !complex 643c4762a1bSJed Brown 644c4762a1bSJed Brown test: 645c4762a1bSJed Brown args: -tao_max_it 5 -tao_gatol 1.e-4 646c4762a1bSJed Brown requires: !single 647c4762a1bSJed Brown 648c4762a1bSJed Brown test: 649c4762a1bSJed Brown suffix: 2 650c4762a1bSJed Brown nsize: 2 651c4762a1bSJed Brown args: -tao_max_it 5 -tao_gatol 1.e-4 652c4762a1bSJed Brown requires: !single 653c4762a1bSJed Brown 654c4762a1bSJed Brown TEST*/ 655