Skip to content
Snippets Groups Projects
SourceItems.cpp 9.33 KiB
Newer Older
  • Learn to ignore specific revisions
  • //  ************************************************************************************************
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    //
    
    //  BornAgain: simulate and fit reflection and scattering
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    //
    
    //! @file      GUI/Model/Beam/SourceItems.cpp
    
    //! @brief     Implements BeamItem hierarchy.
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    //!
    
    //! @homepage  http://www.bornagainproject.org
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    //! @license   GNU General Public License v3 or higher (see COPYING)
    
    //! @copyright Forschungszentrum Jülich GmbH 2018
    //! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    //
    
    //  ************************************************************************************************
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    
    
    #include "GUI/Model/Beam/SourceItems.h"
    
    #include "Base/Axis/Scale.h"
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    #include "Base/Const/Units.h"
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
    #include "Base/Util/Assert.h"
    
    #include "Device/Beam/Beam.h"
    
    #include "Device/Beam/FootprintGauss.h"
    #include "Device/Beam/FootprintSquare.h"
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    #include "GUI/Model/Axis/BasicAxisItem.h"
    
    #include "GUI/Model/Beam/BeamAngleItems.h"
    #include "GUI/Model/Beam/BeamWavelengthItem.h"
    #include "GUI/Model/Beam/FootprintItems.h"
    #include "GUI/Model/Beam/GrazingScanItem.h"
    
    Wuttke, Joachim's avatar
    ..  
    Wuttke, Joachim committed
    
    
    namespace {
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
    namespace Tag {
    
    const QString AzimuthalAngle("AzimuthalAngle");
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
    const QString BaseData("BaseData");
    
    const QString ExpandBeamParametersGroupbox("ExpandBeamParametersGroupbox");
    const QString ExpandFootprintGroupbox("ExpandFootprintGroupbox");
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    const QString Footprint("Footprint");
    const QString GrazingScan("GrazingScan");
    const QString InclinationAngle("InclinationAngle");
    const QString Intensity("Intensity");
    const QString Wavelength("Wavelength");
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
    
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
    } // namespace Tag
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
    
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    } // namespace
    
    //  ************************************************************************************************
    
    //  ************************************************************************************************
    
    
    SourceItem::SourceItem()
    
        m_intensity.init("Intensity", "Beam intensity in neutrons/photons per sec.", 1e8, 3,
                         RealLimits::limited(0.0, 1e32), "intensity");
    
        m_azimuthal_angle_item = std::make_unique<BeamAzimuthalAngleItem>();
    
        m_footprint.initWithArgs("Type", "Footprint type", FootprintItemCatalog::Type::Gaussian);
    
    void SourceItem::writeTo(QXmlStreamWriter* w) const
    
        ASSERT(m_wavelength_item);
    
        XML::writeAttribute(w, XML::Attrib::version, uint(2));
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
    
        // intensity
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        m_intensity.writeTo2(w, Tag::Intensity);
    
        XML::writeTaggedElement(w, Tag::Wavelength, *m_wavelength_item);
        XML::writeTaggedElement(w, Tag::AzimuthalAngle, *m_azimuthal_angle_item);
    
        XML::writeTaggedValue(w, Tag::ExpandBeamParametersGroupbox, expandBeamParameters);
    
        XML::writeTaggedElement(w, Tag::Footprint, m_footprint);
    
        XML::writeTaggedValue(w, Tag::ExpandFootprintGroupbox, expandFootprint);
    
    void SourceItem::readFrom(QXmlStreamReader* r)
    
        ASSERT(m_wavelength_item);
    
        const uint version = XML::readUInt(r, XML::Attrib::version);
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
        Q_UNUSED(version)
    
        while (r->readNextStartElement()) {
            QString tag = r->name().toString();
            if (tag == Tag::Intensity) {
    
                m_intensity.readFrom2(r, tag);
    
            } else if (tag == Tag::Wavelength)
                XML::readTaggedElement(r, tag, *m_wavelength_item);
            else if (tag == Tag::AzimuthalAngle)
                XML::readTaggedElement(r, tag, *m_azimuthal_angle_item);
            else if (tag == Tag::ExpandBeamParametersGroupbox)
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
                expandBeamParameters = XML::readTaggedBool(r, tag);
    
            else if (tag == Tag::Footprint)
                XML::readTaggedElement(r, tag, m_footprint);
            else if (tag == Tag::ExpandFootprintGroupbox)
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
                expandFootprint = XML::readTaggedBool(r, tag);
            else
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
                r->skipCurrentElement();
        }
    
    double SourceItem::wavelength() const
    
        ASSERT(m_wavelength_item);
        return m_wavelength_item->wavelength();
    
    void SourceItem::setWavelength(double value)
    
        ASSERT(m_wavelength_item);
        m_wavelength_item->resetToValue(value);
    
    BeamWavelengthItem* SourceItem::wavelengthItem() const
    
        ASSERT(m_wavelength_item);
        return m_wavelength_item.get();
    
    double SourceItem::azimuthalAngle() const
    
        ASSERT(m_azimuthal_angle_item);
        return m_azimuthal_angle_item->azimuthalAngle();
    
    void SourceItem::setAzimuthalAngle(double value)
    
        ASSERT(m_azimuthal_angle_item);
        m_azimuthal_angle_item->resetToValue(value);
    
    BeamAzimuthalAngleItem* SourceItem::azimuthalAngleItem() const
    
        ASSERT(m_azimuthal_angle_item);
        return m_azimuthal_angle_item.get();
    
    
    void SourceItem::setGaussianFootprint(double value)
    {
    
        m_footprint.setCertainItem(new FootprintGaussianItem(value));
    
    }
    
    void SourceItem::setSquareFootprint(double value)
    {
    
        m_footprint.setCertainItem(new FootprintSquareItem(value));
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
    void SourceItem::setFootprint(const IFootprint* footprint)
    
    {
        if (!footprint)
            return;
    
        if (const auto* const fp = dynamic_cast<const FootprintGauss*>(footprint))
            setGaussianFootprint(fp->widthRatio());
        else if (const auto* const fp = dynamic_cast<const FootprintSquare*>(footprint))
            setSquareFootprint(fp->widthRatio());
    }
    
    
    //  ************************************************************************************************
    
    //  BeamItem
    //  ************************************************************************************************
    
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    BeamItem::BeamItem()
    
        m_wavelength_item = std::make_unique<BeamWavelengthItem>();
        m_inclination_angle_item = std::make_unique<BeamInclinationAngleItem>();
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    void BeamItem::writeTo(QXmlStreamWriter* w) const
    {
    
        ASSERT(m_inclination_angle_item);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        XML::writeAttribute(w, XML::Attrib::version, uint(1));
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        XML::writeBaseElement<SourceItem>(w, this);
    
        XML::writeTaggedElement(w, Tag::InclinationAngle, *m_inclination_angle_item);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    }
    
    void BeamItem::readFrom(QXmlStreamReader* r)
    {
    
        const uint version = XML::readUInt(r, XML::Attrib::version);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        Q_UNUSED(version)
    
        ASSERT(m_inclination_angle_item);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    
        while (r->readNextStartElement()) {
            QString tag = r->name().toString();
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
            if (tag == Tag::BaseData)
                XML::readBaseElement<SourceItem>(r, this);
            else if (tag == Tag::InclinationAngle)
    
                XML::readTaggedElement(r, tag, *m_inclination_angle_item);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
                r->skipCurrentElement();
        }
    }
    
    
    void BeamItem::setInclinationAngle(double value)
    {
    
        ASSERT(m_inclination_angle_item);
        m_inclination_angle_item->resetToValue(value);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    BeamDistributionItem* BeamItem::beamDistributionItem() const
    {
    
        ASSERT(m_inclination_angle_item);
        return m_inclination_angle_item.get();
    
    double BeamItem::getInclinationAngle() const
    {
        return dynamic_cast<BeamInclinationAngleItem*>(beamDistributionItem())->inclinationAngle();
    }
    
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    std::unique_ptr<Beam> BeamItem::createBeam() const
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    {
        double lambda = wavelength();
        double inclination_angle = Units::deg2rad(getInclinationAngle());
        double azimuthal_angle = Units::deg2rad(azimuthalAngle());
    
    
        auto result =
            std::make_unique<Beam>(intensity().dVal(), lambda, inclination_angle, azimuthal_angle);
    
        result->setFootprint(m_footprint.certainItem()->createFootprint().get());
    
        return result;
    
    //  ************************************************************************************************
    //  ScanItem
    
    //  ************************************************************************************************
    
    ScanItem::ScanItem()
    
        m_grazing_scan_item = std::make_unique<GrazingScanItem>();
        m_wavelength_item = std::make_unique<BeamWavelengthItem>(true);
    
    void ScanItem::setScan(const BeamScan* scan)
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        setIntensity(scan->commonIntensity());
    
        if (const auto* s = dynamic_cast<const AlphaScan*>(scan))
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
            setWavelength(s->commonWavelength());
    
        setAzimuthalAngle(0.0);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        BasicAxisItem* axis_item = inclinationAxisItem();
    
        const Scale* axis = scan->coordinateAxis();
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        ASSERT(axis);
    
        // TODO do we need to check that axis is equidistant?
    
        axis_item->resize(static_cast<int>(axis->size()));
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        axis_item->setMin(axis->min() / Units::deg);
        axis_item->setMax(axis->max() / Units::deg);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        setFootprint(scan->commonFootprint());
    
    void ScanItem::writeTo(QXmlStreamWriter* w) const
    
        XML::writeAttribute(w, XML::Attrib::version, uint(2));
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        XML::writeBaseElement<SourceItem>(w, this);
    
        XML::writeTaggedElement(w, Tag::GrazingScan, *m_grazing_scan_item);
    
    void ScanItem::readFrom(QXmlStreamReader* r)
    
        const uint version = XML::readUInt(r, XML::Attrib::version);
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
    
        while (r->readNextStartElement()) {
            QString tag = r->name().toString();
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
            if (tag == Tag::BaseData)
                XML::readBaseElement<SourceItem>(r, this);
            else if (version == 1 && tag == Tag::Footprint)
    
                XML::readTaggedElement(r, tag, m_footprint);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
            else if (version == 1 && tag == Tag::ExpandFootprintGroupbox)
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
                expandFootprint = XML::readTaggedBool(r, tag);
    
            else if (tag == Tag::GrazingScan)
                XML::readTaggedElement(r, tag, *m_grazing_scan_item);
            else
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
                r->skipCurrentElement();
        }
    
    GrazingScanItem* ScanItem::grazingScanItem() const
    {
    
        return m_grazing_scan_item.get();
    
    BasicAxisItem* ScanItem::inclinationAxisItem() const
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        return grazingScanItem()->alphaAxisItem();
    
    void ScanItem::updateToData(const Scale& axis)
    
        if (axis.unit() == "bin") {
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
            grazingScanItem()->initUniformAxis(axis);
            grazingScanItem()->selectUniformAxis();
    
            grazingScanItem()->initListScan(axis);
    
            grazingScanItem()->selectListScan();