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

Blocking scheme sampling #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 29 additions & 1 deletion halmd/observables/dynamics/blocking_scheme.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*
* Copyright © 2011 Peter Colberg and Felix Höfling
* Copyright © 2011 Felix Höfling
* Copyright © 2015 Nicolas Höft
* Copyright © 2011 Peter Colberg
*
* This file is part of HALMD.
*
Expand Down Expand Up @@ -150,6 +152,31 @@ void blocking_scheme::sample()
on_append_sample_();
}

blocking_scheme::step_type blocking_scheme::next() const
{
LOG_TRACE("compute next occurance of a sampling step");
step_type next_step;
step_type const step = clock_->step();

// TODO: is this sufficient?
next_step = step + separation_;

// iterate over all coarse-graining levels
for (unsigned int i = 0; i < interval_.size(); ++i) {
// calculate the next occuring sampling measured from step
step_type next;
if (origin_[i] < step) {
next = origin_[i] + ((step - 1 - origin_[i]) / interval_[i] + 1) * interval_[i];
}
else {
next = origin_[i];
}

next_step = min(next, next_step);
}
return next_step;
}

void blocking_scheme::finalise()
{
on_prepend_finalise_();
Expand Down Expand Up @@ -264,6 +291,7 @@ void blocking_scheme::luaopen(lua_State* L)
.def("on_append_sample", &blocking_scheme::on_append_sample)
.def("on_prepend_finalise", &blocking_scheme::on_prepend_finalise)
.def("on_append_finalise", &blocking_scheme::on_append_finalise)
.def("next", &blocking_scheme::next)
]
]
];
Expand Down
2 changes: 2 additions & 0 deletions halmd/observables/dynamics/blocking_scheme.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class blocking_scheme
return time_;
}

step_type next() const;

/** Lua bindings */
static void luaopen(lua_State* L);

Expand Down
17 changes: 10 additions & 7 deletions lua/halmd/observables/dynamics/blocking_scheme.lua.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--
-- Copyright © 2011-2014 Felix Höfling
-- Copyright © 2015 Nicolas Höft
-- Copyright © 2011-2012 Peter Colberg
--
-- This file is part of HALMD.
Expand Down Expand Up @@ -123,13 +124,15 @@ local M = module(function(args)
local conn = {}
result.disconnect = utility.signal.disconnect(conn, "correlation function")

-- establish internal connections of the correlation function
-- e.g., particle:enable_aux() → sampler.on_prepare_force,
-- and append to our connection table
if tcf.connect then
conn_ = tcf:connect({every = every}) -- FIXME pass actual grid of sampling steps
for i,c in ipairs(conn_) do
table.insert(conn, c)
-- enable auxiliary variable calculation, if necessary for correlation function
if tcf.aux_enable then
local particles = utility.assert_type(tcf.aux_enable, "table")
for i,p in ipairs(particles) do
table.insert(conn, sampler:on_prepare(function()
if self:next() == clock.step then
p:aux_enable()
end
end, 1, clock.step))
end
end

Expand Down
18 changes: 18 additions & 0 deletions lua/halmd/observables/dynamics/correlation.lua.in
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ local blocking_scheme_adaptor = assert(libhalmd.observables.samples.blocking_sch
-- :param args.location: default location within file
-- :type args.location: string table
-- :param string args.desc: module description
-- :param table args.aux_enable: sequence of :class:`halmd.mdsim.particle` instances *(optional)*
--
-- The argument ``acquire`` is a callable or a table of up to 2 callables that
-- yield the samples to be correlated.
Expand All @@ -95,6 +96,13 @@ local blocking_scheme_adaptor = assert(libhalmd.observables.samples.blocking_sch
-- H5MD files, it obeys the structure {``"dynamics"``, particle group, name of
-- correlation function}.
--
-- The parameter ``aux_enable`` is useful if ``acquire()`` depends on one of
-- the auxiliary force variables, see :meth:`halmd.mdsim.particle.aux_enable`
-- for details. In sampling steps of the correlation function, each ``particle``
-- instance listed in ``aux_enable`` is notified to update the auxiliary
-- variables before the sampling step. Thereby, redundant force calculations
-- can be avoided.
--
-- .. method:: acquire()
--
-- Acquire sample(s).
Expand All @@ -113,6 +121,11 @@ local blocking_scheme_adaptor = assert(libhalmd.observables.samples.blocking_sch
--
-- Module description.
--
-- .. attribute:: aux_enable
--
-- Sequence of particle instances that require auxiliary variables
-- during sampling.
--
-- .. class:: writer(args)
--
-- Construct file writer.
Expand All @@ -133,6 +146,7 @@ local M = module(function(args)
local shape = utility.assert_type(args.shape or {}, "table")
local location = utility.assert_type(utility.assert_kwarg(args, "location"), "table")
local desc = utility.assert_type(utility.assert_kwarg(args, "desc"), "string")
local aux_enable = utility.assert_type(args.aux_enable or {}, "table")

-- ensure that acquire is a table
if not (type(acquire) == "table") then
Expand Down Expand Up @@ -165,6 +179,10 @@ local M = module(function(args)
return writer
end end)

self.aux_enable = property(function(self)
return aux_enable
end)

return self
end)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--
-- Copyright © 2013 Nicolas Höft
-- Copyright © 2013-2015 Nicolas Höft
-- Copyright © 2013-2014 Felix Höfling
--
-- This file is part of HALMD.
Expand Down Expand Up @@ -82,19 +82,6 @@ local sampler = require("halmd.observables.sampler")
--
-- Module description.
--
-- .. method:: connect(args)
--
-- :param table args: keyword arguments
-- :param args.every: sampling interval
-- :returns: sequence of signal connections
--
-- *Internal use only.* This function is called upon registration by
-- ``blocking_scheme:correlation()``.
--
-- Connect ``msv.group.particle:aux_enable()`` to the signal
-- ``on_prepend_force`` of :class:`halmd.observables.sampler` using the
-- interval ``every``.
--
-- .. class:: writer(args)
--
-- Construct file writer.
Expand Down Expand Up @@ -136,17 +123,9 @@ local M = module(function(args)
, location = {"dynamics", label, "stress_tensor_autocorrelation"}
-- module description
, desc = ("stress tensor autocorrelation of %s particles"):format(label)
, aux_enable = {msv.group.particle}
}))

self.connect = function(self, args)
local every = utility.assert_kwarg(args, "every")

local conn = {
assert(sampler:on_prepare(function() msv.group.particle:aux_enable() end, every, clock.step))
}
return conn
end

return self
end)

Expand Down