Operators
HyperShape.Operators — Module
HyperShape.OperatorsOperators over HyperFEM computational models.
LinearOperator: auxiliary linear solves (filter, reaction–diffusion step, velocity extension). The model carries the bilinear form only and HyperShape supplies the right-hand side.StateOperator: forward states. The model carries the user's full weak forms; anAdjointProblemdeclares how to differentiate through it (the transposed solve and the formspᵀ ∂R/∂θ).
Both declare their dependencies (objects with a state_version counter), positionally or with depends = .... For a StaticLinearModel both have a default solve (default_solve); any other model needs the user's.
HyperShape.Operators.AbstractOperator — Type
AbstractOperatorCommon supertype of LinearOperator and StateOperator.
HyperShape.Operators.AbstractResponse — Type
AbstractResponseSupertype of the operators that a functional can be differentiated through: a forward state with its adjoint (StateOperator with an AdjointProblem). Declared for the reverse sweep of chains of states, trajectories and eigenproblems (later versions); its contract is not fixed yet.
HyperShape.Operators.AdjointCache — Type
AdjointCacheWhat HyperShape builds from an AdjointProblem when the StateOperator is constructed: the operator that solves Kᵀ p = b, the adjoint field p (a fixed object updated in place), the accumulated right-hand side and the assembler of the seeds. Internal.
HyperShape.Operators.AdjointProblem — Type
AdjointProblem(op; sensitivity, solve = <default>, depends = (), backend = :compiled)
AdjointProblem(; sensitivity)Adjoint problem of a forward StateOperator, given with its adjoint keyword. With R(u; θ)[v] the residual of the forward problem and K = ∂R/∂u its tangent, the adjoint p solves Kᵀ p = Σ ∂J/∂u (the seeds of the functionals that read the state, written by them in dJdu) and the state contributes − pᵀ ∂R/∂θ to the derivative with respect to the physical fields. HyperShape puts the signs; the user writes the partial derivatives.
op: who solvesKᵀ p = b. Either a HyperFEM model that carries the transposed tangent alone and the user's solver, usuallyStaticLinearModel((p, w) -> jac(uh, w, p), U0, V0, bc0; ls)(test and trial swapped, evaluated at the forward state, on a space with homogeneous Dirichlet values), orSymmetricState()for a linear and symmetric forward state (reuses its factorisation). Withoutop(second form) no adjoint is ever solved: onlySelfAdjointfunctionals can read the state (checked when they are built).sensitivity:(u, p) -> (δ -> ∫(…)dΩ), the linear formpᵀ ∂R/∂θ [δ](without sign) at the stateuand the adjointp, or a named tuple of such forms ((u, p) -> (ρ = δ -> …, E = δ -> …)) when θ has several fields. For aStaticLinearModelR(u)[v] = a(u, v) − l(v), so a load that depends on θ contributes− ∂l/∂θ. Written once per state, it serves every functional that reads the state.solve(only with a model): function(model, b, ctx, stale) -> xof aLinearOperator; defaultsolve!(m, b; Assembly = stale)for aStaticLinearModel(default_solve). The matrix is re-assembled (stale = true) only when the forward state changed, so the factorisation serves every gradient of a design.depends: objects that the adjoint matrix reads besides those of the forward state (aParameter, …).backend: who assembles the seeds∂J/∂uon the test space of the adjoint::compiled(HyperFEMKernels) or:lazy(Gridap).
Cost per gradient of a design: one adjoint solve (a back substitution after the first one) for all the functionals that read the state, none when they are all SelfAdjoint.
HyperShape.Operators.LinearOperator — Type
LinearOperator(model[, deps]; depends = (), solve = default_solve(LinearOperator, model))Linear solve with a matrix built by a HyperFEM model (typically a StaticLinearModel with the bilinear form alone) and a right-hand side supplied by HyperShape.
model: the computational model; its matrix and solver are the user's.depsordepends: the objects the matrix depends on (one object or a tuple; each implementsstate_version), given once. Default(): the matrix never changes. A dependency captured by the bilinear form and not declared gives stale results;check_consistencydetects it.solve: function(model, b, ctx, stale) -> xthat solves with the right-hand sideband returns the solution vector.staleistruewhen something in the dependencies changed since the last solve, so it can re-assemble. Default for aStaticLinearModel:solve!(m, b; Assembly = stale)(default_solve); mandatory for any other model.
The matrix must be the one the model assembled at construction (no dependency has changed yet). The returned vector is copied, so it may be a buffer of the model. invalidate! makes the next solve stale whatever the dependencies (the consistency checks use it).
HyperShape.Operators.OptContext — Type
OptContext(phase::Symbol = :setup)Context of the optimisation, passed to every solve function of the user (solve = (m, ctx) -> …) and to the post-processing. Read-only for the user; HyperShape updates it. Fields:
iteration: design iteration (0 before the loop starts).attempt: attempt of the current iteration (1, or more after rejected steps).trial: number of forward evaluations requested in the current iteration.phase::setup(constructors),:check(check_consistency),:optimization(the current design of the loop and its adjoint) or:linesearch(a trial design requested by the optimiser; with SiMPL the design that is finally accepted is first solved in this phase).continuation: named tuple with the current values of the continued parameters, for example(β = 4.0,).sensitivity::design(the derivative with respect to the design, through every map) or:physical(set byReactionDiffusionwhile it computes the sensitivity of its step, when the design chain has an interface map: the reverse sweep takes an interface map,Step()orErsatz(), as the identity weighted by the material,δ ↦ (w₀ + (1 − w₀) χ) δ);:designeverywhere else, also incheck_consistency.void_weight: the weightw₀of the void in the:physicalsensitivity.
For example, RestartState = ctx.trial > 1 restarts a nonlinear solve from scratch at every evaluation after the first one of an iteration (a design tried after a rejected step, or a line-search trial).
HyperShape.Operators.Parameter — Type
Parameter(value)A number of the user that may change during the optimisation: the penalty of a material interpolation, a load factor, a weight. p[] reads it and p[] = v changes it (its state_version grows only if the value changes). Read it inside the weak forms (E(ρ) = Emin + ρ^p[] * (1 - Emin)) and give it as a dependency of the operators whose models read it (StateOperator(model, (θ, p))), so they solve again when it changes; inside a map, give it in the parameters of a PointwiseMap. Continue it in stages with Continuation(p = p => [1, 2, 3]; every). A functional that reads it directly (not through a state or θ) lists it in its parameters. A parameter read by a model but not declared as a dependency is not detected by check_consistency: after it changes, the operator would keep its old solution.
HyperShape.Operators.SelfAdjoint — Type
SelfAdjoint()Value of dJdu in a Functional whose derivative with respect to the state is the load of the state (∂J/∂u = ℓ, for example the compliance J = ℓ(u) of a linear and symmetric problem): the adjoint is the state itself, p = u, and no adjoint is solved. Valid only when the Dirichlet values of the state are zero (checked) and on a StaticLinearModel (checked). The term − pᵀ ∂R/∂θ of the state is already the whole derivative of the compliance: do not add it again in dJdθ.
HyperShape.Operators.StateOperator — Type
StateOperator(model, deps; solve = default_solve(StateOperator, model), adjoint = nothing)
StateOperator(model; depends, solve = …, adjoint = nothing)Forward state: a HyperFEM computational model built by the user (weak forms and solver are the user's) that is solved again only when one of its dependencies changed.
depsordepends(given once, mandatory): the objects the state depends on (one object or a tuple), typicallyθfor a forward state. Each implementsstate_version; physical fields and other state operators are brought up to date first.solve: function(model, ctx) -> resultthat solves the model. The result is either the solution vector (taken as converged) or a tuple whose last element is the convergence flag: aBool, or1/0as theflagconvof(x, flagconv)returned by HyperFEM's nonlinear models. Default for aStaticLinearModel:solve!(m; Assembly = true)(default_solve). Mandatory for any other model, for example(m, ctx) -> solve!(m; stepping = …, RestartState = false); return the tuple, so that a load ramp that does not reach its final factor counts as not converged (an adjoint built on the tangent at the final factor would be inconsistent otherwise).adjoint: anAdjointProblem, needed to differentiate the functionals that read the state: who solves the transposed system (an adjoint model with the transposed tangent, orSymmetricState()) and the formspᵀ ∂R/∂θof the state. Without it the state only gives values.
get_state(S) is the state field of the model, created once, so the forms of other models (the adjoint) can capture it; it always holds the model's current solution vector.
Failures do not throw. When the solve does not converge, or a state this one depends on did not converge (then this one is not solved), apply! returns false, is_converged is false and the functionals that need the state return NaN. After a failure get_state(S) holds whatever the model left (for HyperFEM's nonlinear models, the last converged load increment), not a solution. A failed solve is not repeated until a dependency changes; the next solve starts from what the model holds, as decided by the user's solve (RestartState in HyperFEM).
HyperShape.Operators.SymmetricState — Type
SymmetricState()Operator of an AdjointProblem for a forward state over a linear and symmetric StaticLinearModel: the adjoint is solved with the matrix and the factorisation of the forward model itself (no assembly and no factorisation for the adjoint, one back substitution per gradient). The forward model must be re-assembled by its own solve function (the default one does), because the adjoint solve overwrites its right-hand side vector. With the default solve of the state, the symmetry is checked at the first adjoint solve of each solve! (cᵀK⁻¹b = bᵀK⁻¹c for two fixed vectors, two back substitutions, relative tolerance 1e-6 of ‖c‖‖K⁻¹b‖, far above the rounding of a direct solver): a non-symmetric matrix is an error. With a solve function of the user (an iterative solver) it is not checked.
HyperShape.Operators.add_seed! — Method
add_seed!(S::StateOperator, form, w, key)Adds w ⋅ form to the right-hand side of the adjoint of S; form is the linear form w -> ∫(…) of ∂J/∂u, assembled on the test space of the adjoint (key identifies it for the compiled assembler).
HyperShape.Operators.add_self_seed! — Method
add_self_seed!(S::StateOperator, w)Adds w ⋅ ∂J/∂u for a SelfAdjoint functional: its contribution to the adjoint is w ⋅ u, without a solve.
HyperShape.Operators.adjoint_sensitivity — Method
adjoint_sensitivity(S::StateOperator, p)The forms pᵀ ∂R/∂θ declared in the sensitivity of the AdjointProblem of S, at the adjoint p (without sign).
HyperShape.Operators.apply! — Method
apply!(S::StateOperator, ctx) -> BoolBrings the dependencies up to date and solves the model if one of them changed (or if it was never solved). Returns whether the current state converged. A failed solve is not repeated until a dependency changes; if a state this one depends on did not converge, this one is not solved and counts as not converged.
HyperShape.Operators.assemble_form! — Method
assemble_form!(b, a::FormAssembler, L, key) -> bWrites into b the vector of the linear form L (v -> ∫(…)dΩ) on the test space of the assembler.
HyperShape.Operators.default_solve — Function
default_solve(::Type{<:AbstractOperator}, model)Solve function used when the user gives none. Only a HyperFEM StaticLinearModel has one, and it calls the model's own solver (HyperShape never chooses one):
StateOperator:(m, ctx) -> solve!(m; Assembly = true). The operator is solved only when a dependency changed, so the forms are always re-assembled (Assembly = falsewould also reuse the previous right-hand side).LinearOperator:(m, b, ctx, stale) -> solve!(m, b; Assembly = stale): the matrix is re-assembled and factorised again only when a dependency changed.
Any other model (nonlinear, staggered, transient, or a StaticLinearModel with several right-hand sides) throws: the load stepping and the restart are modelling decisions, so pass solve = (m, ctx) -> solve!(m; stepping = …, RestartState = …). To use another solver (PETSc, an iterative solver with a preconditioner…) build the model with it, or pass your own solve.
HyperShape.Operators.get_adjoint — Method
get_adjoint(S::StateOperator)The adjoint field p of S (a fixed object updated in place): the adjoint of the last gradient computed through S. With several functionals in one sweep it is the adjoint of their weighted sum; with only SelfAdjoint ones, a multiple of the state.
HyperShape.Operators.has_adjoint — Method
has_adjoint(S::StateOperator) -> BoolWhether S was built with an AdjointProblem, so that functionals can be differentiated through it.
HyperShape.Operators.invalidate! — Function
invalidate!(obj) -> objForgets what obj computed, so that it is computed again on next use: the next solve of a LinearOperator is stale (the user re-assembles), a StateOperator is solved again (until then is_converged is false), physical fields recompute their maps and a functional its value. Used by check_consistency; physical fields and functionals extend it.
HyperShape.Operators.is_converged — Method
is_converged(S::StateOperator) -> BoolWhether the current state of S is a converged solution (false before the first solve).
HyperShape.Operators.is_seeded — Method
is_seeded(S::StateOperator) -> BoolWhether a functional has added a seed to the adjoint of S since the last reset_adjoint! or solve.
HyperShape.Operators.num_solves — Method
num_solves(op) -> IntNumber of solves done by a LinearOperator or a StateOperator (failed ones included).
HyperShape.Operators.refresh! — Method
refresh!(obj, ctx)Brings a dependency up to date before its version is compared: physical fields are updated, state operators are solved. Nothing for other objects.
HyperShape.Operators.reset_adjoint! — Method
reset_adjoint!(S::StateOperator)Starts a reverse sweep: the accumulated seeds of the adjoint of S are set to zero.
HyperShape.Operators.solve_adjoint! — Method
solve_adjoint!(S::StateOperator, ctx) -> pSolves the adjoint of S with the accumulated seeds (one solve, none if they are all self-adjoint) and returns the adjoint field p. The seeds are consumed: is_seeded is false afterwards. S must have converged.
HyperShape.Operators.solve_linear! — Method
solve_linear!(x, op::LinearOperator, b, ctx) -> xSolves with the right-hand side b through the user's solve and copies the solution into x.
HyperShape.Operators.state_version — Function
state_version(obj) -> IntCounter that grows every time obj changes. Objects listed in the depends of an operator implement it.