Skip to content

Commit 9e5b343

Browse files
authored
Merge pull request #102 from mberto79/HM/Fix-L2-norm-residual-calculation
Fix residual calculation
2 parents 14f4bc9 + b2f88e1 commit 9e5b343

File tree

6 files changed

+51
-43
lines changed

6 files changed

+51
-43
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Fixed
1313
* Add implementation of `Periodic` boundaries to handle the implicit source term - fixes operation of models that use `Si` terms [#95](@ref)
1414
* Fixed implementation of implicit boundaries in [#96](@ref) which where missing atomics [#100](@ref)
15+
* Fixed calculation of the residuals to use the relative residual norm, norm(b - Ax)/norm(b), the numerator in this expression was calculated incorrectly previously, giving a 1/sqrt(n) relation (where n is the number of cells in the grid). whilst the operation of the solvers remains the same, user may find that convergence criteria may need to be increased (specially for larger grids)[#102](@ref)
1516

1617
### Changed
1718
* Improved stability of `Periodic` boundaries by making the implementation fully implicit [#96](@ref)

examples/2D_cylinder_heated.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mesh_dev = adapt(backend, mesh)
2020

2121
velocity = [0.5, 0.0, 0.0]
2222
noSlip = [0.0, 0.0, 0.0]
23-
nu = 1e-2
23+
nu = 1e-4
2424
Re = (0.2*velocity[1])/nu
2525
gamma = 1.4
2626
cp = 1005.0
@@ -46,24 +46,24 @@ BCs = assign(
4646
(
4747
U = [
4848
Dirichlet(:inlet, velocity),
49-
Neumann(:outlet, 0.0),
49+
Zerogradient(:outlet),
5050
Wall(:cylinder, noSlip),
5151
Symmetry(:bottom, 0.0),
5252
Symmetry(:top, 0.0)
5353
],
5454
p = [
55-
Neumann(:inlet, 0.0),
55+
Zerogradient(:inlet),
5656
Dirichlet(:outlet, pressure),
5757
Wall(:cylinder, 0.0),
58-
Neumann(:bottom, 0.0),
59-
Neumann(:top, 0.0)
58+
Zerogradient(:bottom),
59+
Zerogradient(:top)
6060
],
6161
h = [
6262
FixedTemperature(:inlet, T=300.0, Enthalpy(cp=cp, Tref=288.15)),
63-
Neumann(:outlet, 0.0),
63+
Zerogradient(:outlet),
6464
FixedTemperature(:cylinder, T=330.0, Enthalpy(cp=cp, Tref=288.15)),
65-
Neumann(:bottom, 0.0),
66-
Neumann(:top, 0.0)
65+
Zerogradient(:bottom),
66+
Zerogradient(:top)
6767
]
6868
)
6969
)

examples/2D_cylinder_heated_unsteady.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,24 @@ BCs =assign(
4545
(
4646
U = [
4747
Dirichlet(:inlet, velocity),
48-
Neumann(:outlet, 0.0),
48+
Zerogradient(:outlet, 0.0),
4949
Wall(:cylinder, noSlip),
50-
Neumann(:bottom, 0.0),
51-
Neumann(:top, 0.0)
50+
Zerogradient(:bottom, 0.0),
51+
Zerogradient(:top, 0.0)
5252
],
5353
p = [
54-
Neumann(:inlet, 0.0),
54+
Zerogradient(:inlet, 0.0),
5555
Dirichlet(:outlet, pressure),
56-
Neumann(:cylinder, 0.0),
57-
Neumann(:bottom, 0.0),
58-
Neumann(:top, 0.0)
56+
Zerogradient(:cylinder, 0.0),
57+
Zerogradient(:bottom, 0.0),
58+
Zerogradient(:top, 0.0)
5959
],
6060
h = [
6161
FixedTemperature(:inlet, T=temp, Enthalpy(cp=cp, Tref=288.15)),
62-
Neumann(:outlet, 0.0),
62+
Zerogradient(:outlet, 0.0),
6363
FixedTemperature(:cylinder, T=330.0, Enthalpy(cp=cp, Tref=288.15)),
64-
Neumann(:bottom, 0.0),
65-
Neumann(:top, 0.0)
64+
Zerogradient(:bottom, 0.0),
65+
Zerogradient(:top, 0.0)
6666
]
6767
)
6868
)

examples/2D_flatplate_lowRe.jl

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ BCs = assign(
3535
Dirichlet(:inlet, velocity),
3636
Zerogradient(:outlet),
3737
Wall(:wall, [0.0, 0.0, 0.0]),
38-
Extrapolated(:top)
39-
# Zerogradient(:top)
38+
# Extrapolated(:top)
39+
Zerogradient(:top)
4040
# Symmetry(:top)
4141
],
4242
p = [
@@ -51,16 +51,16 @@ BCs = assign(
5151
Dirichlet(:inlet, k_inlet),
5252
Zerogradient(:outlet),
5353
Dirichlet(:wall, 0.0),
54-
Extrapolated(:top)
55-
# Zerogradient(:top)
54+
# Extrapolated(:top)
55+
Zerogradient(:top)
5656
# Symmetry(:top)
5757
],
5858
omega = [
5959
Dirichlet(:inlet, ω_inlet),
6060
Zerogradient(:outlet),
6161
OmegaWallFunction(:wall),
62-
Extrapolated(:top)
63-
# Zerogradient(:top)
62+
# Extrapolated(:top)
63+
Zerogradient(:top)
6464
# Symmetry(:top)
6565
],
6666
nut = [
@@ -78,7 +78,8 @@ schemes = (
7878
U = Schemes(divergence=Upwind),
7979
p = Schemes(divergence=Upwind),
8080
k = Schemes(divergence=Upwind),
81-
omega = Schemes(divergence=Upwind)
81+
omega = Schemes(divergence=Upwind),
82+
y = Schemes()
8283
)
8384

8485

@@ -87,29 +88,36 @@ solvers = (
8788
solver = Bicgstab(), # Bicgstab(), Gmres()
8889
preconditioner = Jacobi(),
8990
convergence = 1e-7,
90-
relax = 0.7,
91+
relax = 0.8,
9192
),
9293
p = SolverSetup(
9394
solver = Cg(), # Bicgstab(), Gmres()
9495
preconditioner = Jacobi(),
9596
convergence = 1e-7,
9697
relax = 0.2,
9798
),
99+
y = SolverSetup(
100+
solver = Cg(), # Bicgstab(), Gmres()
101+
preconditioner = Jacobi(),
102+
convergence = 1e-7,
103+
relax = 0.9,
104+
itmax = 5000
105+
),
98106
k = SolverSetup(
99107
solver = Bicgstab(), # Bicgstab(), Gmres()
100108
preconditioner = Jacobi(),
101109
convergence = 1e-7,
102-
relax = 0.7,
110+
relax = 0.6,
103111
),
104112
omega = SolverSetup(
105113
solver = Bicgstab(), # Bicgstab(), Gmres()
106114
preconditioner = Jacobi(),
107115
convergence = 1e-7,
108-
relax = 0.7,
116+
relax = 0.6,
109117
)
110118
)
111119

112-
runtime = Runtime(iterations=1000, write_interval=100, time_step=1)
120+
runtime = Runtime(iterations=5000, write_interval=100, time_step=1)
113121

114122
config = Configuration(
115123
solvers=solvers, schemes=schemes, runtime=runtime, hardware=hardware, boundaries=BCs)

examples/3D_cascade_periodic.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,25 @@ solvers = (
7676
U = SolverSetup(
7777
solver = Bicgstab(), #Cg(), # Bicgstab(), Gmres(), #Cg()
7878
preconditioner = Jacobi(),
79-
convergence = 1e-8,
79+
convergence = 1e-7,
8080
# # transient setup
8181
# atol = 1e-6,
8282
# relax=1
8383

8484
# steady setup
85-
relax = 0.6,
85+
relax = 0.7,
8686
rtol = 1e-3
8787
),
8888
p = SolverSetup(
8989
solver = Cg(), #Gmres(), #Cg(), # Bicgstab(), Gmres()
9090
preconditioner = Jacobi(),
91-
convergence = 1e-8,
91+
convergence = 1e-7,
9292
# # transient setup
9393
# atol = 1e-6,
9494
# relax=1
9595

9696
# steady setup
97-
relax = 0.15,
97+
relax = 0.3,
9898
rtol = 1e-3
9999
)
100100
)
@@ -113,9 +113,9 @@ initialise!(model.momentum.p, 0.0)
113113

114114
residuals = run!(model, config, output=OpenFOAM()) # 353 iterations!
115115

116-
# using Plots
117-
# fig = plot(; xlims=(0,runtime.iterations), ylims=(1e-10, 1e-4))
118-
# plot!(fig, 1:runtime.iterations, residuals.Ux, yscale=:log10, label="Ux")
119-
# plot!(fig, 1:runtime.iterations, residuals.Uy, yscale=:log10, label="Uy")
120-
# plot!(fig, 1:runtime.iterations, residuals.p, yscale=:log10, label="p")
121-
# fig
116+
using Plots
117+
fig = plot(; xlims=(0,runtime.iterations), ylims=(1e-10, 1e-1))
118+
plot!(fig, 1:runtime.iterations, residuals.Ux, yscale=:log10, label="Ux")
119+
plot!(fig, 1:runtime.iterations, residuals.Uy, yscale=:log10, label="Uy")
120+
plot!(fig, 1:runtime.iterations, residuals.p, yscale=:log10, label="p")
121+
fig

src/Solve/Solve_1_api.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,12 @@ function residual(eqn, component, config)
406406

407407
# Previous definition
408408
Fx .= A * values
409-
# @inbounds @. R = (b - Fx)^2
410409
xcal_foreach(R, config) do i
411-
R[i] = (b[i] - Fx[i])^2
410+
@inbounds R[i] = (b[i] - Fx[i])^2
412411
end
413412
normb = norm(b)
414-
denominator = ifelse(normb>0,normb, 1)
415-
Residual = sqrt(mean(R)) / denominator
413+
denominator = ifelse(normb > eps(normb), normb, one(normb))
414+
Residual = sqrt(sum(R)) / denominator
416415
return Residual
417416
end
418417

0 commit comments

Comments
 (0)