ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
stoner_wohlfarth_thermal.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025-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#include <config/config.hpp>
21
22#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
23
24#include "Particle.hpp"
27#include "cells.hpp"
28#include "constraints/Constraints.hpp"
29#include "constraints/HomogeneousMagneticField.hpp"
30#include "errorhandling.hpp"
31#include "random.hpp"
32#include "rotation.hpp"
33#include "system/System.hpp"
34#include "thermostat.hpp"
36
37#include <utils/Vector.hpp>
38#include <utils/uniform.hpp>
39
40#include <nlopt.hpp>
41
42#include <cassert>
43#include <cmath>
44#include <memory>
45#include <numbers>
46#include <utility>
47#include <vector>
48
49// small perturbation to avoid starting exactly at a stationary point
50constexpr static double eps_phi = 1e-3;
51// absolute error precision required for the optimiser
52constexpr static double eps_abs = 1e-15;
53// relative error precision required for the optimiser
54constexpr static double eps_rel = 1e-15;
55
56/**
57 * @brief Objective (energy) function for the Stoner-Wohlfarth phi minimisation.
58 *
59 * Evaluate the magnetic energy (normalized by the anisotropy field) for a
60 * given in-plane angle phi according to Eq. 5 in @cite mostarac25a.
61 * Assumes minima lie in the plane phi = zeta and uses trig identities
62 * to reduce the expression.
63 *
64 * @param n Number of optimization variables (should be 1: phi).
65 * @param x Pointer to variables; x[0] is the angle phi.
66 * @param grad If non-null, gradient is written to grad[0].
67 * @param my_func_data Pointer to a double[2] array with {theta, h}.
68 * @return Energy value for the given phi.
69 */
70static double phi_objective(unsigned n, const double *x, double *grad,
71 void *my_func_data) {
72 auto const phi = x[0];
73 auto const *params = reinterpret_cast<double const *>(my_func_data);
74 auto const theta = params[0];
75 auto const h = params[1];
76 if (grad) {
77 grad[0] = std::sin(2. * (phi - theta)) + 2. * h * std::sin(phi);
78 }
79 return -0.5 - 0.5 * std::cos(2. * (phi - theta)) - 2. * h * std::cos(phi);
80}
81
82/**
83 * @brief Find the in-plane angle phi corresponding to the correct
84 * energy minimum for the thermal Stoner-Wohlfarth particles.
85 *
86 * @param theta Angle between anisotropy director and external field (rad).
87 * @param h Reduced field (external + dipolar) normalised by H_k.
88 * @param phi0 Initial in-plane angle guess (rad).
89 * @param ani_param Inverse thermal energy factor (@f$1/(k_B T V)@f$ scaled).
90 * @param tau0_inv Attempt frequency inverse (1/tau0).
91 * @param dt Time increment for switching probability.
92 * @param noise Uniform random number in (0,1) used for the kinetic MC step.
93 * @return In-plane angle phi in range @f$ [0,2\pi) @f$.
94 */
95static double get_phi_at_energy_min(double theta, double h, double phi0,
96 double ani_param, double tau0_inv,
97 double dt, double const &noise) {
98
99 auto constexpr pi = std::numbers::pi_v<double>;
100 auto constexpr two_pi = 2. * pi;
101
102 // critical field, above which there is only one minimum (no need to do the
103 // thermal step); Eq. 6 in @cite mostarac25a.
104 auto const h_crit = std::pow(std::pow(std::sin(theta), 2. / 3.) +
105 std::pow(std::cos(theta), 2. / 3.),
106 -3. / 2.);
107 nlopt::opt opt(nlopt::LD_MMA, 1);
108 double params[] = {theta, h};
109
110 opt.set_min_objective(phi_objective, &params);
111 opt.set_ftol_rel(eps_rel);
112 opt.set_ftol_abs(eps_abs);
113 std::vector<double> phi(1);
114
115 // make initial guess from previous position plus an arbitrary perturbation
116 phi[0] = phi0 + eps_phi;
117 auto min1 = 0.;
118 opt.optimize(phi, min1);
119 auto const phi_min1 = std::fmod(phi[0], two_pi);
120 auto solution = phi_min1;
121 if (std::fabs(h) < h_crit) {
122 opt.set_max_objective(phi_objective, &params);
123 phi[0] = phi0 + eps_phi;
124 auto max1 = 0.;
125 opt.optimize(phi, max1);
126 auto const phi_max1 = std::fmod(phi[0], two_pi);
127 phi[0] = std::fmod(phi_max1 + pi, two_pi);
128 auto max2 = 0.;
129 opt.optimize(phi, max2);
130 // Eqs. 12 in @cite mostarac25a.
131 auto const b1 = std::abs(max1 - min1) * ani_param;
132 auto const b2 = std::abs(max2 - min1) * ani_param;
133 auto const b_min = (b1 < b2) ? b1 : b2;
134 // Eq. 13 in @cite mostarac25a.
135 auto const tau_inv = tau0_inv * exp(-b_min);
136 // switching probability (without backflip)
137 auto const p12 = 1. - exp(-dt * tau_inv);
138 // if MC move accepted, find the location of the other minimum
139 if (noise < p12) {
140 opt.set_min_objective(phi_objective, &params);
141 phi[0] = std::fmod(phi_min1 + pi + eps_phi, two_pi);
142 // try to find another minimum from the other side
143 auto min2 = 0.;
144 opt.optimize(phi, min2);
145 auto const phi_min2 = std::fmod(phi[0], two_pi);
146 solution = phi_min2;
147 }
148 }
149 return std::fmod(solution + two_pi, two_pi);
150}
151
152/**
153 * @brief Collect external homogeneous magnetic field from active constraints.
154 *
155 * Iterate over constraints and sum the homogeneous magnetic field vectors
156 * provided by @ref Constraints::HomogeneousMagneticField objects.
157 *
158 * @return The total external homogeneous magnetic field.
159 */
160static auto get_external_field(Constraints::Constraints const &constraints) {
161 using HomogeneousMagneticField = ::Constraints::HomogeneousMagneticField;
162 Utils::Vector3d ext_fld = {0., 0., 0.};
163 for (auto const &constraint : constraints) {
164 auto ptr = std::dynamic_pointer_cast<HomogeneousMagneticField>(constraint);
165 if (ptr) {
166 ext_fld += ptr->H();
167 }
168 }
169 return ext_fld;
170}
171
172/**
173 * @brief Simplified Stoner-Wohlfarth update in field-free case.
174 *
175 * @param[in,out] p Virtual particle to update.
176 * @param e_k Anisotropy director of the reference particle.
177 * @param kT Thermal energy from thermostat.
178 * @param noise Uniform random number in (0,1) used for the kinetic MC step.
179 */
181 double const kT, double const noise) {
182 auto constexpr pi = std::numbers::pi_v<double>;
183 auto const ani_param = p.magnetic_anisotropy_energy() / kT;
184 auto const tau_inv = p.stoner_wohlfarth_tau0_inv() * std::exp(-ani_param);
185 auto const p12 = 1. - std::exp(-p.stoner_wohlfarth_dt_incr() * tau_inv);
186 auto const kernel = [&](bool flip) {
187 auto const sat_mag = (flip ? -1. : +1.) * p.saturation_magnetization();
188 auto const [quat, dipm] = convert_dip_to_quat(sat_mag * e_k);
189 p.stoner_wohlfarth_phi_0() = flip ? pi : 0.;
190 p.dipm() = dipm;
191 p.quat() = quat;
192 };
193 auto const flip = noise < p12;
194 if (p.stoner_wohlfarth_phi_0() == 0.) {
195 kernel(flip);
196 } else if (p.stoner_wohlfarth_phi_0() == pi) {
197 kernel(not flip);
198 } else {
199 auto const dist_0 = std::abs(p.stoner_wohlfarth_phi_0() - 0.);
200 auto const dist_pi = std::abs(p.stoner_wohlfarth_phi_0() - pi);
201 // Compare the differences and determine the nearest angle (simplifies
202 // down to an XNOR operation, i.e. equality operator for boolean types)
203 kernel(flip == (dist_0 < dist_pi));
204 }
205}
206
207/**
208 * @brief Update virtual site dipole moment according to the full in-field
209 * (incl. dipole field) thermal Stoner-Wohlfarth model (incl. the kinetic MC
210 * step)
211 *
212 * @param[in,out] p Virtual particle to update (modified).
213 * @param e_k Anisotropy director of the reference particle.
214 * @param ext_fld_dpl External homogeneous magnetic field + total dipolar field
215 * acting on the particle.
216 * @param kT Thermal energy from thermostat.
217 * @param noise Uniform random number in (0,1) used for the kinetic MC step.
218 */
220 Utils::Vector3d const &ext_fld_dpl,
221 double const kT, double const noise) {
222
223 auto constexpr pi = std::numbers::pi_v<double>;
224 auto constexpr pi_half = pi / 2.;
225
226 // reduced field; Eq. 4 in @cite mostarac25a.
227 auto h = ext_fld_dpl.norm() * p.magnetic_anisotropy_field_inv();
228 auto e_h = ext_fld_dpl.normalized();
229 auto theta = std::acos(e_h * e_k);
230 if (theta > pi_half) {
231 theta = pi - theta;
232 h = -h;
233 e_h = -e_h;
234 }
235 auto const rot_axis =
236 vector_product(vector_product(e_h, e_k), e_h).normalized();
237 auto const ani_param = p.magnetic_anisotropy_energy() / kT;
238 auto const phi = get_phi_at_energy_min(
239 theta, h, p.stoner_wohlfarth_phi_0(), ani_param,
241 auto const mom = e_h * std::cos(phi) + rot_axis * std::sin(phi);
242 p.stoner_wohlfarth_phi_0() = phi;
243 auto const [quat, dipm] =
245 p.dipm() = dipm;
246 p.quat() = quat;
247}
248
249/**
250 * @brief Run magnetodynamics update for local virtual particles.
251 *
252 * Iterate over local particles and update the dipole moment of virtual
253 * particles according to the thermal Stoner-Wohlfarth model.
254 * Collect active homogeneous external magnetic fields from constraints and
255 * add the per-particle dipolar contribution before performing either the
256 * simplified no-field update or the full thermal Stoner-Wohlfarth update.
257 */
259 // collect HomogeneousMagneticFields if active
260 auto const ext_fld = get_external_field(*constraints);
261 auto const kT = thermostat->kT;
262 cell_structure->for_each_local_particle([&](Particle &p) {
263 if (not p.is_virtual() or not p.stoner_wohlfarth_is_enabled()) {
264 return;
265 }
266 auto *p_ref = get_reference_particle(*cell_structure, p);
267 if (not p_ref) {
268 return;
269 }
270 assert(thermostat->thermo_switch & THERMO_LANGEVIN);
271 auto const &langevin = *thermostat->langevin;
272 auto const e_k = p_ref->calc_director();
273 auto const ext_fld_dpl = ext_fld + p.dip_fld();
274 auto const random_ints =
275 Random::philox_4_uint64s<RNGSalt::THERMAL_STONER_WOHLFARTH>(
276 langevin.rng_counter(), langevin.rng_seed(), p.id());
277 auto const noise = Utils::uniform(random_ints[0]);
278 if (ext_fld_dpl.norm2() == 0.) {
279 stoner_wohlfarth_no_field(p, e_k, kT, noise);
280 } else {
281 // full Stoner-Wohlfarth update with external + dipolar field
282 stoner_wohlfarth_main(p, e_k, ext_fld_dpl, kT, noise);
283 }
284 });
285}
286
287#endif // ESPRESSO_THERMAL_STONER_WOHLFARTH
@ THERMO_LANGEVIN
Vector implementation and trait types for boost qvm interoperability.
This file contains everything related to the global cell structure / cell system.
void integrate_magnetodynamics()
Run magnetodynamics update for local virtual particles.
T norm() const
Definition Vector.hpp:159
Vector normalized() const
Definition Vector.hpp:177
__device__ void vector_product(float const *a, float const *b, float *out)
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
constexpr double uniform(uint64_t in)
Uniformly map unsigned integer to double.
Definition uniform.hpp:36
Random number generation using Philox.
Particle * get_reference_particle(CellStructure &cell_structure, Particle const &p)
Get real particle tracked by a virtual site.
Definition relative.cpp:75
This file contains all subroutines required to process rotational motion.
std::pair< Utils::Quaternion< double >, double > convert_dip_to_quat(const Utils::Vector3d &dip)
convert a dipole moment to quaternions and dipolar strength
Definition rotation.hpp:109
static double phi_objective(unsigned n, const double *x, double *grad, void *my_func_data)
Objective (energy) function for the Stoner-Wohlfarth phi minimisation.
void stoner_wohlfarth_no_field(Particle &p, Utils::Vector3d const &e_k, double const kT, double const noise)
Simplified Stoner-Wohlfarth update in field-free case.
static constexpr double eps_abs
static constexpr double eps_rel
static double get_phi_at_energy_min(double theta, double h, double phi0, double ani_param, double tau0_inv, double dt, double const &noise)
Find the in-plane angle phi corresponding to the correct energy minimum for the thermal Stoner-Wohlfa...
static auto get_external_field(Constraints::Constraints const &constraints)
Collect external homogeneous magnetic field from active constraints.
static constexpr double eps_phi
static void stoner_wohlfarth_main(Particle &p, Utils::Vector3d const &e_k, Utils::Vector3d const &ext_fld_dpl, double const kT, double const noise)
Update virtual site dipole moment according to the full in-field (incl.
Struct holding all information for one particle.
Definition Particle.hpp:435
auto const & dip_fld() const
Definition Particle.hpp:568
auto const & magnetic_anisotropy_energy() const
Definition Particle.hpp:554
auto const & stoner_wohlfarth_phi_0() const
Definition Particle.hpp:542
auto is_virtual() const
Definition Particle.hpp:588
auto const & magnetic_anisotropy_field_inv() const
Definition Particle.hpp:548
auto const & quat() const
Definition Particle.hpp:517
auto const & stoner_wohlfarth_tau0_inv() const
Definition Particle.hpp:558
auto const & saturation_magnetization() const
Definition Particle.hpp:544
auto const & stoner_wohlfarth_dt_incr() const
Definition Particle.hpp:562
auto const & stoner_wohlfarth_is_enabled() const
Definition Particle.hpp:538
auto const & dipm() const
Definition Particle.hpp:533
auto const & id() const
Definition Particle.hpp:454