1use std::{io, path::PathBuf};
2
3use symbolica_notation::MathMlSubsetError;
4use thiserror::Error;
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum EntityKind {
9 Order,
10 Parameter,
11 Particle,
12 Propagator,
13 LorentzStructure,
14 Coupling,
15 VertexRule,
16 ModelFunction,
17 FormFactor,
18}
19
20impl std::fmt::Display for EntityKind {
21 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22 f.write_str(match self {
23 Self::Order => "order",
24 Self::Parameter => "parameter",
25 Self::Particle => "particle",
26 Self::Propagator => "propagator",
27 Self::LorentzStructure => "Lorentz structure",
28 Self::Coupling => "coupling",
29 Self::VertexRule => "vertex rule",
30 Self::ModelFunction => "model function",
31 Self::FormFactor => "form factor",
32 })
33 }
34}
35
36#[derive(Clone, Debug, Error, PartialEq, Eq)]
38pub enum ModelValidationError {
39 #[error("Covariant cut multiplet {physical} {reason}")]
40 CovariantCutMultiplet { physical: i64, reason: String },
41 #[error("model name must not be empty")]
42 EmptyModelName,
43 #[error("duplicate {kind} name '{name}'")]
44 DuplicateName { kind: EntityKind, name: String },
45 #[error("duplicate particle PDG code {pdg}")]
46 DuplicatePdg { pdg: i64 },
47 #[error("{kind} '{name}' references unknown {target_kind} '{target}' through '{field}'")]
48 UnknownReference {
49 kind: EntityKind,
50 name: String,
51 field: &'static str,
52 target_kind: EntityKind,
53 target: String,
54 },
55 #[error(
56 "particle '{particle}' names '{antiparticle}' as its antiparticle, but '{antiparticle}' names '{actual_antiparticle}'"
57 )]
58 NonReciprocalAntiparticle {
59 particle: String,
60 antiparticle: String,
61 actual_antiparticle: String,
62 },
63 #[error(
64 "particle '{particle}' references propagator '{propagator}', which belongs to particle '{propagator_particle}'"
65 )]
66 PropagatorParticleMismatch {
67 particle: String,
68 propagator: String,
69 propagator_particle: String,
70 },
71 #[error(
72 "Lorentz structure '{lorentz_structure}' has spin signature {lorentz_spins:?}, but vertex rule '{vertex}' requires {particle_spins:?}"
73 )]
74 LorentzSpinMismatch {
75 vertex: String,
76 lorentz_structure: String,
77 particle_spins: Vec<i64>,
78 lorentz_spins: Vec<i64>,
79 },
80 #[error("external parameter '{name}' has no value")]
81 ExternalParameterWithoutValue { name: String },
82 #[error("internal parameter '{name}' has neither a value nor an expression")]
83 UnresolvedInternalParameter { name: String },
84 #[error("vertex rule '{name}' has {actual} coupling rows, but {expected} color structures")]
85 CouplingRowCount {
86 name: String,
87 expected: usize,
88 actual: usize,
89 },
90 #[error(
91 "coupling row {row} of vertex rule '{name}' has {actual} entries, but {expected} Lorentz structures"
92 )]
93 CouplingColumnCount {
94 name: String,
95 row: usize,
96 expected: usize,
97 actual: usize,
98 },
99}
100
101#[derive(Debug, Error)]
103pub enum ModelError {
104 #[error(
105 "model '{model}' has no supported minimal photon–fermion or photon–scalar vertex from which to infer electric charge"
106 )]
107 MissingElectricCharge { model: String },
108 #[error(
109 "model '{model}' has inconsistent electric charge normalizations in its photon vertices"
110 )]
111 InconsistentElectricCharge { model: String },
112 #[error("could not read model data from '{}': {source}", path.display())]
113 Read {
114 path: PathBuf,
115 #[source]
116 source: io::Error,
117 },
118 #[error("could not write model data to '{}': {source}", path.display())]
119 Write {
120 path: PathBuf,
121 #[source]
122 source: io::Error,
123 },
124 #[error("invalid mathmlname for parameter '{name}': {source}")]
125 InvalidParameterMathMl {
126 name: String,
127 #[source]
128 source: MathMlSubsetError,
129 },
130 #[error("invalid JSON: {0}")]
131 Json(#[from] serde_json::Error),
132 #[error(
133 "could not parse {kind} '{name}' field '{field}' as a symbolic expression: {message}; expression: {expression}"
134 )]
135 SymbolicParse {
136 kind: EntityKind,
137 name: String,
138 field: &'static str,
139 expression: String,
140 message: String,
141 },
142 #[error(transparent)]
143 Validation(#[from] ModelValidationError),
144 #[error("{kind} '{key}' was not found in model '{model}'")]
145 NotFound {
146 model: String,
147 kind: EntityKind,
148 key: String,
149 },
150 #[error("parameter card references unknown parameter '{name}'")]
151 UnknownCardParameter { name: String },
152 #[error("cyclic internal parameter definitions prevent expansion of {parameters:?}")]
153 CyclicParameterDefinitions { parameters: Vec<String> },
154 #[error("external parameter '{name}' has no value for the default parameter card")]
155 MissingCardValue { name: String },
156 #[error("parameter '{name}' has a non-finite scalar value")]
157 NonFiniteScalarBinding { name: String },
158 #[error("scalar bindings have a cyclic or unresolved named dependency")]
159 UnresolvedScalarBindings,
160}