-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpC_gql_3d.py
173 lines (149 loc) · 5.91 KB
/
rpC_gql_3d.py
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
import time
from configparser import ConfigParser
from pathlib import Path
import sys
import logging
import numpy as np
import dedalus.public as d3
from GQLProjection import GQLProjection as Project
logger = logging.getLogger(__name__)
debug = False
# Parsing .cfg passed
config_file = Path(sys.argv[-1])
logger.info("Running with config file {}".format(str(config_file)))
# Setting global params
runconfig = ConfigParser()
runconfig.read(str(config_file))
datadir = Path("couette_runs") / config_file.stem
# Params
timestepper = d3.RK222
params = runconfig['params']
nx = params.getint('nx')
ny = params.getint('ny')
nz = params.getint('nz')
Lx = params.getfloat('Lx')
Ly = params.getfloat('Ly')
Lz = 2 # fixed by problem definition
ampl = params.getfloat('ampl')
Re = params.getfloat('Re')
Ro = params.getfloat('Ro')
Lambda_x = params.getfloat('Lambda_x')
Lambda_y = params.getfloat('Lambda_y')
run_params = runconfig['run']
restart = run_params.get('restart_file')
stop_wall_time = run_params.getfloat('stop_wall_time')
stop_sim_time = run_params.getfloat('stop_sim_time')
stop_iteration = run_params.getint('stop_iteration')
sim_dt = run_params.getfloat('dt')
# Create bases and domain
start_init_time = time.time()
dealias = 3/2
dtype = np.float64
coords = d3.CartesianCoordinates('x', 'y', 'z')
dist = d3.Distributor(coords, dtype=dtype, mesh=[1,1])
xbasis = d3.RealFourier(coords['x'], size=nx, bounds=(0, Lx), dealias = dealias)
ybasis = d3.RealFourier(coords['y'], size=ny, bounds=(0, Ly), dealias = dealias)
zbasis = d3.ChebyshevT(coords['z'], size=nz, bounds = (-1,1), dealias = dealias)
x = xbasis.local_grid(dealias)
y = xbasis.local_grid(dealias)
z = zbasis.local_grid(dealias)
integ = lambda A: d3.Integrate(d3.Integrate(d3.Integrate(A, 'x'),'y'), 'z')
# Fields
ba = (xbasis,ybasis,zbasis)
ba_p = (xbasis,ybasis)
p = dist.Field(name='p', bases=ba)
u = dist.VectorField(coords, name='u', bases=ba)
tau_u1 = dist.VectorField(coords, name='tau_u1', bases=ba_p)
tau_u2 = dist.VectorField(coords, name='tau_u2', bases=ba_p)
tau_p = dist.Field(name='tau_p')
# NCC
U = dist.VectorField(coords, name='U', bases=(xbasis,ybasis,zbasis))
U.change_scales(dealias)
U['g'][0] = z
ex = dist.VectorField(coords, name='ex')
ey = dist.VectorField(coords, name='ey', bases=(zbasis,))
ez = dist.VectorField(coords, name='ez')
ex['g'][0] = 1
ey['g'][1] = 1
ez['g'][2] = 1
lift_basis = zbasis.clone_with(a=1/2, b=1/2) # First derivative basis
lift = lambda A: d3.Lift(A, lift_basis, -1)
grad_u = d3.grad(u) + ez*lift(tau_u1) # First-order reduction
if dist.comm.rank == 0:
if not datadir.exists():
datadir.mkdir()
problem = d3.IVP([p, u, tau_u1, tau_u2, tau_p], namespace=locals())
Project_high = lambda A: Project(A,[Lambda_x,Lambda_y],'h')
Project_low = lambda A: Project(A,[Lambda_x,Lambda_y],'l')
ul = Project_low(u)
uh = Project_high(u)
problem.add_equation("trace(grad_u) + tau_p = 0")
#problem.add_equation("dt(u) - div(grad_u)/Re + grad(p) + lift(tau_u2) + cross(ey,u)/Ro = -dot(u,grad(u))")
problem.add_equation("dt(u) - div(grad_u)/Re + grad(p) + lift(tau_u2) = -Project_low(ul@grad(ul)) + uh@grad(uh)) - Project_high(ul@grad(uh) + uh@grad(ul)))")
problem.add_equation("dot(ex,u)(z=-1) = -1")
problem.add_equation("dot(ex,u)(z=1) = 1")
problem.add_equation("dot(ey,u)(z=-1) = 0")
problem.add_equation("dot(ey,u)(z=1) = 0")
problem.add_equation("dot(ez,u)(z=-1) = 0")
problem.add_equation("dot(ez,u)(z=1) = 0")
problem.add_equation("integ(p) = 0") # Pressure gauge
solver = problem.build_solver(timestepper)
logger.info("Solver built")
solver.stop_sim_time = stop_sim_time
solver.stop_wall_time = stop_wall_time
solver.stop_iteration = stop_iteration
# Initial conditions
A = dist.VectorField(coords, name='A', bases=ba)
A.change_scales(dealias)
A.fill_random('g', seed=42, distribution='normal')
A.low_pass_filter(scales=(0.5, 0.5, 0.5))
A['g'] *= (Lz**2*(z+1)/Lz * (1 - (z+1)/Lz))**2 # Damp noise at walls
up = d3.curl(A).evaluate()
u.change_scales(dealias)
# u['g'] = ampl*up['g']*Lz**2*(z+1)/Lz * (1 - (z+1)/Lz) + U['g']
u['g'] = ampl*up['g'] + U['g']
divu0 = d3.div(u).evaluate()
logger.info("min, max divu = ({}, {})".format(divu0['g'].min(), divu0['g'].max()))
KE = 0.5 * d3.DotProduct(u,u)
KE.name = 'KE'
u_pert = u - U
KE_pert = 0.5 * d3.DotProduct(u_pert,u_pert)
# Analysis
check = solver.evaluator.add_file_handler(datadir / Path('checkpoints'), iter=10, max_writes=1, virtual_file=True)
check.add_tasks(solver.state)
check_c = solver.evaluator.add_file_handler(datadir / Path('checkpoints_c'),iter=1000,max_writes=100)
check_c.add_tasks(solver.state, layout='c')
# timeseries
timeseries = solver.evaluator.add_file_handler(datadir / Path('timeseries'), iter=10)
timeseries.add_task(integ(KE), name='KE')
timeseries.add_task(integ(KE_pert), name = 'KE_pert')
timeseries.add_task(integ(d3.div(u)*d3.div(u)), name='rms_div_u')
# flow profiles
flow = d3.GlobalFlowProperty(solver, cadence=10)
flow.add_property(KE, name='KE')
flow.add_property(d3.div(u), name='div_u')
flow.add_property(d3.div(u)**2, name='div_u_sq')
flow.add_property(KE_pert, name = 'KE_pert')
# CFL
CFL = d3.CFL(solver, initial_dt=1e-5, cadence=10, safety=0.2, threshold=0.1,
max_change=1.5, min_change=0.5, max_dt=sim_dt)
CFL.add_velocity(u)
# Main loop
end_init_time = time.time()
logger.info('Initialization time: %f' %(end_init_time-start_init_time))
try:
logger.info('Starting loop')
while solver.proceed:
dt = CFL.compute_timestep()
solver.step(dt)
if (solver.iteration-1) % 10 == 0:
logger.info('Iteration: %i, Time: %e, dt: %e' %(solver.iteration, solver.sim_time, dt))
logger.info('Max KE = %e; Max div(u) = %e' %(flow.max('KE'), flow.max('div_u')))
logger.info('Vol RMS div(u) = %e'%flow.volume_integral('div_u_sq'))
logger.info('Max perturbation KE = %e' %flow.max('KE_pert'))
except:
logger.error('Exception raised, triggering end of main loop.')
raise
finally:
solver.evaluate_handlers_now(dt)
solver.log_stats()