Skip to content

Commit

Permalink
Add two new options to manage the format of the numerical output of s…
Browse files Browse the repository at this point in the history
…ome observables (#100)

BaseObservable has two new protected members (`_general_format` and `_precision`) that can be set from the input file and are used to initialise `_number_formatter`, which can in turn used by inheriting observables to print numbers with a format that can be controlled by the user.
  • Loading branch information
mlsample committed May 10, 2024
1 parent dbdc726 commit 6d03f40
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/Observables/BaseObservable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ void BaseObservable::update_data(llint curr_step) {
void BaseObservable::get_settings(input_file &my_inp, input_file &sim_inp) {
getInputString(&my_inp, "id", _id, 0);
getInputLLInt(&my_inp, "update_every", &_update_every, 0);
getInputLLInt(&my_inp, "precision", &_precision, 0);
getInputBool(&my_inp, "general_format", &_general_format, 0);

if (_general_format) {
_number_formatter = Utils::sformat("%%.%dg", _precision);
}
else {
_number_formatter = Utils::sformat("%%10.%dlf", _precision);
}
}

void BaseObservable::init() {
Expand Down
3 changes: 3 additions & 0 deletions src/Observables/BaseObservable.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class BaseObservable {
ConfigInfo *_config_info;

std::string _id;
bool _general_format = false;
std::string _number_formatter;
long long int _precision = 6;
long long int _update_every = 0;
long long int _times_updated = 0;
public:
Expand Down
2 changes: 1 addition & 1 deletion src/Observables/ForceEnergy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ std::string ForceEnergy::get_output_string(llint curr_step) {
}
U /= _config_info->N();

return Utils::sformat("% 10.6lf", U);
return Utils::sformat(_number_formatter, U);
}
4 changes: 2 additions & 2 deletions src/Observables/PotentialEnergy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ std::string PotentialEnergy::get_output_string(llint curr_step) {
if(!_split) {
number energy = get_potential_energy();

return Utils::sformat("% 10.6lf", energy);
return Utils::sformat(_number_formatter, energy);
}
else {
std::string res("");
auto energies = _config_info->interaction->get_system_energy_split(_config_info->particles(), _config_info->lists);
for(auto energy_item : energies) {
number contrib = energy_item.second / _config_info->N();
res = Utils::sformat("%s % 10.6lf", res.c_str(), contrib);
res = Utils::sformat("%s " + _number_formatter, res.c_str(), contrib);
}

return res;
Expand Down
1 change: 1 addition & 0 deletions src/Observables/PotentialEnergy.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class PotentialEnergy: public BaseObservable {
protected:
bool _split;

public:
PotentialEnergy();
virtual ~PotentialEnergy();
Expand Down

0 comments on commit 6d03f40

Please sign in to comment.