1
- import sys
2
1
import logging
2
+ import sys
3
+
3
4
import numpy as np
4
5
from numpy .linalg import norm
5
6
from scipy .optimize .nonlin import TerminationCondition
6
- from openpnm .algorithms import Transport
7
- from openpnm .utils import TypedList , Docorator
8
7
from tqdm .auto import tqdm
9
8
9
+ from openpnm .algorithms import Transport
10
+ from openpnm .utils import Docorator , TypedList
10
11
11
- __all__ = [' ReactiveTransport' ]
12
+ __all__ = [" ReactiveTransport" ]
12
13
13
14
14
15
docstr = Docorator ()
15
16
logger = logging .getLogger (__name__ )
16
17
17
18
18
- @docstr .get_sections (base = ' ReactiveTransportSettings' , sections = [' Parameters' ])
19
+ @docstr .get_sections (base = " ReactiveTransportSettings" , sections = [" Parameters" ])
19
20
@docstr .dedent
20
21
class ReactiveTransportSettings :
21
22
r"""
@@ -45,7 +46,7 @@ class ReactiveTransportSettings:
45
46
x_rtol = 1e-6
46
47
47
48
48
- @docstr .get_sections (base = ' ReactiveTransport' , sections = [' Parameters' ])
49
+ @docstr .get_sections (base = " ReactiveTransport" , sections = [" Parameters" ])
49
50
@docstr .dedent
50
51
class ReactiveTransport (Transport ):
51
52
r"""
@@ -62,11 +63,11 @@ class ReactiveTransport(Transport):
62
63
63
64
"""
64
65
65
- def __init__ (self , name = ' react_trans_?' , ** kwargs ):
66
+ def __init__ (self , name = " react_trans_?" , ** kwargs ):
66
67
super ().__init__ (name = name , ** kwargs )
67
68
self .settings ._update (ReactiveTransportSettings ())
68
69
69
- def set_source (self , pores , propname , mode = ' add' ):
70
+ def set_source (self , pores , propname , mode = " add" ):
70
71
r"""
71
72
Applies a given source term to the specified pores
72
73
@@ -105,21 +106,21 @@ def set_source(self, pores, propname, mode='add'):
105
106
propname = self ._parse_prop (propname , "pore" )
106
107
# Check if any BC is already set in the same locations
107
108
locs_BC = np .zeros (self .Np , dtype = bool )
108
- for item in self [' pore.bc' ].keys ():
109
- locs_BC = np .isfinite (self [f' pore.bc.{ item } ' ])
109
+ for item in self [" pore.bc" ].keys ():
110
+ locs_BC = np .isfinite (self [f" pore.bc.{ item } " ])
110
111
if np .any (locs_BC [pores ]):
111
112
raise Exception ("BCs present in given pores, can't assign source term" )
112
- prop = ' pore.source.' + propname .split ('.' , 1 )[1 ]
113
- if mode == ' add' :
113
+ prop = " pore.source." + propname .split ("." , 1 )[1 ]
114
+ if mode == " add" :
114
115
if prop not in self .keys ():
115
116
self [prop ] = False
116
117
self [prop ][pores ] = True
117
- elif mode == ' remove' :
118
+ elif mode == " remove" :
118
119
self [prop ][pores ] = False
119
- elif mode == ' clear' :
120
+ elif mode == " clear" :
120
121
self [prop ] = False
121
122
else :
122
- raise Exception (f' Unsupported mode { mode } ' )
123
+ raise Exception (f" Unsupported mode { mode } " )
123
124
124
125
def _apply_sources (self ):
125
126
"""
@@ -134,9 +135,9 @@ def _apply_sources(self):
134
135
"""
135
136
try :
136
137
phase = self .project [self .settings .phase ]
137
- for item in self [' pore.source' ].keys ():
138
+ for item in self [" pore.source" ].keys ():
138
139
# Fetch linearized values of the source term
139
- Ps = self [' pore.source.' + item ]
140
+ Ps = self [" pore.source." + item ]
140
141
S1 , S2 = [phase [f"pore.{ item } .{ Si } " ] for Si in ["S1" , "S2" ]]
141
142
# Modify A and b: diag(A) += -S1, b += S2
142
143
diag = self .A .diagonal ()
@@ -173,20 +174,22 @@ def _run_special(self, solver, x0, verbose=None):
173
174
Initial guess of the unknown variable
174
175
175
176
"""
176
- w = self .settings [' relaxation_factor' ]
177
- maxiter = self .settings [' newton_maxiter' ]
178
- f_rtol = self .settings [' f_rtol' ]
179
- x_rtol = self .settings [' x_rtol' ]
177
+ w = self .settings [" relaxation_factor" ]
178
+ maxiter = self .settings [" newton_maxiter" ]
179
+ f_rtol = self .settings [" f_rtol" ]
180
+ x_rtol = self .settings [" x_rtol" ]
180
181
xold = self .x
181
182
dx = self .x - xold
182
- condition = TerminationCondition (f_tol = np .inf , f_rtol = f_rtol , x_rtol = x_rtol , norm = norm )
183
+ condition = TerminationCondition (
184
+ f_tol = np .inf , f_rtol = f_rtol , x_rtol = x_rtol , norm = norm
185
+ )
183
186
184
187
tqdm_settings = {
185
188
"total" : 100 ,
186
189
"desc" : f"{ self .name } : Newton iterations" ,
187
190
"disable" : not verbose ,
188
191
"file" : sys .stdout ,
189
- "leave" : False
192
+ "leave" : False ,
190
193
}
191
194
192
195
with tqdm (** tqdm_settings ) as pbar :
@@ -198,12 +201,12 @@ def _run_special(self, solver, x0, verbose=None):
198
201
if is_converged :
199
202
pbar .update (100 - pbar .n )
200
203
self .soln .is_converged = is_converged
201
- logger .info (f' Solution converged, residual norm: { norm (res ):.4e} ' )
204
+ logger .info (f" Solution converged, residual norm: { norm (res ):.4e} " )
202
205
return
203
206
super ()._run_special (solver = solver , x0 = xold , w = w )
204
207
dx = self .x - xold
205
208
xold = self .x
206
- logger .info (f' Iteration #{ i :<4d} | Residual norm: { norm (res ):.4e} ' )
209
+ logger .info (f" Iteration #{ i :<4d} | Residual norm: { norm (res ):.4e} " )
207
210
self .soln .num_iter = i + 1
208
211
209
212
self .soln .is_converged = False
@@ -217,7 +220,7 @@ def _get_progress(self, res):
217
220
self ._f0_norm = norm (res )
218
221
f_rtol = self .settings .f_rtol
219
222
norm_reduction = norm (res ) / self ._f0_norm / f_rtol
220
- progress = (1 - max (np .log10 (norm_reduction ), 0 ) / np .log10 (1 / f_rtol )) * 100
223
+ progress = (1 - max (np .log10 (norm_reduction ), 0 ) / np .log10 (1 / f_rtol )) * 100
221
224
return max (0 , progress )
222
225
223
226
def _update_A_and_b (self ):
@@ -240,12 +243,12 @@ def _get_residual(self, x=None):
240
243
x = self .x
241
244
return self .A * x - self .b
242
245
243
- def set_BC (self , pores = None , bctype = [], bcvalues = [], mode = ' add' ):
246
+ def set_BC (self , pores = None , bctype = [], bcvalues = [], mode = " add" ):
244
247
msg = "Source term already present in given pores, can't assign BCs"
245
248
# Ensure that given pores do not have source terms already set
246
249
try :
247
- for item in self [' pore.source' ].keys ():
248
- if np .any (self [' pore.source.' + item ][pores ]):
250
+ for item in self [" pore.source" ].keys ():
251
+ if np .any (self [" pore.source." + item ][pores ]):
249
252
raise Exception (msg )
250
253
except KeyError :
251
254
pass
0 commit comments