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
def restart(self, timeStamp): print('A_component.restart() called') if (self.services is None): message = 'Error in A_component restart(): No self.services' print(message) raise Exception(message) services = self.services # Get restart files listed in config file. restart_root = config.get_global_param(self, services, 'RESTART_ROOT') restart_time = config.get_global_param(self, services, 'RESTART_TIME') try: services.get_restart_files(restart_root, restart_time, self.RESTART_FILES) except Exception: message = 'Error in call to get_restart_files()' print(message) self.services.exception(message) raise return 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)
def step(self, timeStamp): print(' ') print('basic_init.step() called') services = self.services # Check if this is a restart simulation simulation_mode = config.get_global_param(self, services, 'SIMULATION_MODE') if simulation_mode == 'RESTART': print('basic_init: RESTART') if simulation_mode not in ['RESTART', 'NORMAL']: logMsg = 'basic_init: unrecoginzed SIMULATION_MODE: ' + \ simulation_mode self.services.error(logMsg) raise ValueError(logMsg) # ------------------------------------------------------------------------------ # # RESTART simulation mode # # ------------------------------------------------------------------------------ if simulation_mode == 'RESTART': # Get restart files listed in config file. Here just the state # files. restart_root = config.get_global_param(self, services, 'RESTART_ROOT') restart_time = config.get_global_param(self, services, 'RESTART_TIME') try: services.get_restart_files(restart_root, restart_time, self.RESTART_FILES) except BaseException: logMsg = 'Error in call to get_restart_files()' self.services.exception(logMsg) raise # Check if there is a config parameter CURRENT_STATE and add data 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() tfinal = timeloop[-1] # Put data into current state. For restart only tfinal gets # changed variable_dict = {'tfinal': tfinal} edit.add_variables_to_output_file(variable_dict, cur_state_file) # ------------------------------------------------------------------------------ # # NORMAL simulation mode # # ------------------------------------------------------------------------------ else: print('basic_init: simulation mode NORMAL') state_file_list = config.get_global_param(self, services, 'STATE_FILES').split(' ') # Generate state files as dummies so framework will have a complete set for file in state_file_list: print('touching state file = ', file) try: subprocess.call(['touch', file]) except Exception: print('No file ', file) init_mode = config.get_component_param(self, services, 'INIT_MODE', optional=True) if init_mode in ['touch_only', 'TOUCH_ONLY']: # Update state try: services.update_state() except Exception as e: print('Error in call to updateState()', e) raise return # Stage input files if any, thereby overwriting the dummy files generated above try: services.stage_input_files(self.INPUT_FILES) except Exception: message = 'basic_init: Error in staging input files' print(message) services.exception(message) raise # Check if there is a config parameter CURRENT_STATE and add data if # so. 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: run_id = config.get_global_param(self, services, 'RUN_ID') timeloop = services.get_time_loop() tinit = timeloop[0] tfinal = timeloop[-1] # Put data into current state variable_dict = { 'run_id': run_id, 'tinit': tinit, 'tfinal': tfinal } edit.add_variables_to_output_file(variable_dict, cur_state_file) # Execute HELPER_CODES if any HELPER_CODES = config.get_component_param(self, services, 'INIT_HELPER_CODES', optional=True, verbose=True) # Check if there are codes to be run if HELPER_CODES is not None and len(HELPER_CODES) > 0: HELPER_CODES = HELPER_CODES.split(' ') for code in HELPER_CODES: cmd = [code] print('Executing ', cmd) services.send_portal_event(event_type='COMPONENT_EVENT', event_comment=cmd) retcode = subprocess.call(cmd) if (retcode != 0): logMsg = 'Error executing '.join(map(str, cmd)) self.services.error(logMsg) raise Exception(logMsg) # Copy files to new names if any COPY_FILES = config.get_component_param(self, services, 'COPY_FILES', optional=True) COPIED_FILES_NEW_NAMES = config.get_component_param( self, services, 'COPIED_FILES_NEW_NAMES', optional=True) # Check if there are files to be copied if COPY_FILES is not None and len(COPY_FILES) > 0: COPY_FILES = COPY_FILES.split(' ') COPIED_FILES_NEW_NAMES = COPIED_FILES_NEW_NAMES.split(' ') # Verify that list lengths are the same if len(COPY_FILES) != len(COPIED_FILES_NEW_NAMES): message = ( 'Error in generic_component init: COPY_FILES and ' 'COPIED_FILES_NEW_NAMES lists are different lengths') print(message) self.services.error(message) raise Exception(message) # Copy the files for i in range(len(COPY_FILES)): try: os.system('cp ' + COPY_FILES[i] + ' ' + COPIED_FILES_NEW_NAMES[i]) except OSError as xxx_todo_changeme: (errno, strerror) = xxx_todo_changeme.args print( 'Error copying file %s to %s' % (COPY_FILES[i], COPIED_FILES_NEW_NAMES[i]), strerror) services.error(COPY_FILES[i] + '-> ' + COPIED_FILES_NEW_NAMES[i]) raise Exception( 'Error copying COPY_FILES -> COPIED_FILES_NEW_NAMES' ) # Update state try: services.update_state() except Exception as e: print('Error in call to updateState()', e) raise # "Archive" output files in history directory services.stage_output_files(timeStamp, self.OUTPUT_FILES) print(' ')
def step(self, timeStamp): print(' ') print('basic_init.step() called') services = self.services # Check if this is a restart simulation simulation_mode = config.get_global_param(self, services, 'SIMULATION_MODE') if simulation_mode == 'RESTART': print('basic_init: RESTART') if simulation_mode not in ['RESTART', 'NORMAL']: logMsg = 'basic_init: unrecoginzed SIMULATION_MODE: ' + \ simulation_mode self.services.error(logMsg) raise ValueError(logMsg) # ------------------------------------------------------------------------------ # # RESTART simulation mode # # ------------------------------------------------------------------------------ if simulation_mode == 'RESTART': # Get restart files listed in config file. Here just the state # files. restart_root = config.get_global_param(self, services, 'RESTART_ROOT') restart_time = config.get_global_param(self, services, 'RESTART_TIME') try: services.get_restart_files(restart_root, restart_time, self.RESTART_FILES) except BaseException: logMsg = 'Error in call to get_restart_files()' self.services.exception(logMsg) raise # Check if there is a config parameter CURRENT_STATE and add data 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() tfinal = timeloop[-1] # Put data into current state. For restart only tfinal gets # changed variable_dict = {'tfinal': tfinal} edit.add_variables_to_output_file(variable_dict, cur_state_file) # ------------------------------------------------------------------------------ # # NORMAL simulation mode # # ------------------------------------------------------------------------------ else: print('basic_init: simulation mode NORMAL') state_file_list = config.get_global_param(self, services, 'STATE_FILES').split(' ') # Generate state files as dummies so framework will have a complete set for file in state_file_list: print('touching state file = ', file) try: subprocess.call(['touch', file]) except Exception: print('No file ', file) init_mode = config.get_component_param(self, services, 'INIT_MODE', optional=True) if init_mode in ['touch_only', 'TOUCH_ONLY']: # Update state try: services.update_state() except Exception as e: print('Error in call to updateState()', e) raise return # Check if there is a config parameter CURRENT_STATE and add data if # so. 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: run_id = config.get_global_param(self, services, 'RUN_ID') timeloop = services.get_time_loop() tinit = timeloop[0] tfinal = timeloop[-1] # Put data into current state variable_dict = { 'run_id': run_id, 'tinit': tinit, 'tfinal': tfinal } edit.add_variables_to_output_file(variable_dict, cur_state_file) # Stage input files if any, thereby overwriting the dummy files generated above try: services.stage_input_files(self.INPUT_FILES) except Exception: message = 'basic_init: Error in staging input files' print(message) services.exception(message) raise INIT_HELPER_CODE_bin = config.get_component_param( self, services, 'INIT_HELPER_CODE', optional=True) if (INIT_HELPER_CODE_bin is not None) and (len(INIT_HELPER_CODE_bin) != 0): cmd = [INIT_HELPER_CODE_bin] print('Executing ', cmd) services.send_portal_event(event_type='COMPONENT_EVENT', event_comment=cmd) retcode = subprocess.call(cmd) if (retcode != 0): logMsg = 'Error executing '.join(map(str, cmd)) self.services.error(logMsg) raise Exception(logMsg) # Update state try: services.update_state() except Exception as e: print('Error in call to updateState()', e) raise # "Archive" output files in history directory services.stage_output_files(timeStamp, self.OUTPUT_FILES)
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
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
def init(self, timeStamp=0): print('C_component.init() 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. 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') # 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 as e: print('Error in call to stageInputFiles()', e) self.services.error('Error in call to stageInputFiles()') raise # Modify data in template input file with data from config file # None required for this example # Run integrator - Not needed during init for this example # Add initial data to state file # None required for this example # 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
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