Native element derivative API

Package consumers normally use the optimization facade in API reference. This reference documents the C++ extension points used by element implementers and native consumers. Declarations are included from the compiled headers, so the documentation follows the checked-out source revision.

See Derivative coverage and reports for solution coverage and reports, and Analytic bar and beam sizing derivatives for the bar/beam formulas.

Designed fields and use flags

DesignedField identifies a raw card field. Its item is zero-based for list columns; scalar fields use -1. FieldUse selects which element computations can change. The flags are conservative dependencies, rather than a promise that a closed derivative is available. Beam equivalent line loads can depend on section/material properties, and the beam families mark those dependencies explicitly.

 1struct DesignedField
 2{
 3    std::string_view card;
 4    std::string_view column;
 5    std::int32_t id = 0;
 6    std::int32_t item = -1;
 7};
 8
 9// What a designed field changes in the computations of an element (the Derivatives facet): the
10// semi-analytic derivatives (design/sensitivity/ElementDerivatives: central differences of the element's facets on the
11// snapshots with the field moved by ±h) compute only these. Leaving a computation out is an optimisation, never a
12// change of result: a computation the field does not enter gives the same bits at +h and −h, a zero difference.
13// `changesLoads` covers the thermal loads an element computes itself, `changesDistributedLoads` its pressure and line
14// loads, including field-dependent equivalent nodal loads of shear-flexible beams. The inertial loads follow
15// the mass.
16struct FieldUse
17{
18    bool changesStiffness = false;
19    bool changesMass = false;
20    bool changesLoads = false;
21    bool changesRecovery = false;
22    bool changesDistributedLoads = false;
23
24    // Every computation: what a field nothing is known about gets.
25    [[nodiscard]] static constexpr FieldUse all() noexcept { return {true, true, true, true, true}; }
26    [[nodiscard]] static constexpr FieldUse none() noexcept { return {}; }
27    [[nodiscard]] static constexpr FieldUse massOnly() noexcept { return {false, true, false, false, false}; }
28    [[nodiscard]] static constexpr FieldUse recoveryOnly() noexcept { return {false, false, false, true, false}; }
29    // A stiffness that is not a section property of the mass (a spring rate, a torsion constant, a shear factor).
30    [[nodiscard]] static constexpr FieldUse stiffnessAndRecovery() noexcept
31    {
32        return {true, false, false, true, false};
33    }
34    // An expansion coefficient or a reference temperature: the thermal loads, and the thermal strain the recovery
35    // subtracts.
36    [[nodiscard]] static constexpr FieldUse loadsAndRecovery() noexcept { return {false, false, true, true, false}; }
37    // The same use without the pressure and line loads: a field of the section or of a material, which the loads an
38    // element distributes over its surface or length do not read (its geometry, its offset and the load values do).
39    [[nodiscard]] constexpr FieldUse withoutDistributedLoads() const noexcept
40    {
41        FieldUse use = *this;
42        use.changesDistributedLoads = false;
43        return use;
44    }
45
46    [[nodiscard]] constexpr bool isRead() const noexcept
47    {
48        return changesStiffness || changesMass || changesLoads || changesRecovery || changesDistributedLoads;
49    }
50};

Load and recovery contexts

ElementLoadContext borrows temperatures and element-specific applied loads. Inertial-load derivatives follow the mass matrix and are assembled separately by the design layer.

1struct ElementLoadContext
2{
3    const ElementTemperatures* temperatures = nullptr;
4    std::span<const ElementPressure> pressures{};
5    std::span<const ElementLineLoad> lineLoads{};
6};

NodalDisplacements borrows the state, temperature data and optional line loads used during recovery. The ordinary bar/beam recovery layout stays the same when its line-load context is present. Each recovered row selects its own batch-row loads. For a linear output at fixed geometry, loads change the affine term while the displacement slopes and their field rates can be reused across subcases.

 1struct NodalDisplacements
 2{
 3    std::span<const double> grid;                      // 6 · NodeTable::rowCount() values (6 per grid slot)
 4    std::span<const double> scalar;                    // one per scalar point row (per scalar slot)
 5    const ElementTemperatures* temperatures = nullptr; // null: no thermal load; not owned
 6    const DisplacementSlots* slots = nullptr;          // null: the rows of the model; not owned
 7    // Optional line loads for recovery, with batch row indices. Families select the loads of each recovered row.
 8    // They change the affine part of a linear output; its displacement slopes keep the ordinary output layout.
 9    std::span<const ElementLineLoad> lineLoads{};
10    // The values of the entities of the formulation (EntityLayout::totalDofCount() values, in the layout's order; with
11    // `slots`, the blocks of the entities the region gives, at DisplacementSlots::entityOffsets), and the layout; empty
12    // and null for a model without entities.
13    std::span<const double> entity{};
14    const EntityLayout* entityLayout = nullptr; // not owned
15    // The acceleration field of the inertial loads of the case (BodyAcceleration), or null: none; not owned. Read by
16    // the elements whose stresses take the particular stresses of a body force (the solids of the mixed hybrid
17    // formulation).
18    const BodyAcceleration* bodyAcceleration = nullptr;
19
20    // The displacements of a g-set vector in the DofMap's order: 6 values per grid point, 1 per scalar point, then
21    // the entity DOFs of `layout` (null: none).
22    [[nodiscard]] static NodalDisplacements ofGSet(std::span<const double> values, std::size_t gridCount,
23                                                   std::size_t scalarCount, const EntityLayout* layout)
24    {
25        NodalDisplacements displacements;
26        displacements.grid = values.first(6 * gridCount);
27        displacements.scalar = values.subspan(6 * gridCount, scalarCount);
28        displacements.entity = values.subspan(6 * gridCount + scalarCount);
29        displacements.entityLayout = layout;
30        return displacements;
31    }
32
33    [[nodiscard]] double valueAt(const ElementDof& dof) const
34    {
35        if (dof.kind == input::PointKind::Entity)
36        {
37            // The displacements of a model with entities are made by ofGSet, with the layout; without it, the entity
38            // values were left out (a bug of the caller, not a zero displacement).
39            OOS_ASSERT(entityLayout != nullptr);
40            const std::int64_t first = slots != nullptr && !slots->entityOffsets.empty()
41                                           ? slots->entityOffsets[static_cast<std::size_t>(dof.row)]
42                                           : entityLayout->dofOffset(dof.row);
43            OOS_ASSERT(first >= 0);
44            const std::size_t at = static_cast<std::size_t>(first) + static_cast<std::size_t>(dof.component);
45            OOS_ASSERT(at < entity.size());
46            return entity[at];
47        }
48        if (dof.kind == input::PointKind::Scalar)
49            return scalar[slotOf(dof)];
50        return grid[slotOf(dof) * 6 + static_cast<std::size_t>(dof.component) - 1];
51    }
52    // The grid or scalar slot of a DOF's point (its row without slots).
53    [[nodiscard]] std::size_t slotOf(const ElementDof& dof) const
54    {
55        if (slots == nullptr)
56            return static_cast<std::size_t>(dof.row);
57        const std::span<const std::int32_t> of =
58            dof.kind == input::PointKind::Scalar ? slots->scalarSlots : slots->gridSlots;
59        return static_cast<std::size_t>(of[static_cast<std::size_t>(dof.row)]);
60    }
61};

Derivative facets

These optional methods append matrix, load or recovered-row rates in the same DOF/row layout as their ordinary facets. A return value of true with an empty matrix/load block means a known zero; false means unavailable and leaves the design layer’s numerical fallback in place. Recovery columns without a supplied formula use NaN according to the individual method’s contract. Only claim a closed derivative when all terms of that requested field and context are included.

The geometric-stiffness methods return partials at fixed preload displacements and temperatures. The solution layer adds preload tangents where supported; those element partials alone do not provide a total buckling Hessian. loadFieldMixedDerivative also supplies a same-field second derivative when its two fields are identical.

  1    // dK/dv and dM/dv of the element of batch row `row` with respect to the designed field `field` of its cards at its
  2    // value in `view` (the design point), appended to `block` as computeStiffness and computeMass append them; false
  3    // when the family has no closed form for that field and that element, and the caller differences the facet
  4    // (design/sensitivity/ElementDerivatives). `field` names a cell the element reads (the field of its own card, of
  5    // its property or of a material of its property).
  6    virtual bool stiffnessFieldDerivative(const ElementBatchView& view, std::int64_t row, const DesignedField& field,
  7                                          ElementMatrixBlock& block) const;
  8    // stiffnessFieldDerivative of the element of batch row `row` for several fields at once: blocks[k] gets what it
  9    // appends for fields[k], isClosed[k] its result (1 or 0). A family overrides it to resolve and formulate the
 10    // element once for all the fields (the 19 fields of a lamination zone reach the same elements), with
 11    // the bits of the calls one by one; by default stiffnessFieldDerivative for each.
 12    virtual void stiffnessFieldDerivatives(const ElementBatchView& view, std::int64_t row,
 13                                           std::span<const DesignedField> fields, std::span<ElementMatrixBlock> blocks,
 14                                           std::span<std::uint8_t> isClosed) const;
 15    virtual bool massFieldDerivative(const ElementBatchView& view, std::int64_t row, const DesignedField& field,
 16                                     const MassOptions& options, ElementMatrixBlock& block) const;
 17    // Derivatives of applied element loads, in grid displacement coordinates. True with an empty block means a
 18    // known zero. False retains numerical element differences. Equal fields request the same-field second rate.
 19    virtual bool loadFieldDerivative(const ElementBatchView& view, std::int64_t row, const DesignedField& field,
 20                                     const ElementLoadContext& loads, ElementVectorBlock& block) const;
 21    virtual bool loadFieldMixedDerivative(const ElementBatchView& view, std::int64_t row, const DesignedField& first,
 22                                          const DesignedField& second, const ElementLoadContext& loads,
 23                                          ElementVectorBlock& block) const;
 24    // dK_d/dv of the element of batch row `row` at fixed preload displacements and temperatures (`preload`, as
 25    // computeDifferentialStiffness takes it), appended as computeDifferentialStiffness appends it; false when the
 26    // family has no closed form for that field, element and preload (the buckling load factors' explicit
 27    // parts).
 28    virtual bool differentialStiffnessFieldDerivative(const ElementBatchView& view, std::int64_t row,
 29                                                      const DesignedField& field, const NodalDisplacements& preload,
 30                                                      ElementMatrixBlock& block) const;
 31    // The gradient of φᵀ·K_d(u)·φ with respect to the preload displacements u of the element of batch row `row`, φ the
 32    // displacements `shape` (the g set, as a preload is given): K_d is linear in u, so entry k is φᵀ·K_d(e_k)·φ, e_k
 33    // the unit displacement of the element's DOF k; the temperatures, a term of K_d that does not depend on u, are
 34    // left out. Appended to `block` on the DOFs computeDifferentialStiffness gives the element (the derivative of a
 35    // buckling factor through its preload). False when the family has no such product (the default): the caller
 36    // then forms K_d of each unit displacement.
 37    virtual bool differentialStiffnessPreloadGradient(const ElementBatchView& view, std::int64_t row,
 38                                                      const NodalDisplacements& shape,
 39                                                      ElementVectorBlock& block) const;
 40    // Mixed field curvature of K_d at a fixed mechanical preload. Its displacement dependence remains linear;
 41    // the buckling Hessian adds both preload tangents separately. False means the formula is unavailable.
 42    virtual bool differentialStiffnessFieldMixedDerivative(const ElementBatchView& view, std::int64_t row,
 43                                                           const DesignedField& first, const DesignedField& second,
 44                                                           const NodalDisplacements& preload,
 45                                                           ElementMatrixBlock& block) const;
 46    // The derivatives of the rows recover() gives the element of batch row `row` under `displacements` with respect to
 47    // the designed field `field` at fixed displacements (the explicit parts of its items), in closed form: `rates` in
 48    // the layout of recover()'s block (the same rows, places and columns), each column's ∂value/∂v, NaN where the
 49    // family gives none (a principal value, an angle); false when it has no closed form for that field and element,
 50    // and the caller differences the recovery on the perturbed snapshots.
 51    virtual bool recoveryFieldRates(const ElementBatchView& view, std::int64_t row, const DesignedField& field,
 52                                    ElementOutput output, const NodalDisplacements& displacements,
 53                                    ElementResultBlock& rates) const;
 54    // recoveryFieldRates of the element of batch row `row` for several fields at once: rates[k] for fields[k],
 55    // isClosed[k] its result (1 or 0); a family overrides it to resolve and formulate the element and recover its
 56    // state once for all the fields, with the bits of the calls one by one; by default
 57    // recoveryFieldRates for each.
 58    virtual void recoveryFieldRatesOfFields(const ElementBatchView& view, std::int64_t row,
 59                                            std::span<const DesignedField> fields, ElementOutput output,
 60                                            const NodalDisplacements& displacements,
 61                                            std::span<ElementResultBlock> rates,
 62                                            std::span<std::uint8_t> isClosed) const;
 63    // recoveryFieldRates for the layer rows of recoverLayers (`rates` in their layout, NaN where the family gives none:
 64    // an angle, a principal value, the element's largest failure index): the plies of a laminated shell along a ply
 65    // thickness; false without a closed form, and the caller differences the layer recovery.
 66    virtual bool layerRecoveryFieldRates(const ElementBatchView& view, std::int64_t row, const DesignedField& field,
 67                                         LayerOutput output, const NodalDisplacements& displacements,
 68                                         ElementResultBlock& rates) const;
 69    // layerRecoveryFieldRates for several fields at once (rates[k], isClosed[k] for fields[k]), as
 70    // recoveryFieldRatesOfFields: a family resolves, formulates and recovers the element once for all of them, with the
 71    // bits of the calls one by one; by default layerRecoveryFieldRates for each.
 72    virtual void layerRecoveryFieldRatesOfFields(const ElementBatchView& view, std::int64_t row,
 73                                                 std::span<const DesignedField> fields, LayerOutput output,
 74                                                 const NodalDisplacements& displacements,
 75                                                 std::span<ElementResultBlock> rates,
 76                                                 std::span<std::uint8_t> isClosed) const;
 77
 78    // The second derivatives with respect to one designed field: what a family gives in closed form for the element
 79    // Hessian blocks of the Hessian-vector products of a weighted sum of responses; each optional, the defaults give
 80    // nothing (the products then difference their linearisation).
 81    //
 82    // d²K/dv² and d²M/dv² of the element of batch row `row` at the field's value in `view`, appended as
 83    // computeStiffness and computeMass append them; true with nothing appended for a zero derivative; false when the
 84    // family has no closed form for that field and element.
 85    virtual bool stiffnessFieldSecondDerivative(const ElementBatchView& view, std::int64_t row,
 86                                                const DesignedField& field, ElementMatrixBlock& block) const;
 87    virtual bool massFieldSecondDerivative(const ElementBatchView& view, std::int64_t row, const DesignedField& field,
 88                                           const MassOptions& options, ElementMatrixBlock& block) const;
 89    // Mixed derivatives in two fields at fixed geometry. The defaults use the same-field methods when the
 90    // fields match, or return a known zero when FieldUse says either field cannot change the matrix.
 91    virtual bool stiffnessFieldMixedDerivative(const ElementBatchView& view, std::int64_t row,
 92                                               const DesignedField& first, const DesignedField& second,
 93                                               ElementMatrixBlock& block) const;
 94    virtual bool massFieldMixedDerivative(const ElementBatchView& view, std::int64_t row, const DesignedField& first,
 95                                          const DesignedField& second, const MassOptions& options,
 96                                          ElementMatrixBlock& block) const;
 97    // The slopes of the rows of recoveryFieldRates (their derivatives at fixed displacements) with respect to the
 98    // element's DOFs, ∂S/∂v: the mixed second derivatives of its Linear columns, in the layout of recoverySlopes (zero
 99    // in the other columns); false when the family gives no closed form (the caller then takes them from
100    // recoveryFieldRates at unit displacements, exact as well: the rates of a Linear column are linear in u).
101    virtual bool recoverySlopeRates(const ElementBatchView& view, std::int64_t row, const DesignedField& field,
102                                    ElementOutput output, RecoverySlopes& rates) const;
103    // ∂²value/∂v² of the rows recover() gives under `displacements` at fixed displacements, in the layout of
104    // recoveryFieldRates, NaN where the family gives none (a column that is not Linear); false without a closed form.
105    virtual bool recoveryFieldSecondRates(const ElementBatchView& view, std::int64_t row, const DesignedField& field,
106                                          ElementOutput output, const NodalDisplacements& displacements,
107                                          ElementResultBlock& rates) const;
108    // Mixed recovered rates at fixed displacement, in recover()'s row layout. False when no closed form is given.
109    virtual bool recoveryFieldMixedRates(const ElementBatchView& view, std::int64_t row, const DesignedField& first,
110                                         const DesignedField& second, ElementOutput output,
111                                         const NodalDisplacements& displacements, ElementResultBlock& rates) const;

Shared bar and beam implementation

BeamFieldDerivatives implements the shared sizing facets. A null other selects a first partial; an identical second field selects the same-field second partial. Unknown fields or unsupported contexts return false. The implementation carries values and two first partials plus their mixed partial through local arithmetic; it does not perturb the model to obtain these rates.

 1class BeamFieldDerivatives
 2{
 3public:
 4    static bool loads(const BeamData& beam, const ElementBatchView& view, std::int64_t row,
 5                      const DesignedField& field, const DesignedField* other, const ElementLoadContext& loads,
 6                      ElementVectorBlock& block);
 7    static bool stiffness(const BeamData& beam, const ElementBatchView& view, std::int64_t row,
 8                           const DesignedField& field, const DesignedField* other, ElementMatrixBlock& block);
 9    static bool mass(const BeamData& beam, const ElementBatchView& view, std::int64_t row,
10                      const DesignedField& field, const DesignedField* other, const MassOptions& options,
11                      TorsionalInertia torsion, ElementMatrixBlock& block);
12    static bool recovery(const BeamData& beam, const ElementFamily& family, const ElementBatchView& view,
13                          std::int64_t row, const DesignedField& field, const DesignedField* other,
14                          ElementOutput output, const NodalDisplacements& displacements, ElementResultBlock& block);
15    static bool slopeRates(const BeamData& beam, const ElementFamily& family, const ElementBatchView& view,
16                            std::int64_t row, const DesignedField& field, ElementOutput output, RecoverySlopes& block);
17    static bool differential(const BeamData& beam, const ElementBatchView& view, std::int64_t row,
18                              const DesignedField& field, const DesignedField* other,
19                              const NodalDisplacements& preload, ElementMatrixBlock& block);
20};

Field lists, load conventions, station meanings and recovery limits are described in Analytic bar and beam sizing derivatives.