Reservoir Modeling Using Matlab The Matalb Reservoir Simulation Toolbox MRST
Reservoir Modeling Using Matlab The Matalb Reservoir Simulation Toolbox MRST
Knut-Andreas Lie
SINTEF Digital, Oslo, Norway
Used both by academia and industry Google Analytics: access pattern for www.mrst.no
Period: 1 July 2018 to 31 December 2019
Reservoir simulation in MATLAB...? 3 / 22
MATLAB is fairly efficient using vectorization, logical indexing, external iterative solvers, etc.
Avoids build process, linking libraries, cross-platform problems
Builtin mathematical abstractions, numerics, data analysis, visualization, debugging/profiling,
Use scripting language as a wrapper when you develop solvers in compiled languages
Community code: software organization 4 / 22
fully implicit
Modular design:
es
flow diagnostics discretizations
ul
od
m
small core with mature and well-tested functionality
n
d -o
Ad
used in many programs or modules grid coarsening multiscale methods
5
MRST core
6
6
6
cells.faces =
1 10
1
1
2
8
7
1
faces.nodes =
1
1
2
2
1
2
1
3
faces.neighbors =
0
2
3
5
2
3
5
0
9 2 2 3 1 6 2
functionality
2 5 3 4 0 6
4 12 3 3 4 1 1 3
3 3 7 4 7 4 1
3 2 5 2 6 4
8
2 4 8 5 3 1 8
1 5 4 12 6 2 8 5
1 7 4 9 6 6 4 7
upscaling 2
7
10
14 5
5
5 11
3
4
7
7
8
3
4
3
7
0
8
7 co2lab
3 6 9 8 5
8 13 6 6 9 3
Original permeability Upscaled (x−direction) Upscaled (y−direction) 4 6 5 9 6
7 13 10 4
3 11 7 14 10 5 CO2 saturation
5 : : : : at 500 years 12%
16%
1 4 7
56%
12%
3%
Residual (traps)
30 10 1.5
8
20 1
6
4
10 0.5
2
0 0 0
−1.5 −1 −0.5 0 0.5 1 1.5 2 2.5 0 50 100 150 200 250 300 350 400 450 500
Non-matching faces
Low permeability
Corner-point
Twisted grid
Thin cells Tetrahedral, prismatic, PEBI
General polyhedral/polytopal
Hybrid, cut-cell, or depogrids
Local refinements . . .
c F(c) f C1 C2
1 1 1 3 1
7
8 1 2 2 1 2
8
1 3 3 1 8
3 1 4 4 9 1
7 2 5 5 4 2
6
2 6 6 2 5
9 4 1 2 7 7 2 6
2 2
2 8 8 2 7
.. .. ..
6 2 2 . . .
.. .. ..
1 5 3 1 . . .
5
.. ..
. .
.. ..
. .
3 4
Map: cell → faces Map: face → cells
Rapid prototyping: discrete differentiation operators 9 / 22
c F(c) f C1 C2
1 1 1 3 1
7
8 1 2 2 1 2
8
1 3 3 1 8
3 1 4 4 9 1
7 2 5 5 4 2
6
2 6 6 2 5
9 4 1 2 7 7 2 6
2 2
2 8 8 2 7
.. .. ..
6 2 2 . . .
.. .. ..
1 5 3 1 . . .
5
.. ..
. .
.. ..
. .
3 4
Map: cell → faces Map: face → cells
For finite volumes, discrete grad operator maps from cell pair C1 (f ), C2 (f ) to face f :
where p[c] is a scalar quantity associated with cell c. Discrete div maps from faces to cells
Both are linear operators and can be represented as sparse matrix multiplications
Close correspondence with mathematics 10 / 22
∂(φρ)
+ ∇ · (ρK∇p) + q = 0 eq = ( pv ( p ).* rho ( p ) - pv ( p0 ).* rho ( p0 ))/ dt ...
∂t
+ div ( avg ( rho ( p )).* T .* grad ( p ))+ q ;
Close correspondence with mathematics 10 / 22
∂(φρ)
+ ∇ · (ρK∇p) + q = 0 eq = ( pv ( p ).* rho ( p ) - pv ( p0 ).* rho ( p0 ))/ dt ...
∂t
+ div ( avg ( rho ( p )).* T .* grad ( p ))+ q ;
General idea:
Any code consists of a limited set of arithmetic operations and elementary functions
Introduce an extended pair, hx, 1i, i.e., the value x and its derivative 1
Use chain rule and elementary derivative rules to mechanically accumulate derivatives at
specific values of x
– Elementary: v = sin(x) −→ hvi = hsin x, cos xi
– Arithmetic: v = f g −→ hvi = hf g, f gx + fx gi
– Chain rule: v = exp(f (x)) −→ hvi = hexp(f (x)), exp(f (x))f 0 (x)i
Use operator overloading to avoid messing up code
∂x ∂x ∂y ∂y ∂z ∂z
∂x ∂y ∂x ∂y ∂x x=1,y=2 ∂y x=1,y=2
Example: incompressible single-phase flow 12 / 22
% Evaluate equations
[p, sW] = initVariablesADI(p0, sW0)[rW,
; rO, vol] = deal(rhoW(p)
% Primary variables , rhoO(p) , pv(p))) ;
[pIx, sIx] = deal(1:nc, (nc+1):(2∗nc)): ; % Indices of p/S in eq. system
[tol, maxits] = deal(1e−5, 15); water =
% (vol. ∗rW.∗sW
Iteration − vol0.∗rW0.∗sW0)./dt + div(vW) ;
control
t = 0; water(inIx) = water(inIx) − inRate.∗rhoWS;
while t < totTime, :
t = t + dt; eqs = {oil, water}; % concatenate equations
resNorm = 1e99; nit=0; eq = cat(eqs{:}) ; % assemble
[p0, sW0] = deal(value(p) , value(sW))res
; =% eq.val;
Prev. time step not %ADresidual
variable
upd = −(eq.jac{1}
while (resNorm > tol) && (nit<= maxits) % Nonlinear \iteration
res) ; % loop
Newton update
% one Newton iteration
end % Update variables
∂O ∂O
i f nit > maxits, p.val = p.val + upd(pIx) ;
error( 'Newton solves did not converge
sW.val' ) = sW.val + upd(sIx) ; ∂p ∂Sw
end sW.val = max( min(sW.val, 1) , 0);
end resNorm = norm(res) ;
nit = nit + 1; ∂W ∂W
∂p ∂Sw
The AD-OO simulator framework 14 / 22
simulateScheduleAD Function(s)
Initial ministep:
State(Ti ), ∆Ti , Controls(Ci ) Nonlinear solver ∆t Time step selector Input
Solves nonlinear problems sub-divided Determines optimal time steps
into one or more mini steps using Contains object
State(Ti + ∆Ti ) Newton’s method SimpleTimeStepSelector,
Adjusted:
∆t̃
IterationCountSelector, Optional output
StateChangeTimeStepSelector, ...
Example:
Input deck
Data
Input parser
Class
Reads complete simulation decks:
grid and petrophysics, fluid and rock Struct
properties, region information, well
definitions, operating schedule, con- Function
vergence control, etc
Input
Contains
Oil (stb)
Water (stb)
2.5 Gas (mscf)
0
0.6
the wellbore 500 1000 1500 2000 2500 3000
qsw , qso , qsg , qsp , pbh
0.4
0.2
Time (days)
0
0 0.2 0.4 0.6 0.8 1
G
Simulator: differentiable graph 17 / 22
Wells
Idea: apply this concept to
Qi,α Qi ρw
α flow property evaluation
W →c qα
WI
PVT
µα pc − pbh − g∆zρmix
PVT calculations
Flux
accumulation, flux, and source terms
state pα Φ wip
∇pα
bhp spatial/temporal discretization
Θα λfα
bα ρα Rsmax
rs gρα ∆z vα
Simulator −→ differentiable graph
Accumulation Tf Tf Θα
pressure
Mi,α Mi Vi,α Vi
Potential concerns:
Total time of a program consists of several parts:
MATLAB is interpreted
programming + debugging cure: JIT, vectorization, logical indexing,
+ documenting + testing + executing pre-allocation, highly-efficient libraries
MRST is designed to prioritize the first four over the last Redundant computations
cure: state functions = dependency
Does this mean that MRST is slow and not scalable? graph + computational cache
Computational overhead
No, I would say its is surprisingly efficient cure: new auto diff backends
Scalability/performance
cure: external high-end iterative solvers
New backends for automatic differentiation 19 / 22
single-phase
2 3ph, immiscible
10
3ph, blackoil
6c, compositional
Assembly time [s]
100
10-2
Berge et al.: Constrained Voronoi grids Al Kobaisi & Zhang: nonlinear FVM Lie & Møyner: multiscale methods
Wong et al.: embedded discrete fractures Olorode et al.; fractured unconventionals March et al.: unified framework, fractures