Loading...
Searching...
No Matches
AtomicDimerInit.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5//
6// AtomicDimerInit.cpp
7// gpr_dimer
8//
9// Created by Maxim Masterov on 16/12/2020.
10//
11
12#include <algorithm>
13#include <cmath>
14#include <limits>
15#include <numbers>
16#include <stdexcept>
17
18#include "AtomicDimer.h"
19#include "gpr/Enums.h"
21#include "managers/io/GpState.h"
22
23namespace atmd {
24
26 const gpr::Observation& init_observations,
27 const gpr::Observation& init_middle_point,
28 const gpr::Coord& init_orientation,
29 const gpr::AtomsConfiguration& init_atoms_config)
30{
31 // Every seed entry uses the same refusal, including a row that never
32 // came from a file. A bad width or a non-finite value stops here.
33 Eigen::Index seed_cols = -1;
34 if (init_middle_point.R.cols() > 0)
35 seed_cols = init_middle_point.R.cols();
36 else if (init_orientation.cols() > 0)
37 seed_cols = init_orientation.cols();
38 gpr::io::refuseNonFiniteSeedRows(init_observations, seed_cols);
39
40 // A non-positive separation is not a dimer stencil. Initialization
41 // refuses it instead of installing a machine-epsilon dedup width.
42 if (!(std::isfinite(parameters.dimer_sep.value) &&
43 parameters.dimer_sep.value > 0.0)) {
44 throw std::invalid_argument("dimer_sep must be finite and positive");
45 }
46 const auto rotation_tolerance_in_range = [](const double value) {
47 return std::isfinite(value) && value > 0.0 &&
48 value < std::numbers::pi_v<double>;
49 };
50 if (!rotation_tolerance_in_range(parameters.T_anglerot_init.value) ||
51 !rotation_tolerance_in_range(parameters.T_anglerot_gp.value)) {
52 throw std::invalid_argument("rotation tolerance must lie in (0, pi)");
53 }
54
55 log_man.setEnabled(parameters.report_level.value >= 1);
56
57 phase_history_.clear();
58 higher_order_restart_ = false;
59 index_search_directions_.resize(0, 0);
60 last_certificate_hessian_.resize(0, 0);
61 last_certificate_positions_.resize(0, 0);
62 last_certificate_force_target_ = std::numeric_limits<double>::infinity();
63 curvature_requires_full_refit_ = false;
64 if (rgmin_trans_) rgmin_trans_->forget();
65 preserve_seed_kernel_ = false;
66
67 if (parameters.method_rot.value == "LBFGS_alg") method_rot = LBFGS_alg;
68 if (parameters.method_trans.value == "LBFGS_alg") method_trans = LBFGS_alg;
69
70 stop_citeria_dimer.force = parameters.T_dimer.value;
71 configured_force_criterion_ = parameters.T_dimer.value;
72 stop_citeria_dimer.angle_rotation = parameters.T_anglerot_init.value;
73
74 convergence_force_norm = static_cast<ConvergenceNormType>(
75 parameters.convergence_force_norm.value);
76
77 stop_citeria_gpr.angle_rotation = parameters.T_anglerot_gp.value;
78 // The live surrogate force stop is surrogateForceTolerance: the current
79 // oracle force divided by divisor_T_dimer_gp, and not below 0.1 * T_dimer.
80 // A non-positive divisor uses that floor alone. stop_citeria_gpr.force is
81 // not that stop.
82 divisor_stop_criteria_gpr = parameters.divisor_T_dimer_gp.value;
83
84 max_iter_init_rot.outer = parameters.num_iter_initrot.value;
85 max_iter_init_rot.inner = parameters.num_iter.value;
86 use_gpr_initial_rotations_ = parameters.initrot_nogp.value == 0;
87
88 max_iter_new_pairs.outer = parameters.num_bigiter.value;
89 max_iter_new_pairs.inner = parameters.num_iter.value;
90
91 max_iter_relax_rot.outer = 1;
92 max_iter_relax_rot.inner = parameters.num_iter_rot_gp.value;
93
94 // A positive value assumes num_iter exceeds what accurate-surface
95 // convergence needs, so a capped inner loop does not resume from the
96 // current geometry. The canonical readcon default 2 and the C default 1
97 // are both on.
98 assume_many_iterations = parameters.islarge_num_iter.value > 0;
99 evaluate_image1_each_phase_ = parameters.eval_image1.value != 0;
100
101 dimer_sep = parameters.dimer_sep.value;
102 ratio_at_limit = parameters.ratio_at_limit.value;
103 actdist_fro = parameters.actdist_fro.value;
104 num_bigiter_initloc = parameters.num_bigiter_initloc.value;
105 num_bigiter_initparam = parameters.num_bigiter_initparam.value;
106
107 // ====================================================================== //
108 // ====================================================================== //
109 gpr::Coord atoms_coords;
110 gpr::FieldMatrixIdx freezed_atoms;
111 cell_dimensions[0] = gpr::vector3_reg(parameters.cell_dimensions.value[0],
112 parameters.cell_dimensions.value[1],
113 parameters.cell_dimensions.value[2]);
114 cell_dimensions[1] = gpr::vector3_reg(parameters.cell_dimensions.value[3],
115 parameters.cell_dimensions.value[4],
116 parameters.cell_dimensions.value[5]);
117 cell_dimensions[2] = gpr::vector3_reg(parameters.cell_dimensions.value[6],
118 parameters.cell_dimensions.value[7],
119 parameters.cell_dimensions.value[8]);
120
121 orient_init = init_orientation;
122
123 middle_point_init = init_middle_point;
124 all_obs = init_observations;
125 refit_oracle_batch_.clear();
126 N_obs_init = all_obs.R.rows();
127 markObservedDataChanged();
128
129 atoms_config = init_atoms_config;
130 if (parameters.gp_nonperiodic.value) {
131 std::fill(std::begin(atoms_config.cell), std::end(atoms_config.cell),
132 0.0);
133 } else {
135 parameters.cell_dimensions.value);
136 }
137
138 // The oracle box stays parameters.cell_dimensions, including a 25 angstrom
139 // vacuum box. copyMicLattice does not install that box on the GP cell, and
140 // gp_nonperiodic zeros only the GP cell. Log the split; do not retarget
141 // the oracle.
142 {
143 const auto oracle_component = [&](const int index) {
144 return cell_dimensions[index / 3](index % 3);
145 };
146 bool cells_differ = false;
147 for (int index = 0; index < 9; ++index) {
148 if (atoms_config.cell[index] != oracle_component(index)) {
149 cells_differ = true;
150 break;
151 }
152 }
153 if (cells_differ) {
154 log_man << "Oracle cell and GP cell differ: oracle";
155 for (int index = 0; index < 9; ++index)
156 log_man << " " << oracle_component(index);
157 log_man << "; GP";
158 for (int index = 0; index < 9; ++index)
159 log_man << " " << atoms_config.cell[index];
160 log_man << ".\n";
161 }
162 }
163
164 // Self-owned path: build the GP descriptor here. External-GP path
165 // (~attachExternalGP~ was called before this): the caller has
166 // already initialised the GP from their session, and re-running
167 // ~initialize~ here would reset SCG/Cholesky scratch and the
168 // hyperparameter state the caller has been accumulating across
169 // outer iterations. Skip in that case.
170 if (!uses_external_gp_) {
171 if (gpr_model == nullptr) {
172 owned_gpr_model_ =
173 std::make_unique<gpr::GaussianProcessRegression>();
174 gpr_model = owned_gpr_model_.get();
175 }
176 gpr_model->initialize(parameters, atoms_config);
177 gpr_model->setModelProfile(self_owned_model_profile_);
178 gpr_model->setUseZBLPriorMean(self_owned_use_zbl_prior_mean_);
179 gpr_model->setUseFFPriorMean(self_owned_use_ff_prior_mean_);
180 // The self-owned surrogate fits exact NLML with SCG. A covering
181 // derivative layout, cutoff 0 included, uses the pair tiles. An
182 // attached session GP keeps the owner's switch.
183 if (gpr_model->pairLocalTilesEnabled())
184 gpr_model->setPairLocalDirectionalTiles(true);
185 }
186
187 transition_param.step_length = parameters.param_trans.value[0];
188 transition_param.max_step_length = parameters.param_trans.value[1];
189 transition_param.rotrem_thresh =
191
192 early_params._dist_metric = parameters.es_dist_metric.value;
193 early_params._threshold = parameters.es_threshold.value;
194 // disp_max is the fixed midpoint displacement cap from the nearest
195 // observation. A positive finite cap tightens the fixed trust threshold.
196 // es_threshold keeps its own default; a non-positive disp_max leaves that
197 // threshold unchanged. The adaptive schedule does not read this value.
198 if (std::isfinite(parameters.disp_max.value) &&
199 parameters.disp_max.value > 0.0) {
200 if (std::isfinite(early_params._threshold)) {
201 early_params._threshold =
202 std::min(early_params._threshold, parameters.disp_max.value);
203 } else {
204 early_params._threshold = parameters.disp_max.value;
205 }
206 }
207
208 // The midpoint dedup threshold is the dimer finite-difference stencil
209 // diameter. Initialization refuses a non-positive separation, so this
210 // width is not replaced by machine epsilon.
211 emd_dedup_threshold_ = 2.0 * dimer_sep;
212
213 // The stencil diameter is also the finest PES structure the dimer's
214 // sampling resolves: floor the kernel length scale there so the MAP
215 // projection cannot keep a collapsed delta-comb fit whose relaxation
216 // endpoints scatter into EMD-distinct probes every phase. Only the
217 // self-owned GP takes the dimer's floor; an attached session GP
218 // carries the chain-derived controls of its owner.
219 if (!uses_external_gp_ && gpr_model != nullptr) {
220 gpr_model->setLengthscaleFloor(emd_dedup_threshold_);
221 }
222
223 early_params._use_adaptive_threshold =
225 early_params._adaptive_A = parameters.es_adaptive_A.value;
226 early_params._adaptive_floor = parameters.es_adaptive_floor.value;
227 early_params._adaptive_t_min = parameters.es_t_min.value;
228 early_params._adaptive_delta_explore = parameters.es_delta_explore.value;
229 early_params._adaptive_half_life = parameters.es_half_life.value;
230
231 debug_output_info.out_dir = parameters.debug_output_dir.value;
232 debug_output_info.file_name_E = parameters.debug_output_file_E.value;
233 debug_output_info.file_name_R = parameters.debug_output_file_R.value;
234 debug_output_info.file_name_G = parameters.debug_output_file_G.value;
235 debug_output_info.file_extension =
237 debug_output_info.offset = parameters.debug_offset_from_mid_point.value;
238 debug_output_info.dy = parameters.debug_dy.value;
239 debug_output_info.dz = parameters.debug_dz.value;
240 debug_level = parameters.debug_level.value;
241 profile_phase_timing_ = parameters.profile_dimer_times.value;
242}
243
244} /* namespace atmd */
@ LBFGS_alg
Definition Enums.h:32
ConvergenceNormType
Definition Enums.h:48
void initialize(const gpr::InputParameters &parameters, const gpr::Observation &initial_data, const gpr::Observation &middle_point, const gpr::Coord &initial_orientation, const gpr::AtomsConfiguration &init_atom_config)
Initilalize with initial data.
gpr::AtomsConfiguration atoms_config
The atomic dimer search (class AtomicDimer) and its status types.
bool copyMicLattice(double dest[9], const double src[9]) noexcept
Copy src onto dest only when src is a MIC lattice.
Definition MinImage.h:68
void refuseNonFiniteSeedRows(const Observation &observations, Eigen::Index expected_cols)
Definition GpState.cpp:587
Eigen::Vector3d vector3_reg
Definition Macros.h:36
Eigen::Matrix< Index_t, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixIdx
Definition Macros.h:29
FieldMatrixd Coord
Definition Macros.h:35
Stores configurations of all atoms in the system.
Definition Structures.h:832
Structure of the input parameters. All input parameters are pairs of string names and values.
Definition Structures.h:370
KeyValuePair< double[2]> param_trans
Definition Structures.h:440
KeyValuePair< double > T_anglerot_gp
Definition Structures.h:484
KeyValuePair< double > es_t_min
Definition Structures.h:659
KeyValuePair< std::string > debug_output_file_G
Definition Structures.h:589
KeyValuePair< int > initrot_nogp
Definition Structures.h:467
KeyValuePair< DistanceMetricType > es_dist_metric
Definition Structures.h:652
KeyValuePair< bool > es_use_adaptive_threshold
Definition Structures.h:655
KeyValuePair< double[9]> cell_dimensions
Definition Structures.h:605
KeyValuePair< double > dimer_sep
Definition Structures.h:425
KeyValuePair< int > islarge_num_iter
Definition Structures.h:544
KeyValuePair< int > report_level
Definition Structures.h:673
KeyValuePair< double > rotation_removal_projection_threshold
Definition Structures.h:667
KeyValuePair< double > T_anglerot_init
Definition Structures.h:474
KeyValuePair< std::string > debug_output_dir
Definition Structures.h:584
KeyValuePair< double > ratio_at_limit
Definition Structures.h:512
KeyValuePair< int > debug_level
Definition Structures.h:597
KeyValuePair< std::string > method_trans
Definition Structures.h:435
KeyValuePair< double > debug_dz
Definition Structures.h:596
KeyValuePair< int > gp_nonperiodic
Definition Structures.h:603
KeyValuePair< double > es_delta_explore
Definition Structures.h:660
KeyValuePair< int > num_bigiter_initloc
Definition Structures.h:519
KeyValuePair< int > convergence_force_norm
Definition Structures.h:461
KeyValuePair< double > actdist_fro
Definition Structures.h:379
KeyValuePair< int > num_bigiter_initparam
Definition Structures.h:527
KeyValuePair< std::string > method_rot
Definition Structures.h:430
KeyValuePair< int > divisor_T_dimer_gp
Definition Structures.h:498
KeyValuePair< int > num_iter
Definition Structures.h:537
KeyValuePair< int > num_iter_initrot
Definition Structures.h:479
KeyValuePair< int > eval_image1
Definition Structures.h:446
KeyValuePair< double > debug_offset_from_mid_point
Definition Structures.h:593
KeyValuePair< double > disp_max
Definition Structures.h:505
KeyValuePair< bool > profile_dimer_times
Definition Structures.h:633
KeyValuePair< std::string > debug_output_file_E
Definition Structures.h:587
KeyValuePair< double > es_adaptive_A
Definition Structures.h:657
KeyValuePair< int > es_half_life
Definition Structures.h:661
KeyValuePair< std::string > debug_output_file_extension
Definition Structures.h:591
KeyValuePair< int > num_bigiter
Definition Structures.h:532
KeyValuePair< double > es_threshold
Definition Structures.h:654
KeyValuePair< double > debug_dy
Definition Structures.h:595
KeyValuePair< std::string > debug_output_file_R
Definition Structures.h:585
KeyValuePair< double > es_adaptive_floor
Definition Structures.h:658
KeyValuePair< double > T_dimer
Definition Structures.h:453
KeyValuePair< int > num_iter_rot_gp
Definition Structures.h:490
Observation data: energy, gradient and positions.