52 const Action &action,
const std::vector<Eigen::VectorXd> &points,
53 double duration,
const std::vector<PotentialEvaluation> &native,
55 const std::function<std::vector<PotentialEvaluation>(
56 const std::vector<Eigen::VectorXd> &)> &ownedEvaluate)
58 using namespace arc_force_gradient_detail;
60 if constexpr (
requires { action.positiveQuadrature(); })
62 throw std::invalid_argument(
63 "Positive primitive quadrature requires a separately specified "
64 "force-gradient scheme");
66 const auto &well = action.wellCurvature();
67 const auto dimension = well.rows();
68 if (points.size() != action.arcWeights().size() + 1 ||
69 !(duration > 0 && duration < action.period()) ||
70 !std::isfinite(duration))
71 throw std::invalid_argument(
72 "Matching arc nodes and finite duration required");
73 requireValues(points, native, dimension);
79 for (std::size_t j = 0; j < points.size(); ++j) {
80 const V residual = native[j].gradient - well * points[j];
81 if (!residual.allFinite())
82 throw std::overflow_error(
"Nonfinite native residual gradient");
88 if (response.size() != points.size())
89 throw std::invalid_argument(
90 "Physical Hessian-action batch shape differs");
91 for (std::size_t j = 0; j < points.size(); ++j) {
92 if (response[j].size() != dimension || !response[j].allFinite())
93 throw std::invalid_argument(
94 "Finite matching physical Hessian actions required");
98 throw std::overflow_error(
99 "Nonfinite physical residual Hessian action");
108 throw std::invalid_argument(
109 "Owned native differences need positive finite scales");
111 std::vector<std::size_t> active;
112 std::vector<double> norms(points.size(), 0.);
113 for (std::size_t j = 0; j < points.size(); ++j) {
115 if (!std::isfinite(norms[j]))
116 throw std::overflow_error(
"Nonfinite residual-gradient norm");
117 if (norms[j] > 0) active.push_back(j);
119 if (active.empty())
return data;
121 auto difference = [&](
double step) {
122 std::vector<V> probes;
123 probes.reserve(2 * active.size());
124 for (
const auto j: active) {
126 probes.push_back(points[j] + displacement);
127 probes.push_back(points[j] - displacement);
129 const auto measured = ownedEvaluate(probes);
130 requireValues(probes, measured, dimension);
131 std::vector<V> response(points.size(), V::Zero(dimension));
132 for (std::size_t slot = 0; slot < active.size(); ++slot) {
133 const auto j = active[slot];
134 response[j] = norms[j] *
135 (measured[2 * slot].gradient -
136 measured[2 * slot + 1].gradient) /
139 if (!response[j].allFinite())
140 throw std::overflow_error(
141 "Nonfinite native Hessian difference");
147 auto coarse = difference(step);
148 for (std::size_t refinement = 0;
151 const auto fine = difference(step);
153 for (
const auto j: active) {
154 const V correction = (fine[j] - coarse[j]) / 3.;
157 !correction.allFinite())
158 throw std::overflow_error(
159 "Nonfinite extrapolated native Hessian action");
160 const double h = effectiveStep(action, j, duration);
168 throw std::runtime_error(
169 "Native residual Hessian action is unresolved at the force tolerance");
178 const Action &action,
const std::vector<Eigen::VectorXd> &points,
179 const std::vector<PotentialEvaluation> &native,
double duration,
181 const std::vector<Eigen::MatrixXd> &searchHessians = {},
182 const std::vector<Eigen::MatrixXd> &searchThirdContractions = {})
184 using namespace arc_force_gradient_detail;
186 auto out = action.evaluate(points, native, duration, searchHessians);
187 out.fluctuationHessianPhysical =
false;
190 const auto n = points.size();
191 const auto &well = action.wellCurvature();
192 const auto dimension = well.rows();
193 requireValues(points, native, dimension);
196 n != action.arcWeights().size() + 1 ||
197 (!searchHessians.empty() && searchHessians.size() != n) ||
198 (!searchThirdContractions.empty() &&
199 (searchThirdContractions.size() != n || searchHessians.empty())))
200 throw std::invalid_argument(
201 "Force-gradient data belongs to a different arc");
202 auto dressed = native;
203 auto curvature = searchHessians;
204 std::vector<V> actionGradient(n, V::Zero(dimension));
205 double actionCorrection = 0.;
206 for (std::size_t j = 0; j < n; ++j) {
208 (data.
coordinates[j].array() != points[j].array()).any() ||
213 throw std::invalid_argument(
214 "Force-gradient response or geometry differs");
215 const V residual = native[j].gradient - well * points[j];
217 throw std::invalid_argument(
218 "Force-gradient data belongs to different native values");
219 const double h = effectiveStep(action, j, duration);
222 [&data, j](
const V &) -> V { return data.residualResponses[j]; },
224 dressed[j].energy += correction.potentialCorrection;
225 dressed[j].gradient += correction.gradientCorrection;
226 const auto &weights = action.arcWeights();
227 const double time = .5 * duration *
228 ((j ? weights[j - 1] : 0.) +
229 (j < weights.size() ? weights[j] : 0.));
230 actionCorrection += time * correction.potentialCorrection;
231 actionGradient[j] = time * correction.gradientCorrection;
232 if (!curvature.empty()) {
233 if (curvature[j].rows() != dimension ||
234 curvature[j].cols() != dimension || !curvature[j].allFinite() ||
235 (curvature[j] - curvature[j].transpose()).norm() >
236 1e-12 * std::max(1., curvature[j].norm()))
237 throw std::invalid_argument(
238 "Finite symmetric search curvature required");
239 const M residual = curvature[j] - well;
240 curvature[j].noalias() += h * h / 12. * residual * residual;
241 if (!searchThirdContractions.empty()) {
242 const auto &third = searchThirdContractions[j];
243 if (third.rows() != dimension || third.cols() != dimension ||
244 !third.allFinite() ||
245 (third - third.transpose()).norm() >
246 1e-12 * std::max(1., third.norm()))
247 throw std::invalid_argument(
248 "Finite symmetric search third contraction required");
249 curvature[j] += h * h / 12. * third;
253 auto out = action.evaluate(points, dressed, duration, curvature);
254 out.durationGradient += 2 * actionCorrection / duration;
255 out.durationHessian += 6 * actionCorrection / (duration * duration);
256 for (std::size_t j = 0; j < n; ++j)
257 out.durationCrossGradient[j] += 2 * actionGradient[j] / duration;
258 out.fluctuationHessianPhysical =
false;
259 if (!std::isfinite(
out.durationGradient) ||
260 !std::isfinite(
out.durationHessian))
261 throw std::overflow_error(
"Nonfinite corrected duration derivative");
262 for (
const auto &cross:
out.durationCrossGradient)
263 if (!cross.allFinite())
264 throw std::overflow_error(
"Nonfinite corrected mixed derivative");
ArcTailForceGradientData prepareArcTailForceGradient(const Action &action, const std::vector< Eigen::VectorXd > &points, double duration, const std::vector< PotentialEvaluation > &native, const ArcTailPotential &potential, const ArcTailSearchOptions &options, const std::function< std::vector< PotentialEvaluation >(const std::vector< Eigen::VectorXd > &)> &ownedEvaluate)