25#include <boost/algorithm/string/classification.hpp>
26#include <boost/algorithm/string/split.hpp>
27#include <boost/math/special_functions/round.hpp>
49 declareParameter(
"GonRotx", 0.0,
"3rd Rotation of Goniometer about the x axis");
50 declareParameter(
"GonRoty", 0.0,
"2nd Rotation of Goniometer about the y axis");
51 declareParameter(
"GonRotz", 0.0,
"1st Rotation of Goniometer about the z axis");
65 std::vector<std::string> OptRunNums;
66 std::string OptRunstemp(
OptRuns);
68 OptRunstemp = OptRunstemp.substr(1, OptRunstemp.size() - 1);
70 if (!OptRunstemp.empty() && OptRunstemp.at(OptRunstemp.size() - 1) ==
'/')
71 OptRunstemp.pop_back();
73 boost::split(OptRunNums, OptRunstemp, boost::is_any_of(
"/"));
75 for (
const auto &OptRunNum : OptRunNums) {
78 declareParameter(
"omega" + OptRunNum, 0.0,
"Omega sample orientation value");
96 for (
int i = 0; i < Peaks->getNumberPeaks(); ++i) {
101 size_t N =
OptRuns.find(
"/" + runNumStr +
"/");
103 double chi =
getParameter(
"chi" + boost::lexical_cast<std::string>(runNumStr));
104 double phi =
getParameter(
"phi" + boost::lexical_cast<std::string>(runNumStr));
105 double omega =
getParameter(
"omega" + boost::lexical_cast<std::string>(runNumStr));
111 Res[runNum] = uniGonio.
getR();
127 int cint = toupper(axis);
128 auto c =
static_cast<char>(cint);
129 std::string S(std::string(
"") + c);
130 size_t axisPos = std::string(
"XYZ").find(S);
137 double rTheta = theta / 180 * M_PI;
140 Res[axisPos][axisPos] = 1.0;
141 Res[(axisPos + 1) % 3][(axisPos + 1) % 3] = cos(rTheta);
142 Res[(axisPos + 1) % 3][(axisPos + 2) % 3] = -sin(rTheta);
143 Res[(axisPos + 2) % 3][(axisPos + 2) % 3] = cos(rTheta);
144 Res[(axisPos + 2) % 3][(axisPos + 1) % 3] = sin(rTheta);
160 int cint = toupper(axis);
161 auto c =
static_cast<char>(cint);
162 std::string S(std::string(
"") + c);
163 size_t axisPos = std::string(
"XYZ").find(S);
170 double rTheta = theta / 180 * M_PI;
173 Res[axisPos][axisPos] = 0.0;
174 Res[(axisPos + 1) % 3][(axisPos + 1) % 3] = -sin(rTheta);
175 Res[(axisPos + 1) % 3][(axisPos + 2) % 3] = -cos(rTheta);
176 Res[(axisPos + 2) % 3][(axisPos + 2) % 3] = -sin(rTheta);
177 Res[(axisPos + 2) % 3][(axisPos + 1) % 3] = cos(rTheta);
178 return Res * (M_PI / 180.);
197 throw std::invalid_argument(
"Peaks not stored under the name " +
PeakWorkspaceName);
199 V3D const samplePosition = peaksWs->componentInfo().samplePosition();
201 std::map<int, Mantid::Kernel::Matrix<double>> RunNum2GonMatrixMap;
203 const DblMatrix &UBx = peaksWs->sample().getOrientedLattice().getUB();
215 double ChiSqTot = 0.0;
216 for (
size_t i = 0; i < nData; i += 3) {
217 int peakNum = boost::math::iround(xValues[i]);
218 const Peak &peak_old = peaksWs->getPeak(peakNum);
224 size_t N =
OptRuns.find(
"/" + runNumStr +
"/");
236 for (
int k = 0; k < 3; k++) {
237 double d1 = hkl[k] - floor(hkl[k]);
248 g_log.
debug() <<
"------------------------Function---------------------------"
249 "--------------------\n";
250 for (
size_t p = 0; p <
nParams(); p++) {
255 for (
size_t p = 0; p <
nParams(); p++) {
258 if ((constr->
check() > 0))
263 g_log.
debug() <<
" Chi**2 = " << ChiSqTot <<
" nData = " << nData <<
'\n';
270 throw std::invalid_argument(
"Peaks not stored under the name " +
PeakWorkspaceName);
272 const DblMatrix &UB = peaksWs->sample().getOrientedLattice().getUB();
283 Matrix<double> GonRot = InvGonRotxMat * InvGonRotyMat * InvGonRotzMat;
289 std::map<int, Kernel::Matrix<double>> RunNums2GonMatrix;
292 g_log.
debug() <<
"----------------------------Derivative------------------------\n";
294 auto const &componentInfo = peaksWs->componentInfo();
295 V3D const samplePosition = componentInfo.samplePosition();
296 const IPeak &ppeak = peaksWs->getPeak(0);
297 double L0 = ppeak.
getL1();
298 double velocity = (L0 + ppeak.
getL2()) / ppeak.
getTOF();
301 V3D const beamDir =
normalize(samplePosition - componentInfo.sourcePosition());
303 const size_t paramNums[] = {
parameterIndex(std::string(
"SampleXOffset")),
307 for (
size_t i = 0; i < nData; i += 3) {
308 int peakNum = boost::math::iround(xValues[i]);
309 const Peak &peak_old = peaksWs->getPeak(peakNum);
315 for (
int kk = 0; kk < static_cast<int>(
nParams()); kk++) {
316 out->
set(i, kk, 0.0);
317 out->
set(i + 1, kk, 0.0);
318 out->
set(i + 2, kk, 0.0);
321 double chi, phi, omega;
322 size_t chiParamNum, phiParamNum, omegaParamNum;
324 size_t N =
OptRuns.find(
"/" + runNumStr);
339 chi = phichiOmega[1];
340 phi = phichiOmega[2];
341 omega = phichiOmega[0];
343 chiParamNum = phiParamNum = omegaParamNum =
nParams() + 10;
368 V3D Dhkl0 = UBinv * InvR * lab;
370 R = omegaMatrix * dchiMatrix * phiMatrix;
371 InvR = InvG * R * InvG * -1;
374 R = domegaMatrix * chiMatrix * phiMatrix;
375 InvR = InvG * R * InvG * -1;
378 out->
set(i, chiParamNum, Dhkl1[0]);
379 out->
set(i + 1, chiParamNum, Dhkl1[1]);
380 out->
set(i + 2, chiParamNum, Dhkl1[2]);
381 out->
set(i, phiParamNum, Dhkl0[0]);
382 out->
set(i + 1, phiParamNum, Dhkl0[1]);
383 out->
set(i + 2, phiParamNum, Dhkl0[2]);
384 out->
set(i, omegaParamNum, Dhkl2[0]);
385 out->
set(i + 1, omegaParamNum, Dhkl2[1]);
386 out->
set(i + 2, omegaParamNum, Dhkl2[2]);
396 V3D DGonx = (UBinv * InvGon * InvGonRotzMat * InvGonRotyMat *
409 out->
set(i, paramnum, DGonx[0]);
410 out->
set(i + 1, paramnum, DGonx[1]);
411 out->
set(i + 2, paramnum, DGonx[2]);
426 double vmag = (L0 + D.
norm()) / peak.
getTOF();
427 double t1 = peak.
getTOF() - L0 / vmag;
434 Dmagdsxsysz *= (-1 / D.
norm());
436 V3D vmagdsxsysz = Dmagdsxsysz / peak.
getTOF();
438 V3D t1dsxsysz = vmagdsxsysz * (L0 / vmag / vmag);
443 for (
int x = 0;
x < 3;
x++) {
446 V3D dQlab1 = pp / -t1 - D * (t1dsxsysz[
x] / t1 / t1);
447 V3D dQlab2 = beamDir * vmagdsxsysz[
x];
448 V3D dQlab = dQlab2 - dQlab1;
451 V3D dQSamp = Gon * dQlab;
452 V3D dhkl = UBinv * dQSamp;
454 out->
set(i, paramNums[
x], dhkl[0]);
455 out->
set(i + 1, paramNums[
x], dhkl[1]);
456 out->
set(i + 2, paramNums[
x], dhkl[2]);
462 double T = peak_old.
getTOF() + T0;
474 {{UnitParams::l2, peak.
getL2()},
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
An interface to a constraint.
virtual double check()=0
Returns a penalty number which is bigger than or equal to zero If zero it means that the constraint i...
This is a specialization of IFunction for functions of one real argument.
virtual IConstraint * getConstraint(size_t i) const
Get constraint of i-th parameter.
Represents the Jacobian in IFitFunction::functionDeriv.
virtual void set(size_t iY, size_t iP, double value)=0
Set a value to a Jacobian matrix element.
Implements the part of IFunction interface dealing with parameters.
size_t parameterIndex(const std::string &name) const override
Returns the index of parameter name.
std::string parameterName(size_t i) const override
Returns the name of parameter i.
void declareParameter(const std::string &name, double initValue=0, const std::string &description="") override
Declare a new parameter.
size_t nParams() const override
Total number of parameters.
double getParameter(size_t i) const override
Get i-th parameter.
std::string PeakWorkspaceName
static Kernel::Matrix< double > DerivRotationMatrixAboutRegAxis(double theta, char axis)
Returns the derivative of the matrix corresponding to a rotation of theta(degrees) around axis with r...
void getRun2MatMap(DataObjects::PeaksWorkspace_sptr &Peaks, const std::string &OptRuns, std::map< int, Mantid::Kernel::Matrix< double > > &Res) const
Updates the map from run number to GoniometerMatrix.
static Kernel::Matrix< double > RotationMatrixAboutRegAxis(double theta, char axis)
Returns the matrix corresponding to a rotation of theta(degrees) around axis.
static DataObjects::Peak createNewPeak(DataObjects::Peak const &peak_old, Kernel::V3D const &samplePos, double T0, double L0)
Creates a new peak, matching the old peak, with its source and detector positions re-read from the ol...
void function1D(double *out, const double *xValues, const size_t nData) const override
Calculates the h,k, and l offsets from an integer for (some of )the peaks, given the parameter values...
void functionDeriv1D(Mantid::API::Jacobian *out, const double *xValues, const size_t nData) override
Derivatives of function with respect to active parameters.
void init() override
Function initialization. Declare function parameters in this method.
void setUpOptRuns()
Declares parameters for the chi,phi and omega angles for the run numbers where these will be optimize...
void setSigmaIntensity(double m_sigmaIntensity) override
Set the error on the integrated peak intensity.
void setRunNumber(int m_runNumber) override
Set the run number that measured this peak.
double getIntensity() const override
Return the integrated peak intensity.
Mantid::Kernel::V3D getSamplePos() const override
Return the sample position vector.
double getSigmaIntensity() const override
Return the error on the integrated peak intensity.
int getRunNumber() const override
Return the run number this peak was measured at.
void setIntensity(double m_intensity) override
Set the integrated peak intensity.
Mantid::Kernel::V3D getHKL() const override
Return the HKL vector.
double getBinCount() const override
Return the # of counts in the bin at its peak.
Mantid::Kernel::Matrix< double > getGoniometerMatrix() const override
Get the goniometer rotation matrix at which this peak was measured.
void setSamplePos(double samX, double samY, double samZ) override
Set sample position.
void setGoniometerMatrix(const Mantid::Kernel::Matrix< double > &goniometerMatrix) override
Set the goniometer rotation matrix at which this peak was measured.
void setBinCount(double m_binCount) override
Set the # of counts in the bin at its peak.
Structure describing a single-crystal peak.
Geometry::Instrument_const_sptr getInstrument() const
Return a shared ptr to the instrument for this peak.
void setWavelength(double wavelength) override
Set the incident wavelength of the neutron.
double getL1() const override
Return the L1 flight path length (source to sample), in meters.
double getInitialEnergy() const override
Get the initial (incident) neutron energy in meV.
Mantid::Kernel::V3D getQLabFrame() const override
Return the Q change (of the lattice, k_i - k_f) for this peak.
int getDetectorID() const override
Get the ID of the detector at the center of the peak
double getWavelength() const override
Calculate the neutron wavelength (in angstroms) at the peak (Note for inelastic scattering - it is th...
double getTOF() const override
Calculate the time of flight (in microseconds) of the neutrons for this peak, using the geometry of t...
virtual Mantid::Kernel::V3D getDetPos() const
Return the detector position vector.
Mantid::Kernel::V3D getQSampleFrame() const override
Return the Q change (of the lattice, k_i - k_f) for this peak.
double getL2() const override
Return the L2 flight path length (sample to detector), in meters.
double getScattering() const override
Calculate the scattering angle of the peak
The class PeaksWorkspace stores information about a set of SCD peaks.
Class to represent a particular goniometer setting, which is described by the rotation matrix.
void setRotationAngle(const std::string &name, double value)
Set rotation angle for an axis using motor name.
std::vector< double > getEulerAngles(const std::string &convention="YZX")
Return Euler angles acording to a convention.
void makeUniversalGoniometer()
Make a default universal goniometer with phi,chi,omega angles according to SNS convention.
const Kernel::DblMatrix & getR() const
Return global rotation matrix.
Structure describing a single-crystal peak.
virtual double getTOF() const =0
virtual double getWavelength() const =0
virtual double getL2() const =0
virtual int getRunNumber() const =0
virtual double getL1() const =0
The Logger class is in charge of the publishing messages from the framework through various channels.
void debug(const std::string &msg)
Logs at debug level.
T Invert()
LU inversion routine.
void zeroMatrix()
Set the matrix to zero.
void initialize(const double &_l1, const int &_emode, const UnitParametersMap ¶ms)
Initialize the unit to perform conversion using singleToTof() and singleFromTof()
double singleFromTOF(const double tof) const override
Convert a single tof value to this unit.
double norm() const noexcept
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< PeaksWorkspace > PeaksWorkspace_sptr
Typedef for a shared pointer to a peaks workspace.
The namespace for concrete units classes.
MANTID_KERNEL_DLL V3D normalize(V3D v)
Normalizes a V3D.
std::string to_string(const wide_integer< Bits, Signed > &n)