Ssfem
Ssfem
Ssfem
clear
close all
% q=input(sprintf('please choose you plane problem: strees problem(A) or strain
problem(B)= '),'s');
%% meshing
% a=[19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 4 4 4 4 4];
a=[ 100 100 100 100 100 100] ;
% m=input('please enter number of element in each row = ');
m=50;
le=a(1)/m;
% water_pressure=input('please enter the value of pressure on the side of dam =
');
nnp=(2*m+1)*6+(m+1)*5;
ndof=2*(2*m+1)*6+2*(m+1)*5; %????? ????? ????? ?? ????
for i=1:6
for j=1:2*m
x(i,j+1)=j*a(i)/(2*m);
end
end
for i=1:6
for j=1:(2*m+1)
y(i,j)=i-1;
end
end
for j=1:m
for i=1:5
xx(i,j+1)=j*b(i)/m;
end
end
for i=1:5
for j=1:(m+1)
yy(i,j)=(i-1)+0.5;
end
end
for i=1:5
X((3*m+2)*i-(3*m+1):(3*m+2)*i)=[x(i,:) xx(i,:)]';
end
X((3*m+2)*i+1:(3*m+2)*i+(2*m+1))=x(end,:);
for i=1:5
Y((3*m+2)*i-(3*m+1):(3*m+2)*i)=[y(i,:) yy(i,:)]';
end
Y((3*m+2)*i+1:(3*m+2)*i+(2*m+1))=y(end,:);
%% degree of freedom
% El=zeros(80,8);
A=[1 2 3 2*m+2 2*m+3 3*m+3 3*m+4 3*m+5];
El(1,:)=A;
for j=1:(m*5)-1
cc=mod(j+1,m);
if cc~=1
El(j+1,:)=[(El(j,1))+2 (El(j,2))+2 (El(j,3))+2 (El(j,4))+1 (El(j,5))+1
(El(j,6))+2 (El(j,7))+2 (El(j,8))+2 ];
else
El(j+1,:)=[(El(j,1))+(m+4) (El(j,2))+(m+4) (El(j,3))+(m+4) (El(j,4))+
((0.5*m+1)*4-1) (El(j,5))+((0.5*m+1)*4-1) (El(j,6))+(m+4) (El(j,7))+(m+4)
(El(j,8))+(m+4)];
end
end
for i=1:5*m
ELEMENT(i,:)=[(El(i,1)) (El(i,2)) (El(i,3)) (El(i,5)) (El(i,8)) (El(i,7))
(El(i,6)) (El(i,4)) ];
end
for i=1:m*5
for w=1:8
DI(i,2*w-1)=2*ELEMENT(i,w)-1;
DI(i,2*w)=2*ELEMENT(i,w);
end
end
LM=cell(5*m,1);
for i=1:5*m
LM{i}=ELEMENT(i,:)';
end
%% sketech
X=X';
Y=Y';
Y=Y/5;
XT=X;
YT=Y;
Xgloba=zeros(5*m,8);
Ygloba=zeros(5*m,8);
for i=1:5*m
for j=1:8
Xgloba(i,j)=X(ELEMENT(i,j));
Ygloba(i,j)=Y(ELEMENT(i,j));
end
end
rr=0;
for i=1:5*m
DI_LM{i}=DI(i,:)';
end
DI_LM=DI_LM';
xnod=[X Y];
nnp1 = size(xnod,1); % number of nodal points
% figure; hold on
% for i=1:5*m
% patch(Xgloba(i,:),Ygloba(i,:),'w');
% end
plot(xnod(:,1), xnod(:,2), 'r*');
text(xnod(:,1), xnod(:,2), num2str((1:nnp1)'), 'FontSize',12);
%% Boundray condition
r=0;
for i=1:(2*m+1)*6+(m+1)*5
if X(i)==0
r=r+1;
co_number_y(r,:)=[i];
end
end
r=0;
for i=1:(2*m+1)*6+(m+1)*5
if X(i)==a(1)
r=r+1;
co_number_y_right(r,:)=[i];
end
end
co_number_fixed_y=sort([co_number_y_right(:,1) ; co_number_y(:,1)]);
DI_co_number_fix_y=zeros(1,2*length(co_number_fixed_y));
for i=1:length(co_number_fixed_y)
DI_co_number_fix_y(2*i-1:2*i)=[2*co_number_fixed_y(i)-1
2*co_number_fixed_y(i)];
end
dof_fixed=zeros(2,2*length(co_number_fixed_y));
dof_fixed(1,:)= DI_co_number_fix_y;
%%
nfe=5*m; %element number
IEN=ELEMENT' ;
ned = 2; % number of dof per node
nen = size(IEN,1); % number of element nodes
nfee = size(IEN,2); % number of finite elements
nnpp = size(xnod,1); % number of nodal points
ndoff = ned*nnp; % number of degrees of freedom (dof)
neq = ned*nen; % number of element equations
%% initial deterministic variables and data
X = 1; % x index
Y = 2; % y index
nue = 0.3; % Poisson coefficient
te = 1; % solid thickness (m)
rhoe = 7850; % material density (kg/m^3)
g = 9.81; % gravity (m/s^2)
be = [0; -rhoe*g]; % mass forces vector
%% random field parameters
%Rf.type = Gaussian
mu_E = 1; % mean value of E
std_E = 0.1; % standard deviation of E
b = [100,1]; % correlation length x e y of the field
KLterms = 4; % number of K-L terms
cov_func = @(x1,x2) exp(-abs(x1-x2)/b);
dombounds = {[0 0],[100 1]}; % {[xmin ymin],[xmax ymax]}
[lambda,phi,phi_v] = KL_fredholm_analytical(KLterms, b, dombounds);
for e = 1:nfe
% element stiffness matrices and force vector initialization
Kbar_e = zeros(neq);
fe = zeros(neq,1);
det_Je = zeros(nglp);
% Eq.(3.9.6) Ref.(3)
dx_dxi = sum(ddN_dxi.*xe); dx_deta = sum(ddN_deta.*xe);
dy_dxi = sum(ddN_dxi.*ye); dy_deta = sum(ddN_deta.*ye);
% Reserve memory
N{e,p,q} = zeros(ned,neq);
B{e,p,q} = zeros(3,neq);
for i = 1:nen
% Eq.(3.9.1)-(3.9.2) Ref.(3)
dNi_dx = (+dy_deta*ddN_dxi(i) - dy_dxi*ddN_deta(i))/det_Je(p,q);
dNi_dy = (-dx_deta*ddN_dxi(i) + dx_dxi*ddN_deta(i))/det_Je(p,q);
% Eq.(3.9.6) Ref.(3)
dx_dxi = sum(ddN_dxi.*xe); dx_deta = sum(ddN_deta.*xe);
dy_dxi = sum(ddN_dxi.*ye); dy_deta = sum(ddN_deta.*ye);
Mbar=sparse(ndof,ndof);
for e = 1:nfe
% element stiffness matrices and force vector initialization
Mbar_e = zeros(neq);
area=(max(xe)-min(xe))*(max(ye)-min(ye));
% Reserve memory
N{e,p,q} = zeros(ned,neq);
for i = 1:nen
N{e,p,q}(:,[2*i-1 2*i]) = [ NN(i) 0 % shape
function matrix
0 NN(i) ];
end
end
end
end
%% % global force vector initialization
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
tt =0:0.0125:2;
f = zeros(ndof,length(tt));
for qq=1:length(tt)
for j=1:N_f
D_Force(j)=(TT(j)*velocity(j)) ; % makan har kodam az niroha DAR TOOL POOL
for i=4*m+1:nfe
if ((i-(4*m+1))*le<=D_Force(j))&(D_Force(j)<=(i-4*m)*le) % ba in shart barnameh
mifahmeh ke niro roye kodam eleman ast.
ft(i,j)= force(j);
X_Force(j)=D_Force(j)-(i-(4*m+1))*le ;
break
end
end
% RR=1.6;
end
for e=1:nfe
fe=zeros(neq,1);
for j=1:length(X_Force)
kesi=((2*X_Force(j)/le)-1);
NN=Nshape(kesi,1);
NS=[NN(1) 0 NN(2) 0 NN(3) 0 NN(4) 0 -NN(5) 0 NN(6) 0 NN(7) 0 NN(8) 0
%%mass
Mtotal=cell(P,P);
for i=1:P
for j=1:P
Mtotal{i,j}=sparse(ndof,ndof);
Mtotal{i,i}=Mbar*PsiSqNorm(i);
if i~=j
Mtotal{i,j}=zeros(ndof);
end
end
end
MM=cell2mat(Mtotal);
t2 = toc;
fprintf('Elapsed time (2nd level of assembly) = %g [s]\n\n',t2);
% figure;
% nz = nnz(MM); spy(MM);
% set(gca,'FontSize',15);
% title(sprintf('$*nnz=%g~*P=%g$',nz,P),'FontSize',15); xlabel('');
%
%
K = cell2mat(K);
% figure;
% nz = nnz(K); spy(K);
% set(gca,'FontSize',15);
% title(sprintf('$*nnz=%g~*P=%g$',nz,P),'FontSize',15); xlabel('');
F=cell(P,size(f,2));
for i=2:P
for j=1:size(f,2)
F{1,j}=sparse(f(:,j));
F{i,j}=sparse(f(:,j))*0;
end
end
F=cell2mat(F);
sndof=dof_fixed(1,:);
%%
gaama = 1/2 ;
beta = 1/4 ;
%%
% Constants used in Newmark's integration
a1 = gaama/(beta*dt) ; a2 = 1/(beta*dt^2) ;
a3 = 1/(beta*dt) ; a4 = gaama/beta ;
a5 = 1/(2*beta) ; a6 = (gaama/(2*beta)-1)*dt ;
depl = zeros(n,nt) ;
vel = zeros(n,nt) ;
accl = zeros(n,nt) ;
U = zeros(sdof,nt) ;
% Initial Conditions
depl(:,1) = zeros;
vel(:,1) = zeros ;
% U(:,1) = phi*depl(:,1) ;
P0 = fc{1} ;
accl(:,1) = MM\(fc{1}-C*vel(:,1)-K*depl(:,1)) ;
Kcap = K+a1*C+a2*MM ;
a = a3*MM+a4*C ;
b = a5*MM+a6*C ;
end
for i=1:2*nnp
X_wreck(i)=a((2*i-1),80)*8;
Y_wreck(i)=a((2*i),80)*8;
end
X=XT';
Y=YT';
for i=1:nnp
X_new(i)=X_wreck(i)+X(i);
Y_new(i)=Y_wreck(i)+Y(i);
end
xnod_new=[X_new' Y_new'];
nnp1 = size(xnod_new,1); % number of nodal points
figure;
plot(xnod_new(:,1), xnod_new(:,2), 'r*');
text(xnod_new(:,1), xnod_new(:,2), num2str((1:nnp1)'), 'FontSize',12);
figure;hold on
plot(0:0.0125:2,a(811*2,:))
aforV80_mesh20=a;
plot(0:0.0125:2,a(811*2,:))
Xgloba=zeros(5*m,8);
Ygloba=zeros(5*m,8);
for i=1:5*m
for j=1:8
Xgloba(i,j)=X_new(ELEMENT(i,j));
Ygloba(i,j)=Y_new(ELEMENT(i,j));
end
end
for i=1:5*m
patch(Xgloba(i,:),Ygloba(i,:),'w');
end
scale = 0.1;%/max(abs(d(:,1
xdef=[X_new' Y_new'];
figure; hold on;
for e = 1:nfe
h1 = line(xnod(IEN([1:nen 1],e),1), xnod(IEN([1:nen 1],e),2)); % original
set(h1,'Color', 'Blue','LineStyle','-');
h2 = line(xdef(IEN([1:nen 1],e),1), xdef(IEN([1:nen 1],e),2)); % deformed
set(h2,'Color', 'Red','LineStyle','--');
end
legend('original','deformed')
% axis equal tight;
% leg = legend('Original','Mean response','Location', 'SouthOutside');
% set(leg,'FontSize',25); set(gca,'FontSize',20);
% title(sprintf('Mean response field (scaled %g times)',scale),'FontSize',20);
% xlabel('X','FontSize',20); ylabel('Y','FontSize',20);
%% plot
for i=1:size(f,2)
aho{i}=a(:,i);
end
for i=1:size(f,2)
d{i} = reshape(aho{i},ndof,P);
end
dmesh20=d;
% covariance response (Eq.(4.27) Ref.(1))
cov_u = zeros(ndof);
for j = 0:P-1
cov_u = cov_u + PsiSqNorm(j+1)*d{50}(:,j+1)*d{50}(:,j+1)';
end
% plot standard deviation field
std_u = sqrt(diag(cov_u));
std_ux = std_u(1:2:end);
std_uy = std_u(2:2:end);
% figure;
% % in x
% subplot(121);
% for e = 1:nfe
% fill(xnod(IEN(:,e),1),xnod(IEN(:,e),2),std_ux(IEN(:,e)))
% end
% axis equal tight; colorbar; set(gca,'FontSize',20);
% title('std field in X','FontSize',20);
% xlabel('X','FontSize',20); ylabel('Y','FontSize',20);
% % in y
% subplot(122);
% for e = 1:nfe
% fill(xnod(IEN(:,e),1),xnod(IEN(:,e),2),std_uy(IEN(:,e)))
% end
% axis equal tight; colorbar; set(gca,'FontSize',20);
% title('std field in Y','FontSize',20);
% xlabel('X','FontSize',20); ylabel('Y','FontSize',20);
%% representation of the response displacement
N = 1e4;
u = zeros(ndof, N);
xi = randn(N, KLterms);
% this step is required to evaluate the polynomials (sym form)
xi_v = sym('xi',[1, KLterms],'real'); % create symbolic vector
xi_n = num2cell(xi,1); % 'experimental design'
func = matlabFunction(Psi_s(2:end),'Vars',xi_v); % take out the 1st because it
is always one
Psi = [ones(N,1), func(xi_n{:})]; % then I add the ones here
% evaluate PCE
for j = 0:P-1
msg = fprintf('Evaluating PC expansion %g/%g', j, P);
psi_j = Psi(:,j+1);
d_j = d{51}(:,j+1);
for i = 1:ndof
u(i,:) = u(i,:) + d_j(i)*psi_j';
end
fprintf(repmat('\b',1,msg));
end
% PC approximation of the r.v. at chosen dof
dof = 811*2;
u_PC = u(dof,:); % evaluation
% [n1,x1] = hist(u_PC,ceil(sqrt(N))); % histogram
[PC_pdf,x2,bw1] = ksdensity(u_PC); % pdf
% [PC_cdf,x3] = ecdf(u_PC); % cdf
%[PC_cdf,x3] = ksdensity(u_PC,'function','cdf'); % cdf
U_PC(ii,:)=u_PC;
%
%
% % pdf
figure; hold on;
% % bar(x1,n1/trapz(x1,n1),'c','EdgeColor',[0,0,0]);
plot(x2,PC_pdf,'r--','LineWidth',2); % 'k-+' 'g-.' 'r--'
%
% grid minor; axis tight;
% xlabel('$$\xi$$','FontSize',15);
% ylabel('$$f_u(\xi)$$','FontSize',15);
% legend('Norm Hist','MCS',sprintf('%dth-order PC
approx',p_order),'Location','Best');
% set(gca,'FontSize',15);
% % cdf
% subplot(3,4,5:12);
% % plot(x5,PC_cdf_mcs,'b-','LineWidth',1); hold on;
% plot(x3,PC_cdf,'r--','LineWidth',1);
% grid minor; axis tight;
% xlabel('$$\xi$$','FontSize',15);
% ylabel('$$F_u(\xi)$$','FontSize',15);
% legend('MCS',sprintf('%dth-order PC approx',p_order),'Location','Best');
% set(gca,'FontSize',15);
end
t4 = toc;
fprintf('Elapsed time (1st level of assembly) = %g [s]\n\n', t4);
U_PC_mesh50_cov10_pcorder_step51=U_PC;
pc1=U_PC(1,:);
[PC_pdf,x2,bw1] = ksdensity(pc1);
pc2=U_PC(2,:);
[PC_pd,x3,bw2] = ksdensity(pc2);
% pc3=U_PC(3,:);
% [PC_p,x4,bw3] = ksdensity(pc3);
figure; hold on
plot(x2,PC_pdf,'r--','LineWidth',2); % 'k-+' 'g-.' 'r--'
plot(x3,PC_pd,'b-.','LineWidth',2); % 'k-+' 'g-.' 'r--'
% plot(x4,PC_p,'k:.','LineWidth',2); % 'k-+' 'g-.' 'r--'