Skip to content

Commit

Permalink
#810 Add the possibility to zip the inputs and outputs for N simulation
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitri Baron <[email protected]>
  • Loading branch information
barondim committed Dec 13, 2024
1 parent 901fea6 commit 08d69a0
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 13 deletions.
3 changes: 3 additions & 0 deletions etc/Dictionaries/DFLError_en_GB.dic
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
//

//------------------ Inputs -------------------------
AlreadyInitializedNetworkFileInput = network file is already initialized
AlreadyInitializedConfigFileInput = config file is already initialized
AlreadyInitializedContingenciesInput = contingencies file is already initialized
ErrorConfigFileRead = error while reading configuration file: %1%
StartingPointModeDoesntExist = starting point mode %1% doesn't exist
SettingFileWithoutAssemblingFile = %1% contains a setting file but not an assembling file
Expand Down
21 changes: 15 additions & 6 deletions scripts/envDFL.sh
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,22 @@ launch() {
if [ ! -f "$2" ]; then
error_exit "IIDM network file $2 doesn't exist"
fi
if [ ! -f "$3" ]; then
error_exit "DFL configuration file $3 doesn't exist"

if [[ "$2" == *.zip ]]; then
echo "$DYNAFLOW_LAUNCHER_INSTALL_DIR/bin/DynaFlowLauncher --log-level $DYNAFLOW_LAUNCHER_LOG_LEVEL --input-archive $2"

$DYNAFLOW_LAUNCHER_INSTALL_DIR/bin/DynaFlowLauncher \
--log-level $DYNAFLOW_LAUNCHER_LOG_LEVEL \
--input-archive $2
else
if [ ! -f "$3" ]; then
error_exit "DFL configuration file $3 doesn't exist"
fi
$DYNAFLOW_LAUNCHER_INSTALL_DIR/bin/DynaFlowLauncher \
--log-level $DYNAFLOW_LAUNCHER_LOG_LEVEL \
--network $2 \
--config $3
fi
$DYNAFLOW_LAUNCHER_INSTALL_DIR/bin/DynaFlowLauncher \
--log-level $DYNAFLOW_LAUNCHER_LOG_LEVEL \
--network $2 \
--config $3
}

launch_gdb() {
Expand Down
1 change: 1 addition & 0 deletions sources/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ target_link_libraries(dfl_Common

PRIVATE
Boost::filesystem
libZIP::libZIP
)
add_library(DynaFlowLauncher::common ALIAS dfl_Common)
install(FILES ${CMAKE_SOURCE_DIR}/etc/Dictionaries/DFLLog_en_GB.dic ${CMAKE_SOURCE_DIR}/etc/Dictionaries/DFLError_en_GB.dic DESTINATION share)
Expand Down
2 changes: 1 addition & 1 deletion sources/Common/include/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#pragma once

#include <boost/optional.hpp>
#include <boost/program_options.hpp>
#include <iostream>
#include <string>
Expand Down Expand Up @@ -44,6 +43,7 @@ class Options {
std::string contingenciesFilePath; ///< Contingencies filepath for security analysis
std::string configPath; ///< Launcher configuration filepath
std::string dynawoLogLevel; ///< chosen log level
std::string zipArchive;
};

/**
Expand Down
79 changes: 73 additions & 6 deletions sources/Common/src/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@
*/

#include "Options.h"

#include "Log.h"
#include "version.h"

#include <libzip/ZipInputStream.h>

#include <DYNFileSystemUtils.h>

#include <algorithm>
#include <boost/filesystem.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <sstream>


namespace dfl {
namespace common {

Expand Down Expand Up @@ -81,14 +88,15 @@ Options::basename(const std::string& filepath) {
return path.filename().replace_extension().generic_string();
}

Options::Options() : desc_{}, config_{"", "", "", "", defaultLogLevel_} {
Options::Options() : desc_{}, config_{"", "", "", "", defaultLogLevel_, ""} {
desc_.add_options()("help,h", "Display help message")(
"log-level", po::value<ParsedLogLevel>(),
(std::string("Dynawo logger level (allowed values are ERROR, WARN, INFO, DEBUG): default is ") + defaultLogLevel_).c_str())(
"network", po::value<std::string>(&config_.networkFilePath)->required(), "Network file path to process (IIDM support only)")(
"contingencies", po::value<std::string>(&config_.contingenciesFilePath), "Contingencies file path to process (Security Analysis)")(
"config", po::value<std::string>(&config_.configPath)->required(), "launcher Configuration file to use")("version,v", "Display version")(
"nsa", "Run steady state calculation followed by security analysis. Requires contingencies file to be defined.");
"network", po::value<std::string>(&config_.networkFilePath), "Network file path to process (IIDM support only)")(
"contingencies", po::value<std::string>(&config_.contingenciesFilePath),
"Contingencies file path to process (Security Analysis)")("config", po::value<std::string>(&config_.configPath), "launcher Configuration file to use")(
"version,v", "Display version")("nsa", "Run steady state calculation followed by security analysis. Requires contingencies file to be defined.")(
"input-archive", po::value<std::string>(&config_.zipArchive), "zip archive");
}

Options::Request
Expand All @@ -106,8 +114,67 @@ Options::parse(int argc, char* argv[]) {
return Request::VERSION;
}

int option_error = false;
if ((vm.count("network") && !vm.count("config")) || (vm.count("config") && !vm.count("network"))) {
option_error = true;
std::cout << "--network and --config must be used together" << std::endl;
}

if ((vm.count("network") || vm.count("config")) && vm.count("input-archive")) {
option_error = true;
std::cout << "--network and --config options can't be used with --input-archive" << std::endl;
}

po::notify(vm);

if (option_error) {
return Request::ERROR;
} else {
std::vector<std::string> zipArchiveFiles;
boost::shared_ptr<zip::ZipFile> archive = zip::ZipInputStream::read(config_.zipArchive);
std::string archiveParentPath = boost::filesystem::path(config_.zipArchive).parent_path().string();
for (std::map<std::string, boost::shared_ptr<zip::ZipEntry>>::const_iterator itE = archive->getEntries().begin(); itE != archive->getEntries().end();
++itE) {
std::string name = itE->first;
std::string data(itE->second->getData());
std::ofstream file;
std::string filepath = createAbsolutePath(name, archiveParentPath);
file.open(createAbsolutePath(name, archiveParentPath).c_str(), std::ios::binary);
file << data;
file.close();
zipArchiveFiles.push_back(filepath);
}
for (const std::string& zipArchiveFile : zipArchiveFiles) {
if (extensionEquals(zipArchiveFile, ".iidm")) {
if (!config_.networkFilePath.empty()) {
throw Error(AlreadyInitializedNetworkFileInput);
}
config_.networkFilePath = zipArchiveFile;
} else if (extensionEquals(zipArchiveFile, ".json")) {
boost::property_tree::ptree tree;
boost::property_tree::read_json(zipArchiveFile, tree);

boost::property_tree::ptree::const_assoc_iterator configTreeIt = tree.find("dfl-config");
if (configTreeIt != tree.not_found()) {
if (!config_.configPath.empty()) {
throw Error(AlreadyInitializedConfigFileInput);
}
config_.configPath = zipArchiveFile;
}

boost::property_tree::ptree::const_assoc_iterator contingenciesTreeIt = tree.find("contingencies");
if (contingenciesTreeIt != tree.not_found()) {
if (!config_.contingenciesFilePath.empty()) {
throw Error(AlreadyInitializedContingenciesInput);
}
config_.contingenciesFilePath = zipArchiveFile;
}
} else {
throw Error(ErrorConfigFileRead, zipArchiveFile);
}
}
}

// These are not binded automatically
if (vm.count("log-level") > 0) {
config_.dynawoLogLevel = vm["log-level"].as<ParsedLogLevel>().logLevelDefinition;
Expand Down

0 comments on commit 08d69a0

Please sign in to comment.