Refiner.h 5.32 KB
Newer Older
1
2
//  ***********************************************************************************************
//
Raza, Zamaan's avatar
Raza, Zamaan committed
3
//  OpenHKL: data reduction for single crystal diffraction
4
//
Wuttke, Joachim's avatar
Wuttke, Joachim committed
5
//! @file      core/algo/Refiner.h
6
//! @brief     Defines class Refiner
7
8
9
10
11
12
13
14
//!
//! @homepage  ###HOMEPAGE###
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Institut Laue-Langevin and Forschungszentrum Jülich GmbH 2016-
//! @authors   see CITATION, MAINTAINER
//
//  ***********************************************************************************************

Raza, Zamaan's avatar
Raza, Zamaan committed
15
16
#ifndef OHKL_CORE_ALGO_REFINER_H
#define OHKL_CORE_ALGO_REFINER_H
jmfisher's avatar
jmfisher committed
17

Wuttke, Joachim's avatar
Wuttke, Joachim committed
18
#include "core/algo/RefinementBatch.h"
19
#include "core/experiment/UnitCellHandler.h"
20
#include "core/integration/IIntegrator.h"
21
22
#include "tables/crystal/UnitCell.h"
#include <vector>
jmfisher's avatar
jmfisher committed
23

Raza, Zamaan's avatar
Raza, Zamaan committed
24
namespace ohkl {
jmfisher's avatar
jmfisher committed
25

26
27
class ProgressHandler;
using sptrProgressHandler = std::shared_ptr<ProgressHandler>;
Raza, Zamaan's avatar
Raza, Zamaan committed
28
enum class Level;
29

30
31
32
/*! \addtogroup python_api
 *  @{*/

33
//! Parameters for refinement
34
struct RefinerParameters {
35
    //! Refine unit cell
36
    bool refine_ub = true;
37
    //! Refine incident wavevector
38
    bool refine_ki = true;
39
    //! Refine sample position
40
    bool refine_sample_position = true;
41
    //! Refine sample orientation
42
    bool refine_sample_orientation = true;
43
    //! Refine detector offset
44
    bool refine_detector_offset = true;
45
    //! Number of refinement batches
46
    int nbatches = 10;
47
    //! Type of residual used in minimisation
48
    ResidualType residual_type = ResidualType::QSpace;
49
    //! Maximum number of iteration for minimisation
50
    unsigned int max_iter = 1000;
51
    //! Use unit cells assigned to batches in further refinement
52
    bool use_batch_cells = true;
53
54
    //! Assign unit cell to peaks when making batches
    bool set_unit_cell = true;
55
56
57

    void log(const Level& level) const;
};
58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*! \addtogroup python_api
 *  @{*/

/*! \brief Refine unit cell and instrument states
 *
 * Use GSL non-linear least squares minimisation to refine instrument states
 * (sample position and orientation, incident wavevector, and detector position)
 * and unit cell parameters. The residual is computed as the difference between
 * a q-vector computed from the unit cell (used to predict peaks) and a q-vector
 * from transforming an observed reflection into reciprocal space. An
 * alternative residual (useful for refining unit cells and incident
 * wavevectors) is computed using the real space difference between a predicted
 * spot and its associated observed spot.
 */

74
class Refiner {
75
 public:
76
77
78
79
    Refiner(UnitCellHandler* cell_handler);

    //! Generate batches of peaks per frame range with the given peak list
    void makeBatches(
Raza, Zamaan's avatar
Raza, Zamaan committed
80
        InstrumentStateList& states, const std::vector<ohkl::Peak3D*>& peaks, sptrUnitCell cell);
Wuttke, Joachim's avatar
Wuttke, Joachim committed
81

82
83
84
    //! Rebuild old batches if refinement failed
    void reconstructBatches(std::vector<Peak3D*> peaks);

85
    //! Sets the lattice B matrix to be refined.
86
    void refineUB();
Wuttke, Joachim's avatar
Wuttke, Joachim committed
87

88
    //! Sets detector offsets in the given list of instrument states to be refined.
89
    void refineDetectorOffset();
Wuttke, Joachim's avatar
Wuttke, Joachim committed
90
91

    //! Sets the sample position in the given list of instrument states to be refined.
92
    void refineSamplePosition();
Wuttke, Joachim's avatar
Wuttke, Joachim committed
93
94

    //! Sets the sample orientation in the given list of instrument states to be refined.
95
    void refineSampleOrientation();
Wuttke, Joachim's avatar
Wuttke, Joachim committed
96

97
    //! Sets the source ki in the given list of instrument states to be refined.
98
    void refineKi();
Wuttke, Joachim's avatar
Wuttke, Joachim committed
99

100
    //! Perform the refinement with the maximum number of iterations as given. N.B. the four
Wuttke, Joachim's avatar
Wuttke, Joachim committed
101
    //! previous functions set the number of free parameters and at least one must be run
102
    //! *before* refine
103
    bool refine();
Wuttke, Joachim's avatar
Wuttke, Joachim committed
104

105
106
107
    //! Updates the centers of predicted peaks, after refinement.
    //! Returns the number of remaining peaks
    //! (some peaks may be rejected with flag PredictionUpdateFailure).
108
    int updatePredictions(std::vector<Peak3D*> peaks);
Wuttke, Joachim's avatar
Wuttke, Joachim committed
109

110
    //! Returns the individual peak/frame batches used during refinement.
111
    const std::vector<RefinementBatch>& batches() const;
112

113
    //! Return the unrefined cell
114
    const UnitCell* unrefinedCell() const;
115

116
    //! Return the refined states
117
    const InstrumentStateList* refinedStates() const;
118

119
    //! Return the unrefined states
120
    const InstrumentStateList* unrefinedStates() const;
121
122
123
124

    //! Return number of frames
    int nframes() const;

125
126
127
    //! Check if this is the first refinement
    bool firstRefine() const;

128
129
130
    //! Write the initial and final cells to the log
    void logChange();

131
132
    //! get a pointer to the parameters
    RefinerParameters* parameters();
133

134
135
136
    //! set the parameters
    void setParameters(const RefinerParameters& params);

137
138
139
    //! Assign batch cells to predicted peaks
    void assignPredictedCells(std::vector<Peak3D*> predicted_peaks);

Raza, Zamaan's avatar
Raza, Zamaan committed
140
141
142
 private:
    //! Determine which unit cell to use in a batch
    sptrUnitCell _getUnitCell(const std::vector<Peak3D*> peaks_subset);
143

144
    UnitCellHandler* _cell_handler;
145
    UnitCell _unrefined_cell;
146
    InstrumentStateList _unrefined_states;
147
    sptrUnitCell _cell;
148
    std::vector<UnitCell*> _batch_cells;
149
    std::vector<RefinementBatch> _batches;
150
    int _nframes;
151
    bool _first_refine = true;
152
    InstrumentStateList* _states;
153
    std::unique_ptr<RefinerParameters> _params;
154
    static constexpr double _eps_norm = 50.0;
155
156
    std::vector<sptrUnitCell> _tmp_vec;
    std::vector<Peak3D*> _peaks;
157
158
};

159
/*! @}*/
Raza, Zamaan's avatar
Raza, Zamaan committed
160
} // namespace ohkl
161

Raza, Zamaan's avatar
Raza, Zamaan committed
162
#endif // OHKL_CORE_ALGO_REFINER_H