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
56
58void CompositeBraggScatterer::addScatterers(const std::vector<BraggScatterer_sptr> &scatterers) {
59 for (const auto &scatterer : scatterers) {
61 }
63}
64
66void CompositeBraggScatterer::setScatterers(const std::vector<BraggScatterer_sptr> &scatterers) {
68 addScatterers(scatterers);
69}
70
72size_t CompositeBraggScatterer::nScatterers() const { return m_scatterers.size(); }
73
76 if (i >= nScatterers()) {
77 throw std::out_of_range("Index is out of range.");
78 }
79
80 return m_scatterers[i];
81}
82
84const std::vector<BraggScatterer_sptr> &CompositeBraggScatterer::getScatterers() const { return m_scatterers; }
85
93
96 if (i >= nScatterers()) {
97 throw std::out_of_range("Index is out of range.");
98 }
99
100 m_scatterers.erase(m_scatterers.begin() + i);
101}
102
111
115 return std::accumulate(
116 m_scatterers.cbegin(), m_scatterers.cend(), StructureFactor(0., 0.),
117 [&hkl](const auto &sum, const auto &scatterer) { return sum + scatterer->calculateStructureFactor(hkl); });
118 ;
119}
120
123void CompositeBraggScatterer::afterPropertySet(const std::string &propertyName) { propagateProperty(propertyName); }
124
127void CompositeBraggScatterer::propagateProperty(const std::string &propertyName) {
128 std::string propertyValue = getPropertyValue(propertyName);
129
130 for (auto &scatterer : m_scatterers) {
131 propagatePropertyToScatterer(scatterer, propertyName, propertyValue);
132 }
133}
134
136 const std::string &propertyName,
137 const std::string &propertyValue) {
138 try {
139 scatterer->setPropertyValue(propertyName, propertyValue);
140 } catch (const Kernel::Exception::NotFoundError &) {
141 // do nothing.
142 }
143}
144
147 if (!scatterer) {
148 throw std::invalid_argument("Cannot process null-scatterer.");
149 }
150
151 BraggScatterer_sptr localScatterer = scatterer->clone();
152 m_scatterers.emplace_back(localScatterer);
153}
154
164 std::map<std::string, size_t> propertyUseCount = getPropertyCountMap();
165
166 for (auto &scatterer : m_scatterers) {
167 // Check if any of the declared properties is in this scatterer (and set
168 // value if that's the case)
169 for (auto &prop : propertyUseCount) {
170 if (scatterer->existsProperty(prop.first)) {
171 prop.second += 1;
172
173 propagatePropertyToScatterer(scatterer, prop.first, getPropertyValue(prop.first));
174 }
175 }
176
177 // Use the properties of this scatterer which have been marked as exposed to
178 // composite
179 std::vector<Property *> properties = scatterer->getPropertiesInGroup(getPropagatingGroupName());
180 for (auto &property : properties) {
181 const std::string &propertyName = property->name();
182 if (!existsProperty(propertyName)) {
183 declareProperty(std::unique_ptr<Property>(property->clone()));
184 }
185 }
186 }
187
188 // Remove unused properties
189 for (auto const &property : propertyUseCount) {
190 if (property.second == 0) {
191 removeProperty(property.first);
192 }
193 }
194}
195
197std::map<std::string, size_t> CompositeBraggScatterer::getPropertyCountMap() const {
198 std::map<std::string, size_t> propertyUseCount;
199
200 std::vector<Property *> compositeProperties = getProperties();
201 for (auto &compositeProperty : compositeProperties) {
202 propertyUseCount.emplace(compositeProperty->name(), 0);
203 }
204 return propertyUseCount;
205}
206
208
209} // 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...
const std::vector< BraggScatterer_sptr > & getScatterers() const
Returns the scatterers.
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.
void addScatterers(const std::vector< BraggScatterer_sptr > &scatterer)
Adds scatterers and assigns clones of the supplied ones.
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