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

feat: Add __len__ to Burst writer #1575

Merged
merged 3 commits into from
Jun 19, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions hoomd/GSDDequeWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ int GSDDequeWriter::getMaxQueueSize() const
return m_queue_size;
}

size_t GSDDequeWriter::getCurrentQueueSize() const
{
return m_frame_queue.size();
}

void GSDDequeWriter::setMaxQueueSize(int new_max_size)
{
m_queue_size = new_max_size;
Expand Down Expand Up @@ -107,6 +112,7 @@ void export_GSDDequeWriter(pybind11::module& m)
.def_property("max_burst_size",
&GSDDequeWriter::getMaxQueueSize,
&GSDDequeWriter::setMaxQueueSize)
.def("__len__", &GSDDequeWriter::getCurrentQueueSize)
.def("dump", &GSDDequeWriter::dump);
}
} // namespace detail
Expand Down
2 changes: 2 additions & 0 deletions hoomd/GSDDequeWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class PYBIND11_EXPORT GSDDequeWriter : public GSDDumpWriter
int getMaxQueueSize() const;
void setMaxQueueSize(int new_max_size);

size_t getCurrentQueueSize() const;

protected:
int m_queue_size;
std::deque<GSDDumpWriter::GSDFrame> m_frame_queue;
Expand Down
17 changes: 17 additions & 0 deletions hoomd/md/pytest/test_burst_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ def test_write_on_start(sim, tmp_path):
sim.run(0)


def test_len(sim, tmp_path):
filename = tmp_path / "temporary_test_file.gsd"

burst_trigger = hoomd.trigger.Periodic(period=2, phase=1)
burst_writer = hoomd.write.Burst(trigger=burst_trigger,
filename=filename,
mode='wb',
dynamic=['property', 'momentum'],
max_burst_size=3,
write_at_start=True)
sim.operations.writers.append(burst_writer)
sim.run(8)
assert len(burst_writer) == 3
burst_writer.dump()
assert len(burst_writer) == 0


def test_burst_dump(sim, tmp_path):
filename = tmp_path / "temporary_test_file.gsd"

Expand Down
6 changes: 6 additions & 0 deletions hoomd/write/gsd_burst.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,9 @@ def dump(self):
"""
if self._attached:
self._cpp_obj.dump()

def __len__(self):
"""Get the current length of the internal frame buffer."""
if self._attached:
return len(self._cpp_obj)
return 0