76 {},
77 double unresolvedTolerance = 1e-12,
78 std::vector<M> modalGreen = {})
79 : omega_(std::move(frequencies)),
80 modes_(std::move(modes)),
81 times_(std::move(times)),
82 period_(period),
83 modalGreen_(std::move(modalGreen))
84 {
85 const auto d = omega_.size(),
86 n = static_cast<Eigen::Index>(times_.size());
87 require(
88 d > 0 && n > 0 && omega_.allFinite() &&
89 (omega_.array() > 0).all() && period_ > 0 &&
90 std::isfinite(period_) && std::isfinite(unresolvedTolerance) &&
91 unresolvedTolerance >= 0,
92 "Positive finite harmonic frequencies and thermal period required");
93 require(n <= std::numeric_limits<Eigen::Index>::max() / d,
94 "Nodal dimension exceeds range");
95 dimension_ = n * d;
96 codimension_ = normal.cols();
97 require(codimension_ <= dimension_ && normal.rows() == dimension_ &&
98 normal.allFinite(),
99 "Matching finite nodal constraint columns required");
100 if (codimension_)
101 require((normal.transpose() * normal -
102 M::Identity(codimension_, codimension_))
103 .norm() <= 1e-10,
104 "Nodal constraints must be jointly orthonormal");
105 require(!modes_.size() ||
106 (modes_.rows() == d && modes_.cols() == d &&
107 modes_.allFinite() &&
108 (modes_.transpose() * modes_ - M::Identity(d, d)).norm() <=
109 1e-10),
110 "Orthonormal harmonic normal modes required");
111 for (Eigen::Index j = 0; j < n; ++j)
112 require(std::isfinite(times_[j]) && times_[j] >= 0 &&
113 times_[j] < period_ &&
114 (!j || times_[j] > times_[j - 1]),
115 "Distinct increasing imaginary times inside one period "
116 "required");
117 require(green(0).allFinite(),
118 "Harmonic reference response exceeds numerical range");
119 require(modalGreen_.empty() ||
120 modalGreen_.size() == static_cast<std::size_t>(d),
121 "One nodal reference Green matrix per harmonic mode required");
122 for (auto& matrix: modalGreen_) {
123 require(matrix.rows() == n && matrix.cols() == n &&
124 matrix.allFinite() &&
125 (matrix - matrix.transpose()).norm() <=
126 1e-12 * std::max(1., matrix.norm()),
127 "Matching finite symmetric modal Green matrices required");
128 matrix = (.5 * matrix + .5 * matrix.transpose()).eval();
129 const Eigen::LDLT<M> factor(matrix);
130 require(factor.info() == Eigen::Success &&
131 (factor.vectorD().array() > 0).all(),
132 "Modal reference Green matrix must be positive");
133 }
134 std::vector<V> columns;
135 std::vector<double> signs;
136 Eigen::Index negativeSigns = 0;
137 for (const auto& update: updates) {
138 require(update.node < times_.size() &&
139 update.direction.size() == d &&
140 update.direction.allFinite() &&
141 std::isfinite(update.strength),
142 "Finite matching point curvature required");
143 if (update.strength == 0) continue;
144 require(update.direction.squaredNorm() > 0,
145 "A nonzero point curvature needs a resolved direction");
146 V column = modes_.size() ? V(modes_.transpose() * update.direction)
147 : update.direction;
148 column *= std::sqrt(std::abs(update.strength));
149 require(column.allFinite(),
150 "Point curvature exceeds the harmonic coordinate range");
151 nodes_.push_back(update.node);
152 columns.push_back(std::move(column));
153 signs.push_back(update.strength < 0 ? -1. : 1.);
154 negativeSigns += update.strength < 0;
155 }
156 const auto r = static_cast<Eigen::Index>(columns.size()),
157 size = r + codimension_;
158 signs_ = Eigen::Map<const V>(signs.data(), r);
159 directions_.resize(d, r);
160 for (Eigen::Index j = 0; j < r; ++j)
161 directions_.col(j) = columns[j];
162 for (Eigen::Index j = 0; j < n; ++j) {
163
164
165 M block = codimension_ == 0 ? M(d, 0) : normal.middleRows(j * d, d);
166 if (modes_.size() && codimension_)
167 block = (modes_.transpose() * block).eval();
168 normals_.push_back(std::move(block));
169 }
170 M small = M::Zero(size, size);
171 V rowScales = V::Ones(size);
172 for (Eigen::Index i = 0; i < r; ++i) {
173 for (Eigen::Index j = 0; j < r; ++j)
174 small(i, j) =
175 (directions_.col(i).array() * directions_.col(j).array() *
176 greenNodes(nodes_[i], nodes_[j]).array())
177 .sum();
178 rowScales[i] += small(i, i);
179 small(i, i) += signs[i];
180 for (Eigen::Index j = 0; j < n && codimension_; ++j)
181 small.block(i, r, 1, codimension_).noalias() +=
182 (directions_.col(i).array() *
183 greenNodes(nodes_[i], j).array())
184 .matrix()
185 .transpose() *
186 normals_[j];
187 }
188 if (codimension_) {
189 small.bottomLeftCorner(codimension_, r) =
190 small.topRightCorner(r, codimension_).transpose();
191 for (Eigen::Index i = 0; i < n; ++i)
192 for (Eigen::Index j = 0; j < n; ++j)
193 small.bottomRightCorner(codimension_, codimension_)
194 .noalias() += normals_[i].transpose() *
195 greenNodes(i, j).asDiagonal() *
196 normals_[j];
197 }
198 require(small.allFinite(), "Reduced harmonic response is nonfinite");
199 small = (.5 * small + .5 * small.transpose()).eval();
200 for (Eigen::Index j = 0; j < codimension_; ++j)
201 rowScales[r + j] = small(r + j, r + j);
202 require(rowScales.allFinite() && (rowScales.array() > 0).all(),
203 "Resolved positive reference Gram diagonals required");
204 Eigen::Index negativeSmall = 0, zeroSmall = 0;
205 if (size) {
206 V scales = V::Ones(size);
207 for (Eigen::Index j = 0; j < size; ++j) {
208
209
210 scales[j] = 1 / std::sqrt(rowScales[j]);
211 logRatio_ -= 2 * std::log(scales[j]);
212 }
213 small = (scales.asDiagonal() * small).eval();
214 small = (small * scales.asDiagonal()).eval();
215 Eigen::SelfAdjointEigenSolver<M> spectrum(small);
216 if (spectrum.info() != Eigen::Success ||
217 !spectrum.eigenvalues().allFinite())
218 throw std::runtime_error(
219 "Reduced harmonic response eigensolve fails");
220 smallVectors_ = scales.asDiagonal() * spectrum.eigenvectors();
221 smallValues_ = spectrum.eigenvalues();
222 const double scale =
223 std::max(1., smallValues_.cwiseAbs().maxCoeff());
224 for (double value: smallValues_) {
225 if (std::abs(value) <= unresolvedTolerance * scale)
226 ++zeroSmall;
227 else {
228 negativeSmall += value < 0;
229 logRatio_ += std::log(std::abs(value));
230 }
231 }
232 } else {
233 smallVectors_.resize(0, 0);
234 smallValues_.resize(0);
235 }
236
237 inertia_.negative = negativeSigns - negativeSmall - zeroSmall;
238 inertia_.unresolved = zeroSmall;
239 inertia_.positive =
240 dimension_ - codimension_ - inertia_.negative - zeroSmall;
241 if (inertia_.negative < 0 || inertia_.positive < 0)
242 throw std::runtime_error(
243 "Reduced harmonic inertia exceeds the nodal subspace");
244 }