Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Energy decomposition function, native contacts by configurational state #144

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion cg_openmm/parameters/evaluate_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,4 +1155,65 @@ def get_replica_reeval_energies(replica, temperature_list, file_list, topology,
# This reducing step gets rid of the simtk units
U_eval_rep[j,k] = (potential_energy*beta_all[j])

return U_eval_rep, replica
return U_eval_rep, replica


def energy_decomposition(cgmodel, positions_file):
"""
Given a cgmodel with a topology and system and coordinates in positions_file,
perform an OpenMM energy decomposition.

:param cgmodel: CGModel() class object to evaluate energy with
:type cgmodel: class

:param positions_file: Path to pdb or dcd file containing molecular coordinates
:type positions_file: str

:returns:
- U_decomposition - dict mapping force names to contribution to total energy.
"""

topology = cgmodel.topology
system = cgmodel.system

# Load in the coordinates as mdtraj object:
if positions_file[-3:] == 'dcd':
traj = md.load(positions_file,top=md.Topology.from_openmm(topology))
else:
traj = md.load(positions_file)

positions = traj[0].xyz[0]*unit.nanometer

# Set up simulation object:
simulation_time_step = 5.0 * unit.femtosecond
friction = 0.0 / unit.picosecond
integrator = LangevinIntegrator(
0.0 * unit.kelvin, friction, simulation_time_step.in_units_of(unit.picosecond)
)
simulation = Simulation(topology, cgmodel.system, integrator)
simulation.context.setPositions(positions)

# Set force groups:
force_names = {}
U_decomposition = {}

for force_index, force in enumerate(simulation.system.getForces()):
# These are the overall classes of forces, not the particle-specific forces
force_names[force_index] = force.__class__.__name__
force.setForceGroup(force_index)

# Need to create a new simulation object with the updated force groups:
integrator_new = LangevinIntegrator(
0.0 * unit.kelvin, friction, simulation_time_step.in_units_of(unit.picosecond)
)
simulation_new = Simulation(topology, simulation.system, integrator_new)
simulation_new.context.setPositions(positions)

total_energy = simulation_new.context.getState(getEnergy=True).getPotentialEnergy()
U_decomposition['total'] = total_energy

for force_index, force in enumerate(simulation_new.system.getForces()):
force_names[force_index] = force.__class__.__name__
U_decomposition[force_names[force_index]] = simulation_new.context.getState(getEnergy=True,groups={force_index}).getPotentialEnergy()

return U_decomposition
Loading