Mantid
Loading...
Searching...
No Matches
UnitConversion.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8#include "MantidKernel/Unit.h"
10
11#include <cmath>
12#include <stdexcept>
13
14namespace Mantid::Kernel {
28double UnitConversion::run(const std::string &src, const std::string &dest, const double srcValue, const double l1,
29 const double l2, const double theta, const DeltaEMode::Type emode, const double efixed) {
30 Unit_sptr srcUnit = UnitFactory::Instance().create(src);
31 Unit_sptr destUnit = UnitFactory::Instance().create(dest);
32 if ((srcUnit->unitID() == "dSpacing") || (destUnit->unitID() == "dSpacing")) {
33 throw std::runtime_error("This signature is deprecated for d Spacing unit conversions");
34 }
36 return UnitConversion::run(*srcUnit, *destUnit, srcValue, l1, emode, params);
37} // namespace Kernel
38
39double UnitConversion::run(const std::string &src, const std::string &dest, const double srcValue, const double l1,
40 const DeltaEMode::Type emode, const UnitParametersMap &params) {
41 Unit_sptr srcUnit = UnitFactory::Instance().create(src);
42 Unit_sptr destUnit = UnitFactory::Instance().create(dest);
43 return UnitConversion::run(*srcUnit, *destUnit, srcValue, l1, emode, params);
44}
45
60double UnitConversion::run(Unit &srcUnit, Unit &destUnit, const double srcValue, const double l1,
61 const DeltaEMode::Type emode, const UnitParametersMap &params) {
62 double factor(0.0), power(0.0);
63 if (srcUnit.quickConversion(destUnit, factor, power)) {
64 return convertQuickly(srcValue, factor, power);
65 } else {
66 return convertViaTOF(srcUnit, destUnit, srcValue, l1, emode, params);
67 }
68}
69
70//---------------------------------------------------------------------------------------------
71// Private methods
72//---------------------------------------------------------------------------------------------
73
82double UnitConversion::convertQuickly(const double srcValue, const double factor, const double power) {
83 return factor * std::pow(srcValue, power);
84}
85
99double UnitConversion::convertViaTOF(Unit &srcUnit, Unit &destUnit, const double srcValue, const double l1,
100 const DeltaEMode::Type emode, const UnitParametersMap &params) {
101 // Translate the emode to the int formulation
102 int emodeAsInt(0);
103 switch (emode) {
105 emodeAsInt = 0;
106 break;
108 emodeAsInt = 1;
109 break;
111 emodeAsInt = 2;
112 break;
113 default:
114 throw std::invalid_argument("UnitConversion::convertViaTOF - Unknown emode " + std::to_string(emode));
115 };
116
117 const double tof = srcUnit.convertSingleToTOF(srcValue, l1, emodeAsInt, params);
118 return destUnit.convertSingleFromTOF(tof, l1, emodeAsInt, params);
119}
120
128double UnitConversion::convertToElasticQ(const double theta, const double efixed) {
129
131 double wavelengthFactor(0.0), wavelengthPower(0.0);
132 energyUnit.quickConversion("Wavelength", wavelengthFactor, wavelengthPower);
133
134 const double stheta = std::sin(theta);
135 // Calculate the wavelength to allow it to be used to convert to elasticQ.
136 double wavelength = wavelengthFactor * std::pow(efixed, wavelengthPower);
137 // The MomentumTransfer value.
138 return 4.0 * M_PI * stheta / wavelength;
139}
140
141} // namespace Mantid::Kernel
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
static double convertQuickly(const double srcValue, const double factor, const double power)
Perform a quick conversion.
static double convertViaTOF(Unit &srcUnit, Unit &destUnit, const double srcValue, const double l1, const DeltaEMode::Type emode, const UnitParametersMap &params)
Convert through TOF.
static double convertToElasticQ(const double theta, const double efixed)
Convert to ElasticQ from Energy.
static double run(const std::string &src, const std::string &dest, const double srcValue, const double l1, const double l2, const double theta, const DeltaEMode::Type emode, const double efixed)
Convert a single value between the given units (as strings)
The base units (abstract) class.
Definition: Unit.h:41
double convertSingleToTOF(const double xvalue, const double &l1, const int &emode, const UnitParametersMap &params)
Convert from the concrete unit to time-of-flight.
Definition: Unit.cpp:168
double convertSingleFromTOF(const double xvalue, const double &l1, const int &emode, const UnitParametersMap &params)
Convert from the time-of-flight to the concrete unit.
Definition: Unit.cpp:198
bool quickConversion(const Unit &destination, double &factor, double &power) const
Is conversion by constant multiplication possible?
Definition: Unit.cpp:62
Energy in milli-electronvolts.
Definition: Unit.h:331
std::unordered_map< UnitParams, double > UnitParametersMap
Definition: Unit.h:30
std::shared_ptr< Unit > Unit_sptr
Shared pointer to the Unit base class.
Definition: Unit.h:229
std::string to_string(const wide_integer< Bits, Signed > &n)
Type
Define the available energy transfer modes It is important to assign enums proper numbers,...
Definition: DeltaEMode.h:29