PFV diffusion matrix
From CFD-Wiki
(Difference between revisions)
(Created page with "Function '''DMatW.m''' for pressure-free velocity diffusion matrix <pre> function [Dm,RowNdx,ColNdx]=DMatW(Xe,Elcon,nn2nft) %DMATW - Returns the affine-mapped element diffusion...") |
|||
Line 2: | Line 2: | ||
<pre> | <pre> | ||
- | function [Dm,RowNdx,ColNdx]=DMatW(Xe,Elcon,nn2nft) | + | function [Dm,RowNdx,ColNdx]=DMatW(eu,Xe,Elcon,nn2nft) |
- | % | + | %DMatW - Returns the element diffusion matrix for the Hermite basis |
- | % | + | % functions with 3, 4, or 6 degrees-of-freedom and defined on a |
- | + | % 3-node (triangle) or 4-node (quadrilateral) element by the class | |
- | + | % instance es using Gauss quadrature on the reference element. | |
- | + | ||
- | + | ||
- | % | + | |
- | % Gauss quadrature on the | + | |
- | + | ||
- | + | ||
- | + | ||
% | % | ||
% Usage: | % Usage: | ||
- | % [Dm,Rndx,Cndx] = DMatW(Xe,Elcon,nn2nft) | + | % [Dm,Rndx,Cndx] = DMatW(Xe,Elcon,nn2nft,es) |
+ | % es - reference for basis function definitions | ||
% Xe(1,:) - x-coordinates of corner nodes of element. | % Xe(1,:) - x-coordinates of corner nodes of element. | ||
% Xe(2,:) - y-coordinates of corner nodes of element. | % Xe(2,:) - y-coordinates of corner nodes of element. | ||
- | + | % Elcon - connectivity matrix for this element. | |
- | % Elcon | + | % nn2nft - global DOF and type of DOF at each node |
- | % nn2nft - global | + | |
% | % | ||
- | % Jonas Holdeman, | + | % Indirectly may use (handle passed by eu): |
+ | % GQuad2 - function providing 2D rectangle quadrature rules. | ||
+ | % TQuad2 - function providing 2D triangle quadrature rules. | ||
+ | % | ||
+ | % Jonas Holdeman, January 2007, revised March 2013 | ||
- | % Constants and fixed data | + | % ------------------- Constants and fixed data --------------------------- |
- | + | nnodes = eu.nnodes; % number of nodes per element (4); | |
- | nn=[-1 -1; 1 -1; 1 1; -1 1] | + | nndofs = eu.nndofs; % nndofs = number of dofs per node, (3|6); |
+ | nedofs=nnodes*nndofs; % nndofs = number of dofs per node, | ||
+ | nn = eu.nn; % defines local nodal order, [-1 -1; 1 -1; 1 1; -1 1] | ||
- | % | + | % ------------------------------------------------------------------------ |
- | + | persistent QQDM4; | |
- | + | if isempty(QQDM4) | |
- | if | + | QRorder = 2*(eu.mxpowr-1)+1; % =9 |
- | + | [QQDM4.xa, QQDM4.ya, QQDM4.wt, QQDM4.nq] = eu.hQuad(QRorder); | |
- | + | end % if isempty... | |
- | + | xa = QQDM4.xa; ya = QQDM4.ya; wt = QQDM4.wt; Nq = QQDM4.nq; | |
- | + | % ------------------------------------------------------------------------ | |
- | + | ||
- | + | ||
- | xa= | + | |
- | + | persistent ZZ_SXd; persistent ZZ_SYd; | |
- | + | if (isempty(ZZ_SXd)||isempty(ZZ_SYd)||size(ZZ_SXd,2)~=Nq) | |
- | if (isempty( | + | |
% Evaluate and save/cache the set of shape functions at quadrature pts. | % Evaluate and save/cache the set of shape functions at quadrature pts. | ||
- | + | ZZ_SXd=cell(nnodes,Nq); ZZ_SYd=cell(nnodes,Nq); | |
- | + | for k=1:Nq | |
- | for m=1: | + | for m=1:nnodes |
- | + | [ZZ_SXd{m,k},ZZ_SYd{m,k}]=eu.DS(nn(m,:),xa(k),ya(k)); | |
end | end | ||
end | end | ||
end % if(isempty(*)) | end % if(isempty(*)) | ||
- | % --------------- End fixed data ---------------- | + | % -------------------------- End fixed data ------------------------------ |
- | Ti=cell(4); | + | affine = eu.isaffine(Xe); % affine? |
- | + | %affine = (sum(abs(Xe(:,1)-Xe(:,2)+Xe(:,3)-Xe(:,4)))<4*eps); % affine? | |
- | for m=1: | + | |
- | Jt=Xe* | + | Ti=cell(nnodes); |
- | + | % Jt=[x_q, x_r; y_q, y_r]; | |
- | + | if affine % (J constant) | |
+ | Jt=Xe*eu.Gm(nn(:,:),eu.cntr(1),eu.cntr(2)); | ||
+ | JtiD=[Jt(2,2),-Jt(1,2); -Jt(2,1),Jt(1,1)]; % det(J)*inv(J) | ||
+ | if nndofs==3 | ||
+ | TT=blkdiag(1,JtiD); | ||
+ | elseif nndofs==4 | ||
+ | TT=blkdiag(1,JtiD,Jt(1,1)*Jt(2,2)+Jt(1,2)*Jt(2,1)); | ||
+ | else | ||
+ | T2=[Jt(1,1)^2, 2*Jt(1,1)*Jt(1,2), Jt(1,2)^2; ... % alt | ||
+ | Jt(1,1)*Jt(2,1), Jt(1,1)*Jt(2,2)+Jt(1,2)*Jt(2,1), Jt(1,2)*Jt(2,2); ... | ||
+ | Jt(2,1)^2, 2*Jt(2,1)*Jt(2,2), Jt(2,2)^2]; | ||
+ | TT=blkdiag(1,JtiD,T2); | ||
+ | Bxy=Xe*eu.DGm(nn(:,:),0,0); % Second cross derivatives | ||
+ | TT(5,2)= Bxy(2); | ||
+ | TT(5,3)=-Bxy(1); | ||
+ | end % nndofs... | ||
+ | for m=1:nnodes, Ti{m}=TT; end | ||
+ | Det=Jt(1,1)*Jt(2,2)-Jt(1,2)*Jt(2,1); % Determinant of Jt & J | ||
+ | Jtd=Jt/Det; | ||
+ | Ji=[Jt(2,2),-Jt(2,1); -Jt(1,2),Jt(1,1)]/Det; | ||
+ | else | ||
+ | for m=1:nnodes % Loop over corner nodes | ||
+ | Jt=Xe*eu.Gm(nn(:,:),nn(m,1),nn(m,2)); | ||
+ | JtiD=[Jt(2,2),-Jt(1,2); -Jt(2,1),Jt(1,1)]; % det(J)*inv(J) | ||
+ | if nndofs==3, | ||
+ | TT=blkdiag(1,JtiD); | ||
+ | elseif nndofs==4 | ||
+ | TT=blkdiag(1,JtiD,(Jt(1,1)*Jt(2,2)+Jt(1,2)*Jt(2,1))); | ||
+ | else | ||
+ | T2=[Jt(1,1)^2, 2*Jt(1,1)*Jt(1,2), Jt(1,2)^2; ... | ||
+ | Jt(1,1)*Jt(2,1), Jt(1,1)*Jt(2,2)+Jt(1,2)*Jt(2,1), Jt(1,2)*Jt(2,2);... | ||
+ | Jt(2,1)^2, 2*Jt(2,1)*Jt(2,2), Jt(2,2)^2]; | ||
+ | TT=blkdiag(1,JtiD,T2); | ||
+ | Bxy=Xe*eu.DGm(nn(:,:),nn(m,1),nn(m,2)); % 2nd cross derivatives | ||
+ | TT(5,2)= Bxy(2); | ||
+ | TT(5,3)=-Bxy(1); | ||
+ | end | ||
+ | Ti{m}=TT; | ||
+ | end % Loop m | ||
end | end | ||
- | % | + | % Allocate arrays |
- | + | Dm=zeros(nedofs,nedofs); Sx=zeros(2,nedofs); Sy=zeros(2,nedofs); | |
- | + | ND=1:nndofs; | |
- | Dm=zeros( | + | |
- | + | ||
for k=1:Nq | for k=1:Nq | ||
- | + | if ~affine | |
- | Jt=Xe* | + | Jt=Xe*eu.Gm(nn(:,:),xa(k),ya(k)); % transpose of Jacobian at (xa,ya) |
- | + | Det=Jt(1,1)*Jt(2,2)-Jt(1,2)*Jt(2,1); % Determinant of Jt & J | |
- | + | Jtd=Jt/Det; | |
- | + | Ji=[Jt(2,2),-Jt(2,1); -Jt(1,2),Jt(1,1)]/Det; | |
- | + | end | |
- | + | ||
% Initialize functions and derivatives at the quadrature point (xa,ya). | % Initialize functions and derivatives at the quadrature point (xa,ya). | ||
- | for m=1: | + | for m=1:nnodes |
- | mm= | + | mm=nndofs*(m-1); |
- | + | Sx(:,mm+ND)=Jtd*(Ji(1,1)*ZZ_SXd{m,k}+Ji(1,2)*ZZ_SYd{m,k})*Ti{m}; | |
- | Sx(:,mm+ND) = | + | Sy(:,mm+ND)=Jtd*(Ji(2,1)*ZZ_SXd{m,k}+Ji(2,2)*ZZ_SYd{m,k})*Ti{m}; |
- | Sy(:,mm+ND) = | + | |
end % loop m | end % loop m | ||
Line 87: | Line 116: | ||
end % end loop k over quadrature points | end % end loop k over quadrature points | ||
- | gf=zeros( | + | gf=zeros(nedofs,1); |
m=0; | m=0; | ||
- | for n=1: | + | for n=1:nnodes % Loop over element nodes |
- | gf(m+ND)=(nn2nft( | + | gf(m+ND)=(nn2nft(Elcon(n),1)-1)+ND; % Get global freedoms |
- | m=m+ | + | m=m+nndofs; |
end | end | ||
- | RowNdx=repmat(gf,1, | + | RowNdx=repmat(gf,1,nedofs); % Row indices |
- | ColNdx=RowNdx'; | + | ColNdx=RowNdx'; % Col indices |
- | Dm = reshape(Dm, | + | Dm = reshape(Dm,nedofs*nedofs,1); |
- | RowNdx=reshape(RowNdx, | + | RowNdx=reshape(RowNdx,nedofs*nedofs,1); |
- | ColNdx=reshape(ColNdx, | + | ColNdx=reshape(ColNdx,nedofs*nedofs,1); |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | |||
- | |||
- | |||
- | |||
return; | return; | ||
</pre> | </pre> |
Latest revision as of 15:58, 15 March 2013
Function DMatW.m for pressure-free velocity diffusion matrix
function [Dm,RowNdx,ColNdx]=DMatW(eu,Xe,Elcon,nn2nft) %DMatW - Returns the element diffusion matrix for the Hermite basis % functions with 3, 4, or 6 degrees-of-freedom and defined on a % 3-node (triangle) or 4-node (quadrilateral) element by the class % instance es using Gauss quadrature on the reference element. % % Usage: % [Dm,Rndx,Cndx] = DMatW(Xe,Elcon,nn2nft,es) % es - reference for basis function definitions % Xe(1,:) - x-coordinates of corner nodes of element. % Xe(2,:) - y-coordinates of corner nodes of element. % Elcon - connectivity matrix for this element. % nn2nft - global DOF and type of DOF at each node % % Indirectly may use (handle passed by eu): % GQuad2 - function providing 2D rectangle quadrature rules. % TQuad2 - function providing 2D triangle quadrature rules. % % Jonas Holdeman, January 2007, revised March 2013 % ------------------- Constants and fixed data --------------------------- nnodes = eu.nnodes; % number of nodes per element (4); nndofs = eu.nndofs; % nndofs = number of dofs per node, (3|6); nedofs=nnodes*nndofs; % nndofs = number of dofs per node, nn = eu.nn; % defines local nodal order, [-1 -1; 1 -1; 1 1; -1 1] % ------------------------------------------------------------------------ persistent QQDM4; if isempty(QQDM4) QRorder = 2*(eu.mxpowr-1)+1; % =9 [QQDM4.xa, QQDM4.ya, QQDM4.wt, QQDM4.nq] = eu.hQuad(QRorder); end % if isempty... xa = QQDM4.xa; ya = QQDM4.ya; wt = QQDM4.wt; Nq = QQDM4.nq; % ------------------------------------------------------------------------ persistent ZZ_SXd; persistent ZZ_SYd; if (isempty(ZZ_SXd)||isempty(ZZ_SYd)||size(ZZ_SXd,2)~=Nq) % Evaluate and save/cache the set of shape functions at quadrature pts. ZZ_SXd=cell(nnodes,Nq); ZZ_SYd=cell(nnodes,Nq); for k=1:Nq for m=1:nnodes [ZZ_SXd{m,k},ZZ_SYd{m,k}]=eu.DS(nn(m,:),xa(k),ya(k)); end end end % if(isempty(*)) % -------------------------- End fixed data ------------------------------ affine = eu.isaffine(Xe); % affine? %affine = (sum(abs(Xe(:,1)-Xe(:,2)+Xe(:,3)-Xe(:,4)))<4*eps); % affine? Ti=cell(nnodes); % Jt=[x_q, x_r; y_q, y_r]; if affine % (J constant) Jt=Xe*eu.Gm(nn(:,:),eu.cntr(1),eu.cntr(2)); JtiD=[Jt(2,2),-Jt(1,2); -Jt(2,1),Jt(1,1)]; % det(J)*inv(J) if nndofs==3 TT=blkdiag(1,JtiD); elseif nndofs==4 TT=blkdiag(1,JtiD,Jt(1,1)*Jt(2,2)+Jt(1,2)*Jt(2,1)); else T2=[Jt(1,1)^2, 2*Jt(1,1)*Jt(1,2), Jt(1,2)^2; ... % alt Jt(1,1)*Jt(2,1), Jt(1,1)*Jt(2,2)+Jt(1,2)*Jt(2,1), Jt(1,2)*Jt(2,2); ... Jt(2,1)^2, 2*Jt(2,1)*Jt(2,2), Jt(2,2)^2]; TT=blkdiag(1,JtiD,T2); Bxy=Xe*eu.DGm(nn(:,:),0,0); % Second cross derivatives TT(5,2)= Bxy(2); TT(5,3)=-Bxy(1); end % nndofs... for m=1:nnodes, Ti{m}=TT; end Det=Jt(1,1)*Jt(2,2)-Jt(1,2)*Jt(2,1); % Determinant of Jt & J Jtd=Jt/Det; Ji=[Jt(2,2),-Jt(2,1); -Jt(1,2),Jt(1,1)]/Det; else for m=1:nnodes % Loop over corner nodes Jt=Xe*eu.Gm(nn(:,:),nn(m,1),nn(m,2)); JtiD=[Jt(2,2),-Jt(1,2); -Jt(2,1),Jt(1,1)]; % det(J)*inv(J) if nndofs==3, TT=blkdiag(1,JtiD); elseif nndofs==4 TT=blkdiag(1,JtiD,(Jt(1,1)*Jt(2,2)+Jt(1,2)*Jt(2,1))); else T2=[Jt(1,1)^2, 2*Jt(1,1)*Jt(1,2), Jt(1,2)^2; ... Jt(1,1)*Jt(2,1), Jt(1,1)*Jt(2,2)+Jt(1,2)*Jt(2,1), Jt(1,2)*Jt(2,2);... Jt(2,1)^2, 2*Jt(2,1)*Jt(2,2), Jt(2,2)^2]; TT=blkdiag(1,JtiD,T2); Bxy=Xe*eu.DGm(nn(:,:),nn(m,1),nn(m,2)); % 2nd cross derivatives TT(5,2)= Bxy(2); TT(5,3)=-Bxy(1); end Ti{m}=TT; end % Loop m end % Allocate arrays Dm=zeros(nedofs,nedofs); Sx=zeros(2,nedofs); Sy=zeros(2,nedofs); ND=1:nndofs; for k=1:Nq if ~affine Jt=Xe*eu.Gm(nn(:,:),xa(k),ya(k)); % transpose of Jacobian at (xa,ya) Det=Jt(1,1)*Jt(2,2)-Jt(1,2)*Jt(2,1); % Determinant of Jt & J Jtd=Jt/Det; Ji=[Jt(2,2),-Jt(2,1); -Jt(1,2),Jt(1,1)]/Det; end % Initialize functions and derivatives at the quadrature point (xa,ya). for m=1:nnodes mm=nndofs*(m-1); Sx(:,mm+ND)=Jtd*(Ji(1,1)*ZZ_SXd{m,k}+Ji(1,2)*ZZ_SYd{m,k})*Ti{m}; Sy(:,mm+ND)=Jtd*(Ji(2,1)*ZZ_SXd{m,k}+Ji(2,2)*ZZ_SYd{m,k})*Ti{m}; end % loop m Dm = Dm+(Sx'*Sx+Sy'*Sy)*(wt(k)*Det); end % end loop k over quadrature points gf=zeros(nedofs,1); m=0; for n=1:nnodes % Loop over element nodes gf(m+ND)=(nn2nft(Elcon(n),1)-1)+ND; % Get global freedoms m=m+nndofs; end RowNdx=repmat(gf,1,nedofs); % Row indices ColNdx=RowNdx'; % Col indices Dm = reshape(Dm,nedofs*nedofs,1); RowNdx=reshape(RowNdx,nedofs*nedofs,1); ColNdx=reshape(ColNdx,nedofs*nedofs,1); return;