Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-28 09:20:53

0001 // Copyright (c) 2025 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _MathUtils_Config_HeaderFile
0015 #define _MathUtils_Config_HeaderFile
0016 
0017 #include <limits>
0018 
0019 //! Modern math solver configuration structures.
0020 namespace MathUtils
0021 {
0022 
0023 //! Squared function tolerance for Newton iterations (matching math_FunctionSetRoot Eps).
0024 constexpr double THE_NEWTON_FTOL_SQ = 1.0e-32;
0025 
0026 //! Relative parametric step tolerance factor for Newton iterations.
0027 constexpr double THE_NEWTON_STEP_TOL_FACTOR = 1.0e-16;
0028 
0029 //! Default maximum iterations for Newton solvers.
0030 constexpr size_t THE_NEWTON_MAX_ITER = 100;
0031 
0032 //=================================================================================================
0033 //! @name Newton 2D Solver Constants
0034 //! Constants for 2D Newton-Raphson solver (MathSys_Newton2D).
0035 //=================================================================================================
0036 
0037 //! Determinant threshold below which 2x2 Jacobian matrix is considered singular.
0038 //! When |det(J)| < threshold, falls back to SVD or gradient descent.
0039 constexpr double THE_NEWTON2D_SINGULAR_DET = 1.0e-25;
0040 
0041 //! Squared gradient threshold below which point is considered a critical point.
0042 //! When |grad|^2 < threshold, Newton iteration is at a stationary point.
0043 constexpr double THE_NEWTON2D_CRITICAL_GRAD_SQ = 1.0e-60;
0044 
0045 //! Maximum step size as fraction of domain size for bounded Newton iterations.
0046 constexpr double THE_NEWTON2D_MAX_STEP_RATIO = 0.5;
0047 
0048 //! Domain extension factor for soft boundary handling.
0049 //! Solutions slightly outside domain (by this fraction) are allowed if converged.
0050 //! Reduced to 1e-4 to closely match old algorithm behavior.
0051 constexpr double THE_NEWTON2D_DOMAIN_EXT = 1.0e-4;
0052 
0053 //! Stagnation progress ratio - improvement required each iteration to avoid stagnation.
0054 //! Value of 0.999 means at least 0.1% improvement required.
0055 constexpr double THE_NEWTON2D_STAGNATION_RATIO = 0.999;
0056 
0057 //! Number of iterations without progress before declaring stagnation.
0058 constexpr int THE_NEWTON2D_STAGNATION_COUNT = 3;
0059 
0060 //! Maximum line search backtracking iterations.
0061 constexpr int THE_NEWTON2D_LINE_SEARCH_MAX = 8;
0062 
0063 //! Relative step threshold below which line search is skipped.
0064 //! If step^2 / domain^2 < threshold, Newton is converging well.
0065 constexpr double THE_NEWTON2D_SKIP_LINESEARCH_SQ = 0.01;
0066 
0067 //! Tolerance relaxation factor for accepting stagnated solutions.
0068 //! Stagnated solution accepted if |F| < tolerance * factor.
0069 constexpr double THE_NEWTON2D_STAGNATION_RELAX = 10.0;
0070 
0071 //! Function increase threshold triggering line search backtracking.
0072 //! If f(new) > f(old) * threshold, backtracking is triggered.
0073 constexpr double THE_NEWTON2D_BACKTRACK_TRIGGER = 1.5;
0074 
0075 //! Backtracking acceptance ratio for line search.
0076 //! Step accepted if f(new) < f(old) * ratio.
0077 constexpr double THE_NEWTON2D_BACKTRACK_ACCEPT = 1.2;
0078 
0079 //! Maximum relaxation factor for solutions at max iterations.
0080 constexpr double THE_NEWTON2D_MAXITER_RELAX = 10.0;
0081 
0082 //=================================================================================================
0083 //! @name Hessian Classification Constants
0084 //! Constants for extremum classification using second derivatives.
0085 //=================================================================================================
0086 
0087 //! Relative tolerance for Hessian degeneracy detection.
0088 //! If |det(H)| < threshold * |H_ii| * |H_jj|, Hessian is considered degenerate.
0089 constexpr double THE_HESSIAN_DEGENERACY_REL = 1.0e-8;
0090 
0091 //! Absolute tolerance for Hessian degeneracy detection.
0092 //! Minimum threshold for determinant comparison.
0093 constexpr double THE_HESSIAN_DEGENERACY_ABS = 1.0e-20;
0094 
0095 //=================================================================================================
0096 //! @name Line Search Constants
0097 //! Constants for line search acceptance conditions.
0098 //=================================================================================================
0099 
0100 //! Armijo condition constant (sufficient decrease).
0101 //! Step accepted if: f(x + alpha*d) <= f(x) + c1 * alpha * grad_f . d
0102 constexpr double THE_ARMIJO_C1 = 1.0e-4;
0103 
0104 //! Configuration for iterative solvers.
0105 //! Provides common settings for convergence criteria and iteration limits.
0106 struct Config
0107 {
0108   int    MaxIterations = 100;     //!< Maximum number of iterations allowed
0109   double Tolerance     = 1.0e-10; //!< General convergence tolerance
0110   double XTolerance    = 1.0e-10; //!< Tolerance for solution change |x_{n+1} - x_n|
0111   double FTolerance    = 1.0e-10; //!< Tolerance for function value |f(x)|
0112 
0113   //! Minimum step size before declaring convergence or failure.
0114   double StepMin = std::numeric_limits<double>::epsilon();
0115 
0116   //! Default constructor with standard tolerances.
0117   Config() = default;
0118 
0119   //! Constructor with custom tolerance (sets all tolerances to same value).
0120   //! @param theTolerance convergence tolerance
0121   //! @param theMaxIter maximum iterations
0122   explicit Config(double theTolerance, int theMaxIter = 100)
0123       : MaxIterations(theMaxIter),
0124         Tolerance(theTolerance),
0125         XTolerance(theTolerance),
0126         FTolerance(theTolerance)
0127   {
0128   }
0129 };
0130 
0131 //! Configuration for bounded 1D optimization and root finding.
0132 //! Extends Config with interval bounds.
0133 struct BoundedConfig : Config
0134 {
0135   double LowerBound = -std::numeric_limits<double>::max(); //!< Lower bound of search interval
0136   double UpperBound = std::numeric_limits<double>::max();  //!< Upper bound of search interval
0137 
0138   //! Default constructor.
0139   BoundedConfig() = default;
0140 
0141   //! Constructor with bounds.
0142   //! @param theLower lower bound
0143   //! @param theUpper upper bound
0144   //! @param theTolerance convergence tolerance
0145   //! @param theMaxIter maximum iterations
0146   BoundedConfig(double theLower,
0147                 double theUpper,
0148                 double theTolerance = 1.0e-10,
0149                 int    theMaxIter   = 100)
0150       : Config(theTolerance, theMaxIter),
0151         LowerBound(theLower),
0152         UpperBound(theUpper)
0153   {
0154   }
0155 };
0156 
0157 //! Configuration for N-dimensional optimization with optional bounds.
0158 //! Bounds are passed separately as math_Vector for flexibility.
0159 struct NDimConfig : Config
0160 {
0161   bool UseBounds = false; //!< Whether to enforce bounds during optimization
0162 
0163   //! Default constructor.
0164   NDimConfig() = default;
0165 
0166   //! Constructor with tolerance.
0167   //! @param theTolerance convergence tolerance
0168   //! @param theMaxIter maximum iterations
0169   //! @param theUseBounds whether to use bounds
0170   explicit NDimConfig(double theTolerance, int theMaxIter = 100, bool theUseBounds = false)
0171       : Config(theTolerance, theMaxIter),
0172         UseBounds(theUseBounds)
0173   {
0174   }
0175 };
0176 
0177 //! Configuration for numerical integration.
0178 //! Provides settings for quadrature order and adaptive refinement.
0179 struct IntegConfig
0180 {
0181   int    InitialOrder  = 15;      //!< Initial number of quadrature points
0182   int    MaxOrder      = 61;      //!< Maximum quadrature order (Gauss-Legendre limit)
0183   int    MaxIterations = 100;     //!< Maximum adaptive subdivision iterations
0184   double Tolerance     = 1.0e-10; //!< Relative tolerance for error estimation
0185 
0186   //! Default constructor.
0187   IntegConfig() = default;
0188 
0189   //! Constructor with custom tolerance.
0190   //! @param theTolerance relative tolerance
0191   //! @param theMaxIter maximum adaptive iterations
0192   explicit IntegConfig(double theTolerance, int theMaxIter = 100)
0193       : MaxIterations(theMaxIter),
0194         Tolerance(theTolerance)
0195   {
0196   }
0197 };
0198 
0199 //! Configuration for linear algebra solvers.
0200 //! Provides settings for singularity detection and pivoting.
0201 struct LinConfig
0202 {
0203   double SingularityTolerance = 1.0e-15; //!< Tolerance for detecting singular matrices
0204   bool   UsePivoting          = true;    //!< Whether to use pivoting for stability
0205 
0206   //! Default constructor.
0207   LinConfig() = default;
0208 };
0209 
0210 } // namespace MathUtils
0211 
0212 #endif // _MathUtils_Config_HeaderFile