Files
Jungfraujoch/tests/LatticeSearchTest.cpp
T
leonarski_fandjungfrau 4dc2534dbf
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 18m57s
Build Packages / Unit tests (push) Skipped
Build Packages / build:windows:nocuda (push) Successful in 16m55s
Build Packages / build:windows:cuda (push) Successful in 18m48s
Build Packages / build:viewer-tgz:cpu (push) Successful in 13m10s
Build Packages / build:viewer-tgz:cuda (push) Successful in 14m45s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 22m23s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 20m12s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 23m7s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 20m43s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 23m9s
Build Packages / XDS test (durin plugin) (push) Successful in 12m26s
Build Packages / build:rpm (rocky9) (push) Successful in 24m58s
Build Packages / Generate python client (push) Successful in 50s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 23m20s
Build Packages / Create release (push) Skipped
Build Packages / XDS test (JFJoch plugin) (push) Successful in 12m37s
Build Packages / build:rpm (rocky8) (push) Successful in 27m58s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 25m38s
Build Packages / Build documentation (push) Successful in 59s
Build Packages / DIALS test (push) Successful in 23m16s
Build Packages / XDS test (neggia plugin) (push) Successful in 6m38s
v1.0.0.rc-162 (#72)
**Files written by Jungfraujoch now import correctly in DIALS, XDS and pyFAI.** A tilted detector, a grid scan, a still recorded at a goniometer position, and saturated or unreadable pixels were each described in a way that a third-party program acted on wrongly. If you process Jungfraujoch data outside Jungfraujoch, prefer this release to any earlier one.

* HDF5: the detector tilt (`rot1`/`rot2`/`rot3`) is exported correctly in the NXmx transformation chain; untilted geometries are unaffected.
* HDF5: a still recorded at a goniometer position is no longer read back as a single image, and a grid scan records a stationary spindle so a program that requires a rotation axis can open it.
* HDF5: the sample transformation chain is written in mounting order, with a Smargon head position told apart from the spindle, one entry per image, `module_offset` as a float unit vector, and `offset_units` on every offset.
* HDF5: saturated, underloaded and unreadable pixels are described so a downstream program masks them - `saturation_value`, `underload_value`, `error_value` and `bit_depth_readout` are written correctly, and a data file missing next to a VDS master reads as the error marker rather than as zero counts.
* HDF5: the rotation axis is read back under whatever name it carries, and `mirror_y` records whether the assembled image is mirrored in Y relative to the detector's raw readout.
* A grid scan and a goniometer axis can both be set; they are no longer alternatives.
* `images_per_file` is chosen from the acquisition when it is not given: a rotation sweep of at most 20000 images goes into a single data file, a grid scan splits on whole fast-axis rows, and stills and serial keep 1000.
* The writer refuses a stream whose start message declares a different pixel format than its images carry, and a DECTRIS detector sending signed images is no longer declared unsigned.
* The image stream can carry the sample transformation chain (`transformations`, in the END message); a producer that does not send it gets the same chain built by the writer.
* rugnux: fixing the space group with `-S` no longer prevents the lattice from being found - a lattice indexed in a different setting is reindexed into that group's own setting, and a run whose crystal does not have that group's lattice stops and names the cell it indexed as, rather than reporting statistics that cannot describe it.
* rugnux: the per-image resolution estimate now predicts the resolution the merged data reach rather than the highest-resolution spot found, and is reported as `SPOT_RESOLUTION_ESTIMATE`.
* rugnux: two runs of the same command on the same images produce the same merged intensities; the azimuthal profile written alongside them is not yet reproducible in the same way.
* rugnux: the offline lattice refinement is bounded by iterations rather than by a wall clock, so a loaded machine can no longer refine to a different lattice; a live acquisition keeps its real-time bound.
* rugnux: the detector-frame modulation correction is fitted on a grid spanning the detector, so whether it is applied no longer depends on how far integration reached.
* rugnux: the geometry pre-pass no longer writes `<prefix>_01.mtz`, `_01.cif`, `_01.hkl` and `_01_image.dat`; the refined second pass writes those files under `<prefix>`, and that is the result to use.
* rugnux: `_process.h5` describes the pixel format of the images it links to, and is written on a thread of its own.
* rugnux: the detector geometry is also logged in XDS's convention (`ORGX`/`ORGY`, detector axis vectors, rotation axis), so it can be compared with an XDS refinement.
* rugnux: an image integrated in pyFAI through the `.poni` file written by `--mode calibration` comes out with the correct azimuth, and the file declares pyFAI's `orientation`, which needs pyFAI 2024.01 or newer. Radial integration is unchanged.
* rugnux: a rotation run is substantially faster throughout - beam-stop detection, first-pass indexing, geometry refinement, integration, scaling and merging - and observations outside the scaling resolution range are dropped as they are ingested. The refined geometry, the space group chosen and the merged statistics are unchanged.
* Faster spot finding and indexing, on the broker as well as in rugnux; the spots found and the lattices indexed are unchanged.
* A run reserves substantially less GPU memory: nothing is allocated for buffers that are never read, and a worker builds only the engines it uses.
* rugnux: with `-N` left at its default the per-image loop of `--mode mx` uses at most 16 workers per GPU, rather than one per hardware thread; an explicit `-N` is obeyed as given.
* CUDA 12 builds now contain device code for Volta, so the RHEL 8 packages and the portable Linux `.tgz` run on a V100; the CUDA 13 artefacts (RHEL 9, Ubuntu, Windows) remain Turing and newer.
* The build resolves a single Eigen for the whole project, and refuses to configure if Ceres picks up a different one; a build that mixed two Eigen versions was undefined behaviour and crashed at -O2.
* Documentation: a security page, and the supported GPU generations and minimum NVIDIA driver version of every released artefact.

**Breaking change to OpenAPI** - regenerate the client (`jfjoch-client` 1.0.0-rc.162, `frontend/src/client`):
* `dataset_settings.images_per_file` is no longer `default: 1000` and no longer accepts `0`; it is optional, and its minimum is 1. A client sending `0` (previously "one file for the whole run") is now rejected - omit the field instead, which for a rotation sweep gives the same single file.
* `file_writer_format` now defaults to `NXmxVDS`, matching the server's own default and the layout recommended for DIALS, XDS and CrystFEL. A generated client that fills in schema defaults and does not set the format explicitly will write VDS masters where it previously wrote legacy ones; set `NXmxLegacy` explicitly to keep them.

---------

Co-authored-by: jungfrau <jungfrau@mx-aare-test.psi.ch>
Reviewed-on: #72
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-08-25 08:21:39 +02:00

515 lines
18 KiB
C++

// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include <catch2/catch_all.hpp>
#include "../common/CrystalLattice.h"
#include "../common/Coord.h"
#include "../common/UnitCell.h"
#include "../image_analysis/lattice_search/LatticeSearch.h"
#include "gemmi/symmetry.hpp"
#include <cmath>
// Helper: check near-equality of unit cell parameters
static void check_uc(const UnitCell& uc, double a, double b, double c,
double alpha, double beta, double gamma,
double eps_len = 1e-6, double eps_ang = 1e-4) {
CHECK(uc.a == Catch::Approx(a).margin(eps_len));
CHECK(uc.b == Catch::Approx(b).margin(eps_len));
CHECK(uc.c == Catch::Approx(c).margin(eps_len));
CHECK(uc.alpha == Catch::Approx(alpha).margin(eps_ang));
CHECK(uc.beta == Catch::Approx(beta ).margin(eps_ang));
CHECK(uc.gamma == Catch::Approx(gamma).margin(eps_ang));
}
TEST_CASE("LatticeSearch - cubic I") {
// Build a body-centered cubic cell with a=40:
// primitive basis vectors (conventional I cubic primitive):
// p1 = (0, a/2, a/2), p2 = (a/2, 0, a/2), p3 = (a/2, a/2, 0)
const double a = 40.0;
CrystalLattice L(
Coord(a, 0, 0),
Coord(0, a, 0),
Coord(0, 0, a)
);
L = L.ToPrimitive('I');
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Cubic);
CHECK(res.centering == 'I');
// Conventional cubic I should have equal edges and 90° angles
auto uc = res.conventional.GetUnitCell();
CHECK(uc.a == Catch::Approx( a )); // In this construction, conventional a matches given a
CHECK(uc.b == Catch::Approx( a ));
CHECK(uc.c == Catch::Approx( a ));
CHECK(uc.alpha == Catch::Approx(90.0));
CHECK(uc.beta == Catch::Approx(90.0));
CHECK(uc.gamma == Catch::Approx(90.0));
}
TEST_CASE("LatticeSearch - cubic F") {
// Build a body-centered cubic cell with a=40:
// primitive basis vectors (conventional I cubic primitive):
// p1 = (0, a/2, a/2), p2 = (a/2, 0, a/2), p3 = (a/2, a/2, 0)
const double a = 40.0;
CrystalLattice L(
Coord(a, 0, 0),
Coord(0, a, 0),
Coord(0, 0, a)
);
L = L.ToPrimitive('F');
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Cubic);
CHECK(res.centering == 'F');
// Conventional cubic I should have equal edges and 90° angles
auto uc = res.conventional.GetUnitCell();
CHECK(uc.a == Catch::Approx( a )); // In this construction, conventional a matches given a
CHECK(uc.b == Catch::Approx( a ));
CHECK(uc.c == Catch::Approx( a ));
CHECK(uc.alpha == Catch::Approx(90.0));
CHECK(uc.beta == Catch::Approx(90.0));
CHECK(uc.gamma == Catch::Approx(90.0));
}
TEST_CASE("LatticeSearch - cubic P") {
// Simple cubic P, a=30
const double a = 30.0;
CrystalLattice L(
Coord(a,0,0),
Coord(0,a,0),
Coord(0,0,a)
);
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Cubic);
CHECK(res.centering == 'P');
auto uc = res.conventional.GetUnitCell();
check_uc(uc, a, a, a, 90.0, 90.0, 90.0, 1e-6, 1e-4);
}
TEST_CASE("LatticeSearch - tetragonal I") {
// Build a body-centered cubic cell with a=40:
// primitive basis vectors (conventional I cubic primitive):
// p1 = (0, a/2, a/2), p2 = (a/2, 0, a/2), p3 = (a/2, a/2, 0)
const double a = 40.0;
const double b = 34.0;
CrystalLattice L(
Coord(a, 0, 0),
Coord(0, a, 0),
Coord(0, 0, b)
);
L = L.ToPrimitive('I');
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Tetragonal);
CHECK(res.centering == 'I');
// Conventional cubic I should have equal edges and 90° angles
auto uc = res.conventional.GetUnitCell();
CHECK(uc.a == Catch::Approx( a )); // In this construction, conventional a matches given a
CHECK(uc.b == Catch::Approx( a ));
CHECK(uc.c == Catch::Approx( b ));
CHECK(uc.alpha == Catch::Approx(90.0));
CHECK(uc.beta == Catch::Approx(90.0));
CHECK(uc.gamma == Catch::Approx(90.0));
}
TEST_CASE("LatticeSearch - tetragonal I - v2") {
// Build a body-centered cubic cell with a=40:
// primitive basis vectors (conventional I cubic primitive):
// p1 = (0, a/2, a/2), p2 = (a/2, 0, a/2), p3 = (a/2, a/2, 0)
const double a = 40.0;
const double b = 54.0;
CrystalLattice L(
Coord(a, 0, 0),
Coord(0, a, 0),
Coord(0, 0, b)
);
L = L.ToPrimitive('I');
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Tetragonal);
CHECK(res.centering == 'I');
// Conventional cubic I should have equal edges and 90° angles
auto uc = res.conventional.GetUnitCell();
CHECK(uc.a == Catch::Approx( a )); // In this construction, conventional a matches given a
CHECK(uc.b == Catch::Approx( a ));
CHECK(uc.c == Catch::Approx( b ));
CHECK(uc.alpha == Catch::Approx(90.0));
CHECK(uc.beta == Catch::Approx(90.0));
CHECK(uc.gamma == Catch::Approx(90.0));
}
// Tetragonal P: a=b!=c, all angles 90, P-centering
TEST_CASE("LatticeSearch - tetragonal P") {
const double a = 37.0, c = 59.0;
CrystalLattice L(
Coord(a,0,0),
Coord(0,a,0),
Coord(0,0,c)
);
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Tetragonal);
CHECK(res.centering == 'P');
auto uc = res.conventional.GetUnitCell();
check_uc(uc, a, a, c, 90.0, 90.0, 90.0, 1e-2, 1e-2);
}
// Orthorhombic F: all angles 90, unequal edges, F-centering
TEST_CASE("LatticeSearch - orthorhombic F") {
const double a = 35.0, b = 41.0, c = 57.0;
CrystalLattice conv(a,b,c, 90.0,90.0,90.0);
CrystalLattice L = conv.ToPrimitive('F');
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Orthorhombic);
CHECK(res.centering == 'F');
auto uc = res.conventional.GetUnitCell();
check_uc(uc, a, b, c, 90.0, 90.0, 90.0, 1e-1, 1e-2);
}
TEST_CASE("LatticeSearch - orthorhombic F - permutation 1") {
const double a = 41.0, b = 57.0, c = 35.0;
CrystalLattice conv(a,b,c, 90.0,90.0,90.0);
CrystalLattice L = conv.ToPrimitive('F');
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Orthorhombic);
CHECK(res.centering == 'F');
auto uc = res.conventional.GetUnitCell();
check_uc(uc, c, a, b, 90.0, 90.0, 90.0, 1e-1, 1e-2);
}
// Orthorhombic C: all angles 90, unequal edges, C-centering
TEST_CASE("LatticeSearch - orthorhombic C") {
const double a = 35.0, b = 41.0, c = 57.0;
CrystalLattice conv(a,b,c, 90.0,90.0,90.0);
CrystalLattice L = conv.ToPrimitive('C');
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Orthorhombic);
CHECK(res.centering == 'C');
auto uc = res.conventional.GetUnitCell();
check_uc(uc, a, b, c, 90.0, 90.0, 90.0, 1e-1, 1e-2);
}
TEST_CASE("LatticeSearch - orthorhombic I") {
const double a = 35.0, b = 41.0, c = 57.0;
CrystalLattice conv(a,b,c, 90.0,90.0,90.0);
CrystalLattice L = conv.ToPrimitive('I');
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Orthorhombic);
CHECK(res.centering == 'I');
auto uc = res.conventional.GetUnitCell();
check_uc(uc, a, b, c, 90.0, 90.0, 90.0, 1e-2, 1e-2);
}
TEST_CASE("LatticeSearch - orthorhombic I - permutation1") {
const double a = 57.0, b = 41.0, c = 35.0;
CrystalLattice conv(a,b,c, 90.0,90.0,90.0);
CrystalLattice L = conv.ToPrimitive('I');
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Orthorhombic);
CHECK(res.centering == 'I');
auto uc = res.conventional.GetUnitCell();
check_uc(uc, c, b, a, 90.0, 90.0, 90.0, 1e-2, 1e-2);
}
TEST_CASE("LatticeSearch - orthorhombic I - permutation2") {
const double a = 41.0, b = 57.0, c = 35.0;
CrystalLattice conv(a,b,c, 90.0,90.0,90.0);
CrystalLattice L = conv.ToPrimitive('I');
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Orthorhombic);
CHECK(res.centering == 'I');
auto uc = res.conventional.GetUnitCell();
check_uc(uc, c, a, b, 90.0, 90.0, 90.0, 1e-2, 1e-2);
}
// Orthorhombic P: all angles 90, unequal edges, P-centering
TEST_CASE("LatticeSearch - orthorhombic P") {
const double a = 35.0, b = 41.0, c = 57.0;
CrystalLattice L(a,b,c, 90.0,90.0,90.0);
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Orthorhombic);
CHECK(res.centering == 'P');
auto uc = res.conventional.GetUnitCell();
check_uc(uc, a, b, c, 90.0, 90.0, 90.0, 1e-6, 1e-4);
}
// Hexagonal P: a=b!=c, alpha=beta=90, gamma=120, P-centering
TEST_CASE("LatticeSearch - hexagonal P") {
const double a = 30.0, c = 48.0;
CrystalLattice L(
Coord(a, 0, 0),
Coord(-a/2, a*std::sqrt(3)/2, 0),
Coord(0, 0, c)
);
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Hexagonal);
CHECK(res.centering == 'P');
auto uc = res.conventional.GetUnitCell();
CHECK(uc.a == Catch::Approx(a).margin(1e-2));
CHECK(uc.b == Catch::Approx(a).margin(1e-2));
CHECK(uc.c == Catch::Approx(c).margin(1e-2));
CHECK(uc.alpha == Catch::Approx(90.0).margin(1e-2));
CHECK(uc.beta == Catch::Approx(90.0).margin(1e-2));
CHECK(uc.gamma == Catch::Approx(120.0).margin(1e-2));
}
TEST_CASE("LatticeSearch - monoclinic C (unique b)") {
const double a = 50.0, b = 60.0, c = 70.0;
const double alpha = 90.0, beta = 96.0, gamma = 90.0;
CrystalLattice conv(a,b,c, alpha,beta,gamma);
auto L = conv.ToPrimitive('C');
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Monoclinic);
CHECK(res.centering == 'C');
auto uc = res.conventional.GetUnitCell();
// Check right angles at alpha,gamma and non-90 beta; lengths comparable
CHECK(std::fabs(uc.alpha - 90.0) < 1e-3);
CHECK(std::fabs(uc.gamma - 90.0) < 1e-3);
CHECK(std::fabs(uc.beta - beta) < 1e-2);
// Lengths should match within small tolerance
CHECK(uc.a == Catch::Approx(a).margin(1e-2));
CHECK(uc.b == Catch::Approx(b).margin(1e-2));
CHECK(uc.c == Catch::Approx(c).margin(1e-2));
}
TEST_CASE("LatticeSearch - monoclinic C (unique b) - v2") {
const double a = 71.0, b = 35.0, c = 90.0;
const double alpha = 90.0, beta = 96.0, gamma = 90.0;
CrystalLattice conv(a,b,c, alpha,beta,gamma);
auto L = conv.ToPrimitive('C');
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Monoclinic);
CHECK(res.centering == 'C');
auto uc = res.conventional.GetUnitCell();
// Check right angles at alpha,gamma and non-90 beta; lengths comparable
CHECK(std::fabs(uc.alpha - 90.0) < 1e-3);
CHECK(std::fabs(uc.gamma - 90.0) < 1e-3);
CHECK(std::fabs(uc.beta - beta) < 1e-2);
// Lengths should match within small tolerance
CHECK(uc.a == Catch::Approx(a).margin(1e-2));
CHECK(uc.b == Catch::Approx(b).margin(1e-2));
CHECK(uc.c == Catch::Approx(c).margin(1e-2));
}
TEST_CASE("LatticeSearch - monoclinic C (unique a)") {
const double a = 60.0, b = 50.0, c = 70.0;
const double alpha = 96.0, beta = 90.0, gamma = 90.0;
CrystalLattice conv(a,b,c, alpha,beta,gamma);
auto L = conv.ToPrimitive('C');
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Monoclinic);
CHECK(res.centering == 'C');
auto uc = res.conventional.GetUnitCell();
// Check right angles at alpha,gamma and non-90 beta; lengths comparable
CHECK(std::fabs(uc.alpha - 90.0) < 1e-3);
CHECK(std::fabs(uc.gamma - 90.0) < 1e-3);
CHECK(std::fabs(uc.beta - alpha) < 1e-2);
// Lengths should match within small tolerance
CHECK(uc.a == Catch::Approx(b).margin(1e-2));
CHECK(uc.b == Catch::Approx(a).margin(1e-2));
CHECK(uc.c == Catch::Approx(c).margin(1e-2));
}
TEST_CASE("LatticeSearch - monoclinic P (unique b)") {
const double a = 50.0, b = 60.0, c = 70.0;
const double alpha = 90.0, beta = 96.0, gamma = 90.0;
CrystalLattice conv(a,b,c, alpha,beta,gamma);
auto res = LatticeSearch(conv, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Monoclinic);
CHECK(res.centering == 'P');
auto uc = res.conventional.GetUnitCell();
// Check right angles at alpha,gamma and non-90 beta; lengths comparable
CHECK(std::fabs(uc.alpha - 90.0) < 1e-3);
CHECK(std::fabs(uc.gamma - 90.0) < 1e-3);
CHECK(std::fabs(uc.beta - beta) < 1e-2);
// Lengths should match within small tolerance
CHECK(uc.a == Catch::Approx(a).margin(1e-2));
CHECK(uc.b == Catch::Approx(b).margin(1e-2));
CHECK(uc.c == Catch::Approx(c).margin(1e-2));
}
TEST_CASE("LatticeSearch - monoclinic P (unique b) - v2") {
const double a = 90.0, b = 35.0, c = 71.0;
const double alpha = 90.0, beta = 96.0, gamma = 90.0;
CrystalLattice conv(a,b,c, alpha,beta,gamma);
auto res = LatticeSearch(conv, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Monoclinic);
CHECK(res.centering == 'P');
auto uc = res.conventional.GetUnitCell();
// Check right angles at alpha,gamma and non-90 beta; lengths comparable
CHECK(std::fabs(uc.alpha - 90.0) < 1e-3);
CHECK(std::fabs(uc.gamma - 90.0) < 1e-3);
CHECK(std::fabs(uc.beta - beta) < 1e-2);
// Lengths should match within small tolerance
CHECK(uc.a == Catch::Approx(c).margin(1e-2));
CHECK(uc.b == Catch::Approx(b).margin(1e-2));
CHECK(uc.c == Catch::Approx(a).margin(1e-2));
}
TEST_CASE("LatticeSearch - triclinic P") {
// General triclinic primitive cell
CrystalLattice L(33.1, 41.7, 52.3, 89.1, 85.0, 76.3);
auto res = LatticeSearch(L, 1e-6);
// System should be triclinic, centering P, and conventional equals some standardized primitive
CHECK(res.system == gemmi::CrystalSystem::Triclinic);
CHECK(res.centering == 'P');
// The conventional cell should be metric-equivalent to input. We verify only the system and centering here.
// Reduced primitive must be non-singular
auto uc_red = res.primitive_reduced.GetUnitCell();
CHECK(uc_red.a > 0);
CHECK(uc_red.b > 0);
CHECK(uc_red.c > 0);
}
TEST_CASE("LatticeSearch - triclinic P - v2") {
// General triclinic primitive cell
CrystalLattice L(33.1, 41.7, 52.3, 100, 92, 115);
auto res = LatticeSearch(L, 1e-6);
// System should be triclinic, centering P, and conventional equals some standardized primitive
CHECK(res.system == gemmi::CrystalSystem::Triclinic);
CHECK(res.centering == 'P');
// The conventional cell should be metric-equivalent to input. We verify only the system and centering here.
// Reduced primitive must be non-singular
auto uc_red = res.primitive_reduced.GetUnitCell();
CHECK(uc_red.a > 0);
CHECK(uc_red.b > 0);
CHECK(uc_red.c > 0);
}
TEST_CASE("LatticeSearch - trigonal R") {
const double a = 32.0;
const double alpha = 80.0;
// Build rhombohedral in rhombohedral setting (primitive axes a=b=c, alpha=beta=gamma)
CrystalLattice L(a, a, a, alpha, alpha, alpha);
auto res = LatticeSearch(L, 1e-6);
CHECK(res.system == gemmi::CrystalSystem::Trigonal);
CHECK(res.centering == 'R');
auto uc_red = res.conventional.GetUnitCell();
CHECK(uc_red.alpha == Catch::Approx(90).margin(1e-2));
CHECK(uc_red.beta == Catch::Approx(90).margin(1e-2));
CHECK(uc_red.gamma == Catch::Approx(120).margin(1e-2));
auto uc_prim = res.primitive_reduced.GetUnitCell();
CHECK(uc_prim.alpha == Catch::Approx(alpha).margin(1e-2));
CHECK(uc_prim.beta == Catch::Approx(alpha).margin(1e-2));
CHECK(uc_prim.gamma == Catch::Approx(alpha).margin(1e-2));
}
// The class-filtered walk: the same table, restricted to one Bravais class. A tetragonal-P lattice is
// also a C-centred orthorhombic one (a_C = a+b, b_C = -a+b, c_C = c), and asking for that class has to
// return that setting even though the plain search rightly prefers the tetragonal one.
TEST_CASE("LatticeSearchForClass - tetragonal P also has a C-centred orthorhombic setting") {
const double a = 50.0, c = 120.0;
const CrystalLattice L(a, a, c, 90, 90, 90);
const auto plain = LatticeSearch(L, 1e-6);
CHECK(plain.system == gemmi::CrystalSystem::Tetragonal);
CHECK(plain.centering == 'P');
const auto ortho = LatticeSearchForClass(L, gemmi::CrystalSystem::Orthorhombic, 'C', 1e-6);
REQUIRE(ortho.has_value());
CHECK(ortho->system == gemmi::CrystalSystem::Orthorhombic);
CHECK(ortho->centering == 'C');
const auto uc = ortho->conventional.GetUnitCell();
// The C cell is the face diagonal on a and b, so twice the volume and a = b = a_tet * sqrt(2).
CHECK(uc.a == Catch::Approx(a * std::sqrt(2.0)).margin(1e-4));
CHECK(uc.b == Catch::Approx(a * std::sqrt(2.0)).margin(1e-4));
CHECK(uc.c == Catch::Approx(c).margin(1e-4));
CHECK(uc.alpha == Catch::Approx(90).margin(1e-4));
CHECK(uc.beta == Catch::Approx(90).margin(1e-4));
CHECK(uc.gamma == Catch::Approx(90).margin(1e-4));
}
TEST_CASE("LatticeSearchForClass - a class the metric cannot carry is refused") {
// A general triclinic metric has no monoclinic-C setting, and an F-centred cubic lattice has no
// hexagonal-P one (its hexagonal description is R-centred).
const CrystalLattice tri(41.0, 47.0, 53.0, 71.0, 83.0, 97.0);
CHECK_FALSE(LatticeSearchForClass(tri, gemmi::CrystalSystem::Monoclinic, 'C').has_value());
const double a = 60.0;
const auto cubic_f = CrystalLattice(a, a, a, 90, 90, 90).ToPrimitive('F');
CHECK(LatticeSearch(cubic_f, 1e-6).centering == 'F');
CHECK_FALSE(LatticeSearchForClass(cubic_f, gemmi::CrystalSystem::Hexagonal, 'P').has_value());
// ... but its rhombohedral setting is there, which is what makes the refusal above a real answer
// rather than an artefact of the filter.
const auto rhomb = LatticeSearchForClass(cubic_f, gemmi::CrystalSystem::Trigonal, 'R');
REQUIRE(rhomb.has_value());
CHECK(rhomb->centering == 'R');
}
TEST_CASE("LatticeSearchForClass - asking for what the plain search found returns the same setting") {
const double a = 40.0;
const auto L = CrystalLattice(a, a, a, 90, 90, 90).ToPrimitive('I');
const auto plain = LatticeSearch(L, 1e-6);
const auto filtered = LatticeSearchForClass(L, plain.system, plain.centering, 1e-6);
REQUIRE(filtered.has_value());
CHECK(filtered->niggli_class == plain.niggli_class);
check_uc(filtered->conventional.GetUnitCell(), a, a, a, 90, 90, 90, 1e-4, 1e-4);
}