IMP logo
IMP Reference Guide  develop.549d75e6f4,2024/11/20
The Integrative Modeling Platform
temperature_rem.py

Temperature replica exchange

1 ## \example temperature_rem.py
2 # Temperature replica exchange
3 
4 import IMP
5 import IMP.mpi
6 import IMP.core
7 import sys
8 
9 IMP.setup_from_argv(sys.argv, "Temperature MPI example")
10 
11 # min and max temperature
12 TEMPMIN_ = 1.0
13 TEMPMAX_ = 5.0
14 
15 # initialize Replica Exchange class
17 # get number of replicas
18 nproc = rem.get_number_of_replicas()
19 # create array of temperatures, in geometric progression
20 temp = rem.create_temperatures(TEMPMIN_, TEMPMAX_, nproc)
21 # get replica index
22 myindex = rem.get_my_index()
23 # set initial value of the parameter (temperature) to exchange
24 rem.set_my_parameter("temp", [temp[myindex]])
25 
26 # create model
27 m = IMP.Model()
28 
29 # add 2 particles
30 ps = []
31 for i in range(2):
32  p = IMP.Particle(m)
34  d.set_coordinates_are_optimized(True)
35  ps.append(p)
36 
37 # add harmonic restraint to distance
38 h = IMP.core.Harmonic(5.0, 1.0)
39 ds = IMP.core.DistanceRestraint(m, h, ps[0], ps[1])
41 
42 # movers
43 movers = []
44 for p in ps:
45  movers.append(IMP.core.BallMover(m, p, 0.5))
46 # serial mover
47 sm = IMP.core.SerialMover(movers)
48 
49 # sampler
50 mc = IMP.core.MonteCarlo(m)
51 mc.set_scoring_function(sf)
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)