Skip to content

Commit

Permalink
Use optional integer to store exchange partner (#455)
Browse files Browse the repository at this point in the history
* Formatting

* Use optional exchange instead of double

This uses an optional integer to represent exchange MPI rank.
  • Loading branch information
mlund authored Jul 19, 2024
1 parent 88331e7 commit ed4dc6a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ void ParallelTempering::_move(Change& change)
}
partner->generate(mpi.world, slump);
if (partner->rank.has_value()) {
exchangeState(change);
exchangeState(change);
}
}

Expand Down Expand Up @@ -921,7 +921,7 @@ void ParallelTempering::_accept([[maybe_unused]] Change& change)
void ParallelTempering::_reject([[maybe_unused]] Change& change)
{
acceptance_map[partner->getPair(mpi.world)] += 0.0;
exchange = -1.0;
exchange = std::nullopt;
writeToFileStream();
}

Expand All @@ -941,8 +941,7 @@ void ParallelTempering::_from_json(const json& j)
void ParallelTempering::writeToFileStream() const
{
if (stream) {
// file to disk?:
*stream << fmt::format("{:d} {:.6E}\n", number_of_attempted_moves , exchange);
*stream << fmt::format("{:d} {}\n", number_of_attempted_moves, exchange.value_or(-1));
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/move.h
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ class ParallelTempering : public Move
Geometry::VolumeMethod volume_scaling_method =
Geometry::VolumeMethod::ISOTROPIC; //!< How to scale volumes
std::map<MPI::Partner::PartnerPair, Average<double>> acceptance_map; //!< Exchange statistics

Random slump; // static instance of Random (shared for all in ParallelTempering)
void _to_json(json& j) const override;
void _from_json(const json& j) override;
Expand All @@ -588,11 +588,11 @@ class ParallelTempering : public Move
double exchangeEnergy(double energy_change); //!< Exchange energy with partner
void exchangeState(Change& change); //!< Exchange positions, charges, volume etc.
void exchangeGroupSizes(Space::GroupVector& groups, int partner_rank);
std::string filename; //file name for exchange statistics
std::unique_ptr<std::ostream> stream; //log exchange statistics in file
double exchange; // if no exchange, this is 0
void writeToFileStream() const; //!< Write exchange statistics to file

std::string filename; //!< File name for exchange statistics
std::unique_ptr<std::ostream> stream; //!< Log exchange statistics in file
std::optional<int> exchange; //!< Rank of exchange partner, if any
void writeToFileStream() const; //!< Write exchange statistics to file

public:
explicit ParallelTempering(Space& spc, const MPI::Controller& mpi);
Expand Down

0 comments on commit ed4dc6a

Please sign in to comment.