#########################################################################################
##
## SOURCE BLOCKS (blocks/sources.py)
##
## This module defines blocks that serve purely as inputs / sources
## for the simulation such as the generic 'Source' block
##
## Milan Rother 2024
##
#########################################################################################
# IMPORTS ===============================================================================
from ._block import Block
# INPUT BLOCKS ==========================================================================
[docs]
class Constant(Block):
"""Produces a constant output signal (SISO)
Notes
-----
same as 'Source' with func=lambda t:value,
therefore one could argue that it is redundant
Parameters
----------
value : float
constant defining block output
"""
def __init__(self, value=1):
super().__init__()
self.value = value
[docs]
def update(self, t):
"""update system equation fixed point loop
Parameters
----------
t : float
evaluation time
Returns
-------
error : float
relative error to previous iteration for convergence control
"""
self.outputs[0] = self.value
return 0.0
[docs]
class Source(Block):
"""Source that produces an arbitrary time dependent output,
defined by the func (callable).
.. math::
\\y(t) = \\mathrm{func}(t)
Example
-------
For example a ramp:
.. code-block:: python
from pathsim.blocks import Source
src = Source(lambda t : t)
or a simple sinusoid with some frequency:
.. code-block:: python
import numpy as np
from pathsim.blocks import Source
#some parameter
omega = 100
#the function that gets evaluated
def f(t):
return np.sin(omega * t)
src = Source(f)
Parameters
----------
func : callable
function defining time dependent block output
"""
def __init__(self, func=lambda t: 1):
super().__init__()
if not callable(func):
raise ValueError(f"'{func}' is not callable")
self.func = func
[docs]
def update(self, t):
"""update system equation fixed point loop
by evaluating the internal function 'func'
Parameters
----------
t : float
evaluation time
Returns
-------
error : float
relative error to previous iteration for convergence control
"""
self.outputs[0] = self.func(t)
return 0.0