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 /* ------------------------------------------------------------------------ 11c4762a1bSJed Brown 12c4762a1bSJed Brown This program uses the one-dimensional Burger's equation 13c4762a1bSJed Brown u_t = mu*u_xx - u u_x, 14c4762a1bSJed Brown on the domain 0 <= x <= 1, with periodic boundary conditions 15c4762a1bSJed Brown 16c4762a1bSJed Brown to demonstrate solving a data assimilation problem of finding the initial conditions 17c4762a1bSJed Brown to produce a given solution at a fixed time. 18c4762a1bSJed Brown 19c4762a1bSJed Brown The operators are discretized with the spectral element method 20c4762a1bSJed Brown 21c4762a1bSJed Brown See the paper PDE-CONSTRAINED OPTIMIZATION WITH SPECTRAL ELEMENTS USING PETSC AND TAO 22c4762a1bSJed Brown by OANA MARIN, EMIL CONSTANTINESCU, AND BARRY SMITH for details on the exact solution 23c4762a1bSJed Brown used 24c4762a1bSJed Brown 25c4762a1bSJed Brown ------------------------------------------------------------------------- */ 26c4762a1bSJed Brown 27c4762a1bSJed Brown #include <petsctao.h> 28c4762a1bSJed Brown #include <petscts.h> 29c4762a1bSJed Brown #include <petscdt.h> 30c4762a1bSJed Brown #include <petscdraw.h> 31c4762a1bSJed Brown #include <petscdmda.h> 32c4762a1bSJed Brown 33c4762a1bSJed Brown /* 34c4762a1bSJed Brown User-defined application context - contains data needed by the 35c4762a1bSJed Brown application-provided call-back routines. 36c4762a1bSJed Brown */ 37c4762a1bSJed Brown 38c4762a1bSJed Brown typedef struct { 39c4762a1bSJed Brown PetscInt n; /* number of nodes */ 40c4762a1bSJed Brown PetscReal *nodes; /* GLL nodes */ 41c4762a1bSJed Brown PetscReal *weights; /* GLL weights */ 42c4762a1bSJed Brown } PetscGLL; 43c4762a1bSJed Brown 44c4762a1bSJed Brown typedef struct { 45c4762a1bSJed Brown PetscInt N; /* grid points per elements*/ 46c4762a1bSJed Brown PetscInt E; /* number of elements */ 47c4762a1bSJed Brown PetscReal tol_L2, tol_max; /* error norms */ 48c4762a1bSJed Brown PetscInt steps; /* number of timesteps */ 49c4762a1bSJed Brown PetscReal Tend; /* endtime */ 50c4762a1bSJed Brown PetscReal mu; /* viscosity */ 51c4762a1bSJed Brown PetscReal L; /* total length of domain */ 52c4762a1bSJed Brown PetscReal Le; 53c4762a1bSJed Brown PetscReal Tadj; 54c4762a1bSJed Brown } PetscParam; 55c4762a1bSJed Brown 56c4762a1bSJed Brown typedef struct { 57c4762a1bSJed Brown Vec obj; /* desired end state */ 58c4762a1bSJed Brown Vec grid; /* total grid */ 59c4762a1bSJed Brown Vec grad; 60c4762a1bSJed Brown Vec ic; 61c4762a1bSJed Brown Vec curr_sol; 62c4762a1bSJed Brown Vec true_solution; /* actual initial conditions for the final solution */ 63c4762a1bSJed Brown } PetscData; 64c4762a1bSJed Brown 65c4762a1bSJed Brown typedef struct { 66c4762a1bSJed Brown Vec grid; /* total grid */ 67c4762a1bSJed Brown Vec mass; /* mass matrix for total integration */ 68c4762a1bSJed Brown Mat stiff; /* stifness matrix */ 69c4762a1bSJed Brown Mat keptstiff; 70c4762a1bSJed Brown Mat grad; 71c4762a1bSJed Brown PetscGLL gll; 72c4762a1bSJed Brown } PetscSEMOperators; 73c4762a1bSJed Brown 74c4762a1bSJed Brown typedef struct { 75c4762a1bSJed Brown DM da; /* distributed array data structure */ 76c4762a1bSJed Brown PetscSEMOperators SEMop; 77c4762a1bSJed Brown PetscParam param; 78c4762a1bSJed Brown PetscData dat; 79c4762a1bSJed Brown TS ts; 80c4762a1bSJed Brown PetscReal initial_dt; 81c4762a1bSJed Brown } AppCtx; 82c4762a1bSJed Brown 83c4762a1bSJed Brown /* 84c4762a1bSJed Brown User-defined routines 85c4762a1bSJed Brown */ 86c4762a1bSJed Brown extern PetscErrorCode FormFunctionGradient(Tao, Vec, PetscReal *, Vec, void *); 87c4762a1bSJed Brown extern PetscErrorCode RHSMatrixLaplaciangllDM(TS, PetscReal, Vec, Mat, Mat, void *); 88c4762a1bSJed Brown extern PetscErrorCode RHSMatrixAdvectiongllDM(TS, PetscReal, Vec, Mat, Mat, void *); 89c4762a1bSJed Brown extern PetscErrorCode InitialConditions(Vec, AppCtx *); 90c4762a1bSJed Brown extern PetscErrorCode TrueSolution(Vec, AppCtx *); 91c4762a1bSJed Brown extern PetscErrorCode ComputeObjective(PetscReal, Vec, AppCtx *); 92c4762a1bSJed Brown extern PetscErrorCode MonitorError(Tao, void *); 93c4762a1bSJed Brown extern PetscErrorCode RHSFunction(TS, PetscReal, Vec, Vec, void *); 94c4762a1bSJed Brown extern PetscErrorCode RHSJacobian(TS, PetscReal, Vec, Mat, Mat, void *); 95c4762a1bSJed Brown 96d71ae5a4SJacob Faibussowitsch int main(int argc, char **argv) 97d71ae5a4SJacob Faibussowitsch { 98c4762a1bSJed Brown AppCtx appctx; /* user-defined application context */ 99c4762a1bSJed Brown Tao tao; 100c4762a1bSJed Brown Vec u; /* approximate solution vector */ 101c4762a1bSJed Brown PetscInt i, xs, xm, ind, j, lenglob; 102c4762a1bSJed Brown PetscReal x, *wrk_ptr1, *wrk_ptr2; 103c4762a1bSJed Brown MatNullSpace nsp; 104c4762a1bSJed Brown PetscMPIInt size; 105c4762a1bSJed Brown 106c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 107c4762a1bSJed Brown Initialize program and set problem parameters 108c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 109c4762a1bSJed Brown PetscFunctionBegin; 110c4762a1bSJed Brown 111327415f7SBarry Smith PetscFunctionBeginUser; 1129566063dSJacob Faibussowitsch PetscCall(PetscInitialize(&argc, &argv, (char *)0, help)); 113c4762a1bSJed Brown 114c4762a1bSJed Brown /*initialize parameters */ 115c4762a1bSJed Brown appctx.param.N = 10; /* order of the spectral element */ 116c4762a1bSJed Brown appctx.param.E = 10; /* number of elements */ 117c4762a1bSJed Brown appctx.param.L = 4.0; /* length of the domain */ 118c4762a1bSJed Brown appctx.param.mu = 0.01; /* diffusion coefficient */ 119c4762a1bSJed Brown appctx.initial_dt = 5e-3; 120c4762a1bSJed Brown appctx.param.steps = PETSC_MAX_INT; 121c4762a1bSJed Brown appctx.param.Tend = 4; 122c4762a1bSJed Brown 1239566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &appctx.param.N, NULL)); 1249566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetInt(NULL, NULL, "-E", &appctx.param.E, NULL)); 1259566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(NULL, NULL, "-Tend", &appctx.param.Tend, NULL)); 1269566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(NULL, NULL, "-mu", &appctx.param.mu, NULL)); 127c4762a1bSJed Brown appctx.param.Le = appctx.param.L / appctx.param.E; 128c4762a1bSJed Brown 1299566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size)); 1303c859ba3SBarry Smith PetscCheck((appctx.param.E % size) == 0, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Number of elements must be divisible by number of processes"); 131c4762a1bSJed Brown 132c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 133c4762a1bSJed Brown Create GLL data structures 134c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 1359566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(appctx.param.N, &appctx.SEMop.gll.nodes, appctx.param.N, &appctx.SEMop.gll.weights)); 1369566063dSJacob Faibussowitsch PetscCall(PetscDTGaussLobattoLegendreQuadrature(appctx.param.N, PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA, appctx.SEMop.gll.nodes, appctx.SEMop.gll.weights)); 137c4762a1bSJed Brown appctx.SEMop.gll.n = appctx.param.N; 138c4762a1bSJed Brown lenglob = appctx.param.E * (appctx.param.N - 1); 139c4762a1bSJed Brown 140c4762a1bSJed Brown /* 141c4762a1bSJed Brown Create distributed array (DMDA) to manage parallel grid and vectors 142c4762a1bSJed Brown and to set up the ghost point communication pattern. There are E*(Nl-1)+1 143c4762a1bSJed Brown total grid values spread equally among all the processors, except first and last 144c4762a1bSJed Brown */ 145c4762a1bSJed Brown 1469566063dSJacob Faibussowitsch PetscCall(DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_PERIODIC, lenglob, 1, 1, NULL, &appctx.da)); 1479566063dSJacob Faibussowitsch PetscCall(DMSetFromOptions(appctx.da)); 1489566063dSJacob Faibussowitsch PetscCall(DMSetUp(appctx.da)); 149c4762a1bSJed Brown 150c4762a1bSJed Brown /* 151c4762a1bSJed Brown Extract global and local vectors from DMDA; we use these to store the 152c4762a1bSJed Brown approximate solution. Then duplicate these for remaining vectors that 153c4762a1bSJed Brown have the same types. 154c4762a1bSJed Brown */ 155c4762a1bSJed Brown 1569566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(appctx.da, &u)); 1579566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u, &appctx.dat.ic)); 1589566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u, &appctx.dat.true_solution)); 1599566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u, &appctx.dat.obj)); 1609566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u, &appctx.SEMop.grid)); 1619566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u, &appctx.SEMop.mass)); 1629566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u, &appctx.dat.curr_sol)); 163c4762a1bSJed Brown 1649566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(appctx.da, &xs, NULL, NULL, &xm, NULL, NULL)); 1659566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArray(appctx.da, appctx.SEMop.grid, &wrk_ptr1)); 1669566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArray(appctx.da, appctx.SEMop.mass, &wrk_ptr2)); 167c4762a1bSJed Brown 168c4762a1bSJed Brown /* Compute function over the locally owned part of the grid */ 169c4762a1bSJed Brown 170c4762a1bSJed Brown xs = xs / (appctx.param.N - 1); 171c4762a1bSJed Brown xm = xm / (appctx.param.N - 1); 172c4762a1bSJed Brown 173c4762a1bSJed Brown /* 174c4762a1bSJed Brown Build total grid and mass over entire mesh (multi-elemental) 175c4762a1bSJed Brown */ 176c4762a1bSJed Brown 177c4762a1bSJed Brown for (i = xs; i < xs + xm; i++) { 178c4762a1bSJed Brown for (j = 0; j < appctx.param.N - 1; j++) { 179c4762a1bSJed Brown x = (appctx.param.Le / 2.0) * (appctx.SEMop.gll.nodes[j] + 1.0) + appctx.param.Le * i; 180c4762a1bSJed Brown ind = i * (appctx.param.N - 1) + j; 181c4762a1bSJed Brown wrk_ptr1[ind] = x; 182c4762a1bSJed Brown wrk_ptr2[ind] = .5 * appctx.param.Le * appctx.SEMop.gll.weights[j]; 183c4762a1bSJed Brown if (j == 0) wrk_ptr2[ind] += .5 * appctx.param.Le * appctx.SEMop.gll.weights[j]; 184c4762a1bSJed Brown } 185c4762a1bSJed Brown } 1869566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArray(appctx.da, appctx.SEMop.grid, &wrk_ptr1)); 1879566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArray(appctx.da, appctx.SEMop.mass, &wrk_ptr2)); 188c4762a1bSJed Brown 189c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 190c4762a1bSJed Brown Create matrix data structure; set matrix evaluation routine. 191c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 1929566063dSJacob Faibussowitsch PetscCall(DMSetMatrixPreallocateOnly(appctx.da, PETSC_TRUE)); 1939566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix(appctx.da, &appctx.SEMop.stiff)); 1949566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix(appctx.da, &appctx.SEMop.grad)); 195c4762a1bSJed Brown /* 196c4762a1bSJed Brown For linear problems with a time-dependent f(u,t) in the equation 197c4762a1bSJed Brown u_t = f(u,t), the user provides the discretized right-hand-side 198c4762a1bSJed Brown as a time-dependent matrix. 199c4762a1bSJed Brown */ 2009566063dSJacob Faibussowitsch PetscCall(RHSMatrixLaplaciangllDM(appctx.ts, 0.0, u, appctx.SEMop.stiff, appctx.SEMop.stiff, &appctx)); 2019566063dSJacob Faibussowitsch PetscCall(RHSMatrixAdvectiongllDM(appctx.ts, 0.0, u, appctx.SEMop.grad, appctx.SEMop.grad, &appctx)); 202c4762a1bSJed Brown /* 203c4762a1bSJed Brown For linear problems with a time-dependent f(u,t) in the equation 204c4762a1bSJed Brown u_t = f(u,t), the user provides the discretized right-hand-side 205c4762a1bSJed Brown as a time-dependent matrix. 206c4762a1bSJed Brown */ 207c4762a1bSJed Brown 2089566063dSJacob Faibussowitsch PetscCall(MatDuplicate(appctx.SEMop.stiff, MAT_COPY_VALUES, &appctx.SEMop.keptstiff)); 209c4762a1bSJed Brown 210c4762a1bSJed Brown /* attach the null space to the matrix, this probably is not needed but does no harm */ 2119566063dSJacob Faibussowitsch PetscCall(MatNullSpaceCreate(PETSC_COMM_WORLD, PETSC_TRUE, 0, NULL, &nsp)); 2129566063dSJacob Faibussowitsch PetscCall(MatSetNullSpace(appctx.SEMop.stiff, nsp)); 2139566063dSJacob Faibussowitsch PetscCall(MatSetNullSpace(appctx.SEMop.keptstiff, nsp)); 2149566063dSJacob Faibussowitsch PetscCall(MatNullSpaceTest(nsp, appctx.SEMop.stiff, NULL)); 2159566063dSJacob Faibussowitsch PetscCall(MatNullSpaceDestroy(&nsp)); 216c4762a1bSJed Brown /* attach the null space to the matrix, this probably is not needed but does no harm */ 2179566063dSJacob Faibussowitsch PetscCall(MatNullSpaceCreate(PETSC_COMM_WORLD, PETSC_TRUE, 0, NULL, &nsp)); 2189566063dSJacob Faibussowitsch PetscCall(MatSetNullSpace(appctx.SEMop.grad, nsp)); 2199566063dSJacob Faibussowitsch PetscCall(MatNullSpaceTest(nsp, appctx.SEMop.grad, NULL)); 2209566063dSJacob Faibussowitsch PetscCall(MatNullSpaceDestroy(&nsp)); 221c4762a1bSJed Brown 222c4762a1bSJed Brown /* Create the TS solver that solves the ODE and its adjoint; set its options */ 2239566063dSJacob Faibussowitsch PetscCall(TSCreate(PETSC_COMM_WORLD, &appctx.ts)); 2249566063dSJacob Faibussowitsch PetscCall(TSSetProblemType(appctx.ts, TS_NONLINEAR)); 2259566063dSJacob Faibussowitsch PetscCall(TSSetType(appctx.ts, TSRK)); 2269566063dSJacob Faibussowitsch PetscCall(TSSetDM(appctx.ts, appctx.da)); 2279566063dSJacob Faibussowitsch PetscCall(TSSetTime(appctx.ts, 0.0)); 2289566063dSJacob Faibussowitsch PetscCall(TSSetTimeStep(appctx.ts, appctx.initial_dt)); 2299566063dSJacob Faibussowitsch PetscCall(TSSetMaxSteps(appctx.ts, appctx.param.steps)); 2309566063dSJacob Faibussowitsch PetscCall(TSSetMaxTime(appctx.ts, appctx.param.Tend)); 2319566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTime(appctx.ts, TS_EXACTFINALTIME_MATCHSTEP)); 2329566063dSJacob Faibussowitsch PetscCall(TSSetTolerances(appctx.ts, 1e-7, NULL, 1e-7, NULL)); 2339566063dSJacob Faibussowitsch PetscCall(TSSetFromOptions(appctx.ts)); 234c4762a1bSJed Brown /* Need to save initial timestep user may have set with -ts_dt so it can be reset for each new TSSolve() */ 2359566063dSJacob Faibussowitsch PetscCall(TSGetTimeStep(appctx.ts, &appctx.initial_dt)); 2369566063dSJacob Faibussowitsch PetscCall(TSSetRHSFunction(appctx.ts, NULL, RHSFunction, &appctx)); 2379566063dSJacob Faibussowitsch PetscCall(TSSetRHSJacobian(appctx.ts, appctx.SEMop.stiff, appctx.SEMop.stiff, RHSJacobian, &appctx)); 238c4762a1bSJed Brown 239c4762a1bSJed Brown /* Set Objective and Initial conditions for the problem and compute Objective function (evolution of true_solution to final time */ 2409566063dSJacob Faibussowitsch PetscCall(InitialConditions(appctx.dat.ic, &appctx)); 2419566063dSJacob Faibussowitsch PetscCall(TrueSolution(appctx.dat.true_solution, &appctx)); 2429566063dSJacob Faibussowitsch PetscCall(ComputeObjective(appctx.param.Tend, appctx.dat.obj, &appctx)); 243c4762a1bSJed Brown 2449566063dSJacob Faibussowitsch PetscCall(TSSetSaveTrajectory(appctx.ts)); 2459566063dSJacob Faibussowitsch PetscCall(TSSetFromOptions(appctx.ts)); 246f32d6360SSatish Balay 247c4762a1bSJed Brown /* Create TAO solver and set desired solution method */ 2489566063dSJacob Faibussowitsch PetscCall(TaoCreate(PETSC_COMM_WORLD, &tao)); 2499566063dSJacob Faibussowitsch PetscCall(TaoSetMonitor(tao, MonitorError, &appctx, NULL)); 2509566063dSJacob Faibussowitsch PetscCall(TaoSetType(tao, TAOBQNLS)); 2519566063dSJacob Faibussowitsch PetscCall(TaoSetSolution(tao, appctx.dat.ic)); 252c4762a1bSJed Brown /* Set routine for function and gradient evaluation */ 2539566063dSJacob Faibussowitsch PetscCall(TaoSetObjectiveAndGradient(tao, NULL, FormFunctionGradient, (void *)&appctx)); 254c4762a1bSJed Brown /* Check for any TAO command line options */ 2559566063dSJacob Faibussowitsch PetscCall(TaoSetTolerances(tao, 1e-8, PETSC_DEFAULT, PETSC_DEFAULT)); 2569566063dSJacob Faibussowitsch PetscCall(TaoSetFromOptions(tao)); 2579566063dSJacob Faibussowitsch PetscCall(TaoSolve(tao)); 258c4762a1bSJed Brown 2599566063dSJacob Faibussowitsch PetscCall(TaoDestroy(&tao)); 2609566063dSJacob Faibussowitsch PetscCall(MatDestroy(&appctx.SEMop.stiff)); 2619566063dSJacob Faibussowitsch PetscCall(MatDestroy(&appctx.SEMop.keptstiff)); 2629566063dSJacob Faibussowitsch PetscCall(MatDestroy(&appctx.SEMop.grad)); 2639566063dSJacob Faibussowitsch PetscCall(VecDestroy(&u)); 2649566063dSJacob Faibussowitsch PetscCall(VecDestroy(&appctx.dat.ic)); 2659566063dSJacob Faibussowitsch PetscCall(VecDestroy(&appctx.dat.true_solution)); 2669566063dSJacob Faibussowitsch PetscCall(VecDestroy(&appctx.dat.obj)); 2679566063dSJacob Faibussowitsch PetscCall(VecDestroy(&appctx.SEMop.grid)); 2689566063dSJacob Faibussowitsch PetscCall(VecDestroy(&appctx.SEMop.mass)); 2699566063dSJacob Faibussowitsch PetscCall(VecDestroy(&appctx.dat.curr_sol)); 2709566063dSJacob Faibussowitsch PetscCall(PetscFree2(appctx.SEMop.gll.nodes, appctx.SEMop.gll.weights)); 2719566063dSJacob Faibussowitsch PetscCall(DMDestroy(&appctx.da)); 2729566063dSJacob Faibussowitsch PetscCall(TSDestroy(&appctx.ts)); 273c4762a1bSJed Brown 274c4762a1bSJed Brown /* 275c4762a1bSJed Brown Always call PetscFinalize() before exiting a program. This routine 276c4762a1bSJed Brown - finalizes the PETSc libraries as well as MPI 277c4762a1bSJed Brown - provides summary and diagnostic information if certain runtime 278c4762a1bSJed Brown options are chosen (e.g., -log_summary). 279c4762a1bSJed Brown */ 2809566063dSJacob Faibussowitsch PetscCall(PetscFinalize()); 281b122ec5aSJacob Faibussowitsch return 0; 282c4762a1bSJed Brown } 283c4762a1bSJed Brown 284c4762a1bSJed Brown /* --------------------------------------------------------------------- */ 285c4762a1bSJed Brown /* 286c4762a1bSJed Brown InitialConditions - Computes the initial conditions for the Tao optimization solve (these are also initial conditions for the first TSSolve() 287c4762a1bSJed Brown 288c4762a1bSJed Brown The routine TrueSolution() computes the true solution for the Tao optimization solve which means they are the initial conditions for the objective function 289c4762a1bSJed Brown 290c4762a1bSJed Brown Input Parameter: 291c4762a1bSJed Brown u - uninitialized solution vector (global) 292c4762a1bSJed Brown appctx - user-defined application context 293c4762a1bSJed Brown 294c4762a1bSJed Brown Output Parameter: 295c4762a1bSJed Brown u - vector with solution at initial time (global) 296c4762a1bSJed Brown */ 297d71ae5a4SJacob Faibussowitsch PetscErrorCode InitialConditions(Vec u, AppCtx *appctx) 298d71ae5a4SJacob Faibussowitsch { 299c4762a1bSJed Brown PetscScalar *s; 300c4762a1bSJed Brown const PetscScalar *xg; 301c4762a1bSJed Brown PetscInt i, xs, xn; 302c4762a1bSJed Brown 303c4762a1bSJed Brown PetscFunctionBegin; 3049566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArray(appctx->da, u, &s)); 3059566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArrayRead(appctx->da, appctx->SEMop.grid, (void *)&xg)); 3069566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(appctx->da, &xs, NULL, NULL, &xn, NULL, NULL)); 307ad540459SPierre Jolivet for (i = xs; i < xs + xn; i++) 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)); 3089566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArray(appctx->da, u, &s)); 3099566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArrayRead(appctx->da, appctx->SEMop.grid, (void *)&xg)); 310*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 311c4762a1bSJed Brown } 312c4762a1bSJed Brown 313c4762a1bSJed Brown /* 314c4762a1bSJed Brown TrueSolution() computes the true solution for the Tao optimization solve which means they are the initial conditions for the objective function. 315c4762a1bSJed Brown 316a5b23f4aSJose E. Roman InitialConditions() computes the initial conditions for the beginning of the Tao iterations 317c4762a1bSJed Brown 318c4762a1bSJed Brown Input Parameter: 319c4762a1bSJed Brown u - uninitialized solution vector (global) 320c4762a1bSJed Brown appctx - user-defined application context 321c4762a1bSJed Brown 322c4762a1bSJed Brown Output Parameter: 323c4762a1bSJed Brown u - vector with solution at initial time (global) 324c4762a1bSJed Brown */ 325d71ae5a4SJacob Faibussowitsch PetscErrorCode TrueSolution(Vec u, AppCtx *appctx) 326d71ae5a4SJacob Faibussowitsch { 327c4762a1bSJed Brown PetscScalar *s; 328c4762a1bSJed Brown const PetscScalar *xg; 329c4762a1bSJed Brown PetscInt i, xs, xn; 330c4762a1bSJed Brown 331c4762a1bSJed Brown PetscFunctionBegin; 3329566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArray(appctx->da, u, &s)); 3339566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArrayRead(appctx->da, appctx->SEMop.grid, (void *)&xg)); 3349566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(appctx->da, &xs, NULL, NULL, &xn, NULL, NULL)); 335ad540459SPierre Jolivet for (i = xs; i < xs + xn; i++) s[i] = 2.0 * appctx->param.mu * PETSC_PI * PetscSinScalar(PETSC_PI * xg[i]) / (2.0 + PetscCosScalar(PETSC_PI * xg[i])); 3369566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArray(appctx->da, u, &s)); 3379566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArrayRead(appctx->da, appctx->SEMop.grid, (void *)&xg)); 338*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 339c4762a1bSJed Brown } 340c4762a1bSJed Brown /* --------------------------------------------------------------------- */ 341c4762a1bSJed Brown /* 342c4762a1bSJed Brown Sets the desired profile for the final end time 343c4762a1bSJed Brown 344c4762a1bSJed Brown Input Parameters: 345c4762a1bSJed Brown t - final time 346c4762a1bSJed Brown obj - vector storing the desired profile 347c4762a1bSJed Brown appctx - user-defined application context 348c4762a1bSJed Brown 349c4762a1bSJed Brown */ 350d71ae5a4SJacob Faibussowitsch PetscErrorCode ComputeObjective(PetscReal t, Vec obj, AppCtx *appctx) 351d71ae5a4SJacob Faibussowitsch { 352c4762a1bSJed Brown PetscScalar *s; 353c4762a1bSJed Brown const PetscScalar *xg; 354c4762a1bSJed Brown PetscInt i, xs, xn; 355c4762a1bSJed Brown 356c4762a1bSJed Brown PetscFunctionBegin; 3579566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArray(appctx->da, obj, &s)); 3589566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArrayRead(appctx->da, appctx->SEMop.grid, (void *)&xg)); 3599566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(appctx->da, &xs, NULL, NULL, &xn, NULL, NULL)); 360c4762a1bSJed Brown for (i = xs; i < xs + xn; i++) { 3619371c9d4SSatish Balay s[i] = 2.0 * appctx->param.mu * PETSC_PI * PetscSinScalar(PETSC_PI * xg[i]) * PetscExpScalar(-PETSC_PI * PETSC_PI * t * appctx->param.mu) / (2.0 + PetscExpScalar(-PETSC_PI * PETSC_PI * t * appctx->param.mu) * PetscCosScalar(PETSC_PI * xg[i])); 362c4762a1bSJed Brown } 3639566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArray(appctx->da, obj, &s)); 3649566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArrayRead(appctx->da, appctx->SEMop.grid, (void *)&xg)); 365*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 366c4762a1bSJed Brown } 367c4762a1bSJed Brown 368d71ae5a4SJacob Faibussowitsch PetscErrorCode RHSFunction(TS ts, PetscReal t, Vec globalin, Vec globalout, void *ctx) 369d71ae5a4SJacob Faibussowitsch { 370c4762a1bSJed Brown AppCtx *appctx = (AppCtx *)ctx; 371c4762a1bSJed Brown 372c4762a1bSJed Brown PetscFunctionBegin; 3739566063dSJacob Faibussowitsch PetscCall(MatMult(appctx->SEMop.grad, globalin, globalout)); /* grad u */ 3749566063dSJacob Faibussowitsch PetscCall(VecPointwiseMult(globalout, globalin, globalout)); /* u grad u */ 3759566063dSJacob Faibussowitsch PetscCall(VecScale(globalout, -1.0)); 3769566063dSJacob Faibussowitsch PetscCall(MatMultAdd(appctx->SEMop.keptstiff, globalin, globalout, globalout)); 377*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 378c4762a1bSJed Brown } 379c4762a1bSJed Brown 380c4762a1bSJed Brown /* 381c4762a1bSJed Brown 382c4762a1bSJed Brown K is the discretiziation of the Laplacian 383c4762a1bSJed Brown G is the discretization of the gradient 384c4762a1bSJed Brown 385c4762a1bSJed Brown Computes Jacobian of K u + diag(u) G u which is given by 386c4762a1bSJed Brown K + diag(u)G + diag(Gu) 387c4762a1bSJed Brown */ 388d71ae5a4SJacob Faibussowitsch PetscErrorCode RHSJacobian(TS ts, PetscReal t, Vec globalin, Mat A, Mat B, void *ctx) 389d71ae5a4SJacob Faibussowitsch { 390c4762a1bSJed Brown AppCtx *appctx = (AppCtx *)ctx; 391c4762a1bSJed Brown Vec Gglobalin; 392c4762a1bSJed Brown 393c4762a1bSJed Brown PetscFunctionBegin; 394c4762a1bSJed Brown /* A = diag(u) G */ 395c4762a1bSJed Brown 3969566063dSJacob Faibussowitsch PetscCall(MatCopy(appctx->SEMop.grad, A, SAME_NONZERO_PATTERN)); 3979566063dSJacob Faibussowitsch PetscCall(MatDiagonalScale(A, globalin, NULL)); 398c4762a1bSJed Brown 399c4762a1bSJed Brown /* A = A + diag(Gu) */ 4009566063dSJacob Faibussowitsch PetscCall(VecDuplicate(globalin, &Gglobalin)); 4019566063dSJacob Faibussowitsch PetscCall(MatMult(appctx->SEMop.grad, globalin, Gglobalin)); 4029566063dSJacob Faibussowitsch PetscCall(MatDiagonalSet(A, Gglobalin, ADD_VALUES)); 4039566063dSJacob Faibussowitsch PetscCall(VecDestroy(&Gglobalin)); 404c4762a1bSJed Brown 405c4762a1bSJed Brown /* A = K - A */ 4069566063dSJacob Faibussowitsch PetscCall(MatScale(A, -1.0)); 4079566063dSJacob Faibussowitsch PetscCall(MatAXPY(A, 1.0, appctx->SEMop.keptstiff, SAME_NONZERO_PATTERN)); 408*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 409c4762a1bSJed Brown } 410c4762a1bSJed Brown 411c4762a1bSJed Brown /* --------------------------------------------------------------------- */ 412c4762a1bSJed Brown 413c4762a1bSJed Brown /* 414c4762a1bSJed Brown RHSMatrixLaplacian - User-provided routine to compute the right-hand-side 415c4762a1bSJed Brown matrix for the heat equation. 416c4762a1bSJed Brown 417c4762a1bSJed Brown Input Parameters: 418c4762a1bSJed Brown ts - the TS context 419c4762a1bSJed Brown t - current time (ignored) 420c4762a1bSJed Brown X - current solution (ignored) 421c4762a1bSJed Brown dummy - optional user-defined context, as set by TSetRHSJacobian() 422c4762a1bSJed Brown 423c4762a1bSJed Brown Output Parameters: 424c4762a1bSJed Brown AA - Jacobian matrix 425c4762a1bSJed Brown BB - optionally different matrix from which the preconditioner is built 426c4762a1bSJed Brown str - flag indicating matrix structure 427c4762a1bSJed Brown 428c4762a1bSJed Brown */ 429d71ae5a4SJacob Faibussowitsch PetscErrorCode RHSMatrixLaplaciangllDM(TS ts, PetscReal t, Vec X, Mat A, Mat BB, void *ctx) 430d71ae5a4SJacob Faibussowitsch { 431c4762a1bSJed Brown PetscReal **temp; 432c4762a1bSJed Brown PetscReal vv; 433c4762a1bSJed Brown AppCtx *appctx = (AppCtx *)ctx; /* user-defined application context */ 434c4762a1bSJed Brown PetscInt i, xs, xn, l, j; 435c4762a1bSJed Brown PetscInt *rowsDM; 436c4762a1bSJed Brown 437c4762a1bSJed Brown PetscFunctionBegin; 438c4762a1bSJed Brown /* 439c4762a1bSJed Brown Creates the element stiffness matrix for the given gll 440c4762a1bSJed Brown */ 4419566063dSJacob Faibussowitsch PetscCall(PetscGaussLobattoLegendreElementLaplacianCreate(appctx->SEMop.gll.n, appctx->SEMop.gll.nodes, appctx->SEMop.gll.weights, &temp)); 442a5b23f4aSJose E. Roman /* workaround for clang analyzer warning: Division by zero */ 4433c859ba3SBarry Smith PetscCheck(appctx->param.N > 1, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Spectral element order should be > 1"); 444c4762a1bSJed Brown 445c4762a1bSJed Brown /* scale by the size of the element */ 446c4762a1bSJed Brown for (i = 0; i < appctx->param.N; i++) { 447c4762a1bSJed Brown vv = -appctx->param.mu * 2.0 / appctx->param.Le; 448c4762a1bSJed Brown for (j = 0; j < appctx->param.N; j++) temp[i][j] = temp[i][j] * vv; 449c4762a1bSJed Brown } 450c4762a1bSJed Brown 4519566063dSJacob Faibussowitsch PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE)); 4529566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(appctx->da, &xs, NULL, NULL, &xn, NULL, NULL)); 453c4762a1bSJed Brown 454c4762a1bSJed Brown xs = xs / (appctx->param.N - 1); 455c4762a1bSJed Brown xn = xn / (appctx->param.N - 1); 456c4762a1bSJed Brown 4579566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(appctx->param.N, &rowsDM)); 458c4762a1bSJed Brown /* 459c4762a1bSJed Brown loop over local elements 460c4762a1bSJed Brown */ 461c4762a1bSJed Brown for (j = xs; j < xs + xn; j++) { 462ad540459SPierre Jolivet for (l = 0; l < appctx->param.N; l++) rowsDM[l] = 1 + (j - xs) * (appctx->param.N - 1) + l; 4639566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(A, appctx->param.N, rowsDM, appctx->param.N, rowsDM, &temp[0][0], ADD_VALUES)); 464c4762a1bSJed Brown } 4659566063dSJacob Faibussowitsch PetscCall(PetscFree(rowsDM)); 4669566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 4679566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 4689566063dSJacob Faibussowitsch PetscCall(VecReciprocal(appctx->SEMop.mass)); 4699566063dSJacob Faibussowitsch PetscCall(MatDiagonalScale(A, appctx->SEMop.mass, 0)); 4709566063dSJacob Faibussowitsch PetscCall(VecReciprocal(appctx->SEMop.mass)); 471c4762a1bSJed Brown 4729566063dSJacob Faibussowitsch PetscCall(PetscGaussLobattoLegendreElementLaplacianDestroy(appctx->SEMop.gll.n, appctx->SEMop.gll.nodes, appctx->SEMop.gll.weights, &temp)); 473*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 474c4762a1bSJed Brown } 475c4762a1bSJed Brown 476c4762a1bSJed Brown /* 477c4762a1bSJed Brown RHSMatrixAdvection - User-provided routine to compute the right-hand-side 478c4762a1bSJed Brown matrix for the Advection equation. 479c4762a1bSJed Brown 480c4762a1bSJed Brown Input Parameters: 481c4762a1bSJed Brown ts - the TS context 482c4762a1bSJed Brown t - current time 483c4762a1bSJed Brown global_in - global input vector 484c4762a1bSJed Brown dummy - optional user-defined context, as set by TSetRHSJacobian() 485c4762a1bSJed Brown 486c4762a1bSJed Brown Output Parameters: 487c4762a1bSJed Brown AA - Jacobian matrix 488c4762a1bSJed Brown BB - optionally different preconditioning matrix 489c4762a1bSJed Brown str - flag indicating matrix structure 490c4762a1bSJed Brown 491c4762a1bSJed Brown */ 492d71ae5a4SJacob Faibussowitsch PetscErrorCode RHSMatrixAdvectiongllDM(TS ts, PetscReal t, Vec X, Mat A, Mat BB, void *ctx) 493d71ae5a4SJacob Faibussowitsch { 494c4762a1bSJed Brown PetscReal **temp; 495c4762a1bSJed Brown AppCtx *appctx = (AppCtx *)ctx; /* user-defined application context */ 496c4762a1bSJed Brown PetscInt xs, xn, l, j; 497c4762a1bSJed Brown PetscInt *rowsDM; 498c4762a1bSJed Brown 499c4762a1bSJed Brown PetscFunctionBegin; 500c4762a1bSJed Brown /* 501c4762a1bSJed Brown Creates the advection matrix for the given gll 502c4762a1bSJed Brown */ 5039566063dSJacob Faibussowitsch PetscCall(PetscGaussLobattoLegendreElementAdvectionCreate(appctx->SEMop.gll.n, appctx->SEMop.gll.nodes, appctx->SEMop.gll.weights, &temp)); 5049566063dSJacob Faibussowitsch PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE)); 505c4762a1bSJed Brown 5069566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(appctx->da, &xs, NULL, NULL, &xn, NULL, NULL)); 507c4762a1bSJed Brown 508c4762a1bSJed Brown xs = xs / (appctx->param.N - 1); 509c4762a1bSJed Brown xn = xn / (appctx->param.N - 1); 510c4762a1bSJed Brown 5119566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(appctx->param.N, &rowsDM)); 512c4762a1bSJed Brown for (j = xs; j < xs + xn; j++) { 513ad540459SPierre Jolivet for (l = 0; l < appctx->param.N; l++) rowsDM[l] = 1 + (j - xs) * (appctx->param.N - 1) + l; 5149566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(A, appctx->param.N, rowsDM, appctx->param.N, rowsDM, &temp[0][0], ADD_VALUES)); 515c4762a1bSJed Brown } 5169566063dSJacob Faibussowitsch PetscCall(PetscFree(rowsDM)); 5179566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 5189566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 519c4762a1bSJed Brown 5209566063dSJacob Faibussowitsch PetscCall(VecReciprocal(appctx->SEMop.mass)); 5219566063dSJacob Faibussowitsch PetscCall(MatDiagonalScale(A, appctx->SEMop.mass, 0)); 5229566063dSJacob Faibussowitsch PetscCall(VecReciprocal(appctx->SEMop.mass)); 5239566063dSJacob Faibussowitsch PetscCall(PetscGaussLobattoLegendreElementAdvectionDestroy(appctx->SEMop.gll.n, appctx->SEMop.gll.nodes, appctx->SEMop.gll.weights, &temp)); 524*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 525c4762a1bSJed Brown } 526c4762a1bSJed Brown /* ------------------------------------------------------------------ */ 527c4762a1bSJed Brown /* 528c4762a1bSJed Brown FormFunctionGradient - Evaluates the function and corresponding gradient. 529c4762a1bSJed Brown 530c4762a1bSJed Brown Input Parameters: 531c4762a1bSJed Brown tao - the Tao context 532c4762a1bSJed Brown IC - the input vector 533a82e8c82SStefano Zampini ctx - optional user-defined context, as set when calling TaoSetObjectiveAndGradient() 534c4762a1bSJed Brown 535c4762a1bSJed Brown Output Parameters: 536c4762a1bSJed Brown f - the newly evaluated function 537c4762a1bSJed Brown G - the newly evaluated gradient 538c4762a1bSJed Brown 539c4762a1bSJed Brown Notes: 540c4762a1bSJed Brown 541c4762a1bSJed Brown The forward equation is 542c4762a1bSJed Brown M u_t = F(U) 543c4762a1bSJed Brown which is converted to 544c4762a1bSJed Brown u_t = M^{-1} F(u) 545c4762a1bSJed Brown in the user code since TS has no direct way of providing a mass matrix. The Jacobian of this is 546c4762a1bSJed Brown M^{-1} J 547c4762a1bSJed Brown where J is the Jacobian of F. Now the adjoint equation is 548c4762a1bSJed Brown M v_t = J^T v 549c4762a1bSJed Brown but TSAdjoint does not solve this since it can only solve the transposed system for the 550c4762a1bSJed Brown Jacobian the user provided. Hence TSAdjoint solves 551c4762a1bSJed Brown w_t = J^T M^{-1} w (where w = M v) 552a5b23f4aSJose E. Roman since there is no way to indicate the mass matrix as a separate entity to TS. Thus one 553c4762a1bSJed Brown must be careful in initializing the "adjoint equation" and using the result. This is 554c4762a1bSJed Brown why 555c4762a1bSJed Brown G = -2 M(u(T) - u_d) 556c4762a1bSJed Brown below (instead of -2(u(T) - u_d) and why the result is 557c4762a1bSJed Brown G = G/appctx->SEMop.mass (that is G = M^{-1}w) 558c4762a1bSJed Brown below (instead of just the result of the "adjoint solve"). 559c4762a1bSJed Brown 560c4762a1bSJed Brown */ 561d71ae5a4SJacob Faibussowitsch PetscErrorCode FormFunctionGradient(Tao tao, Vec IC, PetscReal *f, Vec G, void *ctx) 562d71ae5a4SJacob Faibussowitsch { 563c4762a1bSJed Brown AppCtx *appctx = (AppCtx *)ctx; /* user-defined application context */ 564c4762a1bSJed Brown Vec temp; 565c4762a1bSJed Brown PetscInt its; 566c4762a1bSJed Brown PetscReal ff, gnorm, cnorm, xdiff, errex; 567c4762a1bSJed Brown TaoConvergedReason reason; 568c4762a1bSJed Brown 569c4762a1bSJed Brown PetscFunctionBegin; 5709566063dSJacob Faibussowitsch PetscCall(TSSetTime(appctx->ts, 0.0)); 5719566063dSJacob Faibussowitsch PetscCall(TSSetStepNumber(appctx->ts, 0)); 5729566063dSJacob Faibussowitsch PetscCall(TSSetTimeStep(appctx->ts, appctx->initial_dt)); 5739566063dSJacob Faibussowitsch PetscCall(VecCopy(IC, appctx->dat.curr_sol)); 574c4762a1bSJed Brown 5759566063dSJacob Faibussowitsch PetscCall(TSSolve(appctx->ts, appctx->dat.curr_sol)); 576c4762a1bSJed Brown 5779566063dSJacob Faibussowitsch PetscCall(VecWAXPY(G, -1.0, appctx->dat.curr_sol, appctx->dat.obj)); 578c4762a1bSJed Brown 579c4762a1bSJed Brown /* 580c4762a1bSJed Brown Compute the L2-norm of the objective function, cost function is f 581c4762a1bSJed Brown */ 5829566063dSJacob Faibussowitsch PetscCall(VecDuplicate(G, &temp)); 5839566063dSJacob Faibussowitsch PetscCall(VecPointwiseMult(temp, G, G)); 5849566063dSJacob Faibussowitsch PetscCall(VecDot(temp, appctx->SEMop.mass, f)); 585c4762a1bSJed Brown 586c4762a1bSJed Brown /* local error evaluation */ 5879566063dSJacob Faibussowitsch PetscCall(VecWAXPY(temp, -1.0, appctx->dat.ic, appctx->dat.true_solution)); 5889566063dSJacob Faibussowitsch PetscCall(VecPointwiseMult(temp, temp, temp)); 589c4762a1bSJed Brown /* for error evaluation */ 5909566063dSJacob Faibussowitsch PetscCall(VecDot(temp, appctx->SEMop.mass, &errex)); 5919566063dSJacob Faibussowitsch PetscCall(VecDestroy(&temp)); 592c4762a1bSJed Brown errex = PetscSqrtReal(errex); 593c4762a1bSJed Brown 594c4762a1bSJed Brown /* 595c4762a1bSJed Brown Compute initial conditions for the adjoint integration. See Notes above 596c4762a1bSJed Brown */ 597c4762a1bSJed Brown 5989566063dSJacob Faibussowitsch PetscCall(VecScale(G, -2.0)); 5999566063dSJacob Faibussowitsch PetscCall(VecPointwiseMult(G, G, appctx->SEMop.mass)); 6009566063dSJacob Faibussowitsch PetscCall(TSSetCostGradients(appctx->ts, 1, &G, NULL)); 6019566063dSJacob Faibussowitsch PetscCall(TSAdjointSolve(appctx->ts)); 6029566063dSJacob Faibussowitsch PetscCall(VecPointwiseDivide(G, G, appctx->SEMop.mass)); 603c4762a1bSJed Brown 6049566063dSJacob Faibussowitsch PetscCall(TaoGetSolutionStatus(tao, &its, &ff, &gnorm, &cnorm, &xdiff, &reason)); 605*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 606c4762a1bSJed Brown } 607c4762a1bSJed Brown 608d71ae5a4SJacob Faibussowitsch PetscErrorCode MonitorError(Tao tao, void *ctx) 609d71ae5a4SJacob Faibussowitsch { 610c4762a1bSJed Brown AppCtx *appctx = (AppCtx *)ctx; 611c4762a1bSJed Brown Vec temp; 612c4762a1bSJed Brown PetscReal nrm; 613c4762a1bSJed Brown 614c4762a1bSJed Brown PetscFunctionBegin; 6159566063dSJacob Faibussowitsch PetscCall(VecDuplicate(appctx->dat.ic, &temp)); 6169566063dSJacob Faibussowitsch PetscCall(VecWAXPY(temp, -1.0, appctx->dat.ic, appctx->dat.true_solution)); 6179566063dSJacob Faibussowitsch PetscCall(VecPointwiseMult(temp, temp, temp)); 6189566063dSJacob Faibussowitsch PetscCall(VecDot(temp, appctx->SEMop.mass, &nrm)); 6199566063dSJacob Faibussowitsch PetscCall(VecDestroy(&temp)); 620c4762a1bSJed Brown nrm = PetscSqrtReal(nrm); 6219566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Error for initial conditions %g\n", (double)nrm)); 622*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 623c4762a1bSJed Brown } 624c4762a1bSJed Brown 625c4762a1bSJed Brown /*TEST 626c4762a1bSJed Brown 627c4762a1bSJed Brown build: 628c4762a1bSJed Brown requires: !complex 629c4762a1bSJed Brown 630c4762a1bSJed Brown test: 631c4762a1bSJed Brown args: -tao_max_it 5 -tao_gatol 1.e-4 632c4762a1bSJed Brown requires: !single 633c4762a1bSJed Brown 634c4762a1bSJed Brown test: 635c4762a1bSJed Brown suffix: 2 636c4762a1bSJed Brown nsize: 2 637c4762a1bSJed Brown args: -tao_max_it 5 -tao_gatol 1.e-4 638c4762a1bSJed Brown requires: !single 639c4762a1bSJed Brown 640c4762a1bSJed Brown TEST*/ 641