Operators

HyperShape.Operators — Module
HyperShape.Operators

Operators 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; an AdjointProblem declares how to differentiate through it (the transposed solve and the forms pᵀ ∂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.

source
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 solves Kᵀ p = b. Either a HyperFEM model that carries the transposed tangent alone and the user's solver, usually StaticLinearModel((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), or SymmetricState() for a linear and symmetric forward state (reuses its factorisation). Without op (second form) no adjoint is ever solved: only SelfAdjoint functionals can read the state (checked when they are built).
  • sensitivity: (u, p) -> (δ -> ∫(…)dΩ), the linear form pᵀ ∂R/∂θ [δ] (without sign) at the state u and the adjoint p, or a named tuple of such forms ((u, p) -> (ρ = δ -> …, E = δ -> …)) when θ has several fields. For a StaticLinearModel R(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) -> x of a LinearOperator; default solve!(m, b; Assembly = stale) for a StaticLinearModel (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 (a Parameter, …).
  • backend: who assembles the seeds ∂J/∂u on 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.

source
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.
  • deps or depends: the objects the matrix depends on (one object or a tuple; each implements state_version), given once. Default (): the matrix never changes. A dependency captured by the bilinear form and not declared gives stale results; check_consistency detects it.
  • solve: function (model, b, ctx, stale) -> x that solves with the right-hand side b and returns the solution vector. stale is true when something in the dependencies changed since the last solve, so it can re-assemble. Default for a StaticLinearModel: 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).

source
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 by ReactionDiffusion while it computes the sensitivity of its step, when the design chain has an interface map: the reverse sweep takes an interface map, Step() or Ersatz(), as the identity weighted by the material, δ ↦ (w₀ + (1 − w₀) χ) δ); :design everywhere else, also in check_consistency.
  • void_weight: the weight w₀ of the void in the :physical sensitivity.

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).

source
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.

source
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θ.

source
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.

  • deps or depends (given once, mandatory): the objects the state depends on (one object or a tuple), typically θ for a forward state. Each implements state_version; physical fields and other state operators are brought up to date first.

  • solve: function (model, ctx) -> result that solves the model. The result is either the solution vector (taken as converged) or a tuple whose last element is the convergence flag: a Bool, or 1/0 as the flagconv of (x, flagconv) returned by HyperFEM's nonlinear models. Default for a StaticLinearModel: 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: an AdjointProblem, needed to differentiate the functionals that read the state: who solves the transposed system (an adjoint model with the transposed tangent, or SymmetricState()) and the forms pᵀ ∂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).

source
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.

source
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).

source
HyperShape.Operators.apply! — Method
apply!(S::StateOperator, ctx) -> Bool

Brings 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.

source
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 = false would 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.

source
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.

source
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.

source
HyperShape.Operators.solve_adjoint! — Method
solve_adjoint!(S::StateOperator, ctx) -> p

Solves 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.

source