Table writer errors when writing values equal to zero
tcmoore3 opened this issue · comments
Tim Moore commented
Description
The recent fixes to the table writer for small values leads to a domain error when trying to write 0 because of the log10
call in the _digits_from_decimal
function.
Script
import hoomd
from io import StringIO
logger = hoomd.logging.Logger(categories=['scalar'])
logger[('zero',)] = (lambda: 0.0, 'scalar')
table_writer = hoomd.write.Table(1, logger, StringIO(""))
sim = hoomd.util.make_example_simulation()
sim.operations.add(table_writer)
sim.run(1)
Input files
No response
Output
Traceback (most recent call last):
File "/Users/mtimc/sandbox/test-table.py", line 9, in <module>
sim.run(1)
File "/Users/mtimc/mambaforge/lib/python3.10/site-packages/hoomd/simulation.py", line 562, in run
self._cpp_sys.run(steps_int, write_at_start)
File "/Users/mtimc/mambaforge/lib/python3.10/site-packages/hoomd/write/table.py", line 317, in act
self._write_row(output_dict)
File "/Users/mtimc/mambaforge/lib/python3.10/site-packages/hoomd/write/table.py", line 296, in _write_row
self.delimiter.join(
File "/Users/mtimc/mambaforge/lib/python3.10/site-packages/hoomd/write/table.py", line 297, in <genexpr>
(self._fmt(data[k], headers[k]) for k in headers)))
File "/Users/mtimc/mambaforge/lib/python3.10/site-packages/hoomd/write/table.py", line 97, in __call__
return self.format_num(value, column_width)
File "/Users/mtimc/mambaforge/lib/python3.10/site-packages/hoomd/write/table.py", line 122, in format_num
min_len_repr = self._digits_from_decimal(value) + 1
File "/Users/mtimc/mambaforge/lib/python3.10/site-packages/hoomd/write/table.py", line 102, in _digits_from_decimal
digits = int(log10(abs(num)))
ValueError: math domain error
Expected output
No errors.
Platform
CPU, macOS
Installation method
Conda package
HOOMD-blue version
4.2.0
Python version
3.10.12