% ce445_sdof_anim.m
%Animation function for the lateral vibration of an SDOF building
% base is not fixed, requires base displacement data

clear all; close all; clc; 
set(0,'defaultaxesfontsize',14);
set(0,'defaulttextfontsize',14);


%%  GET USER INPUT and READ DATA
name='Input Parameters';
p1='damping ratio ';
p2='natural period (sec)';
p3='initial displacement (in)';
p4='initial velocity (in/s)';

prompt={p1,p2,p3,p4};
numlines=1;
defaultanswer={'0.05','0.5','0','0'};
answer=inputdlg(prompt, name, numlines,defaultanswer);
zeta=str2double(answer{1}); %use str2num if there is text
Tn=str2double(answer{2});   
xo=str2double(answer{3});  
vo=str2double(answer{4});  

load data   % acc, displ, dt
N=length(acc); 
acc=acc*986; % convert acceleration unit to in/sec^2 
t=(0:N-1)*dt;       % time vector
f=acc;   % in/sec^2 


% state space model
wn=2*pi/Tn;
A = [ 0  1 ; -wn^2  -2*zeta*wn ]; % current A
B = [ 0 ; -1 ]; C = [ 1  0 ]; D = 0; %these don’t change 
sys=ss(A, B, C, D);

% compute response
init=[xo,vo];
x1=lsim(sys,f,t,init); % this is in inches
%Sd1=max(abs(x1)); % peak deformation
x1max=max(abs(x1)); % peak deformation
x2=x1+displ; %total disp= deformation+ground displacement
x2max=max(abs(x2));

%% set geometric parameters
%h=wall heights W=floor width th=floor thickness
h=16;      W=0.4;   th=1;
slabx=[0,0,W,W]; 
slaby=[-th,0,0,-th];%plotting coordinates for floors
basex=[-W/5,-W/5,W+W/5,W+W/5]; 
basey=[-th,th,th,-th];%plotting coordinates for floors
%wall vertical coordinate arrays
s1=linspace(0,h,100);

%%  initialize plots
figure('Units','Normalized','Position',[0.05,0.05,0.85,0.85]);

%deformation
Ha_plot1=subplot('Position',[.55 .7 .4 .15]);
axis([t(1) t(end) -1.2*x1max 1.2*x1max]),
grid on; 
ylabel('x(in)'); 
tit1=['System: \zeta=', num2str(zeta),' and T_n= ', num2str(Tn),' s'];
tit2=['Initial conditions: xo =', num2str(xo),' in and vo = ', num2str(vo),' in/s'];
titstr={tit1;tit2};
title(titstr,'fontsize',14)
Hl_plotx1=line(t(1),x1(1),'Color','b','linewidth',2,'linestyle','-'); %deformation
legend('deformation')

%total displacement 
Ha_plot2=subplot('Position',[.55 .5 .4 .15]);
axis([t(1) t(end) -1.2*x2max 1.2*x2max]),
grid on; xlabel('t (s)'),ylabel('x(in)'); 
titstr=['Deformation and total displacement for \zeta=', num2str(zeta),' and T_n= ', num2str(Tn),' sec'];
Hl_plotx2=line(t(1),x2(1),'Color','b','linewidth',2,'linestyle','-'); % total disp
legend('total displacement')

%ground displacement vs t
pgd=max(abs(displ));
Ha_ploteq1=subplot('Position',[.1 .25 .4 .15]);
axis([t(1) t(end) -1.2*pgd 1.2*pgd]),
grid on; 
ylabel('d_g (in)');
title('Ground motion','fontsize',14)
Hl_ploteq1=line(t(1),displ(1),'Color','r','linewidth',2,'linestyle','-');
legend('displacement')

%ground acceleration vs t
pga=max(abs(acc));
Ha_ploteq2=subplot('Position',[.1 .05 .4 .15]);
axis([t(1) t(end) -1.2*pga 1.2*pga]),
grid on; xlabel('t (s)'),ylabel('a_g(in/s^2)');
Hl_ploteq2=line(t(1),acc(1),'Color','r','linewidth',2,'linestyle','-');
legend('acceleration')

% animation
Ha_anim=subplot('Position',[.1,.5,.45,.45]);  % animated structure
axis off; axis([-W 2*W 0 2*h]),% grid on; axis off;
%draw 1st floor walls
v1=x1(1)*(-2*s1.^3/h^3+3*s1.^2/h^2);
Hl_Lwall1=line(v1,s1,'Color','k','Linewidth',3);
Hl_Rwall1=line(v1+W,s1,'Color','k','Linewidth',3);
Hp_floor1=patch(slabx+x2(1),slaby+h,'b');
Hp_base=patch(basex+displ(1),basey,'r');

%% draw initial plots and hold
drawnow 
%Run animation
xmax=max([x1max,x2max]);
scale=0.2/xmax;
tic;
for n=1:length(t)
    %plots
    set(Hl_plotx1,'XData',t(1:n),'YData',x1(1:n));
    set(Hl_plotx2,'XData',t(1:n),'YData',x2(1:n));
    set(Hl_ploteq1,'XData',t(1:n),'YData',displ(1:n));
    set(Hl_ploteq2,'XData',t(1:n),'YData',acc(1:n));
    v1=scale*x1(n)*(-2*s1.^3/h^3+3*s1.^2/h^2); % wall shapes
    % animation
    set(Hl_Lwall1,'XData',v1+scale*displ(n));
    set(Hl_Rwall1,'XData',v1+scale*displ(n)+W);
    set(Hp_floor1,'XData',slabx+scale*x2(n));
    set(Hp_base,'XData',basex+scale*displ(n));
  
   drawnow
end

