All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.farao_community.farao.gridcapa_swe_commons.shift.CountryBalanceComputation Maven / Gradle / Ivy

There is a newer version: 1.31.1
Show newest version
/*
 * Copyright (c) 2023, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.farao_community.farao.gridcapa_swe_commons.shift;

import com.farao_community.farao.gridcapa_swe_commons.exception.SweBaseCaseUnsecureException;
import com.farao_community.farao.gridcapa_swe_commons.resource.SweEICode;
import com.powsybl.balances_adjustment.util.CountryArea;
import com.powsybl.balances_adjustment.util.CountryAreaFactory;
import com.powsybl.computation.local.LocalComputationManager;
import com.powsybl.iidm.network.Country;
import com.powsybl.iidm.network.Network;
import com.powsybl.loadflow.LoadFlow;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.loadflow.LoadFlowResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Ameni Walha {@literal }
 */
public final class CountryBalanceComputation {
    private static final Logger LOGGER = LoggerFactory.getLogger(CountryBalanceComputation.class);

    private CountryBalanceComputation() {
         // Should not be instantiated
    }

    public static Map computeSweCountriesBalances(Network network, LoadFlowParameters loadFlowParameters) {
        LOGGER.info("Computing initial SWE countries balance");
        Map countriesBalances = new HashMap<>();
        runLoadFlow(network, network.getVariantManager().getWorkingVariantId(), loadFlowParameters);
        Map bordersExchanges = computeSweBordersExchanges(network);
        countriesBalances.put(SweEICode.PT_EIC,  -bordersExchanges.get("ES_PT"));
        countriesBalances.put(SweEICode.ES_EIC, bordersExchanges.values().stream().reduce(0., Double::sum));
        countriesBalances.put(SweEICode.FR_EIC, -bordersExchanges.get("ES_FR"));

        return countriesBalances;
    }

    public static Map computeSweBordersExchanges(Network network) {
        Map borderExchanges = new HashMap<>();
        Map countryAreaPerCountry = Stream.of(Country.FR, Country.ES, Country.PT)
                .collect(Collectors.toMap(Function.identity(), country -> new CountryAreaFactory(country).create(network)));
        borderExchanges.put("ES_FR", getBorderExchange(Country.ES, Country.FR, countryAreaPerCountry));
        borderExchanges.put("ES_PT", getBorderExchange(Country.ES, Country.PT, countryAreaPerCountry));
        return borderExchanges;
    }

    private static void runLoadFlow(Network network, String workingStateId, LoadFlowParameters loadFlowParameters) {
        LoadFlowResult result = LoadFlow.run(network, workingStateId, LocalComputationManager.getDefault(), loadFlowParameters);
        if (!result.isOk()) {
            LOGGER.error("Loadflow computation diverged on network '{}'", network.getId());
            throw new SweBaseCaseUnsecureException(String.format("Loadflow computation diverged on network %s", network.getId()));
        }
    }

    private static double getBorderExchange(Country fromCountry, Country toCountry, Map countryAreaPerCountry) {
        return countryAreaPerCountry.get(fromCountry).getLeavingFlowToCountry(countryAreaPerCountry.get(toCountry));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy