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

com.farao_community.farao.cse.data.cnec.CnecUtil Maven / Gradle / Ivy

There is a newer version: 1.38.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.farao_community.farao.cse.data.cnec;

import com.powsybl.openrao.commons.Unit;
import com.farao_community.farao.cse.data.CseDataException;
import com.powsybl.openrao.data.cracapi.Crac;
import com.powsybl.openrao.data.cracapi.cnec.Cnec;
import com.powsybl.openrao.data.cracapi.cnec.FlowCnec;
import com.powsybl.openrao.data.raoresultapi.RaoResult;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Mohamed BenRejeb {@literal }
 */
public final class CnecUtil {

    private CnecUtil() {
    }

    public static FlowCnec getWorstCnec(Crac crac, RaoResult raoResult) {
        double worstMargin = Double.MAX_VALUE;
        Optional worstCnec = Optional.empty();
        Set optimizedFlowCnecs = crac.getFlowCnecs().stream().filter(Cnec::isOptimized).collect(Collectors.toSet());
        for (FlowCnec flowCnec : optimizedFlowCnecs) {
            double margin = computeFlowMargin(flowCnec, raoResult);
            if (margin < worstMargin) {
                worstMargin = margin;
                worstCnec = Optional.of(flowCnec);
            }
        }
        return worstCnec.orElseThrow(() -> new CseDataException("Exception occurred while retrieving the most limiting element."));
    }

    private static double computeFlowMargin(FlowCnec flowCnec, RaoResult raoResult) {
        return raoResult.getMargin(flowCnec.getState().getInstant(), flowCnec, Unit.AMPERE);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy