yaeos__math Module

Mathematical methods for yaeos

Description

This module provides all the relevant mathematical functions used in this library. Most important ones are:

  • newton: Newton solving method
  • solve_system: Solving linear system Ax = b
  • continuation: Continuation method for line tracing

Examples

Squared error calculation

 use yaeos__constants, only: pr
 use yaeos__math, only: sq_error
 print *, sq_error(2.5_pr, 3.0_pr)
 use yaeos__constants, only: pr
 use yaeos__math, only: sq_error
 real(pr) :: x(2) = [2.5_pr, 5.0_pr], y(2) = [3.0_pr, 4.5_pr]
 ! It also works with arrays
 print *, sq_error(x, y)

Uses

  • module~~yaeos__math~~UsesGraph module~yaeos__math yaeos__math module~yaeos__constants yaeos__constants module~yaeos__math->module~yaeos__constants module~yaeos__math_continuation yaeos__math_continuation module~yaeos__math->module~yaeos__math_continuation module~yaeos__math_linalg yaeos__math_linalg module~yaeos__math->module~yaeos__math_linalg iso_fortran_env iso_fortran_env module~yaeos__constants->iso_fortran_env module~yaeos__math_continuation->module~yaeos__constants module~yaeos__math_continuation->module~yaeos__math_linalg module~yaeos__math_linalg->module~yaeos__constants

Used by

  • module~~yaeos__math~~UsedByGraph module~yaeos__math yaeos__math module~yaeos__equilibria_binaries yaeos__equilibria_binaries module~yaeos__equilibria_binaries->module~yaeos__math module~yaeos__equilibria_boundaries_auxiliar yaeos__equilibria_boundaries_auxiliar module~yaeos__equilibria_boundaries_auxiliar->module~yaeos__math module~yaeos__equilibria_boundaries_generalized_isopleths yaeos__equilibria_boundaries_generalized_isopleths module~yaeos__equilibria_boundaries_generalized_isopleths->module~yaeos__math module~yaeos__equilibria_boundaries_phase_envelopes_mp yaeos__equilibria_boundaries_phase_envelopes_mp module~yaeos__equilibria_boundaries_phase_envelopes_mp->module~yaeos__math module~yaeos__equilibria_boundaries_phase_envelopes_mp->module~yaeos__equilibria_boundaries_auxiliar module~yaeos__equilibria_boundaries_phase_envelopes_mp_px yaeos__equilibria_boundaries_phase_envelopes_mp_px module~yaeos__equilibria_boundaries_phase_envelopes_mp_px->module~yaeos__math module~yaeos__equilibria_boundaries_phase_envelopes_mp_px->module~yaeos__equilibria_boundaries_auxiliar module~yaeos__equilibria_boundaries_phase_envelopes_mp_tx yaeos__equilibria_boundaries_phase_envelopes_mp_tx module~yaeos__equilibria_boundaries_phase_envelopes_mp_tx->module~yaeos__math module~yaeos__equilibria_boundaries_phase_envelopes_mp_tx->module~yaeos__equilibria_boundaries_auxiliar module~yaeos__equilibria_boundaries_phase_envelopes_pt3 yaeos__equilibria_boundaries_phase_envelopes_pt3 module~yaeos__equilibria_boundaries_phase_envelopes_pt3->module~yaeos__math module~yaeos__equilibria_boundaries_phase_envelopes_px3 yaeos__equilibria_boundaries_phase_envelopes_px3 module~yaeos__equilibria_boundaries_phase_envelopes_px3->module~yaeos__math module~yaeos__equilibria_boundaries_phase_envelopes_px3->module~yaeos__equilibria_boundaries_auxiliar module~yaeos__models_ge_uniquac yaeos__models_ge_uniquac module~yaeos__models_ge_uniquac->module~yaeos__math proc~check_critical_jump check_critical_jump proc~check_critical_jump->module~yaeos__math proc~critical_line critical_line proc~critical_line->module~yaeos__math proc~critical_point critical_point proc~critical_point->module~yaeos__math proc~detect_critical detect_critical proc~detect_critical->module~yaeos__math proc~homotopy homotopy proc~homotopy->module~yaeos__math proc~look_for_cep look_for_cep proc~look_for_cep->module~yaeos__math proc~solve_generalized_point solve_generalized_point proc~solve_generalized_point->module~yaeos__math proc~solve_mp_flash_point solve_mp_flash_point proc~solve_mp_flash_point->module~yaeos__math proc~solve_point PXEnvelMP%solve_point proc~solve_point->module~yaeos__math proc~solve_point_psat solve_point_psat proc~solve_point_psat->module~yaeos__math proc~solve_point~2 TXEnvelMP%solve_point proc~solve_point~2->module~yaeos__math proc~solve_point~4 PTEnvelMP%solve_point proc~solve_point~4->module~yaeos__math proc~solve_tp solve_TP proc~solve_tp->module~yaeos__math proc~solve_vxvyt solve_VxVyT proc~solve_vxvyt->module~yaeos__math proc~three_phase_line_f three_phase_line_F proc~three_phase_line_f->module~yaeos__math proc~three_phase_line_f_solve three_phase_line_F_solve proc~three_phase_line_f_solve->module~yaeos__math proc~volume~2 ArModel%volume proc~volume~2->module~yaeos__math proc~volume~3 Gerg2008%volume proc~volume~3->module~yaeos__math module~yaeos__equilibria yaeos__equilibria module~yaeos__equilibria->module~yaeos__equilibria_binaries module~yaeos__equilibria->module~yaeos__equilibria_boundaries_generalized_isopleths module~yaeos__equilibria->module~yaeos__equilibria_boundaries_phase_envelopes_mp module~yaeos__equilibria->module~yaeos__equilibria_boundaries_phase_envelopes_mp_px module~yaeos__equilibria->module~yaeos__equilibria_boundaries_phase_envelopes_mp_tx module~yaeos__equilibria->module~yaeos__equilibria_boundaries_phase_envelopes_pt3 module~yaeos__equilibria->module~yaeos__equilibria_boundaries_phase_envelopes_px3 module~yaeos__equilibria_boundaries_phase_envelopes_pt yaeos__equilibria_boundaries_phase_envelopes_pt module~yaeos__equilibria->module~yaeos__equilibria_boundaries_phase_envelopes_pt module~yaeos__equilibria_boundaries_phase_envelopes_px yaeos__equilibria_boundaries_phase_envelopes_px module~yaeos__equilibria->module~yaeos__equilibria_boundaries_phase_envelopes_px module~yaeos__equilibria_boundaries_phase_envelopes_tx yaeos__equilibria_boundaries_phase_envelopes_tx module~yaeos__equilibria->module~yaeos__equilibria_boundaries_phase_envelopes_tx module~yaeos__equilibria_boundaries_phase_envelopes_pt->module~yaeos__equilibria_boundaries_phase_envelopes_mp module~yaeos__equilibria_boundaries_phase_envelopes_px->module~yaeos__equilibria_boundaries_auxiliar module~yaeos__equilibria_boundaries_phase_envelopes_tx->module~yaeos__equilibria_boundaries_auxiliar module~yaeos__models_ge_implementations yaeos__models_ge_implementations module~yaeos__models_ge_implementations->module~yaeos__models_ge_uniquac proc~update_specification~3 update_specification proc~update_specification~3->module~yaeos__equilibria_boundaries_auxiliar module~yaeos yaeos module~yaeos->module~yaeos__equilibria module~yaeos__models yaeos__models module~yaeos->module~yaeos__models module~yaeos__models->module~yaeos__models_ge_implementations proc~psrk PSRK proc~psrk->module~yaeos__models_ge_implementations module~yaeos__extra_fluids yaeos__extra_fluids module~yaeos__extra_fluids->module~yaeos__models proc~co2_h2o_isop co2_h2o_isop proc~co2_h2o_isop->module~yaeos__models

Interfaces

public interface newton

  • public subroutine newton_1d(f, x, tol, max_iters, failed)

    newton_1d

    Description

    Find a root of a scalar function by damped Newton iteration. Each step is halved until its magnitude is at most half the current iterate’s magnitude. This is a step-size safeguard, not a residual line search.

    Arguments

    Type IntentOptional Attributes Name
    procedure(f_1d) :: f

    Function and derivative callback.

    real(kind=pr), intent(inout) :: x

    Initial guess, replaced by the root estimate.

    real(kind=pr), intent(in) :: tol

    Absolute tolerance for residual and step size.

    integer, intent(in) :: max_iters

    Maximum Newton iterations.

    logical, intent(out) :: failed

    True when the iteration limit is exhausted.


Abstract Interfaces

abstract interface

  • public subroutine f_1d(x, f, df)

    Callback interface for a scalar residual and its derivative.

    Arguments

    Type IntentOptional Attributes Name
    real(kind=pr), intent(in) :: x

    Current scalar iterate.

    real(kind=pr), intent(out) :: f

    Residual value .

    real(kind=pr), intent(out) :: df

    Derivative .


Derived Types

type, public ::  Point

Represents the single point of a line segment.

Read more…

Components

Type Visibility Attributes Name Initial
integer, public :: i

Index of the first line segment

integer, public :: j

Index of the second line segment

real(kind=pr), public :: x

X coordinate

real(kind=pr), public :: y

Y coordinate


Functions

public function central_numdiff(f, x, eps) result(df)

Approximate the Jacobian of a vector function by centered differences. Column i is evaluated using a perturbation ; thus this routine requires nonzero entries in x for finite differences.

Arguments

Type IntentOptional Attributes Name
public function f(x)

Vector-valued function differentiated by central_numdiff.

Arguments
Type IntentOptional Attributes Name
real(kind=pr), intent(in) :: x(:)

Function input vector.

Return Value real(kind=pr), (size(x))

Function output vector.

real(kind=pr), intent(in) :: x(:)

Evaluation point.

real(kind=pr), intent(in) :: eps

Relative finite-difference step multiplier.

Return Value real(kind=pr), (size(x),size(x))

Approximate Jacobian matrix.

public function derivative_d2xk_dnidnj(n) result(d2xk_dnidnj)

Calculate the mole fraction second derivatives respect to mole numbers

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=pr), intent(in) :: n(:)

Component mole amounts.

Return Value real(kind=pr), (size(n),size(n),size(n))

, indexed (k,i,j).

public function derivative_dxk_dni(n) result(dxk_dni)

Calculate the mole fraction first derivatives respect to mole numbers

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=pr), intent(in) :: n(:)

Component mole amounts.

Return Value real(kind=pr), (size(n),size(n))

, indexed (k,i).

public function dx_to_dn(x, dx) result(dn)

Convert the mole fraction derivatives of a quantity (calculated so they do not sum to 1) to mole number derivatives (where the mole fractions do sum to one). Requires the derivatives and the mole fractions of the mixture. From Chemicals (Python)

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=pr), intent(in) :: x(:)

Mole-fraction vector.

real(kind=pr), intent(in) :: dx(:)

Derivative of the property with respect to mole fractions.

Return Value real(kind=pr), (size(x))

public elemental function interpol(x1, x2, y1, y2, x_obj) result(y)

Linear interpolation.

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=pr), intent(in) :: x1

First point x value

real(kind=pr), intent(in) :: x2

Second point x value

real(kind=pr), intent(in) :: y1

First point y value

real(kind=pr), intent(in) :: y2

Second point y value

real(kind=pr), intent(in) :: x_obj

Desired x value to interpolate

Return Value real(kind=pr)

y value at x_obj

public function intersect_one_line(lx, ly) result(intersections)

Find the intersections of a single line with itself.

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=pr), intent(in) :: lx(:)

X coordinates of the polyline vertices.

real(kind=pr), intent(in) :: ly(:)

Y coordinates of the polyline vertices.

Return Value type(Point), allocatable, (:)

Detected non-adjacent segment intersections.

public elemental function sq_error(exp, pred)

Returns the squared relative error . exp must be nonzero; the result is dimensionless and elementally applicable to scalar or array values.

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=pr), intent(in) :: exp

Reference or expected value; must be nonzero.

real(kind=pr), intent(in) :: pred

Predicted value being compared with exp.

Return Value real(kind=pr)


Subroutines

public subroutine intersects(x1, x2, x3, x4, y1, y2, y3, y4, s, t)

Calculate the intersection between two line segments.

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=pr), intent(in) :: x1

X coordinate of the first endpoint of segment 1.

real(kind=pr), intent(in) :: x2

X coordinate of the second endpoint of segment 1.

real(kind=pr), intent(in) :: x3

X coordinate of the first endpoint of segment 2.

real(kind=pr), intent(in) :: x4

X coordinate of the second endpoint of segment 2.

real(kind=pr), intent(in) :: y1

Y coordinate of the first endpoint of segment 1.

real(kind=pr), intent(in) :: y2

Y coordinate of the second endpoint of segment 1.

real(kind=pr), intent(in) :: y3

Y coordinate of the first endpoint of segment 2.

real(kind=pr), intent(in) :: y4

Y coordinate of the second endpoint of segment 2.

real(kind=pr), intent(out) :: s

Normalized position along segment 1; s and t both in [0,1] iff the segments intersect.

real(kind=pr), intent(out) :: t

Normalized position along segment 2.

public subroutine levenberg_marquardt(fun, tol, X, F, info)

Solve a nonlinear least-squares system with MINPACK’s lmdif1. The routine updates X and returns residuals and MINPACK status.

Arguments

Type IntentOptional Attributes Name
public subroutine fun(m, n, x, fvec, iflag)

fun

Description

MINPACK residual callback for rectangular least-squares systems.

Arguments
Type IntentOptional Attributes Name
integer, intent(in) :: m

Number of residual equations.

integer, intent(in) :: n

Number of unknowns.

real(kind=pr), intent(in) :: x(n)

Current unknown vector.

real(kind=pr), intent(out) :: fvec(m)

Residual vector.

integer, intent(inout) :: iflag

MINPACK callback control/status flag.

real(kind=pr), intent(in) :: tol

Convergence tolerance passed to MINPACK.

real(kind=pr), intent(inout) :: X(:)

Variables vector

real(kind=pr), intent(out) :: F(:)

Function values at solved point

integer, intent(inout) :: info

MINPACK termination code.

public subroutine newton_1d(f, x, tol, max_iters, failed)

Find a root of a scalar function by damped Newton iteration. Each step is halved until its magnitude is at most half the current iterate’s magnitude. This is a step-size safeguard, not a residual line search.

Read more…

Arguments

Type IntentOptional Attributes Name
procedure(f_1d) :: f

Function and derivative callback.

real(kind=pr), intent(inout) :: x

Initial guess, replaced by the root estimate.

real(kind=pr), intent(in) :: tol

Absolute tolerance for residual and step size.

integer, intent(in) :: max_iters

Maximum Newton iterations.

logical, intent(out) :: failed

True when the iteration limit is exhausted.

public subroutine powel_hybrid(fun, tol, X, F, info)

Solve a square nonlinear system with MINPACK’s Powell hybrid method.

Read more…

Arguments

Type IntentOptional Attributes Name
public subroutine fun(n, x, fvec, iflag)

fun

Description

MINPACK residual callback for a square nonlinear system.

Arguments
Type IntentOptional Attributes Name
integer, intent(in) :: n

Number of unknowns and residuals.

real(kind=pr), intent(in) :: x(n)

Current unknown vector.

real(kind=pr), intent(out) :: fvec(n)

Residual vector.

integer, intent(inout) :: iflag

MINPACK callback control/status flag.

real(kind=pr), intent(in) :: tol

Convergence tolerance passed to MINPACK.

real(kind=pr), intent(inout) :: X(:)

Variables vector

real(kind=pr), intent(out) :: F(:)

Function values at solved point

integer, intent(out) :: info

MINPACK termination code.