1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.xray;
39
40 import static ffx.numerics.math.DoubleMath.length2;
41 import static ffx.numerics.math.DoubleMath.sub;
42 import static java.lang.Math.sqrt;
43
44 import ffx.potential.bonded.Atom;
45 import ffx.xray.RefinementMinimize.RefinementMode;
46
47
48
49
50
51
52
53 public final class SolventPolyFormFactor implements FormFactor {
54
55 private final Atom atom;
56 private final double[] xyz = new double[3];
57 private final double[] dxyz = new double[3];
58 private final double[] g = new double[3];
59 private final double iw;
60 private final double aradMinusW, aradPlusW;
61 private final double aradMinusW2, aradPlusW2;
62 private final double wMinusArad;
63
64
65
66
67
68
69
70
71 public SolventPolyFormFactor(Atom atom, double arad, double w) {
72 this(atom, arad, w, atom.getXYZ(null));
73 }
74
75
76
77
78
79
80
81
82
83 public SolventPolyFormFactor(Atom atom, double arad, double w, double[] xyz) {
84 this.atom = atom;
85 this.iw = 1.0 / w;
86 aradMinusW = arad - w;
87 aradPlusW = arad + w;
88 wMinusArad = w - arad;
89 aradMinusW2 = aradMinusW * aradMinusW;
90 aradPlusW2 = aradPlusW * aradPlusW;
91 update(xyz);
92 }
93
94
95 @Override
96 public double rho(double f, double lambda, double[] xyz) {
97 sub(this.xyz, xyz, dxyz);
98 double ri2 = length2(dxyz);
99 if (ri2 <= aradMinusW2) {
100 return 0.0;
101 }
102 if (ri2 >= aradPlusW2) {
103 return f;
104 }
105 return rho(f, lambda, sqrt(ri2));
106 }
107
108
109
110
111
112
113
114
115
116 public double rho(double f, double lambda, double ri) {
117 if (ri <= aradMinusW) {
118 return 0.0;
119 }
120 if (ri >= aradPlusW) {
121 return f;
122 }
123 double d = ri + wMinusArad;
124 double dw = d * iw;
125 double dw2 = dw * dw;
126 return f * (0.75 - 0.25 * dw) * dw2;
127 }
128
129
130 @Override
131 public void rhoGrad(double[] xyz, double dfc, RefinementMode refinementmode) {
132 if (refinementmode == RefinementMode.BFACTORS
133 || refinementmode == RefinementMode.OCCUPANCIES
134 || refinementmode == RefinementMode.BFACTORS_AND_OCCUPANCIES) {
135 return;
136 }
137 sub(this.xyz, xyz, dxyz);
138 double ri2 = length2(dxyz);
139 if (ri2 <= aradMinusW2 || ri2 >= aradPlusW2) {
140 return;
141 }
142 double ri = sqrt(ri2);
143 double d = ri + wMinusArad;
144 double dw = d * iw;
145 double dw2 = dw * dw;
146 double rho = (0.75 - 0.25 * dw) * dw2;
147 double iri = 1.0 / ri;
148 double dp = (1.5 * dw - 0.75 * dw2) * iri * iw;
149 double prefactor = -dp * (dfc / rho);
150 g[0] = prefactor * dxyz[0];
151 g[1] = prefactor * dxyz[1];
152 g[2] = prefactor * dxyz[2];
153 atom.addToXYZGradient(g[0], g[1], g[2]);
154 }
155
156
157 @Override
158 public void update(double[] xyz) {
159 update(xyz, 0.0);
160 }
161
162
163 @Override
164 public void update(double[] xyz, double badd) {
165 this.xyz[0] = xyz[0];
166 this.xyz[1] = xyz[1];
167 this.xyz[2] = xyz[2];
168 }
169 }