IMP  2.2.0
The Integrative Modeling Platform
temperature_rem.py
1 ## \example temperature_rem.py
2 # Temperature replica exchange
3 
4 import IMP
5 import IMP.base
6 import IMP.mpi
7 import IMP.core
8 import sys
9 
10 IMP.base.setup_from_argv(sys.argv, "Temperature MPI example")
11 
12 # min and max temperature
13 TEMPMIN_ = 1.0
14 TEMPMAX_ = 5.0
15 
16 # initialize Replica Exchange class
18 # get number of replicas
19 nproc = rem.get_number_of_replicas()
20 # create array of temperatures, in geometric progression
21 temp = rem.create_temperatures(TEMPMIN_, TEMPMAX_, nproc)
22 # get replica index
23 myindex = rem.get_my_index()
24 # set initial value of the parameter (temperature) to exchange
25 rem.set_my_parameter("temp", [temp[myindex]])
26 
27 # create model
28 m = IMP.kernel.Model()
29 
30 # add 2 particles
31 ps = []
32 for i in range(2):
33  p = IMP.kernel.Particle(m)
35  d.set_coordinates_are_optimized(True)
36  ps.append(p)
37 
38 # add harmonic restraint to distance
39 h = IMP.core.Harmonic(5.0, 1.0)
40 ds = IMP.core.DistanceRestraint(h, ps[0], ps[1])
41 m.add_restraint(ds)
42 
43 # movers
44 movers = []
45 for p in ps:
46  movers.append(IMP.core.BallMover([p], 0.5))
47 # serial mover
48 sm = IMP.core.SerialMover(movers)
49 
50 # sampler
51 mc = IMP.core.MonteCarlo(m)
52 mc.set_kt(temp[myindex])
53 mc.set_return_best(False)
54 mc.add_mover(sm)
55 
56 # prepare output
57 log = open("log" + str(myindex), "w")
58 
59 # start sampling loop
60 for istep in range(0, 100):
61  # do optimization
62  score = mc.optimize(100)
63 
64  # get my replica index and temperature
65  myindex = rem.get_my_index()
66  mytemp = rem.get_my_parameter("temp")[0]
67  # score divided by kbt
68  myscore = score / mytemp
69 
70  # printout
71  log.write("%4d %2d %6.3f\n" % (istep, myindex, score))
72 
73  # get my friend index and temperature
74  findex = rem.get_friend_index(istep)
75  ftemp = rem.get_friend_parameter("temp", findex)[0]
76  # score divided by kbt
77  fscore = score / ftemp
78 
79  # try exchange
80  flag = rem.do_exchange(myscore, fscore, findex)
81  # if accepted, change temperature
82  if (flag):
83  mc.set_kt(ftemp)
A Monte Carlo optimizer.
Definition: MonteCarlo.h:45
A class to implement Hamiltonian Replica Exchange.
See IMP.base for more information.
Definition: base/Array.h:20
static XYZ setup_particle(kernel::Model *m, ParticleIndex pi)
Definition: XYZ.h:51
Modify a set of continuous variables by perturbing them within a ball.
Distance restraint between two particles.
See IMP.mpi for more information.
Definition: mpi_config.h:107
Class to handle individual model particles.
See IMP.core for more information.
VectorD< 3 > Vector3D
Definition: VectorD.h:395
Strings setup_from_argv(const Strings &argv, std::string description, std::string positional_description, int num_positional)
Apply a list of movers one at a time.
Definition: SerialMover.h:23
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:72
Harmonic function (symmetric about the mean)
Definition: core/Harmonic.h:24