From 35d53f7d20d1d0881e8c3fb1f9f70ba836d48a33 Mon Sep 17 00:00:00 2001 From: Lars Ivar Hatledal Date: Fri, 18 Oct 2024 12:20:23 +0200 Subject: [PATCH] misc --- include/ecos/scenario/scenario.hpp | 2 +- src/ecos/algorithm/fixed_step_algorithm.cpp | 16 +++++++------- src/ecos/fmi/fmi_model_sub_resolver.cpp | 3 ++- src/ecos/model_resolver.cpp | 2 +- src/ecos/simulation.cpp | 21 +++++++++---------- src/ecos/simulation_runner.cpp | 2 +- src/ecos/util/temp_dir.cpp | 4 ++-- src/proxyfmu/process_helper.hpp | 23 ++++++++++----------- src/proxyfmu/proxy_fmu.cpp | 4 ++-- 9 files changed, 38 insertions(+), 39 deletions(-) diff --git a/include/ecos/scenario/scenario.hpp b/include/ecos/scenario/scenario.hpp index e58deeb..1878a1d 100644 --- a/include/ecos/scenario/scenario.hpp +++ b/include/ecos/scenario/scenario.hpp @@ -89,7 +89,7 @@ class scenario private: - bool active_; + bool active_{false}; std::vector> initActions; std::vector timedActions; std::vector predicateActions; diff --git a/src/ecos/algorithm/fixed_step_algorithm.cpp b/src/ecos/algorithm/fixed_step_algorithm.cpp index 7cc478e..06b4f43 100644 --- a/src/ecos/algorithm/fixed_step_algorithm.cpp +++ b/src/ecos/algorithm/fixed_step_algorithm.cpp @@ -21,12 +21,12 @@ int calculateDecimationFactor(const model_instance& m, double baseStepSize) static double EPS = 1e-3; - auto stepSizeHint = m.stepSizeHint(); + const auto& stepSizeHint = m.stepSizeHint(); if (!stepSizeHint) return 1; - int decimationFactor = std::max(1, static_cast(std::ceil(*stepSizeHint / baseStepSize))); - double actualStepSize = baseStepSize * decimationFactor; - double diff = std::fabs(actualStepSize - *stepSizeHint); + const int decimationFactor = std::max(1, static_cast(std::ceil(*stepSizeHint / baseStepSize))); + const double actualStepSize = baseStepSize * decimationFactor; + const double diff = std::fabs(actualStepSize - *stepSizeHint); if (diff >= EPS) { log::warn("Actual stepSize for {} will be {} rather than requested value {}", m.instanceName(), actualStepSize, *stepSizeHint); } @@ -41,14 +41,14 @@ class fixed_step_algorithm::impl public: impl(double stepSize, bool parallel) - : stepSize_(stepSize) - , parallel_(parallel) + : parallel_(parallel) + , stepSize_(stepSize) , stepNumber_(0) { } void model_instance_added(model_instance* instance) { - int decimationFactor = calculateDecimationFactor(*instance, stepSize_); + const int decimationFactor = calculateDecimationFactor(*instance, stepSize_); instances_.emplace_back(instance_wrapper{decimationFactor, instance}); } @@ -92,7 +92,7 @@ void fixed_step_algorithm::model_instance_added(model_instance* instance) pimpl_->model_instance_added(instance); } -double ecos::fixed_step_algorithm::step(double currentTime) +double fixed_step_algorithm::step(double currentTime) { return pimpl_->step(currentTime); } diff --git a/src/ecos/fmi/fmi_model_sub_resolver.cpp b/src/ecos/fmi/fmi_model_sub_resolver.cpp index bde8400..5c47fff 100644 --- a/src/ecos/fmi/fmi_model_sub_resolver.cpp +++ b/src/ecos/fmi/fmi_model_sub_resolver.cpp @@ -9,7 +9,8 @@ using namespace ecos; std::unique_ptr fmi_model_sub_resolver::resolve(const std::filesystem::path& base, const std::string& uri) { const auto fmuFile = base / uri; - if (!std::filesystem::exists(fmuFile)) { return nullptr; } + if (!exists(fmuFile)) { return nullptr; } if (fmuFile.extension() != ".fmu") { return nullptr; } + return std::make_unique(fmuFile); } diff --git a/src/ecos/model_resolver.cpp b/src/ecos/model_resolver.cpp index 0de90fc..1fa55fa 100644 --- a/src/ecos/model_resolver.cpp +++ b/src/ecos/model_resolver.cpp @@ -23,7 +23,7 @@ std::shared_ptr model_resolver::resolve(const std::filesystem::path& base return cache_.at(key); } for (auto& resolver : subResolvers_) { - std::shared_ptr model = resolver->resolve(base, uri); + std::shared_ptr model = resolver->resolve(base, uri); if (model) { cache_[key] = model; return model; diff --git a/src/ecos/simulation.cpp b/src/ecos/simulation.cpp index 98265d6..7f7b400 100644 --- a/src/ecos/simulation.cpp +++ b/src/ecos/simulation.cpp @@ -83,7 +83,7 @@ double simulation::step(unsigned int numStep) throw std::runtime_error("init() has not been invoked!"); } - double newT; + double newT{}; for (unsigned i = 0; i < numStep; ++i) { for (auto& [_, listener] : listeners_) { @@ -127,7 +127,7 @@ void simulation::step_until(double t) void simulation::step_for(double t) { - double newT = currentTime_ + t; + const double newT = currentTime_ + t; step_until(newT); } @@ -168,7 +168,6 @@ void simulation::add_listener(const std::string& name, std::shared_ptr instance) real_connection* simulation::make_real_connection(const variable_identifier& source, const variable_identifier& sink) { - auto p1 = get_real_property(source); + const auto p1 = get_real_property(source); if (!p1) throw std::runtime_error("No such real property: " + source.str()); - auto p2 = get_real_property(sink); + const auto p2 = get_real_property(sink); if (!p2) throw std::runtime_error("No such real property: " + sink.str()); connections_.emplace_back(std::make_unique(p1, p2)); @@ -212,9 +211,9 @@ real_connection* simulation::make_real_connection(const variable_identifier& sou int_connection* simulation::make_int_connection(const variable_identifier& source, const variable_identifier& sink) { - auto p1 = get_int_property(source); + const auto p1 = get_int_property(source); if (!p1) throw std::runtime_error("No such int property: " + source.str()); - auto p2 = get_int_property(sink); + const auto p2 = get_int_property(sink); if (!p2) throw std::runtime_error("No such int property: " + sink.str()); connections_.emplace_back(std::make_unique(p1, p2)); @@ -223,9 +222,9 @@ int_connection* simulation::make_int_connection(const variable_identifier& sourc bool_connection* simulation::make_bool_connection(const variable_identifier& source, const variable_identifier& sink) { - auto p1 = get_bool_property(source); + const auto p1 = get_bool_property(source); if (!p1) throw std::runtime_error("No such bool property: " + source.str()); - auto p2 = get_bool_property(sink); + const auto p2 = get_bool_property(sink); if (!p2) throw std::runtime_error("No such bool property: " + sink.str()); connections_.emplace_back(std::make_unique(p1, p2)); @@ -234,9 +233,9 @@ bool_connection* simulation::make_bool_connection(const variable_identifier& sou string_connection* simulation::make_string_connection(const variable_identifier& source, const variable_identifier& sink) { - auto p1 = get_string_property(source); + const auto p1 = get_string_property(source); if (!p1) throw std::runtime_error("No such string property: " + source.str()); - auto p2 = get_string_property(sink); + const auto p2 = get_string_property(sink); if (!p2) throw std::runtime_error("No such string property: " + sink.str()); connections_.emplace_back(std::make_unique(p1, p2)); diff --git a/src/ecos/simulation_runner.cpp b/src/ecos/simulation_runner.cpp index c74cbc3..281375c 100644 --- a/src/ecos/simulation_runner.cpp +++ b/src/ecos/simulation_runner.cpp @@ -76,7 +76,7 @@ void simulation_runner::run() std::this_thread::sleep_for(std::chrono::nanoseconds(1)); } } - double elapsed = sw.elapsed().count(); + const double elapsed = sw.elapsed().count(); const double t = sim_.time(); wallClock_ += elapsed; diff --git a/src/ecos/util/temp_dir.cpp b/src/ecos/util/temp_dir.cpp index d2e3099..c3a0cee 100644 --- a/src/ecos/util/temp_dir.cpp +++ b/src/ecos/util/temp_dir.cpp @@ -10,13 +10,13 @@ using namespace ecos; temp_dir::temp_dir(const std::string& name) : path_(std::filesystem::temp_directory_path() /= "ecos_" + name + "_" + generate_uuid()) { - std::filesystem::create_directories(path_); + create_directories(path_); } temp_dir::~temp_dir() { std::error_code status; - std::filesystem::remove_all(path_, status); + remove_all(path_, status); if (status) { log::warn("Failed to remove temp folder '{}': {}", path_.string(), status.message()); } diff --git a/src/proxyfmu/process_helper.hpp b/src/proxyfmu/process_helper.hpp index 42897dd..e51c3a3 100644 --- a/src/proxyfmu/process_helper.hpp +++ b/src/proxyfmu/process_helper.hpp @@ -24,7 +24,7 @@ namespace proxyfmu { -std::string getLoc() +inline std::string getLoc() { #ifdef __linux__ char result[PATH_MAX]; @@ -36,7 +36,7 @@ std::string getLoc() #endif } -void start_process( +inline void start_process( const std::filesystem::path& fmuPath, const std::string& instanceName, int& port, @@ -51,10 +51,10 @@ void start_process( executable = "proxyfmu.exe"; #endif - if (!std::filesystem::exists(executable)) { + if (!exists(executable)) { const std::string loc = getLoc(); const auto alt_executable = std::filesystem::path(loc).parent_path().string() / executable; - if (std::filesystem::exists(alt_executable)) { + if (exists(alt_executable)) { executable = alt_executable; } } @@ -67,11 +67,11 @@ void start_process( #endif std::cout << "[proxyfmu] Checking if proxyfmu is available.." << std::endl; - int statusCode = system(("\"" + execStr + "\" -v").c_str()); + const int statusCode = system(("\"" + execStr + "\" -v").c_str()); if (statusCode != 0) { std::cerr << "ERROR - unable to invoke proxyfmu!" << std::endl; - std::lock_guard lck(mtx); + std::lock_guard lck(mtx); port = -999; cv.notify_one(); return; @@ -93,7 +93,7 @@ void start_process( std::string line(buffer); if (!bound && line.substr(0, 16) == "[proxyfmu] port=") { { - std::lock_guard lck(mtx); + std::lock_guard lck(mtx); port = std::stoi(line.substr(16)); std::cout << "[proxyfmu] FMU instance '" << instanceName << "' instantiated using port " << port << std::endl; } @@ -111,13 +111,12 @@ void start_process( if (result == 0 && status == 0 && bound) { return; - } else { - std::cerr << "[proxyfmu] External proxy process for instance '" - << instanceName << "' returned with status " - << std::to_string(status) << ". Unable to bind.." << std::endl; } + std::cerr << "[proxyfmu] External proxy process for instance '" + << instanceName << "' returned with status " + << std::to_string(status) << ". Unable to bind.." << std::endl; } - std::lock_guard lck(mtx); + std::lock_guard lck(mtx); port = -999; cv.notify_one(); diff --git a/src/proxyfmu/proxy_fmu.cpp b/src/proxyfmu/proxy_fmu.cpp index 8dc82ad..7685bb0 100644 --- a/src/proxyfmu/proxy_fmu.cpp +++ b/src/proxyfmu/proxy_fmu.cpp @@ -14,10 +14,10 @@ namespace proxyfmu proxy_fmu::proxy_fmu(const std::filesystem::path& fmuPath, std::optional remote) : fmuPath_(fmuPath) - , remote_(std::move(remote)) , modelDescription_(fmilibcpp::loadFmu(fmuPath)->get_model_description()) + , remote_(std::move(remote)) { - if (!std::filesystem::exists(fmuPath)) { + if (!exists(fmuPath)) { throw std::runtime_error("No such file: " + std::filesystem::absolute(fmuPath).string() + "!"); } }