-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror_estimation.lua
More file actions
245 lines (196 loc) · 6.49 KB
/
error_estimation.lua
File metadata and controls
245 lines (196 loc) · 6.49 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
--------------------------------------------------------------
-- Solving a Poisson problem on parallel adaptive multigrid --
-- hierarchies using residual-based error estimation. --
-- --
-- author: Markus Breit --
-- date: 2019-04-02 --
--------------------------------------------------------------
ug_load_script("ug_util.lua")
ug_load_script("util/load_balancing_util.lua")
-- dimension of the problem
dim = util.GetParamNumber("-dim", 2)
if dim ~= 2 and dim ~= 3 then
print("Dimension " .. dim .. " not supported. Only 2 and 3 are. Aborting.")
exit()
end
InitUG(dim, AlgebraType("CPU", 1))
-------------------------------------
-- process command line parameters --
-------------------------------------
-- grid file
gridName = util.GetParam("-grid", "Examples/grids/laplace_sample_grid_"..dim.."d.ugx")
-- adaption parameters
maxLvl = util.GetParamNumber("-maxLvl", 15)
refTol = util.GetParamNumber("-refTol", 5e-2, "refinement tolerance")
-------------------------------------------
-- create domain and approximation space --
-------------------------------------------
-- load and check domain
dom = Domain()
LoadDomain(dom, gridName)
if util.CheckSubsets(dom, {"Inner", "Boundary"}) == false then
print("Subset check failed. Aborting...")
exit()
end
-- approx space
approxSpace = ApproximationSpace(dom)
approxSpace:add_fct("c", "Lagrange", 1)
approxSpace:init_levels()
approxSpace:init_surfaces()
approxSpace:init_top_surface()
approxSpace:print_statistic()
-- prepare refiner
refiner = HangingNodeDomainRefiner(dom)
--------------------
-- discretizatiom --
--------------------
-- define source and boundary terms
function source2d(x, y, t)
local r = math.sqrt(x*x + y*y)
if r < 1e-9 then return 0.0 end
return -200*(1.0 + math.exp(-200*r+160) - 200*r + 200*math.exp(-200*r+160)*r)
* math.exp(-200*r+160) * math.pow(1.0 + math.exp(-200*r+160),-3) / r
end
function dirichletBnd2d(x, y, t)
local r = math.sqrt(x*x + y*y)
return true, 1.0 / (1.0 + math.exp(-200*(r-0.8)))
end
function source3d(x, y, z, t)
local r = math.sqrt(x*x + y*y + z*z)
if r < 1e-9 then return 0.0 end
return -400*(1.0 + math.exp(-200*r+160) - 100*r + 100*math.exp(-200*r+160)*r)
* math.exp(-200*r+160) * math.pow(1.0 + math.exp(-200*r+160),-3) / r
end
function dirichletBnd3d(x, y, z, t)
local r = math.sqrt(x*x + y*y + z*z)
return true, 1.0 / (1.0 + math.exp(-200*(r-0.8)))
end
-- set up element discretization and constraints
elemDisc = ConvectionDiffusion("c", "Inner", "fv1")
elemDisc:set_diffusion(1.0)
elemDisc:set_source("source"..dim.."d")
dirichletBND = DirichletBoundary()
dirichletBND:add("dirichletBnd"..dim.."d", "c", "Boundary")
hangingNodeConstraint = SymP1Constraints()
-- setup error estimators
ee = SideAndElemErrEstData(2, 2, "Inner")
eeMult = MultipleSideAndElemErrEstData(approxSpace)
eeMult:add(ee, "c")
elemDisc:set_error_estimator(ee)
dirichletBND:set_error_estimator(eeMult)
-- put together global domain discretization
domainDisc = DomainDiscretization(approxSpace)
domainDisc:add(elemDisc)
domainDisc:add(dirichletBND)
domainDisc:add(hangingNodeConstraint)
-- prepare linear operator
linOp = AssembledLinearOperator()
linOp:set_discretization(domainDisc)
--------------------
-- prepare solver --
--------------------
-- GMG setup
gmg = GeometricMultiGrid(approxSpace)
gmg:set_discretization(domainDisc)
gmg:set_base_level(0)
gmg:set_base_solver(LU())
gmg:set_gathered_base_solver_if_ambiguous(true)
gmg:set_smoother(GaussSeidel())
gmg:set_num_presmooth(3)
gmg:set_num_postsmooth(3)
gmg:set_smooth_on_surface_rim(true)
gmg:set_cycle_type(1) -- V-cycle
gmg:set_rap(true)
-- linear solver
linSolver = LinearSolver()
linSolver:set_preconditioner(gmg)
linSolver:set_convergence_check(ConvCheck(100, 1e-12, 1e-12))
------------------
-- solve system --
------------------
-- in parallel environments, use a load balancer to distribute the grid
balancer.partitioner = "parmetis"
loadBalancer = balancer.CreateLoadBalancer(dom)
-- prepare grid functions for solution and rhs
u = GridFunction(approxSpace)
u:set(0.0)
b = GridFunction(approxSpace)
-- prepare adaptive remeshing
refStrat = StdRefinementMarking(refTol, maxLvl)
coarseStrat = StdCoarseningMarking(refTol, 8.0)
-- prepare output of error indicators
approxSpace_vtk = ApproximationSpace(dom)
approxSpace_vtk:add_fct("eta_squared", "piecewise-constant")
u_vtk = GridFunction(approxSpace_vtk)
out_error = VTKOutput()
out_error:clear_selection()
out_error:select_all(false)
out_error:select_element("eta_squared", "error")
-- start adaptation loop
breakAtEndOfLoop = false
for i = 1, maxLvl do
print(" ####### START adaption " .. i .." #######")
-- rebalance
if loadBalancer ~= nil then
loadBalancer:rebalance()
loadBalancer:create_quality_record("step "..i..":")
end
-- init operator
linOp:init_op_and_rhs(b)
-- set dirichlet values in solution
linOp:set_dirichlet_values(u)
-- init solver for linear operator
if not linSolver:init(linOp) then
print("Solver initialization failed. Aborting")
exit()
end
-- apply solver
if not linSolver:apply(u,b) then
print("Solver application failed. Aborting")
exit()
end
-- export solution to vtk
WriteGridFunctionToVTK(u, "solution_"..i)
-- estimate error and mark
domainDisc:calc_error(u, u_vtk)
WriteGridFunctionToVTK(u_vtk, "error_indicators_"..i)
if i < maxLvl then
-- coarsen if possible
domainDisc:mark_with_strategy(refiner, coarseStrat)
numElemBeforeCoarsening = dom:domain_info():num_elements()
numCoarsen = refiner:num_marked_elements()
if numCoarsen >= numElemBeforeCoarsening/5 then
print("Coarsening.")
refiner:coarsen()
domainDisc:calc_error(u)
end
refiner:clear_marks()
-- refine if required
domainDisc:mark_with_strategy(refiner, refStrat)
if refiner:num_marked_elements() > 0 then
print("Refining.")
refiner:refine()
else
print("Solution has reached desired tolerance.")
breakAtEndOfLoop = true
end
refiner:clear_marks()
-- print approximation space statistics
approxSpace:print_statistic()
end
print(" ####### END adaption " .. i .." #######")
if breakAtEndOfLoop then
break
end
end
--[[
-- save final grid hierarchy
offset = 3.0
if dim == 2 then offset = 0.4 end
SaveGridHierarchyTransformed(dom:grid(), dom:subset_handler(),
"grid_hierarchy_transformed_p" .. ProcRank() .. ".ugx", offset)
--]]
-- print distribution quality statistics
if loadBalancer ~= nil then
loadBalancer:print_quality_records()
end