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 = atom.symbol;
45}
46
48std::string IsotropicAtomBraggScatterer::getElement() const { return m_label; }
49
52
54double IsotropicAtomBraggScatterer::getOccupancy() const { return getProperty("Occupancy"); }
55
57double IsotropicAtomBraggScatterer::getU() const { return getProperty("U"); }
58
69 double amplitude = getOccupancy() * getDebyeWallerFactor(hkl) * getScatteringLength();
70
71 double phase = 2.0 * M_PI * m_position.scalar_prod(hkl);
72
73 return amplitude * StructureFactor(cos(phase), sin(phase));
74}
75
89 // Default behavior requires this.
90 setElement("H");
91
92 std::shared_ptr<BoundedValidator<double>> uValidator = std::make_shared<BoundedValidator<double>>();
93 uValidator->setLower(0.0);
94
95 declareProperty(std::make_unique<PropertyWithValue<double>>("U", 0.0, uValidator),
96 "Isotropic atomic displacement in Angstrom^2");
97
98 IValidator_sptr occValidator = std::make_shared<BoundedValidator<double>>(0.0, 1.0);
99 declareProperty(std::make_unique<PropertyWithValue<double>>("Occupancy", 1.0, occValidator),
100 "Site occupancy, values on interval [0,1].");
101
103 "Element", "H", std::make_shared<MandatoryValidator<std::string>>()));
104}
105
107void IsotropicAtomBraggScatterer::afterScattererPropertySet(const std::string &propertyName) {
108 if (propertyName == "Element") {
109 setElement(getPropertyValue(propertyName));
110 }
111}
112
116 V3D dstar = getCell().getB() * hkl;
117
118 return exp(-2.0 * M_PI * M_PI * getU() * dstar.norm2());
119}
120
123
125
126
138 : m_scattererString(std::move(scattererString)) {}
139
141std::vector<BraggScatterer_sptr> IsotropicAtomBraggScattererParser::operator()() const {
143 std::vector<BraggScatterer_sptr> scatterers;
144 scatterers.reserve(tokens.size());
145 std::transform(tokens.cbegin(), tokens.cend(), std::back_inserter(scatterers),
146 [this](const auto &token) { return getScatterer(token); });
147 return scatterers;
148}
149
153 std::vector<std::string> tokens;
154 boost::split(tokens, singleScatterer, boost::is_any_of(" "));
155
156 if (tokens.size() < 4 || tokens.size() > 6) {
157 throw std::invalid_argument("Could not parse scatterer string: " + singleScatterer);
158 }
159
160 std::vector<std::string> cleanScattererTokens = getCleanScattererTokens(tokens);
161 std::vector<std::string> properties = {"Element", "Position", "Occupancy", "U"};
162
163 ::Json::Value root;
164 for (size_t i = 0; i < cleanScattererTokens.size(); ++i) {
165 root[properties[i]] = cleanScattererTokens[i];
166 }
167
168 std::string initString = Mantid::JsonHelpers::jsonToString(root);
169
170 return BraggScattererFactory::Instance().createScatterer("IsotropicAtomBraggScatterer", initString);
171}
172
174std::vector<std::string>
175IsotropicAtomBraggScattererParser::getCleanScattererTokens(const std::vector<std::string> &tokens) const {
176 std::vector<std::string> cleanTokens;
177
178 // Element
179 cleanTokens.emplace_back(tokens[0]);
180
181 // X, Y, Z
182 cleanTokens.emplace_back("[" + tokens[1] + "," + tokens[2] + "," + tokens[3] + "]");
183
184 for (size_t i = 4; i < tokens.size(); ++i) {
185 cleanTokens.emplace_back(tokens[i]);
186 }
187
188 return cleanTokens;
189}
190
192 IsotropicAtomBraggScatterer_sptr isotropicAtom = std::dynamic_pointer_cast<IsotropicAtomBraggScatterer>(scatterer);
193
194 if (!isotropicAtom) {
195 throw std::invalid_argument("Printing function can only process IsotropicAtomBraggScatterer.");
196 }
197
198 std::string rawPositionString = isotropicAtom->getProperty("Position");
199 std::vector<std::string> positionComponents = getTokenizedPositionString(rawPositionString);
200
201 std::stringstream outStream;
202 outStream << isotropicAtom->getElement() << " " << positionComponents[0] << " " << positionComponents[1] << " "
203 << positionComponents[2] << " " << isotropicAtom->getOccupancy() << " " << isotropicAtom->getU();
204
205 return outStream.str();
206}
207
208} // 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...
std::string getElement() const
Returns the string representation of the contained element.
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.
PhysicalConstants::NeutronAtom getNeutronAtom() const
Returns the internally stored NeutronAtom that holds element specific data.
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.
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.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
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.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
@ 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:274
constexpr double norm2() const noexcept
Vector length squared.
Definition: V3D.h:265
Struture to hold the common information for an atom.
Definition: Atom.h:20
const std::string symbol
The atomic symbol. In other words the one or two character abbreviation.
Definition: Atom.h:31
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:3167
STL namespace.
Structure to store neutronic scattering information for the various elements.
Definition: NeutronAtom.h:22
double coh_scatt_length_real
The real part of the coherent scattering length in fm.
Definition: NeutronAtom.h:51