-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblepipe.lua
408 lines (315 loc) · 10.9 KB
/
bubblepipe.lua
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
-------------------------------------------------------------------------------------------------------
--
-- Bubble flow
--
-- Author: Christian Wehner
--
------------------------------------------------------------------------------------------------------
ug_load_script("ug_util.lua")
dim = util.GetParamNumber("-dim", 2) -- default dimension is 2.
-- chose "fvcr" or "stabil"
discType = util.GetParam("-type", "fvcr")
elemType = util.GetParam("-elem", "quads")
InitUG(dim, AlgebraType("CPU", 1));
if dim == 2 then
gridName = util.GetParam("-grid", "grids/bubblerectangle.ugx")
else print("Chosen Dimension " .. dim .. "not supported. Exiting."); exit(); end
dt = util.GetParamNumber("-dt", 0.05)
numTimeSteps = util.GetParamNumber("-numTimeSteps", 500)
numPreRefs = util.GetParamNumber("-numPreRefs", 0)
numRefs = util.GetParamNumber("-numRefs",3)
print(" Chosen Parameters:")
print(" dim = " .. dim)
print(" numTotalRefs = " .. numRefs)
print(" numPreRefs = " .. numPreRefs)
print(" type = " .. discType)
print(" dt = " .. dt)
print(" numTimeSteps = " .. numTimeSteps)
print(" grid = " .. gridName)
--------------------------------------------
--------------------------------------------
-- Problem Parameters
--------------------------------------------
--------------------------------------------
-- bubble radius
radius = 0.35/2.0
-- surface tension factor
sigma = 2
-- viscosity
mu_fluid = 10.4e-2
mu_bubble = 4.8e-2
-- density
rho_fluid = 0.995
rho_bubble = 0.001107
-- gravitation
gravitation = 980
--------------------------------------------
--------------------------------------------
-- Loading Domain and Domain Refinement
--------------------------------------------
--------------------------------------------
-- Lets define a list of all subsets that we need
requiredSubsets = {"Inner", "Top", "Bottom", "Sides"}
dom = util.CreateAndDistributeDomain(gridName, numRefs, numPreRefs, requiredSubsets)
-- All subset are ok. So we can create the Approximation Space
approxSpace = ApproximationSpace(dom)
if dim >= 1 then approxSpace:add_fct("u", "Crouzeix-Raviart") end
if dim >= 2 then approxSpace:add_fct("v", "Crouzeix-Raviart") end
if dim >= 3 then approxSpace:add_fct("w", "Crouzeix-Raviart") end
approxSpace:add_fct("p", "piecewise-constant")
-- finally we print some statistic on the distributed dofs
approxSpace:init_levels()
approxSpace:init_top_surface()
approxSpace:print_statistic()
approxSpace:print_local_dof_statistic(2)
approxSpaceLevelSet = ApproximationSpace(dom)
approxSpaceLevelSet:add_fct("lsf", "Lagrange", 1)
approxSpaceLevelSet:add_fct("curvature", "piecewise-constant")
OrderLex(approxSpace, "lr");
OrderLex(approxSpaceLevelSet, "lr");
--------------------------------
--------------------------------
-- Discretization
--------------------------------
--------------------------------
fctUsed = "u"
if dim >= 2 then fctUsed = fctUsed .. ", v" end
if dim >= 3 then fctUsed = fctUsed .. ", w" end
fctUsed = fctUsed .. ", p"
NavierStokesDisc = NavierStokes(fctUsed, "Inner", "fvcr")
-- set upwind
noUpwind = NavierStokesNoUpwind();
fullUpwind = NavierStokesFullUpwind();
weightedUpwind = NavierStokesWeightedUpwind(0.5);
NavierStokesDisc:set_upwind(fullUpwind)
NavierStokesDisc:set_peclet_blend(true)
NavierStokesDisc:set_exact_jacobian(false)
NavierStokesDisc:set_laplace(false)
NavierStokesDisc:set_stokes(false)
-- Level set class
lsDisc = FV1LevelSetDisc();
----------------------------------
----------------------------------
-- Initial Level Set Function
----------------------------------
----------------------------------
function phiInit(x, y)
return math.sqrt( (x-0.25)*(x-0.25)+(y-0.5)*(y-0.5) )-0.35/2;
end
phiNew = GridFunction(approxSpaceLevelSet);
phiOld = GridFunction(approxSpaceLevelSet);
phiNew:set(0);
solfunctor = LuaUserNumber("phiInit");
Interpolate("phiInit", phiNew, "lsf");
VecAssign(phiOld,phiNew);
----------------------------------
----------------------------------
-- source
----------------------------------
----------------------------------
density = LevelSetUserData(approxSpaceLevelSet,phiNew)
density:set_inside(rho_bubble)
density:set_outside(rho_fluid)
density:set_eval_type(0)
viscosity = LevelSetUserData(approxSpaceLevelSet,phiNew)
viscosity:set_inside(mu_bubble)
viscosity:set_outside(mu_fluid)
viscosity:set_eval_type(1)
source = CRTwoPhaseSource(approxSpaceLevelSet,phiNew)
source:set_sigma(sigma)
source:set_density(density)
source:set_gravitation(gravitation)
NavierStokesDisc:set_source(source)
NavierStokesDisc:set_density(density)
NavierStokesDisc:set_kinematic_viscosity(viscosity)
----------------------------------
----------------------------------
-- Boundary conditions
----------------------------------
----------------------------------
-- Inlet condition
function inletVel2d(x, y, t)
return 0,-64 * x * (0.5-x)
end
wall = NavierStokesWall(NavierStokesDisc)
wall:add("Sides")
inflow = NavierStokesInflow(NavierStokesDisc)
inflow:add("inletVel"..dim.."d", "Bottom")
outflow = NavierStokesNoNormalStressOutflow(NavierStokesDisc)
outflow:add("Top")
--------------------------------
--------------------------------
-- Solution of the Problem
--------------------------------
--------------------------------
-- set up domain discretization
domainDisc = DomainDiscretization(approxSpace)
domainDisc:add(NavierStokesDisc)
domainDisc:add(inflow)
domainDisc:add(wall)
domainDisc:add(outflow)
-- create time discretization
timeDisc = ThetaTimeStep(domainDisc)
timeDisc:set_theta(1.0) -- 1.0 is implicit euler
op = AssembledOperator(timeDisc)
op:init()
u = GridFunction(approxSpace)
u:set(0)
-- set level set velocity
velocity = GridFunctionVectorData(u, "u,v");
lsDisc:set_velocity(velocity)
lsDisc:set_dt(dt)
function StartValue_u(x,y,t) return 0 end
function StartValue_v(x,y,t) return 0 end
function StartValue_p(x,y,t) return 0 end
Interpolate("StartValue_u", u, "u")
Interpolate("StartValue_v", u, "v")
Interpolate("StartValue_p", u, "p")
vanka = LineVanka(approxSpace)
vanka:set_num_steps(4,4,4,4,0,0)
-- vanka = CRILU()
-- vanka = Vanka()
vanka:set_damp(0.9)
-- vanka = Vanka()
-- vanka = BlockVanka(approxSpace)
-- vanka:set_blocksize(0.333334/2/2,0.333334/2/2,0.1)
-- vanka:update()
-- vanka:set_relax(0.9);
-- vanka = DiagVanka()
vankaSolver = LinearSolver()
vankaSolver:set_preconditioner(vanka)
vankaSolver:set_convergence_check(ConvCheck(100000, 1e-9, 1e-10, true))
baseConvCheck = ConvCheck()
baseConvCheck:set_maximum_steps(10000)
baseConvCheck:set_minimum_defect(1e-9)
baseConvCheck:set_reduction(1e-1)
baseConvCheck:set_verbose(false)
vankaBase = LinearSolver()
vankaBase:set_preconditioner(Vanka())
vankaBase:set_convergence_check(baseConvCheck)
gmg = GeometricMultiGrid(approxSpace)
gmg:set_discretization(domainDisc)
gmg:set_base_level(0)
gmg:set_base_solver(vankaBase)
gmg:set_smoother(vanka)
gmg:set_cycle_type(1)
gmg:set_num_presmooth(1)
gmg:set_num_postsmooth(1)
gmg:set_damp(MinimalResiduumDamping())
-- gmg:set_damp(0.8)
-- gmg:set_damp(MinimalEnergyDamping())
--gmg:set_debug(dbgWriter)
-- create Linear Solver
BiCGStabSolver = BiCGStab()
BiCGStabSolver:set_preconditioner(vanka)
-- BiCGStabSolver:set_preconditioner(gmg)
BiCGStabSolver:set_convergence_check(ConvCheck(100000, 1e-9, 1e-10, true))
gmgSolver = LinearSolver()
gmgSolver:set_preconditioner(gmg)
gmgSolver:set_convergence_check(ConvCheck(10000, 1e-9, 1e-10, true))
-- choose a solver
solver = BiCGStabSolver
solver = vankaSolver
solver = LU()
-- solver = gmgSolver
newtonConvCheck = ConvCheck()
newtonConvCheck:set_maximum_steps(10000)
newtonConvCheck:set_minimum_defect(1e-9)
newtonConvCheck:set_reduction(1e-10)
newtonConvCheck:set_verbose(true)
newtonLineSearch = StandardLineSearch()
newtonLineSearch:set_maximum_steps(20)
newtonLineSearch:set_lambda_start(1.0)
newtonLineSearch:set_reduce_factor(0.7)
newtonLineSearch:set_accept_best(false)
dbgWriter = GridFunctionDebugWriter(approxSpace)
dbgWriter:set_vtk_output(false)
newtonSolver = NewtonSolver()
newtonSolver:set_linear_solver(solver)
newtonSolver:set_convergence_check(newtonConvCheck)
-- newtonSolver:set_line_search(newtonLineSearch)
-- newtonSolver:set_debug(dbgWriter)
newtonSolver:init(op)
if newtonSolver:prepare(u) == false then
print ("Newton solver prepare failed."); exit();
end
SaveVectorForConnectionViewer(u, "StartSolution.vec")
-- if newtonSolver:apply(u) == false then
-- print ("Newton solver apply failed."); exit();
-- end
--------------------------------------------------------------------------------
-- Apply Solver
--------------------------------------------------------------------------------
-- start
time = 0.0
step = 0
-- setup the lua functions ...
function Pressure_StartValue2d(x, y, t) return 0.0 end
function VelX_StartValue2d(x, y, t) return 0.0 end
function VelY_StartValue2d(x, y, t) return 0.0 end
-- Now interpolate the function
time = 0.0
Interpolate("Pressure_StartValue"..dim.."d", u, "p", time);
Interpolate("VelX_StartValue"..dim.."d", u, "u", time);
Interpolate("VelY_StartValue"..dim.."d", u, "v", time);
-- filename
filename = "Sol"
-- write start solution
-- print("Writing start values")
-- out = VTKOutput()
-- out:print(filename, u, step, time)
out = VTKOutput()
out:clear_selection()
out:select_all(false)
out:select_element("u,v", "velocity")
out:select_element("u", "u")
out:select_element("v", "v")
out:select_element("p", "p")
out:print("twoPhase", u,0,0)
lsfilename = "lsf"
outls = VTKOutput()
step=0;
time=0;
outls:select_element("lsf", "lsf")
outls:print(lsfilename, phiNew, step, time)
-- create new grid function for old value
uOld = u:clone()
tBefore = os.clock()
-- store grid function in vector of old solutions
solTimeSeries = SolutionTimeSeries()
solTimeSeries:push(uOld, time)
lsDisc:compute_curvature(phiNew);
for step = 1, numTimeSteps do
print("++++++ TIMESTEP " .. step .. " BEGIN ++++++")
-- choose time step
do_dt = dt
-- 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
print ("Newton solver failed at step "..step.."."); exit();
end
-- update new time
time = solTimeSeries:time(0) + do_dt
-- 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("++++++ TIMESTEP " .. step .. " END ++++++");
lsDisc:advect_lsf(phiNew,phiOld);
lsDisc:compute_curvature(phiNew);
VecAssign(phiOld,phiNew);
-- plot solution
out:clear_selection()
out:print("twoPhase", u,step,time)
outls:print(lsfilename, phiNew, step, time)
end
tAfter = os.clock()
print("Computation took " .. tAfter-tBefore .. " seconds.");
print("done.")