martes, 22 de octubre de 2019

MATLAB code False Position Method (root)



Flase_position
%The sequence of points that converges to the root is
%generated via c=[a*f(b)-b*f(a)]/[f(b)-f(a)]

f = @(x)(x*cos(x)+1);

%
% False position uses the regula falsi method to approximate a root of f(x) = 0
% in the interval [a,b].
%
%       f is an anonymous function representing f(x),
%       a and b are the limits of interval [a,b],
%       kmax is the maximum number of iterations (default 20),
%       tol is the scalar tolerance for convergence (default 1e-4),
%
%       r is the approximate root of f(x) = 0,
%       k is the number of iterations needed for convergence.
%
a=-2;
b=4;
tol = 1e-2;
kmax = 20;
c = zeros(1,kmax);      % Pre-allocate
if f(a)*f(b) > 0
     r = 'failure';
     return
end
disp(' k      a           b')
for k = 1:kmax,
    c(k) = (a*f(b)-b*f(a))/(f(b)-f(a));      % Find the x-intercept
    if f(c(k)) == 0      % Stop if a root has been found
        return
    end
   fprintf('%2i      %11.6f%11.6f\n',k,a,b)
    if f(b)*f(c(k)) > 0      % Check sign changes
        b = c(k);          % Adjust the endpoint of interval
    else a = c(k);
    end
    c(k+1) = (a*f(b)-b*f(a))/(f(b)-f(a));   % Find the next x-intercept
    if abs(c(k+1)-c(k)) < tol,     % Stop if tolerance is met
        r = c(k+1);
        break
    end
end

fx = f(r);
fprintf('r =  %11.6f\n', r);
fprintf('k = %11.1f\n', k);

mp = linspace(-2,4);
fp = arrayfun(f,mp,'UniformOutput',false); %func(mp);
fp = cat(1, fp{:});
hold on
plot(mp,fp),grid
plot(r,fx,'.','MarkerSize',20)
hold off
k      a           b
 1        -2.000000   4.000000
 2         1.189493   4.000000
 3         1.189493   2.515720
 4         1.960504   2.515720
r =     2.073843
k =         4.0

MATLAB code Bisection Method (root)



Raices_bisection
%Bisection method
%Step 1: Choose lower xl and upper xu guesses for
%the root such that the function changes sign
%over the interval. This can be checked by ensuring
%that f(xl)*f(xu) < 0.
%Step 2: An estimate of the root xr is determined
%by xr = (xl + xu)/2
%Step 3: Make the following evaluations to determine
%in which subinterval the root lies:
%(a) If f(xl)f(xr) < 0, the root lies in the lower
%subinterval. Therefore, set xu 5 xr and return to step 2.
%(b) If f(xl)f(xr) > 0, the root lies in the upper
%subinterval. Therefore, set xl 5 xr and return to step 2.
%(c) If f(xl)f(xr) = 0, the root equals xr; terminate the computation.

%Example


miércoles, 29 de mayo de 2019

Pipes and connections between functions in R

A recent addition to R is the pipe-forwarding mechanism (%>%) within the magrittr package. This is extremely useful when using the dplyr, ggvis, and tidyr packages, among others. Pipe forwarding is an alternative to nesting that yields code that can be read from top to bottom. Here we demonstrate an example that compares traditional (nested) dplyr function calls to the new pipe operator.


jueves, 23 de mayo de 2019

Summary of Matrix Operators in R

Summary of Matrix Operators in R

Parallel with R. Example with snow

Snow provides support for easily executing R functions in parallel. Most of the parallel execution functions in snow are variations of the standard lapply() function, making snow fairly easy to learn. To implement these parallel operations, snow uses a master/ worker architecture, where the master sends tasks to the workers, and the workers execute the tasks and return the results to the master.


The basic cluster creation function is makeCluster() which can create any type of cluster. snow includes a number of functions that we could use, including clusterApply(), clusterApplyLB(), and parLapply(). For this example, we’ll use clusterApply(). You call it exactly the same as lapply(), except that it takes a snow cluster object as the first argument. We also need to load MASS on the workers, rather than on the master, since it’s the workers that use the “Boston” dataset.

We’ll use snow.time() to gather timing information about the overall execution. We will also use snow.time()’s plotting capability to visualize the task execution on the workers. 

martes, 19 de marzo de 2019

ARS (Cartesian)

ARS is a basic reservoir simulation spreadsheet. With ARS you can do examples 
considering the follow features:

    -One dimension
    -One phase (oil)
    -Incompressible or Slightly compressible fluid
    -Rock porosity constant or depending of pressure
    -5 different types of boundary conditions
    -One well (specified production rate or specified flowing pressure)


miércoles, 25 de octubre de 2017

Reservoir Engineering Tools Android App Tutorial


Reservoir Engineering tools

Android App with Equation of state, Fluid flow in porous media, Reservoir Simulation (One phase-1D) and Waterflooding

Download it in Playstore

jueves, 27 de abril de 2017

Bisection method, Java code

The bisection method, which is alternatively called binary chopping, interval halving, or Bolzano’s method, is one type of incremental search method in which the interval is always divided in half. If a function changes sign over an interval, the function value at the midpoint is evaluated. The location of the root is then determined as lying at the midpoint of the subinterval within which the sign change occurs. The process is repeated to obtain refined estimates. A simple algorithm for the bisection calculation is listed in the following figure.


Reference; Numerical Methods for Engineers. Steven C. Chapra and Raymond P. Canale

martes, 25 de abril de 2017

Lagrange Polynomial Java code


The Lagrange interpolating polynomial is simply a reformulation of the Newton polynomial
that avoids the computation of divided differences. It can be represented concisely as

where

viernes, 17 de febrero de 2017

Linear Interpolation code C# and FORTRAN


With this method any two points are simple joined together by a straight line segment. The desired interpolation point should therefore lie on this same line segment. The next codes calculate single point linear interpolation (xval,yval) using to vectors data (x,y).


You can this code to linear interpolation in FORTRAN

       

SUBROUTINE LinearInterpolation(x,y,xval,yval,n)
  real::x(n),y(n),xval,yval
  integer::n,i
     yval = 0
     do i=1,n
  
   IF (xval >= x(i) .AND. xval < x(i+1)) then   
   
   yval = y(i)+(xval-x(i))*(y(i+1)-y(i))/(x(i+1)-x(i));
 
   end if
      end do
   return
   
      end SUBROUTINE 



jueves, 16 de febrero de 2017

Reservoir simulation code (1D-horizontal-single phase) FORTRAN

With this code you can practice with examples of reservoir simulation excersices with the follows considerations:

One dimension
Horizontal
One phase (oil)
Slightly compressible fluid
4 different types of boundary conditions
Implicit formulation
Peaceman well model
One well (specified production rate or specified flowing pressure)

You can change the number of cells, rock and fluid properties, boundaries conditions and well especification.

Z-factor: Dranchuk-Purvis-Robinson correlation C# code


Code to calculate Z factor with Dranchuk-Purvis-Robinson Method in C#.

Dranchuk et. al. (1974) developed a correlation based on the Benedict-Webb-Rubin type of equation of state. Fittig the equation to 1500 data points from the Standing and Katz Z-factor chart optimized the eight coefficient of the proposed equations. The equation has the following form:

jueves, 9 de febrero de 2017

Academic WaterFlooding (AWF)

Academic WaterFlooding (AWF)

AWF is part of a serie of basic reservoir engineering spreadsheets. With AWD you can do examples using thw Buckley-Leverett model and Welge solution estimating the displacement performance for a linear waterflood at constant injection rate.The necessary data is shown in the next picture.

miércoles, 14 de diciembre de 2016

ARS (1D - radial-Fractured)


ARS (1D - radial-Fractured) is part of a serie of basic reservoir simulation spreadsheets. With ARS (1D - radial-Fractured) you can do examples considering the follow features:

Naturally fractured reservoir (Warren and Root model)
One dimension in radial coordinates
One phase (oil)
Slightly compressible fluid
Rock porosity depending of pressure
4 different types of boundary conditions
Well with specified production rate or specified flowing pressure)

You will need the petrophysics properties of matrix and fracture (Porosity, compressibility, permeability and matrix-fracture shape fracture). You have the option to use a constant or variable time step.


In the follow image you can observe the double slope expected from the Warren and Root model.

You could check your results of your calculation by hand or your own code
Check it and tell us what do you think
Enjoy it


martes, 13 de diciembre de 2016

Academic Reservoir Simulator (1D - radial)

 ARS (1D - radial) is part of a serie of basic reservoir simulation spreadsheets. With ARS (1D - radial) you can do examples considering the follow features:

One dimension in radial coordinates
One phase (oil)
Slightly compressible fluid
Rock porosity constant or depending of pressure
4 different types of boundary conditions
Well with specified production rate or specified flowing pressure)


You could chech your result of your calculation by hand or your own code
Check it and tell us wath you think
Enjoy it



sábado, 19 de noviembre de 2016

Thomas Algorithmto to solve tridiagonal matrix (fortran and MATLAB)

Thomas algorithm offers an efficient algorithm to solve tridiagonal matrix. A tridiagonal matrix is generated when finite-difference equations are written in one dimension. This algorithm is executed in two major steps: Forward solution and Backward solution

The algorithm can be implemented as follows, in fortran or matlab:

Fortran
 MATLAB