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 """The numerical fit of 2-site Bloch-McConnell equations for CPMG-type experiments, the U{NS CPMG 2-site expanded<http://wiki.nmr-relax.com/NS_CPMG_2-site_expanded>} model.
29
30 Description
31 ===========
32
33 This function is exact, just as the explicit Bloch-McConnell numerical treatments. It comes from a Maple derivation based on the Bloch-McConnell equations. It is much faster than the numerical Bloch-McConnell solution. It was derived by Nikolai Skrynnikov and is provided with his permission.
34
35
36 Code origin
37 ===========
38
39 The code originates as optimization function number 5 from the fitting_main_kex.py script from Mathilde Lescanne, Paul Schanda, and Dominique Marion (see U{http://thread.gmane.org/gmane.science.nmr.relax.devel/4138}, U{https://web.archive.org/web/https://gna.org/task/?7712#comment2} and U{https://web.archive.org/web/https://gna.org/support/download.php?file_id=18262}).
40
41 Links to the copyright licensing agreements from all authors are:
42
43 - Nikolai Skrynnikov, U{http://article.gmane.org/gmane.science.nmr.relax.devel/4279},
44 - Martin Tollinger, U{http://article.gmane.org/gmane.science.nmr.relax.devel/4276},
45 - Paul Schanda, U{http://article.gmane.org/gmane.science.nmr.relax.devel/4271},
46 - Mathilde Lescanne, U{http://article.gmane.org/gmane.science.nmr.relax.devel/4138},
47 - Dominique Marion, U{http://article.gmane.org/gmane.science.nmr.relax.devel/4157}.
48
49
50 Code evolution
51 --------------
52
53 The complex path of the code from the original Maple to relax can be described as:
54
55 - p3.analytical (Maple input text file at U{https://web.archive.org/web/https://gna.org/task/?7712#comment8}),
56 - Automatically generated FORTRAN,
57 - Manually converted to Matlab by Nikolai (sim_all.tar at U{https://web.archive.org/web/https://gna.org/task/?7712#comment5})
58 - Manually converted to Python by Paul, Mathilde, and Dominique (fitting_main.py at U{https://web.archive.org/web/https://gna.org/task/?7712#comment1})
59 - Converted into Python code for relax (here).
60
61
62 Maple p3.analytical script
63 --------------------------
64
65 For reference, the original Maple script written by Nikolai for the expansion of the equations is::
66
67 with(linalg):
68 with(tensor):
69 #Ka:=30;
70 #Kb:=1200;
71 #dW:=300;
72 #N:=2;
73 #tcp:=0.040/N;
74
75 Ksym:=sqrt(Ka*Kb);
76 #dX:=(Ka-Kb+I*dw)/2; # Ra=Rb
77 dX:=((Ra-Rb)+(Ka-Kb)+I*dw)/2;
78
79 L:=([[-dX, Ksym], [Ksym, dX]]);
80
81 # in the end everything is multiplied by exp(-0.5*(Ra+Rb+Ka+Kb)*(Tc+2*tpalmer))
82 # where 0.5*(Ra+Rb) is the same as Rinf, and (Ka+Kb) is kex.
83
84 y:=eigenvects(L);
85 TP1:=array([[y[1][3][1][1],y[2][3][1][1]],[y[1][3][1][2],y[2][3][1][2]]]);
86 iTP1:=inverse(TP1);
87 P1:=array([[exp(y[1][1]*tcp/2),0],[0,exp(y[2][1]*tcp/2)]]);
88
89 P1palmer:=array([[exp(y[1][1]*tpalmer),0],[0,exp(y[2][1]*tpalmer)]]);
90
91 TP2:=map(z->conj(z),TP1);
92 iTP2:=map(z->conj(z),iTP1);
93 P2:=array([[exp(conj(y[1][1])*tcp),0],[0,exp(conj(y[2][1])*tcp)]]);
94
95 P2palmer:=array([[exp(conj(y[1][1])*tpalmer),0],[0,exp(conj(y[2][1])*tpalmer)]]);
96
97 cP1:=evalm(TP1&*P1&*iTP1);
98 cP2:=evalm(TP2&*P2&*iTP2);
99
100 cP1palmer:=evalm(TP1&*P1palmer&*iTP1);
101 cP2palmer:=evalm(TP2&*P2palmer&*iTP2);
102
103 Ps:=evalm(cP1&*cP2&*cP1);
104 # Ps is symmetric; cf. simplify(Ps[1,2]-Ps[2,1]);
105 Pspalmer:=evalm(cP2palmer&*cP1palmer);
106
107
108 dummy:=array([[a,b],[b,c]]);
109 x:=eigenvects(dummy);
110 TG1:=array([[x[1][3][1][1],x[2][3][1][1]],[x[1][3][1][2],x[2][3][1][2]]]);
111 iTG1:=inverse(TG1);
112 G1:=array([[x[1][1]^(N/4),0],[0,x[2][1]^(N/4)]]);
113 GG1:=evalm(TG1&*G1&*iTG1);
114 GG2:=map(z->conj(z),GG1);
115
116 cGG:=evalm(GG2&*Pspalmer&*GG1);
117
118 #s0:=array([Kb, Ka]);
119 s0:=array([sqrt(Kb),sqrt(Ka)]); # accounts for exchange symmetrization
120 st:=evalm(cGG&*s0);
121 #obs:=(1/(Ka+Kb))*st[1];
122 obs:=(sqrt(Kb)/(Ka+Kb))*st[1]; # accounts for exchange symmetrization
123
124 obs1:=eval(obs,[a=Ps[1,1],b=Ps[1,2],c=Ps[2,2]]);
125 #obs2:=simplify(obs1):
126
127 print(obs1):
128
129 cGGref:=evalm(Pspalmer);
130 stref:=evalm(cGGref&*s0);
131 obsref:=(sqrt(Kb)/(Ka+Kb))*stref[1]; # accounts for exchange symmetrization
132
133 print(obsref):
134
135 writeto(result_test):
136
137 fortran([intensity=obs1, intensity_ref=obsref], optimized):
138
139
140 Matlab sim_all.tar funNikolai.m script
141 --------------------------------------
142
143 Also for reference, the Matlab code from Nikolai and Martin manually converted from the automatically generated FORTRAN from the previous script into the funNikolai.m file is::
144
145 function residual = funNikolai(optpar)
146
147 % extended Carver's equation derived via Maple, Ra-Rb = 0 by Skrynnikov
148
149 global nu_0 x y Rcalc rms nfields
150 global Tc
151
152 Rcalc=zeros(nfields,size(x,2));
153
154 tau_ex=optpar(1);
155 pb=optpar(2);
156
157 pa=1-pb;
158 kex=1/tau_ex;
159 Ka=kex*pb;
160 Kb=kex*pa;
161
162 nu_cpmg=x;
163 tcp=1./(2*nu_cpmg);
164 N=round(Tc./tcp);
165
166 for k=1:nfields
167 dw=2*pi*nu_0(k)*optpar(3);
168 Rinf=optpar(3+k);
169
170 t3 = i;
171 t4 = t3*dw;
172 t5 = Kb^2;
173 t8 = 2*t3*Kb*dw;
174 t10 = 2*Kb*Ka;
175 t11 = dw^2;
176 t14 = 2*t3*Ka*dw;
177 t15 = Ka^2;
178 t17 = sqrt(t5-t8+t10-t11+t14+t15);
179 t21 = exp((-Kb+t4-Ka+t17)*tcp/4);
180 t22 = 1/t17;
181 t28 = exp((-Kb+t4-Ka-t17)*tcp/4);
182 t31 = t21*t22*Ka-t28*t22*Ka;
183 t33 = sqrt(t5+t8+t10-t11-t14+t15);
184 t34 = Kb+t4-Ka+t33;
185 t37 = exp((-Kb-t4-Ka+t33)*tcp/2);
186 t39 = 1/t33;
187 t41 = Kb+t4-Ka-t33;
188 t44 = exp((-Kb-t4-Ka-t33)*tcp/2);
189 t47 = t34*t37*t39/2-t41*t44*t39/2;
190 t49 = Kb-t4-Ka-t17;
191 t51 = t21*t49*t22;
192 t52 = Kb-t4-Ka+t17;
193 t54 = t28*t52*t22;
194 t55 = -t51+t54;
195 t60 = t37*t39*Ka-t44*t39*Ka;
196 t62 = t31.*t47+t55.*t60/2;
197 t63 = 1/Ka;
198 t68 = -t52*t63*t51/2+t49*t63*t54/2;
199 t69 = t62.*t68/2;
200 t72 = t37*t41*t39;
201 t76 = t44*t34*t39;
202 t78 = -t34*t63*t72/2+t41*t63*t76/2;
203 t80 = -t72+t76;
204 t82 = t31.*t78/2+t55.*t80/4;
205 t83 = t82.*t55/2;
206 t88 = t52*t21*t22/2-t49*t28*t22/2;
207 t91 = t88.*t47+t68.*t60/2;
208 t92 = t91.*t88;
209 t95 = t88.*t78/2+t68.*t80/4;
210 t96 = t95.*t31;
211 t97 = t69+t83;
212 t98 = t97.^2;
213 t99 = t92+t96;
214 t102 = t99.^2;
215 t108 = t62.*t88+t82.*t31;
216 t112 = sqrt(t98-2*t99.*t97+t102+4*(t91.*t68/2+t95.*t55/2).*t108);
217 t113 = t69+t83-t92-t96-t112;
218 t115 = N/2;
219 t116 = (t69/2+t83/2+t92/2+t96/2+t112/2).^t115;
220 t118 = 1./t112;
221 t120 = t69+t83-t92-t96+t112;
222 t122 = (t69/2+t83/2+t92/2+t96/2-t112/2).^t115;
223 t127 = 1./t108;
224 t139 = 1/(Ka+Kb)*((-t113.*t116.*t118/2+t120.*t122.*t118/2)*Kb+(-t113.*t127.*t116.*t120.*t118/2+t120.*t127.*t122.*t113.*t118/2)*Ka/2);
225
226 intensity0 = pa; % pA
227 intensity = real(t139)*exp(-Tc*Rinf); % that's "homogeneous" relaxation
228 Rcalc(k,:)=(1/Tc)*log(intensity0./intensity);
229
230 end
231
232 if (size(Rcalc)==size(y))
233 residual=sum(sum((y-Rcalc).^2));
234 rms=sqrt(residual/(size(y,1)*size(y,2)));
235 end
236
237
238 Links
239 =====
240
241 More information on the NS CPMG 2-site expanded model can be found in the:
242
243 - U{relax wiki<http://wiki.nmr-relax.com/NS_CPMG_2-site_expanded>},
244 - U{relax manual<http://www.nmr-relax.com/manual/NS_2_site_expanded_CPMG_model.html>},
245 - U{relaxation dispersion page of the relax website<http://www.nmr-relax.com/analyses/relaxation_dispersion.html#NS_CPMG_2-site_expanded>}.
246 """
247
248
249 from numpy import array, argmax, exp, isfinite, power, log, min, sqrt, sum
250
251
252 from lib.float import isNaN
253
254
255 -def r2eff_ns_cpmg_2site_expanded(r20=None, pA=None, dw=None, k_AB=None, k_BA=None, relax_time=None, inv_relax_time=None, tcp=None, back_calc=None, num_points=None, num_cpmg=None):
256 """The 2-site numerical solution to the Bloch-McConnell equation using complex conjugate matrices.
257
258 This function calculates and stores the R2eff values.
259
260
261 @keyword r20: The R2 value for both states A and B in the absence of exchange.
262 @type r20: float
263 @keyword pA: The population of state A.
264 @type pA: float
265 @keyword dw: The chemical exchange difference between states A and B in rad/s.
266 @type dw: float
267 @keyword k_AB: The rate of exchange from site A to B (rad/s).
268 @type k_AB: float
269 @keyword k_BA: The rate of exchange from site B to A (rad/s).
270 @type k_BA: float
271 @keyword relax_time: The total relaxation time period (in seconds).
272 @type relax_time: float
273 @keyword inv_relax_time: The inverse of the total relaxation time period (in inverse seconds).
274 @type inv_relax_time: float
275 @keyword tcp: The tau_CPMG times (1 / 4.nu1).
276 @type tcp: numpy rank-1 float array
277 @keyword back_calc: The array for holding the back calculated R2eff values. Each element corresponds to one of the CPMG nu1 frequencies.
278 @type back_calc: numpy rank-1 float array
279 @keyword num_points: The number of points on the dispersion curve, equal to the length of the tcp and back_calc arguments.
280 @type num_points: int
281 @keyword num_cpmg: The array of numbers of CPMG blocks.
282 @type num_cpmg: numpy int16, rank-1 array
283 """
284
285
286 if dw == 0.0 or pA == 1.0 or k_AB == 0.0:
287 back_calc[:] = array([r20]*num_points)
288 return
289
290
291 half_tcp = 0.5 * tcp
292 k_AB_plus_k_BA = k_AB + k_BA
293 k_BA_minus_k_AB = k_BA - k_AB
294
295
296 t3 = 1.j
297 t4 = t3 * dw
298 two_t4 = 2.0 * t4
299 t5 = k_BA**2
300 t8 = two_t4 * k_BA
301 t10 = 2.0 * k_BA * k_AB
302 t11 = dw**2
303 t14 = two_t4 * k_AB
304 t15 = k_AB**2
305 t5_t10_t11_t15 = t5 + t10 - t11 + t15
306 t8_t14 = t8 - t14
307 t17 = sqrt(t5_t10_t11_t15 - t8_t14)
308
309 k_AB_plus_k_BA_minus_t4 = k_AB_plus_k_BA - t4
310 t21 = exp((t17 - k_AB_plus_k_BA_minus_t4) * half_tcp)
311 t22 = 1.0/t17
312 t28 = exp(-(t17 + k_AB_plus_k_BA_minus_t4) * half_tcp)
313 t31 = t22*k_AB * (t21 - t28)
314 t33 = sqrt(t5_t10_t11_t15 + t8_t14)
315
316 k_AB_plus_k_BA_plus_t4 = k_AB_plus_k_BA + t4
317 k_BA_minus_k_AB_plus_t4 = k_BA_minus_k_AB + t4
318 t34 = k_BA_minus_k_AB_plus_t4 + t33
319 t37 = exp((t33 - k_AB_plus_k_BA_plus_t4) * tcp)
320 t39 = 1.0/t33
321 t41 = k_BA_minus_k_AB_plus_t4 - t33
322 t44 = exp(-(t33 + k_AB_plus_k_BA_plus_t4) * tcp)
323 t47 = 0.5*t39 * (t34*t37 - t41*t44)
324
325 k_BA_minus_k_AB_minus_t4 = k_BA_minus_k_AB - t4
326 t49 = k_BA_minus_k_AB_minus_t4 - t17
327 t51 = t21 * t49 * t22
328 t52 = k_BA_minus_k_AB_minus_t4 + t17
329 t54 = t28 * t52 * t22
330 t55 = -t51 + t54
331 t60 = 0.5*t39*k_AB * (t37 - t44)
332 t62 = t31*t47 + t55*t60
333 t63 = 1.0/k_AB
334 t68 = 0.5*t63 * (t49*t54 - t52*t51)
335 t69 = 0.5*t62 * t68
336 t72 = t37 * t41 * t39
337 t76 = t44 * t34 * t39
338 t78 = 0.5*t63 * (t41*t76 - t34*t72)
339 t80 = 0.5 * (t76 - t72)
340 t82 = 0.5 * (t31*t78 + t55*t80)
341 t83 = t82 * t55/2.0
342 t88 = 0.5 * t22 * (t52*t21 - t49*t28)
343 t91 = t88 * t47 + t68*t60
344 t92 = t91 * t88
345 t95 = 0.5 * (t88*t78 + t68*t80)
346 t96 = t95 * t31
347 t97 = t69 + t83
348 t98 = t97**2
349 t99 = t92 + t96
350 t102 = t99**2
351 t108 = t62 * t88 + t82 * t31
352 t112 = sqrt(t98 - 2.0 * t99 * t97 + t102 + 2.0 * (t91 * t68 + t95 * t55) * t108)
353 t97_t99 = t97 + t99
354 t97_nt99 = t97 - t99
355 t113 = t97_nt99 - t112
356 t115 = num_cpmg
357 t116 = power(0.5*(t97_t99 + t112), t115)
358 t118 = 1.0/t112
359 t120 = t97_nt99 + t112
360 t122 = power(0.5*(t97_t99 - t112), t115)
361 t127 = 0.5/t108
362 t120_t122 = t120*t122
363 t139 = 0.5/(k_AB + k_BA) * ((t120_t122 - t113*t116)*t118*k_BA + (t120_t122 - t116*t120)*t127*t113*t118*k_AB)
364
365
366 intensity0 = pA
367 intensity = t139.real * exp(-relax_time * r20)
368
369
370 Mx = intensity / intensity0
371
372
373 R2eff = -inv_relax_time * log(Mx)
374
375
376
377 if not isfinite(sum(R2eff)) or min(Mx) <= 0.0 or not isfinite(sum(Mx)):
378 R2eff = array([1e100]*num_points)
379
380 back_calc[:] = R2eff
381