Skip to content

Commit

Permalink
Added observables
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomDefaultUser committed Jan 18, 2022
1 parent 1145afa commit f2cb52d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
31 changes: 31 additions & 0 deletions mcmala/montecarlo/markovchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def __init__(self, temperatureK, evaluator: Calculator,
self.observables[entry] = {"rdf": None, "distances": None}
else:
self.observables[entry] = 0.0
if entry == "ion_ion_energy":
self.observables[entry] = 0.0
if entry == "static_structure_factor":
self.observables[entry] = {"static_structure_factor": None, "kpoints": None}
if entry == "tpcf":
self.observables[entry] = {"tpcf": None, "radii": None}


def run(self, steps_to_evolve, print_energies=False,
save_run=True):
Expand Down Expand Up @@ -131,6 +138,30 @@ def run(self, steps_to_evolve, print_energies=False,
self.evaluator.results[entry][0]) / \
accepted_steps
self.observables[entry]["distances"] = self.evaluator.results[entry][1]
if entry == "static_structure_factor":
if self.observables[entry]["static_structure_factor"] is None:
self.observables[entry]["static_structure_factor"] = \
self.evaluator.results[entry][0]
else:
self.observables[entry]["static_structure_factor"] = \
((self.observables[entry]["static_structure_factor"]
* (accepted_steps - 1)) +
self.evaluator.results[entry][0]) / \
accepted_steps
self.observables[entry]["kpoints"] = self.evaluator.results[entry][1]
if entry == "tpcf":
if self.observables[entry]["tpcf"] is None:
self.observables[entry]["tpcf"] = \
self.evaluator.results[entry][0]
else:
self.observables[entry]["tpcf"] = \
((self.observables[entry]["tpcf"]
* (accepted_steps - 1)) +
self.evaluator.results[entry][0]) / \
accepted_steps
self.observables[entry]["radii"] = self.evaluator.results[entry][1]
if entry == "ion_ion_energy":
self.observables[entry] = self.evaluator.results[entry]
all_observables_counter = 0

end_time = datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)")
Expand Down
34 changes: 31 additions & 3 deletions mcmala/simulation/atom_displacer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Configuration suggester for atomistic simulations."""
from copy import deepcopy
from random import randrange, random

from ase import Atoms
Expand All @@ -14,9 +13,36 @@ class AtomDisplacer(ConfigurationSuggester):
Displaces a random atom in an ASE atoms object by a random vector.
"""

def __init__(self, maximum_displacement):
def __init__(self, maximum_displacement, enforce_pbc=True):
super(AtomDisplacer, self).__init__()
self.maximum_displacement = maximum_displacement
self.enforce_pbc = enforce_pbc

@staticmethod
def _enforce_pbc(atoms):
"""
Explictly enforeces the PBC on an ASE atoms object.
QE (and potentially other codes?) do that internally. Meaning that the
raw positions of atoms (in Angstrom) can lie outside of the unit cell.
When setting up the DFT calculation, these atoms get shifted into
the unit cell. Since we directly use these raw positions for the
descriptor calculation, we need to enforce that in the ASE atoms
objects, the atoms are explicitly in the unit cell.
Parameters
----------
atoms : ase.atoms
The ASE atoms object for which the PBC need to be enforced.
Returns
-------
new_atoms : ase.atoms
The ASE atoms object for which the PBC have been enforced.
"""
new_atoms = atoms.copy()
new_atoms.set_scaled_positions(new_atoms.get_scaled_positions())
return new_atoms

def suggest_new_configuration(self, old_configuration: Atoms):
"""
Expand All @@ -33,7 +59,7 @@ def suggest_new_configuration(self, old_configuration: Atoms):
New configuration as suggested by algorithm.
"""
new_configuration = deepcopy(old_configuration)
new_configuration = old_configuration.copy()

# Which atom do we want to displace?
number_atom = randrange(0, len(old_configuration))
Expand All @@ -54,5 +80,7 @@ def suggest_new_configuration(self, old_configuration: Atoms):
positions = new_configuration.get_positions()
positions[number_atom, direction//2] += displacement
new_configuration.set_positions(positions)
if self.enforce_pbc:
new_configuration = AtomDisplacer._enforce_pbc(new_configuration)
return new_configuration

0 comments on commit f2cb52d

Please sign in to comment.