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 (auto log = dynamic_cast<Kernel::TimeSeriesProperty<double> *>(inputWS->run().getProperty(Tstring))) {
62 Temp = log->getStatistics().mean;
63 } else {
64 throw std::invalid_argument(Tstring + " is not a double-valued log.");
65 }
66 } else {
67 Temp = boost::lexical_cast<double>(Tstring);
68 }
69 } catch (...) {
70 Tstring += " is not a valid log, nor is it a number";
71 throw std::invalid_argument(Tstring);
72 }
73
74 double oneOverT = PhysicalConstants::meVtoKelvin / Temp;
75 // Run the exponential correction algorithm explicitly to enable progress
76 // reporting
77 auto expcor = createChildAlgorithm("OneMinusExponentialCor", 0.0, 1.0);
78 expcor->setProperty<MatrixWorkspace_sptr>("InputWorkspace", inputWS);
79 expcor->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", outputWS);
80 expcor->setProperty<double>("C1", M_PI);
81 expcor->setProperty<double>("C", oneOverT);
82 expcor->setPropertyValue("Operation", "Multiply");
83 expcor->executeAsChildAlg();
84 // Get back the result
85 outputWS = expcor->getProperty("OutputWorkspace");
86
87 // Select the unit, transform if different than energy
88 std::string unit = getProperty("OutputUnits");
89 if (unit == "Frequency") {
90 auto convert = createChildAlgorithm("ConvertUnits");
91 convert->setProperty<MatrixWorkspace_sptr>("InputWorkspace", outputWS);
92 convert->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", outputWS);
93 convert->setProperty<std::string>("Target", "DeltaE_inFrequency");
94 convert->executeAsChildAlg();
95 }
96 setProperty("OutputWorkspace", outputWS);
97}
98
99} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
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.
Definition: Algorithm.cpp:842
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