Calibrating a plant#
plant.calibrate is the plant version of aquakin.calibrate
(Calibration): it fits plant parameters, by name, against
measured channels of one or more streams or units. It uses the same
transforms, priors, multistart and Laplace posterior,
behind the plant’s cap-free adjoint, so the gradient is finite for a stiff
plant with no dtmax to tune. Two defaults differ from aquakin.calibrate:
the loss is "mse" and laplace=False, so pass loss="nll", sigma= and
laplace=True for uncertainties.
Warning
The shipped asm1 and adm1 models declare priors on their calibration
parameters, and plant.calibrate applies them by default (use_priors=True).
With the default loss="mse" the prior is added to a mean squared error in the
square of the data’s units, so how strongly it pulls the fit depends on the
units the data happen to be in. Fit with loss="nll" and each channel’s
sigma (or sigma="estimate") so the prior is weighed against a likelihood,
or pass use_priors=False.
The data of a plant fit#
Times are on the plant’s clock.
t_obsis in the plant’s time unit (Plant(name, time_unit=...); days for the BSM plants) unless you passtime_unit=, and, as for a reactor fit, are strictly ascending and not negative.The solve runs over
t_span, which defaults to(t_obs[0], t_obs[-1]). Starting it earlier,t_span=(t_start, t_obs[-1])witht_startbefore the first sample, gives a spin-up window in which a mismatch betweeny0and the real plant decays before the first observation is compared.y0is the state att_span[0]. Without it the fit starts fromplant.initial_state(), the units’ own default states, which need not resemble the plant’s operating point. Pass a steady state (plant.steady_state().state) or a warm start (bsm1_warm_start(plant),bsm2_warm_start(plant)), ideally under the influent that preceded the data; a BSM2 plant should always be warm-started.What is measured is one stream with
target=andobserved_channels=, or several withobservables=, a list of observables. Each observable is a stream (a registered name such as"effluent", a"unit.port", or a unit name for that unit’s outlet) with its channels, written asPlantObservable("effluent", ["SNH", "SNO"])or the pair("effluent", ["SNH", "SNO"]). The columns ofobservationsrun in observable order, and in channel order within each: two observables with two and one channels give three columns. A channel can be a species or one of the measured channels below;NaNmarks a missing sample, as for a reactor.
The examples below use BSM2, built with its influent wired as in
Plant-wide simulation, and need a measured
observations array. A BSM2 solve takes minutes, and a fit many solves, so
they are not run on this page.
from aquakin.plant.bsm import build_bsm2, bsm2_warm_start
# plant = build_bsm2(...) with its influent, as in Plant-wide simulation
result = plant.calibrate(
observations, # (n_t, n_channels) measured data
t_obs,
["asm1.muH", "asm1.bH"], # plant parameters to fit (see plant.parameter_names())
target="effluent", # a registered stream, or "unit.port"
observed_channels=["SNH", "SNO"],
loss="nll", sigma=jnp.array([0.2, 0.5]), laplace=True,
y0=bsm2_warm_start(plant), # warm start recommended for a stiff plant
)
result.params_named
Fit against several streams at once with
observables=[PlantObservable(...), ...], fit initial states of the assembled
plant with free_ic=FreeICConfig([...]), and run a joint multi-batch fit by
passing list-valued observations, t_obs and y0. How well the fitted
values are determined, and how to report them, works as for a reactor
(Uncertainty and model comparison); the plant tools for
it are below.
Measured channels#
A channel need not be a species. The channels correspond to what a laboratory or an online sensor measures:
Channel |
Measures |
|---|---|
|
the stream’s flow and temperature (a stream carries a temperature when the influents upstream of it have one) |
|
total COD as a COD test sees it: dissolved oxygen and nitrate count none, nor do dissolved hydrogen and methane |
|
total Kjeldahl nitrogen: ammonium and organic nitrogen |
|
nitrate plus nitrite nitrogen |
|
|
|
total phosphorus |
|
the model’s own TSS species, or 0.75 × the particulate COD for the ASM1 family |
|
any other weighted sum |
|
the same weighted sum, written out |
COD, TKN, NOx, TN and TP come from the model’s composition table, so their
weights follow the composition parameters of the run (i_XB, iN_BM, …): a
fit or a Fisher information with "asm1.i_XB" free sees total nitrogen move
with it. A digester’s headspace (S_gas_*) is in no channel. Channels work
everywhere an observable does: calibration, Fisher information, profile
likelihood and sensor ranking.
from aquakin.plant import Measure, PlantObservable
PlantObservable("effluent", ("TKN", "NOx", "TSS", "Q"))
A channel of your own#
A channel the table does not cover is written as a weighted sum: name = and
then the sum, with the species in brackets and the measurements above named as
they are:
PlantObservable("effluent", ("SNH", "BOD5 = 0.65*[SS] + 0.65*[XS]", "sCOD = COD - [XS] - [XI]"))
It is the same thing as Measure("BOD5", {"SS": 0.65, "XS": 0.65}), in a form
that also fits in a study file and in the flowsheet editor, where it is typed
straight into the channels field of the study dialog. Each is reported under
its name, so the one above gives the columns effluent.BOD5 and
effluent.sCOD.
A channel built on COD, TN, … carries their dependence on the composition
parameters with it, so "half_TN = 0.5*TN" moves with a fitted i_XB exactly
as TN does.
The sum has to stay linear in the stream: * needs a plain number on one
side, / a plain number on the right, and there is no constant term (so a
temperature in °C is not a channel). This keeps every channel a column of one
measurement matrix, which lets a sensor ranking score many candidate designs
from a single solve. An expression that breaks the rule, or cannot be read at
all, raises MeasureSyntaxError (a ValueError) when a study using it is
written, or in Python when a fit or analysis first uses it.
MeasureSyntaxError: a measure is a weighted sum, so it cannot multiply two
measured quantities (at position 8); one side of a '*' must be a number
aquakin.plant.parse_measure reads one on its own, if you want to check an
expression or build the Measure it stands for.
Bounds, initial states and noise in a plant fit#
Everything in Fitting parameters to data about bounds, initial pools and noise applies to a plant fit too:
The free parameters stay inside the
bounds:their models declare, andOptimizerConfig(bounds=...)keys are plant parameter names such as"asm1.muH". A fit that ends on a bound lists it inresult.at_bound, with ananLaplace standard deviation.The fitted initial states are named
"unit.species"(also the keys of a per-poolFreeICConfig(bounds={...})), andfixed_total=Truefits their split of the total iny0.With
loss="nll",sigmagives one noise level per observed column, in column order, andsigma="estimate"estimates the noise level of each.observation_units={"SNH": "mg/L", "Q": "m3/h"}declares channels measured in other units, converted as for a reactor fit. A species is in its model’s unit,Qin m³ per the plant’s time unit,Tin K,COD,TKN,NOx,TNandTPin g/m³, and a measure in the one unit its terms share.
Progress and cancelling#
plant.calibrate, plant_fisher_information, plant_profile_likelihood and
plant_rank_sensor_sets report their progress to a progress= callable,
which receives an aquakin.Progress (stage, done, total, message) at
each objective evaluation, profile point or run. Raising aquakin.Cancelled
from it stops the analysis.
Studies#
A plant calibration, profile likelihood, Fisher information or sensor ranking
can also be saved as a study, run from the command line or the flowsheet
editor, with its results kept beside it and checked for staleness. Calibration
and profile studies always fit the Gaussian likelihood, so they need each
channel’s sigma or estimate_sigma: true. See
Studies.
Identifiability of plant parameters#
aquakin.plant.plant_fisher_information and
aquakin.plant.plant_profile_likelihood are the plant versions of
fisher_information (Experiment design) and
profile_likelihood (Profile likelihood);
the ideas behind them are in Key ideas. They name the measured channels
the way plant.calibrate does, and work in its coordinates, so their results
feed aquakin.identifiability and aquakin.design_criterion directly.
from aquakin.plant import plant_fisher_information, plant_profile_likelihood
fim_plant = plant_fisher_information(
plant,
[("effluent", ["SNH", "SNO"]), ("digester", ["S_IN", "S_gas_ch4"])],
t_obs,
["asm1.muA", "asm1.KNH_A", "adm1.k_m_ac"],
sigma=[0.2, 0.5, 0.01, 0.005], # one per channel, in observable order
y0=bsm2_warm_start(plant),
)
rep_plant = aquakin.identifiability(fim_plant)
fim_plant.std() # Cramér-Rao standard deviations
A unit name reads that unit’s outlet, so a reactor’s or the digester’s state
can be measured directly, as above. sigma is absolute by default. With
relative_sigma=True it is a fraction of the predicted signal, with
sigma_floor as a lower bound. mask= leaves out samples that are not taken.
A list of t_obs arrays describes several runs whose information adds. y0,
t_span, sigma, mask and the plant itself can then be lists too, one per
run, for runs at different initial states or influents.
The sensitivity comes from plant.solve_sensitivity by default
(mode="forward"), whose memory does not grow with the number of
observations, so it suits long horizons. mode="reverse" differentiates
plant.solve with the cap-free adjoint, one pass per observation, which suits
a few observations of many parameters. At a plant.calibrate result with
loss="nll", no priors and LaplaceConfig(method="gauss_newton"), the
reverse-mode matrix equals the fit’s hessian_unconstrained (both are the
Fisher information).
To choose what to measure, aquakin.plant.plant_rank_sensor_sets scores
candidate sensor sets by a design criterion of the information each adds to
the sensors already in place (base=). A SensorSet is a list of
observables, in either form, with the noise of their channels: sigma is one
value for every channel or one per channel in column order, absolute unless
relative_sigma=True. One
sensitivity solve is shared by every candidate, so each extra candidate costs
only linear algebra. It returns the same CandidateScore list as
rank_experiments, best first, and takes criterion=, subset= and
param_draws= as that does:
from aquakin.plant import SensorSet, plant_rank_sensor_sets
online = SensorSet([("effluent", ["SNH"])], sigma=0.2)
sensor_candidates = [
SensorSet([("effluent", ["SNO"])], sigma=0.5),
SensorSet([("tank3", ["SO"])], sigma=0.1),
SensorSet([("digester", ["S_IN"])], sigma=0.05, relative_sigma=True),
]
ranked_sensors = plant_rank_sensor_sets(plant, sensor_candidates, t_obs,
["asm1.muA", "asm1.KNH_A"], base=online,
y0=bsm2_warm_start(plant))
ranked_sensors[0].index, ranked_sensors[0].improvement
plant_profile_likelihood re-fits the other parameters with plant.calibrate
at each grid value of the profiled one, with the same warm-started sweep as
profile_likelihood, and every grid point reuses one compiled objective.
Other keywords go to plant.calibrate:
prof_plant = plant_profile_likelihood(
plant, observations, t_obs, ["asm1.muA", "asm1.KNH_A"],
grid=np.linspace(0.4, 1.2, 9), profile_param="asm1.muA",
observables=[("effluent", ["SNH"])], sigma=jnp.asarray([0.2]),
y0=bsm2_warm_start(plant))
prof_plant.ci