ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
DipolarP3M.hpp
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2022-2026 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_DP3M
25
26
#include "
Actor.hpp
"
27
28
#include "
core/magnetostatics/dp3m.hpp
"
29
30
#include "
script_interface/get_value.hpp
"
31
32
#include <memory>
33
#include <optional>
34
#include <stdexcept>
35
#include <string>
36
#include <utility>
37
38
#ifdef FFTW3_H
39
#error "The FFTW3 library shouldn't be visible in this translation unit"
40
#endif
41
42
namespace
ScriptInterface
{
43
namespace
Dipoles
{
44
45
template
<Arch Architecture>
46
class
DipolarP3M
:
public
Actor
<DipolarP3M<Architecture>, ::DipolarP3M> {
47
TuningParameters
m_tuning;
48
bool
m_tune;
49
50
public
:
51
using
Base
=
Actor<DipolarP3M<Architecture>
,
::DipolarP3M
>;
52
using
Base::actor
;
53
using
Base::add_parameters
;
54
using
Base::context
;
55
56
protected
:
57
using
Base::m_actor
;
58
59
public
:
60
DipolarP3M
() {
61
add_parameters
({
62
{
"single_precision"
,
AutoParameter::read_only
,
63
[
this
]() {
return
not
actor
()->is_double_precision(); }},
64
{
"alpha_L"
,
AutoParameter::read_only
,
65
[
this
]() {
return
actor
()->dp3m_params.alpha_L; }},
66
{
"r_cut_iL"
,
AutoParameter::read_only
,
67
[
this
]() {
return
actor
()->dp3m_params.r_cut_iL; }},
68
{
"mesh"
,
AutoParameter::read_only
,
69
[
this
]() {
return
actor
()->dp3m_params.mesh; }},
70
{
"mesh_off"
,
AutoParameter::read_only
,
71
[
this
]() {
return
actor
()->dp3m_params.mesh_off; }},
72
{
"cao"
,
AutoParameter::read_only
,
73
[
this
]() {
return
actor
()->dp3m_params.cao; }},
74
{
"accuracy"
,
AutoParameter::read_only
,
75
[
this
]() {
return
actor
()->dp3m_params.accuracy; }},
76
{
"epsilon"
,
AutoParameter::read_only
,
77
[
this
]() {
return
actor
()->dp3m_params.epsilon; }},
78
{
"a"
,
AutoParameter::read_only
,
79
[
this
]() {
return
actor
()->dp3m_params.a; }},
80
{
"alpha"
,
AutoParameter::read_only
,
81
[
this
]() {
return
actor
()->dp3m_params.alpha; }},
82
{
"r_cut"
,
AutoParameter::read_only
,
83
[
this
]() {
return
actor
()->dp3m_params.r_cut; }},
84
{
"is_tuned"
,
AutoParameter::read_only
,
85
[
this
]() {
return
actor
()->is_tuned(); }},
86
{
"verbose"
,
AutoParameter::read_only
,
87
[
this
]() {
return
m_tuning.
verbose
; }},
88
{
"timings"
,
AutoParameter::read_only
,
89
[
this
]() {
return
m_tuning.
timings
; }},
90
{
"tune_limits"
,
AutoParameter::read_only
,
91
[
this
]() {
92
#if defined(__clang__) and defined(__cray__) or defined(__INTEL_LLVM_COMPILER)
93
auto
const
&
range_min
= m_tuning.
limits
.first;
94
auto
const
&
range_max
= m_tuning.
limits
.second;
95
#else
96
auto
const
&[
range_min
,
range_max
] = m_tuning.
limits
;
97
#endif
98
std::vector<Variant>
retval
= {
99
range_min
?
Variant
{*
range_min
} :
Variant
{
None
{}},
100
range_max
?
Variant
{*
range_max
} :
Variant
{
None
{}},
101
};
102
return
retval
;
103
}},
104
{
"tune"
,
AutoParameter::read_only
, [
this
]() {
return
m_tune; }},
105
});
106
}
107
108
void
do_construct
(
VariantMap
const
¶ms)
override
{
109
m_tune =
get_value<bool>
(params,
"tune"
);
110
m_tuning.
timings
=
get_value<int>
(params,
"timings"
);
111
m_tuning.
verbose
=
get_value<bool>
(params,
"verbose"
);
112
m_tuning.
limits
= {std::nullopt, std::nullopt};
113
if
(params.contains(
"tune_limits"
)) {
114
auto
const
&
variant
= params.at(
"tune_limits"
);
115
std::size_t
range_length
= 0
u
;
116
if
(
is_type
<std::vector<int>>(
variant
)) {
117
auto
const
range
=
get_value<std::vector<int>
>(
variant
);
118
range_length
=
range
.size();
119
if
(
range_length
== 2u) {
120
m_tuning.
limits
= {
range
[0
u
],
range
[1u]};
121
}
122
}
else
{
123
auto
const
range
=
get_value<std::vector<Variant>
>(
variant
);
124
range_length
=
range
.size();
125
if
(
range_length
== 2u) {
126
if
(
not
is_none
(
range
[0
u
])) {
127
m_tuning.
limits
.first =
get_value<int>
(
range
[0
u
]);
128
}
129
if
(
not
is_none
(
range
[1u])) {
130
m_tuning.
limits
.second =
get_value<int>
(
range
[1u]);
131
}
132
}
133
}
134
context
()->
parallel_try_catch
([&]() {
135
if
(
range_length
!= 2u) {
136
throw
std::invalid_argument(
"Parameter 'tune_limits' needs 2 values"
);
137
}
138
if
(m_tuning.
limits
.first
and
*m_tuning.
limits
.first <= 0) {
139
throw
std::domain_error(
"Parameter 'tune_limits' must be > 0"
);
140
}
141
if
(m_tuning.
limits
.second
and
*m_tuning.
limits
.second <= 0) {
142
throw
std::domain_error(
"Parameter 'tune_limits' must be > 0"
);
143
}
144
});
145
}
146
auto
const
single_precision
=
get_value<bool>
(params,
"single_precision"
);
147
static_assert
(
Architecture
==
Arch::CPU
,
"GPU not implemented"
);
148
context
()->
parallel_try_catch
([&]() {
149
auto
p3m =
P3MParameters
{!
get_value_or<bool>
(params,
"is_tuned"
, !m_tune),
150
get_value<double>
(params,
"epsilon"
),
151
get_value<double>
(params,
"r_cut"
),
152
get_value<Utils::Vector3i>
(params,
"mesh"
),
153
get_value<Utils::Vector3d>
(params,
"mesh_off"
),
154
get_value<int>
(params,
"cao"
),
155
get_value<double>
(params,
"alpha"
),
156
get_value<double>
(params,
"accuracy"
)};
157
m_actor
=
new_dipolar_p3m_heffte
(std::move(p3m), m_tuning,
158
get_value<double>
(params,
"prefactor"
),
159
single_precision
,
Architecture
);
160
});
161
}
162
};
163
164
}
// namespace Dipoles
165
}
// namespace ScriptInterface
166
167
#endif
// ESPRESSO_DP3M
ScriptInterface::AutoParameters::add_parameters
void add_parameters(std::vector< AutoParameter > &¶ms)
Definition
AutoParameters.hpp:123
ScriptInterface::Context::parallel_try_catch
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
ScriptInterface::Dipoles::Actor
Common interface for magnetostatic actors.
Definition
magnetostatics/Actor.hpp:45
ScriptInterface::Dipoles::Actor< DipolarP3M< Architecture >, ::DipolarP3M >::actor
std::shared_ptr< CoreActorClass > actor()
Definition
magnetostatics/Actor.hpp:73
ScriptInterface::Dipoles::Actor< DipolarP3M< Architecture >, ::DipolarP3M >::m_actor
std::shared_ptr< CoreActorClass > m_actor
Definition
magnetostatics/Actor.hpp:54
ScriptInterface::Dipoles::DipolarP3M
Definition
DipolarP3M.hpp:46
ScriptInterface::Dipoles::DipolarP3M::do_construct
void do_construct(VariantMap const ¶ms) override
Definition
DipolarP3M.hpp:108
ScriptInterface::Dipoles::DipolarP3M::DipolarP3M
DipolarP3M()
Definition
DipolarP3M.hpp:60
ScriptInterface::None
Type to indicate no value in Variant.
Definition
None.hpp:31
ScriptInterface::ObjectHandle::context
Context * context() const
Responsible context.
Definition
ObjectHandle.hpp:57
stream
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
Definition
common_cuda.cu:34
config.hpp
dp3m.hpp
P3M algorithm for long-range magnetic dipole-dipole interaction.
new_dipolar_p3m_heffte
std::shared_ptr< DipolarP3M > new_dipolar_p3m_heffte(P3MParameters &&p3m_params, TuningParameters const &tuning_params, double prefactor, bool single_precision, Arch arch)
Definition
dp3m_heffte.cpp:52
get_value.hpp
Actor.hpp
Dipoles
Definition
magnetostatics/actor.hpp:30
ScriptInterface
Definition
script_interface/accumulators/AccumulatorBase.hpp:33
ScriptInterface::is_none
constexpr bool is_none(Variant const &v)
Definition
Variant.hpp:163
ScriptInterface::is_type
constexpr bool is_type(Variant const &v)
Check is a Variant holds a specific type.
Definition
Variant.hpp:159
ScriptInterface::VariantMap
std::unordered_map< std::string, Variant > VariantMap
Definition
Variant.hpp:133
Arch::CPU
@ CPU
DipolarP3M
Dipolar P3M solver.
Definition
dp3m.hpp:54
P3MParameters
Structure to hold P3M parameters and some dependent variables.
Definition
p3m/common.hpp:62
ScriptInterface::AutoParameter::read_only
static constexpr const ReadOnly read_only
Definition
AutoParameter.hpp:42
ScriptInterface::impl::recursive_variant
Recursive variant implementation.
Definition
Variant.hpp:84
TuningParameters
Definition
p3m/common.hpp:249
TuningParameters::timings
int timings
Definition
p3m/common.hpp:250
TuningParameters::verbose
bool verbose
Definition
p3m/common.hpp:252
TuningParameters::limits
std::pair< std::optional< int >, std::optional< int > > limits
Definition
p3m/common.hpp:251
src
script_interface
magnetostatics
DipolarP3M.hpp
Generated on Thu Mar 5 2026 02:40:46 for ESPResSo by
1.9.8