Bsoft  2.1.0
Bernard's software package
mol_monte.cpp
Go to the documentation of this file.
1 
9 #include "rwmolecule.h"
10 #include "rwimg.h"
11 #include "rwmd.h"
12 #include "mol_md.h"
13 #include "mol_monte.h"
14 #include "mol_bonds.h"
15 #include "mol_map_energy.h"
16 #include "mol_compare.h"
17 #include "mol_transform.h"
18 #include "mol_util.h"
19 #include "Matrix.h"
20 #include "linked_list.h"
21 #include "random_numbers.h"
22 #include "utilities.h"
23 
24 // Declaration of global variables
25 extern int verbose; // Level of output to the screen
26 
28 {
29  cout << "#";
30  if ( md->Kbond ) cout << "\tEbond";
31  if ( md->Kangle ) cout << "\tEangle";
32  if ( md->Kvdw ) cout << "\tEvdw";
33  if ( md->Kelec ) cout << "\tEelec";
34  if ( md->Ksep ) cout << "\tEsep";
35  if ( md->Kpoint ) cout << "\tEpoint";
36  if ( md->Kmap ) cout << "\tEmap";
37  cout << "\tEpot\tdEpot" << endl;
38 
39  return 0;
40 }
41 
42 int display_energy(long iter, Bmd* md, double dE)
43 {
44  cout << iter;
45  if ( md->Kbond ) cout << tab << md->Ebond;
46  if ( md->Kangle ) cout << tab << md->Eangle;
47  if ( md->Kvdw ) cout << tab << md->Evdw;
48  if ( md->Kelec ) cout << tab << md->Eelec;
49  if ( md->Ksep ) cout << tab << md->Esep;
50  if ( md->Kpoint ) cout << tab << md->Epoint;
51  if ( md->Kmap ) cout << tab << md->Emap;
52  cout << tab << md->Epot << tab << dE << endl;
53 
54  return 0;
55 }
56 
72  double beta, double max_angle, double max_shift,
73  long max_iter, int rigid,
74  double (Efunc)(Bmolgroup*, Bimage*, Bmd*),
75  int (Tfunc)(Bmolgroup*, double, double))
76 {
77  int done(0), accept, select_min(0);
78  long i, isel(0), cycle(0), naccept(0);
79  double r, E, pE, bestE, Z(0), Ze, Zi, Emax;
80  double irm = 1.0/get_rand_max();
82  Bmolgroup* mgc = molgroup_list_copy(molgroup);
83  Bmolgroup* mg = NULL;
84  Bmolgroup* mgold = NULL;
85  Bmolecule* mol;
86 
87  double* Elist = new double[max_iter+1];
88 
89  long nmg = count_list((char *) molgroup);
90  long nmol = count_list((char *) molgroup->mol);
91  long nbond = count_list((char *) molgroup->bond);
92  Vector3<double>* iloc = new Vector3<double>[nmol];
93 
94  if ( verbose & VERB_RESULT ) {
95  cout << "Monte carlo metropolis energy minimization:" << endl;
96  cout << "Molecule file: " << molgroup->filename << endl;
97  cout << "Number of molecule groups: " << nmg << endl;
98  cout << "Number of molecules: " << nmol << endl;
99  if ( map ) cout << "Map file: " << map->file_name() << endl;
100  cout << "Coordinate minima and maxima: " << molgroup->min << " " << molgroup->max << endl;
101  cout << "Bounding box: " << molgroup->box << endl;
102  cout << "Kbond: " << md->Kbond << endl;
103  cout << "Kangle: " << md->Kangle << endl;
104  cout << "Kelectrostatic: " << md->Kelec << endl;
105  cout << "Kvdw: " << md->Kvdw << endl;
106  cout << "Kmap: " << md->Kmap << endl;
107  cout << "Kseparation: " << md->Ksep << endl;
108  cout << "Kpoint: " << md->Kpoint << " (" << md->pointdecay << ")" << endl;
109  cout << "location: " << md->point << endl;
110  cout << "beta constant: " << beta << endl;
111  cout << "Cutoff distance: " << md->cutoff << endl;
112  cout << "Separation distance: " << md->sepdist << endl;
113  cout << "Maximum shift: " << max_shift << endl;
114  cout << "Maximum angular increment: " << max_angle*180.0/M_PI << endl;
115  cout << "Steps along bond: " << md->bondsteps << endl;
116  cout << "Number of bonds: " << nbond << endl;
117  if ( rigid < 1 ) cout << "Treating the whole ensemble as a rigid body" << endl;
118  else if ( rigid == 1 ) cout << "Treating each file with a molecule group as a rigid body" << endl;
119  else if ( rigid == 2 ) cout << "Treating each molecule as a rigid body" << endl;
120  else cout << "Modifying each atom's coordinates" << endl;
121  cout << endl;
122  md_calculate_deviations(molgroup, md->wrap);
123  }
124 
125  if ( rigid < 1 ) {
126  for ( mg = mgc; mg; mg = mg->next ) mg->select = "ALL";
127  } else if ( rigid == 1 ) {
128  for ( mg = mgc; mg; mg = mg->next ) {
129  mg->sel = 0;
130  mg->select = "FILE";
131  }
132  } else if ( rigid == 2 ) {
133  for ( i=0, mol = mgc->mol; mol; mol = mol->next, ++i ) {
134  mol->sel = 0;
135  iloc[i] = mol_center_of_mass(mol);
136  }
137  mgc->select = "MOL";
138  } else mgc->select = "ATOM";
139 
140  pE = (Efunc)(mgc, map, md);
141 
142  bestE = E = pE;
143  Elist[0] = pE;
144 
145  if ( verbose & VERB_RESULT ) {
147  display_energy(0, md, E-pE);
148  }
149  while ( !done && ( cycle < max_iter ) ) {
150  cycle++;
151  mgold = molgroup_list_copy(mgc);
152  if ( rigid == 1 ) {
153  if ( select_min ) {
154  for ( i=0, Emax=-1e30, mg=mgc; mg; mg=mg->next, i++ ) {
155  if ( Emax < mg->fom ) {
156  Emax = mg->fom;
157  isel = i;
158  }
159  }
160  } else {
161  isel = (long) ((nmg-0.001)*irm*random());
162  }
163  for ( i=0, mg=mgc; mg; mg=mg->next, i++ ) {
164  if ( i == isel ) mg->sel = 1;
165  else mg->sel = 0;
166  }
167  } else if ( rigid == 2 ) {
168  if ( select_min ) {
169  for ( i=0, Emax=-1e30, mol=mgc->mol; mol; mol=mol->next, i++ ) {
170  if ( Emax < mol->fom ) {
171  Emax = mol->fom;
172  isel = i;
173  }
174  }
175  } else {
176  isel = (long) ((nmol-0.001)*irm*random());
177  }
178  for ( i=0, mol=mgc->mol; mol; mol=mol->next, i++ ) {
179  if ( i == isel ) mol->sel = 1;
180  else mol->sel = 0;
181  }
182  }
183  (Tfunc)(mgc, max_angle, max_shift);
184  E = (Efunc)(mgc, map, md);
185  Elist[cycle] = E;
186 // display_energy(cycle, md, E-pE);
187  if ( bestE > E ) {
188  bestE = E;
189  molgroup_list_kill(molgroup);
190  molgroup = molgroup_list_copy(mgc);
191  if ( verbose & VERB_RESULT )
192  display_energy(cycle, md, E-pE);
193  }
194  accept = 1;
195  if ( E > 1e30 ) accept = 0; // High energy indicates the molecule might be outside the box
196  if ( E > pE ) {
197  r = random()*irm;
198  if ( exp(beta*(pE-E)) < r ) accept = 0;
199  }
200  if ( accept ) {
201  pE = E;
202  naccept++;
203  molgroup_list_kill(mgold);
204  } else {
205  molgroup_list_kill(mgc);
206  mgc = mgold;
207  }
208 // molgroup_test_if_within_box(mgc, mgc->min, mgc->max);
209  }
210 
211  molgroup_list_kill(mgc);
212 
213  for ( i=0, Z=Ze=0; i<=cycle; i++ ) {
214  Zi = exp(-beta*Elist[i]);
215  Z += Zi;
216  if ( Elist[i] - bestE < 0.001 ) Ze += Zi;
217  }
218  if ( Z <= 0 ) Z = 1;
219 
220  delete[] Elist;
221 
222  md->Epot = bestE;
223 
224  gloc = molgroup_center_of_mass(molgroup) - gloc;
225 
226  if ( verbose & VERB_RESULT ) {
227  cout << "Total shift: " << gloc << endl;
228  for ( i=0, mol = molgroup->mol; mol; mol = mol->next, ++i ) {
229  iloc[i] = mol_center_of_mass(mol) - iloc[i];
230  E = mol_map_energy(mol, map, md->Kmap);
231  cout << tab << mol->id << tab << iloc[i] << tab << E << endl;
232  }
233  cout << "Fraction of perturbations accepted: " << naccept*1.0/cycle << endl;
234  cout << "Final energy: " << bestE << endl;
235  cout << "Partition function: " << Z << endl;
236  cout << "Probability of final orientation: " << Ze/Z << endl << endl;
237  md_calculate_deviations(molgroup, md->wrap);
238  }
239 
240  delete[] iloc;
241 
242  return molgroup;
243 }
244 
261  Vector3<double> grid_sampling, Bstring filename)
262 {
263  Bstring basename("t.pdb");
264  long i, x, y, z, n(0);
265  Vector3<double> loc;
266  Vector3<double> bbmin = molgroup->min + grid_sampling;
267  Vector3<double> bbmax = molgroup->max - grid_sampling;
269  Bmolgroup* mglist = NULL;
270  Bmolgroup* mgc = NULL;
271 
272  if ( verbose ) {
273  cout << "Generating molecules on a masked grid:" << endl;
274  cout << "Bounding box: " << bbmin << " " << bbmax << endl;
275  cout << "Grid sampling: " << grid_sampling << endl;
276  if ( pmask ) cout << "Mask file: " << pmask->file_name() << endl;
277  }
278 
279  int accept_flag;
280 
281  for ( loc[2]=bbmin[2]; loc[2]<=bbmax[2]; loc[2]+=grid_sampling[2] ) {
282  for ( loc[1]=bbmin[1]; loc[1]<=bbmax[1]; loc[1]+=grid_sampling[1] ) {
283  for ( loc[0]=bbmin[0]; loc[0]<=bbmax[0]; loc[0]+=grid_sampling[0] ) {
284  accept_flag = 1;
285  if ( pmask ) {
286  x = (long) (loc[0]/pmask->sampling(0)[0] + pmask->image->origin()[0] + 0.5);
287  y = (long) (loc[1]/pmask->sampling(0)[1] + pmask->image->origin()[1] + 0.5);
288  z = (long) (loc[2]/pmask->sampling(0)[2] + pmask->image->origin()[2] + 0.5);
289  i = pmask->index(0, x, y, z, 0);
290  if ( i < 0 ) accept_flag = 0;
291  else if ( (*pmask)[i] < 0.5 ) accept_flag = 0;
292  }
293  if ( accept_flag ) {
294  n++;
295  if ( mgc ) {
296  mgc->next = molgroup_copy(molgroup);
297  mgc = mgc->next;
298  } else {
299  mgc = molgroup_copy(molgroup);
300  mglist = mgc;
301  }
302  molgroup_coor_shift(mgc, (loc - com));
304  if ( filename.length() )
305  mgc->filename = filename.pre_rev('.') + Bstring(n, "_%06d.") + filename.post_rev('.');
306  else
307  mgc->filename = basename.pre_rev('.') + Bstring(n, "_%06d.") + basename.post_rev('.');
308  }
309  }
310  }
311  }
312 
313  if ( verbose )
314  cout << "Molecules generated: " << n << endl << endl;
315 
316  return mglist;
317 }
318 
320  Vector3<double> grid_sampling, Bimage* pmask)
321 {
322  long i, x, y, z, id(0);
323  Vector3<double> loc;
324  Vector3<double> bbmin = molgroup->min + grid_sampling;
325  Vector3<double> bbmax = molgroup->max - grid_sampling;
326  Bmodel* model = model_init("Grid");
327  Bcomponent* comp = NULL;
328  Bstring comptype("1");
329  Bcomptype* ct = model_add_type_by_id(model, comptype);
330  ct->fmod = molgroup->filename;
331 
332  if ( verbose ) {
333  cout << "Generating models on a masked grid:" << endl;
334  cout << "Molecule file name: " << molgroup->filename << endl;
335  cout << "Bounding box: " << bbmin << " " << bbmax << endl;
336  cout << "Grid sampling: " << grid_sampling << endl;
337  if ( pmask ) cout << "Mask file: " << pmask->file_name() << endl;
338  }
339 
340  int accept_flag;
341 
342  for ( loc[2]=bbmin[2]; loc[2]<=bbmax[2]; loc[2]+=grid_sampling[2] ) {
343  for ( loc[1]=bbmin[1]; loc[1]<=bbmax[1]; loc[1]+=grid_sampling[1] ) {
344  for ( loc[0]=bbmin[0]; loc[0]<=bbmax[0]; loc[0]+=grid_sampling[0] ) {
345  accept_flag = 1;
346  if ( pmask ) {
347  x = (long) (loc[0]/pmask->sampling(0)[0] + pmask->image->origin()[0] + 0.5);
348  y = (long) (loc[1]/pmask->sampling(0)[1] + pmask->image->origin()[1] + 0.5);
349  z = (long) (loc[2]/pmask->sampling(0)[2] + pmask->image->origin()[2] + 0.5);
350  i = pmask->index(0, x, y, z, 0);
351  if ( i < 0 ) accept_flag = 0;
352  else if ( (*pmask)[i] < 0.5 ) accept_flag = 0;
353  }
354  if ( accept_flag ) {
355  comp = (Bcomponent *) add_item((char **) &comp, sizeof(Bcomponent));
356  if ( !model->comp ) model->comp = comp;
357  comp->id = Bstring(++id, "%d");
358  comp->type = ct;
359  comp->loc = loc;
360  comp->view[2] = 1;
361  comp->sel = 1;
362  }
363  }
364  }
365  }
366 
367  if ( verbose )
368  cout << "Components generated: " << id << endl << endl;
369 
370  return model;
371 }
372 
387  double angle_step, Bstring filename, int whole)
388 {
389  Bstring basename("t.pdb");
390  int n(0), nphi;
391  double theta, phi, phi_step;
392  Bmolgroup* mglist = NULL;
393  Bmolgroup* mgc = NULL;
394  Bmolecule* mol;
395  Transform t;
396  t.origin = molgroup_center_of_mass(molgroup);
397 
398  if ( verbose & VERB_FULL )
399  cout << "Theta\tAxis.x\tAxis.y" << endl;
400  for ( theta=0; theta<M_PI+angle_step/2; theta+=angle_step ) {
401  nphi = (int) (2*M_PI*sin(theta)/angle_step + 0.5); // Number of views at this theta
402  phi_step = 3*M_PI;
403  if ( nphi ) phi_step = M_PI*2.0/nphi;
404  for ( phi=-M_PI; phi<M_PI-phi_step/2; phi+=phi_step ) {
405  n++;
406  t.axis = Vector3<double>(cos(phi), sin(phi), 0);
407  t.angle = theta;
408  if ( verbose & VERB_FULL )
409  cout << theta*180.0/M_PI << tab << t.axis[0] << tab << t.axis[1] << endl;
410  if ( mglist ) {
411  mgc->next = molgroup_copy(molgroup);
412  mgc = mgc->next;
413  } else mglist = mgc = molgroup_copy(molgroup);
414  if ( whole ) {
415  molgroup_coor_rotate(mgc, t);
416  } else {
417  for ( mol = mgc->mol; mol; mol = mol->next ) {
418  t.origin = mol_center_of_mass(mol);
419  mol_coor_rotate(mol, t);
420  }
421  }
422  if ( filename.length() )
423  mgc->filename = filename.pre_rev('.') + Bstring(n, "_%06d.") + filename.post_rev('.');
424  else
425  mgc->filename = basename.pre_rev('.') + Bstring(n, "_%06d.") + basename.post_rev('.');
426  }
427  }
428 
429  if ( verbose )
430  cout << "Molecules generated: " << n << endl << endl;
431 
432  return mglist;
433 }
434 
436 {
437  int id(0), nphi;
438  double theta, phi, phi_step;
440  Bmodel* model = model_init("Orientations");
441  Bcomponent* comp = NULL;
442  Bstring comptype("1");
443  Bcomptype* ct = model_add_type_by_id(model, comptype);
444  ct->fmod = molgroup->filename;
445 
446  for ( theta=0; theta<M_PI+angle_step/2; theta+=angle_step ) {
447  nphi = (int) (2*M_PI*sin(theta)/angle_step + 0.5); // Number of views at this theta
448  phi_step = 3*M_PI;
449  if ( nphi ) phi_step = M_PI*2.0/nphi;
450  for ( phi=-M_PI; phi<M_PI-phi_step/2; phi+=phi_step ) {
451  comp = (Bcomponent *) add_item((char **) &comp, sizeof(Bcomponent));
452  if ( !model->comp ) model->comp = comp;
453  comp->id = Bstring(++id, "%d");
454  comp->type = ct;
455  comp->loc = com;
456  comp->view = View(sin(theta)*cos(phi), sin(theta)*sin(phi), cos(theta), 0);
457  comp->sel = 1;
458  }
459  }
460 
461  if ( verbose )
462  cout << "Components generated: " << id << endl << endl;
463 
464  return model;
465 }
466 
485 int mcm_molecule_list(Bmolgroup* molgroup, Bmodel* model, Bmd* md, Bimage* map, double beta,
486  double max_angle, double max_shift, long max_iter, int rigid)
487 {
488  int i;
489  Bmolgroup* mc = NULL;
490  Bcomponent* comp = NULL;
491  Quaternion q;
492  Transform t;
493  t.origin = molgroup_center_of_mass(molgroup);
494 
495  model->fmap = map->file_name();
496 
497  for ( i=1, comp = model->comp; comp; comp = comp->next, i++ ) {
498  comp->type = model_add_type_by_filename(model, molgroup->filename, 0);
499  mc = molgroup_copy(molgroup);
500 // q = quaternion_from_view(comp->view);
501  q = comp->view.quaternion();
502 // t = transform_from_quaternion(q);
503  t = Transform(q);
505  t.trans = comp->loc - t.origin;
506  molgroup_coor_rotate(mc, t);
507  if ( map ) molgroup_set_box_to_map_boundaries(mc, map);
508  mc = monte_carlo_metropolis(mc, md, map, beta, max_angle, max_shift,
510  comp->loc = molgroup_center_of_mass(mc);
511  t = molgroup_find_transformation(mc, molgroup);
512 // comp->view = view_from_angle_and_axis3(t.angle, t.axis);
513  comp->view = View(t.angle, t.axis);
514  comp->fom = exp(-0.14*md->Epot);
515  molgroup_kill(mc);
516  if ( verbose )
517  cout << i << ":\t" << comp->loc[0] << tab << comp->loc[1] << tab <<
518  comp->loc[2] << tab << comp->view[0] << tab << comp->view[1] << tab <<
519  comp->view[2] << tab << comp->view.angle()*180.0/M_PI << tab << md->Epot << endl;
520  }
521 
522  return i;
523 }
524 
543 int mcm_molecule_groups(Bmolgroup* molgroup, Bmodel* model, Bmd* md, Bimage* map, double beta,
544  double max_angle, double max_shift, long max_iter, int rigid)
545 {
546  int i = 1;
547  Bmolgroup* mglist = NULL;
548  Bmolgroup* mc = NULL;
549  Bcomponent* comp = NULL;
550  Quaternion q;
551  Transform t;
552  t.origin = molgroup_center_of_mass(molgroup);
553 
554  model->fmap = map->file_name();
555 
556 // Bstring filename;
557 
558  for ( comp = model->comp; comp; comp = comp->next ) {
559  comp->type = model_add_type_by_filename(model, molgroup->filename, 0);
560  if ( !mglist ) {
561  mglist = mc = molgroup_copy(molgroup);
562  } else {
563  mc->next = molgroup_copy(molgroup);
564  mc = mc->next;
565  }
566 // q = quaternion_from_view(comp->view);
567  q = comp->view.quaternion();
568 // t = transform_from_quaternion(q);
569  t = Transform(q);
571  t.trans = comp->loc - t.origin;
572  molgroup_coor_rotate(mc, t);
573  if ( map ) molgroup_set_box_to_map_boundaries(mc, map);
574 // filename = Bstring(i++, "test_%03d.pdb");
575 // write_molecule(filename, mc);
576  }
577 
578  mglist = monte_carlo_metropolis(mglist, md, map, beta, max_angle, max_shift,
580 
581  for ( i=1, comp = model->comp, mc = mglist; comp && mc; comp = comp->next, mc = mc->next, i++ ) {
582  comp->loc = molgroup_center_of_mass(mc);
583  t = molgroup_find_transformation(mc, molgroup);
584 // comp->view = view_from_angle_and_axis3(t.angle, t.axis);
585  comp->view = View(t.angle, t.axis);
586  comp->fom = exp(-0.14*md->Epot);
587  if ( verbose )
588  cout << i << ":\t" << comp->loc[0] << tab << comp->loc[1] << tab <<
589  comp->loc[2] << tab << comp->view[0] << tab << comp->view[1] << tab <<
590  comp->view[2] << tab << comp->view.angle()*180.0/M_PI << tab << md->Epot << endl;
591  }
592 
593  molgroup_list_kill(mglist);
594 
595  return i;
596 }
597 
605 {
606  if ( !map ) return 0;
607 
608  molgroup->min[0] = -map->image->origin()[0]*map->sampling(0)[0];
609  molgroup->min[1] = -map->image->origin()[1]*map->sampling(0)[1];
610  molgroup->min[2] = -map->image->origin()[2]*map->sampling(0)[2];
611  molgroup->max[0] = molgroup->min[0] + map->sizeX()*map->sampling(0)[0];
612  molgroup->max[1] = molgroup->min[1] + map->sizeY()*map->sampling(0)[1];
613  molgroup->max[2] = molgroup->min[2] + map->sizeZ()*map->sampling(0)[2];
614  molgroup->box = molgroup->max - molgroup->min;
615 
616  return 0;
617 }
618 
627 {
628  Bmolecule* mol;
629  Bresidue* res;
630  Batom* atom;
631  long i, natom(0), natom_in(0);
632 
633  for ( mol = molgroup->mol; mol; mol = mol->next ) {
634  for( res = mol->res; res; res = res->next ) {
635  for ( atom = res->atom; atom; atom = atom->next ) {
636  i = ( atom->coord[0] >= min[0] && atom->coord[0] <= max[0] );
637  i += ( atom->coord[1] >= min[1] && atom->coord[1] <= max[1] );
638  i += ( atom->coord[2] >= min[2] && atom->coord[2] <= max[2] );
639  if ( i > 2 ) natom_in++;
640  natom++;
641  }
642  }
643  }
644 
645  if ( verbose )
646  cout << "Fraction within bounding box: " << natom_in*1.0/natom << endl;
647 
648  return natom_in;
649 }
650 
661 double monte_rigid_body_fit_energy(Bmolgroup* molgroup, Bimage* map, Bmd* md)
662 {
663  md->Epot = molgroup_atom_overlap(molgroup, md);
664 
665  if ( map && md->Kmap ) {
666  md->Emap = molgroup_map_energy(molgroup, map, md->Kmap);
667  md->Epot += md->Emap;
668  }
669 
670  if ( md->Kpoint ) {
671  md->Epoint = md->Kpoint*md->point.distance(molgroup_center_of_mass(molgroup));
672  md->Epot += md->Epoint;
673  }
674 
675  if ( md->Kbond ) {
676  md->Ebond = md_bond_forces(molgroup, md->Kbond, md->wrap);
677  md->Epot += md->Ebond;
678  }
679 
680  return md->Epot;
681 }
682 
693 double monte_atom_fit_energy(Bmolgroup* molgroup, Bimage* map, Bmd* md)
694 {
695  md_zero_forces(molgroup);
696 
697  md->Ebond = md_bond_forces(molgroup, md->Kbond, md->wrap);
698 
699  md->Eangle = md_angular_forces(molgroup, md->Kangle, md->wrap);
700 
701  md_nonbonded_forces(molgroup, md);
702 
703  md->Epoint = 0;
704  if ( md->Kpoint )
705  md->Epoint = md->Kpoint*md->point.distance(molgroup_center_of_mass(molgroup));
706 
707  md->Emap = 0;
708  if ( map ) md->Emap = molgroup_map_energy(molgroup, map, md->Kmap);
709 
710  md->Epot = md->Ebond + md->Eangle + md->Evdw + md->Eelec + md->Epoint + md->Emap;
711 
712  return md->Epot;
713 }
714 
725 double monte_bond_fit_energy(Bmolgroup* molgroup, Bimage* map, Bmd* md)
726 {
727  md_zero_forces(molgroup);
728 
729  md->Ebond = md_bond_forces(molgroup, md->Kbond, md->wrap);
730 
731  md->Eangle = md_angular_forces(molgroup, md->Kangle, md->wrap);
732 
733  md_nonbonded_forces(molgroup, md);
734 
735  md->Epoint = 0;
736  if ( md->Kpoint )
737  md->Epoint = md->Kpoint*md->point.distance(molgroup_center_of_mass(molgroup));
738 
739  md->Emap = 0;
740  if ( map ) md->Emap = molgroup_bond_fit_map_energy(molgroup, map, md->Kmap, md->bondsteps);
741 
742  md->Epot = md->Ebond + md->Eangle + md->Evdw + md->Eelec + md->Epoint + md->Emap;
743 
744  return md->Epot;
745 }
746 
747 
759 double molgroup_atom_overlap(Bmolgroup* molgroup, Bmd* md)
760 {
761  if ( md->Ksep <= 0 ) return 0;
762 
763  long n(0), i, x, y, z;
764  Vector3<double> box = molgroup->box;
765  Vector3<double> sampling(md->sepdist, md->sepdist, md->sepdist);
766  Vector3<int> size;
767  size[0] = (int) (box[0]/sampling[0] + 0.001);
768  size[1] = (int) (box[1]/sampling[1] + 0.001);
769  size[2] = (int) (box[2]/sampling[2] + 0.001);
770  size = size.max(1);
771  sampling[0] = box[0]/size[0] + 0.001;
772  sampling[1] = box[1]/size[1] + 0.001;
773  sampling[2] = box[2]/size[2] + 0.001;
774  long boxsize = (long) size.volume();
775 
776  Bmolgroup* mg;
777  Bmolecule* mol;
778  Bresidue* res;
779  Batom* atom;
780 
781  Bimage* p = new Bimage(Float, TSimple, size, 1);
782  float* data = (float *) p->data_pointer();
783  p->next = new Bimage(Float, TSimple, p->size(), p->images());
784  float* dataone = (float *) p->next->data_pointer();
785 
786  for ( mg = molgroup; mg; mg = mg->next ) {
787  for ( mol = mg->mol; mol; mol = mol->next ) {
788  for ( res = mol->res; res; res = res->next ) {
789  for ( atom = res->atom; atom; atom = atom->next ) {
790  x = (long) ((atom->coord[0] - mg->min[0])/sampling[0]);
791  y = (long) ((atom->coord[1] - mg->min[1])/sampling[1]);
792  z = (long) ((atom->coord[2] - mg->min[2])/sampling[2]);
793  if ( x>=0 && x<size[0] && y>=0 && y<size[1] && z>=0 && z<size[2] ) {
794  i = p->index(x, y, z);
795  dataone[i] = 1;
796  }
797  }
798  }
799  if ( !mg->select.contains("FILE") ) {
800  for ( i=0; i<boxsize; i++ ) {
801  data[i] += dataone[i];
802  dataone[i] = 0;
803  }
804  }
805  }
806  if ( mg->select.contains("FILE") ) {
807  for ( i=0; i<boxsize; i++ ) {
808  data[i] += dataone[i];
809  dataone[i] = 0;
810  }
811  }
812  }
813 
814  if ( verbose & VERB_DEBUG ) {
815  Bstring filename("overlap.map");
816  write_img(filename, p, 0);
817  }
818 
819  md->Esep = 0;
820 
821  for ( i=n=0; i<boxsize; i++ ) {
822  if ( data[i] ) {
823  n++;
824  if ( data[i] > 1 ) md->Esep += data[i] - 1;
825  }
826  }
827 
828  md->Esep *= md->Ksep * md->sepdist/n;
829 
830  delete p;
831 
832  return md->Esep;
833 }
834 
847 int molgroup_rigid_body_transform(Bmolgroup* molgroup, double max_angle, double shift_std)
848 {
849  double irm = 1.0/get_rand_max();
850  Bmolecule* mol;
851 
852  Transform t;
853 
854  t.axis = vector3_random(-1, 1);
855  t.axis.normalize();
856 
857  t.angle = max_angle*(random()*irm*2 - 1);
858 
859  t.trans = vector3_random_gaussian(0, shift_std);
860 
861  if ( molgroup->select.contains("ALL") ) {
862  t.origin = molgroup_center_of_mass(molgroup);
863  molgroup_coor_rotate(molgroup, t);
864  } else if ( molgroup->select.contains("FILE") ) {
865  for ( ; molgroup; molgroup=molgroup->next ) if ( molgroup->sel ) {
866  t.origin = molgroup_center_of_mass(molgroup);
867  molgroup_coor_rotate(molgroup, t);
868  molgroup->sel = 0;
869  }
870  } else {
871  for ( mol=molgroup->mol; mol; mol=mol->next ) if ( mol->sel ) {
872  t.origin = mol_center_of_mass(mol);
873  mol_coor_rotate(mol, t);
874  mol->sel = 0;
875  }
876  }
877 
878  return 0;
879 }
880 
891 int molgroup_move_atoms_down_energy(Bmolgroup* molgroup, double max_angle, double max_shift)
892 {
893  Bmolecule* mol;
894  Bresidue* res;
895  Batom* atom;
896 
897  double irm = 1.0/get_rand_max();
898  Vector3<double> shift;
899 
900  for ( mol = molgroup->mol; mol; mol = mol->next ) {
901  for ( res = mol->res; res; res = res->next ) {
902  for ( atom = res->atom; atom; atom = atom->next ) {
903  shift = atom->F * (random()*irm);
904  shift = shift.min(max_shift);
905  atom->coord += shift;
906  }
907  }
908  }
909 
910  return 0;
911 }
912 
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bcomponent
Definition: rwmodel.h:51
Bmd::Epoint
double Epoint
Definition: rwmd.h:78
mcm_molecule_list
int mcm_molecule_list(Bmolgroup *molgroup, Bmodel *model, Bmd *md, Bimage *map, double beta, double max_angle, double max_shift, long max_iter, int rigid)
Monte Carlo fit of a molecule to a list of locations and orientations.
Definition: mol_monte.cpp:485
mol_center_of_mass
Vector3< double > mol_center_of_mass(Bmolecule *mol)
Calculates the center of mass of a molecule.
Definition: mol_util.cpp:401
mol_map_energy
double mol_map_energy(Bmolecule *mol, Bimage *map, double Kmap)
Energy calculation of a molecule fit into a map.
Definition: mol_map_energy.cpp:83
molgroup_kill
int molgroup_kill(Bmolgroup *molgroup)
Destroys a molecule group.
Definition: rwmolecule.cpp:374
Bmolgroup::select
Bstring select
Definition: rwmolecule.h:185
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bresidue::next
Bresidue * next
Definition: rwmolecule.h:50
Bcomptype::fmod
Bstring fmod
Definition: rwmodel.h:29
Quaternion
Definition: Quaternion.h:25
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
Bstring
Definition: Bstring.h:29
Bimage::next
Bimage * next
Definition: Bimage.h:179
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
vector3_random_gaussian
Vector3< double > vector3_random_gaussian(double avg, double std)
Generates a random vector within a random gaussian-distributed length.
Definition: random_numbers.cpp:502
molgroup_map_energy
double molgroup_map_energy(Bmolgroup *molgroup, Bimage *map, double Kmap)
Energy calculation of a molecule fit into a map.
Definition: mol_map_energy.cpp:23
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
molgroup_list_kill
int molgroup_list_kill(Bmolgroup *molgroup)
Destroys a molecule group linked list.
Definition: rwmolecule.cpp:347
monte_bond_fit_energy
double monte_bond_fit_energy(Bmolgroup *molgroup, Bimage *map, Bmd *md)
Calculates the potential energy for fitting bonds to a map.
Definition: mol_monte.cpp:725
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bresidue::atom
Batom * atom
Definition: rwmolecule.h:54
monte_atom_fit_energy
double monte_atom_fit_energy(Bmolgroup *molgroup, Bimage *map, Bmd *md)
Calculates the potential energy for fitting atoms to a map.
Definition: mol_monte.cpp:693
Bmd::Emap
double Emap
Definition: rwmd.h:79
Bcomptype
Definition: rwmodel.h:25
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
random_numbers.h
Header file for functions for creating random images.
rwmd.h
Header to read and write molecular dynamics parameters in STAR format.
display_energy
int display_energy(long iter, Bmd *md, double dE)
Definition: mol_monte.cpp:42
Bmd::point
Vector3< float > point
Definition: rwmd.h:82
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
mcm_molecule_groups
int mcm_molecule_groups(Bmolgroup *molgroup, Bmodel *model, Bmd *md, Bimage *map, double beta, double max_angle, double max_shift, long max_iter, int rigid)
Monte Carlo fit of a set of molecule groups.
Definition: mol_monte.cpp:543
Bmd::Kpoint
double Kpoint
Definition: rwmd.h:64
vector3_random
Vector3< double > vector3_random(const double min, const double max)
Generates a random vector within a defined cube.
Definition: random_numbers.cpp:440
View
Definition: View.h:26
Bmd
Definition: rwmd.h:56
Bmd::Ebond
double Ebond
Definition: rwmd.h:73
verbose
int verbose
Bmd::Ksep
double Ksep
Definition: rwmd.h:63
Bmd::Kbond
double Kbond
Definition: rwmd.h:59
display_energy_headers
int display_energy_headers(Bmd *md)
Definition: mol_monte.cpp:27
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
mol_transform.h
Library routines used for atomic coordinate transformations.
Transform::origin
Vector3< double > origin
Definition: Transform.h:27
Batom::next
Batom * next
Definition: rwmolecule.h:25
Bmd::Evdw
double Evdw
Definition: rwmd.h:76
Bmolecule::res
Bresidue * res
Definition: rwmolecule.h:122
md_nonbonded_forces
double md_nonbonded_forces(Bmolgroup *molgroup, Bmd *md)
Calculates the non-bonded forces and energy.
Definition: mol_md.cpp:283
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
molgroup_coor_rotate
int molgroup_coor_rotate(Bmolgroup *molgroup, Transform t)
Rotates and translates a molecule group.
Definition: mol_transform.cpp:87
model_init
Bmodel * model_init(Bstring &id)
Initializes a model structure.
Definition: rwmodel.cpp:147
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
model_add_type_by_filename
Bcomptype * model_add_type_by_filename(Bmodel *model, Bstring &filename, int num)
Add a type to a model if the type with the given file name does not exist already.
Definition: rwmodel.cpp:437
mol_coor_rotate
int mol_coor_rotate(Bmolecule *mol, Transform t)
Rotates and translates a molecule.
Definition: mol_transform.cpp:131
Bimage::images
long images()
Definition: Bimage.h:504
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
TSimple
@ TSimple
Definition: Bimage.h:59
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Bmd::Esep
double Esep
Definition: rwmd.h:77
Vector3::normalize
double normalize()
Definition: Vector3.h:238
Bmd::Kmap
double Kmap
Definition: rwmd.h:65
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
mol_bonds.h
Header for molecular dynamics.
rwmolecule.h
Header file for reading reflection files.
molgroup_bond_fit_map_energy
double molgroup_bond_fit_map_energy(Bmolgroup *molgroup, Bimage *map, double Kmap, int steps)
Energy calculation of a molecule fit into a map.
Definition: mol_map_energy.cpp:250
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
molgroup_find_transformation
Transform molgroup_find_transformation(Bmolgroup *molgroup1, Bmolgroup *molgroup2)
Determines the transformation between two groups of identical molecules.
Definition: mol_compare.cpp:51
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
Bimage
General image parameter class.
Definition: Bimage.h:177
Bmolgroup::box
Vector3< double > box
Definition: rwmolecule.h:184
Bmolgroup::filename
Bstring filename
Definition: rwmolecule.h:172
Bmolgroup::next
Bmolgroup * next
Definition: rwmolecule.h:171
Bmd::Kelec
double Kelec
Definition: rwmd.h:61
molgroup_rigid_body_transform
int molgroup_rigid_body_transform(Bmolgroup *molgroup, double max_angle, double shift_std)
Randomly transforms a molecule or molecule group.
Definition: mol_monte.cpp:847
molgroup_generate_masked_grid_list
Bmolgroup * molgroup_generate_masked_grid_list(Bmolgroup *molgroup, Bimage *pmask, Vector3< double > grid_sampling, Bstring filename)
Generates multiple versions of a molecule at different locations.
Definition: mol_monte.cpp:260
Bmolgroup::sel
int sel
Definition: rwmolecule.h:186
mol_compare.h
Library routines used to compare sets of atomic coordinates.
Bmodel
Definition: rwmodel.h:119
molgroup_coor_shift
int molgroup_coor_shift(Bmolgroup *molgroup, Vector3< double > shift)
Translates a molecule group.
Definition: mol_transform.cpp:28
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Bmd::pointdecay
double pointdecay
Definition: rwmd.h:70
M_PI
#define M_PI
Definition: utilities.h:63
molgroup_set_box_to_map_boundaries
int molgroup_set_box_to_map_boundaries(Bmolgroup *molgroup, Bimage *map)
Sets the box in a molgroup to that defined by the map boundaries.
Definition: mol_monte.cpp:604
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bmd::Kangle
double Kangle
Definition: rwmd.h:60
molgroup_move_atoms_down_energy
int molgroup_move_atoms_down_energy(Bmolgroup *molgroup, double max_angle, double max_shift)
Move atoms random distances down the energy gradient.
Definition: mol_monte.cpp:891
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bcomponent::view
View view
Definition: rwmodel.h:59
Batom::F
Vector3< double > F
Definition: rwmolecule.h:36
Bcomponent::fom
float fom
Definition: rwmodel.h:65
monte_rigid_body_fit_energy
double monte_rigid_body_fit_energy(Bmolgroup *molgroup, Bimage *map, Bmd *md)
Calculates the potential energy for rigid body fitting.
Definition: mol_monte.cpp:661
Bmolgroup::max
Vector3< double > max
Definition: rwmolecule.h:183
Bcomponent::type
Bcomptype * type
Definition: rwmodel.h:54
molgroup_center_of_mass
Vector3< double > molgroup_center_of_mass(Bmolgroup *molgroup)
Calculates the center of mass of a molecule group.
Definition: mol_util.cpp:307
Bmd::Eelec
double Eelec
Definition: rwmd.h:75
monte_carlo_metropolis
Bmolgroup * monte_carlo_metropolis(Bmolgroup *molgroup, Bmd *md, Bimage *map, double beta, double max_angle, double max_shift, long max_iter, int rigid, double(Efunc)(Bmolgroup *, Bimage *, Bmd *), int(Tfunc)(Bmolgroup *, double, double))
Definition: mol_monte.cpp:71
mol_monte.h
Headers of functions using a monte carlo metroplis algorithm to energy minimize molecular positions.
model
Bmodel * model
Definition: bshow.cpp:34
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
Vector3::volume
double volume()
Definition: Vector3.h:237
Bmolgroup::min
Vector3< double > min
Definition: rwmolecule.h:182
Transform::trans
Vector3< double > trans
Definition: Transform.h:30
rwimg.h
Header file for 2D and 3D image I/O.
md_angular_forces
double md_angular_forces(Bmolgroup *molgroup, double Kangle, int wrap)
Calculates the covalent bond angular forces and energy.
Definition: mol_md.cpp:233
Matrix.h
Generalized matrix class.
molgroup_generate_orientation_list
Bmolgroup * molgroup_generate_orientation_list(Bmolgroup *molgroup, double angle_step, Bstring filename, int whole)
Generates multiple versions of a molecule at different locations.
Definition: mol_monte.cpp:386
beta
double beta(double volt)
Calculates the relative electron velocity.
Definition: ctf.cpp:500
md_zero_forces
int md_zero_forces(Bmolgroup *molgroup)
Zero all atomic forces.
Definition: mol_md.cpp:161
View::quaternion
Quaternion quaternion()
Definition: View.h:130
molgroup_test_if_within_box
long molgroup_test_if_within_box(Bmolgroup *molgroup, Vector3< double > min, Vector3< double > max)
Tests if a molecule overlaps with a defined box.
Definition: mol_monte.cpp:626
Bresidue
Definition: rwmolecule.h:49
linked_list.h
Header file for generalized linked list functions.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
Bmd::Kvdw
double Kvdw
Definition: rwmd.h:62
model_add_type_by_id
Bcomptype * model_add_type_by_id(Bmodel *model, Bstring &id)
Add a type to a model if the identifier does not exist already.
Definition: rwmodel.cpp:414
molgroup_atom_overlap
double molgroup_atom_overlap(Bmolgroup *molgroup, Bmd *md)
Calculates an energy term based on atom overlap.
Definition: mol_monte.cpp:759
md_bond_forces
double md_bond_forces(Bmolgroup *molgroup, double Kbond, int wrap)
Calculates the covalent bond length forces and energy.
Definition: mol_md.cpp:190
mol_map_energy.h
Headers of functions to calculate estimates of the fitting of molecules to maps.
Vector3::min
Type min()
Definition: Vector3.h:169
irm
double irm(1.0L/get_rand_max())
Bmolgroup
Definition: rwmolecule.h:170
Transform::axis
Vector3< double > axis
Definition: Transform.h:28
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Transform::angle
double angle
Definition: Transform.h:29
get_rand_max
long get_rand_max()
Finds the maximum random number for a system.
Definition: random_numbers.cpp:22
Bmd::Epot
double Epot
Definition: rwmd.h:81
Transform
Definition: Transform.h:24
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
Bmd::wrap
int wrap
Definition: rwmd.h:72
molgroup_list_copy
Bmolgroup * molgroup_list_copy(Bmolgroup *molgroup)
Copies a molecule group list.
Definition: rwmolecule.cpp:522
Bmolgroup::fom
double fom
Definition: rwmolecule.h:187
Bmolecule::sel
int sel
Definition: rwmolecule.h:120
Bmolecule::fom
double fom
Definition: rwmolecule.h:121
Bmd::sepdist
double sepdist
Definition: rwmd.h:68
Float
@ Float
Definition: utilities.h:161
View::angle
double angle()
Definition: View.h:98
Bmd::cutoff
double cutoff
Definition: rwmd.h:69
molgroup_copy
Bmolgroup * molgroup_copy(Bmolgroup *molgroup)
Copies a molecule group.
Definition: rwmolecule.cpp:547
Bmd::Eangle
double Eangle
Definition: rwmd.h:74
Batom::coord
Vector3< double > coord
Definition: rwmolecule.h:30
mol_md.h
Header for molecular dynamics.
Batom
Definition: rwmolecule.h:24
Bmd::bondsteps
int bondsteps
Definition: rwmd.h:71
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110
Bcomponent::sel
int sel
Definition: rwmodel.h:66
mol_util.h
Library routines used for atomic coordinates.
md_calculate_deviations
double md_calculate_deviations(Bmolgroup *molgroup, int wrap)
Calculates deviations from the reference bond lengths and angles.
Definition: mol_bonds.cpp:742
Vector3::max
Type max()
Definition: Vector3.h:175