Skip to content

Commit

Permalink
Refs #11510. Use muParser to parse atom position strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wedel committed Apr 10, 2015
1 parent 12516c7 commit a7b9131
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Expand Up @@ -72,6 +72,7 @@ class MANTID_GEOMETRY_DLL BraggScattererInCrystalStructure

virtual void declareProperties();

Kernel::V3D getPositionFromString(const std::string &positionString) const;
void recalculateEquivalentPositions();

Kernel::V3D m_position;
Expand Down
Expand Up @@ -3,9 +3,13 @@

#include <boost/regex.hpp>
#include <boost/make_shared.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/range/algorithm/remove_if.hpp>
#include "MantidKernel/ListValidator.h"
#include "MantidGeometry/Crystal/SpaceGroupFactory.h"

#include <muParser.h>

namespace Mantid {
namespace Geometry {

Expand Down Expand Up @@ -69,7 +73,7 @@ void BraggScattererInCrystalStructure::declareProperties() {
setSpaceGroup(SpaceGroupFactory::Instance().createSpaceGroup("P 1"));

declareProperty(
new Kernel::PropertyWithValue<V3D>("Position", V3D(0.0, 0.0, 0.0)),
new Kernel::PropertyWithValue<std::string>("Position", "[0, 0, 0]"),
"Position of the scatterer");

IValidator_sptr unitCellStringValidator =
Expand All @@ -81,7 +85,7 @@ void BraggScattererInCrystalStructure::declareProperties() {
exposePropertyToComposite("UnitCell");

IValidator_sptr spaceGroupValidator =
boost::make_shared<ListValidator<std::string>>(
boost::make_shared<ListValidator<std::string> >(
SpaceGroupFactory::Instance().subscribedSpaceGroupSymbols());
declareProperty(new Kernel::PropertyWithValue<std::string>(
"SpaceGroup", "P 1", spaceGroupValidator),
Expand All @@ -91,6 +95,35 @@ void BraggScattererInCrystalStructure::declareProperties() {
declareScattererProperties();
}

V3D BraggScattererInCrystalStructure::getPositionFromString(
const std::string &positionString) const {
std::string positionStringClean = positionString;
positionStringClean.erase(
boost::remove_if(positionStringClean, boost::is_any_of("[]")),
positionStringClean.end());


mu::Parser parser;
parser.SetArgSep(',');
parser.SetDecSep('.');
parser.SetExpr(positionStringClean);

int nComponents;
mu::value_type *components = parser.Eval(nComponents);

if (nComponents != 3) {
throw std::invalid_argument("Cannot parse '" + positionString +
"' as a position.");
}

V3D position;
for (size_t i = 0; i < static_cast<size_t>(nComponents); ++i) {
position[i] = components[i];
}

return position;
}

/**
* Additional property processing
*
Expand All @@ -105,9 +138,9 @@ void BraggScattererInCrystalStructure::declareProperties() {
void BraggScattererInCrystalStructure::afterPropertySet(
const std::string &propertyName) {
if (propertyName == "Position") {
PropertyWithValue<V3D> *position = dynamic_cast<PropertyWithValue<V3D> *>(
getPointerToProperty("Position"));
setPosition((*position)());
std::string position = getProperty("Position");

setPosition(getPositionFromString(position));
} else if (propertyName == "SpaceGroup") {
setSpaceGroup(SpaceGroupFactory::Instance().createSpaceGroup(
getProperty("SpaceGroup")));
Expand All @@ -124,7 +157,7 @@ void BraggScattererInCrystalStructure::recalculateEquivalentPositions() {
m_equivalentPositions.clear();

if (m_spaceGroup) {
m_equivalentPositions = m_spaceGroup * m_position;
m_equivalentPositions = m_spaceGroup->getEquivalentPositions(m_position);
} else {
m_equivalentPositions.push_back(m_position);
}
Expand Down

0 comments on commit a7b9131

Please sign in to comment.