Пример #1
0
def simple_execution(end_time=Fraction(100), fig_file=None):
    """The demo function"""
    plt.clf()
    non_default = dict()
    csnet = cs_network(generate_parameters(non_default))
    slaves, connections = csnet
    step_sizes = {name: Fraction(1, 4) for name in slaves}
    make_zoh: cs.ConverterConstructor = cs.Zoh
    rate_converters = {
        cs.Connection(src, dst): make_zoh
        for dst, src in connections.items()
    }
    initial_tokens = autoconfig.find_initial_tokens(csnet, step_sizes,
                                                    rate_converters)
    cosimulation = csnet, step_sizes, rate_converters, initial_tokens
    results = cs.execute(cosimulation, end_time)
    print(f"v_right = {results.tokens['middle_oscillator', 'v_right'][-1]}")
    print(f"v_left = {results.tokens['middle_oscillator', 'v_left'][-1]}")
    print(f"F_right = {results.tokens['left_oscillator', 'F'][-1]}")
    print(f"F_left = {results.tokens['right_oscillator', 'F'][-1]}")
    ts, vals = cs.get_signal_samples(cosimulation, results,
                                     'middle_oscillator', 'F_right')
    # ts, vals = cs.get_signal_samples(cosimulation, results, 'right_oscillator', 'v')
    plt.stem(ts, vals)
    show_figure(plt.gcf(), fig_file)
Пример #2
0
def _plot_error_lines(cosimulation, results, y2, ax2):
    ts, vals = cs.get_signal_samples(cosimulation, results, 'PT2', 'y')
    for t, val in zip(ts, vals):
        ax2.plot([t, t], [val, y2(t)], 'r-')
    t, val = ts[-1], vals[-1]
    ax2.plot([t, t], [val, y2(t)],
             'r-',
             label=r'$|\gamma_{21}[k_2]- y_{21}(k_2h_2)|$')
Пример #3
0
def automatic_configuration(
        end_time=Fraction(20), tolerance=0.1, fig_file=None):
    """The demo function"""
    plt.clf()
    non_default = dict()
    csnet = cs_network(generate_parameters(non_default))
    cosimulation, results = _auto_results_only(csnet, end_time, tolerance)
    ts, vals = cs.get_signal_samples(cosimulation, results, 'right_oscillator',
                                     'v')
    plt.stem(ts, vals)
    show_figure(plt.gcf(), fig_file)
Пример #4
0
def plot_cs_output(cosimulation, results, axs):
    """Plots the output of the control co-simulation"""
    signals = [('PI', 'y'), ('PT2', 'y')]
    for signal, ax, output in zip(signals, axs, range(1, 3)):
        label_str = r'$\gamma_{' + str(output) + '1}[k_' + str(output) + ']$'
        instance, port = signal
        ts, vals = cs.get_signal_samples(cosimulation, results, instance, port)
        ax.stem(ts,
                vals,
                label=label_str,
                markerfmt='ks',
                basefmt='C7--',
                linefmt='C7--')  # , use_line_collection=True)
Пример #5
0
def get_cs_responses(parameters: EngineExperiment,
                     h=Fraction(1, 10),
                     end_time=Fraction(10, 1)):
    """Three co-simulations and the analytic response"""

    cosimulations = {
        'GS12': gauss_seidel(parameters, h, False),
        'GS21': gauss_seidel(parameters, h, True),
        'GJ': gauss_jacobi(parameters, h)
    }

    signals = [('engine', 'tau'), ('engine', 'omega'), ('inertia', 'tau'),
               ('inertia', 'omega')]
    responses = dict()

    for title, cosimulation in cosimulations.items():
        results = cs.execute(cosimulation, end_time)
        for simulator, port in signals:
            responses[title, simulator,
                      port] = cs.get_signal_samples(cosimulation, results,
                                                    simulator, port)

    return responses
Пример #6
0
def print_error_measurement(K=1., T1=5., Ts=1., end_time=20):
    """Runs the example"""

    experiments = [
        (gauss_seidel(K, T1, Ts), 'Gauss-Seidel 21 [y11(0)]', 0.0740),
        (gauss_seidel(K, T1, Ts,
                      inverse=True), 'Gauss-Seidel 12 [y21(0)]', 0.0890),
        (gauss_seidel(K, T1, Ts,
                      init=True), 'Gauss-Seidel 21 [y11(h)]', 0.0726),
        (gauss_jacobi(K, T1, Ts), 'Gauss-Jacobi', 0.0928),
        (multi_rate(K, T1, Ts), 'Multi-rate 211', 0.0352),
    ]

    _, y2 = analytic_solution(K, T1, Ts)

    print('Mean absolute error of')
    for cosimulation, lbl, expected_mae in experiments:
        results = cs.execute(cosimulation, end_time)
        ts, vals = cs.get_signal_samples(cosimulation, results, 'PT2', 'y')
        y2s = np.array(y2(ts))
        mean_abs_err = np.sum(np.abs(y2s - vals)) / len(vals)
        assert abs(mean_abs_err - expected_mae
                   ) < 1e-3, "Backwards compatibility seems to be broken"
        print(f' - {lbl} is equal to {mean_abs_err:.4f}')
Пример #7
0
def _important_signals(cosimulation, end_time, samples, num):
    results = cs.execute(cosimulation, end_time)
    samples[f'tf{num}'], samples[f'vf{num}'] = cs.get_signal_samples(
        cosimulation, results, 'middle_oscillator', 'F_right')
    samples[f'tv{num}'], samples[f'vv{num}'] = cs.get_signal_samples(
        cosimulation, results, 'right_oscillator', 'v')
Пример #8
0
def _get_comparison_signals(cosimulation, results, samples, xil):
    """A helper function"""
    tpis, vpis = cs.get_signal_samples(cosimulation, results, 'PI', 'y')
    tpt2s, vpt2s = cs.get_signal_samples(cosimulation, results, 'PT2', 'y')
    samples[xil] = tpis, vpis, tpt2s, vpt2s