[0c7481]: devel / example / Ficticious_Domain / Steady_state / no_slip_bc / NS_with_no_slip_bc.m  Maximize  Restore  History

Download this file

147 lines (112 with data), 4.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
## Copyright (C) 2013 Marco Vassallo <gedeone-octave@users.sourceforge.net>
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.
# In this example we solve the 2D NS equation for a the flow around a square
# cylinder in a chennel. In this example, homogeneous no-slip BC are imposed
# on the square sides.
# WARNING: read the warning inside generate_mesh.m
pkg load fem-fenics msh
generate_mesh;
mesh = Mesh (msh19);
plot (mesh);
import_ufl_Problem ('A');
import_ufl_BilinearForm ('B');
import_ufl_BilinearForm ('C');
import_ufl_Functional ('Err_u');
import_ufl_Functional ('Err_p');
import_ufl_FunctionSpace ('C');
TH1 = FunctionSpace ('A', mesh);
TH2 = FunctionSpace ('C', mesh);
bc1 = DirichletBC (TH1, @(x, y, z, n) [0, 0],
[1, 5, 8, 19, 23, 26, 7, 16, 11, 21, 27, 30]);
bc2 = DirichletBC (TH1, @(x, y) [4*(1 - y)*(y) , 0], [4, 13, 20, 28]);
bc = {bc1, bc2};
u0 = Expression ('u0', @(x, y) [0; 0]);
f = Expression ('f', @(x, y) [0; 0]);
nu = 1/40;
nu = Expression ('nu', @(x, y) nu);
sig = Expression ('sig', @(x, y) 0);
a = BilinearForm ('A', TH1, TH1, nu, sig, u0);
L = LinearForm ('A', TH1, f);
[A, ff] = assemble_system (a, L, bc{:});
b = BilinearForm ('B', TH1, TH2);
B = assemble(b);
m = BilinearForm ('C', TH2, TH2, nu);
M = assemble(m);
[x1, y1, v1] = find (A);
[x2, y2, v2] = find (B');
y2 += size (A, 1);
[x3, y3, v3] = find (B);
x3 += size (A, 1);
[x4, y4, v4] = find (M);
x4 += size (A, 1);
y4 += size (A, 1);
C = sparse ([x1; x2; x3],[y1; y2; y3],[v1; v2; v3],
(size (A,1) + size (B,1)), (size (A, 1) + size (B, 1)));
P = sparse ([x1; x4],[y1; y4],[v1; v4],
(size (A,1) + size (B,1)), (size (A, 1) + size (B, 1)));
F = [ff; (zeros (size (B, 1), 1))];
[sol, flag, relres, iter, resvec] = gmres (C, F, 200, 1e-6, 1000, P);
fprintf('Gmres converges in %d Iteration\n',iter (2));
u = Function ('u', TH1, sol(1: (size(A,1))));
p = Function ('p', TH2, sol((size(A,1))+1 : end));
#Compute the initial norm
p0 = Expression ('p0', @(x, y) 0);
E1 = Functional ('Err_u', mesh, u, u0);
normu0 = sqrt (assemble(E1));
E2 = Functional ('Err_p', mesh, p, p0);
normp0 = sqrt (assemble(E2));
u0 = Function('u0',TH1, sol(1: (size(A,1))));
err = 10;
tol = 1e-4;
maxit = 100;
i = 1;
while (err > tol && i < maxit)
a = BilinearForm ('A', TH1, TH1, nu, sig, u0);
[A, ff] = assemble_system (a, L, bc{:});
[x1, y1, v1] = find (A);
C = sparse ([x1; x2; x3],[y1; y2; y3],[v1; v2; v3],
(size (A,1) + size (B,1)), (size (A, 1) + size (B, 1)));
F = [ff; (zeros (size (B, 1), 1))];
[sol, flag, relres, iter, resvec] = gmres (C, F, 100, 1e-6, 2000, P);
fprintf('iteration %d: Gmres converges in %d Iteration\n',i, iter (2));
u = Function ('u', TH1, sol(1: (size(A,1))));
p = Function ('p', TH2, sol((size(A,1))+1 : end));
E1 = Functional ('Err_u', mesh, u, u0);
normu = sqrt (assemble(E1));
E2 = Functional ('Err_p', mesh, p, p0);
normp = sqrt (assemble(E2));
err = normu/normu0 + normp/normp0;
fprintf('the error is %f \n',err);
pause (0);
i++;
u0 = Function ('u0',TH1, sol(1: (size(A,1))));
p0 = Function ('p0', TH2, sol((size(A,1))+1 : end));
save (u, 'velocity');
save (p, 'pressure');
end
plot (u);
save (u, 'velocity');
save (p, 'pressure');
dom = @(x, y) (x <= 1+r)*(x >= 1)*(y >= (0.5 - r/2))*(y <= (0.5 + r/2));
norm_err = 0;
for i = 1:size(msh19.p, 2)
x = msh19.p (1, i);
y = msh19.p (2, i);
if (dom (x, y) == true)
err_L2 = feval (u, [x, y]);
norm_err += err_L2(1).^2 + err_L2(2).^2;
endif
endfor
error_on_bc = sqrt (norm_err)

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks