Azzera filtri
Azzera filtri

I wrote code on MATLAB to solve Stokes equations using Bramble and Pasciak method but I got strange values of error for pressure p

3 visualizzazioni (ultimi 30 giorni)
%Define problem parameters
mu = 1.0; % viscosity
%f = [1; 0; 1; 2]; % forcing term for velocity
%g = [2; 1]; % forcing term for pressure
%% Mesh creation
h = 0.5; % discretization step
[node,elem] = squaremesh([-1,1,-1,1],h);
Unrecognized function or variable 'squaremesh'.
subplot(1,2,1); showmesh(node,elem);
N = size(node,1);
NTc = size(elem,1);
% Uniform refinement to get a fine grid
[node,elem] = uniformrefine(node,elem);
% Uniform refinement to get a fine grid
subplot(1,2,2); showmesh(node,elem);
%%extract boundary conditions
[Dirichlet, bdFlag] = ExtractBoundaryEdges(elem, node);
%% Load Stokes Data Partial Differential Equations
%Different velocity and pressure equation models
pde = StokesModelMustafaNew;%StokesModelMustafa;%StokesModelMustafaNew;%Stokesdata3;%StokesModelMustafa2;%StokesModelMustafaSin;%;
option.elemType = 'isoP2P0';
option.fquadorder = 3;
tolerance = 1e-3;
max_iterations = 1000;
mesh.node = node;
mesh.elem = elem;
mesh.bdFlag = bdFlag;
%The following line of code is only run to get the eqn (equation structure) easier without defining it every time
[soln,eqn,info] = StokesisoP2P0(node,elem,bdFlag,pde, option);
%% Call the solvers and calulate L2 errors, run time and outcome as well as convergence rate
% [soln,eqn,err,time,solver] = femStokes(mesh,pde,option);
% return
uI = pde.exactu([node; (node(eqn.edge(:,1),:)+node(eqn.edge(:,2),:))/2]);
uI = reshape(uI, [2*length(uI),1]);
pI = pde.exactp([node(elem(:,1),1) node(elem(:,3),2)]);
% Define matrix H
%H = [1 0 0 2; 0 2 1 0; 2 3 1 0; 0 1 0 1];
% Define matrix A
%A = H * H';
% Define matrix B
%B = [1 2 1 0; 0 1 0 0];
% Assemble the finite element system
%[A, B, ~, ~] = assempde(pde,node,elem,'f',f,'g',g,'mu',mu);
tic;
% Assemble the Stokes matrix
N = size(eqn.A,1);
M = size(eqn.B,1);
%StokesMatrix = [eqn.A, eqn.B'; eqn.B, sparse(M,M)];
% Define dimensions of eqn.B'
eqn.B_transpose = eqn.B'; % Transpose of eqn.B
% Construct a sparse MxM matrix (adjust M to your desired size)
M = size(eqn.B, 1); % Number of columns in eqn.B (or rows in eqn.B')
sparse_M_M = sparse(M, M);
% Check dimensions of matrices involved in the problematic line
size_eqn.B = size(eqn.B);
size_eqn.A = size(eqn.A);
% Perform the matrix multiplication explicitly and verify dimensions
result = 2 * eqn.B / (eqn.A)* eqn.B_transpose;
size_result = size(result);
% Concatenate eqn.A, eqn.B_transpose, eqn.B, and sparse_M_M
StokesMatrix = [eqn.A, eqn.B_transpose; eqn.B, sparse_M_M];
% Define matrix M
%M = [2*eye(size(eqn.A,1)) 2*(eqn.A)\eqn.B_transpose; eqn.B 2*eqn.B*(eqn.A)\eqn.B_transpose];
M_top = [2 * eye(size(eqn.A, 1)), 2 * (eqn.A\eqn.B_transpose)];
M_bottom = [eqn.B, 2 * eqn.B * (eqn.A \ eqn.B_transpose)];
M = [M_top; M_bottom];
% Define matrix F
F = [2 * eqn.A \ eqn.f; 2 * eqn.B * (eqn.A \ eqn.f) - eqn.g];
% Define the right-hand side
rhs = [eqn.f; eqn.g];
x0 = ones(size(F));
% Solve the Stokes system
solution = StokesMatrix \ rhs;
sol = M \ F;
% Extract the velocity and pressure components from the solution
u = solution(1:N);
p = solution(N+1:end);
% Calculate the residual R
R = F - M * sol;
% Define matrix C
C = [0.5 * eqn.A zeros(size(transpose(eqn.B))); zeros(size(eqn.B)) eye(size(eqn.B,1))];
% Define an appropriate non-zero vector x
[res_conjgrad, iterCg, relresCg, H1ErrUCg, L2ErrPCg, L2ErrGradUandPCg] = conjgradNew3(eqn.A, C,F ,[eqn.f; eqn.g],x0 , max_iterations, tolerance, u, p, h, M);
for i = 1:iterCg
if H1ErrUCg(i) == 0 && i == 1
H1ErrUCg(i) = max(H1ErrUCg);
elseif H1ErrUCg(i) == 0
H1ErrUCg(i) = H1ErrUCg(i-1);
end
end
for i = 1:iterCg
if L2ErrPCg(i) == 0 && i == 1
L2ErrPCg(i) = max(L2ErrPCg);
elseif L2ErrPCg(i) == 0
L2ErrPCg(i) = L2ErrPCg(i-1);
end
end
for i = 1:iterCg
if L2ErrGradUandPCg(i) == 0 && i == 1
L2ErrGradUandPCg(i) = max(L2ErrGradUandPCg);
elseif L2ErrGradUandPCg(i) == 0
L2ErrGradUandPCg(i) = L2ErrGradUandPCg(i-1);
end
end
p = res_conjgrad(size(eqn.A,1)+1:end);
u = res_conjgrad(1:size(eqn.A,1));
time_BPCG_finish = toc;
function [inner_pro] = inner_x_y(C, x,y)
%C = [A 0 ; 0 1]
%(x,y) = (Cx,y) = (Ax_1,y_1) + (x_2,y_2) = transpose(x_1)*A*y_1 +transpose(x_2)*y_2
%trans(y_1)*x_2
inner_pro = transpose(y) * C * x;
end
  17 Commenti
Walter Roberson
Walter Roberson il 13 Gen 2024
You can copy my entire post to your system and test it locally.
I suspect that something you have installed does not match the latest code at https://github.com/lyc102/ifem
Mostafa Kassoum
Mostafa Kassoum il 23 Gen 2024
I copied your entire post and runned it but I got also a big value of L2 error 11.3255614294248 11.3251245255107
As you can see they are almost equal to the values I obtained previously. Where do you think the mistake is?

Accedi per commentare.

Risposte (0)

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by