Skip to content

Commit

Permalink
Refs #9095. Started to implement algorith.
Browse files Browse the repository at this point in the history
1. Add files;
2. Modify CMakeList;
3. Checkpointing work in progress
  • Loading branch information
wdzhou committed May 14, 2014
1 parent ad8571c commit 1819a44
Show file tree
Hide file tree
Showing 4 changed files with 337 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ set ( SRC_FILES
src/EQSANSTofStructure.cpp
src/EditInstrumentGeometry.cpp
src/ElasticWindow.cpp
src/EstimatePDDetectorResolution.cpp
src/Exponential.cpp
src/ExponentialCorrection.cpp
src/ExportTimeSeriesLog.cpp
Expand Down Expand Up @@ -310,6 +311,7 @@ set ( INC_FILES
inc/MantidAlgorithms/EQSANSTofStructure.h
inc/MantidAlgorithms/EditInstrumentGeometry.h
inc/MantidAlgorithms/ElasticWindow.h
inc/MantidAlgorithms/EstimatePDDetectorResolution.h
inc/MantidAlgorithms/Exponential.h
inc/MantidAlgorithms/ExponentialCorrection.h
inc/MantidAlgorithms/ExportTimeSeriesLog.h
Expand Down Expand Up @@ -546,6 +548,7 @@ set ( TEST_FILES
DiffractionFocussingTest.h
DivideTest.h
EditInstrumentGeometryTest.h
EstimatePDDetectorResolutionTest.h
ExponentialCorrectionTest.h
ExponentialTest.h
ExportTimeSeriesLogTest.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#ifndef MANTID_ALGORITHMS_ESTIMATEPDDETECTORRESOLUTION_H_
#define MANTID_ALGORITHMS_ESTIMATEPDDETECTORRESOLUTION_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidGeometry/Instrument.h"

namespace Mantid
{
namespace Algorithms
{
/** EstimatePDDetectorResolution : TODO: DESCRIPTION
Copyright © 2014 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport EstimatePDDetectorResolution : public API::Algorithm
{
public:
EstimatePDDetectorResolution();
virtual ~EstimatePDDetectorResolution();

/// Algorithm's name for identification overriding a virtual method
virtual const std::string name() const { return "EstimatePDDetectorResolution";}
/// Algorithm's version for identification overriding a virtual method
virtual int version() const { return 1;}
/// Algorithm's category for identification overriding a virtual method
virtual const std::string category() const { return "Diffraction";}

private:
/// Sets documentation strings for this algorithm
virtual void initDocs();
/// Implement abstract Algorithm methods
void init();
/// Implement abstract Algorithm methods
void exec();

///
void processAlgProperties();

///
void retrieveInstrumentParameters();

/// Create output workspace
void createOutputWorkspace();

/// Calculate detector resolution
void estimateDetectorResolution();


//------------------------------------------------------

/// Input workspace
API::MatrixWorkspace_sptr m_inputWS;

/// Output workspace
API::MatrixWorkspace_sptr m_outputWS;

API::MatrixWorkspace_sptr m_solidangleWS;

/// Centre neutron velocity
double m_centreVelocity;

};


} // namespace Algorithms
} // namespace Mantid

#endif /* MANTID_ALGORITHMS_ESTIMATEPDDETECTORRESOLUTION_H_ */
182 changes: 182 additions & 0 deletions Code/Mantid/Framework/Algorithms/src/EstimatePDDetectorResolution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*WIKI*
*WIKI*/
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAlgorithms/EstimatePDDetectorResolution.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/V3D.h"

using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;

using namespace std;

namespace Mantid
{
namespace Algorithms
{

DECLARE_ALGORITHM(EstimatePDDetectorResolution)


//----------------------------------------------------------------------------------------------
/** Constructor
*/
EstimatePDDetectorResolution::EstimatePDDetectorResolution()
{
}

//----------------------------------------------------------------------------------------------
/** Destructor
*/
EstimatePDDetectorResolution::~EstimatePDDetectorResolution()
{
}

//----------------------------------------------------------------------------------------------
/**
*/
void EstimatePDDetectorResolution::initDocs()
{
setWikiSummary("Estimate the resolution of each detector for a powder diffractometer. ");
setOptionalMessage("Estimate the resolution of each detector for a powder diffractometer. ");
}

//----------------------------------------------------------------------------------------------
void EstimatePDDetectorResolution::init()
{
declareProperty(new WorkspaceProperty<MatrixWorkspace>("InputWorkspace", "", Direction::Input),
"Name of the workspace to have detector resolution calculated. ");

declareProperty(new WorkspaceProperty<MatrixWorkspace>("OutputWorkspace", "", Direction::Output),
"Name of the output workspace containing delta(d)/d of each detector/spectrum. ");
}

//----------------------------------------------------------------------------------------------
/**
*/
void EstimatePDDetectorResolution::exec()
{
processAlgProperties();

retrieveInstrumentParameters();

createOutputWorkspace();

estimateDetectorResolution();

setProperty("OutputWorkspace", m_outputWS);
}

//----------------------------------------------------------------------------------------------
/**
*/
void EstimatePDDetectorResolution::processAlgProperties()
{
m_inputWS = getProperty("InputWorkspace");

}

//----------------------------------------------------------------------------------------------
/**
*/
void EstimatePDDetectorResolution::retrieveInstrumentParameters()
{
// Call SolidAngle to get solid angles for all detectors
Algorithm_sptr calsolidangle = createChildAlgorithm("SolidAngle", -1, -1, true);
calsolidangle->initialize();

calsolidangle->setProperty("InputWorkspace", m_inputWS);

calsolidangle->execute();
if (!calsolidangle->isExecuted())
throw runtime_error("Unable to run solid angle. ");

m_solidangleWS = calsolidangle->getProperty("OutputWorkspace");
if (!m_solidangleWS)
throw runtime_error("Unable to get solid angle workspace from SolidAngle(). ");


size_t numspec = m_solidangleWS->getNumberHistograms();
for (size_t i = 0; i < numspec; ++i)
g_log.debug() << "[DB]: " << m_solidangleWS->readY(i)[0] << "\n";

// Calculate centre neutron velocity
Property* cwlproperty = m_inputWS->run().getProperty("LambdaRequest");
if (!cwlproperty)
throw runtime_error("Unable to locate property LambdaRequest as central wavelength. ");
TimeSeriesProperty<double>* cwltimeseries = dynamic_cast<TimeSeriesProperty<double>* >(cwlproperty);
if (!cwltimeseries)
throw runtime_error("LambdaReqeust is not a TimeSeriesProperty in double. ");
if (cwltimeseries->size() != 1)
throw runtime_error("LambdaRequest should contain 1 and only 1 entry. ");

double centrewavelength = cwltimeseries->nthValue(0);
string unit = cwltimeseries->units();
if (unit.compare("Angstrom") == 0)
centrewavelength *= 1.0E-10;
else
throw runtime_error("Unit is not recognized");

m_centreVelocity = PhysicalConstants::h/PhysicalConstants::NeutronMass/centrewavelength;
g_log.notice() << "Centre wavelength = " << centrewavelength << ", Centre neutron velocity = " << m_centreVelocity << "\n";

// Calcualte L1 sample to source
Instrument_const_sptr instrument = m_inputWS->getInstrument();
V3D samplepos = instrument->getSample()->getPos();
V3D sourcepos = instrument->getSource()->getPos();
double m_L1 = samplepos.distance(sourcepos);
g_log.notice() << "L1 = " << m_L1 << "\n";

return;
}

//----------------------------------------------------------------------------------------------
/**
*/
void EstimatePDDetectorResolution::createOutputWorkspace()
{
size_t numspec = m_inputWS->getNumberHistograms();

m_outputWS = boost::dynamic_pointer_cast<MatrixWorkspace>(
WorkspaceFactory::Instance().create("Workspace2D", numspec, 1, 1));

return;
}
//----------------------------------------------------------------------------------------------
/**
*/
void EstimatePDDetectorResolution::estimateDetectorResolution()
{
size_t numspec = m_inputWS->getNumberHistograms();
for (size_t i = 0; i < numspec; ++i)
{
// Get the distance from















}

return;
}

} // namespace Algorithms
} // namespace Mantid
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef MANTID_ALGORITHMS_ESTIMATEPDDETECTORRESOLUTIONTEST_H_
#define MANTID_ALGORITHMS_ESTIMATEPDDETECTORRESOLUTIONTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidAlgorithms/EstimatePDDetectorResolution.h"
#include "MantidDataHandling/LoadNexusProcessed.h"

using Mantid::Algorithms::EstimatePDDetectorResolution;

using namespace Mantid::API;
using namespace Mantid::Kernel;

class EstimatePDDetectorResolutionTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static EstimatePDDetectorResolutionTest *createSuite() { return new EstimatePDDetectorResolutionTest(); }
static void destroySuite( EstimatePDDetectorResolutionTest *suite ) { delete suite; }


/** Test init
*/
void test_Init()
{
EstimatePDDetectorResolution alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize());
TS_ASSERT(alg.isInitialized());
}

/** Test POWGEN
*/
void test_PG3()
{
// Load data file
Mantid::DataHandling::LoadNexusProcessed loader;
loader.initialize();
loader.setProperty("Filename","PG3_2538_meta.nxs");
loader.setProperty("OutputWorkspace", "PG3_2538");
loader.execute();

// Set up and run
EstimatePDDetectorResolution alg;
alg.initialize();

TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("InputWorkspace", "PG3_2538"));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "PG3_Resolution"));

alg.execute();
TS_ASSERT(alg.isExecuted());



AnalysisDataService::Instance().remove("PG3_2538");

TS_ASSERT_EQUALS(1, 3);
}


};


#endif /* MANTID_ALGORITHMS_ESTIMATEPDDETECTORRESOLUTIONTEST_H_ */

0 comments on commit 1819a44

Please sign in to comment.