Mantid
Loading...
Searching...
No Matches
ApplyDetailedBalance.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 "MantidAPI/Run.h"
16
17#include "boost/lexical_cast.hpp"
18#include <cmath>
19
20using std::string;
21using namespace Mantid::Kernel;
22using namespace Mantid::API;
23using namespace Mantid::DataObjects;
24
25namespace Mantid::Algorithms {
26
27// Register the algorithm into the AlgorithmFactory
28DECLARE_ALGORITHM(ApplyDetailedBalance)
29
30
33 auto wsValidator = std::make_shared<CompositeValidator>();
34 wsValidator->add<WorkspaceUnitValidator>("DeltaE");
35 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input, wsValidator),
36 "An input workspace.");
37 declareProperty(std::make_unique<PropertyWithValue<string>>("Temperature", "", Direction::Input),
38 "SampleLog variable name that contains the temperature, or a number");
39 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
40 "An output workspace.");
41 std::vector<std::string> unitOptions{"Energy", "Frequency"};
42 declareProperty("OutputUnits", "Energy", std::make_shared<StringListValidator>(unitOptions),
43 "Susceptibility as a function of energy (meV) or frequency (GHz)");
44}
45
49 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
50 MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
51 // If input and output workspaces are not the same, create a new workspace for
52 // the output
53 if (outputWS != inputWS) {
54 outputWS = create<MatrixWorkspace>(*inputWS);
55 }
56
57 std::string Tstring = getProperty("Temperature");
58 double Temp;
59 try {
60 if (inputWS->run().hasProperty(Tstring)) {
61 if (const auto log =
62 dynamic_cast<const Kernel::TimeSeriesProperty<double> *>(inputWS->run().getProperty(Tstring))) {
63 Temp = log->getStatistics().mean;
64 } else {
65 throw std::invalid_argument(Tstring + " is not a double-valued log.");
66 }
67 } else {
68 Temp = boost::lexical_cast<double>(Tstring);
69 }
70 } catch (...) {
71 Tstring += " is not a valid log, nor is it a number";
72 throw std::invalid_argument(Tstring);
73 }
74
75 double oneOverT = PhysicalConstants::meVtoKelvin / Temp;
76 // Run the exponential correction algorithm explicitly to enable progress
77 // reporting
78 auto expcor = createChildAlgorithm("OneMinusExponentialCor", 0.0, 1.0);
79 expcor->setProperty<MatrixWorkspace_sptr>("InputWorkspace", inputWS);
80 expcor->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", outputWS);
81 expcor->setProperty<double>("C1", M_PI);
82 expcor->setProperty<double>("C", oneOverT);
83 expcor->setPropertyValue("Operation", "Multiply");
84 expcor->executeAsChildAlg();
85 // Get back the result
86 outputWS = expcor->getProperty("OutputWorkspace");
87
88 // Select the unit, transform if different than energy
89 std::string unit = getProperty("OutputUnits");
90 if (unit == "Frequency") {
91 auto convert = createChildAlgorithm("ConvertUnits");
92 convert->setProperty<MatrixWorkspace_sptr>("InputWorkspace", outputWS);
93 convert->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", outputWS);
94 convert->setProperty<std::string>("Target", "DeltaE_inFrequency");
95 convert->executeAsChildAlg();
96 }
97 setProperty("OutputWorkspace", outputWS);
98}
99
100} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
A property class for workspaces.
A validator which checks that the unit of the workspace referred to by a WorkspaceProperty is the exp...
ApplyDetailedBalance : The algorithm calculates the imaginary part of the dynamic susceptibility chi'...
void exec() override
Run the algorithm.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
The concrete, templated class for properties.
A specialised Property class for holding a series of time-value pairs.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
static constexpr double meVtoKelvin
1 meV in Kelvin.
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54