Bsoft  2.1.0
Bernard's software package
symmetry.cpp
Go to the documentation of this file.
1 
9 #include "symmetry.h"
10 #include "random_numbers.h"
11 #include "Euler.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 
40 {
41  int i, theorder, helix_dyad = 1;
42  double helix_rise = 1, helix_angle = M_PI;
43  Matrix3 mat(0,-1,0,1,0,0,0,0,1); // 90 degree rotation for I90
44 
45  Bsymmetry* sym = new Bsymmetry;
46  memset(sym, 0, sizeof(Bsymmetry));
47 
48  sym->label = symmetry_clean_pointgroup(symmetry_string);
49 
50  if ( verbose & VERB_FULL )
51  cout << endl << "Getting symmetry operators for " << sym->label << endl;
52 
53  if ( sym->label[0] == 'C' ) { // Cyclic point groups
54  if ( sym->label == "Cs" ) theorder = 1; // Reflection
55  else theorder = sym->label.substr(1,10).integer();
56  sym->n = 1;
57  sym->point = 100 + theorder;
58  sym->op = new Bsymop[sym->n]; memset(sym->op, 0, sym->n*sizeof(Bsymop));;
59  sym->op[0].axis[2] = 1;
60  sym->op[0].order = theorder;
61  sym->op[0].angle = M_PI*2.0L/theorder;
62  } else if ( sym->label[0] == 'D' ) { // Dihedral point groups
63  theorder = sym->label.substr(1,10).integer();
64  sym->n = 2;
65  sym->point = 200 + theorder;
66  sym->op = new Bsymop[sym->n]; memset(sym->op, 0, sym->n*sizeof(Bsymop));;
67  sym->op[0].axis[2] = 1;
68  sym->op[0].order = theorder;
69  sym->op[0].angle = M_PI*2.0L/theorder;
70  sym->op[1].axis[0] = 1; // Rotate around 2-fold x-axis
71  sym->op[1].order = 2;
72  sym->op[1].angle = M_PI;
73  } else if ( sym->label[0] == 'T' ) { // Tetrahedral point group
74  sym->point = 320;
75  sym->n = 3;
76  if ( sym->label.contains("-3") ) sym->n = 2;
77  sym->op = new Bsymop[sym->n]; memset(sym->op, 0, sym->n*sizeof(Bsymop));;
78  i = 0;
79  if ( !sym->label.contains("-3") ) {
80  sym->op[i].axis[0] = sym->op[i].axis[1] =
81  sym->op[i].axis[2] = 1; // Rotate around 3-fold (1,1,1)
82  sym->op[i].order = 3;
83  sym->op[i].angle = M_PI*2.0L/3.0;
84  i++;
85  }
86  sym->op[i].axis[2] = 1; // Rotate around 2-fold z-axis
87  sym->op[i].order = 2;
88  sym->op[i].angle = M_PI;
89  i++;
90  sym->op[i].axis[0] = 1; // Rotate around 2-fold x-axis
91  sym->op[i].order = 2;
92  sym->op[i].angle = M_PI;
93  } else if ( sym->label[0] == 'O' ) { // Octahedral point group
94  sym->point = 432;
95  sym->n = 3;
96  if ( sym->label.contains("-") ) sym->n = 2;
97  sym->op = new Bsymop[sym->n]; memset(sym->op, 0, sym->n*sizeof(Bsymop));;
98  i = 0;
99  if ( !sym->label.contains("-3") ) {
100  sym->op[i].axis[0] = sym->op[i].axis[1] =
101  sym->op[i].axis[2] = 1; // Rotate around 3-fold (1,1,1)
102  sym->op[i].order = 3;
103  sym->op[i].angle = M_PI*2.0L/3.0;
104  i++;
105  }
106  if ( sym->label.contains("-4") ) {
107  sym->op[i].axis[0] = 1; // Rotate around 2-fold
108  sym->op[i].axis[1] = -1;
109  sym->op[i].order = 2;
110  sym->op[i].angle = M_PI;
111  } else if ( sym->label.contains("-2") ) {
112  sym->op[i].axis[2] = 1; // Rotate around 4-fold z-axis
113  sym->op[i].order = 4;
114  sym->op[i].angle = M_PI_2;
115  } else {
116  sym->op[i].axis[2] = 1; // Rotate around 4-fold z-axis
117  sym->op[i].order = 4;
118  sym->op[i].angle = M_PI_2;
119  i++;
120  sym->op[i].axis[0] = 1; // Rotate around 2-fold x-axis
121  sym->op[i].order = 2;
122  sym->op[i].angle = M_PI;
123  }
124  } else if ( sym->label[0] == 'I' ) { // Icosahedral point group
125  sym->point = 532;
126  sym->n = 4;
127  if ( sym->label.contains("-") ) sym->n = 3;
128  sym->op = new Bsymop[sym->n]; memset(sym->op, 0, sym->n*sizeof(Bsymop));;
129  i = 0;
130  if ( !sym->label.contains("-3") ) {
131  sym->op[i].axis[0] = sym->op[i].axis[1] = sym->op[i].axis[2] = 1; // Rotate around 3-fold (1,1,1)
132  sym->op[i].order = 3;
133  sym->op[i].angle = M_PI*2.0L/3.0;
134  i++;
135  }
136  if ( sym->label.contains("-5") ) {
137  sym->op[i].axis[0] = 1; // Rotate around 2-fold
138  } else if ( sym->label.contains("-2") ) {
139  sym->op[i].axis[0] = 1; // Rotate around 2-fold
140  sym->op[i].axis[1] = -1;
141  sym->op[i].order = 2;
142  sym->op[i].angle = M_PI;
143  } else {
144  sym->op[i].axis[0] = 1; // Rotate around 2-fold
145  sym->op[i].axis[1] = 1.0L/GOLDEN;
146  sym->op[i].axis[2] = GOLDEN;
147  }
148  sym->op[i].order = 2;
149  sym->op[i].angle = M_PI;
150  i++;
151  if ( !sym->label.contains("-5") ) {
152  sym->op[i].axis[0] = 1.0L/GOLDEN; // Rotate around 5-fold
153  sym->op[i].axis[1] = 1;
154  sym->op[i].order = 5;
155  sym->op[i].angle = M_PI*2.0L/5.0;
156  i++;
157  }
158  if ( !sym->label.contains("-2") ) {
159  sym->op[i].axis[2] = 1; // Rotate around 2-fold z-axis
160  sym->op[i].order = 2;
161  sym->op[i].angle = M_PI;
162  }
163  if ( sym->label.contains("90") )
164  for ( i=0; i<sym->n; i++ )
165  sym->op[i].axis = mat * sym->op[i].axis;
166  } else if ( sym->label[0] == 'H' ) { // Helical symmetry
167  sscanf(sym->label.c_str(), "H%lf,%lf,%d", &helix_rise, &helix_angle, &helix_dyad);
168  sym->n = 1;
169  if ( helix_dyad == 2 ) sym->n = 2;
170  sym->op = new Bsymop[sym->n]; memset(sym->op, 0, sym->n*sizeof(Bsymop));;
171  sym->point = 600;
172  sym->op[0].axis[2] = 1; // Helical axis
173  sym->op[0].shift = helix_rise; // Rise in angstrom
174  sym->op[0].angle = helix_angle*M_PI/180.0L;
175  if ( helix_dyad == 2 ) {
176  sym->op[1].axis[0] = 1; // Dyad axis
177  sym->op[1].order = 2;
178  sym->op[1].angle = M_PI;
179  }
180  } else {
181  error_show("Error in init_point_group_symmetry", __FILE__, __LINE__);
182  cerr << "This is not a valid symmetry designation: " << sym->label << endl;
183  kill_symmetry(sym);
184  return NULL;
185  }
186 
187  for ( i=0; i<sym->n; i++ )
188  sym->op[i].axis.normalize();
189 
190  if ( verbose & VERB_DEBUG )
191  cout << "DEBUG init_point_group_symmetry: label=" << sym->label << endl;
192 
193  return sym;
194 }
195 
197 {
198  Bstring symmetry_string("C1");
199  return init_point_group_symmetry(symmetry_string);
200 }
201 
208 {
209  if ( !sym ) return 0;
210 
211  if ( sym->n > 0 && sym->op ) delete[] sym->op;
212 
213  delete sym;
214 
215  return 0;
216 }
217 
227 {
228  int j(0);
229 
230  Bstring clean_sym_str = "C1";
231 
232  if ( verbose & VERB_DEBUG )
233  cout << "DEBUG symmetry_clean_pointgroup: symmetry_string=" << symmetry_string << endl;
234 
235  if ( symmetry_string.length() < 1 ) return clean_sym_str;
236 
237  // Remove leading blanks and convert to upper case
238  clean_sym_str = symmetry_string.no_space();
239 
240  // Alternate nomenclature for point groups
241  if ( isdigit(clean_sym_str[0]) ) {
242  j = clean_sym_str.integer();
243  if ( clean_sym_str.contains("532") && clean_sym_str.contains("90") )
244  clean_sym_str = "I90";
245  else if ( clean_sym_str.contains("532") )
246  clean_sym_str = "I";
247  else if ( clean_sym_str.contains("432") )
248  clean_sym_str = "O";
249  else if ( clean_sym_str.contains("23") )
250  clean_sym_str = "T";
251  else if ( j > 1 && clean_sym_str[1] == 2 )
252  clean_sym_str = Bstring(j, "D%d");
253  else if ( j > 0 )
254  clean_sym_str = Bstring(j, "C%d");
255  else
256  clean_sym_str = "C1";
257  }
258 
259  clean_sym_str[0] = toupper(clean_sym_str[0]);
260 
261  if ( verbose & VERB_DEBUG )
262  cout << "DEBUG symmetry_clean_pointgroup: clean_sym_str=" << clean_sym_str << endl;
263 
264  return clean_sym_str;
265 }
266 
280 string symmetry_helical_label(double helix_rise, double helix_angle,
281  int dyad_axis, int cyclic, double seam_shift)
282 {
283  string label = "H" + to_string(helix_rise);
284  label += "," + to_string(helix_angle*180.0/M_PI);
285  label += "," + to_string(dyad_axis);
286  label += "," + to_string(cyclic);
287  label += "," + to_string(seam_shift);
288 
289  return label;
290 }
291 
302 {
303  int i, order(1);
304 
305  for ( i=0; i<sym->n; i++ ) order *= sym->op[i].order;
306 
307  return order;
308 }
309 
316 {
317  View ref;
318 
319  if ( sym->point < 200 ) {
320  ref[0] = 1;
321  ref[2] = 0;
322  } else if ( sym->point < 300 ) {
323  ref[0] = sin(M_PI/4);
324  ref[2] = cos(M_PI/4);
325  } else if ( sym->point == 320 ) {
326  ref[0] = sin(M_PI/4);
327  ref[2] = cos(M_PI/4);
328  } else if ( sym->point == 432 ) {
329  ref[0] = sin(M_PI/8);
330  ref[2] = cos(M_PI/8);
331  } else if ( sym->point == 532 ) {
332  ref[0] = sin(M_PI/18);
333  ref[2] = cos(M_PI/18);
334  } else if ( sym->point == 600 ) { // What is it for helical symmetry?
335  }
336 
337  return ref;
338 }
339 
347 Matrix3 symmetry_rotate_to_axis(Bsymmetry* sym, long axis, long axis_flag)
348 {
349  double sqrt2(sqrt(2));
350  Matrix3 mat(1), mat2(1);
351 
352  if ( axis_flag ) mat2 = Matrix3(Vector3<double>(0,0,1), M_PI_2);
353 
354  if ( sym->point < 200 ) { // Cyclic
355  } else if ( sym->point < 300 ) { // Dihedral
356  if ( axis_flag ) mat2 = Matrix3(Vector3<double>(0,0,1), M_PI/sym->op[0].order);
357  if ( axis > 2 ) mat = Matrix3(Vector3<double>(0,1,0), M_PI_2);
358  } else if ( sym->point < 400 ) { // Tetrahedral
359  if ( axis == 3 ) mat = Matrix3(Vector3<double>(-1/sqrt2,1/sqrt2,0), atan(sqrt2));
360  } else if ( sym->point < 500 ) { // Octahedral
361  if ( axis == 3 ) mat = Matrix3(Vector3<double>(-1/sqrt2,1/sqrt2,0), atan(sqrt2));
362  if ( axis == 2 ) mat = Matrix3(Vector3<double>(0,1,0), M_PI/4);
363  } else { // Icosahedral
364  if ( axis == 3 ) mat = Matrix3(Vector3<double>(0,1,0), atan(1/(GOLDEN*sqrt(3))));
365  if ( axis == 5 ) mat = Matrix3(Vector3<double>(1,0,0), atan(1/GOLDEN));
366  }
367 
368  mat = mat2*mat;
369 
370  return mat;
371 }
372 
378 vector<Vector3<double>> symmetry_get_axes(Bsymmetry* sym)
379 {
380  long i, j, k, m, ns;
381  Vector3<double> axis1;
382  Matrix3 mat;
383 
384  if ( verbose & VERB_DEBUG )
385  cout << "DEBUG symmetry_get_axes: label=" << sym->label << endl;
386 
387  if ( sym->point < 300 ) { // Cyclic and dihedral
388  axis1[2] = 1;
389  } else if ( sym->point < 400 ) { // Tetrahedral
390  axis1[0] = axis1[1] = axis1[2] = 1/sqrt(3.0);
391  } else if ( sym->point < 500 ) { // Octahedral
392  if ( sym->label.contains("-4") ) {
393  axis1[2] = 1;
394  } else if ( sym->label.contains("-3") ) {
395  axis1[0] = axis1[1] = axis1[2] = 1/sqrt(3.0);
396  } else {
397  cerr << "Error: The symmetry designation must be either O-3 or O-4!" << endl;
398  bexit(-1);
399  }
400  } else if ( sym->point < 600 ) { // Icosahedral
401  if ( sym->label.contains("-5") ) {
402  axis1[1] = 1/sqrt(2+GOLDEN);
403  axis1[2] = 1/sqrt(3-GOLDEN);
404  } else if ( sym->label.contains("-3") ) {
405  axis1[0] = axis1[1] = axis1[2] = 1/sqrt(3.0);
406  } else {
407  cerr << "Error: The symmetry designation must be either I-3 or I-5!" << endl;
408  bexit(-1);
409  }
410  }
411 
412  vector<Vector3<double>> axis;
413 
414  axis.push_back(axis1);
415 
416  for ( i=0, k=1, ns=1; i<sym->n; i++ ) {
417  for ( j=1; j<sym->op[i].order; j++ ) {
418  mat = Matrix3(sym->op[i].axis, j*TWOPI/sym->op[i].order);
419  for ( m=0; m<ns; m++, k++ ) axis.push_back(mat * axis[m]);
420  }
421  ns = k;
422  }
423 
424  if ( verbose & VERB_FULL )
425  for ( i=0; i<axis.size(); i++ )
426  cout << "Axis[" << i+1 << "]: " << axis[i] << endl;
427 
428  return axis;
429 }
430 
443 {
444  int i, j, k;
445  double angle;
446  Quaternion q, qv;
447 
448  View* view = NULL;
449  View* v;
450  View* vn = (View *) add_item((char **) &view, sizeof(View));
451  *vn = asu_view;
452  vn->normalize();
453  vn->next = NULL;
454 
455  if ( verbose & VERB_FULL ) {
456  cout << "Getting all the symmetric views:" << endl;
457  cout << "Symmetry: " << sym->label << endl;
458  cout << "View: " << asu_view << endl;
459  }
460 
461  int nview(1);
462  for ( i=0; i<sym->n; i++ ) {
463  for ( j=1; j<sym->op[i].order; j++ ) {
464  angle = j*TWOPI*1.0L/sym->op[i].order;
465  q = Quaternion(sym->op[i].axis, angle);
466  for ( k=0, v=view; k<nview; k++, v=v->next ) {
467  qv = v->quaternion();
468  qv = q * qv;
469 // vn = (View *) add_item((char **) &view, sizeof(View));
470  vn = (View *) add_item((char **) &vn, sizeof(View));
471  *vn = View(qv);
472  if ( verbose & VERB_DEBUG )
473  cout << "DEBUG symmetry_get_all_views: " << vn << endl;
474  }
475  }
476  nview *= sym->op[i].order;
477  }
478 
479  if ( verbose & VERB_FULL )
480  for ( i=1, v=view; v; v=v->next, i++ )
481  cout << "View " << i << ": " << tab << *v << endl;
482 
483  return view;
484 }
485 
487 {
488  View* v;
489  View* vt;
490  View* vc = NULL;
491  View* vn = NULL;
492 
493  for ( v=views; v; v=v->next ) {
494  vt = symmetry_get_all_views(sym, *v);
495  if ( vc ) vc->next = vt;
496  else vn = vc = vt;
497  while ( vc->next ) vc = vc->next;
498  }
499 
500  return vn;
501 }
502 
515 {
516  int i, j, k, n, ns;
517  double angle;
518  Matrix3 mt;
519 
520  for ( i=0, ns=1; i<sym->n; i++ ) ns *= sym->op[i].order;
521  Matrix3* mat = new Matrix3[ns];
522  for ( i=0; i<ns; i++ ) mat[i] = Matrix3(1);
523 
524  if ( verbose & VERB_FULL ) {
525  cout << "Getting all the symmetric matrices:" << endl;
526  cout << "Symmetry: " << sym->label << endl;
527  }
528 
529  for ( i=0, n=ns=1; i<sym->n; i++ ) {
530  for ( j=1; j<sym->op[i].order; j++ ) {
531  angle = j*TWOPI*1.0L/sym->op[i].order;
532  mt = Matrix3(sym->op[i].axis, angle);
533  for ( k=0; k<ns; k++ )
534  mat[n++] = mt*mat[k];
535  }
536  ns *= sym->op[i].order;
537  }
538 
539  if ( verbose & VERB_FULL ) {
540  for ( i=0; i<ns; i++ ) {
541  cout << "Matrix " << i+1 << ":" << endl;
542  matrix3_show_hp(mat[i]);
543  }
544  }
545 
546  nsym = ns;
547 
548  return mat;
549 }
550 
564 View* asymmetric_unit_views(Bsymmetry* sym, double theta_step, double phi_step, int flag)
565 {
566  if ( theta_step < M_PI/1800 ) theta_step = M_PI/180;
567  if ( phi_step < M_PI/1800 ) phi_step = M_PI/180;
568 
569  int temp_sym = 0;
570  if ( !sym ) {
571  temp_sym = 1;
573  }
574 
575  char full = flag & 1;
576  char inplanerot = !(flag & 2);
577  int i, j, n = 0, ntheta, nphi, nrphi, istart;
578  double theta, phi, max_theta, theta_start, phi_start, phi_end;
579 
580  if ( verbose & VERB_DEBUG )
581  cout << "DEBUG asymmetric_unit_views: flag=" << flag << " full=" << full << endl;
582 
583  max_theta = M_PI_2 + 1e-6; // Most symmetries limited to upper half
584  ntheta = (int) (max_theta/theta_step + 0.5);
585  nphi = (int) (M_PI*2.0/(sym->op[0].order*phi_step) + 0.5);
586 
587  if ( sym->point < 200 ) {
588  if ( full ) {
589  max_theta = M_PI + 1e-6;
590  ntheta += ntheta;
591  }
592  } else if ( sym->point == 432 ) {
593  max_theta = M_PI_4 + 1e-6;
594  ntheta = (int) (max_theta/theta_step + 0.5);
595  } else if ( sym->point == 532 ) {
596  max_theta = atan(1/(GOLDEN+1)) + 1e-6;
597  ntheta = (int) (max_theta/theta_step + 2.5);
598  nphi = (int) (atan(1/GOLDEN)/phi_step + 1.5);
599  } else if ( sym->point == 600 ) {
600  ntheta = 1;
601  nphi = (int) (M_PI*2.0/phi_step + 1);
602  }
603 
604  if ( ntheta < 1 ) ntheta = 1;
605  if ( nphi < 1 ) nphi = 1;
606 
607  if ( verbose & (VERB_PROCESS | VERB_LABEL) ) {
608  if ( full )
609  cout << "Getting all the asymmetric unit views for symmetry " << sym->label << endl;
610  else
611  cout << "Getting half the asymmetric unit views for symmetry " << sym->label << endl;
612  cout << "Theta and phi step sizes: " <<
613  theta_step*180/M_PI << " " << phi_step*180/M_PI << " degrees" << endl;
614  cout << "Theta and phi steps: " << ntheta << " " << nphi << endl;
615  }
616 
617  if ( verbose & VERB_DEBUG )
618  cout << "DEBUG asymmetric_unit_views: Views allocated = " << 2*ntheta*nphi << endl;
619 
620  View* view = NULL;
621  View* v = NULL;
622 
623  if ( sym->point < 500 ) { // Top view for all but icosahedral and helical symmetry
624  v = (View *) add_item((char **) &view, sizeof(View));
625  (*v)[2] = 1;
626  n = 1;
627  if ( verbose & VERB_DEBUG )
628  cout << "DEBUG asymmetric_unit_views: Adding the top view" << endl;
629  }
630 
631  if ( sym->point > 100 && sym->point < 200 ) {
632  for ( theta=theta_step; theta<=max_theta; theta+=theta_step ) {
633  nrphi = (int) (nphi*sin(theta)/2 + 0.5); // Number of views at this radius and theta
634  phi = istart = 0;
635  if ( nrphi ) {
636  phi = M_PI*1.0/(sym->op[0].order*nrphi);
637  istart = -nrphi;
638  if ( sym->point < 102 ) nrphi -= 1;
639  }
640  for ( j=istart; j<=nrphi; j++ ) {
641  v = (View *) add_item((char **) &view, sizeof(View));
642  *v = View(sin(theta)*cos(j*phi), sin(theta)*sin(j*phi), cos(theta), 0);
643  if ( inplanerot ) (*v)[3] = j*phi;
644  n++;
645  }
646  }
647  if ( full && v->z() > -0.999999 ) {
648  v = (View *) add_item((char **) &view, sizeof(View));
649  (*v)[2] = -1;
650  n++;
651  }
652  } else if ( sym->point > 200 && sym->point < 300 ) {
653  for ( theta=theta_step; theta<=max_theta; theta+=theta_step ) {
654  nrphi = (int) (nphi*sin(theta)/2 + 0.5); // Number of views at this radius and theta
655  phi = 0;
656  if ( nrphi ) phi = M_PI*1.0/(sym->op[0].order*nrphi);
657  istart = 0;
658  if ( full ) istart = -nrphi;
659  for ( j=istart; j<=nrphi; j++ ) {
660  v = (View *) add_item((char **) &view, sizeof(View));
661  *v = View(sin(theta)*cos(j*phi), sin(theta)*sin(j*phi), cos(theta), 0);
662  if ( inplanerot ) (*v)[3] = j*phi;
663  n++;
664  }
665  }
666  } else if ( sym->point == 320 ) {
667  for ( theta=theta_step; theta<=max_theta; theta+=theta_step ) {
668  phi_start = 0;
669  phi_end = theta;
670  if ( theta > M_PI_4 ) phi_end = M_PI_2 - theta;
671  if ( full ) phi_start = -theta;
672  if ( full && theta > M_PI_4 ) phi_start = theta - M_PI_2;
673  for ( phi=phi_start; phi<=phi_end; phi+=phi_step ) {
674  v = (View *) add_item((char **) &view, sizeof(View));
675  *v = View(sin(theta), sin(phi), cos(theta), 0);
676  n++;
677  }
678  }
679  } else if ( sym->point == 432 ) {
680  for ( theta=theta_step; theta<=max_theta; theta+=theta_step ) {
681  phi_start = 0;
682  if ( full ) phi_start = -theta;
683  for ( phi=phi_start; phi<=theta; phi+=phi_step ) {
684  v = (View *) add_item((char **) &view, sizeof(View));
685  *v = View(sin(theta), sin(phi), cos(theta), 0);
686  n++;
687  }
688  }
689  } else if ( sym->point == 532 ) {
690  if ( sym->label.contains("I90") ) {
691  theta_start = 0;
692  if ( full ) theta_start = -ntheta*theta_step;
693  for ( theta=theta_start; theta<=max_theta; theta+=theta_step ) {
694  for ( phi=0; phi<=GOLDEN*(max_theta - fabs(theta)); phi+=phi_step ) {
695  v = (View *) add_item((char **) &view, sizeof(View));
696  *v = View(tan(phi), tan(theta), cos(theta), 0);
697  n++;
698  }
699  }
700  } else {
701  for ( theta=0; theta<=max_theta; theta+=theta_step ) {
702  nrphi = (int) (GOLDEN*(max_theta - theta)/phi_step);
703  phi_start = 0;
704  if ( full ) phi_start = -nrphi*phi_step;
705  for ( phi=phi_start; phi<=GOLDEN*(max_theta - theta); phi+=phi_step ) {
706  v = (View *) add_item((char **) &view, sizeof(View));
707  *v = View(tan(theta), tan(phi), cos(theta), 0);
708  n++;
709  }
710  }
711  }
712  } else if ( sym->point == 600 ) { // Helical symmetry
713  for ( phi = 0; phi < TWOPI-0.001; phi += phi_step ) {
714  v = (View *) add_item((char **) &view, sizeof(View));
715  *v = View(cos(phi), sin(phi), 0, 0);
716  if ( inplanerot ) (*v)[3] = phi;
717  n++;
718  }
719  } else {
720  cerr << "Warning: Symmetry type " << sym->point << " not supported!" << endl;
721  }
722 
723  if ( verbose & ( VERB_PROCESS | VERB_LABEL ) )
724  cout << "Views generated: " << n << endl << endl;
725 
726  for ( v=view; v; v = v->next ) v->normalize();
727 
728  if ( verbose & VERB_FULL ) {
729  cout << "View\tx\ty\tz\ta" << endl;
730  for ( v=view, i=1; v; v = v->next, i++ )
731  cout << i << tab << *v << endl;
732  cout << endl;
733  }
734 
735  if ( temp_sym ) kill_symmetry(sym);
736 
737  return view;
738 }
739 
740 View* asymmetric_unit_views(Bsymmetry* sym, double theta_step, double phi_step, double alpha_step, int flag)
741 {
742  View* view = asymmetric_unit_views(sym, theta_step, phi_step, flag);
743 
744  View* view2 = view_list_expand_angles(view, -M_PI, M_PI - alpha_step/2, alpha_step);
745 
746  kill_list((char *) view, sizeof(View));
747 
748  return view2;
749 }
750 
763 View* side_views(Bsymmetry* sym, double side_ang, double theta_step, double phi_step)
764 {
765  if ( side_ang < 0 ) side_ang = -side_ang;
766  if ( theta_step <= 0 ) {
767  side_ang = 0;
768  theta_step = M_PI/180;
769  } else {
770  side_ang = theta_step*floor(side_ang/theta_step);
771  }
772  if ( phi_step <= 0 ) phi_step = M_PI_2;
773 
774  int order = sym->op[0].order;
775  if ( order < 1 ) order = 1;
776 
777  double phi, theta;
778  int n;
779  Euler euler;
780  View* view = NULL;
781  View* v = NULL;
782 
783  if ( verbose & VERB_PROCESS ) {
784  cout << "Getting all the asymmetric unit side views for symmetry " << sym->label << endl;
785  cout << "Step size around equator: " << phi_step*180.0/M_PI << " degrees" << endl;
786  if ( side_ang ) {
787  cout << "Deviation from equator: " << side_ang*180.0/M_PI << " degrees" << endl;
788  cout << "Step size from equator: " << theta_step*180.0/M_PI << " degrees" << endl;
789  }
790  }
791 
792  for ( n = 0, phi = -M_PI*1.0L/order; phi < M_PI*1.0L/order; phi += phi_step ) {
793  for ( theta = M_PI_2 - side_ang; theta <= M_PI_2 + side_ang; theta += theta_step ) {
794  v = (View *) add_item((char **) &view, sizeof(View));
795  euler = Euler(0, theta, phi);
796  *v = euler.view();
797  n++;
798 // cout << "theta=" << theta*180.0/M_PI << " phi=" << phi*180.0/M_PI << endl;
799  }
800  }
801 
802  if ( verbose & VERB_PROCESS )
803  cout << "Number of views: " << n << endl << endl;
804 
805  return view;
806 }
807 
808 View* side_views(Bsymmetry* sym, double side_ang, double theta_step, double phi_step, double alpha_step)
809 {
810  View* view = side_views(sym, side_ang, theta_step, phi_step);
811 
812  View* view2 = view_list_expand_angles(view, -M_PI, M_PI - alpha_step/2, alpha_step);
813 
814  kill_list((char *) view, sizeof(View));
815 
816  return view2;
817 }
818 
829 {
830  if ( sym->point < 102 ) return 0;
831 
832  View* v;
833 
834  for ( v=view; v; v=v->next )
835  *v = find_asymmetric_unit_view(sym, *v);
836 
837  return 0;
838 }
839 
851 {
852  if ( sym->point < 102 ) return theview;
853  if ( sym->point >= 600 ) return theview;
854 
855  View* v, bv(theview), tv;
856  View* view = symmetry_get_all_views(sym, theview);
857 
858  int found(0);
859  double tol(1e-10);
860  double lim = tan(M_PI*1.0L/sym->op[0].order);
861 
862  for ( v=view; v && !found; v=v->next )
863  if ( ( v->x() - tol >= 0 ) || ( v->x() + tol >= 0 && v->y() >= 0 ) ) {
864  tv.x(0); tv.y(0); tv.z(0);
865  if ( sym->point < 200 ) {
866  if ( sym->op[0].order == 2 ) {
867  tv = *v;
868  } else {
869  if ( fabs(v->y()) <= v->x()*lim + tol )
870  tv = *v;
871  }
872  } else if ( sym->point > 200 && sym->point < 300 ) {
873  if ( v->z() + tol >= 0 ) {
874  if ( sym->op[0].order == 2 ) {
875  tv = *v;
876  } else {
877  if ( fabs(v->y()) <= v->x()*lim + tol ) tv = *v;
878  }
879  }
880  } else if ( sym->point == 320 ) {
881  if ( fabs(v->y()) <= v->x() + tol && fabs(v->y()) <= v->z() + tol )
882  tv = *v;
883  } else if ( sym->point == 432 ) {
884  if ( fabs(v->y()) <= v->x() + tol && v->x() <= v->z() + tol )
885  tv = *v;
886  } else if ( sym->point == 532 ) {
887  if ( sym->label.contains("I90") ) {
888  if ( v->z() + tol >= 0 && fabs(v->y()) <= (v->z()/GOLDEN - v->x())/GOLDEN + tol )
889  tv = *v;
890  } else {
891  if ( v->z() + tol >= 0 && fabs(v->y()) <= v->z()/GOLDEN - v->x()*GOLDEN + tol )
892  tv = *v;
893  }
894  } else if ( sym->point >= 600 ) { // What is it for helical symmetry?
895  tv = theview;
896  }
897  if ( tv.vector_size() > 0.9 ) {
898  bv = tv;
899  found++;
900  }
901  }
902 
903  if ( !found ) {
904  cerr << "ASU view not found: " << theview << endl;
905 // for ( v=view; v; v=v->next )
906 // cerr << *v << endl;
907  } else {
908  if ( verbose & VERB_STATS ) {
909  cout << "Old view:\t" << theview << endl;
910  cout << "New view:\t" << bv << endl;
911  }
912  }
913 
914  kill_list((char *) view, sizeof(View));
915 
916  bv.next = theview.next;
917 
918  return bv;
919 }
920 
934 {
935  if ( sym->point < 102 ) return view;
936  if ( sym->point >= 600 ) return view;
937 
938  if ( verbose & VERB_DEBUG )
939  cout << "DEBUG find_closest_symmetric_view: " << sym->label << endl;
940 
941  double r, rb(1e30);
942  View* v, bv(view);
943  View* viewsym = symmetry_get_all_views(sym, view);
944 
945  for ( v=viewsym; v; v=v->next ) {
946 // r = v->residual(view_ref);
947  r = v->angle(view_ref);
948  if ( rb > r ) {
949  rb = r;
950  bv = *v;
951  }
952  }
953 
954  kill_list((char *) viewsym, sizeof(View));
955 
956  bv.next = view.next;
957 
958  return bv;
959 }
960 
967 {
968  View ref;
969 
970  if ( sym->point < 200 ) {
971  ref[0] = 1;
972  ref[2] = 0;
973  } else if ( sym->point < 300 ) {
974  ref[0] = ref[2] = 1;
975  } else if ( sym->point == 320 ) {
976  ref[0] = ref[1] = cos(M_PI/8.0);
977  ref[2] = sin(M_PI/8.0);
978  } else if ( sym->point == 432 ) {
979  ref[0] = cos(M_PI/3.0);
980  ref[2] = sin(M_PI/3.0);
981  } else if ( sym->point == 532 ) {
982  if ( sym->label.contains("I90") ) {
983  ref[1] = 1;
984  ref[2] = GOLDEN + 1;
985  } else {
986  ref[0] = 1;
987  ref[2] = GOLDEN + 1;
988  }
989  } else if ( sym->point == 600 ) { // What is it for helical symmetry?
990  }
991 
992  ref.normalize();
993 
994  View* rv = symmetry_get_all_views(sym, ref);
995 
996  return rv;
997 }
998 
1006 {
1007  View* v;
1008  View* view = symmetry_get_all_views(sym, asu_view);
1009 
1010  long n = symmetry_order(sym);
1011 
1012  long j, i(n*random()*0.99999L/get_rand_max());
1013 
1014  for ( j=0, v=view; v && j<i; v=v->next, j++ ) ;
1015 
1016  View vr(*v);
1017 
1018  kill_list((char *) view, sizeof(View));
1019 
1020  return vr;
1021 }
1022 
1030 {
1031  int i, n = 0;
1032  double a, amin = TWOPI;
1033 
1035  View* v;
1036 
1037  for ( i=0, v=rv; v; v=v->next, i++ ) {
1038  a = theview.angle(*v);
1039  if ( amin > a ) {
1040  amin = a;
1041  n = i;
1042  }
1043  }
1044 
1045  kill_list((char *) rv, sizeof(View));
1046 
1047  return n;
1048 }
1049 
1063 float* sym_matrices_from_text_list(int nsym, char* symop, int line_len)
1064 {
1065  // Set up the symmetry operator matrix
1066  int i, j, k, l;
1067  float* mat = new float[nsym*12];
1068  for ( i=0; i<nsym*12; i++ ) mat[i] = 0;
1069 
1070  char op[200];
1071  for ( i=0; i<nsym; i++ ) {
1072  if ( verbose & VERB_DEBUG )
1073  cout << "DEBUG sym_matrices_from_text_list: Symmetry operator " << i+1 << ":" << endl;
1074  k = 0;
1075  for ( j=0; j<3; j++ ) {
1076  l = 0;
1077  memset(op, 0, line_len);
1078  while ( k<line_len && symop[i*line_len+k] != ',' ) {
1079  op[l] = tolower(symop[i*line_len+k]);
1080  k++;
1081  l++;
1082  }
1083  op[l] = 0;
1084  if ( strstr(op,"-x") ) mat[i*12+4*j] = -1;
1085  else if ( strstr(op,"x") ) mat[i*12+4*j] = 1;
1086  if ( strstr(op,"-y") ) mat[i*12+4*j+1] = -1;
1087  else if ( strstr(op,"y") ) mat[i*12+4*j+1] = 1;
1088  if ( strstr(op,"-z") ) mat[i*12+4*j+2] = -1;
1089  else if ( strstr(op,"z") ) mat[i*12+4*j+2] = 1;
1090  if ( strstr(op,"1/2") ) mat[i*12+4*j+3] = 0.5;
1091  if ( strstr(op,"1/4") ) mat[i*12+4*j+3] = 0.25;
1092  if ( strstr(op,"3/4") ) mat[i*12+4*j+3] = 0.75;
1093  if ( strstr(op,"1/3") ) mat[i*12+4*j+3] = 1.0/3.0;
1094  if ( strstr(op,"2/3") ) mat[i*12+4*j+3] = 2.0/3.0;
1095  if ( strstr(op,"1/6") ) mat[i*12+4*j+3] = 1.0/6.0;
1096  if ( strstr(op,"5/6") ) mat[i*12+4*j+3] = 5.0/6.0;
1097  k++;
1098  if ( verbose & VERB_DEBUG )
1099  cout << "|" << mat[i*12+4*j] << " " << mat[i*12+4*j+1]
1100  << " " << mat[i*12+4*j+2] << "| |" << mat[i*12+4*j+3] << "|" << endl;
1101  }
1102  }
1103 
1104  return 0;
1105 }
1106 
1113 {
1114  int i, nview = 1;
1115  for ( i=0; i<sym->n; i++ ) nview *= sym->op[i].order;
1116 
1117  View ref;
1118  View* view = symmetry_get_all_views(sym, ref);
1119  View* v;
1120  Matrix3 mat;
1121 
1122  cout << "\nSymmetry matrices (" << nview << "):" << endl;
1123  for ( i=1, v=view; v; i++, v=v->next ) {
1124  mat = v->matrix();
1125  cout << "Matrix " << i << ":" << endl;
1126  matrix3_show_hp(mat);
1127  }
1128  cout << endl;
1129 
1130  kill_list((char *) view, sizeof(View));
1131 
1132  return nview;
1133 }
1134 
1141 {
1142  int i;
1143  Matrix3 mat;
1144 
1145  for ( i=0; i<sym->n; i++ ) {
1146  mat = Matrix3(sym->op[i].axis, M_PI*2.0/sym->op[i].order);
1147  cout << "Operation " << i << ":" << endl;
1148  cout << "Axis and angle: " << sym->op[i].axis << " " << 360.0/sym->op[i].order << endl;
1149  cout << mat << endl;
1150  }
1151  cout << endl;
1152 
1153  return sym->n;
1154 }
1155 
1162 {
1163  long nsym;
1164  int i, j, k;
1165  double t(0);
1166 
1167  Matrix3* mat = symmetry_get_all_matrices(sym, nsym);
1168 
1169  for ( i=0; i<nsym; i++ ) {
1170  for ( j=0; j<3; j++ ) {
1171  cout << "BIOMT" << j+1 << setw(4) << i+1 << setprecision(6);
1172  for ( k=0; k<3; k++ )
1173  cout << setw(10) << mat[i][j][k];
1174  cout << setprecision(6) << setw(15) << t << endl;
1175  }
1176  }
1177 
1178  delete[] mat;
1179 
1180  return sym->n;
1181 }
1182 
1183 
View::next
View * next
Definition: View.h:28
Bsymmetry::n
int n
Definition: symmetry.h:42
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
reference_asymmetric_unit_views
View * reference_asymmetric_unit_views(Bsymmetry *sym)
Returns a reference view for each asymmetric unit.
Definition: symmetry.cpp:966
Bsymmetry::point
int point
Definition: symmetry.h:39
Quaternion
Definition: Quaternion.h:25
M_PI_4
#define M_PI_4
Definition: utilities.h:69
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Bstring
Definition: Bstring.h:29
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
symmetry_get_all_matrices
Matrix3 * symmetry_get_all_matrices(Bsymmetry *sym, long &nsym)
Get all symmetry-related views of one given view.
Definition: symmetry.cpp:514
random_symmetric_view
View random_symmetric_view(View &asu_view, Bsymmetry *sym)
Returns a randomly picked symmetry view.
Definition: symmetry.cpp:1005
sym_show_matrices
int sym_show_matrices(Bsymmetry *sym)
Show symmetry matrices.
Definition: symmetry.cpp:1112
View::vector_size
double vector_size()
Definition: View.h:106
Bstring::integer
long integer()
Definition: Bstring.cpp:547
symmetry.h
Header file for general symmetry functions.
Vector3< double >
random_numbers.h
Header file for functions for creating random images.
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
view_symmetry_reference
View view_symmetry_reference(Bsymmetry *sym)
Returns an asymmetric unit reference point.
Definition: symmetry.cpp:315
GOLDEN
#define GOLDEN
Definition: utilities.h:85
View::y
double y()
Definition: View.h:96
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
test_asymmetric_unit_view
int test_asymmetric_unit_view(View theview, Bsymmetry *sym)
Returns an asymmetric unit index number.
Definition: symmetry.cpp:1029
View
Definition: View.h:26
verbose
int verbose
side_views
View * side_views(Bsymmetry *sym, double side_ang, double theta_step, double phi_step)
Initializes a set of views around the z-axis for helical projection.
Definition: symmetry.cpp:763
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bsymmetry
Definition: symmetry.h:38
M_PI_2
#define M_PI_2
Definition: utilities.h:66
View::z
double z()
Definition: View.h:97
Bsymop::axis
Vector3< double > axis
Definition: symmetry.h:25
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
change_views_to_asymmetric_unit
int change_views_to_asymmetric_unit(Bsymmetry *sym, View *view)
Change the views to those in the asymmetric unit.
Definition: symmetry.cpp:828
Matrix3
Definition: Matrix3.h:18
Vector3::normalize
double normalize()
Definition: Vector3.h:238
view_list_expand_angles
View * view_list_expand_angles(View *views, double amin, double amax, double astep)
Expands each view to several views with different rotation angles.
Definition: View.cpp:435
symmetry_get_axes
vector< Vector3< double > > symmetry_get_axes(Bsymmetry *sym)
Get all symmetry axes.
Definition: symmetry.cpp:378
symmetry_rotate_to_axis
Matrix3 symmetry_rotate_to_axis(Bsymmetry *sym, long axis, long axis_flag)
Rotation matrix to orient a symmetry axis on the z axis.
Definition: symmetry.cpp:347
Bsymop::order
int order
Definition: symmetry.h:26
asymmetric_unit_views
View * asymmetric_unit_views(Bsymmetry *sym, double theta_step, double phi_step, int flag)
Initializes a well-distributed set of views in an asymmetric unit.
Definition: symmetry.cpp:564
View::normalize
double normalize()
Definition: View.h:109
symmetry_get_all_views
View * symmetry_get_all_views(Bsymmetry *sym, View asu_view)
Get all symmetry-related views of one given view.
Definition: symmetry.cpp:442
find_closest_symmetric_view
View find_closest_symmetric_view(Bsymmetry *sym, View view_ref, View view)
Finds the closest symmetric match between two views.
Definition: symmetry.cpp:933
symmetry_order
int symmetry_order(Bsymmetry *sym)
Returns the symmetry order.
Definition: symmetry.cpp:301
Euler::view
View view()
Definition: Euler.h:62
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
init_point_group_symmetry
Bsymmetry * init_point_group_symmetry(Bstring symmetry_string)
Sets up a symmetry structure from a string identifier.
Definition: symmetry.cpp:39
Bstring::substr
Bstring substr(long start, long len)
Definition: Bstring.cpp:332
Bsymop::shift
double shift
Definition: symmetry.h:28
Bstring::length
long length() const
Definition: Bstring.cpp:263
sym_show_pdb_matrices
int sym_show_pdb_matrices(Bsymmetry *sym)
Show PDB format symmetry matrices associated with each symmetry operator.
Definition: symmetry.cpp:1161
find_asymmetric_unit_view
View find_asymmetric_unit_view(Bsymmetry *sym, View theview)
Finds the corresponding view in the asymmetric unit.
Definition: symmetry.cpp:850
sym_show_operational_matrices
int sym_show_operational_matrices(Bsymmetry *sym)
Show symmetry matrices associated with each symmetry operator.
Definition: symmetry.cpp:1140
symmetry_helical_label
string symmetry_helical_label(double helix_rise, double helix_angle, int dyad_axis, int cyclic, double seam_shift)
Returns the label for helical symmetry.
Definition: symmetry.cpp:280
linked_list.h
Header file for generalized linked list functions.
symmetry_clean_pointgroup
Bstring symmetry_clean_pointgroup(Bstring &symmetry_string)
Corrects an existing point group string.
Definition: symmetry.cpp:226
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
VERB_STATS
#define VERB_STATS
Definition: utilities.h:112
Bsymmetry::op
Bsymop * op
Definition: symmetry.h:43
get_rand_max
long get_rand_max()
Finds the maximum random number for a system.
Definition: random_numbers.cpp:22
Bsymmetry::label
Bstring label
Definition: symmetry.h:41
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
Euler.h
Euler object.
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bsymop
Definition: symmetry.h:24
View::angle
double angle()
Definition: View.h:98
Euler
Definition: Euler.h:28
Bstring::no_space
Bstring no_space()
Definition: Bstring.cpp:340
Bsymop::angle
double angle
Definition: symmetry.h:27
View::x
double x()
Definition: View.h:95
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
sym_matrices_from_text_list
float * sym_matrices_from_text_list(int nsym, char *symop, int line_len)
Calculates symmetry matrices from a list of strings.
Definition: symmetry.cpp:1063
TWOPI
#define TWOPI
Definition: utilities.h:72
matrix3_show_hp
int matrix3_show_hp(Matrix3 &mat)
Print out a matrix.
Definition: Matrix3.cpp:31