Example #1
0
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
Example #2
0
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
Example #3
0
osString = platform.system()
isWindows = osString.startswith("Win")

dymola = None
try:
    # Instantiate the Dymola interface and start Dymola
    dymola = DymolaInterface()

    # Call a function in Dymola and check its return value
    result = dymola.simulateModel("Modelica.Mechanics.Rotational.Examples.CoupledClutches")
    if not result:
        print("Simulation failed. Below is the translation log.")
        log = dymola.getLastErrorLog()
        print(log)
        exit(1)

    dymola.plot(["J1.w", "J2.w", "J3.w", "J4.w"])
    if (isWindows):
        plotPath = "C:/temp/plot.png"
    else:
        plotPath = "/tmp/plot.png";
    dymola.ExportPlotAsImage(plotPath)
    print("OK")
except DymolaException as ex:
    print(("Error: " + str(ex)))
finally:
    if dymola is not None:
        dymola.close()
        dymola = None
Example #4
0
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