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