% 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(i)=pi-theta'(i)
%        |    \ 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) = / U(1)*...U(i)  bU(1)*...*U(i)*e1+R(i) \
%                        \       0                  1           /
%      =  / Q(i)  R(i+1) \
%         \   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
close all
view(25,0)
[a,b,c]=sphere(10);
N = 4
for t = 1:240
   %N = 10;
   omega =   pi/120;
   theta = 2*pi/3;
   %r1=0;r2=0;r3=0;
   R=zeros(3,N);
   B=zeros(3,N);
   B(1,:)=1;
   % Build Initial Configuration as a Spiral Staircase
   % count flops for construction of B, R
   %flops(0);
   phi0 = 0;
   Q = rotmat(phi0,theta);
   B(:,1) = Q(:,1); 
   l1 = 1;
   for l = 2:N  
      ct=cos(theta); st=sin(theta); 
      phi(l1) = omega*l1^2*t;
      cp=cos(phi(l1));sp=sin(phi(l1));
      DI = [ cp -sp ; sp  cp ]; % 2x2 rotation abt x (in yz plane)
      BA = [-ct  st ;-st -ct ]; % 2x2 rotation abt z (in xy plane)
      Q(:,2:3) = Q(:,2:3)*DI;  % same as Q = Q*/1 0 \
      %                                        \0 DI/ rot abt x-axis
      Q(:,1:2) = Q(:,1:2)*BA;  % same as Q = Q*/BA 0\
      %                                        \0  1/ rot abt z-axis                        
      B(:,l) = Q(:,1);
      l1=l;
   end
   R=B;
   k1=1;
   for k = 2:N
      R(:,k)=R(:,k1)+R(:,k);
      k1=k;
   end
   %a0(N)=flops;
%end
%plot(1:N,a0(1:N))
%figure
%clf
hold on
axis([-2.5,2.5,-2.5,2.5,-2.5,2.5])
for i = 1:N
   surf(.05*a+R(1,i),.05*b+R(2,i),.05*c+R(3,i));
end
plot3(R(1,:),R(2,:),R(3,:),'y-')
hold off
pause(1)
end
%for kk = 1:40
%   pause(1)
%  view(6*kk,50)
%end
