1 |
################################################ |
2 |
## ## |
3 |
## October 2006 ## |
4 |
## ## |
5 |
## 3D Rayleigh-Taylor instability benchmark ## |
6 |
## by Laurent Bourgouin ## |
7 |
## ## |
8 |
################################################ |
9 |
|
10 |
|
11 |
### IMPORTS ### |
12 |
from esys.escript import * |
13 |
import esys.finley |
14 |
from esys.finley import finley |
15 |
from esys.escript.linearPDEs import LinearPDE |
16 |
from esys.escript.pdetools import Projector, SaddlePointProblem |
17 |
import sys |
18 |
import math |
19 |
|
20 |
### DEFINITION OF THE DOMAIN ### |
21 |
l0=1. |
22 |
l1=1. |
23 |
n0=10 # IDEALLY 80... |
24 |
n1=10 # IDEALLY 80... |
25 |
mesh=esys.finley.Brick(l0=l0, l1=l1, l2=l0, order=2, n0=n0, n1=n1, n2=n0) |
26 |
|
27 |
### PARAMETERS OF THE SIMULATION ### |
28 |
rho1 = 1.0e3 # DENSITY OF THE FLUID AT THE BOTTOM |
29 |
rho2 = 1.01e3 # DENSITY OF THE FLUID ON TOP |
30 |
eta1 = 1.0e2 # VISCOSITY OF THE FLUID AT THE BOTTOM |
31 |
eta2 = 1.0e2 # VISCOSITY OF THE FLUID ON TOP |
32 |
penalty = 1.0e3 # PENALTY FACTOR FOT THE PENALTY METHOD |
33 |
g=10. # GRAVITY |
34 |
t_step = 0 |
35 |
t_step_end = 2000 |
36 |
reinit_max = 30 # NUMBER OF ITERATIONS DURING THE REINITIALISATION PROCEDURE |
37 |
reinit_each = 3 # NUMBER OF TIME STEPS BETWEEN TWO REINITIALISATIONS |
38 |
h = Lsup(mesh.getSize()) |
39 |
numDim = mesh.getDim() |
40 |
smooth = h*2.0 # SMOOTHING PARAMETER FOR THE TRANSITION ACROSS THE INTERFACE |
41 |
|
42 |
### DEFINITION OF THE PDE ### |
43 |
velocityPDE = LinearPDE(mesh, numEquations=numDim) |
44 |
|
45 |
advectPDE = LinearPDE(mesh) |
46 |
advectPDE.setReducedOrderOn() |
47 |
advectPDE.setValue(D=1.0) |
48 |
advectPDE.setSolverMethod(solver=LinearPDE.DIRECT) |
49 |
|
50 |
reinitPDE = LinearPDE(mesh, numEquations=1) |
51 |
reinitPDE.setReducedOrderOn() |
52 |
reinitPDE.setSolverMethod(solver=LinearPDE.LUMPING) |
53 |
my_proj=Projector(mesh) |
54 |
|
55 |
### BOUNDARY CONDITIONS ### |
56 |
xx = mesh.getX()[0] |
57 |
yy = mesh.getX()[1] |
58 |
zz = mesh.getX()[2] |
59 |
top = whereZero(zz-l1) |
60 |
bottom = whereZero(zz) |
61 |
left = whereZero(xx) |
62 |
right = whereZero(xx-l0) |
63 |
front = whereZero(yy) |
64 |
back = whereZero(yy-l0) |
65 |
b_c = (bottom+top)*[1.0, 1.0, 1.0] + (left+right)*[1.0,0.0, 0.0] + (front+back)*[0.0, 1.0, 0.0] |
66 |
velocityPDE.setValue(q = b_c) |
67 |
|
68 |
pressure = Scalar(0.0, ContinuousFunction(mesh)) |
69 |
velocity = Vector(0.0, ContinuousFunction(mesh)) |
70 |
|
71 |
### INITIALISATION OF THE INTERFACE ### |
72 |
func = -(-0.1*cos(math.pi*xx/l0)*cos(math.pi*yy/l0)-zz+0.4) |
73 |
phi = func.interpolate(ReducedSolution(mesh)) |
74 |
|
75 |
|
76 |
def advect(phi, velocity, dt): |
77 |
### SOLVES THE ADVECTION EQUATION ### |
78 |
|
79 |
Y = phi.interpolate(Function(mesh)) |
80 |
for i in range(numDim): |
81 |
Y -= (dt/2.0)*velocity[i]*grad(phi)[i] |
82 |
advectPDE.setValue(Y=Y) |
83 |
phi_half = advectPDE.getSolution() |
84 |
|
85 |
Y = phi |
86 |
for i in range(numDim): |
87 |
Y -= dt*velocity[i]*grad(phi_half)[i] |
88 |
advectPDE.setValue(Y=Y) |
89 |
phi = advectPDE.getSolution() |
90 |
|
91 |
print "Advection step done" |
92 |
return phi |
93 |
|
94 |
def reinitialise(phi): |
95 |
### SOLVES THE REINITIALISATION EQUATION ### |
96 |
s = sign(phi.interpolate(Function(mesh))) |
97 |
w = s*grad(phi)/length(grad(phi)) |
98 |
dtau = 0.3*h |
99 |
iter =0 |
100 |
previous = 100.0 |
101 |
mask = whereNegative(abs(phi)-1.2*h) |
102 |
reinitPDE.setValue(q=mask, r=phi) |
103 |
print "Reinitialisation started." |
104 |
while (iter<=reinit_max): |
105 |
prod_scal =0.0 |
106 |
for i in range(numDim): |
107 |
prod_scal += w[i]*grad(phi)[i] |
108 |
coeff = s - prod_scal |
109 |
ps2=0 |
110 |
for i in range(numDim): |
111 |
ps2 += w[i]*grad(my_proj(coeff))[i] |
112 |
reinitPDE.setValue(D=1.0, Y=phi+dtau*coeff-0.5*dtau**2*ps2) |
113 |
phi = reinitPDE.getSolution() |
114 |
error = Lsup((previous-phi)*whereNegative(abs(phi)-3.0*h))/h |
115 |
print "Reinitialisation iteration :", iter, " error:", error |
116 |
previous = phi |
117 |
iter +=1 |
118 |
print "Reinitialisation finalized." |
119 |
return phi |
120 |
|
121 |
def update_phi(phi, velocity, dt, t_step): |
122 |
### CALLS THE ADVECTION PROCEDURE AND THE REINITIALISATION IF NECESSARY ### |
123 |
phi=advect(phi, velocity, dt) |
124 |
if t_step%reinit_each ==0: |
125 |
phi = reinitialise(phi) |
126 |
return phi |
127 |
|
128 |
def update_parameter(phi, param_neg, param_pos): |
129 |
### UPDATES THE PARAMETERS TABLE USING THE SIGN OF PHI, A SMOOTH TRANSITION IS DONE ACROSS THE INTERFACE ### |
130 |
mask_neg = whereNonNegative(-phi-smooth) |
131 |
mask_pos = whereNonNegative(phi-smooth) |
132 |
mask_interface = whereNegative(abs(phi)-smooth) |
133 |
param = param_pos*mask_pos + param_neg*mask_neg + ((param_pos+param_neg)/2 +(param_pos-param_neg)*phi/(2.*smooth))*mask_interface |
134 |
return param |
135 |
|
136 |
class StokesProblem(SaddlePointProblem): |
137 |
""" |
138 |
simple example of saddle point problem |
139 |
""" |
140 |
def __init__(self,domain,debug=False): |
141 |
super(StokesProblem, self).__init__(self,debug) |
142 |
self.domain=domain |
143 |
self.__pde_u=LinearPDE(domain,numEquations=self.domain.getDim(),numSolutions=self.domain.getDim()) |
144 |
self.__pde_u.setSymmetryOn() |
145 |
|
146 |
self.__pde_p=LinearPDE(domain) |
147 |
self.__pde_p.setReducedOrderOn() |
148 |
self.__pde_p.setSymmetryOn() |
149 |
|
150 |
def initialize(self,f=Data(),fixed_u_mask=Data(),eta=1): |
151 |
self.eta=eta |
152 |
A =self.__pde_u.createCoefficientOfGeneralPDE("A") |
153 |
for i in range(self.domain.getDim()): |
154 |
for j in range(self.domain.getDim()): |
155 |
A[i,j,j,i] += self.eta |
156 |
A[i,j,i,j] += self.eta |
157 |
self.__pde_p.setValue(D=1/self.eta) |
158 |
self.__pde_u.setValue(A=A,q=fixed_u_mask,Y=f) |
159 |
|
160 |
def inner(self,p0,p1): |
161 |
return integrate(p0*p1,Function(self.__pde_p.getDomain())) |
162 |
|
163 |
def solve_f(self,u,p,tol=1.e-8): |
164 |
self.__pde_u.setTolerance(tol) |
165 |
g=grad(u) |
166 |
self.__pde_u.setValue(X=self.eta*symmetric(g)+p*kronecker(self.__pde_u.getDomain())) |
167 |
return self.__pde_u.getSolution() |
168 |
|
169 |
def solve_g(self,u,tol=1.e-8): |
170 |
self.__pde_p.setTolerance(tol) |
171 |
self.__pde_p.setValue(X=-u) |
172 |
dp=self.__pde_p.getSolution() |
173 |
return dp |
174 |
|
175 |
sol=StokesProblem(velocity.getDomain(),debug=True) |
176 |
def solve_vel_uszawa(rho, eta, velocity, pressure): |
177 |
### SOLVES THE VELOCITY PROBLEM USING A PENALTY METHOD FOR THE INCOMPRESSIBILITY ### |
178 |
Y = Vector(0.0,Function(mesh)) |
179 |
Y[1] -= rho*g |
180 |
sol.initialize(fixed_u_mask=b_c,eta=eta,f=Y) |
181 |
velocity,pressure=sol.solve(velocity,pressure,iter_max=100,tolerance=0.01) #,accepted_reduction=None) |
182 |
return velocity, pressure |
183 |
|
184 |
def solve_vel_penalty(rho, eta, velocity, pressure): |
185 |
### SOLVES THE VELOCITY PROBLEM USING A PENALTY METHOD FOR THE INCOMPRESSIBILITY ### |
186 |
velocityPDE.setSolverMethod(solver=LinearPDE.DIRECT) |
187 |
error = 1.0 |
188 |
ref = pressure*1.0 |
189 |
p_iter=0 |
190 |
while (error >= 1.0e-2): |
191 |
|
192 |
A=Tensor4(0.0, Function(mesh)) |
193 |
for i in range(numDim): |
194 |
for j in range(numDim): |
195 |
A[i,j,i,j] += eta |
196 |
A[i,j,j,i] += eta |
197 |
A[i,i,j,j] += penalty*eta |
198 |
|
199 |
Y = Vector(0.0,Function(mesh)) |
200 |
Y[1] -= rho*g |
201 |
|
202 |
X = Tensor(0.0, Function(mesh)) |
203 |
for i in range(numDim): |
204 |
X[i,i] += pressure |
205 |
|
206 |
velocityPDE.setValue(A=A, X=X, Y=Y) |
207 |
velocity = velocityPDE.getSolution() |
208 |
p_iter +=1 |
209 |
if p_iter >=500: |
210 |
print "You're screwed..." |
211 |
sys.exit(1) |
212 |
|
213 |
pressure -= penalty*eta*(trace(grad(velocity))) |
214 |
error = penalty*Lsup(trace(grad(velocity)))/Lsup(grad(velocity)) |
215 |
print "\nPressure iteration number:", p_iter |
216 |
print "error", error |
217 |
ref = pressure*1.0 |
218 |
|
219 |
return velocity, pressure |
220 |
|
221 |
### MAIN LOOP, OVER TIME ### |
222 |
while t_step <= t_step_end: |
223 |
print "######################" |
224 |
print "Time step:", t_step |
225 |
print "######################" |
226 |
rho = update_parameter(phi, rho1, rho2) |
227 |
eta = update_parameter(phi, eta1, eta2) |
228 |
|
229 |
velocity, pressure = solve_vel_uszawa(rho, eta, velocity, pressure) |
230 |
dt = 0.3*Lsup(mesh.getSize())/Lsup(velocity) |
231 |
phi = update_phi(phi, velocity, dt, t_step) |
232 |
|
233 |
### PSEUDO POST-PROCESSING ### |
234 |
print "########## Saving image", t_step, " ###########" |
235 |
saveVTK("phi3D.%2.2i.vtk"%t_step,layer=phi) |
236 |
|
237 |
t_step += 1 |
238 |
|
239 |
# vim: expandtab shiftwidth=4: |