Beispiel #1
0
def cpp_nlcore_full():
    label = 'test'
    path = os.path.join(here, label)

    method2cpp(nlcore.to_method(), objlabel=label, path=path)
    if not os.name.lower().startswith('nt'):
        shutil.rmtree(path)
    return True
Beispiel #2
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
Beispiel #3
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
Beispiel #4
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