~mok0/clipper/trunk

« back to all changes in this revision

Viewing changes to clipper/contrib/sfcalc_obs.cpp

  • Committer: Morten Kjeldgaard
  • Date: 2011-02-09 21:25:56 UTC
  • Revision ID: mok@bioxray.dk-20110209212556-3zbon5ukh4zf3x00
Tags: 2.0.3
versionĀ 2.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* sfweight.cpp: structure factor calculation vs observed implementation */
 
2
//C Copyright (C) 2000-2006 Kevin Cowtan and University of York
 
3
//L
 
4
//L  This library is free software and is distributed under the terms
 
5
//L  and conditions of version 2.1 of the GNU Lesser General Public
 
6
//L  Licence (LGPL) with the following additional clause:
 
7
//L
 
8
//L     `You may also combine or link a "work that uses the Library" to
 
9
//L     produce a work containing portions of the Library, and distribute
 
10
//L     that work under terms of your choice, provided that you give
 
11
//L     prominent notice with each copy of the work that the specified
 
12
//L     version of the Library is used in it, and that you include or
 
13
//L     provide public access to the complete corresponding
 
14
//L     machine-readable source code for the Library including whatever
 
15
//L     changes were used in the work. (i.e. If you make changes to the
 
16
//L     Library you must distribute those, but you do not need to
 
17
//L     distribute source or object code to those portions of the work
 
18
//L     not covered by this licence.)'
 
19
//L
 
20
//L  Note that this clause grants an additional right and does not impose
 
21
//L  any additional restriction, and so does not affect compatibility
 
22
//L  with the GNU General Public Licence (GPL). If you wish to negotiate
 
23
//L  other terms, please contact the maintainer.
 
24
//L
 
25
//L  You can redistribute it and/or modify the library under the terms of
 
26
//L  the GNU Lesser General Public License as published by the Free Software
 
27
//L  Foundation; either version 2.1 of the License, or (at your option) any
 
28
//L  later version.
 
29
//L
 
30
//L  This library is distributed in the hope that it will be useful, but
 
31
//L  WITHOUT ANY WARRANTY; without even the implied warranty of
 
32
//L  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
33
//L  Lesser General Public License for more details.
 
34
//L
 
35
//L  You should have received a copy of the CCP4 licence and/or GNU
 
36
//L  Lesser General Public License along with this library; if not, write
 
37
//L  to the CCP4 Secretary, Daresbury Laboratory, Warrington WA4 4AD, UK.
 
38
//L  The GNU Lesser General Public can also be obtained by writing to the
 
39
//L  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 
40
//L  MA 02111-1307 USA
 
41
 
 
42
 
 
43
#include "sfcalc_obs.h"
 
44
#include "edcalc.h"
 
45
#include "../core/hkl_compute.h"
 
46
#include "../core/resol_targetfn.h"
 
47
 
 
48
 
 
49
namespace clipper {
 
50
 
 
51
 
 
52
template<class T> bool SFcalc_obs_bulk<T>::operator() ( HKL_data<datatypes::F_phi<T> >& fphi, const HKL_data<datatypes::F_sigF<T> >& fsig, const Atom_list& atoms )
 
53
{
 
54
  // set U value constants
 
55
  double u_atom = Util::b2u( 20.0 );
 
56
  double u_mask = Util::b2u( 50.0 );
 
57
 
 
58
  // increase the U values
 
59
  Atom_list atomu = atoms;
 
60
  U_aniso_orth uadd( u_atom ), u;
 
61
  for ( int i = 0; i < atomu.size(); i++ ) if ( !atomu[i].is_null() ) {
 
62
    u = atomu[i].u_aniso_orth();
 
63
    if ( u.is_null() ) u = U_aniso_orth( atomu[i].u_iso() );
 
64
    atomu[i].set_u_aniso_orth( u + uadd );
 
65
  }
 
66
 
 
67
  // now make the map for ed calcs
 
68
  const HKL_info&   hkls = fsig.base_hkl_info();
 
69
  const Spacegroup& spgr = hkls.spacegroup();
 
70
  const Cell&       cell = fsig.base_cell();
 
71
  HKL_data<datatypes::F_phi<T> >
 
72
    fphi_atom( hkls, cell ), fphi_mask( hkls, cell );
 
73
  const Grid_sampling grid( spgr, cell, hkls.resolution() );
 
74
  Xmap<float> xmap( spgr, cell, grid );
 
75
 
 
76
  // do ed calc from atomu
 
77
  EDcalc_aniso<ftype32> edcalc;
 
78
  edcalc( xmap, atomu );
 
79
  xmap.fft_to( fphi_atom );
 
80
  fphi_atom.compute( fphi_atom, datatypes::Compute_scale_u<datatypes::F_phi<T> >( 1.0, u_atom ) );
 
81
 
 
82
  // do density calc from mask
 
83
  EDcalc_mask<ftype32> emcalc;
 
84
  emcalc( xmap, atomu );
 
85
  for ( Xmap<ftype32>::Map_reference_index ix = xmap.first();
 
86
        !ix.last(); ix.next() )
 
87
    xmap[ix] = 1.0 - xmap[ix];
 
88
  xmap.fft_to( fphi_mask );
 
89
  fphi_mask.compute( fphi_mask, datatypes::Compute_scale_u<datatypes::F_phi<T> >( 1.0, -u_mask ) );
 
90
 
 
91
  // try some different scale factors
 
92
  std::vector<double> params( nparams, 1.0 );
 
93
  BasisFn_spline basisfn( hkls, nparams, 1.0 );
 
94
  TargetFn_scaleF1F2<datatypes::F_phi<T>,datatypes::F_sigF<T> > targetfn( fphi, fsig );
 
95
  T x1 = 0.35, dx = 0.35, x;
 
96
  ftype y[3] = { 0.0, 0.0, 0.0 };
 
97
  for ( int i = 0; i < 8; i++ ) {
 
98
    // take 3 samples of function
 
99
    for ( int d = -1; d <= 1; d++ ) if ( y[d+1] == 0.0 ) {
 
100
      HKL_data<data32::F_phi>::HKL_reference_index ih;
 
101
      x = x1 + T(d)*dx;
 
102
      for ( ih = fphi.first(); !ih.last(); ih.next() )
 
103
        fphi[ih] = std::complex<T>(fphi_atom[ih]) +
 
104
               x * std::complex<T>(fphi_mask[ih]);
 
105
      ResolutionFn rfn( hkls, basisfn, targetfn, params );
 
106
      double r = 0.0;
 
107
      for ( ih = fsig.first(); !ih.last(); ih.next() )
 
108
        if ( !fsig[ih].missing() ) {
 
109
          double eps = ih.hkl_class().epsilon();
 
110
          r += (2.0/eps) * fabs( sqrt(rfn.f(ih))*fphi[ih].f() - fsig[ih].f() );
 
111
          // r += ( 2.0/eps ) * pow( rfn.f(ih) * pow(fphi[ih].f(),2)/eps - pow(fsig[ih].f(),2)/eps, 2 );
 
112
        }
 
113
      y[d+1] = r;
 
114
      //std::cout << d << "\t" << x << "\t" << r << "\n";
 
115
    }
 
116
    // find minimum of current 3 samples
 
117
    if      ( y[0] < y[1] && y[0] < y[2] ) { y[1] = y[0]; x1 -= dx; }
 
118
    else if ( y[2] < y[1] && y[2] < y[0] ) { y[1] = y[2]; x1 += dx; }
 
119
    // reduce step and search again
 
120
    y[0] = y[2] = 0.0;
 
121
    dx /= 2.0;
 
122
  }
 
123
 
 
124
  // adopt final scale
 
125
  for ( HKL_data<data32::F_phi>::HKL_reference_index ih = fphi.first();
 
126
        !ih.last(); ih.next() )
 
127
    fphi[ih] = std::complex<T>(fphi_atom[ih]) +
 
128
          x1 * std::complex<T>(fphi_mask[ih]);
 
129
 
 
130
  // store stats
 
131
  ftype64 w, s0 = 0.0, s1 = 0.0;
 
132
  for ( Xmap<ftype32>::Map_reference_index ix = xmap.first();
 
133
        !ix.last(); ix.next() ) {
 
134
    w = 1.0/ftype64( xmap.multiplicity( ix.coord() ) );
 
135
    s0 += w;
 
136
    s1 += w*xmap[ix];
 
137
  }
 
138
  bulkfrc = s1/s0;
 
139
  bulkscl = x1;
 
140
 
 
141
  return true;
 
142
}
 
143
 
 
144
 
 
145
// compile templates
 
146
 
 
147
template class SFcalc_obs_bulk<ftype32>;
 
148
 
 
149
template class SFcalc_obs_bulk<ftype64>;
 
150
 
 
151
 
 
152
} // namespace clipper