Checks
HyperShape.Checks — Module
HyperShape.ChecksConsistency checks of the derivatives and of the declared dependencies: check_consistency.
HyperShape.Checks.CheckResult — Type
CheckResultOne line of a ConsistencyReport: label, passed, a message with the details (observed rates of the Taylor tests, relative differences) and applicable (false for a derivative check of a physical field without derivative, through Step(): neither passed nor failed, shown with –).
HyperShape.Checks.ConsistencyReport — Type
ConsistencyReportResult of check_consistency: results, a vector of CheckResult, passed, true when all of them passed (those not applicable do not count), and notes (how the check was done when it departs from the default, for example at a probe design). Displayed as a table.
HyperShape.Checks.check_consistency — Method
check_consistency(J1, J2, …; ctx = OptContext(:check), kwargs...) -> ConsistencyReport
check_consistency((name1 = J1, name2 = J2, …); ctx = OptContext(:check), kwargs...)Checks, at the current design, that the derivatives the user wrote and the dependencies the user declared are consistent. The functionals must share the same optimisation variables z. Four kinds of checks:
- Gradient of each functional (Taylor test):
J(z + hδ) − J(z) − h⟨∇J, δ⟩must decrease with order 2 whenhis halved. It covers the partial derivatives of the functional (dJdu,dJdθ), the adjoint and thesensitivityof the state, and the maps x → θ at once. - Design chain of each physical field (Taylor test of
½∫θ²through the maps alone, for everyPhysicalFieldsreached from the functionals): if it passes and the gradient of a functional fails, the error is in the derivatives of the functional or of the state or in the adjoint, not in the maps. - Symmetry of each filter:
cᵀA⁻¹b = bᵀA⁻¹cfor two random vectors, with the user's solve. - Dependencies: at a perturbed design, value and gradient computed as usual are compared with the ones obtained after forcing every state and filter reached through the declared dependencies to assemble and solve again. A difference means that one of those operators depends on something it does not declare (for example a filter matrix that reads the design with
depends = (), or a state listed independsthat under-declares its own dependencies). The check only changes the design: aParameterread by a model but not declared is not detected (it does not change while the check runs). An object that no declared dependency leads to cannot be checked: its stale values go unnoticed when the derivatives treat it as fixed too.
Optionally, tangents = (S => (res, jac), …) adds a Taylor test of the tangent jac(u, du, v) against the residual res(u, v) of the state S at its current solution, in a random direction of its free values (the user's forms in Gridap's convention; for HyperFEM's load-factor forms pass res(Λ), jac(Λ)).
A Taylor test passes when two consecutive steps give an observed order of at least min_rate (both errors above rounding), or when every error is at rounding level while J does change along δ (a functional linear in z). It fails, as not tested, when J does not change along δ at all (for example a fully saturated projection): choose another direction or design. It fails as inconclusive when only the largest step gives an error above rounding (a nearly linear functional): try a larger h0. With h0 = 1e-2 a relative error of about 0.1 % in a gradient can still pass.
Keyword arguments:
ctx: passed to the solve functions (defaultOptContext(:check)).direction: direction δ in z (default: random, scaled by the width of the bounds, zero within 1e-3 of the width from a bound). When every value of z is at a bound (the solid start of a level set) and no direction is given, the checks are done at a probe design inside the bounds,lb + (ub − lb)(0.5 + 0.25 sin(1.7 i))(a level set then crosses zero, so cells are cut), and the report says so in a note; z is restored afterwards as usual.h0: first step (default: 90 % of the largest step that keeps z + hδ inside the bounds, at most1e-2).steps = 5: number of steps of each Taylor test (h halved each time).seed = 1: seed of the random directions and vectors.min_rate = 1.8: observed order required.rtol = 1e-8: relative tolerance of the symmetry and dependency checks (increase it with iterative solvers or Newton tolerances).
Cost: each forward state is solved at most steps + 4 times and each adjoint at most 4 times (states shared by several functionals are solved once per design); each filter is solved once per step for checks 1 and 2, twice for check 3, and again whenever a gradient is pulled back. Use small meshes for nonlinear problems.
The verdict is kept with each functional whose gradient was tested (not one through Step()): the banner of the next solve! says whether every functional of the problem was checked and passed (derivatives: line); it is kept with the Functional object, so a check on another problem (a coarse mesh) does not count. On return, z holds its initial values and the fields and states are brought back to it (one more solve of each state). The scales of the functionals are the ones they had; a scale = :initial not yet fixed stays unfixed (its functional is checked unscaled), fixed scales are used as they are. If a dependency check fails, the operators may hold matrices assembled during the check: fix the depends and build them again. Errors thrown by the user's forms or solves propagate (z and the scales are restored first).