Files
Jungfraujoch/image_analysis/scale_merge/AndersonMixing.cpp
T
leonarski_f afbe844b20
Build Packages / Create release (push) Successful in 17s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 4m41s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 20m49s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 13m6s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 23m44s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 7m28s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 11m50s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 14m9s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 16m25s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 15m42s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 20m50s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 20m36s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 22m18s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 23m29s
Build Packages / Generate python client (push) Successful in 30s
Build Packages / Build documentation (push) Successful in 1m10s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 21m38s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 17m8s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 23m10s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 19m16s
Build Packages / Unit tests (push) Successful in 2h8m4s
Build Packages / Validation Battery (push) Successful in 40m53s
v1.0.0-rc.178 (#88)
* Rugnux: Processes serial crystallography data from XFELs - SACLA (MPCCD), PAL-XFEL and European XFEL (CXI), LCLS (XTC) and SwissFEL Alvra (JUNGFRAU with a CrystFEL module geometry) - with several files or Jungfraujoch master files merged in one run (`rugnux *_master.h5`).
* Rugnux: Stills processing takes a known unit cell and space group as a starting point, finds the beam centre (and, with a cell, the detector distance) from the images, detects the saturation level in other facilities' files and refines the placement of individual detector panels.
* Rugnux: The stills report gives R_split and CC1/2 from frame half-sets, comparable with CrystFEL.
* jfjoch_viewer: Opens the same XFEL file formats.
* Validation: A stills battery checks Rugnux against deposited structures from SACLA, PAL-XFEL, LCLS, European XFEL and SwissFEL.
* Rugnux: More reliable convergence of rotation scaling, and live molecular replacement starts as soon as the model is uploaded.
* jfjoch_broker: Initialize clears the previous dataset's bearer tokens, image buffer and live Rugnux status, and a slow CUDA JIT cache is reported at start.

Reviewed-on: #88
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-10-21 11:20:01 +02:00

52 lines
2.0 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "AndersonMixing.h"
std::vector<double> AndersonMixing::Next(const std::vector<double> &x, const std::vector<double> &fx,
const std::vector<double> &w) {
// Anderson acceleration, type II, after Walker & Ni (2011) SIAM J. Numer. Anal. 49, 1715-1735
// (Anderson (1965) J. ACM 12, 547-560): with dX and dR the differences of the states and of the
// residuals of consecutive rounds, gamma = argmin |r - dR gamma|_w and the next state is
// F(x) - (dX + dR) gamma.
const int n = static_cast<int>(x.size());
const Eigen::Map<const Eigen::VectorXd> xv(x.data(), n), fxv(fx.data(), n), wv(w.data(), n);
const Eigen::VectorXd r = fxv - xv;
std::vector<uint8_t> support(n);
for (int i = 0; i < n; ++i) support[i] = w[i] > 0.0 ? 1 : 0;
const double r_norm = (wv.array() * r.array().square()).sum();
if (stopped || (x_last.size() == n && r_norm > r_norm_last)) {
stopped = true;
return fx;
}
if (x_last.size() == n && support == support_last) {
dx.push_back(xv - x_last);
dr.push_back(r - r_last);
if (static_cast<int>(dx.size()) > depth) {
dx.pop_front();
dr.pop_front();
}
} else if (!dx.empty()) {
dx.clear();
dr.clear();
++restarts;
}
x_last = xv;
r_last = r;
support_last = std::move(support);
r_norm_last = r_norm;
std::vector<double> out = fx;
const int m = static_cast<int>(dr.size());
if (m == 0) return out;
const Eigen::VectorXd sqrt_w = wv.cwiseMax(0.0).cwiseSqrt();
Eigen::MatrixXd M(n, m);
for (int j = 0; j < m; ++j) M.col(j) = sqrt_w.cwiseProduct(dr[j]);
const Eigen::VectorXd gamma = M.colPivHouseholderQr().solve(sqrt_w.cwiseProduct(r));
for (int j = 0; j < m; ++j)
for (int i = 0; i < n; ++i)
if (w[i] > 0.0) out[i] -= gamma[j] * (dx[j][i] + dr[j][i]);
return out;
}