Ejemplo n.º 1
0
    def pre_step_logic(self, services, timeStamp):

        # Check if there is a config parameter CURRENT_STATE and update t0, t1
        # if so.
        cur_state_file = config.get_global_param(
            self, services, 'CURRENT_STATE', optional=True)
        print('pre-step-logic: cur_state_file = ', cur_state_file)
        if cur_state_file is not None and len(cur_state_file) > 0:
            state_dict = edit.input_file_to_variable_dict(cur_state_file)
            change_dict = {'t0': state_dict['t1'], 't1': timeStamp}
            edit.modify_variables_in_file(change_dict, cur_state_file)
        return
Ejemplo n.º 2
0
    def step(self, timestamp=0):

        services = self.services
        services.stage_state()
        services.stage_input_files(self.INPUT_FILES)

        # get list of ports
        ports = config.get_global_param(self, services, 'PORTS')
        port_names = ports['NAMES'].split()
        print('PORTS =', port_names)
        port_dict = {}
        port_id_list = []

        # Instantiate components in port_names list, except DRIVER itself
        # which is done by the framework
        print(' ')

        for port_name in port_names:
            if port_name in ["DRIVER"]:
                continue
            port = services.get_port(port_name)
            if(port is None):
                logMsg = 'Error accessing ' + port_name + ' component'
                services.error(logMsg)
                raise Exception(logMsg)
            port_dict[port_name] = port
            port_id_list.append(port)
            print(' ')

        # Is this a simulation startup or restart
        sim_mode = config.get_global_param(self, services, 'SIMULATION_MODE')

        # Get timeloop for simulation
        timeloop = services.get_time_loop()
        t = timeloop[0]

        # Initialize components in PORTS list for startup or restart
        print(' ')

        init_mode = 'init'
        if sim_mode == 'RESTART':
            init_mode = 'restart'

        for port_name in port_names:
            if port_name in ['INIT', 'DRIVER']:
                continue
            self.component_call(
                services,
                port_name,
                port_dict[port_name],
                init_mode,
                t)

        # Get state files into driver work directory
        services.stage_state()

        # Get Portal RUNID and save to a file
        run_id = config.get_global_param(self, services, 'PORTAL_RUNID')
        sym_root = config.get_global_param(self, services, 'SIM_ROOT')
        path = os.path.join(sym_root, 'PORTAL_RUNID')
        runid_file = open(path, 'a')
        runid_file.writelines(run_id + '\n')
        runid_file.close()

        # Check if there is a config parameter CURRENT_STATE, which means SWIM Plasma 
        # State, and add data if so. In this case set t0 = t1 = tinit
        cur_state_file = config.get_global_param(
            self, services, 'CURRENT_STATE', optional=True)
        if cur_state_file is not None and len(cur_state_file) > 0:
            timeloop = services.get_time_loop()
            variable_dict = {'t0': timeloop[0], 't1': timeloop[0]}
            if sim_mode == 'NORMAL':
                edit.add_variables_to_output_file(
                    variable_dict, cur_state_file)
            if sim_mode == 'RESTART':
                edit.modify_variables_in_file(variable_dict, cur_state_file)
            if sim_mode not in ['NORMAL', 'RESTART']:
                message = 'Unknown Simulation mode ' + sim_mode
                print(message)
                services.exception(message)
                raise Exception(message)

            services.update_state()

        # Post init processing: stage  state, stage output
        services.stage_output_files(t, self.OUTPUT_FILES)

        print(' init sequence complete--ready for time loop')

        INIT_ONLY = config.get_component_param(
            self, services, 'INIT_ONLY', optional=True)
        if INIT_ONLY in [True, 'true', 'True', 'TRUE']:
            message = 'INIT_ONLY: Intentional stop after INIT phase'
            print(message)
            return

# ------------------------------------------------------------------------------
#
# Start Physics Layer
#
# ------------------------------------------------------------------------------

        # Iterate through the timeloop
        for t in timeloop[1:len(timeloop)]:
            print(' ')
            print('Driver: step to time = ', t)
            services.update_time_stamp(t)

        # pre_step_logic
        # It is sometimes necessary to do some tasks before starting a new
        # time step. This could entail evaluating logic based on results of
        # last time step, or calculations not part of any of the components.
        # If there is a current state file it usually at least involves setting
        # the start time of this time step, t0, equal to the end time of the
        # last step, t1, and setting t1 = t.

        # call pre_step_logic
            services.stage_state()
            self.pre_step_logic(services, float(t))
            services.update_state()
            print(' ')

        # Call step for each component

            print(' ')
            for port_name in port_names:
                if port_name in ['INIT', 'DRIVER']:
                    continue
                self.component_call(
                    services, port_name, port_dict[port_name], 'step', t)

            services.stage_state()

        # Post step processing: stage  state, checkpoint components and self
            services.stage_output_files(t, self.OUTPUT_FILES)
            services.checkpoint_components(port_id_list, t)
            self.checkpoint(t)

# ------------------------------------------------------------------------------
#
# End of Physics Layer
#
# ------------------------------------------------------------------------------

        # Post simulation: call checkpoint on each component

        services.checkpoint_components(port_id_list, t, Force=True)
        self.checkpoint(t)

        # Post simulation: call finalize on each component
        print(' ')
        for port_name in port_names:
            if port_name in ['INIT', 'DRIVER']:
                continue
            self.component_call(
                services,
                port_name,
                port_dict[port_name],
                'finalize',
                t)
Ejemplo n.º 3
0
    def step(self, timeStamp):
        print('A_component.step() called')

        if (self.services is None):
            message = 'Error in A_component step (): No self.services'
            print(message)
            raise Exception(message)
        services = self.services

        # Get global configuration parameters
        cur_state_file = config.get_global_param(self, services,
                                                 'CURRENT_STATE')

        # Get component-specific configuration parameters.
        NPROC = config.get_component_param(self, services, 'NPROC')
        EXECUTABLE = config.get_component_param(self, services, 'EXECUTABLE')
        a_lin = config.get_component_param(self, services, 'a_lin')
        b_nonlin = config.get_component_param(self, services, 'b_nonlin')

        # Copy state files over to working directory
        try:
            services.stage_state()
        except Exception as e:
            print('Error in call to stage_state()', e)
            services.exception('Error in call to stage_state()')
            raise

    # Get input files
        try:
            services.stage_input_files(self.INPUT_FILES)
        except Exception:
            print('Error in call to stageInputFiles()')
            self.services.exception('Error in call to stageInputFiles()')
            raise

    # Modify data in template input file with data from config file
        change_dict = {'a_lin': a_lin, 'b_nonlin': b_nonlin}
        edit.modify_variables_in_file(change_dict, 'X_dot_code.in')

        # Modify data in template input file with data from state file
        state_dict = edit.input_file_to_variable_dict(cur_state_file)
        change_dict = {'X': state_dict['X'], 'Y': state_dict['Y']}
        edit.modify_variables_in_file(change_dict, 'X_dot_code.in')

        # Run X_dot_code with modified template input file
        cmd = EXECUTABLE
        print('Executing = ', cmd)
        services.send_portal_event(event_type='COMPONENT_EVENT',
                                   event_comment=cmd)
        cwd = services.get_working_dir()
        task_id = services.launch_task(NPROC, cwd, cmd)
        retcode = services.wait_task(task_id)
        if (retcode != 0):
            message = 'Error executing ', cmd
            print(message)
            self.services.error(message)
            raise Exception(message)
        print(cmd, ' finished \n')

        # Update state files in state work directory
        try:
            services.update_state()
        except Exception:
            message = 'Error in call to update_state()'
            print(message)
            services.error(message)
            raise

# "Archive" output files in history directory
        try:
            services.stage_output_files(timeStamp, self.OUTPUT_FILES)
        except Exception:
            message = 'Error in call to stage_output_files()'
            print(message)
            services.error(message)
            raise

        return
Ejemplo n.º 4
0
    def init(self, timeStamp=0):
        print('A_component.init() called')

        if (self.services is None):
            message = 'Error in A_component init (): No self.services'
            print(message)
            raise Exception(message)
        services = self.services

        # Get global configuration parameters
        cur_state_file = config.get_global_param(self, services,
                                                 'CURRENT_STATE')

        # Get component-specific configuration parameters. Note: Not all of these
        # are used in 'init' but if any are missing we get an exception now
        # instead of later
        BIN_PATH = config.get_component_param(self, services, 'BIN_PATH')
        RESTART_FILES = config.get_component_param(self, services,
                                                   'RESTART_FILES')
        NPROC = config.get_component_param(self, services, 'NPROC')
        EXECUTABLE = config.get_component_param(self, services, 'EXECUTABLE')
        X0 = config.get_component_param(self, services, 'X0')
        a_lin = config.get_component_param(self, services, 'a_lin')
        b_nonlin = config.get_component_param(self, services, 'b_nonlin')

        # Copy  state files over to working directory
        try:
            services.stage_state()
        except Exception as e:
            print('Error in call to stage_state()', e)
            services.exception('Error in call to stage_state()')
            raise

    # Get input files
        try:
            services.stage_input_files(self.INPUT_FILES)
        except Exception as e:
            print('Error in call to stageInputFiles()', e)
            self.services.exception('Error in call to stage_input_files()')
            raise

    # Modify data in template input file with data from config file
        change_dict = {'a_lin': a_lin, 'b_nonlin': b_nonlin}
        edit.modify_variables_in_file(change_dict, 'X_dot_code.in')

        # Run X_dot_code - Not needed during init for this example

        # Add initial data to state file
        variable_dict = {'X': X0}
        edit.add_variables_to_output_file(variable_dict, cur_state_file)

        # Update  state files in state work directory
        try:
            services.update_state()
        except Exception:
            message = 'Error in call to update_state()'
            print(message)
            services.exception(message)
            raise

# "Archive" output files in history directory

        try:
            services.stage_output_files(timeStamp, self.OUTPUT_FILES)
        except Exception:
            message = 'Error in call to stage_output_files()'
            print(message)
            services.exception(message)
            raise

        return
Ejemplo n.º 5
0
    def step(self, timeStamp):
        print('C_component.step() called')

        if (self.services is None):
            message = 'Error in C_component init (): No self.services'
            print(message)
            raise Exception(message)
        services = self.services

        # Get global configuration parameters
        cur_state_file = config.get_global_param(self, services,
                                                 'CURRENT_STATE')

        # Get component-specific configuration parameters.
        NPROC = config.get_component_param(self, services, 'NPROC')
        EXECUTABLE = config.get_component_param(self, services, 'EXECUTABLE')

        # Copy  state files over to working directory
        try:
            services.stage_state()
        except Exception as e:
            print('Error in call to stage_state()', e)
            services.error('Error in call to stage_state()')
            raise

    # Get input files
        try:
            services.stage_input_files(self.INPUT_FILES)
        except Exception:
            print('Error in call to stageInputFiles()')
            self.services.error('Error in call to stageInputFiles()')
            raise

    # Modify data in template input file with data from config file
    # no data to modify for this example.

    # Modify data in template input file with data from state files
    # Get data from cur_state_file
        state_dict = edit.input_file_to_variable_dict(cur_state_file)
        change_dict = {'X': state_dict['X'], 'Y': state_dict['Y']}
        delta_t = float(state_dict['t1']) - float(state_dict['t0'])
        change_dict['delta_t'] = delta_t

        # Get data from X_dot_code.out
        state_dict = edit.input_file_to_variable_dict('X_dot_code.out')
        change_dict['X_dot'] = state_dict['X_dot']

        # Get data from X_dot_code.out
        state_dict = edit.input_file_to_variable_dict('Y_dot_code.out')
        change_dict['Y_dot'] = state_dict['Y_dot']

        # Put data in integrator input fule
        edit.modify_variables_in_file(change_dict, 'integrator.in')

        # Run integrator with modified template input file
        cmd = EXECUTABLE
        print('Executing = ', cmd)
        services.send_portal_event(event_type='COMPONENT_EVENT',
                                   event_comment=cmd)
        cwd = services.get_working_dir()
        task_id = services.launch_task(NPROC, cwd, cmd)
        retcode = services.wait_task(task_id)
        if (retcode != 0):
            message = 'Error executing ', cmd
            print(message)
            self.services.error(message)
            raise Exception(message)
        print(cmd, ' finished \n')

        # Modify data in state files from output of C_code.
        variable_dict = edit.input_file_to_variable_dict('integrator.out')
        change_dict = {'X': variable_dict['X'], 'Y': variable_dict['Y']}
        edit.modify_variables_in_file(change_dict, cur_state_file)
        services.update_state()
        print(' ')

        # "Archive" output files in history directory
        try:
            services.stage_output_files(timeStamp, self.OUTPUT_FILES)
        except Exception:
            message = 'Error in call to stage_output_files()'
            print(message)
            services.error(message)
            raise

        return