示例#1
0
def add_cswrapper2(filename, outfilename=None):

    from fmpy.fmucontainer import Variable, Configuration, Component, create_fmu_container

    model_description = read_model_description(filename)

    model_identifier = model_description.modelExchange.modelIdentifier

    variables = []

    for variable in model_description.modelVariables:
        variables.append(
            Variable(type=variable.type,
                     variability=variable.variability,
                     causality=variable.causality,
                     initial=variable.initial,
                     name=variable.name,
                     start=variable.start,
                     description=variable.description,
                     mapping=[(model_identifier, variable.name)]))

    configuration = Configuration(variables=variables,
                                  components=[
                                      Component(filename=filename,
                                                interfaceType='ModelExchange',
                                                name=model_identifier)
                                  ])

    create_fmu_container(configuration, outfilename)
示例#2
0
def test_create_fmu_container_me(resources_dir):

    configuration = Configuration(
        parallelDoStep=False,
        variables=[
            Variable(type='Real',
                     variability='continuous',
                     causality='output',
                     initial='calculated',
                     name='h',
                     description='Height',
                     mapping=[('ball', 'h')]),
            Variable(type='Boolean',
                     variability='discrete',
                     causality='output',
                     initial='calculated',
                     name='reset',
                     description="Reset",
                     mapping=[('bounce', 'reset')]),
            Variable(type='Real',
                     variability='discrete',
                     causality='output',
                     initial='calculated',
                     name='ticks',
                     description='Ticks',
                     mapping=[('ticker', 'ticks')]),
        ],
        components=[
            Component(filename=resources_dir / 'Bounce.fmu',
                      interfaceType='ModelExchange',
                      name='bounce'),
            Component(filename=resources_dir / 'Ball.fmu',
                      interfaceType='ModelExchange',
                      name='ball'),
            Component(filename=resources_dir / 'Ticker.fmu',
                      interfaceType='ModelExchange',
                      name='ticker')
        ],
        connections=[
            Connection('ball', 'h', 'bounce', 'h'),
            Connection('bounce', 'reset', 'ball', 'reset'),
        ])

    filename = 'BouncingAndBall.fmu'

    create_fmu_container(configuration, filename)

    problems = validate_fmu(filename)

    assert not problems

    result = simulate_fmu(filename, stop_time=3.5, fmi_call_logger=None)
示例#3
0
    def test_create_fmu_container(self):

        resources = os.path.join(os.path.dirname(__file__), 'resources')

        configuration = Configuration(
            parallelDoStep=True,
            description="A controlled drivetrain",
            variableNamingConvention='structured',
            unitDefinitions=[
                Unit(name='rad/s',
                     baseUnit=BaseUnit(rad=1, s=-1),
                     displayUnits=[
                         DisplayUnit(name='rpm', factor=0.1047197551196598)
                     ])
            ],
            typeDefinitions=[
                SimpleType(name='AngularVelocity', type='Real', unit='rad/s')
            ],
            variables=[
                Variable(type='Real',
                         variability='tunable',
                         causality='parameter',
                         name='k',
                         start='100',
                         description='Gain of controller',
                         mapping=[('controller', 'PI.k')]),
                Variable(type='Real',
                         variability='continuous',
                         causality='input',
                         name='w_ref',
                         start='0',
                         description='Reference speed',
                         mapping=[('controller', 'u_s')],
                         declaredType='AngularVelocity'),
                Variable(type='Real',
                         variability='continuous',
                         causality='output',
                         name='w',
                         description="Gain of controller",
                         mapping=[('drivetrain', 'w')],
                         unit='rad/s',
                         displayUnit='rpm'),
            ],
            components=[
                Component(filename=os.path.join(resources, 'Controller.fmu'),
                          name='controller'),
                Component(
                    filename=os.path.join(resources, 'Drivetrain.fmu'),
                    name='drivetrain',
                )
            ],
            connections=[
                Connection('drivetrain', 'w', 'controller', 'u_m'),
                Connection('controller', 'y', 'drivetrain', 'tau'),
            ])

        filename = 'ControlledDrivetrain.fmu'

        create_fmu_container(configuration, filename)

        problems = validate_fmu(filename)

        self.assertEqual(problems, [])

        w_ref = np.array([(0.5, 0), (1.5, 1), (2, 1), (3, 0)],
                         dtype=[('time', 'f8'), ('w_ref', 'f8')])

        result = simulate_fmu(filename,
                              start_values={'k': 20},
                              input=w_ref,
                              output=['w_ref', 'w'],
                              stop_time=4)
示例#4
0
def test_create_fmu_container_cs(resources_dir):

    configuration = Configuration(
        parallelDoStep=True,
        description="A controlled drivetrain",
        variableNamingConvention='structured',
        unitDefinitions=[
            Unit(name='rad/s',
                 baseUnit=BaseUnit(rad=1, s=-1),
                 displayUnits=[
                     DisplayUnit(name='rpm', factor=0.1047197551196598)
                 ])
        ],
        typeDefinitions=[
            SimpleType(name='AngularVelocity', type='Real', unit='rad/s')
        ],
        variables=[
            Variable(type='Real',
                     variability='tunable',
                     causality='parameter',
                     initial='exact',
                     name='k',
                     start='40',
                     description='Gain of controller',
                     mapping=[('controller', 'PI.k')]),
            Variable(type='Real',
                     variability='continuous',
                     causality='input',
                     name='w_ref',
                     start='0',
                     description='Reference speed',
                     mapping=[('controller', 'u_s')],
                     declaredType='AngularVelocity'),
            Variable(type='Real',
                     variability='continuous',
                     causality='output',
                     initial='calculated',
                     name='w',
                     description="Gain of controller",
                     mapping=[('drivetrain', 'w')],
                     unit='rad/s',
                     displayUnit='rpm'),
        ],
        components=[
            Component(filename=resources_dir / 'Controller.fmu',
                      interfaceType='CoSimulation',
                      name='controller'),
            Component(
                filename=resources_dir / 'Drivetrain.fmu',
                interfaceType='CoSimulation',
                name='drivetrain',
            )
        ],
        connections=[
            Connection('drivetrain', 'w', 'controller', 'u_m'),
            Connection('controller', 'y', 'drivetrain', 'tau'),
        ])

    filename = 'ControlledDrivetrain.fmu'

    create_fmu_container(configuration, filename)

    problems = validate_fmu(filename)

    assert not problems

    input = read_csv(resources_dir / 'ControlledDrivetrain_in.csv')

    result = simulate_fmu(filename,
                          input=input,
                          output=['w_ref', 'w', 'k'],
                          stop_time=5,
                          output_interval=5e-2)

    t_band, y_min, y_max, i_out = validate_signal(t=result['time'],
                                                  y=result['w'],
                                                  t_ref=input['time'],
                                                  y_ref=input['w_ref'],
                                                  dx=100,
                                                  dy=0.4)

    assert result['k'][0] == 40, 'Default start value has not been set.'

    assert not i_out.any()
示例#5
0
    def test_create_fmu_container(self):

        examples = os.path.join(os.environ['SSP_STANDARD_DEV'],
                                'SystemStructureDescription', 'examples')

        configuration = {

            # description of the container
            'description':
            'A controlled drivetrain',

            # optional dictionary to customize attributes of exposed variables
            'variables': {
                'controller.PI.k': {
                    'name': 'k'
                },
                'controller.u_s': {
                    'name': 'w_ref',
                    'description': 'Reference speed'
                },
                'drivetrain.w': {
                    'name': 'w',
                    'description': 'Motor speed'
                },
            },

            # models to include in the container
            'components': [
                {
                    'filename':
                    os.path.join(examples,
                                 'Controller.fmu'),  # filename of the FMU
                    'name': 'controller',  # instance name
                    'variables':
                    ['u_s', 'PI.k']  # variables to expose in the container
                },
                {
                    'filename': os.path.join(examples, 'Drivetrain.fmu'),
                    'name': 'drivetrain',
                    'variables': ['w']
                }
            ],

            # connections between the FMU instances
            'connections': [
                # <from_instance>, <from_variable>, <to_instance>, <to_variable>
                ('drivetrain', 'w', 'controller', 'u_m'),
                ('controller', 'y', 'drivetrain', 'tau'),
            ]
        }

        filename = 'ControlledDrivetrain.fmu'

        create_fmu_container(configuration, filename)

        w_ref = np.array([(0.5, 0), (1.5, 1), (2, 1), (3, 0)],
                         dtype=[('time', 'f8'), ('w_ref', 'f8')])

        result = simulate_fmu(filename,
                              start_values={'k': 20},
                              input=w_ref,
                              output=['w_ref', 'w'],
                              stop_time=4)