Bsoft  2.1.0
Bernard's software package
mg_xtal.cpp
Go to the documentation of this file.
1 
9 #include "mg_processing.h"
10 #include "mg_xtal.h"
11 #include "matrix_linear.h"
12 #include "Matrix.h"
13 #include "linked_list.h"
14 #include "Complex.h"
15 #include "utilities.h"
16 #include "timer.h"
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
33 {
34  long i;
35  vector<double> bx(2,0), by(2,0);
36  Matrix a(2,2);
37  Bstrucfac* sf;
38 
39  for ( i = 0, sf = mg->sf; sf; sf = sf->next, i++ ) {
40  a[0][0] += sf->index[0]*sf->index[0];
41  a[0][1] += sf->index[0]*sf->index[1];
42  a[1][1] += sf->index[1]*sf->index[1];
43  bx[0] += sf->index[0]*sf->loc[0];
44  bx[1] += sf->index[1]*sf->loc[0];
45  by[0] += sf->index[0]*sf->loc[1];
46  by[1] += sf->index[1]*sf->loc[1];
47  }
48  a[1][0] = a[0][1];
49 
50  if ( i < 2 ) {
51  cerr << "Error: Too few structure factors to calculate unit cell vectors!" << endl;
52  return -1;
53  }
54 
55  a.LU_decomposition();
56  a.multiply_in_place(bx);
57  a.multiply_in_place(by);
58 
59  mg->hvec[0] = bx[0];
60  mg->hvec[1] = by[0];
61  mg->kvec[0] = bx[1];
62  mg->kvec[1] = by[1];
63 
64  return 0;
65 }
66 
80 long mg_generate_reflections(Bmicrograph* mg, Vector3<double> real_size, double resolution)
81 {
82  if ( resolution < 2*mg->pixel_size[0] )
83  resolution = 2*mg->pixel_size[0];
84 
85  double hkl_lim = real_size[0]/resolution;
86  if ( real_size[1] > real_size[0] ) hkl_lim = real_size[1]/resolution;
87  double hkl_lim2 = hkl_lim*hkl_lim + 0.001;
88 
89  if ( verbose )
90  cout << "Generating reflections to " << resolution << " Å" << endl;
91 
92  long n(0), h, k, l, hmin, kmin, lmin, hmax, kmax, lmax;
93  double h2, k2, l2, hlen, klen;
94  Vector3<double> loc;
95  Bstrucfac* sf = NULL;
96 
97  hlen = mg->hvec.length();
98  hmax = (long) (hkl_lim/hlen);
99  hmin = -hmax;
100 
101  klen = mg->kvec.length();
102  kmax = (long) (hkl_lim/klen);
103  kmin = -kmax;
104 
105  lmin = lmax = 0;
106 
107  kill_list((char *) mg->sf, sizeof(Bstrucfac));
108  mg->sf = NULL;
109 
110  if ( verbose ) {
111  cout << "real_size=" << real_size << endl;
112  cout << "hvec=" << mg->hvec << endl;
113  cout << "kvec=" << mg->kvec << endl;
114  cout << "hmax=" << hmax << " kmax=" << kmax << endl;
115  cout << "mg->origin=" << mg->origin << endl;
116  }
117 
118  for ( l=lmin; l<=lmax; l++ ) {
119  l2 = l*l;
120  for ( k=kmin; k<=kmax; k++ ) {
121  k2 = k*klen;
122  k2 *= k2;
123  for ( h=hmin; h<=hmax; h++ ) {
124  h2 = h*hlen;
125  h2 *= h2;
126  if ( h2 + k2 + l2 <= hkl_lim2 ) {
127  loc[0] = h*mg->hvec[0] + k*mg->kvec[0] + l*mg->lvec[0];
128  loc[1] = h*mg->hvec[1] + k*mg->kvec[1] + l*mg->lvec[1];
129  loc[2] = h*mg->hvec[2] + k*mg->kvec[2] + l*mg->lvec[2];
130  sf = (Bstrucfac *) add_item((char **) &sf, sizeof(Bstrucfac));
131  if ( !mg->sf ) mg->sf = sf;
132  sf->index[0] = h;
133  sf->index[1] = k;
134  sf->index[2] = l;
135  sf->loc = loc;
136  sf->fom = sf->sel = 1;
137  n++;
138  }
139  }
140  }
141  }
142 
143  return n;
144 }
145 
153 int img_mask_reflections(Bimage* p, Bstrucfac* sflist, double radius)
154 {
155  long i, datasize = (long) p->size().volume();
156 // Vector3<long> mid(p->size()/2);
157  Vector3<double> loc;
158  Bstrucfac* sf = NULL;
159 
160  Bimage* pmask = new Bimage(Float, p->compound_type(), p->size(), 1);
161  Complex<double> cv;
162 
163  if ( p->compound_type() == TSimple ) { // Power spectrum
164  for ( sf = sflist; sf; sf = sf->next )
165  pmask->sphere(sf->loc, radius, 0, FILL_USER, 1);
166  for ( i=0; i<datasize; i++ )
167  if ( (*pmask)[i] < 1 ) p->set(i, 0);
168  } else if ( p->compound_type() == TComplex ) { // Standard Fourier transform
169  for ( sf = sflist; sf; sf = sf->next ) {
170  loc = vector3_set_PBC(sf->loc, p->size());
171  pmask->sphere(loc, radius, 0, FILL_USER, 1);
172  }
173  for ( i=0; i<datasize; i++ )
174  if ( (*pmask)[i] < 1 ) p->set(i, cv);;
175  }
176 
177  delete pmask;
178 
179  return 0;
180 }
181 
182 
Bstrucfac::fom
double fom
Definition: mg_processing.h:164
Bmicrograph::sf
Bstrucfac * sf
Definition: mg_processing.h:246
Bmicrograph
Definition: mg_processing.h:201
matrix_linear.h
Header file for matrix functions.
Bstrucfac
Definition: mg_processing.h:156
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Complex
Definition: Complex.h:23
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
Bstrucfac::sel
long sel
Definition: mg_processing.h:165
Matrix::multiply_in_place
int multiply_in_place(vector< double > &vec)
Definition: matrix.cpp:49
Vector3< double >
Bstrucfac::loc
Vector3< double > loc
Definition: mg_processing.h:158
Complex.h
Class for complex numbers.
TComplex
@ TComplex
Definition: Bimage.h:60
FILL_USER
#define FILL_USER
Definition: utilities.h:176
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bmicrograph::lvec
Vector3< double > lvec
Definition: mg_processing.h:228
TSimple
@ TSimple
Definition: Bimage.h:59
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:223
mg_xtal.h
Header file for functions to process crystallographic data.
Bmicrograph::hvec
Vector3< double > hvec
Definition: mg_processing.h:226
kmax
long kmax(long i, long km, long m)
Definition: Bimage_filter.cpp:22
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
timer.h
Utilities for timing functions.
Bstrucfac::next
Bstrucfac * next
Definition: mg_processing.h:157
Bmicrograph::kvec
Vector3< double > kvec
Definition: mg_processing.h:227
Matrix::LU_decomposition
double LU_decomposition()
Matrix inversion by LU decomposition.
Definition: matrix.cpp:78
mg_unitcell_vectors
int mg_unitcell_vectors(Bmicrograph *mg)
Calculates the unit cell vectors for a 2D crystal.
Definition: mg_xtal.cpp:32
vector3_set_PBC
Vector3< T1 > vector3_set_PBC(Vector3< T1 > coord, Vector3< T2 > box)
Definition: Vector3.h:684
utilities.h
Header file for general utilities.
Bstrucfac::index
Vector3< int > index
Definition: mg_processing.h:159
verbose
int verbose
Vector3::volume
double volume()
Definition: Vector3.h:237
img_mask_reflections
int img_mask_reflections(Bimage *p, Bstrucfac *sflist, double radius)
Masks the image using the list of reflections.
Definition: mg_xtal.cpp:153
Matrix.h
Generalized matrix class.
mg_processing.h
Header file for micrograph processing.
linked_list.h
Header file for generalized linked list functions.
mg_generate_reflections
long mg_generate_reflections(Bmicrograph *mg, Vector3< double > real_size, double resolution)
Generates reflections given the unit cell vectors.
Definition: mg_xtal.cpp:80
Bmicrograph::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:215
Vector3::length
double length()
Definition: Vector3.h:222
kmin
long kmin(long i, long hk)
Definition: Bimage_filter.cpp:17
Matrix
Definition: Matrix.h:22
Float
@ Float
Definition: utilities.h:161
Bimage::sphere
int sphere(Vector3< double > center, double radius, double width=0, int fill_type=0, double fill=0)
Fills a sphere within an image with a uniform value.
Definition: Bimage_edit.cpp:440