def minimize_cost_scipy(costFun, x0, optMethod, printVerbose=False): if not printVerbose: OLDSTDOUT = tools.disable_console_output() from scipy.optimize import minimize import numpy as np dfoResults = minimize(costFun, x0, method=optMethod, options={'xtol': 1e-5, 'disp': True}) optParams = [float(k) for k in dfoResults['x']] if not printVerbose: tools.enable_console_output(OLDSTDOUT) return optParams
def load_fmu_pyfmi(fmuFile, logFile, printVerbose=False): if not printVerbose: OLDSTDOUT = tools.disable_console_output() from pyfmi import load_fmu print "Loading " + fmuFile print "" FMUMODEL = load_fmu(fmuFile, log_file_name=logFile) if not printVerbose: tools.enable_console_output(OLDSTDOUT) return FMUMODEL
def simulate_fmu(FMUMODEL, fmuOpts, startTime, finalTime, controlInput=False, printVerbose=False): if not printVerbose: OLDSTDOUT = tools.disable_console_output() if controlInput: fmuResults = FMUMODEL.simulate(input=controlInput, options=fmuOpts, start_time=startTime, final_time=finalTime) else: fmuResults = FMUMODEL.simulate(options=fmuOpts, start_time=startTime, final_time=finalTime) if not printVerbose: tools.enable_console_output(OLDSTDOUT) return (FMUMODEL, fmuResults)
def compile_fmu_openmodelica(modelicaClass, modelicaFile, saveDir, printVerbose=False): if not printVerbose: OLDSTDOUT = tools.disable_console_output() print "Compiling " + modelicaClass + " in" print saveDir print "" OMPython.execute("loadModel(Modelica)") OMPython.execute("loadFile(\"" + modelicaFile + "\")") OMPython.execute("translateModelFMU(" + modelicaClass + ")") fmuName = os.path.join(os.environ['MANTRA_TEMP'], modelicaClass.replace('.', '_')+'.fmu') if not printVerbose: tools.enable_console_output(OLDSTDOUT) return fmuName
def run_tracking_experiment(elementModel, modelicaFile): global fmuOpts global FPSCLOCK global DISPLAYSURF global JOYOBJECT global FMUMODEL global u_hist global y_hist # Calculate variables for preview circles calculate_preview_parameters() # Compile and initialize controlled element dynamic model logFile = os.path.join(tempDir, elementModel.replace('.', '_') + '_log.txt') resultsFile = os.path.join(tempDir, elementModel.replace('.', '_') + '_results.txt') fmuOpts = fmi.set_fmu_options(False, resultsFile, stepTime, solverName='CVode') fmuName = fmi.compile_fmu_openmodelica(elementModel, modelicaFile, saveDir=tempDir) FMUMODEL = fmi.load_fmu_pyfmi(fmuName, logFile) FMUMODEL.initialize() # Calculate input and display gains calculate_gains(elementModel=elementModel, saveDir=tempDir) # Look for joystick input device pygame.init() JOYOBJECT = get_input_device() # Allow user to initiate experiment raw_input("Press 'Enter' to bring up the display, then press any key except 'q' to start the experiment.\n") # Start up pygame window FPSCLOCK = pygame.time.Clock() DISPLAYSURF = pygame.display.set_mode((winWidth, winHeight)) pygame.display.set_caption('Python manual tracking (pymantra) task') # Run each frame of tracking experiment clear_tracking_display() import time a = time.time() OLDSTDOUT = tools.disable_console_output() for frame_current in frame_hist[0:-1]: run_one_frame(frame_current) tools.enable_console_output(OLDSTDOUT) print time.time() - a exit_tracking_program() return (u_hist, y_hist)