def gauss_jacobi(K, T1, Ts, h=Fraction(1, 2)) -> cs.Cosimulation: """The SDF representation of a co-simulation master""" y1, y2 = analytic_solution(K, T1, Ts) step_sizes = {'PI': h, 'PT2': h} tokens = { sdf.Dst('PT2', 'u'): [y1(0.5)], sdf.Dst('PI', 'u'): [y2(0.5)], } return cs_network(K, T1, Ts), step_sizes, rate_converters(), tokens
def gauss_jacobi( parameters: EngineExperiment, h=Fraction(1, 2)) -> cs.Cosimulation: """The SDF representation of a co-simulation master""" step_sizes: cs.StepSizes = {'engine': h, 'inertia': h, 'alpha': h} tokens: cs.InitialTokens = { sdf.Dst('engine', 'omega'): [0.], sdf.Dst('engine', 'alpha'): [0.], sdf.Dst('inertia', 'tau'): [0.], } return cs_network(parameters), step_sizes, rate_converters(), tokens
def gauss_seidel(K, T1, Ts, inverse=False, init=False) -> cs.Cosimulation: """The SDF representation of a co-simulation master""" h = Fraction(3, 4) y1, y2 = analytic_solution(K, T1, Ts) step_sizes = {'PI': h, 'PT2': h} tokens = { sdf.Dst('PT2', 'u'): [] if inverse else [y1(h if init else 0)], sdf.Dst('PI', 'u'): [y2(h if init else 0)] if inverse else [], } return cs_network(K, T1, Ts), step_sizes, rate_converters(), tokens
def multi_rate(K, T1, Ts) -> cs.Cosimulation: """The SDF representation of a co-simulation master""" y1, _ = analytic_solution(K, T1, Ts) hpi, hpt2 = Fraction(3, 4), Fraction(3, 8) step_sizes = {'PI': hpi, 'PT2': hpt2} tokens = { sdf.Dst('PT2', 'u'): [y1(0), y1(hpt2)], sdf.Dst('PI', 'u'): [], } return cs_network(K, T1, Ts), step_sizes, rate_converters(), tokens
def get_two_unit_graph(tokens12=13): """ Two units in a loop """ agents = {'one': sdf.Gain(1), 'two': sdf.Gain(1)} buffers = [ sdf.Buffer(src=sdf.Src('one', 'y'), dst=sdf.Dst('two', 'u'), tokens=deque([tokens12])), sdf.Buffer(src=sdf.Src('two', 'y'), dst=sdf.Dst('one', 'u'), tokens=deque()) ] return (agents, buffers)
def test_defect_calculation_control(): """Tests whether defect calculation give sane results""" csnet = example.control.cs_network() slaves, connections = csnet step_sizes = {name: Fraction(1, 2) 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 = {sdf.Dst('PI', 'u'): [0.], sdf.Dst('PT2', 'u'): [0.]} cosim = csnet, step_sizes, rate_converters, initial_tokens defect = cs.evaluate(cosim, Fraction(20.)) for val in defect.connection.values(): assert val < float('inf') for val in defect.output.values(): assert val < float('inf')
def test_invalid_connection1(): """ check for invalid tokens """ sdf_graph = get_two_unit_graph() agents, buffers = sdf_graph buffers.append( sdf.Buffer(src=sdf.Src('nonexisting_actor', 'nonexisting_port'), dst=sdf.Dst('two', 'u'), tokens=deque([4]))) assert not sdf.validate_graph(sdf_graph)
def null_jacobi_initial_tokens( connections: cs.Connections, step_sizes: cs.StepSizes ) -> cs.InitialTokens: """The initial tokens for fully parallel execution""" rpv = cs.repetition_vector(connections, step_sizes) return { sdf.Dst(dst.slave, dst.port): [0.] * rpv[dst.slave] for dst in connections.keys() }
def _next_tokens(connections, step_sizes, results) -> cs.InitialTokens: """Calculates the next iteration of the tokens""" rpv = cs.repetition_vector(connections, step_sizes) dsts = ( (src.slave, sdf.Dst('_'.join([src.slave, src.port, dst.slave, dst.port]), 'u'), sdf.Dst(dst.slave, dst.port)) for dst, src in connections.items() ) def resample_tokens(buffer, num_src, num_dst): return [buffer[(i * num_src) // num_dst] for i in range(num_dst)] next_tokens = { dst: resample_tokens(results.tokens[src], rpv[src_slave], rpv[dst.agent]) for src_slave, src, dst in dsts } return next_tokens
def test_invalid_connection2(): """Actors do not match the connections!""" sdf_graph = get_two_unit_graph() actors, buffers = sdf_graph buffers[0] = sdf.Buffer(src=sdf.Src('nonexisting_actor1', 'nonexisting_port1'), dst=sdf.Dst('nonexisting_actor2', 'nonexisting_port2'), tokens=buffers[0].tokens) assert not sdf.validate_graph(sdf_graph)
def ramp_cosimulation(slope1=2., slope2=3., step1=Fraction(5), step2=Fraction(7)): """Used for testing the defect calculation""" csnet = _semiconnected_ramps(slope1, slope2) _, connections = csnet step_sizes = {'Ramp1': step1, 'Ramp2': step2} make_zoh: cs.ConverterConstructor = cs.Zoh rate_converters = { cs.Connection(src, dst): make_zoh for dst, src in connections.items() } alpha = Fraction(int(lcm(step1.numerator, step2.numerator)), int(gcd(step1.denominator, step2.denominator))) num1, num2 = tuple(map(int, [alpha / step for step in (step1, step2)])) initial_tokens = { sdf.Dst('Ramp1', 'u'): [(i - num1 + 1) * step2 * slope2 for i in range(num1)], sdf.Dst('Ramp2', 'u'): [(i - num2 + 1) * step1 * slope1 for i in range(num2)] } return csnet, step_sizes, rate_converters, initial_tokens