utils

Overview

This module contains various utility functions that are used throughout the htf module. These are imported automatically when htf is imported.

Details

utils.center_of_mass(positions, mapping, box_size, name='center-of-mass')

Comptue mapping of the given positions (N x 3) and mapping (M x N) considering PBC. Returns mapped particles. :param positions: The tensor of particle positions :param mapping: The coarse-grain mapping used to produce the particles in system :param box_size: A list contain the size of the box [Lx, Ly, Lz] :param name: The name of the op to add to the TF graph

utils.compute_nlist(positions, r_cut, NN, box_size, sorted=False)

Compute particle pairwise neighbor lists.

Parameters:
  • positions – Positions of the particles
  • r_cut – Cutoff radius (HOOMD units)
  • NN – Maximum number of neighbors per particle
  • box_size – A list contain the size of the box [Lx, Ly, Lz]
  • sorted – Whether to sort neighbor lists by distance
Returns:

An [N X NN X 4] tensor containing neighbor lists of all particles and index

utils.compute_pairwise_potential(model_directory, r, potential_tensor_name, checkpoint=-1, feed_dict={})

Compute the pairwise potential at r for the given model.

Parameters:
  • model_directory – The model directory
  • r – A 1D grid of points at which to compute the potential.
  • potential_tensor_name – The tensor containing potential energy.
  • checkpoint – Which checkpoint to load. Default is -1, which loads latest checkpoint. An integer indicates loading from the model directory. If you pass a string, it is interpreted as a path.
  • feed_dict – Allows you to add any other placeholder values that need to be added to compute potential in your model
Returns:

A tuple of 1D arrays. First is the potentials corresponding to the pairwise distances in r, second is the forces.

utils.eds_bias(cv, set_point, period, learning_rate=1, cv_scale=1, name=None)

This method computes and returns the Lagrange multiplier/EDS coupling constant (alpha) to be used as the EDS bias in the simulation.

Parameters:
  • cv – The collective variable which is biased in the simulation
  • set_point – The set point value of the collective variable. This is a constant value which is pre-determined by the user and unique to each cv.
  • period – Time steps over which the coupling constant is updated. HOOMD time units are used. If period=100 alpha will be updated each 100 time steps.
  • learninig_rate – Learninig_rate in the EDS method.
  • cv_scale – Used to adjust the units of the bias to HOOMD units.
  • name – Name used as prefix on variables.
Returns:

Alpha, the EDS coupling constant.

utils.find_molecules(system)

Given a hoomd system, return a mapping from molecule index to particle index. This is a slow function and should only be called once.

Parameters:system – The molecular system in HOOMD.
Returns:A list of length L (number of molecules) whose elements are lists of atom indices
utils.force_matching(mapped_forces, calculated_cg_forces, learning_rate=0.001)

This will minimize the difference between the mapped forces and calculated CG forces using the Adam oprimizer.

Parameters:
  • mapped_forces (tensor) – A tensor with shape M x 3 where M is number of CG beads in the system. These are forces mapped from an all atom system.
  • calculated_cg_forces (tensor) – A tensor with shape M x 3 where M is number of CG beads in the system. These are CG forces estimated using a function or a basis set.
  • learning_rate (float) – The learning_rate for optimization
Returns:

optimizer, cost

utils.load_variables(model_directory, names, checkpoint=-1, feed_dict={})

Adds variables from model_directory to the TF graph loaded from a checkpoint, optionally other than the most recent one, or setting values with a feed dict.

Parameters:
  • model_directory – Directory from which to load model variables.
  • names – names of TensorFlow variables to load
  • checkpoint – checkpoint number of the trained model to load from. Default value is -1 for most recently saved model.
  • feed_dict – optionally, use a feed dictionary to populate the model
utils.matrix_mapping(molecule, beads_distribution)
This will create a M x N mass weighted mapping matrix where M is the number
of atoms in the molecule and N is the number of mapping beads.
Parameters:
  • molecule – This is atom selection in the molecule (MDAnalysis Atoms object).
  • beads_distribution – This is a list of beads distribution lists, Note that

each list should contain the atoms as strings just like how they appear in the topology file.

Returns:An array of M x N.
utils.mol_angle(mol_positions, type_i, type_j, type_k)

This method calculates the bond angle given three atoms batched by molecule

mol_positions
Positions tensor of atoms batched by molecules. Can be created by calling build_mol_rep() method in graphbuilder
type_i
Index of the first atom (int type)
type_j
Index of the second atom (int type)
type_k
Index of the third atom (int type)
angles
Tensor containing bond angles
utils.mol_bond_distance(mol_positions, type_i, type_j)

This method calculates the bond distance given two atoms batched by molecule

mol_positions
Positions tensor of atoms batched by molecules. Can be created by calling build_mol_rep() method in graphbuilder
type_i
Index of the first atom (int type)
type_j
Index of the second atom (int type)
v_ij
Tensor containing bond distances
utils.mol_dihedral(mol_positions, type_i, type_j, type_k, type_l)

This method calculates the dihedral angle given four atoms batched by molecule

mol_positions
Positions tensor of atoms batched by molecules. Can be created by calling build_mol_rep() method in graphbuilder
type_i
Index of the first atom (int type)
type_j
Index of the second atom (int type)
type_k
Index of the third atom (int type)
type_l
Index of the fourth atom (int type)
dihedrals
Tensor containing dihedral angles
utils.run_from_trajectory(model_directory, universe, selection='all', r_cut=10.0, period=10, feed_dict={})

This will process information from a trajectory and run the user defined model on the nlist computed from the trajectory.

Parameters:
  • model_directory (string) – The model directory
  • universe – The MDAnalysis universe
  • selection (string) – The atom groups to extract from universe
  • r_cut (float) – The cutoff raduis to use in neighbor list calculations
  • period (int) – Frequency of reading the trajectory frames
  • feed_dict (dict) – Allows you to add any other placeholder values that need to be added to compute potential in your model
utils.sparse_mapping(molecule_mapping, molecule_mapping_index, system=None)

This will create the necessary indices and values for defining a sparse tensor in tensorflow that is a mass-weighted M x N mapping operator.

Parameters:
  • molecule_mapping – This is a list of L x M matrices, where M is the number of atoms in the molecule and L is the number of coarse-grain sites that should come out of the mapping. There should be one matrix per molecule. The ordering of the atoms should follow what is defined in the output from find_molecules
  • molecule_mapping_index – This is the output from find_molecules. A list of length L (number of molecules) whose elements are lists of atom indices
  • system – The hoomd system. This is used to get mass values for the mapping, if you would like to weight by mass of the atoms.
Returns:

A sparse tensorflow tensor of dimension N x N, where N is number of atoms