int main(int argc, char **argv) { DM dm; SNES snes; Vec u, r; AppCtx user; PetscErrorCode ierr; ierr = PetscInitialize(&argc, &argv, NULL, help);CHKERRQ(ierr); ierr = ProcessOptions(PETSC_COMM_WORLD, &user);CHKERRQ(ierr); ierr = SNESCreate(PETSC_COMM_WORLD, &snes);CHKERRQ(ierr); ierr = CreateMesh(PETSC_COMM_WORLD, &user, &dm);CHKERRQ(ierr); ierr = SNESSetDM(snes, dm);CHKERRQ(ierr); ierr = PetscMalloc(3 * sizeof(void (*)()), &user.exactFuncs);CHKERRQ(ierr); ierr = SetupDiscretization(dm, &user);CHKERRQ(ierr); ierr = DMCreateGlobalVector(dm, &u);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) u, "solution");CHKERRQ(ierr); ierr = VecDuplicate(u, &r);CHKERRQ(ierr); ierr = DMPlexSetSNESLocalFEM(dm,&user,&user,&user);CHKERRQ(ierr); ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); ierr = DMProjectFunction(dm, user.exactFuncs, NULL, INSERT_ALL_VALUES, u);CHKERRQ(ierr); ierr = DMSNESCheckFromOptions(snes, u, user.exactFuncs, NULL);CHKERRQ(ierr); if (user.runType == RUN_FULL) { PetscErrorCode (*initialGuess[3])(PetscInt dim, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx); PetscReal error; initialGuess[0] = zero; initialGuess[1] = zero; initialGuess[2] = zero; ierr = DMProjectFunction(dm, initialGuess, NULL, INSERT_VALUES, u);CHKERRQ(ierr); ierr = VecViewFromOptions(u, NULL, "-initial_vec_view");CHKERRQ(ierr); ierr = DMComputeL2Diff(dm, user.exactFuncs, NULL, u, &error);CHKERRQ(ierr); if (error < 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "Initial L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);} else {ierr = PetscPrintf(PETSC_COMM_WORLD, "Initial L_2 Error: %g\n", error);CHKERRQ(ierr);} ierr = SNESSolve(snes, NULL, u);CHKERRQ(ierr); ierr = DMComputeL2Diff(dm, user.exactFuncs, NULL, u, &error);CHKERRQ(ierr); if (error < 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "Final L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);} else {ierr = PetscPrintf(PETSC_COMM_WORLD, "Final L_2 Error: %g\n", error);CHKERRQ(ierr);} } ierr = VecViewFromOptions(u, NULL, "-sol_vec_view");CHKERRQ(ierr); ierr = VecDestroy(&u);CHKERRQ(ierr); ierr = VecDestroy(&r);CHKERRQ(ierr); ierr = SNESDestroy(&snes);CHKERRQ(ierr); ierr = DMDestroy(&dm);CHKERRQ(ierr); ierr = PetscFree(user.exactFuncs);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
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; }
int main(int argc, char **argv) { DM dm; /* Problem specification */ SNES snes; /* Nonlinear solver */ Vec u; /* Solutions */ AppCtx user; /* User-defined work context */ PetscErrorCode ierr; ierr = PetscInitialize(&argc, &argv, NULL,help);if (ierr) return ierr; ierr = ProcessOptions(PETSC_COMM_WORLD, &user);CHKERRQ(ierr); /* Primal system */ ierr = SNESCreate(PETSC_COMM_WORLD, &snes);CHKERRQ(ierr); ierr = CreateMesh(PETSC_COMM_WORLD, &user, &dm);CHKERRQ(ierr); ierr = SNESSetDM(snes, dm);CHKERRQ(ierr); ierr = SetupDiscretization(dm, "potential", SetupPrimalProblem, &user);CHKERRQ(ierr); ierr = DMCreateGlobalVector(dm, &u);CHKERRQ(ierr); ierr = VecSet(u, 0.0);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) u, "potential");CHKERRQ(ierr); ierr = DMPlexSetSNESLocalFEM(dm, &user, &user, &user);CHKERRQ(ierr); ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); ierr = SNESSolve(snes, NULL, u);CHKERRQ(ierr); ierr = SNESGetSolution(snes, &u);CHKERRQ(ierr); ierr = VecViewFromOptions(u, NULL, "-potential_view");CHKERRQ(ierr); if (user.spectral) { PetscInt planeDir[2] = {0, 1}; PetscReal planeCoord[2] = {0., 1.}; ierr = ComputeSpectral(dm, u, 2, planeDir, planeCoord, &user);CHKERRQ(ierr); } /* Adjoint system */ if (user.adjoint) { DM dmAdj; SNES snesAdj; Vec uAdj; ierr = SNESCreate(PETSC_COMM_WORLD, &snesAdj);CHKERRQ(ierr); ierr = PetscObjectSetOptionsPrefix((PetscObject) snesAdj, "adjoint_");CHKERRQ(ierr); ierr = DMClone(dm, &dmAdj);CHKERRQ(ierr); ierr = SNESSetDM(snesAdj, dmAdj);CHKERRQ(ierr); ierr = SetupDiscretization(dmAdj, "adjoint", SetupAdjointProblem, &user);CHKERRQ(ierr); ierr = DMCreateGlobalVector(dmAdj, &uAdj);CHKERRQ(ierr); ierr = VecSet(uAdj, 0.0);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) uAdj, "adjoint");CHKERRQ(ierr); ierr = DMPlexSetSNESLocalFEM(dmAdj, &user, &user, &user);CHKERRQ(ierr); ierr = SNESSetFromOptions(snesAdj);CHKERRQ(ierr); ierr = SNESSolve(snesAdj, NULL, uAdj);CHKERRQ(ierr); ierr = SNESGetSolution(snesAdj, &uAdj);CHKERRQ(ierr); ierr = VecViewFromOptions(uAdj, NULL, "-adjoint_view");CHKERRQ(ierr); /* Error representation */ { DM dmErr, dmErrAux, dms[2]; Vec errorEst, errorL2, uErr, uErrLoc, uAdjLoc, uAdjProj; IS *subis; PetscReal errorEstTot, errorL2Norm, errorL2Tot; PetscInt N, i; PetscErrorCode (*funcs[1])(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *) = {trig_u}; void (*identity[1])(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]) = {f0_identityaux_u}; void *ctxs[1] = {0}; ctxs[0] = &user; ierr = DMClone(dm, &dmErr);CHKERRQ(ierr); ierr = SetupDiscretization(dmErr, "error", SetupErrorProblem, &user);CHKERRQ(ierr); ierr = DMGetGlobalVector(dmErr, &errorEst);CHKERRQ(ierr); ierr = DMGetGlobalVector(dmErr, &errorL2);CHKERRQ(ierr); /* Compute auxiliary data (solution and projection of adjoint solution) */ ierr = DMGetLocalVector(dmAdj, &uAdjLoc);CHKERRQ(ierr); ierr = DMGlobalToLocalBegin(dmAdj, uAdj, INSERT_VALUES, uAdjLoc);CHKERRQ(ierr); ierr = DMGlobalToLocalEnd(dmAdj, uAdj, INSERT_VALUES, uAdjLoc);CHKERRQ(ierr); ierr = DMGetGlobalVector(dm, &uAdjProj);CHKERRQ(ierr); ierr = PetscObjectCompose((PetscObject) dm, "dmAux", (PetscObject) dmAdj);CHKERRQ(ierr); ierr = PetscObjectCompose((PetscObject) dm, "A", (PetscObject) uAdjLoc);CHKERRQ(ierr); ierr = DMProjectField(dm, 0.0, u, identity, INSERT_VALUES, uAdjProj);CHKERRQ(ierr); ierr = PetscObjectCompose((PetscObject) dm, "dmAux", NULL);CHKERRQ(ierr); ierr = PetscObjectCompose((PetscObject) dm, "A", NULL);CHKERRQ(ierr); ierr = DMRestoreLocalVector(dmAdj, &uAdjLoc);CHKERRQ(ierr); /* Attach auxiliary data */ dms[0] = dm; dms[1] = dm; ierr = DMCreateSuperDM(dms, 2, &subis, &dmErrAux);CHKERRQ(ierr); if (0) { PetscSection sec; ierr = DMGetSection(dms[0], &sec);CHKERRQ(ierr); ierr = PetscSectionView(sec, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = DMGetSection(dms[1], &sec);CHKERRQ(ierr); ierr = PetscSectionView(sec, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = DMGetSection(dmErrAux, &sec);CHKERRQ(ierr); ierr = PetscSectionView(sec, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } ierr = DMViewFromOptions(dmErrAux, NULL, "-dm_err_view");CHKERRQ(ierr); ierr = ISViewFromOptions(subis[0], NULL, "-super_is_view");CHKERRQ(ierr); ierr = ISViewFromOptions(subis[1], NULL, "-super_is_view");CHKERRQ(ierr); ierr = DMGetGlobalVector(dmErrAux, &uErr);CHKERRQ(ierr); ierr = VecViewFromOptions(u, NULL, "-map_vec_view");CHKERRQ(ierr); ierr = VecViewFromOptions(uAdjProj, NULL, "-map_vec_view");CHKERRQ(ierr); ierr = VecViewFromOptions(uErr, NULL, "-map_vec_view");CHKERRQ(ierr); ierr = VecISCopy(uErr, subis[0], SCATTER_FORWARD, u);CHKERRQ(ierr); ierr = VecISCopy(uErr, subis[1], SCATTER_FORWARD, uAdjProj);CHKERRQ(ierr); ierr = DMRestoreGlobalVector(dm, &uAdjProj);CHKERRQ(ierr); for (i = 0; i < 2; ++i) {ierr = ISDestroy(&subis[i]);CHKERRQ(ierr);} ierr = PetscFree(subis);CHKERRQ(ierr); ierr = DMGetLocalVector(dmErrAux, &uErrLoc);CHKERRQ(ierr); ierr = DMGlobalToLocalBegin(dm, uErr, INSERT_VALUES, uErrLoc);CHKERRQ(ierr); ierr = DMGlobalToLocalEnd(dm, uErr, INSERT_VALUES, uErrLoc);CHKERRQ(ierr); ierr = DMRestoreGlobalVector(dmErrAux, &uErr);CHKERRQ(ierr); ierr = PetscObjectCompose((PetscObject) dmAdj, "dmAux", (PetscObject) dmErrAux);CHKERRQ(ierr); ierr = PetscObjectCompose((PetscObject) dmAdj, "A", (PetscObject) uErrLoc);CHKERRQ(ierr); /* Compute cellwise error estimate */ ierr = VecSet(errorEst, 0.0);CHKERRQ(ierr); ierr = DMPlexComputeCellwiseIntegralFEM(dmAdj, uAdj, errorEst, &user);CHKERRQ(ierr); ierr = PetscObjectCompose((PetscObject) dmAdj, "dmAux", NULL);CHKERRQ(ierr); ierr = PetscObjectCompose((PetscObject) dmAdj, "A", NULL);CHKERRQ(ierr); ierr = DMRestoreLocalVector(dmErrAux, &uErrLoc);CHKERRQ(ierr); ierr = DMDestroy(&dmErrAux);CHKERRQ(ierr); /* Plot cellwise error vector */ ierr = VecViewFromOptions(errorEst, NULL, "-error_view");CHKERRQ(ierr); /* Compute ratio of estimate (sum over cells) with actual L_2 error */ ierr = DMComputeL2Diff(dm, 0.0, funcs, ctxs, u, &errorL2Norm);CHKERRQ(ierr); ierr = DMPlexComputeL2DiffVec(dm, 0.0, funcs, ctxs, u, errorL2);CHKERRQ(ierr); ierr = VecViewFromOptions(errorL2, NULL, "-l2_error_view");CHKERRQ(ierr); ierr = VecNorm(errorL2, NORM_INFINITY, &errorL2Tot);CHKERRQ(ierr); ierr = VecNorm(errorEst, NORM_INFINITY, &errorEstTot);CHKERRQ(ierr); ierr = VecGetSize(errorEst, &N);CHKERRQ(ierr); ierr = VecPointwiseDivide(errorEst, errorEst, errorL2);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) errorEst, "Error ratio");CHKERRQ(ierr); ierr = VecViewFromOptions(errorEst, NULL, "-error_ratio_view");CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "N: %D L2 error: %g Error Ratio: %g/%g = %g\n", N, (double) errorL2Norm, (double) errorEstTot, (double) PetscSqrtReal(errorL2Tot), (double) errorEstTot/PetscSqrtReal(errorL2Tot));CHKERRQ(ierr); ierr = DMRestoreGlobalVector(dmErr, &errorEst);CHKERRQ(ierr); ierr = DMRestoreGlobalVector(dmErr, &errorL2);CHKERRQ(ierr); ierr = DMDestroy(&dmErr);CHKERRQ(ierr); } ierr = DMDestroy(&dmAdj);CHKERRQ(ierr); ierr = VecDestroy(&uAdj);CHKERRQ(ierr); ierr = SNESDestroy(&snesAdj);CHKERRQ(ierr); } /* Cleanup */ ierr = VecDestroy(&u);CHKERRQ(ierr); ierr = SNESDestroy(&snes);CHKERRQ(ierr); ierr = DMDestroy(&dm);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }