% script Local_Moves; 
%                    perform localized moves in a polymer chain.
% >> Perform moves of the chain that involve only 
%     a subset of atoms. Augment to avoid steric clashes.  <<
% PROBLEM 1: given chain, deform torsion angles so first and last atoms
%            approach to distance 1
% PROBLEM 2: further deform torsion angles to allow joining of first and
%            last atoms with correct bond angle
% PROBLEM 3: introduce arbitrary moves of closed chain, centered about any
%            given torsion angle. Find maximally distorting infinitesimal 
%            deformation (leading singular vector).
%
% For simplicity, consider a polymer where successive 
% bond-angles are fixed, equal to >> theta <<, while torsion
% angles phi_j are entirely free. 
%                                               7
%        ____________i+2                       /
%        |            ^                       /
%        |            |                      /\theta=pi-theta'
%        |    \ b(i)  |____n(i+1) --------->----------> x-axis
%        i-----\----->i+1                   \    b(i;0)
%       / ^   phi(i)   \                     \
%      /   \            \                     \
%     n(i)  \            \                     V z-axis
%           i-1------------         
%                                 q(i-1) * p(i)* b(i;0) = b(i)
%      q(i)   = q(i-1)   * p(i;0)
%      p(i;0) = d(i-1;0) * a(i;0)
%      d(i;0) =    Ux(phi(i))   = /     1           0         0     \
%                                 |     0        cos(phi) -sin(phi) |
%                                 \     0        sin(phi)  cos(phi) /
%      a(i;0) =    Uz(theta-pi) = / -cos(theta)  sin(theta)   0     \
%                                 | -sin(theta) -cos(theta)   0     |
%                                 \     0           0         1     /
%  Then, 
%     if cos(theta)=ct, sin(theta)=st, cos(phi)=cp(i), sin(phi)=sp(i),   
%        W(i) = /  -ct       st        0   -b*ct     \
%               |  -st*cp   -ct*cp   -sp   -b*st*cp  |
%               |  -st*sp   -ct*sp    cp   -b*st*sp  |
%               \    0        0        0       1     /
% This latter matrix works as follows:
% Since 
%        R(i+1) = R(i) + b(i)
%  and
%        b(i) = b*U(1)*U(2)...*U(i)*e1 
%  then  
%        R(i) = W(1)*...*W(i)(1:3,4)
%  since 
%        W(1)*...*W(i) = / Q(1)*...Q(i)  Q(1)*...*Q(i)*e1+R(i) \
%                        \       0                  1          /
%      =  / Q(1-i)  R(i) \
%         \   0      1   /
%
%      n(i)   = q(i-1)*d(i-1;0)*e3
%      b(i)   = q(i)*e1 = q(i-1)*d(i-1;0)*a(i;0)*e1
%      b(0)   = e1 (unused arm on Atom 1 afixed to the x-axis;
%                   other arm at a(1)*e1, d(0) = I )
%      phi(1), phi(N) may be defined but have no effect                        
%                                  
%                                 |sin(phi(i))| = ||n(i)Xn(i+1)||
%    R(i+1) = R(i) + b(i)
%    n(i+1) = b(i) X b(i+1)
%
%  here we implement this slightly differently:
%  Form B = [Q(1) Q(2) Q(3) ... Q(N)] e1
%   and R = [B(1) S_1^2B(i) S_1^3B(i) ... S_1^NB(i)];
%   compute flops to compare (cheaper by over twice!)
% CLOSURE CONDITIONS
%
%  I.               R(1)=R(N+1)
% II.               b(N)=e1
%      
%    
% To place the chain given the dihedral angles phi(1:n)
% proceed from atoms 1 to N:
%   
%   
% BAD ALGORITHM: it scales like N^2 ...
clear all
[a,b,c]=sphere(10);
N = 10;
omega2 =   pi/12;
theta2 = pi/3;
ct = cos(theta2); st = sin(theta2); % this is really theta/2!
P=zeros(4,2);
R=zeros(3,N);
figure
B=zeros(3,N);
B(1,:)=1;
% Initial Configuration is arranged along the +x-axis
% each bond, begining from the end, is rotated about the +z-axis
% to form an angle of theta-p with its predecessor. Then it is 
% rotated by angle phi about its predecessor (i.e. the +x-axis).
% count flops for construction of B, R
flops(0);
phi0 = 0;
Q = zeros(4,N);
Q(:,1) = [st;0;0;-ct];
B(:,1) = [2*st^st-1;-st*ct;0]; 
l1 = 1;
a0=flops
for l = 2:N
   phi(l1) = omega; %this is omega/2
   cp=cos(phi(l1));sp=sin(phi(l1));% d(l-1)=[cp,sp,0,0]
   P = Q(:,l1)*[cp sp]; % 2x2 rotation abt x (in yz plane)
   Q(1,l) = P(1,1)-P(2,2); Q(2,l) = P(1,2)+P(2,1);
   Q(3,l) = P(3,1)+P(4,2); Q(4,l) = P(4,1)-P(3,2);
   P = Q(:,l)*[st -ct]; % 2x2 rotation abt z (in xy plane)
   Q(1,l) = P(1,1)-P(4,2); Q(2,l) = P(3,2)+P(2,1);
   Q(3,l) = P(3,1)-P(2,2); Q(4,l) = P(4,1)+P(1,2);
   a0=flops
   
   ----------------------CONTINUE
   B(:,l) = Q(:,1);
   l1=l;
end
a0=flops
R=B;
%flops(0)
k1=1;
for k = 2:N
   R(:,k)=R(:,k1)+R(:,k);
   k1=k;
end
hold on
a0=flops
for i = 1:N
   surf(.1*a+R(1,i),.1*b+R(2,i),.1*c+R(3,i));
end
plot3(R(1,:),R(2,:),R(3,:),'y-')
hold off
for kk = 1:40
   pause(1)
   view(6*kk,50)
end
