forked from acroucher/PyTOUGH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t2incons.py
executable file
·288 lines (262 loc) · 13.2 KB
/
t2incons.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
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
"""For reading and writing TOUGH2 initial conditions files.
Copyright 2012 University of Auckland.
This file is part of PyTOUGH.
PyTOUGH is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
PyTOUGH 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with PyTOUGH. If not, see <http://www.gnu.org/licenses/>."""
from mulgrids import *
from fixed_format_file import *
t2incon_format_specification = {
'header_short': [['title'], ['5s']],
'header_long': [['s1', 'nele', 's2', 'sumtim'], ['31s', '5d', '19s', '12.6e']],
'incon1': [['name', 'nseq', 'nadd', 'porx'],
['5s', '5d', '5d', '15.9e']],
'incon1_toughreact': [['name', 'nseq', 'nadd', 'porx', 'k1', 'k2', 'k3'],
['5s', '5d', '5d'] + ['15.9e'] * 4],
'incon2': [['x1', 'x2', 'x3', 'x4'], ['20.13e'] * 4],
'timing': [['kcyc', 'iter', 'nm', 'tstart', 'sumtim'],
['5d'] * 3 + ['15.9e'] * 2],
'timing_toughreact': [['kcyc', 'iter', 'nm', 'tstart', 'sumtim'],
['6d'] * 2 + ['3d'] + ['15.9e'] * 2]
}
class t2incon_parser(fixed_format_file):
"""Class for parsing TOUGH2 incon file."""
def __init__(self, filename, mode, read_function = fortran_read_function):
super(t2incon_parser,self).__init__(filename, mode,
t2incon_format_specification, read_function)
class t2blockincon(object):
"""Class for a single set of initial conditions at one block."""
def __init__(self, variable, block = '', porosity = None, permeability = None,
nseq = None, nadd = None):
self.block = block
self.variable = list(variable)
self.porosity = porosity
self.permeability = permeability
self.nseq, self.nadd = nseq, nadd
def __getitem__(self, key): return self.variable[key]
def __setitem__(self, key, value): self.variable[key] = value
def __repr__(self):
result = self.block + ':' + str(self.variable)
if self.porosity is not None:
result += ' ' + str(self.porosity)
if self.permeability is not None:
result += ' ' + str(self.permeability)
if self.nseq is not None:
result += ' (' + str(self.nseq) + ', ' + str(self.nadd) + ')'
return result
class t2incon(object):
"""Class for a set of initial conditions over a TOUGH2 grid."""
def __init__(self, filename = '',
read_function = fortran_read_function, num_variables = None):
self.simulator = 'TOUGH2'
self.read_function = read_function
self.empty()
if filename: self.read(filename, num_variables)
def __getitem__(self, key):
if isinstance(key, (int, slice)): return self._blocklist[key]
elif isinstance(key, str): return self._block[key]
else: return None
def __setitem__(self, key, value):
if isinstance(value,(list,tuple)):
value = t2blockincon(value,key)
if value.block != key: value.block = key
self.add_incon(value)
def __repr__(self):
return self.simulator + ' initial conditions for ' + \
str(self.num_blocks) + ' blocks'
def empty(self):
self._block = {}
self._blocklist = []
self.timing = None
def get_num_blocks(self): return len(self._blocklist)
num_blocks = property(get_num_blocks)
def get_num_variables(self):
if self.num_blocks > 0: return len(self._blocklist[0].variable)
else: return 0
num_variables = property(get_num_variables)
def get_variable(self):
"""Returns an array of initial condition variables."""
return np.array([inc.variable for inc in self._blocklist])
def set_variable(self, val):
"""Sets all initial condition variables to values in an array."""
for i, inc in enumerate(self._blocklist): inc.variable = val[i]
variable = property(get_variable, set_variable)
def get_porosity(self):
"""Returns an array of porosities for each block."""
return np.array([inc.porosity for inc in self._blocklist])
def set_porosity(self, por):
if isinstance(por, np.ndarray):
if len(por) == self.num_blocks:
for i, p in enumerate(por): self._blocklist[i].porosity = p
else:
raise Exception('Porosity array is the wrong length (' + \
str(len(por)) + ').')
elif isinstance(por, float) or (por == None):
for blk in self._blocklist: blk.porosity = por
porosity = property(get_porosity, set_porosity)
def get_permeability(self):
"""Returns an array of permeabilities for each block."""
return np.array([inc.permeability for inc in self._blocklist])
def set_permeability(self, perm):
if isinstance(perm, np.ndarray):
shape = np.shape(perm)
if shape == (3,):
from copy import copy
for blk in self._blocklist: blk.permeability = copy(perm)
elif shape == (self.num_blocks, 3):
for i, p in enumerate(perm): self._blocklist[i].permeability = p
else:
raise Exception('Permeability array is the wrong shape (' + \
str(shape) + ').')
elif isinstance(perm, float):
for blk in self._blocklist:
blk.permeability = perm * np.ones(3)
elif perm == None:
for blk in self._blocklist:
blk.permeability = None
porosity = property(get_porosity, set_porosity)
permeability = property(get_permeability, set_permeability)
def get_blocklist(self):
"""Returns an ordered list of blocks."""
return [inc.block for inc in self._blocklist]
blocklist = property(get_blocklist)
def add_incon(self, incon):
"""Adds a t2blockincon."""
if incon.block in self._block:
i = self._blocklist.index(self._block[incon.block])
self._blocklist[i] = incon
else: self._blocklist.append(incon)
self._block[incon.block] = incon
def insert_incon(self, index, incon):
"""Inserts a t2blockincon at the specified index."""
self._blocklist.insert(index, incon)
self._block[incon.block] = incon
def delete_incon(self, block):
"""Deletes a t2blockincon."""
if block in self._block:
incon = self._block[block]
del self._block[block]
self._blocklist.remove(incon)
def read(self, filename, num_variables = None):
"""Reads initial conditions from file."""
self.empty()
mode = 'r' if sys.version_info > (3,) else 'rU'
infile = t2incon_parser(filename, mode, read_function = self.read_function)
infile.readline() # skip header
finished = False
timing = False
while not finished:
line = infile.readline()
if line.strip():
if line.startswith('+++'):
finished = True
timing = True
else:
line = padstring(line)
[blkname, nseq, nadd, porosity, k1, k2, k3] = \
infile.parse_string(line, 'incon1_toughreact')
if valid_blockname(blkname):
blkname = fix_blockname(blkname)
if (k1 is None or k2 is None or k3 is None): permeability = None
else:
permeability = np.array([k1, k2, k3])
self.simulator = 'TOUGHREACT'
vals, more = [], True
while more:
linevals = infile.read_values('incon2')
while linevals and linevals[-1] is None: linevals.pop()
vals += linevals
more = False if num_variables is None else \
len(vals) < num_variables
incon = t2blockincon(vals, blkname, porosity, permeability,
nseq, nadd)
self.add_incon(incon)
else:
raise Exception('Invalid block name (' + blkname + \
') in incon file: ' + filename)
else: finished = True
self.timing = None
if timing:
line = infile.readline()
if line.strip():
line = padstring(line)
timing_fmt = 'timing'
if self.simulator == 'TOUGHREACT': timing_fmt += '_toughreact'
[kcyc, itr, nm, tstart, sumtim] = infile.parse_string(line, timing_fmt)
self.timing = {'kcyc': kcyc, 'iter': itr, 'nm': nm,
'tstart': tstart, 'sumtim': sumtim}
infile.close()
def write(self, filename, reset = True):
"""Writes initial conditions to file."""
outfile = t2incon_parser(filename, 'w')
if (self.timing is None) or reset:
outfile.write_values(['INCON'], 'header_short')
else:
outfile.write_values(['INCON -- INITIAL CONDITIONS FOR', self.num_blocks,
' ELEMENTS AT TIME ', self.timing['sumtim']], 'header_long')
for incon in self._blocklist:
blkname = unfix_blockname(incon.block)
if self.simulator == 'TOUGHREACT' and incon.permeability is not None:
outfile.write_values([blkname, incon.nseq, incon.nadd, incon.porosity] +
list(incon.permeability), 'incon1_toughreact')
else:
outfile.write_values([blkname, incon.nseq, incon.nadd, incon.porosity], 'incon1')
vals = list(incon.variable)
while vals:
linelen = min(len(vals), 4)
linevals = vals[:linelen]
outfile.write_values(linevals, 'incon2')
vals = vals[linelen:]
if (self.timing is None) or reset: outfile.write('\n\n')
else:
outfile.write('+++\n')
timing_fmt = 'timing'
if self.simulator == 'TOUGHREACT': timing_fmt += '_toughreact'
outfile.write_value_line(self.timing, timing_fmt)
outfile.close()
def transfer_from(self, sourceinc, sourcegeo, geo, mapping = {}, colmapping = {}):
"""Transfers initial conditions from another t2incon object, using the
two corresponding geometry objects, and the optionally
specified block and column mappings between them (these are
created if not specified). If there are no atmosphere blocks
in the source initial conditions, default atmosphere
conditions are assigned if necessary.
"""
self.empty()
if (colmapping == {}) or (mapping == {}):
mapping, colmapping = sourcegeo.block_mapping(geo, True)
from copy import copy
# atmosphere blocks:
default_atm_incons = t2blockincon([1.013e5, 20.])
if geo.atmosphere_type == 0: # single atmosphere block
atmblk = geo.block_name(geo.layerlist[0].name, geo.atmosphere_column_name)
if sourcegeo.atmosphere_type == 0: self[atmblk] = copy(sourceinc[0])
elif sourcegeo.atmosphere_type == 1:
# take average over source column atmosphere incons
varsum = np.zeros(len(sourceinc[0].variable))
for col in sourcegeo.columnlist:
blk = sourcegeo.block_name(sourcegeo.layerlist[0].name, col.name)
varsum+=np.array(sourceinc[blk].variable)
self[atmblk] = t2blockincon(varsum / sourcegeo.num_columns)
else: self[atmblk] = copy(default_atm_incons)
elif geo.atmosphere_type == 1:
# atmosphere block over each column
if sourcegeo.atmosphere_type == 0:
# broadcast single source atmosphere incons to each column
for col in geo.columnlist:
blk = geo.block_name(geo.layerlist[0].name, col.name)
self[blk] = copy(sourceinc[0])
elif sourcegeo.atmosphere_type == 1:
# atmosphere over each column in both source and destination
for col in geo.columnlist:
mappedcol = colmapping[col.name]
oldatmosblockname = sourcegeo.block_name(sourcegeo.layerlist[0].name, mappedcol)
blk = geo.block_name(geo.layerlist[0].name, col.name)
self[blk] = copy(sourceinc[oldatmosblockname])
else: # use default
for col in geo.columnlist:
blk = geo.block_name(geo.layerlist[0].name, col.name)
self[blk] = copy(default_atm_incons)
# underground blocks:
for blk in geo.block_name_list[geo.num_atmosphere_blocks:]:
self[blk] = copy(sourceinc[mapping[blk]])