TwoWindingsTransformerAdderImpl.java

/**
 * Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium)
 * 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/.
 * SPDX-License-Identifier: MPL-2.0
 */
package com.powsybl.iidm.network.impl;

import com.powsybl.iidm.network.*;

import static com.powsybl.iidm.network.util.LoadingLimitsUtil.copyOperationalLimits;

/**
 * @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
 */
class TwoWindingsTransformerAdderImpl extends AbstractBranchAdder<TwoWindingsTransformerAdderImpl> implements TwoWindingsTransformerAdder {

    private final SubstationImpl substation;

    private final TwoWindingsTransformer copiedTwoWindingsTransformer;

    private double r = Double.NaN;

    private double x = Double.NaN;

    private double g = 0.0;

    private double b = 0.0;

    private double ratedU1 = Double.NaN;

    private double ratedU2 = Double.NaN;

    private double ratedS = Double.NaN;

    TwoWindingsTransformerAdderImpl(SubstationImpl substation) {
        this.substation = substation;
        this.copiedTwoWindingsTransformer = null;
    }

    TwoWindingsTransformerAdderImpl(SubstationImpl substation, TwoWindingsTransformer copiedTwoWindingsTransformer) {
        this.substation = substation;
        this.copiedTwoWindingsTransformer = copiedTwoWindingsTransformer;
        TwoWindingsTransformerAdder.fillTwoWindingsTransformerAdder(this, copiedTwoWindingsTransformer);
    }

    @Override
    protected NetworkImpl getNetwork() {
        return substation.getNetwork();
    }

    @Override
    protected String getTypeDescription() {
        return "2 windings transformer";
    }

    @Override
    public TwoWindingsTransformerAdder setR(double r) {
        this.r = r;
        return this;
    }

    @Override
    public TwoWindingsTransformerAdder setX(double x) {
        this.x = x;
        return this;
    }

    @Override
    public TwoWindingsTransformerAdder setB(double b) {
        this.b = b;
        return this;
    }

    @Override
    public TwoWindingsTransformerAdder setG(double g) {
        this.g = g;
        return this;
    }

    @Override
    public TwoWindingsTransformerAdder setRatedU1(double ratedU1) {
        this.ratedU1 = ratedU1;
        return this;
    }

    @Override
    public TwoWindingsTransformerAdder setRatedU2(double ratedU2) {
        this.ratedU2 = ratedU2;
        return this;
    }

    @Override
    public TwoWindingsTransformerAdder setRatedS(double ratedS) {
        this.ratedS = ratedS;
        return this;
    }

    @Override
    public TwoWindingsTransformer add() {
        String id = checkAndGetUniqueId();
        checkConnectableBuses();
        VoltageLevelExt voltageLevel1 = checkAndGetVoltageLevel1();
        VoltageLevelExt voltageLevel2 = checkAndGetVoltageLevel2();
        if (voltageLevel1.getSubstation().map(s -> s != substation).orElse(true) || voltageLevel2.getSubstation().map(s -> s != substation).orElse(true)) {
            throw new ValidationException(this,
                    "the 2 windings of the transformer shall belong to the substation '"
                            + substation.getId() + "' ('" + voltageLevel1.getSubstation().map(Substation::getId).orElse("null") + "', '"
                            + voltageLevel2.getSubstation().map(Substation::getId).orElse("null") + "')");
        }
        if (Double.isNaN(ratedU1)) {
            ratedU1 = voltageLevel1.getNominalV();
        }
        if (Double.isNaN(ratedU2)) {
            ratedU2 = voltageLevel2.getNominalV();
        }
        TerminalExt terminal1 = checkAndGetTerminal1();
        TerminalExt terminal2 = checkAndGetTerminal2();

        ValidationUtil.checkR(this, r);
        ValidationUtil.checkX(this, x);
        ValidationUtil.checkG(this, g);
        ValidationUtil.checkB(this, b);
        ValidationUtil.checkRatedU1(this, ratedU1);
        ValidationUtil.checkRatedU2(this, ratedU2);
        ValidationUtil.checkRatedS(this, ratedS);

        TwoWindingsTransformerImpl transformer
                = new TwoWindingsTransformerImpl(substation.getNetworkRef(), id, getName(), isFictitious(),
                (SubstationImpl) voltageLevel1.getSubstation().orElse(null),
                r, x, g, b,
                ratedU1, ratedU2, ratedS);
        transformer.addTerminal(terminal1);
        transformer.addTerminal(terminal2);

        copyOperationalLimits(copiedTwoWindingsTransformer, transformer);

        // check that the two windings transformer is attachable on both side
        voltageLevel1.getTopologyModel().attach(terminal1, true);
        voltageLevel2.getTopologyModel().attach(terminal2, true);

        voltageLevel1.getTopologyModel().attach(terminal1, false);
        voltageLevel2.getTopologyModel().attach(terminal2, false);
        getNetwork().getIndex().checkAndAdd(transformer);
        getNetwork().getListeners().notifyCreation(transformer);
        return transformer;
    }
}