def kennfeld_simulieren(mp, Root_simulationsergebnisse, model_kennfeld_path, model_kennfeld_name): dymola = None try: #erzeuge Instanz von Dymola dymola = DymolaInterface() print(model_kennfeld_path, model_kennfeld_name) #öffne das Model dymola.openModel(path=model_kennfeld_path) dymola.translateModel(problem=model_kennfeld_name) print('simulieren') result = dymola.simulateExtendedModel( problem=model_kennfeld_name, stopTime=200000, method='radau IIa', resultFile=Root_simulationsergebnisse + '\simulationsergebnis_mp' + str(mp)) print(result[0]) if not result: print("Simulation failed. Below is the translation log.") log = dymola.getLastError() print(log) except DymolaException as ex: print("Error: " + str(ex)) if dymola is not None: dymola.close() dymola = None return
def simulate_optimal(ladephasen, entladephasen, simulationsdauer, dir_laden_daten, dir_modell_sim, name_modell_sim, Root_simulationsergebnisse): ladephasen = clear_phasen(ladephasen) entladephasen = clear_phasen(entladephasen) input_laden = merge(ladephasen, dir_laden_daten) input_entladen = merge(entladephasen, dir_laden_daten) try: #erzeuge Instanz von Dymola dymola = DymolaInterface() dymola.openModel(path=dir_modell_sim) dymola.translateModel(problem=name_modell_sim) dymola.ExecuteCommand("laden.table=" + input_laden) dymola.ExecuteCommand("entladen.table=" + input_entladen) result = dymola.simulateExtendedModel(problem=name_modell_sim, stopTime=simulationsdauer * 3600, method='radau IIa', finalNames=['Stromkosten.y'], resultFile=os.path.join( Root_simulationsergebnisse, 'simulation_optimal')) print(result[0]) Stromkosten = result[1] print('optimale Stromkosten', Stromkosten[0]) if not result: print("Simulation failed. Below is the translation log.") log = dymola.getLastError() print(log) dymola.plot(["elektrische_leistungsaufnahme.y"]) dymola.ExportPlotAsImage( os.path.join(Root_simulationsergebnisse, "Leistungsaufnahme_optimal.png")) dymola.plot(["__ctt__strompreis.y[1]"]) dymola.ExportPlotAsImage( os.path.join(Root_simulationsergebnisse, "Strompreis.png")) dymola.plot(["cost_calculation1.out_kkm1_drehzahl_min_unterschritten"]) dymola.ExportPlotAsImage( os.path.join(Root_simulationsergebnisse, "Drehzahl_unterschritten.png")) dymola.plot(["cost_calculation1.out_Investitionskosten_kkm1"]) dymola.ExportPlotAsImage( os.path.join(Root_simulationsergebnisse, "Invest_KKM.png")) dymola.plot(["cost_calculation1.out_Investitionskosten_Speicher"]) dymola.ExportPlotAsImage( os.path.join(Root_simulationsergebnisse, "Invest_Speicher.png")) except DymolaException as ex: print("Error: " + str(ex)) if dymola is not None: dymola.close() dymola = None return input_laden, input_entladen, ladephasen, entladephasen
def simulation(paths, models, times, A, t, htc, modelon, lcl, dir_save): modelon = modelon lcl = lcl A = A t = t htc = htc dir_save = dir_save for pat, model, time in zip(paths, models, times): dymola = None try: # Instantiate the Dymola interface and start Dymola dymola = DymolaInterface() # Loading libs dymola.openModel(path=modelon) dymola.openModel(path=lcl) # Load model dymola.openModel(path=pat) dymola.translateModel(model) # Call a function in Dymola and check its return value result = dymola.simulateExtendedModel( problem=model, startTime=0, stopTime=time, outputInterval=0.5, method="Esdirk45a", initialNames=["A", "T", "htc"], initialValues=[A, t, htc], resultFile=dir_save + "\\" + model) # result = dymola.simulateModel("C_Two_M_and_I", startTime=0, stopTime=t_stop, resultFile=dir_path + "_result") if not result: print("Simulation failed. Below is the translation log.") log = dymola.getLastErrorLog() print(log) # dymola.plot(["MotorStator.T", "Inverter_Al.T"], legends=['T_motor', 'T_inverter']) # dymola.ExportPlotAsImage(r'C:\Users\sanjio.durasevic\Desktop\C2\12_11_2018_Motor_Inverter_script\plots\slika.png') except DymolaException as ex: print("Error: " + str(ex)) finally: if dymola is not None: dymola.close() dymola = None
def simulate(): dir_aixlib = r"D:\Sciebo\Programmierung\GIT\_Standard\AixLib\AixLib" dir_result = r"D:\Sciebo\Programmierung\Tests\Dymola" dymola = DymolaInterface() try: dymola.openModel(path=os.path.join(dir_aixlib, 'package.mo')) # dymola.openModel(r"D:\Sciebo\Programmierung\Tests\Dymola\Test_BUDO.mo") dymola.translateModel( 'AixLib.Fluid.BoilerCHP.Examples.Test_BUDO.CompleteModel') output = dymola.simulateExtendedModel( problem='AixLib.Fluid.BoilerCHP.Examples.Test_BUDO.CompleteModel', startTime=0.0, stopTime=86400, outputInterval=500, method="Dassl", tolerance=0.0001, resultFile=os.path.join(dir_result, 'demo_results'), finalNames=['boiler_system.temperature_sensor_2.T'], ) print(output[1]) error = dymola.getLastError() print(error) # dymola.plot( # ["boiler_system.temperature_sensor_2.T", "combiTimeTable.y[3]"]) # dymola.ExportPlotAsImage(os.path.join(dir_result, "plot.png")) # list_var = dymola.list( # r"D:\Sciebo\Programmierung\Tests\Dymola\Test_BUDO.txt", variables='*"H02.1"*') # print(list_var) # list_var = dymola.variables( # r"D:\Sciebo\Programmierung\Tests\Dymola\Test_BUDO.txt") # print(list_var) # list_var = dymola.getExperiment() # print(list_var) # list_var = dymola.listfunctions(filter="*") # print(list_var) dymola.exit() except: print("Unexpected error:", sys.exc_info()) dymola.exit()
def simulate_compare(simulationsdauer, dir_modell_sim, name_modell_sim, Root_simulationsergebnisse): try: #erzeuge Instanz von Dymola dymola = DymolaInterface() dymola.openModel(path=dir_modell_sim) dymola.translateModel(problem=name_modell_sim) result = dymola.simulateExtendedModel(problem=name_modell_sim, stopTime=simulationsdauer * 3600, method='radau IIa', finalNames=['Stromkosten.y'], resultFile=os.path.join( Root_simulationsergebnisse, 'simulation_optimal')) print(result[0]) Stromkosten = result[1] #ergebnis[step-1,1]=Stromkosten[0]☺ print('Vergleichs-Stromkosten', Stromkosten[0]) if not result: print("Simulation failed. Below is the translation log.") log = dymola.getLastError() print(log) dymola.plot(["elektrische_leistungsaufnahme.y"]) dymola.ExportPlotAsImage( os.path.join(Root_simulationsergebnisse, "vergleichs_ergebnis.png")) except DymolaException as ex: print("Error: " + str(ex)) if dymola is not None: dymola.close() dymola = None return Stromkosten
def TranslateModel(model_path, name, position): """ Function to handle the Compilation of Modelica models inputs model_path: path of the model name: name of the subsystems position: position number of the subsystems returns: None """ global gl_model_compiled if gl_model_compiled[position - 1] == False: import os import sys global dymola """ Dymola configurations""" if dymola is None: # Work-around for the environment variable sys.path.insert( 0, os.path.join( r'C:\Program Files\Dymola 2018 FD01\Modelica\Library\python_interface\dymola.egg' )) # Import Dymola Package from dymola.dymola_interface import DymolaInterface # Start the interface dymola = DymolaInterface() """ Compilation """ # Open dymola library for lib in Init.path_lib: check1 = dymola.openModel(os.path.join(lib, 'package.mo')) print("Opening successful " + str(check1)) # Force Dymola to use 64 bit compiler dymola.ExecuteCommand("Advanced.CompileWith64=2") dymola.cd(Init.path_res + '\\' + Init.name_wkdir + '\\' + name) # Translate the model check2 = dymola.translateModel(model_path) print("Translation successful " + str(check2)) if check2 is True: gl_model_compiled[position - 1] = True
def simulate_flex(model_name, model_path, sim_duration, flex_var, param_dict): dymola = DymolaInterface() #Model muss noch geöffnet sein openModel = dymola.openModel(path=model_path) print(openModel) translateModel = dymola.translateModel(problem=model_name) print(translateModel) if flex_var == 1: #erste Ebene dymola.ExecuteCommand("konf_Bivalenz=true") dymola.ExecuteCommand("konf_HK=true") dymola.ExecuteCommand("konf_BHKW_stromgefuehrt=false") #zweite Ebene dymola.ExecuteCommand(str("konf_BHKW=" + param_dict["konf_BHKW"])) dymola.ExecuteCommand(str("konf_WRG=" + param_dict["konf_WRG"])) dymola.ExecuteCommand(str("konf_Puffer=" + param_dict["konf_Puffer"])) dymola.ExecuteCommand(str("Anzahl_HK=" + param_dict["Anzahl_HK"])) #die if-Bedingung ist nur zur Sicherheit if param_dict["konf_WRG"] == "true": dymola.ExecuteCommand(str("Anzahl_WRG=" + param_dict["Anzahl_WRG"])) else: dymola.ExecuteCommand("Anzahl_WRG=0") elif flex_var == 2: #erste Ebene dymola.ExecuteCommand("konf_BHKW=true") dymola.ExecuteCommand("konf_Bivalenz=false") dymola.ExecuteCommand("konf_BHKW_Puffer=true") dymola.ExecuteCommand("konf_BHKW_stromgefuehrt=true") #zweite Ebene dymola.ExecuteCommand(str("konf_HK=" + param_dict["konf_HK"])) dymola.ExecuteCommand(str("konf_WRG=" + param_dict["konf_WRG"])) #die if-Bedingung ist nur zur Sicherheit if param_dict["konf_HK"] == "true": dymola.ExecuteCommand(str("Anzahl_HK=" + param_dict["Anzahl_HK"])) else: dymola.ExecuteCommand("Anzahl_HK=0") #die if-Bedingung ist nur zur Sicherheit if param_dict["konf_WRG"] == "true": dymola.ExecuteCommand(str("Anzahl_WRG=" + param_dict["Anzahl_WRG"])) else: dymola.ExecuteCommand("Anzahl_WRG=0") else: print("Auswahl der Flexibilisierungsmaßnahme fehlerhaft") result = dymola.simulateExtendedModel(problem=model_name, stopTime=sim_duration, finalNames=[ "konf_Bivalenz", "konf_HK", "konf_BHKW_stromgefuehrt", "konf_BHKW", "konf_WRG", "konf_Puffer", "Anzahl_WRG", "Anzahl_HK" ]) if not result[0]: print("Simulation failed. Below is the translation log.") log = dymola.getLastError() print(log) print('ERGEBNIS_Inhalt:', "konf_Bivalenz", "konf_HK", "konf_BHKW_stromgefuehrt", "konf_BHKW", "konf_WRG", "konf_Puffer", "Anzahl_WRG", "Anzahl_HK") #saveModel=dymola.saveTotalModel('C:/Users/theisinger_l/waerme_save.mo', "waerme") #Achtung Dymola speichert mit saveTotalModel anscheinend nicht parameterwerte ab.. #print(saveModel) dymola.close() return result
import os # Import dymola package from dymola.dymola_interface import DymolaInterface # Start the interface dymola = DymolaInterface() # Location of your local AixLib clone dir_aixlib = r"C:\Users\hinack\Dropbox\09_Modelica_Library\AixLib" # Location where to store the results dir_result = r"C:\Users\hinack\Dropbox\18_ModelManagement" # Open AixLib dymola.openModel(path=os.path.join(dir_aixlib, 'package.mo')) # Translate any model you'd like to simulate dymola.translateModel('AixLib.ThermalZones.ReducedOrder.Examples.ThermalZone') # Simulate the model output = dymola.simulateExtendedModel( problem='AixLib.ThermalZones.ReducedOrder.Examples.ThermalZone', startTime=0.0, stopTime=3.1536e+07, outputInterval=3600, method="Dassl", tolerance=0.0001, resultFile=os.path.join(dir_result, 'demo_results'), finalNames=['thermalZone.TAir'], ) dymola.close()
def optimierung(liste_minima, liste_maxima, liste_ladephasen, array_names, simulationsdauer, dir_laden_daten, dir_modell_sim, name_modell_sim, Root_simulationsergebnisse, ergebnis_compare): Zyklenanzahl = len(liste_ladephasen) Zyklus = 1 liste_entladephasen = np.zeros(np.shape(liste_ladephasen)) #ein Zyklus bedeutet die Zeitdauer von Anfang eines Minimum bis zum Ende des darauffolgenden Maximums while Zyklus < Zyklenanzahl + 1: print('Aktueller Strompreiszyklus in dem Optimiert wird:', Zyklus) SOC = get_SOC(array_names, liste_ladephasen, Zyklus) entladedauer = get_entladedauer(array_names, SOC, Zyklus) ladedauer = reverse_ladedauer(array_names, SOC, Zyklus) print('SOC', SOC) print('entladedauer', entladedauer) print('ladedauer', ladedauer) liste_ladephasen[Zyklus - 1, 0] = liste_minima[Zyklus - 1] - (ladedauer / (2 * 3600)) liste_ladephasen[Zyklus - 1, 1] = liste_minima[Zyklus - 1] + (ladedauer / (2 * 3600)) print('liste_ladephasen', liste_ladephasen) liste_entladephasen[Zyklus - 1, 0] = liste_maxima[Zyklus - 1] - (entladedauer / (3 * 3600)) # &&& liste_entladephasen[Zyklus - 1, 1] = liste_maxima[Zyklus - 1] + (2 * entladedauer / (3 * 3600)) # &&& print('liste_entladephasen', liste_entladephasen) correct_left_result = correct_left(liste_ladephasen, liste_entladephasen, Zyklus, liste_minima) liste_ladephasen[Zyklus - 1, :] = correct_left_result[0][Zyklus - 1, :] liste_entladephasen[Zyklus - 1, :] = correct_left_result[1][Zyklus - 1, :] print('liste_phasen nach correct left', liste_ladephasen, liste_entladephasen) correct_right_result = correct_right(liste_ladephasen, liste_entladephasen, Zyklus, liste_maxima, liste_minima, array_names) liste_ladephasen[Zyklus - 1, :] = correct_right_result[0][Zyklus - 1, :] liste_entladephasen[Zyklus - 1, :] = correct_right_result[1][Zyklus - 1, :] print('liste_phasen nach correct right', liste_ladephasen, liste_entladephasen) ergebnis = np.array([[0, 0]]) ladedauer_neu = liste_ladephasen[Zyklus - 1, 1] - liste_ladephasen[Zyklus - 1, 0] print('ladedauer_neu_check', ladedauer_neu) better = True opt = 1 #Nummer des Optimierungsdurchgangs in dem jeweiligen Strompreiszyklus while better == True: #solange sich die Stromkosten verkleinern werden die Phasen verkürzt if liste_ladephasen[Zyklus - 1, 0] == 0 and liste_ladephasen[Zyklus - 1, 1] == 0: break ladephase_neu = np.array([[ liste_minima[Zyklus - 1] - (ladedauer_neu / (2)), liste_minima[Zyklus - 1] + (ladedauer_neu / (2)) ]]) SOC_neu = get_SOC(array_names, ladephase_neu, Zyklus) entladedauer_neu = get_entladedauer(array_names, SOC_neu, Zyklus) print('SOC:', SOC_neu) print('entladedauer:', entladedauer_neu) entladephase_neu = np.array([[ liste_maxima[Zyklus - 1] - (entladedauer_neu / (3 * 3600)), liste_maxima[Zyklus - 1] + (2 * entladedauer_neu / (3 * 3600)) ]]) # &&& print('ladephase die getestet wird:', ladephase_neu) print('entladephase die getestet wird:', entladephase_neu) input_laden = merge(ladephase_neu, dir_laden_daten) input_entladen = merge(entladephase_neu, dir_laden_daten) try: #erzeuge Instanz von Dymola dymola = DymolaInterface() dymola.openModel(path=dir_modell_sim) dymola.translateModel(problem=name_modell_sim) dymola.ExecuteCommand("laden.table=" + input_laden) dymola.ExecuteCommand("entladen.table=" + input_entladen) result = dymola.simulateExtendedModel( problem=name_modell_sim, stopTime=simulationsdauer * 3600, method='radau IIa', finalNames=['Stromkosten.y'], resultFile=os.path.join(Root_simulationsergebnisse, 'optimization_test')) Stromkosten = result[1] column = np.array([ladedauer_neu, Stromkosten[0]]) ergebnis = np.vstack((ergebnis, column)) print('aktuelle Stromkosten', Stromkosten[0]) if not result: print("Simulation failed. Below is the translation log.") log = dymola.getLastError() print(log) dymola.plot(["elektrische_leistungsaufnahme.y"]) dymola.ExportPlotAsImage( os.path.join( Root_simulationsergebnisse, "Ergebnis_von" + str(Zyklus) + str(opt) + ".png")) except DymolaException as ex: print("Error: " + str(ex)) if dymola is not None: dymola.close() dymola = None print( 'Optimierungsdurchgang der in dem Entsprechenden Strompreiszyklus durchgeführt wurde', opt) if opt > 1: if ergebnis[opt, 1] < ergebnis[opt - 1, 1]: better = True else: better = False else: better = True print('better:', better) if better == True: liste_ladephasen[Zyklus - 1, :] = ladephase_neu liste_entladephasen[Zyklus - 1, :] = entladephase_neu ladedauer_neu = ladedauer_neu * 0.9 #ladedauer wird um 10% verkürzt opt = opt + 1 if ergebnis[opt - 2, 1] > ergebnis_compare: print('hier_ergebnis', ergebnis) print('hier_ladeph', liste_ladephasen) print('hier_opt', opt) liste_ladephasen[Zyklus - 1, :] = np.zeros((1, 2)) liste_entladephasen[Zyklus - 1, :] = np.zeros((1, 2)) print('hier_ladeph', liste_ladephasen) Zyklus = Zyklus + 1 print('ergebnis_tabelle des Zyklusses', ergebnis) #&&& hier einfügen, dass eine Lade/und Entladephase nur verwendet wird, wenn das ergebnis besser als das vergleichsergebnis ist return liste_ladephasen, liste_entladephasen
def simulate(simSettings, showWindow=False, closeWindow=True, simID='', seed=0, singleTranslate=True): ''' simSettings => dictionary of setting parameters (see below for details) showWindow => =True to launch Dymola GUI closeWindow => =False to prevent auto-closing of the Dymola GUI when done simID => simulation ID to differentiate output files (e.g., simID_dsres0.mat vs. dsres0.mat) seed => starting seed value for output file naming (e.g., seed+0, ..., seed+len(experiments)) singleTranslate => =True to only translate the model once - !!! If variables needed to be changed require retranslation then add them to problem name. i.e., simSettings['problem']=['Example.Test(var1=1,var2=5)'] or try to set 'annotation(Evaluate=false)' in the model simSettings details: - All settings, besides `=None`, must be enclosed in brackets [] - !!! 'initialNames' and 'initialValues' are set different than others. Specify each variable indepenently. The script will generate the tests and collapse all 'non-standard' keys. - 'problem', 'resultFile', and 'autoLoad' only support 1 or None entries - Only specify 'numberOfIntervals' or 'outputInterval', not both - 'fixedstepsize' is only for fixed step size solvers - showWindow=True and closeWindow=False only a specified number of simulations are retained in memory according, not all simulations. - The experiments generated are a list of dictionaries which are saved as experiments.pickle in the working directory of the Modelica simulation (cwdMOD). To open the pickle in python: with open(os.path.join('PATHTOPICKLE,'experiments.pickle'), 'rb') as handle: experiments = pickle.load(handle) - !!! Settings not specified assume Dymola default settings. Any simulation settings (tolerance, solver, etc.) located in the model are ignored. Default Settings: 'problem' = '' => Modelica file (.mo) to be run (e.g., ['TRANSFORM.Fluid.Examples.RankineCycl'] ) 'startTime' = 0 => Start time of simulation 'stopTime' = 1 => Stop time of simulation 'numberOfIntervals' = 500 => Number of intervals to be saved to result file 'outputInterval' = 0 => Time (seconds) between intervals to be saved to result file 'method' = 'dassl' => Solver 'tolerance' = 0.0001 => Solver tolerance 'fixedstepsize' = 0 => Step size for solver (certain solvers only, e.g., Euler) 'resultFile' = 'dsres.mat' => Name of the result file 'initialNames' = '' => Names of variables in model 'initialValues' = '' => Value of variables in model (len(initialNames) = len(initialValues)) 'finalNames' = '' => Variable for Dymola to print to screen when simulation is finished 'autoLoad' = true => Automatically load (true) the result file into the Dymola plot window Possible Methods: 'methods' = Lsodar, dassl, Euler, Rkfix2, Rkfix3, Rkfix4, Esdirk23a, Esdirk34a, Esdirk45a, Dopri45, Dopri853, Sdirk34hw, Cerk23, Cerk34, Cerk34, Cvode For the current supported solvers and their use see Dymola ''' # Check User Input checkInput(simSettings) try: if int(seed) >= 0: seed = int(seed) else: raise NameError("seedNum must be a positive integer") except: raise NameError("seedNum must be a positive integer") # Modify simID for naminc conventions if simID != '': simID = simID + '_' # Generate all experiment permutations experimentsRaw = genExperimentsRaw(simSettings) # Filter experiments to generate key/value pairs for 'initialNames' and 'initialValues' experiments = genExperiments(experimentsRaw) # Instantiate the Dymola interface and start Dymola dymola = None result_tran = False try: # Open Dymola dymola = DymolaInterface(showwindow=showWindow) # Get working directory cwdMod = dymola.ExecuteCommand( 'Modelica.Utilities.System.getWorkDirectory();') # Translate the model if singleTranslate: result_tran = dymola.translateModel(experiments[0]['problem']) if not result_tran: raise Exception( "Translation failed. Aborting parametric simulation. Investigate model in IDE for details." ) # Run all experiments saveResult = [] j = seed print('Total experiments = {} for simID = "{}"'.format( len(experiments), simID)) print('Experiment numbering started at seed = {}'.format(seed)) for i, value in enumerate(experiments): j = seed + i print('Experiment {} (= {}/{})'.format(j, i + 1, len(experiments))) print(value) # ModelTranslate = "Dymola.Path.To.Your.Model(Dymola.Path.to.Variable=1000)" if not singleTranslate: result_tran = False result_tran = dymola.translateModel(value['problem']) if not result_tran: print( "Translation failed. Aborting parametric simulation. Investigate model in IDE for details." ) break # Simulate the model result = dymola.simulateExtendedModel(**value)[0] # Save the result (success/fail) saveResult.append(result) # Rename the log files and return new log file for debugging ref. dslogNew = renameFiles(simID, j, value, cwdMod, result) # Print last line of error if not result: print( "Simulation failed. Below is the translation log. For more details see: {}" .format(dslogNew)) log = dymola.getLastErrorLog() print('### Log Start ###\n' + log + '### Log End ###') except DymolaException as ex: print(("Error: " + str(ex))) except Exception as inst: print('{}'.format(inst.message)) finally: if dymola is not None: if showWindow == True and closeWindow == False: pass else: dymola.close() if result_tran: # Save experiment dictionary as pickle in cwdMod with open( os.path.join( cwdMod, '{}experiments_{}to{}.pickle'.format(simID, seed, j)), 'wb') as handle: pickle.dump(experiments, handle, protocol=pickle.HIGHEST_PROTOCOL) # Save summary off success/fail (true/false) of simulations with open( os.path.join(cwdMod, '{}summary_{}to{}.txt'.format(simID, seed, j)), 'w') as fil: fil.write('Summary of success/fail (true/false) of experiments\n') for i, val in enumerate(saveResult): fil.write('\t'.join( ['Experiment', '{}'.format(i), '{}'.format(val)]) + '\n')