Skip to content

Commit

Permalink
Controlling fixed precision for logging float point values (#775)
Browse files Browse the repository at this point in the history
* Added an option to file_observer_config to set
fixed precision value.

* Fixed build failure
  • Loading branch information
davidhjp01 authored Oct 8, 2024
1 parent 44115de commit 5c6e196
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
13 changes: 13 additions & 0 deletions include/cosim/observer/file_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace cosim

class file_observer;


/**
* Configuration options for file_observer.
*/
Expand Down Expand Up @@ -88,9 +89,21 @@ class file_observer_config
*/
static file_observer_config parse(const filesystem::path& configPath);

/**
* Sets fixed precision value for floating point numbers
*
* \param precision the number of digits after the decimal point
*/
void fixed_precision(const int precision)
{
precision_ = precision;
}

private:
bool timeStampedFileNames_{true};
size_t defaultDecimationFactor_{1};
int precision_{-1};

std::unordered_map<std::string, std::pair<size_t, std::vector<std::string>>> variablesToLog_;

[[nodiscard]] bool should_log_simulator(const std::string& name) const
Expand Down
26 changes: 22 additions & 4 deletions src/cosim/observer/file_observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void clear_file_contents_if_exists(const cosim::filesystem::path& filePath, std:
fsw.close();
}
}

} // namespace


Expand All @@ -60,12 +61,18 @@ class file_observer::slave_value_writer
initialize_default();
}

slave_value_writer(observable* observable, cosim::filesystem::path& logDir, size_t decimationFactor,
const std::vector<variable_description>& variables, bool timeStampedFileNames = true)
slave_value_writer(
observable* observable,
cosim::filesystem::path& logDir,
size_t decimationFactor,
const std::vector<variable_description>& variables,
bool timeStampedFileNames = true,
const int precision = -1)
: observable_(observable)
, logDir_(logDir)
, decimationFactor_(decimationFactor)
, timeStampedFileNames_(timeStampedFileNames)
, precision_(precision)
{
initialize_config(variables);
}
Expand Down Expand Up @@ -301,10 +308,19 @@ class file_observer::slave_value_writer
void persist()
{
std::stringstream ss;
const auto defaultPrecision = ss.precision();

if (fsw_.is_open()) {

for (const auto& [stepCount, times] : timeSamples_) {
ss << times << "," << stepCount;
if (precision_ > -1) {
ss.precision(defaultPrecision);
ss << times << "," << stepCount;
ss.precision(precision_);
ss << std::fixed;
} else {
ss << times << "," << stepCount;
}

if (realSamples_.count(stepCount)) write<double>(realSamples_[stepCount], ss);
if (intSamples_.count(stepCount)) write<int>(intSamples_[stepCount], ss);
Expand Down Expand Up @@ -340,6 +356,7 @@ class file_observer::slave_value_writer
std::atomic<bool> recording_ = true;
std::mutex mutex_;
bool timeStampedFileNames_ = true;
int precision_ = -1;
};

file_observer::file_observer(const cosim::filesystem::path& logDir, std::optional<file_observer_config> config)
Expand Down Expand Up @@ -387,7 +404,8 @@ void file_observer::simulator_added(
logDir_,
config.decimationFactor,
config.variables,
config.timeStampedFileNames);
config.timeStampedFileNames,
config_->precision_);
} else {
return;
}
Expand Down

0 comments on commit 5c6e196

Please sign in to comment.