コード例 #1
0
ファイル: SimulationPlotsTest.py プロジェクト: pyphs/pyphs
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
コード例 #2
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
コード例 #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++'}
        '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
コード例 #4
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
コード例 #5
0
ファイル: SimulationPlotsTest.py プロジェクト: pyphs/pyphs
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
コード例 #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++'}
        '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
コード例 #7
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
コード例 #8
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()
コード例 #9
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