19#include <boost/algorithm/string.hpp>
24using namespace CurveFitting;
25using namespace Constraints;
31using namespace Geometry;
33using namespace Kernel;
37 :
ParamFunction(), m_latticeSystem(
PointGroup::LatticeSystem::Triclinic), m_profileFunctionCenterParameterName() {}
49 if (attName ==
"LatticeSystem") {
51 }
else if (attName ==
"ProfileFunction") {
55 ParamFunction::setAttribute(attName, attValue);
64 case PointGroup::LatticeSystem::Cubic: {
68 uc.
setError(aErr, aErr, aErr, 0.0, 0.0, 0.0);
71 case PointGroup::LatticeSystem::Tetragonal: {
78 case PointGroup::LatticeSystem::Hexagonal: {
85 case PointGroup::LatticeSystem::Rhombohedral: {
90 UnitCell uc(a, a, a, alpha, alpha, alpha);
91 uc.
setError(aErr, aErr, aErr, alphaErr, alphaErr, alphaErr);
94 case PointGroup::LatticeSystem::Orthorhombic: {
99 case PointGroup::LatticeSystem::Monoclinic: {
104 case PointGroup::LatticeSystem::Triclinic: {
122 }
catch (
const std::invalid_argument &) {
128 }
catch (
const std::invalid_argument &) {
134 }
catch (
const std::invalid_argument &) {
139 }
catch (
const std::invalid_argument &) {
144 }
catch (
const std::invalid_argument &) {
184 throw std::invalid_argument(
"PawleyFunction can only use IPeakFunctions to "
185 "calculate peak profiles.");
213 switch (latticeSystem) {
214 case PointGroup::LatticeSystem::Cubic:
219 case PointGroup::LatticeSystem::Hexagonal:
220 case PointGroup::LatticeSystem::Tetragonal:
227 case PointGroup::LatticeSystem::Orthorhombic:
236 case PointGroup::LatticeSystem::Monoclinic:
248 case PointGroup::LatticeSystem::Rhombohedral:
278 auto cellEdgeConstraint = std::make_unique<BoundaryConstraint>(
this,
parameterName, 0.0,
true);
279 cellEdgeConstraint->setPenaltyFactor(1e12);
285 auto cellAngleConstraint = std::make_unique<BoundaryConstraint>(
this,
parameterName, 0.0, 180.0,
true);
286 cellAngleConstraint->setPenaltyFactor(1e12);
293 if (profileFunction) {
302 :
IPawleyFunction(), m_compositeFunction(), m_pawleyParameterFunction(), m_peakProfileComposite(), m_hkls(),
303 m_dUnit(), m_wsUnit(), m_peakRadius(5) {
305 m_peakRadius = peakRadius.get_value_or(5);
314 if (std::dynamic_pointer_cast<Units::Empty>(wsUnit) || std::dynamic_pointer_cast<Units::dSpacing>(wsUnit)) {
317 double factor, power;
318 if (wsUnit->quickConversion(*
m_dUnit, factor, power)) {
321 throw std::invalid_argument(
"Can not use quick conversion for unit.");
350 newFunction->setCentre(oldFunction->centre());
352 newFunction->setFwhm(oldFunction->fwhm());
356 newFunction->setHeight(oldFunction->height());
380 for (
size_t i = 0; i <
m_hkls.size(); ++i) {
389 size_t domainSize = domain.
size();
391 const double *domainEnd = domain.
getPointerAt(domainSize);
393 double centre = peak->centre();
396 auto lb = std::lower_bound(domainBegin, domainEnd, centre - dx);
397 auto ub = std::upper_bound(lb, domainEnd, centre + dx);
399 size_t n = std::distance(lb, ub);
402 throw std::invalid_argument(
"Null-domain");
406 localValues.
reset(localDomain);
410 return std::distance(domainBegin, lb);
444 }
catch (
const std::invalid_argument &) {
450 }
catch (
const std::bad_cast &) {
468 for (
const auto &hkl : hkls) {
500 throw std::out_of_range(
"Peak index out of range.");
509 throw std::out_of_range(
"Peak index out of range.");
522 throw std::runtime_error(
"PawleyFunction could not construct internal CompositeFunction.");
533 throw std::invalid_argument(
"PawleyFunction only works with "
534 "CompositeFunction. Selecting another "
535 "decorated function is not possible.");
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
IPeaksWorkspace_sptr workspace
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Class to represent the axis of a workspace.
const std::shared_ptr< Kernel::Unit > & unit() const
The unit for this axis.
1D domain - a wrapper around an array of doubles.
Represent a domain for functions of one real argument.
size_t size() const override
Return the number of arguments in the domain.
const double * getPointerAt(size_t i) const
Get a pointer to i-th value.
Base class that represents the domain of a function.
void setDecoratedFunction(const std::string &wrappedFunctionName)
IFunction_sptr m_wrappedFunction
A class to store values calculated by a function.
void addToCalculated(size_t i, double value)
Add a number to a calculated value.
void zeroCalculated()
Set all calculated values to zero.
double * getPointerToCalculated(size_t i)
Get a pointer to calculated data at index i.
void reset(const FunctionDomain &domain)
Reset the values to match a new domain.
Attribute is a non-fitting parameter.
std::string asString() const
Returns string value if attribute is a string, throws exception otherwise.
virtual double getParameter(size_t i) const =0
Get i-th parameter.
void declareAttribute(const std::string &name, const API::IFunction::Attribute &defaultValue)
Declare a single attribute.
virtual void setParameter(size_t, const double &value, bool explicitlySet=true)=0
Set i-th parameter.
virtual std::string parameterName(size_t i) const =0
Returns the name of parameter i.
virtual double getError(size_t i) const =0
Get the fitting error for a parameter.
virtual void addConstraint(std::unique_ptr< IConstraint > ic)
Add a constraint to function.
virtual void declareParameter(const std::string &name, double initValue=0, const std::string &description="")=0
Declare a new parameter.
Represents the Jacobian in IFitFunction::functionDeriv.
Implements the part of IFunction interface dealing with parameters.
void clearAllParameters()
Nonvirtual member which removes all declared parameters.
The Pawley approach to obtain lattice parameters from a powder diffractogram works by placing peak pr...
PawleyParameterFunction_sptr m_pawleyParameterFunction
API::CompositeFunction_sptr m_compositeFunction
Kernel::V3D getPeakHKL(size_t i) const override
Return the HKL of the i-th peak.
PawleyParameterFunction_sptr getPawleyParameterFunction() const
Returns the internally stored PawleyParameterFunction.
void init() override
Does nothing.
Kernel::Unit_sptr m_dUnit
API::CompositeFunction_sptr m_peakProfileComposite
size_t getPeakCount() const override
Returns the number of peaks that are stored in the function.
API::IPeakFunction_sptr getPeakFunction(size_t i) const override
Returns the profile function stored for the i-th peak.
void function(const API::FunctionDomain &domain, API::FunctionValues &values) const override
Calculates the function values on the supplied domain.
size_t calculateFunctionValues(const API::IPeakFunction_sptr &peak, const API::FunctionDomain1D &domain, API::FunctionValues &localValues) const
std::vector< Kernel::V3D > m_hkls
void addPeak(const Kernel::V3D &hkl, double fwhm, double height) override
Adds a peak with the supplied FWHM and height.
double getTransformedCenter(double d) const
Transform d value to workspace unit.
void beforeDecoratedFunctionSet(const API::IFunction_sptr &fn) override
Checks that the decorated function has the correct structure.
void clearPeaks() override
Removes all peaks from the function.
void setUnitCell(const std::string &unitCellString) override
Sets the unit cell from a string with either 6 or 3 space-separated numbers.
void setLatticeSystem(const std::string &latticeSystem) override
Sets the crystal system on the internal parameter function and updates the exposed parameters.
void setPeakPositions(const std::string ¢reName, double zeroShift, const Geometry::UnitCell &cell) const
void setPeaks(const std::vector< Kernel::V3D > &hkls, double fwhm, double height) override
Clears peaks and adds a peak for each hkl, all with the same FWHM and height.
void setMatrixWorkspace(std::shared_ptr< const API::MatrixWorkspace > workspace, size_t wi, double startX, double endX) override
Set matrix workspace.
void setProfileFunction(const std::string &profileFunction) override
Sets the profile function and replaces already existing functions in the internally stored CompositeF...
Kernel::Unit_sptr m_wsUnit
void setProfileFunction(const std::string &profileFunction)
Sets the profile function.
void addAngleConstraint(const std::string ¶meterName)
Adds a default constraint so cell angles are in the range 0 to 180.
void setAttribute(const std::string &attName, const Attribute &attValue) override
Sets the supplied attribute value.
std::string m_profileFunctionCenterParameterName
void setParametersFromUnitCell(const Geometry::UnitCell &cell)
Sets the function's parameters from the supplied UnitCell.
void createLatticeSystemParameters(Geometry::PointGroup::LatticeSystem latticeSystem)
This method clears all parameters and declares parameters according to the supplied crystal system.
Geometry::PointGroup::LatticeSystem getLatticeSystem() const
Returns the crystal system.
Geometry::PointGroup::LatticeSystem m_latticeSystem
Geometry::UnitCell getUnitCellFromParameters() const
Returns a UnitCell object constructed from the function's parameters.
void function(const API::FunctionDomain &domain, API::FunctionValues &values) const override
This method does nothing.
void init() override
Declares attributes and generates parameters based on the defaults.
void setLatticeSystem(const std::string &latticeSystem)
Assigns the lattice system.
void addLengthConstraint(const std::string ¶meterName)
Adds a default constraint so that cell edge lengths can not be less than 0.
void functionDeriv(const API::FunctionDomain &domain, API::Jacobian &jacobian) override
This method does nothing.
PawleyParameterFunction()
Constructor.
void setCenterParameterNameFromFunction(const API::IPeakFunction_sptr &profileFunction)
Tries to extract and store the center parameter name from the function.
A class containing the Point Groups for a crystal.
Class to implement unit cell of crystals.
double alpha() const
Get lattice parameter.
double a(int nd) const
Get lattice parameter a1-a3 as function of index (0-2)
double c() const
Get lattice parameter.
double d(double h, double k, double l) const
Return d-spacing ( ) for a given h,k,l coordinate.
double beta() const
Get lattice parameter.
void setError(double _aerr, double _berr, double _cerr, double _alphaerr, double _betaerr, double _gammaerr, const int angleunit=angDegrees)
Set lattice parameter errors.
double b() const
Get lattice parameter.
double gamma() const
Get lattice parameter.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
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)
std::shared_ptr< IPeakFunction > IPeakFunction_sptr
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
std::shared_ptr< CompositeFunction > CompositeFunction_sptr
shared pointer to the composite function base class
std::shared_ptr< PawleyParameterFunction > PawleyParameterFunction_sptr
MANTID_GEOMETRY_DLL PointGroup::LatticeSystem getLatticeSystemFromString(const std::string &latticeSystem)
Returns the lattice system enum that corresponds to the supplied string or throws an invalid_argument...
MANTID_GEOMETRY_DLL UnitCell strToUnitCell(const std::string &unitCellString)
std::shared_ptr< Unit > Unit_sptr
Shared pointer to the Unit base class.