Ejemplo n.º 1
0
    def validate(self, build_dir, fmi_types=['ModelExchange', 'CoSimulation'], models=models, compile=False):

        from fmpy.util import read_csv, validate_result

        for model in models:

            print(model)

            fmu_filename = os.path.join(build_dir, 'dist', model + '.fmu')

            if model == 'Feedthrough':
                start_values = {'real_fixed_param': 1, 'string_param': "FMI is awesome!"}
                in_csv = os.path.join(test_fmus_dir, model, model + '_in.csv')
                input = read_csv(in_csv)
            else:
                start_values = {}
                input = None

            ref_csv = os.path.join(test_fmus_dir, model, model + '_ref.csv')

            for fmi_type in fmi_types:

                ref = read_csv(ref_csv)

                if compile:
                    compile_platform_binary(fmu_filename)

                result = simulate_fmu(fmu_filename,
                                      fmi_type=fmi_type,
                                      start_values=start_values,
                                      input=input)

                dev = validate_result(result, ref)

                self.assertLess(dev, 0.2, "Failed to validate " + model)
Ejemplo n.º 2
0
    def test_compile(self):
        """ Compile the platform binary """

        for fmu in self.fmus:

            filename = download_file(self.url + fmu)

            compile_platform_binary(filename)

            result = simulate_fmu(filename=filename)
            self.assertIsNotNone(result)
Ejemplo n.º 3
0
    def test_compile(self):
        """ Compile directly """

        for fmu in self.fmus:
            download_file(self.url + fmu)

            filename = os.path.basename(fmu)

            # compile in-place
            result = simulate_fmu(filename=filename, use_source_code=True)
            self.assertIsNotNone(result)

            # add binary to FMU
            compile_platform_binary(filename)

            result = simulate_fmu(filename=filename)
            self.assertIsNotNone(result)
Ejemplo n.º 4
0
    def test_compile(self):
        """ Compile the platform binary """

        # add debug info
        if os.name == 'nt':
            compiler_options = '/LDd /Zi'
        else:
            compiler_options = '-g -fPIC'

        for fmu in self.fmus:

            filename = download_file(self.url + fmu)

            compile_platform_binary(filename, compiler_options=compiler_options)

            result = simulate_fmu(filename=filename)
            self.assertIsNotNone(result)
Ejemplo n.º 5
0
def validate(build_dir, fmi_types, models, compile=False):

    from fmpy.util import read_csv, validate_result

    test_fmus_dir = Path(__file__).parent

    for model in models:

        print(model)

        fmu_filename = os.path.join(build_dir, 'dist', model + '.fmu')

        problems = validate_fmu(fmu_filename)

        assert not problems

        if model == 'Feedthrough':
            start_values = {'Float64_fixed_parameter': 1, 'String_parameter': "FMI is awesome!"}
            in_csv = os.path.join(test_fmus_dir, model, model + '_in.csv')
            input = read_csv(in_csv)
        else:
            start_values = {}
            input = None

        ref_csv = os.path.join(test_fmus_dir, model, model + '_ref.csv')

        for fmi_type in fmi_types:

            ref = read_csv(ref_csv)

            if compile:
                if model == 'Resource' and os.name == 'nt':
                    continue
                compile_platform_binary(fmu_filename)

            result = simulate_fmu(fmu_filename,
                                  fmi_type=fmi_type,
                                  start_values=start_values,
                                  input=input,
                                  solver='Euler')

            dev = validate_result(result, ref)

            assert dev < 0.2, "Failed to validate " + model
Ejemplo n.º 6
0
    def test_compile(self):

        fmus = [
            'c-code/dSPACE_TargetLink/Release_2016-B/poscontrol/FmuController.fmu',
            'c-code/MapleSim/2016.2/Rectifier/Rectifier.fmu',
            'c-code/Dymola/2017/IntegerNetwork1/IntegerNetwork1.fmu',
         ]

        for fmu in fmus:
            download_file(self.url + fmu)

            filename = os.path.basename(fmu)

            # compile in-place
            result = simulate_fmu(filename=filename, use_source_code=True)
            self.assertIsNotNone(result)

            # add binary to FMU
            compile_platform_binary(filename)

            result = simulate_fmu(filename=filename)
            self.assertIsNotNone(result)
Ejemplo n.º 7
0
def main():

    import argparse
    import textwrap

    description = """\
    Validate and simulate Functional Mock-up Units (FMUs)

    Get information about an FMU:
       
        fmpy info Rectifier.fmu
     
    Simulate an FMU:
     
        fmpy simulate Rectifier.fmu --show-plot
        
    Compile a source code FMU:
    
        fmpy compile Rectifier.fmu
        
    Create a Jupyter Notebook
    
        fmpy create-jupyter-notebook Rectifier.fmu
    """

    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=textwrap.dedent(description))

    parser.add_argument('command',
                        choices=[
                            'info', 'validate', 'simulate', 'compile',
                            'add-cswrapper', 'add-remoting',
                            'create-cmake-project', 'create-jupyter-notebook'
                        ],
                        help="Command to execute")
    parser.add_argument('fmu_filename', help="filename of the FMU")

    parser.add_argument('--validate',
                        action='store_true',
                        help="validate the FMU")
    parser.add_argument('--start-time',
                        type=float,
                        help="start time for the simulation")
    parser.add_argument('--stop-time',
                        type=float,
                        help="stop time for the simulation")
    parser.add_argument('--solver',
                        choices=['Euler', 'CVode'],
                        default='CVode',
                        help="solver to use for Model Exchange")
    parser.add_argument('--step-size',
                        type=float,
                        help="step size for fixed-step solvers")
    parser.add_argument(
        '--relative-tolerance',
        type=float,
        help=
        "relative tolerance for the 'CVode' solver and FMI 2.0 co-simulation FMUs"
    )
    parser.add_argument(
        '--dont-record-events',
        action='store_true',
        help="dont't record outputs at events (model exchange only)")
    parser.add_argument('--start-values',
                        nargs='+',
                        help="name-value pairs of start values")
    parser.add_argument(
        '--apply-default-start-values',
        action='store_true',
        help="apply the start values from the model description")
    parser.add_argument('--output-interval',
                        type=float,
                        help="interval for sampling the output")
    parser.add_argument('--input-file', help="CSV file to use as input")
    parser.add_argument('--output-variables',
                        nargs='+',
                        help="Variables to record")
    parser.add_argument('--output-file', help="CSV to store the results")
    parser.add_argument('--timeout',
                        type=float,
                        help="max. time to wait for the simulation to finish")
    parser.add_argument('--debug-logging',
                        action='store_true',
                        help="enable the FMU's debug logging")
    parser.add_argument('--visible',
                        action='store_true',
                        help="enable interactive mode")
    parser.add_argument('--fmi-logging',
                        action='store_true',
                        help="enable FMI logging")
    parser.add_argument('--show-plot',
                        action='store_true',
                        help="plot the results")
    parser.add_argument('--cmake-project-dir',
                        help="Directory for the CMake project")
    parser.add_argument('--target-platform',
                        help="The target platform to compile the binary for")

    args = parser.parse_args()

    if args.command == 'info':

        from fmpy import dump
        dump(args.fmu_filename)

    elif args.command == 'validate':

        import sys
        from fmpy.validation import validate_fmu

        problems = validate_fmu(args.fmu_filename)

        if len(problems) == 0:
            print('No problems found.')
        else:
            print('%d problems were found:' % len(problems))
            for message in problems:
                print()
                print(message)

        sys.exit(len(problems))

    elif args.command == 'compile':

        from fmpy.util import compile_platform_binary
        compile_platform_binary(args.fmu_filename,
                                target_platform=args.target_platform)

    elif args.command == 'add-cswrapper':

        from fmpy.cswrapper import add_cswrapper
        add_cswrapper(args.fmu_filename)

    elif args.command == 'add-remoting':

        from fmpy.util import add_remoting
        from fmpy import supported_platforms

        platforms = supported_platforms(args.fmu_filename)

        if 'win32' in platforms and 'win64' not in platforms:
            add_remoting(args.fmu_filename, 'win64', 'win32')
        elif 'win64' in platforms and 'linux64' not in platforms:
            add_remoting(args.fmu_filename, 'linux64', 'win64')
        else:
            print("Failed to add remoting binaries.")

    elif args.command == 'create-cmake-project':

        import os
        from fmpy.util import create_cmake_project

        project_dir = args.cmake_project_dir

        if project_dir is None:
            project_dir = os.path.basename(args.fmu_filename)
            project_dir, _ = os.path.splitext(project_dir)
            print("Creating CMake project in %s" %
                  os.path.abspath(project_dir))

        create_cmake_project(args.fmu_filename, project_dir)

    elif args.command == 'create-jupyter-notebook':

        from fmpy.util import create_jupyter_notebook

        create_jupyter_notebook(args.fmu_filename)

    elif args.command == 'simulate':

        from fmpy import simulate_fmu
        from fmpy.util import read_csv, write_csv, plot_result

        if args.start_values:
            if len(args.start_values) % 2 != 0:
                raise Exception("Start values must be name-value pairs.")
            start_values = {
                k: v
                for k, v in zip(args.start_values[::2],
                                args.start_values[1::2])
            }
        else:
            start_values = {}

        input = read_csv(args.input_file) if args.input_file else None

        if args.fmi_logging:
            fmi_call_logger = lambda s: print('[FMI] ' + s)
        else:
            fmi_call_logger = None

        result = simulate_fmu(
            args.fmu_filename,
            validate=args.validate,
            start_time=args.start_time,
            stop_time=args.stop_time,
            solver=args.solver,
            step_size=args.step_size,
            relative_tolerance=args.relative_tolerance,
            output_interval=args.output_interval,
            record_events=not args.dont_record_events,
            fmi_type=None,
            start_values=start_values,
            apply_default_start_values=args.apply_default_start_values,
            input=input,
            output=args.output_variables,
            timeout=args.timeout,
            debug_logging=args.debug_logging,
            visible=args.visible,
            fmi_call_logger=fmi_call_logger)

        if args.output_file:
            write_csv(filename=args.output_file, result=result)

        if args.show_plot:
            plot_result(result=result, window_title=args.fmu_filename)
Ejemplo n.º 8
0
def main():

    import argparse
    import textwrap

    description = """\
    Validate and simulate Functional Mock-up Units (FMUs)

    Get information about an FMU:
       
        fmpy info Rectifier.fmu
     
    Simulate an FMU:
     
        fmpy simulate Rectifier.fmu --show-plot
        
    Compile a source code FMU:
    
        fmpy compile Rectifier.fmu
    """

    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=textwrap.dedent(description))

    parser.add_argument(
        'command',
        choices=['info', 'validate', 'simulate', 'compile', 'add-remoting'],
        help="Command to execute")
    parser.add_argument('fmu_filename', help="filename of the FMU")

    parser.add_argument('--validate',
                        action='store_true',
                        help="validate the FMU")
    parser.add_argument('--start-time',
                        type=float,
                        help="start time for the simulation")
    parser.add_argument('--stop-time',
                        type=float,
                        help="stop time for the simulation")
    parser.add_argument('--solver',
                        choices=['Euler', 'CVode'],
                        default='CVode',
                        help="solver to use for Model Exchange")
    parser.add_argument('--step-size',
                        type=float,
                        help="step size for fixed-step solvers")
    parser.add_argument(
        '--relative-tolerance',
        type=float,
        help=
        "relative tolerance for the 'CVode' solver and FMI 2.0 co-simulation FMUs"
    )
    parser.add_argument(
        '--dont-record-events',
        action='store_true',
        help="dont't record outputs at events (model exchange only)")
    parser.add_argument('--start-values',
                        nargs='+',
                        help="name-value pairs of start values")
    parser.add_argument(
        '--apply-default-start-values',
        action='store_true',
        help="apply the start values from the model description")
    parser.add_argument('--output-interval',
                        type=float,
                        help="interval for sampling the output")
    parser.add_argument('--input-file', help="CSV file to use as input")
    parser.add_argument('--output-variables',
                        nargs='+',
                        help="Variables to record")
    parser.add_argument('--output-file', help="CSV to store the results")
    parser.add_argument('--timeout',
                        type=float,
                        help="max. time to wait for the simulation to finish")
    parser.add_argument('--debug-logging',
                        action='store_true',
                        help="enable the FMU's debug logging")
    parser.add_argument('--visible',
                        action='store_true',
                        help="enable interactive mode")
    parser.add_argument('--fmi-logging',
                        action='store_true',
                        help="enable FMI logging")
    parser.add_argument('--show-plot',
                        action='store_true',
                        help="plot the results")

    args = parser.parse_args()

    if args.command == 'info':

        from fmpy import dump
        dump(args.fmu_filename)

    elif args.command == 'validate':

        import sys
        from fmpy.util import validate_fmu

        messages = validate_fmu(args.fmu_filename)

        if len(messages) == 0:
            print('The validation passed')
        else:
            print('The following errors were found:')
            for message in messages:
                print()
                print(message)
            sys.exit(1)

    elif args.command == 'compile':

        from fmpy.util import compile_platform_binary
        compile_platform_binary(args.fmu_filename)

    elif args.command == 'add-remoting':

        from fmpy.util import add_remoting
        add_remoting(args.fmu_filename)

    elif args.command == 'simulate':

        from fmpy import simulate_fmu
        from fmpy.util import read_csv, write_csv, plot_result

        if args.start_values:
            if len(args.start_values) % 2 != 0:
                raise Exception("Start values must be name-value pairs.")
            start_values = {
                k: v
                for k, v in zip(args.start_values[::2],
                                args.start_values[1::2])
            }
        else:
            start_values = {}

        input = read_csv(args.input_file) if args.input_file else None

        if args.fmi_logging:
            fmi_call_logger = lambda s: print('[FMI] ' + s)
        else:
            fmi_call_logger = None

        result = simulate_fmu(
            args.fmu_filename,
            validate=args.validate,
            start_time=args.start_time,
            stop_time=args.stop_time,
            solver=args.solver,
            step_size=args.step_size,
            relative_tolerance=args.relative_tolerance,
            output_interval=args.output_interval,
            record_events=not args.dont_record_events,
            fmi_type=None,
            start_values=start_values,
            apply_default_start_values=args.apply_default_start_values,
            input=input,
            output=args.output_variables,
            timeout=args.timeout,
            debug_logging=args.debug_logging,
            visible=args.visible,
            fmi_call_logger=fmi_call_logger)

        if args.output_file:
            write_csv(filename=args.output_file, result=result)

        if args.show_plot:
            plot_result(result=result, window_title=args.fmu_filename)
Ejemplo n.º 9
0
def main():

    import argparse
    import textwrap

    description = """\
    Validate and simulate Functional Mock-up Units (FMUs)

    Get information about an FMU:
       
        fmpy info Rectifier.fmu
     
    Simulate an FMU:
     
        fmpy simulate Rectifier.fmu --show-plot
        
    Compile a source code FMU:
    
        fmpy compile Rectifier.fmu
    """

    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=textwrap.dedent(description))

    parser.add_argument('command',
                        choices=['info', 'simulate', 'compile'],
                        help="Command to execute")
    parser.add_argument('fmu_filename', help="filename of the FMU")

    parser.add_argument('--solver',
                        choices=['Euler', 'CVode'],
                        default='CVode',
                        help="solver to use for Model Exchange")
    parser.add_argument('--input-file', help="CSV file to use as input")
    parser.add_argument('--output-variables',
                        nargs='+',
                        help="Variables to record")
    parser.add_argument('--output-file', help="CSV to store the results")
    parser.add_argument('--num-samples',
                        default=500,
                        type=int,
                        help="number of samples to record")
    parser.add_argument('--step-size',
                        type=float,
                        help="step size for fixed-step solvers")
    parser.add_argument('--start-time',
                        type=float,
                        help="start time for the simulation")
    parser.add_argument('--stop-time',
                        type=float,
                        help="stop time for the simulation")
    parser.add_argument('--show-plot',
                        action='store_true',
                        help="plot the results")
    parser.add_argument('--timeout',
                        type=float,
                        help="max. time to wait for the simulation to finish")
    parser.add_argument('--fmi-logging',
                        action='store_true',
                        help="enable FMI logging")
    parser.add_argument('--start-values',
                        nargs='+',
                        help="name-value pairs of start values")

    args = parser.parse_args()

    if args.command == 'info':

        from fmpy import dump
        dump(args.fmu_filename)

    elif args.command == 'compile':

        from fmpy.util import compile_platform_binary
        compile_platform_binary(args.fmu_filename)

    elif args.command == 'simulate':

        from fmpy import simulate_fmu
        from fmpy.util import read_csv, write_csv, plot_result

        if args.start_values:
            if len(args.start_values) % 2 != 0:
                raise Exception("Start values must be name-value pairs.")
            start_values = {
                k: v
                for k, v in zip(args.start_values[::2],
                                args.start_values[1::2])
            }
        else:
            start_values = {}

        input = read_csv(args.input_file) if args.input_file else None

        result = simulate_fmu(args.fmu_filename,
                              validate=True,
                              start_time=args.start_time,
                              stop_time=args.stop_time,
                              solver=args.solver,
                              step_size=args.step_size,
                              output_interval=None,
                              fmi_type=None,
                              start_values=start_values,
                              input=input,
                              output=args.output_variables,
                              timeout=args.timeout,
                              fmi_logging=args.fmi_logging)

        if args.output_file:
            write_csv(filename=args.output_file, result=result)

        if args.show_plot:
            plot_result(result=result, window_title=args.fmu_filename)
Ejemplo n.º 10
0
    if len(fmus) == 0:
        print("Could not find any fmus within path: {}".format(args.fmus))
        sys.exit(-1)
else:
    if not args.fmus.endswith('.fmu'):
        print("Target file is not an FMU: {}".format(args.fmus))
        sys.exit(-1)
    if not os.path.exists(args.fmus):
        print("Could not find target fmu file: {}".format(args.fmus))
        sys.exit(-1)
    fmus.append(args.fmus)

# Create destrination directory
try:
    os.mkdir(args.dst)
except OSError as error:
    print(error)
    sys.exit(-1)

# Cross compile fmus with fmpy
for fmu in fmus:
    print("Compiling: {}...".format(fmu))

    filename = os.path.join(args.dst, os.path.basename(fmu))
    print("To destination: {}".format(filename))
    try:
        compile_platform_binary(fmu, filename)
    except:
        print("Error compiling {}".format(fmu))