Skip to content

Commit

Permalink
added in PotentialSet changes to epsrManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Danbr4d committed Oct 31, 2024
1 parent 8e345d2 commit 0e0b8e9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
9 changes: 1 addition & 8 deletions src/modules/epsrManager/epsrManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include "classes/potentialSet.h"
#include "classes/scatteringMatrix.h"
#include "generator/generator.h"
#include "math/averaging.h"
Expand All @@ -28,20 +29,12 @@ class EPSRManagerModule : public Module
std::optional<int> modifyPotential_{1};
// Vector storing atom pairs and associated potentials
std::vector<std::tuple<std::shared_ptr<AtomType>, std::shared_ptr<AtomType>, Data1D>> potentials_;
struct EPData
{
Data1D ep;
double count{0};
std::shared_ptr<AtomType> at1, at2;
};
// Potential scalings
std::string potentialScalings_;
// Number of historical partial sets to combine into final partials
std::optional<int> averagingLength_;
// Weighting scheme to use when averaging partials
Averaging::AveragingScheme averagingScheme_{Averaging::LinearAveraging};
// Vector of averaged potentials over multiple iterations
std::vector<std::map<std::string, EPData>> averagedPotentialsStore;

/*
* Functions
Expand Down
71 changes: 43 additions & 28 deletions src/modules/epsrManager/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,19 @@ bool EPSRManagerModule::setUp(ModuleContext &moduleContext, Flags<KeywordBase::K
// Run main processing
Module::ExecutionResult EPSRManagerModule::process(ModuleContext &moduleContext)
{
if (averagingLength_)
Messenger::print("Potentials will be averaged over {} sets (scheme = {}).\n", averagingLength_.value(),
Averaging::averagingSchemes().keyword(averagingScheme_));
else
Messenger::print("Potentials: No averaging of potentials will be performed.\n");

auto &moduleData = moduleContext.dissolve().processingModuleData();

std::map<std::string, EPData> potentials;
// Does a PotentialSet already exist for this Configuration?
auto originalPotentialsObject =
moduleData.realiseIf<PotentialSet>(fmt::format("AveragingPotentials"), name_, GenericItem::InRestartFileFlag);

PotentialSet potentials = originalPotentialsObject.first;

// Loop over target data
for (auto *module : target_)
Expand All @@ -38,40 +48,45 @@ Module::ExecutionResult EPSRManagerModule::process(ModuleContext &moduleContext)
for (auto &&[at1, at2, ep] : eps)
{
auto key = EPSRManagerModule::pairKey(at1, at2);
auto keyIt = potentials.find(key);
if (keyIt == potentials.end())
potentials[key] = {ep, 1, at1, at2};
auto keyIt = potentials.potential().find(key);
if (keyIt == potentials.potential().end())
potentials.potential()[key] = {ep, 1, at1, at2};
else
{
Interpolator::addInterpolated(ep, potentials[key].ep, 1.0);
++potentials[key].count;
Interpolator::addInterpolated(ep, potentials.potential()[key].ep, 1.0);
++potentials.potential()[key].count;
}
}
}

// Form averages
for (auto &&[key, epData] : potentials)
epData.ep /= epData.count;
// Perform averaging of potentials data if requested
if (averagingLength_)
Averaging::average<std::vector<PotentialSet>>(moduleContext.dissolve().processingModuleData(), "AveragingPotentials",
name(), averagingLength_.value(), averagingScheme_);

std::map<std::string, EPData> averagedPotentials = potentials;
/* // Form averages
for (auto &&[key, epData] : potentials)
epData.ep /= epData.count;
averagedPotentialsStore.emplace_back(potentials);
// Check if ran the right amount of iterations before averaging
if (averagedPotentialsStore.size() > averagingLength_)
{
averagedPotentialsStore.pop_back();
}
std::map<std::string, EPData> averagedPotentials = potentials;
// Average the potentials and replace the map with the new averaged
for (const auto &pots : averagedPotentialsStore)
{
for (auto &&[key, epData] : pots)
{
averagedPotentials[key].ep += epData.ep;
averagedPotentials[key].ep /= averagingLength_.value();
}
}
potentials = averagedPotentials;
averagedPotentialsStore.emplace_back(potentials);
// Check if ran the right amount of iterations before averaging
if (averagedPotentialsStore.size() > averagingLength_)
{
averagedPotentialsStore.pop_back();
}
// Average the potentials and replace the map with the new averaged
for (const auto &pots : averagedPotentialsStore)
{
for (auto &&[key, epData] : pots)
{
averagedPotentials[key].ep += epData.ep;
averagedPotentials[key].ep /= averagingLength_.value();
}
}
potentials = averagedPotentials; */

// Apply potential scalings
auto scalings = DissolveSys::splitString(potentialScalings_, ",");
Expand All @@ -91,7 +106,7 @@ Module::ExecutionResult EPSRManagerModule::process(ModuleContext &moduleContext)

Messenger::print("Apply scaling factor of {} to potential(s) {}-{}...\n", scaleFactor, typeA, typeB);
auto count = 0;
for (auto &&[key, epData] : potentials)
for (auto &&[key, epData] : potentials.potential())
{
// Is this potential a match
if ((DissolveSys::sameWildString(typeA, epData.at1->name()) &&
Expand All @@ -108,7 +123,7 @@ Module::ExecutionResult EPSRManagerModule::process(ModuleContext &moduleContext)
}

// Adjust global potentials
for (auto &&[key, epData] : potentials)
for (auto &&[key, epData] : potentials.potential())
{
// Grab pointer to the relevant pair potential (if it exists)
auto *pp = moduleContext.dissolve().pairPotential(epData.at1, epData.at2);
Expand Down

0 comments on commit 0e0b8e9

Please sign in to comment.