-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_model.py
208 lines (182 loc) · 7.1 KB
/
data_model.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
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
# Copyright (C) <2012> <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
model.py
Evan Cummings
01.16.12
FEniCS solution to firn enthalpy / density profile. First arg should is the
end time in years, second is boolean val to plot the solution or not.
"""
from numpy import *
from fenics import *
from plot import *
from firn import *
from solvers import TransientSolver
from physical_constants import *
from scipy.interpolate import interp1d
from scipy.io import loadmat
import sys
import time
#===============================================================================
# constants :
# model variables :
spy = 365*24*60*60 # seconds per year ............... s/a
cpi = 2009. # const. heat capacitity of ice .. J/(kg K)
Tw = 273.15 # triple point water ............. degrees K
n = 100 # num of z-positions
rhos = 360. # initial density at surface ..... kg/m^3
rhoi = 917. # density of ice ................. kg/m^3
rhoin = 500. # initial density at surface ..... kg/m^3
adot = 0.25 # accumulation rate .............. m/a
Tavg = Tw - 20.0 # average temperature ............ degrees K
cp = 152.5 + 7.122*Tavg # heat capacity of ice ........... J/(kg K)
cp = cpi # heat capacity of ice ........... J/(kg K)
zs = 0. # surface start .................. m
zb = -500.0 # depth .......................... m
dt1 = 10.0*spy # time-step ...................... s
dt2 = 1/365.0*spy # time-step ...................... s
t0 = 0.0 # begin time ..................... s
tf = sys.argv[1] # end-time ....................... string
tf = float(tf)*spy # end-time ....................... s
bp = int(sys.argv[2]) # plot or not .................... bool
# enthalpy surface condition with cyclical 2-meter air temperature :
data = loadmat('data/CrawfordPt_MAR.mat')
times = data['years'].T[0] * spy
temp = data['TT_9_Monthly'].T[0] + Tw
dens = data['RO1_Monthly'].T[0]
dens[dens == 0.0] = 100.0
adot = data['SF_Monthly'].T[0] * 1000 * 365 / spy
rain = data['RF_Monthly'].T[0] * 1000 * 365 / spy
melt = data['ME_Monthly'].T[0] * 1000 * 365 / spy
t0 = 1000.0
tm = times[0]
tf = times[-1]
adoti = average(adot)
rhoin = average(dens)
Tin = average(temp)
raini = average(rain)
temp_i = interp1d(times, temp, 'slinear', bounds_error=False, fill_value=Tin)
dens_i = interp1d(times, dens, 'slinear', bounds_error=False, fill_value=rhoin)
adot_i = interp1d(times, adot, 'slinear', bounds_error=False, fill_value=adoti)
rain_i = interp1d(times, rain, 'slinear', bounds_error=False, fill_value=raini)
# enthalpy BC :
class BCH(Expression):
def __init__(self, t, c):
self.t = t
self.c = c
def eval(self, values, x):
values[0] = self.c * temp_i(self.t)
H_exp = BCH(times[0], cp)
# density BC :
class BCrho(Expression):
def __init__(self, t):
self.t = t
def eval(self, values, x):
values[0] = dens_i(self.t)
rho_exp = BCrho(times[0])
# velocity BC :
class BCw(Expression):
def __init__(self, t, rhos, adot):
self.t = t
self.rhos = rhos
self.adot = adot
def eval(self, values, x):
self.adot = adot_i(self.t)
values[0] = - rhoi / self.rhos * self.adot / spy
w_exp = BCw(times[0], dens[0], adot[0])
#===============================================================================
# initialize the firn object :
firn = Firn(Tin, rhoin, rhos, adoti, dt1)
firn.set_geometry(zs, zb)
firn.generate_uniform_mesh(n)
firn.refine_mesh(divs=3, i=1/3., k=1/20.)
firn.refine_mesh(divs=2, i=1/5., k=1/4.)
firn.refine_mesh(divs=2, i=1/5., k=1/4.)
firn.refine_mesh(divs=2, i=1/5., k=1/4.)
firn.set_parameters(FirnParameters())
firn.set_boundary_conditions(H_exp, rho_exp, w_exp)
firn.initialize_variables()
# load initialization data :
#firn.set_ini_conv(ex)
set_log_active(False)
params = {'newton_solver' : {'relaxation_parameter' : 1.00,
'maximum_iterations' : 25,
'error_on_nonconvergence' : False,
'relative_tolerance' : 1e-10,
'absolute_tolerance' : 1e-10}}
config = { 'mode' : 'transient',
't_start' : t0,
't_mid' : tm,
't_end' : tf,
'time_step' : dt2,
'dt_list' : [dt1, dt2],
'output_path' : '.',
'log' : True,
'coupled' :
{
'on' : False,
'inner_tol' : 0.0,
'max_iter' : 0
},
'enthalpy' :
{
'on' : False,
'use_surface_climate' : False,
'T_surface' : None,
'q_geo' : None,
'lateral_boundaries' : None,
'solver_params' : params,
'log' : True
},
'free_surface' :
{
'on' : False
},
'age' :
{
'on' : True,
'use_smb_for_ela' : True,
'ela' : None,
'solver_params' : params,
},
'surface_climate' :
{
'on' : False,
'T_ma' : None,
'T_ju' : None,
'beta_w' : None,
'sigma' : None,
'precip' : None
},
'plot' :
{
'on' : bp,
'zMin' : -25,
'zMax' : 5,
'wMax' : 300,
'wMin' : -1500,
'rhoMax' : 600,
'ageMax' : 15,
}}
F = TransientSolver(firn, config)
tstart = time.clock()
F.solve()
tfin = time.clock()
ttot = tfin - tstart
thours = ttot/60
print "total time to process %i years: %.2e mins" % ((tf - t0)/spy, thours)
# plot the surface height trend :
F.plot.plot_height(F.times, firn.ht, firn.origHt)