Mantid
Loading...
Searching...
No Matches
IsotropicAtomBraggScatterer.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 +
9#include "MantidJson/Json.h"
10#include "MantidKernel/Atom.h"
14
15#include <algorithm>
16#include <boost/algorithm/string.hpp>
17#include <json/json.h>
18#include <stdexcept>
19#include <utility>
20
21namespace Mantid::Geometry {
22
23using namespace Kernel;
24
28
31 IsotropicAtomBraggScatterer_sptr clone = std::make_shared<IsotropicAtomBraggScatterer>();
32 clone->initialize();
33 clone->setProperties(this->asString(false));
34
35 return clone;
36}
37
40void IsotropicAtomBraggScatterer::setElement(const std::string &element) {
42
43 m_atom = atom.neutron;
44 m_label = element;
45}
46
48double IsotropicAtomBraggScatterer::getOccupancy() const { return getProperty("Occupancy"); }
49
51double IsotropicAtomBraggScatterer::getU() const { return getProperty("U"); }
52
63 double amplitude = getOccupancy() * getDebyeWallerFactor(hkl) * getScatteringLength();
64
65 double phase = 2.0 * M_PI * m_position.scalar_prod(hkl);
66
67 return amplitude * StructureFactor(cos(phase), sin(phase));
68}
69
83 // Default behavior requires this.
84 setElement("H");
85
86 std::shared_ptr<BoundedValidator<double>> uValidator = std::make_shared<BoundedValidator<double>>();
87 uValidator->setLower(0.0);
88
89 declareProperty(std::make_unique<PropertyWithValue<double>>("U", 0.0, uValidator),
90 "Isotropic atomic displacement in Angstrom^2");
91
92 IValidator_sptr occValidator = std::make_shared<BoundedValidator<double>>(0.0, 1.0);
93 declareProperty(std::make_unique<PropertyWithValue<double>>("Occupancy", 1.0, occValidator),
94 "Site occupancy, values on interval [0,1].");
95
97 "Element", "H", std::make_shared<MandatoryValidator<std::string>>()));
98}
99
101void IsotropicAtomBraggScatterer::afterScattererPropertySet(const std::string &propertyName) {
102 if (propertyName == "Element") {
103 setElement(getPropertyValue(propertyName));
104 }
105}
106
110 V3D dstar = getCell().getB() * hkl;
111
112 return exp(-2.0 * M_PI * M_PI * getU() * dstar.norm2());
113}
114
117
119
120
132 : m_scattererString(std::move(scattererString)) {}
133
135std::vector<BraggScatterer_sptr> IsotropicAtomBraggScattererParser::operator()() const {
137 std::vector<BraggScatterer_sptr> scatterers;
138 scatterers.reserve(tokens.size());
139 std::transform(tokens.cbegin(), tokens.cend(), std::back_inserter(scatterers),
140 [this](const auto &token) { return getScatterer(token); });
141 return scatterers;
142}
143
147 std::vector<std::string> tokens;
148 boost::split(tokens, singleScatterer, boost::is_any_of(" "));
149
150 if (tokens.size() < 4 || tokens.size() > 6) {
151 throw std::invalid_argument("Could not parse scatterer string: " + singleScatterer);
152 }
153
154 std::vector<std::string> cleanScattererTokens = getCleanScattererTokens(tokens);
155 std::vector<std::string> properties = {"Element", "Position", "Occupancy", "U"};
156
157 ::Json::Value root;
158 for (size_t i = 0; i < cleanScattererTokens.size(); ++i) {
159 root[properties[i]] = cleanScattererTokens[i];
160 }
161
162 std::string initString = Mantid::JsonHelpers::jsonToString(root);
163
164 return BraggScattererFactory::Instance().createScatterer("IsotropicAtomBraggScatterer", initString);
165}
166
168std::vector<std::string>
169IsotropicAtomBraggScattererParser::getCleanScattererTokens(const std::vector<std::string> &tokens) const {
170 std::vector<std::string> cleanTokens;
171
172 // Element
173 cleanTokens.emplace_back(tokens[0]);
174
175 // X, Y, Z
176 cleanTokens.emplace_back("[" + tokens[1] + "," + tokens[2] + "," + tokens[3] + "]");
177
178 for (size_t i = 4; i < tokens.size(); ++i) {
179 cleanTokens.emplace_back(tokens[i]);
180 }
181
182 return cleanTokens;
183}
184
186 IsotropicAtomBraggScatterer_sptr isotropicAtom = std::dynamic_pointer_cast<IsotropicAtomBraggScatterer>(scatterer);
187
188 if (!isotropicAtom) {
189 throw std::invalid_argument("Printing function can only process IsotropicAtomBraggScatterer.");
190 }
191
192 std::string rawPositionString = isotropicAtom->getProperty("Position");
193 std::vector<std::string> positionComponents = getTokenizedPositionString(rawPositionString);
194
195 std::stringstream outStream;
196 outStream << isotropicAtom->getElement() << " " << positionComponents[0] << " " << positionComponents[1] << " "
197 << positionComponents[2] << " " << isotropicAtom->getOccupancy() << " " << isotropicAtom->getU();
198
199 return outStream.str();
200}
201
202} // namespace Mantid::Geometry
#define DECLARE_BRAGGSCATTERER(classname)
This class provides an extension of BraggScatterer, suitable for scatterers that are part of a crysta...
UnitCell getCell() const
Returns the cell which is currently set.
std::vector< std::string > getCleanScattererTokens(const std::vector< std::string > &tokens) const
Converts tokens for getScatterer method so they can be processed by factory.
std::vector< BraggScatterer_sptr > operator()() const
Operator that returns vector of IsotropicAtomBraggScatterers.
BraggScatterer_sptr getScatterer(const std::string &singleScatterer) const
Returns IsotropicAtomBraggScatterer for string with format "Element x y z occupancy u_iso".
IsotropicAtomBraggScatterer calculates the structure factor for a given HKL using the following equat...
double getScatteringLength() const
Returns the scattering length of the stored element.
void declareScattererProperties() override
Declares properties of this scatterer model.
void afterScattererPropertySet(const std::string &propertyName) override
After setting the element as a string, the corresponding.
void setElement(const std::string &element)
Tries to obtain element specific data for the given symbol using PhysicalConstants::getAtom.
BraggScatterer_sptr clone() const override
Clones the instance.
double getDebyeWallerFactor(const Kernel::V3D &hkl) const
Returns the Debye-Waller factor, using an isotropic atomic displacement and the stored unit cell.
double getU() const
Returns the isotropic atomic displacement parameter.
StructureFactor calculateStructureFactor(const Kernel::V3D &hkl) const override
Calculates the structure factor.
IsotropicAtomBraggScatterer()
Constructor which takes an element symbol, fractional coordinates, isotropic atomic displacement para...
const Kernel::DblMatrix & getB() const
Get the B-matrix.
Definition UnitCell.cpp:758
Validator to check that a property is not left empty.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
std::string asString(bool withDefaultValues=false) const override
Return the property manager serialized as a string.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
void declareProperty(std::unique_ptr< Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
The concrete, templated class for properties.
@ TOK_TRIM
remove leading and trailing whitespace from tokens
std::size_t size() const noexcept
Get the total number of tokens.
ConstIterator cend() const
Const iterator referring to the past-the-end element in the container.
ConstIterator cbegin() const
Const iterator referring to first element in the container.
Class for 3D vectors.
Definition V3D.h:34
constexpr double scalar_prod(const V3D &v) const noexcept
Calculates the cross product.
Definition V3D.h:280
constexpr double norm2() const noexcept
Vector length squared.
Definition V3D.h:271
Struture to hold the common information for an atom.
Definition Atom.h:20
const NeutronAtom neutron
Handle to class containing neutronic atomic properties.
Definition Atom.h:57
std::shared_ptr< IsotropicAtomBraggScatterer > IsotropicAtomBraggScatterer_sptr
MANTID_GEOMETRY_DLL std::vector< std::string > getTokenizedPositionString(const std::string &position)
Returns components of comma-separated position string, cleaned from [ and ].
MANTID_GEOMETRY_DLL std::string getIsotropicAtomBraggScattererString(const BraggScatterer_sptr &scatterer)
std::shared_ptr< BraggScatterer > BraggScatterer_sptr
std::complex< double > StructureFactor
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Definition IValidator.h:26
MANTID_KERNEL_DLL const Atom & getAtom(const uint16_t z_number, const uint16_t a_number=0)
Definition Atom.cpp:3183
STL namespace.
double coh_scatt_length_real
The real part of the coherent scattering length in fm.
Definition NeutronAtom.h:51