Skip to content

Named constants in setpoint expressions, as plant parameters - #1099

Queued
ctdegroot wants to merge 6 commits into
mainfrom
claude/eager-johnson-19stlm
Queued

ctdegroot wants to merge 6 commits into
mainfrom
claude/eager-johnson-19stlm

Conversation

@ctdegroot

Copy link
Copy Markdown
Member

Closes #1073

Summary

A setpoint written as an expression of time can now name its numbers, and each named number is a plant parameter:

IdealClarifier("clar", model, underflow_Q="Q0 + A*sin(2*pi*t)", constants={"Q0": 385.0, "A": 75.0})

makes "clar.Q0" and "clar.A" parameters, so a sensitivity, a Fisher analysis or a calibration reaches the numbers of a schedule. The ideal and Takács clarifiers, the primary clarifier and the setpoint and threshold splitters take constants=, in Python and in a flowsheet file (the catalogue, spec reader/writer and validation all read the names).

  • core/time_expr.py: compile_time_expression(..., constants=) reads named constants as inputs (overridable per call, so they trace under jax.grad); time_expression_reads reports whether an expression reads t and checks the names; check_constant_names is shared with the control-law language, which now refuses a reserved constant name with the same message.
  • plant/flow_setpoint.py: constants become FlowSetpoint slots after the settings; a constant that clashes with a setting's name, or that no expression reads, is refused. NamedConstantsUnit is the mixin the units use.
  • Recycle maps: an expression that reads only constants (not t) no longer disables the cached recycle / flow maps; one that reads t still does (owner's decision: keep the maps when safe).

Things a reviewer should know

  • Per-unit constants (owner's decision), not a plant-wide table.
  • written_when_set field metadata. constants and size_parameters (Unit sizes as opt-in plant parameters #1083) are left out of a saved spec and of spec_fingerprint while at their default. Unit sizes as opt-in plant parameters #1083 (unreleased) had changed every existing plant's fingerprint by writing size_parameters: false, which would have made every saved study stale; this restores the example projects' fingerprints to their pre-Unit sizes as opt-in plant parameters #1083 values.
  • Two test edits, both because the code they read changed on purpose:
    • test_operating_parameters.py::test_each_setting_is_a_named_parameter_seeded_with_its_value reads the slots through _parameter_setpoints() instead of _flow_setpoints(), since constants are parameter slots but not flow settings.
    • test_spec_properties.py::_round_trip accepts a written_when_set setting being absent at its default.
  • Removed dtype=float on the expression clock leaf: t * time_scale is already a float, so it changed nothing (two equivalent mutants).
  • Merged main (Name unit settings in words, fold advanced ones away, and move a model in one step #1091 conflicted in catalog.py and clarifier.py; both sides' fields kept). constants sits under Parameters, not Other Parameters, so it is not an advanced setting.

Testing

  • tests/unit tests/ui plus test_operating_parameters, test_setpoint_expressions, test_spec_properties, test_plant_spec after the merge: 3525 passed. Before the merge, the whole fast tier: all passed once the round-trip test was updated.
  • Mutation testing: aquakin.core.time_expr 244/244 killed; aquakin.plant.control_law 149/150 (the recorded survivor).
  • Strict docs build (-W) and the docs/influents.md examples passed before the merge.
  • ruff, ruff format --check, scripts/check_types.py clean.
  • Not run: the slow and validation suites (this needs full-ci: recycle and plant assembly code changed).

Patch coverage

scripts/patch_coverage.py: 100.00% of 131 changed lines (before the merge commit).

Rule proposals

  • What went wrong: Unit sizes as opt-in plant parameters #1083 added a setting whose default the spec writer filled in, so every saved plant's spec_fingerprint changed and every study would have gone stale; no test noticed.
  • A check can replace prose: a test pinning the spec_fingerprint of each example project under examples/ (rewritten only with the owner's agreement, like the golden numbers).
  • Rule text (if prose is wanted too): "Do not change what Plant.to_spec writes for an existing plant: a new setting is written_when_set or the saved studies go stale."
  • Where: .claude/rules/plant-spec.md, which loads for plant/spec.py and catalog.py, the files such a change touches. Nothing it replaces.

Generated by Claude Code

A setpoint written as an expression of time can name its numbers through
the unit's `constants` (`underflow_Q="Q0 + A*sin(2*pi*t)"` with
`constants={"Q0": 385.0, "A": 75.0}`); each is the plant parameter
"<unit>.<name>", resolved by the expression at every evaluation, so a
sensitivity or a calibration reaches the numbers of a schedule. The ideal
and Takacs clarifiers, the primary clarifier and the setpoint and threshold
splitters take them (`NamedConstantsUnit`), in Python, the catalogue and a
flowsheet file. An expression that reads only constants, not `t`, is
constant in time and keeps the cached recycle maps.

`compile_time_expression` takes `constants=` and returns `f(t, values=None)`;
`time_expression_reads` names what an expression reads; the control law
shares the reserved-name check (`check_constant_names`).

`size_parameters` and `constants` are marked `written_when_set`: left out of
a saved spec and its fingerprint at their defaults, so adding them does not
turn an existing plant's studies stale.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ault

The spec writer deliberately omits a setting such as size_parameters while it
holds its default, so the property test now reads such a setting's default
when it is absent. Drop the clock leaf's dtype argument: t * time_scale is
already a float, so the argument changed nothing a test could see.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…19stlm

# Conflicts:
#	aquakin/plant/catalog.py
#	aquakin/plant/clarifier.py
@ctdegroot ctdegroot added the full-ci Run the full slow+validation suite on this PR (opt-in pre-merge check) label Dec 4, 2026 — with Claude
…19stlm

# Conflicts:
#	.claude/rules/plant-spec.md
The test now passes a valid port ahead of the bad one, so it checks that
the validation goes on past a good entry; it also takes both arcs of that
loop's check in one test, which Codecov's per-shard reports need.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…19stlm

# Conflicts:
#	.claude/rules/plant-spec.md
@ctdegroot
ctdegroot added this pull request to the merge queue Dec 4, 2026
Any commits made after this event will not be merged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the full slow+validation suite on this PR (opt-in pre-merge check)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Named constants in setpoint expressions, as plant parameters

2 participants