-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathelder_adapt.lua
More file actions
436 lines (350 loc) · 14.1 KB
/
elder_adapt.lua
File metadata and controls
436 lines (350 loc) · 14.1 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
----------------------------------------------------------
--
-- Lua - Script to perform the Elder-Problem on an adaptive grid
--
-- Author: Andreas Vogel, Sebastian Reiter
--
----------------------------------------------------------
PrintBuildConfiguration()
ug_load_script("../scripts/ug_util.lua")
ug_load_script("util/load_balancing_util.lua")
ug_load_script("util/solver_util.lua")
dim = util.GetParamNumber("-dim", 2)
-- default parameters
if dim == 2 then
gridName = "grids/elder_quads_8x2.ugx"
zOutputTransform = 10
defRefTol = 0.1
defCoarsenTol = 0.01
elseif dim == 3 then
gridName = "grids/elder_hex_8x8x2.ugx"
zOutputTransform = 300
defRefTol = 2.5
defCoarsenTol = 0.25
else print("Dimension "..dim.." not supported"); exit(); end
numTimeSteps = util.GetParamNumber("-numTimeSteps", 100)
maxLvl = util.GetParamNumber("-maxLvl", 5)
-- timestep in seconds: 3153600 sec = 0.1 year
--dt = util.GetParamNumber("-dt", 3.1536e6)
dt = util.GetParamNumber("-dt", 2.e6)
minStepSize = util.GetParamNumber("-minStepSize", dt/40);
stepReducFac = util.GetParamNumber("-stepReducFac", 0.25);
verbose = util.HasParamOption("-verbose");
refTol = util.GetParamNumber("-refTol", defRefTol, "refinement tolerance for error-indicator. -1 -> no refs")
coarsenTol = util.GetParamNumber("-coarsenTol", defCoarsenTol, "coarsening tolerance for error-indicator. -1 -> no coarsening")
stepsBetweenRedist = util.GetParamNumber("-stepsBetweenRedist", 1, "Number of timesteps before a new redistribution is attempted - only for parallel use. Default is 1")
numDampSteps = util.GetParamNumber("-numDampSteps", 3, "Number of timesteps in which dt is damped. Damping is reduced with each time step. Default is 3")
maxDamping = util.GetParamNumber("-maxDamping", 0.1, "The maximal damping with which dt is damped during damp-steps. Damping is reduced with each time step. Default is 0.1")
discardSolution = util.HasParamOption("-discardSolution", "solution won't be saved to a file.")
writeGridLayout = util.HasParamOption("-writeGridLayout", "adapted grid layouts will be written to a file.")
stepsBetweenWrite = util.GetParamNumber("-stepsBetweenWrite", 1, "Number of timesteps until the next solution or grid-layout is written to a file. Default is 1")
stepsBetweenRedist = math.floor(stepsBetweenRedist)
if(stepsBetweenRedist < 1) then stepsBetweenRedist = 1; end
balancer.partitioner = "dynBisection"
balancer.ParseParameters()
balancer.PrintParameters()
print("")
util.CheckAndPrintHelp()
--------------------------------------------------------------------------------
-- Setup Domain and Approximation Space
--------------------------------------------------------------------------------
InitUG(dim, AlgebraType("CPU", 1))
-- Create a domain for the specified grid file
neededSubsets = {"Inner", "Boundary"}
dom = util.CreateDomain(gridName, 0, neededSubsets)
-- create Approximation Space
print("Create ApproximationSpace")
approxSpace = ApproximationSpace(dom)
approxSpace:add_fct("c", "Lagrange", 1)
approxSpace:add_fct("p", "Lagrange", 1)
--approxSpace:init_levels()
approxSpace:init_top_surface()
approxSpace:print_statistic()
--------------------------------------------------------------------------------
-- Setup FV Element Discretization
--------------------------------------------------------------------------------
ug_load_script("elder_user_data.lua")
-- User Data
Porosity = 0.1
MolecularDiffusion = 3.565e-6
Permeability = 4.845e-13
Viscosity = 1e-3;
Gravity = ConstUserVector(0.0)
Gravity:set_entry(dim-1, -9.81)
function DensityFct(c) return 1000 + 200 * c end
function DDensityFct_c(c) return 200 end
-- create dirichlet boundary for concentration
dirichletBND = DirichletBoundary()
dirichletBND:add("ConcentrationDirichletBnd", "c", "Boundary")
dirichletBND:add("PressureDirichletBnd", "p", "Boundary")
-- molecular Diffusion
Diffusion = ScaleAddLinkerMatrix()
Diffusion:add(Porosity, MolecularDiffusion)
-- Density
Density = LuaUserFunctionNumber("DensityFct", 1);
Density:set_deriv(0, "DDensityFct_c");
-- Darcy Velocity
DarcyVelocity = DarcyVelocityLinker();
DarcyVelocity:set_permeability(Permeability)
DarcyVelocity:set_viscosity(Viscosity)
DarcyVelocity:set_density(Density)
DarcyVelocity:set_gravity(Gravity)
print("Darcy Velocity created.")
-- set the product Density * Porosity (only Porosity for Bussinesq)
rhophi = Porosity
FlowEq = ConvectionDiffusion("p", "Inner", "fv1")
FlowEq:set_mass(rhophi)
FlowEq:set_mass_scale(0.0)
FlowEq:set_flux(DarcyVelocity)
print("Flow Equation created.")
TransportEq = ConvectionDiffusion("c", "Inner", "fv1")
--TransportEq:set_upwind(FullUpwind())
TransportEq:set_mass_scale(rhophi)
TransportEq:set_velocity(DarcyVelocity)
TransportEq:set_diffusion(Diffusion)
print("Transport Equation created.")
Density:set_input(0, TransportEq:value())
DarcyVelocity:set_pressure_gradient(FlowEq:gradient())
constraints = OneSideP1Constraints()
--constraints = SymP1Constraints() -- only for fe
-- add Element Discretization to discretization
domainDisc = DomainDiscretization(approxSpace)
domainDisc:add(TransportEq)
domainDisc:add(FlowEq)
domainDisc:add(dirichletBND)
domainDisc:add(constraints)
-- create time discretization
timeDisc = ThetaTimeStep(domainDisc)
timeDisc:set_theta(1.0) -- 1.0 is implicit euler
-- create operator from discretization
op = AssembledOperator()
op:set_discretization(timeDisc)
op:init()
--------------------------------------------------------------------------------
-- Solver
--------------------------------------------------------------------------------
solverDesc = {
type = "newton",
convCheck = {
type = "standard",
iterations = 30, -- maximum number of iterations
absolute = 5e-6, -- absolut value of defect to be reached; usually 1e-7 - 1e-9
reduction = 1e-10, -- reduction factor of defect to be reached; usually 1e-6 - 1e-8
verbose = true -- print convergence rates if true
},
linSolver =
{
type = "bicgstab",
precond = {
type = "gmg", -- preconditioner ["gmg", "ilu", "ilut", "jac", "gs", "sgs"]
adaptive = false,
smoother = "ilu",
cycle = "V", -- gmg-cycle ["V", "F", "W"]
preSmooth = 3, -- number presmoothing steps
postSmooth = 3, -- number postsmoothing steps
rap = false, -- comutes RAP-product instead of assembling if true
rim = false, -- smooth on surface rim
emulateFullRefined = false, -- emulate full grid (works with rap=true only)
baseLevel = 0, -- gmg - baselevel
gatheredBaseSolverIfAmbiguous = true,
baseSolver = "lu",
approxSpace = approxSpace
},
convCheck = {
type = "standard",
iterations = 100, -- number of iterations
absolute = 1e-8, -- absolut value of defact to be reached; usually 1e-8 - 1e-10 (must be larger than in newton section)
reduction = 1e-3, -- reduction factor of defect to be reached; usually 1e-7 - 1e-8 (must be larger than in newton section)
verbose = true, -- print convergence rates if true
}
}
}
newtonSolver = util.solver.CreateSolver(solverDesc)
newtonSolver:init(op)
linSolver = solverDesc.linSolver.instance
--------------------------------------------------------------------------------
-- Find Start Grid
--------------------------------------------------------------------------------
u = GridFunction(approxSpace)
refiner = HangingNodeDomainRefiner(dom)
loadBalancer = balancer.CreateLoadBalancer(dom)
write("Creating initial grid levels:")
for i = 1,maxLvl do
--write(" " .. i)
print("Pre-Adaption " .. i)
-- 1. Intepolate start value
Interpolate("ConcentrationStart", u, "c", 0.0)
-- 2. estimate error and mark
--MarkForAdaption_GradientIndicator(refiner, u, "c", 1e-8, 0.5, 0.2, maxLvl-1);
MarkForAdaption_AbsoluteGradientIndicator(refiner, u, "c", refTol, -1, 0, maxLvl)
-- 3. refine
refiner:refine()
refiner:clear_marks()
if(loadBalancer ~= nil) then
loadBalancer:rebalance()
loadBalancer:create_quality_record("initial-redist-"..i..":")
end
end
--write(" done\n")
print ("Pre-Adaption done")
--SaveDebugHierarchy(dom, "initial")
approxSpace:print_statistic()
print(dom:domain_info():to_string())
--------------------------------------------------------------------------------
-- TimeLoop
--------------------------------------------------------------------------------
time = 0.0
step = 0
-- set initial value
print("Interpolation start values")
Interpolate("PressureStart", u, "p", time)
Interpolate("ConcentrationStart", u, "c", time)
-- filename
filename = "Elder"
-- write start solution
if(discardSolution == false) then
print("Writing start values")
out = VTKOutput()
out:select_nodal("c", "c")
out:select_nodal("p", "p")
out:select_element(DarcyVelocity, "DarcyVelocity")
out:print(filename, u, step, time)
end
-- create new grid function for old value
uOld = GridFunction(approxSpace)
uOld:assign(u)
-- store grid function in vector of old solutions
solTimeSeries = SolutionTimeSeries()
solTimeSeries:push(uOld, time)
local linSolStatFile = io.open("LinSolverStats.txt", "w+")
linSolStatFile:write("# " .. "Timestep" .. "Num Step" .. " \t " .. "Defect" .. " \t " .. "Rate" .. " \t " .. "Avg rate" .. " \n")
if (not linSolStatFile) then
write("Gnuplot Error: cannot open output file: '")
write("LinSolverStats.txt" .. " '\n");
return 1
end
io.close(linSolStatFile)
numTotalNewtonSteps = 0
for step = 1, numTimeSteps do
print("++++++ TIMESTEP " .. step .. " BEGINS at " .. time .. " ++++++")
if(writeGridLayout == true) and (step % stepsBetweenWrite == 0) then
SaveParallelGridLayout(dom:grid(),
"layout-step-"..step.."-p"..ProcRank()..".ugx", zOutputTransform)
end
-- choose time step
do_dt = dt
-- start with smaller timestep for first steps
if step <= numDampSteps then
-- perform linear interpolation between maximal damped time step and normal time step
local ia = (step - 1) / numDampSteps
do_dt = do_dt * ia + (1 - ia) * do_dt * maxDamping;
end
print("Size of timestep dt: " .. do_dt)
bSuccess = false;
while bSuccess == false do
-- setup time Disc for old solutions and timestep
timeDisc:prepare_step(solTimeSeries, do_dt)
-- prepare newton solver
if newtonSolver:prepare(u) == false then
print ("Newton solver failed at step "..step.."."); exit();
end
-- apply newton solver
if newtonSolver:apply(u) == false then
do_dt = do_dt * stepReducFac;
print("\n++++++ Newton solver failed. Trying decreased stepsize " .. do_dt);
if(do_dt < minStepSize) then
print("++++++ Time Step to small. Cannot solve problem.");
exit();
end
else
bSuccess = true;
end
numTotalNewtonSteps = numTotalNewtonSteps + newtonSolver:num_newton_steps()
end
-- linear solver statistics
linSolStat = {}
linSolStat.step = linSolver:convergence_check():step()
linSolStat.defect = linSolver:convergence_check():defect()
linSolStat.rate = linSolver:convergence_check():reduction()
linSolStat.avg_rate = linSolver:convergence_check():avg_rate()
print(" ##### LIN SOLVER STATS AT STEP " .. step .. " #####")
print(" ## steps: " .. linSolStat.step)
print(" ## defect: " .. linSolStat.defect)
print(" ## rate: " .. linSolStat.rate)
print(" ## avg_red: " .. linSolStat.avg_rate)
print(" ##############################################")
local linSolStatFile = io.open("LinSolverStats.txt", "a")
linSolStatFile:write(" " .. step .. " \t " .. linSolStat.step .. " \t " .. linSolStat.defect .. " \t " .. linSolStat.rate .. " \t " .. linSolStat.avg_rate .. " \n")
io.close(linSolStatFile)
-- update new time
time = solTimeSeries:time(0) + do_dt
-- 2: PERFORM GRID ADAPTION
local curNumVrts = ParallelSum(dom:grid():num_vertices())
print(" Current number of vertices (global): " .. curNumVrts)
print(" Coarsening")
local adptionRunning = true
local i = 1
while (adptionRunning == true) do
MarkForAdaption_AbsoluteGradientIndicator(refiner, u, "c", -1, coarsenTol, 0, maxLvl)
local nodesBefore = ParallelSum(dom:grid():num_vertices())
refiner:coarsen()
local nodesAfter = ParallelSum(dom:grid():num_vertices())
refiner:clear_marks()
if(nodesAfter == nodesBefore) then
adptionRunning = false
print(" New number of vertices left (global): " .. nodesAfter)
else
write (" " .. i .. " ")
i = i + 1
end
end
print(" Refining")
adptionRunning = true
i = 1
while (adptionRunning == true) do
MarkForAdaption_AbsoluteGradientIndicator(refiner, u, "c", refTol, -1, 0, maxLvl)
local nodesBefore = ParallelSum(dom:grid():num_vertices())
refiner:refine()
local nodesAfter = ParallelSum(dom:grid():num_vertices())
refiner:clear_marks()
if(nodesAfter == nodesBefore) then
adptionRunning = false
print(" New number of vertices left (global): " .. nodesAfter)
else
write (" " .. i .. " ")
i = i + 1
end
end
if((step ~= numTimeSteps) and (loadBalancer ~= nil) and (step % stepsBetweenRedist == 0)) then
loadBalancer:rebalance()
loadBalancer:create_quality_record("step-"..step..":")
end
-- plot solution
if(discardSolution == false) and (step % stepsBetweenWrite == 0) then
out:print(filename, u, step, time)
end
-- get oldest solution
oldestSol = solTimeSeries:oldest()
-- copy values into oldest solution (we reuse the memory here)
VecScaleAssign(oldestSol, 1.0, u)
-- push oldest solutions with new values to front, oldest sol pointer is poped from end
solTimeSeries:push_discard_oldest(oldestSol, time)
print("dof statistics")
approxSpace:print_statistic()
print("domain info:")
print(dom:domain_info():to_string())
print("++++++ TIMESTEP " .. step .. " END ++++++");
end
-- end timeseries, produce gathering file
if(discardSolution == false) then
out:write_time_pvd(filename, u)
end
if loadBalancer ~= nil then
print("Distribution quality statistics:")
loadBalancer:print_quality_records()
end
print("Solver info:")
print(" num newton steps: " .. numTotalNewtonSteps)
print(" num lin-solver calls: " .. newtonSolver:total_linsolver_calls())
print(" num lin-solver steps: " .. newtonSolver:total_linsolver_steps())
print("")