Newer
Older
Savici, Andrei T.
committed
#include "MantidAlgorithms/ApplyDetailedBalance.h"
#include "MantidAPI/WorkspaceUnitValidator.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/PhysicalConstants.h"
Savici, Andrei T.
committed
#include "MantidKernel/PropertyWithValue.h"
#include "MantidKernel/TimeSeriesProperty.h"
Savici, Andrei T.
committed
#include "boost/lexical_cast.hpp"
#include <cmath>
using std::string;
using namespace Mantid::Kernel;
using namespace Mantid::API;
namespace Mantid {
namespace Algorithms {
Savici, Andrei T.
committed
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ApplyDetailedBalance)
Savici, Andrei T.
committed
//----------------------------------------------------------------------------------------------
/** Constructor
*/
ApplyDetailedBalance::ApplyDetailedBalance() {}
Savici, Andrei T.
committed
//----------------------------------------------------------------------------------------------
/** Destructor
*/
ApplyDetailedBalance::~ApplyDetailedBalance() {}
Savici, Andrei T.
committed
//----------------------------------------------------------------------------------------------
Savici, Andrei T.
committed
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void ApplyDetailedBalance::init() {
auto wsValidator = boost::make_shared<CompositeValidator>();
wsValidator->add<WorkspaceUnitValidator>("DeltaE");
declareProperty(new WorkspaceProperty<>("InputWorkspace", "",
Direction::Input, wsValidator),
"An input workspace.");
declareProperty(
new WorkspaceProperty<>("OutputWorkspace", "", Direction::Output),
"An output workspace.");
declareProperty(
new PropertyWithValue<string>("Temperature", "", Direction::Input),
"SampleLog variable name that contains the temperature, or a number");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void ApplyDetailedBalance::exec() {
MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
// If input and output workspaces are not the same, create a new workspace for
// the output
if (outputWS != inputWS) {
outputWS = API::WorkspaceFactory::Instance().create(inputWS);
}
Savici, Andrei T.
committed
std::string Tstring = getProperty("Temperature");
double Temp;
try {
if (inputWS->run().hasProperty(Tstring)) {
if (auto log = dynamic_cast<Kernel::TimeSeriesProperty<double> *>(
inputWS->run().getProperty(Tstring))) {
Temp = log->getStatistics().mean;
} else {
throw std::invalid_argument(Tstring + " is not a double-valued log.");
}
} else {
Temp = boost::lexical_cast<double>(Tstring);
} catch (...) {
Tstring += " is not a valid log, nor is it a number";
throw std::invalid_argument(Tstring);
Savici, Andrei T.
committed
}
double oneOverT = PhysicalConstants::meVtoKelvin / Temp;
// Run the exponential correction algorithm explicitly to enable progress
// reporting
IAlgorithm_sptr expcor =
createChildAlgorithm("OneMinusExponentialCor", 0.0, 1.0);
expcor->setProperty<MatrixWorkspace_sptr>("InputWorkspace", inputWS);
expcor->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", outputWS);
expcor->setProperty<double>("C1", M_PI);
expcor->setProperty<double>("C", oneOverT);
expcor->setPropertyValue("Operation", "Multiply");
expcor->executeAsChildAlg();
// Get back the result
outputWS = expcor->getProperty("OutputWorkspace");
Savici, Andrei T.
committed
setProperty("OutputWorkspace", outputWS);
}
Savici, Andrei T.
committed
} // namespace Mantid
} // namespace Algorithms