Mantid
Loading...
Searching...
No Matches
CompositeBraggScatterer.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 <stdexcept>
10
11namespace Mantid::Geometry {
12
13using namespace Kernel;
14
17
21 CompositeBraggScatterer_sptr compositeScatterer = std::make_shared<CompositeBraggScatterer>();
22 compositeScatterer->initialize();
23
24 return compositeScatterer;
25}
26
29CompositeBraggScatterer_sptr CompositeBraggScatterer::create(const std::vector<BraggScatterer_sptr> &scatterers) {
31
32 collection->setScatterers(scatterers);
33
34 return collection;
35}
36
40 CompositeBraggScatterer_sptr clone = std::make_shared<CompositeBraggScatterer>();
41 clone->initialize();
42
43 clone->setScatterers(m_scatterers);
44
45 clone->setProperties(this->asString(false));
46
47 return clone;
48}
49
55}
56
58void CompositeBraggScatterer::setScatterers(const std::vector<BraggScatterer_sptr> &scatterers) {
60
61 for (const auto &scatterer : scatterers) {
63 }
64
66}
67
69size_t CompositeBraggScatterer::nScatterers() const { return m_scatterers.size(); }
70
73 if (i >= nScatterers()) {
74 throw std::out_of_range("Index is out of range.");
75 }
76
77 return m_scatterers[i];
78}
79
84
86}
87
90 if (i >= nScatterers()) {
91 throw std::out_of_range("Index is out of range.");
92 }
93
94 m_scatterers.erase(m_scatterers.begin() + i);
95}
96
99 while (nScatterers() > 0) {
101 }
102
104}
105
109 return std::accumulate(
110 m_scatterers.cbegin(), m_scatterers.cend(), StructureFactor(0., 0.),
111 [&hkl](const auto &sum, const auto &scatterer) { return sum + scatterer->calculateStructureFactor(hkl); });
112 ;
113}
114
117void CompositeBraggScatterer::afterPropertySet(const std::string &propertyName) { propagateProperty(propertyName); }
118
121void CompositeBraggScatterer::propagateProperty(const std::string &propertyName) {
122 std::string propertyValue = getPropertyValue(propertyName);
123
124 for (auto &scatterer : m_scatterers) {
125 propagatePropertyToScatterer(scatterer, propertyName, propertyValue);
126 }
127}
128
130 const std::string &propertyName,
131 const std::string &propertyValue) {
132 try {
133 scatterer->setPropertyValue(propertyName, propertyValue);
134 } catch (const Kernel::Exception::NotFoundError &) {
135 // do nothing.
136 }
137}
138
141 if (!scatterer) {
142 throw std::invalid_argument("Cannot process null-scatterer.");
143 }
144
145 BraggScatterer_sptr localScatterer = scatterer->clone();
146 m_scatterers.emplace_back(localScatterer);
147}
148
158 std::map<std::string, size_t> propertyUseCount = getPropertyCountMap();
159
160 for (auto &scatterer : m_scatterers) {
161 // Check if any of the declared properties is in this scatterer (and set
162 // value if that's the case)
163 for (auto &prop : propertyUseCount) {
164 if (scatterer->existsProperty(prop.first)) {
165 prop.second += 1;
166
167 propagatePropertyToScatterer(scatterer, prop.first, getPropertyValue(prop.first));
168 }
169 }
170
171 // Use the properties of this scatterer which have been marked as exposed to
172 // composite
173 std::vector<Property *> properties = scatterer->getPropertiesInGroup(getPropagatingGroupName());
174 for (auto &property : properties) {
175 const std::string &propertyName = property->name();
176 if (!existsProperty(propertyName)) {
177 declareProperty(std::unique_ptr<Property>(property->clone()));
178 }
179 }
180 }
181
182 // Remove unused properties
183 for (auto &property : propertyUseCount) {
184 if (property.second == 0) {
185 removeProperty(property.first);
186 }
187 }
188}
189
191std::map<std::string, size_t> CompositeBraggScatterer::getPropertyCountMap() const {
192 std::map<std::string, size_t> propertyUseCount;
193
194 std::vector<Property *> compositeProperties = getProperties();
195 for (auto &compositeProperty : compositeProperties) {
196 propertyUseCount.emplace(compositeProperty->name(), 0);
197 }
198 return propertyUseCount;
199}
200
202
203} // namespace Mantid::Geometry
#define DECLARE_BRAGGSCATTERER(classname)
BraggScatterer is a general interface for representing scatterers in the unit cell of a periodic stru...
const std::string & getPropagatingGroupName() const
Returns the group name that is used to mark properties that are propagated.
CompositeBraggScatterer accumulates scatterers, for easier calculation of structure factors.
std::map< std::string, size_t > getPropertyCountMap() const
Returns a map with all declared property names and 0.
void removeScatterer(size_t i)
Removes the i-th scatterer from the composite or throws an std::out_of_range exception.
StructureFactor calculateStructureFactor(const Kernel::V3D &hkl) const override
Calculates the structure factor for the given HKL by summing all contributions from contained scatter...
virtual void addScatterer(const BraggScatterer_sptr &scatterer)
Clones the supplied scatterer, assigns the internal space group and unit cell to the clone and adds i...
size_t nScatterers() const
Returns the number of scatterers contained in the composite.
std::vector< BraggScatterer_sptr > m_scatterers
void afterPropertySet(const std::string &propertyName) override
Makes sure that space group and unit cell are propagated to all stored scatterers.
BraggScatterer_sptr clone() const override
Recursively clones all contained scatterers and returns the resulting composite.
void redeclareProperties()
Synchronize properties with scatterer members.
void setScatterers(const std::vector< BraggScatterer_sptr > &scatterers)
Clears all scatterers and assigns clones of the supplied ones.
void addScattererImplementation(const BraggScatterer_sptr &scatterer)
This method performs the actual cloning and adding of a new scatterer.
BraggScatterer_sptr getScatterer(size_t i) const
Returns the i-th scatterer or throws an std::out_of_range exception.
void propagatePropertyToScatterer(BraggScatterer_sptr &scatterer, const std::string &propertyName, const std::string &propertyValue)
void propagateProperty(const std::string &propertyName)
Propagates the given property to all contained scatterers that have this property.
static CompositeBraggScatterer_sptr create()
Static method that creates a new instance of CompositeBraggScatterer and returns it (wrapped by a sma...
void removeScattererImplementation(size_t i)
This method performs the actual removal of the i-th scatterer.
Exception for when an item is not found in a collection.
Definition: Exception.h:145
void removeProperty(const std::string &name, const bool delproperty=true) override
removes the property from properties map
bool existsProperty(const std::string &name) const override
Checks whether the named property is already in the list of managed 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.
const std::vector< Property * > & getProperties() const override
Get the list of managed properties.
Class for 3D vectors.
Definition: V3D.h:34
std::shared_ptr< BraggScatterer > BraggScatterer_sptr
std::shared_ptr< CompositeBraggScatterer > CompositeBraggScatterer_sptr
std::complex< double > StructureFactor