Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
168 lines (137 loc) · 8.08 KB
/
Copy pathtest.py
File metadata and controls
168 lines (137 loc) · 8.08 KB
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
"""Public checks for block coefficients and the supplied solver."""
import unittest
from copy import deepcopy
from unittest.mock import patch
import tempfile
from pathlib import Path
import numpy as np
import scipy.sparse
import yaml
from assignment13 import OneDimReservoir
from verification_cases import case, independent_system
def bare(inputs):
"""Inspect input normalization or a coefficient without assembling matrices."""
with patch.object(OneDimReservoir, 'fill_matrices'):
return OneDimReservoir(inputs)
def expected_steps(inputs, steps, initial=None):
t, b, q = independent_system(inputs)
dt = inputs['numerical']['time step']
solver = inputs['numerical']['solver']
theta = {'explicit': 1., 'implicit': 0.}.get(solver) if isinstance(solver, str) else solver['mixed method']['theta']
p = np.full(len(b), inputs['initial conditions']['pressure']) if initial is None else initial.copy()
storage = np.diag(b) / dt
for _ in range(steps):
p = np.linalg.solve(storage + (1 - theta) * t, (storage - theta * t) @ p + q)
return p
class TestProvidedParsing(unittest.TestCase):
def test_scalar_broadcast_and_default_width(self):
i = case(); i['reservoir'].update(permeability=50., porosity=.2, **{'cross sectional area': 200000.})
i['numerical'].pop('delta x'); m = bare(i)
for name, value in [('permeability', 50), ('porosity', .2), ('area', 200000), ('delta_x', 2500)]:
np.testing.assert_array_equal(getattr(m, name), np.full(4, value))
def test_vector_types_and_independent_copies(self):
for make in [list, tuple, np.array]:
i = case(); values = make([11., 25., 70., 19.]); i['reservoir']['permeability'] = values
m = bare(i); np.testing.assert_array_equal(m.permeability, values)
m.permeability[0] = 99
self.assertEqual(values[0], 11)
def test_scalar_width(self):
i = case(); i['numerical']['delta x'] = 1200.
np.testing.assert_array_equal(bare(i).delta_x, np.full(4, 1200.))
def test_reject_invalid_block_values(self):
for key in ['permeability', 'porosity', 'cross sectional area']:
for value in [[1, 2], [[1, 2], [3, 4]], [1, 2, 3, 0], -1, True, 'file.txt', [1, 2, 3, np.inf], [1, 2, 3, np.nan], [1, True, 2, 3]]:
with self.subTest(key=key, value=value):
i = case(); i['reservoir'][key] = value
with self.assertRaises(ValueError): bare(i)
def test_reject_invalid_widths(self):
for value in [[100, 200], 0, False, [1, 2, 3, -1]]:
i = case(); i['numerical']['delta x'] = value
with self.assertRaises(ValueError): bare(i)
def test_reject_invalid_fluid_and_timestep(self):
for key in ['viscosity', 'formation volume factor', 'compressibility']:
i = case(); i['fluid']['water'][key] = 0
with self.assertRaises(ValueError): bare(i)
i = case(); i['numerical']['time step'] = np.nan
with self.assertRaises(ValueError): bare(i)
def test_grid_count(self):
for n in [1, True, 4.5]:
i = case(); i['numerical']['number of grids']['x'] = n
with self.assertRaises(ValueError): bare(i)
def test_yaml_input(self):
i = case()
with tempfile.TemporaryDirectory() as folder:
p = Path(folder) / 'inputs.yml'; p.write_text(yaml.safe_dump(i))
m = bare(str(p)); np.testing.assert_array_equal(m.delta_x, i['numerical']['delta x'])
class TestCoefficients(unittest.TestCase):
def test_uniform_limit(self):
i = case(); i['reservoir'].update(permeability=50., porosity=.2, **{'cross sectional area': 200000.})
i['numerical'].pop('delta x'); m = bare(i)
self.assertAlmostEqual(m.compute_transmissibility(0, 1), 4000.)
self.assertAlmostEqual(m.compute_transmissibility(3, 3), 4000.)
self.assertAlmostEqual(m.compute_accumulation(2), 100.)
def test_unequal_resistances_and_symmetry(self):
i = case(); m = bare(i)
# First face: half-widths 1000 and 1500 ft; kA values 2e6 and 15e6.
expected = 1 / (1000 / 2e6 + 1500 / 15e6)
self.assertAlmostEqual(m.compute_transmissibility(0, 1), expected)
self.assertAlmostEqual(m.compute_transmissibility(1, 0), expected)
def test_full_block_boundary_value(self):
m = bare(case())
self.assertAlmostEqual(m.compute_transmissibility(3, 3), 20 * 220000 / 3500)
def test_each_accumulation(self):
m = bare(case())
for j, expected in enumerate([72., 103.5, 54., 123.2]):
self.assertAlmostEqual(m.compute_accumulation(j), expected)
def test_fluid_scaling(self):
i = case(); base = bare(i)
i['fluid']['water'].update(viscosity=2., **{'formation volume factor': 1.25})
m = bare(i)
self.assertAlmostEqual(m.compute_transmissibility(1, 2), base.compute_transmissibility(1, 2) / 2.5)
self.assertAlmostEqual(m.compute_accumulation(1), base.compute_accumulation(1) / 1.25)
def test_no_conversion_in_coefficients(self):
i = case(); a = bare(i); i['conversion factor'] = .037; b = bare(i)
self.assertEqual(a.compute_transmissibility(0, 1), b.compute_transmissibility(0, 1))
self.assertEqual(a.compute_accumulation(0), b.compute_accumulation(0))
class TestFlow(unittest.TestCase):
def test_sparse_assembly(self):
i = case(); m = OneDimReservoir(i); t, b, q = independent_system(i)
self.assertTrue(scipy.sparse.issparse(m.T)); self.assertTrue(scipy.sparse.issparse(m.B))
np.testing.assert_allclose(m.T.toarray(), t); np.testing.assert_allclose(m.B.diagonal(), b)
np.testing.assert_allclose(m.Q, q)
def test_all_solvers_repeated_steps(self):
for solver in ['implicit', 'explicit', {'mixed method': {'theta': .5}}, {'mixed method': {'theta': .23}}]:
with self.subTest(solver=solver):
i = case(solver, .04); m = OneDimReservoir(i)
for _ in range(4): m.solve_one_step()
np.testing.assert_allclose(m.get_solution(), expected_steps(i, 4), atol=1e-7, rtol=1e-10)
def test_reversed_boundary_conditions(self):
i = case(); i['boundary conditions'] = {'left': {'type': 'prescribed flux', 'value': 0}, 'right': {'type': 'prescribed pressure', 'value': 1800}}
m = OneDimReservoir(i); m.solve_one_step()
np.testing.assert_allclose(m.get_solution(), expected_steps(i, 1), atol=1e-7)
def test_closed_mass_conservation(self):
i = case({'mixed method': {'theta': .35}}, .1)
for bc in i['boundary conditions'].values(): bc.update(type='prescribed flux', value=0)
m = OneDimReservoir(i); m.p = np.array([700., 1200., 850., 1100.]); before = m.B.diagonal() @ m.p
np.testing.assert_allclose(m.T @ np.ones(4), 0, atol=1e-12)
for _ in range(7): m.solve_one_step()
self.assertAlmostEqual(m.B.diagonal() @ m.p, before, places=6)
def test_both_pressure_boundaries(self):
i = case(); i['boundary conditions']['right'] = {'type': 'prescribed pressure', 'value': 500}
m = OneDimReservoir(i); m.solve_one_step()
np.testing.assert_allclose(m.get_solution(), expected_steps(i, 1), atol=1e-7)
def test_invalid_boundary(self):
for kind, value in [('unknown', 0), ('prescribed flux', 4)]:
i = case(); i['boundary conditions']['right'].update(type=kind, value=value)
with self.assertRaises(ValueError): OneDimReservoir(i)
def test_solver_failure_preserves_pressure(self):
i = case({'mixed method': {'theta': .5}}); m = OneDimReservoir(i); old = m.get_solution()
with patch('scipy.sparse.linalg.cg', return_value=(np.zeros(4), 1)):
with self.assertRaises(RuntimeError): m.solve_one_step()
np.testing.assert_array_equal(m.get_solution(), old)
def test_nonunit_fluid_flow(self):
i = case(); i['fluid']['water'].update(viscosity=1.8, **{'formation volume factor': 1.3})
m = OneDimReservoir(i); m.solve_one_step()
np.testing.assert_allclose(m.get_solution(), expected_steps(i, 1), atol=1e-7)
if __name__ == '__main__':
unittest.main()