% script Local_Moves; 
%                    perform localized moves in a polymer chain
% for simplicity, consider a simple polymer where successive 
% bond-angles are fixed, equal to >> theta <<, while torsion
% angles are entirely free. Perform moves of the chain that involve
% only a subset of atoms. Augment to avoid steric clashes.
% Idea: state can be expressed in terms of local quaternions. Each
% atom and bond can be reconstructed, given the torsion angles.
% To ensure that the localization conditions are enforced, assume
% the backbone quaternions are numbered consecutively,
% q1,q2,...,qm; let the total quaternions be Qn=q1*q2*...*qn.
% I want a move that fixes QN1, QN2, with N2=N1+L; L is the 
% length of a (small) segment of the backbone that will be
% perturbed, subject to the constraint that QN1, QN2 remain
% fixed (to be more precise, RN1 and RN2 must also remain
% fixed; this just adds a few more constraints, but the 
% analysis is similar, so let's leave it out of the 
% discussion!). Define q(N1+k):=p(k) (then p(L)=q(N2))
% Since QN2 = QN1*p(1)*...*p(L) let's assume that
% all the intermediate quaternions, p(k) are perturbed
% slightly:  p(k) -->p(k) + d(k), d(k) small. Then,
% QN2 = QN1*(p(1)+d(1))*...*(p(L)+d(L)) = QN1*p(1)*...*p(L)
% and for small d(k) expand keeping only the linear terms.
% This gives a rank=4 constraint on the d(k); it's null
% vectors are all the allowable deformations that leave
% the two ends of the chain fixed (if we also include the
% position vectors, then we get 6 conditions which are
% again linear for small deformations). 
% 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).
clear
[a,b,c]=sphere(10);
N = 10;
omega = .35123*pi;
theta = pi/3;
r1=0;r2=0;r3=0;
for i = 1:N
   phi(i) = omega*i*i;
   X(i)=-cos(theta); Y(i)=sin(theta);Z(i)=Y(i)*sin(phi(i));Y(i)=Y(i)*cos(phi(i));
 %  [X(i),Y(i),Z(i)] = rotmat(phi(i),theta);
   X(i) = r1 + X(i);Y(i) = r2 + Y(i); Z(i) = r3 + Z(i);
   r1   = X(i); r2 = Y(i); r3 = Z(i);
end
hold on
for i = 1:N
   surf(.1*a+X(i),.1*b+Y(i),.1*c+Z(i));
end
plot3(X,Y,Z,'y-')
hold off
for kk = 1:60
   r1=0;r2=0;r3=0;
   for i = 1:N
      phi(i) = omega*i*i;
      if i==5, phi(5) = omega*25*kk;end
      X(i)=-cos(theta); Y(i)=sin(theta);Z(i)=Y(i)*sin(phi(i));
      Y(i)=Y(i)*cos(phi(i));
      %  [X(i),Y(i),Z(i)] = rotmat(phi(i),theta);
      X(i) = r1 + X(i);Y(i) = r2 + Y(i); Z(i) = r3 + Z(i);
      r1   = X(i); r2 = Y(i); r3 = Z(i);
   end
   view(6*kk,30)
   hold on
   for i = 1:N
      surf(.1*a+X(i),.1*b+Y(i),.1*c+Z(i));
   end
   plot3(X,Y,Z,'y-')
   pause(1)
   hold off
end

hold off

%function Q = rotmat(phi,theta)
% st=sin(theta);ct=cos(theta);sp=sin(phi);cp=cos(phi);
% Q(1,1)=-ct;   Q(1,2)=-st;    Q(1,3)=0;   Q(1,4)=-b*ct;
% Q(2,1)=st*cp; Q(2,2)=-ct*cp; Q(2,3)=-sp; Q(2,4)=b*st*cp;
% Q(3,1)=st*sp; Q(3,2)=-ct*sp; Q(3,3)=cp;  Q(3,4)=b*st*sp;
% Q(4,1)=0;     Q(4,2)=2;      Q(4,3)=0;   Q(4,4)=1;
