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

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

The 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.commons.report.ReportNode;
import com.powsybl.computation.local.LocalComputationManager;
import com.powsybl.contingency.Contingency;
import com.powsybl.contingency.ContingencyContext;
import com.powsybl.iidm.network.Branch;
import com.powsybl.iidm.network.Network;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.sensitivity.*;
import org.jgrapht.alg.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Hugo Schindler {@literal }
 */
abstract class AbstractSensitivityAnalyser {
    public static final SensitivityFunctionType SENSITIVITY_FUNCTION_TYPE = SensitivityFunctionType.BRANCH_ACTIVE_POWER_1;
    public static final List CONTINGENCIES = Collections.emptyList();
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractSensitivityAnalyser.class);
    private static final boolean DC_LOAD_FLOW = true;
    protected final SensitivityAnalysisParameters sensitivityAnalysisParameters;
    protected final SensitivityAnalysis.Runner runner;

    AbstractSensitivityAnalyser(LoadFlowParameters loadFlowParameters, SensitivityAnalysis.Runner runner) {
        this.sensitivityAnalysisParameters = initSensitivityAnalysisParameters(loadFlowParameters);
        this.runner = runner;
    }

    protected static SensitivityAnalysisParameters initSensitivityAnalysisParameters(LoadFlowParameters loadFlowParameters) {
        SensitivityAnalysisParameters parameters = SensitivityAnalysisParameters.load();
        parameters.setLoadFlowParameters(enforceDcLoadFlowCalculation(loadFlowParameters));
        LOGGER.debug("Using following sensitivity analysis parameters: {}", parameters);
        return parameters;
    }

    private static LoadFlowParameters enforceDcLoadFlowCalculation(LoadFlowParameters initialLoadFlowParameters) {
        LoadFlowParameters dcEnforcedParameters = initialLoadFlowParameters.copy();
        dcEnforcedParameters.setDc(DC_LOAD_FLOW);
        return dcEnforcedParameters;
    }

    protected List> getFunctionVariableFactors(List variableList,
                                                                    List functionList) {
        List> factors = new ArrayList<>();
        variableList.forEach(
            variable -> functionList.forEach(
                function -> factors.add(Pair.of(function.getId(), variable))));
        return factors;
    }

    protected static SensitivityFactorReader getSensitivityFactorReader(List> factors, SensitivityVariableType sensitivityVariableType, boolean sensitivityVariableSet) {
        return handler -> factors.forEach(
            pair -> handler.onFactor(SENSITIVITY_FUNCTION_TYPE,
                pair.getFirst(),
                sensitivityVariableType,
                pair.getSecond(),
                sensitivityVariableSet,
                ContingencyContext.none()));
    }

    protected void runSensitivityAnalysis(Network network, SensitivityFactorReader factorReader, SensitivityResultWriter valueWriter, List sensitivityVariableSets) {
        runner.run(network,
            network.getVariantManager().getWorkingVariantId(),
            factorReader,
            valueWriter,
            CONTINGENCIES,
            sensitivityVariableSets,
            sensitivityAnalysisParameters,
            LocalComputationManager.getDefault(),
            ReportNode.NO_OP
        );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy