Skip to content

Commit 05bb033

Browse files
refactor: Retrofit AtomicMC MPI (#2205)
1 parent 4040c83 commit 05bb033

1 file changed

Lines changed: 44 additions & 73 deletions

File tree

src/nodes/atomicMC/process.cpp

Lines changed: 44 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
// Copyright (c) 2025 Team Dissolve and contributors
13

24
#include "base/timer.h"
35
#include "classes/box.h"
4-
#include "classes/changeStore.h"
56
#include "classes/configuration.h"
6-
#include "classes/regionalDistributor.h"
77
#include "kernels/producer.h"
88
#include "main/dissolve.h"
99
#include "math/mathFunc.h"
@@ -30,11 +30,6 @@ NodeConstants::ProcessResult AtomicMCNode::process()
3030
message("Target acceptance rate is {}.\n", targetAcceptanceRate);
3131
message("\n");
3232

33-
// Create a Molecule distributor
34-
RegionalDistributor distributor(targetConfiguration_->nMolecules(), targetConfiguration_->cells());
35-
36-
// Create a local ChangeStore and EnergyKernel
37-
ChangeStore changeStore;
3833
auto kernel =
3934
KernelProducer::energyKernel(targetConfiguration_, dissolve().potentialMap(), dissolve().pairPotentialRange());
4035

@@ -45,86 +40,62 @@ NodeConstants::ProcessResult AtomicMCNode::process()
4540
EnergyResult er;
4641

4742
Timer timer;
48-
while (distributor.cycle())
43+
// Loop over target Molecules
44+
for (auto mol : targetConfiguration_->molecules())
4945
{
50-
// Get next set of Molecule targets from the distributor
51-
auto &targetMolecules = distributor.assignedMolecules();
46+
/*
47+
* Calculation Begins
48+
*/
5249

53-
// Loop over target Molecules
54-
for (auto molId : targetMolecules)
50+
// Loop over atoms in the Molecule
51+
for (const auto &i : mol->atoms())
5552
{
56-
/*
57-
* Calculation Begins
58-
*/
53+
// Calculate reference energies for the Atom
54+
er = kernel->totalEnergy(*i);
55+
currentEnergy = er.totalUnbound();
56+
currentIntraEnergy = er.geometry() * termScale;
5957

60-
// Get Molecule pointer
61-
std::shared_ptr<Molecule> mol = targetConfiguration_->molecule(molId);
58+
// Loop over number of shakes per Atom
59+
for (auto n = 0; n < nShakesPerAtom; ++n)
60+
{
61+
auto moveInitialPos = i->r();
6262

63-
// Set current Atom targets in ChangeStore (whole Molecule)
64-
changeStore.add(mol);
65-
auto storeIndex = 0;
63+
// Create a random translation vector
64+
rDelta.set(DissolveMath::randomPlusMinusOne() * stepSize, DissolveMath::randomPlusMinusOne() * stepSize,
65+
DissolveMath::randomPlusMinusOne() * stepSize);
6666

67-
// Loop over atoms in the Molecule
68-
for (const auto &i : mol->atoms())
69-
{
70-
// Calculate reference energies for the Atom
67+
// Translate Atom and update its Cell position
68+
i->translateCoordinates(rDelta);
69+
targetConfiguration_->updateAtomLocation(i);
70+
71+
// Calculate new energy
7172
er = kernel->totalEnergy(*i);
72-
currentEnergy = er.totalUnbound();
73-
currentIntraEnergy = er.geometry() * termScale;
73+
newEnergy = er.totalUnbound();
74+
newIntraEnergy = er.geometry() * termScale;
7475

75-
// Loop over number of shakes per Atom
76-
for (auto n = 0; n < nShakesPerAtom; ++n)
77-
{
78-
// Create a random translation vector
79-
rDelta.set(DissolveMath::randomPlusMinusOne() * stepSize, DissolveMath::randomPlusMinusOne() * stepSize,
80-
DissolveMath::randomPlusMinusOne() * stepSize);
76+
// Trial the transformed Atom position
77+
delta = (newEnergy + newIntraEnergy) - (currentEnergy + currentIntraEnergy);
78+
accept = delta < 0 ? true : (DissolveMath::random() < exp(-delta * rRT));
8179

82-
// Translate Atom and update its Cell position
83-
i->translateCoordinates(rDelta);
80+
// Increase attempt counters
81+
if (accept)
82+
{
83+
totalDelta += delta;
84+
++nAccepted;
85+
}
86+
else
87+
{
88+
// Move not accepted - revert to initial position
89+
i->setCoordinates(moveInitialPos);
8490
targetConfiguration_->updateAtomLocation(i);
85-
86-
// Calculate new energy
87-
er = kernel->totalEnergy(*i);
88-
newEnergy = er.totalUnbound();
89-
newIntraEnergy = er.geometry() * termScale;
90-
91-
// Trial the transformed Atom position
92-
delta = (newEnergy + newIntraEnergy) - (currentEnergy + currentIntraEnergy);
93-
accept = delta < 0 ? true : (DissolveMath::random() < exp(-delta * rRT));
94-
95-
if (accept)
96-
{
97-
// Accept new (current) position of target Atom
98-
changeStore.updateAtom(storeIndex);
99-
currentEnergy = newEnergy;
100-
}
101-
else
102-
changeStore.revert(storeIndex);
103-
104-
// Increase attempt counters
105-
if (accept)
106-
{
107-
totalDelta += delta;
108-
++nAccepted;
109-
}
110-
++nAttempts;
11191
}
112-
113-
// Increment index of target atom in ChangeStore
114-
++storeIndex;
92+
++nAttempts;
11593
}
116-
117-
// Store modifications to Atom positions ready for broadcast later
118-
changeStore.storeAndReset();
119-
120-
/*
121-
* Calculation End
122-
*/
12394
}
12495

125-
// Now all target Molecules have been processes, broadcast the changes made
126-
changeStore.apply(targetConfiguration_);
127-
changeStore.reset();
96+
/*
97+
* Calculation End
98+
*/
12899
}
129100

130101
timer.stop();

0 commit comments

Comments
 (0)