% 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:
%   
%  FULL SPACE SEARCH; DETERMINE MINIMA BY DIRECT GRID NEIGHBOR COMPARISONS
%  M IS THE NUMBER OF HOURS, I.E.                    dphi = 2*pi/M 
% BAD ALGORITHM: it scales like N^2*M^N ...
clear all
close all
view(0,0)
[a,b,c]=sphere(10);
N = 5;
M = 60;
flops(0);
Ener = zeros(M,M);
dphi =   2*pi/M; %initial dphi; will cover space    
% Build Initial Configuration as a Spiral Staircase
% Define initial angles; will iterate;
phi(1:N)  = pi;
theta(1:N)= 2*pi/3;
phi(2) = 0;
%hold on
for i = 1:M
   phi(3) = 0;
   i
   for j = 1:M
      [R,B] = phi2r(phi,theta);
      ener(i,j) = energy(R,B,phi,theta,.3);
      phi(3) = phi(3)+dphi;
   end 
   phi(2) = phi(2)+dphi;
end
axis([0,2*pi,0,2*pi])
ax=linspace(0,2*pi,M);
contour3(ax,ax,ener,20);
flops
figure
hold on
for i = 2:M-1
   i
   for j = 2:M-1
      a00=ener(i,j); a10=ener(i+1,j); a01=ener(i,j+1);
      a20=ener(i-1,j); a21=ener(i,j-1);
      if ((a00 < a10) & (a00 < a01) & (a00 < a21) & (a00 < a20))
         phi(2)= dphi*i;            phi(3)=dphi*j;
         [R,B] = phi2r(phi,theta);
         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-')
      end
   end 
   pause(1)
end
%axis([0,2.5,-2.5,2.5,-2.5,2.5])
hold off
   
%for kk = 1:10
%   pause(1)
%  view(6*kk,50)
%end
