Skip to main content

phasesmith_workflows/
lib.rs

1//! Application-neutral native refinement and project workflows.
2//!
3//! This crate is the shared host boundary for Python adapters and future
4//! native application consumers. It contains no `PyO3`, Tauri, GUI, or
5//! file-format types.
6//! Applications normally use it through
7//! [`phasesmith::workflows`](https://docs.rs/phasesmith/latest/phasesmith/).
8//!
9//! # Workflow families
10//!
11//! - **Backgrounds:** differentiable Chebyshev, polynomial, point, amorphous,
12//!   and composite models.
13//! - **Le Bail:** intensity extraction with optional profile/lattice
14//!   refinement, covariance, checkpoints, and dynamic reflection domains.
15//! - **TOF Le Bail:** typed microsecond-domain fixed-instrument calculation and
16//!   nonnegative intensity extraction with fused d/instrument derivatives.
17//! - **Rietveld:** calculation, prepared objective products, structural and
18//!   general refinement, staged recipes, and joint multi-histogram refinement.
19//! - **Quantitative analysis:** validated Hill–Howard phase fractions.
20//! - **Infrastructure:** typed parameters, bounds, constraints, residuals,
21//!   cancellation, limits, events, clocks, diagnostics, and checkpoints.
22//!
23//! # Selecting the level
24//!
25//! Use `calculate_*` functions for a deterministic forward calculation. Use a
26//! `Prepared*Objective` when integrating a custom optimizer and consuming
27//! values/JVP/VJP products directly. Use `refine_*` for the built-in solver, and
28//! the corresponding `_with_runtime` variant when an application needs
29//! cancellation, progress events, or checkpoint sinks.
30//!
31//! [`LeBailInput`] and [`RietveldInput`] own validated observations and domain
32//! state. Result/checkpoint records and structured event diagnostics are the
33//! stable programmatic output; human-readable event messages should not be
34//! parsed.
35//!
36//! # Application integration
37//!
38//! Workflows are synchronous and application-neutral. A desktop or async host
39//! runs them on its chosen worker mechanism, connects
40//! [`RefinementRuntime`] to its cancellation/event bridge, and uses
41//! `phasesmith-persistence` for native project state. The `joint_pbso4` example
42//! in this package demonstrates a complete Rust-only X-ray/neutron refinement.
43//! The facade provides collected mathematical references for
44//! [refinement](https://docs.rs/phasesmith/latest/phasesmith/guide/mathematics/refinement/)
45//! and
46//! [background models](https://docs.rs/phasesmith/latest/phasesmith/guide/mathematics/backgrounds/).
47
48mod backgrounds;
49mod constraints;
50mod lattice;
51mod lebail;
52mod parameters;
53mod phase_scale_estimation;
54mod profile_estimation;
55mod quantitative;
56mod residuals;
57mod rietveld;
58mod rietveld_general_objective;
59mod rietveld_general_parameters;
60mod rietveld_general_solver;
61mod rietveld_joint;
62mod rietveld_joint_solver;
63mod rietveld_objective;
64mod rietveld_parameters;
65mod rietveld_project;
66mod rietveld_recipe;
67mod rietveld_solver;
68mod runtime;
69mod sample_physics;
70mod tof_lebail;
71mod tof_multibank;
72mod tof_multibank_geometry;
73mod tof_multibank_instrument;
74mod tof_multibank_lattice;
75mod tof_multibank_project;
76mod tof_project;
77mod tof_structural_multibank;
78mod tof_structural_multibank_solver;
79mod tof_structural_project;
80
81pub use backgrounds::{
82    AmorphousBackground, AmorphousPeak, BackgroundBasis, BackgroundError, BackgroundModel,
83    ChebyshevBackground, CompositeBackground, DifferentiableBackground, PointBackground,
84    PolynomialBackground,
85};
86pub use constraints::{
87    AffineConstraint, Constraint, ConstraintDerivativeMatrix, ConstraintError, ConstraintTransform,
88    FixedConstraint, LinearConstraint, LinearTerm,
89};
90pub use lattice::{
91    CwLatticeGeometry, GeneratedLatticeDomain, LatticeBounds, LatticeError,
92    LatticeParameterization, LatticeReflectionDomain, TofLatticeGeometry, cw_lattice_geometry,
93    tof_lattice_geometry,
94};
95pub use lebail::{
96    CoincidentReflectionGroup, CovarianceMatrix, IntensityExtractionResult, LeBailCalculation,
97    LeBailCheckpoint, LeBailError, LeBailInput, LeBailIterationRecord, LeBailOptions, LeBailPhase,
98    LeBailResult, ParameterChange, PhasePatternComponent, ReflectionIntensity,
99    build_lebail_parameter_set, build_lebail_parameter_set_with_lattice, calculate_lebail_pattern,
100    calculate_lebail_pattern_with_background, extract_lebail_intensities,
101    initialize_lebail_intensities, iterate_lebail_once, lebail_background_parameter_key,
102    lebail_instrument_parameter_key, lebail_lattice_parameter_key, lebail_phase_scale_key,
103    lebail_reflection_position_key, refine_lebail, refine_lebail_with_runtime,
104};
105pub use parameters::{ParameterBounds, ParameterError, ParameterKey, ParameterSet, ParameterSpec};
106pub use phase_scale_estimation::{
107    PhaseScaleEstimationError, PhaseScaleEstimationResult, estimate_initial_phase_scales,
108};
109pub use profile_estimation::{
110    ProfileEstimationError, ProfileEstimationInput, ProfileEstimationMode,
111    ProfileEstimationOptions, ProfileEstimationResult, ProfileEstimationStage,
112    ProfileEstimationStageKind, estimate_effective_profile, starting_profile_from_fwhm,
113};
114pub use quantitative::{
115    PhaseWeightFraction, QuantitativeError, QuantitativePhase, QuantitativePhaseAnalysis,
116    quantitative_phase_analysis, quantitative_phase_analysis_with_covariance,
117};
118pub use residuals::{
119    ResidualError, ResidualEvaluation, ResidualOptions, evaluate_residuals, evaluate_tof_residuals,
120};
121pub use rietveld::{
122    RietveldCalculation, RietveldCalculationOptions, RietveldError, RietveldInput, RietveldPhase,
123    RietveldPhaseCalculation, RietveldTopologyChange, calculate_rietveld_pattern,
124};
125pub use rietveld_general_objective::{
126    DEFAULT_MAX_LINEARIZATION_ELEMENTS, PreparedGeneralFreeLinearization,
127    PreparedGeneralRietveldObjective, RietveldGeneralObjectiveError,
128};
129pub use rietveld_general_parameters::{
130    RietveldGeneralParameterError, RietveldInstrumentParameter, RietveldParameterLayout,
131    RietveldParameterSelection,
132};
133pub use rietveld_general_solver::{
134    RietveldCovarianceMatrix, RietveldCovarianceOptions, RietveldGeneralCheckpoint,
135    RietveldGeneralRefinementError, RietveldGeneralRefinementResult, RietveldParameterCorrelation,
136    refine_general_rietveld, refine_general_rietveld_with_runtime,
137};
138pub use rietveld_joint::{
139    JointRietveldError, JointRietveldGradient, JointRietveldHistogram, JointRietveldLayout,
140    JointRietveldProduct, PreparedJointRietveldObjective,
141};
142pub use rietveld_joint_solver::{
143    JointRietveldCheckpoint, JointRietveldIterationRecord, JointRietveldMetrics,
144    JointRietveldRefinementError, JointRietveldRefinementOptions, JointRietveldRefinementResult,
145    JointRietveldTopologyChange, refine_joint_rietveld, refine_joint_rietveld_with_runtime,
146};
147pub use rietveld_objective::{
148    PreparedRietveldLinearization, PreparedRietveldObjective, RietveldObjectiveError,
149};
150pub use rietveld_parameters::{
151    RietveldParameterError, RietveldStructuralLayout, RietveldStructuralSelection,
152    SiteCoordinateModel,
153};
154pub use rietveld_project::{RietveldAnalysis, RietveldProjectError, RietveldProjectState};
155pub use rietveld_recipe::{
156    RietveldRecipe, RietveldRecipeError, RietveldRecipeMode, RietveldRecipeSinks, RietveldStage,
157    RietveldStageResult, RietveldWorkflowResult, intelligent_rietveld_recipe, run_rietveld_recipe,
158    run_rietveld_recipe_with_sinks, validate_rietveld_recipe,
159};
160pub use rietveld_solver::{
161    RietveldCheckpoint, RietveldIterationRecord, RietveldRefinementError,
162    RietveldRefinementOptions, RietveldRefinementResult, refine_rietveld,
163    refine_rietveld_with_runtime,
164};
165pub use runtime::{
166    CancellationError, CancellationToken, CheckpointSink, DiagnosticValue, MonotonicClock,
167    RefinementEvent, RefinementEventKind, RefinementEventSink, RefinementLimits, RefinementRuntime,
168    RefinementStop, RuntimeClock, RuntimeError, TerminationReason,
169};
170pub use sample_physics::{
171    EvaluatedSamplePhysics, RietveldSamplePhysicsModel, SamplePhysicsError, SamplePhysicsParameter,
172};
173pub use tof_lebail::{
174    TofChebyshevBackground, TofChebyshevBasis, TofLeBailCalculation, TofLeBailCheckpoint,
175    TofLeBailError, TofLeBailInput, TofLeBailIterationRecord, TofLeBailOptions, TofLeBailPhase,
176    TofLeBailResult, TofReflectionIntensity, calculate_tof_lebail_pattern, refine_tof_lebail,
177    refine_tof_lebail_with_runtime,
178};
179pub use tof_multibank::{
180    TofLeBailBank, TofMultiBankCheckpoint, TofMultiBankCheckpointBank, TofMultiBankError,
181    TofMultiBankInput, TofMultiBankIterationRecord, TofMultiBankMetrics, TofMultiBankResult,
182    TofMultiBankResultBank, refine_tof_multibank, refine_tof_multibank_with_runtime,
183};
184pub use tof_multibank_geometry::{
185    TofGeometryCorrelation, TofGeometryDiagnostics, TofGeometryParameterKey,
186    TofMultiBankGeometryCheckpoint, TofMultiBankGeometryCheckpointBank, TofMultiBankGeometryError,
187    TofMultiBankGeometryInput, TofMultiBankGeometryIterationRecord, TofMultiBankGeometryOptions,
188    TofMultiBankGeometryResult, refine_tof_multibank_geometry,
189    refine_tof_multibank_geometry_with_runtime,
190};
191pub use tof_multibank_instrument::{
192    TofBankInstrumentModel, TofBankInstrumentState, TofInstrumentCorrelation,
193    TofInstrumentDiagnostics, TofInstrumentParameterBound, TofInstrumentParameterChange,
194    TofMultiBankInstrumentCheckpoint, TofMultiBankInstrumentCheckpointBank,
195    TofMultiBankInstrumentError, TofMultiBankInstrumentInput,
196    TofMultiBankInstrumentIterationRecord, TofMultiBankInstrumentOptions,
197    TofMultiBankInstrumentResult, refine_tof_multibank_instrument,
198    refine_tof_multibank_instrument_with_runtime,
199};
200pub use tof_multibank_lattice::{
201    TofLatticeParameterChange, TofMultiBankLatticeCheckpoint, TofMultiBankLatticeError,
202    TofMultiBankLatticeInput, TofMultiBankLatticeIterationRecord, TofMultiBankLatticeOptions,
203    TofMultiBankLatticeResult, TofSharedLatticePhase, TofSharedLatticeState,
204    refine_tof_multibank_lattice, refine_tof_multibank_lattice_with_runtime,
205};
206pub use tof_multibank_project::{
207    TofMultiBankGeometryAnalysis, TofMultiBankGeometryProjectState, TofMultiBankProjectError,
208};
209pub use tof_project::{TofLeBailAnalysis, TofLeBailProjectState, TofProjectError};
210pub use tof_structural_multibank::{
211    PreparedStructuralTofMultiBankObjective, StructuralTofBank, StructuralTofBankCalculation,
212    StructuralTofMultiBankCalculation, StructuralTofMultiBankError, StructuralTofMultiBankGradient,
213    StructuralTofMultiBankInput, StructuralTofMultiBankLayout, StructuralTofMultiBankProduct,
214};
215pub use tof_structural_multibank_solver::{
216    StructuralTofMultiBankCheckpoint, StructuralTofMultiBankIterationRecord,
217    StructuralTofMultiBankRefinementError, StructuralTofMultiBankRefinementOptions,
218    StructuralTofMultiBankRefinementResult, refine_structural_tof_multibank,
219    refine_structural_tof_multibank_with_runtime,
220};
221pub use tof_structural_project::{
222    StructuralTofMultiBankAnalysis, StructuralTofMultiBankProjectState, StructuralTofProjectError,
223};