Files
leonarski_f aecd48b9d3
Build Packages / Build documentation (push) Successful in 48s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 15m47s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 23m19s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 18m47s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m54s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 11m27s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 26m59s
Build Packages / Unit tests (push) Successful in 1h50m25s
Build Packages / Validation Battery (push) Successful in 46m38s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 32m52s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 32m32s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 28m34s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 29m19s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 30m11s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 33m58s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 31m26s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 34m4s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 32m17s
Build Packages / Generate python client (push) Successful in 32s
Build Packages / Create release (push) Successful in 9s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 31m50s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 19m38s
v1.0.0-rc.186 (#97)
* jfjoch_broker (experimental): Live Rugnux covers serial collections from DECTRIS detectors; a rotation collection too large for the image buffer falls back to standard per-image processing with a warning, and a stream that ends without its end message finishes the collection instead of hanging it.
* jfjoch_broker (experimental): Live Rugnux settings to build the solvent during refinement and to process a rotation collection as stills, a Clear action for the last result, a `_report.txt` for live rotation runs, R-free reported for the refined model, and ice-ring detection on by default for rotation as in the command line.
* jfjoch_broker (experimental): More complete and reproducible live serial merges, analysed for twinning, pseudo-translation and anisotropy, with merging statistics by resolution shell in the live status.
* jfjoch_writer: Live results with large models are written reliably; update jfjoch_writer together with jfjoch_broker.
* Rugnux: Several masters can be processed together with `--force-still`, including grid scans; more robust and faster merging of several sweeps; improved twinning, pseudo-translation, anisotropy, anomalous-signal and space-group decisions.
* Rugnux: Molecular replacement places the copies of a multi-copy solution together; the pseudo-translation warning quotes the Patterson peak and is not raised once a model is placed; the report advises on extending the rotation or reorienting the crystal where the coverage would gain from it.
* Web frontend: Light and dark modes, a phone layout, a hideable page list, a redesigned Rugnux live page with step-through of the Fo-Fc peaks in the structure view.
* jfjoch_viewer: A broker URL given on the command line follows the running collection.

Reviewed-on: #97
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-11-13 20:13:43 +01:00

175 lines
7.3 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "RugnuxStructure.h"
#include <memory>
#include "spdlog/fmt/fmt.h"
#include "../common/JFJochException.h"
#include "../frame_serialize/CBORStream2Serializer.h"
#include "../rugnux/Rugnux.h"
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstring>
#include "gemmi/asudata.hpp"
#include "gemmi/calculate.hpp" // calculate_fractional_box
#include "gemmi/ccp4.hpp"
#include "gemmi/fourier.hpp" // get_size_for_hkl, get_f_phi_on_grid
#include "../image_analysis/structure_refinement/ModelFFT.h"
#include "../writer/WriteModel.h"
namespace {
const MtzTable::Column &FindColumn(const MtzTable &mtz, const std::string &label) {
for (const auto &c: mtz.columns)
if (c.label == label)
return c;
throw std::runtime_error("map coefficients have no column " + label);
}
}
RugnuxStructure::RugnuxStructure(std::shared_ptr<const gemmi::Structure> model, const UnitCell &cell,
const gemmi::SpaceGroup &space_group, std::optional<MtzTable> coefficients)
: model(std::move(model)), cell(cell), space_group(space_group), coefficients(std::move(coefficients)) {}
bool RugnuxStructure::HasMaps() const {
return coefficients.has_value();
}
bool RugnuxStructure::HasAnomalousMap() const {
if (!coefficients)
return false;
for (const auto &c: coefficients->columns)
if (c.label == "ANOM")
return true;
return false;
}
std::string RugnuxStructure::ModelCif() const {
std::unique_lock ul(structure_mutex);
if (!model_cif)
model_cif = PlacedModelMmcif(*model, cell, space_group);
return *model_cif;
}
std::string RugnuxStructure::Map(RugnuxMapType type) const {
if (!coefficients || (type == RugnuxMapType::Anomalous && !HasAnomalousMap()))
return {};
std::unique_lock ul(structure_mutex);
std::optional<std::string> &cached = type == RugnuxMapType::TwoFoFc ? map_2fofc
: type == RugnuxMapType::FoFc ? map_fofc : map_anom;
if (cached)
return *cached;
const MtzTable &mtz = *coefficients;
const auto &h = FindColumn(mtz, "H"), &k = FindColumn(mtz, "K"), &l = FindColumn(mtz, "L");
const char *f_label = type == RugnuxMapType::TwoFoFc ? "FWT" : type == RugnuxMapType::FoFc ? "DELFWT" : "ANOM";
const char *phi_label = type == RugnuxMapType::TwoFoFc ? "PHWT" : type == RugnuxMapType::FoFc ? "PHDELWT" : "PHANOM";
const auto &f = FindColumn(mtz, f_label);
const auto &phi = FindColumn(mtz, phi_label);
const gemmi::SpaceGroup *sg = gemmi::find_spacegroup_by_name(mtz.space_group_name);
if (sg == nullptr)
throw std::runtime_error("unknown space group '" + mtz.space_group_name + "' of the map coefficients");
gemmi::AsuData<std::complex<float>> coef;
coef.unit_cell_ = mtz.unit_cell;
coef.spacegroup_ = sg;
for (size_t i = 0; i < h.values.size(); i++)
if (std::isfinite(f.values[i]) && std::isfinite(phi.values[i])) // missing: no Bijvoet pair (ANOM)
coef.v.push_back({{static_cast<int>(h.values[i]), static_cast<int>(k.values[i]), static_cast<int>(l.values[i])},
std::polar(f.values[i], phi.values[i] * static_cast<float>(M_PI / 180.0))});
coef.ensure_sorted();
// Sampled as ModelValidation samples its maps.
const std::array<int, 3> size = gemmi::get_size_for_hkl(coef, {{0, 0, 0}}, 3.0);
gemmi::Ccp4<float> map;
map.grid = MapFromFPhi(gemmi::get_f_phi_on_grid<float>(coef, size, true));
map.update_ccp4_header(2, true); // the statistics of the whole cell
gemmi::Structure st = *model;
st.cell = mtz.unit_cell;
const gemmi::Box<gemmi::Fractional> model_box = gemmi::calculate_fractional_box(st, MAP_MARGIN_A);
gemmi::Box<gemmi::Fractional> box;
for (int i = 0; i < 3; i++) {
const double centre = (model_box.minimum.at(i) + model_box.maximum.at(i)) / 2;
const double half = std::max(0.5, (model_box.maximum.at(i) - model_box.minimum.at(i)) / 2);
box.minimum.at(i) = centre - half;
box.maximum.at(i) = centre + half - 1e-9; // one cell is exactly one cell of grid points
}
map.set_extent(box);
std::string out(4 * map.ccp4_header.size() + sizeof(float) * map.grid.data.size(), '\0');
memcpy(out.data(), map.ccp4_header.data(), 4 * map.ccp4_header.size());
memcpy(out.data() + 4 * map.ccp4_header.size(), map.grid.data.data(), sizeof(float) * map.grid.data.size());
cached = std::move(out);
return *cached;
}
void PublishStructures(RugnuxLiveStatus &status, const PlacedModelOutput &placed, const ModelMaps *maps,
const PlacedModelOutput *refined, const ModelMaps *refined_maps) {
std::optional<MtzTable> coefficients;
if (maps)
coefficients = maps->coefficients;
std::shared_ptr<const RugnuxStructure> refined_structure;
if (refined) {
std::optional<MtzTable> refined_coefficients;
if (refined_maps)
refined_coefficients = refined_maps->coefficients;
refined_structure = std::make_shared<RugnuxStructure>(refined->model, refined->cell, refined->space_group,
std::move(refined_coefficients));
}
status.Structure(std::make_shared<RugnuxStructure>(placed.model, placed.cell, placed.space_group,
std::move(coefficients)),
std::move(refined_structure));
}
void DropResultBlocks(EndMessage &message) {
message.merged.reset();
message.map_coefficients.reset();
message.placed_model.reset();
message.refined_model.reset();
message.refined_map_coefficients.reset();
message.blobs.reset();
}
namespace {
bool FitsEndMessage(const EndMessage &message, uint8_t *scratch) {
CBORStream2Serializer serializer(scratch, MESSAGE_SIZE_FOR_START_END);
try {
serializer.SerializeSequenceEnd(message);
return true;
} catch (const JFJochException &) {
return false;
}
}
}
void DropResultBlocksThatDoNotFit(EndMessage &message, RugnuxLiveStatus *status, Logger &logger) {
const auto scratch = std::make_unique_for_overwrite<uint8_t[]>(MESSAGE_SIZE_FOR_START_END);
if (FitsEndMessage(message, scratch.get()))
return;
// The structures first: the merged data are the smaller block and the one the run is for
message.map_coefficients.reset();
message.placed_model.reset();
message.refined_model.reset();
message.refined_map_coefficients.reset();
message.blobs.reset();
std::string text;
if (FitsEndMessage(message, scratch.get()))
text = fmt::format("the map coefficients and models do not fit in the end message ({} MiB) and are not "
"written", MESSAGE_SIZE_FOR_START_END / (1024 * 1024));
else {
message.merged.reset();
text = fmt::format("the merged data, map coefficients and models do not fit in the end message ({} MiB) "
"and are not written", MESSAGE_SIZE_FOR_START_END / (1024 * 1024));
}
logger.Error("rugnux live: {}", text);
if (status)
status->Warning(text);
}