4 #include "mpicontroller.h" 7 #include "smart_montecarlo.h" 8 #include "aux/timers.h" 9 #include "aux/sparsehistogram.h" 10 #include <range/v3/view/filter.hpp> 11 #include <range/v3/view/iota.hpp> 12 #include <range/v3/view/transform.hpp> 13 #include <range/v3/view/indirect.hpp> 17 namespace Speciation {
18 class GroupDeActivator;
41 virtual void _move(
Change& change) = 0;
42 virtual void _accept(
Change& change);
43 virtual void _reject(
Change& change);
44 virtual void _to_json(
json& j)
const = 0;
45 virtual void _from_json(
const json& j) = 0;
50 unsigned long number_of_accepted_moves = 0;
51 unsigned long number_of_rejected_moves = 0;
52 unsigned int sweep_interval = 1;
53 const std::string cite;
59 unsigned long number_of_attempted_moves = 0;
64 void from_json(
const json& j);
65 void to_json(
json& j)
const;
67 void accept(
Change& change);
68 void reject(
Change& change);
69 void setRepeat(
int repeat);
70 virtual double bias(
Change& change,
double old_energy,
72 Move(
Space& spc, std::string_view name, std::string_view cite);
73 inline virtual ~
Move() =
default;
74 [[nodiscard]]
bool isStochastic()
const;
75 [[nodiscard]]
const std::string& getName()
const;
89 std::unique_ptr<XTCReader> reader =
nullptr;
91 bool end_of_trajectory =
false;
93 const double force_accept =
96 void _move(
Change& change)
override;
97 void _to_json(
json&)
const override;
98 void _from_json(
const json&)
override;
99 double bias(
Change&,
double,
double)
override;
103 ReplayMove(
Space& spc,
const std::string& name,
const std::string& cite);
121 void _to_json(
json&)
const override;
122 void _from_json(
const json&)
override;
123 typename ParticleVector::iterator randomAtom();
124 void _move(
Change& change)
override;
125 double bias(
Change&,
double,
127 void _accept(
Change&)
override;
128 void _reject(
Change&)
override;
143 ParticleVector::const_iterator latest_particle;
145 std::map<int, SparseHistogram<>>
147 double energy_resolution = 0.0;
148 double latest_displacement_squared;
149 double default_dp = 0.0;
150 double default_dprot = 0.0;
151 void sampleEnergyHistogram();
152 void saveHistograms();
153 void checkMassCenter(
165 void _to_json(
json&)
const override;
166 void _from_json(
const json&)
override;
167 ParticleVector::iterator randomAtom();
169 virtual void translateParticle(ParticleVector::iterator,
double);
170 void _move(
Change&)
override;
171 void _accept(
Change&)
override;
172 void _reject(
Change&)
override;
175 const std::string& name,
const std::string& cite);
238 using OptionalGroup = std::optional<std::reference_wrapper<Space::GroupType>>;
240 void _to_json(
json& j)
const override;
247 double latest_displacement_squared = 0.0;
248 double latest_rotation_angle_squared = 0.0;
249 double translational_displacement = 0.0;
250 double rotational_displacement = 0.0;
251 Point translational_direction = {1, 1, 1};
252 Point fixed_rotation_axis = {0, 0, 0};
254 virtual OptionalGroup findRandomMolecule();
259 void _from_json(
const json& j)
override;
260 void _move(
Change& change)
override;
261 void _accept(
Change&)
override;
262 void _reject(
Change&)
override;
281 void _to_json(
json& j)
const override;
282 OptionalGroup findRandomMolecule()
override;
283 double bias(
Change& change,
double old_energy,
double new_energy)
override;
305 enum class CopyPolicy
314 CopyPolicy copy_policy;
318 ParticleVector::iterator destination)
const;
319 void _to_json(
json& j)
const override;
320 void _from_json(
const json& j)
override;
321 void _move(
Change& change)
override;
323 void checkConformationSize()
const;
324 void checkMassCenterDrift(
const Point& old_mass_center,
326 void registerChanges(
Change& change,
335 {{ConformationSwap::CopyPolicy::INVALID,
nullptr},
336 {ConformationSwap::CopyPolicy::ALL,
"all"},
337 {ConformationSwap::CopyPolicy::PATCHES,
"patches"},
338 {ConformationSwap::CopyPolicy::POSITIONS,
"positions"},
339 {ConformationSwap::CopyPolicy::CHARGES,
"charges"}})
341 class VolumeMove :
public Move 345 double logarithmic_volume_displacement_factor = 0.0;
346 double old_volume = 0.0;
347 double new_volume = 0.0;
348 void _move(
Change& change)
override;
349 void _from_json(
const json& j)
override;
355 virtual void setNewVolume();
356 void _to_json(
json& j)
const override;
357 void _accept(
Change& change)
override;
358 void _reject(
Change& change)
override;
361 VolumeMove(
Space& spc, std::string_view name);
362 explicit VolumeMove(
Space& spc);
374 void _move(
Change& change)
override;
375 void _accept(
Change&)
override;
376 void _reject(
Change&)
override;
377 void _from_json(
const json& j)
override;
378 [[nodiscard]]
virtual double getChargeDisplacement(
const Particle& particle)
const;
382 double max_charge_displacement = 0.0;
383 double charge_displacement = 0.0;
384 ParticleVector::size_type particle_index;
385 void _to_json(
json& j)
const override;
401 void _to_json(
json& j)
const override;
402 [[nodiscard]]
double getChargeDisplacement(
const Particle& particle)
const override;
403 double bias(
Change& change,
double old_energy,
double new_energy)
override;
416 double dq = 0, deltaq = 0;
422 size_t numOfAtoms = 0;
425 std::vector<double> min, max;
426 std::vector<double> molrange;
427 std::vector<double> ratio;
428 std::vector<double> changeQ;
437 void _to_json(
json& j)
const override;
438 void _from_json(
const json& j)
override;
439 void _move(
Change& change)
override;
440 void _accept(
Change&)
override;
441 void _reject(
Change&)
override;
460 Point dir = {1, 1, 1};
461 std::vector<size_t> index;
465 void _to_json(
json& j)
const override;
466 void _from_json(
const json& j)
override;
467 void _move(
Change& change)
override;
469 void _accept(
Change&)
override { msqd += _sqd; }
471 void _reject(
Change&)
override { msqd += 0; }
492 class GibbsEnsembleHelper
495 using VectorOfMolIds = std::vector<MoleculeData::index_type>;
496 const MPI::Controller& mpi;
497 int partner_rank = -1;
498 double total_volume = 0;
499 int total_num_particles = 0;
500 VectorOfMolIds molids;
502 currentNumParticles(
const Space& spc)
const;
503 std::pair<double, double>
504 currentVolumes(
const Space& spc)
const;
505 double exchange(
double value)
const;
506 GibbsEnsembleHelper(
const Space& spc,
const MPI::Controller& mpi,
const VectorOfMolIds& molids);
512 class GibbsVolumeMove :
public VolumeMove
515 MPI::Controller& mpi;
516 std::unique_ptr<GibbsEnsembleHelper> gibbs;
517 bool direct_volume_displacement =
519 void setNewVolume()
override;
520 void _from_json(
const json& j)
override;
521 bool volumeTooExtreme()
const;
524 void _move(
Change& change)
override;
527 GibbsVolumeMove(
Space& spc, MPI::Controller& mpi);
528 double bias(
Change& change,
double old_energy,
double new_energy)
override;
537 class GibbsMatterMove :
public Move 541 MoleculeData::index_type molid;
542 MPI::Controller& mpi;
543 std::unique_ptr<GibbsEnsembleHelper> gibbs;
544 std::unique_ptr<Speciation::GroupDeActivator> molecule_bouncer;
545 void _from_json(
const json& j)
override;
546 void _to_json(
json& j)
const override;
549 void _move(
Change& change)
override;
552 GibbsMatterMove(
Space& spc, MPI::Controller& mpi);
553 double bias(
Change& change,
double old_energy,
double new_energy)
override;
567 class ParallelTempering :
public Move 570 const MPI::Controller& mpi;
571 MPI::ExchangeParticles exchange_particles;
572 std::unique_ptr<MPI::Partner> partner;
574 Geometry::VolumeMethod::ISOTROPIC;
575 std::map<MPI::Partner::PartnerPair, Average<double>> acceptance_map;
578 void _to_json(
json& j)
const override;
579 void _from_json(
const json& j)
override;
580 void _move(
Change& change)
override;
581 void _accept(
Change& change)
override;
582 void _reject(
Change& change)
override;
583 double bias(
Change& change,
double uold,
584 double unew)
override;
585 double exchangeEnergy(
double energy_change);
586 void exchangeState(
Change& change);
587 void exchangeGroupSizes(Space::GroupVector& groups,
int partner_rank);
589 std::string filename;
590 std::unique_ptr<std::ostream> stream;
591 std::optional<int> exchange;
592 void writeToFileStream()
const;
595 explicit ParallelTempering(
Space& spc,
const MPI::Controller& mpi);
606 [[maybe_unused]] std::unique_ptr<Move> createMove(
const std::string& name,
const json& properties,
616 unsigned int number_of_moves_per_sweep;
618 std::vector<double> repeats;
619 std::discrete_distribution<unsigned int> distribution;
620 using move_iterator = decltype(moves.
vec)::iterator;
621 move_iterator sample();
626 void addMove(std::shared_ptr<Move>&& move);
642 auto is_valid_and_stochastic = [&](
auto move) {
643 return move < moves.end() && (*move)->isStochastic();
645 using namespace ranges::cpp20;
646 return views::iota(0U, number_of_moves_per_sweep) |
647 views::transform([&]([[maybe_unused]]
auto count) {
return sample(); }) |
648 views::filter(is_valid_and_stochastic) |
649 ranges::views::indirect;
667 auto is_static_and_time_to_sample = [&, sweep_number](
const auto& move) {
668 return (!move->isStochastic()) && (sweep_number % move->sweep_interval == 0);
670 return moves | std::views::filter(is_static_and_time_to_sample);
nlohmann::json json
JSON object.
Definition: json_support.h:10
Eigen::Vector3d Point
3D vector used for positions, velocities, forces etc.
Definition: coordinates.h:7
Simple data structure to store a trajectory frame in native Faunus format.
Definition: io.h:563
const std::string name
Name of move.
Definition: move.h:56
Random number generator.
Definition: random.h:34
Replay simulation from a trajectory.
Definition: move.h:87
Class storing a list of MC moves with their probability weights and randomly selecting one...
Definition: move.h:613
auto repeatedStochasticMoves()
Generates a range of repeated, randomized move pointers guaranteed to be valid.
Definition: move.h:640
Base class for all moves (MC, Langevin, ...)
Definition: move.h:38
Translate and rotate individual atoms.
Definition: move.h:141
Average< double > mean_square_displacement
mean squared displacement
Definition: move.h:161
Translate and rotate an atom on a 2D hypersphere-surface.
Definition: move.h:235
As ChargeMove but performs displacements in squared charge.
Definition: move.h:397
Swap the charge of a single atom.
Definition: move.h:112
Transfers charge between two molecules.
Definition: move.h:412
std::vector< Particle > ParticleVector
Storage type for collections of particles.
Definition: particle.h:253
Change::GroupChange cdata
Data for change object.
Definition: move.h:163
Properties of changed groups.
Definition: space.h:40
Particle class for storing positions, id, and other properties.
Definition: particle.h:220
Cell list class templates.
Definition: actions.cpp:11
Space & spc
Space to operate on.
Definition: move.h:57
std::string molecule_name
name of molecule to operate on
Definition: move.h:162
End of Group class.
Definition: group.h:177
Displaces charge on a single atom.
Definition: move.h:368
Aggregate and sum energy terms.
Definition: energy.h:1954
Smart Monte Carlo version of molecular translation and rotation.
Definition: move.h:277
NLOHMANN_JSON_SERIALIZE_ENUM(SpheroCylinderData::PatchType, {{SpheroCylinderData::PatchType::Invalid, nullptr}, {SpheroCylinderData::PatchType::Full, "full"}, {SpheroCylinderData::PatchType::Capped, "capped"}, {SpheroCylinderData::PatchType::None, "none"}}) class AtomData
General properties for atoms.
Definition: atomdata.h:61
Specify changes made to a system.
Definition: space.h:29
static Random slump
Shared for all moves.
Definition: move.h:62
std::vector< value_type > vec
Vector of shared pointers to base class.
Definition: auxiliary.h:240
Placeholder for atoms and molecules.
Definition: space.h:92
Helper class for storing vectors of base pointers.
Definition: auxiliary.h:237
QuadrantJump translates a molecule to another quadrant considering as the origin the center of the bo...
Definition: move.h:456
Inserts molecules into random positions in the container.
Definition: molecule.h:47
auto constantIntervalMoves(const unsigned int sweep_number=1)
Range of moves excluded from the sample() algorithm above due to ZERO weight.
Definition: move.h:665
Helper class for constructing smart monte carlo moves.
Definition: smart_montecarlo.h:149
VolumeMethod
Various methods of volume scaling,.
Definition: geometry.h:52