Skip to content

Commit c364834

Browse files
committed
Add a function for a serial calculation rather than calling the intepreter inside
1 parent c732445 commit c364834

File tree

1 file changed

+111
-9
lines changed
  • src/libra_py/workflows/nbra

1 file changed

+111
-9
lines changed

src/libra_py/workflows/nbra/rpi.py

Lines changed: 111 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import os
2222
import sys
2323
import numpy as np
24+
import scipy.sparse as sp
2425
import h5py
2526
import time
2627
import multiprocessing as mp
@@ -32,6 +33,105 @@
3233
from libra_py import data_conv
3334
import libra_py.data_read as data_read
3435
import util.libutil as comn
36+
import libra_py.dynamics.tsh.compute as tsh_dynamics
37+
38+
def run_patch_dyn_serial(rpi_params, istep, fstep, istate):
39+
"""
40+
This function runs a single patch dynamics calculation for the time segment from istep to fstep and a given initial state.
41+
For conducting a coherent Ehrenfest propagation, the `libra_py.dynamics.tsh.compute` module is used.
42+
43+
Args:
44+
45+
rpi_params ( dictionary ): parameters controlling the execution of the RPI dynamics
46+
Can contain:
47+
48+
* **rpi_params["path_to_save_Hvibs"]** ( string ): The path of the vibronic Hamiltonian files.
49+
50+
* **rpi_params["basis_type"]** ( string ): The electronic basis type (such as the Slater determinant or CI adpatation, i.e., 'sd' or 'ci').
51+
52+
* **rpi_params["dt"]** ( double ): the time step in the atomic unit.
53+
54+
* **rpi_params["nstates"]** ( int ): The number of electronic states.
55+
56+
istep ( int ): the start index to read the vibronic Hamiltonian and time overlap files
57+
58+
fstep ( int ): the end index to read the vibronic Hamiltonian and time overlap files
59+
60+
istate ( int ): the initial electronic state for this patch dynamics
61+
62+
Return:
63+
None: but performs the action
64+
"""
65+
path_to_save_Hvibs, basis_type, dt, NSTATES = rpi_params["path_to_save_Hvibs"], rpi_params["basis_type"], rpi_params["dt"], rpi_params["nstates"]
66+
NSTEPS = fstep - istep # The patch duration
67+
68+
# Read the vibronic Hamiltonian and time overlap files in a selected basis
69+
E = []
70+
for step in range(istep, fstep):
71+
energy_filename = F"{path_to_save_Hvibs}/Hvib_{basis_type}_{step}_re.npz"
72+
energy_mat = sp.load_npz(energy_filename)
73+
E.append( np.array( np.diag( energy_mat.todense() ) ) )
74+
E = np.array(E)
75+
76+
St = []
77+
for step in range(istep, fstep):
78+
St_filename = F"{path_to_save_Hvibs}/St_{basis_type}_{step}_re.npz"
79+
St_mat = sp.load_npz(St_filename)
80+
St.append( np.array( St_mat.todense() ) )
81+
St = np.array(St)
82+
83+
NAC = []
84+
Hvib = []
85+
for c, step in enumerate(range(istep, fstep)):
86+
nac_filename = F"{path_to_save_Hvibs}/Hvib_{basis_type}_{step}_im.npz"
87+
nac_mat = sp.load_npz(nac_filename)
88+
NAC.append( np.array( nac_mat.todense() ) )
89+
Hvib.append( np.diag(E[c, :])*(1.0+1j*0.0) - (0.0+1j)*nac_mat[:, :] )
90+
91+
NAC = np.array(NAC)
92+
Hvib = np.array(Hvib)
93+
94+
# The interface function
95+
class tmp:
96+
pass
97+
98+
def compute_model(q, params, full_id):
99+
"""
100+
This function serves as an interface function for a serial patch dynamics calculation.
101+
"""
102+
103+
timestep = params["timestep"]
104+
nst = params["nstates"]
105+
obj = tmp()
106+
107+
obj.ham_adi = data_conv.nparray2CMATRIX( np.diag(E[timestep, : ]) )
108+
obj.nac_adi = data_conv.nparray2CMATRIX( NAC[timestep, :, :] )
109+
obj.hvib_adi = data_conv.nparray2CMATRIX( Hvib[timestep, :, :] )
110+
obj.basis_transform = CMATRIX(nst,nst); obj.basis_transform.identity() #basis_transform
111+
obj.time_overlap_adi = data_conv.nparray2CMATRIX( St[timestep, :, :] )
112+
113+
return obj
114+
115+
# Set the NBRA model
116+
model_params = {"timestep":0, "icond":0, "model0":0, "nstates":NSTATES}
117+
118+
# Setting the coherent Ehrenfest propagation. Define the argument-dependent part first.
119+
dyn_general = {"nsteps":NSTEPS, "nstates":NSTATES, "dt":dt, "nfiles": NSTEPS, "which_adi_states":range(NSTATES), "which_dia_states":range(NSTATES)}
120+
121+
dyn_general.update({"ntraj":1, "mem_output_level":2, "progress_frequency":1, "properties_to_save":["timestep", "time", "se_pop_adi"], "prefix":"out", "isNBRA":0,
122+
"ham_update_method":2, "ham_transform_method":0, "time_overlap_method":0, "nac_update_method":0,
123+
"hvib_update_method":0, "force_method":0, "rep_force":1, "hop_acceptance_algo":0, "momenta_rescaling_algo":0, "rep_tdse":1,
124+
"electronic_integrator":2, "tsh_method":-1, "decoherence_algo":-1, "decoherence_times_type":-1, "do_ssy":0, "dephasing_informed":0})
125+
126+
# Nuclear DOF - these parameters don't matter much in the NBRA calculations
127+
nucl_params = {"ndof":1, "init_type":3, "q":[-10.0], "p":[0.0], "mass":[2000.0], "force_constant":[0.0], "verbosity":-1 }
128+
129+
# Electronic DOF
130+
elec_params = {"ndia":NSTATES, "nadi":NSTATES, "verbosity":-1, "init_dm_type":0, "init_type":1, "rep":1,"istate":istate }
131+
132+
rnd = Random()
133+
134+
res = tsh_dynamics.generic_recipe(dyn_general, compute_model, model_params, elec_params, nucl_params, rnd)
35135

36136
def run_patch_rpi(rpi_params):
37137
"""
@@ -50,6 +150,8 @@ def run_patch_rpi(rpi_params):
50150
* **rpi_params["run_python_file"]** ( string ): The path of a template running script.
51151
52152
* **rpi_params["path_to_save_Hvibs"]** ( string ): The path of the vibronic Hamiltonian files.
153+
154+
* **rpi_params["basis_type"]** ( string ): The electronic basis type (such as the Slater determinant or CI adpatation, i.e., 'sd' or 'ci').
53155
54156
* **rpi_params["submission_exe"]** ( string ): The submission executable
55157
@@ -86,18 +188,17 @@ def run_patch_rpi(rpi_params):
86188
path_to_save_Hvibs, basis_type = rpi_params["path_to_save_Hvibs"], rpi_params["basis_type"]
87189
iread, fread = rpi_params["iread"], rpi_params["fread"]
88190
nsteps, dt = rpi_params["nsteps"], rpi_params["dt"]
89-
npatches, nstates = rpi_params["npatches"], rpi_params["nstates"]
191+
iconds, npatches, nstates = rpi_params["iconds"], rpi_params["npatches"], rpi_params["nstates"]
90192

91193
out_dir = rpi_params["path_to_save_patch"]
92194
if not os.path.exists(out_dir):
93195
os.mkdir(out_dir)
94196

95197
os.chdir(out_dir)
96198

97-
for ibatch, icond in enumerate(rpi_params["iconds"]):
98-
for ipatch in range(rpi_params["npatches"]):
99-
for istate in range(rpi_params["nstates"]):
100-
print(F"Submitting patch dynamics job of icond = {ibatch}, ipatch = {ipatch}, istate = {istate}")
199+
for ibatch, icond in enumerate(iconds):
200+
for ipatch in range(npatches):
201+
for istate in range(nstates):
101202

102203
# Compute the istep and fstep here, for each patch
103204
istep = iread + icond + ipatch * int( nsteps / npatches )
@@ -111,19 +212,20 @@ def run_patch_rpi(rpi_params):
111212

112213
# Submit jobs or run each patch dynamics through this loop
113214
if rpi_params["run_slurm"]:
215+
print(F"Submitting a patch dynamics job of icond = {ibatch}, ipatch = {ipatch}, istate = {istate}")
114216
os.system('cp ../../%s %s' % (rpi_params["run_python_file"], rpi_params["run_python_file"]))
115217
os.system('cp ../../%s %s' % (rpi_params["submit_template"], rpi_params["submit_template"]))
116218

117219
file = open(rpi_params["submit_template"], 'a')
118-
args_fmt = ' --path_to_save_Hvibs %s --basis_type %s --istep %d --fstep %d --istate %d --dt %f'
220+
args_fmt = ' --path_to_save_Hvibs %s --basis_type %s --istep %d --fstep %d --dt %f --istate %d'
119221
file.write('%s %s' % (rpi_params["python_exe"], rpi_params["run_python_file"]) + \
120-
args_fmt % (path_to_save_Hvibs, basis_type, istep, fstep, istate, dt) + " >log" )
222+
args_fmt % (path_to_save_Hvibs, basis_type, istep, fstep, dt, istate) + " >log" )
121223
file.close()
122224

123225
os.system('%s %s' % (rpi_params["submission_exe"], rpi_params["submit_template"]))
124226
else:
125-
# Just in case you want to use a bash file and not submitting
126-
os.system("python run.py > log")
227+
print(F"Running patch dynamics of icond = {ibatch}, ipatch = {ipatch}, istate = {istate}")
228+
run_patch_dyn_serial(rpi_params, istep, fstep, istate)
127229
os.chdir('../')
128230

129231
os.chdir('../')

0 commit comments

Comments
 (0)