Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(
self,
required_properties,
force_handle,
position_conversion=1.0,
force_conversion=1.0,
property_conversion={},
detach=True,
):
self.results = {}
self.force_handle = force_handle
self.required_properties = required_properties
# Perform automatic conversion of units
self.position_conversion = MDUnits.parse_mdunit(position_conversion)
self.force_conversion = MDUnits.parse_mdunit(force_conversion)
self.property_conversion = {
p: MDUnits.parse_mdunit(property_conversion[p]) for p in property_conversion
}
self._init_default_conversion()
self.detach = detach
Args:
model (torch.nn.module): the model which is used for property calculation
required_properties (list): list of properties that are calculated by the model
force_handle (str): name of the forces property in the model output
position_conversion (float): conversion factor for positions
force_conversion (float): conversion factor for forces
property_conversion (dict): dictionary with conversion factors for other properties
calculator (src.schnetpack.md.calculator.Calculator): calculator object
Returns:
the calculator object
"""
_log.info(f"Using {calculator}")
position_conversion = MDUnits.parse_mdunit(position_conversion)
force_conversion = MDUnits.parse_mdunit(force_conversion)
if calculator == "schnet_calculator":
model = load_model(device=device)
return SchnetPackCalculator(
model,
required_properties=required_properties,
force_handle=force_handle,
position_conversion=position_conversion,
force_conversion=force_conversion,
property_conversion=property_conversion,
)
else:
raise NotImplementedError
def __init__(
self,
required_properties,
force_handle,
position_conversion=1.0,
force_conversion=1.0,
property_conversion={},
detach=True,
):
self.results = {}
self.force_handle = force_handle
self.required_properties = required_properties
# Perform automatic conversion of units
self.position_conversion = MDUnits.parse_mdunit(position_conversion)
self.force_conversion = MDUnits.parse_mdunit(force_conversion)
self.property_conversion = {
p: MDUnits.parse_mdunit(property_conversion[p]) for p in property_conversion
}
self._init_default_conversion()
self.detach = detach
def __init__(
self,
n_beads,
time_step,
temperature,
transformation=NormalModeTransformer,
device="cuda",
):
super(RingPolymer, self).__init__(time_step, device=device)
self.n_beads = n_beads
# Compute the ring polymer frequency
self.omega = MDUnits.kB * n_beads * temperature / MDUnits.hbar
self.transformation = transformation(n_beads, device=self.device)
# Set up omega_normal, the ring polymer frequencies in normal mode
# representation
self.omega_normal = (
2
* self.omega
* torch.sin(
torch.arange(self.n_beads, device=device).float() * np.pi / self.n_beads
)
)
# Initialize the propagator matrices
self.propagator = self._init_propagator()
self.positions = torch.zeros(
self.n_replicas, self.n_molecules, self.max_n_atoms, 3, device=self.device
)
self.momenta = torch.zeros(
self.n_replicas, self.n_molecules, self.max_n_atoms, 3, device=self.device
)
# 5) Populate arrays according to the data provided in molecules
for i in range(self.n_molecules):
# Static properties
self.atom_types[:, i, : self.n_atoms[i]] = torch.from_numpy(
molecules[i].get_atomic_numbers()
)
self.atom_masks[:, i, : self.n_atoms[i]] = 1.0
self.masses[i, : self.n_atoms[i]] = torch.from_numpy(
molecules[i].get_masses() * MDUnits.d2amu
)
# Dynamic properties
self.positions[:, i, : self.n_atoms[i], :] = torch.from_numpy(
molecules[i].positions * MDUnits.angs2bohr
)
# 6) Do proper broadcasting here for easier use in e.g. integrators and
# thermostats afterwards
self.masses = self.masses[None, :, :, None]
self.atom_masks = self.atom_masks[..., None]
# 7) Build neighbor lists
if self.neighbor_list is not None:
self.neighbor_list = self.neighbor_list(self)
def temperature(self):
"""
Convenience property for accessing the instantaneous temperatures of
each replica and molecule.
Returns:
torch.Tensor: Tensor of the instantaneous temperatures (in
Kelvin) with the shape n_replicas x n_molecules
"""
temperature = (
2.0
/ (3.0 * MDUnits.kB * self.n_atoms.float()[None, :])
* self.kinetic_energy
)
return temperature
mol_idx (int): Index of the molecule to extract, by default uses the first molecule (mol_idx=0)
replica_idx (int): Replica of the molecule to extract (e.g. for ring polymer molecular dynamics). If
replica_idx is set to None (default), the centroid is returned if multiple replicas are
present.
Returns:
np.array: N_steps array containing the temperature of every configuration in Kelvin.
"""
# Get the velocities
# Get the kinetic energy
kinetic_energy = self.get_kinetic_energy(
mol_idx=mol_idx, replica_idx=replica_idx
)
# Compute the temperature
temperature = 2.0 / (3.0 * MDUnits.kB * self.n_atoms[mol_idx]) * kinetic_energy
return temperature
def __init__(
self,
model,
required_properties=[Properties.energy, Properties.forces],
force_handle=Properties.forces,
position_conversion=1.0 / MDUnits.angs2bohr,
force_conversion=1.0 / MDUnits.Ha2kcalpmol / MDUnits.angs2bohr,
property_conversion={},
detach=True,
):
super(SGDMLCalculator, self).__init__(
required_properties,
force_handle,
position_conversion,
force_conversion,
property_conversion,
detach,
)
self.model = model
def config():
"""configuration for the calculator ingredient"""
calculator = "schnet_calculator"
required_properties = ["y", "dydx"]
force_handle = "dydx"
position_conversion = 1.0 / MDUnits.angs2bohr
force_conversion = 1.0 / MDUnits.auforces2aseforces
property_conversion = {}
model_path = "eth_ens_01.model"
Args:
mol_idx (int): Index of the molecule to extract, by default uses the first molecule (mol_idx=0)
replica_idx (int): Replica of the molecule to extract (e.g. for ring polymer molecular dynamics). If
replica_idx is set to None (default), the centroid is returned if multiple replicas are
present.
Returns:
np.array: N_steps array containing the kinetic eenrgy of every configuration in atomic units.
"""
# Get the velocities
velocities = self.get_velocities(mol_idx=mol_idx, replica_idx=replica_idx)
# Get the masses and convert to correct units
masses = (
data.atomic_masses[self.properties[Properties.Z][mol_idx]] * MDUnits.d2amu
)
# Compute the kinetic energy as 1/2*m*v^2
kinetic_energy = 0.5 * np.sum(
masses[None, :, None] * velocities ** 2, axis=(1, 2)
)
return kinetic_energy