KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
Kernel_Conserve.cpp
Go to the documentation of this file.
2
3#include "kids/Kernel_Elec.h"
4#include "kids/hash_fnv1a.h"
5#include "kids/macro_utils.h"
6#include "kids/vars_list.h"
7
8namespace PROJECT_NS {
9
10const std::string Kernel_Conserve::getName() { return "Kernel_Conserve"; }
11
13
14void Kernel_Conserve::setInputParam_impl(std::shared_ptr<Param>& PM) {
15 conserve_scale = PM->get_bool("conserve_scale", LOC(), false); // default as false
16}
17
18void Kernel_Conserve::setInputDataSet_impl(std::shared_ptr<DataSet>& DS) {
19 vpes = DS->def(DATA::model::vpes);
23 Etot_init = DS->def(DATA::init::Etot);
24 Etot_prev = DS->def(DATA::last::Etot);
25 E = DS->def(DATA::model::rep::E);
26 m = DS->def(DATA::integrator::m);
27 p = DS->def(DATA::integrator::p);
28 succ_ptr = DS->def(DATA::iter::succ);
30 frez_ptr = DS->def(DATA::iter::frez);
32}
33
35 bool conserve_scale_bak = conserve_scale;
36 conserve_scale = false;
37 executeKernel(stat); // calc Epot
38 conserve_scale = conserve_scale_bak;
39
40 for (int iP = 0; iP < Dimension::P; ++iP) {
41 Etot[iP] = Ekin[iP] + Epot[iP], Etot_init[iP] = Etot[iP];
42 Etot_prev[iP] = Etot_init[iP];
43 }
44 return stat;
45};
46
48 if (!succ_ptr[0] && fail_type_ptr[0] == 1) {
49 return stat; //
50 }
51 if (frez_ptr[0]) {
52 return stat; //
53 }
54
55 for (int iP = 0; iP < Dimension::P; ++iP) {
56 kids_real* E = this->E + iP * Dimension::F;
57 kids_real* p = this->p + iP * Dimension::N;
58 kids_real* m = this->m + iP * Dimension::N;
59 kids_real* Etot = this->Etot + iP;
60 kids_real* Etot_init = this->Etot_init + iP;
61 kids_real* Ekin = this->Ekin + iP;
62 kids_real* Epot = this->Epot + iP;
63 kids_real* vpes = this->vpes + iP;
64
65 Ekin[0] = 0.0e0;
66 for (int j = 0; j < Dimension::N; ++j) Ekin[0] += 0.5e0 * p[j] * p[j] / m[j];
67
68 double thres = 0.02;
69 if (last_attempt_ptr[0] && fail_type_ptr[0] != 2) { cnt_loose = 3; }
70 if (cnt_loose > 0) {
71 thres = cnt_loose * cnt_loose; // help for last_attempt of mndo failure
72 std::cout << "disable conserve around last try mndo\n";
73 cnt_loose--;
74 }
75
76 if (last_attempt_ptr[0] && fail_type_ptr[0] == 2) {
77 thres = 0.5; // loose threshold
78 std::cout << "last try conservation with thres = " << thres << "\n";
79 }
80
81 if (std::abs(Ekin[0] + Epot[0] - Etot_prev[0]) * phys::au_2_kcal_1mea > thres) {
82 std::cout << "fail in conserve ERROR: " //
83 << fabs(Ekin[0] + Epot[0] - Etot_prev[0]) * phys::au_2_kcal_1mea << " > " << thres << "\n";
84 succ_ptr[0] = false;
85 fail_type_ptr[0] = 2;
86 } else {
87 Etot_prev[0] = Ekin[0] + Epot[0];
88 succ_ptr[0] = true;
89 fail_type_ptr[0] = 0;
90 }
91
92 if (conserve_scale) {
93 double scale = std::sqrt(std::max({Etot_init[0] - Epot[0], 0.0e0}) / Ekin[0]);
94 for (int j = 0; j < Dimension::N; ++j) p[j] *= scale;
95 // Ekin[0] = Etot_init[0] - Epot[0];
96 }
97 }
98 return stat;
99}
100
101
102}; // namespace PROJECT_NS
this file provides Kernel_Conserve class enabling energy tracing and conservation.
initialization kernels for electonic DOFs
virtual void setInputParam_impl(std::shared_ptr< Param > &PM)
Virtual function to set input parameters for the kernel implementation.
virtual int getType() const
Get the type of the kernel.
virtual const std::string getName()
Get the name of the kernel.
kids_real * Etot
total energy
virtual void setInputDataSet_impl(std::shared_ptr< DataSet > &DS)
Virtual function to set input data set for the kernel implementation.
virtual Status & executeKernel_impl(Status &stat)
Virtual function to execute the kernel implementation.
kids_real * Ekin
kinematic energy
kids_real * p
nuclear momemtum
kids_real * E
adiabatic energies
kids_real * vpes
potential energy (only nuclear part)
kids_real * Epot
potential energy
kids_real * Etot_init
total energy at initial time
kids_real * Etot_prev
total energy in previous step
kids_real * m
nuclear mass
virtual Status & initializeKernel_impl(Status &stat)
Virtual function to initialize the kernel implementation.
Status & executeKernel(Status &stat)
Execute the kernel's algorithm and those of its children.
Definition Kernel.cpp:48
#define LOC()
show the location information for debug
Definition fmt.h:49
#define FUNCTION_NAME
Definition macro_utils.h:9
VARIABLE< kids_real > Etot
VARIABLE< kids_real > p
VARIABLE< kids_real > Epot
VARIABLE< kids_real > m
VARIABLE< kids_real > Etot
VARIABLE< kids_real > Ekin
VARIABLE< kids_bint > frez
VARIABLE< kids_bint > last_attempt
VARIABLE< kids_int > fail_type
VARIABLE< kids_bint > succ
VARIABLE< kids_real > Etot
VARIABLE< kids_real > E
VARIABLE< kids_real > vpes
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 F
Number of electronic degrees of freedom.
Definition vars_list.cpp:11
< http://warp.povusers.org/FunctionParser/fparser.html
Definition Context.h:39
double kids_real
Alias for real number type.
Definition Types.h:59
static CONSTTYPE real_precision au_2_kcal_1mea
Definition phys.h:996
constexpr uint32_t hash(const char *str)
Definition hash_fnv1a.h:12
declaration of variables used in the program.