periodic_convection_imexrk4.py 9.24 KB
Newer Older
1
2
3
4
5
6
7
from dgpy import *
from math import *
import time, os, sys
import gmshPartition
import numpy as np

nbSteps = int(sys.argv[1])
zhang's avatar
zhang committed
8
compError = 1
9
10
11
12
13
14
15
16
17
18
19

#### Physical constants ###
gamma=1.4
Rd=287.0
p0=1.0e5
g=9.80616
Cv=Rd/(gamma-1.0)
Cp=Rd*(1.0+1.0/(gamma-1.0))
###########################
#### Begin and end time ###
Ti=0.0
zhang's avatar
zhang committed
20
Tf=100.0
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
###########################

#Period of the oscillations
period = 100

order=4
dimension=3

model = GModel()
name='periodic_convection'
if Msg.GetCommSize()>1:
  partStr='_part_%i' % Msg.GetCommSize()
else:
  partStr=''
model.load(name + partStr + '_3d.msh')

groups = dgGroupCollection(model, dimension, order)
groups.splitGroupsByPhysicalTag();

claw = dgEulerAtmLaw(dimension)
zhang's avatar
zhang committed
41
42
43
if compError:
  ####Load reference solution from file###
  reloaded =dgDofContainer(groups,claw.getNbFields())
44
  reloaded.importIdx("refSolERK"+str(period)+"_16"+"/subwindow1/subwindow1.idx")
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
solution = dgDofContainer(groups, claw.getNbFields())

XYZ = groups.getFunctionCoordinates();

def initialCondition(cmap,FCT, XYZ) :
  FCT[:,:] = 0

def boundaryForcing(cmap, FCT,  XYZ, time, rhoHs, thetaHs) :
  thetac=5
  xc=500.0
  yc=500.0
  zc=0.0-600
  rc=250.0
  currentTime=time[0]    
  r=np.sqrt((XYZ[:,0]-xc)**2+(XYZ[:,1]-yc)**2+(XYZ[:,2]-zc)**2)
  thetaPert = np.empty(thetaHs.shape)
  outR = (r>rc)
  thetaPert[outR]=thetaHs[outR]
  inR=np.invert(outR)
  thetaPert[inR]=thetaHs[inR]+thetac/2.0*(1.0+np.cos(pi*r[inR]/rc))*np.sin(currentTime/period*2.0*pi)**2
  FCT[:,0]=0
  FCT[:,1]=0
  FCT[:,2]=0
  FCT[:,3]=0
  FCT[:,4]=rhoHs*thetaPert-rhoHs*thetaHs

def thetaHydrostatic(cmap, FCT) :
  FCT[:]=300
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
def exnerHydrostatic(cmap, FCT,  XYZ, thetaHs) :
  FCT[:]=1.0-g/(Cp*thetaHs)*XYZ[:,2]

def rhoHydrostatic(cmap, FCT, thetaHs, exnerHs) :
  FCT[:]=p0/(Rd*thetaHs)*exnerHs**(Cv/Rd)

def rhoThetaHydrostatic(cmap, FCT, rhoHs, thetaHs) :
  FCT[:]=rhoHs*thetaHs
        
def getVelocity(cmap, FCT, sol, rhoHs) :
  invrho=1/(rhoHs+sol[:,0])
  FCT[:,0]=sol[:,1]*invrho
  FCT[:,1]=sol[:,2]*invrho
  FCT[:,2]=sol[:,3]*invrho

def getRhop(cmap, FCT, sol) :
  FCT[:]=sol[:,0]

def getpp(cmap, FCT, sol, rhoHs, thetaHs) :
  rho=rhoHs+sol[:,0]
  rhoTheta = rhoHs*thetaHs+sol[:,4]
  pHs=p0*(rhoHs*thetaHs*Rd/p0)**gamma
  p=p0*(rhoTheta*Rd/p0)**gamma
  FCT[:]=p-pHs

def getpHs(cmap, FCT, sol, rhoHs, thetaHs) :
  pHs=p0*(rhoHs*thetaHs*Rd/p0)**gamma
  FCT[:]=pHs

def getp(cmap, FCT, sol, rhoHs, thetaHs) :
  rhoTheta = rhoHs*thetaHs+sol[:,4]
  p=p0*(rhoTheta*Rd/p0)**gamma
  FCT[:]=p

def getThetap(cmap, FCT, sol, rhoHs, thetaHs) :
  rho=rhoHs+sol[:,0]
  FCT[:]=(sol[:,4]+(rhoHs*thetaHs))/rho-thetaHs

thetaHsC = functionNumpy(1, thetaHydrostatic,[])
exnerHsC = functionNumpy(1, exnerHydrostatic, [XYZ, thetaHsC])
rhoHsC = functionNumpy(1, rhoHydrostatic, [thetaHsC, exnerHsC])
rhoThetaHsC = functionNumpy(1, rhoThetaHydrostatic, [rhoHsC, thetaHsC])

uvw=functionNumpy(3, getVelocity, [solution.getFunction(), rhoHsC])
rhop=functionNumpy(1, getRhop, [solution.getFunction()])
pp=functionNumpy(1, getpp, [solution.getFunction(), rhoHsC, thetaHsC])
thetap=functionNumpy(1, getThetap, [solution.getFunction(), rhoHsC, thetaHsC])

initF=functionNumpy(claw.getNbFields(), initialCondition, [XYZ])
solution.interpolate(initF)

rhoHs = dgDofContainer(groups, 1)
rhoHs.interpolate(rhoHsC)
rhoThetaHs = dgDofContainer(groups, 1)
rhoThetaHs.interpolate(rhoThetaHsC)

claw.setHydrostaticState(rhoHs,rhoThetaHs)
claw.setPhysicalConstants(gamma,Rd,p0,g)

blaise's avatar
   
blaise committed
133
134
nuh = functionConstant(.4);
nuv = functionConstant(.4);
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
zero = functionConstant(0);
claw.setNu(nuh, nuv, zero)

boundaryWall = claw.newBoundaryWall()
timeFunction = function.getTime()
boundaryForcingF=functionNumpy(5, boundaryForcing, [XYZ, timeFunction, rhoHsC, thetaHsC])
toReplace = VectorFunctorConst([function.getSolution()])
replaceBy = VectorFunctorConst([boundaryForcingF])
boundaryHeated = claw.newOutsideValueBoundaryGeneric("",toReplace,replaceBy)
claw.addBoundaryCondition('none', boundaryWall)
claw.addBoundaryCondition('bottom', boundaryWall)
claw.addBoundaryCondition('left', boundaryWall)
claw.addBoundaryCondition('right', boundaryWall)
claw.addBoundaryCondition('top', boundaryWall)
claw.addBoundaryCondition('bottom_surf', boundaryWall)
claw.addBoundaryCondition('top_surf', boundaryWall)
claw.addBoundaryCondition('bottom_bottom_surf', boundaryHeated)
claw.addBoundaryCondition('top_bottom_surf', boundaryWall)


#petsc = linearSystemPETScBlockDouble()
#petsc.setParameter("petscOptions",  "-pc_type lu -ksp_type preonly -pc_factor_mat_solver_package superlu_dist")

##petsc.setParameter("petscOptions",  "-ksp_atol 1e-12 -ksp_rtol 1e-12")
#if (Msg.GetCommSize()==1):
#    petsc.setParameter("petscOptions",  "-pc_type lu")#-ksp_atol 1e-12 -ksp_rtol 1e-12")
#dof = dgDofManager.newDGBlock(groups, claw.getNbFields(), petsc)
#timeIter = dgIMEXRK(claw, dof, 2)     #timeorder
#timeIter.getNewton().setVerb(1)     #Verbosity


claw.setFilterMode(FILTER_LINEAR);
167
168
169
#petscIm = linearSystemPETScBlockDouble()

petscIm = linearSystemPETScDouble()
170
171
172
173
#petscIm.setParameter("petscOptions",  "-ksp_type preonly -pc_type lu")
#if (Msg.GetCommSize()>1):
#  Msg.Fatal("Direct LU is for serial jobs only. Please change the solver or run serial")
#petscIm.setParameter("petscOptions",  "-ksp_atol 1.e-6 -ksp_rtol 1.e-9 -ksp_type gmres -ksp_gmres_restart 100 -pc_type jacobi -pc_factor_levels 6")
blaise's avatar
   
blaise committed
174
#petscIm.setParameter("petscOptions",  "-ksp_atol 1e-6 -ksp_rtol 1e-9 -pc_type asm")
175
176
177
178
#petscIm.setParameter("petscOptions",  "-ksp_atol 1e-6 -ksp_rtol 1e-9 -pc_type hypre -pc_hypre_type boomeramg -pc_hypre_boomeramg_max_iter 2 -pc_hypre_boomeramg_rtol 0 -ksp_monitor -log_summary")
#petscIm.setParameter("petscOptions",  "-ksp_atol 1e-6 -ksp_rtol 1e-9 -pc_type hypre -pc_hypre_type euclid -ksp_monitor -log_summary")
#petscIm.setParameter("petscOptions",  "-ksp_atol 1e-6 -ksp_rtol 1e-9")
#petscIm.setParameter("petscOptions",  "-ksp_atol 1e-6 -ksp_rtol 1e-9  -pc_type asm -pc_factor_in_place")
blaise's avatar
blaise committed
179

180
181
182
183
184
#mat_mkl_pardiso_65 : number of threads
#mat_mkl_pardiso_68 : verbosity level
#mat_mkl_pardiso_60 : out-of-core
#petscIm.setParameter("petscOptions",  "-ksp_type preonly -pc_type lu -pc_factor_mat_solver_package mkl_pardiso -mat_mkl_pardiso_68 0")

blaise's avatar
   
blaise committed
185
petscIm.setParameter("petscOptions",  "-ksp_type preonly -pc_type lu -pc_factor_mat_solver_package mumps")
blaise's avatar
blaise committed
186

187
188
189
190
# Out o core -mat_mumps_icntl_22 1

#dofIm = dgDofManager.newDGBlock(groups, claw.getNbFields(), petscIm)
dofIm = dgDofManager.newDG(groups, claw, petscIm)
blaise's avatar
blaise committed
191
#Last argument: compute sparsity from mesh? If not, computed from equation
192
timeIter = dgIMEXRK(claw, dofIm, IMEX_ARK4, None, True)     #timeorder 
193
194
timeIter.getNewton().setVerb(1)     #Verbosity

195
196
197
198
199
200
201
202
#if ( os.path.isfile("/scratch/sblaise/matrix.bin") ):
#  if (Msg.GetCommRank()==0):
#    print("Reloading matrix...")
#  petscIm.loadBinaryMat("/scratch/sblaise/matrix.bin")
#  if (Msg.GetCommRank()==0):
#    print("Done")


203
204
#timeIter = dgERKLinear(claw, groups, DG_ERK_22)

zhang's avatar
zhang committed
205
206
207
208
209
210
211
212
213
214
215
216
217
if compError: 
  # Compute relative error
  def getRefSolSq(cmap, FCT,  refSol) :
    FCT[:,:] = refSol[:,:]**2;
  solRefSq=functionNumpy(5, getRefSolSq, [reloaded.getFunction()])
  #
  def getError(cmap, FCT, refSol, compSol) :
    FCT[:,:] = (refSol[:,:]-compSol[:,:])**2
  error = functionNumpy(5, getError, [reloaded.getFunction(), solution.getFunction()])
  integratorError = dgFunctionIntegrator(groups, error)
  intErr = fullMatrixDouble(5,1)
  integratorRefSq = dgFunctionIntegrator(groups, solRefSq)
  intRefSq = fullMatrixDouble(5,1)
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

#Export data
def getExp(cmap, FCT, uvw, rhop, thetap, pp, rhoHs, thetaHs) :
  FCT[:,0]=uvw[:,0]
  FCT[:,1]=uvw[:,1]
  FCT[:,2]=uvw[:,2]
  FCT[:,3]=rhop[:]
  FCT[:,4]=thetap[:]
  FCT[:,5]=pp[:]
  FCT[:,6]=rhoHs[:]
  FCT[:,7]=thetaHs[:]
nCompExp=[3,1,1,1,1,1]
namesExp=["uvw","rhop","thetap","pp","rhoHsC", "thetaHsC"]
Exp=functionNumpy(sum(nCompExp), getExp, [uvw, rhop, thetap, pp, rhoHsC, thetaHsC])

zhang's avatar
zhang committed
233
#solution.exportFunctionVtk(Exp,'output/export', 0, 0,"solution",nCompExp,namesExp)
234
235
236
237
238
239
240
241

dt=(Tf-Ti)/nbSteps
if (Msg.GetCommRank()==0):
    print ("Time step:",dt)

t=Ti
n_export=0
start = time.clock()
242

243
for i in range(0,nbSteps):
blaise's avatar
blaise committed
244
245
246
  elapsed = (time.clock() - start)
  if (Msg.GetCommRank()==0):
    print ('[',elapsed,' s]')
zhang's avatar
zhang committed
247
248
249
250
251
  #if (i%50==0 or i==nbSteps-1):
  #  solution.exportFunctionVtk(Exp,'output/export', i, i,"solution",nCompExp,namesExp)
  #  if (Msg.GetCommRank()==0):
  #    elapsed = (time.clock() - start)
  #    print ('Exported. Elapsed time: ',elapsed,' s. Step ',i,' over ',nbSteps)
252
  norm = timeIter.iterate (solution, dt, t)
253
254
#  if (i==0):
#    petscIm.writeBinaryMat("/scratch/sblaise/matrix.bin")
255
256
257
258
259
260
261
262
263
  t=t+dt
  if (Msg.GetCommRank()==0):
    sys.stdout.write('.')
    sys.stdout.flush()
print ('')    
elapsed = (time.clock() - start)
if (Msg.GetCommRank()==0):
    print ('Time elapsed: ',elapsed,' s')

zhang's avatar
zhang committed
264
265
266
267
268
269
270
271
272
if compError:
  integratorError.compute(intErr) 
  integratorRefSq.compute(intRefSq)
  errp = sqrt(intErr(0,0)/intRefSq(0,0))
  erru = sqrt(intErr(1,0)/intRefSq(1,0))
  errv = sqrt(intErr(2,0)/intRefSq(2,0))
  errw = sqrt(intErr(3,0)/intRefSq(3,0))
  errthetap = sqrt(intErr(4,0)/intRefSq(4,0))
  errall = sqrt((intErr(0,0)+intErr(1,0)+intErr(2,0)+intErr(3,0)+intErr(4,0))/(intRefSq(0,0)+intRefSq(1,0)+intRefSq(2,0)+intRefSq(3,0)+intRefSq(4,0)))
273
#
zhang's avatar
zhang committed
274
  if (Msg.GetCommRank()==0):
275
    dataFile = file("periodic_convection_16_3d_IMEXARK4_poly4.dat","a")
zhang's avatar
zhang committed
276
277
    dataFile.write("%d %15f %e %e %e %e %e %e \n" % (nbSteps,elapsed,errp,erru,errv,errw,errthetap,errall))
    dataFile.close()
278
279

Msg.Exit(0)