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