-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
268 lines (212 loc) · 7.4 KB
/
utils.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
import os
import errno
import numpy as np
import re
from . import geometry as ge
# from tempfile import mkdtemp
# from joblib import Memory
# cachedir = mkdtemp()
# memory = Memory(cachedir=None, verbose=False)
# @memory.cache
def compute_trans(G, K):
nx = G['nx']; hx = G['hx']
ny = G['ny']; hy = G['hy']
nz = G['nz']; hz = G['hz']
L = 1.0/K
tx = 2*hy*hz/hx; TX = np.zeros((nz,ny,nx+1))
ty = 2*hx*hz/hy; TY = np.zeros((nz,ny+1,nx))
tz = 2*hx*hy/hz; TZ = np.zeros((nz+1,ny,nx))
TX[:,:,1:nx] = tx/(L[:,:,0:nx-1,0]+L[:,:,1:nx,0])
TY[:,1:ny,:] = ty/(L[:,0:ny-1,:,1]+L[:,1:ny,:,1])
TZ[1:nz,:,:] = tz/(L[0:nz-1,:,:,2]+L[1:nz,:,:,2])
return TX,TY,TZ
def interpolate(P_cg, G, CG, bases, corr):
N_cg = len(CG['cells'])
P = np.zeros((G['nz']*G['ny']*G['nx'],1))
for k in range(N_cg):
P = P + P_cg[k]*bases[k]
P = P + corr
return np.array(P)
def crop_bases(CG, DG, bases):
""" Crop the set of bases """
N_cg = CG['nz']*CG['ny']*CG['nx']
geo = DG['bases_geo']
cropped_bases = np.zeros_like(bases)
for k in range(N_cg):
basis = bases[k]
shape = np.shape(geo[k]['cells'])
cells = geo[k]['cells'].ravel()
cropped_bases[k] = np.reshape(basis[cells].toarray(), shape)
return cropped_bases
def get_mini_Ks(K, DG):
""" Generate the permeability patches """
Ks = []
for geo in DG['bases_geo']:
dy,dx = geo['cells_idxs']
Ks.append(K[:,dy,dx,:])
return Ks
def mkdir_p(path):
""" Create a directory if doesn't exist """
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def rot90(x, n):
""" n times rotation ignoring last axis """
rot_x = []
for i in range(x.shape[-1]):
rot_x.append(np.rot90(np.squeeze(x[...,i], axis=0), n))
rot_x = np.array(rot_x)
return np.rollaxis(rot_x, 0, rot_x.ndim)[None,...]
def pu_scale(bases, DG):
"""
Scale the bases in order to ensure partition of unity.
We assume a_i' = a_i*(1 + c).
Note: 'bases' is modified in place.
"""
bases_sum = sum(bases).toarray()
N_cg = len(bases)
for k in range(N_cg):
cells = DG['bases_geo'][k]['cells'].ravel()
bases[k][cells] = bases[k][cells]/bases_sum[cells]
def pu_correct(bases, p, DG):
"""
Add a correction term to ensure partition of unity.
We assume a_i' = a_i + c*a_i^p.
When p = 1 this is the same as pu_scale(),
i.e. linear scaling a_i' = a_i*(1 + c).
Note: 'bases' is modified in place.
"""
bases_sum = sum(bases).toarray()
bases_psum = np.zeros_like(bases_sum)
N_cg = len(bases)
# compute bases_psum
for k in range(N_cg):
cells = DG['bases_geo'][k]['inner_cells'].ravel()
bases_psum[cells] += bases[k][cells].toarray()**p
# correct bases
for k in range(N_cg):
cells = DG['bases_geo'][k]['inner_cells'].ravel()
bases[k][cells] += \
bases[k][cells].toarray()**p*(1-bases_sum[cells])/bases_psum[cells]
def parse_perm(dirname):
"""
Extract arguments from perm dirname.
Returns dictionary with extracted arguments.
"""
[nz, ny, nx, length, sigma, nperm] = re.findall(r'\d+\.\d+|\d+',
dirname)
args = {}
args['nz'] = int(nz)
args['ny'] = int(ny)
args['nx'] = int(nx)
args['length'] = float(length)
args['sigma'] = float(sigma)
args['nperm'] = int(nperm)
return args
def unparse_perm(nz, ny, nx, length, sigma, nperm):
"""
Create directory name based on arguments.
"""
dirname = str(nz) + 'x' + str(ny) + 'x' + str(nx) \
+ '_' + 'L' +str(length) + '_' + 's' + str(sigma) \
+ '_' + 'n' + str(nperm)
return dirname
def parse_dataset(dirname):
"""
Extract arguments from dataset dirname.
Returns dictionary with extracted arguments.
"""
[Ny, Nx, nz, ny, nx, length, sigma, nperm] = \
re.findall(r'\d+\.\d+|\d+', dirname)
args = {}
args['Ny'] = int(Ny)
args['Nx'] = int(Nx)
args['nz'] = int(nz)
args['ny'] = int(ny)
args['nx'] = int(nx)
args['length'] = float(length)
args['sigma'] = float(sigma)
args['nperm'] = int(nperm)
return args
def unparse_dataset(Ny, Nx, nz, ny, nx, length, sigma, nperm):
"""
Create directory name based on arguments.
"""
dirname = str(Nx) + 'x' + str(Nx) + '_' + \
str(nz) + 'x' + str(ny) + 'x' + str(nx) + '_' + \
'L' +str(length) + '_' + 's' + str(sigma) + '_' + \
'n' + str(nperm)
return dirname
def read_dataset(datadir, dim=1, ravel=True):
args = parse_dataset(datadir)
# read data as (n_samples, my, mx, dim)
shp = get_mm_shapes({'ny':args['ny'], 'nx':args['nx']},
{'ny':args['Ny'], 'nx':args['Nx']},
args['nperm'], dim,
per_perm_sample=False, ravel=ravel)
X_corner = np.memmap(os.path.join(datadir,'X_corner'),
dtype=float,
shape=shp['corX'],
mode='r')
Y_corner = np.memmap(os.path.join(datadir,'Y_corner'),
dtype=float,
shape=shp['corY'],
mode='r')
X_side = np.memmap(os.path.join(datadir,'X_side'),
dtype=float,
shape=shp['sidX'],
mode='r')
Y_side = np.memmap(os.path.join(datadir,'Y_side'),
dtype=float,
shape=shp['sidY'],
mode='r')
X_inner = np.memmap(os.path.join(datadir,'X_inner'),
dtype=float,
shape=shp['innX'],
mode='r')
Y_inner = np.memmap(os.path.join(datadir,'Y_inner'),
dtype=float,
shape=shp['innY'],
mode='r')
return {'X_inner': X_inner, 'Y_inner': Y_inner,
'X_side': X_side, 'Y_side': Y_side,
'X_corner': X_corner, 'Y_corner': Y_corner}
def get_mm_shapes(G, CG, nperm, dim=1, per_perm_sample=True,
ravel=False):
inn_shape, sid_shape, cor_shape = ge.get_basis_shapes(G, CG)
n_inn, n_sid, n_cor = ge.get_basis_amounts(CG)
if per_perm_sample:
mm_innX = (nperm, n_inn)
mm_sidX = (nperm, n_sid)
mm_corX = (nperm, n_cor)
mm_innY = (nperm, n_inn)
mm_sidY = (nperm, n_sid)
mm_corY = (nperm, n_cor)
else:
mm_innX = (nperm*n_inn,)
mm_sidX = (nperm*n_sid,)
mm_corX = (nperm*n_cor,)
mm_innY = (nperm*n_inn,)
mm_sidY = (nperm*n_sid,)
mm_corY = (nperm*n_cor,)
if ravel:
mm_innX += (inn_shape[0]*inn_shape[1]*dim,)
mm_sidX += (sid_shape[0]*sid_shape[1]*dim,)
mm_corX += (cor_shape[0]*cor_shape[1]*dim,)
mm_innY += (inn_shape[0]*inn_shape[1],)
mm_sidY += (sid_shape[0]*sid_shape[1],)
mm_corY += (cor_shape[0]*cor_shape[1],)
else:
mm_innX += inn_shape + (dim,)
mm_sidX += sid_shape + (dim,)
mm_corX += cor_shape + (dim,)
mm_innY += inn_shape
mm_sidY += sid_shape
mm_corY += cor_shape
return {'innX':mm_innX, 'innY':mm_innY,
'sidX':mm_sidX, 'sidY':mm_sidY,
'corX':mm_corX, 'corY':mm_corY}