예제 #1
0
def simulation_rlc_without_split():
    rlc = core.__copy__()
    # Define the simulation parameters
    config = {'fs': 48e3,               # Sample rate
              'grad': 'discret',    # in {'discret', 'theta', 'trapez'}
              'theta': 0.5,             # theta-scheme for the structure
              'split': False,           # apply core.split_linear() beforehand
              'maxit': 10,              # Max iteration for NL solvers
              'eps': 1e-16,          # Global numerical tolerance
              'path': path,             # Path to the results folder
              'pbar': False,      # Display a progress bar
              'timer': True,            # Display minimal timing infos
              'lang': 'python',     # in {'python', 'c++'}
              }

    simu = Simulation(rlc.to_method(), config=config)

    dur = 0.01
    u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs'])

    def sequ():
        for el in u():
            yield (el, )

    simu.init(u=sequ(), nt=int(dur*simu.config['fs']))

    simu.process()

    if not os.name.lower().startswith('nt'):
        shutil.rmtree(path)
    return True
예제 #2
0
def simulation_rlc_cpp():
    rlc = core.__copy__()
    # Define the simulation parameters
    config = {'fs': 48e3,               # Sample rate
              'grad': 'discret',    # in {'discret', 'theta', 'trapez'}
              'theta': 0.5,             # theta-scheme for the structure
              'split': True,           # apply core.split_linear() beforehand
              'maxit': 10,              # Max iteration for NL solvers
              'eps': 1e-16,          # Global numerical tolerance
              'path': path,             # Path to the results folder
              'pbar': False,      # Display a progress bar
              'timer': True,            # Display minimal timing infos
              'lang': 'c++',     # in {'python', 'c++'}
              }

    simu = Simulation(rlc.to_method(), config=config)

    dur = 0.01
    u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs'])

    def sequ():
        for el in u():
            yield (el, )

    simu.init(u=sequ(), nt=int(dur*simu.config['fs']))
    simu2cpp(simu)

    if not os.name.lower().startswith('nt'):
        shutil.rmtree(path)
    return True
예제 #3
0
def plot_rlc_with_split():
    # Define the simulation parameters
    config = {'fs': 48e3,               # Sample rate
              'grad': 'discret',    # in {'discret', 'theta', 'trapez'}
              'theta': 0.5,             # theta-scheme for the structure
              'split': False,           # apply core.split_linear() beforehand
              'maxit': 10,              # Max iteration for NL solvers
              'eps': 1e-16,          # Global numerical tolerance
              'path': path,             # Path to the results folder
              'pbar': False,      # Display a progress bar
              'timer': True,            # Display minimal timing infos
              'lang': 'python',     # in {'python', 'c++'}
              }

    # retrieve the pyphs.Core of a linear RLC from the examples
    from pyphs.examples.rlc.rlc import core as rlc

    print(rlc.w)
    print(rlc.z)

    simu = Simulation(rlc.to_method(), config=config)

    dur = 0.01
    u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs'])

    def sequ():
        for el in u():
            yield (el, )

    simu.init(u=sequ(), nt=int(dur*simu.config['fs']))
    simu.process()

    print(simu.nums.method.w)
    print(simu.nums.method.z)

    dims = simu.nums.method.dims
    # plot u, y
    simu.data.plot([('u', i) for i in range(dims.y())] +
                   [('y', i) for i in range(dims.y())], show=False)
    # plot w, z
    simu.data.plot([('w', 0), ('z', 0)], show=False)

    simu.data.plot([('dtx', i) for i in range(dims.x())] +
                   [('dxH', i) for i in range(dims.x())], show=False)

    if not os.name.lower().startswith('nt'):
        shutil.rmtree(path)
    return True
예제 #4
0
    def to_simulation(self, config=None, inits=None, erase=True):
        """
        Return a Numeric object for the evaluation of the PHS numerical method.

        Parameter
        ---------

        inits : dict or None (optional)
            Dictionary with variable name as keys and initialization values
            as value. E.g: inits = {'x': [0, 0, 1]} to initalize state x
            with dim(x) = 3, x[0] = x[1] = 0 and x[2] = 1.

        erase : bool (optional)
            If True and a h5file exists with same path than simulation data,
            it is erased. Else, it is used to initialize the data object. The
            default is True.

        Output
        ------

        numeric : pyphs.Simulation
            Object for the numerical evaluation of the PHS numerical method.
        """
        from pyphs import Simulation
        return Simulation(self, inits=inits, config=config, erase=erase)
예제 #5
0
def plot_power_balance_rlc_with_split():
    # Define the simulation parameters
    config = {
        'fs': 48e3,  # Sample rate
        'grad': 'discret',  # in {'discret', 'theta', 'trapez'}
        'theta': 0.5,  # theta-scheme for the structure
        'split': False,  # apply core.split_linear() beforehand
        'maxit': 10,  # Max iteration for NL solvers
        'path': path,  # Path to the results folder
        'pbar': False,  # Display a progress bar
        'timer': True,  # Display minimal timing infos
        'lang': 'python',  # in {'python', 'c++'}
    }

    # retrieve the pyphs.Core of a linear RLC from the examples
    from pyphs.examples.rlc.rlc import core as rlc

    rlc_ = rlc.__copy__()
    rlc_.reduce_z()
    simu = Simulation(rlc_.to_method(), config=config)

    dur = 0.01
    u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs'])

    sequ = u[:, np.newaxis]
    simu.init(u=sequ)
    simu.process()

    simu.data.plot_powerbal(mode='single', show=False)
    simu.data.plot_powerbal(mode='multi', show=False)

    if not os.name.lower().startswith('nt'):
        shutil.rmtree(path)

    return True
예제 #6
0
def plot_power_balance_nlcore_with_split():
    # Define the simulation parameters
    config = {'fs': 48e3,               # Sample rate
              'grad': 'discret',    # in {'discret', 'theta', 'trapez'}
              'theta': 0.5,             # theta-scheme for the structure
              'split': True,           # apply core.split_linear() beforehand
              'maxit': 10,              # Max iteration for NL solvers
              'eps': 1e-16,          # Global numerical tolerance
              'path': path,             # Path to the results folder
              'pbar': False,      # Display a progress bar
              'timer': True,            # Display minimal timing infos
              'lang': 'python',     # in {'python', 'c++'}
              }


    # retrieve the pyphs.Core of a nonlinear RLC from
    # the tutorial on Core
    from pyphs.tutorials.core import core as nlcore
    nlcore.reduce_z()
    simu = Simulation(nlcore.to_method(), config=config)

    dur = 0.01
    u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs'])

    def sequ():
        for el in u():
            yield (el, )

    simu.init(u=sequ(), nt=int(dur*simu.config['fs']))
    simu.process()

    simu.data.plot_powerbal(mode='single', show=False)
    simu.data.plot_powerbal(mode='multi', show=False)

    if not os.name.lower().startswith('nt'):
        shutil.rmtree(path)
    return True
예제 #7
0
def plot_rlc_with_split():
    # Define the simulation parameters
    config = {
        'fs': 48e3,  # Sample rate
        'grad': 'discret',  # in {'discret', 'theta', 'trapez'}
        'theta': 0.5,  # theta-scheme for the structure
        'split': False,  # apply core.split_linear() beforehand
        'maxit': 10,  # Max iteration for NL solvers
        'eps': 1e-16,  # Global numerical tolerance
        'path': path,  # Path to the results folder
        'pbar': False,  # Display a progress bar
        'timer': True,  # Display minimal timing infos
        'lang': 'python',  # in {'python', 'c++'}
        'eigen': None,  # path to Eigen library
    }

    # retrieve the pyphs.Core of a linear RLC from the examples
    from pyphs.examples.rlc.rlc import core as rlc

    print(rlc.w)
    print(rlc.z)

    simu = Simulation(rlc, config=config)

    dur = 0.01
    u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs'])

    def sequ():
        for el in u():
            yield (el, )

    simu.init(u=sequ(), nt=int(dur * simu.config['fs']))
    simu.process()

    print(simu.nums.method.w)
    print(simu.nums.method.z)

    dims = simu.nums.method.dims
    # plot u, y
    simu.data.plot([('u', i)
                    for i in range(dims.y())] + [('y', i)
                                                 for i in range(dims.y())],
                   show=False)
    # plot w, z
    simu.data.plot([('w', 0), ('z', 0)], show=False)

    simu.data.plot([('dtx', i)
                    for i in range(dims.x())] + [('dxH', i)
                                                 for i in range(dims.x())],
                   show=False)

    if not os.name.lower().startswith('nt'):
        shutil.rmtree(path)
    return True
예제 #8
0
    def to_simulation(self, config=None, inits=None):
        """
        Return a Numeric object for the evaluation of the PHS numerical method.

        Parameter
        ---------

        inits : dict or None (optional)
            Dictionary with variable name as keys and initialization values
            as value. E.g: inits = {'x': [0, 0, 1]} to initalize state x
            with dim(x) = 3, x[0] = x[1] = 0 and x[2] = 1.

        Return
        ------
        numeric : pyphs.Simulation
            Object for the numerical evaluation of the PHS numerical method.
        """
        from pyphs import Simulation
        return Simulation(self, inits=inits, config=config)
예제 #9
0
def dataH5File():
    # Define the simulation parameters
    config = {
        'fs': 48e3,  # Sample rate
        'grad': 'discret',  # in {'discret', 'theta', 'trapez'}
        'theta': 0.5,  # theta-scheme for the structure
        'split': True,  # apply core.split_linear() beforehand
        'maxit': 10,  # Max iteration for NL solvers
        'path': path,  # Path to the results folder
        'pbar': False,  # Display a progress bar
        'timer': True,  # Display minimal timing infos
        'lang': 'python',  # in {'python', 'c++'}
    }

    # retrieve the pyphs.Core of a nonlinear RLC from
    # the tutorial on Core
    from pyphs.tutorials.core import core as nlcore
    nlcore.reduce_z()
    simu = Simulation(nlcore.to_method(), config=config)

    dur = 0.01
    u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs'])

    sequ = u[:, np.newaxis]
    simu.init(u=sequ)
    simu.process()

    simu.data.plot_powerbal(mode='single', show=False)
    simu.data.plot_powerbal(mode='multi', show=False)

    simu2 = Simulation(nlcore.to_method(), config=config, erase=False)

    start = int(simu.data.nt / 10.)
    stop = int(9 * simu.data.nt / 10.)
    step = 3

    simu2.data.start = start
    simu2.data.stop = stop
    simu2.data.step = step

    test = len(range(start, stop, step)) == len(simu2.data['x', :, 0])

    return test
예제 #10
0
def plot_power_balance_nlcore_with_split():
    # Define the simulation parameters
    config = {
        'fs': 48e3,  # Sample rate
        'grad': 'discret',  # in {'discret', 'theta', 'trapez'}
        'theta': 0.5,  # theta-scheme for the structure
        'split': True,  # apply core.split_linear() beforehand
        'maxit': 10,  # Max iteration for NL solvers
        'eps': 1e-16,  # Global numerical tolerance
        'path': path,  # Path to the results folder
        'pbar': False,  # Display a progress bar
        'timer': True,  # Display minimal timing infos
        'lang': 'python',  # in {'python', 'c++'}
        'eigen': None,  # path to Eigen library
    }

    # retrieve the pyphs.Core of a nonlinear RLC from
    # the tutorial on Core
    from pyphs.tutorials.core import core as nlcore
    nlcore.reduce_z()
    simu = Simulation(nlcore, config=config)

    dur = 0.01
    u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs'])

    def sequ():
        for el in u():
            yield (el, )

    simu.init(u=sequ(), nt=int(dur * simu.config['fs']))
    simu.process()

    simu.data.plot_powerbal(mode='single', show=False)
    simu.data.plot_powerbal(mode='multi', show=False)

    if not os.name.lower().startswith('nt'):
        shutil.rmtree(path)
    return True
예제 #11
0
netlist = Netlist(netlist_filename)
graph = Graph(netlist=netlist)
core = graph.to_core()

# %% ------------------------------ SIMULATION ------------------------------ #

# UNCOMMENT BELOW FOR SIMULATION AND PLOTS

if __name__ == '__main__':

    from pyphs import Netlist, Graph, Simulation, signalgenerator
    import numpy as np


    tsig = 0.01

    fs = 48000.

    config = {'fs': 48e3,
              'pbar': True,
              }
    simu = Simulation(core.to_method(), config)

    u = signalgenerator(which='sin', f0=500., A=200., tsig=tsig, fs=fs)

    simu.init(u=u[:, np.newaxis])

    simu.process()

    simu.data.plot_powerbal()
예제 #12
0
파일: simulation.py 프로젝트: yomguy/pyphs
    # to index imax, rendering one element out of the number decim
    'load': {
        'imin': 0,
        'imax': None,
        'decim': 1
    }
}

# state initialization
# !!! must be numpy array with shape (core.dims.x(), )
x0 = list(map(sympy.sympify, [
    0.,
] * core.dims.x()))

# Instantiate a pyphs.Simulation object associated with a given core
simu = Simulation(core.to_method(), config=config, inits={'x': x0})

# def simulation time
tmax = 1e-2
nmax = int(tmax * simu.config['fs'])
t = [n / simu.config['fs'] for n in range(nmax)]
nt = len(t)


# def input signal
def sig(tn, mode='sin'):
    freq = 300.
    amp = 1.
    if mode == 'sin':
        pi = numpy.pi
        sin = numpy.sin
예제 #13
0
        'split': False,  # apply core.split_linear() beforehand
        'maxit': 10,  # Max iteration for NL solvers
        'eps': 1e-30,  # Global numerical tolerance
        'path': None,  # Path to the results folder
        'pbar': False,  # Display a progress bar
        'timer': False,  # Display minimal timing infos
        'lang': 'python',  # in {'python', 'c++'}
        'theano': False
    }

    #    x0 = np.zeros(core.dims.x())
    #    x0[0] = 1
    #    x0_expr = list(map(sp.sympify, x0))
    #    inits = {'x': x0_expr}
    #    simu = Simulation(core, config=config, inits=inits)
    simu = Simulation(core, config=config)

    dur = 0.01
    u = signalgenerator(which='sin', f0=800., tsig=dur, fs=simu.config['fs'])

    def sequ():
        for el in u():
            yield (el, )

    simu.init(u=sequ(), nt=int(dur * simu.config['fs']))

    # Run the simulation
    simu.process()

    # Plots
    simu.data.plot_powerbal(mode='single')
예제 #14
0
def simulation_nlcore_full():

    # Define the simulation parameters
    config = {'fs': 48e3,               # Sample rate
              'grad': 'discret',    # in {'discret', 'theta', 'trapez'}
              'theta': 0.5,             # theta-scheme for the structure
              'split': False,           # apply core.split_linear() beforehand
              'maxit': 10,              # Max iteration for NL solvers
              'eps': 1e-16,          # Global numerical tolerance
              'path': path,             # Path to the results folder
              'pbar': False,      # Display a progress bar
              'timer': True,            # Display minimal timing infos
              'lang': 'python',     # in {'python', 'c++'}
              }


    # state initialization
    # !!! must be array with shape (core.dims.x(), )
    x0 = list(map(sympy.sympify, (0., 0., 0.)))

    # Instantiate a pyphs.Simulation object associated with a given core
    simu = Simulation(nlcore.to_method(), config=config, inits={'x': x0})

    # def simulation time
    tmax = 0.02
    nmax = int(tmax*simu.config['fs'])
    t = [n/simu.config['fs'] for n in range(nmax)]
    nt = len(t)

    # def input signal
    def sig(tn, mode='impact'):
        freq = 1000.
        amp = 1000.
        if mode == 'sin':
            pi = numpy.pi
            sin = numpy.sin
            out = amp * sin(2*pi*freq*tn)
        elif mode == 'impact':
            dur = 0.5*1e-3  # duration: 0.5ms
            start = 0.001   # start at 1ms
            out = amp if start <= tn < start + dur else 0.
        elif mode == 'const':
            out = 1.
        return out

    # def generator for sequence of inputs to feed in the Simulation object
    def sequ():
        """
        generator of input sequence for Simulation
        """
        for tn in t:
            u1 = sig(tn)

            # !!! must be array with shape (core.dims.u(), )
            yield numpy.array([u1, ])  # numpy.array([u1, u2, ...])

    # Initialize the simulation
    simu.init(u=sequ(), nt=nt)

    # Proceed
    simu.process()

    if not os.name.lower().startswith('nt'):
        shutil.rmtree(path)
    return True