Skip to main content

feynkit_model/
evaluation.rs

1use std::{collections::BTreeMap, error::Error};
2
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5
6use crate::{ComplexValue, EntityKind, ModelError};
7
8/// A callable UFO function supplied to an evaluator as a transport DTO.
9#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
10pub struct EvaluationFunction {
11    pub name: String,
12    pub arguments: Vec<String>,
13    pub expression: Option<String>,
14}
15
16/// UFO form-factor metadata supplied to an evaluator as a transport DTO.
17#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
18pub struct EvaluationFormFactor {
19    pub name: String,
20    pub type_name: Option<String>,
21    pub value: Option<String>,
22}
23
24/// A named expression whose value must be recomputed.
25#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
26pub struct ModelExpression {
27    pub name: String,
28    pub expression: String,
29}
30
31/// Owned input passed to a model evaluator.
32#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
33pub struct EvaluationRequest {
34    /// External parameters and expressionless internal constants.
35    pub known_parameters: BTreeMap<String, ComplexValue>,
36    pub internal_parameters: Vec<ModelExpression>,
37    pub couplings: Vec<ModelExpression>,
38    /// Callable UFO functions available while parsing expressions.
39    pub functions: Vec<EvaluationFunction>,
40    /// Dynamic form-factor metadata referenced by Lorentz structures.
41    pub form_factors: Vec<EvaluationFormFactor>,
42}
43
44/// Owned values returned by a model evaluator.
45#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
46pub struct EvaluatedValues {
47    pub internal_parameters: BTreeMap<String, ComplexValue>,
48    pub couplings: BTreeMap<String, ComplexValue>,
49}
50
51/// Expression-evaluation boundary for a [`crate::Model`].
52///
53/// Implementations may use Symbolica, another expression engine, or an
54/// external service. The request and response are owned so an evaluator may
55/// retain, transfer, or asynchronously process their data.
56pub trait ModelEvaluator {
57    type Error: Error + 'static;
58
59    fn evaluate(&mut self, request: EvaluationRequest) -> Result<EvaluatedValues, Self::Error>;
60}
61
62impl<F, E> ModelEvaluator for F
63where
64    F: FnMut(EvaluationRequest) -> Result<EvaluatedValues, E>,
65    E: Error + 'static,
66{
67    type Error = E;
68
69    fn evaluate(&mut self, request: EvaluationRequest) -> Result<EvaluatedValues, Self::Error> {
70        self(request)
71    }
72}
73
74/// Errors returned while applying inputs and recomputing dependent values.
75#[derive(Debug, Error)]
76pub enum RecomputeError<E>
77where
78    E: Error + 'static,
79{
80    #[error(transparent)]
81    Model(#[from] ModelError),
82    #[error("model evaluator failed: {0}")]
83    Evaluator(#[source] E),
84    #[error("model evaluator did not return a value for {kind} '{name}'")]
85    MissingValue { kind: EntityKind, name: String },
86    #[error("model evaluator returned an unexpected value for {kind} '{name}'")]
87    UnexpectedValue { kind: EntityKind, name: String },
88}