Clipper
fftmap.h
1 
4 //C Copyright (C) 2000-2006 Kevin Cowtan and University of York
5 //L
6 //L This library is free software and is distributed under the terms
7 //L and conditions of version 2.1 of the GNU Lesser General Public
8 //L Licence (LGPL) with the following additional clause:
9 //L
10 //L `You may also combine or link a "work that uses the Library" to
11 //L produce a work containing portions of the Library, and distribute
12 //L that work under terms of your choice, provided that you give
13 //L prominent notice with each copy of the work that the specified
14 //L version of the Library is used in it, and that you include or
15 //L provide public access to the complete corresponding
16 //L machine-readable source code for the Library including whatever
17 //L changes were used in the work. (i.e. If you make changes to the
18 //L Library you must distribute those, but you do not need to
19 //L distribute source or object code to those portions of the work
20 //L not covered by this licence.)'
21 //L
22 //L Note that this clause grants an additional right and does not impose
23 //L any additional restriction, and so does not affect compatibility
24 //L with the GNU General Public Licence (GPL). If you wish to negotiate
25 //L other terms, please contact the maintainer.
26 //L
27 //L You can redistribute it and/or modify the library under the terms of
28 //L the GNU Lesser General Public License as published by the Free Software
29 //L Foundation; either version 2.1 of the License, or (at your option) any
30 //L later version.
31 //L
32 //L This library is distributed in the hope that it will be useful, but
33 //L WITHOUT ANY WARRANTY; without even the implied warranty of
34 //L MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 //L Lesser General Public License for more details.
36 //L
37 //L You should have received a copy of the CCP4 licence and/or GNU
38 //L Lesser General Public License along with this library; if not, write
39 //L to the CCP4 Secretary, Daresbury Laboratory, Warrington WA4 4AD, UK.
40 //L The GNU Lesser General Public can also be obtained by writing to the
41 //L Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
42 //L MA 02111-1307 USA
43 
44 
45 #ifndef CLIPPER_FFTMAP
46 #define CLIPPER_FFTMAP
47 
48 
49 #include "coords.h"
50 
51 #include <complex>
52 
53 
54 namespace clipper
55 {
56  // fft type
57  typedef float ffttype;
58 
59  // forward definition
60  namespace datatypes {
61  template<class T> class F_phi;
62  }
63 
64  // base class for FFT classes
65  class FFTmap_base {
66  public:
67  enum FFTtype { Default, Measure, Estimate };
68  protected:
69  static Mutex mutex;
70  };
71 
73 
80  class FFTmap_p1 : public FFTmap_base
81  {
82  public:
84  FFTmap_p1();
86  FFTmap_p1( const FFTmap_p1& other ) { copy(other); }
88  FFTmap_p1( const Grid_sampling& grid_sam, const FFTtype type = Default );
90  const FFTmap_p1& operator =(const FFTmap_p1& other) { return copy(other); }
92  void init( const Grid_sampling& grid_sam, const FFTtype type = Default );
94  void reset();
95 
97  const Grid_sampling& grid_real() const { return grid_sam_; }
99  const Grid& grid_reci() const { return grid_reci_; }
101  bool uniq_reci( const Coord_grid& c ) const { return ( (c.w()>0 && c.w()<grid_half_.nw()) || (c.w()<=grid_half_.nw() && ( (c.v()>0 && c.v()<grid_half_.nv()) || (c.v()<=grid_half_.nv() && c.u()<=grid_half_.nu()) ) ) ); }
102 
104  void fft_h_to_x( const ftype& scale );
106  void fft_x_to_h( const ftype& scale );
107 
109  std::complex<ffttype> get_hkl( const HKL& hkl ) const;
111  void set_hkl( const HKL& hkl, const std::complex<ffttype>& f );
113  const std::complex<ffttype>& cplx_data( const Coord_grid& c ) const
114  { return data_c[ grid_reci_.index( c ) ]; }
116  std::complex<ffttype>& cplx_data( const Coord_grid& c )
117  { return data_c[ grid_reci_.index( c ) ]; }
119  const ffttype& real_data( const Coord_grid& c ) const
120  { return data_r[ grid_real_.index( c ) ]; }
122  ffttype& real_data( const Coord_grid& c )
123  { return data_r[ grid_real_.index( c ) ]; }
124 
126  static FFTtype& default_type() { return default_type_; }
127 
128  void debug() const;
129 
130  protected:
131  const FFTmap_p1& copy( const FFTmap_p1& other );
132 
133  enum FFTmode { NONE, RECI, REAL, OTHER };
134 
135  FFTmode mode;
136  FFTtype type_;
141 
143  std::vector<char> req_l, req_u;
144 
145  std::vector<ffttype> datavec;
146  ffttype* data_r;
147  std::complex<ffttype>* data_c;
148 
149  static FFTtype default_type_;
150  };
151 
152 
154 
165  class FFTmap : private FFTmap_p1
166  {
167  public:
169  FFTmap();
171  FFTmap( const Spacegroup& spacegroup, const Cell& cell, const Grid_sampling grid_sam, const FFTtype type = Default );
173  void init( const Spacegroup& spacegroup, const Cell& cell, const Grid_sampling grid_sam, const FFTtype type = Default );
175  void reset();
176 
178  const Cell& cell() const { return cell_; }
180  const Spacegroup& spacegroup() const { return spacegroup_; }
182  const Grid_sampling& grid_sampling() const { return FFTmap_p1::grid_real(); }
184  void fft_h_to_x();
186  void fft_x_to_h();
187 
189  template<class T> void get_recip_data( const HKL& rfl, datatypes::F_phi<T>& fphi ) const;
191  template<class T> void set_recip_data( const HKL& rfl, const datatypes::F_phi<T>& fphi );
192 
194  template<class T> void get_real_data( const Coord_grid& c, T& datum ) const;
196  template<class T> void set_real_data( const Coord_grid& c, const T& datum );
197 
199  datatypes::F_phi<ffttype> get_recip_data( const HKL& rfl ) const;
201  const ffttype& get_real_data( const Coord_grid& c ) const
202  { return real_data(c.unit(grid_real())); }
203 
205  template<class H, class X> void fft_rfl_to_map( const H& h, X& x );
207  template<class H, class X> void fft_map_to_rfl( const X& x, H& h );
208 
209  void debug() const;
210 
211  protected:
214  std::vector<Isymop> isymop;
215  };
216 
217 
218  // template implementations
219 
220 
232  template<class H, class X> void FFTmap::fft_rfl_to_map( const H& h, X& x )
233  {
234  // zero the map
235  reset();
236 
237  // copy from reflection data
238  typename H::HKL_reference_index ih;
239  for ( ih = h.first_data(); !ih.last(); h.next_data( ih ) )
240  set_recip_data( ih.hkl(), h[ih] );
241 
242  // fft
243  fft_h_to_x();
244 
245  // and copy into the map
246  typename X::Map_reference_index ix;
247  for ( ix = x.first(); !ix.last(); ix.next() )
248  get_real_data( ix.coord(), x[ix] );
249  }
250 
251 
264  template<class H, class X> void FFTmap::fft_map_to_rfl( const X& x, H& h )
265  {
266  // zero the map
267  reset();
268 
269  // copy into the map
270  typename X::Map_reference_index ix;
271  for ( ix = x.first(); !ix.last(); ix.next() )
272  set_real_data( ix.coord(), x[ix] );
273 
274  // fft
275  fft_x_to_h();
276 
277  // now fill it
278  typename H::HKL_reference_index ih;
279  for ( ih = h.first(); !ih.last(); ih.next() )
280  get_recip_data( ih.hkl(), h[ih] );
281  }
282 
283 
284 } // namespace clipper
285 
286 #endif