KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
Kernel_Representation.cpp
Go to the documentation of this file.
2
4#include "kids/hash_fnv1a.h"
5#include "kids/linalg.h"
6// #include "kids/linalg_tpl.h"
7#include "kids/macro_utils.h"
8#include "kids/vars_list.h"
9
10namespace PROJECT_NS {
11
12const std::string Kernel_Representation::getName() { return "Kernel_Representation"; }
13
15
16
18 RepresentationPolicy::_type from, RepresentationPolicy::_type to,
19 SpacePolicy::_type Stype) {
20 if (from == to) return 0;
21 int lda = (Stype == SpacePolicy::L) ? fdim : 1;
22 if (from == RepresentationPolicy::Diabatic && to == RepresentationPolicy::Adiabatic) {
23 ARRAY_MATMUL_TRANS1(A, T, A, fdim, fdim, lda);
24 if (Stype == SpacePolicy::L) ARRAY_MATMUL(A, A, T, fdim, fdim, fdim);
25 }
26 if (from == RepresentationPolicy::Adiabatic && to == RepresentationPolicy::Diabatic) {
27 ARRAY_MATMUL(A, T, A, fdim, fdim, lda);
28 if (Stype == SpacePolicy::L) ARRAY_MATMUL_TRANS2(A, A, T, fdim, fdim, fdim);
29 }
30 return 0;
31}
32
33void Kernel_Representation::setInputParam_impl(std::shared_ptr<Param>& PM) {
34 std::string rep_string = PM->get_string("representation_flag", LOC(), "Diabatic");
35 representation_type = RepresentationPolicy::_from(rep_string);
36 inp_repr_type = RepresentationPolicy::_from(PM->get_string("inp_repr_flag", LOC(), rep_string));
37 ele_repr_type = RepresentationPolicy::_from(PM->get_string("ele_repr_flag", LOC(), rep_string));
38 nuc_repr_type = RepresentationPolicy::_from(PM->get_string("nuc_repr_flag", LOC(), rep_string));
39 tcf_repr_type = RepresentationPolicy::_from(PM->get_string("tcf_repr_flag", LOC(), rep_string));
40 phase_correction = PM->get_bool("phase_correction", LOC(), false);
41 basis_switch = PM->get_bool("basis_switch", LOC(), false);
42}
43
44void Kernel_Representation::setInputDataSet_impl(std::shared_ptr<DataSet>& DS) {
45 V = DS->def(DATA::model::V);
46 dV = DS->def(DATA::model::dV);
47 // ddV = DS->def(DATA::model::ddV);
48 E_copy = DS->def(DATA::integrator::E);
49 E = DS->def(DATA::model::rep::E);
50 T = DS->def(DATA::model::rep::T);
52 dE = DS->def(DATA::model::rep::dE);
53 // ddE = DS->def(DATA::model::rep::ddE);
54 L = DS->def(DATA::model::rep::L);
55 R = DS->def(DATA::model::rep::R);
56 H = DS->def(DATA::model::rep::H);
57
58 m = DS->def(DATA::integrator::m);
59 p = DS->def(DATA::integrator::p);
62
66}
67
69 do_refer = false;
70 executeKernel(stat);
71 do_refer = true;
72 _dataset->def_real("init.T", T, Dimension::PFF);
73 return stat;
74}
75
77 if (Dimension::F <= 1) return stat;
78
79 for (int iP = 0; iP < Dimension::P; ++iP) {
80 kids_real* V = this->V + iP * Dimension::FF;
81 kids_real* E = this->E + iP * Dimension::F;
82 kids_real* T = this->T + iP * Dimension::FF;
83 kids_real* Told = this->Told + iP * Dimension::FF;
84 kids_real* dV = this->dV + iP * Dimension::NFF;
85 kids_real* dE = this->dE + iP * Dimension::NFF;
86 kids_real* L = this->L + iP * Dimension::F;
87 kids_complex* R = this->R + iP * Dimension::FF;
88 kids_complex* H = this->H + iP * Dimension::FF;
89
90 kids_real* p = this->p + iP * Dimension::N;
91 kids_real* m = this->m + iP * Dimension::N;
92 int* occ_nuc = this->occ_nuc + iP;
93 kids_complex* rho_ele = this->rho_ele + iP * Dimension::FF;
94
95 switch (representation_type) {
96 case RepresentationPolicy::Diabatic: {
98 for (int ik = 0; ik < Dimension::FF; ++ik) H[ik] = V[ik];
99 break;
100 }
101 case RepresentationPolicy::Adiabatic: {
102 if (!onthefly) { // solve adiabatic from diabatic (otherwise E/dE should be provided from Ab Initio
103
104 for (int i = 0; i < Dimension::FF; ++i) Told[i] = T[i]; // backup old T matrix
105 EigenSolve(E, T, V, Dimension::F); // solve new eigen problem
106
107 if (do_refer) {
108 // calculate permutation matrix = rountint(T^ * Told)
110
111 // ARRAY_SHOW(E, 1, Dimension::F);
112 // ARRAY_SHOW(T, Dimension::F, Dimension::F);
113 // ARRAY_SHOW(TtTold, Dimension::F, Dimension::F);
114 // ARRAY_SHOW(TtTold, Dimension::F, Dimension::F); // @debug
115
116 if (!basis_switch) {
117 for (int i = 0, ik = 0; i < Dimension::F; ++i) {
118 for (int k = 0; k < Dimension::F; ++k, ++ik) {
119 TtTold[ik] = (i == k) ? copysign(1.0f, TtTold[ik]) : 0;
120 }
121 }
122 } else {
123 double vset = 0.1 * std::sqrt(1.0e0 / Dimension::F);
124 for (int i = 0; i < Dimension::F; ++i) {
125 double maxnorm = 0;
126 int csr1 = 0, csr2 = 0, csr12 = 0;
127 for (int k1 = 0, k1k2 = 0; k1 < Dimension::F; ++k1) {
128 for (int k2 = 0; k2 < Dimension::F; ++k2, ++k1k2) {
129 // vmax must be larger than sqrt(1/fdim)
130 if (std::abs(TtTold[k1k2]) > maxnorm) {
131 maxnorm = std::abs(TtTold[k1k2]);
132 csr1 = k1, csr2 = k2, csr12 = k1k2;
133 }
134 }
135 }
136 double vsign = copysign(1.0f, TtTold[csr12]);
137 for (int k2 = 0, k1k2 = csr1 * Dimension::F; //
138 k2 < Dimension::F; //
139 ++k2, ++k1k2) {
140 TtTold[k1k2] = 0;
141 }
142 for (int k1 = 0, k1k2 = csr2; k1 < Dimension::F; ++k1, k1k2 += Dimension::F) {
143 TtTold[k1k2] = 0;
144 }
145 TtTold[csr12] = vsign * vset;
146 }
147 for (int i = 0; i < Dimension::FF; ++i) TtTold[i] = round(TtTold[i] / vset);
148 }
149
150 // ARRAY_SHOW(TtTold, Dimension::F, Dimension::F);
151
152 // adjust order of eigenvectors & eigenvalues
154 if (basis_switch) {
155 for (int i = 0; i < Dimension::FF; ++i) TtTold[i] = std::abs(TtTold[i]);
157 }
158 }
159
161 int& B = FORCE_OPT::nbath;
162 int& J = FORCE_OPT::Nb;
163 int JFF = J * Dimension::FF;
164 double* dVb0 = dV;
165 double* dEb0 = dE;
166 for (int b = 0, bb = 0; b < B; ++b, bb += Dimension::Fadd1, dVb0 += JFF, dEb0 += JFF) {
169 for (int j = 0, jik = 0, jbb = bb; j < J; ++j, jbb += Dimension::FF) {
170 double scale = dVb0[jbb] / dVb0[bb];
171 for (int ik = 0; ik < Dimension::FF; ++ik, ++jik) { dEb0[jik] = dEb0[ik] * scale; }
172 }
173 }
174 } else {
175 // Eigen::Map<EigMX<double>> Map_T(T, Dimension::F, Dimension::F);
176 // Eigen::Map<EigMX<double>> Map_dV(dV, Dimension::NF, Dimension::F);
177 // Eigen::Map<EigMX<double>> Map_dE1(dE, Dimension::NF, Dimension::F);
178 // Eigen::Map<EigMX<double>> Map_dE2(dE, Dimension::F, Dimension::NF);
179
180 // Map_dE2 = Map_dV.transpose();
181 // Map_dE2 = (Map_T.adjoint() * Map_dE2).eval();
182 // Map_dE1 = (Map_dE1 * Map_T).eval();
183 // Map_dE1 = Map_dE2.transpose().eval();
184
189 }
190 }
191
192 // calc H = E - im * nacv * p / m, and note here nacv_{ij} = dEij / (Ej - Ei)
193 for (int i = 0; i < Dimension::N; ++i) ve[i] = p[i] / m[i];
195
196 double Emean = 0.0e0;
197 for (int i = 0; i < Dimension::F; ++i) {
198 Emean += E[i];
199 E_copy[i] = E[i];
200 }
201 Emean /= Dimension::F;
202
203 for (int i = 0, ij = 0; i < Dimension::F; ++i) {
204 for (int j = 0; j < Dimension::F; ++j, ++ij) { //
205 H[ij] = ((i == j) ? E[i] - Emean : -phys::math::im * vedE[ij] / (E[j] - E[i]));
206 }
207 }
208
209 if (phase_correction) {
210 kids_real Ekin = 0;
211 for (int j = 0; j < Dimension::N; ++j) Ekin += 0.5f * p[j] * p[j] / m[j];
212 double Epes = 0.0f;
213 if (Kernel_NADForce::NADForce_type == NADForcePolicy::BO) {
214 Epes = E[*occ_nuc];
215 } else {
216 for (int i = 0, ii = 0; i < Dimension::F; ++i, ii += Dimension::Fadd1) {
217 Epes += std::real(rho_ele[ii]) * E[i];
218 }
219 }
220 for (int i = 0, ii = 0; i < Dimension::F; ++i, ii += Dimension::Fadd1) {
221 H[ii] = -2 * Ekin * sqrt(std::max<double>(1.0 + (Epes - E[i]) / Ekin, 0.0f));
222 }
223 }
224 EigenSolve(L, R, H, Dimension::F); // R*L*R^ = H
225 break;
226 }
227 case RepresentationPolicy::Force:
228 case RepresentationPolicy::Density:
229 default:
230 break;
231 }
232 }
233
234 return stat;
235}
236
237RepresentationPolicy::_type Kernel_Representation::representation_type;
238RepresentationPolicy::_type Kernel_Representation::inp_repr_type;
239RepresentationPolicy::_type Kernel_Representation::ele_repr_type;
240RepresentationPolicy::_type Kernel_Representation::nuc_repr_type;
241RepresentationPolicy::_type Kernel_Representation::tcf_repr_type;
243
244}; // namespace PROJECT_NS
this file provides Kernel_NADForce class enabling force weighting from electronic properties.
#define ARRAY_MATMUL_TRANS2(_A, _B, _C, _n1, _n2, _n3)
#define ARRAY_MATMUL3_TRANS1(_A, _B, _C, _D, _n1, _n2, _n0, _n3)
#define ARRAY_MATMUL(_A, _B, _C, _n1, _n2, _n3)
#define ARRAY_MATMUL_TRANS1(_A, _B, _C, _n1, _n2, _n3)
static NADForcePolicy::_type NADForce_type
virtual void setInputDataSet_impl(std::shared_ptr< DataSet > &DS)
Virtual function to set input data set for the kernel implementation.
static RepresentationPolicy::_type ele_repr_type
representation for electronic dynamics
static RepresentationPolicy::_type nuc_repr_type
representation for nuclear dynamics
virtual Status & executeKernel_impl(Status &stat)
Virtual function to execute the kernel implementation.
virtual const std::string getName()
Get the name of the kernel.
virtual int getType() const
Get the type of the kernel.
static RepresentationPolicy::_type representation_type
root representation
virtual void setInputParam_impl(std::shared_ptr< Param > &PM)
Virtual function to set input parameters for the kernel implementation.
static int transform(kids_complex *A, kids_real *T, int fdim, RepresentationPolicy::_type from, RepresentationPolicy::_type to, SpacePolicy::_type Stype)
virtual Status & initializeKernel_impl(Status &stat)
Virtual function to initialize the kernel implementation.
static bool onthefly
flag indicated the ab inition calculation
static RepresentationPolicy::_type tcf_repr_type
representation for intrinsic calculation of TCF
static RepresentationPolicy::_type inp_repr_type
representatioin for input (quantities)
Status & executeKernel(Status &stat)
Execute the kernel's algorithm and those of its children.
Definition Kernel.cpp:48
std::shared_ptr< DataSet > _dataset
Shared pointer to the DataSet object associated with this kernel.
Definition Kernel.h:278
#define LOC()
show the location information for debug
Definition fmt.h:49
Provide linalg APIs.
#define FUNCTION_NAME
Definition macro_utils.h:9
VARIABLE< kids_real > TtTold
VARIABLE< kids_real > vedE
VARIABLE< kids_real > p
VARIABLE< kids_complex > rho_ele
VARIABLE< kids_real > m
VARIABLE< kids_int > occ_nuc
VARIABLE< kids_real > E
VARIABLE< kids_real > Told
VARIABLE< kids_real > dE
VARIABLE< kids_real > E
VARIABLE< kids_real > L
VARIABLE< kids_real > T
VARIABLE< kids_complex > R
VARIABLE< kids_complex > H
VARIABLE< kids_real > V
VARIABLE< kids_real > dV
std::size_t NFF
Product of N, F, and F (N * F * F).
Definition vars_list.cpp:22
std::size_t N
Number of nuclear degrees of freedom.
Definition vars_list.cpp:10
std::size_t PFF
Product of P, F, and F (P * F * F).
Definition vars_list.cpp:17
std::size_t P
Number of parallel trajectories (swarms of trajectories) in each Monte Carlo run.
Definition vars_list.cpp:9
std::size_t F
Number of electronic degrees of freedom.
Definition vars_list.cpp:11
std::size_t Fadd1
F plus 1 (F + 1).
Definition vars_list.cpp:24
std::size_t FF
Product of F and F (F * F).
Definition vars_list.cpp:21
std::size_t NF
Product of N and F (N * F).
Definition vars_list.cpp:19
< http://warp.povusers.org/FunctionParser/fparser.html
Definition Context.h:39
void ARRAY_TRANSPOSE(kids_real *A, size_t N1, size_t N2)
Definition linalg.cpp:500
double kids_real
Alias for real number type.
Definition Types.h:59
std::complex< double > kids_complex
Alias for complex number type.
Definition Types.h:60
void EigenSolve(kids_real *E, kids_real *T, kids_real *A, size_t N)
Solve the eigenvalue problem for real matrices.
Definition linalg_1.cpp:44
constexpr std::complex< real_precision > im(0.0L, 1.0L)
Imaginary Unit.
constexpr uint32_t hash(const char *str)
Definition hash_fnv1a.h:12
declaration of variables used in the program.