Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controlling fixed precision for logging float point values #775

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a way to avoid setting precision twice pr. sample is to use two string writers, one for custom & one for default precision. Then at the end should be able to just go fsw_ << ss_default.rdbuf() << ss_custom.rdbuf();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each line will have time & stepCount so don't think we can concatenate at once after the loop.

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