Skip to content

Commit 28c0d35

Browse files
committed
Add support for different-charge model_devi systems in gromacs+gaussian and bug fix in training_resue_stop_batch keyword
1 parent 19bdd46 commit 28c0d35

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

dpgen/generator/lib/gaussian.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,13 @@ def make_gaussian_input(sys_data, fp_params):
114114
keywords = [keywords]
115115
else:
116116
keywords = keywords.copy()
117+
117118
# assume default charge is zero and default spin multiplicity is 1
118-
charge = fp_params.get('charge', 0)
119+
if 'charge' in sys_data.keys():
120+
charge = sys_data['charge']
121+
else:
122+
charge = fp_params.get('charge', 0)
123+
119124
use_fragment_guesses = False
120125
multiplicity = fp_params.get('multiplicity', 'auto')
121126
if type(multiplicity) == int:

dpgen/generator/run.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,12 @@ def dump_to_poscar(dump, poscar, type_map, fmt = "lammps/dump") :
186186
sys = dpdata.System(dump, fmt = fmt, type_map = type_map)
187187
sys.to_vasp_poscar(poscar)
188188

189-
def dump_to_deepmd_raw(dump, deepmd_raw, type_map, fmt='gromacs/gro'):
189+
def dump_to_deepmd_raw(dump, deepmd_raw, type_map, fmt='gromacs/gro', charge=None):
190190
system = dpdata.System(dump, fmt = fmt, type_map = type_map)
191191
system.to_deepmd_raw(deepmd_raw)
192+
if charge is not None:
193+
with open(os.path.join(deepmd_raw, "charge"), 'w+') as f:
194+
f.write(str(charge))
192195

193196

194197
def make_train (iter_index,
@@ -208,7 +211,14 @@ def make_train (iter_index,
208211
training_init_model = jdata.get('training_init_model', False)
209212
training_reuse_iter = jdata.get('training_reuse_iter')
210213
training_reuse_old_ratio = jdata.get('training_reuse_old_ratio', None)
211-
training_reuse_stop_batch = jdata.get('training_reuse_stop_batch', 400000)
214+
215+
if 'training_reuse_stop_batch' in jdata.keys():
216+
training_reuse_stop_batch = jdata['training_reuse_stop_batch']
217+
elif 'training_reuse_numb_steps' in jdata.keys():
218+
training_reuse_stop_batch = jdata['training_reuse_numb_steps']
219+
else:
220+
training_reuse_stop_batch = 40000
221+
212222
training_reuse_start_lr = jdata.get('training_reuse_start_lr', 1e-4)
213223
training_reuse_start_pref_e = jdata.get('training_reuse_start_pref_e', 0.1)
214224
training_reuse_start_pref_f = jdata.get('training_reuse_start_pref_f', 100)
@@ -349,10 +359,12 @@ def make_train (iter_index,
349359
# set training reuse model
350360
if training_reuse_iter is not None and iter_index >= training_reuse_iter:
351361
if LooseVersion('1') <= LooseVersion(mdata["deepmd_version"]) < LooseVersion('2'):
362+
jinput['training']['stop_batch'] = training_reuse_stop_batch
352363
jinput['training']['auto_prob_style'] \
353364
="prob_sys_size; 0:%d:%f; %d:%d:%f" \
354365
%(old_range, training_reuse_old_ratio, old_range, len(init_data_sys), 1.-training_reuse_old_ratio)
355366
elif LooseVersion('2') <= LooseVersion(mdata["deepmd_version"]) < LooseVersion('3'):
367+
jinput['training']['numb_steps'] = training_reuse_stop_batch
356368
jinput['training']['training_data']['auto_prob'] \
357369
="prob_sys_size; 0:%d:%f; %d:%d:%f" \
358370
%(old_range, training_reuse_old_ratio, old_range, len(init_data_sys), 1.-training_reuse_old_ratio)
@@ -363,7 +375,7 @@ def make_train (iter_index,
363375
if jinput['loss'].get('start_pref_f') is not None:
364376
jinput['loss']['start_pref_f'] = training_reuse_start_pref_f
365377
jinput['learning_rate']['start_lr'] = training_reuse_start_lr
366-
jinput['training']['stop_batch'] = training_reuse_stop_batch
378+
367379

368380
for ii in range(numb_models) :
369381
task_path = os.path.join(work_path, train_task_fmt % ii)
@@ -1128,7 +1140,7 @@ def _make_model_devi_native_gromacs(iter_index, jdata, mdata, conf_systems):
11281140
create_path(task_path)
11291141
gromacs_settings = jdata.get("gromacs_settings" , "")
11301142
for key,file in gromacs_settings.items():
1131-
if key != "traj_filename" and key != "mdp_filename":
1143+
if key != "traj_filename" and key != "mdp_filename" and key != "group_name":
11321144
os.symlink(os.path.join(cc,file), os.path.join(task_path, file))
11331145
# input.json for DP-Gromacs
11341146
with open(os.path.join(cc, "input.json")) as f:
@@ -1374,6 +1386,10 @@ def _make_fp_vasp_inner (modd_path,
13741386
system_index.sort()
13751387

13761388
fp_tasks = []
1389+
1390+
charges_recorder = [] # record charges for each fp_task
1391+
charges_map = jdata.get("sys_charges", [])
1392+
13771393
cluster_cutoff = jdata['cluster_cutoff'] if jdata.get('use_clusters', False) else None
13781394
# skip save *.out if detailed_report_make_fp is False, default is True
13791395
detailed_report_make_fp = jdata.get("detailed_report_make_fp", True)
@@ -1487,11 +1503,11 @@ def _make_fp_vasp_inner (modd_path,
14871503
continue
14881504

14891505
if fp_cluster_vacuum is not None:
1490-
assert fp_cluster_vacuum >0
1491-
skip_cluster = check_cluster(conf_name, fp_cluster_vacuum)
1492-
if skip_cluster:
1493-
count_bad_cluster +=1
1494-
continue
1506+
assert fp_cluster_vacuum >0
1507+
skip_cluster = check_cluster(conf_name, fp_cluster_vacuum)
1508+
if skip_cluster:
1509+
count_bad_cluster +=1
1510+
continue
14951511

14961512
# link job.json
14971513
job_name = os.path.join(tt, "job.json")
@@ -1507,6 +1523,8 @@ def _make_fp_vasp_inner (modd_path,
15071523
fp_task_path = os.path.join(work_path, fp_task_name)
15081524
create_path(fp_task_path)
15091525
fp_tasks.append(fp_task_path)
1526+
if charges_map:
1527+
charges_recorder.append(charges_map[int(ss)])
15101528
cwd = os.getcwd()
15111529
os.chdir(fp_task_path)
15121530
if cluster_cutoff is None:
@@ -1524,13 +1542,16 @@ def _make_fp_vasp_inner (modd_path,
15241542
dlog.info("system {0:s} skipped {1:6d} confs with bad cluster, {2:6d} remains".format(ss, count_bad_cluster, numb_task - count_bad_cluster))
15251543
if cluster_cutoff is None:
15261544
cwd = os.getcwd()
1527-
for ii in fp_tasks:
1528-
os.chdir(ii)
1545+
for idx, task in enumerate(fp_tasks):
1546+
os.chdir(task)
15291547
if model_devi_engine == "lammps":
15301548
dump_to_poscar('conf.dump', 'POSCAR', type_map, fmt = "lammps/dump")
15311549
elif model_devi_engine == "gromacs":
15321550
# dump_to_poscar('conf.dump', 'POSCAR', type_map, fmt = "gromacs/gro")
1533-
dump_to_deepmd_raw('conf.dump', 'deepmd.raw', type_map, fmt = 'gromacs/gro')
1551+
if charges_map:
1552+
dump_to_deepmd_raw('conf.dump', 'deepmd.raw', type_map, fmt='gromacs/gro', charge=charges_recorder[idx])
1553+
else:
1554+
dump_to_deepmd_raw('conf.dump', 'deepmd.raw', type_map, fmt='gromacs/gro', charge=None)
15341555
else:
15351556
raise RuntimeError("unknown model_devi engine", model_devi_engine)
15361557
os.chdir(cwd)
@@ -1956,6 +1977,8 @@ def make_fp_gaussian(iter_index,
19561977
sys_data = dpdata.System('POSCAR').data
19571978
elif model_devi_engine == "gromacs":
19581979
sys_data = dpdata.System("deepmd.raw", fmt='deepmd/raw').data
1980+
if os.path.isfile('deepmd.raw/charge'):
1981+
sys_data['charge'] = int(np.loadtxt('deepmd.raw/charge', dtype=int))
19591982
ret = make_gaussian_input(sys_data, fp_params)
19601983
with open('input', 'w') as fp:
19611984
fp.write(ret)

0 commit comments

Comments
 (0)