Skip to content

Commit

Permalink
feat: broadenedTR added to TR module (#2034)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danbr4d authored Feb 7, 2025
1 parent 64d0df9 commit 80fa8cb
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 32 deletions.
2 changes: 0 additions & 2 deletions src/modules/neutronSQ/gui/neutronSQWidgetFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ NeutronSQModuleWidget::NeutronSQModuleWidget(QWidget *parent, NeutronSQModule *m
// Set up user interface
ui_.setupUi(this);

refreshing_ = true;

// Set up graph (defaulting to total F(Q))
graph_ = ui_.PlotWidget->dataViewer();
// -- Set view
Expand Down
1 change: 1 addition & 0 deletions src/modules/tr/gui/trWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class TRModuleWidget : public ModuleWidget
*/
private Q_SLOTS:
void on_PartialsButton_clicked(bool checked);
void on_RepresentativePartialsButton_clicked(bool checked);
void on_TotalButton_clicked(bool checked);
void on_FilterEdit_textChanged(QString text);
void on_ClearFilterButton_clicked(bool checked);
Expand Down
47 changes: 24 additions & 23 deletions src/modules/tr/gui/trWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,8 @@
<string>RDF Controls</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>4</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="TotalButton">
<property name="toolTip">
Expand Down Expand Up @@ -83,6 +65,25 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="RepresentativePartialsButton">
<property name="toolTip">
<string>Show the partial t(r) formed from summation over all target configurations</string>
</property>
<property name="text">
<string>Representative Partial t(r)</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<property name="autoExclusive">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="ConfigurationTargetCombo">
<property name="sizePolicy">
Expand All @@ -96,12 +97,12 @@
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
<width>13</width>
<height>17</height>
</size>
</property>
</spacer>
Expand Down Expand Up @@ -166,10 +167,10 @@
<bool>true</bool>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
<enum>QFrame::Shape::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
<enum>QFrame::Shadow::Sunken</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing">
Expand Down
39 changes: 36 additions & 3 deletions src/modules/tr/gui/trWidgetFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ TRModuleWidget::TRModuleWidget(QWidget *parent, TRModule *module, Dissolve &diss
// Set up user interface
ui_.setupUi(this);

// Set up RDF graph
// Set up TR graph
trGraph_ = ui_.TRPlotWidget->dataViewer();
// -- Set view
trGraph_->view().setViewType(View::FlatXYView);
Expand All @@ -36,6 +36,8 @@ TRModuleWidget::TRModuleWidget(QWidget *parent, TRModule *module, Dissolve &diss
trGraph_->groupManager().setGroupColouring("Unbound", RenderableGroup::AutomaticIndividualColouring);
trGraph_->groupManager().setGroupVerticalShifting("Unbound", RenderableGroup::IndividualVerticalShifting);
trGraph_->groupManager().setGroupStipple("Unbound", LineStipple::DotStipple);
trGraph_->groupManager().setGroupColouring("Total", RenderableGroup::AutomaticIndividualColouring);
trGraph_->groupManager().setGroupVerticalShifting("Total", RenderableGroup::IndividualVerticalShifting);

refreshing_ = false;
}
Expand Down Expand Up @@ -99,18 +101,36 @@ void TRModuleWidget::updateControls(const Flags<ModuleWidget::UpdateFlags> &upda
"Calculated");
auto boundTotal = trGraph_->createRenderable<RenderableData1D>(
std::format("{}//WeightedTR//BoundTotal", module_->name()), "Bound T(R)", "Calculated");
boundTotal->setColour(StockColours::GreenStockColour);
boundTotal->lineStyle().setStipple(LineStipple::DotStipple);
auto unboundTotal = trGraph_->createRenderable<RenderableData1D>(
std::format("{}//WeightedTR//UnboundTotal", module_->name()), "Unbound T(R)", "Calculated");
unboundTotal->setColour(StockColours::GreenStockColour);
unboundTotal->lineStyle().setStipple(LineStipple::HalfDashStipple);
auto refTR = trGraph_->createRenderable<RenderableData1D>(std::format("{}//ReferenceTR", module_->name()),
"Reference T(r)", "Reference");
refTR->setColour(StockColours::RedStockColour);
auto repTR = trGraph_->createRenderable<RenderableData1D>(
std::format("{}//RepresentativeTR//Total", module_->name()), "Via FT T(R)", "Calculated");
repTR->lineStyle().setStipple(LineStipple::HalfDashStipple);
repTR->setColour(StockColours::GreenStockColour);
auto repBoundTotal = trGraph_->createRenderable<RenderableData1D>(
std::format("{}//RepresentativeTR//BoundTotal", module_->name()), "Via FT Bound T(R)", "Calculated");
repBoundTotal->setColour(StockColours::GreenStockColour);
repBoundTotal->lineStyle().setStipple(LineStipple::DotStipple);
auto repUnboundTotal = trGraph_->createRenderable<RenderableData1D>(
std::format("{}//RepresentativeTR//UnboundTotal", module_->name()), "Via FT Unbound T(R)", "Calculated");
repUnboundTotal->setColour(StockColours::GreenStockColour);
repUnboundTotal->lineStyle().setStipple(LineStipple::HalfDashStipple);
}
else if (ui_.PartialsButton->isChecked())
{
targetPartials_ = dissolve_.processingModuleData().valueIf<PartialSet>("WeightedTR", module_->name());
createPartialSetRenderables("WeightedTR");
}
else if (ui_.RepresentativePartialsButton->isChecked())
{
targetPartials_ = dissolve_.processingModuleData().valueIf<PartialSet>("RepresentativeTR", module_->name());
createPartialSetRenderables("RepresentativeTR");
}
}

// Validate renderables if they need it
Expand Down Expand Up @@ -142,12 +162,25 @@ void TRModuleWidget::on_TotalButton_clicked(bool checked)
if (!checked)
return;

ui_.ConfigurationTargetCombo->setEnabled(false);

trGraph_->view().axes().setTitle(1, "T(r)");
trGraph_->groupManager().setVerticalShiftAmount(RenderableGroupManager::OneVerticalShift);

updateControls(ModuleWidget::RecreateRenderablesFlag);
}

void TRModuleWidget::on_RepresentativePartialsButton_clicked(bool checked)
{
if (!checked)
return;

trGraph_->view().axes().setTitle(1, "t(r)");
trGraph_->groupManager().setVerticalShiftAmount(RenderableGroupManager::TwoVerticalShift);

updateControls(ModuleWidget::RecreateRenderablesFlag);
}

void TRModuleWidget::on_FilterEdit_textChanged(QString text) { updateControls(ModuleWidget::RecreateRenderablesFlag); }

void TRModuleWidget::on_ClearFilterButton_clicked(bool checked)
Expand Down
119 changes: 116 additions & 3 deletions src/modules/tr/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "keywords/module.h"
#include "main/dissolve.h"
#include "math/ft.h"
#include "module/context.h"
#include "modules/gr/gr.h"
#include "modules/neutronSQ/neutronSQ.h"
Expand All @@ -21,6 +22,7 @@ Module::ExecutionResult TRModule::process(ModuleContext &moduleContext)
return ExecutionResult::Failed;
}

// Get target SQ module
auto optSQModule = sourceNeutronSQ_->keywords().get<const SQModule *, ModuleKeyword<const SQModule>>("SourceSQs");
const SQModule *sqModule{nullptr};
if (optSQModule)
Expand All @@ -32,25 +34,131 @@ Module::ExecutionResult TRModule::process(ModuleContext &moduleContext)
return ExecutionResult::Failed;
}

// Get target gr module
auto *grModule = sqModule->sourceGR();
if (!sqModule)
{
Messenger::error("A source GR module (in the target SQ module) must be set.\n");
return ExecutionResult::Failed;
}

// Retrieve weights, GR and SQ
const auto &weights = moduleData.value<NeutronWeights>("FullWeights", sourceNeutronSQ_->name());
// Retrieve GR and SQ
const auto unweightedGR = moduleData.value<PartialSet>("UnweightedGR", grModule->name());
auto unweightedSQ = moduleData.value<PartialSet>("UnweightedSQ", sqModule->name());
auto referenceSQ = moduleData.value<Data1D>("ReferenceData", sourceNeutronSQ_->name());

// Make weightedGR Partial set
PartialSet representativeGR;
representativeGR.setUpPartials(unweightedSQ.atomTypeMix(), false);

// Get effective atomic density of underlying g(r)
const auto rho = grModule->effectiveDensity();

// Create weightedTR PartialSet in rtstart file
auto [weightedTR, wGRstatus] = moduleContext.dissolve().processingModuleData().realiseIf<PartialSet>(
"WeightedTR", name_, GenericItem::InRestartFileFlag);
if (wGRstatus == GenericItem::ItemStatus::Created)
weightedTR.setUpPartials(unweightedGR.atomTypeMix(), false);

// Retrieve weights
const auto &weights = moduleData.value<NeutronWeights>("FullWeights", sourceNeutronSQ_->name());
// Get Q-range and window function to use for transformation of reference F(Q) to G(r)
auto refftQMin = refQMin_.value_or(0.0);
auto refftQMax = refQMax_.value();
if (refWindowFunction_ == WindowFunction::Form::None)
Messenger::print("TR: No window function will be applied in Fourier transform of S(Q) to g(r).");
else
Messenger::print("TR: Window function to be applied in Fourier transform of S(Q) is {}.",
WindowFunction::forms().keyword(refWindowFunction_));

// FT Reference data to ReresentativeTotalGR
Fourier::sineFT(referenceSQ, 1.0 / (2 * PI * PI * rho.value()), refftQMin, qDelta_, refftQMax, refWindowFunction_,
refQBroadening_);

// Get Q-range and window function to use for transformation of total F(Q) to G(r)
auto repftQMin = repQMin_.value_or(0.0);
auto repftQMax = repQMax_.value();
if (repWindowFunction_ == WindowFunction::Form::None)
Messenger::print("TR: No window function will be applied in Fourier transform of S(Q) to g(r).");
else
Messenger::print("TR: Window function to be applied in Fourier transform of S(Q) is {}.",
WindowFunction::forms().keyword(repWindowFunction_));
// FT unweightedSQ to unweightedGR to get better representation of calculations
dissolve::for_each_pair(
ParallelPolicies::par, 0, unweightedSQ.nAtomTypes(),
[&](int n, int m)
{
// Total partial
representativeGR.partial(n, m).copyArrays(unweightedSQ.partial(n, m));
Fourier::sineFT(representativeGR.partial(n, m), 1.0 / (2 * PI * PI * rho.value()), repftQMin, qDelta_, repftQMax,
WindowFunction::Form::None, repQBroadening_);
representativeGR.partial(n, m) += 1.0;

// Bound partial
representativeGR.boundPartial(n, m).copyArrays(unweightedSQ.boundPartial(n, m));
Fourier::sineFT(representativeGR.boundPartial(n, m), 1.0 / (2 * PI * PI * rho.value()), repftQMin, qDelta_,
repftQMax, WindowFunction::Form::None, repQBroadening_);

// Unbound partial
representativeGR.unboundPartial(n, m).copyArrays(unweightedSQ.unboundPartial(n, m));
Fourier::sineFT(representativeGR.unboundPartial(n, m), 1.0 / (2 * PI * PI * rho.value()), repftQMin, qDelta_,
repftQMax, WindowFunction::Form::None, repQBroadening_);
representativeGR.unboundPartial(n, m) += 1.0;
},
false);

// Calculate TR from GR
auto [referenceCalcTR, bGRstatus] =
moduleContext.dissolve().processingModuleData().realiseIf<Data1D>("ReferenceTR", name_, GenericItem::InRestartFileFlag);

referenceCalcTR.copyArrays(referenceSQ);
// T(r)=4 * PI * x * rho * (G(r) + BCAS)
// 1) (G(r) + BCAS)
referenceCalcTR += weights.boundCoherentAverageOfSquares();
// 2) 4 * PI * x * rho
referenceCalcTR *= 4 * PI * rho.value();
referenceCalcTR *= referenceCalcTR.xAxis();

// Calculate RepresentativeTR
auto [representativeTR, rTRstatus] = moduleContext.dissolve().processingModuleData().realiseIf<PartialSet>(
"RepresentativeTR", name_, GenericItem::InRestartFileFlag);
if (rTRstatus == GenericItem::ItemStatus::Created)
representativeTR.setUpPartials(representativeGR.atomTypeMix(), false);

dissolve::for_each_pair(
ParallelPolicies::par, 0, representativeGR.nAtomTypes(),
[&weights, &rho, &representativeGR, &representativeTR](const auto typeI, const auto typeJ)
{
double intraWeight = weights.intramolecularWeight(typeI, typeJ);
auto cj = weights.atomTypes()[typeJ].fraction();
auto factor = 4.0 * PI * rho.value() * cj;
representativeTR.boundPartial(typeI, typeJ).copyArrays(representativeGR.boundPartial(typeI, typeJ));
representativeTR.unboundPartial(typeI, typeJ).copyArrays(representativeGR.unboundPartial(typeI, typeJ));
representativeTR.partial(typeI, typeJ).copyArrays(representativeGR.partial(typeI, typeJ));
representativeTR.boundPartial(typeI, typeJ).copyArrays(representativeGR.boundPartial(typeI, typeJ));
for (auto &&[x, y] :
zip(representativeTR.boundPartial(typeI, typeJ).xAxis(), representativeTR.boundPartial(typeI, typeJ).values()))
{
y *= x * factor;
}
// Unbound partial (multiplied by the full weight)
representativeTR.unboundPartial(typeI, typeJ).copyArrays(representativeGR.unboundPartial(typeI, typeJ));
for (auto &&[x, y] : zip(representativeTR.unboundPartial(typeI, typeJ).xAxis(),
representativeTR.unboundPartial(typeI, typeJ).values()))
{
y *= x * factor;
}
// Full partial, summing bound and unbound terms
representativeTR.partial(typeI, typeJ).copyArrays(representativeGR.partial(typeI, typeJ));
for (auto &&[x, y] :
zip(representativeTR.partial(typeI, typeJ).xAxis(), representativeTR.partial(typeI, typeJ).values()))
{
y *= x * factor;
}
},
false);

// Calculate weightedTR
dissolve::for_each_pair(
ParallelPolicies::seq, 0, unweightedGR.nAtomTypes(),
[&weights, &rho, &unweightedGR, &weightedTR](const auto typeI, const auto typeJ)
Expand Down Expand Up @@ -84,12 +192,17 @@ Module::ExecutionResult TRModule::process(ModuleContext &moduleContext)
},
false);

// Sum into total
// Sum into totals
weightedTR.formTRTotals(weights);
representativeTR.formTRTotals(weights);

// Save data if requested
if (saveTR_ && (!MPIRunMaster(moduleContext.processPool(), weightedTR.save(name_, "WeightedTR", "tr", "Q, 1/Angstroms"))))
return ExecutionResult::Failed;
// Save data if requested
if (saveRepTR_ &&
(!MPIRunMaster(moduleContext.processPool(), representativeTR.save(name_, "RepresentativeTR", "tr", "Q, 1/Angstroms"))))
return ExecutionResult::Failed;

return ExecutionResult::Success;
}
Loading

0 comments on commit 80fa8cb

Please sign in to comment.