"""Residence weight of the states a run has not certified.
For ``[amsel] state_confidence = coverage`` a state is certified when its
confidence meets ``[AKMC] confidence``. The run searches the state it is in
until that holds and then leaves, so every state it left was certified when
it was left. The network it has seen may still hold states that are not: the
state it stands in, and states whose tables changed after it left them.
``amsel.uncertified_residence_share`` gives the share of the mean time to
leave the network, from the state the run began in, that is spent in those
states. The network is the visited states with the processes between them;
a process whose product was never entered leaves the network.
The share is reported and nothing acts on it. The run does not go back to an
uncertified state because of it.
"""
from __future__ import annotations
import logging
from typing import Any
logger = logging.getLogger("amsel")
# Label of a product state the run never entered. Visited states are
# numbered from zero and stay far below it.
_UNVISITED = 1 << 31
[docs]
def visited_network(states: Any) -> tuple[list[int], list[int], list[tuple[int, int, float]]]:
"""Visited states, the unvisited products they lead to, and the rates."""
count = int(states.get_num_states())
transient = list(range(count))
absorbing: list[int] = []
rates: list[tuple[int, int, float]] = []
for number in transient:
state = states.get_state(number)
for process, _rate, _prefactor in state.get_ratetable():
proc = state.procs[process]
rate = float(proc["rate"])
if not rate > 0.0:
continue
product = int(proc["product"])
if product == number:
continue
if 0 <= product < count:
rates.append((number, product, rate))
else:
label = _UNVISITED + len(absorbing)
absorbing.append(label)
rates.append((number, label, rate))
return transient, absorbing, rates
[docs]
def residence_report(states: Any, config: Any, entry: int = 0) -> dict | None:
"""``eta_res`` of the visited network, or None when it cannot be formed.
None for a network with no way out, which a run has before its first
search returns, and when amsel does not provide the function.
"""
try:
from amsel import uncertified_residence_share
except ImportError:
return None
transient, absorbing, rates = visited_network(states)
if not absorbing:
return None
level = float(config.akmc_confidence)
uncertified = [
number for number in transient if states.get_state(number).get_confidence() < level
]
try:
out = uncertified_residence_share(transient, absorbing, rates, int(entry), uncertified)
except Exception as exc:
logger.info("amsel residence share not formed: %s", exc)
return None
report = {
"eta_res": float(out["share"]),
"mean_time": float(out["mean_time"]),
"uncertified_time": float(out["uncertified_time"]),
"states": len(transient),
"uncertified": uncertified,
"enforced": False,
}
logger.info(
"amsel residence share (reported, not enforced): eta_res=%.6g over %d states, "
"%d not certified %s, mean time %.6e s",
report["eta_res"],
report["states"],
len(uncertified),
uncertified,
report["mean_time"],
)
return report