run_old.py 10.6 KB
Newer Older
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
from dgpy import *
from math import *
import os, time
from param import *



#GModel is a mesh database
model = GModel()

model.load(path_mesh + '_partitioned' + str(Msg.GetCommSize()) + '.msh' )

#Groups are used to differentiate differents elements of the mesh
#Surface elements and edges belong to different groups
groups = dgGroupCollection(model)

#Coordinates of each dof
XYZ = groups.getFunctionCoordinates()
XYZ_PC = functionPrecomputed(groups, 3, 3)
XYZ_PC.compute(XYZ)

# exportation de la colatitude et de la longitude

if exportLat:

  spherical_exp = functionNumpy(2, carToSph, [XYZ])
  glo_exp = functionNumpy(2, sphSimToGlo, [spherical_exp])
  groups.exportFunctionMsh(glo_exp,"lat")

#Spherical coordinates
spherical = functionNumpy(2, carToSph, [XYZ_PC])
spherical_PC = functionPrecomputed(groups, 3, 2)
spherical_PC.compute(spherical)

#Global spherical (??) coordinates
glo = functionNumpy(2, sphSimToGlo, [spherical_PC])
glo_PC = functionPrecomputed(groups, 3, 2)
glo_PC.compute(glo)


#2D shallow-water
claw = dgConservationLawShallowWater2d()
#Linear equations ?
if dissip is "quad" or dissip is "manning":

  claw.setIsLinear(False)
  claw.setLaxFriedrichs(False)
  
else:

  claw.setIsLinear(True)
  claw.setLaxFriedrichs(False)

#dofContainer for the solution
solution = dgDofContainer(groups, claw.getNbFields())
solution.setAll(0.)
solution.setFieldName(0, 'eta')
solution.setFieldName(1, 'u')
solution.setFieldName(2, 'v')



########################################################################
############################## BATHYMETRY ##############################
########################################################################

# si la bathymetrie est obtenue par les donnees, il faut l'augmenter a 
# un minimum sinon on a des zones assechees, ce qui genere des 
# problemes.
# UPGRADE: utiliser wetting-drying

#dofContainer for the bathymetry
bath = dgDofContainer(groups, 1)
bath.setFieldName(0, 'bath')

76
bath.importIdx(path_mesh + "B" + bathy_acc + "_bath_part/" + domain + mesh_acc + "B" + bathy_acc + "_bath_part.idx")
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

bath_PC = functionPrecomputed(groups, 3, 1)
bath_PC.compute(bath.getFunction())

bathGradient_PC = functionPrecomputed(groups, 3, 3)
bathGradient_PC.compute(bath.getFunctionGradient())


claw.setBathymetry(bath.getFunction())
claw.setBathymetryGradient(bathGradient_PC)

########################################################################
############################ WETTING-DRYING ############################
########################################################################

#claw.setMovingBathWettingDrying(alpha)
#bath_mod = claw.getBathymetry()

########################################################################
############################# DISSIPATION ##############################
########################################################################

# represente la dissipation due e la friction de fond selon un modele
# lineaire, quadratique ou de Chezy-Manning

if dissip is "lin":

  #constante
  gammaFunction = functionConstant(gamm_lin)
  gamma_PC = functionPrecomputed(groups, 3, 1)
  gamma_PC.compute(gammaFunction)
  claw.setLinearDissipation(gamma_PC)

elif dissip is "quad": 

  Cd = functionNumpy(1, funQuad, [solution.getFunction(), claw.getBathymetry()])
  gammaFunction = Cd
  claw.setQuadraticDissipation(Cd)

elif dissip is "manning":

  #chezy-maning
  manning = functionConstant(coeff_manning)
  Cd = functionNumpy(1, funManning, [solution.getFunction(), claw.getBathymetry(), manning])
  gammaFunction = Cd
  claw.setQuadraticDissipation(Cd)

else:

  gammaFunction = functionConstant(0.0)
  gamma_PC = functionPrecomputed(groups, 3, 1)
  gamma_PC.compute(gammaFunction)
  claw.setLinearDissipation(gamma_PC)

########################################################################
############################# DIFFUSIVITY ##############################
########################################################################

# Represente le terme de diffusion en utilisant la diffusion moleculaire 
# ou le modele de smagorinsky (smagorinsky ne fonctionne pas pour des 
# maillages "grossiers".

# Area of each element.
area = dgDofContainer(groups, 1)
area.computeArea()

if diff is "smag" and mesh_acc is "f":

  nu = functionNumpy(1, smagorinsky, [solution.getFunctionGradient(), area.getFunction()]) # smagorinsky

elif diff is "smag" and mesh_acc != "f":
  
  print("desole, smagorinsky n'est pas disponible pour un maillage aussi grossier. ")
  choix = raw_input("Voulez-vous implementer la viscosite moleculaire(O) a la place ou une viscosite nulle (1)?")

  if choix == 0:
    nu = functionConstant(diff_mol) #(cf lorenz et al 2010 et these guillaume)
  else:
    nu = functionConstant(0.0)

elif diff is "mol":

  nu = functionConstant(diff_mol) #(cf lorenz et al 2010 et these guillaume)

else: 

  Di = 0.0
  nu = functionConstant(Di)


claw.setDiffusivity(nu)


########################################################################
################################ SOURCE ################################
########################################################################

#Tide - Source term
if setTide:
  tide = functionNumpy(2, tideDyn, [glo_PC, function.getTime()])
else:
  tide = functionConstant([0.,0.])

#Wind - Source term
if setWind:
  wind = functionNumpy(2, wind, [bath_PC, solution.getFunction(), function.getTime()])
else:
  wind = functionConstant([0.,0.])

#Set the source term in the swe2D
source = functionNumpy(2, sum2, [tide, wind])
claw.setSource(source)

#Coriolis
coriolis = functionNumpy(1, coriolis, [spherical_PC]) 
coriolis_PC = functionPrecomputed(groups, 3, 1)
coriolis_PC.compute(coriolis)
claw.setCoriolisFactor(coriolis_PC)

#If we use stereographic coordinates
#claw.setIsSpherical(R)

# Gravity acceleration
claw.setGravity(g)

#Boundary conditions
claw.addBoundaryCondition('Wall',claw.newBoundaryWall(slip))


########################################################################
######################## NUMERICAL RESOLUTION ##########################
########################################################################


sys = linearSystemPETScBlockDouble()
sys.setParameter("petscOptions", "-ksp_rtol 1e-3")
dof = dgDofManager.newDGBlock(groups, 3, sys)
rk = dgDIRK(claw, dof, 2)
rk.getNewton().setRtol(1e-6)
rk.getNewton().setVerb(n_print_RK)

t = 0.0
#export in idx
exporterIdx = dgIdxExporter(solution, path_output + '/idx')
exporterIdx.exportIdx(0, 0.)

def velocityVector(FCT, solution):
  for i in range(FCT.size1()):
    FCT.set(i,0,solution(i,1))
    FCT.set(i,1,solution(i,2))
    FCT.set(i,2,0)
    
velocityVec = functionPython(3, velocityVector, [solution.getFunction()]);

######################### Mass conservation ###########################

f = open(path_output + "/Energy.dat","w")
f.write("start\n")
f.close()
f2 = open(path_output + "/Mass.dat","w")
f2.write("start\n")
f2.close()
#SLIM function which computes total water mass.
eta = functionNumpy(1,  getVector,  [function.getSolution()])
mass = functionNumpy(1,  sum3,  [function.getSolution(), bath_PC])
#Mass integration type.
integrator_mass_var = dgFunctionIntegrator(groups, eta, solution)
integrator_mass_tot = dgFunctionIntegrator(groups, mass, solution)
#Current total water mass.
totalMass = fullMatrixDouble(1, 1)
varMass = fullMatrixDouble(1, 1)
integrator_mass_tot.compute(totalMass, "")
integrator_mass_var.compute(varMass, "")
######################## Energy conservation ##########################

#To compute the potential energy
E_p = fullMatrixDouble(1,1)
pot = functionNumpy(1,  potentialEnergy,  [function.getSolution(), mass])
integratorpot = dgFunctionIntegrator(groups,pot,solution) 
integratorpot.compute(E_p)

#To compute the kinetic energy
E_k = fullMatrixDouble(1,1)
kin = functionNumpy(1,  kineticEnergy,  [function.getSolution(), mass])
integratorkin = dgFunctionIntegrator(groups,kin,solution) 
integratorkin.compute(E_k)

#To compute the dissipation
dissip = fullMatrixDouble(1,1)
integratordissip = dgFunctionIntegrator(groups,gammaFunction) 
integratordissip.compute(dissip)

####################### Viscosity Exportation #########################

grad = dgDofContainer(groups, 1)
grad.setAll(0.)
grad.setFieldName(0, 'nu')
exporterIdxgrad = dgIdxExporter(grad, path_output + '/idxgrad')
exporterIdxgrad.exportIdx(0, 0.)


####################### Surface Modification ##########################

if modif:
  comp = dgDofContainer(groups, 1)
  comp.setAll(0.)
  comp.setFieldName(0, 'comp')
  exporterIdxcomp = dgIdxExporter(comp, path_output + '/idxcomp')
  exporterIdxcomp.exportIdx(0, 0.)

######################### Solver Time Loop ############################

for i in range (1, nt):
  
  rk.iterate(solution, dt, t)
  t = t + dt
  turn = t // T
  turnRest = float(t % T)
  days = t / T
   
  grad.interpolate(nu)

########################## Output Writting ############################

  if (i%n_print_cons == 0):
    print('|ITER|',i,'|TURN|',turn,'|DAYS|',days,'|DT|',dt, '|floor(t)|', floor(t))
    
  if (i%n_print_file == 0):
    if modif:
      elevation = functionNumpy(1, sum3, [solution.getFunction(), old_bath])
      comp.interpolate(elevation)
      exporterIdxcomp.exportIdx(i, t)

    #groups.exportFunctionMsh(velocityVec, path_output + "/titanV-%06d" % i, t, i)
    #solution.exportMsh(path_output + "/titan-%06d" % i, t, i)
    exporterIdx.exportIdx(i, t)
    #exporterIdxgrad.exportIdx(i, t)

    integratordissip.compute(dissip)
    integratorpot.compute(E_p)
    integratorkin.compute(E_k)
    E_tot = E_p(0,0) + E_k(0,0) - dissip(0,0)

    f = open(path_output + "/Energy.dat","a")
    f.write("Energy %8.10f time %d\n" % (E_tot,t))
    f.close()


  integrator_mass_tot.compute(totalMass, "")
  integrator_mass_var.compute(varMass, "")
  massT = totalMass(0, 0)
  delta_mass = varMass(0,0)
  error_mass = delta_mass/massT
  f2 = open(path_output + "/Mass.dat","a")
  f2.write("mass %8.10f time %d\n" % (error_mass,t))
  f2.close()

  if(isnan(massT)):
   Msg.Fatal("NaN detected!")

############### Elevation Comparison between two days #################

  if i==1378:# jour 3

    day1=dgDofContainer(groups,1)
    day1.setAll(0.0)
    temp = functionNumpy(1, getVector,[solution.getFunction()])
    day1.interpolate(temp)

  if i==1844:# jour 4

    day2=dgDofContainer(groups,1)
    day2.setAll(0.0)
    temp2 =functionNumpy(1, getVector,[solution.getFunction()])
    day2.interpolate(temp2)

  if i==2296:# jour 5

    day3=dgDofContainer(groups,1)
    day3.setAll(0.0)
    temp3 =functionNumpy(1, getVector,[solution.getFunction()])
    day3.interpolate(temp3)

    diff_cont = dgDofContainer(groups,1)
    diff = functionNumpy(1, diff2, [day1.getFunction(), day2.getFunction()])
    diff_cont.interpolate(diff)
    diff_cont.exportMsh('diff')
    

solution.save('solution_old')    
  
  

Msg.Exit(0)