Bsoft  2.1.0
Bernard's software package
mol_transform.cpp
Go to the documentation of this file.
1 
9 #include "rwmolecule.h"
10 #include "mol_transform.h"
11 #include "mol_util.h"
12 #include "linked_list.h"
13 #include "utilities.h"
14 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
18 // Internal function prototypes
21 
29 {
30  if ( shift.length() < 1e-30 ) return 0;
31 
32  Bmolecule* mol;
33  Bresidue* res;
34  Batom* atom;
35 
36  if ( verbose & VERB_FULL )
37  cout << "Shifting coordinates: " << shift << endl;
38 
39  for ( mol = molgroup->mol; mol; mol = mol->next ) {
40  for( res = mol->res; res; res = res->next ) {
41  for ( atom = res->atom; atom; atom = atom->next ) {
42  atom->coord = atom->coord + shift;
43  }
44  }
45  }
46 
47  return 0;
48 }
49 
57 {
58  if ( shift.length() < 1e-30 ) return 0;
59 
60  Bresidue* res;
61  Batom* atom;
62 
63  if ( verbose & VERB_FULL )
64  cout << "Shifting coordinates: " << shift << endl;
65 
66  for( res = mol->res; res; res = res->next ) {
67  for ( atom = res->atom; atom; atom = atom->next ) {
68  atom->coord = atom->coord + shift;
69  }
70  }
71 
72 // mol_stats(mol);
73 
74  return 0;
75 }
76 
88 {
89  if ( ( t.angle == 0 || t.axis.length() < 1e-10 ) && ( t.trans.length() < 1e-10 ) )return 0;
90 
91  Matrix3 mat = Matrix3(t.axis, t.angle);
92 
93  Bmolecule* mol;
94  Bresidue* res;
95  Batom* atom;
96 
97  if ( verbose & VERB_FULL ) {
98  cout << endl << "Rotating and translating coordinates:" << endl;
99  cout << "Rotation axis and angle: " << t.axis << " " << t.angle*180.0/M_PI << endl;
100  cout << "Rotation origin: " << t.origin << endl;
101  cout << "Rotation matrix:" << endl;
102  cout << mat << endl;
103  cout << "Determinant: " << mat.determinant() << endl;
104  cout << "Translation: " << t.trans << endl << endl;
105  }
106 
107  Vector3<double> shift = t.origin + t.trans;
108  for ( mol = molgroup->mol; mol; mol = mol->next ) {
109  for( res = mol->res; res; res = res->next ) {
110  for ( atom = res->atom; atom; atom = atom->next ) {
111  atom->coord = atom->coord - t.origin;
112  atom->coord = mat * atom->coord;
113  atom->coord = atom->coord + shift;
114  }
115  }
116  }
117 
118  return 0;
119 }
120 
132 {
133  if ( verbose & VERB_DEBUG )
134  cout << "DEBUG mol_coor_rotate: t.angle=" << t.angle << endl;
135 
136  if ( t.angle < 1e-20 ) return 0;
137 
138  if ( t.axis.length() < 1e-10 ) return 0;
139 
140  Matrix3 mat = Matrix3(t.axis, t.angle);
141 
142  Bresidue* res;
143  Batom* atom;
144 
145  if ( verbose & VERB_FULL ) {
146  cout << endl << "Rotating and translating coordinates:" << endl;
147  cout << "Rotation axis and angle: " << t.axis << " " << t.angle*180.0/M_PI << endl;
148  cout << "Rotation origin: " << t.origin << endl;
149  cout << "Rotation matrix:" << endl;
150  cout << mat << endl;
151  cout << "Determinant: " << mat.determinant() << endl;
152  cout << "Translation: " << t.trans << endl << endl;
153  }
154 
155  Vector3<double> shift = t.origin + t.trans;
156  for( res = mol->res; res; res = res->next ) {
157  for ( atom = res->atom; atom; atom = atom->next ) {
158  atom->coord = atom->coord - t.origin;
159  atom->coord = mat * atom->coord;
160  atom->coord = atom->coord + shift;
161  }
162  }
163 
164 // mol_stats(mol);
165 
166  return 0;
167 }
168 
181 {
182  if ( verbose & VERB_FULL ) {
183  cout << "Rotating and translating coordinates:" << endl;
184  cout << "Rotation to view: " << view << endl;
185  }
186 
187  Matrix3 mat = view.matrix();
188 
189  return molgroup_rotate(molgroup, mat, origin, trans);
190 }
191 
204 {
205  if ( verbose & VERB_FULL ) {
206  cout << "Rotating and translating coordinates:" << endl;
207  cout << "Rotation from view: " << view << endl;
208  }
209 
210  Matrix3 mat = view.matrix();
211  mat = mat.transpose();
212 
213  return molgroup_rotate(molgroup, mat, origin, trans);
214 }
215 
217 {
218  Bmolgroup* mol_rot = molgroup_copy(molgroup);
219 
220  Vector3<double> shift = origin + trans;
221 
222  Bmolecule* mol;
223  Bresidue* res;
224  Batom* atom;
225 
226  if ( verbose & VERB_FULL ) {
227  cout << "Origin: " << origin << endl;
228  cout << "Rotation matrix:" << endl;
229  cout << mat << endl;
230  cout << "Determinant: " << mat.determinant() << endl;
231  cout << "Translation: " << trans << endl << endl;
232  }
233 
234  for ( mol = mol_rot->mol; mol; mol = mol->next ) {
235  for( res = mol->res; res; res = res->next ) {
236  for ( atom = res->atom; atom; atom = atom->next ) {
237  atom->coord = atom->coord - origin;
238  atom->coord = mat * atom->coord;
239  atom->coord = atom->coord + shift;
240  }
241  }
242  }
243 
244  return mol_rot;
245 }
246 
259 {
260  if ( verbose & VERB_FULL ) {
261  cout << "Rotating and translating coordinates:" << endl;
262  cout << "Rotation to view: " << view << endl;
263  }
264 
265  Matrix3 mat = view.matrix();
266 
267  return mol_rotate(mol, mat, origin, trans);
268 }
269 
282 {
283  if ( verbose & VERB_FULL ) {
284  cout << "Rotating and translating coordinates:" << endl;
285  cout << "Rotation from view: " << view << endl;
286  }
287 
288  Matrix3 mat = view.matrix();
289  mat = mat.transpose();
290 
291  return mol_rotate(mol, mat, origin, trans);
292 }
293 
295 {
296  Bmolecule* mol_rot = molecule_copy(mol);
297 
298  Vector3<double> shift = origin + trans;
299 
300  Bresidue* res;
301  Batom* atom;
302 
303  if ( verbose & VERB_FULL ) {
304  cout << "Origin: " << origin << endl;
305  cout << "Rotation matrix:" << endl;
306  cout << mat << endl;
307  cout << "Determinant: " << mat.determinant() << endl;
308  cout << "Translation: " << trans << endl << endl;
309  }
310 
311  for( res = mol_rot->res; res; res = res->next ) {
312  for ( atom = res->atom; atom; atom = atom->next ) {
313  atom->coord = atom->coord + origin;
314  atom->coord = mat * atom->coord;
315  atom->coord = atom->coord + shift;
316  }
317  }
318 
319  return mol_rot;
320 }
321 
333 {
334  Matrix3 mat = Matrix3(t.axis, t.angle);
335 
336  Bmolecule* mol;
337  Bresidue* res;
338  Batom* atom;
339 
340  if ( verbose & VERB_FULL ) {
341  cout << "Transforming coordinates:" << endl;
342  cout << "Rotation axis and angle: " << t.axis << " " << t.angle*180.0/M_PI << endl;
343  cout << "Rotation origin: " << t.origin << endl;
344  cout << "Rotation matrix:" << endl;
345  cout << mat << endl;
346  cout << "Determinant: " << mat.determinant() << endl;
347  cout << "Translation: " << t.trans << endl;
348  cout << "Scale: " << t.scale << endl << endl;
349  }
350 
351  mat = t.scale * mat;
352 
353  Vector3<double> shift = t.origin + t.trans;
354 
355  for ( mol = molgroup->mol; mol; mol = mol->next ) {
356  for( res = mol->res; res; res = res->next ) {
357  for ( atom = res->atom; atom; atom = atom->next ) {
358  atom->coord = atom->coord - t.origin;
359  atom->coord = mat * atom->coord;
360  atom->coord = atom->coord + shift;
361  }
362  }
363  }
364 
365  return 0;
366 }
367 
379 {
380  Matrix3 mat = Matrix3(t.axis, t.angle);
381 
382  Bresidue* res;
383  Batom* atom;
384 
385  if ( verbose & VERB_FULL ) {
386  cout << "Transforming and translating coordinates:" << endl;
387  cout << "Rotation axis and angle: " << t.axis << " " << t.angle*180.0/M_PI << endl;
388  cout << "Rotation origin: " << t.origin << endl;
389  cout << "Rotation matrix:" << endl;
390  cout << mat << endl;
391  cout << "Determinant: " << mat.determinant() << endl;
392  cout << "Translation: " << t.trans << endl;
393  cout << "Scale: " << t.scale << endl << endl;
394  }
395 
396  mat = t.scale * mat;
397 
398  Vector3<double> shift = t.origin + t.trans;
399 
400  for( res = mol->res; res; res = res->next ) {
401  for ( atom = res->atom; atom; atom = atom->next ) {
402  atom->coord = atom->coord - t.origin;
403  atom->coord = mat * atom->coord;
404  atom->coord = atom->coord + shift;
405  }
406  }
407 
408 // mol_stats(mol);
409 
410  return 0;
411 }
412 
420 {
421  Bmolecule* mol;
422  Bresidue* res;
423  Batom* atom;
424 
425  if ( verbose & VERB_PROCESS )
426  cout << "Inverting coordinates: " << point << endl;
427 
428  for ( mol = molgroup->mol; mol; mol = mol->next ) {
429  for( res = mol->res; res; res = res->next ) {
430  for ( atom = res->atom; atom; atom = atom->next ) {
431  atom->coord[0] = 2*point[0] - atom->coord[0];
432  atom->coord[1] = 2*point[1] - atom->coord[1];
433  atom->coord[2] = 2*point[2] - atom->coord[2];
434  }
435  }
436  }
437 
438  return 0;
439 }
440 
451 {
452  Bmolecule* mol;
453  Bresidue* res;
454  Batom* atom;
455  double summass;
456  Vector3<double> csum, shift;
457  Vector3<double> box = molgroup->box;
458 
459  if ( verbose & VERB_PROCESS ) {
460  cout << "Resolving periodic box coordinates:" << endl;
461  cout << "Box: " << molgroup->box << endl << endl;
462  }
463 
464  for ( mol = molgroup->mol; mol; mol = mol->next ) {
465  csum = 0;
466  summass = 0;
467  for( res = mol->res; res; res = res->next ) {
468  for ( atom = res->atom; atom; atom = atom->next ) {
469  if ( summass == 0 ) {
470  csum = atom->coord * atom->mass;
471  } else {
472  shift = csum / summass;
473  shift += vector3_difference_PBC(atom->coord, shift, box);
474  csum += shift * atom->mass;
475  }
476  summass += atom->mass;
477  }
478  }
479  if ( summass ) csum /= summass;
480  if ( csum[0] < 0 ) csum[0] += box[0];
481  if ( csum[1] < 0 ) csum[1] += box[1];
482  if ( csum[2] < 0 ) csum[2] += box[2];
483  if ( csum[0] > molgroup->box[0] ) csum[0] -= box[0];
484  if ( csum[1] > molgroup->box[1] ) csum[1] -= box[1];
485  if ( csum[2] > molgroup->box[2] ) csum[2] -= box[2];
486  if ( verbose & VERB_FULL )
487  cout << "COM for molecule " << mol->id << ", mass " << summass << ": " << csum << endl;
488  for( res = mol->res; res; res = res->next ) {
489  for ( atom = res->atom; atom; atom = atom->next ) {
490  shift = atom->coord - csum;
491  if ( 2*shift[0] > box[0] ) shift[0] = -box[0];
492  else if ( 2*shift[0] < -box[0] ) shift[0] = box[0];
493  else shift[0] = 0;
494  if ( 2*shift[1] > box[1] ) shift[1] = -box[1];
495  else if ( 2*shift[1] < -box[1] ) shift[1] = box[1];
496  else shift[1] = 0;
497  if ( 2*shift[2] > box[2] ) shift[2] = -box[2];
498  else if ( 2*shift[2] < -box[2] ) shift[2] = box[2];
499  else shift[2] = 0;
500  atom->coord = atom->coord + shift;
501  }
502  }
503  }
504 
505  return 0;
506 }
507 
514 {
515  Bmolecule* mol;
516  Bresidue* res;
517  Batom* atom;
518 
519  if ( verbose & VERB_PROCESS ) {
520  cout << "Wrapping coordinates within a periodic box:" << endl;
521  cout << "Box: " << molgroup->box << endl << endl;
522  }
523 
524  for ( mol = molgroup->mol; mol; mol = mol->next ) {
525  for( res = mol->res; res; res = res->next ) {
526  for ( atom = res->atom; atom; atom = atom->next ) {
527  atom->coord = vector3_set_PBC(atom->coord, molgroup->box);
528  }
529  }
530  }
531 
532  return 0;
533 }
534 
545 {
546  if ( shift.length2() < 1e-30 ) return 0;
547 
548  Bmolecule* mol;
549  Bresidue* res;
550  Batom* atom;
551 
552  if ( verbose & VERB_PROCESS ) {
553  cout << "Shifting coordinates: " << shift << endl;
554  cout << "Wrapping within box: " << molgroup->box << endl;
555  }
556 
557  for ( mol = molgroup->mol; mol; mol = mol->next ) {
558  for( res = mol->res; res; res = res->next ) {
559  for ( atom = res->atom; atom; atom = atom->next ) {
560  atom->coord = atom->coord + shift;
561  atom->coord = vector3_set_PBC(atom->coord, molgroup->box);
562  }
563  }
564  }
565 
566  return 0;
567 }
568 
582  Matrix3 mat, Vector3<double> shift)
583 {
584  if ( shift.length2() < 1e-30 ) return 0;
585 
586  Bmolecule* mol;
587  Bresidue* res;
588  Batom* atom;
589 
590  if ( verbose & VERB_PROCESS ) {
591  cout << endl << "Rotating and translating coordinates:" << endl;
592  cout << "Rotation origin: " << origin << endl;
593  cout << "Rotation matrix:" << endl;
594  cout << mat << endl;
595  cout << "Determinant: " << mat.determinant() << endl;
596  cout << "Translation after rotation: " << shift << endl;
597  cout << "Wrapping within box: " << molgroup->box << endl << endl;
598  }
599 
600  shift += origin;
601 
602  for ( mol = molgroup->mol; mol; mol = mol->next ) {
603  for( res = mol->res; res; res = res->next ) {
604  for ( atom = res->atom; atom; atom = atom->next ) {
605  atom->coord = atom->coord - origin;
606  atom->coord = mat * atom->coord;
607  atom->coord = atom->coord + shift;
608  atom->coord = vector3_set_PBC(atom->coord, molgroup->box);
609  }
610  }
611  }
612 
613  return 0;
614 }
615 
626 {
628 
629  com = -com;
630 
631  molgroup_coor_shift(molgroup, com);
632 
633  return 0;
634 }
635 
646 {
648 
649  com = -com;
650 
651  mol_coor_shift(mol, com);
652 
653  return 0;
654 }
655 
667 {
669 
670  molgroup_coor_shift(molgroup, (location - com));
671 
672  return 0;
673 }
674 
675 
mol_coor_shift
int mol_coor_shift(Bmolecule *mol, Vector3< double > shift)
Translates a molecule.
Definition: mol_transform.cpp:56
vector3_difference_PBC
Vector3< T1 > vector3_difference_PBC(Vector3< T1 > &v1, Vector3< T1 > &v2, Vector3< T2 > &box)
Definition: Vector3.h:697
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
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bresidue::next
Bresidue * next
Definition: rwmolecule.h:50
Matrix3::transpose
Matrix3 transpose() const
Definition: Matrix3.h:268
Bresidue::atom
Batom * atom
Definition: rwmolecule.h:54
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Vector3< double >
molgroup_rotate
Bmolgroup * molgroup_rotate(Bmolgroup *molgroup, Matrix3 mat, Vector3< double > origin, Vector3< double > trans)
Definition: mol_transform.cpp:216
View
Definition: View.h:26
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
mol_transform.h
Library routines used for atomic coordinate transformations.
Transform::origin
Vector3< double > origin
Definition: Transform.h:27
molgroup_coor_shift
int molgroup_coor_shift(Bmolgroup *molgroup, Vector3< double > shift)
Translates a molecule group.
Definition: mol_transform.cpp:28
Batom::next
Batom * next
Definition: rwmolecule.h:25
Bmolecule::res
Bresidue * res
Definition: rwmolecule.h:122
mol_rotate_to_view
Bmolecule * mol_rotate_to_view(Bmolecule *mol, View view, Vector3< double > origin, Vector3< double > trans)
Rotates a molecule to a specified view and translates it afterwards.
Definition: mol_transform.cpp:258
mol_rotate
Bmolecule * mol_rotate(Bmolecule *mol, Matrix3 mat, Vector3< double > origin, Vector3< double > trans)
Definition: mol_transform.cpp:294
molgroup_coor_invert
int molgroup_coor_invert(Bmolgroup *molgroup, Vector3< double > point)
Inverts the coordinates of a molecule through a given point.
Definition: mol_transform.cpp:419
Matrix3
Definition: Matrix3.h:18
mol_shift_to_center_of_mass
int mol_shift_to_center_of_mass(Bmolecule *mol)
Translates a set of coordinates to the center of mass.
Definition: mol_transform.cpp:645
mol_rotate_from_view
Bmolecule * mol_rotate_from_view(Bmolecule *mol, View view, Vector3< double > origin, Vector3< double > trans)
Rotates a molecule from a specified view and translates it afterwards.
Definition: mol_transform.cpp:281
rwmolecule.h
Header file for reading reflection files.
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
mol_coor_rotate
int mol_coor_rotate(Bmolecule *mol, Transform t)
Rotates and translates a molecule.
Definition: mol_transform.cpp:131
Bmolecule
Definition: rwmolecule.h:107
Bmolgroup::box
Vector3< double > box
Definition: rwmolecule.h:184
molgroup_place_at_coordinates
int molgroup_place_at_coordinates(Bmolgroup *molgroup, Vector3< double > location)
Translates a set of coordinates to a defined center-of-mass.
Definition: mol_transform.cpp:666
verbose
int verbose
molgroup_rotate_to_view
Bmolgroup * molgroup_rotate_to_view(Bmolgroup *molgroup, View view, Vector3< double > origin, Vector3< double > trans)
Rotates a molecule group to a specified view and translates it afterwards.
Definition: mol_transform.cpp:180
Transform::scale
Vector3< double > scale
Definition: Transform.h:31
vector3_set_PBC
Vector3< T1 > vector3_set_PBC(Vector3< T1 > coord, Vector3< T2 > box)
Definition: Vector3.h:684
molgroup_coor_shift_rotate_PBC
int molgroup_coor_shift_rotate_PBC(Bmolgroup *molgroup, Vector3< double > origin, Matrix3 mat, Vector3< double > shift)
Translates and rotates a molecule within a periodic box.
Definition: mol_transform.cpp:581
molgroup_coor_rotate
int molgroup_coor_rotate(Bmolgroup *molgroup, Transform t)
Rotates and translates a molecule group.
Definition: mol_transform.cpp:87
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Batom::mass
double mass
Definition: rwmolecule.h:32
molgroup_coor_transform
int molgroup_coor_transform(Bmolgroup *molgroup, Transform t)
Rotates and translates a molecule group.
Definition: mol_transform.cpp:332
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
Transform::trans
Vector3< double > trans
Definition: Transform.h:30
mol_coor_transform
int mol_coor_transform(Bmolecule *mol, Transform t)
Rotates and translates a molecule.
Definition: mol_transform.cpp:378
molgroup_rotate_from_view
Bmolgroup * molgroup_rotate_from_view(Bmolgroup *molgroup, View view, Vector3< double > origin, Vector3< double > trans)
Rotates a molecule group from a specified view and translates it afterwards.
Definition: mol_transform.cpp:203
molgroup_pack_in_periodic_box
int molgroup_pack_in_periodic_box(Bmolgroup *molgroup)
Distributes coordinates across periodic box boundaries.
Definition: mol_transform.cpp:513
Matrix3::determinant
double determinant()
Definition: Matrix3.h:260
Bresidue
Definition: rwmolecule.h:49
molgroup_resolve_pbc
int molgroup_resolve_pbc(Bmolgroup *molgroup)
Recombines coordinates split across periodic box boundaries.
Definition: mol_transform.cpp:450
linked_list.h
Header file for generalized linked list functions.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bmolgroup
Definition: rwmolecule.h:170
Transform::axis
Vector3< double > axis
Definition: Transform.h:28
Transform::angle
double angle
Definition: Transform.h:29
View::matrix
Matrix3 matrix()
Definition: View.h:153
Transform
Definition: Transform.h:24
Vector3::length
double length()
Definition: Vector3.h:222
molgroup_coor_shift_PBC
int molgroup_coor_shift_PBC(Bmolgroup *molgroup, Vector3< double > shift)
Translates a molecule within a periodic box.
Definition: mol_transform.cpp:544
molgroup_copy
Bmolgroup * molgroup_copy(Bmolgroup *molgroup)
Copies a molecule group.
Definition: rwmolecule.cpp:547
Vector3::length2
double length2()
Definition: Vector3.h:221
Batom::coord
Vector3< double > coord
Definition: rwmolecule.h:30
molecule_copy
Bmolecule * molecule_copy(Bmolecule *mol)
Copies a molecule.
Definition: rwmolecule.cpp:599
Batom
Definition: rwmolecule.h:24
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110
mol_util.h
Library routines used for atomic coordinates.
molgroup_shift_to_center_of_mass
int molgroup_shift_to_center_of_mass(Bmolgroup *molgroup)
Translates a set of coordinates to the center of mass.
Definition: mol_transform.cpp:625