eOn 3.2.0
Long-timescale dynamics: aKMC, NEB, parallel replica
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
NEBObjectiveFunction.cpp
Go to the documentation of this file.
1
/*
2
** This file is part of eOn.
3
**
4
** SPDX-License-Identifier: BSD-3-Clause
5
**
6
** Copyright (c) 2010--present, eOn Development Team
7
** All rights reserved.
8
**
9
** Repo:
10
** https://github.com/TheochemUI/eOn
11
*/
12
#include "
eon/NudgedElasticBand.h
"
13
#include "
eon/SolidStateNEB.h
"
14
15
namespace
eonc
{
16
namespace
{
17
18
long
nebSegment(
const
NudgedElasticBand
&
neb
) {
19
return
3L *
neb
.atoms + (
neb
.solidState() ? 9L : 0L);
20
}
21
22
}
// namespace
23
24
VectorXd
NEBObjectiveFunction::getGradient
(
bool
fdstep) {
25
if
(
neb
->movedAfterForceCall)
26
neb
->updateForces();
27
const
long
seg = nebSegment(*
neb
);
28
const
long
atomDof = 3L *
neb
->atoms;
29
VectorXd gradV(seg *
neb
->numImages);
30
for
(
long
i = 1; i <=
neb
->numImages; i++) {
31
// Negate in-place during copy to avoid a second pass over 40KB
32
gradV.segment(seg * (i - 1), atomDof) =
33
-VectorXd::Map(
neb
->projectedForce[i]->data(), atomDof);
34
if
(
neb
->solidState()) {
35
const
eonc::neb::CartesianStep
step =
eonc::neb::solidStateCartesianStep
(
36
*
neb
->path[i], *
neb
->projectedForce[i],
neb
->cellForce(i),
37
neb
->solidJacobian());
38
gradV.segment(seg * (i - 1), atomDof) =
39
-VectorXd::Map(step.
positions
.data(), atomDof);
40
gradV.segment(seg * (i - 1) + atomDof, 9) =
41
-VectorXd::Map(step.
cell
.data(), 9);
42
}
43
}
44
return
gradV;
45
}
46
47
double
NEBObjectiveFunction::getEnergy
() {
48
// The band update evaluates dirty images as one batch; summing image
49
// energies on a moved band would otherwise evaluate them one at a time.
50
if
(
neb
->movedAfterForceCall)
51
neb
->updateForces();
52
double
Energy{0};
53
for
(
long
i = 1; i <=
neb
->numImages; i++) {
54
Energy +=
neb
->path[i]->getPotentialEnergy();
55
}
56
return
Energy;
57
}
58
59
void
NEBObjectiveFunction::setPositions
(
const
VectorXd &x) {
60
neb
->movedAfterForceCall =
true
;
61
const
long
seg = nebSegment(*
neb
);
62
const
long
atomDof = 3L *
neb
->atoms;
63
for
(
long
i = 1; i <=
neb
->numImages; i++) {
64
const
long
offset = seg * (i - 1);
65
if
(
neb
->solidState()) {
66
Matrix3d
cell = Matrix3d::Map(x.segment(offset + atomDof, 9).data());
67
cell(0, 1) = 0.0;
68
cell(0, 2) = 0.0;
69
cell(1, 2) = 0.0;
70
neb
->path[i]->setCell(cell);
71
}
72
neb
->path[i]->setPositions(
73
AtomMatrix::Map(x.segment(offset, atomDof).data(),
neb
->atoms, 3));
74
}
75
}
76
77
VectorXd
NEBObjectiveFunction::getPositions
() {
78
const
long
seg = nebSegment(*
neb
);
79
const
long
atomDof = 3L *
neb
->atoms;
80
VectorXd posV(seg *
neb
->numImages);
81
for
(
long
i = 1; i <=
neb
->numImages; i++) {
82
const
long
offset = seg * (i - 1);
83
posV.segment(offset, atomDof) =
84
VectorXd::Map(
neb
->path[i]->getPositions().data(), atomDof);
85
if
(
neb
->solidState()) {
86
posV.segment(offset + atomDof, 9) =
87
VectorXd::Map(
neb
->path[i]->getCell().data(), 9);
88
}
89
}
90
return
posV;
91
}
92
93
int
NEBObjectiveFunction::degreesOfFreedom
() {
94
return
static_cast<
int
>
(nebSegment(*
neb
) *
neb
->numImages);
95
}
96
97
bool
NEBObjectiveFunction::isUncertain
() {
98
double
maxMaxUnc = std::numeric_limits<double>::lowest();
99
double
currentMaxUnc{0};
100
for
(
long
idx = 0; idx <=
neb
->numImages + 1; idx++) {
101
currentMaxUnc =
neb
->path[idx]->getEnergyVariance();
102
if
(currentMaxUnc > maxMaxUnc) {
103
maxMaxUnc = currentMaxUnc;
104
}
105
}
106
bool
unc_conv{maxMaxUnc >
params
.gp_surrogate_options().uncertainty};
107
if
(unc_conv) {
108
this->
status
=
NudgedElasticBand::NEBStatus::MAX_UNCERTAINTY
;
109
}
110
return
unc_conv;
111
}
112
113
bool
NEBObjectiveFunction::isConverged
() {
114
bool
force_conv =
getConvergence
() <
params
.neb_options().force_tolerance;
115
return
force_conv;
116
}
117
118
double
NEBObjectiveFunction::getConvergence
() {
119
return
neb
->convergenceForce();
120
}
121
122
VectorXd
NEBObjectiveFunction::getMasses
()
const
{
123
long
count = 0;
124
for
(
long
i = 1; i <=
neb
->numImages; ++i) {
125
const
Matter
&image = *
neb
->path[
static_cast<
size_t
>
(i)];
126
const
AtomMatrix
mask = image.
getFree
();
127
for
(
long
atom = 0; atom < image.
numberOfAtoms
(); ++atom) {
128
if
(mask.row(atom).sum() > 0.5) {
129
++count;
130
}
131
}
132
}
133
VectorXd masses(count);
134
long
written = 0;
135
for
(
long
i = 1; i <=
neb
->numImages; ++i) {
136
const
Matter
&image = *
neb
->path[
static_cast<
size_t
>
(i)];
137
const
auto
all = image.
getMasses
();
138
const
AtomMatrix
mask = image.
getFree
();
139
for
(
long
atom = 0; atom < image.
numberOfAtoms
(); ++atom) {
140
if
(mask.row(atom).sum() > 0.5) {
141
masses(written++) = all(atom);
142
}
143
}
144
}
145
return
masses;
146
}
147
148
VectorXd
NEBObjectiveFunction::difference
(
const
VectorXd &a,
149
const
VectorXd &b) {
150
const
long
seg = nebSegment(*
neb
);
151
const
long
atomDof = 3L *
neb
->atoms;
152
VectorXd pbcDiff(seg *
neb
->numImages);
153
for
(
int
i = 1; i <=
neb
->numImages; i++) {
154
const
int
n = (i - 1) *
static_cast<
int
>
(seg);
155
pbcDiff.segment(n, atomDof) =
156
neb
->path[i]->pbcV(a.segment(n, atomDof) - b.segment(n, atomDof));
157
if
(
neb
->solidState()) {
158
pbcDiff.segment(n + atomDof, 9) =
159
a.segment(n + atomDof, 9) - b.segment(n + atomDof, 9);
160
}
161
}
162
return
pbcDiff;
163
}
164
165
}
// namespace eonc
Matrix3d
Eigen::Matrix< double, 3, 3, eOnStorageOrder > Matrix3d
Definition
Eigen.h:35
AtomMatrix
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition
Eigen.h:37
NudgedElasticBand.h
SolidStateNEB.h
eonc::Matter
Definition
Matter.h:94
eonc::Matter::getFree
AtomMatrix getFree() const
Definition
Matter.cpp:799
eonc::Matter::numberOfAtoms
long int numberOfAtoms() const
Definition
Matter.cpp:343
eonc::Matter::getMasses
Eigen::Matrix< double, Eigen::Dynamic, 1 > getMasses() const
Definition
Matter.cpp:846
eonc::NEBObjectiveFunction::getGradient
VectorXd getGradient(bool fdstep=false)
Definition
NEBObjectiveFunction.cpp:24
eonc::NEBObjectiveFunction::getPositions
VectorXd getPositions()
Definition
NEBObjectiveFunction.cpp:77
eonc::NEBObjectiveFunction::status
NudgedElasticBand::NEBStatus status
Definition
NudgedElasticBand.h:172
eonc::NEBObjectiveFunction::getConvergence
double getConvergence()
Definition
NEBObjectiveFunction.cpp:118
eonc::NEBObjectiveFunction::getEnergy
double getEnergy()
Definition
NEBObjectiveFunction.cpp:47
eonc::NEBObjectiveFunction::neb
NudgedElasticBand * neb
Definition
NudgedElasticBand.h:175
eonc::NEBObjectiveFunction::difference
VectorXd difference(const VectorXd &a, const VectorXd &b)
Definition
NEBObjectiveFunction.cpp:148
eonc::NEBObjectiveFunction::isConverged
bool isConverged()
Definition
NEBObjectiveFunction.cpp:113
eonc::NEBObjectiveFunction::degreesOfFreedom
int degreesOfFreedom()
Definition
NEBObjectiveFunction.cpp:93
eonc::NEBObjectiveFunction::isUncertain
bool isUncertain()
Definition
NEBObjectiveFunction.cpp:97
eonc::NEBObjectiveFunction::getMasses
VectorXd getMasses() const override
Definition
NEBObjectiveFunction.cpp:122
eonc::NEBObjectiveFunction::setPositions
void setPositions(const VectorXd &x)
Definition
NEBObjectiveFunction.cpp:59
eonc::NudgedElasticBand
Definition
NudgedElasticBand.h:38
eonc::NudgedElasticBand::NEBStatus::MAX_UNCERTAINTY
@ MAX_UNCERTAINTY
Definition
NudgedElasticBand.h:47
eonc::ObjectiveFunction::params
const Parameters & params
Definition
ObjectiveFunction.h:22
eonc::neb
Definition
NEBForceProjection.cpp:18
eonc::neb::solidStateCartesianStep
CartesianStep solidStateCartesianStep(const Matter &image, const AtomMatrix &atomicForce, const Matrix3d &cellForce, double jacobian)
Definition
SolidStateNEB.cpp:231
eonc
RAII resource manager for the ARTn C library with global synchronization.
Definition
ARTnSaddleSearch.cpp:23
eonc::neb::CartesianStep
One steepest step in Cartesian coordinates and the cell matrix.
Definition
SolidStateNEB.h:80
eonc::neb::CartesianStep::positions
AtomMatrix positions
Definition
SolidStateNEB.h:81
eonc::neb::CartesianStep::cell
Matrix3d cell
Definition
SolidStateNEB.h:82
client
NEBObjectiveFunction.cpp
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf