![JAR search and dependency download from the Maven repository](/logo.png)
com.powsybl.openrao.data.cracimpl.InjectionRangeActionImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of open-rao-crac-impl Show documentation
Show all versions of open-rao-crac-impl Show documentation
Object model for CRAC implementation
/*
* 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.data.cracimpl;
import com.powsybl.action.GeneratorActionBuilder;
import com.powsybl.action.LoadActionBuilder;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.data.cracapi.NetworkElement;
import com.powsybl.openrao.data.cracapi.range.StandardRange;
import com.powsybl.openrao.data.cracapi.rangeaction.InjectionRangeAction;
import com.powsybl.openrao.data.cracapi.usagerule.UsageRule;
import com.powsybl.iidm.network.Generator;
import com.powsybl.iidm.network.Load;
import com.powsybl.iidm.network.Network;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author Baptiste Seguinot {@literal }
*/
public class InjectionRangeActionImpl extends AbstractRangeAction implements InjectionRangeAction {
private final Map injectionDistributionKeys;
private final List ranges;
private final double initialSetpoint;
InjectionRangeActionImpl(String id, String name, String operator, String groupId, Set usageRules,
List ranges, double initialSetpoint, Map injectionDistributionKeys, Integer speed) {
super(id, name, operator, usageRules, groupId, speed);
this.ranges = ranges;
this.initialSetpoint = initialSetpoint;
this.injectionDistributionKeys = injectionDistributionKeys;
}
@Override
public Set getNetworkElements() {
return injectionDistributionKeys.keySet();
}
@Override
public Map getInjectionDistributionKeys() {
return injectionDistributionKeys;
}
@Override
public List getRanges() {
return ranges;
}
@Override
public double getMinAdmissibleSetpoint(double previousInstantSetPoint) {
return StandardRangeActionUtils.getMinAdmissibleSetpoint(previousInstantSetPoint, ranges, initialSetpoint);
}
@Override
public double getMaxAdmissibleSetpoint(double previousInstantSetPoint) {
return StandardRangeActionUtils.getMaxAdmissibleSetpoint(previousInstantSetPoint, ranges, initialSetpoint);
}
@Override
public double getInitialSetpoint() {
return initialSetpoint;
}
@Override
public void apply(Network network, double targetSetpoint) {
injectionDistributionKeys.forEach((ne, sk) -> applyInjection(network, ne.getId(), targetSetpoint * sk));
}
private void applyInjection(Network network, String injectionId, double targetSetpoint) {
Generator generator = network.getGenerator(injectionId);
if (generator != null) {
new GeneratorActionBuilder()
.withId("id")
.withGeneratorId(injectionId)
.withActivePowerRelativeValue(false)
.withActivePowerValue(targetSetpoint)
.build()
.toModification()
.apply(network, true, ReportNode.NO_OP);
return;
}
Load load = network.getLoad(injectionId);
if (load != null) {
new LoadActionBuilder()
.withId("id")
.withLoadId(injectionId)
.withRelativeValue(false)
.withActivePowerValue(-targetSetpoint)
.build()
.toModification()
.apply(network, true, ReportNode.NO_OP);
return;
}
if (network.getIdentifiable(injectionId) == null) {
throw new OpenRaoException(String.format("Injection %s not found in network", injectionId));
} else {
throw new OpenRaoException(String.format("%s refers to an object of the network which is not an handled Injection (not a Load, not a Generator)", injectionId));
}
}
@Override
public double getCurrentSetpoint(Network network) {
List currentSetpoints = injectionDistributionKeys.entrySet().stream()
.map(entry -> getInjectionSetpoint(network, entry.getKey().getId(), entry.getValue()))
.collect(Collectors.toList());
if (currentSetpoints.size() == 1) {
return currentSetpoints.get(0);
} else {
Collections.sort(currentSetpoints);
if (Math.abs(currentSetpoints.get(0) - currentSetpoints.get(currentSetpoints.size() - 1)) < 1) {
return currentSetpoints.get(0);
} else {
throw new OpenRaoException(String.format("Cannot evaluate reference setpoint of InjectionRangeAction %s, as the injections are not distributed according to their key", this.getId()));
}
}
}
public double getInjectionSetpoint(Network network, String injectionId, double distributionKey) {
Generator generator = network.getGenerator(injectionId);
if (generator != null) {
return generator.getTargetP() / distributionKey;
}
Load load = network.getLoad(injectionId);
if (load != null) {
return -load.getP0() / distributionKey;
}
if (network.getIdentifiable(injectionId) == null) {
throw new OpenRaoException(String.format("Injection %s not found in network", injectionId));
} else {
throw new OpenRaoException(String.format("%s refers to an object of the network which is not an handled Injection (not a Load, not a Generator)", injectionId));
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
return this.injectionDistributionKeys.equals(((InjectionRangeAction) o).getInjectionDistributionKeys())
&& this.ranges.equals(((InjectionRangeAction) o).getRanges());
}
@Override
public int hashCode() {
int hashCode = super.hashCode();
for (StandardRange range : ranges) {
hashCode += 31 * range.hashCode();
}
hashCode += 31 * injectionDistributionKeys.hashCode();
return hashCode;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy