Skip to content

Solvers

Fixed-step solvers. Simulator needs one of the DAE solvers.

solvers

logger module-attribute

logger = logging.getLogger(__name__)

Euler

Euler(f, t0, x0, t_end=np.inf, dt=0.005, **kwargs)
Source code in src/tops/solvers.py
def __init__(self, f, t0, x0, t_end=np.inf, dt=5e-3, **kwargs):
    self.f = f
    self.t = t0
    self.x = x0.copy()
    self.y = self.x
    self.t_end = t_end
    self.dt = dt

    for key, value in kwargs.items():
        if key == 'max_step':
            self.dt = value

f instance-attribute

f = f

t instance-attribute

t = t0

x instance-attribute

x = x0.copy()

y instance-attribute

y = self.x

t_end instance-attribute

t_end = t_end

dt instance-attribute

dt = dt

step

step()
Source code in src/tops/solvers.py
def step(self):
    if self.t < self.t_end:
        self.x[:] = self.x + self.f(self.t, self.x)*self.dt
        self.t += self.dt

EulerDAE

EulerDAE(f, g_inv, t0, x0, *args, v0=None, **kwargs)

Bases: Euler

Similar to Euler solver-class, but ensures that algebraic equations (stored in self.v) are always updated at the end of each time step.

Parameters:

Name Type Description Default
f

Function that takes time, states and algebraic variables (t, x and v) as arguments and returns state derivatives.

required
g_inv

Function that takes time, states and an initial guess of the algebraic variables (t, x and v) as arguments and solves algebraic equations (i.e. returns bus voltages of buses in reduced system).

required
t0

Initial time.

required
x0

Initial states.

required
args

Passed on to Euler (e.g. t_end).

()
v0

Initial guess of the algebraic variables, passed to g_inv in the first solve. If None, g_inv chooses its own starting point (PowerSystemModel.solve_algebraic uses a flat start).

None
kwargs

Passed on to Euler (e.g. dt, max_step).

{}
Source code in src/tops/solvers.py
def __init__(self, f, g_inv, t0, x0, *args, v0=None, **kwargs):
    '''
    Similar to Euler solver-class, but ensures that algebraic equations (stored in self.v) are always updated at the end of each time step.
    :param f: Function that takes time, states and algebraic variables (t, x and v) as arguments and returns state
    derivatives.
    :param g_inv: Function that takes time, states and an initial guess of the algebraic variables (t, x and v)
    as arguments and solves algebraic equations (i.e. returns bus voltages of buses in reduced system).
    :param t0: Initial time.
    :param x0: Initial states.
    :param args: Passed on to Euler (e.g. t_end).
    :param v0: Initial guess of the algebraic variables, passed to g_inv in the first solve. If None, g_inv
    chooses its own starting point (PowerSystemModel.solve_algebraic uses a flat start).
    :param kwargs: Passed on to Euler (e.g. dt, max_step).
    '''
    super().__init__(f, t0, x0, *args, **kwargs)
    self.g_inv = g_inv
    self.v = self.g_inv(self.t, self.x, v0)

g_inv instance-attribute

g_inv = g_inv

v instance-attribute

v = self.g_inv(self.t, self.x, v0)

step

step()
Source code in src/tops/solvers.py
def step(self):
    if self.t < self.t_end:
        self.x[:] = self.x + self.f(self.t, self.x, self.v) * self.dt
        self.t += self.dt
        self.v[:] = self.g_inv(self.t, self.x, self.v)

    else:
        logger.info('End of simulation time reached.')

ModifiedEuler

ModifiedEuler(*args, n_it=1, **kwargs)

Bases: Euler

Source code in src/tops/solvers.py
def __init__(self, *args, n_it=1, **kwargs):
    super().__init__(*args, **kwargs)
    self.n_it = n_it

n_it instance-attribute

n_it = n_it

step

step()
Source code in src/tops/solvers.py
def step(self):
    if self.t < self.t_end:
        dxdt_0 = self.f(self.t, self.x)
        x_1 = self.x + dxdt_0*self.dt
        for _ in range(self.n_it):
            dxdt_1 = self.f(self.t + self.dt, x_1)
            dxdt_est = (dxdt_0 + dxdt_1) / 2
            x_1 = self.x + dxdt_est*self.dt

        self.x[:] = x_1
        self.t += self.dt

    else:
        logger.info('End of simulation time reached.')

ModifiedEulerDAE

ModifiedEulerDAE(*args, n_it=1, **kwargs)

Bases: EulerDAE

Source code in src/tops/solvers.py
def __init__(self, *args, n_it=1, **kwargs):
    super().__init__(*args, **kwargs)
    self.n_it = n_it
    self.f_ode = lambda t, x: self.f(t, x, self.g_inv(t, x, self.v))

n_it instance-attribute

n_it = n_it

f_ode instance-attribute

f_ode = lambda t, x: self.f(t, x, self.g_inv(t, x, self.v))

step

step()
Source code in src/tops/solvers.py
def step(self):
    if self.t < self.t_end:
        dxdt_0 = self.f(self.t, self.x, self.v)
        x_1 = self.x + dxdt_0*self.dt
        for _ in range(self.n_it):
            dxdt_1 = self.f_ode(self.t + self.dt, x_1)
            dxdt_est = (dxdt_0 + dxdt_1) / 2
            x_1 = self.x + dxdt_est*self.dt

        self.x[:] = x_1
        self.v[:] = self.g_inv(self.t, self.x, self.v)
        self.t += self.dt

    else:
        logger.info('End of simulation time reached.')

SimpleRK4

SimpleRK4(*args, **kwargs)

Bases: Euler

Source code in src/tops/solvers.py
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)

step

step()
Source code in src/tops/solvers.py
def step(self):
    x = self.x
    t = self.t
    dt = self.dt

    if t < self.t_end:
        k_1 = self.f(t, x)
        k_2 = self.f(t + dt / 2, x + (dt / 2) * k_1)
        k_3 = self.f(t + dt / 2, x + (dt / 2) * k_2)
        k_4 = self.f(t + dt, x + dt * k_3)

        self.x[:] = x + (dt / 6) * (k_1 + 2 * k_2 + 2 * k_3 + k_4)
        self.t = t + dt
    else:
        logger.info('End of simulation time reached.')

solvers_sde

logger module-attribute

logger = logging.getLogger(__name__)

EulerDAE_SDE

EulerDAE_SDE(*args, dim_w=0, **kwargs)

Bases: EulerDAE

First implementation of a Stochastic Differential Equation Solver, based on following code snippet: https://en.wikipedia.org/wiki/Euler%E2%80%93Maruyama_method

Source code in src/tops/solvers_sde.py
def __init__(self, *args, dim_w=0, **kwargs):
    '''
    First implementation of a Stochastic Differential Equation Solver, based on following code snippet:
    https://en.wikipedia.org/wiki/Euler%E2%80%93Maruyama_method
    '''
    super().__init__(*args, **kwargs)
    self.dim_w = dim_w
    self.b = lambda t, x, v: np.zeros((len(self.x), self.dim_w))
    self.dw = np.zeros(self.dim_w)

dim_w instance-attribute

dim_w = dim_w

b instance-attribute

b = lambda t, x, v: np.zeros((len(self.x), self.dim_w))

dw instance-attribute

dw = np.zeros(self.dim_w)

step

step()
Source code in src/tops/solvers_sde.py
def step(self):
    self.dw = np.random.normal(loc=0.0, scale=np.sqrt(self.dt), size=self.dim_w)
    if self.t < self.t_end:
        self.x[:] = self.x + self.f(self.t, self.x, self.v)*self.dt + np.dot(self.b(self.t, self.x, self.v), self.dw)
        self.t += self.dt
        self.v[:] = self.g_inv(self.t, self.x)

    else:
        logger.info('End of simulation time reached.')