ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
CoulombScafacos.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include "config/config.hpp"
23
24#ifdef SCAFACOS
25
26#include "Actor.hpp"
27
28#include "core/MpiCallbacks.hpp"
32
35
36#include <iomanip>
37#include <memory>
38#include <regex>
39#include <set>
40#include <sstream>
41#include <string>
42#include <vector>
43
44namespace ScriptInterface {
45namespace Coulomb {
46
47class CoulombScafacos : public Actor<CoulombScafacos, ::CoulombScafacos> {
48 std::shared_ptr<boost::mpi::environment> m_mpi_env_lock;
49
50public:
53 {"method_name", AutoParameter::read_only,
54 [this]() { return actor()->get_method(); }},
55 {"method_params", AutoParameter::read_only,
56 [this]() {
57 auto const m_tuned_methods =
58 std::set<std::string>{"ewald", "p2nfft", "p3m"};
59 auto parameters_string = actor()->get_parameters();
60 auto const method_name = actor()->get_method();
61 auto const delegate = actor()->get_near_field_delegation();
62 if (delegate and m_tuned_methods.count(method_name)) {
63 auto const tuned_r_cut = actor()->get_r_cut();
64 auto const field_name = method_name + "_r_cut";
65 std::ostringstream serializer;
66 serializer << std::scientific << std::setprecision(17);
67 serializer << tuned_r_cut;
68 auto const tuned_r_cut_string = serializer.str();
69 auto const replacement =
70 "," + field_name + "," + tuned_r_cut_string;
71 if (parameters_string.find(field_name) == field_name.npos) {
72 parameters_string += replacement;
73 } else {
74 auto const field_pattern =
75 std::regex("," + field_name + ",[0-9eE\\-\\+\\.]+");
76 parameters_string = std::regex_replace(
77 parameters_string, std::regex(field_pattern), replacement);
78 }
79 }
80 return Scafacos::deserialize_parameters(parameters_string);
81 }},
82 });
83 }
84
85 ~CoulombScafacos() override {
86 m_actor.reset();
87 m_mpi_env_lock.reset();
88 }
89
90 void do_construct(VariantMap const &params) override {
91 auto const method_name = get_value<std::string>(params, "method_name");
92 auto const param_list = params.at("method_params");
93 auto const prefactor = get_value<double>(params, "prefactor");
94
97 auto const method_params = Scafacos::serialize_parameters(param_list);
98 m_actor = make_coulomb_scafacos(method_name, method_params);
99 actor()->set_prefactor(prefactor);
100 });
102 // MPI communicator is needed to destroy the FFT plans
103 m_mpi_env_lock = ::Communication::mpiCallbacksHandle()->share_mpi_env();
104 }
105
106 Variant do_call_method(std::string const &name,
107 VariantMap const &params) override {
108 if (name == "set_near_field_delegation") {
109 auto const delegate = get_value<bool>(params, "delegate");
111 [&]() { actor()->set_near_field_delegation(delegate); });
112 return {};
113 }
114 if (name == "get_near_field_delegation") {
115 return actor()->get_near_field_delegation();
116 }
117 if (name == "get_available_methods") {
119 }
121 }
122};
123
124} // namespace Coulomb
125} // namespace ScriptInterface
126
127#endif // SCAFACOS
Communication::MpiCallbacks manages MPI communication using a visitor pattern.
ScafacosContextBase provides the public interface of the ScaFaCoS bridge.
void add_parameters(std::vector< AutoParameter > &&params)
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
Common interface for electrostatic actors.
Variant do_call_method(std::string const &name, VariantMap const &params) override
void do_construct(VariantMap const &params) override
Variant do_call_method(std::string const &name, VariantMap const &params) override
boost::string_ref name() const
Context * context() const
Responsible context.
This file contains the defaults for ESPResSo.
This file contains the asynchronous MPI communication.
std::shared_ptr< CoulombScafacos > make_coulomb_scafacos(std::string const &method, std::string const &parameters)
std::shared_ptr< MpiCallbacks > mpiCallbacksHandle()
std::unordered_map< std::string, Variant > deserialize_parameters(std::string const &parameters)
Convert flattened parameters to a map.
Definition scafacos.cpp:156
std::string serialize_parameters(Variant const &pack)
Flatten a parameter map.
Definition scafacos.cpp:127
std::vector< std::string > available_methods()
Fetch list of methods compiled in ScaFaCoS.
Definition scafacos.cpp:49
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:82
auto make_vector_of_variants(std::vector< T > const &v)
Definition Variant.hpp:101
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.
Definition Variant.hpp:80
static SteepestDescentParameters params
Currently active steepest descent instance.
static void sanity_check_method(std::string const &method_name)
static constexpr const ReadOnly read_only