IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
linear_and_harmonic_scores.py
1## \example core/linear_and_harmonic_scores.py
2# An example for setting a linear or harmonic score between two particles
3# using either a point distance (between particle centers) or a sphere
4# distance (between particle surfaces).
5#
6# Note: this example relies on matplotlib for plotting the scores,
7# but it can be easily modified to just print the scores.
8#
9# Author: Barak Raveh, 2022/12/13
10
11import IMP
12import IMP.algebra
13import IMP.core
14try:
15 import matplotlib.pyplot
16except ImportError:
17 matplotlib = None
18import numpy
19import sys
20
21
22IMP.setup_from_argv(sys.argv, "linear or harmonic score example")
23
24
25DEFAULT_RADIUS = 2.0 # radius of particles
26LINEAR_OFFSET = 4.0 # distance at which the function is zero (note this is not the Y-axis intercept!)
27LINEAR_SLOPE = 3.0 # slope of linear score (= force in kcal/mol/A)
28HARMONIC_MEAN = 3 # distance at which the function is minimal/maximal
29HARMONIC_K = 2.0 # quadratic coefficient
30
31def create_particle(m, radius = DEFAULT_RADIUS):
32 p = IMP.Particle(m)
33 s = IMP.algebra.Sphere3D([0,0,0], radius)
35 return p
36
37def create_linear_point_pair_score():
38 ''' slope*x + intercept for point distance '''
39 linear_functor = IMP.core.Linear(LINEAR_OFFSET, LINEAR_SLOPE)
40 return IMP.core.DistancePairScore(linear_functor)
41
42def create_harmonic_point_pair_score():
43 ''' 0.5*k*(x-mean)^2 for point distance '''
44 harmonic_functor = IMP.core.Harmonic(HARMONIC_MEAN, HARMONIC_K)
45 return IMP.core.DistancePairScore(harmonic_functor)
46
47def create_linear_sphere_pair_score():
48 ''' slope*x + intercept for sphere distance '''
49 linear_functor = IMP.core.Linear(LINEAR_OFFSET, LINEAR_SLOPE)
50 return IMP.core.SphereDistancePairScore(linear_functor)
51
52def create_harmonic_sphere_pair_score():
53 ''' 0.5*k*(x-mean)^2 for sphere distance '''
54 harmonic_functor = IMP.core.Harmonic(HARMONIC_MEAN, HARMONIC_K)
55 return IMP.core.SphereDistancePairScore(harmonic_functor)
56
57def create_model(pair_score):
58 m = IMP.Model()
59 particles = [create_particle(m) for x in range(2)]
60 restraint = IMP.core.PairRestraint(m, pair_score, particles)
61 xyzrs = [IMP.core.XYZR(p) for p in particles]
62 return m, xyzrs, restraint
63
64def plot_score(pair_score, caption,
65 xmin = -15.0, xmax = 15.0, xstep = 0.01):
66 '''
67 Plots a pair_score between two particles, one particle
68 being at [0,0,0] and the other particle being at [x,0,0]
69 for x in the closed interval [xmin:xstep:xmax]
70 '''
71 m, xyzrs, restraint = create_model(pair_score)
72 xyzrs[0].set_coordinates([0,0,0])
73 X = numpy.arange(xmin, xmax+0.1*xstep, xstep)
74 Y = 0.0*X
75 for i,x in enumerate(X):
76 xyzrs[0].set_coordinates([x,0,0])
77 Y[i] = restraint.get_score()
78 if not matplotlib:
79 print("Not showing plot; matplotlib is not installed "
80 "or could not be imported")
82 print("Not showing plot, as we are running test cases")
83 else:
84 matplotlib.pyplot.plot(X,Y,'-')
85 matplotlib.pyplot.title(caption)
86 matplotlib.pyplot.xlabel(r"$X_2$ [$\AA$]")
87 matplotlib.pyplot.ylabel(r"Energy [$kcal \cdot mol^{-1}$]")
88 matplotlib.pyplot.gca().spines['bottom'].set_position(('data', 0))
89 matplotlib.pyplot.show()
90
91if __name__ == "__main__":
92 # NOTE: sphere distance is not distance!
93 linear_str = "{:.1f}*(dist-{:.1f})".format(LINEAR_SLOPE, LINEAR_OFFSET)
94 harmonic_str = "{:.1f}*(dist-{:.1f})^2".format(HARMONIC_K, HARMONIC_MEAN)
95 plot_score(create_linear_point_pair_score(),
96 caption="Linear point distance\n{}"
97 .format(linear_str))
98 plot_score(create_linear_sphere_pair_score(),
99 caption="Linear sphere distance (R={:.1f} A)\n{}"
100 .format(DEFAULT_RADIUS, linear_str))
101 plot_score(create_harmonic_point_pair_score(),
102 caption="Harmonic point distance\n{}"
103 .format(harmonic_str))
104 plot_score(create_harmonic_sphere_pair_score(),
105 caption="Harmonic sphere distance (R={:.1f} A)\n{}"
106 .format(DEFAULT_RADIUS, harmonic_str))
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
Class to handle individual particles of a Model object.
Definition Particle.h:45
Score a pair of particles based on the distance between their centers.
Harmonic function (symmetric about the mean)
Linear function
Definition Linear.h:22
Applies a PairScore to a Pair.
A score on the distance between the surfaces of two spheres.
A decorator for a particle with x,y,z coordinates and a radius.
Definition XYZR.h:27
static XYZR setup_particle(Model *m, ParticleIndex pi)
Definition XYZR.h:48
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
Basic functionality that is expected to be used by a wide variety of IMP users.
bool get_is_quick_test()
Definition flags.h:183
void setup_from_argv(int argc, char **argv, std::string description)
Parse the command line flags and return the positional arguments.