From 4a91456a05913dcde6581939b76db33f30e5463b Mon Sep 17 00:00:00 2001 From: Matthew Andrew <matthew.andrew@tessella.com> Date: Tue, 14 Jan 2020 15:03:19 +0000 Subject: [PATCH] Updated function checking to allow Temp function Re #26881 --- .../ConvFunctionModel.cpp | 15 ++++++- .../Indirect/test/ConvFunctionModelTest.h | 11 +++++ .../common/src/ConvolutionFunctionModel.cpp | 10 +++-- .../test/ConvolutionFunctionModelTest.h | 43 ++++++++++++++----- 4 files changed, 63 insertions(+), 16 deletions(-) diff --git a/qt/scientific_interfaces/Indirect/IndirectFunctionBrowser/ConvFunctionModel.cpp b/qt/scientific_interfaces/Indirect/IndirectFunctionBrowser/ConvFunctionModel.cpp index 01fd001365d..e0aed29ffd5 100644 --- a/qt/scientific_interfaces/Indirect/IndirectFunctionBrowser/ConvFunctionModel.cpp +++ b/qt/scientific_interfaces/Indirect/IndirectFunctionBrowser/ConvFunctionModel.cpp @@ -10,6 +10,7 @@ #include "MantidAPI/ITableWorkspace.h" #include "MantidAPI/MultiDomainFunction.h" #include "MantidQtWidgets/Common/FunctionBrowser/FunctionBrowserUtils.h" +#include <iostream> namespace MantidQt { namespace CustomInterfaces { @@ -39,7 +40,7 @@ void ConvFunctionModel::setFunction(IFunction_sptr fun) { clearData(); if (!fun) return; - + std::cout << fun->asString() << std::endl; bool isBackgroundSet = false; if (fun->name() == "Convolution") { checkConvolution(fun); @@ -78,7 +79,15 @@ void ConvFunctionModel::checkConvolution(IFunction_sptr fun) { throw std::runtime_error("Function has wrong structure."); } isResolutionSet = true; - } else if (name == "CompositeFunction") { + } else if (name == "ProductFunction") { + if (innerFunction->getFunction(0)->name() != "UserFunction") { + throw std::runtime_error("Function has wrong structure."); + } + m_hasTempCorrection = true; + checkConvolution(innerFunction->getFunction(1)); + } + + else if (name == "CompositeFunction") { checkComposite(innerFunction); } else if (FitTypeStringToEnum.count(name) == 1) { if (isFitTypeSet) { @@ -128,6 +137,8 @@ void ConvFunctionModel::checkComposite(IFunction_sptr fun) { } } +// void ConvFunctionModel::checkProductFunction(IFunction_sptr fun) {} + IFunction_sptr ConvFunctionModel::getFitFunction() const { return m_model.getFitFunction(); } diff --git a/qt/scientific_interfaces/Indirect/test/ConvFunctionModelTest.h b/qt/scientific_interfaces/Indirect/test/ConvFunctionModelTest.h index f1902fe0041..a3cd1b4b50d 100644 --- a/qt/scientific_interfaces/Indirect/test/ConvFunctionModelTest.h +++ b/qt/scientific_interfaces/Indirect/test/ConvFunctionModelTest.h @@ -114,6 +114,17 @@ public: TS_ASSERT_THROWS(m_model->setFunction(fun), std::runtime_error &) } + void test_setFunction_does_not_throw_for_valid_temp_function() { + m_model->setFitType(FitType::OneLorentzian); + m_model->setTempCorrection(true); + auto func = m_model->getFitFunction()->clone()->clone(); + + m_model->setFunction(func); + + TS_ASSERT_EQUALS(m_model->getCurrentFunction()->asString(), + func->asString()) + } + private: std::unique_ptr<MantidQt::CustomInterfaces::IDA::ConvFunctionModel> m_model; }; diff --git a/qt/widgets/common/src/ConvolutionFunctionModel.cpp b/qt/widgets/common/src/ConvolutionFunctionModel.cpp index 19d126d1fd8..af3b3e23a16 100644 --- a/qt/widgets/common/src/ConvolutionFunctionModel.cpp +++ b/qt/widgets/common/src/ConvolutionFunctionModel.cpp @@ -11,6 +11,7 @@ #include "MantidAPI/MultiDomainFunction.h" #include "MantidKernel/Logger.h" #include "MantidQtWidgets/Common/FunctionBrowser/FunctionBrowserUtils.h" +#include <iostream> namespace { Mantid::Kernel::Logger g_log("ConvolutionFunctionModel"); @@ -74,10 +75,8 @@ void ConvolutionFunctionModel::setModel( const std::string &peaks, bool hasDeltaFunction, const std::vector<double> &qValues, const bool isQDependent, bool hasTempCorrection) { - std::string resolution, convolution, function, modifiedPeaks; auto fitFunction = boost::make_shared<MultiDomainFunction>(); - auto const nf = m_numberDomains > 0 ? static_cast<int>(m_numberDomains) : 1; for (int i = 0; i < nf; ++i) { CompositeFunction_sptr domainFunction; @@ -86,11 +85,14 @@ void ConvolutionFunctionModel::setModel( qValue, hasTempCorrection); auto workspace = resolutionWorkspaces.empty() ? "" : resolutionWorkspaces[i].first; + auto workspaceIndex = + resolutionWorkspaces.empty() ? 0 : resolutionWorkspaces[i].second; auto resolutionFunction = - createResolutionFunction(workspace, resolutionWorkspaces[i].second); + createResolutionFunction(workspace, workspaceIndex); domainFunction = createConvolutionFunction(resolutionFunction, domainFunction); domainFunction = addBackground(domainFunction, background); + fitFunction->addFunction(domainFunction); fitFunction->setDomainIndex(i, i); } @@ -138,7 +140,7 @@ CompositeFunction_sptr ConvolutionFunctionModel::createInnerFunction( FunctionFactory::Instance().createFunction("DeltaFunction"); innerFunction->addFunction(deltaFunction); } - if (hasTempCorrection && (functionSpecified || hasDeltaFunction)) { + if (hasTempCorrection) { innerFunction = addTempCorrection(innerFunction); } diff --git a/qt/widgets/common/test/ConvolutionFunctionModelTest.h b/qt/widgets/common/test/ConvolutionFunctionModelTest.h index 8c2fa79659d..7215b019ff3 100644 --- a/qt/widgets/common/test/ConvolutionFunctionModelTest.h +++ b/qt/widgets/common/test/ConvolutionFunctionModelTest.h @@ -403,17 +403,17 @@ public: "ProductFunction,NumDeriv=false;name=UserFunction,Formula=(x*11.606/" "Temp)/(1-exp( " "-(x*11.606/" - "Temp))),Temp=0;(name=Lorentzian,Amplitude=1,PeakCentre=0,FWHM=0;name=" - "Lorentzian,Amplitude=1,PeakCentre=0,FWHM=0;name=DeltaFunction,Height=" - "1,Centre=0))));(composite=CompositeFunction,NumDeriv=false,$domains=i;" - "name=FlatBackground,A0=0;(composite=Convolution,FixResolution=true," - "NumDeriv=true;name=Resolution,Workspace=abc,WorkspaceIndex=2,X=(),Y=()" - ";(composite=ProductFunction,NumDeriv=false;name=UserFunction,Formula=(" - "x*11.606/Temp)/(1-exp( " + "Temp))),Temp=100;(name=Lorentzian,Amplitude=1,PeakCentre=0,FWHM=0;" + "name=Lorentzian,Amplitude=1,PeakCentre=0,FWHM=0;name=DeltaFunction," + "Height=1,Centre=0))));(composite=CompositeFunction,NumDeriv=false,$" + "domains=i;name=FlatBackground,A0=0;(composite=Convolution," + "FixResolution=true,NumDeriv=true;name=Resolution,Workspace=abc," + "WorkspaceIndex=2,X=(),Y=();(composite=ProductFunction,NumDeriv=false;" + "name=UserFunction,Formula=(x*11.606/Temp)/(1-exp( " "-(x*11.606/" - "Temp))),Temp=0;(name=Lorentzian,Amplitude=1,PeakCentre=0,FWHM=0;name=" - "Lorentzian,Amplitude=1,PeakCentre=0,FWHM=0;name=DeltaFunction,Height=" - "1,Centre=0))))"); + "Temp))),Temp=100;(name=Lorentzian,Amplitude=1,PeakCentre=0,FWHM=0;" + "name=Lorentzian,Amplitude=1,PeakCentre=0,FWHM=0;name=DeltaFunction," + "Height=1,Centre=0))))"); } void test_component_prefixes_set_correctly_without_temp_correction() { @@ -475,6 +475,29 @@ public: TS_ASSERT_EQUALS(model.tempFunctionPrefix().value().toStdString(), "f1.f1.f0."); } + + void test_component_prefixes_if_only_temp_set() { + auto algo = FrameworkManager::Instance().createAlgorithm("CreateWorkspace"); + algo->initialize(); + algo->setPropertyValue("DataX", "1,2,3"); + algo->setPropertyValue("DataY", "1,2,3"); + algo->setPropertyValue("OutputWorkspace", "abc"); + algo->execute(); + ConvolutionFunctionModel model; + model.setNumberDomains(2); + auto pair1 = std::make_pair<std::string, int>("abc", 1); + auto pair2 = std::make_pair<std::string, int>("abc", 2); + auto fitResolutions = std::vector<std::pair<std::string, int>>(); + fitResolutions.emplace_back(pair1); + fitResolutions.emplace_back(pair2); + + model.setModel("", fitResolutions, "", false, std::vector<double>(), false, + true); + auto fitFunctionAsString = model.getFitFunction()->asString(); + TS_ASSERT_EQUALS(model.convolutionPrefix().value().toStdString(), ""); + TS_ASSERT_EQUALS(model.tempFunctionPrefix().value().toStdString(), + "f1.f0."); + } }; #endif // MANTIDWIDGETS_CONVOLUTIONFUNCTIONMODELTEST_H_ -- GitLab