KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
Model_SystemBath.cpp
Go to the documentation of this file.
2
6#include "kids/hash_fnv1a.h"
7#include "kids/linalg.h"
8#include "kids/macro_utils.h"
9#include "kids/vars_list.h"
10
11namespace PROJECT_NS {
12
13const std::string Model_SystemBath::getName() { return "Model_SystemBath"; }
14
16
17void Model_SystemBath::setInputParam_impl(std::shared_ptr<Param>& PM) {
18 // size information
19 Nb = _param->get_int("Nb", LOC());
20 nbath = _param->get_int("nbath", LOC());
22
25
26 system_type = SystemPolicy::_from(_param->get_string("system_flag", LOC(), "SB"));
27 coupling_type = CouplingPolicy::_from(_param->get_string("coupling_flag", LOC(), "SB"));
28 nsamp_type = NSampPolicy::_from(_param->get_string("nsamp_flag", LOC(), "Wigner"));
29}
30
31void Model_SystemBath::setInputDataSet_impl(std::shared_ptr<DataSet>& DS) {
33 Hsys = DS->def_real("model.Hsys", Dimension::FF);
34 memset(Hsys, 0, Dimension::FF * sizeof(kids_real));
35 L = 1;
36 switch (system_type) {
37 case SystemPolicy::SB: {
39 L = 2;
40 double bias = _param->get_double("bias", LOC(), phys::energy_d, 1.0f);
41 double delta = _param->get_double("delta", LOC(), phys::energy_d, 1.0f);
42 double HSB[4] = {bias, delta, delta, -bias};
43 for (int i = 0; i < Dimension::FF; ++i) Hsys[i] = HSB[i];
44 break;
45 }
46 case SystemPolicy::FMO: {
49 double tmp_unit = phys::au_2_wn;
50 memset(Hsys, 0, Dimension::FF * sizeof(kids_real));
51 for (int i = 0, ik = 0; i < 7; ++i) {
52 for (int k = 0; k < 7; ++k, ++ik) { Hsys[i * Dimension::F + k] = HFMO_data[ik] / tmp_unit; }
53 }
54 break;
55 }
56 case SystemPolicy::SF3a: {
58 double tmp_unit = phys::au_2_ev;
59 for (int i = 0; i < Dimension::FF; ++i) Hsys[i] = HSF3a_data[i] / tmp_unit;
60 break;
61 }
62 case SystemPolicy::SF3b: {
64 double tmp_unit = phys::au_2_ev;
65 for (int i = 0; i < Dimension::FF; ++i) Hsys[i] = HSF3b_data[i] / tmp_unit;
66 break;
67 }
68 case SystemPolicy::SF5a: {
70 double tmp_unit = phys::au_2_ev;
71 for (int i = 0; i < Dimension::FF; ++i) Hsys[i] = HSF5a_data[i] / tmp_unit;
72 break;
73 }
74 case SystemPolicy::SF5b: {
76 double tmp_unit = phys::au_2_ev;
77 for (int i = 0; i < Dimension::FF; ++i) Hsys[i] = HSF5b_data[i] / tmp_unit;
78 break;
79 }
80 case SystemPolicy::FCP: {
83 double tmp_unit = phys::au_2_wn;
84 for (int i = 0, ik = 0; i < 9; ++i) {
85 for (int k = 0; k < 9; ++k, ++ik) { Hsys[i * Dimension::F + k] = HFCP_data[ik] / tmp_unit; }
86 }
87 break;
88 }
89 case SystemPolicy::AGG: {
91 double delta = _param->get_double("delta", LOC(), phys::energy_d, 1.0f);
92 for (int i = 0, ik = 0; i < Dimension::F; ++i) {
93 for (int k = 0; k < Dimension::F; ++k, ++ik) Hsys[ik] = (i - k == 1 || k - i == 1) ? delta : 0.0f;
94 }
95 break;
96 }
97 case SystemPolicy::CYC: {
99 double delta = _param->get_double("delta", LOC(), phys::energy_d, 1.0f);
100 for (int i = 0, ik = 0; i < Dimension::F; ++i) {
101 for (int k = 0; k < Dimension::F; ++k, ++ik) Hsys[ik] = (i - k == 1 || k - i == 1) ? delta : 0.0f;
102 }
103 Hsys[0 * Dimension::F + (Dimension::F - 1)] = delta;
104 Hsys[(Dimension::F - 1) * Dimension::F + 0] = delta;
105 break;
106 }
107 case SystemPolicy::Read: {
108 std::string system_readfile = _param->get_string("system_readfile", LOC(), "system.dat");
109 std::ifstream ifs(system_readfile);
110 std::string H_unit_str;
111 std::string firstline;
112 getline(ifs, firstline);
113 std::stringstream sstr(firstline);
114 sstr >> H_unit_str;
115
116 double H_unit = phys::us::conv(phys::au::unit, phys::us::parse(H_unit_str));
117 kids_real val;
118 for (int i = 0; i < Dimension::FF; ++i)
119 if (ifs >> val) Hsys[i] = val / H_unit;
120 ifs.close();
121 }
122 }
123
125 for (auto pkernel : _child_kernels) pkernel->setInputDataSet(DS);
130
132 Q = DS->def(DATA::model::coupling::Q);
133 CL = DS->def(DATA::model::coupling::CL);
134 QL = DS->def(DATA::model::coupling::QL);
136 switch (coupling_type) {
137 case CouplingPolicy::SB: {
138 Q[0] = 1.0f, Q[1] = 0.0f, Q[2] = 0.0f, Q[3] = -1.0f;
139 break;
140 }
141 case CouplingPolicy::SE: {
143 for (int i = 0, idx = 0; i < nbath; ++i) {
144 for (int j = 0; j < Dimension::F; ++j) {
145 for (int k = 0; k < Dimension::F; ++k, ++idx) Q[idx] = (i == j && i == k) ? 1.0f : 0.0f;
146 }
147 }
148 break;
149 }
150 default: {
151 std::string coupling_readfile = _param->get_string("coupling_readfile", LOC(), "coupling.dat");
152 std::ifstream ifs(coupling_readfile);
153 kids_real tmp;
154 for (int i = 0; i < nbath * Dimension::FF; ++i)
155 if (ifs >> tmp) Q[i] = tmp;
156 ifs.close();
157 }
158 }
159
161 for (int ibath = 0, idx = 0; ibath < nbath; ++ibath) {
162 for (int j = 0; j < Nb; ++j) {
163 for (int i = 0, iL = 0; i < Dimension::FF; ++i, ++idx) {
164 double Qval = Q[ibath * Dimension::FF + i];
165 if (Qval != 0) {
166 QL[iL * nbath * Dimension::FF + ibath * Dimension::FF + i] = 1; // record there is a nonzero value
167 CL[iL * Nb + j] = coeffs[j] * Qval; // reduce this value to CL
168 iL++;
169 if (iL > L) throw std::runtime_error(" Q shoule be sparsed!");
170 }
171 Xnj[idx] = coeffs[j] * Q[ibath * Dimension::FF + i]; // merge coeffs into Q to obtain Xnj
172 }
173 }
174 }
175
176 // model field
177 mass = DS->def(DATA::model::mass);
178 for (int j = 0; j < Dimension::N; ++j) mass[j] = 1.0f;
179
180 vpes = DS->def(DATA::model::vpes);
181 grad = DS->def(DATA::model::grad);
182 hess = DS->def(DATA::model::hess);
183 V = DS->def(DATA::model::V);
184 dV = DS->def(DATA::model::dV);
185 // ddV = DS->def(DATA::model::ddV);
186
187 // init & integrator
188 x = DS->def(DATA::integrator::x);
189 p = DS->def(DATA::integrator::p);
190
191 // exit(-1);
192}
193
195 for (int iP = 0; iP < Dimension::P; ++iP) {
196 kids_real* x = this->x + iP * Dimension::N;
197 kids_real* p = this->p + iP * Dimension::N;
198
201 for (int ibath = 0, idxR = 0; ibath < nbath; ++ibath) {
202 for (int j = 0; j < Nb; ++j, ++idxR) {
203 x[idxR] = x[idxR] * x_sigma[j];
204 p[idxR] = p[idxR] * p_sigma[j];
205 }
206 }
207
208 if (nsamp_type == NSampPolicy::QCT) {
209 for (int ibath = 0, idxR = 0; ibath < nbath; ++ibath) {
210 for (int j = 0; j < Nb; ++j, ++idxR) {
211 double Eq = omegas[j] * (0 + 0.5);
212 double Ec = 0.5 * p[idxR] * p[idxR] + 0.5 * omegas[j] * omegas[j] * x[idxR] * x[idxR];
213 double scale = sqrt(Eq / Ec);
214 x[idxR] = x[idxR] * scale;
215 p[idxR] = p[idxR] * scale;
216 }
217 }
218 }
219 }
220
221 _dataset->def_real("init.x", x, Dimension::PN);
222 _dataset->def_real("init.p", p, Dimension::PN);
223 executeKernel(stat);
224 return stat;
225}
226
228 for (int iP = 0; iP < Dimension::P; ++iP) {
229 kids_real* x = this->x + iP * Dimension::N;
230 kids_real* vpes = this->vpes + iP;
231 kids_real* grad = this->grad + iP * Dimension::N;
232 kids_real* hess = this->hess + iP * Dimension::NN;
233 kids_real* V = this->V + iP * Dimension::FF;
234 kids_real* dV = this->dV + iP * Dimension::NFF;
235
236 // note we implement mass = 1
237
238 // calculate nuclear vpes and grad
239 double term = 0.0f;
240 for (int ibath = 0, idxR = 0; ibath < nbath; ++ibath) {
241 for (int j = 0; j < Nb; ++j, ++idxR) {
242 term += omegas[j] * omegas[j] * x[idxR] * x[idxR];
243 grad[idxR] = omegas[j] * omegas[j] * x[idxR];
244 }
245 }
246 vpes[0] = 0.5 * term;
247
248 // calculate electronic V and dV
249 for (int i = 0; i < Dimension::FF; ++i) V[i] = Hsys[i];
250 switch (coupling_type) {
251 case CouplingPolicy::SB: {
252 double term = 0;
253 for (int ibath = 0, idxR = 0; ibath < nbath; ++ibath) {
254 for (int j = 0; j < Nb; ++j, ++idxR) { term += coeffs[j] * x[idxR]; }
255 }
256 V[0] += term;
257 V[3] -= term;
258 break;
259 }
260 case CouplingPolicy::SE: {
261 for (int ibath = 0, idxV = 0, idxR = 0; ibath < nbath; ++ibath, idxV += Dimension::Fadd1) {
262 for (int j = 0; j < Nb; ++j, ++idxR) { V[idxV] += coeffs[j] * x[idxR]; }
263 }
264 break;
265 }
266 default: {
267 for (int ibath = 0, idxR = 0, idxQ0 = 0; ibath < nbath; ++ibath, idxQ0 += Dimension::FF) {
268 for (int j = 0; j < Nb; ++j, ++idxR) {
269 double cxj = coeffs[j] * x[idxR];
270 for (int i = 0, idxQ = idxQ0; i < Dimension::FF; ++i, ++idxQ) { V[i] += Q[idxQ] * cxj; }
271 }
272 }
273 break;
274 }
275 }
276
277 if (count_exec == 0) { // only calculate once time
279 for (int ibath = 0, idxQ0 = 0, idxdV = 0; ibath < nbath; ++ibath, idxQ0 += Dimension::FF) {
280 for (int j = 0; j < Nb; ++j) {
281 for (int i = 0, idxQ = idxQ0; i < Dimension::FF; ++i, ++idxQ, ++idxdV) {
282 dV[idxdV] = Q[idxQ] * coeffs[j];
283 }
284 }
285 }
286 // ARRAY_SHOW(dV, Dimension::N, Dimension::FF);
287 // exit(-1);
288 }
289
290 // if (flag < 2) return 0;
291
292 if (count_exec == 0) {
294 for (int j = 0, idx = 0, Nadd1 = Dimension::N + 1; j < Dimension::N; ++j, idx += Nadd1) {
295 hess[idx] = omegas[j % Nb] * omegas[j % Nb];
296 }
297 // for (int i = 0; i < NNFF; ++i) ddV[i] = 0;
298 }
299 }
300 return stat;
301}
302
303}; // namespace PROJECT_NS
this file provides Kernel_NADForce class enabling force weighting from electronic properties.
#define ARRAY_CLEAR(_A, _n)
Definition array_macro.h:36
static int rand_gaussian(kids_real *res_arr, int N=1, kids_real sigma=1.0, kids_real mu=0.0)
std::vector< std::shared_ptr< Kernel > > _child_kernels
Vector containing shared pointers to the child kernels of this kernel.
Definition Kernel.h:298
std::shared_ptr< Param > _param
Shared pointer to the Param object associated with this kernel.
Definition Kernel.h:273
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
virtual void setInputDataSet_impl(std::shared_ptr< DataSet > &DS)
Virtual function to set input data set for the kernel implementation.
SystemPolicy::_type system_type
virtual int getType() const
Get the type of the kernel.
virtual Status & initializeKernel_impl(Status &stat)
Virtual function to initialize the kernel implementation.
virtual Status & executeKernel_impl(Status &stat)
Virtual function to execute the kernel implementation.
kids_real * Xnj
used in Stochastic Schrodinger Equation Methods
kids_real * coeffs
save coupling coefficients (only for simple model, L=1)
virtual const std::string getName()
Get the name of the kernel.
CouplingPolicy::_type coupling_type
kids_real * omegas
save discrete frequencies (only for simple model, L=1)
kids_real * CL
save coupling coefficients with Qj (Qj has L no. of nonzero elements)
virtual void setInputParam_impl(std::shared_ptr< Param > &PM)
Virtual function to set input parameters for the kernel implementation.
kids_real * QL
save coulping matrix, each and L no. of nonzero elements
static uval parse(const std::string &str)
Definition phys.h:790
CONSTEXPR_DECOR value_type conv(const uval &u)
Definition phys.h:772
#define LOC()
show the location information for debug
Definition fmt.h:49
int count_exec
Counter for the number of executions performed by this kernel.
Definition Kernel.h:258
const double HSF5b_data[]
const double HSF5a_data[]
const double HSF3b_data[]
const double HSF3a_data[]
const double HFMO_data[]
const double HFCP_data[]
Provide linalg APIs.
#define FUNCTION_NAME
Definition macro_utils.h:9
VARIABLE< kids_real > p
VARIABLE< kids_real > x
VARIABLE< kids_real > p_sigma
VARIABLE< kids_real > x_sigma
VARIABLE< kids_real > omegas
VARIABLE< kids_real > coeffs
VARIABLE< kids_real > hess
VARIABLE< kids_real > vpes
VARIABLE< kids_real > V
VARIABLE< kids_real > dV
VARIABLE< kids_real > grad
VARIABLE< kids_real > mass
std::size_t PN
Product of P and N (P * N).
Definition vars_list.cpp:14
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 P
Number of parallel trajectories (swarms of trajectories) in each Monte Carlo run.
Definition vars_list.cpp:9
std::size_t NN
Product of N and N (N * N).
Definition vars_list.cpp:20
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
< http://warp.povusers.org/FunctionParser/fparser.html
Definition Context.h:39
double kids_real
Alias for real number type.
Definition Types.h:59
constexpr dimension7 energy_d
[M*L^2/T^2] force integrate length
Definition phys.h:251
static CONSTTYPE real_precision au_2_wn
Definition phys.h:998
static CONSTTYPE real_precision au_2_ev
Definition phys.h:994
constexpr uint32_t hash(const char *str)
Definition hash_fnv1a.h:12
declaration of variables used in the program.