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

com.powsybl.flow_decomposition.ZonalSensitivityAnalyser Maven / Gradle / Ivy

There is a newer version: 2.13.0
Show newest version
/*
 * Copyright (c) 2022, 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.powsybl.flow_decomposition;

import com.powsybl.iidm.network.Branch;
import com.powsybl.iidm.network.Country;
import com.powsybl.iidm.network.Network;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.sensitivity.SensitivityAnalysis;
import com.powsybl.sensitivity.SensitivityAnalysisResult;
import com.powsybl.sensitivity.SensitivityFactorReader;
import com.powsybl.sensitivity.SensitivityResultWriter;
import com.powsybl.sensitivity.SensitivityVariableSet;
import com.powsybl.sensitivity.SensitivityVariableType;
import com.powsybl.sensitivity.WeightedSensitivityVariable;
import org.jgrapht.alg.util.Pair;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * This class performs a Sensitivity Analysis to get zonal PTDFs.
 *
 * @author Hugo Schindler {@literal }
 */
public class ZonalSensitivityAnalyser extends AbstractSensitivityAnalyser {
    private static final boolean SENSITIVITY_VARIABLE_SET = true;

    public ZonalSensitivityAnalyser(LoadFlowParameters loadFlowParameters, SensitivityAnalysis.Runner runner) {
        super(loadFlowParameters, runner);
    }

    public Map> run(Network network,
                                                 Map> glsks,
                                                 SensitivityVariableType sensitivityVariableType) {
        List functionList = NetworkUtil.getAllValidBranches(network);
        List variableList = getVariableList(glsks);
        List sensitivityVariableSets = getSensitivityVariableSets(glsks);
        List> factors = getFunctionVariableFactors(variableList, functionList);
        return getSensitivityAnalysisResult(network,
            factors, sensitivityVariableSets, sensitivityVariableType);
    }

    private List getVariableList(Map> glsks) {
        return glsks.keySet().stream().map(Country::toString).collect(Collectors.toList());
    }

    private List getSensitivityVariableSets(Map> glsks) {
        return glsks.entrySet().stream().map(
            this::getSensitivityVariableSet).collect(Collectors.toList());
    }

    private SensitivityVariableSet getSensitivityVariableSet(Map.Entry> countryMapEntry) {
        return new SensitivityVariableSet(countryMapEntry.getKey().toString(),
            getWeighteitedSensitivityVariables(countryMapEntry.getValue()));
    }

    private List getWeighteitedSensitivityVariables(Map singleCountryGlsks) {
        return singleCountryGlsks.entrySet().stream().map(this::getWeightedSensitivityVariable).collect(Collectors.toList());
    }

    private WeightedSensitivityVariable getWeightedSensitivityVariable(Map.Entry stringDoubleEntry) {
        return new WeightedSensitivityVariable(stringDoubleEntry.getKey(), stringDoubleEntry.getValue());
    }

    private Map> getSensitivityAnalysisResult(Network network, List> factors, List sensitivityVariableSets, SensitivityVariableType sensitivityVariableType) {
        Map> zonalPtdf = new HashMap<>();
        SensitivityFactorReader factorReader = getSensitivityFactorReader(factors, sensitivityVariableType, SENSITIVITY_VARIABLE_SET);
        SensitivityResultWriter valueWriter = getSensitivityResultWriter(factors, zonalPtdf);
        runSensitivityAnalysis(network, factorReader, valueWriter, sensitivityVariableSets);
        return zonalPtdf;
    }

    private static SensitivityResultWriter getSensitivityResultWriter(List> factors, Map> zonalPtdf) {
        return new SensitivityResultWriter() {
            @Override
            public void writeSensitivityValue(int factorIndex, int contingencyIndex, double value, double functionReference) {
                Pair factor = factors.get(factorIndex);
                String branchId = factor.getFirst();
                zonalPtdf.putIfAbsent(branchId, new EnumMap<>(Country.class));
                Country country = Country.valueOf(factor.getSecond());
                zonalPtdf.get(branchId).put(country, value);
            }

            @Override
            public void writeContingencyStatus(int contingencyIndex, SensitivityAnalysisResult.Status status) {
                // We do not manage contingency yet
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy