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

com.powsybl.openrao.searchtreerao.commons.adapter.BranchResultAdapterImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2021, 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.openrao.searchtreerao.commons.adapter;

import com.powsybl.iidm.network.Network;
import com.powsybl.openrao.data.crac.api.cnec.FlowCnec;
import com.powsybl.iidm.network.TwoSides;
import com.powsybl.openrao.loopflowcomputation.LoopFlowComputation;
import com.powsybl.openrao.searchtreerao.commons.AbsolutePtdfSumsComputation;
import com.powsybl.openrao.searchtreerao.result.api.FlowResult;
import com.powsybl.openrao.searchtreerao.result.impl.EmptyFlowResultImpl;
import com.powsybl.openrao.searchtreerao.result.impl.FlowResultImpl;
import com.powsybl.openrao.sensitivityanalysis.SystematicSensitivityResult;

import java.util.Map;
import java.util.Set;

/**
 * @author Joris Mancini {@literal }
 */
public final class BranchResultAdapterImpl implements BranchResultAdapter {
    private FlowResult fixedPtdfs = new EmptyFlowResultImpl();
    private AbsolutePtdfSumsComputation absolutePtdfSumsComputation;
    private Set flowCnecs;
    private FlowResult fixedCommercialFlows = new EmptyFlowResultImpl();
    private LoopFlowComputation loopFlowComputation;
    private Set loopFlowCnecs;

    private BranchResultAdapterImpl() {
        // Should not be used
    }

    public static BranchResultAdpaterBuilder create() {
        return new BranchResultAdpaterBuilder();
    }

    @Override
    public FlowResult getResult(SystematicSensitivityResult systematicSensitivityResult, Network network) {
        FlowResult ptdfs = null;
        Map> ptdfsMap = null;
        if (absolutePtdfSumsComputation != null) {
            ptdfsMap = absolutePtdfSumsComputation.computeAbsolutePtdfSums(flowCnecs, systematicSensitivityResult);
        } else {
            ptdfs = fixedPtdfs;
        }

        FlowResult commercialFlows = null;
        Map> commercialFlowsMap = null;
        if (loopFlowComputation != null) {
            commercialFlowsMap = loopFlowComputation.buildLoopFlowsFromReferenceFlowAndPtdf(
                    systematicSensitivityResult,
                    loopFlowCnecs,
                    network
            ).getCommercialFlowsMap();
        } else {
            commercialFlows = fixedCommercialFlows;
        }
        return new FlowResultImpl(systematicSensitivityResult, commercialFlowsMap, commercialFlows, ptdfsMap, ptdfs);
    }

    public static final class BranchResultAdpaterBuilder {
        private FlowResult fixedPtdfs = new EmptyFlowResultImpl();
        private AbsolutePtdfSumsComputation absolutePtdfSumsComputation;
        private Set cnecs;
        private FlowResult fixedCommercialFlows = new EmptyFlowResultImpl();
        private LoopFlowComputation loopFlowComputation;
        private Set loopFlowCnecs;

        public BranchResultAdpaterBuilder withPtdfsResults(FlowResult fixedPtdfs) {
            this.fixedPtdfs = fixedPtdfs;
            return this;
        }

        public BranchResultAdpaterBuilder withPtdfsResults(AbsolutePtdfSumsComputation absolutePtdfSumsComputation, Set cnecs) {
            this.absolutePtdfSumsComputation = absolutePtdfSumsComputation;
            this.cnecs = cnecs;
            return this;
        }

        public BranchResultAdpaterBuilder withCommercialFlowsResults(FlowResult fixedCommercialFlows) {
            this.fixedCommercialFlows = fixedCommercialFlows;
            return this;
        }

        public BranchResultAdpaterBuilder withCommercialFlowsResults(LoopFlowComputation loopFlowComputation, Set loopFlowCnecs) {
            this.loopFlowComputation = loopFlowComputation;
            this.loopFlowCnecs = loopFlowCnecs;
            return this;
        }

        public BranchResultAdapterImpl build() {
            BranchResultAdapterImpl adapter = new BranchResultAdapterImpl();
            adapter.fixedPtdfs = fixedPtdfs;
            adapter.absolutePtdfSumsComputation = absolutePtdfSumsComputation;
            adapter.flowCnecs = cnecs;
            adapter.fixedCommercialFlows = fixedCommercialFlows;
            adapter.loopFlowComputation = loopFlowComputation;
            adapter.loopFlowCnecs = loopFlowCnecs;
            return adapter;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy