예제 #1
0
    def init(self, sid, work_dir=None, model_name=None, instance_name=None, var_table=None, fmi_me_version=None,
             step_size=1, step_factor=1, integrator='ck', logging_on=False, stop_before_event=False,
             event_search_precision=1e-10, translation_table=None, automated_initialization=True):
        self.sid = sid
        # Set simulator parameters needed mostly for the FMU:
        self.work_dir = work_dir
        self.model_name = model_name
        self.instance_name = instance_name
        self.step_size = step_size
        self.step_factor = step_factor
        self.logging_on = logging_on
        self.stop_before_event = stop_before_event
        self.integrator = getattr(fmipp, integrator)
        self.event_search_precision = event_search_precision
        self.fmi_me_version = fmi_me_version
        self.automated_initialization = automated_initialization

        # Extract the module from the FMU:
        path_to_fmu = os.path.join(self.work_dir, self.model_name + '.fmu')
        self.uri_to_extracted_fmu = fmipp.extractFMU(path_to_fmu, self.work_dir)
        # Get the model description and use it if the user did not specify the variables:
        xmlfile = os.path.join(self.work_dir, self.model_name, 'modelDescription.xml')
        if var_table is None:
            self.var_table, self.translation_table = mosaik_fmume_test.parse_xml.get_var_table(xmlfile)
        else:
            self.var_table = var_table
            self.translation_table = translation_table
        self.adjust_var_table()
        self.adjust_meta()

        return self.meta
예제 #2
0
    def __init__(self,
                 host,
                 input_attributes=None,
                 output_attributes=None,
                 is_first=False):
        # Implement OBNL client node
        super(Node, self).__init__(host,
                                   'obnl_vhost',
                                   'obnl',
                                   'obnl',
                                   'config_file.json',
                                   input_attributes=input_attributes,
                                   output_attributes=output_attributes,
                                   is_first=is_first)

        self.redis = redis.StrictRedis(host=host, port=6379, db=0)

        # Declare model
        work_dir = os.path.split(
            os.path.abspath(__file__))[0]  # define working directory
        model_name = 'IBPSA_Fluid_FixedResistances_Examples_PlugFlowPipe'  # define FMU model name
        print(work_dir)
        path_to_fmu = os.path.join(work_dir,
                                   model_name + '.fmu')  # path to FMU
        uri_to_extracted_fmu = fmipp.extractFMU(path_to_fmu,
                                                work_dir)  # extract FMU

        logging_on = False
        time_diff_resolution = 1e-9
        self.fmu = fmipp.FMUCoSimulationV2(uri_to_extracted_fmu, model_name,
                                           logging_on, time_diff_resolution)

        print('successfully loaded the FMU')

        start_time = 0.
        stop_time = 3600. * 24.  # 24 hours

        instance_name = "trnsys_fmu_test"
        visible = False
        interactive = False
        status = self.fmu.instantiate(instance_name, start_time, visible,
                                      interactive)
        assert status == fmipp.fmiOK

        print('successfully instantiated the FMU')

        stop_time_defined = True
        status = self.fmu.initialize(start_time, stop_time_defined, stop_time)
        assert status == fmipp.fmiOK

        print('successfully initialized the FMU')

        # Set initial values / model parameters
        with open('init_values.json') as json_data:
            init_values = json.load(json_data)

        for key, val in init_values.items():
            self.fmu.setRealValue(key, val)

        print("DH set up done")
예제 #3
0
    def init(self, sid, work_dir=None, model_name=None, instance_name=None, var_table=None, step_size=1, logging_on=False,
             time_diff_resolution=1e-9, interactive=False, visible=False, stop_time_defined=False, step_factor=1,
             stop_time=10000):
        self.sid = sid
        # Set simulator parameters needed mostly for the FMU:
        self.work_dir = work_dir
        self.model_name = model_name
        self.instance_name = instance_name
        self.step_size = step_size
        self.step_factor = step_factor
        self.logging_on = logging_on
        self.time_diff_resolution =time_diff_resolution
        self.interactive = interactive
        self.visible = visible
        self.stop_time_defined = stop_time_defined
        self.stop_time = stop_time

        # Extract the module from the FMU:
        path_to_fmu = os.path.join(self.work_dir, self.model_name + '.fmu')
        self.uri_to_extracted_fmu = fmipp.extractFMU(path_to_fmu, self.work_dir)
        # Get the model description and use it if the user did not specify the variables:
        xmlfile = os.path.join(self.work_dir, self.model_name, 'modelDescription.xml')
        if var_table is None:
            self.var_table, self.translation_table = mosaik_fmume_test.parse_xml.get_var_table(xmlfile)
        else:
            self.var_table = var_table
            self.translation_table = {}

        self.adjust_var_table()
        self.adjust_meta()

        return self.meta
예제 #4
0
    def init(self,
             sid,
             work_dir,
             model_name,
             instance_name,
             step_size,
             start_time=0,
             stop_time=0,
             logging_on=False,
             time_diff_resolution=1e-9,
             timeout=0,
             interactive=False,
             visible=False,
             stop_time_defined=False,
             seconds_per_mosaik_timestep=1,
             var_table=None,
             translation_table=None,
             verbose=False):

        self.step_size = step_size
        self.work_dir = work_dir
        self.model_name = model_name
        self.instance_name = instance_name
        self.start_time = start_time
        self.stop_time = stop_time
        self.logging_on = logging_on
        self.time_diff_resolution = time_diff_resolution  # How close should two events be to be considered equal?
        self.timeout = timeout
        self.interactive = interactive
        self.visible = visible
        self.stop_time_defined = stop_time_defined
        self.sec_per_mt = seconds_per_mosaik_timestep  # Number of seconds of internaltime per mosaiktime (Default: 1, mosaiktime measured in seconds)
        self.verbose = verbose

        path_to_fmu = os.path.join(self.work_dir, self.model_name + '.fmu')
        if self.verbose:
            print('Attempted to extract FMU {0}, Path {1}'.format(
                path_to_fmu, self.work_dir))
        self.uri_to_extracted_fmu = fmipp.extractFMU(path_to_fmu,
                                                     self.work_dir)
        assert self.uri_to_extracted_fmu is not None
        '''If no variable table is given by user, parse the modelDescription.xml for a table -
        however, this will not work properly for some FMUs due to varying conventions.'''
        xmlfile = os.path.join(self.work_dir, self.model_name,
                               'modelDescription.xml')
        if var_table is None:
            self.var_table, self.translation_table = self.get_var_table(
                xmlfile)
        else:
            self.var_table = var_table
            self.translation_table = translation_table

        self.adjust_var_table()

        return self.meta
예제 #5
0
    def __init__(
        self, name, group, inputs_map, outputs, init_values
    ):  # If needed you can pass more values to your node constructor.
        super(MyNode, self).__init__(
            name, group, inputs_map, outputs, init_values
        )  # Keep this line, it triggers the parent class method.

        # This is where you define the attribute of your model, this one is pretty basic.
        # FMU loading
        work_dir = os.path.split(
            os.path.abspath(__file__))[0]  # define working directory
        model_name = 'Exercise1D'  # define FMU model name
        path_to_fmu = os.path.join(work_dir,
                                   model_name + '.fmu')  # path to FMU
        uri_to_extracted_fmu = fmipp.extractFMU(path_to_fmu,
                                                work_dir)  # extract FMU
        logging_on = False
        time_diff_resolution = 1e-9
        self.fmu = fmipp.FMUCoSimulationV1(uri_to_extracted_fmu, model_name,
                                           logging_on, time_diff_resolution)
        print('successfully loaded the FMU')

        ## FMU instantiation
        start_time = 0.
        stop_time = 3600. * 24.  # 24 hours
        self.step_size = 3600  # 1 hour
        self.tempo = self.step_size
        instance_name = "eplus_fmu_test"
        visible = False
        interactive = False
        status = self.fmu.instantiate(instance_name, start_time, visible,
                                      interactive)
        assert status == fmipp.fmiOK
        print('successfully instantiated the FMU')

        ## FMU initialization
        stop_time_defined = True
        status = self.fmu.initialize(start_time, stop_time_defined, stop_time)
        assert status == fmipp.fmiOK
        print('successfully initialized the FMU')
예제 #6
0
    def __init__(self):
        super().__init__(
        )  # Keep this line, it triggers the parent class __init__ method.

        # This is where you define the attribute of your model, this one is pretty basic.
        # FMU loading
        work_dir = os.path.split(
            os.path.abspath(__file__))[0]  # define working directory
        #self.ModelName = 'Residential_DH'  # define FMU model name
        self.modelName = 'Residential_DH_2'
        path_to_fmu = os.path.join(work_dir,
                                   self.modelName + '.fmu')  # path to FMU
        uri_to_extracted_fmu = fmipp.extractFMU(path_to_fmu,
                                                work_dir)  # extract FMU
        logging_on = False
        time_diff_resolution = 1e-9
        self.fmu = fmipp.FMUCoSimulationV1(uri_to_extracted_fmu,
                                           self.modelName, logging_on,
                                           time_diff_resolution)
        print('successfully loaded the FMU')

        ## FMU instantiation
        start_time = 3600. * 24. * 0.
        stop_time = 3600. * 24. * 31.  # 24 hours
        self.step_size = 3600.  # 1/6 hour
        self.tempo = start_time  # self.step_size
        instance_name = "eplus_fmu_test"
        visible = False
        interactive = False
        status = self.fmu.instantiate(instance_name, start_time, visible,
                                      interactive)
        assert status == fmipp.fmiOK
        print('successfully instantiated the FMU')

        ## FMU initialization
        stop_time_defined = True
        status = self.fmu.initialize(start_time, stop_time_defined, stop_time)
        assert status == fmipp.fmiOK
        print('successfully initialized the FMU')
예제 #7
0
    def __init__(self):
        super().__init__(
        )  # Keep this line, it triggers the parent class __init__ method.

        # This is where you define the attribute of your model, this one is pretty basic.
        # FMU loading
        work_dir = os.path.split(
            os.path.abspath(__file__))[0]  # define working directory
        model_name = 'COGplant'  # define FMU model name
        path_to_fmu = os.path.join(work_dir,
                                   model_name + '.fmu')  # path to FMU
        uri_to_extracted_fmu = fmipp.extractFMU(path_to_fmu,
                                                work_dir)  # extract FMU
        logging_on = False
        time_diff_resolution = 1e-9
        self.fmu = fmipp.FMUCoSimulationV1(uri_to_extracted_fmu, model_name,
                                           logging_on, time_diff_resolution)
        print('successfully loaded the FMU')

        ## FMU instantiation
        start_time = 0.
        stop_time = 450. * 8. * 24 * 31.  # 24 hours
        self.step_size = 3600  # 450. # 1 hour
        self.tempo = self.step_size
        instance_name = "trnsys_fmu_test"
        visible = False
        interactive = False
        status = self.fmu.instantiate(instance_name, start_time, visible,
                                      interactive)
        assert status == fmipp.fmiOK
        print('successfully instantiated the FMU')

        ## FMU initialization
        stop_time_defined = True
        status = self.fmu.initialize(start_time, stop_time_defined, stop_time)
        assert status == fmipp.fmiOK
        #fmu.setRealValue( 'Treturn', 40. ) # Very important to initialize
        print('successfully initialized the FMU')
예제 #8
0
import fmipp
import os.path
import math

work_dir = os.path.split(
    os.path.abspath(__file__))[0]  # define working directory
model_name = 'Exercise1D'  # define FMU model name

path_to_fmu = os.path.join(work_dir, model_name + '.fmu')  # path to FMU
uri_to_extracted_fmu = fmipp.extractFMU(path_to_fmu, work_dir)  # extract FMU

logging_on = False
time_diff_resolution = 1e-9
fmu = fmipp.FMUCoSimulationV1(uri_to_extracted_fmu, model_name, logging_on,
                              time_diff_resolution)

print('successfully loaded the FMU')

start_time = 0.
stop_time = 3600. * 24.  # 24 hours

instance_name = "eplus_fmu_test"
visible = False
interactive = False
status = fmu.instantiate(instance_name, start_time, visible, interactive)
assert status == fmipp.fmiOK

print('successfully instantiated the FMU')

stop_time_defined = True
status = fmu.initialize(start_time, stop_time_defined, stop_time)
예제 #9
0
work_dir = os.getcwd()
logging_on = False

#------------------------------------------------------------------------------
# the matlab controller
#------------------------------------------------------------------------------

model_name_M = 'RmsConverterController_R2014b_sf'
stop_before_event = False
event_search_precision = 1e-10
integrator_type = fmipp.eu

path_to_fmu_M = os.path.join(work_dir, model_name_M + '.fmu')
# Extract FMU and retrieve URI to directory.
uri_to_extracted_fmu_M = fmipp.extractFMU(path_to_fmu_M, work_dir)
# using the FMI 2.0 specification
fmuM = fmipp.FMUModelExchangeV2(uri_to_extracted_fmu_M, model_name_M,
                                logging_on, stop_before_event,
                                event_search_precision, integrator_type)
# Instantiate the FMU.
status1 = fmuM.instantiate("my_RMS_controller")  # instantiate model

assert status1 == fmipp.fmiOK
# Initialize the FMU.
status1 = fmuM.initialize()  # initialize model
assert status1 == fmipp.fmiOK

# Initialize the FMU.
stop_time = 20.
stop_time_defined = True
예제 #10
0
def test_IncrementalFMU(zip_command):
    work_dir = os.path.split(
        os.path.abspath(__file__))[0]  # define working directory
    model_name = 'zigzag'  # define FMU model name

    path_to_fmu = os.path.join(work_dir, model_name + '.fmu')  # path to FMU

    uri_to_extracted_fmu = fmipp.extractFMU(path_to_fmu,
                                            work_dir,
                                            command=zip_command)  # extract FMU

    # create FMI++ wrapper for FMU for Model Exchange (Version 1.0)
    logging_on = False
    event_search_precision = 1e-7
    integrator_type = fmipp.bdf
    fmu = fmipp.IncrementalFMU(uri_to_extracted_fmu, model_name, logging_on,
                               event_search_precision, integrator_type)

    # number of parameters that should be initialized
    n_init = 2

    # construct string array for init parameter names
    init_vars = fmipp.new_string_array(n_init)
    fmipp.string_array_setitem(init_vars, 0, 'k')
    fmipp.string_array_setitem(init_vars, 1, 'x')

    # construct string array for init parameter values
    init_vals = fmipp.new_double_array(n_init)
    fmipp.double_array_setitem(init_vals, 0, 1.0)
    fmipp.double_array_setitem(init_vals, 1, 0.0)

    # define number of real outputs
    n_real_outputs = 1

    # construct string array with output names
    outputs = fmipp.new_string_array(n_real_outputs)
    fmipp.string_array_setitem(outputs, 0, 'x')

    # define real outputs
    fmu.defineRealOutputs(outputs, n_real_outputs)

    start_time = 0.0
    stop_time = 5.0
    step_size = 0.3
    horizon = 2 * step_size
    int_step_size = step_size / 2

    status = fmu.init('zigzag1', init_vars, init_vals, n_init, start_time,
                      horizon, step_size, int_step_size)  # initialize model
    assert status == 1  # check status

    time = start_time
    next = start_time

    x_expected = {
        0.0: 0.0,
        0.3: 0.3,
        0.6: 0.6,
        0.9: 0.9,
        1.0: 1.0,
        1.3: 0.7,
        1.6: 0.4,
        1.9: 0.1,
        2.2: -0.2,
        2.5: -0.5,
        2.8: -0.8,
        3.0: -1.0,
        3.3: -0.7,
        3.6: -0.4,
        3.9: -0.1,
        4.2: 0.2,
        4.5: 0.5,
        4.8: 0.8,
    }

    while (time + step_size - stop_time < 1e-6):
        oldnext = next
        next = fmu.sync(time, min(time + step_size, next))
        result = fmu.getRealOutputs()
        x = fmipp.double_array_getitem(result, 0)
        time = min(time + step_size, oldnext)
        assert round(x, 1) == x_expected[round(time, 1)]
예제 #11
0
def test_FMUExport(zip_command):
    from fmipp.export.createFMU import createFMU
    from FMUExportTest import FMUExportTestClass

    model_name = 'FMUExportTestCS'  # define FMU model name

    start_values = {
        'pr_y': 2.2,
        'pr_x': 1.1,
        'ir_y': 2.,
        'ir_x': 1.,
        'pi_y': 3,
        'pi_x': 6,
        'ii_y': 4,
        'ii_x': 5,
        'pb_y': True,
        'pb_x': True,
        'ib_y': False,
        'ib_x': True,
        # 'ps_y' : 'abc', 'ps_x' : 'def',
        # 'is_y' : 'ghi', 'is_x' : 'jkl'
    }

    optional_files = ['extra.dat']

    createFMU(FMUExportTestClass,
              model_name,
              fmi_version='2',
              verbose=False,
              start_values=start_values,
              optional_files=optional_files)

    work_dir = os.path.split(
        os.path.abspath(__file__))[0]  # define working directory

    path_to_fmu = os.path.join(work_dir, model_name + '.fmu')  # path to FMU

    uri_to_extracted_fmu = fmipp.extractFMU(path_to_fmu,
                                            work_dir,
                                            command=zip_command)  # extract FMU

    logging_on = False
    time_diff_resolution = 1e-9
    fmu = fmipp.FMUCoSimulationV2(uri_to_extracted_fmu, model_name, logging_on,
                                  time_diff_resolution)

    start_time = 0.
    stop_time = 10.

    instance_name = "test1"
    visible = False
    interactive = False
    status = fmu.instantiate(instance_name, start_time, visible, interactive)
    assert status == fmipp.fmiOK

    stop_time_defined = True
    status = fmu.initialize(start_time, stop_time_defined, stop_time)
    assert status == fmipp.fmiOK

    time = 0.
    step_size = 1.

    new_step = True
    status = fmu.doStep(time, step_size, new_step)
    assert status == fmipp.fmiOK

    assert 1.1 == fmu.getRealValue('or_x')
    assert fmu.getLastStatus() == fmipp.fmiOK

    assert 4.4 == fmu.getRealValue('or_y')
    assert fmu.getLastStatus() == fmipp.fmiOK

    assert 30 == fmu.getIntegerValue('oi_x')
    assert fmu.getLastStatus() == fmipp.fmiOK

    assert 12 == fmu.getIntegerValue('oi_y')
    assert fmu.getLastStatus() == fmipp.fmiOK

    assert True == fmu.getBooleanValue('ob_x')
    assert fmu.getLastStatus() == fmipp.fmiOK

    assert False == fmu.getBooleanValue('ob_y')
    assert fmu.getLastStatus() == fmipp.fmiOK
예제 #12
0
def test_FMUModelExchange(zip_command):
    work_dir = os.path.split(
        os.path.abspath(__file__))[0]  # define working directory
    model_name = 'zigzag'  # define FMU model name

    path_to_fmu = os.path.join(work_dir, model_name + '.fmu')  # path to FMU

    uri_to_extracted_fmu = fmipp.extractFMU(path_to_fmu,
                                            work_dir,
                                            command=zip_command)  # extract FMU

    # create FMI++ wrapper for FMU for Model Exchange (Version 1.0)
    logging_on = False
    stop_before_event = False
    event_search_precision = 1e-10
    integrator_type = fmipp.bdf
    fmu = fmipp.FMUModelExchangeV1(uri_to_extracted_fmu, model_name,
                                   logging_on, stop_before_event,
                                   event_search_precision, integrator_type)

    status = fmu.instantiate("my_test_model_1")  # instantiate model
    assert status == fmipp.fmiOK  # check status

    status = fmu.setRealValue('k', 1.0)  # set value of parameter 'k'
    assert status == fmipp.fmiOK  # check status

    status = fmu.initialize()  # initialize model
    assert status == fmipp.fmiOK  # check status

    t = 0.0
    stepsize = 0.125
    tstop = 3.0

    x_expected = {
        0.125: 0.125,
        0.250: 0.250,
        0.375: 0.375,
        0.500: 0.500,
        0.625: 0.625,
        0.750: 0.750,
        0.875: 0.875,
        1.000: 1.000,
        1.000: 1.000,
        1.125: 0.875,
        1.250: 0.750,
        1.375: 0.625,
        1.500: 0.500,
        1.625: 0.375,
        1.750: 0.250,
        1.875: 0.125,
        2.000: 0.000,
        2.125: -0.125,
        2.250: -0.250,
        2.375: -0.375,
        2.500: -0.500,
        2.625: -0.625,
        2.750: -0.750,
        2.875: -0.875,
        3.000: -1.000,
    }

    while ((t + stepsize) - tstop < 1e-6):
        t = fmu.integrate(t + stepsize)  # integrate model
        x = fmu.getRealValue("x")  # retrieve output variable 'x'
        assert round(x, 3) == x_expected[round(t, 3)]
예제 #13
0
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------

work_dir=os.getcwd()
logging_on = False
stop_before_event = False
event_search_precision = 1e-10
integrator_type = fmipp.eu

#------------------------------------------------------------------------------
# The First Matlab Controller (FRT)
#------------------------------------------------------------------------------

model_name_M='FRTController_mar_v2_sf'
path_to_fmu_M = os.path.join(work_dir, model_name_M + '.fmu')   # Extract FMU and retrieve URI to directory.
uri_to_extracted_fmu_M= fmipp.extractFMU( path_to_fmu_M, work_dir )   # using the FMI 2.0 specification
fmuM1 = fmipp.FMUModelExchangeV2(uri_to_extracted_fmu_M, model_name_M, logging_on, stop_before_event, event_search_precision, integrator_type )

status1 = fmuM1.instantiate( "my_FRT_controller" ) # Instantiate the FMU model
assert status1 == fmipp.fmiOK

# set all internal parameters of the second FMU 
#------------------------------------------------------------------------------ 
fmuM1.setRealValue("Parameters.StateMachine_dT", step_size)   # dT: time step size of the simulation federate
fmuM1.setRealValue("Parameters.State_Machine.state_transition_RpS3", 0.5)   #ramping rate when in state=3,, determines how long the converter will ramp up the active power and when it returns to normal operation, i.e., state=1

# Initializing the FMU model
#------------------------------------------------------------------------------ 
fmuM1.setRealValue("Udc",1.0)   # - Udc
fmuM1.setRealValue("VPCC",1.0)   # - VPCC
fmuM1.setBooleanValue("release",False)   # - release