37#include <unordered_map>
42namespace Interactions {
45 using container_type =
46 std::unordered_map<
unsigned int,
47 std::shared_ptr<NonBondedInteractionHandle>>;
50 using key_type =
typename container_type::key_type;
54 container_type m_nonbonded_ia_params;
55 std::shared_ptr<::InteractionsNonBonded> m_handle;
56 std::shared_ptr<std::function<void()>> m_notify_cutoff_change;
59 m_handle = std::make_shared<::InteractionsNonBonded>();
60 m_notify_cutoff_change = std::make_shared<std::function<void()>>([]() {});
64 auto const max_type = m_handle->get_max_seen_particle_type();
66 m_handle->make_particle_type_exist(max_type);
67 m_handle->bind_system(
m_system.lock());
68 m_handle->on_non_bonded_ia_change();
69 *m_notify_cutoff_change = [
this]() {
70 if (m_handle and not
m_system.expired()) {
71 m_handle->on_non_bonded_ia_change();
76 std::pair<int, int> get_key(
Variant const &key)
const {
78 auto const types = get_value<std::vector<int>>(key);
79 if (types.size() != 2ul or types[0] < 0 or types[1] < 0) {
80 throw Exception(
"need two particle types");
82 return {std::min(types[0], types[1]), std::max(types[0], types[1])};
84 if (
context()->is_head_node()) {
85 throw std::invalid_argument(
86 "NonBondedInteractions[] expects two particle types as indices");
95 if (method ==
"reset") {
96 if (not
context()->is_head_node()) {
99 auto const max_type = m_handle->get_max_seen_particle_type();
100 auto const obj_params =
VariantMap{{
"notify",
false}};
101 for (
int i = 0; i <= max_type; i++) {
102 for (
int j = 0; j <= i; j++) {
103 auto const key = m_handle->get_ia_param_key(i, j);
104 if (m_nonbonded_ia_params.contains(key)) {
105 m_nonbonded_ia_params.at(key)->call_method(
"reset", obj_params);
112 if (method ==
"get_handle") {
114 auto const [type_min, type_max] = get_key(
params.at(
"key"));
115 if (type_max > m_handle->get_max_seen_particle_type()) {
116 m_handle->make_particle_type_exist(type_max);
118 if (not ctx->is_head_node()) {
121 auto const key = m_handle->get_ia_param_key(type_min, type_max);
122 if (m_nonbonded_ia_params.contains(key)) {
123 return m_nonbonded_ia_params.at(key);
125 auto so = std::dynamic_pointer_cast<NonBondedInteractionHandle>(
126 ctx->make_shared(
"Interactions::NonBondedInteractionHandle", {}));
127 m_nonbonded_ia_params[key] = so;
131 if (method ==
"internal_set_max_type") {
132 m_handle->make_particle_type_exist(get_value<int>(
params,
"max_type"));
135 if (method ==
"internal_attach") {
136 auto so = std::dynamic_pointer_cast<NonBondedInteractionHandle>(
137 get_value<ObjectRef>(
params,
"obj"));
138 auto const [i, j] = get_key(
params.at(
"key"));
139 auto const cb_register =
140 [
this, i, j](std::shared_ptr<::IA_parameters>
const &core_ia) {
141 m_handle->set_ia_param(i, j, core_ia);
143 so->attach(cb_register, m_notify_cutoff_change);
151 auto const max_type = m_handle->get_max_seen_particle_type();
152 std::vector<std::string> object_states;
154 for (
int i = 0; i <= max_type; i++) {
155 for (
int j = 0; j <= i; j++) {
156 auto const key = m_handle->get_ia_param_key(i, j);
157 if (m_nonbonded_ia_params.contains(key)) {
158 object_states.emplace_back(
159 m_nonbonded_ia_params.at(key)->serialize());
161 object_states.emplace_back(
"");
170 auto const object_states = Utils::unpack<std::vector<std::string>>(state);
171 auto const max_type = Utils::unpack<int>(object_states.front());
172 call_method(
"internal_set_max_type", {{
"max_type", max_type}});
173 auto const end = object_states.end();
174 auto it = object_states.begin() + 1;
175 for (
int i = 0; i <= max_type; i++) {
176 for (
int j = 0; j <= i; j++) {
177 auto const key = m_handle->get_ia_param_key(i, j);
178 auto const &buffer = *it;
179 if (not buffer.empty()) {
180 auto so = std::dynamic_pointer_cast<NonBondedInteractionHandle>(
182 m_nonbonded_ia_params[key] = so;
184 {{
"key", std::vector<int>{{j, i}}}, {
"obj", so}});
The ScriptInterface counterparts of the non-bonded interactions parameters structs from the core are ...
void set_internal_state(std::string const &state) override
typename container_type::mapped_type mapped_type
std::string get_internal_state() const override
typename container_type::key_type key_type
void on_bind_system(::System::System &system) override
void do_construct(VariantMap const ¶ms) override
Variant do_call_method(std::string const &method, VariantMap const ¶ms) override
Variant call_method(const std::string &name, const VariantMap ¶ms)
Call a method on the object.
Context * context() const
Responsible context.
static ObjectRef deserialize(const std::string &state, Context &ctx)
Make object from serialized state.
Script interface wrapper for a component of the system class.
auto const & get_system() const
std::weak_ptr<::System::System > m_system
std::shared_ptr< InteractionsNonBonded > nonbonded_ias
std::unordered_map< std::string, Variant > VariantMap
boost::make_recursive_variant< None, bool, int, std::size_t, double, std::string, ObjectRef, Utils::Vector3b, Utils::Vector3i, Utils::Vector2d, Utils::Vector3d, Utils::Vector4d, std::vector< int >, std::vector< double >, std::vector< boost::recursive_variant_ >, std::unordered_map< int, boost::recursive_variant_ >, std::unordered_map< std::string, boost::recursive_variant_ > >::type Variant
Possible types for parameters.
std::string pack(T const &v)
Pack a serialize type into a string.
Various procedures concerning interactions between particles.
static SteepestDescentParameters params
Currently active steepest descent instance.