Skip to main content

tensor4all_treetn/linsolve/common/
options.rs

1//! Common options for linsolve algorithms.
2
3use crate::TruncationOptions;
4use tensor4all_core::SvdTruncationPolicy;
5
6/// Options for the linsolve algorithm.
7#[derive(Debug, Clone)]
8pub struct LinsolveOptions {
9    /// Number of full sweeps to perform.
10    ///
11    /// A full sweep visits each edge twice (forward and backward) using an Euler tour.
12    pub nfullsweeps: usize,
13    /// Truncation options for factorization.
14    pub truncation: TruncationOptions,
15    /// Tolerance for GMRES convergence.
16    pub krylov_tol: f64,
17    /// Maximum GMRES iterations per local solve.
18    pub krylov_maxiter: usize,
19    /// Krylov subspace dimension (restart parameter).
20    pub krylov_dim: usize,
21    /// Coefficient a₀ in (a₀ + a₁ * A) * x = b.
22    pub a0: f64,
23    /// Coefficient a₁ in (a₀ + a₁ * A) * x = b.
24    pub a1: f64,
25    /// Convergence tolerance for early termination.
26    /// If Some(tol), stop when relative residual < tol.
27    pub convergence_tol: Option<f64>,
28}
29
30impl Default for LinsolveOptions {
31    fn default() -> Self {
32        Self {
33            nfullsweeps: 5,
34            truncation: TruncationOptions::default(),
35            krylov_tol: 1e-10,
36            krylov_maxiter: 100,
37            krylov_dim: 30,
38            a0: 0.0,
39            a1: 1.0,
40            convergence_tol: None,
41        }
42    }
43}
44
45impl LinsolveOptions {
46    /// Create new options with specified number of full sweeps.
47    pub fn new(nfullsweeps: usize) -> Self {
48        Self {
49            nfullsweeps,
50            ..Default::default()
51        }
52    }
53
54    /// Set number of full sweeps.
55    pub fn with_nfullsweeps(mut self, nfullsweeps: usize) -> Self {
56        self.nfullsweeps = nfullsweeps;
57        self
58    }
59
60    /// Set truncation options.
61    pub fn with_truncation(mut self, truncation: TruncationOptions) -> Self {
62        self.truncation = truncation;
63        self
64    }
65
66    /// Set maximum bond dimension.
67    pub fn with_max_rank(mut self, max_rank: usize) -> Self {
68        self.truncation = self.truncation.with_max_rank(max_rank);
69        self
70    }
71
72    /// Set the SVD truncation policy used by TreeTN truncation steps.
73    pub fn with_svd_policy(mut self, policy: SvdTruncationPolicy) -> Self {
74        self.truncation = self.truncation.with_svd_policy(policy);
75        self
76    }
77
78    /// Set GMRES tolerance.
79    pub fn with_krylov_tol(mut self, tol: f64) -> Self {
80        self.krylov_tol = tol;
81        self
82    }
83
84    /// Set maximum GMRES iterations.
85    pub fn with_krylov_maxiter(mut self, maxiter: usize) -> Self {
86        self.krylov_maxiter = maxiter;
87        self
88    }
89
90    /// Set Krylov subspace dimension.
91    pub fn with_krylov_dim(mut self, dim: usize) -> Self {
92        self.krylov_dim = dim;
93        self
94    }
95
96    /// Set coefficients a₀ and a₁.
97    pub fn with_coefficients(mut self, a0: f64, a1: f64) -> Self {
98        self.a0 = a0;
99        self.a1 = a1;
100        self
101    }
102
103    /// Set convergence tolerance for early termination.
104    pub fn with_convergence_tol(mut self, tol: f64) -> Self {
105        self.convergence_tol = Some(tol);
106        self
107    }
108}
109
110#[cfg(test)]
111mod tests;