Skip to content

Commit 57cc434

Browse files
committed
Cleaned up a bit some of the F-tracking procedures, made them more efficient, fixed some exports, added some experimental versions of the F-tracking, but not what's the current manuscript uses - something to consider in the future
1 parent 3d7a3ea commit 57cc434

4 files changed

Lines changed: 128 additions & 26 deletions

File tree

src/dyn/dyn_ham.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ void update_Hamiltonian_variables(dyn_control_params& prms, dyn_variables& dyn_v
7171
*/
7272

7373
int nadi = ham.nadi;
74+
int ndof = dyn_var.ndof;
7475
int ntraj = ham.children.size();
7576

7677
MATRIX& q = *dyn_var.q;
@@ -147,6 +148,44 @@ void update_Hamiltonian_variables(dyn_control_params& prms, dyn_variables& dyn_v
147148

148149
if(update_type==0 || update_type==1){
149150

151+
152+
// Derivative NAC correction option:
153+
if(prms.do_nac_phase_correction==2){ // Experimental option to fix the phase of NACVs
154+
155+
vector<int> traj_id(1,0);
156+
for(int traj=0; traj<ntraj; traj++){
157+
traj_id[0] = traj;
158+
CMATRIX Eadi(ham.children[traj]->get_ham_adi());
159+
MATRIX e_curr(ham.children[traj]->get_ham_adi().real());
160+
MATRIX e_prev(ham_prev.children[traj]->get_ham_adi().real());
161+
CMATRIX f_curr = ham.children[traj]->all_forces_adi(traj_id);
162+
CMATRIX f_prev = ham_prev.children[traj]->all_forces_adi(traj_id);
163+
//ham->get_hvib_adi().show_matrix();
164+
MATRIX pp(dyn_var.ndof, 1);
165+
double dt = prms.dt;
166+
pp = p.col(traj);
167+
int act_state = dyn_var.act_states[traj];
168+
169+
//vector<MATRIX> T_new(compute_F_cost_matrix_dof_resolved(f_curr, f_prev, e_curr, e_prev, pp, iM, dt, act_state));
170+
MATRIX T_new(compute_F_cost_matrix(f_curr, f_prev, e_curr, e_prev, pp, iM, dt, act_state).real() );
171+
172+
for(int k=0; k<ndof;k++){
173+
for(int i=0; i<nadi;i++){
174+
for(int j=i+1; j<nadi; j++){
175+
//double sgn = T_new[k].get(i,j);
176+
double sgn = T_new.get(i,j);
177+
if(sgn > 0.5){ sgn = -1; } // change sign
178+
else{ sgn = 1.0; }
179+
180+
ham.children[traj]->dc1_adi[k]->scale(i,j, sgn);
181+
ham.children[traj]->dc1_adi[k]->scale(j,i, sgn);
182+
}// for j
183+
}// for i
184+
185+
}// for k
186+
}// for traj
187+
}// if correction
188+
150189
//========================== Couplings ===============================
151190
// Don't update NACs - they may have been read in step 1
152191
if(prms.nac_update_method==0){ ;; }

src/dyn/dyn_projectors.cpp

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,60 +1115,111 @@ CMATRIX compute_F_cost_matrix(CMATRIX& F_curr, CMATRIX& F_prev, MATRIX& e_curr,
11151115

11161116
MATRIX vel(ndof, 1); vel.dot_product(iM, p);
11171117
MATRIX tmp(ndof, 1);
1118-
tmp = fi_curr.T(); ai.dot_product(iM, tmp);
1119-
tmp = fj_curr.T(); aj.dot_product(iM, tmp);
11201118

11211119
double val = 0.0;
11221120
for(i=0; i<nst; i++){
1123-
double sum = 1.0;
11241121

1125-
for(j=0;j<nst; j++){
1122+
fi_curr = F_curr.row(i).real();
1123+
fi_prev = F_prev.row(i).real();
1124+
tmp = fi_curr.T(); ai.dot_product(iM, tmp);
1125+
dq_i = (vel * dt + 0.5 * ai * dt * dt);
1126+
double dE_i = -(fi_prev * dq_i).get(0,0);
11261127

1127-
fi_curr = F_curr.row(i).real();
1128-
fi_prev = F_prev.row(i).real();
1128+
for(j=0;j<nst; j++){
11291129

11301130
fj_curr = F_curr.row(j).real();
11311131
fj_prev = F_prev.row(j).real();
1132-
1133-
dq_i = (vel * dt + 0.5 * ai * dt * dt);
1134-
double dE = e_prev.get(j,j) - e_prev.get(i,i);
1135-
double dE_i = -(fi_prev * dq_i).get(0,0);
1136-
1132+
tmp = fj_curr.T(); aj.dot_product(iM, tmp);
11371133
dq_j = (vel * dt + 0.5 * aj * dt * dt);
11381134
double dE_j = -(fj_prev * dq_j).get(0,0);
1135+
1136+
double dE = e_prev.get(j,j) - e_prev.get(i,i);
11391137
double dE_new = dE + dE_j - dE_i;
11401138

11411139
val = 0.5*(1.0 - SIGN( dE ) * SIGN( dE_new )) ;
1142-
/*
1143-
if(i==j){ val = 0.0; }
1144-
//else{ val = 1.0 - SIGN( dE ) * SIGN( dE_new ); sum -= val; }
1145-
else{
1146-
double val1;
1147-
val = SIGN( dE ) * SIGN( dE_new );
1148-
if(val<0.0){ val1 = 1.0; }
1149-
else{ val1 = 0.0; }
1150-
val = val1;
1151-
//sum -= val;
1152-
}
1153-
*/
11541140

11551141
res.set(i, j, complex<double>( val, 0.0) );
11561142
}// for j
1157-
1158-
//if(sum<=0.0){ sum = 0.0; }
1159-
//res.set(i, i, complex<double>(sum, 0.0) );
11601143
}// for i
11611144

11621145
return res;
11631146
}
11641147

11651148

1149+
1150+
vector<MATRIX> compute_F_cost_matrix_dof_resolved(CMATRIX& F_curr, CMATRIX& F_prev, MATRIX& e_curr, MATRIX& e_prev, MATRIX& _p, MATRIX& iM, double dt, int act_state){
1151+
/** F_curr, F_prev - CMATRIX(nadi, ndof)
1152+
p - MATRIX(ndof, 1) - current momentum
1153+
iM - MATRIX(ndof, 1) - inverse matrices
1154+
dt - time step
1155+
act_state - the current active state
1156+
*/
1157+
int i,j;
1158+
int ndof = F_curr.n_cols;
1159+
int nst = F_curr.n_rows;
1160+
vector<MATRIX> res(ndof, MATRIX(nst, nst) );
1161+
//MATRIX fi_curr(1, ndof);
1162+
//MATRIX fj_curr(1, ndof);
1163+
//MATRIX fi_prev(1, ndof);
1164+
//MATRIX fj_prev(1, ndof);
1165+
//MATRIX ai(ndof, 1);
1166+
//MATRIX aj(ndof, 1);
1167+
//MATRIX dq_i(ndof,1);
1168+
//MATRIX dq_j(ndof,1);
1169+
MATRIX p(_p);
1170+
1171+
// correction, because the current p is already a half-step propagated using old forces
1172+
//p -= dt * F_prev.row(act_state).real().T();
1173+
1174+
MATRIX vel(ndof, 1); vel.dot_product(iM, p);
1175+
MATRIX tmp(ndof, 1);
1176+
1177+
double val = 0.0;
1178+
for(int idof=0; idof<ndof; idof++){
1179+
1180+
double vel = p.get(idof, 0) * iM.get(idof, 0);
1181+
1182+
for(i=0; i<nst; i++){
1183+
1184+
double fi_curr = F_curr.get(i,idof).real();
1185+
double fi_prev = F_prev.get(i,idof).real();
1186+
double ai = fi_curr * iM.get(idof,0);
1187+
double dq_i = vel * dt + 0.5 * ai * dt * dt;
1188+
double dE_i = -fi_prev * dq_i;
1189+
1190+
for(j=0;j<nst; j++){
1191+
1192+
double fj_curr = F_curr.get(j,idof).real();
1193+
double fj_prev = F_prev.get(j,idof).real();
1194+
double aj = fj_curr * iM.get(idof,0);
1195+
double dq_j = vel * dt + 0.5 * ai * dt * dt;
1196+
double dE_j = -fj_prev * dq_j;
1197+
1198+
double dE = e_prev.get(j,j) - e_prev.get(i,i);
1199+
double dE_new = dE + dE_j - dE_i;
1200+
1201+
val = SIGN( dE ) * SIGN( dE_new ) ;
1202+
res[idof].set(i, j, val);
1203+
1204+
}// for j
1205+
}// for i
1206+
}// for idof
1207+
1208+
return res;
1209+
}
1210+
1211+
1212+
1213+
11661214
CMATRIX compute_F_cost_matrix2(CMATRIX& F_curr, CMATRIX& F_prev, MATRIX& e_curr, MATRIX& e_prev, MATRIX& _p, MATRIX& iM, double dt, int act_state){
11671215
/** F_curr, F_prev - CMATRIX(nadi, ndof)
11681216
p - MATRIX(ndof, 1) - current momentum
11691217
iM - MATRIX(ndof, 1) - inverse matrices
11701218
dt - time step
11711219
act_state - the current active state
1220+
1221+
This is an experimental function - merely a placeholder - not used anywhere yet!
1222+
11721223
*/
11731224
int i,j;
11741225
int ndof = F_curr.n_cols;

src/dyn/dyn_projectors.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ CMATRIX raw_to_dynconsyst(CMATRIX& amplitudes, vector<CMATRIX>& projectors);
6464
CMATRIX dynconsyst_to_raw(CMATRIX& amplitudes, vector<CMATRIX>& projectors);
6565

6666
CMATRIX compute_F_cost_matrix(CMATRIX& F_curr, CMATRIX& F_prev, MATRIX& e_curr, MATRIX& e_prev, MATRIX& p, MATRIX& iM, double dt, int act_state);
67+
vector<MATRIX> compute_F_cost_matrix_dof_resolved(CMATRIX& F_curr, CMATRIX& F_prev, MATRIX& e_curr, MATRIX& e_prev,
68+
MATRIX& _p, MATRIX& iM, double dt, int act_state);
69+
6770

6871
}// namespace libdyn
6972
}// liblibra

src/dyn/libdyn.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,15 @@ void export_dyn_projectors_objects(){
802802
def("get_stochastic_reordering3", expt_get_stochastic_reordering3_v2);
803803

804804

805+
CMATRIX (*expt_compute_F_cost_matrix_v1)
806+
(CMATRIX& F_curr, CMATRIX& F_prev, MATRIX& e_curr, MATRIX& e_prev,
807+
MATRIX& p, MATRIX& iM, double dt, int act_state) = &compute_F_cost_matrix;
808+
def("compute_F_cost_matrix", expt_compute_F_cost_matrix_v1);
809+
810+
vector<MATRIX> (*expt_compute_F_cost_matrix_dof_resolved_v1)
811+
(CMATRIX& F_curr, CMATRIX& F_prev, MATRIX& e_curr, MATRIX& e_prev,
812+
MATRIX& _p, MATRIX& iM, double dt, int act_state) = &compute_F_cost_matrix_dof_resolved;
813+
def("compute_F_cost_matrix_dof_resolved", expt_compute_F_cost_matrix_dof_resolved_v1);
805814

806815
}
807816

0 commit comments

Comments
 (0)