80 if (!(sampleRate > 0.0) || !std::isfinite(sampleRate))
return;
81 invFs2_ = 0.5 / sampleRate;
82 fs2_ = 2.0 * sampleRate;
112 void setParameters(
double ms,
double a,
double alpha,
double k,
double c)
noexcept
114 if (!std::isfinite(ms) || !std::isfinite(a) || !std::isfinite(alpha)
115 || !std::isfinite(k) || !std::isfinite(c))
117 ms_ = std::max(ms, 1.0);
118 a_ = std::max(a, 1.0);
119 alpha_ = std::max(alpha, 0.0);
120 k_ = std::max(k, 1.0);
121 c_ = std::clamp(c, 0.0, 0.999);
138 if (!(relative > 0.0) || !std::isfinite(relative))
return;
139 tolerance_ = std::clamp(relative, 1e-12, 1e-3);
140 bySlope_ = tolerance_ > 1e-9;
154 if (period < 0 || period >
kMaxPeriod || period == period_)
return;
172 const double chiAn = ms_ / (3.0 * a_);
173 return c_ * chiAn / (1.0 - c_ * alpha_ * chiAn);
187 if (!std::isfinite(fieldH))
return static_cast<T
>(m_);
189 const double h =
static_cast<double>(fieldH);
190 const double hDot = (1.0 + kAlpha) * 0.5 * fs2_ * (h - hPrev_) - kAlpha * hDotPrev_;
194 constexpr double kGain = 1.0 / (1.0 + kAlpha);
195 const double step = 2.0 * invFs2_ * kGain;
196 const double rhs = m_ + step * kAlpha * wPrev_;
197 double m = m_ + 2.0 * invFs2_ * wPrev_;
198 if (period_ > 0 && pastCount_ >= 2 * period_)
202 const double one = past_[
static_cast<std::size_t
>((pastAt_ + kRing - period_) & (kRing - 1))];
203 const double two = past_[
static_cast<std::size_t
>((pastAt_ + kRing - 2 * period_) & (kRing - 1))];
204 m = rhs + step * (2.0 * one - two);
207 double w = 0.0, dwdm = 0.0;
208 bool settled =
false;
209 const double stop = tolerance_ * ms_;
211 for (
int it = 0; it < 4; ++it)
213 evaluate(m, h, hDot, w, dwdm);
214 const double g = m - rhs - step * w;
215 const double gp = 1.0 - step * dwdm;
216 const double dm = g / gp;
218 const double size = std::abs(dm);
221 const bool shrunk = bySlope_ && it > 0 && size < 0.5 * before && size * size * size < stop * before * before;
223 if (size < stop || shrunk)
233 if (!settled || std::abs(m) > ms_)
236 m = std::clamp(m, -ms_, ms_);
237 evaluate(m, h, hDot, w, dwdm);
242 past_[
static_cast<std::size_t
>(pastAt_)] = w;
243 pastAt_ = (pastAt_ + 1) & (kRing - 1);
244 if (pastCount_ < 2 * period_) ++pastCount_;
249 return static_cast<T
>(m);
254 static void langevin(
double x,
double& l,
double& lp,
double& lpp)
noexcept
256 const double ax = std::abs(x);
260 const double x2 = x * x;
261 l = x * (1.0 / 3.0 - x2 / 45.0);
262 lp = 1.0 / 3.0 - x2 / 15.0;
263 lpp = -2.0 * x / 15.0;
268 l = (x > 0.0 ? 1.0 : -1.0) - 1.0 / x;
270 lpp = -2.0 / (x * x * x);
277 const double u = std::expm1(2.0 * x);
278 const double r = 1.0 / u;
279 const double coth = 1.0 + 2.0 * r;
280 const double csch2 = 4.0 * (u + 1.0) * r * r;
281 const double ix = 1.0 / x;
283 lp = ix * ix - csch2;
284 lpp = 2.0 * (csch2 * coth - ix * ix * ix);
289 void evaluate(
double m,
double h,
double hDot,
double& w,
double& dwdm)
const noexcept
298 const double q = (h + alpha_ * m) / a_;
299 double l = 0.0, lp = 0.0, lpp = 0.0;
300 langevin(q, l, lp, lpp);
302 const double mAn = ms_ * l;
303 const double dM = mAn - m;
304 const double delta = (hDot > 0.0) ? 1.0 : -1.0;
305 const double deltaM = (dM * delta > 0.0) ? 1.0 : 0.0;
308 const double oneMc = 1.0 - c_;
309 double d1 = oneMc * delta * k_ - alpha_ * dM;
310 const double d1Min = 0.01 * oneMc * k_;
311 if (std::abs(d1) < d1Min)
312 d1 = (d1 >= 0.0) ? d1Min : -d1Min;
314 const double invD1 = 1.0 / d1;
315 const double phi1 = oneMc * deltaM * dM * invD1;
316 const double cChiLp = c_ * chi_ * lp;
318 const double num = phi1 + cChiLp;
324 double den = 1.0 - alpha_ * cChiLp;
325 if (std::abs(den) < 0.01)
326 den = (den >= 0.0) ? 0.01 : -0.01;
327 const double invDen = 1.0 / den;
328 w = hDot * num * invDen;
331 const double alphaOverA = alpha_ / a_;
332 const double dmAn = ms_ * lp * alphaOverA - 1.0;
333 const double dPhi1 = oneMc * deltaM * dmAn * (oneMc * delta * k_) * (invD1 * invD1);
334 const double dLp = lpp * alphaOverA;
335 const double dNum = dPhi1 + c_ * chi_ * dLp;
336 const double dDen = -alpha_ * c_ * chi_ * dLp;
337 dwdm = hDot * (dNum * den - num * dDen) * (invDen * invDen);
342 static constexpr double kAlpha = 0.9;
348 static constexpr int kRing = 16;
351 double ms_ = 3.5e5, a_ = 2.2e4, alpha_ = 1.6e-3, k_ = 2.7e4, c_ = 1.7e-1;
352 double chi_ = 3.5e5 / 2.2e4;
353 double tolerance_ = 1e-9;
354 bool bySlope_ =
false;
356 double invFs2_ = 0.5 / 48000.0;
357 double fs2_ = 2.0 * 48000.0;
361 double hDotPrev_ = 0.0;
363 std::array<double, kRing> past_ {};
364 int pastAt_ = 0, pastCount_ = 0, period_ = 0;
void setParameters(double ms, double a, double alpha, double k, double c) noexcept
Sets the Jiles-Atherton parameters.
void setPredictorPeriod(int period) noexcept
The period of the field in samples, for the predictor (0, the default, for none). With a field that r...