geff.utility.general

Some general purpose utility functions.

 1"""
 2Some general purpose utility functions.
 3"""
 4import numpy as np
 5from typing import Callable
 6
 7class AuxTol:
 8    """
 9    A simple wrapper which passes absolute and relative tolerance parameters.
10
11    Wraps a function `f(*args, rtol, atol)` to give `f(*args, self.rtol, self.atol)` 
12
13
14    """
15    atol:float = 1e-20
16    """Absolute tolerance"""
17    rtol:float = 1e-6
18    """Relative tolerance"""
19
20    def __init__(self, func:Callable):
21        self.func = func
22
23    def __call__(self, *args):
24        return self.func(*args, rtol=self.rtol, atol=self.atol)
25
26@AuxTol
27@np.vectorize
28def heaviside(x : float, y:float, rtol:float, atol:float) -> float:
29    r"""
30    A smoothed version of the heaviside function.
31
32    Returns 1 if x > y and 0 if x < y.
33    
34    The smoothness of the  transition is regulated by `max(abs(x)*rtol, atol))`.
35
36    The function is wrapped by `AuxTol`, and will be updated according to the tolerances of a `GEFSolver`.
37
38    It is vectorized using `numpy.vectorize`
39    
40    Parameters
41    ----------
42    x, y : floats
43        the arguments of the heaviside function
44    rtol, atol : floats
45        tolerance parameters
46    """
47    eps = max(abs(x)*rtol, atol)
48    arg = np.clip((x-y)/eps, -1e2, 1e18)
49    return 1/(1+np.exp(-arg))
class AuxTol:
 8class AuxTol:
 9    """
10    A simple wrapper which passes absolute and relative tolerance parameters.
11
12    Wraps a function `f(*args, rtol, atol)` to give `f(*args, self.rtol, self.atol)` 
13
14
15    """
16    atol:float = 1e-20
17    """Absolute tolerance"""
18    rtol:float = 1e-6
19    """Relative tolerance"""
20
21    def __init__(self, func:Callable):
22        self.func = func
23
24    def __call__(self, *args):
25        return self.func(*args, rtol=self.rtol, atol=self.atol)

A simple wrapper which passes absolute and relative tolerance parameters.

Wraps a function f(*args, rtol, atol) to give f(*args, self.rtol, self.atol)

atol: float = 1e-20

Absolute tolerance

rtol: float = 1e-06

Relative tolerance

heaviside = <AuxTol object>

A smoothed version of the heaviside function.

Returns 1 if x > y and 0 if x < y.

The smoothness of the transition is regulated by max(abs(x)*rtol, atol)).

The function is wrapped by AuxTol, and will be updated according to the tolerances of a GEFSolver.

It is vectorized using numpy.vectorize

Parameters
  • x, y (floats): the arguments of the heaviside function
  • rtol, atol (floats): tolerance parameters