Loading...
Searching...
No Matches
dimer::SphereRotation Class Reference

#include <gpr/dimer/SphereRotation.h>

Public Types

using Evaluator = std::function<bool(gpr::Observation&)>

Public Member Functions

bool start (const gpr::Coord &center, const Eigen::VectorXd &gradient, double separation, const Eigen::MatrixXd &physical, std::size_t memory, const gpr::Observation &endpoint, const gpr::Observation *previous=nullptr)
bool step (const gpr::Coord &center, const gpr::Coord &orientation, const Eigen::VectorXd &gradient, double separation, const Eigen::MatrixXd &physical, std::size_t memory, const Evaluator &evaluate, const gpr::Observation *initial_endpoint, gpr::Coord &rotated, gpr::Observation &endpoint)

Detailed Description

A persistent orientation solve at one measured midpoint. The objective is [E(R+h v)-E(reference)-g(R).(R+h v-reference)]/h^2 on v=Q u, where u is a unit vector. Its gradient is Q^T [g(R+h v)-g(R)]/h.

Definition at line 23 of file SphereRotation.h.

Member Typedef Documentation

◆ Evaluator

using dimer::SphereRotation::Evaluator = std::function<bool(gpr::Observation&)>

Definition at line 25 of file SphereRotation.h.

Member Function Documentation

◆ start()

bool dimer::SphereRotation::start ( const gpr::Coord & center,
const Eigen::VectorXd & gradient,
double separation,
const Eigen::MatrixXd & physical,
std::size_t memory,
const gpr::Observation & endpoint,
const gpr::Observation * previous = nullptr )
inline

Definition at line 27 of file SphereRotation.h.

31 {
32 center_ = center;
33 gradient_ = gradient;
34 physical_ = physical;
35 separation_ = separation;
36 if (!valid(endpoint) || physical.cols() == 0) return false;
37 reference_ = endpoint;
38 accepted_ = endpoint;
39 accepted_axis_.resize(0);
40 accepted_orientation_.resize(0, 0);
41 rgmin_control_t control{};
42 control.maxiter = 1;
43 control.gtol = 0.0;
44 control.istep = 1.0;
45 control.memory = memory;
47 solver_.reset(
48 rgmin_solver_create(RGMIN_LBFGS, &control, physical.cols()));
49 if (!solver_) return false;
50 rgmin_solver_set_manifold(solver_.get(), RGMIN_MANIFOLD_SPHERE);
51 rgmin_solver_set_accept(solver_.get(), RGMIN_ACCEPT_ENERGY);
52 if (previous && valid(*previous)) {
53 const Eigen::VectorXd old_axis =
54 (physical_.transpose() *
55 (previous->R - center_).row(0).transpose())
56 .normalized();
57 const Eigen::VectorXd axis =
58 (physical_.transpose() *
59 (endpoint.R - center_).row(0).transpose())
60 .normalized();
61 Eigen::VectorXd old_gradient =
62 physical_.transpose() *
63 (previous->G.row(0).transpose() - gradient_) / separation_;
64 Eigen::VectorXd new_gradient =
65 physical_.transpose() *
66 (endpoint.G.row(0).transpose() - gradient_) / separation_;
67 old_gradient -= old_axis.dot(old_gradient) * old_axis;
68 new_gradient -= axis.dot(new_gradient) * axis;
69 Eigen::VectorXd displacement = axis - old_axis;
70 displacement -= axis.dot(displacement) * axis;
71 const Eigen::VectorXd change =
72 new_gradient - old_gradient + axis.dot(old_gradient) * axis;
73 rgmin_solver_push_pair(solver_.get(), displacement.data(),
74 change.data(), axis.size());
75 }
76 return true;
77 }
void requireCompatibleLibrary()
Definition RgminAbi.h:90

◆ step()

bool dimer::SphereRotation::step ( const gpr::Coord & center,
const gpr::Coord & orientation,
const Eigen::VectorXd & gradient,
double separation,
const Eigen::MatrixXd & physical,
std::size_t memory,
const Evaluator & evaluate,
const gpr::Observation * initial_endpoint,
gpr::Coord & rotated,
gpr::Observation & endpoint )
inline

Definition at line 79 of file SphereRotation.h.

85 {
86 if (physical.rows() != center.cols() || physical.cols() == 0 ||
87 !physical.allFinite())
88 return false;
89 const bool same_problem =
90 solver_ && separation_ == separation && equal(center_, center) &&
91 equal(gradient_, gradient) && equal(physical_, physical);
92 Eigen::VectorXd axis;
93 if (same_problem && equal(orientation, accepted_orientation_)) {
94 // The accepted reduced coordinate belongs to the solver state.
95 // A Cartesian round trip must not introduce an external secant.
96 axis = accepted_axis_;
97 } else {
98 axis = physical.transpose() * orientation.row(0).transpose();
99 const double norm = axis.norm();
100 if (!(norm > 0.0) || !std::isfinite(norm)) return false;
101 axis /= norm;
102 }
103 if (!same_problem) {
104 center_ = center;
105 gradient_ = gradient;
106 physical_ = physical;
107 separation_ = separation;
108 gpr::Observation seed;
109 seed.R = geometry(axis);
110 if (initial_endpoint && initial_endpoint->R.isApprox(seed.R)) {
111 seed = *initial_endpoint;
112 } else if (!evaluate(seed)) {
113 return false;
114 }
115 if (!start(center, gradient, separation, physical, memory, seed))
116 return false;
117 }
118
119 struct Context {
120 SphereRotation& rotation;
121 const Evaluator& evaluate;
122 gpr::Observation last;
123 bool failed = false;
124 } context{*this, evaluate, accepted_};
125 const auto callback = [](void* user, const DLManagedTensorVersioned* x,
126 double* value,
127 DLManagedTensorVersioned* g) noexcept {
128 auto& ctx = *static_cast<Context*>(user);
129 auto& state = ctx.rotation;
130 try {
131 const auto* xp = reinterpret_cast<const double*>(
132 static_cast<const uint8_t*>(x->dl_tensor.data) +
133 x->dl_tensor.byte_offset);
134 auto* gp = reinterpret_cast<double*>(
135 static_cast<uint8_t*>(g->dl_tensor.data) +
136 g->dl_tensor.byte_offset);
137 const Eigen::Map<const Eigen::VectorXd> u(
138 xp, state.physical_.cols());
139 gpr::Observation image;
140 image.R = state.geometry(u);
141 if (equal(image.R, state.accepted_.R)) {
142 image = state.accepted_;
143 } else if (!ctx.evaluate(image)) {
144 ctx.failed = true;
145 return RGMIN_INTERNAL_ERROR;
146 }
147 if (!state.valid(image)) {
148 ctx.failed = true;
149 return RGMIN_INTERNAL_ERROR;
150 }
151 const double linear =
152 state.gradient_.dot((image.R - state.reference_.R).row(0));
153 *value = (image.E(0, 0) - state.reference_.E(0, 0) - linear) /
154 (state.separation_ * state.separation_);
155 Eigen::Map<Eigen::VectorXd> output(gp, state.physical_.cols());
156 output = state.physical_.transpose() *
157 (image.G.row(0).transpose() - state.gradient_) /
158 state.separation_;
159 ctx.last = std::move(image);
160 return RGMIN_SUCCESS;
161 } catch (...) {
162 ctx.failed = true;
163 return RGMIN_INTERNAL_ERROR;
164 }
165 };
166 using Tensor = std::unique_ptr<DLManagedTensorVersioned,
167 decltype(&rgmin_tensor_free)>;
168 Tensor tensor(rgmin_tensor_borrow_cpu_f64(axis.data(), axis.size()),
169 rgmin_tensor_free);
170 rgmin_report_t report{};
171 if (!tensor ||
172 rgmin_solver_step_fg(solver_.get(), callback, &context,
173 tensor.get(), &report) != RGMIN_SUCCESS ||
174 context.failed || !axis.allFinite())
175 return false;
176 const gpr::Coord position = geometry(axis);
177 if (equal(position, context.last.R)) {
178 endpoint = std::move(context.last);
179 } else if (equal(position, accepted_.R)) {
180 endpoint = accepted_;
181 } else {
182 endpoint.R = position;
183 if (!evaluate(endpoint) || !valid(endpoint)) return false;
184 }
185 rotated = (physical_ * axis).transpose();
186 accepted_axis_ = axis;
187 accepted_orientation_ = rotated;
188 accepted_ = endpoint;
189 return true;
190 }
@ x
Definition Enums.h:28
bool start(const gpr::Coord &center, const Eigen::VectorXd &gradient, double separation, const Eigen::MatrixXd &physical, std::size_t memory, const gpr::Observation &endpoint, const gpr::Observation *previous=nullptr)
eon_relax_band_t eon_relax_surface_fn void * user
VecchiaResult evaluate(const Eigen::MatrixXd &points, const Eigen::VectorXd &y, double lengthscale, double noise, int max_neighbours)
FieldMatrixd Coord
Definition Macros.h:35
FieldMatrixd E

The documentation for this class was generated from the following file: