Skip to content

Rungekutta

RungeKutta

Bases: _Generator_

This class is an implementation of Generator for a Runge-Kutta 4th order data generator.

Source code in phaseportrait/trajectories/rungekutta.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class RungeKutta(_Generator_):
    """
    This class is an implementation of _Generator_ for a Runge-Kutta 4th order data generator.
    """
    def __init__(self, portrait, dF, dimension, max_values, *, dt=0.1, dF_args=None, initial_values=None, thermalization=0):
        """
        Args:
            portrait (Object) : Class that uses the RungeKutta objects.
            dF (callable) : A dF type funcion.
            dimension (int) : Number of dimensions in which it calculates the next values. Must equal the amount of outputs the `dF`
                funcion gives.
            max_values (int) : Max number of values saved.
            dt (float, optional, by default 0.1) : Time interval used in the Runge-Kutta 4th order method.
            dF_args (dict) : If necesary, must contain the kargs for the `dF` funcion. By default, None. 
            initial_values (float, list, optional) : Initial set of conditions, by default None.
                If None, random initial conditions are aplied in the interval [0,1) for each coordinate.
            thermalization (int, optional) : Thermalization steps before data is saved, by default None. 
                If None, thermalization steps are set to 0.
        """
        super().__init__(portrait, dF, dimension, max_values, dF_args=dF_args,
                         initial_values=initial_values, thermalization=thermalization)

        self.dt = dt
        self.positions = self._create_values_array()
        self.velocities = self._create_values_array()

    @classmethod
    def instance_and_compute_all(cls, portrait, dF, dimension, dF_args, initial_values, max_values, save_freq=1, dt=0.1, thermalization=0):
        """Creates an instance of phase-portrait.trajectories.RungeKutta. 
        Computes all the data requested and returns the instance.

        Args:
            portrait (Object):  Class that uses the RungeKutta objects.
            dF (callable) : A dF type funcion.
            dimension (int) : Number of dimensions in which it calculates the next values. Must equal the amount of outputs the `dF`
                funcion gives.
            dF_args (dict) : If necesary, must contain the kargs for the `dF` funcion. By default, None.
            initial_values (float, list, optional) : Initial set of conditions, by default None.
                If None, random initial conditions are aplied in the interval [0,1) for each coordinat
            max_values (int) : Max number of values saved.
            save_freq (int, optional, by default 1) : Number of values computed before saving them.
            dt (float, optional, by default 0.1) : Time interval used in the Runge-Kutta 4th order method.
            thermalization (int, optional) : Thermalization steps before data is saved, by default None. 
                If None, thermalization steps are set to 0.
        """
        instance = cls(portrait, dF, dimension, dt=dt, dF_args=dF_args, initial_values=initial_values, thermalization=thermalization)

        instance.compute_all()
        return instance


    def _next(self):
        """
        Generates from `self.position` its following value via the Runge-Kutta 4th order method.
        """
        k1 = np.array(self.dF(*(self.position), **self.dF_args))
        k2 = np.array(self.dF(*(self.position+0.5*k1*self.dt), **self.dF_args))
        k3 = np.array(self.dF(*(self.position+0.5*k2*self.dt), **self.dF_args))
        k4 = np.array(self.dF(*(self.position+k3*self.dt), **self.dF_args))
        self.velocity = 1/6*(k1+2*k2+2*k3+k4)
        if (self.velocity == 0).all():
            self.flag_stop = True
        self.position += self.velocity*self.dt


    def save(self, i):
        """
        Saves `self.position` in the attribute `self.positions`, and `self.velocity` in `self.velocities`.

        Args:
            i (int) : Index in which the data is saved.
        """
        try:
            self.positions[:, i] = self.position
            self.velocities[:, i] = self.velocity
        except IndexError:
            np.concatenate(self.positions, self._create_values_array(), axis=1)
            np.concatenate(self.velocities, self._create_values_array(), axis=1)
            self.max_values += 2000
            self.save(i)


    def clear_values(self):
        """
        Clears the data arrays `self.positions` and `self.velocities`.
        """
        self.positions[:,1:] = 0
        self.velocity[:,1:] = 0

__init__(portrait, dF, dimension, max_values, *, dt=0.1, dF_args=None, initial_values=None, thermalization=0)

Parameters:

Name Type Description Default
portrait Object)

Class that uses the RungeKutta objects.

required
dF callable)

A dF type funcion.

required
dimension int)

Number of dimensions in which it calculates the next values. Must equal the amount of outputs the dF funcion gives.

required
max_values int)

Max number of values saved.

required
dt float, optional, by default 0.1)

Time interval used in the Runge-Kutta 4th order method.

0.1
dF_args dict)

If necesary, must contain the kargs for the dF funcion. By default, None.

None
initial_values float, list, optional)

Initial set of conditions, by default None. If None, random initial conditions are aplied in the interval [0,1) for each coordinate.

None
thermalization int, optional)

Thermalization steps before data is saved, by default None. If None, thermalization steps are set to 0.

0
Source code in phaseportrait/trajectories/rungekutta.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def __init__(self, portrait, dF, dimension, max_values, *, dt=0.1, dF_args=None, initial_values=None, thermalization=0):
    """
    Args:
        portrait (Object) : Class that uses the RungeKutta objects.
        dF (callable) : A dF type funcion.
        dimension (int) : Number of dimensions in which it calculates the next values. Must equal the amount of outputs the `dF`
            funcion gives.
        max_values (int) : Max number of values saved.
        dt (float, optional, by default 0.1) : Time interval used in the Runge-Kutta 4th order method.
        dF_args (dict) : If necesary, must contain the kargs for the `dF` funcion. By default, None. 
        initial_values (float, list, optional) : Initial set of conditions, by default None.
            If None, random initial conditions are aplied in the interval [0,1) for each coordinate.
        thermalization (int, optional) : Thermalization steps before data is saved, by default None. 
            If None, thermalization steps are set to 0.
    """
    super().__init__(portrait, dF, dimension, max_values, dF_args=dF_args,
                     initial_values=initial_values, thermalization=thermalization)

    self.dt = dt
    self.positions = self._create_values_array()
    self.velocities = self._create_values_array()

clear_values()

Clears the data arrays self.positions and self.velocities.

Source code in phaseportrait/trajectories/rungekutta.py
87
88
89
90
91
92
def clear_values(self):
    """
    Clears the data arrays `self.positions` and `self.velocities`.
    """
    self.positions[:,1:] = 0
    self.velocity[:,1:] = 0

instance_and_compute_all(portrait, dF, dimension, dF_args, initial_values, max_values, save_freq=1, dt=0.1, thermalization=0) classmethod

Creates an instance of phase-portrait.trajectories.RungeKutta. Computes all the data requested and returns the instance.

Parameters:

Name Type Description Default
portrait Object

Class that uses the RungeKutta objects.

required
dF callable)

A dF type funcion.

required
dimension int)

Number of dimensions in which it calculates the next values. Must equal the amount of outputs the dF funcion gives.

required
dF_args dict)

If necesary, must contain the kargs for the dF funcion. By default, None.

required
initial_values float, list, optional)

Initial set of conditions, by default None. If None, random initial conditions are aplied in the interval [0,1) for each coordinat

required
max_values int)

Max number of values saved.

required
save_freq int, optional, by default 1)

Number of values computed before saving them.

1
dt float, optional, by default 0.1)

Time interval used in the Runge-Kutta 4th order method.

0.1
thermalization int, optional)

Thermalization steps before data is saved, by default None. If None, thermalization steps are set to 0.

0
Source code in phaseportrait/trajectories/rungekutta.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@classmethod
def instance_and_compute_all(cls, portrait, dF, dimension, dF_args, initial_values, max_values, save_freq=1, dt=0.1, thermalization=0):
    """Creates an instance of phase-portrait.trajectories.RungeKutta. 
    Computes all the data requested and returns the instance.

    Args:
        portrait (Object):  Class that uses the RungeKutta objects.
        dF (callable) : A dF type funcion.
        dimension (int) : Number of dimensions in which it calculates the next values. Must equal the amount of outputs the `dF`
            funcion gives.
        dF_args (dict) : If necesary, must contain the kargs for the `dF` funcion. By default, None.
        initial_values (float, list, optional) : Initial set of conditions, by default None.
            If None, random initial conditions are aplied in the interval [0,1) for each coordinat
        max_values (int) : Max number of values saved.
        save_freq (int, optional, by default 1) : Number of values computed before saving them.
        dt (float, optional, by default 0.1) : Time interval used in the Runge-Kutta 4th order method.
        thermalization (int, optional) : Thermalization steps before data is saved, by default None. 
            If None, thermalization steps are set to 0.
    """
    instance = cls(portrait, dF, dimension, dt=dt, dF_args=dF_args, initial_values=initial_values, thermalization=thermalization)

    instance.compute_all()
    return instance

save(i)

Saves self.position in the attribute self.positions, and self.velocity in self.velocities.

Parameters:

Name Type Description Default
i int)

Index in which the data is saved.

required
Source code in phaseportrait/trajectories/rungekutta.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def save(self, i):
    """
    Saves `self.position` in the attribute `self.positions`, and `self.velocity` in `self.velocities`.

    Args:
        i (int) : Index in which the data is saved.
    """
    try:
        self.positions[:, i] = self.position
        self.velocities[:, i] = self.velocity
    except IndexError:
        np.concatenate(self.positions, self._create_values_array(), axis=1)
        np.concatenate(self.velocities, self._create_values_array(), axis=1)
        self.max_values += 2000
        self.save(i)