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

com.powsybl.metrix.integration.MetrixTimeSeriesVariantProvider Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020, 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/.
 * SPDX-License-Identifier: MPL-2.0
 */
package com.powsybl.metrix.integration;

import com.google.common.collect.Range;
import com.powsybl.commons.datasource.DataSource;
import com.powsybl.commons.datasource.DataSourceUtil;
import com.powsybl.contingency.ContingenciesProvider;
import com.powsybl.iidm.network.Identifiable;
import com.powsybl.iidm.network.Network;
import com.powsybl.metrix.integration.contingency.Probability;
import com.powsybl.metrix.integration.metrix.MetrixChunkParam;
import com.powsybl.metrix.integration.timeseries.InitOptimizedTimeSeriesWriter;
import com.powsybl.metrix.mapping.*;
import com.powsybl.timeseries.ReadOnlyTimeSeriesStore;
import com.powsybl.timeseries.TimeSeriesIndex;
import com.powsybl.timeseries.TimeSeriesTable;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;

import static com.powsybl.metrix.integration.timeseries.InitOptimizedTimeSeriesWriter.INPUT_OPTIMIZED_FILE_NAME;

/**
 * @author Paul Bui-Quang {@literal }
 */
public class MetrixTimeSeriesVariantProvider implements MetrixVariantProvider {

    private static final Set METRIX_EQUIPMENT_VARIABLES = EnumSet.of(EquipmentVariable.TARGET_P,
            EquipmentVariable.MIN_P,
            EquipmentVariable.MAX_P,
            EquipmentVariable.ACTIVE_POWER_SETPOINT,
            EquipmentVariable.P0,
            EquipmentVariable.FIXED_ACTIVE_POWER,
            EquipmentVariable.VARIABLE_ACTIVE_POWER,
            EquipmentVariable.PHASE_TAP_POSITION,
            EquipmentVariable.OPEN,
            EquipmentVariable.DISCONNECTED);

    public static boolean isMetrixVariable(MappingVariable variable) {
        if (variable instanceof MetrixVariable) {
            return true;
        } else if (variable instanceof EquipmentVariable) {
            return METRIX_EQUIPMENT_VARIABLES.contains(variable);
        }
        return false;
    }

    private final Network network;

    private final ReadOnlyTimeSeriesStore store;

    private final MappingParameters mappingParameters;

    private final ContingenciesProvider contingenciesProvider;

    private final TimeSeriesMappingConfig config;

    private final MetrixDslData metrixDslData;

    private final int version;

    private final Range variantRange;

    private final boolean ignoreLimits;

    private final boolean ignoreEmptyFilter;

    private final boolean isNetworkPointComputation;

    private final PrintStream err;

    public MetrixTimeSeriesVariantProvider(Network network, ReadOnlyTimeSeriesStore store, MappingParameters mappingParameters,
                                           TimeSeriesMappingConfig config, MetrixDslData metrixDslData, MetrixChunkParam metrixChunkParam,
                                           Range variantRange, PrintStream err) {

        this.network = Objects.requireNonNull(network);
        this.store = Objects.requireNonNull(store);
        this.mappingParameters = Objects.requireNonNull(mappingParameters);
        this.config = Objects.requireNonNull(config);
        this.metrixDslData = metrixDslData;
        this.version = metrixChunkParam.version;
        this.variantRange = variantRange;
        this.ignoreLimits = metrixChunkParam.ignoreLimits;
        this.ignoreEmptyFilter = metrixChunkParam.ignoreEmptyFilter;
        this.isNetworkPointComputation = metrixChunkParam.networkPointFile != null;
        this.contingenciesProvider = metrixChunkParam.contingenciesProvider;
        this.err = Objects.requireNonNull(err);
    }

    @Override
    public Range getVariantRange() {
        return variantRange;
    }

    @Override
    public TimeSeriesIndex getIndex() {
        return new TimeSeriesMappingConfigTableLoader(config, store).checkIndexUnicity();
    }

    @Override
    public Set getMappedBreakers() {
        return config.getBreakerToTimeSeriesMapping().keySet()
                .stream()
                .map(MappingKey::getId)
                .collect(Collectors.toSet());
    }

    @Override
    public void readVariants(Range variantReadRange, MetrixVariantReader reader, Path workingDir) {
        Objects.requireNonNull(variantReadRange);
        if (!variantRange.encloses(variantReadRange)) {
            throw new IllegalArgumentException("Variant range " + variantRange + " do not enclose read range " + variantReadRange);
        }
        Objects.requireNonNull(reader);

        List observers = new ArrayList<>(1);
        observers.add(createBalanceSummary(reader));
        if (isNetworkPointComputation) {
            observers.add(createNetworkPointWriter(workingDir));
        }
        if (metrixDslData != null && (!metrixDslData.getHvdcFlowResults().isEmpty() || !metrixDslData.getPstAngleTapResults().isEmpty())) {
            observers.add(createInitOptimizedTimeSeriesWriter(workingDir, variantReadRange));
        }
        TimeSeriesMapperParameters parameters = new TimeSeriesMapperParameters(new TreeSet<>(Collections.singleton(version)), variantReadRange, ignoreLimits, ignoreEmptyFilter, !isNetworkPointComputation, getContingenciesProbabilitiesTs(), mappingParameters.getToleranceThreshold());
        TimeSeriesMapper mapper = new TimeSeriesMapper(config, parameters, network, new TimeSeriesMappingLogger());
        mapper.mapToNetwork(store, observers);
    }

    private Set getContingenciesProbabilitiesTs() {
        return contingenciesProvider.getContingencies(network)
                .stream()
                .filter(contingency -> contingency.getExtension(Probability.class) != null && contingency.getExtension(Probability.class).getProbabilityTimeSeriesRef() != null)
                .map(contingency -> contingency.getExtension(Probability.class).getProbabilityTimeSeriesRef())
                .collect(Collectors.toSet());
    }

    private TimeSeriesMapperObserver createBalanceSummary(MetrixVariantReader reader) {
        return new BalanceSummary(err) {
            @Override
            public void timeSeriesMappingStart(int point, TimeSeriesIndex index) {
                super.timeSeriesMappingStart(point, index);
                reader.onVariantStart(point);
            }

            @Override
            public void map(int version, int point, TimeSeriesTable table) {
                reader.onVariant(version, point, table);
            }

            @Override
            public void timeSeriesMappedToEquipment(int point, String timeSeriesName, Identifiable identifiable, MappingVariable variable, double equipmentValue) {
                super.timeSeriesMappedToEquipment(point, timeSeriesName, identifiable, variable, equipmentValue);
                if (isMetrixVariable(variable) && !Double.isNaN(equipmentValue)) {
                    reader.onEquipmentVariant(identifiable, variable, equipmentValue);
                }
            }

            @Override
            public void timeSeriesMappingEnd(int point, TimeSeriesIndex index, double balance) {
                super.timeSeriesMappingEnd(point, index, balance);
                reader.onVariantEnd(point);
            }
        };
    }

    protected TimeSeriesMapperObserver createNetworkPointWriter(Path workingDir) {
        Objects.requireNonNull(workingDir);
        DataSource dataSource = DataSourceUtil.createDataSource(workingDir.resolve(network.getId()), null);
        return new NetworkPointWriter(network, dataSource);
    }

    private TimeSeriesMapperObserver createInitOptimizedTimeSeriesWriter(Path workingDir, Range pointRange) {
        Objects.requireNonNull(workingDir);
        Objects.requireNonNull(pointRange);
        try {
            BufferedWriter writer = Files.newBufferedWriter(workingDir.resolve(INPUT_OPTIMIZED_FILE_NAME), StandardCharsets.UTF_8);
            return new InitOptimizedTimeSeriesWriter(network, metrixDslData, pointRange, writer);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy