46 throw std::runtime_error(
"ReplicaExchangeJob::runFromMatter: null Matter");
52 if (rex.replicas < 1) {
53 throw std::invalid_argument(
54 "ReplicaExchangeJob: replica_exchange.replicas must be >= 1");
56 if (rex.temperature_low <= 0.0) {
57 rex.temperature_low =
params.main_options().temperature > 0.0
58 ?
params.main_options().temperature
61 if (rex.temperature_high <= rex.temperature_low) {
62 rex.temperature_high = rex.temperature_low * 1.5;
66 static_cast<long>(
params.replica_exchange_options().sampling_time /
67 params.dynamics_options().time_step +
69 long exchangePeriodSteps =
70 static_cast<long>(
params.replica_exchange_options().exchange_period /
71 params.dynamics_options().time_step +
73 const double kB =
params.constants().kB;
74 if (samplingSteps <= 0)
76 if (exchangePeriodSteps <= 0)
77 exchangePeriodSteps = 1;
79 QUILL_LOG_DEBUG(
log,
"Running Replica Exchange");
81 const long refForceCalls =
static_cast<long>(
pot->forceCallCounter.load());
83 const long nReplicas =
params.replica_exchange_options().replicas;
84 std::vector<std::shared_ptr<Matter>> replica(nReplicas);
85 std::vector<std::unique_ptr<Dynamics>> replicaDynamics(nReplicas);
89 const bool perImage =
pot->needsPerImageInstance();
92 std::vector<std::shared_ptr<Potential>> replicaPots;
93 for (
long i = 0; i < nReplicas; i++) {
99 replicaPots.push_back(replicaPot);
101 replica[i] = std::make_shared<Matter>(replicaPot,
params);
103 replica[i]->setPotential(replicaPot);
104 replicaDynamics[i] = std::make_unique<Dynamics>(replica[i].get(),
params);
107 std::vector<double> replicaTemperature(nReplicas);
109 QUILL_LOG_DEBUG(
log,
"Temperature distribution:");
111 replicaTemperature[0] =
params.replica_exchange_options().temperature_low;
112 replicaDynamics[0]->setTemperature(replicaTemperature[0]);
113 replicaDynamics[0]->setThermalVelocity();
114 }
else if (
params.replica_exchange_options().temperature_distribution ==
116 for (
long i = 0; i < nReplicas; i++) {
117 replicaTemperature[i] =
118 params.replica_exchange_options().temperature_low +
119 static_cast<double>(i) /
static_cast<double>(nReplicas - 1) *
120 (
params.replica_exchange_options().temperature_high -
121 params.replica_exchange_options().temperature_low);
122 replicaDynamics[i]->setTemperature(replicaTemperature[i]);
123 replicaDynamics[i]->setThermalVelocity();
125 }
else if (
params.replica_exchange_options().temperature_distribution ==
127 double kTemp = std::log(
params.replica_exchange_options().temperature_high /
128 params.replica_exchange_options().temperature_low) /
129 static_cast<double>(nReplicas - 1);
130 for (
long i = 0; i < nReplicas; i++) {
131 replicaTemperature[i] =
132 params.replica_exchange_options().temperature_low *
133 std::exp(kTemp *
static_cast<double>(i));
134 replicaDynamics[i]->setTemperature(replicaTemperature[i]);
135 replicaDynamics[i]->setThermalVelocity();
136 QUILL_LOG_DEBUG(
log,
"replica: {} temperature {:.0f}", i + 1,
137 replicaTemperature[i]);
140 throw std::invalid_argument(
141 "replica_exchange.temperature_distribution must be linear or "
146 log,
"Replica Exchange sampling for {:.0f} fs; {} steps; {} replicas.",
147 params.replica_exchange_options().sampling_time * 10.18, samplingSteps,
148 params.replica_exchange_options().replicas);
151 const bool canParallel =
params.main_options().parallel &&
154 for (
long step = 1; step <= samplingSteps; step++) {
155 if (canParallel && nReplicas > 1) {
157 std::vector<std::thread> threads;
158 threads.reserve(nReplicas);
159 for (
long i = 0; i < nReplicas; i++) {
160 threads.emplace_back([&, i] {
163 const long userSeed =
params.main_options().randomSeed;
164 const long base = (userSeed > 0) ? userSeed : 1;
166 replicaDynamics[i]->oneStep();
169 for (
auto &t : threads) {
173 for (
long i = 0; i < nReplicas; i++) {
174 replicaDynamics[i]->oneStep();
179 if (nReplicas >= 2 && (step % exchangePeriodSteps) == 0) {
181 trial <
params.replica_exchange_options().exchange_trials; trial++) {
183 double energyLow = replica[i]->getPotentialEnergy();
184 double energyHigh = replica[i + 1]->getPotentialEnergy();
185 double kbTLow = kB * replicaTemperature[i];
186 double kbTHigh = kB * replicaTemperature[i + 1];
188 std::min(1.0, std::exp((energyHigh - energyLow) *
193 "step: {} trial swap, i {}, elow: {:.5f}, ehigh: "
194 "{:.5f}, pAcc: {:.5f}, rand: {}",
195 step, i, energyLow, energyHigh, pAcc, rnd);
197 QUILL_LOG_INFO(
log,
"swap");
199 auto potLow = replica[i]->getPotential();
200 auto potHigh = replica[i + 1]->getPotential();
202 *replica[i] = *replica[i + 1];
203 *replica[i + 1] = tmp;
204 replica[i]->setPotential(potLow);
205 replica[i + 1]->setPotential(potHigh);
206 replicaDynamics[i]->setThermalVelocity();
207 replicaDynamics[i + 1]->setThermalVelocity();
209 QUILL_LOG_INFO(
log,
"no swap");
215 forceCalls =
static_cast<long>(
pot->forceCallCounter.load()) - refForceCalls;
216 for (
const auto &replicaPot : replicaPots) {
217 forceCalls +=
static_cast<long>(replicaPot->forceCallCounter.load());
219 if (!replica.empty()) {