145 double window,
double fine,
double coarse,
int order)
147 if (!(period > 0) || !(window > 0) || !(fine > 0) || !(coarse >= fine) ||
149 throw std::invalid_argument(
150 "Positive period, window and element lengths required");
151 std::vector<std::pair<double, double>>
153 for (
double c: centres) {
154 double a = std::fmod(c - window, period), b;
155 if (a < 0) a += period;
158 intervals.push_back({a, period});
159 intervals.push_back({0, b - period});
161 intervals.push_back({a, b});
164 std::sort(intervals.begin(), intervals.end());
165 std::vector<double> cuts{0};
167 const auto fill = [&](
double to,
double length) {
168 const double span = to - cursor;
169 if (span <= 0)
return;
171 static_cast<Eigen::Index
>(std::ceil(span / length - 1e-12));
172 for (Eigen::Index i = 1; i <= n; ++i)
173 cuts.push_back(cursor + span *
static_cast<double>(i) / n);
176 for (
const auto& [a, b]: intervals) {
177 if (b <= cursor)
continue;
178 fill(std::max(a, cursor), coarse);
181 fill(period, coarse);
219 const std::function<
Matrix(Eigen::Index)>& curvature)
222 throw std::invalid_argument(
"Invalid mesh or dimension");
224 const Eigen::Index n = mesh.
nodes(), N = n * d;
226 std::vector<Eigen::Triplet<double>> jt, mt;
227 for (Eigen::Index e = 0; e < mesh.
elements(); ++e) {
230 throw std::invalid_argument(
"Element boundaries must increase");
235 for (
int i = 0; i <= mesh.
order; ++i)
236 for (
int j = 0; j <= mesh.
order; ++j) {
237 const Eigen::Index gi = (e * mesh.
order + i) % n,
238 gj = (e * mesh.
order + j) % n;
239 for (Eigen::Index a = 0; a < d; ++a)
240 jt.emplace_back(gi * d + a, gj * d + a, stiffness(i, j));
243 for (Eigen::Index g = 0; g < n; ++g) {
244 const Matrix h = curvature(g);
245 if (h.rows() != d || h.cols() != d || !h.allFinite())
246 throw std::invalid_argument(
"Finite d x d curvature required");
247 for (Eigen::Index a = 0; a < d; ++a) {
248 mt.emplace_back(g * d + a, g * d + a, nodes.mass(g));
249 for (Eigen::Index b = 0; b < d; ++b)
250 jt.emplace_back(g * d + a, g * d + b,
251 nodes.mass(g) * .5 * (h(a, b) + h(b, a)));
259 p.
mass.setFromTriplets(mt.begin(), mt.end());
296 const std::function<
Matrix(Eigen::Index)>& curvature)
298 const Eigen::Index d = reference.rows();
299 if (d < 1 || reference.cols() != d || !reference.allFinite())
300 throw std::invalid_argument(
301 "Finite square reference curvature required");
302 Eigen::SelfAdjointEigenSolver<Matrix> modes(
303 .5 * (reference + reference.transpose()));
304 if (modes.info() != Eigen::Success || !(modes.eigenvalues().minCoeff() > 0))
305 throw std::domain_error(
"A positive reference curvature is required");
307 const Eigen::Index n = nodes.tau.size();
308 std::vector<Matrix> delta(n);
309 for (Eigen::Index g = 0; g < n; ++g)
310 delta[g] = curvature(g) - reference;
311 double correction = 0;
312 for (Eigen::Index a = 0; a < d; ++a) {
313 const double w2 = modes.eigenvalues()(a), w = std::sqrt(w2);
314 const Vector v = modes.eigenvectors().col(a);
315 const double continuum = 1 / (2 * w * std::tanh(mesh.
period() * w / 2));
317 mesh, 1, [&](Eigen::Index) {
return Matrix::Constant(1, 1, w2); });
319 if (ldlt.info() != Eigen::Success)
320 throw std::runtime_error(
"Reference factorisation failed");
321 Vector unit = Vector::Zero(n);
322 for (Eigen::Index g = 0; g < n; ++g) {
324 const double discrete = ldlt.solve(unit)(g);
327 nodes.mass(g) * (continuum - discrete) * v.dot(delta[g] * v);
355 if (u.size() != p.
dimension || !(u.norm() > 0))
356 throw std::invalid_argument(
"Zero mode of the pencil's size required");
359 std::vector<Eigen::Triplet<double>> t;
363 t.emplace_back(it.row(), it.col(), it.value());
364 for (Eigen::Index i = 0; i < N; ++i)
366 t.emplace_back(i, N, mu(i));
367 t.emplace_back(N, i, mu(i));
369 Sparse bordered(N + 1, N + 1);
370 bordered.setFromTriplets(t.begin(), t.end());
371 Eigen::SparseLU<Sparse> lu;
372 lu.analyzePattern(bordered);
373 lu.factorize(bordered);
374 if (lu.info() != Eigen::Success)
375 throw std::runtime_error(
"Bordered fluctuation factorisation failed");
377 out.norm = u.dot(mu);
378 out.logAbs = lu.logAbsDeterminant() - std::log(
out.norm);
419 const Mesh& mesh, Eigen::Index d,
const std::vector<Eigen::Index>& support,
420 const std::function<
Matrix(Eigen::Index)>& curvature,
423 if (d < 1 || reference.rows() != d || reference.cols() != d ||
425 throw std::invalid_argument(
426 "A d x d reference and a nonempty support are required");
427 Eigen::SelfAdjointEigenSolver<Matrix> modes(
428 .5 * (reference + reference.transpose()));
429 if (modes.info() != Eigen::Success || !(modes.eigenvalues().minCoeff() > 0))
430 throw std::domain_error(
"A positive reference curvature is required");
431 const Matrix& R = modes.eigenvectors();
433 const Eigen::Index n = nodes.tau.size(),
434 s =
static_cast<Eigen::Index
>(support.size()), S = s * d;
435 const bool primed = zeroMode.size() != 0;
436 if (primed && zeroMode.size() != n * d)
437 throw std::invalid_argument(
"A zero mode over every node is required");
438 std::vector<char> inSupport(n, 0);
439 for (Eigen::Index g: support) {
440 if (g < 0 || g >= n || inSupport[g])
441 throw std::invalid_argument(
442 "Support nodes must be distinct mesh nodes");
446 std::vector<Matrix> weighted(s);
447 for (Eigen::Index i = 0; i < s; ++i) {
448 const Matrix h = curvature(support[i]);
449 if (h.rows() != d || h.cols() != d || !h.allFinite())
450 throw std::invalid_argument(
"Finite d x d curvature required");
452 nodes.mass(support[i]) *
453 (R.transpose() * (.5 * (h + h.transpose()) - reference) * R);
457 std::vector<Matrix> columns(d,
Matrix(n, s));
458 std::vector<Eigen::SimplicialLDLT<Sparse>> scalar(d);
459 for (Eigen::Index a = 0; a < d; ++a) {
460 const double w2 = modes.eigenvalues()(a);
461 scalar[a].compute(
assemble(mesh, 1, [&](Eigen::Index) {
462 return Matrix::Constant(1, 1, w2);
464 if (scalar[a].info() != Eigen::Success)
465 throw std::runtime_error(
"Reference factorisation failed");
466 Vector unit = Vector::Zero(n);
467 for (Eigen::Index j = 0; j < s; ++j) {
468 unit(support[j]) = 1;
469 columns[a].col(j) = scalar[a].solve(unit);
470 unit(support[j]) = 0;
473 const Eigen::Index N = primed ? S + 1 : S;
474 Matrix A = Matrix::Identity(N, N);
475 for (Eigen::Index i = 0; i < s; ++i)
476 for (Eigen::Index j = 0; j < s; ++j)
477 for (Eigen::Index a = 0; a < d; ++a)
478 A.block(i * d + a, j * d, 1, d) +=
479 columns[a](support[i], j) * weighted[j].row(a);
485 std::vector<Vector> modeU(d,
Vector(n)), gmu(d);
486 for (Eigen::Index g = 0; g < n; ++g) {
487 const Vector r = R.transpose() * zeroMode.segment(g * d, d);
488 for (Eigen::Index a = 0; a < d; ++a)
491 for (Eigen::Index a = 0; a < d; ++a)
492 gmu[a] = scalar[a].solve(
Vector(nodes.mass.cwiseProduct(modeU[a])));
494 for (Eigen::Index i = 0; i < s; ++i)
495 for (Eigen::Index a = 0; a < d; ++a) {
496 A(i * d + a, N - 1) = gmu[a](support[i]);
497 A(N - 1, i * d + a) =
498 nodes.mass(support[i]) * modeU[a](support[i]);
500 for (Eigen::Index a = 0; a < d; ++a) {
501 Vector offMass = nodes.mass.cwiseProduct(modeU[a]);
502 for (Eigen::Index g: support)
505 const Vector reach = columns[a].transpose() * offMass;
506 for (Eigen::Index j = 0; j < s; ++j)
507 A.block(N - 1, j * d, 1, d) -= reach(j) * weighted[j].row(a);
508 A(N - 1, N - 1) -= offMass.dot(gmu[a]);
510 for (Eigen::Index g = 0; g < n; ++g)
512 nodes.mass(g) * zeroMode.segment(g * d, d).squaredNorm();
514 Eigen::PartialPivLU<Matrix> lu(A);
515 const Matrix& U = lu.matrixLU();
516 out.sign = lu.permutationP().determinant() > 0 ? 1 : -1;
517 for (Eigen::Index i = 0; i < N; ++i) {
519 throw std::domain_error(
"Singular Fredholm matrix");
520 out.logAbs += std::log(std::abs(U(i, i)));
521 if (U(i, i) < 0)
out.sign = -
out.sign;
524 out.logAbs -= std::log(
out.norm);