JOSE L. CARBALLAL JR. and BERNARDO HOROWITZ. THE COMPLEX STEP METHOD APPLIED TO WATERFLOODING 45 The Complex Step Method Applied to Waterlooding Optimization José L. Carballal Jr. 1, Bernardo Horowitz 2 joseloisjr@hotmail.com, horowitz@upe.br 1 Is an undergraduate student o Civil Engineering at The Federal University o Pernambuco, Recie, Brazil. 2 Ccorresponding author, is an Associate Pro. o Civil Engineering at the Federal University o Pernambuco, Recie, Brazil. Abstract In Oil Reservoir Engineering a problem o great interest is the dynamic optimization o waterlooding management. In optimization as well as in many engineering problems the use o sensitivity analysis is required, where the computation o irst order derivatives is necessary. In this work the complex step method is applied to an in-house reservoir simulator, used in waterlooding optimization computing unctional values as well as gradients. This method computes numerically the irst derivatives to ull CPU precision. In this work the complex step is detailed and a comparison with analytic results and inite-dierence method is presented. Index Terms Complex-step method, Reservoir simulation, Sensitivities, Optimization. I. INTRODUCTION ATER looding is by ar the most commonly used Wmethod to improve oil recovery ater primary depletion. In spite its many avorable characteristics, reservoir heterogeneity, permeability contrast in particular, can adversely impact the perormance o waterlooding. Moreover, it is well known that the presence o high permeability streaks can severely reduce the sweep eiciency leading to an early water arrival at the producers and bypassed oil. One approach to counteract the impact o the heterogeneity and to improve waterlooding sweep eiciency is through optimal rate allocation or BHP settings o injector and producer wells. Initially the optimal waterlooding problem is deined. This is a simulation based optimization problem where an in-house two-dimensional, two-phase code is used based on a ully implicit ormulation. Numerical derivatives are necessary or the Jacobian terms and gradients are necessary or optimization problems using the SQP algorithm. Thereore the complex step method is detailed and used initially to assess the quality o the derivatives o residual terms o the nonlinear low equations and inally the gradient o the objective unction o the waterlooding optimization problem. Manuscript received March 30, 2012. This work was supported in part by the ANP PRH-26 program. II. THE WATERFLOODING PROBLEM The waterlooding optimization problem can be ormulated as ollows: Maximize Subject to NPV ( x) g( x) 0 l x x u x, i 1.. ndv wi wi wi The design variables xwi may be the rate allocated to well i or its BHP; ndv= number o design variables; the objective unction is the Net Present Value; constraints may include limitation on total platorm rates or various well controls; side constraints are also included or all design variables. In the example presented design variables are the BHP s o producer wells and no general constrains are considered. One o the most successul general purpose optimization algorithms is the Sequential Quadratic Programming method. In this algorithm the gradients o involved unction are necessary to build up an approximation o the Hessian matrix o the Lagrangian unction o the problem. Gradients may be computed numerically or analytically. Analytical computation requires a complete code revision with signiicant programming eort. Numerical computation o derivative is non intrusive but one is then aced with the step size dilemma o inite dierence computation. This study addresses this problem in particular in the ollowing sections. III. NUMERICAL COMPUTATION OF DERIVATIVES A. Finite Dierences In sensitivity analysis the inite-dierence is the most commonly used method to estimate the value o irst derivatives. The inite-dierencing equations results rom truncation o Taylor series expanded about a point x. Considering an expansion o the Taylor series o the scalar unction (x+h) we have: d h² d² h³ d ³ ( x h) ( x) h... (2) dx 2! dx ² 3! dx ³ (1)
INTERNATIONAL JOURNAL OF MODELING AND SIM ULATION FOR PETROLEUM INDUSTRY, VOL. 6, NO. 1, JUNE 2012 46 Solving or d/dx, we have the orward-dierence ormula: d ( x h) ( x) h d²... dx h 2! dx² (3) hd² ( h)... 2! dx² (4) Where h is the inite dierence step size, and O(h) is the truncation error, which represents a irst-order approximation. To reduce the truncation error a second order expression can be deduced by using a combination o alternate Taylor series expansions. I Taylor series is expanded by a negative step size h: d h² d² h³ d³ ( x h) ( x) h... (5) dx 2! dx ² 3! dx ³ Subtracting (5) rom (2) and solving or d/dx we arrive at the central dierence ormula: d ( x h) ( x h) h² d³... (6) dx 2h 3! dx³ With a O(h²) truncation error, a second order expression is obtained, but the number o unction evaluation is increased compared to the orward-dierence, which increases the computational cost o the derivative. When inite-dierence is used to estimate derivatives, the problem o what step-size to use is encountered. A suiciently small step-size is needed to minimize the truncation error, but at the same time the subtractive cancelation error due to a small step-size should be avoided. B. Complex-Step Method As a solution to the problem o step-size encountered in inite-dierencing the complex-step method will be used. Instead o a real step h, an imaginary step size h is used to expand the Taylor series: d [ ( x ih)] ( h²) (9) dx h As can be seen in (9), there is no subtractive cancelation error since there is no subtraction operation in the deduced expression. Another positive aspect o the complex-step method is that the truncation error gives a second order approximation with only one unction evaluation, dierent rom the central dierence that needs two unction evaluation to reach this error order. Since there is no subtractive cancelation error in this method the step-size h can be suiciently small down to the smallest nonzero number represented by the machine [1]. Considering n the number o derivatives desired, the complex step method only requires n simulations compared to n+1 simulations required or a orward dierence and 2n simulations or a central dierence. Although this method has a reduced simulation number compared to inite dierences, it has the drawback o using complex arithmetic which results on an increased simulation time [2]. An additional disadvantage o the complex-step derivative approximation method is that the amount o memory used is approximately doubled due to the introduction o the new complex data type, since a real and imaginary part o a complex variable needs to be saved at the memory. Equation (10) is used as an example to show the behavior o inite dierence and complex-step when computing the irst derivative using dierent step sizes. x 2 3 ( x ) xe sin 4 x ² 2 (10) At the point x=0.5 the derivative is approximated by inite dierence, both orward and central dierence, and the complex-step method, and the result is compared with the analytic result. In ig (1) the behavior o both methods is shown with respect to step size h used. d h² d ² ih³ d³ ( x ih) ( x) ih... (7) dx 2! dx ² 3! dx ³ Separating the real and the imaginary part o (7) : h² d² 4 [ ( x ih)] ( x) ( h ) 2! dx² d h³ d ³ 5 [ ( x ih)] h ( h ) dx 3! dx³ (8) Where [.] is the real part the complex number and [.] is its imaginary part. Solving or d/dx: Fig. 1. Normalized error in the sensitivity approximation by inite dierence and complex-step method. As can be seen both complex step and central dierence converges quadratically, but as already mentioned the
JOSE L. CARBALLAL JR. and BERNARDO HOROWITZ. THE COMPLEX STEP METHOD APPLIED TO WATERFLOODING 47 subtractive cancelation error is present or a step smaller then 10 9 or orward dierence, and 10 6 or central dierence. For a step size 10 9 or smaller the complex step method reaches sixteen digit accuracy, while inite dierences only reaches a raction o this value. Although the size o the complex step can be made extremely small, there is a lower limit when using initeprecision arithmetic. The range o real numbers that can be represented by the CPU depends on the precision and compiler used. When double precision arithmetic is used the smallest nonzero number that can be represented is around 10 308. I a number smaller than this value is used, underlow occurs and the representation o that number results in a zero value and the derivative approximation will be NaN, not a number [3]. IV. IMPLEMENTATION OF THE COMPLEX-STEP Reerence [4] shows the general procedure or implementing the complex-step method or an arbitrary computer program. It can be summarized by the ollowing three steps: 1) Substitute all real type variables declarations with complex declarations, where it is not strictly necessary to declare all variables complex, but it is much easier. 2) Deine all unctions and operators that are not deined or complex arguments. 3) Add a small complex step to the desired x, run the algorithm that evaluates, and then compute d/dx using (8). To automate steps 1 and 2 or Fortran 90 codes a script written in python language that processes the Fortran source code and a Fortran module developed by [3] was used. The python ile prepares the source code by inserting a statement that ensures that the complex unctions module is used in every subroutine and substitutes all the real type declarations by complex, while the module overload all unctions that are not deined or complex arguments, such as the absolute value unction. Fig. (2) shows some o the necessary modiications that must happen in the source code. Logical operators such as greater than or less than, are not deined or complex numbers deserving a special attention during the implementation. These logical operators are usually used in combination with condition unctions such as the i unction. To maintain the same behavior as the original program, the complexiied version should compare only the real part o the number when subjected to logical operators. The programmer should use the real(x) unction which returns the real part o a complex number x. When the algorithm uses condition unctions, the program probably has a discontinuity where the unction itsel or its derivative turns out to be discontinuous. When a unction is discontinuous the inite dierence method ails in the derivative computation, while the complx-step method achieves this objective i the unction is deined in the discontinuity point [5]. As an example (11) is used, there is a discontinuity at the point x=1 and its graph can be seen in Fig (3). Since the unction is discontinuous, by deinition there is no derivative at the discontinuity point, but the complex-step method is capable o calculating one side derivative, i.e., calculate the derivative o the unction that is deined in the discontinuity point. Since the unction y=x² is deined in x=1, the value o the derivative at this point returned by the complex-step method is 2. I a inite dierence is used, only the backward dierence would return a reasonable value or the derivative, but the step-size dilemma would again be encountered. Other relevant actors or the correct implementation o the complex-step method are to correctly change the data iles and the maintenance o the precision o the variables. Depending on the type o compiler used, the ormat o the data iles should be changed since complex numbers are read dierently rom real variables. Regarding the accuracy, most compilers works with two kinds o precisions, the simple and the double precision, where the main dierence is that the single precision uses 4 bytes o the memory to store the value o the variable, while the double precision uses twice as much. x², i x 1 yx ( ) (11) x 1, i x 1 Fig. 2. Original and modiied version o a ortran 90 code. Fig. 3. Discontinuous unction
INTERNATIONAL JOURNAL OF MODELING AND SIM ULATION FOR PETROLEUM INDUSTRY, VOL. 6, NO. 1, JUNE 2012 48 Single precision represents only 6-7 digits accurately while the double precision represents 15 to 16 digits with accuracy [6]. Since the objective o this work is to achieve derivatives with high accuracy then all computations were made with double precision variables. A commonly used unction while programming with complex variables using Fortran 90 language is the cmplx(a,b) unction. This unction receives two real numbers a and b and creates a complex variable where a is attributed to the real part and b do the imaginary part o the complex number. An extra attention is needed while using the cmplx(a,b) unction, since even i a and b are double precision variables the unction will return a complex number with the deault precision o the compiler, where in most cases is the single precision. To solve this accuracy problem the deault precision o the compiler should be changed to double precision or the unction should be called in the ollowing ormat cmplx(a,b,8), where 8 stands or the number o bytes used or the double precision variable. V. RESERVOIR SIMULATOR A two-dimensional, two-phase (oil/water) reservoir simulator was developed in Fortran 90 using the ully implicit ormulation, based on [7], or research purposes. Centered rectangular grid blocks were adopted as it can be seen in Fig. (4). Each block in the reservoir has important inormation that deines the reservoir at that point such as rock properties and luid pressure and saturation. In the simulation, to compute pressure and saturation o a block on a uture time step the inormation o the neighboring blocks is used, leading to a nonlinear system o equations [8]. Ax b (12) During the development o the simulator some veriications were made to check the correct operation o the simulator. The complex-step method was used to check the quality o the Jacaboian matrix calculated internally by the analytic equations o the simulator, leading to no signiicant dierence. A sample reservoir was also simulated both in the in house simulator as in the commercial simulator o the Computer Modeling Group [10] IMEX: Implicit-Explicit Black Oil Simulator and the results were compared. The reservoir grid is made up o 51 by 51 square blocks, containing two producers and one injector. More o its characteristics are presented in Table I and its permeability map can be seen in Fig. (5). TABLE I CHARACTERISTICS OF THE SAMPLE RESERVOIR Simulation grid 51 (510 m) x 51 (510 m) x 1 (4 m) Porosity 30% Rock compressibility at 200 kg/cm² 5. 10-5 (kg/cm²)-1 Saturation Pressure (P sat) 273 kg/cm² Viscosity at T res, P sat 0,97 cp Initial Water Saturation 0,088 Gridtop depth 2000m Fig. 5. Sample Reservoir Fig 4. Example o a 2D reservoir. The main variables in the simulation problem are oil pressure and water saturation, where the governing equations are written in a ully implicit orm. In this kind o problem the iterative method Newton Raphson is used, where or each iteration, (12) must be solved, where A is the Jacobian matrix containing the partial derivatives o the residual equation with respect to the problem main variables, oil pressure and water saturation, b is the residual vector, and x is the solution vector. The Intel Pardiso [9] package is used or solving this unsymmetric nonlinear systems o equations. The results obtained rom the simulation o the sample reservoir in the IMEX and in the house simulator were compared. To illustrate the correct implementation o the in house simulator, results o oil rate in the producers wells and well block pressures o both injector and producers wells are shown in the graphs o Fig. (6). In the irst years o simulation, the oil rate o producer well 2 is higher than the well 1 due to its location in a high permeability zone. In the year o 2012 its rate decreases sharply since water reached the well and started to be produced. In producer well 1, due to its low permeability, water production only occurs in the year 2015. VI. EXAMPLE OPTIMIZATION PROBLEM Consider the reservoir shown in Fig. (5). The water
JOSE L. CARBALLAL JR. and BERNARDO HOROWITZ. THE COMPLEX STEP METHOD APPLIED TO WATERFLOODING 49 (a) Oil Rate, IMEX. (b) Oil Rate, In House. (c) Well Block Pressure, IMEX. (d) Well Block Pressure, In House. Fig.6 Results Comparison looding optimization problem is deined as: Maximize NPV ( x) Subject to : l u x x x, i 1..4 wi wi wi (13) In this problem the injector well has a ixed rate o 44m³/day. The design variables are the BHP o the producers. With a concession period o 16 years two control cycles were considered: 0 to 6 years, and 6 to 16 years. Side constraints limit BHP o producers between 190 and 210 kg/cm². In Table II a comparison is made between the base case and the optimal solution. As can be seen the optimal solution kept a low BHP or the irst cycle improving oil production in the beginning o the simulation. Initial reservoir pressure is 200 kg/cm²,thereore producer well 2 o the base case starts the simulation with no production at all, since its BHP value is greater than the reservoir pressure. TABLE II BHP COMPARISON Producer Well BHP (Base Case) BHP (Optimal) P1 (1 st Cycle) 195 kg/cm² 190 kg/cm² P2 (1 st Cycle) 205 kg/cm² 190 kg/cm² P1 (2 nd Cycle) 205 kg/cm² 190 kg/cm² P2 (2 nd Cycle) 200 kg/cm² 203.8 kg/cm² The optimal solution is $1,381,429, 3.3% better than the base case. This problem was also solved using Surrogate Based Sequential Approximation Optimization [11], yield virtually the same solution with an NPV=1,381,388. VII. CONCLUSION In the numerical computation o derivative when using inite dierence the problem o choosing the optimal step size is encountered. I it is too large or too small the error may be excessive. The optimal step size depends strongly on the type o unction, and yet the accuracy o the obtained numerical value is not guaranteed. The method o complex step does not present the drawback o the subtractive cancellation error or the case o extremely small step size. Since this method is insensitive to suiciently small step sizes, the value o the derivative is exact to the accuracy o the CPU. Thus, the value o the pertubation can always be taken, or example, as the square root o smaller loating point number represented by the CPU, corresponding to the number o bits o the variable.
INTERNATIONAL JOURNAL OF MODELING AND SIM ULATION FOR PETROLEUM INDUSTRY, VOL. 6, NO. 1, JUNE 2012 50 Routines are available to pre-process source codes or using the complex step resulting in easy implementation. Some additional guidance is discussed to code in Fortran 90 language. Application examples o the complex step in reservoir engineering are presented. REFERENCES [1] W. Squire, G. Trapp, Using Complex Variables to Estimate Derivatives o Real Functions, SIAM Review, 40(1), pp. 110-112, Mar. 1998. [2] D.J.W. De Pauw, P.A. Vanrolleghem, Using the complex-step derivative approximation method to calculate local sensitivity unctions o highly nonlinear bioprocess models. Ghent University. [3] J.R.R.A. Martins, P. Strudza, and J.J. Alonso, The Connection between the Complex-Step Derivative Approximation and Algorithmic Dierentiation, AIAA Paper, 2001-0921, Jan. 2001. [4] J.R.R.A. Martins, A Coupled-Adjoint Method or High-Fidelity Aero- Structural Optimization, Ph.D. dissertation, Stanord Univ., Palo Alto, CA, 2003. [5] J.R.R.A. Martins, I.M. Kroo, and J.J. Alonso, An Automated Method or Sensitivity Analysis using Complex Variables, AIAA Paper, 2000-0689, Jan. 2000. [6] S. J. Chapman, Fortran 95/2003 or Scientists and Engineers, Third Edition, McGraw-Hill, 2007. [7] T. Ertekin, J.H. Abou-Kassem, and G.R. King, Basic Applied Reservoir Simulation. SPE Textbook Series, Vol. 7, Richardson, TX, 2001. [8] A.A.M Soares, Simulação de Reservatórios de Petróleo em Arquiteturas Paralelas com Memória Distribuída, M.S. thesis, Dept. Civil. Eng., Pernambuco Federal Univ., Recie, PE, Brazil, 2002. [9] Intel Math Kernel Library Reerence Manual, Pardiso, Parallel Direct Sparse Solver Interace. [10] IMEX User s Guide, CMGL, Computer Modeling Group Ltd, 2009. [11] B. Horowitz, S.M.B. Aonso, C.V.P. Mendonça, Using Control Cycle Switching Times as Design Variables in Optimum Waterlooding Management, 2 nd International Conerence on Engineering Optimization, September 6, Lisbon, Portugal.