ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
CoulombP3M.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 ESPRESSO_P3M
25
26#include "Actor.hpp"
27
29
31
32#include <memory>
33#include <optional>
34#include <stdexcept>
35#include <string>
36#include <utility>
37
38namespace ScriptInterface {
39namespace Coulomb {
40
41template <Arch Architecture>
42class CoulombP3M : public Actor<CoulombP3M<Architecture>, ::CoulombP3M> {
43 TuningParameters m_tuning;
44 bool m_tune;
45 bool m_single_precision;
46
47public:
49 using Base::actor;
51 using Base::context;
52
53protected:
54 using Base::m_actor;
56
57public:
60 {"single_precision", AutoParameter::read_only,
61 [this]() { return not actor()->is_double_precision(); }},
62 {"alpha_L", AutoParameter::read_only,
63 [this]() { return actor()->p3m_params.alpha_L; }},
64 {"r_cut_iL", AutoParameter::read_only,
65 [this]() { return actor()->p3m_params.r_cut_iL; }},
67 [this]() { return actor()->p3m_params.mesh; }},
68 {"mesh_off", AutoParameter::read_only,
69 [this]() { return actor()->p3m_params.mesh_off; }},
71 [this]() { return actor()->p3m_params.cao; }},
72 {"accuracy", AutoParameter::read_only,
73 [this]() { return actor()->p3m_params.accuracy; }},
74 {"epsilon", AutoParameter::read_only,
75 [this]() { return actor()->p3m_params.epsilon; }},
77 [this]() { return actor()->p3m_params.a; }},
79 [this]() { return actor()->p3m_params.alpha; }},
81 [this]() { return actor()->p3m_params.r_cut; }},
82 {"is_tuned", AutoParameter::read_only,
83 [this]() { return actor()->is_tuned(); }},
84 {"verbose", AutoParameter::read_only,
85 [this]() { return m_tuning.verbose; }},
86 {"timings", AutoParameter::read_only,
87 [this]() { return m_tuning.timings; }},
88 {"tune_limits", AutoParameter::read_only,
89 [this]() {
90#if defined(__clang__) and defined(__cray__)
91 auto const &range_min = m_tune_limits.first;
92 auto const &range_max = m_tune_limits.second;
93#else
94 auto const &[range_min, range_max] = m_tuning.limits;
95#endif
96 std::vector<Variant> retval = {
99 };
100 return retval;
101 }},
102 {"tune", AutoParameter::read_only, [this]() { return m_tune; }},
103 });
104 }
105
106 void do_construct(VariantMap const &params) override {
107 m_tune = get_value<bool>(params, "tune");
108 m_tuning.timings = get_value<int>(params, "timings");
109 m_tuning.verbose = get_value<bool>(params, "verbose");
110 m_tuning.limits = {std::nullopt, std::nullopt};
111 if (params.contains("tune_limits")) {
112 auto const &variant = params.at("tune_limits");
113 std::size_t range_length = 0u;
114 if (is_type<std::vector<int>>(variant)) {
116 range_length = range.size();
117 if (range_length == 2u) {
118 m_tuning.limits = {range[0u], range[1u]};
119 }
120 } else {
122 range_length = range.size();
123 if (range_length == 2u) {
124 if (not is_none(range[0u])) {
125 m_tuning.limits.first = get_value<int>(range[0u]);
126 }
127 if (not is_none(range[1u])) {
128 m_tuning.limits.second = get_value<int>(range[1u]);
129 }
130 }
131 }
132 context()->parallel_try_catch([&]() {
133 if (range_length != 2u) {
134 throw std::invalid_argument("Parameter 'tune_limits' needs 2 values");
135 }
136 if (m_tuning.limits.first and *m_tuning.limits.first <= 0) {
137 throw std::domain_error("Parameter 'tune_limits' must be > 0");
138 }
139 if (m_tuning.limits.second and *m_tuning.limits.second <= 0) {
140 throw std::domain_error("Parameter 'tune_limits' must be > 0");
141 }
142 });
143 }
144 auto const single_precision = get_value<bool>(params, "single_precision");
145 context()->parallel_try_catch([&]() {
146 auto p3m = P3MParameters{!get_value_or<bool>(params, "is_tuned", !m_tune),
147 get_value<double>(params, "epsilon"),
148 get_value<double>(params, "r_cut"),
151 get_value<int>(params, "cao"),
152 get_value<double>(params, "alpha"),
153 get_value<double>(params, "accuracy")};
154 m_actor = new_coulomb_p3m_heffte(std::move(p3m), m_tuning,
155 get_value<double>(params, "prefactor"),
157 });
159 }
160};
161
162} // namespace Coulomb
163} // namespace ScriptInterface
164
165#endif // ESPRESSO_P3M
void add_parameters(std::vector< AutoParameter > &&params)
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
Common interface for electrostatic actors.
void do_construct(VariantMap const &params) override
Type to indicate no value in Variant.
Definition None.hpp:32
Context * context() const
Responsible context.
constexpr bool is_none(Variant const &v)
Definition Variant.hpp:163
constexpr bool is_type(Variant const &v)
Check is a Variant holds a specific type.
Definition Variant.hpp:159
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
P3M algorithm for long-range Coulomb interaction.
std::shared_ptr< CoulombP3M > new_coulomb_p3m_heffte(P3MParameters &&p3m_params, TuningParameters const &tuning_params, double prefactor, bool single_precision, Arch arch)
static SteepestDescentParameters params
Currently active steepest descent instance.
P3M solver.
Definition p3m.hpp:57
Structure to hold P3M parameters and some dependent variables.
static constexpr const ReadOnly read_only
Recursive variant implementation.
Definition Variant.hpp:84
std::pair< std::optional< int >, std::optional< int > > limits