From 959f8ae739b3877381c13a8a28af532febcde15c Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Mon, 16 Nov 2026 13:03:29 +0100 Subject: [PATCH] Refinement: L-BFGS on the Gauss-Newton normal matrix, one protocol on the CPU and the GPU The minimiser's initial inverse Hessian is the target's sparse normal matrix, solved by conjugate gradients: the restraints' Gauss-Newton J^T J in full (RestraintNormalMatrix, on the slot's threads or its GPU), the X-ray target's curvature per atom on the diagonal (calibrated once per trial), the U restraints' exact Hessian, and per TLS body a dense 20 x 20 block. The L-BFGS pairs learn what the matrix leaves out (the X-ray coupling of neighbouring atoms). 10 iterations for the positions and 7 for B, U and TLS per macro-cycle, instead of 30 and 20. The protocol is the same on both paths: no population (kept as a setting, off by default), two rounds; lean_on_cpu is gone. Measured, refine-only, 19 sets on the GPU against the former GPU protocol (population, 2 rounds, plain L-BFGS): R-free -0.0005 mean, -0.0012 median, worse by > 0.002 on 2 and better on 6, -LL free lower on 11; 158k -> 49k evaluations. The same protocol with the plain L-BFGS on the CPU's former protocol took 76k. Quiet single runs at 32 threads: cytc CPU 113 s (was 114 s with one round), GPU 6.1 s (10.9 s); myob CPU 41.5 s (53.1 s), GPU 4.1 s (5.1 s). The damped Newton steps, the joint xyz+B target, the X-ray coupling term, the adaptive stop and the experiment switches of the prototype are removed: pure Gauss-Newton steps converged each trial further than the protocol's decisions were made for, R-free +0.0005 to +0.0022 in the mean of the 19 sets. BRestraintCurvature and the L-BFGS's diagonal and Oren-Shanno scaling are gone with them. Tests: RefinementMinimiser_quadratic on the new interface, RefinementMinimiser_conjugate_gradients, RefinementRestraints_gauss_newton (J^T J against the Hessian near the ideal geometry). Docs: CPU_DATA_ANALYSIS_REFINEMENT 15.6 and the population, references, ACKNOWLEDGEMENT (Hestenes & Stiefel 1952, the normal matrix after Murshudov, Vagin & Dodson 1997). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- docs/ACKNOWLEDGEMENT.md | 20 +- docs/CPU_DATA_ANALYSIS.md | 4 +- docs/CPU_DATA_ANALYSIS_REFINEMENT.md | 60 +- .../structure_refinement/Refinement.cpp | 722 +++--------------- .../structure_refinement/Refinement.h | 16 +- .../RefinementMinimiser.h | 315 ++------ .../RefinementRestraints.cpp | 12 - .../RefinementRestraints.h | 5 +- tests/RefinementTest.cpp | 102 ++- 9 files changed, 329 insertions(+), 927 deletions(-) diff --git a/docs/ACKNOWLEDGEMENT.md b/docs/ACKNOWLEDGEMENT.md index 83de168ef..0c86fbbd6 100644 --- a/docs/ACKNOWLEDGEMENT.md +++ b/docs/ACKNOWLEDGEMENT.md @@ -598,20 +598,12 @@ Cryst. A34, 791-809 [doi:10.1107/S0567739478001618](https://doi.org/10.1107/S056 memory BFGS method for large scale optimization" (1989), Math. Program. 45, 503-528 [doi:10.1007/BF01589116](https://doi.org/10.1007/BF01589116); its two-loop recursion: J. Nocedal, "Updating quasi-Newton matrices with limited storage" (1980), Math. Comp. 35, 773-782 -[doi:10.1090/S0025-5718-1980-0572855-7](https://doi.org/10.1090/S0025-5718-1980-0572855-7). The -initial inverse Hessian is rescaled at each iteration by the latest pair, the self-scaling of -S. S. Oren and D. G. Luenberger, "Self-scaling variable metric (SSVM) algorithms. Part I" (1974), -Management Sci. 20, 845-862 [doi:10.1287/mnsc.20.5.845](https://doi.org/10.1287/mnsc.20.5.845), and -D. F. Shanno and K.-H. Phua, "Matrix conditioning and nonlinear optimization" (1978), Math. Program. -14, 149-160 [doi:10.1007/BF01588962](https://doi.org/10.1007/BF01588962). - -**Newton minimiser (experimental)** — damped Newton steps on a sparse normal matrix: the X-ray -target's curvature per atom on the diagonal and the restraints' Gauss-Newton matrix in full, as -REFMAC refines (Murshudov, Vagin & Dodson 1997, above), solved by conjugate gradients with the -damping of K. Levenberg, "A method for the solution of certain non-linear problems in least squares" -(1944), Q. Appl. Math. 2, 164-168 [doi:10.1090/qam/10666](https://doi.org/10.1090/qam/10666), and -D. W. Marquardt, "An algorithm for least-squares estimation of nonlinear parameters" (1963), J. Soc. -Ind. Appl. Math. 11, 431-441 [doi:10.1137/0111030](https://doi.org/10.1137/0111030). +[doi:10.1090/S0025-5718-1980-0572855-7](https://doi.org/10.1090/S0025-5718-1980-0572855-7). Its +initial inverse Hessian is the target's sparse normal matrix - the X-ray target's curvature per atom +on the diagonal and the restraints' Gauss-Newton matrix in full - as REFMAC refines on its normal +matrix (Murshudov, Vagin & Dodson 1997, above), solved by conjugate gradients: M. R. Hestenes and +E. Stiefel, "Methods of conjugate gradients for solving linear systems" (1952), J. Res. Natl. Bur. +Stand. 49, 409-436 [doi:10.6028/jres.049.044](https://doi.org/10.6028/jres.049.044). **Geometry restraints** — those of the CCP4 Monomer Library, a subset of which rugnux carries (see [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md)). A. A. Vagin, R. A. Steiner, A. A. Lebedev, diff --git a/docs/CPU_DATA_ANALYSIS.md b/docs/CPU_DATA_ANALYSIS.md index bfc7217c1..503030a2f 100644 --- a/docs/CPU_DATA_ANALYSIS.md +++ b/docs/CPU_DATA_ANALYSIS.md @@ -66,12 +66,13 @@ The methods draw on, and in places reimplement, solutions from: - H. F. Walker & P. Ni, "Anderson acceleration for fixed-point iterations", *SIAM J. Numer. Anal.* **49** (2011), 1715-1735, after D. G. Anderson, "Iterative procedures for nonlinear integral equations", *J. ACM* **12** (1965), 547-560 (the acceleration of the per-frame scaling loops, §10.6). - R. A. Fisher, "Frequency distribution of the values of the correlation coefficient in samples from an indefinitely large population", *Biometrika* **10** (1915), 507-521 (the z-transformation on which the correction surfaces' held-out half-set CC1/2 is compared). - R. C. Agarwal, "A new least-squares refinement technique based on the fast Fourier transform algorithm", *Acta Cryst.* **A34** (1978), 791-809 (the gradient of the refinement target, §15). -- N. S. Pannu & R. J. Read, "Improved structure refinement through maximum likelihood", *Acta Cryst.* **A52** (1996), 659-668, and G. N. Murshudov, A. A. Vagin & E. J. Dodson, "Refinement of macromolecular structures by the maximum-likelihood method", *Acta Cryst.* **D53** (1997), 240-255 (the maximum-likelihood amplitude target and the B restraints, §15). +- N. S. Pannu & R. J. Read, "Improved structure refinement through maximum likelihood", *Acta Cryst.* **A52** (1996), 659-668, and G. N. Murshudov, A. A. Vagin & E. J. Dodson, "Refinement of macromolecular structures by the maximum-likelihood method", *Acta Cryst.* **D53** (1997), 240-255 (the maximum-likelihood amplitude target, the B restraints and the minimiser's sparse normal matrix, §15). - V. Y. Lunin & T. P. Skovoroda, "R-free likelihood-based estimates of errors for phases calculated from atomic models", *Acta Cryst.* **A51** (1995), 880-887 (the likelihood parameters from the free set, §15). - A. A. Vagin et al., "REFMAC5 dictionary: organization of prior chemical knowledge and guidelines for its use", *Acta Cryst.* **D60** (2004), 2184-2195 (the restraint dictionary, §15). - P. V. Afonine et al., "Towards automated crystallographic structure refinement with phenix.refine", *Acta Cryst.* **D68** (2012), 352-367 (the weight chosen among refinements by R-free, §15). - D. C. Liu & J. Nocedal, "On the limited memory BFGS method for large scale optimization", *Math. Program.* **45** (1989), 503-528 (the minimiser, §15). - J. Nocedal, "Updating quasi-Newton matrices with limited storage", *Math. Comp.* **35** (1980), 773-782 (the two-loop recursion of the minimiser, §15.6). +- M. R. Hestenes & E. Stiefel, "Methods of conjugate gradients for solving linear systems", *J. Res. Natl. Bur. Stand.* **49** (1952), 409-436 (the normal equations of the minimiser, §15.6). - G. N. Murshudov, P. Skubák, A. A. Lebedev et al., "REFMAC5 for the refinement of macromolecular crystal structures", *Acta Cryst.* **D67** (2011), 355-367 (the likelihood's D from the working set and its variance from the free, and the Kullback-Leibler B restraints, §15.3, §15.5). - P. Roversi, E. Blanc, S. Johnson & S. M. Lea, "Tetartohedral twinning could happen to you too", *Acta Cryst.* **D68** (2012), 418-424 (twin refinement with more than two domains, and the free set closed under the twin domains, §15.12). - K. A. Sultana, I. Alexeev, I. Kursula, P. Mäntsälä, J. Niemi & G. Schneider, "Structure determination by multiwavelength anomalous diffraction of aclacinomycin oxidoreductase: indications of multidomain pseudomerohedral twinning", *Acta Cryst.* **D63** (2007), 149-159 (a six-domain twin of a monoclinic crystal on a hexagonal metric, §15.12). @@ -120,7 +121,6 @@ The methods draw on, and in places reimplement, solutions from: - V. Luzzati, "Traitement statistique des erreurs dans la détermination des structures cristallines", *Acta Cryst.* **5** (1952), 802-810 (the fall-off of $\sigma_A$ with resolution for a model's coordinate error, Luzzati's $D$, in the likelihood of a placement, §14.10). - D. Liebschner, P. V. Afonine, A. G. Urzhumtsev & P. D. Adams, "Implementation of the riding hydrogen model in CCTBX to support the next generation of X-ray and neutron joint refinement in Phenix", *Methods Enzymol.* **634** (2020), 177-199 (the riding hydrogens, placed by GEMMI's code, §15.2). - D. L. Theobald, "Rapid calculation of RMSDs using a quaternion-based characteristic polynomial", *Acta Cryst.* **A61** (2005), 478-480, and P. Liu, D. K. Agrafiotis & D. L. Theobald, *J. Comput. Chem.* **31** (2010), 1561-1563 (the QCP superposition behind the NCS rms criterion, via GEMMI, §15.5). -- S. S. Oren & D. G. Luenberger, "Self-scaling variable metric (SSVM) algorithms. Part I", *Management Sci.* **20** (1974), 845-862, and D. F. Shanno & K.-H. Phua, "Matrix conditioning and nonlinear optimization", *Math. Program.* **14** (1978), 149-160 (the rescaling of the L-BFGS initial inverse Hessian by the latest pair, §15.6). (list is not exhaustive; the full citations, with DOIs, are in [ACKNOWLEDGEMENT.md](ACKNOWLEDGEMENT.md)) diff --git a/docs/CPU_DATA_ANALYSIS_REFINEMENT.md b/docs/CPU_DATA_ANALYSIS_REFINEMENT.md index 0a03eb028..293b59be8 100644 --- a/docs/CPU_DATA_ANALYSIS_REFINEMENT.md +++ b/docs/CPU_DATA_ANALYSIS_REFINEMENT.md @@ -286,26 +286,41 @@ Whether the restraints are used at all, and how tightly ($\sigma$), the weight t ### 15.6 The minimiser -Positions and B factors are minimised in turn, each by an L-BFGS of rugnux's own (`RefinementMinimiser`), -with the two-loop recursion of Nocedal and of Liu and Nocedal, a memory of 20 pairs and a backtracking line -search from the full step to sufficient decrease, its first trial moving no position by more than 0.3 Å and -no B by more than 10 Ų. What sets it apart from a textbook L-BFGS: +Positions, B factors, anisotropic U and TLS parameters are minimised in turn, each by an L-BFGS of rugnux's own +(`RefinementMinimiser`), with the two-loop recursion of Nocedal and of Liu and Nocedal, a memory of 20 pairs and a +backtracking line search from the full step to sufficient decrease, its first trial moving no position by more +than 0.3 Å and no B by more than 10 Ų. What sets it apart from a textbook L-BFGS is its **initial inverse +Hessian: the target's Gauss-Newton normal matrix**, as REFMAC refines on its sparse normal matrix (Murshudov, +Vagin & Dodson 1997). The matrix holds what is known of the curvature in advance; the L-BFGS pairs add what it +leaves out. -- **Its memory is kept from one macro-cycle to the next.** A macro-cycle is a few dozen iterations on a target - whose scale and likelihood parameters were refitted in between; starting each from what the earlier ones - learnt of the curvature, instead of from a steepest-descent step, is what lets two macro-cycles of a - weight trial get as far as many restarted ones. Each weight trial starts with an empty memory. -- **The B steps are scaled by their curvature.** The initial inverse Hessian of the B factors is a - diagonal: the X-ray target's curvature in each atom's B - its shape from the likelihood's curvature in - $|F_\mathrm{model}|$ and the atom's own scattering, summed in resolution shells, calibrated once per trial - against central differences of the gradient along a random $\pm1$ direction - plus the B restraints' - curvature, each element at least a thousandth of the median. The positions take none: their restraints' - curvature is that of stiff bonds and angles, which move atoms together, and as a diagonal it slows the soft - (torsion and X-ray) directions rather than scaling them. +- **The restraints' part is their Gauss-Newton matrix $J^\mathsf{T}J$, in full.** Every restraint is the square of + a residual (or close to it: the NCS distances' robust loss), so its gradient is $2r\,\nabla r$ and + $J = \nabla E / \sqrt{2E}$ is its row of the Jacobian, $\sqrt2\,\nabla r$; a plane contributes one row per + atom. $J^\mathsf{T}J$ is positive semi-definite where the exact Hessian is not (torsions, repulsions), and near + the ideal geometry it is the exact Hessian. Its products $J^\mathsf{T}(Jv)$ are two gathers - over the rows, + then over each atom's entries - on the slot's threads, or on its GPU. +- **The X-ray part is per atom, on the diagonal.** The X-ray target's curvature in an atom's $x$, $y$ and $z$ is + the likelihood's curvature in $|F_\mathrm{model}|$ summed in resolution shells against the atom's own part of + it, $(2\pi)^2 s^2/3\,f(s)^2\,e^{-Bs^2/2}$ (the reflections taken as spread evenly over directions); in its B, + $(s^2/4)^2 f(s)^2 e^{-Bs^2/2}$, a third of it to each element of a U; and a TLS body's dense $20\times20$ block + is that curvature carried through the derivatives of its atoms' U in the body's parameters. Each is calibrated + once per trial against central differences of the gradient along a random $\pm1$ direction. The coupling of + neighbouring atoms through the X-ray target is not in it; the pairs learn it. +- **The anisotropic U restraints** are quadratic in U, so their Hessian is exact by one difference of their + gradient. +- **The matrix is solved, not inverted.** Each L-BFGS direction solves the normal equations by conjugate + gradients to a tenth of the residual (at most 50 iterations), preconditioned by each atom's $3\times3$ block for + the positions and by the diagonal elsewhere. The Jacobian is taken again at each point the minimiser stands on. +- **Its memory is kept from one macro-cycle to the next.** Each weight trial starts with an empty memory. -The diagonal is rescaled at each iteration by the latest pair (Oren-Shanno scaling in that metric). A solve -stops after its iteration budget (30 for the positions and 20 for the B factors per macro-cycle) or where an -iteration lowers the target by less than $10^{-7}$ of it. +A solve stops after its iteration budget (10 for the positions and 7 for the B factors, U or TLS per +macro-cycle) or where an iteration lowers the target by less than $10^{-7}$ of it. Against the plain L-BFGS this +replaced (a diagonal initial inverse Hessian for the B factors, none for the positions, 30 and 20 iterations), a +trial takes about a third of the evaluations. With it the population (§15.7) no longer gains, and the protocol +is the same on the CPU and the GPU - two rounds, no population - at a third of the evaluations the GPU's protocol +took before and R-free 0.0005 lower in the mean over 19 refinements (-LL free lower on 11 of them), and at fewer +evaluations than the CPU's former one-round protocol. ### 15.7 The weights, decided by trial @@ -337,8 +352,10 @@ are: stays within 0.001 of the best so far, and the trajectory ends at its best step. On a deposited model, whose free set the depositor's refinement has already fitted, R-work falls while R-free rises, and the trajectory ends where it began. -3. **The population**, from where the trajectory ended: twelve members (at most as many as the trial slots, so - that they run in one wave; none below two) refined side by side, each a trial at the +3. **The population** (off by default, `RefinementSettings::population`: with the minimiser of §15.6 its members + took nothing off R-free over 19 refinements that the second round did not), from where the trajectory ended: + up to twelve members (at most as many as the trial slots, so that they run in one wave; none below two) refined + side by side, each a trial at the trajectory's weights but on its own path - the positions as they are or kicked at random by up to 0.2-0.3 Å in each coordinate, on the data as they are or **blurred** along a schedule, 60 then 20 Ų (the landscape of 4 Å, then of 2.2 Å), 100 then 30, or 30 then 10. The blur raises the likelihood's variance of each reflection @@ -741,7 +758,7 @@ What the refinement does not do yet - each a known gap, not a design choice: - L. Piela, J. Kostrowicki & H. A. Scheraga, "On the multiple-minima problem in the conformational analysis of molecules: deformation of the potential energy hypersurface by the diffusion equation method", *J. Phys. Chem.* **93** (1989), 3339-3346, and A. Jack & M. Levitt, "Refinement of large structures by simultaneous minimization of energy and R factor", *Acta Cryst.* **A34** (1978), 931-935 (the members refined on blurred data, §15.7). - J. Praznikar, P. V. Afonine, G. Guncar, P. D. Adams & D. Turk, "Averaged kick maps: less noise, more signal... and probably less bias", *Acta Cryst.* **D65** (2009), 921-931, and J. C. Nwachukwu, M. R. Southern, J. R. Kiefer, P. V. Afonine, P. D. Adams, T. C. Terwilliger & K. W. Nettles, "Improved crystallographic structures using extensive combinatorial refinement", *Structure* **21** (2013), 1923-1930 (the kicked members, their averaged map and the crossover residue by residue, §15.7). - J. Desmet, M. De Maeyer, B. Hazes & I. Lasters, "The dead-end elimination theorem and its use in protein side-chain positioning", *Nature* **356** (1992), 539-542; J. Besag, "On the statistical analysis of dirty pictures", *J. R. Statist. Soc.* **B48** (1986), 259-279; R. P. Joosten, K. Joosten, S. X. Cohen, G. Vriend & A. Perrakis, "Automatic rebuilding and optimization of crystallographic structures in the Protein Data Bank", *Bioinformatics* **27** (2011), 3392-3398 (the side-chain field: states, the conditional-modes solver and its order, §15.5). -- N. S. Pannu & R. J. Read, "Improved structure refinement through maximum likelihood", *Acta Cryst.* **A52** (1996), 659-668, and G. N. Murshudov, A. A. Vagin & E. J. Dodson, "Refinement of macromolecular structures by the maximum-likelihood method", *Acta Cryst.* **D53** (1997), 240-255 (the maximum-likelihood amplitude target and its figure of merit, §15.2, §15.8). +- N. S. Pannu & R. J. Read, "Improved structure refinement through maximum likelihood", *Acta Cryst.* **A52** (1996), 659-668, and G. N. Murshudov, A. A. Vagin & E. J. Dodson, "Refinement of macromolecular structures by the maximum-likelihood method", *Acta Cryst.* **D53** (1997), 240-255 (the maximum-likelihood amplitude target and its figure of merit, §15.2, §15.8; the minimiser's sparse normal matrix, §15.6). - G. N. Murshudov, P. Skubák, A. A. Lebedev, N. S. Pannu, R. A. Steiner, R. A. Nicholls, M. D. Winn, F. Long & A. A. Vagin, "REFMAC5 for the refinement of macromolecular crystal structures", *Acta Cryst.* **D67** (2011), 355-367 (D from the working set and the variance from the free, §15.3; the Kullback-Leibler form of the B restraints, §15.5; the twinned refinement §15.12 simplifies, its twin laws pruned by fraction and its fractions refined by least squares). - R. J. Read, "Improved Fourier coefficients for maps using phases from partial structures with errors", *Acta Cryst.* **A42** (1986), 140-149 (the m, D weighting of the map coefficients, §15.8). - T. O. Yeates, "Detecting and overcoming crystal twinning", *Methods Enzymol.* **276** (1997), 344-358 (the twinned intensity, §15.12). @@ -762,6 +779,7 @@ What the refinement does not do yet - each a known gap, not a design choice: - Z. Dauter & M. Dauter, "Entering a new phase: using solvent halide ions in protein structure determination", *Structure* **9** (2001), R21-R26 (halides at sites with few donors, §15.10). - N. Echols et al., "Automated identification of elemental ions in macromolecular crystal structures", *Acta Cryst.* **D70** (2014), 1104-1114 (a light ion flagged by its density above the waters', §15.10). - J. Nocedal, "Updating quasi-Newton matrices with limited storage", *Math. Comp.* **35** (1980), 773-782, and D. C. Liu & J. Nocedal, "On the limited memory BFGS method for large scale optimization", *Math. Program.* **45** (1989), 503-528 (the minimiser, §15.6). +- M. R. Hestenes & E. Stiefel, "Methods of conjugate gradients for solving linear systems", *J. Res. Natl. Bur. Stand.* **49** (1952), 409-436 (the normal equations of the minimiser, §15.6). - A. T. Brünger, M. Karplus & G. A. Petsko, "Crystallographic refinement by simulated annealing: application to crambin", *Acta Cryst.* **A45** (1989), 50-61 (the starting weight from the ratio of the gradients, §15.7). - P. V. Afonine et al., "Towards automated crystallographic structure refinement with phenix.refine", *Acta Cryst.* **D68** (2012), 352-367 (weights chosen among refinements by R-free within geometry bounds, and the overall B moved into the atoms, §15.3, §15.7). - A. T. Brünger, "Free R value: a novel statistical quantity for assessing the accuracy of crystal structures", *Nature* **355** (1992), 472-475 (R-free). diff --git a/image_analysis/structure_refinement/Refinement.cpp b/image_analysis/structure_refinement/Refinement.cpp index c64486ade..8b5c834db 100644 --- a/image_analysis/structure_refinement/Refinement.cpp +++ b/image_analysis/structure_refinement/Refinement.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -100,12 +99,6 @@ constexpr double MAX_STEP_XYZ = 0.3, MAX_STEP_B = 10.0; // The loosest NCS restraint a trajectory step tries (A); looser than that, the copies are refined independently. constexpr double NCS_SIGMA_MAX = 0.4; -// PROTOTYPE instrumentation (lane k): MINSTAT/TRIALSTAT/... lines on stderr where RUGNUX_REFINE_STATS is set. -bool RefineStats() { - static const bool on = getenv("RUGNUX_REFINE_STATS") != nullptr; - return on; -} - // The figure of merit of the likelihood, , at X = 2 Fo D Fc / var: I1(X)/I0(X) for an acentric // reflection, tanh(X / 2) for a centric one. // Following Murshudov, Vagin & Dodson (1997) Acta Cryst. D53, 240-255 @@ -619,8 +612,7 @@ struct Context { std::vector XrayBCurvatureShape() const { return XrayCurvatureShape(rows, false); } // The same for each of an atom's x, y and z: its own part of the likelihood's curvature in |F_model| is then // (2 pi)^2 s^2 / 3 f(s)^2 exp(-B s^2 / 2), the reflections taken as spread evenly over directions - the X-ray - // block of the normal matrix as a multiple of I per atom (Murshudov, Vagin & Dodson 1997 keep the X-ray blocks - // of atoms on the diagonal). `r`: the row model the engine has (blurred or not). + // block of the normal matrix as a multiple of I per atom. `r`: the row model the engine has (blurred or not). std::vector XrayXyzCurvatureShape(const XrayRowModel &r) const { return XrayCurvatureShape(r, true); } std::vector XrayCurvatureShape(const XrayRowModel &rows, bool xyz) const { constexpr int SHELLS = 32; @@ -665,144 +657,6 @@ struct Context { return shape; } - // The X-ray normal matrix between the positions of neighbouring atoms of `params` (riders left out), in the units of - // XrayXyzCurvatureShape(): for atoms k and l a vector r apart, the sum over the shells of - // w f_k f_l exp(-(B_k + B_l) s^2 / 4) (2 pi s)^2 [(j1(x) / x) I - j2(x) r r^T / r^2], x = 2 pi s r - the overlap of - // their density derivatives, the reflections spread evenly over directions; at r = 0 the shape's own (2 pi)^2 s^2 / 3. - // Out to where it has fallen below a hundredth of its value at 0 for two carbons at the median B, and stays there. - // Per parameter index its neighbours and blocks (xx yy zz xy xz yz), both ways round. - struct XrayCoupling { - std::vector start, other; - std::vector> block; - }; - XrayCoupling XrayXyzCoupling(const XrayRowModel &rows, const std::vector ¶ms) const { - constexpr int SHELLS = 32; - double s2_max = 0; - for (double s2 : refl.inv_d2) - s2_max = std::max(s2_max, s2); - std::vector w(SHELLS, 0), s2_sum(SHELLS, 0); - std::vector n(SHELLS, 0); - for (size_t m = 0; m < refl.Size(); m++) { - if (refl.free[m]) - continue; - const int k = std::min(SHELLS - 1, static_cast(refl.inv_d2[m] / s2_max * SHELLS)); - const double kd = rows.k_total[m] * rows.d[m]; - w[k] += 2.0 * kd * kd / rows.var[m]; - s2_sum[k] += refl.inv_d2[m]; - n[k]++; - } - std::vector s(SHELLS, 0); - for (int k = 0; k < SHELLS; k++) - s[k] = n[k] > 0 ? std::sqrt(s2_sum[k] / n[k]) : 0.0; - std::map, std::vector> fs; // per element and charge, f per shell - auto form = [&](const gemmi::Atom &a) -> const std::vector & { - std::vector &f = fs[{static_cast(a.element.elem), a.charge}]; - if (f.empty()) { - const auto &coef = IonFormFactor(a.element.elem, a.charge); - f.resize(SHELLS, 0); - for (int k = 0; k < SHELLS; k++) - f[k] = coef.calculate_sf(s[k] * s[k] / 4.0); - } - return f; - }; - // a = j1(x) / x and b = j2(x) per shell at distance r, for f_k f_l exp(-(B_k + B_l) s^2 / 4) given per shell - auto kernel = [&](const std::vector &ff, double r, double &a_sum, double &b_sum) { - a_sum = b_sum = 0; - for (int k = 0; k < SHELLS; k++) { - if (n[k] == 0) - continue; - const double x = 2 * M_PI * s[k] * r, c = w[k] * ff[k] * 4 * M_PI * M_PI * s[k] * s[k]; - double a, b; - if (x < 1e-3) { - a = 1.0 / 3.0 - x * x / 30.0; - b = x * x / 15.0; - } else { - const double sx = std::sin(x), cx = std::cos(x); - a = (sx / (x * x) - cx / x) / x; - b = (3.0 / (x * x) - 1.0) * sx / x - 3.0 * cx / (x * x); - } - a_sum += c * a; - b_sum += c * b; - } - }; - // the cutoff, for two carbons at the median B - std::vector bs; - for (int i : params) - bs.push_back(atoms[i]->b_iso); - XrayCoupling out; - out.start.assign(params.size() + 1, 0); - if (bs.empty()) - return out; - std::nth_element(bs.begin(), bs.begin() + bs.size() / 2, bs.end()); - const double b_med = bs[bs.size() / 2]; - gemmi::Atom carbon; - carbon.element = gemmi::El::C; - std::vector ff(SHELLS); - const std::vector &fc = form(carbon); - for (int k = 0; k < SHELLS; k++) - ff[k] = fc[k] * fc[k] * std::exp(-b_med * s[k] * s[k] / 2.0); - double a0, b0; - kernel(ff, 0.0, a0, b0); - double cutoff = 1.0; - for (double r = 1.0; r <= 8.0; r += 0.1) { - double a, b; - kernel(ff, r, a, b); - if (std::max(std::fabs(a), std::fabs(a - b)) > 0.01 * a0) - cutoff = r + 0.1; - } - // the neighbours by a grid of cells one cutoff wide - std::map, std::vector> cells; - auto cell_of = [&](const gemmi::Position &p) { - return std::array{static_cast(std::floor(p.x / cutoff)), static_cast(std::floor(p.y / cutoff)), - static_cast(std::floor(p.z / cutoff))}; - }; - auto is_param = [&](int i) { return rider_parent.empty() || rider_parent[i] < 0; }; - for (size_t k = 0; k < params.size(); k++) - if (is_param(params[k])) - cells[cell_of(atoms[params[k]]->pos)].push_back(static_cast(k)); - std::vector>>> rows_of(params.size()); - for (size_t k = 0; k < params.size(); k++) { - const gemmi::Atom &ak = *atoms[params[k]]; - if (!is_param(params[k])) - continue; - const std::array c = cell_of(ak.pos); - const std::vector &fk = form(ak); - for (int dx = -1; dx <= 1; dx++) - for (int dy = -1; dy <= 1; dy++) - for (int dz = -1; dz <= 1; dz++) { - const auto it = cells.find({c[0] + dx, c[1] + dy, c[2] + dz}); - if (it == cells.end()) - continue; - for (int l : it->second) { - if (l == static_cast(k)) - continue; - const gemmi::Atom &al = *atoms[params[l]]; - const gemmi::Position d = al.pos - ak.pos; - const double r = d.length(); - if (r >= cutoff || r <= 0) - continue; - const std::vector &fl = form(al); - for (int q = 0; q < SHELLS; q++) - ff[q] = fk[q] * fl[q] * ak.occ * al.occ * std::exp(-(ak.b_iso + al.b_iso) * s[q] * s[q] / 4.0); - double a, b; - kernel(ff, r, a, b); - const double u[3] = {d.x / r, d.y / r, d.z / r}; - rows_of[k].push_back({l, {a - b * u[0] * u[0], a - b * u[1] * u[1], a - b * u[2] * u[2], - -b * u[0] * u[1], -b * u[0] * u[2], -b * u[1] * u[2]}}); - } - } - std::sort(rows_of[k].begin(), rows_of[k].end(), [](const auto &x, const auto &y) { return x.first < y.first; }); - } - for (size_t k = 0; k < params.size(); k++) { - out.start[k + 1] = out.start[k] + static_cast(rows_of[k].size()); - for (const auto &[l, blk] : rows_of[k]) { - out.other.push_back(l); - out.block.push_back(blk); - } - } - return out; - } - // The factor that turns XrayBCurvatureShape() into the X-ray target's curvature: its second derivative along a // random +-1 direction of the B factors of `params`, by central differences of the gradient, over what the shape // predicts for it. @@ -953,7 +807,6 @@ public: gradient != nullptr ? &gr : nullptr, restraints); } *cost = weight_ * xray + restraints; - last_xray_ = xray; if (gradient == nullptr) return std::isfinite(*cost); ctx_.FoldRiders(gx); @@ -981,9 +834,6 @@ public: } Mode GetMode() const { return mode_; } - // The X-ray target and its gradient per atom (riders folded) at the last Evaluate(). - double LastXray() const { return last_xray_; } - const std::vector> &LastXrayGradient() const { return gx_; } double Weight() const { return weight_; } const std::vector &Atoms() const { return param_atoms_; } @@ -1002,66 +852,6 @@ private: // the gradients per atom, kept from evaluation to evaluation (7 MB each on a large model: made anew, their // pages fault in every time) mutable std::vector> gx_, gr_; - mutable double last_xray_ = 0; -}; - -// PROTOTYPE (RUGNUX_NEWTON_JOINT=1): the positions and the B factors at once, against X + R_geometry / w + R_B / adp_w - -// stationary where the positions' target (w X + R_geometry) and the B factors' (adp_w X + R_B) both are, so that one -// evaluation moves both. The parameters: x y z of every atom, then its B in units of B_UNIT (one step bound for all). -class XyzBTarget { -public: - static constexpr double B_UNIT = MAX_STEP_B / MAX_STEP_XYZ; - XyzBTarget(Context &ctx, const std::vector ¶m_atoms, double w, double adp_w, const RefinementTopology &topo) - : ctx_(ctx), xyz_(ctx, AtomTarget::Mode::Xyz, param_atoms, w, 0, topo), - b_(ctx, AtomTarget::Mode::B, param_atoms, adp_w, 1, topo), n_(param_atoms.size()), w_(w), adp_w_(adp_w) {} - - std::vector Get() const { - std::vector p = xyz_.Get(); - for (double b : b_.Get()) - p.push_back(b / B_UNIT); - return p; - } - void Set(const double *p) const { - std::vector b(p + 3 * n_, p + 4 * n_); - for (double &x : b) - x *= B_UNIT; - xyz_.Set(p); - b_.Set(b.data()); - } - double MaxStep() const { return MAX_STEP_XYZ; } - - bool Evaluate(const double *p, double *cost, double *gradient) const { - // each part's own evaluation at the same point gives w X + R_geometry and adp_w X + R_B, and their gradients - std::vector g_xyz(3 * n_), g_b(n_), b(p + 3 * n_, p + 4 * n_); - for (double &x : b) - x *= B_UNIT; - b_.Set(b.data()); - double f_xyz = 0; - if (!xyz_.Evaluate(p, &f_xyz, g_xyz.data())) - return false; - // the B part at the same model: its X-ray target is the same one, only its restraints are added - std::vector> gr; - const double r_b = ctx_.engine->EvaluateRestraints(1, ctx_.XyzB(), &gr); - const double xray = xyz_.LastXray(); - *cost = xray + (f_xyz - w_ * xray) / w_ + r_b / adp_w_; - const std::vector> &gx = xyz_.LastXrayGradient(); - for (size_t k = 0; k < 3 * n_; k++) - gradient[k] = g_xyz[k] / w_; - for (size_t k = 0; k < n_; k++) { - const int i = xyz_.Atoms()[k]; - const double g = gx[i][3] + gr[i][3] / adp_w_; - const double bk = b[k]; - const bool held = (bk <= ctx_.BLow(i) && g > 0) || (bk >= ctx_.BHigh(i) && g < 0); - gradient[3 * n_ + k] = held ? 0.0 : g * B_UNIT; - } - return std::isfinite(*cost); - } - -private: - Context &ctx_; - AtomTarget xyz_, b_; - size_t n_; - double w_, adp_w_; }; // The X-ray target's derivatives in U of every atom, with what the target says of a riding hydrogen's B (its parent's @@ -1203,114 +993,6 @@ private: std::vector radius_; }; -// The minimiser's diagonal for the B factors of `param_atoms` (RefinementMinimiser::SetDiagonal): weight times the -// X-ray target's curvature in B plus the B restraints', each element at least a thousandth of the median. The -// positions take none: their restraints' curvature is that of stiff bonds and angles, which move atoms together, so -// as a diagonal it slows rather than scales the minimiser. -std::vector BDiagonal(const std::vector &xray, const std::vector &restraints, - const std::vector ¶m_atoms, double weight) { - std::vector h; - for (int i : param_atoms) - h.push_back(weight * xray[i] + restraints[i]); - if (h.empty()) // no atom the dictionary describes - return h; - std::vector sorted = h; - std::nth_element(sorted.begin(), sorted.begin() + sorted.size() / 2, sorted.end()); - const double floor = std::max(1e-3 * sorted[sorted.size() / 2], 1e-12); - for (double &v : h) - v = std::max(v, floor); - return h; -} - -// `minimiser` carries its memory from the call before; `diagonal` is the curvature it scales the steps by (none: 1). -template -void Minimise(Context &ctx, const Target &target, int iterations, RefinementMinimiser &minimiser, - std::vector diagonal = {}, const char *tag = "?") { - std::vector p = target.Get(); - if (p.empty()) - return; - minimiser.SetDiagonal(diagonal.empty() ? std::vector(p.size(), 1.0) : std::move(diagonal)); - const RefinementMinimiser::Outcome o = minimiser.Minimise([&](const std::vector &x, double &f, std::vector &g) { - return target.Evaluate(x.data(), &f, g.data()); - }, - p, iterations, MINIMISER_F_TOLERANCE, target.MaxStep(), [&]() { return ctx.Cancelled(); }); - target.Set(p.data()); // where the minimiser ended, not where its last line search probed - if (RefineStats()) fprintf(stderr, "MINSTAT lbfgs.%s it=%d ev=%d lim=%d n=%zu f0=%.6g f1=%.6g\n", tag, o.iterations, o.evaluations, - iterations, p.size(), o.f_start, o.f_end); -} - -// PROTOTYPE (RUGNUX_NEWTON=1): the minimisation by damped Newton steps (RefinementNewton) on the Gauss-Newton normal -// matrix, as REFMAC refines on its sparse normal matrix: the X-ray target's curvature per parameter on the diagonal (no -// coupling between atoms), the restraints' J^T J in full. -// Following Murshudov, Vagin & Dodson (1997) Acta Cryst. D53, 240-255 -struct NewtonSettings { - int xyz_iterations = 30, b_iterations = 20; // the most per macro-cycle, as the L-BFGS's - double relative_stop = 0.0; - double step_scale = 1.0; // the step bound, times the L-BFGS's -}; -const NewtonSettings &Newton() { - static const NewtonSettings s = [] { - NewtonSettings n; - if (const char *e = getenv("RUGNUX_NEWTON_XYZ")) - n.xyz_iterations = std::atoi(e); - if (const char *e = getenv("RUGNUX_NEWTON_B")) - n.b_iterations = std::atoi(e); - if (const char *e = getenv("RUGNUX_NEWTON_RELSTOP")) - n.relative_stop = std::atof(e); - if (const char *e = getenv("RUGNUX_NEWTON_STEPSCALE")) - n.step_scale = std::atof(e); - return n; - }(); - return s; -} - -bool NewtonQuasi(); -// `target` by `newton` from where it is, with H v = product and D = diagonal (each element > 0). -template -void MinimiseNewton(Context &ctx, const Target &target, RefinementNewton &newton, const RefinementNewton::Product &product, - const std::vector &diagonal, int iterations, const char *tag, - const RefinementNewton::Preconditioner &precondition = nullptr) { - std::vector p = target.Get(); - if (p.empty()) - return; - if (NewtonQuasi() && newton.lbfgs != nullptr) { - // RUGNUX_NEWTON=2: the L-BFGS, its initial inverse Hessian the Gauss-Newton matrix solved by CG - RefinementMinimiser &m = *newton.lbfgs; - if (const char *e = getenv("RUGNUX_NEWTON_CGTOL")) - newton.cg_tolerance = std::atof(e); - const int cg_before = newton.cg_iterations; - m.SetPreconditioner([&](const std::vector &x, std::vector &q) { - std::vector z; - newton.SolveUndamped(product, diagonal, precondition, x, q, z); - q.swap(z); - }); - const RefinementMinimiser::Outcome o = m.Minimise( - [&](const std::vector &x, double &f, std::vector &g) { return target.Evaluate(x.data(), &f, g.data()); }, - p, iterations, MINIMISER_F_TOLERANCE, target.MaxStep(), [&]() { return ctx.Cancelled(); }); - m.SetPreconditioner(nullptr); - target.Set(p.data()); - if (RefineStats()) - fprintf(stderr, "MINSTAT qn.%s it=%d ev=%d lim=%d cg=%d f0=%.6g f1=%.6g\n", tag, o.iterations, o.evaluations, - iterations, newton.cg_iterations - cg_before, o.f_start, o.f_end); - return; - } - newton.relative_stop = Newton().relative_stop; - newton.trace = getenv("RUGNUX_NEWTON_TRACE") != nullptr; - if (const char *e = getenv("RUGNUX_NEWTON_CGMAX")) - newton.cg_max = std::atoi(e); - if (const char *e = getenv("RUGNUX_NEWTON_CGTOL")) - newton.cg_tolerance = std::atof(e); - const int cg_before = newton.cg_iterations; - const RefinementMinimiser::Outcome o = newton.Minimise( - [&](const std::vector &x, double &f, std::vector &g) { return target.Evaluate(x.data(), &f, g.data()); }, - product, diagonal, p, iterations, MINIMISER_F_TOLERANCE, target.MaxStep() * Newton().step_scale, - [&]() { return ctx.Cancelled(); }, precondition); - target.Set(p.data()); - if (RefineStats()) - fprintf(stderr, "MINSTAT newton.%s it=%d ev=%d lim=%d cg=%d f0=%.6g f1=%.6g\n", tag, o.iterations, o.evaluations, - iterations, newton.cg_iterations - cg_before, o.f_start, o.f_end); -} - // Each element at least a thousandth of the median (a parameter the target does not see still takes a finite step). std::vector FlooredDiagonal(std::vector d) { if (d.empty()) @@ -1323,6 +1005,37 @@ std::vector FlooredDiagonal(std::vector d) { return d; } +// The minimisers' initial inverse Hessian (RefinementMinimiser::SetInverseHessian) is the Gauss-Newton normal matrix of +// the target, as REFMAC refines on its sparse normal matrix: the X-ray target's curvature per atom on the diagonal and +// the restraints' J^T J in full, solved for each direction by conjugate gradients to a tenth of the residual. +// Following Murshudov, Vagin & Dodson (1997) Acta Cryst. D53, 240-255 +constexpr int NORMAL_CG_ITERATIONS = 50; +constexpr double NORMAL_CG_TOLERANCE = 0.1; + +// `target` by `minimiser` (its memory from the call before) from where it is, H0 the inverse of the normal matrix: +// hv = H v at x (product), z = M^-1 r for a preconditioner M of it (precondition). +template +void MinimiseWithNormalMatrix(Context &ctx, const Target &target, RefinementMinimiser &minimiser, int iterations, + const std::function &, const std::vector &, + std::vector &)> &product, + const std::function &, std::vector &)> &precondition) { + std::vector p = target.Get(); + if (p.empty()) + return; + minimiser.SetInverseHessian([&](const std::vector &x, std::vector &q) { + std::vector z; + ConjugateGradients([&](const std::vector &v, std::vector &hv) { product(x, v, hv); }, precondition, + q, z, NORMAL_CG_ITERATIONS, NORMAL_CG_TOLERANCE); + q.swap(z); + }); + minimiser.Minimise([&](const std::vector &x, double &f, std::vector &g) { + return target.Evaluate(x.data(), &f, g.data()); + }, + p, iterations, MINIMISER_F_TOLERANCE, target.MaxStep(), [&]() { return ctx.Cancelled(); }); + minimiser.SetInverseHessian(nullptr); + target.Set(p.data()); // where the minimiser ended, not where its last line search probed +} + // The restraints' Gauss-Newton normal matrix J^T J (RestraintGaussNewton) on the x y z or the B of `param_atoms`, at the // point last given to At(); an atom that is not a parameter (a rider, an atom held) drops out of it. With the engine on // a GPU, on its card (RestraintNormalMatrixGPU). @@ -1340,7 +1053,7 @@ public: } void At(const RestraintSet &set, const std::vector> &xyzb) { - const RefinementStage stage("newton.jacobian"); + const RefinementStage stage("normal.jacobian"); #ifdef JFJOCH_USE_CUDA if (gpu_) return gpu_->At(set, xyzb); @@ -1371,7 +1084,7 @@ public: // hv = J^T J v: J v per row, then per parameter atom the sum over its entries void Multiply(const std::vector &v, std::vector &hv) { - const RefinementStage stage("newton.product"); + const RefinementStage stage("normal.product"); #ifdef JFJOCH_USE_CUDA if (gpu_) return gpu_->Multiply(v, hv); @@ -1444,27 +1157,6 @@ private: #endif }; -// hv[offset + 3k ..] += scale sum over the neighbours l of k of the X-ray block (k, l) times v[offset + 3l ..]. -void AddXrayCoupling(const Context::XrayCoupling &c, double scale, const std::vector &v, size_t offset, - std::vector &hv, size_t nthreads) { - const int n = static_cast(c.start.size()) - 1; - ParallelChunks(n, ThreadsForWork(n, nthreads, 4096), [&](int lo, int hi) { - for (int k = lo; k < hi; k++) { - double s0 = 0, s1 = 0, s2 = 0; - for (int q = c.start[k]; q < c.start[k + 1]; q++) { - const std::array &m = c.block[q]; - const double *x = &v[offset + 3 * c.other[q]]; - s0 += m[0] * x[0] + m[3] * x[1] + m[4] * x[2]; - s1 += m[3] * x[0] + m[1] * x[1] + m[5] * x[2]; - s2 += m[4] * x[0] + m[5] * x[1] + m[2] * x[2]; - } - hv[offset + 3 * k] += scale * s0; - hv[offset + 3 * k + 1] += scale * s1; - hv[offset + 3 * k + 2] += scale * s2; - } - }); -} - // z = M^-1 r for M symmetric positive definite (xx yy zz xy xz yz), by its adjugate. std::array Solve3(const std::array &m, double r0, double r1, double r2) { const double a = m[0], b = m[1], c = m[2], d = m[3], e = m[4], f = m[5]; @@ -1475,43 +1167,40 @@ std::array Solve3(const std::array &m, double r0, double r (c02 * r0 + c12 * r1 + c22 * r2) / det}; } -// The positions' preconditioner: per atom its 3 x 3 block of H (`blocks`, the restraints' over `scale`, plus the X-ray -// curvature `h`) and lambda times the damping's diagonal `diagonal`; parameters 3k..3k+2 of atom k from `offset` on. -void PreconditionBlocks(const std::vector> &blocks, double scale, const std::vector &h, - const std::vector &diagonal, size_t offset, const std::vector &r, double lambda, - std::vector &z) { +// The positions' preconditioner: per atom its 3 x 3 block of H, the restraints' (`blocks`) plus the X-ray curvature `h`. +void PreconditionBlocks(const std::vector> &blocks, const std::vector &h, + const std::vector &r, std::vector &z) { for (size_t k = 0; k < blocks.size(); k++) { - const size_t o = offset + 3 * k; - std::array m; - for (int c = 0; c < 6; c++) - m[c] = blocks[k][c] / scale; + std::array m = blocks[k]; for (int c = 0; c < 3; c++) - m[c] += h[o + c] + lambda * diagonal[o + c]; - const std::array s = Solve3(m, r[o], r[o + 1], r[o + 2]); - z[o] = s[0]; - z[o + 1] = s[1]; - z[o + 2] = s[2]; + m[c] += h[3 * k + c]; + const std::array s = Solve3(m, r[3 * k], r[3 * k + 1], r[3 * k + 2]); + z[3 * k] = s[0]; + z[3 * k + 1] = s[1]; + z[3 * k + 2] = s[2]; } } -// The positions or the B factors: H = diag(xray) (per atom, already times the weight) + the restraints' J^T J of `set`. -void MinimiseAtomsNewton(Context &ctx, const AtomTarget &target, RefinementNewton &newton, const std::vector &xray, - const RestraintSet &set, int iterations, const Context::XrayCoupling *coupling = nullptr, - double coupling_scale = 0) { +// The positions or the B factors: H = diag(xray) (per atom, already times the weight) + the restraints' J^T J of `set`, +// the Jacobian at the point of the product. Preconditioned per atom: by its 3 x 3 block of H at the start for the +// positions, by the diagonal for the B factors. +void MinimiseAtoms(Context &ctx, const AtomTarget &target, RefinementMinimiser &minimiser, const std::vector &xray, + const RestraintSet &set, int iterations) { const bool xyz = target.GetMode() == AtomTarget::Mode::Xyz; const int per = xyz ? 3 : 1; - std::vector p = target.Get(); - const size_t n = p.size(); + std::vector x_at = target.Get(); + const size_t n = x_at.size(); std::vector h(n); for (size_t k = 0; k < n; k++) h[k] = xray[target.Atoms()[k / per]]; RestraintNormalMatrix normal(ctx, target.Atoms(), xyz); - std::vector x_at = p; - target.Set(p.data()); + target.Set(x_at.data()); normal.At(set, ctx.XyzB()); std::vector diagonal = normal.Diagonal(); for (size_t k = 0; k < n; k++) diagonal[k] += h[k]; + diagonal = FlooredDiagonal(diagonal); + const std::vector> blocks = xyz ? normal.Blocks() : std::vector>(); auto product = [&](const std::vector &x, const std::vector &v, std::vector &hv) { if (x != x_at) { x_at = x; @@ -1521,86 +1210,22 @@ void MinimiseAtomsNewton(Context &ctx, const AtomTarget &target, RefinementNewto normal.Multiply(v, hv); for (size_t k = 0; k < n; k++) hv[k] += h[k] * v[k]; - if (coupling != nullptr) - AddXrayCoupling(*coupling, coupling_scale, v, 0, hv, ctx.nthreads); }; - diagonal = FlooredDiagonal(diagonal); - if (!xyz) { - MinimiseNewton(ctx, target, newton, product, diagonal, iterations, "b"); - return; - } - // each atom's 3 x 3 block of H at the start, fixed for the call - const std::vector> blocks = normal.Blocks(); - MinimiseNewton(ctx, target, newton, product, diagonal, iterations, "xyz", - [&](const std::vector &r, double lambda, std::vector &z) { - PreconditionBlocks(blocks, 1.0, h, diagonal, 0, r, lambda, z); - }); -} - -// The positions and the B factors at once (XyzBTarget): H the two blocks of MinimiseAtomsNewton(), each over its weight -// (`xray_xyz`, `xray_b`: per atom, the X-ray curvature alone), no coupling between them. -void MinimiseJointNewton(Context &ctx, const XyzBTarget &target, const std::vector &atoms, RefinementNewton &newton, - const std::vector &xray_xyz, const std::vector &xray_b, const RestraintSet &set_xyz, - const RestraintSet &set_b, double w, double adp_w, int iterations, - const Context::XrayCoupling *coupling = nullptr, double coupling_scale = 0) { - const size_t n = atoms.size(); - const double u2 = XyzBTarget::B_UNIT * XyzBTarget::B_UNIT; - RestraintNormalMatrix normal_xyz(ctx, atoms, true), normal_b(ctx, atoms, false); - std::vector x_at = target.Get(); - auto at = [&](const std::vector &x) { - target.Set(x.data()); - const std::vector> xyzb = ctx.XyzB(); - normal_xyz.At(set_xyz, xyzb); - normal_b.At(set_b, xyzb); + auto precondition = [&](const std::vector &r, std::vector &z) { + z.resize(r.size()); + if (xyz) + PreconditionBlocks(blocks, h, r, z); + else + for (size_t k = 0; k < n; k++) + z[k] = r[k] / diagonal[k]; }; - at(x_at); - std::vector diagonal(4 * n); - { - const std::vector dx = normal_xyz.Diagonal(), db = normal_b.Diagonal(); - for (size_t k = 0; k < n; k++) { - for (int c = 0; c < 3; c++) - diagonal[3 * k + c] = xray_xyz[atoms[k]] + dx[3 * k + c] / w; - diagonal[3 * n + k] = u2 * (xray_b[atoms[k]] + db[k] / adp_w); - } - } - std::vector vx(3 * n), vb(n), hx, hb; - auto product = [&](const std::vector &x, const std::vector &v, std::vector &hv) { - if (x != x_at) { - x_at = x; - at(x); - } - std::copy(v.begin(), v.begin() + 3 * n, vx.begin()); - std::copy(v.begin() + 3 * n, v.end(), vb.begin()); - normal_xyz.Multiply(vx, hx); - normal_b.Multiply(vb, hb); - hv.resize(4 * n); - for (size_t k = 0; k < n; k++) { - for (int c = 0; c < 3; c++) - hv[3 * k + c] = hx[3 * k + c] / w + xray_xyz[atoms[k]] * v[3 * k + c]; - hv[3 * n + k] = u2 * (hb[k] / adp_w + xray_b[atoms[k]] * v[3 * n + k]); - } - if (coupling != nullptr) - AddXrayCoupling(*coupling, coupling_scale, v, 0, hv, ctx.nthreads); - }; - diagonal = FlooredDiagonal(diagonal); - // the positions' 3 x 3 blocks of H at the start, fixed for the call; the B factors' diagonal - const std::vector> blocks = normal_xyz.Blocks(); - std::vector h(3 * n); - for (size_t k = 0; k < n; k++) - for (int c = 0; c < 3; c++) - h[3 * k + c] = xray_xyz[atoms[k]]; - MinimiseNewton(ctx, target, newton, product, diagonal, iterations, "xyzb", - [&](const std::vector &r, double lambda, std::vector &z) { - PreconditionBlocks(blocks, w, h, diagonal, 0, r, lambda, z); - for (size_t k = 3 * n; k < 4 * n; k++) - z[k] = r[k] / ((1 + lambda) * diagonal[k]); - }); + MinimiseWithNormalMatrix(ctx, target, minimiser, iterations, product, precondition); } // Each atom's U (in 8 pi^2 U): H = diag(xray) per element + the U restraints' Hessian, exact by differences of their -// gradient (they are quadratic in U); D the diagonal the L-BFGS takes. -void MinimiseUNewton(Context &ctx, const AdpTarget &target, RefinementNewton &newton, const std::vector &xray, - const std::vector &diagonal, int iterations) { +// gradient (they are quadratic in U); preconditioned by `diagonal`. +void MinimiseU(Context &ctx, const AdpTarget &target, RefinementMinimiser &minimiser, const std::vector &xray, + const std::vector &diagonal, int iterations) { const size_t n = xray.size(); std::vector x_at, g_at(n), xp(n), gp(n); auto product = [&](const std::vector &x, const std::vector &v, std::vector &hv) { @@ -1623,13 +1248,19 @@ void MinimiseUNewton(Context &ctx, const AdpTarget &target, RefinementNewton &ne for (size_t k = 0; k < n; k++) hv[k] += xray[k] * v[k]; }; - MinimiseNewton(ctx, target, newton, product, diagonal, iterations, "u"); + auto precondition = [&](const std::vector &r, std::vector &z) { + z.resize(n); + for (size_t k = 0; k < n; k++) + z[k] = r[k] / diagonal[k]; + }; + MinimiseWithNormalMatrix(ctx, target, minimiser, iterations, product, precondition); } // The TLS bodies: X-ray only, H = sum over each body's atoms of A^T C A, A the derivatives of the atom's U in the body's -// parameters (linear in them), C the X-ray curvature of its U per element (`xray_b`: of its B, a third of it to each). -void MinimiseTlsNewton(Context &ctx, const AdpTarget &target, RefinementNewton &newton, const std::vector &xray_b, - int iterations) { +// parameters (linear in them), C the X-ray curvature of its U per element (`xray_b`: of its B, a third of it to each); +// one dense block per body, preconditioned by its diagonal. +void MinimiseTls(Context &ctx, const AdpTarget &target, RefinementMinimiser &minimiser, const std::vector &xray_b, + int iterations) { constexpr int P = TlsBody::PARAMETERS; const size_t nb = ctx.tls.size(); std::vector> blocks(nb); @@ -1654,6 +1285,7 @@ void MinimiseTlsNewton(Context &ctx, const AdpTarget &target, RefinementNewton & for (size_t b = 0; b < nb; b++) for (int k = 0; k < P; k++) diagonal[b * P + k] = blocks[b][k * P + k]; + diagonal = FlooredDiagonal(diagonal); auto product = [&](const std::vector &, const std::vector &v, std::vector &hv) { hv.assign(v.size(), 0.0); for (size_t b = 0; b < nb; b++) @@ -1661,7 +1293,12 @@ void MinimiseTlsNewton(Context &ctx, const AdpTarget &target, RefinementNewton & for (int l = 0; l < P; l++) hv[b * P + k] += blocks[b][k * P + l] * v[b * P + l]; }; - MinimiseNewton(ctx, target, newton, product, FlooredDiagonal(diagonal), iterations, "tls"); + auto precondition = [&](const std::vector &r, std::vector &z) { + z.resize(r.size()); + for (size_t k = 0; k < r.size(); k++) + z[k] = r[k] / diagonal[k]; + }; + MinimiseWithNormalMatrix(ctx, target, minimiser, iterations, product, precondition); } // Groups of atoms that move as rigid bodies (one per chain) against the X-ray target: per body an angle-axis @@ -1840,18 +1477,6 @@ double BalancedWeight(Context &ctx, const std::vector &refinable) { std::vector> gx, gr; ctx.engine->Evaluate(ctx.Model(), &gx); std::vector> xyzb = ctx.XyzB(); - { // MEASUREMENT (lane k): the ratio of the gradients' norms at the model as given, unshaken - std::vector> g0; - ctx.engine->EvaluateRestraints(0, xyzb, &g0); - double a = 0, b = 0; - for (int i : refinable) - for (int c = 0; c < 3; c++) { - a += gx[i][c] * gx[i][c]; - b += g0[i][c] * g0[i][c]; - } - if (RefineStats()) fprintf(stderr, "WEIGHTSTAT unshaken_ratio=%.4g |gx|=%.4g |gr0|=%.4g\n", a > 0 ? std::sqrt(b / a) : 0.0, - std::sqrt(a), std::sqrt(b)); - } std::mt19937 rng(1); std::normal_distribution shake(0.0, 0.1); for (std::array &a : xyzb) @@ -2496,40 +2121,13 @@ double XrayGradientNorm(Context &ctx, const std::vector &refinable) { return n; } -// PROTOTYPE switch: RUGNUX_NEWTON=1 the positions, the B factors, the U and the TLS bodies by damped Newton steps. -bool NewtonOn() { - static const bool on = getenv("RUGNUX_NEWTON") != nullptr && std::string(getenv("RUGNUX_NEWTON")) != "0"; - return on; -} -// RUGNUX_NEWTON_PARTS (default "xyz,b,tls,u"): which of the minimisations take the Newton steps. -bool NewtonOn(const char *part) { - static const std::string parts = getenv("RUGNUX_NEWTON_PARTS") != nullptr ? getenv("RUGNUX_NEWTON_PARTS") : "xyz,b,tls,u"; - return NewtonOn() && parts.find(part) != std::string::npos; -} -// RUGNUX_NEWTON=2: the L-BFGS on top of the Gauss-Newton matrix (MinimiseNewton). -bool NewtonQuasi() { - static const bool on = getenv("RUGNUX_NEWTON") != nullptr && std::string(getenv("RUGNUX_NEWTON")) == "2"; - return on; -} -// RUGNUX_NEWTON_COUPLING=1: the X-ray normal matrix between neighbouring atoms' positions in H (XrayXyzCoupling). -bool NewtonCoupling() { - static const bool on = getenv("RUGNUX_NEWTON_COUPLING") != nullptr; - return on; -} -// RUGNUX_NEWTON_JOINT=1: where the B factors are refined isotropically, they and the positions at once (XyzBTarget). -bool NewtonJoint() { - static const bool on = getenv("RUGNUX_NEWTON_JOINT") != nullptr; - return on; -} // The X-ray curvature per atom in each of x, y, z at weight w, for the row model `rows` the engine has; `scale` // calibrated at the first call of the trial (negative: not yet). -std::vector NewtonXyzCurvature(Context &ctx, const std::vector &refinable, const XrayRowModel &rows, - double w, double &scale) { +std::vector XyzCurvature(Context &ctx, const std::vector &refinable, const XrayRowModel &rows, double w, + double &scale) { std::vector shape = ctx.XrayXyzCurvatureShape(rows); - if (scale < 0) { + if (scale < 0) scale = ctx.CalibrateXyzCurvature(refinable, shape); - if (RefineStats()) fprintf(stderr, "NEWTONCAL xyz_scale=%.4g\n", scale); - } for (double &x : shape) x *= w * scale; return shape; @@ -2584,13 +2182,9 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector &refinable, const s b(ctx, AtomTarget::Mode::B, refinable, adp_w, 1, topo); const AdpTarget tls_target(ctx, AdpTarget::Mode::Tls, refinable, 1.0, nullptr); const AdpTarget u_target(ctx, AdpTarget::Mode::U, u_atoms, adp_w, &u_restraints); - RefinementMinimiser xyz_minimiser(20, ctx.nthreads), b_minimiser(20, ctx.nthreads), tls_minimiser(20, ctx.nthreads); - RefinementNewton xyz_newton, b_newton, tls_newton, u_newton; - xyz_newton.lbfgs = &xyz_minimiser; - b_newton.lbfgs = &b_minimiser; - tls_newton.lbfgs = &tls_minimiser; - u_newton.lbfgs = &b_minimiser; - double xray_scale = 0, xyz_scale = -1, tls_scale = -1; + RefinementMinimiser xyz_minimiser, b_minimiser, tls_minimiser; + // the X-ray curvature's calibrations (CalibrateXyzCurvature, CalibrateBCurvature), once per trial + double xyz_scale = -1, b_scale = -1; for (int cycle = 0; cycle < cycles && !ctx.Cancelled(); cycle++) { PlaceRiders(ctx, topo); if (cycle == 0 && fit != nullptr) @@ -2603,15 +2197,11 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector &refinable, const s } if (adp_w > 0) ctx.MoveOverallB(); - bool joint_done = false; // the B factors refined with the positions (MinimiseJointNewton) if (!hold_positions) { - RestraintSet xyz_set; + const RestraintSet set = RestraintsNow(ctx, topo, ncs, ncs_sigma); { - const RestraintSet set = RestraintsNow(ctx, topo, ncs, ncs_sigma); const RefinementStage s1("set_restraints"); ctx.engine->SetRestraints(0, set); - if (NewtonOn()) - xyz_set = set; } const RefinementStage s2("minimise.xyz"); const double b_h = cycle < static_cast(tw.blur.size()) ? tw.blur[cycle] : 0.0; @@ -2626,62 +2216,33 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector &refinable, const s ctx.engine->SetRowModel(blurred); const double nh = XrayGradientNorm(ctx, refinable); const AtomTarget xyz_h(ctx, AtomTarget::Mode::Xyz, refinable, nh > 0 ? w * std::sqrt(n0 / nh) : w, 0, topo); - if (NewtonOn("xyz")) { - const std::vector h = NewtonXyzCurvature(ctx, refinable, blurred, xyz_h.Weight(), xyz_scale); - const Context::XrayCoupling c = NewtonCoupling() ? ctx.XrayXyzCoupling(blurred, refinable) - : Context::XrayCoupling(); - MinimiseAtomsNewton(ctx, xyz_h, xyz_newton, h, xyz_set, Newton().xyz_iterations, - NewtonCoupling() ? &c : nullptr, xyz_h.Weight() * xyz_scale); - } - else - Minimise(ctx, xyz_h, settings.xyz_iterations, xyz_minimiser, {}, "xyz_blur"); + MinimiseAtoms(ctx, xyz_h, xyz_minimiser, XyzCurvature(ctx, refinable, blurred, xyz_h.Weight(), xyz_scale), + set, settings.xyz_iterations); ctx.engine->SetRowModel(ctx.rows); - } else if (NewtonOn() && NewtonJoint() && kind == AdpHypothesis::Kind::Isotropic && adp_w > 0) { - const RestraintSet b_set = BRestraintsNow(ctx, topo); - ctx.engine->SetRestraints(1, b_set); - std::vector xray_b = ctx.XrayBCurvatureShape(); - if (cycle == 0) - xray_scale = ctx.CalibrateBCurvature(refinable, xray_b); - for (double &x : xray_b) - x *= xray_scale; - const XyzBTarget xyz_b(ctx, refinable, w, adp_w, topo); - const std::vector h = NewtonXyzCurvature(ctx, refinable, ctx.rows, 1.0, xyz_scale); - const Context::XrayCoupling c = NewtonCoupling() ? ctx.XrayXyzCoupling(ctx.rows, refinable) - : Context::XrayCoupling(); - MinimiseJointNewton(ctx, xyz_b, refinable, xyz_newton, h, xray_b, xyz_set, b_set, w, adp_w, - Newton().xyz_iterations, NewtonCoupling() ? &c : nullptr, xyz_scale); - joint_done = true; - } else if (NewtonOn("xyz")) { - const std::vector h = NewtonXyzCurvature(ctx, refinable, ctx.rows, w, xyz_scale); - const Context::XrayCoupling c = NewtonCoupling() ? ctx.XrayXyzCoupling(ctx.rows, refinable) - : Context::XrayCoupling(); - MinimiseAtomsNewton(ctx, xyz, xyz_newton, h, xyz_set, Newton().xyz_iterations, - NewtonCoupling() ? &c : nullptr, w * xyz_scale); - } - else - Minimise(ctx, xyz, settings.xyz_iterations, xyz_minimiser, {}, "xyz"); + } else + MinimiseAtoms(ctx, xyz, xyz_minimiser, XyzCurvature(ctx, refinable, ctx.rows, w, xyz_scale), set, + settings.xyz_iterations); } + // the X-ray curvature in B for the atoms as they are, which TLS, U and B take a share of + auto b_curvature = [&]() { + std::vector c = ctx.XrayBCurvatureShape(); + if (b_scale < 0) + b_scale = ctx.CalibrateBCurvature(refinable, c); + for (double &x : c) + x *= b_scale; + return c; + }; if (kind == AdpHypothesis::Kind::Tls) { ctx.ApplyTls(); // the bodies' U at the atoms as they are const RefinementStage s4("minimise.tls"); - if (NewtonOn("tls")) { - std::vector xray = ctx.XrayBCurvatureShape(); - if (tls_scale < 0) - tls_scale = ctx.CalibrateBCurvature(refinable, xray); - for (double &x : xray) - x *= tls_scale; - MinimiseTlsNewton(ctx, tls_target, tls_newton, xray, Newton().b_iterations); - } else - Minimise(ctx, tls_target, settings.b_iterations, tls_minimiser, {}, "tls"); + MinimiseTls(ctx, tls_target, tls_minimiser, b_curvature(), settings.b_iterations); } if (adp_w > 0 && kind == AdpHypothesis::Kind::Anisotropic) { - // each U's six elements take a third of the curvature of its B - std::vector xray = ctx.XrayBCurvatureShape(); - if (cycle == 0) - xray_scale = ctx.CalibrateBCurvature(refinable, xray); - // and the restraints' curvature in 8 pi^2 U: per pair 2 / sigma^2 of the similarity and about a third of - // the rigid bond's, per isotropic atom 2 / sigma^2 - std::vector restraint(xray.size(), 0.0); + const std::vector xray_b = b_curvature(); + // each U's six elements take a third of the curvature of its B; and the restraints' curvature in 8 pi^2 U: + // per pair 2 / sigma^2 of the similarity and about a third of the rigid bond's, per isotropic atom + // 2 / sigma^2 - the preconditioner's diagonal + std::vector restraint(xray_b.size(), 0.0); const double u2 = gemmi::u_to_b() * gemmi::u_to_b(); const double per_pair = (2.0 / (URestraints::SIGMA_SIMILAR * URestraints::SIGMA_SIMILAR) + 2.0 / (3.0 * URestraints::SIGMA_RIGID * URestraints::SIGMA_RIGID)) / u2; @@ -2691,35 +2252,21 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector &refinable, const s } for (int i : u_restraints.isotropic) restraint[i] += 2.0 / (URestraints::SIGMA_ISOTROPIC * URestraints::SIGMA_ISOTROPIC) / u2; - for (double &x : xray) - x *= xray_scale / 3.0; - std::vector diagonal; - for (double h : BDiagonal(xray, restraint, u_atoms, adp_w)) - diagonal.insert(diagonal.end(), 6, h); + std::vector h, diagonal; + for (int i : u_atoms) { + h.insert(h.end(), 6, adp_w * xray_b[i] / 3.0); + diagonal.insert(diagonal.end(), 6, adp_w * xray_b[i] / 3.0 + restraint[i]); + } const RefinementStage s3("minimise.u"); - if (NewtonOn("u")) { - std::vector h; - for (int i : u_atoms) - h.insert(h.end(), 6, adp_w * xray[i]); - MinimiseUNewton(ctx, u_target, u_newton, h, diagonal, Newton().b_iterations); - } else - Minimise(ctx, u_target, settings.b_iterations, b_minimiser, diagonal, "u"); - } else if (adp_w > 0 && !joint_done) { + MinimiseU(ctx, u_target, b_minimiser, h, FlooredDiagonal(diagonal), settings.b_iterations); + } else if (adp_w > 0) { const RestraintSet b_set = BRestraintsNow(ctx, topo); ctx.engine->SetRestraints(1, b_set); - std::vector xray = ctx.XrayBCurvatureShape(); - if (cycle == 0) - xray_scale = ctx.CalibrateBCurvature(refinable, xray); - for (double &x : xray) - x *= xray_scale; + std::vector xray_b = b_curvature(); + for (double &x : xray_b) + x *= adp_w; const RefinementStage s3("minimise.b"); - if (NewtonOn("b")) { - for (double &x : xray) - x *= adp_w; - MinimiseAtomsNewton(ctx, b, b_newton, xray, b_set, Newton().b_iterations); - } else - Minimise(ctx, b, settings.b_iterations, b_minimiser, - BDiagonal(xray, BRestraintCurvature(b_set, ctx.XyzB()), refinable, adp_w), "b"); + MinimiseAtoms(ctx, b, b_minimiser, xray_b, b_set, settings.b_iterations); } } PlaceRiders(ctx, topo); @@ -2764,8 +2311,6 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector &refinable, const s out.fit = ctx.Fitted(); out.ran = true; out.evaluations = ctx.evaluations - evaluations_before; - if (RefineStats()) fprintf(stderr, "TRIALSTAT kind=%d adp_w=%.3g w=%.3g cycles=%d ev=%d rfree=%.4f\n", static_cast(kind), adp_w, w, cycles, - out.evaluations, t.r_free); return out; } @@ -2831,11 +2376,6 @@ std::vector RunTrials(const std::vector &trial_slots, for (const std::exception_ptr &e : errors) if (e) std::rethrow_exception(e); - int ev = 0; - for (const TrialOutcome &o : out) - ev += o.evaluations; - if (RefineStats()) fprintf(stderr, "TRIALSSET t=%.3f n=%zu cycles=%d ev=%d\n", - std::chrono::duration(std::chrono::system_clock::now().time_since_epoch()).count(), out.size(), cycles, ev); return out; } @@ -3862,10 +3402,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector(std::max(settings.population, 0)) - : std::min(settings.population, trial_slots.size()); + const size_t population = std::min(std::max(settings.population, 0), trial_slots.size()); if (population > 1 && state.ran && !ctx.Cancelled()) { const RefinementStage pop_stage("population"); const double w = state.trial.weight, adp = state.trial.adp_weight, ncs_s = state.trial.ncs_sigma; @@ -5027,30 +4564,11 @@ RefinementResult RefineModel(const gemmi::Structure &placed, const std::vector &data = moved > 0 ? closed : merged; - // RefinementSettings::lean_on_cpu #ifdef JFJOCH_USE_CUDA const bool on_cpu = !settings.gpu || get_gpu_count() == 0; #else const bool on_cpu = true; #endif - if (settings.lean_on_cpu && on_cpu) { - settings.population = 0; - settings.rounds = 1; - logger.Info("Refinement on the CPU: no population and one round (half of the evaluations)"); - } - // PROTOTYPE (lane k): RUGNUX_REFINE_PROTOCOL=,[,] on the CPU and the GPU alike - if (const char *e = getenv("RUGNUX_REFINE_PROTOCOL")) { - int v[5] = {settings.population, settings.rounds, settings.macro_cycles, settings.xyz_iterations, - settings.b_iterations}; - sscanf(e, "%d,%d,%d,%d,%d", &v[0], &v[1], &v[2], &v[3], &v[4]); - settings.population = v[0]; - settings.rounds = v[1]; - settings.macro_cycles = v[2]; - settings.xyz_iterations = v[3]; - settings.b_iterations = v[4]; - logger.Info("Refinement EXPERIMENT protocol: population {}, {} rounds, {} macro-cycles", settings.population, - settings.rounds, settings.macro_cycles); - } // Each arm, untwinned and twinned, is a whole refinement - the solvent built and the displacement model chosen // against its own target - and the decision is taken on where they ended. A model only scored is scored both // ways and decided by the same rule, so the R-free it reports is not an untwinned one by default. diff --git a/image_analysis/structure_refinement/Refinement.h b/image_analysis/structure_refinement/Refinement.h index 484001fd5..65f3f158e 100644 --- a/image_analysis/structure_refinement/Refinement.h +++ b/image_analysis/structure_refinement/Refinement.h @@ -48,8 +48,10 @@ struct RefinementSettings { bool score_only = false; // only score the model as given (RefinementResult::score_start), no stage bool group_b = true; // the placement stage's B shift per residue int macro_cycles = 2; // per weight trial, from the model the trial starts from - int xyz_iterations = 30; // L-BFGS iterations per macro-cycle - int b_iterations = 20; + // L-BFGS iterations per macro-cycle (on the Gauss-Newton normal matrix: RefinementMinimiser), of the positions and of + // the B factors, U and TLS + int xyz_iterations = 10; + int b_iterations = 7; // The first trials, from the placed model: the positions' X-ray weight, times the gradient-balanced one // (BalancedWeight), each with the B factors' X-ray weight their own balanced one times each of adp_ladder (0: B // held). @@ -84,8 +86,9 @@ struct RefinementSettings { // The population (RefineOnce, 2a.): this many members at most, refined from where the weight trajectory ended in // one wave, each on the data blurred along a schedule of its own and its positions kicked or not, then crossed // residue by residue on their averaged map and refined once more; the child kept where the free set prefers it. - // Capped at the trial slots; fewer than two: no population. - int population = 12; + // Capped at the trial slots; fewer than two: no population (the default: with the positions minimised on the + // Gauss-Newton matrix, the members took nothing off R-free over 19 sets that the two rounds did not). + int population = 0; // A refinement against the data as twinned by each twin law the lattice admits (RefinementTwinLaws), beside the // untwinned one, and the model of whichever the evidence favours (RefineModel). bool twin = true; @@ -97,11 +100,6 @@ struct RefinementSettings { // model of this crystal scored on a free set drawn here). bool as_given_competes = true; bool gpu = true; - // Where the refinement runs on the CPU (no GPU wanted, or none to run on), where an evaluation costs some thirty - // times a GPU's: no population and one round, for about half of the evaluations, at an R-free 0.001 higher in the - // median of 19 sets (at most 0.015, a placed prediction the population helps). The twinned data are still refined - // against: without that arm a crystal 30% twinned is refined as untwinned. - bool lean_on_cpu = true; size_t nthreads = 1; size_t gpu_resident_bytes = 0; // held on the card by what runs beside // The most weight trials run side by side, 0 for as many as the ladders and the cards take; the result does not diff --git a/image_analysis/structure_refinement/RefinementMinimiser.h b/image_analysis/structure_refinement/RefinementMinimiser.h index eeb4e85be..6353fff8d 100644 --- a/image_analysis/structure_refinement/RefinementMinimiser.h +++ b/image_analysis/structure_refinement/RefinementMinimiser.h @@ -4,41 +4,34 @@ #pragma once #include -#include #include #include #include #include -#include "../../common/ParallelFor.h" - -// Limited-memory BFGS whose memory outlives one call, so that a refinement's macro-cycles - each a few dozen -// iterations on a target whose scale and likelihood parameters were refitted in between - go on from what the -// earlier ones learnt of the curvature instead of starting each from a steepest-descent step. The initial inverse -// Hessian is a diagonal the caller gives (SetDiagonal: an estimate of the Hessian's diagonal, so that a parameter -// with ten times the curvature of another takes a tenth of its step), scaled each iteration by the latest pair -// (Oren-Shanno in that metric). The line search backtracks from the full step to sufficient decrease. +// Limited-memory BFGS whose initial inverse Hessian is the caller's (SetInverseHessian: H0 q, here the Gauss-Newton +// normal matrix solved by ConjugateGradients), and whose memory outlives one call, so that a refinement's +// macro-cycles - each a few iterations on a target whose scale and likelihood parameters were refitted in between - +// go on from what the earlier ones learnt. The normal matrix holds what is known of the curvature in advance (the +// restraints exactly, the X-ray target per atom); the pairs add what it leaves out (the X-ray target's coupling of +// neighbouring atoms). The line search backtracks from the full step to sufficient decrease. // Following Nocedal (1980) Math. Comp. 35, 773-782 and Liu & Nocedal (1989) Math. Program. 45, 503-528 class RefinementMinimiser { public: // f(x, g): the target at x, and its gradient in g (sized as x); false where it cannot be evaluated there. using Target = std::function &x, double &f, std::vector &g)>; + // q = H0 q at x. + using InverseHessian = std::function &x, std::vector &q)>; - // `nthreads`: the threads the sweeps over the parameters run on (the dot products stay serial chains). - explicit RefinementMinimiser(size_t memory = 20, size_t nthreads = 1) : memory_(memory), nthreads_(nthreads) {} + explicit RefinementMinimiser(size_t memory = 20) : memory_(memory) {} - // Forget the pairs (the target changed beyond what the memory still describes, e.g. another weight). + // Forget the pairs (the target changed beyond what the memory still describes). void Reset() { s_.clear(); y_.clear(); sy_.clear(); } - // The estimated diagonal of the Hessian; every element > 0. - void SetDiagonal(std::vector h) { h_ = std::move(h); } - // The initial inverse Hessian as an operator instead of the diagonal: q = H0 q at x (null: the diagonal). It is - // taken as it is, without the Oren-Shanno factor. - using Preconditioner = std::function &x, std::vector &q)>; - void SetPreconditioner(Preconditioner p) { precondition_ = std::move(p); } + void SetInverseHessian(InverseHessian h0) { h0_ = std::move(h0); } struct Outcome { int iterations = 0; @@ -52,8 +45,6 @@ public: Outcome Minimise(const Target &target, std::vector &x, int iterations, double f_tolerance, double max_step, const std::function &stop = nullptr) { const size_t n = x.size(); - if (h_.size() != n) - h_.assign(n, 1.0); Outcome out; double f = 0; std::vector g(n), d(n), x_new(n), g_new(n); @@ -66,7 +57,7 @@ public: break; Direction(x, g, d); double gd = Dot(g, d); - if (!(gd < 0)) { // not a descent direction: the memory no longer fits, start again from the diagonal + if (!(gd < 0)) { // not a descent direction: the memory no longer fits, start again from H0 Reset(); Direction(x, g, d); gd = Dot(g, d); @@ -80,7 +71,8 @@ public: double f_new = 0; bool accepted = false; for (int ls = 0; ls < 10; ls++) { - Sweep(n, [&](size_t k) { x_new[k] = x[k] + alpha * d[k]; }); + for (size_t k = 0; k < n; k++) + x_new[k] = x[k] + alpha * d[k]; const bool ok = target(x_new, f_new, g_new); out.evaluations++; if (ok && f_new <= f + 1e-4 * alpha * gd) { @@ -93,29 +85,21 @@ public: } if (!accepted) break; - // the pair's vectors are the ones the memory last let go, where it did: made anew, their pages fault in - std::vector s = std::move(spare_s_), y = std::move(spare_y_); - s.resize(n); - y.resize(n); - Sweep(n, [&](size_t k) { + std::vector s(n), y(n); + for (size_t k = 0; k < n; k++) { s[k] = x_new[k] - x[k]; y[k] = g_new[k] - g[k]; - }); + } const double sy = Dot(s, y); if (sy > 1e-10 * std::sqrt(Dot(s, s) * Dot(y, y))) { s_.push_back(std::move(s)); y_.push_back(std::move(y)); sy_.push_back(sy); if (s_.size() > memory_) { - spare_s_ = std::move(s_.front()); - spare_y_ = std::move(y_.front()); s_.pop_front(); y_.pop_front(); sy_.pop_front(); } - } else { - spare_s_ = std::move(s); - spare_y_ = std::move(y); } const double decrease = f - f_new; x.swap(x_new); @@ -137,82 +121,8 @@ private: return s; } - // fn(k) for every k below n on the threads, each element on its own: the serial loop's bits. - template - void Sweep(size_t n, Fn fn) const { - ParallelChunks(static_cast(n), ThreadsForWork(n, nthreads_), [&](int lo, int hi) { - for (int k = lo; k < hi; k++) - fn(static_cast(k)); - }); - } - - // d = -H g by the two-loop recursion, H0 = gamma diag(1/h). Every dot product is a serial chain of n - // additions, so each sweep over the parameters takes the next pair's dot product while it applies the - // previous pair's update to q - the same operations on each element in the same order as a sweep for - // each would do, with the update's memory traffic hidden under the chain. + // d = -H g by the two-loop recursion, H0 the caller's. void Direction(const std::vector &x, const std::vector &g, std::vector &d) const { - if (precondition_) { - DirectionPreconditioned(x, g, d); - return; - } - const size_t n = g.size(), m = s_.size(); - std::vector &q = q_; // kept from call to call: made anew, its pages fault in every time - q.assign(g.begin(), g.end()); - std::vector a(m), rho(m); - for (size_t i = 0; i < m; i++) - rho[i] = 1.0 / sy_[i]; - double dot = m > 0 ? Dot(s_[m - 1], q) : 0.0, yhy = 0.0; - for (size_t i = m; i-- > 0;) { - a[i] = rho[i] * dot; - const double ai = a[i]; - const double *y = y_[i].data(); - dot = 0.0; - if (i > 0) { - const double *s = s_[i - 1].data(); - for (size_t k = 0; k < n; k++) { - q[k] -= ai * y[k]; - dot += s[k] * q[k]; - } - } else { - const double *yl = y_[m - 1].data(); - for (size_t k = 0; k < n; k++) { - q[k] -= ai * y[k]; - yhy += yl[k] * yl[k] / h_[k]; - } - } - } - const double gamma = m > 0 ? sy_[m - 1] / yhy : 1.0; - if (m > 0) { - const double *y = y_[0].data(); - for (size_t k = 0; k < n; k++) { - q[k] *= gamma / h_[k]; - dot += y[k] * q[k]; - } - } else - for (size_t k = 0; k < n; k++) - q[k] *= gamma / h_[k]; - for (size_t i = 0; i < m; i++) { - // b on its own and (a[i] - b) in the loop, as before: written as a[i] - rho[i] * dot the compiler - // contracts it into a fused multiply-add, and the bits change - const double b = rho[i] * dot; - const double *s = s_[i].data(); - dot = 0.0; - if (i + 1 < m) { - const double *y = y_[i + 1].data(); - for (size_t k = 0; k < n; k++) { - q[k] += s[k] * (a[i] - b); - dot += y[k] * q[k]; - } - } else - for (size_t k = 0; k < n; k++) - q[k] += s[k] * (a[i] - b); - } - for (size_t k = 0; k < n; k++) - d[k] = -q[k]; - } - - // The two-loop recursion with H0 the preconditioner. - void DirectionPreconditioned(const std::vector &x, const std::vector &g, std::vector &d) const { const size_t m = s_.size(), n = g.size(); std::vector q = g, a(m); for (size_t i = m; i-- > 0;) { @@ -220,7 +130,7 @@ private: for (size_t k = 0; k < n; k++) q[k] -= a[i] * y_[i][k]; } - precondition_(x, q); + h0_(x, q); for (size_t i = 0; i < m; i++) { const double b = Dot(y_[i], q) / sy_[i]; for (size_t k = 0; k < n; k++) @@ -231,159 +141,50 @@ private: d[k] = -q[k]; } - Preconditioner precondition_; - size_t memory_, nthreads_; + size_t memory_; + InverseHessian h0_; std::deque> s_, y_; std::deque sy_; // s.y of each pair, as taken when it was kept - std::vector spare_s_, spare_y_; // the pair the memory last let go, for the next one to reuse - mutable std::vector q_; // Direction()'s working vector - std::vector h_; }; -// Damped Newton steps (Levenberg-Marquardt) on a positive semi-definite approximation of the Hessian: each iteration -// solves (H + lambda D) d = -g by conjugate gradients preconditioned with (1 + lambda) D - H v the caller's product at -// the point of the last accepted evaluation, D H's diagonal - and takes d where it lowers f. lambda falls after a -// step the quadratic model predicted well and rises after one it did not; a step that does not lower f is solved again -// with the larger lambda, without a new gradient. An iteration is one evaluation of the target where its step is -// taken. A solve stops where an iteration lowers f by less than `relative_stop` of what the call has lowered it by. -// Following Levenberg (1944) Q. Appl. Math. 2, 164-168 and Marquardt (1963) J. SIAM 11, 431-441 -class RefinementNewton { -public: - using Target = RefinementMinimiser::Target; - // hv = H v at x, the point of the last accepted evaluation. - using Product = std::function &x, const std::vector &v, std::vector &hv)>; - using Outcome = RefinementMinimiser::Outcome; - - int cg_iterations = 0; // summed over the calls, for the log - RefinementMinimiser *lbfgs = nullptr; // PROTOTYPE (RUGNUX_NEWTON=2): the L-BFGS the GN matrix preconditions - int cg_max = 50; - double cg_tolerance = 0.1; - double relative_stop = 0.0; - bool trace = false; - - // z = M^-1 r for M an approximation of H + lambda D that is cheap to solve (null: (1 + lambda) D). - using Preconditioner = std::function &r, double lambda, std::vector &z)>; - - Outcome Minimise(const Target &target, const Product &product, const std::vector &diagonal, - std::vector &x, int iterations, double f_tolerance, double max_step, - const std::function &stop = nullptr, const Preconditioner &precondition = nullptr) { - const size_t n = x.size(); - Outcome out; - double f = 0; - std::vector g(n), d(n), x_new(n), g_new(n), hd(n); - if (!target(x, f, g)) - return out; - out.evaluations++; - out.f_start = out.f_end = f; - for (int it = 0; it < iterations; it++) { - if (stop && stop()) - break; - bool accepted = false; - double f_new = 0; - for (int attempt = 0; attempt < 6 && !accepted; attempt++) { - Solve(product, diagonal, precondition, x, g, d); - double d_max = 0; - for (double v : d) - d_max = std::max(d_max, std::fabs(v)); - if (d_max > max_step) - for (double &v : d) - v *= max_step / d_max; - product(x, d, hd); - const double gd = Dot(g, d), predicted = -(gd + 0.5 * Dot(d, hd)); - if (!(gd < 0)) - break; - for (size_t k = 0; k < n; k++) - x_new[k] = x[k] + d[k]; - const bool ok = target(x_new, f_new, g_new); - out.evaluations++; - const double rho = ok ? (f - f_new) / predicted : -1.0; - if (trace) - fprintf(stderr, "NTRACE it=%d att=%d dmax=%.3g lambda=%.3g pred=%.4g act=%.4g rho=%.3f\n", it, attempt, - d_max, lambda_, predicted, f - f_new, rho); - if (ok && f_new <= f + 1e-4 * gd) { - accepted = true; - if (rho > 0.75) - lambda_ = std::max(lambda_ / 3.0, 1e-4); - else if (rho < 0.25) - lambda_ *= 2.0; - } else - lambda_ = std::max(4.0 * lambda_, 0.1); - } - if (!accepted) - break; - const double decrease = f - f_new; - x.swap(x_new); - g.swap(g_new); - f = f_new; - out.iterations++; - out.f_end = f; - if (decrease <= f_tolerance * std::max(std::fabs(f), 1.0) || decrease < relative_stop * (out.f_start - f)) - break; - } - return out; - } - - // z = H^-1 q (lambda 0), by the same conjugate gradients: H0 for the L-BFGS (RefinementMinimiser::SetPreconditioner). - void SolveUndamped(const Product &product, const std::vector &diagonal, const Preconditioner &precondition, - const std::vector &x, const std::vector &q, std::vector &z) { - const double l = lambda_; - lambda_ = 0; - std::vector minus(q.size()); - for (size_t k = 0; k < q.size(); k++) - minus[k] = -q[k]; - Solve(product, diagonal, precondition, x, minus, z); - lambda_ = l; - } - -private: - static double Dot(const std::vector &a, const std::vector &b) { +// z with H z = b to `tolerance` of |b| (at most `max_iterations`), by conjugate gradients preconditioned with M: +// hv = H v (product), z = M^-1 r (precondition), both symmetric positive definite. Returns the iterations taken. +// Following Hestenes & Stiefel (1952) J. Res. Natl. Bur. Stand. 49, 409-436 +inline int ConjugateGradients(const std::function &v, std::vector &hv)> &product, + const std::function &r, std::vector &z)> &precondition, + const std::vector &b, std::vector &x, int max_iterations, double tolerance) { + auto dot = [](const std::vector &u, const std::vector &v) { double s = 0; - for (size_t k = 0; k < a.size(); k++) - s += a[k] * b[k]; + for (size_t k = 0; k < u.size(); k++) + s += u[k] * v[k]; return s; - } - - // d with (H + lambda D) d = -g to `cg_tolerance` of |g|, by preconditioned conjugate gradients. - void Solve(const Product &product, const std::vector &diagonal, const Preconditioner &precondition, - const std::vector &x, const std::vector &g, std::vector &d) { - const size_t n = g.size(); - d.assign(n, 0.0); - std::vector r(n), z(n), p(n), hp(n); - auto apply = [&]() { - if (precondition) - precondition(r, lambda_, z); - else - for (size_t j = 0; j < n; j++) - z[j] = r[j] / ((1 + lambda_) * diagonal[j]); - }; - for (size_t k = 0; k < n; k++) - r[k] = -g[k]; - apply(); - p = z; - double rz = Dot(r, z); - const double r0 = std::sqrt(Dot(r, r)); - for (int k = 0; k < cg_max; k++) { - cg_iterations++; - product(x, p, hp); - for (size_t j = 0; j < n; j++) - hp[j] += lambda_ * diagonal[j] * p[j]; - const double php = Dot(p, hp); - if (!(php > 0)) - break; - const double a = rz / php; - for (size_t j = 0; j < n; j++) { - d[j] += a * p[j]; - r[j] -= a * hp[j]; - } - if (std::sqrt(Dot(r, r)) < cg_tolerance * r0) - break; - apply(); - const double rz_new = Dot(r, z); - for (size_t j = 0; j < n; j++) - p[j] = z[j] + rz_new / rz * p[j]; - rz = rz_new; + }; + const size_t n = b.size(); + x.assign(n, 0.0); + std::vector r = b, z(n), p, hp(n); + precondition(r, z); + p = z; + double rz = dot(r, z); + const double r0 = std::sqrt(dot(r, r)); + int k = 0; + while (k < max_iterations) { + k++; + product(p, hp); + const double php = dot(p, hp); + if (!(php > 0)) + break; + const double a = rz / php; + for (size_t j = 0; j < n; j++) { + x[j] += a * p[j]; + r[j] -= a * hp[j]; } + if (std::sqrt(dot(r, r)) < tolerance * r0) + break; + precondition(r, z); + const double rz_new = dot(r, z); + for (size_t j = 0; j < n; j++) + p[j] = z[j] + rz_new / rz * p[j]; + rz = rz_new; } - - double lambda_ = 0.01; // kept from call to call, as the L-BFGS memory is -}; + return k; +} diff --git a/image_analysis/structure_refinement/RefinementRestraints.cpp b/image_analysis/structure_refinement/RefinementRestraints.cpp index 7d0813183..73cc05952 100644 --- a/image_analysis/structure_refinement/RefinementRestraints.cpp +++ b/image_analysis/structure_refinement/RefinementRestraints.cpp @@ -551,18 +551,6 @@ RestraintJacobian RestraintGaussNewton(const RestraintSet &set, const std::vecto return jac; } -std::vector BRestraintCurvature(const RestraintSet &set, const std::vector> &xyzb) { - std::vector h(xyzb.size(), 0.0); - for (const RestraintRecord &r : set.records) - if (r.kind == static_cast(RestraintKind::BSimilarity)) { - // d2E/dB_i^2 of BSimilarityTerm = 2 B_j / (kappa^2 B_i^3) - const double bi = xyzb[r.atom[0]][3], bj = xyzb[r.atom[1]][3], k2 = double(r.sigma) * r.sigma; - h[r.atom[0]] += 2.0 * bj / (k2 * bi * bi * bi); - h[r.atom[1]] += 2.0 * bi / (k2 * bj * bj * bj); - } - return h; -} - gemmi::Position SpecialPosition::OnSite(const gemmi::Position &p) const { gemmi::Vec3 sum = p; for (const gemmi::Transform &t : ops) diff --git a/image_analysis/structure_refinement/RefinementRestraints.h b/image_analysis/structure_refinement/RefinementRestraints.h index 23e8ff026..6fce88e58 100644 --- a/image_analysis/structure_refinement/RefinementRestraints.h +++ b/image_analysis/structure_refinement/RefinementRestraints.h @@ -49,7 +49,7 @@ double RestraintValue(const RestraintSet &set, const RestraintRecord &r, const s double EvaluateRestraints(const RestraintSet &set, const std::vector> &xyzb, std::vector> *grad); -// PROTOTYPE: the Gauss-Newton approximation of the set's Hessian, sum over rows of J J^T: per row (a restraint's +// The Gauss-Newton approximation of the set's Hessian, sum over rows of J J^T: per row (a restraint's // residual) its derivatives in (x, y, z, B) of the atoms atom[start[r] ... start[r + 1]). struct RestraintJacobian { std::vector start; @@ -58,9 +58,6 @@ struct RestraintJacobian { }; RestraintJacobian RestraintGaussNewton(const RestraintSet &set, const std::vector> &xyzb); -// The diagonal of the Hessian of the set's B similarity restraints with respect to each atom's B, at `xyzb`. -std::vector BRestraintCurvature(const RestraintSet &set, const std::vector> &xyzb); - // An atom on a crystallographic special position: one that some symmetry operations of the group, each with a // lattice translation, take onto itself - the site's symmetry. Its position moves only within the subspace those // operations leave as it is (a 2-fold axis: along the axis; a 4-fold's: along it; a centre of inversion: not at all), diff --git a/tests/RefinementTest.cpp b/tests/RefinementTest.cpp index e34653877..b681cd69b 100644 --- a/tests/RefinementTest.cpp +++ b/tests/RefinementTest.cpp @@ -788,6 +788,55 @@ TEST_CASE("RefinementRestraints_deposited_ligands", "[Refinement]") { } } +TEST_CASE("RefinementRestraints_gauss_newton", "[Refinement]") { + // near the ideal geometry the restraints' Hessian is their Gauss-Newton J^T J (the residuals' own curvature times + // residuals that are small): v.J^T J v against central differences of the gradient along v, for random v + const gemmi::UnitCell cell(14, 15, 16, 90, 90, 90); + gemmi::Structure st = residue_crystal({"TYR", "DG"}, cell, "P 21 21 21", 5); + st.models[0] = shaken(st.models[0], 0.005, 0.0, 2); + const RefinementTopology topo = BuildRefinementTopology(st); + RestraintSet set = topo.geometry; + set.Finalize(atoms_of(st.models[0]).size()); + std::vector> xyzb; + for (gemmi::Atom *a : atoms_of(st.models[0])) + xyzb.push_back({a->pos.x, a->pos.y, a->pos.z, a->b_iso}); + const RestraintJacobian jac = RestraintGaussNewton(set, xyzb); + REQUIRE(jac.start.size() > 10); + std::mt19937 rng(4); + std::normal_distribution normal(0.0, 1.0); + for (int trial = 0; trial < 5; trial++) { + std::vector> v(xyzb.size(), {0, 0, 0, 0}); + for (auto &e : v) + for (int c = 0; c < 3; c++) + e[c] = normal(rng); + double gn = 0; + for (size_t r = 0; r + 1 < jac.start.size(); r++) { + double jv = 0; + for (int e = jac.start[r]; e < jac.start[r + 1]; e++) + for (int c = 0; c < 3; c++) + jv += jac.j[e][c] * v[jac.atom[e]][c]; + gn += jv * jv; + } + const double h = 1e-5; + std::array>, 2> g; + for (int side = 0; side < 2; side++) { + auto x = xyzb; + for (size_t i = 0; i < x.size(); i++) + for (int c = 0; c < 3; c++) + x[i][c] += (side == 0 ? h : -h) * v[i][c]; + g[side].assign(x.size(), {0, 0, 0, 0}); + EvaluateRestraints(set, x, &g[side]); + } + double exact = 0; + for (size_t i = 0; i < xyzb.size(); i++) + for (int c = 0; c < 3; c++) + exact += v[i][c] * (g[0][i][c] - g[1][i][c]) / (2 * h); + CAPTURE(trial, gn, exact); + CHECK(gn > 0); + CHECK(gn == Catch::Approx(exact).epsilon(0.1)); + } +} + TEST_CASE("RefinementRestraints_gradient", "[Refinement]") { const gemmi::UnitCell cell(14, 15, 16, 90, 90, 90); gemmi::Structure st = residue_crystal({"TYR", "DG"}, cell, "P 21 21 21", 5); @@ -1161,18 +1210,21 @@ TEST_CASE("RefinementMinimiser_quadratic", "[Refinement]") { return d; }; - // the exact diagonal: one step + // the exact inverse Hessian: one step RefinementMinimiser exact; - std::vector h(n); - for (size_t k = 0; k < n; k++) - h[k] = 2 * a[k]; - exact.SetDiagonal(h); + exact.SetInverseHessian([&](const std::vector &, std::vector &q) { + for (size_t k = 0; k < n; k++) + q[k] /= 2 * a[k]; + }); std::vector x(n, 0.0); exact.Minimise(target, x, 1, 0.0, 10.0); CHECK(distance(x) < 1e-9); - // without it, the memory carried from one call to the next does what one long call does + // with the identity, the memory carried from one call to the next does what one long call does RefinementMinimiser split, whole; + const RefinementMinimiser::InverseHessian identity = [](const std::vector &, std::vector &) {}; + split.SetInverseHessian(identity); + whole.SetInverseHessian(identity); std::vector xs(n, 0.0), xw(n, 0.0); for (int call = 0; call < 6; call++) split.Minimise(target, xs, 10, 0.0, 10.0); @@ -1182,6 +1234,44 @@ TEST_CASE("RefinementMinimiser_quadratic", "[Refinement]") { CHECK(xs[k] == Catch::Approx(xw[k]).margin(1e-12)); } +TEST_CASE("RefinementMinimiser_conjugate_gradients", "[Refinement]") { + // a tridiagonal positive definite system, solved to the tolerance with and without a (Jacobi) preconditioner + const size_t n = 200; + auto product = [&](const std::vector &v, std::vector &hv) { + hv.assign(n, 0.0); + for (size_t k = 0; k < n; k++) { + hv[k] = (2.0 + 0.01 * k) * v[k]; + if (k > 0) + hv[k] -= v[k - 1]; + if (k + 1 < n) + hv[k] -= v[k + 1]; + } + }; + std::vector b(n); + for (size_t k = 0; k < n; k++) + b[k] = std::sin(0.1 * k); + auto residual = [&](const std::vector &x) { + std::vector hx; + product(x, hx); + double r = 0, b2 = 0; + for (size_t k = 0; k < n; k++) { + r += (hx[k] - b[k]) * (hx[k] - b[k]); + b2 += b[k] * b[k]; + } + return std::sqrt(r / b2); + }; + std::vector x; + ConjugateGradients(product, [](const std::vector &r, std::vector &z) { z = r; }, b, x, 500, 1e-8); + CHECK(residual(x) < 1e-8); + const int jacobi = ConjugateGradients(product, [&](const std::vector &r, std::vector &z) { + z.resize(n); + for (size_t k = 0; k < n; k++) + z[k] = r[k] / (2.0 + 0.01 * k); + }, b, x, 500, 0.1); + CHECK(residual(x) < 0.1); + CHECK(jacobi < 50); +} + TEST_CASE("Refinement_restrained_recovers_model", "[Refinement]") { const gemmi::UnitCell cell(36, 40, 44, 90, 90, 90); const gemmi::SpaceGroup &sg = *gemmi::find_spacegroup_by_name("P 21 21 21");