33#ifdef GPR_LINALG_SCALAPACK
47 if (E_R.size() == 0)
return;
54 if (E_reference.rows() == E_R.rows() && E_reference.cols() == E_R.cols()) {
55 E_R = E_R - E_reference;
58 if (E_reference.size() == 1) {
59 const double e0 = E_reference(0, 0);
64 if (E_reference.cols() == E_R.cols() && E_reference.rows() == 1) {
65 E_R.rowwise() -= E_reference.row(0);
69 if (E_reference.rows() == E_R.rows() && E_reference.cols() == 1) {
70 E_R.colwise() -= E_reference.col(0);
76 E_R = E_R - E_reference;
88 const Eigen::Index n_atoms = atoms_config.
is_frozen.size();
90 if (n_moving == n_atoms || R.cols() != 3 * n_atoms)
return R;
92 for (Eigen::Index i = 0; i < R.rows(); ++i) {
94 for (Eigen::Index a = 0; a < n_atoms; ++a) {
96 for (Eigen::Index c = 0; c < 3; ++c)
97 packed(i, 3 * m + c) = R(i, 3 * a + c);
112template <
class Position>
114 const std::vector<gpr::Index_t>& order,
115 const Position& position,
116 const gpr::geometry::PairBox& box)
118 const auto pool =
static_cast<gpr::Index_t>(order.size());
121 if (n < 3)
return false;
122 const Eigen::Vector3d base = position(order[0]);
123 const Eigen::Vector3d u = box.
displacement(base, position(order[1]));
125 const Eigen::Vector3d v =
127 if (u.cross(v).norm() > 1e-6 * u.norm() * v.norm())
return true;
132 if (spansPlane(n))
return n;
139 const double activation_distance,
142 const gpr::Coord R = movingColumns(R_given, atoms_config);
148 std::vector<int> activated_atoms(inactive_atoms->
positions.cols() / 3,
150 std::vector<gpr::Index_t> activated_atomtypes;
151 bool active_set_changed =
false;
159 inactive_atoms->
type.size() != 0 || active_atoms->
type.size() != 0,
160 "Error! Frozen atoms exist but neither the active nor the "
161 "inactive frozen set holds any!\n");
171 active_set_changed = active_atoms->
type.size() != n_active_before_cleanup;
183 if (pool == 0)
return active_set_changed;
196 return k < n_act_old ? *(oa_type.data() + k)
197 : *(oi_type.data() + (k - n_act_old));
200 std::vector<double> mindist(pool);
202 const Eigen::Vector3d p = pool_pos(k);
203 double dmin = std::numeric_limits<double>::infinity();
204 for (Eigen::Index i = 0; i < R.rows(); ++i)
205 for (Eigen::Index m = 0, end = R.cols() / 3; m < end; ++m)
210 std::vector<gpr::Index_t> order(pool);
211 std::iota(order.begin(), order.end(), 0);
212 std::stable_sort(order.begin(), order.end(),
214 return mindist[a] < mindist[b];
217 anchorsFixingRigidMotion(nn_frozen, order, pool_pos, box);
222 bool changed = active_set_changed || (n_new_act != n_act_old);
223 for (
gpr::Index_t r = 0; r < n_new_act && !changed; ++r)
224 if (order[r] >= n_act_old) changed =
true;
229 if (!changed)
return false;
230 if (n_new_act > nn_frozen)
231 std::cout <<
"\n Using " << n_new_act <<
" nearest frozen atoms "
232 <<
"instead of " << nn_frozen <<
": fewer anchors leave "
233 <<
"a rigid rotation of the moving atoms that pair "
234 <<
"distances do not see.\n";
236 active_atoms->
resize(n_new_act);
237 inactive_atoms->
resize(pool - n_new_act);
242 *(active_atoms->
type.data() + r) = pool_type(src);
246 *(inactive_atoms->
type.data() + d) = pool_type(src);
252 std::set<gpr::Index_t> umov(
253 moving_atoms->
type.data(),
254 moving_atoms->
type.data() + moving_atoms->
type.size());
255 std::set<gpr::Index_t> uact(
256 active_atoms->
type.data(),
257 active_atoms->
type.data() + active_atoms->
type.size());
258 for (
const auto& ai: umov) {
259 for (
const auto& aj: uact) {
274 if (inactive_atoms->
positions.size() != 0) {
275 if (activation_distance > 0.) {
280 std::vector<int> activated_atoms(num_inactive_atoms,
EMPTY);
281 for (Eigen::Index i = 0; i < R.rows(); ++i) {
283 Eigen::Vector3d atom_fro_inactive =
285 for (Eigen::Index m = 0, end = R.cols() / 3; m < end; ++m) {
286 if (box.
dist2(atom_fro_inactive,
288 activation_distance * activation_distance)
289 activated_atoms[n] = n;
295 long num_activated_atoms = activated_atoms.size() -
296 std::count(activated_atoms.begin(),
297 activated_atoms.end(),
EMPTY);
300 if (num_activated_atoms != 0) {
317 std::cout <<
"\n Now we have " << tot_activated_atoms
318 <<
" total active atoms\n";
319 active_atoms->
resize(tot_activated_atoms);
320 activated_atomtypes.resize(num_activated_atoms);
327 *(active_atoms->
type.data() + k) =
328 *(existing_type.data() + k);
334 for (
gpr::Index_t n = 0; n < activated_atoms.size(); ++n) {
335 if (activated_atoms[n] !=
EMPTY) {
339 *(active_atoms->
type.data() + counter) =
340 *(inactive_atoms->
type.data() + n);
341 activated_atomtypes[new_idx] =
342 *(active_atoms->
type.data() + counter);
349 for (
int n = (
int)activated_atoms.size() - 1; n >= 0; --n) {
350 if (activated_atoms[n] !=
EMPTY) {
358 active_set_changed =
true;
362 active_set_changed =
true;
364 inactive_atoms->
clear();
368 if (active_set_changed) {
370 std::set<gpr::Index_t> unique_atomtype_mov(
371 moving_atoms->
type.data(),
372 moving_atoms->
type.data() + moving_atoms->
type.size());
373 std::set<gpr::Index_t> unique_atomtype_activated(
374 activated_atomtypes.begin(), activated_atomtypes.end());
378 for (
auto& at_i: unique_atomtype_mov) {
379 for (
auto& at_j: unique_atomtype_activated) {
407 if (atoms_config.
evict_frozen && activation_distance > 0. &&
411 std::vector<gpr::Index_t> keep, evict;
412 keep.reserve(n_active);
414 const Eigen::Vector3d pa =
417 for (Eigen::Index i = 0; i < R.rows() && !within; ++i) {
418 for (Eigen::Index m = 0, end = R.cols() / 3; m < end; ++m) {
420 activation_distance * activation_distance) {
431 if (!evict.empty()) {
441 for (std::size_t k = 0; k < keep.size(); ++k) {
445 *(active_atoms->
type.data() + k) =
446 *(old_active_type.data() + keep[k]);
451 inactive_atoms->
resize(n_inactive_old +
456 *(inactive_atoms->
type.data() + k) =
457 *(old_inactive_type.data() + k);
459 for (std::size_t e = 0; e < evict.size(); ++e) {
464 *(inactive_atoms->
type.data() + dst) =
465 *(old_active_type.data() + evict[e]);
467 std::cout <<
"\n Evicted " << evict.size() <<
" frozen atoms; "
468 << keep.size() <<
" remain active\n";
469 active_set_changed =
true;
472 return active_set_changed;
479 const Eigen::Index num_atoms = atoms_config.
is_frozen.size();
480 const Eigen::Index num_images = unfreezed_atoms.rows();
482 coord_atom_new.resize(num_images, atoms_config.
positions.cols());
483 for (Eigen::Index row = 0; row < num_images; ++row) {
484 const Eigen::Index source_row =
485 atoms_config.
positions.rows() == num_images ? row : 0;
486 coord_atom_new.row(row) = atoms_config.
positions.row(source_row);
489 for (Eigen::Index n = 0; n < num_atoms; ++n) {
506 Eigen::Index N_mov = atomtype_mov.size();
507 for (Eigen::Index i = 0; i < N_mov - 1; ++i) {
509 for (Eigen::Index j = i + 1; j < N_mov; ++j) {
511 if (pairtype(at_i, at_j) ==
EMPTY) {
513 pairtype(at_i, at_j) = n_pt +
EMPTY;
514 pairtype(at_j, at_i) = n_pt +
EMPTY;
525 std::vector<gpr::Index_t> clones_act;
526 std::vector<gpr::Index_t> clones_inact;
527 Eigen::Index num_active_atoms = active_atoms.
type.size();
528 Eigen::Index num_inactive_atoms = inactive_atoms.
type.size();
529 Eigen::Index num_moving_atoms = moving_atoms.
type.size();
531 if (num_moving_atoms == 0)
return;
533 for (Eigen::Index m = 0; m < num_moving_atoms; ++m) {
535 for (Eigen::Index n = 0; n < num_active_atoms; ++n) {
536 Eigen::Vector3d act_at_pos =
538 if ((mov_at_pos - act_at_pos).norm() <= DBL_EPSILON) {
539 clones_act.push_back(n);
542 for (Eigen::Index n = 0; n < num_inactive_atoms; ++n) {
543 Eigen::Vector3d inact_at_pos =
545 if ((mov_at_pos - inact_at_pos).norm() <= DBL_EPSILON) {
546 clones_inact.push_back(n);
552 std::sort(clones_act.begin(), clones_act.end());
553 auto last_act = std::unique(clones_act.begin(), clones_act.end());
554 clones_act.erase(last_act, clones_act.end());
556 std::sort(clones_inact.begin(), clones_inact.end());
557 auto last_inact = std::unique(clones_inact.begin(), clones_inact.end());
558 clones_inact.erase(last_inact, clones_inact.end());
560 eraseClones(clones_act, active_atoms);
561 eraseClones(clones_inact, inactive_atoms);
564void ProblemSetUp::eraseClones(
const std::vector<gpr::Index_t>& clones,
567 if (!clones.empty()) {
569 log_man <<
"Warning! Some (in)active frozen atoms were discovered as "
570 "duplicates of the moving atoms and will be removed from "
572 for (
int n = (
int)clones.size() - 1; n >= 0; --n) {
585 const Eigen::Index num_im = middle_point.
R.rows();
586 const Eigen::Index dim = middle_point.
R.cols();
587 const Eigen::Index num_atoms = atoms_config.
is_frozen.size();
591 box[0] = cell_dimensions[0](0);
592 box[1] = cell_dimensions[0](1);
593 box[2] = cell_dimensions[0](2);
595 box[3] = cell_dimensions[1](0);
596 box[4] = cell_dimensions[1](1);
597 box[5] = cell_dimensions[1](2);
599 box[6] = cell_dimensions[2](0);
600 box[7] = cell_dimensions[2](1);
601 box[8] = cell_dimensions[2](2);
603 middle_point.
E.resize(num_im, 1);
604 middle_point.
E.setZero();
605 middle_point.
G.resize(num_im, dim);
606 middle_point.
G.setZero();
608 updateConf(middle_point.
R, atoms_config, coord_atom_new);
609 const Eigen::Index full_dim = coord_atom_new.cols();
625 ?
static_cast<int>(num_of_calls %
634 std::vector<double> energies(
static_cast<std::size_t
>(num_im), 0.0);
635 Eigen::MatrixXd gradients = Eigen::MatrixXd::Zero(num_im, full_dim);
638 const auto nonfinite = [](
double energy,
const Eigen::RowVectorXd& grad) {
639 return !std::isfinite(energy) || !grad.allFinite();
642 if (!failure.empty())
break;
645 const std::size_t B = group.size();
646 const auto stride =
static_cast<std::size_t
>(full_dim);
647 std::vector<double> R(B * stride);
648 std::vector<double> F(B * stride,
649 std::numeric_limits<double>::quiet_NaN());
650 std::vector<double> U(B,
651 std::numeric_limits<double>::quiet_NaN());
652 for (std::size_t b = 0; b < B; ++b) {
653 const Eigen::Index n = group[b];
654 std::copy(coord_atom_new.row(n).data(),
655 coord_atom_new.row(n).data() + full_dim,
656 R.data() + b * stride);
659 general_potential.
forceBatch(num_atoms, B, R.data(),
661 F.data(), U.data(),
nullptr, box);
662 for (std::size_t b = 0; b < B; ++b) {
663 const Eigen::Index n = group[b];
664 energies[
static_cast<std::size_t
>(n)] = U[b];
665 gradients.row(n) = Eigen::Map<const Eigen::RowVectorXd>(
666 F.data() + b * stride, full_dim);
667 if (nonfinite(U[b], gradients.row(n)))
668 throw std::runtime_error(
669 "Potential evaluation returned nonfinite energy "
673 for (
const Eigen::Index n: group) {
675 double E_local = std::numeric_limits<double>::quiet_NaN();
676 Eigen::RowVectorXd G_local = Eigen::RowVectorXd::Constant(
677 full_dim, std::numeric_limits<double>::quiet_NaN());
678 general_potential.
force(
679 num_atoms, coord_atom_new.row(n).data(),
680 atoms_config.
atomicNrs.data(), G_local.data(), &E_local,
682 energies[
static_cast<std::size_t
>(n)] = E_local;
683 gradients.row(n) = G_local;
684 if (nonfinite(E_local, G_local))
685 throw std::runtime_error(
686 "Potential evaluation returned nonfinite energy "
690 }
catch (
const std::exception& error) {
691 failure = error.what();
692 if (failure.empty()) failure =
"Potential evaluation failed";
694 failure =
"Potential evaluation raised a non-standard exception";
707 MPI_Allreduce(&local_new_calls, &global_calls, 1, MPI_UINT32_T, MPI_SUM,
709 num_of_calls += global_calls;
711 num_of_calls += local_new_calls;
714 num_of_calls += local_new_calls;
717 middle_point.
E.setConstant(std::numeric_limits<double>::quiet_NaN());
718 middle_point.
G.setConstant(std::numeric_limits<double>::quiet_NaN());
719 throw std::runtime_error(outcome.message);
729 for (Eigen::Index n = 0; n < num_im; ++n) {
730 middle_point.
E(n, 0) = energies[
static_cast<std::size_t
>(n)];
732 for (Eigen::Index m = 0; m < num_atoms; ++m) {
734 middle_point.
G(n, 3 * counter) = gradients(n, 3 * m);
735 middle_point.
G(n, 3 * counter + 1) = gradients(n, 3 * m + 1);
736 middle_point.
G(n, 3 * counter + 2) = gradients(n, 3 * m + 2);
740 middle_point.
G.row(n) *= -1.;
750 middle_point.
E.resize(E_reference.rows(), E_reference.cols());
752 general_potential, num_of_calls);
One step's oracle points shared out over the ranks of a communicator, with a layout-independent owner...
void removeDuplicatedAtoms(const gpr::AtomsPositionAndType &moving_atoms, gpr::AtomsPositionAndType &active_atoms, gpr::AtomsPositionAndType &inactive_atoms)
Remove clones of the moving atoms from the set of inactive frozen atoms.
void updateConf(const gpr::Coord &unfreezed_atoms, const gpr::AtomsConfiguration &atoms_config, gpr::Coord &coord_atom_new)
Update an atomic configuration with new coordinates for the unfreezed atoms.
bool activateFrozenAtoms(const gpr::Coord &R, const double activation_distance, gpr::AtomsConfiguration &atoms_config)
Update the active frozen-atom set for one or more configurations.
void calculateGeneralPotential(const gpr::AtomsConfiguration &atoms_config, const gpr::vector3_reg(&cell_dimensions)[3], gpr::Observation &mid_point, pot::PotentialWrapper &general_potential, gpr::Index_t &num_of_calls, Eigen::Index batch_chunk=0)
Invoke potential calculation.
void initializeEnergyAndGradient(const gpr::FieldMatrixd &E_reference, const gpr::AtomsConfiguration &atoms_config, const gpr::vector3_reg(&system_size)[3], gpr::Observation &middle_point, pot::PotentialWrapper &general_potential, gpr::Index_t &num_of_calls)
Initialize energy and gradient.
void cutOffEnergy(const gpr::FieldMatrixd &E_reference, gpr::FieldMatrixd &E_R)
Subtract reference energy value from E_R.
void setPairtypeForMovingAtoms(const gpr::FieldMatrixIdx &atomtype_mov, gpr::Index_t &n_pt, gpr::FieldMatrixi &pairtype)
Set pairtype indices for moving+moving atom pairs and gives the updated number of active pairtypes.
void displacement(const double p[3], const double q[3], double dr[3]) const
Displacement from p to q. Cartesian q - p when the cell is zero.
double dist2(const Eigen::Vector3d &p, const Eigen::Vector3d &q) const
MPI_Comm world_comm() const
static DistributedState & instance()
void initialize(int block_size=0, int nprow=0)
Type-erased wrapper around any potential that provides a force() interface.
void forceBatch(long N, std::size_t B, const double *R, const int *atomicNrs, double *F, double *U, const double *box)
Batched means; requires hasForceBatch().
bool hasForceBatch() const noexcept
void force(long N, const double *R, const int *atomicNrs, double *F, double *U, double *energy_noise_rms, const double *box)
Invoke the wrapped potential.
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
void set(FieldMatrixd &mat, Index_t i, const Eigen::Vector3d &vec)
void deleteColumnAt(FieldMatrixd &mat, Index_t j)
Index_t numPoints(const FieldMatrixd &mat)
void deleteColumn(Eigen::PlainObjectBase< Derived > &mat, Eigen::Index j)
Eigen::Vector3d vector3_reg
Eigen::Matrix< int, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixi
Eigen::Matrix< Index_t, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixIdx
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd
@ Failed
A geometry was refused or a value was not finite; message set.
OracleBatchOutcome agreeOracleBatchStatus(OracleBatchStatus local_status, const std::string &local_message, const OracleBatchLayout &layout)
std::vector< std::vector< Eigen::Index > > ownedOracleGroups(Eigen::Index count, const OracleBatchLayout &layout)
The groups this rank evaluates, each one batch call, in point order.
void allreduceOwnedOracleValues(std::vector< double > &energies, Eigen::MatrixXd &gradients, const OracleBatchLayout &layout, double *communication_seconds)
Stores configurations of all atoms in the system.
FieldMatrixi atomicNrs
Atomic Number of each atom.
AtomsPositionAndType atoms_froz_inactive
Info on inactive frozen atoms.
FieldMatrixU8 is_frozen
Indicates whether atom is frozen or not.
AtomsPositionAndType atoms_froz_active
Info on active frozen atoms.
AtomsPositionAndType atoms_mov
Info on moving atoms.
Coord positions
Positions of all atoms in the system.
Structure of atoms positions and types.
void resize(const Index_t num_atoms)
Observation data: energy, gradient and positions.
How a step's oracle points are shared out.
static OracleBatchLayout world(Eigen::Index chunk=0, int owner_offset=0)
The layout of the running communicator (rank 0 of 1 without MPI).